Browse Source

Cleaned up image decompression code.

Lasse Öörni 13 years ago
parent
commit
109f76600a
4 changed files with 39 additions and 40 deletions
  1. 10 33
      Engine/Resource/Decompress.cpp
  2. 6 2
      Engine/Resource/Decompress.h
  3. 21 3
      Engine/Resource/Image.cpp
  4. 2 2
      Engine/Resource/Image.h

+ 10 - 33
Engine/Resource/Decompress.cpp

@@ -398,7 +398,7 @@ static void DecompressETC(unsigned char* pDestData, const void* pSrcData)
     }
     }
 }
 }
 
 
-static void DecompressImageETC( unsigned char* rgba, const void* blocks, int width, int height )
+void DecompressImageETC( unsigned char* rgba, const void* blocks, int width, int height )
 {
 {
     // initialise the block input
     // initialise the block input
     unsigned char const* sourceBlock = reinterpret_cast< unsigned char const* >( blocks );
     unsigned char const* sourceBlock = reinterpret_cast< unsigned char const* >( blocks );
@@ -792,9 +792,9 @@ static unsigned TwiddleUV(unsigned YSize, unsigned XSize, unsigned YPos, unsigne
     return Twiddled;
     return Twiddled;
 }
 }
 
 
-static void DecompressImagePVRTC(unsigned char* dest, void *src, const int XDim, const int YDim, CompressedFormat format)
+void DecompressImagePVRTC(unsigned char* dest, const void *blocks, int width, int height, CompressedFormat format)
 {
 {
-    AMTC_BLOCK_STRUCT* pCompressedData = (AMTC_BLOCK_STRUCT*)src;
+    AMTC_BLOCK_STRUCT* pCompressedData = (AMTC_BLOCK_STRUCT*)blocks;
     int AssumeImageTiles = 1;
     int AssumeImageTiles = 1;
     int Do2bitMode = format == CF_PVRTC_RGB_2BPP || format == CF_PVRTC_RGBA_2BPP;
     int Do2bitMode = format == CF_PVRTC_RGB_2BPP || format == CF_PVRTC_RGBA_2BPP;
     
     
@@ -840,22 +840,22 @@ static void DecompressImagePVRTC(unsigned char* dest, void *src, const int XDim,
     }
     }
     
     
     // For MBX don't allow the sizes to get too small
     // For MBX don't allow the sizes to get too small
-    BlkXDim = _MAX(2, XDim / XBlockSize);
-    BlkYDim = _MAX(2, YDim / BLK_Y_SIZE);
+    BlkXDim = _MAX(2, width / XBlockSize);
+    BlkYDim = _MAX(2, height / BLK_Y_SIZE);
     
     
     // Step through the pixels of the image decompressing each one in turn
     // Step through the pixels of the image decompressing each one in turn
     //
     //
     // Note that this is a hideously inefficient way to do this!
     // Note that this is a hideously inefficient way to do this!
-    for(y = 0; y < YDim; y++)
+    for(y = 0; y < height; y++)
     {
     {
-        for(x = 0; x < XDim; x++)
+        for(x = 0; x < width; x++)
         {
         {
             // Map this pixel to the top left neighbourhood of blocks
             // Map this pixel to the top left neighbourhood of blocks
             BlkX = (x - XBlockSize/2);
             BlkX = (x - XBlockSize/2);
             BlkY = (y - BLK_Y_SIZE/2);
             BlkY = (y - BLK_Y_SIZE/2);
             
             
-            BlkX = LIMIT_COORD(BlkX, XDim, AssumeImageTiles);
-            BlkY = LIMIT_COORD(BlkY, YDim, AssumeImageTiles);
+            BlkX = LIMIT_COORD(BlkX, width, AssumeImageTiles);
+            BlkY = LIMIT_COORD(BlkY, height, AssumeImageTiles);
             
             
             BlkX /= XBlockSize;
             BlkX /= XBlockSize;
             BlkY /= BLK_Y_SIZE;
             BlkY /= BLK_Y_SIZE;
@@ -928,7 +928,7 @@ static void DecompressImagePVRTC(unsigned char* dest, void *src, const int XDim,
             }
             }
             
             
             // Store the result in the output image
             // Store the result in the output image
-            uPosition = (x+y*XDim)<<2;
+            uPosition = (x+y*width)<<2;
             dest[uPosition+0] = (unsigned char)Result[0];
             dest[uPosition+0] = (unsigned char)Result[0];
             dest[uPosition+1] = (unsigned char)Result[1];
             dest[uPosition+1] = (unsigned char)Result[1];
             dest[uPosition+2] = (unsigned char)Result[2];
             dest[uPosition+2] = (unsigned char)Result[2];
@@ -936,26 +936,3 @@ static void DecompressImagePVRTC(unsigned char* dest, void *src, const int XDim,
         }
         }
     }
     }
 }
 }
