texture loading
[convexer.git] / nbvtf / nbvtf.h
index aa82cbf6a302823416a011f99a956e89e0f43383..5db305b812d8620e8db6270ee3ce4727f82d4360 100644 (file)
@@ -57,10 +57,12 @@ extern "C" {
  
  #define NBVTF_SHOW_STDERR
  #define STB_IMAGE_IMPLEMENTATION
+ #define STB_IMAGE_WRITE_IMPLEMENTATION
 #endif
 
 #define STBI_NO_THREAD_LOCALS
 #include "stb/stb_image.h"
+#include "stb/stb_image_write.h"
 
 #ifdef USE_LIBRGBCX
  // #define RGBCX_NO_ALGORITHM
@@ -88,40 +90,39 @@ extern "C" {
 
 typedef enum EImageFormat
 {
-       // Name                                                                 // Supported?
+   /* Format                           Export   Import */
        k_EImageFormat_NONE = -1,
-       k_EImageFormat_RGBA8888 = 0,            // YES
-       k_EImageFormat_ABGR8888, 
-       k_EImageFormat_RGB888,                          // YES
-       k_EImageFormat_BGR888,
-       k_EImageFormat_RGB565,
-       k_EImageFormat_I8,
-       k_EImageFormat_IA88,
-       k_EImageFormat_P8,
-       k_EImageFormat_A8,
-       k_EImageFormat_RGB888_BLUESCREEN,
-       k_EImageFormat_BGR888_BLUESCREEN,
-       k_EImageFormat_ARGB8888,
-       k_EImageFormat_BGRA8888,
-       k_EImageFormat_DXT1,                                    // YES
-       k_EImageFormat_DXT3,
-       k_EImageFormat_DXT5,                                    // YES
-       k_EImageFormat_BGRX8888,
-       k_EImageFormat_BGR565,
-       k_EImageFormat_BGRX5551,
-       k_EImageFormat_BGRA4444,
-       k_EImageFormat_DXT1_ONEBITALPHA,
-       k_EImageFormat_BGRA5551,
-       k_EImageFormat_UV88,
-       k_EImageFormat_UVWQ8888,
-       k_EImageFormat_RGBA16161616F,
-       k_EImageFormat_RGBA16161616,
-       k_EImageFormat_UVLX8888
+       k_EImageFormat_RGBA8888 = 0,       // -        yes
+       k_EImageFormat_ABGR8888,         // yes      yes
+       k_EImageFormat_RGB888,                          // -        yes
+       k_EImageFormat_BGR888,           // yes      yes
+       k_EImageFormat_RGB565,           // -        planned
+       k_EImageFormat_I8,               // -        planned
+       k_EImageFormat_IA88,             // -        planned
+       k_EImageFormat_P8,               // -        -
+       k_EImageFormat_A8,               // -        -
+       k_EImageFormat_RGB888_BLUESCREEN,// -        -
+       k_EImageFormat_BGR888_BLUESCREEN,// -        -
+       k_EImageFormat_ARGB8888,         // -        yes
+       k_EImageFormat_BGRA8888,         // -        yes
+       k_EImageFormat_DXT1,                                    // yes      yes
+       k_EImageFormat_DXT3,             // -        -
+       k_EImageFormat_DXT5,                                    // yes      yes
+       k_EImageFormat_BGRX8888,         // -        -
+       k_EImageFormat_BGR565,           // -        planned
+       k_EImageFormat_BGRX5551,         // -        -
+       k_EImageFormat_BGRA4444,         // -        -
+       k_EImageFormat_DXT1_ONEBITALPHA, // -        planned
+       k_EImageFormat_BGRA5551,         // -        -
+       k_EImageFormat_UV88,             // -        -
+       k_EImageFormat_UVWQ8888,         // -        -
+       k_EImageFormat_RGBA16161616F,    // -        -
+       k_EImageFormat_RGBA16161616,     // -        -
+       k_EImageFormat_UVLX8888          // -        -
 } EImageFormat_t;
 
 const char *vtf_format_strings[] = 
 {
-       // Name                                                                 // Supported?
        "RGBA8888",
        "ABGR8888",
        "RGB888",
@@ -564,6 +565,29 @@ void nbvtf_dxt_block( uint8_t *dest, uint8_t *src, int alpha, int qual )
 #endif
 }
 
+void nbvtf_dxt_block_unpack( uint8_t *src, uint8_t *dest, int alpha )
+{
+#ifdef USE_LIBRGBCX
+       if( alpha )
+       {
+               rgbcx__unpack_bc3( src, dest );
+       }
+       else
+       {
+               rgbcx__unpack_bc1( src, dest );
+       }
+#endif
+
+#if USE_STB_DXT
+   stb_decompress_dxt_block( src, dest, alpha );
+#endif
+
+#ifndef HAS_DXT_COMPRESSOR
+   for( int i=0; i<alpha? 128: 64; i++ )
+      dest[i] = i%1? 0xff: 0x00;
+#endif
+}
+
 void nbvtf_compress_dxt( uint8_t *src, int w, int h, int alpha, int qual,
       uint8_t *dest )
 {
@@ -623,7 +647,49 @@ void nbvtf_compress_dxt( uint8_t *src, int w, int h, int alpha, int qual,
        }
 }
 
-void nbvtf_swizzle_to( uint8_t *src, int n, int nc, uint8_t *dest )
+/* Decompress block to rgba, padding currently doesn't get read */
+void nbvtf_decompress_dxt(uint8_t *src, int w, int h, int alpha, uint8_t *dest )
+{
+       uint32_t blocks_x, blocks_y;
+
+       blocks_x = ((uint32_t)w) >> 2;
+       blocks_y = ((uint32_t)h) >> 2;
+       
+       int padx = w % 4 != 0? 1: 0;
+       int pady = h % 4 != 0? 1: 0;
+       
+       int block_size = alpha? 16: 8;
+       
+       uint8_t *dxt_block = src;
+       uint8_t working_block[ 4*4*4 ];
+       
+       // Compress rows
+       for( int y = 0; y < blocks_y; y ++ )
+       {
+               for( int x = 0; x < blocks_x; x ++ )
+               {
+         nbvtf_dxt_block_unpack( dxt_block, working_block, alpha );
+
+                       uint8_t *dest_begin = dest + (y*w*4 + x*4)*4;
+                       for( int i = 0; i < 4; i ++ )
+                               memcpy( dest_begin + w*4*i, working_block + i*4*4, 4*4 );
+                       
+                       dxt_block += block_size;
+               }
+               
+               if( padx )
+                       dxt_block += block_size;
+       }
+       
+       if( pady )
+               for( int x = 0; x < blocks_x; x ++ )
+                       dxt_block += block_size;
+       
+       if( padx && pady )
+       {
+       }
+}
+static void nbvtf_swizzle_to( uint8_t *src, int n, int nc, uint8_t *dest )
 {
        for( int i = 0; i < n; i ++ )
        {
@@ -634,7 +700,7 @@ void nbvtf_swizzle_to( uint8_t *src, int n, int nc, uint8_t *dest )
        }
 }
 
-void nbvtf_write_img_data( uint8_t *src, int w, int h, 
+static void nbvtf_write_img_data( uint8_t *src, int w, int h, 
       EImageFormat_t format, int qual, uint8_t *wb, FILE *file )
 {
        switch( format )
@@ -661,7 +727,119 @@ void nbvtf_write_img_data( uint8_t *src, int w, int h,
        }
 }
 
+static size_t nbvtf_img_size( int w, int h, EImageFormat_t format )
+{
+   int block_count = nbvtf__max(1, ((w + 3) / 4)) * 
+                     nbvtf__max(1, ((h + 3) / 4));
 
+   switch( format )
+   {
+      case k_EImageFormat_RGBA8888:
+      case k_EImageFormat_ABGR8888:
+      case k_EImageFormat_ARGB8888:
+      case k_EImageFormat_BGRA8888:
+         return 4*w*h;
+
+      case k_EImageFormat_RGB888:
+      case k_EImageFormat_BGR888:
+         return 3*w*h;
+
+      case k_EImageFormat_RGB565:
+      case k_EImageFormat_IA88:
+         return 2*w*h;
+
+      case k_EImageFormat_I8:
+         return w*h;
+      
+      case k_EImageFormat_DXT1:
+         return block_count * BLOCK_SIZE_DXT1;
+
+      case k_EImageFormat_DXT5:
+         return block_count * BLOCK_SIZE_DXT5;
+
+      default:
+         break;
+   }
+}
+
+static void nbvtf_read_img_data( uint8_t *src, int w, int h, 
+      EImageFormat_t format, uint8_t *dst )
+{
+   switch( format )
+   {
+      case k_EImageFormat_RGBA8888:
+         for( int i=0; i<w*h*4; i++ )
+            dst[i] = src[i];
+         break;
+
+      case k_EImageFormat_ABGR8888:
+         for( int i=0; i<w*h; i++ )
+         {
+            dst[i*4+0] = src[i*4+3];
+            dst[i*4+1] = src[i*4+2];
+            dst[i*4+2] = src[i*4+1];
+            dst[i*4+3] = src[i*4+0];
+         }
+         break;
+
+      case k_EImageFormat_RGB888:
+         for( int i=0; i<w*h; i++ )
+         {
+            dst[i*4+0] = src[i*3+0];
+            dst[i*4+1] = src[i*3+1];
+            dst[i*4+2] = src[i*3+2];
+            dst[i*4+3] = 0xFF;
+         }
+         break;
+
+      case k_EImageFormat_BGR888:
+         for( int i=0; i<w*h; i++ )
+         {
+            dst[i*4+0] = src[i*3+2];
+            dst[i*4+1] = src[i*3+1];
+            dst[i*4+2] = src[i*3+0];
+            dst[i*4+3] = 0xFF;
+         }
+         break;
+
+      case k_EImageFormat_RGB565:
+      case k_EImageFormat_I8:
+      case k_EImageFormat_IA88:
+         /* Planned */
+         break;
+      
+      case k_EImageFormat_ARGB8888:
+         for( int i=0; i<w*h; i++ )
+         {
+            dst[i*4+0] = src[i*4+1];
+            dst[i*4+1] = src[i*4+2];
+            dst[i*4+2] = src[i*4+3];
+            dst[i*4+3] = src[i*4+0];
+         }
+         break;
+
+      case k_EImageFormat_BGRA8888:
+         for( int i=0; i<w*h; i++ )
+         {
+            dst[i*4+0] = src[i*4+2];
+            dst[i*4+1] = src[i*4+1];
+            dst[i*4+2] = src[i*4+0];
+            dst[i*4+3] = src[i*4+3];
+         }
+         break;
+      
+      case k_EImageFormat_DXT1:
+         nbvtf_decompress_dxt( src, w, h, 0, dst );
+         break;
+
+      case k_EImageFormat_DXT5:
+         nbvtf_decompress_dxt( src, w, h, 1, dst );
+         break;
+
+      default:
+         break;
+   }
+}
 
 #ifdef NBVTF_AS_SO
 __attribute__((visibility("default")))
@@ -709,6 +887,17 @@ int nbvtf_write_dds_dxt1( uint8_t *reference, int w, int h, int qual, const char
    return 1;
 }
 
+static void nbvtf_correct_normal( uint8_t *src, uint8_t *dst, int w, int h )
+{
+   for( int i=0; i < w*h; i++ )
+   {
+      dst[i*4+0] = src[i*4+0];
+      dst[i*4+1] = 0xFF-src[i*4+1];
+      dst[i*4+2] = src[i*4+2];
+      dst[i*4+3] = src[i*4+3];
+   }
+}
+
 #ifdef NBVTF_AS_SO
 __attribute__((visibility("default")))
 #endif
@@ -730,13 +919,7 @@ int nbvtf_write( uint8_t *reference, int w, int h, int mipmap,
    if( usr_flags & TEXTUREFLAGS_NORMAL )
    {
       src = malloc( w*h*4 );
-      for( int i = 0; i < w*h; i ++ )
-      {
-         src[i*4+0] = reference[i*4+0];
-         src[i*4+1] = 0xFF-reference[i*4+1];
-         src[i*4+2] = reference[i*4+2];
-         src[i*4+3] = reference[i*4+3];
-      }
+      nbvtf_correct_normal( reference, src, w, h );
    }
    else
       src = reference;
@@ -880,6 +1063,84 @@ int nbvtf_write( uint8_t *reference, int w, int h, int mipmap,
        return 1;
 }
 
+#ifdef NBVTF_AS_SO
+__attribute__((visibility("default")))
+#endif
+uint8_t *nbvtf_read( vtfheader_t *header, int32_t *w, int32_t *h, int normal )
+{
+   *w = header->width;
+   *h = header->height;
+
+   uint8_t *rgba = malloc( header->width * header->height * 4 );
+   
+   if( !rgba )
+      return NULL;
+   
+       size_t memory = 0;
+   int x = header->width,
+       y = header->height;
+
+   int mip_iter = 0;
+       
+       while( nbvtf_lower( &x, &y ) )
+   {
+      if( mip_iter == header->mipmapCount )
+         break;
+
+      mip_iter ++;
+               memory += nbvtf_img_size( x, y, header->highResImageFormat );
+   }
+
+       if( header->lowResImageWidth == 0 || header->lowResImageHeight == 0 ||
+         header->lowResImageFormat == 0xffffffff )
+   {
+      /* no thumbnail? */
+   }
+   else
+      memory += nbvtf_img_size( header->lowResImageWidth,
+                                header->lowResImageHeight,
+                                header->lowResImageFormat );
+
+   /* Decode high res image into rgba */
+   nbvtf_read_img_data( ((uint8_t *)header) + header->headerSize + memory,
+                        header->width, header->height,
+                        header->highResImageFormat,
+                        rgba );
+
+   if( normal )
+   {
+      nbvtf_correct_normal( rgba, rgba, header->width, header->height );
+      stbi_write_png( "/tmp/cxr_hello_n.png", header->width, header->height,
+            4, rgba, header->width*4 );
+   }
+   else
+      stbi_write_png( "/tmp/cxr_hello.png", header->width, header->height,
+            4, rgba, header->width*4 );
+
+
+   return rgba;
+}
+
+#ifdef NBVTF_AS_SO
+__attribute__((visibility("default")))
+#endif
+uint8_t *nbvtf_raw_data( vtfheader_t *header, int32_t *w, int32_t *h, int32_t *format )
+{
+   *w = header->width;
+   *h = header->height;
+   *format = header->highResImageFormat;
+
+   return ((uint8_t *)header) + header->headerSize;
+}
+
+#ifdef NBVTF_AS_SO
+__attribute__((visibility("default")))
+#endif
+void nbvtf_free( uint8_t *data )
+{
+   free(data);
+}
+
 #ifdef NBVTF_AS_SO
 __attribute__((visibility("default")))
 #endif