]> icculus.org git repositories - divverent/netradiant.git/blob - plugins/imagepng/plugin.cpp
Merge branch 'icculus'
[divverent/netradiant.git] / plugins / imagepng / plugin.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "plugin.h"
23
24 #include "debugging/debugging.h"
25
26 #include "ifilesystem.h"
27 #include "iimage.h"
28
29 #include "imagelib.h"
30
31 // ====== PNG loader functionality ======
32
33 #include "png.h"
34 #include <stdlib.h>
35
36 void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
37 {
38   globalErrorStream() << "libpng warning: " << warning_msg << "\n";
39 }
40
41 void user_error_fn(png_structp png_ptr, png_const_charp error_msg)
42 {
43   globalErrorStream() << "libpng error: " << error_msg << "\n";
44   longjmp(png_jmpbuf(png_ptr), 0);
45 }
46
47 void user_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
48 {
49   png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr(png_ptr);
50   memcpy(data, *p_p_fbuffer, length);
51   *p_p_fbuffer += length;
52 }
53
54 Image* LoadPNGBuff (unsigned char* fbuffer)
55 {
56   png_byte** row_pointers;
57   png_bytep p_fbuffer;
58
59   p_fbuffer = fbuffer;
60         
61   // the reading glue
62   // http://www.libpng.org/pub/png/libpng-manual.html
63
64   png_structp png_ptr = png_create_read_struct
65     (PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
66     user_error_fn, user_warning_fn);
67   if (!png_ptr)
68   {
69     globalErrorStream() << "libpng error: png_create_read_struct\n";
70     return 0;
71   }
72                 
73   png_infop info_ptr = png_create_info_struct(png_ptr);
74   if (!info_ptr) {
75     png_destroy_read_struct(&png_ptr,
76       (png_infopp)NULL, (png_infopp)NULL);
77     globalErrorStream() << "libpng error: png_create_info_struct (info_ptr)\n";
78     return 0;
79   }
80         
81   png_infop end_info = png_create_info_struct(png_ptr);
82   if (!end_info) {
83     png_destroy_read_struct(&png_ptr, &info_ptr,
84       (png_infopp)NULL);
85     globalErrorStream() << "libpng error: png_create_info_struct (end_info)\n";
86     return 0;
87   }
88
89   // configure the read function
90   png_set_read_fn(png_ptr, (voidp)&p_fbuffer, (png_rw_ptr)&user_read_data);
91
92   if (setjmp(png_jmpbuf(png_ptr))) {
93     png_destroy_read_struct(&png_ptr, &info_ptr,
94       &end_info);
95     return 0;
96   }
97
98   png_read_info(png_ptr, info_ptr);
99
100   int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
101   int color_type = png_get_color_type(png_ptr, info_ptr);
102
103   // we want to treat all images the same way
104   //   The following code transforms grayscale images of less than 8 to 8 bits, 
105   //   changes paletted images to RGB, and adds a full alpha channel if there is 
106   //   transparency information in a tRNS chunk.
107   if (color_type == PNG_COLOR_TYPE_PALETTE)
108    png_set_palette_to_rgb(png_ptr);
109
110   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
111     png_set_expand_gray_1_2_4_to_8(png_ptr);
112
113   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
114     png_set_tRNS_to_alpha(png_ptr);
115
116   if ( ! ( color_type & PNG_COLOR_MASK_ALPHA ) ) {
117     // Set the background color to draw transparent and alpha images over.
118     png_color_16 my_background, *image_background;
119
120     if (png_get_bKGD(png_ptr, info_ptr, &image_background))
121       png_set_background(png_ptr, image_background, 
122       PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
123     else
124       png_set_background(png_ptr, &my_background,
125       PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
126
127     // Add alpha byte after each RGB triplet
128     png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
129   }
130
131   // read the sucker in one chunk
132   png_read_update_info(png_ptr, info_ptr);
133
134   color_type = png_get_color_type(png_ptr, info_ptr);
135   bit_depth = png_get_bit_depth(png_ptr, info_ptr);
136
137   int width = png_get_image_width(png_ptr, info_ptr);
138   int height = png_get_image_height(png_ptr, info_ptr);
139
140   // allocate the pixel buffer, and the row pointers
141   RGBAImage* image = new RGBAImage(width, height);
142
143   row_pointers = (png_byte**) malloc((height) * sizeof(png_byte*));
144
145   int i;
146   for(i = 0; i < (height); i++)
147     row_pointers[i] = (png_byte*)(image->getRGBAPixels()) + i * 4 * (width);
148
149   // actual read
150   png_read_image(png_ptr, row_pointers);
151
152   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
153   png_read_end(png_ptr, info_ptr);
154
155   /* free up the memory structure */
156   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
157
158   free(row_pointers);
159
160   return image;
161 }
162
163 Image* LoadPNG(ArchiveFile& file)
164 {
165   ScopedArchiveBuffer buffer(file);
166   return LoadPNGBuff( buffer.buffer );
167 }
168
169
170 #include "modulesystem/singletonmodule.h"
171
172
173 class ImageDependencies : public GlobalFileSystemModuleRef
174 {
175 };
176
177 class ImagePNGAPI
178 {
179   _QERPlugImageTable m_imagepng;
180 public:
181   typedef _QERPlugImageTable Type;
182   STRING_CONSTANT(Name, "png");
183
184   ImagePNGAPI()
185   {
186     m_imagepng.loadImage = LoadPNG;
187   }
188   _QERPlugImageTable* getTable()
189   {
190     return &m_imagepng;
191   }
192 };
193
194 typedef SingletonModule<ImagePNGAPI, ImageDependencies> ImagePNGModule;
195
196 ImagePNGModule g_ImagePNGModule;
197
198
199 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
200 {
201   initialiseModule(server);
202
203   g_ImagePNGModule.selfRegister();
204 }