]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - image_png.c
cbaddf3e7398173d27e669a595c6b22eb3fb599e
[xonotic/darkplaces.git] / image_png.c
1 /*
2         Copyright (C) 2006  Serge "(515)" Ziryukin, Forest "LordHavoc" Hale
3
4         This program is free software; you can redistribute it and/or
5         modify it under the terms of the GNU General Public License
6         as published by the Free Software Foundation; either version 2
7         of the License, or (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to:
17
18                 Free Software Foundation, Inc.
19                 59 Temple Place - Suite 330
20                 Boston, MA  02111-1307, USA
21
22 */
23
24 //[515]: png implemented into DP ONLY FOR TESTING 2d stuff with csqc
25 // so delete this bullshit :D
26 //
27 //LordHavoc: rewrote most of this.
28
29 #include "quakedef.h"
30 #include "image.h"
31 #include "image_png.h"
32
33 static void                             (*qpng_set_sig_bytes)           (void*, int);
34 static int                              (*qpng_sig_cmp)                         (const unsigned char*, size_t, size_t);
35 static void*                    (*qpng_create_read_struct)      (const char*, void*, void*, void*);
36 static void*                    (*qpng_create_info_struct)      (void*);
37 static void                             (*qpng_read_info)                       (void*, void*);
38 static void                             (*qpng_set_expand)                      (void*);
39 static void                             (*qpng_set_gray_1_2_4_to_8)     (void*);
40 static void                             (*qpng_set_palette_to_rgb)      (void*);
41 static void                             (*qpng_set_tRNS_to_alpha)       (void*);
42 static void                             (*qpng_set_gray_to_rgb)         (void*);
43 static void                             (*qpng_set_filler)                      (void*, unsigned int, int);
44 static void                             (*qpng_read_update_info)        (void*, void*);
45 static void                             (*qpng_read_image)                      (void*, unsigned char**);
46 static void                             (*qpng_read_end)                        (void*, void*);
47 static void                             (*qpng_destroy_read_struct)     (void**, void**, void**);
48 static void                             (*qpng_set_read_fn)                     (void*, void*, void*);
49 static unsigned int             (*qpng_get_valid)                       (void*, void*, unsigned int);
50 static unsigned int             (*qpng_get_rowbytes)            (void*, void*);
51 static unsigned char    (*qpng_get_channels)            (void*, void*);
52 static unsigned char    (*qpng_get_bit_depth)           (void*, void*);
53 static unsigned int             (*qpng_get_IHDR)                        (void*, void*, unsigned int*, unsigned int*, int *, int *, int *, int *, int *);
54 static char*                    (*qpng_get_libpng_ver)          (void*);
55
56 static dllfunction_t pngfuncs[] =
57 {
58         {"png_set_sig_bytes",           (void **) &qpng_set_sig_bytes},
59         {"png_sig_cmp",                         (void **) &qpng_sig_cmp},
60         {"png_create_read_struct",      (void **) &qpng_create_read_struct},
61         {"png_create_info_struct",      (void **) &qpng_create_info_struct},
62         {"png_read_info",                       (void **) &qpng_read_info},
63         {"png_set_expand",                      (void **) &qpng_set_expand},
64         {"png_set_gray_1_2_4_to_8",     (void **) &qpng_set_gray_1_2_4_to_8},
65         {"png_set_palette_to_rgb",      (void **) &qpng_set_palette_to_rgb},
66         {"png_set_tRNS_to_alpha",       (void **) &qpng_set_tRNS_to_alpha},
67         {"png_set_gray_to_rgb",         (void **) &qpng_set_gray_to_rgb},
68         {"png_set_filler",                      (void **) &qpng_set_filler},
69         {"png_read_update_info",        (void **) &qpng_read_update_info},
70         {"png_read_image",                      (void **) &qpng_read_image},
71         {"png_read_end",                        (void **) &qpng_read_end},
72         {"png_destroy_read_struct",     (void **) &qpng_destroy_read_struct},
73         {"png_set_read_fn",                     (void **) &qpng_set_read_fn},
74         {"png_get_valid",                       (void **) &qpng_get_valid},
75         {"png_get_rowbytes",            (void **) &qpng_get_rowbytes},
76         {"png_get_channels",            (void **) &qpng_get_channels},
77         {"png_get_bit_depth",           (void **) &qpng_get_bit_depth},
78         {"png_get_IHDR",                        (void **) &qpng_get_IHDR},
79         {"png_get_libpng_ver",          (void **) &qpng_get_libpng_ver},
80         {NULL, NULL}
81 };
82
83 // Handle for PNG DLL
84 dllhandle_t png_dll = NULL;
85
86
87 /*
88 =================================================================
89
90   DLL load & unload
91
92 =================================================================
93 */
94
95 /*
96 ====================
97 PNG_OpenLibrary
98
99 Try to load the PNG DLL
100 ====================
101 */
102 qboolean PNG_OpenLibrary (void)
103 {
104         const char* dllnames [] =
105         {
106 #ifdef WIN32
107                 "libpng12.dll",
108 #elif defined(MACOSX)
109                 "libpng12.dylib",
110 #else
111                 "libpng12.so.0",
112 #endif
113                 NULL
114         };
115
116         // Already loaded?
117         if (png_dll)
118                 return true;
119
120         // Load the DLL
121         if (! Sys_LoadLibrary (dllnames, &png_dll, pngfuncs))
122         {
123                 Con_Printf ("PNG support disabled\n");
124                 return false;
125         }
126
127         Con_Printf ("PNG support enabled\n");
128         return true;
129 }
130
131
132 /*
133 ====================
134 PNG_CloseLibrary
135
136 Unload the PNG DLL
137 ====================
138 */
139 void PNG_CloseLibrary (void)
140 {
141         Sys_UnloadLibrary (&png_dll);
142 }
143
144
145 /*
146 =================================================================
147
148         PNG decompression
149
150 =================================================================
151 */
152
153 #define PNG_LIBPNG_VER_STRING "1.2.4"
154
155 #define PNG_COLOR_MASK_PALETTE    1
156 #define PNG_COLOR_MASK_COLOR      2
157 #define PNG_COLOR_MASK_ALPHA      4
158
159 #define PNG_COLOR_TYPE_GRAY 0
160 #define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
161 #define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
162 #define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
163 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
164
165 #define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
166 #define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
167
168 #define PNG_INFO_tRNS 0x0010
169
170 // this struct is only used for status information during loading
171 struct
172 {
173         const unsigned char     *tmpBuf;
174         int             tmpBuflength;
175         int             tmpi;
176         //int           FBgColor;
177         //int           FTransparent;
178         unsigned int    FRowBytes;
179         //double        FGamma;
180         //double        FScreenGamma;
181         unsigned char   **FRowPtrs;
182         unsigned char   *Data;
183         //char  *Title;
184         //char  *Author;
185         //char  *Description;
186         int             BitDepth;
187         int             BytesPerPixel;
188         int             ColorType;
189         unsigned int    Height;
190         unsigned int    Width;
191         int             Interlace;
192         int             Compression;
193         int             Filter;
194         //double        LastModified;
195         //int           Transparent;
196 } my_png;
197
198 //LordHavoc: removed __cdecl prefix, added overrun protection, and rewrote this to be more efficient
199 void PNG_fReadData(void *png, unsigned char *data, size_t length)
200 {
201         size_t l;
202         l = my_png.tmpBuflength - my_png.tmpi;
203         if (l < length)
204         {
205                 Con_Printf("PNG_fReadData: overrun by %i bytes\n", length - l);
206                 // a read going past the end of the file, fill in the remaining bytes
207                 // with 0 just to be consistent
208                 memset(data + l, 0, length - l);
209                 length = l;
210         }
211         memcpy(data, my_png.tmpBuf + my_png.tmpi, length);
212         my_png.tmpi += length;
213         Com_HexDumpToConsole(data, length);
214 }
215
216 void PNG_error_fn(void *png, const char *message)
217 {
218         Con_Printf("PNG_LoadImage: error: %s\n", message);
219 }
220
221 void PNG_warning_fn(void *png, const char *message)
222 {
223         Con_Printf("PNG_LoadImage: warning: %s\n", message);
224 }
225
226 extern int      image_width;
227 extern int      image_height;
228
229 unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchwidth, int matchheight)
230 {
231         unsigned int    y;
232         void *png, *pnginfo;
233         unsigned char *imagedata;
234         unsigned char ioBuffer[8192];
235
236         // FIXME: register an error handler so that abort() won't be called on error
237
238         // No DLL = no PNGs
239         if (!png_dll)
240                 return NULL;
241
242         if(qpng_sig_cmp(raw, 0, filesize))
243                 return NULL;
244         png = qpng_create_read_struct(PNG_LIBPNG_VER_STRING, 0, PNG_error_fn, PNG_warning_fn);
245         if(!png)
246                 return NULL;
247
248         // NOTE: this relies on jmp_buf being the first thing in the png structure
249         // created by libpng! (this is correct for libpng 1.2.x)
250         if (setjmp(png))
251         {
252                 qpng_destroy_read_struct(&png, &pnginfo, 0);
253                 return NULL;
254         }
255         //
256
257         pnginfo = qpng_create_info_struct(png);
258         if(!pnginfo)
259         {
260                 qpng_destroy_read_struct(&png, &pnginfo, 0);
261                 return NULL;
262         }
263         qpng_set_sig_bytes(png, 0);
264
265         memset(&my_png, 0, sizeof(my_png));
266         my_png.tmpBuf = raw;
267         my_png.tmpBuflength = filesize;
268         my_png.tmpi = 0;
269         //my_png.Data           = NULL;
270         //my_png.FRowPtrs       = NULL;
271         //my_png.Height         = 0;
272         //my_png.Width          = 0;
273         my_png.ColorType        = PNG_COLOR_TYPE_RGB;
274         //my_png.Interlace      = 0;
275         //my_png.Compression    = 0;
276         //my_png.Filter         = 0;
277         qpng_set_read_fn(png, ioBuffer, PNG_fReadData);
278         qpng_read_info(png, pnginfo);
279         qpng_get_IHDR(png, pnginfo, &my_png.Width, &my_png.Height,&my_png.BitDepth, &my_png.ColorType, &my_png.Interlace, &my_png.Compression, &my_png.Filter);
280         if ((matchwidth && my_png.Width != (unsigned int)matchwidth) || (matchheight && my_png.Height != (unsigned int)matchheight))
281         {
282                 qpng_destroy_read_struct(&png, &pnginfo, 0);
283                 return NULL;
284         }
285         if (my_png.ColorType == PNG_COLOR_TYPE_PALETTE)
286                 qpng_set_palette_to_rgb(png);
287         if (my_png.ColorType == PNG_COLOR_TYPE_GRAY || my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
288         {
289                 qpng_set_gray_to_rgb(png);
290                 if (my_png.BitDepth < 8)
291                         qpng_set_gray_1_2_4_to_8(png);
292         }
293
294         if (qpng_get_valid(png, pnginfo, PNG_INFO_tRNS))
295                 qpng_set_tRNS_to_alpha(png);
296         if (my_png.BitDepth == 8 && !(my_png.ColorType  & PNG_COLOR_MASK_ALPHA))
297                 qpng_set_filler(png, 255, 1);
298         if (( my_png.ColorType == PNG_COLOR_TYPE_GRAY) || (my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA ))
299                 qpng_set_gray_to_rgb(png);
300         if (my_png.BitDepth < 8)
301                 qpng_set_expand(png);
302
303         qpng_read_update_info(png, pnginfo);
304
305         my_png.FRowBytes = qpng_get_rowbytes(png, pnginfo);
306         my_png.BytesPerPixel = qpng_get_channels(png, pnginfo);
307
308         my_png.FRowPtrs = Mem_Alloc(tempmempool, my_png.Height * sizeof(*my_png.FRowPtrs));
309         if (my_png.FRowPtrs)
310         {
311                 my_png.Data = Mem_Alloc(tempmempool, my_png.Height * my_png.FRowBytes);
312                 if(my_png.Data)
313                 {
314                         for(y = 0;y < my_png.Height;y++)
315                                 my_png.FRowPtrs[y] = my_png.Data + y * my_png.FRowBytes;
316                         qpng_read_image(png, my_png.FRowPtrs);
317                 }
318                 else
319                         Con_DPrintf("PNG_LoadImage : not enough memory\n");
320                 Mem_Free(my_png.FRowPtrs);
321         }
322         else
323                 Con_DPrintf("PNG_LoadImage : not enough memory\n");
324
325         qpng_read_end(png, pnginfo);
326         qpng_destroy_read_struct(&png, &pnginfo, 0);
327
328         image_width = my_png.Width;
329         image_height = my_png.Height;
330         imagedata = my_png.Data;
331
332         if (my_png.BitDepth != 8)
333         {
334                 Con_Printf ("PNG_LoadImage : bad color depth\n");
335                 Mem_Free(imagedata);
336                 imagedata = NULL;
337         }
338
339         return imagedata;
340 }
341