1+ #include < stdlib.h>
2+ #include " Image.h"
13#include < png.h>
4+ #include " _PNG.h"
5+ #include < MT2D/MessageBox/MT2D_MessageBox.h>
6+ #include < MT2D/MT2D_Display.h>
7+ #include < MT2D/MT2D.h>
28
39/*
410* A simple libpng example program
1723* of the X11 license.
1824*
1925*/
20- /*
21- void read_png_file(char *filename) {
26+
27+ extern char str_buffer[200 ];
28+
29+
30+ Image *PNG_read_file (char *filename) {
31+ Image *img = Image_CreateBlank ();
32+ PNG_Info *img_struct = (PNG_Info*)malloc (sizeof (PNG_Info));
2233 FILE *fp = fopen (filename, " rb" );
2334
24- png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
25- if (!png) abort();
35+ img_struct->png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL , NULL , NULL );
36+ if (!img_struct->png ) {
37+ sprintf (str_buffer, " PNG ERROR:Unable to read image:" , filename);
38+ MT2D_MessageBox (str_buffer);
39+ fclose (fp);
40+ return 0 ;
41+ }
2642
27- png_infop info = png_create_info_struct(png);
28- if (!info) abort();
43+ img_struct->info = png_create_info_struct (img_struct->png );
44+ if (!img_struct->info ) {
45+ sprintf (str_buffer, " PNG ERROR:Unable to create the info of from this image: " , filename);
46+ MT2D_MessageBox (str_buffer);
47+ fclose (fp);
48+ return 0 ;
49+ }
2950
30- if (setjmp(png_jmpbuf(png))) abort();
51+ if (setjmp (png_jmpbuf (img_struct->png ))) {
52+ sprintf (str_buffer, " PNG ERROR:I really dont know whats this error is but if the original author said it's an error, so is it. : " , filename);
53+ MT2D_MessageBox (str_buffer);
54+ fclose (fp);
55+ return 0 ;
56+ }
3157
32- png_init_io(png, fp);
58+ png_init_io (img_struct-> png , fp);
3359
34- png_read_info(png, info);
60+ png_read_info (img_struct-> png , img_struct-> info );
3561
36- width = png_get_image_width(png, info);
37- height = png_get_image_height(png, info);
38- color_type = png_get_color_type(png, info);
39- bit_depth = png_get_bit_depth(png, info);
62+ img-> Width = png_get_image_width (img_struct-> png , img_struct-> info );
63+ img-> Height = png_get_image_height (img_struct-> png , img_struct-> info );
64+ png_byte color_type = png_get_color_type (img_struct-> png , img_struct-> info );
65+ png_byte bit_depth = png_get_bit_depth (img_struct-> png , img_struct-> info );
4066
4167 // Read any color_type into 8bit depth, RGBA format.
4268 // See http://www.libpng.org/pub/png/libpng-manual.txt
4369
4470 if (bit_depth == 16 )
45- png_set_strip_16(png);
71+ png_set_strip_16 (img_struct-> png );
4672
4773 if (color_type == PNG_COLOR_TYPE_PALETTE)
48- png_set_palette_to_rgb(png);
74+ png_set_palette_to_rgb (img_struct-> png );
4975
5076 // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
5177 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 )
52- png_set_expand_gray_1_2_4_to_8(png);
78+ png_set_expand_gray_1_2_4_to_8 (img_struct-> png );
5379
54- if (png_get_valid(png, info, PNG_INFO_tRNS))
55- png_set_tRNS_to_alpha(png);
80+ if (png_get_valid (img_struct-> png , img_struct-> info , PNG_INFO_tRNS))
81+ png_set_tRNS_to_alpha (img_struct-> png );
5682
5783 // These color_type don't have an alpha channel then fill it with 0xff.
5884 if (color_type == PNG_COLOR_TYPE_RGB ||
5985 color_type == PNG_COLOR_TYPE_GRAY ||
6086 color_type == PNG_COLOR_TYPE_PALETTE)
61- png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
87+ png_set_filler (img_struct-> png , 0xFF , PNG_FILLER_AFTER);
6288
6389 if (color_type == PNG_COLOR_TYPE_GRAY ||
6490 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
65- png_set_gray_to_rgb(png);
91+ png_set_gray_to_rgb (img_struct-> png );
6692
67- png_read_update_info(png, info);
93+ png_read_update_info (img_struct-> png , img_struct-> info );
6894
69- row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height );
70- for (int y = 0; y < height ; y++) {
71- row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
95+ png_bytep * row_pointers = (png_bytep*)malloc (sizeof (png_bytep) * img-> Height );
96+ for (int y = 0 ; y < img-> Height ; y++) {
97+ row_pointers[y] = (png_byte*)malloc (png_get_rowbytes (img_struct-> png , img_struct-> info ));
7298 }
7399
74- png_read_image(png, row_pointers);
100+ png_read_image (img_struct->png , row_pointers);
101+ img_struct->row_pointers = row_pointers;
102+ img->ImagePointer = img_struct;
75103
76104 fclose (fp);
105+ return img;
77106}
78107
79- */
108+ Pixel *PNG_Get_Pixel (Image *img, int X, int Y) {
109+ PNG_Info *Pi = (PNG_Info*)img->ImagePointer ;
110+ Pixel *P = (Pixel*)malloc (sizeof (Pixel));
111+ if (png_get_color_type (Pi->png , Pi->info ) == PNG_COLOR_TYPE_RGB);
112+ // abort_("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA "
113+ // "(lacks the alpha channel)");
114+
115+ if (png_get_color_type (Pi->png , Pi->info ) != PNG_COLOR_TYPE_RGBA);
116+ // abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (%d) (is %d)",
117+ // PNG_COLOR_TYPE_RGBA, png_get_color_type(img->png, img->info));
118+
119+ png_byte* row = Pi->row_pointers [Y];
120+ png_byte* ptr = &(row[X * 4 ]);
121+ // printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n",
122+ // x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
123+ P->red = ptr[0 ];
124+ P->green = ptr[1 ];
125+ P->blue = ptr[2 ];
126+ return P;
127+ }
0 commit comments