/*
- Copyright (C) 1999-2007 id Software, Inc. and contributors.
+ 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.
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-// =============================================================================
-// global tables
-
#include "plugin.h"
-_QERFuncTable_1 g_FuncTable;
-_QERFileSystemTable g_FileSystemTable;
-
-// =============================================================================
-// SYNAPSE
-
-#include "synapse.h"
-
-class CSynapseClientImage : public CSynapseClient
-{
-public:
-// CSynapseClient API
-bool RequestAPI( APIDescriptor_t *pAPI );
-const char* GetInfo();
-
-CSynapseClientImage() { }
-virtual ~CSynapseClientImage() { }
-};
-
-CSynapseServer* g_pSynapseServer = NULL;
-CSynapseClientImage g_SynapseClient;
-
-#if __GNUC__ >= 4
-#pragma GCC visibility push(default)
-#endif
-extern "C" CSynapseClient * SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
-#if __GNUC__ >= 4
-#pragma GCC visibility pop
-#endif
- if ( strcmp( version, SYNAPSE_VERSION ) ) {
- Syn_Printf( "ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version );
- return NULL;
- }
- g_pSynapseServer = pServer;
- g_pSynapseServer->IncRef();
- Set_Syn_Printf( g_pSynapseServer->Get_Syn_Printf() );
-
- g_SynapseClient.AddAPI( IMAGE_MAJOR, "png", sizeof( _QERPlugImageTable ) );
- g_SynapseClient.AddAPI( RADIANT_MAJOR, NULL, sizeof( _QERFuncTable_1 ), SYN_REQUIRE, &g_FuncTable );
- // NOTE: if imagepng starts being used for non "VFS" "pk3" config, need to add a dynamic config chunk
- g_SynapseClient.AddAPI( VFS_MAJOR, "pk3", sizeof( _QERFileSystemTable ), SYN_REQUIRE, &g_FileSystemTable );
-
- return &g_SynapseClient;
-}
-
-bool CSynapseClientImage::RequestAPI( APIDescriptor_t *pAPI ){
- if ( !strcmp( pAPI->major_name, IMAGE_MAJOR ) ) {
- _QERPlugImageTable* pTable = static_cast<_QERPlugImageTable*>( pAPI->mpTable );
- if ( !strcmp( pAPI->minor_name, "png" ) ) {
- pTable->m_pfnLoadImage = &LoadImage;
- return true;
- }
- }
-
- Syn_Printf( "ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo() );
- return false;
-}
-
-#include "version.h"
-
-const char* CSynapseClientImage::GetInfo(){
- return "PNG loader module built " __DATE__ " " RADIANT_VERSION;
-}
+#include "debugging/debugging.h"
+#include "ifilesystem.h"
+#include "iimage.h"
+#include "imagelib.h"
// ====== PNG loader functionality ======
#include "png.h"
-
-#ifdef __APPLE__ //tigital
#include <stdlib.h>
-#endif
void user_warning_fn( png_structp png_ptr, png_const_charp warning_msg ){
- g_FuncTable.m_pfnSysPrintf( "libpng warning: %s\n", warning_msg );
+ globalErrorStream() << "libpng warning: " << warning_msg << "\n";
}
void user_error_fn( png_structp png_ptr, png_const_charp error_msg ){
- g_FuncTable.m_pfnSysPrintf( "libpng error: %s\n", error_msg );
+ globalErrorStream() << "libpng error: " << error_msg << "\n";
- longjmp( png_jmpbuf( png_ptr ), 0 );
+ longjmp( png_jmpbuf(png_ptr), 0 );
}
void user_read_data( png_structp png_ptr, png_bytep data, png_uint_32 length ){
*p_p_fbuffer += length;
}
-void LoadImage( const char *filename, unsigned char **pic, int *width, int *height ){
+Image* LoadPNGBuff( unsigned char* fbuffer ){
png_byte** row_pointers;
- unsigned char *fbuffer = NULL;
png_bytep p_fbuffer;
- int nLen = g_FileSystemTable.m_pfnLoadFile( (char *)filename, (void **)&fbuffer, 0 );
- if ( nLen == -1 ) {
- return;
- }
-
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, NULL,
+ ( PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
user_error_fn, user_warning_fn );
if ( !png_ptr ) {
- g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_read_struct\n" );
- return;
+ 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,
- NULL, NULL );
- g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_info_struct (info_ptr)\n" );
- return;
+ (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,
- NULL );
- g_FuncTable.m_pfnSysPrintf( "libpng error: png_create_info_struct (end_info)\n" );
- return;
+ (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, ( void * ) & p_fbuffer, ( png_rw_ptr ) & user_read_data );
+ png_set_read_fn( png_ptr, ( png_voidp ) & p_fbuffer, ( png_rw_ptr ) & user_read_data );
- if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
+ if ( setjmp( png_jmpbuf(png_ptr) ) ) {
png_destroy_read_struct( &png_ptr, &info_ptr,
&end_info );
- if ( *pic ) {
- g_free( *pic );
- free( row_pointers );
- }
- return;
+ return 0;
}
png_read_info( png_ptr, info_ptr );
// 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_PALETTE ) {
+
+ 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 );
}
color_type = png_get_color_type( png_ptr, info_ptr );
bit_depth = png_get_bit_depth( png_ptr, info_ptr );
- *width = png_get_image_width( png_ptr, info_ptr );
- *height = png_get_image_height( 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
- int size = ( *width ) * ( *height ) * 4;
- // still have to use that g_malloc heresy
- *pic = (unsigned char *)g_malloc( size );
- row_pointers = (png_byte**) malloc( ( *height ) * sizeof( png_byte* ) );
+ 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*)( *pic ) + i * 4 * ( *width );
+ 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 );
png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
free( row_pointers );
- g_FileSystemTable.m_pfnFreeFile( fbuffer );
+
+ 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();
}
typedef struct pngBuffer_s
{
byte *buffer;
- int size, offset;
+ png_size_t size, offset;
}
pngBuffer_t;
static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
png_struct *png;
png_info *info, *end;
- pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
- int i, bitDepth, colorType, channels;
- png_uint_32 w, h;
+ pngBuffer_t pb;
++ //pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
+ int bitDepth, colorType;
+ png_uint_32 w, h, i;
byte **rowPointers;
-
/* dummy check */
if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
return;
}
/* set read callback */
- pb.buffer = buffer;
- pb.size = size;
- pb.offset = 0;
+ pb->buffer = buffer;
+ pb->size = size;
+ pb->offset = 0;
png_set_read_fn( png, &pb, PNGReadData );
+ //png->io_ptr = &pb; /* hack! */
/* set error longjmp */
- if ( setjmp( png_jmpbuf( png ) ) ) {
+ if ( setjmp( png_jmpbuf(png) ) ) {
Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
png_destroy_read_struct( &png, &info, &end );
return;
png_get_IHDR( png, info,
&w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
- /* read number of channels */
- channels = png_get_channels( png, info );
-
/* the following will probably bork on certain types of png images, but hey... */
/* force indexed/gray/trans chunk to rgb */
char name[ 1024 ];
int size;
byte *buffer = NULL;
+ qboolean alphaHack = qfalse;
/* init */
if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
}
+ alphaHack = qtrue;
}
else
{
image->refCount = 1;
numImages++;
+ if ( alphaHack ) {
+ StripExtension( name );
+ strcat( name, "_alpha.jpg" );
+ size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+ if ( size > 0 ) {
+ unsigned char *pixels;
+ int width, height;
+ if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != NULL ) {
+ Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
+ }
+ if ( pixels && width == image->width && height == image->height ) {
+ int i;
+ for ( i = 0; i < width * height; ++i )
+ image->pixels[4 * i + 3] = pixels[4 * i + 2]; // copy alpha from blue channel
+ }
+ free( pixels );
+ free( buffer );
+ }
+ }
+
/* return the image */
return image;
}