diff --git a/bin/main.c b/bin/main.c index 7a7bd2c..fcae7c9 100644 --- a/bin/main.c +++ b/bin/main.c @@ -9,6 +9,7 @@ #include #include "pnglite.h" +#include #include #include #include @@ -524,7 +525,8 @@ error_sram_open: static int import_tile(int argc, char **argv) { cammy_sram *sram; - int fd; + cammy_image *dest, *src; + int photo; if (argc < 2) { usage(argc, argv, "No camera SRAM file provided"); @@ -534,6 +536,12 @@ static int import_tile(int argc, char **argv) { usage(argc, argv, "No Game Boy screen tile data file provided"); } + photo = atoi(argv[3]); + + if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) { + usage(argc, argv, "Invalid photo number"); + } + if ((sram = cammy_sram_open(argv[2])) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "cammy_sram_open()", argv[2], strerror(errno)); @@ -541,18 +549,41 @@ static int import_tile(int argc, char **argv) { goto error_sram_open; } - if ((fd = open(argv[4], O_RDONLY)) < 0) { + if ((src = cammy_image_open_tile(argv[4], CAMMY_SCREEN_WIDTH, + CAMMY_SCREEN_HEIGHT)) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "open()", argv[2], strerror(errno)); + argv[0], "cammy_image_open_tile()", argv[4], strerror(errno)); - goto error_open; + goto error_image_open_tile; } + if ((dest = malloc(sizeof(*dest))) == NULL) { + goto error_malloc_dest; + } + + dest->format = CAMMY_IMAGE_TILE; + dest->tiles = (cammy_tile *)&sram->data->photos[photo-1].tiles; + dest->width = CAMMY_PHOTO_WIDTH; + dest->height = CAMMY_PHOTO_HEIGHT; + + cammy_image_copy(dest, src, + 0, 0, 16, 16, CAMMY_PHOTO_WIDTH, CAMMY_PHOTO_HEIGHT); + + free(dest); + + cammy_image_close(src); + + cammy_sram_close(sram); + return 0; -error_sram_open: +error_malloc_dest: + cammy_image_close(src); + +error_image_open_tile: cammy_sram_close(sram); +error_sram_open: return 1; } diff --git a/include/cammy/image.h b/include/cammy/image.h index 18136e3..b5a5ae8 100644 --- a/include/cammy/image.h +++ b/include/cammy/image.h @@ -74,13 +74,13 @@ void cammy_image_merge_tiles(uint8_t *dest, size_t height, int depth); -void cammy_image_tile_copy(cammy_tile *dest, - cammy_tile *src, - size_t x_dest, - size_t y_dest, - size_t x_src, - size_t y_src, - size_t width, - size_t height); +void cammy_image_copy(cammy_image *dest, + cammy_image *src, + size_t x_dest, + size_t y_dest, + size_t x_src, + size_t y_src, + size_t width, + size_t height); #endif /* _CAMMY_IMAGE_H */ diff --git a/src/image.c b/src/image.c index a5c0d09..c30f114 100644 --- a/src/image.c +++ b/src/image.c @@ -102,8 +102,7 @@ static inline uint8_t tile_read(cammy_tile *tiles, return ((tile->data[ tile_y<<1] & (0x80 >> tile_x)) >> (tile_x ^ 7) - | ((tile->data[(tile_y<<1)|1] & (0x80 >> tile_x)) >> (tile_x ^ 7) << 1)) - ^ 3; + | ((tile->data[(tile_y<<1)|1] & (0x80 >> tile_x)) >> (tile_x ^ 7) << 1)); } static inline void tile_write(cammy_tile *tiles, @@ -116,6 +115,9 @@ static inline void tile_write(cammy_tile *tiles, int tile_x = x & 7, tile_y = y & 7; + tile->data[ tile_y<<1] &= ~(1 << (tile_x ^ 7)); + tile->data[(tile_y<<1)|1] &= ~(1 << (tile_x ^ 7)); + tile->data[ tile_y<<1] |= (value & 0x01) << (tile_x ^ 7); tile->data[(tile_y<<1)|1] |= ((value & 0x02) >> 1) << (tile_x ^ 7); } @@ -232,7 +234,7 @@ void cammy_image_split_to_tiles(cammy_tile *destr, for (y=0; ycolors[value][0], palette->colors[value][1], @@ -292,9 +294,9 @@ void cammy_image_merge_tiles(uint8_t *dest, for (y=0; yformat != CAMMY_IMAGE_TILE || src->format != CAMMY_IMAGE_TILE) { + return; + } + for (y_offset=0; y_offset dest->height) { break;