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