This commit is contained in:
XANTRONIX Development 2016-06-07 00:07:58 -05:00
parent 726e3999f2
commit 109a3a9efb
3 changed files with 163 additions and 14 deletions

View file

@ -33,12 +33,14 @@ static void usage(int argc, char **argv, const char *message, ...) {
fprintf(stderr, "\n");
}
fprintf(stderr, "usage: %1$s import file.sav photo1..30 input.png\n"
" %1$s export file.sav photo1..30 output.png\n"
" %1$s dither input.png output.png\n"
" %1$s split file.sav rn gn bn input.png\n"
" %1$s merge file.sav rn gn bn output.png\n"
" %1$s convert printer.tile output.png\n",
fprintf(stderr, "usage: %1$s import file.sav 1..30 input.png\n"
" %1$s export file.sav 1..30 output.png\n"
" %1$s dither input.png output.png\n"
" %1$s split file.sav rn gn bn input.png\n"
" %1$s merge 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.png\n"
" %1$s convert-tile printer.tile output.png\n",
argv[0]);
exit(1);
@ -520,7 +522,41 @@ error_sram_open:
return 1;
}
static int convert(int argc, char **argv) {
static int import_tile(int argc, char **argv) {
cammy_sram *sram;
int fd;
if (argc < 2) {
usage(argc, argv, "No camera SRAM file provided");
} else if (argc < 3) {
usage(argc, argv, "No picture number provided");
} else if (argc < 4) {
usage(argc, argv, "No Game Boy screen tile data file provided");
}
if ((sram = cammy_sram_open(argv[2])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_sram_open()", argv[2], strerror(errno));
goto error_sram_open;
}
if ((fd = open(argv[4], O_RDONLY)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "open()", argv[2], strerror(errno));
goto error_open;
}
return 0;
error_sram_open:
cammy_sram_close(sram);
return 1;
}
static int convert_tile(int argc, char **argv) {
int fd;
void *in, *out;
@ -591,13 +627,14 @@ static struct {
char *name;
int (*fun)(int, char **);
} commands[] = {
{ "import", import },
{ "export", export },
{ "dither", dither },
{ "split", split },
{ "merge", merge },
{ "convert", convert },
{ NULL, NULL }
{ "import", import },
{ "export", export },
{ "dither", dither },
{ "split", split },
{ "merge", merge },
{ "import-tile", import_tile },
{ "convert-tile", convert_tile },
{ NULL, NULL }
};
int main(int argc, char **argv) {

View file

@ -9,6 +9,35 @@
#define CAMMY_IMAGE_Y_COEFFICIENT_G 0.7152
#define CAMMY_IMAGE_Y_COEFFICIENT_B 0.0722
#define CAMMY_IMAGE_CHUNK_SIZE 4096
typedef enum {
CAMMY_IMAGE_NONE,
CAMMY_IMAGE_TILE,
CAMMY_IMAGE_RGB,
CAMMY_IMAGE_RGBA
} cammy_image_format;
typedef struct _cammy_image {
cammy_image_format format;
size_t width,
height;
union {
uint8_t *buf;
cammy_tile *tile;
};
} cammy_image;
cammy_image *cammy_image_open_tile(const char *filename,
size_t width,
size_t height);
void cammy_image_close(cammy_image *image);
int cammy_image_save(cammy_image *image, const char *filename);
void cammy_image_dither(uint8_t *dest,
uint8_t *src,
size_t width,
@ -45,4 +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);
#endif /* _CAMMY_IMAGE_H */

View file

@ -1,6 +1,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <cammy/image.h>
@ -136,6 +138,63 @@ static inline void buf_write(uint8_t *buf,
buf[depth*stride*y + depth*x + 2] = b;
}
cammy_image *cammy_image_open_tile(const char *filename,
size_t width,
size_t height) {
cammy_image *image;
int fd;
size_t size = (width * height) >> 2,
off;
if ((fd = open(filename, O_RDONLY)) < 0) {
goto error_open;
}
if ((image = malloc(sizeof(*image))) == NULL) {
goto error_malloc_image;
}
if ((image->tile = malloc(size)) == NULL) {
goto error_malloc_buf;
}
for (off=0; off<size;) {
ssize_t len;
if ((len = read(fd, &image->buf[off], CAMMY_IMAGE_CHUNK_SIZE)) < 0) {
goto error_read;
}
off += len;
}
close(fd);
image->format = CAMMY_IMAGE_TILE;
image->width = width;
image->height = height;
return image;
error_read:
free(image->buf);
error_malloc_buf:
free(image);
error_malloc_image:
close(fd);
error_open:
return NULL;
}
void cammy_image_close(cammy_image *image) {
free(image->buf);
free(image);
}
void cammy_image_dither(uint8_t *dest,
uint8_t *src,
size_t width,
@ -243,3 +302,18 @@ void cammy_image_merge_tiles(uint8_t *dest,
}
}
}
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) {
size_t y, y_max = y_src + height;
for (y=y_src; y<y_max; y++) {
size_t x, x_max = x_src + width;
}
}