-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-\r
-// =============================================================================\r
-// global tables\r
-\r
-#include "plugin.h"\r
-\r
-_QERFuncTable_1 g_FuncTable;\r
-_QERFileSystemTable g_FileSystemTable;\r
-\r
-// =============================================================================\r
-// SYNAPSE\r
-\r
-#include "synapse.h"\r
-\r
-class CSynapseClientImage : public CSynapseClient\r
-{\r
-public:\r
- // CSynapseClient API\r
- bool RequestAPI(APIDescriptor_t *pAPI);\r
- const char* GetInfo();\r
- \r
- CSynapseClientImage() { }\r
- virtual ~CSynapseClientImage() { }\r
-};\r
-\r
-CSynapseServer* g_pSynapseServer = NULL;\r
-CSynapseClientImage g_SynapseClient;\r
-\r
-extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)\r
-{\r
- if (strcmp(version, SYNAPSE_VERSION))\r
- {\r
- Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);\r
- return NULL;\r
- }\r
- g_pSynapseServer = pServer;\r
- g_pSynapseServer->IncRef();\r
- Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());\r
-\r
- g_SynapseClient.AddAPI(IMAGE_MAJOR, "png", sizeof(_QERPlugImageTable));\r
- g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(_QERFuncTable_1), SYN_REQUIRE, &g_FuncTable);\r
- // NOTE: if imagepng starts being used for non "VFS" "pk3" config, need to add a dynamic config chunk\r
- // see:\r
- // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=794\r
- // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=800\r
- g_SynapseClient.AddAPI(VFS_MAJOR, "pk3", sizeof(_QERFileSystemTable), SYN_REQUIRE, &g_FileSystemTable);\r
-\r
- return &g_SynapseClient;\r
-}\r
-\r
-bool CSynapseClientImage::RequestAPI(APIDescriptor_t *pAPI)\r
-{\r
- if (!strcmp(pAPI->major_name, IMAGE_MAJOR))\r
- { \r
- _QERPlugImageTable* pTable= static_cast<_QERPlugImageTable*>(pAPI->mpTable);\r
- if (!strcmp(pAPI->minor_name, "png"))\r
- {\r
- pTable->m_pfnLoadImage = &LoadImage;\r
- return true;\r
- }\r
- }\r
-\r
- Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());\r
- return false;\r
-}\r
-\r
-#include "version.h"\r
-\r
-const char* CSynapseClientImage::GetInfo()\r
-{\r
- return "PNG loader module built " __DATE__ " " RADIANT_VERSION;\r
-}\r
-\r
-\r
-\r
-// ====== PNG loader functionality ======\r
-\r
-#include "png.h"\r
-\r
-#ifdef __APPLE__ //tigital\r
-#include <stdlib.h>\r
-#endif\r
-\r
-void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)\r
-{\r
- g_FuncTable.m_pfnSysPrintf ("libpng warning: %s\n", warning_msg);\r
-}\r
-\r
-void user_error_fn(png_structp png_ptr, png_const_charp error_msg)\r
-{\r
- g_FuncTable.m_pfnSysPrintf ("libpng error: %s\n", error_msg);\r
- longjmp(png_ptr->jmpbuf, 0);\r
-}\r
-\r
-void user_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)\r
-{\r
- png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr(png_ptr);\r
- memcpy(data, *p_p_fbuffer, length);\r
- *p_p_fbuffer += length;\r
-}\r
-\r
-void LoadImage (const char *filename, unsigned char **pic, int *width, int *height)\r
-{\r
- png_byte** row_pointers;\r
- unsigned char *fbuffer = NULL;\r
- png_bytep p_fbuffer;\r
-\r
- int nLen = g_FileSystemTable.m_pfnLoadFile( (char *)filename, (void **)&fbuffer, 0 );\r
- if (nLen == -1)\r
- return;\r
-\r
- p_fbuffer = fbuffer;\r
- \r
- // the reading glue\r
- // http://www.libpng.org/pub/png/libpng-manual.html\r
-\r
- png_structp png_ptr = png_create_read_struct\r
- (PNG_LIBPNG_VER_STRING, png_voidp_NULL,\r
- user_error_fn, user_warning_fn);\r
- if (!png_ptr)\r
- {\r
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_read_struct\n");\r
- return;\r
- }\r
- \r
- png_infop info_ptr = png_create_info_struct(png_ptr);\r
- if (!info_ptr) {\r
- png_destroy_read_struct(&png_ptr,\r
- png_infopp_NULL, png_infopp_NULL);\r
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (info_ptr)\n");\r
- return;\r
- }\r
- \r
- png_infop end_info = png_create_info_struct(png_ptr);\r
- if (!end_info) {\r
- png_destroy_read_struct(&png_ptr, &info_ptr,\r
- png_infopp_NULL);\r
- g_FuncTable.m_pfnSysPrintf ("libpng error: png_create_info_struct (end_info)\n");\r
- return;\r
- }\r
-\r
- // configure the read function\r
- png_set_read_fn(png_ptr, (voidp)&p_fbuffer, (png_rw_ptr)&user_read_data);\r
-\r
- if (setjmp(png_ptr->jmpbuf)) {\r
- png_destroy_read_struct(&png_ptr, &info_ptr,\r
- &end_info);\r
- if (*pic)\r
- {\r
- g_free(*pic);\r
- free(row_pointers);\r
- }\r
- return;\r
- }\r
-\r
- png_read_info(png_ptr, info_ptr);\r
-\r
- int bit_depth = png_get_bit_depth(png_ptr, info_ptr);\r
- int color_type = png_get_color_type(png_ptr, info_ptr);\r
-\r
- // we want to treat all images the same way\r
- // The following code transforms grayscale images of less than 8 to 8 bits, \r
- // changes paletted images to RGB, and adds a full alpha channel if there is \r
- // transparency information in a tRNS chunk.\r
- if (color_type == PNG_COLOR_TYPE_PALETTE)\r
- png_set_palette_to_rgb(png_ptr);\r
-\r
- if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)\r
- png_set_gray_1_2_4_to_8(png_ptr);\r
-\r
- if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))\r
- png_set_tRNS_to_alpha(png_ptr);\r
-\r
- if ( ! ( color_type & PNG_COLOR_MASK_ALPHA ) ) {\r
- // Set the background color to draw transparent and alpha images over.\r
- png_color_16 my_background, *image_background;\r
-\r
- if (png_get_bKGD(png_ptr, info_ptr, &image_background))\r
- png_set_background(png_ptr, image_background, \r
- PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);\r
- else\r
- png_set_background(png_ptr, &my_background,\r
- PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);\r
-\r
- // Add alpha byte after each RGB triplet\r
- png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);\r
- }\r
-\r
- // read the sucker in one chunk\r
- png_read_update_info(png_ptr, info_ptr);\r
-\r
- color_type = png_get_color_type(png_ptr, info_ptr);\r
- bit_depth = png_get_bit_depth(png_ptr, info_ptr);\r
-\r
- *width = png_get_image_width(png_ptr, info_ptr);\r
- *height = png_get_image_height(png_ptr, info_ptr);\r
-\r
- // allocate the pixel buffer, and the row pointers\r
- int size = (*width)*(*height)*4;\r
- // still have to use that g_malloc heresy\r
- // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=491\r
- *pic = (unsigned char *)g_malloc(size);\r
- row_pointers = (png_byte**) malloc((*height) * sizeof(png_byte*));\r
-\r
- int i;\r
- for(i = 0; i < (*height); i++)\r
- row_pointers[i] = (png_byte*)(*pic) + i * 4 * (*width);\r
-\r
- // actual read\r
- png_read_image(png_ptr, row_pointers);\r
-\r
- /* read rest of file, and get additional chunks in info_ptr - REQUIRED */\r
- png_read_end(png_ptr, info_ptr);\r
-\r
- /* free up the memory structure */\r
- png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);\r
-\r
- free(row_pointers);\r
- g_FileSystemTable.m_pfnFreeFile (fbuffer);\r
-}\r
-\r
-\r
+/*
+ Copyright (C) 1999-2006 Id Software, Inc. and contributors.
+ For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+ This file is part of GtkRadiant.
+
+ GtkRadiant is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ GtkRadiant is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GtkRadiant; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "plugin.h"
+
+#include "debugging/debugging.h"
+
+#include "ifilesystem.h"
+#include "iimage.h"
+
+#include "imagelib.h"
+
+// ====== PNG loader functionality ======
+
+#include "png.h"
+#include <stdlib.h>
+
+void user_warning_fn( png_structp png_ptr, png_const_charp warning_msg ){
+ globalErrorStream() << "libpng warning: " << warning_msg << "\n";
+}
+
+void user_error_fn( png_structp png_ptr, png_const_charp error_msg ){
+ globalErrorStream() << "libpng error: " << error_msg << "\n";
+ longjmp( png_jmpbuf( png_ptr ), 0 );
+}
+
+void user_read_data( png_structp png_ptr, png_bytep data, png_uint_32 length ){
+ png_bytep *p_p_fbuffer = (png_bytep*)png_get_io_ptr( png_ptr );
+ memcpy( data, *p_p_fbuffer, length );
+ *p_p_fbuffer += length;
+}
+
+Image* LoadPNGBuff( unsigned char* fbuffer ){
+ png_byte** row_pointers;
+ png_bytep p_fbuffer;
+
+ p_fbuffer = fbuffer;
+
+ // the reading glue
+ // http://www.libpng.org/pub/png/libpng-manual.html
+
+ png_structp png_ptr = png_create_read_struct
+ ( PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
+ user_error_fn, user_warning_fn );
+ if ( !png_ptr ) {
+ globalErrorStream() << "libpng error: png_create_read_struct\n";
+ return 0;
+ }
+
+ png_infop info_ptr = png_create_info_struct( png_ptr );
+ if ( !info_ptr ) {
+ png_destroy_read_struct( &png_ptr,
+ (png_infopp)NULL, (png_infopp)NULL );
+ globalErrorStream() << "libpng error: png_create_info_struct (info_ptr)\n";
+ return 0;
+ }
+
+ png_infop end_info = png_create_info_struct( png_ptr );
+ if ( !end_info ) {
+ png_destroy_read_struct( &png_ptr, &info_ptr,
+ (png_infopp)NULL );
+ globalErrorStream() << "libpng error: png_create_info_struct (end_info)\n";
+ return 0;
+ }
+
+ // configure the read function
+ png_set_read_fn( png_ptr, ( png_voidp ) & p_fbuffer, ( png_rw_ptr ) & user_read_data );
+
+ if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
+ png_destroy_read_struct( &png_ptr, &info_ptr,
+ &end_info );
+ return 0;
+ }
+
+ png_read_info( png_ptr, info_ptr );
+
+ int bit_depth = png_get_bit_depth( png_ptr, info_ptr );
+ int color_type = png_get_color_type( png_ptr, info_ptr );
+
+ // we want to treat all images the same way
+ // The following code transforms grayscale images of less than 8 to 8 bits,
+ // changes paletted images to RGB, and adds a full alpha channel if there is
+ // transparency information in a tRNS chunk.
+
+ if ( color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) {
+ png_set_gray_to_rgb( png_ptr );
+ }
+ else if ( color_type == PNG_COLOR_TYPE_PALETTE ) {
+ png_set_palette_to_rgb( png_ptr );
+ }
+
+ if ( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 ) {
+ png_set_expand_gray_1_2_4_to_8( png_ptr );
+ }
+
+ if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) ) {
+ png_set_tRNS_to_alpha( png_ptr );
+ }
+
+ if ( !( color_type & PNG_COLOR_MASK_ALPHA ) ) {
+ // Set the background color to draw transparent and alpha images over.
+ png_color_16 my_background, *image_background;
+
+ if ( png_get_bKGD( png_ptr, info_ptr, &image_background ) ) {
+ png_set_background( png_ptr, image_background,
+ PNG_BACKGROUND_GAMMA_FILE, 1, 1.0 );
+ }
+ else{
+ png_set_background( png_ptr, &my_background,
+ PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0 );
+ }
+
+ // Add alpha byte after each RGB triplet
+ png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
+ }
+
+ // read the sucker in one chunk
+ png_read_update_info( png_ptr, info_ptr );
+
+ color_type = png_get_color_type( png_ptr, info_ptr );
+ bit_depth = png_get_bit_depth( png_ptr, info_ptr );
+
+ int width = png_get_image_width( png_ptr, info_ptr );
+ int height = png_get_image_height( png_ptr, info_ptr );
+
+ // allocate the pixel buffer, and the row pointers
+ RGBAImage* image = new RGBAImage( width, height );
+
+ row_pointers = (png_byte**) malloc( ( height ) * sizeof( png_byte* ) );
+
+ int i;
+ for ( i = 0; i < ( height ); i++ )
+ row_pointers[i] = (png_byte*)( image->getRGBAPixels() ) + i * 4 * ( width );
+
+ // actual read
+ png_read_image( png_ptr, row_pointers );
+
+ /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
+ png_read_end( png_ptr, info_ptr );
+
+ /* free up the memory structure */
+ png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
+
+ free( row_pointers );
+
+ return image;
+}
+
+Image* LoadPNG( ArchiveFile& file ){
+ ScopedArchiveBuffer buffer( file );
+ return LoadPNGBuff( buffer.buffer );
+}
+
+
+#include "modulesystem/singletonmodule.h"
+
+
+class ImageDependencies : public GlobalFileSystemModuleRef
+{
+};
+
+class ImagePNGAPI
+{
+_QERPlugImageTable m_imagepng;
+public:
+typedef _QERPlugImageTable Type;
+STRING_CONSTANT( Name, "png" );
+
+ImagePNGAPI(){
+ m_imagepng.loadImage = LoadPNG;
+}
+_QERPlugImageTable* getTable(){
+ return &m_imagepng;
+}
+};
+
+typedef SingletonModule<ImagePNGAPI, ImageDependencies> ImagePNGModule;
+
+ImagePNGModule g_ImagePNGModule;
+
+
+extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
+ initialiseModule( server );
+
+ g_ImagePNGModule.selfRegister();
+}