Skip to content

Commit

Permalink
Use the old libpng API so it will work on pre-1.6 installations.
Browse files Browse the repository at this point in the history
The simplified API is so much nicer, but 1.2 is still extremely common.
  • Loading branch information
e-n-f committed Oct 8, 2013
1 parent 981bea7 commit 546a7e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
all: encode render enumerate merge

PNG_CFLAGS=$(shell pkg-config libpng16 --cflags)
PNG_LDFLAGS=$(shell pkg-config libpng16 --libs)
PNG_CFLAGS=
PNG_LDFLAGS=-lpng

ENCODE_OBJS = encode.o util.o
RENDER_OBJS = render.o util.o graphics.o
ENUMERATE_OBJS = enumerate.o util.o
MERGE_OBJS = merge.o util.o

encode: $(ENCODE_OBJS)
$(CC) -g -Wall -O3 -o $@ $^ -lm $(PNG_LDFLAGS)
$(CC) -g -Wall -O3 -o $@ $^ -lm

render: $(RENDER_OBJS)
$(CC) -g -Wall -O3 -o $@ $^ -lm -lz $(PNG_LDFLAGS)

enumerate: $(ENUMERATE_OBJS)
$(CC) -g -Wall -O3 -o $@ $^ -lm $(PNG_LDFLAGS)
$(CC) -g -Wall -O3 -o $@ $^ -lm

merge: $(MERGE_OBJS)
$(CC) -g -Wall -O3 -o $@ $^ -lm $(PNG_LDFLAGS)
$(CC) -g -Wall -O3 -o $@ $^ -lm

.c.o:
$(CC) -g -Wall -O3 $(PNG_CFLAGS) -c $<
Expand Down
38 changes: 27 additions & 11 deletions graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "util.h"
#include "graphics.h"

#if PNG_LIBPNG_VER < 10600
#error libpng >= 1.6 is required
#endif
static void fail(png_structp png_ptr, png_const_charp error_msg) {
fprintf(stderr, "PNG error %s\n", error_msg);
exit(EXIT_FAILURE);
}

void out(double *src, double *cx, double *cy, int width, int height, int transparency, double gamma, int invert, int color, int color2, int saturate, int mask) {
unsigned char *buf = malloc(width * height * 4);
Expand Down Expand Up @@ -131,16 +132,31 @@ void out(double *src, double *cx, double *cy, int width, int height, int transpa
}
}

png_image image;
unsigned char *rows[height];
for (i = 0 ; i < height; i++) {
rows[i] = buf + i * (4 * width);
}

png_structp png_ptr;
png_infop info_ptr;

memset(&image, 0, sizeof image);
image.version = PNG_IMAGE_VERSION;
image.format = PNG_FORMAT_RGBA;
image.width = width;
image.height = height;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, fail, fail, fail);
if (png_ptr == NULL) {
fprintf(stderr, "PNG failure (write struct)\n");
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fprintf(stderr, "PNG failure (info struct)\n");
exit(EXIT_FAILURE);
}

png_image_write_to_stdio(&image, stdout, 0, buf, 4 * width, NULL);
png_image_free(&image);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_rows(png_ptr, info_ptr, rows);
png_init_io(png_ptr, stdout);
png_write_png(png_ptr, info_ptr, 0, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);

free(buf);
}
Expand Down

0 comments on commit 546a7e0

Please sign in to comment.