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