Update to CMake, tweaks & dds
[convexer.git] / nbvtf / nbvtf.h
index 111ba9e2f301a07d6c0874bedeeae5b2cff8c27f..6be4714ed99a26c0d340898bce37986c9abc4851 100644 (file)
@@ -235,6 +235,76 @@ typedef struct vtfheader
 
 #pragma pack(pop)
 
+#pragma pack(push, 1)
+struct DDS_PIXELFORMAT
+{
+       uint32_t dwSize;
+       uint32_t dwFlags;
+       uint32_t dwFourCC;
+       uint32_t dwRGBBitCount;
+       uint32_t dwRBitMask;
+       uint32_t dwGBitMask;
+       uint32_t dwBBitMask;
+       uint32_t dwABitMask;
+};
+
+struct DDS_HEADER {
+       uint32_t           dwSize;
+       uint32_t           dwFlags;
+       uint32_t           dwHeight;
+       uint32_t           dwWidth;
+       uint32_t           dwPitchOrLinearSize;
+       uint32_t           dwDepth;
+       uint32_t           dwMipMapCount;
+       uint32_t           dwReserved1[11];
+       struct DDS_PIXELFORMAT  ddspf;
+       uint32_t           dwCaps;
+       uint32_t           dwCaps2;
+       uint32_t           dwCaps3;
+       uint32_t           dwCaps4;
+       uint32_t           dwReserved2;
+};
+
+#pragma pack(pop)
+
+uint32_t swap_endian(uint32_t val)
+{
+       return (val << 24) | ((val << 8) & 0x00ff0000) |
+                  ((val >> 8) & 0x0000ff00) | (val >> 24);
+}
+
+#define DDSD_CAPS 0x1
+#define DDSD_HEIGHT 0x2
+#define DDSD_WIDTH 0x4
+#define DDSD_PITCH 0x8
+#define DDSD_PIXELFORMAT 0x1000
+#define DDSD_MIPMAPCOUNT 0x20000
+#define DDSD_LINEARSIZE 0x80000
+#define DDSD_DEPTH 0x800000
+
+#define DDPF_ALPHAPIXELS 0x1
+#define DDPF_ALPHA 0x2
+#define DDPF_FOURCC 0x4
+#define DDPF_RGB 0x40
+#define DDPF_YUV 0x200
+#define DDPF_LUMINANCE 0x20000
+
+#define DDSCAPS_COMPLEX 0x8
+#define DDSCAPS_MIPMAP 0x400000
+#define DDSCAPS_TEXTURE 0x1000
+
+#define BLOCK_SIZE_DXT1 8
+#define BLOCK_SIZE_DXT5 16
+
+#define BBP_RGB888 24
+#define BBP_RGBA8888 32
+
+#define DDS_HEADER_SIZE 124
+#define DDS_HEADER_PFSIZE 32
+#define DDS_MAGICNUM 0x20534444;
+
+#define DDS_FLIP_VERTICALLY_ON_WRITE
+
 typedef struct mipimg
 {
        uint32_t w;
@@ -573,6 +643,54 @@ void nbvtf_write_img_data( uint8_t *src, int w, int h,
        }
 }
 
+
+
+#ifdef NBVTF_AS_SO
+__attribute__((visibility("default")))
+#endif
+int nbvtf_write_dds_dxt1( uint8_t *reference, int w, int h, int qual, const char *dest )
+{
+       if( !nbvtf_power2x(w,h) )
+       {
+               NBVTF_ERR( "nbvtf_write:err image dimentions were not power of two (%d %d)\n", w, h );
+               return 0;
+       }
+
+       struct DDS_HEADER header = {0};
+       header.dwSize = DDS_HEADER_SIZE;
+       header.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
+       header.dwHeight = h;
+       header.dwWidth = w;
+   header.dwPitchOrLinearSize = nbvtf__max(1, ((w + 3) / 4)) * BLOCK_SIZE_DXT1;
+       header.ddspf.dwSize = DDS_HEADER_PFSIZE;
+   header.ddspf.dwFlags |= DDPF_FOURCC;
+   header.ddspf.dwFourCC = ((uint32_t)'D'<<0) |
+                           ((uint32_t)'X'<<8) |
+                           ((uint32_t)'T'<<16) | 
+                           ((uint32_t)'1'<<24);
+
+   header.dwFlags |= DDSD_LINEARSIZE;
+       header.dwMipMapCount = 0;
+       header.dwCaps = DDSCAPS_TEXTURE;
+
+       // Magic number
+       uint32_t magic = DDS_MAGICNUM;
+   
+   FILE *file = fopen( dest, "wb" );
+   fwrite( &magic, sizeof(uint32_t), 1, file );
+   fwrite( &header, DDS_HEADER_SIZE, 1, file );
+
+       uint32_t size_highres = nbvtf_sizeimg( w, h, k_EImageFormat_DXT1 );
+       uint8_t *working_buffer = malloc( size_highres );
+
+   nbvtf_compress_dxt( reference, w, h, 0, qual, working_buffer );
+   fwrite( working_buffer, size_highres, 1, file );
+
+   free( working_buffer );
+   fclose( file );
+   return 1;
+}
+
 #ifdef NBVTF_AS_SO
 __attribute__((visibility("default")))
 #endif