Debian Codegraph @

plugins/imagepng/plugin.cpp

raw symbols SCIP
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 "debugging/debugging.h"
23
24 #include "ifilesystem.h"
25 #include "iimage.h"
26
27 #include "imagelib.h"
28
29 // ====== PNG loader functionality ======
30
31 #include "png.h"
32 #include <stdlib.h>
33
34 void user_warning_fn( png_structp png_ptr, png_const_charp warning_msg ){
35 globalErrorStream() << "libpng warning: " << warning_msg << "\n";
36 }
37
38 void user_error_fn( png_structp png_ptr, png_const_charp error_msg ){
39 globalErrorStream() << "libpng error: " << error_msg << "\n";
40 longjmp( png_jmpbuf(png_ptr), 0 );
41 }
42
43 void user_read_data( png_structp png_ptr, png_bytep data, png_size_t length ){
44 png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr( png_ptr );
45 memcpy( data, *p_p_fbuffer, length );
46 *p_p_fbuffer += length;
47 }
48
49 Image* LoadPNGBuff( unsigned char* fbuffer ){
50 png_byte** row_pointers;
51 png_bytep p_fbuffer;
52
53 p_fbuffer = fbuffer;
54
55 // the reading glue
56 // http://www.libpng.org/pub/png/libpng-manual.html
57
58 png_structp png_ptr = png_create_read_struct
59 ( PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
60 user_error_fn, user_warning_fn );
61 if ( !png_ptr ) {
62 globalErrorStream() << "libpng error: png_create_read_struct\n";
63 return 0;
64 }
65
66 png_infop info_ptr = png_create_info_struct( png_ptr );
67 if ( !info_ptr ) {
68 png_destroy_read_struct( &png_ptr,
69 (png_infopp)NULL, (png_infopp)NULL );
70 globalErrorStream() << "libpng error: png_create_info_struct (info_ptr)\n";
71 return 0;
72 }
73
74 png_infop end_info = png_create_info_struct( png_ptr );
75 if ( !end_info ) {
76 png_destroy_read_struct( &png_ptr, &info_ptr,
77 (png_infopp)NULL );
78 globalErrorStream() << "libpng error: png_create_info_struct (end_info)\n";
79 return 0;
80 }
81
82 // configure the read function
83 png_set_read_fn( png_ptr, (png_voidp) &p_fbuffer, &user_read_data );
84
85 if ( setjmp( png_jmpbuf(png_ptr) ) ) {
86 png_destroy_read_struct( &png_ptr, &info_ptr,
87 &end_info );
88 return 0;
89 }
90
91 png_read_info( png_ptr, info_ptr );
92
93 int bit_depth = png_get_bit_depth( png_ptr, info_ptr );
94 int color_type = png_get_color_type( png_ptr, info_ptr );
95
96 // we want to treat all images the same way
97 // The following code transforms grayscale images of less than 8 to 8 bits,
98 // changes paletted images to RGB, and adds a full alpha channel if there is
99 // transparency information in a tRNS chunk.
100
101 if ( color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) {
102 png_set_gray_to_rgb( png_ptr );
103 }
104 else if ( color_type == PNG_COLOR_TYPE_PALETTE ) {
105 png_set_palette_to_rgb( png_ptr );
106 }
107
108 if ( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 ) {
109 png_set_expand_gray_1_2_4_to_8( png_ptr );
110 }
111
112 // Strip 16-bit to 8-bit
113 if ( bit_depth == 16 ) {
114 png_set_strip_16( png_ptr );
115 }
116
117 if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) ) {
118 png_set_tRNS_to_alpha( png_ptr );
119 }
120
121 else if ( !( color_type & PNG_COLOR_MASK_ALPHA ) ) {
122 // Set the background color to draw transparent and alpha images over.
123 png_color_16 my_background, *image_background;
124
125 if ( png_get_bKGD( png_ptr, info_ptr, &image_background ) ) {
126 png_set_background( png_ptr, image_background,
127 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0 );
128 }
129 else{
130 png_set_background( png_ptr, &my_background,
131 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0 );
132 }
133
134 // Add alpha byte after each RGB triplet
135 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
136 }
137
138 // read the sucker in one chunk
139 png_read_update_info( png_ptr, info_ptr );
140
141 color_type = png_get_color_type( png_ptr, info_ptr );
142 bit_depth = png_get_bit_depth( png_ptr, info_ptr );
143
144 int width = png_get_image_width( png_ptr, info_ptr );
145 int height = png_get_image_height( png_ptr, info_ptr );
146
147 // allocate the pixel buffer, and the row pointers
148 RGBAImage* image = new RGBAImage( width, height );
149
150 row_pointers = (png_byte**) malloc( ( height ) * sizeof( png_byte* ) );
151
152 int i;
153 for ( i = 0; i < ( height ); i++ )
154 row_pointers[i] = (png_byte*)( image->getRGBAPixels() ) + i * 4 * ( width );
155
156 // actual read
157 png_read_image( png_ptr, row_pointers );
158
159 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
160 png_read_end( png_ptr, info_ptr );
161
162 /* free up the memory structure */
163 png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
164
165 free( row_pointers );
166
167 return image;
168 }
169
170 Image* LoadPNG( ArchiveFile& file ){
171 ScopedArchiveBuffer buffer( file );
172 return LoadPNGBuff( buffer.buffer );
173 }
174
175
176 #include "modulesystem/singletonmodule.h"
177
178
179 class ImageDependencies : public GlobalFileSystemModuleRef
180 {
181 };
182
183 class ImagePNGAPI
184 {
185 _QERPlugImageTable m_imagepng;
186 public:
187 typedef _QERPlugImageTable Type;
188
189 STRING_CONSTANT( Name, "png" );
190
191 ImagePNGAPI(){
192 m_imagepng.loadImage = LoadPNG;
193 }
194
195 _QERPlugImageTable* getTable(){
196 return &m_imagepng;
197 }
198 };
199
200 typedef SingletonModule<ImagePNGAPI, ImageDependencies> ImagePNGModule;
201
202 ImagePNGModule g_ImagePNGModule;
203
204
205 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
206 initialiseModule( server );
207
208 g_ImagePNGModule.selfRegister();
209 }