2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
22 // =============================================================================
\r
27 _QERFuncTable_1 g_FuncTable;
\r
28 _QERFileSystemTable g_FileSystemTable;
\r
30 // =============================================================================
\r
33 #include "synapse.h"
\r
35 class CSynapseClientImage : public CSynapseClient
\r
38 // CSynapseClient API
\r
39 bool RequestAPI(APIDescriptor_t *pAPI);
\r
40 const char* GetInfo();
\r
42 CSynapseClientImage() { }
\r
43 virtual ~CSynapseClientImage() { }
\r
46 CSynapseServer* g_pSynapseServer = NULL;
\r
47 CSynapseClientImage g_SynapseClient;
\r
49 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
\r
51 if (strcmp(version, SYNAPSE_VERSION))
\r
53 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
\r
56 g_pSynapseServer = pServer;
\r
57 g_pSynapseServer->IncRef();
\r
58 Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
\r
60 g_SynapseClient.AddAPI(IMAGE_MAJOR, "png", sizeof(_QERPlugImageTable));
\r
61 g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(_QERFuncTable_1), SYN_REQUIRE, &g_FuncTable);
\r
62 // NOTE: if imagepng starts being used for non "VFS" "pk3" config, need to add a dynamic config chunk
\r
64 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=794
\r
65 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=800
\r
66 g_SynapseClient.AddAPI(VFS_MAJOR, "pk3", sizeof(_QERFileSystemTable), SYN_REQUIRE, &g_FileSystemTable);
\r
68 return &g_SynapseClient;
\r
71 bool CSynapseClientImage::RequestAPI(APIDescriptor_t *pAPI)
\r
73 if (!strcmp(pAPI->major_name, IMAGE_MAJOR))
\r
75 _QERPlugImageTable* pTable= static_cast<_QERPlugImageTable*>(pAPI->mpTable);
\r
76 if (!strcmp(pAPI->minor_name, "png"))
\r
78 pTable->m_pfnLoadImage = &LoadImage;
\r
83 Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
\r
87 #include "version.h"
\r
89 const char* CSynapseClientImage::GetInfo()
\r
91 return "PNG loader module built " __DATE__ " " RADIANT_VERSION;
\r
96 // ====== PNG loader functionality ======
\r
100 #ifdef __APPLE__ //tigital
\r
101 #include <stdlib.h>
\r
104 void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
\r
106 g_FuncTable.m_pfnSysPrintf ("libpng warning: %s\n", warning_msg);
\r
109 void user_error_fn(png_structp png_ptr, png_const_charp error_msg)
\r
111 g_FuncTable.m_pfnSysPrintf ("libpng error: %s\n", error_msg);
\r
112 longjmp(png_ptr->jmpbuf, 0);
\r
115 void user_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
\r
117 png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr(png_ptr);
\r
118 memcpy(data, *p_p_fbuffer, length);
\r
119 *p_p_fbuffer += length;
\r
122 void LoadImage (const char *filename, unsigned char **pic, int *width, int *height)
\r
124 png_byte** row_pointers;
\r
125 unsigned char *fbuffer = NULL;
\r
126 png_bytep p_fbuffer;
\r
128 int nLen = g_FileSystemTable.m_pfnLoadFile( (char *)filename, (void **)&fbuffer, 0 );
\r
132 p_fbuffer = fbuffer;
\r
134 // the reading glue
\r
135 // http://www.libpng.org/pub/png/libpng-manual.html
\r
137 png_structp png_ptr = png_create_read_struct
\r
138 (PNG_LIBPNG_VER_STRING, png_voidp_NULL,
\r
139 user_error_fn, user_warning_fn);
\r
142 g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_read_struct\n");
\r
146 png_infop info_ptr = png_create_info_struct(png_ptr);
\r
148 png_destroy_read_struct(&png_ptr,
\r
149 png_infopp_NULL, png_infopp_NULL);
\r
150 g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (info_ptr)\n");
\r
154 png_infop end_info = png_create_info_struct(png_ptr);
\r
156 png_destroy_read_struct(&png_ptr, &info_ptr,
\r
158 g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (end_info)\n");
\r
162 // configure the read function
\r
163 png_set_read_fn(png_ptr, (voidp)&p_fbuffer, (png_rw_ptr)&user_read_data);
\r
165 if (setjmp(png_ptr->jmpbuf)) {
\r
166 png_destroy_read_struct(&png_ptr, &info_ptr,
\r
171 free(row_pointers);
\r
176 png_read_info(png_ptr, info_ptr);
\r
178 int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
\r
179 int color_type = png_get_color_type(png_ptr, info_ptr);
\r
181 // we want to treat all images the same way
\r
182 // The following code transforms grayscale images of less than 8 to 8 bits,
\r
183 // changes paletted images to RGB, and adds a full alpha channel if there is
\r
184 // transparency information in a tRNS chunk.
\r
185 if (color_type == PNG_COLOR_TYPE_PALETTE)
\r
186 png_set_palette_to_rgb(png_ptr);
\r
188 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
\r
189 png_set_gray_1_2_4_to_8(png_ptr);
\r
191 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
\r
192 png_set_tRNS_to_alpha(png_ptr);
\r
194 if ( ! ( color_type & PNG_COLOR_MASK_ALPHA ) ) {
\r
195 // Set the background color to draw transparent and alpha images over.
\r
196 png_color_16 my_background, *image_background;
\r
198 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
\r
199 png_set_background(png_ptr, image_background,
\r
200 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
\r
202 png_set_background(png_ptr, &my_background,
\r
203 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
\r
205 // Add alpha byte after each RGB triplet
\r
206 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
\r
209 // read the sucker in one chunk
\r
210 png_read_update_info(png_ptr, info_ptr);
\r
212 color_type = png_get_color_type(png_ptr, info_ptr);
\r
213 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
\r
215 *width = png_get_image_width(png_ptr, info_ptr);
\r
216 *height = png_get_image_height(png_ptr, info_ptr);
\r
218 // allocate the pixel buffer, and the row pointers
\r
219 int size = (*width)*(*height)*4;
\r
220 // still have to use that g_malloc heresy
\r
221 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=491
\r
222 *pic = (unsigned char *)g_malloc(size);
\r
223 row_pointers = (png_byte**) malloc((*height) * sizeof(png_byte*));
\r
226 for(i = 0; i < (*height); i++)
\r
227 row_pointers[i] = (png_byte*)(*pic) + i * 4 * (*width);
\r
230 png_read_image(png_ptr, row_pointers);
\r
232 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
\r
233 png_read_end(png_ptr, info_ptr);
\r
235 /* free up the memory structure */
\r
236 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
\r
238 free(row_pointers);
\r
239 g_FileSystemTable.m_pfnFreeFile (fbuffer);
\r