Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

574 lines
17 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef IMAGEFORMAT_H
  7. #define IMAGEFORMAT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <stdio.h>
  12. enum NormalDecodeMode_t
  13. {
  14. NORMAL_DECODE_NONE = 0
  15. };
  16. // Forward declaration
  17. #ifdef _WIN32
  18. typedef enum _D3DFORMAT D3DFORMAT;
  19. #endif
  20. //-----------------------------------------------------------------------------
  21. // The various image format types
  22. //-----------------------------------------------------------------------------
  23. // don't bitch that inline functions aren't used!!!!
  24. #pragma warning(disable : 4514)
  25. enum ImageFormat
  26. {
  27. IMAGE_FORMAT_UNKNOWN = -1,
  28. IMAGE_FORMAT_RGBA8888 = 0,
  29. IMAGE_FORMAT_ABGR8888,
  30. IMAGE_FORMAT_RGB888,
  31. IMAGE_FORMAT_BGR888,
  32. IMAGE_FORMAT_RGB565,
  33. IMAGE_FORMAT_I8,
  34. IMAGE_FORMAT_IA88,
  35. IMAGE_FORMAT_P8,
  36. IMAGE_FORMAT_A8,
  37. IMAGE_FORMAT_RGB888_BLUESCREEN,
  38. IMAGE_FORMAT_BGR888_BLUESCREEN,
  39. IMAGE_FORMAT_ARGB8888,
  40. IMAGE_FORMAT_BGRA8888,
  41. IMAGE_FORMAT_DXT1,
  42. IMAGE_FORMAT_DXT3,
  43. IMAGE_FORMAT_DXT5,
  44. IMAGE_FORMAT_BGRX8888,
  45. IMAGE_FORMAT_BGR565,
  46. IMAGE_FORMAT_BGRX5551,
  47. IMAGE_FORMAT_BGRA4444,
  48. IMAGE_FORMAT_DXT1_ONEBITALPHA,
  49. IMAGE_FORMAT_BGRA5551,
  50. IMAGE_FORMAT_UV88,
  51. IMAGE_FORMAT_UVWQ8888,
  52. IMAGE_FORMAT_RGBA16161616F,
  53. IMAGE_FORMAT_RGBA16161616,
  54. IMAGE_FORMAT_UVLX8888,
  55. IMAGE_FORMAT_R32F, // Single-channel 32-bit floating point
  56. IMAGE_FORMAT_RGB323232F,
  57. IMAGE_FORMAT_RGBA32323232F,
  58. // Depth-stencil texture formats for shadow depth mapping
  59. IMAGE_FORMAT_NV_DST16, //
  60. IMAGE_FORMAT_NV_DST24, //
  61. IMAGE_FORMAT_NV_INTZ, // Vendor-specific depth-stencil texture
  62. IMAGE_FORMAT_NV_RAWZ, // formats for shadow depth mapping
  63. IMAGE_FORMAT_ATI_DST16, //
  64. IMAGE_FORMAT_ATI_DST24, //
  65. IMAGE_FORMAT_NV_NULL, // Dummy format which takes no video memory
  66. // Compressed normal map formats
  67. IMAGE_FORMAT_ATI2N, // One-surface ATI2N / DXN format
  68. IMAGE_FORMAT_ATI1N, // Two-surface ATI1N format
  69. #if defined( _X360 )
  70. // Depth-stencil texture formats
  71. IMAGE_FORMAT_X360_DST16,
  72. IMAGE_FORMAT_X360_DST24,
  73. IMAGE_FORMAT_X360_DST24F,
  74. // supporting these specific formats as non-tiled for procedural cpu access
  75. IMAGE_FORMAT_LINEAR_BGRX8888,
  76. IMAGE_FORMAT_LINEAR_RGBA8888,
  77. IMAGE_FORMAT_LINEAR_ABGR8888,
  78. IMAGE_FORMAT_LINEAR_ARGB8888,
  79. IMAGE_FORMAT_LINEAR_BGRA8888,
  80. IMAGE_FORMAT_LINEAR_RGB888,
  81. IMAGE_FORMAT_LINEAR_BGR888,
  82. IMAGE_FORMAT_LINEAR_BGRX5551,
  83. IMAGE_FORMAT_LINEAR_I8,
  84. IMAGE_FORMAT_LINEAR_RGBA16161616,
  85. IMAGE_FORMAT_LE_BGRX8888,
  86. IMAGE_FORMAT_LE_BGRA8888,
  87. #endif
  88. IMAGE_FORMAT_DXT1_RUNTIME,
  89. IMAGE_FORMAT_DXT5_RUNTIME,
  90. NUM_IMAGE_FORMATS
  91. };
  92. #if defined( POSIX ) || defined( DX_TO_GL_ABSTRACTION )
  93. typedef enum _D3DFORMAT
  94. {
  95. D3DFMT_INDEX16,
  96. D3DFMT_D16,
  97. D3DFMT_D24S8,
  98. D3DFMT_A8R8G8B8,
  99. D3DFMT_A4R4G4B4,
  100. D3DFMT_X8R8G8B8,
  101. D3DFMT_R5G6R5,
  102. D3DFMT_X1R5G5B5,
  103. D3DFMT_A1R5G5B5,
  104. D3DFMT_L8,
  105. D3DFMT_A8L8,
  106. D3DFMT_A,
  107. D3DFMT_DXT1,
  108. D3DFMT_DXT3,
  109. D3DFMT_DXT5,
  110. D3DFMT_V8U8,
  111. D3DFMT_Q8W8V8U8,
  112. D3DFMT_X8L8V8U8,
  113. D3DFMT_A16B16G16R16F,
  114. D3DFMT_A16B16G16R16,
  115. D3DFMT_R32F,
  116. D3DFMT_A32B32G32R32F,
  117. D3DFMT_R8G8B8,
  118. D3DFMT_D24X4S4,
  119. D3DFMT_A8,
  120. D3DFMT_R5G6B5,
  121. D3DFMT_D15S1,
  122. D3DFMT_D24X8,
  123. D3DFMT_VERTEXDATA,
  124. D3DFMT_INDEX32,
  125. // adding fake D3D format names for the vendor specific ones (eases debugging/logging)
  126. // NV shadow depth tex
  127. D3DFMT_NV_INTZ = 0x5a544e49, // MAKEFOURCC('I','N','T','Z')
  128. D3DFMT_NV_RAWZ = 0x5a574152, // MAKEFOURCC('R','A','W','Z')
  129. // NV null tex
  130. D3DFMT_NV_NULL = 0x4c4c554e, // MAKEFOURCC('N','U','L','L')
  131. // ATI shadow depth tex
  132. D3DFMT_ATI_D16 = 0x36314644, // MAKEFOURCC('D','F','1','6')
  133. D3DFMT_ATI_D24S8 = 0x34324644, // MAKEFOURCC('D','F','2','4')
  134. // ATI 1N and 2N compressed tex
  135. D3DFMT_ATI_2N = 0x32495441, // MAKEFOURCC('A', 'T', 'I', '2')
  136. D3DFMT_ATI_1N = 0x31495441, // MAKEFOURCC('A', 'T', 'I', '1')
  137. D3DFMT_UNKNOWN
  138. } D3DFORMAT;
  139. #endif
  140. //-----------------------------------------------------------------------------
  141. // Color structures
  142. //-----------------------------------------------------------------------------
  143. struct BGRA8888_t;
  144. struct BGRX8888_t;
  145. struct RGBA8888_t;
  146. struct RGB888_t;
  147. struct BGR888_t;
  148. struct BGR565_t;
  149. struct BGRA5551_t;
  150. struct BGRA4444_t;
  151. struct RGBX5551_t;
  152. struct BGRA8888_t
  153. {
  154. unsigned char b; // change the order of names to change the
  155. unsigned char g; // order of the output ARGB or BGRA, etc...
  156. unsigned char r; // Last one is MSB, 1st is LSB.
  157. unsigned char a;
  158. inline BGRA8888_t& operator=( const BGRA8888_t& in )
  159. {
  160. *( unsigned int * )this = *( unsigned int * )&in;
  161. return *this;
  162. }
  163. };
  164. struct BGRX8888_t
  165. {
  166. unsigned char b; // change the order of names to change the
  167. unsigned char g; // order of the output ARGB or BGRA, etc...
  168. unsigned char r; // Last one is MSB, 1st is LSB.
  169. unsigned char x;
  170. inline BGRX8888_t& operator=( const BGRX8888_t& in )
  171. {
  172. *( unsigned int * )this = *( unsigned int * ) &in;
  173. return *this;
  174. }
  175. };
  176. struct RGBA8888_t
  177. {
  178. unsigned char r; // change the order of names to change the
  179. unsigned char g; // order of the output ARGB or BGRA, etc...
  180. unsigned char b; // Last one is MSB, 1st is LSB.
  181. unsigned char a;
  182. inline RGBA8888_t& operator=( const BGRA8888_t& in );
  183. inline RGBA8888_t& operator=( const RGB888_t& in );
  184. inline RGBA8888_t& operator=( const BGRX8888_t& in );
  185. };
  186. struct RGB888_t
  187. {
  188. unsigned char r;
  189. unsigned char g;
  190. unsigned char b;
  191. inline RGB888_t& operator=( const BGRA8888_t& in )
  192. {
  193. r = in.r;
  194. g = in.g;
  195. b = in.b;
  196. return *this;
  197. }
  198. inline bool operator==( const RGB888_t& in ) const
  199. {
  200. return ( r == in.r ) && ( g == in.g ) && ( b == in.b );
  201. }
  202. inline bool operator!=( const RGB888_t& in ) const
  203. {
  204. return ( r != in.r ) || ( g != in.g ) || ( b != in.b );
  205. }
  206. };
  207. struct BGR888_t
  208. {
  209. unsigned char b;
  210. unsigned char g;
  211. unsigned char r;
  212. inline BGR888_t& operator=( const BGRA8888_t& in )
  213. {
  214. r = in.r;
  215. g = in.g;
  216. b = in.b;
  217. return *this;
  218. }
  219. };
  220. // 360 uses this structure for x86 dxt decoding
  221. #if defined( _X360 )
  222. #pragma bitfield_order( push, lsb_to_msb )
  223. #endif
  224. struct BGR565_t
  225. {
  226. unsigned short b : 5; // order of names changes
  227. unsigned short g : 6; // byte order of output to 32 bit
  228. unsigned short r : 5;
  229. inline BGR565_t& operator=( const BGRA8888_t& in )
  230. {
  231. r = in.r >> 3;
  232. g = in.g >> 2;
  233. b = in.b >> 3;
  234. return *this;
  235. }
  236. inline BGR565_t &Set( int red, int green, int blue )
  237. {
  238. r = red >> 3;
  239. g = green >> 2;
  240. b = blue >> 3;
  241. return *this;
  242. }
  243. };
  244. #if defined( _X360 )
  245. #pragma bitfield_order( pop )
  246. #endif
  247. struct BGRA5551_t
  248. {
  249. unsigned short b : 5; // order of names changes
  250. unsigned short g : 5; // byte order of output to 32 bit
  251. unsigned short r : 5;
  252. unsigned short a : 1;
  253. inline BGRA5551_t& operator=( const BGRA8888_t& in )
  254. {
  255. r = in.r >> 3;
  256. g = in.g >> 3;
  257. b = in.b >> 3;
  258. a = in.a >> 7;
  259. return *this;
  260. }
  261. };
  262. struct BGRA4444_t
  263. {
  264. unsigned short b : 4; // order of names changes
  265. unsigned short g : 4; // byte order of output to 32 bit
  266. unsigned short r : 4;
  267. unsigned short a : 4;
  268. inline BGRA4444_t& operator=( const BGRA8888_t& in )
  269. {
  270. r = in.r >> 4;
  271. g = in.g >> 4;
  272. b = in.b >> 4;
  273. a = in.a >> 4;
  274. return *this;
  275. }
  276. };
  277. struct RGBX5551_t
  278. {
  279. unsigned short r : 5;
  280. unsigned short g : 5;
  281. unsigned short b : 5;
  282. unsigned short x : 1;
  283. inline RGBX5551_t& operator=( const BGRA8888_t& in )
  284. {
  285. r = in.r >> 3;
  286. g = in.g >> 3;
  287. b = in.b >> 3;
  288. return *this;
  289. }
  290. };
  291. //-----------------------------------------------------------------------------
  292. // Conversion assignments
  293. //-----------------------------------------------------------------------------
  294. RGBA8888_t& RGBA8888_t::operator=( const BGRA8888_t& in )
  295. {
  296. r = in.r;
  297. g = in.g;
  298. b = in.b;
  299. a = in.a;
  300. return *this;
  301. }
  302. RGBA8888_t& RGBA8888_t::operator=( const RGB888_t& in )
  303. {
  304. r = in.r;
  305. g = in.g;
  306. b = in.b;
  307. a = 0xFF;
  308. return *this;
  309. }
  310. RGBA8888_t& RGBA8888_t::operator=( const BGRX8888_t& in )
  311. {
  312. r = in.r;
  313. g = in.g;
  314. b = in.b;
  315. a = 0xFF;
  316. return *this;
  317. }
  318. //-----------------------------------------------------------------------------
  319. // some important constants
  320. //-----------------------------------------------------------------------------
  321. #define ARTWORK_GAMMA ( 2.2f )
  322. #define IMAGE_MAX_DIM ( 2048 )
  323. //-----------------------------------------------------------------------------
  324. // information about each image format
  325. //-----------------------------------------------------------------------------
  326. struct ImageFormatInfo_t
  327. {
  328. const char* m_pName;
  329. int m_NumBytes;
  330. int m_NumRedBits;
  331. int m_NumGreeBits;
  332. int m_NumBlueBits;
  333. int m_NumAlphaBits;
  334. bool m_IsCompressed;
  335. };
  336. //-----------------------------------------------------------------------------
  337. // Various methods related to pixelmaps and color formats
  338. //-----------------------------------------------------------------------------
  339. namespace ImageLoader
  340. {
  341. bool GetInfo( const char *fileName, int *width, int *height, enum ImageFormat *imageFormat, float *sourceGamma );
  342. int GetMemRequired( int width, int height, int depth, ImageFormat imageFormat, bool mipmap );
  343. int GetMipMapLevelByteOffset( int width, int height, enum ImageFormat imageFormat, int skipMipLevels );
  344. void GetMipMapLevelDimensions( int *width, int *height, int skipMipLevels );
  345. int GetNumMipMapLevels( int width, int height, int depth = 1 );
  346. bool Load( unsigned char *imageData, const char *fileName, int width, int height, enum ImageFormat imageFormat, float targetGamma, bool mipmap );
  347. bool Load( unsigned char *imageData, FILE *fp, int width, int height,
  348. enum ImageFormat imageFormat, float targetGamma, bool mipmap );
  349. // convert from any image format to any other image format.
  350. // return false if the conversion cannot be performed.
  351. // Strides denote the number of bytes per each line,
  352. // by default assumes width * # of bytes per pixel
  353. bool ConvertImageFormat( const unsigned char *src, enum ImageFormat srcImageFormat,
  354. unsigned char *dst, enum ImageFormat dstImageFormat,
  355. int width, int height, int srcStride = 0, int dstStride = 0 );
  356. // must be used in conjunction with ConvertImageFormat() to pre-swap and post-swap
  357. void PreConvertSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 );
  358. void PostConvertSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 );
  359. void ByteSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 );
  360. bool IsFormatValidForConversion( ImageFormat fmt );
  361. //-----------------------------------------------------------------------------
  362. // convert back and forth from D3D format to ImageFormat, regardless of
  363. // whether it's supported or not
  364. //-----------------------------------------------------------------------------
  365. ImageFormat D3DFormatToImageFormat( D3DFORMAT format );
  366. D3DFORMAT ImageFormatToD3DFormat( ImageFormat format );
  367. // Flags for ResampleRGBA8888
  368. enum
  369. {
  370. RESAMPLE_NORMALMAP = 0x1,
  371. RESAMPLE_ALPHATEST = 0x2,
  372. RESAMPLE_NICE_FILTER = 0x4,
  373. RESAMPLE_CLAMPS = 0x8,
  374. RESAMPLE_CLAMPT = 0x10,
  375. RESAMPLE_CLAMPU = 0x20,
  376. };
  377. struct ResampleInfo_t
  378. {
  379. ResampleInfo_t() : m_nFlags(0), m_flAlphaThreshhold(0.4f), m_flAlphaHiFreqThreshhold(0.4f), m_nSrcDepth(1), m_nDestDepth(1)
  380. {
  381. m_flColorScale[0] = 1.0f, m_flColorScale[1] = 1.0f, m_flColorScale[2] = 1.0f, m_flColorScale[3] = 1.0f;
  382. m_flColorGoal[0] = 0.0f, m_flColorGoal[1] = 0.0f, m_flColorGoal[2] = 0.0f, m_flColorGoal[3] = 0.0f;
  383. }
  384. unsigned char *m_pSrc;
  385. unsigned char *m_pDest;
  386. int m_nSrcWidth;
  387. int m_nSrcHeight;
  388. int m_nSrcDepth;
  389. int m_nDestWidth;
  390. int m_nDestHeight;
  391. int m_nDestDepth;
  392. float m_flSrcGamma;
  393. float m_flDestGamma;
  394. float m_flColorScale[4]; // Color scale factors RGBA
  395. float m_flColorGoal[4]; // Color goal values RGBA DestColor = ColorGoal + scale * (SrcColor - ColorGoal)
  396. float m_flAlphaThreshhold;
  397. float m_flAlphaHiFreqThreshhold;
  398. int m_nFlags;
  399. };
  400. bool ResampleRGBA8888( const ResampleInfo_t &info );
  401. bool ResampleRGBA16161616( const ResampleInfo_t &info );
  402. bool ResampleRGB323232F( const ResampleInfo_t &info );
  403. void ConvertNormalMapRGBA8888ToDUDVMapUVLX8888( const unsigned char *src, int width, int height,
  404. unsigned char *dst_ );
  405. void ConvertNormalMapRGBA8888ToDUDVMapUVWQ8888( const unsigned char *src, int width, int height,
  406. unsigned char *dst_ );
  407. void ConvertNormalMapRGBA8888ToDUDVMapUV88( const unsigned char *src, int width, int height,
  408. unsigned char *dst_ );
  409. void ConvertIA88ImageToNormalMapRGBA8888( const unsigned char *src, int width,
  410. int height, unsigned char *dst,
  411. float bumpScale );
  412. void NormalizeNormalMapRGBA8888( unsigned char *src, int numTexels );
  413. //-----------------------------------------------------------------------------
  414. // Gamma correction
  415. //-----------------------------------------------------------------------------
  416. void GammaCorrectRGBA8888( unsigned char *src, unsigned char* dst,
  417. int width, int height, int depth, float srcGamma, float dstGamma );
  418. //-----------------------------------------------------------------------------
  419. // Makes a gamma table
  420. //-----------------------------------------------------------------------------
  421. void ConstructGammaTable( unsigned char* pTable, float srcGamma, float dstGamma );
  422. //-----------------------------------------------------------------------------
  423. // Gamma corrects using a previously constructed gamma table
  424. //-----------------------------------------------------------------------------
  425. void GammaCorrectRGBA8888( unsigned char* pSrc, unsigned char* pDst,
  426. int width, int height, int depth, unsigned char* pGammaTable );
  427. //-----------------------------------------------------------------------------
  428. // Generates a number of mipmap levels
  429. //-----------------------------------------------------------------------------
  430. void GenerateMipmapLevels( unsigned char* pSrc, unsigned char* pDst, int width,
  431. int height, int depth, ImageFormat imageFormat, float srcGamma, float dstGamma,
  432. int numLevels = 0 );
  433. // Low quality mipmap generation, but way faster.
  434. void GenerateMipmapLevelsLQ( unsigned char* pSrc, unsigned char* pDst, int width, int height,
  435. ImageFormat imageFormat, int numLevels );
  436. //-----------------------------------------------------------------------------
  437. // operations on square images (src and dst can be the same)
  438. //-----------------------------------------------------------------------------
  439. bool RotateImageLeft( const unsigned char *src, unsigned char *dst,
  440. int widthHeight, ImageFormat imageFormat );
  441. bool RotateImage180( const unsigned char *src, unsigned char *dst,
  442. int widthHeight, ImageFormat imageFormat );
  443. bool FlipImageVertically( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 );
  444. bool FlipImageHorizontally( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 );
  445. bool SwapAxes( unsigned char *src,
  446. int widthHeight, ImageFormat imageFormat );
  447. //-----------------------------------------------------------------------------
  448. // Returns info about each image format
  449. //-----------------------------------------------------------------------------
  450. ImageFormatInfo_t const& ImageFormatInfo( ImageFormat fmt );
  451. //-----------------------------------------------------------------------------
  452. // Gets the name of the image format
  453. //-----------------------------------------------------------------------------
  454. inline char const* GetName( ImageFormat fmt )
  455. {
  456. return ImageFormatInfo(fmt).m_pName;
  457. }
  458. //-----------------------------------------------------------------------------
  459. // Gets the size of the image format in bytes
  460. //-----------------------------------------------------------------------------
  461. inline int SizeInBytes( ImageFormat fmt )
  462. {
  463. return ImageFormatInfo(fmt).m_NumBytes;
  464. }
  465. //-----------------------------------------------------------------------------
  466. // Does the image format support transparency?
  467. //-----------------------------------------------------------------------------
  468. inline bool IsTransparent( ImageFormat fmt )
  469. {
  470. return ImageFormatInfo(fmt).m_NumAlphaBits > 0;
  471. }
  472. //-----------------------------------------------------------------------------
  473. // Is the image format compressed?
  474. //-----------------------------------------------------------------------------
  475. inline bool IsCompressed( ImageFormat fmt )
  476. {
  477. return ImageFormatInfo(fmt).m_IsCompressed;
  478. }
  479. //-----------------------------------------------------------------------------
  480. // Is any channel > 8 bits?
  481. //-----------------------------------------------------------------------------
  482. inline bool HasChannelLargerThan8Bits( ImageFormat fmt )
  483. {
  484. ImageFormatInfo_t info = ImageFormatInfo(fmt);
  485. return ( info.m_NumRedBits > 8 || info.m_NumGreeBits > 8 || info.m_NumBlueBits > 8 || info.m_NumAlphaBits > 8 );
  486. }
  487. inline bool IsRuntimeCompressed( ImageFormat fmt )
  488. {
  489. return ( fmt == IMAGE_FORMAT_DXT1_RUNTIME ) || ( fmt == IMAGE_FORMAT_DXT5_RUNTIME );
  490. }
  491. } // end namespace ImageLoader
  492. #endif // IMAGEFORMAT_H