cammy/bin/dither.c

80 lines
1.7 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <cammy/image.h>
#include <cammy/photo.h>
#include "pnglite.h"
#include "png.h"
#include "commands.h"
static cammy_tile_palette palette = {
.colors = {
{ 0, 0, 0 },
{ 85, 85, 85 },
{ 171, 171, 171 },
{ 255, 255, 255 }
}
};
int cammy_command_dither(int argc, char **argv) {
uint8_t *bufin, *bufout;
size_t width, height;
int depth;
if (argc < 3) {
cammy_command_usage(argc, argv, "No PNG input file provided");
} else if (argc < 4) {
cammy_command_usage(argc, argv, "No PNG output filename provided");
}
if ((bufin = cammy_png_load(argv[2], &width,
&height,
&depth)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "cammy_png_load()", strerror(errno));
goto error_png_load;
}
if ((bufout = malloc(width * height * 3)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_bufout;
}
cammy_image_dither(bufout, bufin, width, height, depth, &palette);
if (cammy_png_save(argv[3], bufout,
width,
height, 8, PNG_TRUECOLOR) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_png_save()", argv[3], strerror(errno));
goto error_png_save;
}
free(bufout);
free(bufin);
return 0;
error_png_save:
free(bufout);
error_malloc_bufout:
free(bufin);
error_png_load:
return 1;
}