I pass the savings on to you!
This commit is contained in:
parent
d35675ce56
commit
31867af95c
1 changed files with 60 additions and 42 deletions
102
bin/main.c
102
bin/main.c
|
@ -85,6 +85,54 @@ error_malloc_png:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint8_t *read_png_file(const char *file,
|
||||||
|
size_t *width,
|
||||||
|
size_t *height,
|
||||||
|
int *depth) {
|
||||||
|
png_t *png;
|
||||||
|
uint8_t *buf;
|
||||||
|
|
||||||
|
png_init(malloc, free);
|
||||||
|
|
||||||
|
if ((png = malloc(sizeof(*png))) == NULL) {
|
||||||
|
goto error_malloc_png;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (png_open_file_read(png, file) < 0) {
|
||||||
|
goto error_png_open_file_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
*width = png->width;
|
||||||
|
*height = png->height;
|
||||||
|
*depth = png->bpp;
|
||||||
|
|
||||||
|
if ((buf = malloc(*width * *height * *depth)) == NULL) {
|
||||||
|
goto error_malloc_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (png_get_data(png, buf) < 0) {
|
||||||
|
goto error_png_get_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_close_file(png);
|
||||||
|
|
||||||
|
free(png);
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
error_png_get_data:
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
error_malloc_buf:
|
||||||
|
png_close_file(png);
|
||||||
|
|
||||||
|
error_png_open_file_read:
|
||||||
|
free(png);
|
||||||
|
|
||||||
|
error_malloc_png:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static int import(int argc, char **argv) {
|
static int import(int argc, char **argv) {
|
||||||
cammy_sram *sram;
|
cammy_sram *sram;
|
||||||
png_t *png;
|
png_t *png;
|
||||||
|
@ -239,10 +287,9 @@ error_sram_open:
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dither(int argc, char **argv) {
|
static int dither(int argc, char **argv) {
|
||||||
png_t *in;
|
|
||||||
uint8_t *bufin, *bufout;
|
uint8_t *bufin, *bufout;
|
||||||
|
size_t width, height;
|
||||||
int error;
|
int depth;
|
||||||
|
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
usage(argc, argv, "No PNG input file provided");
|
usage(argc, argv, "No PNG input file provided");
|
||||||
|
@ -250,75 +297,46 @@ static int dither(int argc, char **argv) {
|
||||||
usage(argc, argv, "No PNG output filename provided");
|
usage(argc, argv, "No PNG output filename provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
png_init(malloc, free);
|
if ((bufin = read_png_file(argv[2], &width,
|
||||||
|
&height,
|
||||||
if ((in = malloc(sizeof(*in))) == NULL) {
|
&depth)) == NULL) {
|
||||||
goto error_malloc_in;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((error = png_open_file_read(in, argv[2])) < 0) {
|
|
||||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
||||||
argv[0], "png_open_file_read()", argv[2], png_error_string(error));
|
|
||||||
|
|
||||||
goto error_png_open_file_read;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((bufin = malloc(in->width * in->height * in->bpp)) == NULL) {
|
|
||||||
fprintf(stderr, "%s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
argv[0], "malloc()", strerror(errno));
|
argv[0], "read_png_file()", strerror(errno));
|
||||||
|
|
||||||
goto error_malloc_bufin;
|
goto error_read_png_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((bufout = malloc(in->width * in->height * 3)) == NULL) {
|
if ((bufout = malloc(width * height * 3)) == NULL) {
|
||||||
fprintf(stderr, "%s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
argv[0], "malloc()", strerror(errno));
|
argv[0], "malloc()", strerror(errno));
|
||||||
|
|
||||||
goto error_malloc_bufout;
|
goto error_malloc_bufout;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((error = png_get_data(in, bufin)) < 0) {
|
cammy_image_dither(bufout, bufin, width, height, depth, &palette);
|
||||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
||||||
argv[0], "png_get_data()", argv[2], png_error_string(error));
|
|
||||||
|
|
||||||
goto error_png_get_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
cammy_image_dither(bufout, bufin, in->width, in->height, in->bpp, &palette);
|
|
||||||
|
|
||||||
if (save_buf_to_png_file(argv[3], bufout,
|
if (save_buf_to_png_file(argv[3], bufout,
|
||||||
in->width,
|
width,
|
||||||
in->height, 8, PNG_TRUECOLOR) < 0) {
|
height, 8, PNG_TRUECOLOR) < 0) {
|
||||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
||||||
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
|
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
|
||||||
|
|
||||||
goto error_save_buf_to_png_file;
|
goto error_save_buf_to_png_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_close_file(in);
|
|
||||||
|
|
||||||
free(bufout);
|
free(bufout);
|
||||||
|
|
||||||
free(bufin);
|
free(bufin);
|
||||||
|
|
||||||
free(in);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_save_buf_to_png_file:
|
error_save_buf_to_png_file:
|
||||||
error_png_get_data:
|
|
||||||
free(bufout);
|
free(bufout);
|
||||||
|
|
||||||
error_malloc_bufout:
|
error_malloc_bufout:
|
||||||
free(bufin);
|
free(bufin);
|
||||||
|
|
||||||
error_malloc_bufin:
|
error_read_png_file:
|
||||||
png_close_file(in);
|
|
||||||
|
|
||||||
error_png_open_file_read:
|
|
||||||
free(in);
|
|
||||||
|
|
||||||
error_malloc_in:
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue