diff options
Diffstat (limited to 'src/image.c')
-rw-r--r-- | src/image.c | 74 |
1 files changed, 63 insertions, 11 deletions
diff --git a/src/image.c b/src/image.c index 618a854..71fb52b 100644 --- a/src/image.c +++ b/src/image.c @@ -1,8 +1,61 @@ #include <stdlib.h> +#include <string.h> +#include <assert.h> #include <png.h> #include "image.h" +void rotate_rgba_counterclockwise(uint8_t** bufp, size_t size, size_t width) +{ + uint32_t* buf = (uint32_t*)*bufp; + uint32_t* out = malloc(size); + + size_t pixel_count = (size/4); + size_t height = pixel_count/width; + + for (uint32_t h = 0; h < height; ++h) + { + for (uint32_t w = 0; w < width; ++w) + { + size_t buf_index = (w + (h * width)); + size_t out_index = (height * (width-1)) - (height * w) + h; + + assert(pixel_count > buf_index && pixel_count > out_index); + + out[out_index] = buf[buf_index]; + } + } + + free(*bufp); + *bufp = (uint8_t*)out; +} + + +void rotate_rgba_clockwise(uint8_t** bufp, size_t size, size_t width) +{ + uint32_t* buf = (uint32_t*)*bufp; + uint32_t* out = malloc(size); + + size_t pixel_count = (size/4); + size_t height = pixel_count/width; + + for (uint32_t h = 0; h < height; ++h) + { + for (uint32_t w = 0; w < width; ++w) + { + size_t buf_index = (w + (h * width)); + size_t out_index = ((height * (w + 1)) - 1) - h; + + assert(pixel_count > buf_index && pixel_count > out_index); + + out[out_index] = buf[buf_index]; + } + } + + free(*bufp); + *bufp = (uint8_t*)out; +} + size_t bin_to_rgba(uint8_t** bufp, size_t size) { size_t out_size = (size / 3) * 4; @@ -17,6 +70,9 @@ size_t bin_to_rgba(uint8_t** bufp, size_t size) } free(*bufp); *bufp = buf; + + rotate_rgba_counterclockwise(bufp, out_size, BIN_DEFAULT_WIDTH); + return out_size; } @@ -26,6 +82,9 @@ size_t rgba_to_bin(uint8_t** bufp, size_t size) size_t out_size = (size / 4) * 3; uint8_t* buf = malloc(out_size); + size_t height = (out_size/3)/BIN_DEFAULT_WIDTH; + rotate_rgba_clockwise(bufp, size, height); + for (size_t i = 0, j = 0; i < size; i+=4, j+=3) { (buf+j)[0] = (*bufp+i)[2]; @@ -34,6 +93,7 @@ size_t rgba_to_bin(uint8_t** bufp, size_t size) } free(*bufp); *bufp = buf; + return out_size; } @@ -56,8 +116,6 @@ size_t png_to_rgba(uint8_t** bufp, size_t size) uint32_t w = png_get_image_width(png, info); uint32_t h = png_get_image_height(png, info); - printf("%i, %i\n", w, h); - png_byte color_type = png_get_color_type(png, info); if(color_type == PNG_COLOR_TYPE_PALETTE) @@ -87,8 +145,6 @@ size_t png_to_rgba(uint8_t** bufp, size_t size) return out_size; } -#define DEFAULT_WIDTH 240 - size_t rgba_to_png(uint8_t** bufp, size_t size) { uint32_t* buf = (uint32_t*)*bufp; @@ -100,12 +156,8 @@ size_t rgba_to_png(uint8_t** bufp, size_t size) FILE* fp = fmemopen(out, size, "wb"); png_bytep* row_pointers = NULL; - uint32_t w = 240; - uint32_t h = (uint32_t)((size/4)/w); - - printf("%i, %i\n", w, h); - - printf("%li\n", size); + uint32_t h = BIN_DEFAULT_WIDTH; + uint32_t w = (uint32_t)((size/4)/h); /* initialize stuff */ png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); @@ -135,7 +187,7 @@ size_t rgba_to_png(uint8_t** bufp, size_t size) while (!out[out_size/4]) --out_size; free(buf); - *bufp = out; + *bufp = (uint8_t*)out; return out_size; }
\ No newline at end of file |