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