BinaryImage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. /*
  23. ================================================================================================
  24. idBinaryImage
  25. ================================================================================================
  26. */
  27. #include "tr_local.h"
  28. #include "dxt/DXTCodec.h"
  29. #include "color/ColorSpace.h"
  30. idCVar image_highQualityCompression( "image_highQualityCompression", "0", CVAR_BOOL, "Use high quality (slow) compression" );
  31. /*
  32. ========================
  33. idBinaryImage::Load2DFromMemory
  34. ========================
  35. */
  36. void idBinaryImage::Load2DFromMemory( int width, int height, const byte * pic_const, int numLevels, textureFormat_t & textureFormat, textureColor_t & colorFormat, bool gammaMips ) {
  37. fileData.textureType = TT_2D;
  38. fileData.format = textureFormat;
  39. fileData.colorFormat = colorFormat;
  40. fileData.width = width;
  41. fileData.height = height;
  42. fileData.numLevels = numLevels;
  43. byte * pic = (byte *)Mem_Alloc( width * height * 4, TAG_TEMP );
  44. memcpy( pic, pic_const, width * height * 4 );
  45. if ( colorFormat == CFM_YCOCG_DXT5 ) {
  46. // convert the image data to YCoCg and use the YCoCgDXT5 compressor
  47. idColorSpace::ConvertRGBToCoCg_Y( pic, pic, width, height );
  48. } else if ( colorFormat == CFM_NORMAL_DXT5 ) {
  49. // Blah, HQ swizzles automatically, Fast doesn't
  50. if ( !image_highQualityCompression.GetBool() ) {
  51. for ( int i = 0; i < width * height; i++ ) {
  52. pic[i*4+3] = pic[i*4+0];
  53. pic[i*4+0] = 0;
  54. pic[i*4+2] = 0;
  55. }
  56. }
  57. } else if ( colorFormat == CFM_GREEN_ALPHA ) {
  58. for ( int i = 0; i < width * height; i++ ) {
  59. pic[i*4+1] = pic[i*4+3];
  60. pic[i*4+0] = 0;
  61. pic[i*4+2] = 0;
  62. pic[i*4+3] = 0;
  63. }
  64. }
  65. int scaledWidth = width;
  66. int scaledHeight = height;
  67. images.SetNum( numLevels );
  68. for ( int level = 0; level < images.Num(); level++ ) {
  69. idBinaryImageData &img = images[ level ];
  70. // Images that are going to be DXT compressed and aren't multiples of 4 need to be
  71. // padded out before compressing.
  72. byte * dxtPic = pic;
  73. int dxtWidth = 0;
  74. int dxtHeight = 0;
  75. if ( textureFormat == FMT_DXT5 || textureFormat == FMT_DXT1 ) {
  76. if ( ( scaledWidth & 3 ) || ( scaledHeight & 3 ) ) {
  77. dxtWidth = ( scaledWidth + 3 ) & ~3;
  78. dxtHeight = ( scaledHeight + 3 ) & ~3;
  79. dxtPic = (byte *)Mem_ClearedAlloc( dxtWidth*4*dxtHeight, TAG_IMAGE );
  80. for ( int i = 0; i < scaledHeight; i++ ) {
  81. memcpy( dxtPic + i*dxtWidth*4, pic + i*scaledWidth*4, scaledWidth*4 );
  82. }
  83. } else {
  84. dxtPic = pic;
  85. dxtWidth = scaledWidth;
  86. dxtHeight = scaledHeight;
  87. }
  88. }
  89. img.level = level;
  90. img.destZ = 0;
  91. img.width = scaledWidth;
  92. img.height = scaledHeight;
  93. // compress data or convert floats as necessary
  94. if ( textureFormat == FMT_DXT1 ) {
  95. idDxtEncoder dxt;
  96. img.Alloc( dxtWidth * dxtHeight / 2 );
  97. if ( image_highQualityCompression.GetBool() ) {
  98. dxt.CompressImageDXT1HQ( dxtPic, img.data, dxtWidth, dxtHeight );
  99. } else {
  100. dxt.CompressImageDXT1Fast( dxtPic, img.data, dxtWidth, dxtHeight );
  101. }
  102. } else if ( textureFormat == FMT_DXT5 ) {
  103. idDxtEncoder dxt;
  104. img.Alloc( dxtWidth * dxtHeight );
  105. if ( colorFormat == CFM_NORMAL_DXT5 ) {
  106. if ( image_highQualityCompression.GetBool() ) {
  107. dxt.CompressNormalMapDXT5HQ( dxtPic, img.data, dxtWidth, dxtHeight );
  108. } else {
  109. dxt.CompressNormalMapDXT5Fast( dxtPic, img.data, dxtWidth, dxtHeight );
  110. }
  111. } else if ( colorFormat == CFM_YCOCG_DXT5 ) {
  112. if ( image_highQualityCompression.GetBool() ) {
  113. dxt.CompressYCoCgDXT5HQ( dxtPic, img.data, dxtWidth, dxtHeight );
  114. } else {
  115. dxt.CompressYCoCgDXT5Fast( dxtPic, img.data, dxtWidth, dxtHeight );
  116. }
  117. } else {
  118. fileData.colorFormat = colorFormat = CFM_DEFAULT;
  119. if ( image_highQualityCompression.GetBool() ) {
  120. dxt.CompressImageDXT5HQ( dxtPic, img.data, dxtWidth, dxtHeight );
  121. } else {
  122. dxt.CompressImageDXT5Fast( dxtPic, img.data, dxtWidth, dxtHeight );
  123. }
  124. }
  125. } else if ( textureFormat == FMT_LUM8 || textureFormat == FMT_INT8 ) {
  126. // LUM8 and INT8 just read the red channel
  127. img.Alloc( scaledWidth * scaledHeight );
  128. for ( int i = 0; i < img.dataSize; i++ ) {
  129. img.data[ i ] = pic[ i * 4 ];
  130. }
  131. } else if ( textureFormat == FMT_ALPHA ) {
  132. // ALPHA reads the alpha channel
  133. img.Alloc( scaledWidth * scaledHeight );
  134. for ( int i = 0; i < img.dataSize; i++ ) {
  135. img.data[ i ] = pic[ i * 4 + 3 ];
  136. }
  137. } else if ( textureFormat == FMT_L8A8 ) {
  138. // L8A8 reads the alpha and red channels
  139. img.Alloc( scaledWidth * scaledHeight * 2 );
  140. for ( int i = 0; i < img.dataSize / 2; i++ ) {
  141. img.data[ i * 2 + 0 ] = pic[ i * 4 + 0 ];
  142. img.data[ i * 2 + 1 ] = pic[ i * 4 + 3 ];
  143. }
  144. } else if ( textureFormat == FMT_RGB565 ) {
  145. img.Alloc( scaledWidth * scaledHeight * 2 );
  146. for ( int i = 0; i < img.dataSize / 2; i++ ) {
  147. unsigned short color = ( ( pic[ i * 4 + 0 ] >> 3 ) << 11 ) | ( ( pic[ i * 4 + 1 ] >> 2 ) << 5 ) | ( pic[ i * 4 + 2 ] >> 3 );
  148. img.data[ i * 2 + 0 ] = ( color >> 8 ) & 0xFF;
  149. img.data[ i * 2 + 1 ] = color & 0xFF;
  150. }
  151. } else {
  152. fileData.format = textureFormat = FMT_RGBA8;
  153. img.Alloc( scaledWidth * scaledHeight * 4 );
  154. for ( int i = 0; i < img.dataSize; i++ ) {
  155. img.data[ i ] = pic[ i ];
  156. }
  157. }
  158. // if we had to pad to quads, free the padded version
  159. if ( pic != dxtPic ) {
  160. Mem_Free( dxtPic );
  161. dxtPic = NULL;
  162. }
  163. // downsample for the next level
  164. byte * shrunk = NULL;
  165. if ( gammaMips ) {
  166. shrunk = R_MipMapWithGamma( pic, scaledWidth, scaledHeight );
  167. } else {
  168. shrunk = R_MipMap( pic, scaledWidth, scaledHeight );
  169. }
  170. Mem_Free( pic );
  171. pic = shrunk;
  172. scaledWidth = Max( 1, scaledWidth >> 1 );
  173. scaledHeight = Max( 1, scaledHeight >> 1 );
  174. }
  175. Mem_Free( pic );
  176. }
  177. /*
  178. ========================
  179. PadImageTo4x4
  180. DXT Compression requres a complete 4x4 block, even if the GPU will only be sampling
  181. a subset of it, so pad to 4x4 with replicated texels to maximize compression.
  182. ========================
  183. */
  184. static void PadImageTo4x4( const byte *src, int width, int height, byte dest[64] ) {
  185. // we probably will need to support this for non-square images, but I'll address
  186. // that when needed
  187. assert( width <= 4 && height <= 4 );
  188. assert( width > 0 && height > 0 );
  189. for ( int y = 0 ; y < 4 ; y++ ) {
  190. int sy = y % height;
  191. for ( int x = 0 ; x < 4 ; x++ ) {
  192. int sx = x % width;
  193. for ( int c = 0 ; c < 4 ; c++ ) {
  194. dest[(y*4+x)*4+c] = src[(sy*width+sx)*4+c];
  195. }
  196. }
  197. }
  198. }
  199. /*
  200. ========================
  201. idBinaryImage::LoadCubeFromMemory
  202. ========================
  203. */
  204. void idBinaryImage::LoadCubeFromMemory( int width, const byte * pics[6], int numLevels, textureFormat_t & textureFormat, bool gammaMips ) {
  205. fileData.textureType = TT_CUBIC;
  206. fileData.format = textureFormat;
  207. fileData.colorFormat = CFM_DEFAULT;
  208. fileData.height = fileData.width = width;
  209. fileData.numLevels = numLevels;
  210. images.SetNum( fileData.numLevels * 6 );
  211. for ( int side = 0; side < 6; side++ ) {
  212. const byte *orig = pics[side];
  213. const byte *pic = orig;
  214. int scaledWidth = fileData.width;
  215. for ( int level = 0; level < fileData.numLevels; level++ ) {
  216. // compress data or convert floats as necessary
  217. idBinaryImageData &img = images[ level * 6 + side ];
  218. // handle padding blocks less than 4x4 for the DXT compressors
  219. ALIGN16( byte padBlock[64] );
  220. int padSize;
  221. const byte *padSrc;
  222. if ( scaledWidth < 4 && ( textureFormat == FMT_DXT1 || textureFormat == FMT_DXT5 ) ) {
  223. PadImageTo4x4( pic, scaledWidth, scaledWidth, padBlock );
  224. padSize = 4;
  225. padSrc = padBlock;
  226. } else {
  227. padSize = scaledWidth;
  228. padSrc = pic;
  229. }
  230. img.level = level;
  231. img.destZ = side;
  232. img.width = padSize;
  233. img.height = padSize;
  234. if ( textureFormat == FMT_DXT1 ) {
  235. img.Alloc( padSize * padSize / 2 );
  236. idDxtEncoder dxt;
  237. dxt.CompressImageDXT1Fast( padSrc, img.data, padSize, padSize );
  238. } else if ( textureFormat == FMT_DXT5 ) {
  239. img.Alloc( padSize * padSize );
  240. idDxtEncoder dxt;
  241. dxt.CompressImageDXT5Fast( padSrc, img.data, padSize, padSize );
  242. } else {
  243. fileData.format = textureFormat = FMT_RGBA8;
  244. img.Alloc( padSize * padSize * 4 );
  245. memcpy( img.data, pic, img.dataSize );
  246. }
  247. // downsample for the next level
  248. byte * shrunk = NULL;
  249. if ( gammaMips ) {
  250. shrunk = R_MipMapWithGamma( pic, scaledWidth, scaledWidth );
  251. } else {
  252. shrunk = R_MipMap( pic, scaledWidth, scaledWidth );
  253. }
  254. if ( pic != orig ) {
  255. Mem_Free( (void *)pic );
  256. pic = NULL;
  257. }
  258. pic = shrunk;
  259. scaledWidth = Max( 1, scaledWidth >> 1 );
  260. }
  261. if ( pic != orig ) {
  262. // free the down sampled version
  263. Mem_Free( (void *)pic );
  264. pic = NULL;
  265. }
  266. }
  267. }
  268. /*
  269. ========================
  270. idBinaryImage::WriteGeneratedFile
  271. ========================
  272. */
  273. ID_TIME_T idBinaryImage::WriteGeneratedFile( ID_TIME_T sourceFileTime ) {
  274. idStr binaryFileName;
  275. MakeGeneratedFileName( binaryFileName );
  276. idFileLocal file( fileSystem->OpenFileWrite( binaryFileName, "fs_basepath" ) );
  277. if ( file == NULL ) {
  278. idLib::Warning( "idBinaryImage: Could not open file '%s'", binaryFileName.c_str() );
  279. return FILE_NOT_FOUND_TIMESTAMP;
  280. }
  281. idLib::Printf( "Writing %s\n", binaryFileName.c_str() );
  282. fileData.headerMagic = BIMAGE_MAGIC;
  283. fileData.sourceFileTime = sourceFileTime;
  284. file->WriteBig( fileData.sourceFileTime );
  285. file->WriteBig( fileData.headerMagic );
  286. file->WriteBig( fileData.textureType );
  287. file->WriteBig( fileData.format );
  288. file->WriteBig( fileData.colorFormat );
  289. file->WriteBig( fileData.width );
  290. file->WriteBig( fileData.height );
  291. file->WriteBig( fileData.numLevels );
  292. for ( int i = 0; i < images.Num(); i++ ) {
  293. idBinaryImageData &img = images[ i ];
  294. file->WriteBig( img.level );
  295. file->WriteBig( img.destZ );
  296. file->WriteBig( img.width );
  297. file->WriteBig( img.height );
  298. file->WriteBig( img.dataSize );
  299. file->Write( img.data, img.dataSize );
  300. }
  301. return file->Timestamp();
  302. }
  303. /*
  304. ==========================
  305. idBinaryImage::LoadFromGeneratedFile
  306. Load the preprocessed image from the generated folder.
  307. ==========================
  308. */
  309. ID_TIME_T idBinaryImage::LoadFromGeneratedFile( ID_TIME_T sourceFileTime ) {
  310. idStr binaryFileName;
  311. MakeGeneratedFileName( binaryFileName );
  312. idFileLocal bFile = fileSystem->OpenFileRead( binaryFileName );
  313. if ( bFile == NULL ) {
  314. return FILE_NOT_FOUND_TIMESTAMP;
  315. }
  316. if ( LoadFromGeneratedFile( bFile, sourceFileTime ) ) {
  317. return bFile->Timestamp();
  318. }
  319. return FILE_NOT_FOUND_TIMESTAMP;
  320. }
  321. /*
  322. ==========================
  323. idBinaryImage::LoadFromGeneratedFile
  324. Load the preprocessed image from the generated folder.
  325. ==========================
  326. */
  327. bool idBinaryImage::LoadFromGeneratedFile( idFile * bFile, ID_TIME_T sourceFileTime ) {
  328. if ( bFile->Read( &fileData, sizeof( fileData ) ) <= 0 ) {
  329. return false;
  330. }
  331. idSwapClass<bimageFile_t> swap;
  332. swap.Big( fileData.sourceFileTime );
  333. swap.Big( fileData.headerMagic );
  334. swap.Big( fileData.textureType );
  335. swap.Big( fileData.format );
  336. swap.Big( fileData.colorFormat );
  337. swap.Big( fileData.width );
  338. swap.Big( fileData.height );
  339. swap.Big( fileData.numLevels );
  340. if ( BIMAGE_MAGIC != fileData.headerMagic ) {
  341. return false;
  342. }
  343. if ( fileData.sourceFileTime != sourceFileTime && !fileSystem->InProductionMode() ) {
  344. return false;
  345. }
  346. int numImages = fileData.numLevels;
  347. if ( fileData.textureType == TT_CUBIC ) {
  348. numImages *= 6;
  349. }
  350. images.SetNum( numImages );
  351. for ( int i = 0; i < numImages; i++ ) {
  352. idBinaryImageData &img = images[ i ];
  353. if ( bFile->Read( &img, sizeof( bimageImage_t ) ) <= 0 ) {
  354. return false;
  355. }
  356. idSwapClass<bimageImage_t> swap;
  357. swap.Big( img.level );
  358. swap.Big( img.destZ );
  359. swap.Big( img.width );
  360. swap.Big( img.height );
  361. swap.Big( img.dataSize );
  362. assert( img.level >= 0 && img.level < fileData.numLevels );
  363. assert( img.destZ == 0 || fileData.textureType == TT_CUBIC );
  364. assert( img.dataSize > 0 );
  365. // DXT images need to be padded to 4x4 block sizes, but the original image
  366. // sizes are still retained, so the stored data size may be larger than
  367. // just the multiplication of dimensions
  368. assert( img.dataSize >= img.width * img.height * BitsForFormat( (textureFormat_t)fileData.format ) / 8 );
  369. img.Alloc( img.dataSize );
  370. if ( img.data == NULL ) {
  371. return false;
  372. }
  373. if ( bFile->Read( img.data, img.dataSize ) <= 0 ) {
  374. return false;
  375. }
  376. }
  377. return true;
  378. }
  379. /*
  380. ==========================
  381. idBinaryImage::MakeGeneratedFileName
  382. ==========================
  383. */
  384. void idBinaryImage::MakeGeneratedFileName( idStr & gfn ) {
  385. GetGeneratedFileName( gfn, GetName() );
  386. }
  387. /*
  388. ==========================
  389. idBinaryImage::GetGeneratedFileName
  390. ==========================
  391. */
  392. void idBinaryImage::GetGeneratedFileName( idStr & gfn, const char *name ) {
  393. gfn.Format( "generated/images/%s.bimage", name );
  394. gfn.Replace( "(", "/" );
  395. gfn.Replace( ",", "/" );
  396. gfn.Replace( ")", "" );
  397. gfn.Replace( " ", "" );
  398. }