]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/imagepng/plugin.cpp
moved to the web repository. online at http://icculus.org/gtkradiant/documentation...
[xonotic/netradiant.git] / plugins / imagepng / plugin.cpp
1 /*
2    Copyright (C) 1999-2007 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 // =============================================================================
23 // global tables
24
25 #include "plugin.h"
26
27 _QERFuncTable_1 g_FuncTable;
28 _QERFileSystemTable g_FileSystemTable;
29
30 // =============================================================================
31 // SYNAPSE
32
33 #include "synapse.h"
34
35 class CSynapseClientImage : public CSynapseClient
36 {
37 public:
38 // CSynapseClient API
39 bool RequestAPI( APIDescriptor_t *pAPI );
40 const char* GetInfo();
41
42 CSynapseClientImage() { }
43 virtual ~CSynapseClientImage() { }
44 };
45
46 CSynapseServer* g_pSynapseServer = NULL;
47 CSynapseClientImage g_SynapseClient;
48
49 #if __GNUC__ >= 4
50 #pragma GCC visibility push(default)
51 #endif
52 extern "C" CSynapseClient * SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
53 #if __GNUC__ >= 4
54 #pragma GCC visibility pop
55 #endif
56         if ( strcmp( version, SYNAPSE_VERSION ) ) {
57                 Syn_Printf( "ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version );
58                 return NULL;
59         }
60         g_pSynapseServer = pServer;
61         g_pSynapseServer->IncRef();
62         Set_Syn_Printf( g_pSynapseServer->Get_Syn_Printf() );
63
64         g_SynapseClient.AddAPI( IMAGE_MAJOR, "png", sizeof( _QERPlugImageTable ) );
65         g_SynapseClient.AddAPI( RADIANT_MAJOR, NULL, sizeof( _QERFuncTable_1 ), SYN_REQUIRE, &g_FuncTable );
66         // NOTE: if imagepng starts being used for non "VFS" "pk3" config, need to add a dynamic config chunk
67         g_SynapseClient.AddAPI( VFS_MAJOR, "pk3", sizeof( _QERFileSystemTable ), SYN_REQUIRE, &g_FileSystemTable );
68
69         return &g_SynapseClient;
70 }
71
72 bool CSynapseClientImage::RequestAPI( APIDescriptor_t *pAPI ){
73         if ( !strcmp( pAPI->major_name, IMAGE_MAJOR ) ) {
74                 _QERPlugImageTable* pTable = static_cast<_QERPlugImageTable*>( pAPI->mpTable );
75                 if ( !strcmp( pAPI->minor_name, "png" ) ) {
76                         pTable->m_pfnLoadImage = &LoadImage;
77                         return true;
78                 }
79         }
80
81         Syn_Printf( "ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo() );
82         return false;
83 }
84
85 #include "version.h"
86
87 const char* CSynapseClientImage::GetInfo(){
88         return "PNG loader module built " __DATE__ " " RADIANT_VERSION;
89 }
90
91
92
93 // ====== PNG loader functionality ======
94
95 #include "png.h"
96
97 #ifdef __APPLE__    //tigital
98 #include <stdlib.h>
99 #endif
100
101 void user_warning_fn( png_structp png_ptr, png_const_charp warning_msg ){
102         g_FuncTable.m_pfnSysPrintf( "libpng warning: %s\n", warning_msg );
103 }
104
105 void user_error_fn( png_structp png_ptr, png_const_charp error_msg ){
106         g_FuncTable.m_pfnSysPrintf( "libpng error: %s\n", error_msg );
107         longjmp( png_jmpbuf(png_ptr), 0 );
108 }
109
110 void user_read_data( png_structp png_ptr, png_bytep data, png_uint_32 length ){
111         png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr( png_ptr );
112         memcpy( data, *p_p_fbuffer, length );
113         *p_p_fbuffer += length;
114 }
115
116 void LoadImage( const char *filename, unsigned char **pic, int *width, int *height ){
117         png_byte** row_pointers;
118         unsigned char *fbuffer = NULL;
119         png_bytep p_fbuffer;
120
121         int nLen = g_FileSystemTable.m_pfnLoadFile( (char *)filename, (void **)&fbuffer, 0 );
122         if ( nLen == -1 ) {
123                 return;
124         }
125
126         p_fbuffer = fbuffer;
127
128         // the reading glue
129         // http://www.libpng.org/pub/png/libpng-manual.html
130
131         png_structp png_ptr = png_create_read_struct
132                                                           ( PNG_LIBPNG_VER_STRING, NULL,
133                                                           user_error_fn, user_warning_fn );
134         if ( !png_ptr ) {
135                 g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_read_struct\n" );
136                 return;
137         }
138
139         png_infop info_ptr = png_create_info_struct( png_ptr );
140         if ( !info_ptr ) {
141                 png_destroy_read_struct( &png_ptr,
142                                                                  NULL, NULL );
143                 g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_info_struct (info_ptr)\n" );
144                 return;
145         }
146
147         png_infop end_info = png_create_info_struct( png_ptr );
148         if ( !end_info ) {
149                 png_destroy_read_struct( &png_ptr, &info_ptr,
150                                                                  NULL );
151                 g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_info_struct (end_info)\n" );
152                 return;
153         }
154
155         // configure the read function
156         png_set_read_fn( png_ptr, ( void * ) & p_fbuffer, ( png_rw_ptr ) & user_read_data );
157
158         if ( setjmp( png_jmpbuf(png_ptr) ) ) {
159                 png_destroy_read_struct( &png_ptr, &info_ptr,
160                                                                  &end_info );
161                 if ( *pic ) {
162                         g_free( *pic );
163                         free( row_pointers );
164                 }
165                 return;
166         }
167
168         png_read_info( png_ptr, info_ptr );
169
170         int bit_depth = png_get_bit_depth( png_ptr, info_ptr );
171         int color_type = png_get_color_type( png_ptr, info_ptr );
172
173         // we want to treat all images the same way
174         //   The following code transforms grayscale images of less than 8 to 8 bits,
175         //   changes paletted images to RGB, and adds a full alpha channel if there is
176         //   transparency information in a tRNS chunk.
177         if ( color_type == PNG_COLOR_TYPE_PALETTE ) {
178                 png_set_palette_to_rgb( png_ptr );
179         }
180
181         if ( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 ) {
182                 png_set_expand_gray_1_2_4_to_8( png_ptr );
183         }
184
185         if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) ) {
186                 png_set_tRNS_to_alpha( png_ptr );
187         }
188
189         if ( !( color_type & PNG_COLOR_MASK_ALPHA ) ) {
190                 // Set the background color to draw transparent and alpha images over.
191                 png_color_16 my_background, *image_background;
192
193                 if ( png_get_bKGD( png_ptr, info_ptr, &image_background ) ) {
194                         png_set_background( png_ptr, image_background,
195                                                                 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0 );
196                 }
197                 else{
198                         png_set_background( png_ptr, &my_background,
199                                                                 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0 );
200                 }
201
202                 // Add alpha byte after each RGB triplet
203                 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
204         }
205
206         // read the sucker in one chunk
207         png_read_update_info( png_ptr, info_ptr );
208
209         color_type = png_get_color_type( png_ptr, info_ptr );
210         bit_depth = png_get_bit_depth( png_ptr, info_ptr );
211
212         *width = png_get_image_width( png_ptr, info_ptr );
213         *height = png_get_image_height( png_ptr, info_ptr );
214
215         // allocate the pixel buffer, and the row pointers
216         int size = ( *width ) * ( *height ) * 4;
217         // still have to use that g_malloc heresy
218         *pic = (unsigned char *)g_malloc( size );
219         row_pointers = (png_byte**) malloc( ( *height ) * sizeof( png_byte* ) );
220
221         int i;
222         for ( i = 0; i < ( *height ); i++ )
223                 row_pointers[i] = (png_byte*)( *pic ) + i * 4 * ( *width );
224
225         // actual read
226         png_read_image( png_ptr, row_pointers );
227
228         /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
229         png_read_end( png_ptr, info_ptr );
230
231         /* free up the memory structure */
232         png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
233
234         free( row_pointers );
235         g_FileSystemTable.m_pfnFreeFile( fbuffer );
236 }