]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/image.c
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
index 2af46a6f7721761f3b2f30f68e95471d46481751..5ab1d48b1c1a4799b295564851401d751a93a6e0 100644 (file)
@@ -97,7 +97,7 @@ static void LoadDDSBuffer( byte *buffer, int size, byte **pixels, int *width, in
 typedef struct pngBuffer_s
 {
        byte    *buffer;
 typedef struct pngBuffer_s
 {
        byte    *buffer;
-       int size, offset;
+       png_size_t size, offset;
 } pngBuffer_t;
 
 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
 } pngBuffer_t;
 
 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
@@ -122,9 +122,10 @@ void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
        png_struct  *png;
        png_info    *info, *end;
 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
        png_struct  *png;
        png_info    *info, *end;
-    pngBuffer_t pb;
-       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 */
        byte        **rowPointers;
 
        /* dummy check */
@@ -165,10 +166,11 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
        }
 
        /* set read callback */
        }
 
        /* set read callback */
-    pb.buffer = buffer;
-    pb.size = size;
-    pb.offset = 0;
-       png_set_read_fn( png, (void*)&pb, PNGReadData );
+       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) ) ) {
 
        /* set error longjmp */
        if ( setjmp( png_jmpbuf(png) ) ) {
@@ -186,9 +188,6 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
        png_get_IHDR( png, info,
                                  &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
 
        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 */
        /* the following will probably bork on certain types of png images, but hey... */
 
        /* force indexed/gray/trans chunk to rgb */
@@ -338,6 +337,7 @@ image_t *ImageLoad( const char *filename ){
        char name[ 1024 ];
        int size;
        byte        *buffer = NULL;
        char name[ 1024 ];
        int size;
        byte        *buffer = NULL;
+       qboolean alphaHack = qfalse;
 
 
        /* init */
 
 
        /* init */
@@ -404,6 +404,7 @@ image_t *ImageLoad( const char *filename ){
                                if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
                                        Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
                                }
                                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
                        {
                        }
                        else
                        {
@@ -452,6 +453,26 @@ image_t *ImageLoad( const char *filename ){
        image->refCount = 1;
        numImages++;
 
        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;
 }
        /* return the image */
        return image;
 }