]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/image.c
KTX support - something Git didn't want to commit
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
index bb6a52563206a2e65bc92ae1551e05611e8d25c3..c1c737f51b3282b74d827a80fd9fe06110ec7346 100644 (file)
@@ -98,8 +98,7 @@ typedef struct pngBuffer_s
 {
        byte    *buffer;
        png_size_t size, offset;
-}
-pngBuffer_t;
+} pngBuffer_t;
 
 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
        pngBuffer_t     *pb = (pngBuffer_t*) png_get_io_ptr( png );
@@ -124,11 +123,11 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
        png_struct  *png;
        png_info    *info, *end;
        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;
@@ -171,9 +170,10 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
        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;
@@ -402,7 +402,8 @@ image_t *ImageLoad( const char *filename ){
                        size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
                        if ( size > 0 ) {
                                if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
-                                       Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
+                                       // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
+                                       Sys_Printf( "WARNING: LoadJPGBuff %s %s\n", name, (unsigned char*) image->pixels );
                                }
                                alphaHack = qtrue;
                        }
@@ -429,6 +430,16 @@ image_t *ImageLoad( const char *filename ){
                                        }
                                        #endif
                                }
+                               else
+                               {
+                                       /* attempt to load ktx */
+                                       StripExtension( name );
+                                       strcat( name, ".ktx" );
+                                       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+                                       if ( size > 0 ) {
+                                               LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
+                                       }
+                               }
                        }
                }
        }
@@ -460,15 +471,19 @@ image_t *ImageLoad( const char *filename ){
                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
+                       if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) {
+                               if (pixels) {
+                                       // On error, LoadJPGBuff might store a pointer to the error message in pixels
+                                       Sys_Printf( "WARNING: LoadJPGBuff %s %s\n", name, (unsigned char*) pixels );
+                               }                               
+                       } else {
+                               if ( 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( pixels );
                        free( buffer );
                }
        }