main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include "DXTCompressor.h"
  5. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  6. bool LoadTGAFromFile( const char* pFilename, void **image, int* width, int* height )
  7. {
  8. typedef struct
  9. {
  10. char identsize;
  11. char colourmaptype;
  12. char imagetype;
  13. unsigned short colourmapstart;
  14. unsigned short colourmaplength;
  15. char colourmapbits;
  16. unsigned short xstart;
  17. unsigned short ystart;
  18. unsigned short width;
  19. unsigned short height;
  20. char bits;
  21. char descriptor;
  22. } TGA_HEADER;
  23. // Open the file
  24. FILE* pic;
  25. if((pic=fopen( pFilename, "rb"))==NULL )
  26. {
  27. return false;
  28. }
  29. // Zero out the header
  30. TGA_HEADER TGAheader;
  31. memset(&TGAheader,0,sizeof(TGA_HEADER));
  32. // Read the header
  33. fread(&TGAheader.identsize,sizeof(char),1,pic);
  34. fread(&TGAheader.colourmaptype,sizeof(char),1,pic);
  35. fread(&TGAheader.imagetype,sizeof(char),1,pic);
  36. fread(&TGAheader.colourmapstart,sizeof(unsigned short),1,pic);
  37. fread(&TGAheader.colourmaplength,sizeof(unsigned short),1,pic);
  38. fread(&TGAheader.colourmapbits,sizeof(char),1,pic);
  39. fread(&TGAheader.xstart,sizeof(unsigned short),1,pic);
  40. fread(&TGAheader.ystart,sizeof(unsigned short),1,pic);
  41. fread(&TGAheader.width,sizeof(unsigned short),1,pic);
  42. fread(&TGAheader.height,sizeof(unsigned short),1,pic);
  43. fread(&TGAheader.bits,sizeof(char),1,pic);
  44. fread(&TGAheader.descriptor,sizeof(char),1,pic);
  45. *width = TGAheader.width;
  46. *height = TGAheader.height;
  47. int DataSize = TGAheader.width*TGAheader.height*4;
  48. // Read the pixels
  49. *image = new char[DataSize];
  50. if ((TGAheader.descriptor>>5) & 1)
  51. {
  52. // Right side up
  53. fread(*image, sizeof(char),DataSize, pic);
  54. }
  55. else
  56. {
  57. //Upside down
  58. for (int row=TGAheader.height-1; row >=0; row--)
  59. {
  60. fread(((char*) (*image))+row*TGAheader.width*TGAheader.bits/8, TGAheader.bits/8, TGAheader.width, pic);
  61. }
  62. }
  63. // Close the file
  64. fclose(pic);
  65. // TGA is stored on disk BGRA
  66. // Endian swap bits so that the image is actually in RGBA format
  67. if( TGAheader.bits == 32 )
  68. {
  69. unsigned char* pRunner = (unsigned char*)*image;
  70. for( int i = 0; i < DataSize; i+=4 )
  71. {
  72. char color[4] =
  73. {
  74. pRunner[ 0 ],
  75. pRunner[ 1 ],
  76. pRunner[ 2 ],
  77. pRunner[ 3 ],
  78. };
  79. pRunner[ 0 ] = color[ 2 ];
  80. pRunner[ 1 ] = color[ 1 ];
  81. pRunner[ 2 ] = color[ 0 ];
  82. pRunner[ 3 ] = color[ 3 ];
  83. pRunner += 4;
  84. }
  85. }
  86. return true;
  87. }
  88. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  89. int main( int argc, const char* argv[] )
  90. {
  91. // Initialize the compressor
  92. DXTCompressor::Initialize();
  93. // Load sample .tga
  94. void* pSourceData;
  95. int w, h;
  96. //bool bFileLoaded = LoadTGAFromFile( "1600x1200.tga", &pSourceData, &w, &h );
  97. bool bFileLoaded = LoadTGAFromFile( "320x200.tga", &pSourceData, &w, &h );
  98. if( bFileLoaded )
  99. {
  100. // Test performance
  101. // const int numIterations = 100;
  102. // for( int i = 0; i < numIterations; i++ )
  103. // {
  104. // // Compress the data
  105. // void* pOutputData;
  106. // int outputLength;
  107. // bool bCompressSuccess = DXTCompressor::CompressRGBAImageData( DXT1, pSourceData, w, h, &pOutputData, &outputLength, false );
  108. //
  109. // // Clean up
  110. // delete [] pOutputData;
  111. // pOutputData = NULL;
  112. // }
  113. // Print total stats
  114. // printf( "\n\n****Total stats on %d iterations****\n", numIterations );
  115. // DXTCompressor::PrintPerformanceLog();
  116. // Now test saving to DDS memory file
  117. {
  118. // Compress the data
  119. // void* pCompressedOutput;
  120. // int compressedOutputLength;
  121. // bool bCompressSuccess = DXTCompressor::CompressRGBAImageData( DXT1, pSourceData, w, h, &pCompressedOutput, &compressedOutputLength, false );
  122. char *outputData;
  123. int bufferSize = DXTCompressor::GetBufferSize(DXT1,
  124. w,
  125. h);
  126. int ddsHeaderSize = DXTCompressor::GetDDSHeaderSize();
  127. outputData = (char*) malloc(bufferSize + ddsHeaderSize );
  128. bool bCompressSuccess = DXTCompressor::CompressRGBAImageData(
  129. DXT1,
  130. pSourceData,
  131. w,
  132. h,
  133. outputData+ddsHeaderSize, false );
  134. if( bCompressSuccess )
  135. {
  136. // Save DDS file
  137. // void* pOutputDDSFile;
  138. // int outputDDSFileLength;
  139. // DXTCompressor::WriteDDSMemoryFile( DXT1, w, h, pCompressedOutput, compressedOutputLength, &pOutputDDSFile, &outputDDSFileLength );
  140. // Clean up
  141. // delete [] pCompressedOutput;
  142. // pCompressedOutput = NULL;
  143. // delete [] pOutputDDSFile;
  144. // pOutputDDSFile = NULL;
  145. DXTCompressor::WriteDDSHeader(DXT1,
  146. w,
  147. h,
  148. bufferSize,
  149. outputData);
  150. FILE *fp = fopen("DXTCompressorTGAtoDDS.dds", "wb");
  151. fwrite(outputData,1,bufferSize + ddsHeaderSize,fp);
  152. fclose(fp);
  153. free(outputData);
  154. }
  155. }
  156. }
  157. // Shutdown the compressor
  158. DXTCompressor::Shutdown();
  159. return 0;
  160. }
粤ICP备19079148号