]> git.xonotic.org Git - xonotic/netradiant.git/commitdiff
Merge branch 'master' of ssh://icculus.org/netradiant
authorRudolf Polzer <divVerent@xonotic.org>
Wed, 23 Feb 2011 13:19:58 +0000 (14:19 +0100)
committerRudolf Polzer <divVerent@xonotic.org>
Wed, 23 Feb 2011 13:19:58 +0000 (14:19 +0100)
libs/jpeg6/jpgload.cpp
tools/quake3/q3map2/image.c

index 323b073a877fddda501c8b3c131e81a34ebbdd1a..fa3523ed47deae8726ee076559e851cdab68251b 100644 (file)
@@ -51,6 +51,7 @@ GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic,
   unsigned char *out, *bbuf;
   int nSize;
   int jmpret;
+  int i;
 
   // Rad additions: initialize the longjmp buffer
   jmpret = setjmp( rad_loadfailed );
@@ -98,18 +99,12 @@ GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic,
    * with the stdio data source.
    */
   
-  /* ydnar: radiant only handles RGB, non-progressive format jpegs */
-  if( cinfo.output_components != 4 )
+  if( cinfo.output_components != 1 && cinfo.output_components != 3 && cinfo.output_components != 4 )
   {
-    *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-RGB JPEG encountered (unsupported)"));
+    *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-Y/RGB/RGBA JPEG encountered (unsupported)"));
     return -1;
   }
-  if( cinfo.progressive_mode )
-  {
-    *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Progressive JPEG encountered (unsupported)"));
-    return -1;
-  }
-  
+
   /* We may need to do some setup of our own at this point before reading
    * the data.  After jpeg_start_decompress() we have the correct scaled
    * output image dimensions available, as well as the output colormap
@@ -119,7 +114,7 @@ GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic,
   
   /* JSAMPLEs per row in output buffer */
   row_stride = cinfo.output_width * cinfo.output_components;
-  nSize = cinfo.output_width*cinfo.output_height*cinfo.output_components;
+  nSize = cinfo.output_width*cinfo.output_height*4;
   
   out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
   memset( out, 255, nSize + 1 );
@@ -142,20 +137,39 @@ GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic,
      */
        bbuf = out + row_stride * cinfo.output_scanline;
        buffer = &bbuf;
-    (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
-  }
-
-  // clear all the alphas to 255
-  {
-    int i, j;
-    unsigned char *buf;
-
-    buf = *pic;
-
-    j = cinfo.output_width * cinfo.output_height * 4;
-    for ( i = 3 ; i < j ; i+=4 ) {
-      buf[i] = 255;
-    }
+       (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
+
+       // we convert downwards to not overwrite values we want to read later
+       switch(cinfo.output_components)
+       {
+               case 4:
+                       // clear all the alphas to 255
+                       for(i = cinfo.output_width; i-- > 0; )
+                       {
+                               bbuf[i*4+3] = 255;
+                       }
+                       break;
+               case 3:
+                       // expand 3 to 4
+                       for(i = cinfo.output_width; i-- > 0; )
+                       {
+                               bbuf[i*4+3] = 255;
+                               bbuf[i*4+2] = bbuf[i*3+2];
+                               bbuf[i*4+1] = bbuf[i*3+1];
+                               bbuf[i*4+0] = bbuf[i*3+0];
+                       }
+                       break;
+               case 1:
+                       // expand 1 to 4
+                       for(i = cinfo.output_width; i-- > 0; )
+                       {
+                               bbuf[i*4+3] = 255;
+                               bbuf[i*4+2] = bbuf[i];
+                               bbuf[i*4+1] = bbuf[i];
+                               bbuf[i*4+0] = bbuf[i];
+                       }
+                       break;
+       }
   }
 
   /* Step 7: Finish decompression */
index 0289e77c826bf662c45d95ec4b12e0952515e0f1..929b15d96cf5ec6c5e256955c16d0b40f0d9fdda 100644 (file)
@@ -346,6 +346,7 @@ image_t *ImageLoad( const char *filename )
        char            name[ 1024 ];
        int                     size;
        byte            *buffer = NULL;
+       qboolean        alphaHack = qfalse;
 
        
        /* init */
@@ -410,6 +411,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 );
+                               alphaHack = qtrue;
                        }
                        else
                        {
@@ -460,6 +462,28 @@ image_t *ImageLoad( const char *filename )
        /* set count */
        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;