cammy/bin/dither.c
2016-07-06 17:12:47 -05:00

94 lines
2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.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 }
}
};
static void usage(int argc, char **argv, const char *message, ...) {
if (message) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
fprintf(stderr, "\n");
}
fprintf(stderr, "usage: %1$s dither input.png output.png\n", argv[0]);
exit(1);
}
int cammy_dither(int argc, char **argv) {
uint8_t *bufin, *bufout;
size_t width, height;
int depth;
if (argc < 3) {
usage(argc, argv, "No PNG input file provided");
} else if (argc < 4) {
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;
}