summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan-Peter Nilsson <peppe@pappkartong.se>2011-02-05 13:11:39 +0100
committerJan-Peter Nilsson <peppe@pappkartong.se>2011-02-05 13:11:39 +0100
commit9db690139fa7296daf0bda734a01d9d86b0705ee (patch)
tree64aacc5beaaebdf57811a937d731e7cf1ca0f7d4
parentd4c209bb212ed9aa231dc48fb0bfc3553a94ebb6 (diff)
Write image using libpng
-rw-r--r--test.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/test.c b/test.c
index 21e294e..7b54cb6 100644
--- a/test.c
+++ b/test.c
@@ -3,6 +3,7 @@
#include <assert.h>
#include <stdlib.h>
#include <gd.h>
+#include <png.h>
typedef struct
@@ -93,7 +94,42 @@ int main()
gdImageDestroy(im2);
gdImageDestroy(im);
- free(bitmap);
+ //Using libpng
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_bytep row;
+
+ fp = fopen("lpng.png", "wb");
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ info_ptr = png_create_info_struct(png_ptr);
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ fprintf(stderr, "Error during png creation\n");
+ }
+
+ png_init_io(png_ptr, fp);
+ png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+ png_write_info(png_ptr, info_ptr);
+
+ row = (png_bytep) malloc(4 * width * sizeof(png_byte));
+
+ //Write image data
+ for (int y=0 ; y<height ; y++) {
+ for (int x=0 ; x<width ; x++) {
+ int* data = (int*)&(row[x*4]);
+ *data = bitmap[x+y*width];
+ }
+ png_write_row(png_ptr, row);
+ }
+
+ // End write
+ png_write_end(png_ptr, NULL);
+ if(fp != NULL) fclose(fp);
+ if(info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
+ if(png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
+ if(row != NULL) free(row);
+
+ free(bitmap);
return 0;
}