From 51347b348b99d5734006f7f1a4be67ae0bc0b277 Mon Sep 17 00:00:00 2001 From: Kushal K S V S Date: Tue, 11 Jul 2017 23:45:50 +0530 Subject: [PATCH] Read PNG function --- tests/make_png/bitmap.c | 66 ++++++++++++++++++++++++++++++++++++++++- tests/make_png/bitmap.h | 3 +- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/tests/make_png/bitmap.c b/tests/make_png/bitmap.c index 104acdc1c..deeff15c3 100644 --- a/tests/make_png/bitmap.c +++ b/tests/make_png/bitmap.c @@ -279,4 +279,68 @@ void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){ Generate_PNG (& fruit, file_name, render_mode); free (fruit.pixels); -} \ No newline at end of file +} + +void Read_PNG(char *filename, IMAGE * after_effect) { + + int width, height; + png_bytep *row_pointers; + + FILE *fp = fopen(filename, "rb"); + + png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if(!png) abort(); + + png_infop info = png_create_info_struct(png); + if(!info) abort(); + + if(setjmp(png_jmpbuf(png))) abort(); + + png_init_io(png, fp); + + png_set_user_limits(png, 0x7fffffffL, 0x7fffffffL); + + png_read_info(png, info); + + width = png_get_image_width(png, info); + height = png_get_image_height(png, info); + + after_effect->width = width; + after_effect->height = height; + + printf("%d %d\n",width,height ); + + row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height); + for(int y = 0; y < height; y++) { + row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info)); + } + + png_read_image(png, row_pointers); + + after_effect->pixels = (PIXEL*)malloc(width * height * sizeof(PIXEL)); + + for(int y = 0; y < height; y++) { + + png_bytep row = row_pointers[y]; + + for(int x = 0; x < width; x++ ) { + + png_bytep px = &(row[x * 4]); + + PIXEL * pixel = Pixel_At ( after_effect, x, y); + + pixel->red = px[0]; + pixel->green = px[1]; + pixel->blue = px[2]; + pixel->alpha = px[3]; + + printf("%d %d %d %d\n", pixel->red, + pixel->green, + pixel->blue, + pixel->alpha ); + } + } + + fclose(fp); +} + diff --git a/tests/make_png/bitmap.h b/tests/make_png/bitmap.h index 426d62218..bed203db9 100644 --- a/tests/make_png/bitmap.h +++ b/tests/make_png/bitmap.h @@ -64,4 +64,5 @@ PIXEL * Pixel_At (IMAGE * bitmap, int x, int y); // Returns a pointer to pixel void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode); // Image to file int Generate_PNG (IMAGE *bitmap, const char *path,int render_mode); - +// Read PNG +void Read_PNG(char *filename, IMAGE * after_effect);