-
-void DecompressImage(unsigned char* dest, unsigned char* src, unsigned width, unsigned height, CompressedFormat format)
-{
-    switch (format)
-    {
-    case CF_DXT1:
-    case CF_DXT3:
-    case CF_DXT5:
-        DecompressImageDXT(dest, src, width, height, format);
-        break;
-        
-    case CF_ETC1:
-        DecompressImageETC(dest, src, width, height);
-        break;
-        
-    case CF_PVRTC_RGB_2BPP:
-    case CF_PVRTC_RGBA_2BPP:
-    case CF_PVRTC_RGB_4BPP:
-    case CF_PVRTC_RGBA_4BPP:
-        DecompressImagePVRTC(dest, src, width, height, format);
-        break;
-    }
-}

+ 6 - 2
Engine/Resource/Decompress.h

@@ -25,5 +25,9 @@
 
 
 #include "Image.h"
 #include "Image.h"
 
 
-/// Decompress a compressed image format to RGBA.
-void DecompressImage(unsigned char* dest, unsigned char* src, unsigned width, unsigned height, CompressedFormat format);
+/// Decompress a DXT compressed image to RGBA.
+void DecompressImageDXT(unsigned char* dest, const void* blocks, int width, int height, CompressedFormat format);
+/// Decompress an ETC1 compressed image to RGBA.
+void DecompressImageETC(unsigned char* dest, const void* blocks, int width, int height);
+/// Decompress a PVRTC compressed image to RGBA.
+void DecompressImagePVRTC(unsigned char* dest, const void* blocks, int width, int height, CompressedFormat format);

+ 21 - 3
Engine/Resource/Image.cpp

@@ -161,11 +161,29 @@ bool CompressedLevel::Decompress(unsigned char* dest)
 {
 {
     if (!data_)
     if (!data_)
         return false;
         return false;
-    else
+    
+    switch (format_)
     {
     {
-        DecompressImage(dest, data_, width_, height_, compressedFormat_);
+    case CF_DXT1:
+    case CF_DXT3:
+    case CF_DXT5:
+        DecompressImageDXT(dest, data_, width_, height_, format_);
+        return true;
+        
+    case CF_ETC1:
+        DecompressImageETC(dest, data_, width_, height_);
+        return true;
+        
+    case CF_PVRTC_RGB_2BPP:
+    case CF_PVRTC_RGBA_2BPP:
+    case CF_PVRTC_RGB_4BPP:
+    case CF_PVRTC_RGBA_4BPP:
+        DecompressImagePVRTC(dest, data_, width_, height_, format_);
         return true;
         return true;
     }
     }
+    
+    // Unknown format
+    return false;
 }
 }
 
 
 OBJECTTYPESTATIC(Image);
 OBJECTTYPESTATIC(Image);
@@ -694,7 +712,7 @@ CompressedLevel Image::GetCompressedLevel(unsigned index) const
         return level;
         return level;
     }
     }
     
     
-    level.compressedFormat_ = compressedFormat_;
+    level.format_ = compressedFormat_;
     level.width_ = width_;
     level.width_ = width_;
     level.height_ = height_;
     level.height_ = height_;
     
     

+ 2 - 2
Engine/Resource/Image.h

@@ -55,13 +55,13 @@ struct CompressedLevel
     {
     {
     }
     }
     
     
-    /// Decompress. The destination buffer required is width * height * 4 bytes. Return true if successful.
+    /// Decompress to RGBA. The destination buffer required is width * height * 4 bytes. Return true if successful.
     bool Decompress(unsigned char* dest);
     bool Decompress(unsigned char* dest);
     
     
     /// Compressed image data.
     /// Compressed image data.
     unsigned char* data_;
     unsigned char* data_;
     /// Compression format.
     /// Compression format.
-    CompressedFormat compressedFormat_;
+    CompressedFormat format_;
     /// Width.
     /// Width.
     int width_;
     int width_;
     /// Height.
     /// Height.