diff --git a/bin/main.c b/bin/main.c index e07f6ff..ccbe586 100644 --- a/bin/main.c +++ b/bin/main.c @@ -41,7 +41,8 @@ static void usage(int argc, char **argv, const char *message, ...) { " %1$s merge-rgb file.sav rn gn bn output.png\n" " %1$s import-tile file.sav 1..30 printer.tile\n" " %1$s export-tile file.sav 1..30 output.tile\n" - " %1$s convert-tile printer.tile output.png\n", + " %1$s convert-tile printer.tile output.png\n" + " %1$s stripe input.png\n", argv[0]); exit(1); @@ -753,6 +754,34 @@ error_open: return errno; } +static int stripe(int argc, char **argv) { + uint8_t *buf; + size_t width, height; + int depth; + + if (argc < 3) { + usage(argc, argv, "No input PNG image provided"); + } + + if ((buf = read_png_file(argv[2], &width, &height, &depth)) == NULL) { + goto error_read_png_file; + } + + if (cammy_image_stripe(buf, width, height, depth) < 0) { + goto error_image_stripe; + } + + free(buf); + + return 0; + +error_image_stripe: + free(buf); + +error_read_png_file: + return 1; +} + static struct { char *name; int (*fun)(int, char **); @@ -765,6 +794,7 @@ static struct { { "import-tile", import_tile }, { "export-tile", export_tile }, { "convert-tile", convert_tile }, + { "stripe", stripe }, { NULL, NULL } }; diff --git a/include/cammy/image.h b/include/cammy/image.h index 860f04f..4bb94aa 100644 --- a/include/cammy/image.h +++ b/include/cammy/image.h @@ -56,8 +56,16 @@ void cammy_image_dither(uint8_t *dest, void cammy_image_dither_to_tile(cammy_tile *dest, uint8_t *src, + size_t x_dest, + size_t y_dest, + size_t x_src, + size_t y_src, size_t width, size_t height, + size_t dest_width, + size_t dest_height, + size_t src_width, + size_t src_height, int depth); void cammy_image_split_to_tiles(cammy_tile *destr, @@ -92,4 +100,6 @@ void cammy_image_copy(cammy_image *dest, size_t width, size_t height); +int cammy_image_stripe(uint8_t *buf, size_t width, size_t height, int depth); + #endif /* _CAMMY_IMAGE_H */ diff --git a/src/image.c b/src/image.c index fd181a4..c5a6e3e 100644 --- a/src/image.c +++ b/src/image.c @@ -1,10 +1,13 @@ +#include #include #include #include +#include #include #include #include +#include #include static uint32_t bayer_matrix[256] = { @@ -350,20 +353,39 @@ void cammy_image_copy_from_tile(uint8_t *dest, void cammy_image_dither_to_tile(cammy_tile *dest, uint8_t *src, + size_t x_dest, + size_t y_dest, + size_t x_src, + size_t y_src, size_t width, size_t height, + size_t dest_width, + size_t dest_height, + size_t src_width, + size_t src_height, int depth) { - size_t x, y; + size_t x_offset, y_offset; - memset(dest, '\x00', CAMMY_TILE_SIZE * (width >> 3) * (height >> 3)); + printf("dest dimensions %zux%zu\n", dest_width, dest_height); - for (y=0; y dest_height) { + break; + } + + for (x_offset=0; x_offset dest_width) { + break; + } - tile_write(dest, x, y, width, rgb_to_tile_2bpp(r, g, b, x, y) ^ 3); + buf_read(src, x_src + x_offset, y_src + y_offset, src_width, + &r, &g, &b, depth); + + tile_write(dest, x_dest + x_offset, y_dest + y_offset, + dest_width, rgb_to_tile_2bpp(r, g, b, x_dest + x_offset, + y_dest + y_offset) ^ 3); } } } @@ -426,3 +448,83 @@ void cammy_image_copy(cammy_image *dest, } } } + +static int save_tile_to_file(cammy_tile *tiles, size_t width, + size_t height, + int file) { + static const char *format = "slice%04d.tile"; + char name[32]; + int fd; + struct stat st; + + if (snprintf(name, sizeof(name), format, file) < 0) { + goto error_snprintf_name; + } + + if (stat(name, &st) == 0) { + errno = EEXIST; + + goto error_exists; + } + + if ((fd = open(name, O_CREAT | O_WRONLY, 0644)) < 0) { + goto error_open; + } + + if (write(fd, tiles, (width * height) >> 2) < 0) { + goto error_write; + } + + close(fd); + + return 0; + +error_write: + close(fd); + +error_open: +error_exists: +error_snprintf_name: + return -1; +} + +int cammy_image_stripe(uint8_t *buf, size_t width, size_t height, int depth) { + cammy_tile *tiles; + int file = 0; + + size_t x, y; + + if ((tiles = malloc(CAMMY_SCREEN_SIZE)) == NULL) { + goto error_malloc_tiles; + } + + for (x=0; xtiles, src, + 0, 0, 0, 0, + CAMMY_PHOTO_WIDTH, + CAMMY_PHOTO_HEIGHT, + CAMMY_PHOTO_WIDTH, + CAMMY_PHOTO_HEIGHT, CAMMY_PHOTO_WIDTH, CAMMY_PHOTO_HEIGHT, depth);