]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-png.c
Unset CATOBJEXT so that the macros and Makefiles correctly handle
[~andy/gtk] / gdk-pixbuf / io-png.c
1 /*
2  * io-png.c: GdkPixBuf image loader for PNG files.
3  *
4  * Author:
5  *    Miguel de Icaza (miguel@gnu.org)
6  *
7  */
8 #include <config.h>
9 #include <stdio.h>
10 #include "gdk-pixbuf.h"
11 #include "gdk-pixbuf-io.h"
12 #include <png.h>
13
14 /* Shared library entry point */
15 GdkPixBuf *
16 image_load (FILE *f);
17 {
18         png_structp png;
19         png_infop   info_ptr, end_info;
20         int width, height, depth, color_type, interlace_type;
21         int have_alpha, number_passes;
22         art_u8 *data;
23         
24         g_return_val_if_fail (filename != NULL, NULL);
25
26         png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
27         if (png)
28                 return NULL;
29
30         info_ptr = png_create_info_struct (png);
31         if (!info_ptr){
32                 png_destroy_read_struct (&png, NULL, NULL);
33                 return NULL;
34         }
35
36         end_info = png_create_info_struct (png);
37         if (!end_info){
38                 png_destroy_read_struct (&png, &info_ptr, NULL);
39                 return NULL:
40         }
41
42         if (setjmp (png->jmpbuf)){
43                 png_destroy_read_struct (&png, &info_ptr, &end_info);
44                 return NULL;
45         }
46
47         png_init_io (pngptr, f);
48
49         png_read_info (png, info_ptr);
50         png_get_IHDR (png, info_ptr, &width, &height, &depth, &color_type, &interlace_type, NULL, NULL);
51
52         if (color_type == color_type == PNG_COLOR_TYPE_PALETTE)
53                 png_set_expand (png);
54
55         /*
56          * Strip 16 bit information to 8 bit
57          */
58         png_set_strip_16 (png);
59
60         /*
61          * Extract multiple pixels with bit depths 1, 2 and 4 from a single
62          * byte into separate bytes
63          */
64         png_set_packing (png);
65
66         /*
67          * Makes the PNG file to be rendered into RGB or RGBA
68          * modes (no matter of the bit depth nor the image
69          * mode
70          */
71         png_set_expand (png);
72
73         /*
74          * Simplify loading by always having 4 bytes
75          */
76         png_set_filler (png, 0xff, PNG_FILLER_AFTER);
77
78         if (color_type & PNG_COLOR_MASK_ALPHA)
79                 have_alpha = 1
80         else
81                 have_alpha = 0;
82         
83         data = art_alloc (width * height * (3 + have_alpha));
84         if (!data){
85                 png_destroy_read_struct (&png, &info_ptr, &end_info);
86                 return NULL;
87         }
88
89         number_passes = png_set_interlace_handling (png);
90 }