Advertisement
Guest User

Untitled

a guest
Jan 4th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.75 KB | None | 0 0
  1. //===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8.  
  9. #ifndef IMAGEFORMAT_H
  10. #define IMAGEFORMAT_H
  11.  
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15.  
  16.  
  17. #include <stdio.h>
  18.  
  19. //-----------------------------------------------------------------------------
  20. // The various image format types
  21. //-----------------------------------------------------------------------------
  22.  
  23.  
  24. // don't bitch that inline functions aren't used!!!!
  25. #pragma warning(disable : 4514)
  26.  
  27. enum ImageFormat
  28. {
  29. IMAGE_FORMAT_UNKNOWN = -1,
  30. IMAGE_FORMAT_RGBA8888 = 0,
  31. IMAGE_FORMAT_ABGR8888,
  32. IMAGE_FORMAT_RGB888,
  33. IMAGE_FORMAT_BGR888,
  34. IMAGE_FORMAT_RGB565,
  35. IMAGE_FORMAT_I8,
  36. IMAGE_FORMAT_IA88,
  37. IMAGE_FORMAT_P8,
  38. IMAGE_FORMAT_A8,
  39. IMAGE_FORMAT_RGB888_BLUESCREEN,
  40. IMAGE_FORMAT_BGR888_BLUESCREEN,
  41. IMAGE_FORMAT_ARGB8888,
  42. IMAGE_FORMAT_BGRA8888,
  43. IMAGE_FORMAT_DXT1,
  44. IMAGE_FORMAT_DXT3,
  45. IMAGE_FORMAT_DXT5,
  46. IMAGE_FORMAT_BGRX8888,
  47. IMAGE_FORMAT_BGR565,
  48. IMAGE_FORMAT_BGRX5551,
  49. IMAGE_FORMAT_BGRA4444,
  50. IMAGE_FORMAT_DXT1_ONEBITALPHA,
  51. IMAGE_FORMAT_BGRA5551,
  52. IMAGE_FORMAT_UV88,
  53. IMAGE_FORMAT_UVWQ8888,
  54. IMAGE_FORMAT_RGBA16161616F,
  55. // GR - HDR
  56. IMAGE_FORMAT_RGBA16161616,
  57. IMAGE_FORMAT_UVLX8888,
  58. IMAGE_FORMAT_R32F, // Single-channel 32-bit floating point
  59. IMAGE_FORMAT_RGB323232F,
  60. IMAGE_FORMAT_RGBA32323232F,
  61.  
  62. #ifdef _XBOX
  63. // adds support for these linear formats
  64. // non-linear uncompressed formats are swizzled
  65. IMAGE_FORMAT_LINEAR_BGRX8888,
  66. IMAGE_FORMAT_LINEAR_RGBA8888,
  67. IMAGE_FORMAT_LINEAR_ABGR8888,
  68. IMAGE_FORMAT_LINEAR_ARGB8888,
  69. IMAGE_FORMAT_LINEAR_BGRA8888,
  70. IMAGE_FORMAT_LINEAR_RGB888,
  71. IMAGE_FORMAT_LINEAR_BGR888,
  72. IMAGE_FORMAT_LINEAR_BGRX5551,
  73. IMAGE_FORMAT_LINEAR_I8,
  74. #endif
  75.  
  76. NUM_IMAGE_FORMATS
  77. };
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Color structures
  82. //-----------------------------------------------------------------------------
  83. struct BGRA8888_t
  84. {
  85. unsigned char b; // change the order of names to change the
  86. unsigned char g; // order of the output ARGB or BGRA, etc...
  87. unsigned char r; // Last one is MSB, 1st is LSB.
  88. unsigned char a;
  89. inline BGRA8888_t& operator=( const BGRA8888_t& in )
  90. {
  91. *( unsigned int * )this = *( unsigned int * )&in;
  92. return *this;
  93. }
  94. };
  95.  
  96. struct RGBA8888_t
  97. {
  98. unsigned char r; // change the order of names to change the
  99. unsigned char g; // order of the output ARGB or BGRA, etc...
  100. unsigned char b; // Last one is MSB, 1st is LSB.
  101. unsigned char a;
  102. inline RGBA8888_t& operator=( const BGRA8888_t& in )
  103. {
  104. r = in.r;
  105. g = in.g;
  106. b = in.b;
  107. a = in.a;
  108. return *this;
  109. }
  110. };
  111.  
  112. struct RGB888_t
  113. {
  114. unsigned char r;
  115. unsigned char g;
  116. unsigned char b;
  117. inline RGB888_t& operator=( const BGRA8888_t& in )
  118. {
  119. r = in.r;
  120. g = in.g;
  121. b = in.b;
  122. return *this;
  123. }
  124. inline bool operator==( const RGB888_t& in ) const
  125. {
  126. return ( r == in.r ) && ( g == in.g ) && ( b == in.b );
  127. }
  128. inline bool operator!=( const RGB888_t& in ) const
  129. {
  130. return ( r != in.r ) || ( g != in.g ) || ( b != in.b );
  131. }
  132. };
  133.  
  134. struct BGR888_t
  135. {
  136. unsigned char b;
  137. unsigned char g;
  138. unsigned char r;
  139. inline BGR888_t& operator=( const BGRA8888_t& in )
  140. {
  141. r = in.r;
  142. g = in.g;
  143. b = in.b;
  144. return *this;
  145. }
  146. };
  147.  
  148. struct BGR565_t
  149. {
  150. unsigned short b : 5; // order of names changes
  151. unsigned short g : 6; // byte order of output to 32 bit
  152. unsigned short r : 5;
  153. inline BGR565_t& operator=( const BGRA8888_t& in )
  154. {
  155. r = in.r >> 3;
  156. g = in.g >> 2;
  157. b = in.b >> 3;
  158. return *this;
  159. }
  160. };
  161.  
  162. struct BGRA5551_t
  163. {
  164. unsigned short b : 5; // order of names changes
  165. unsigned short g : 5; // byte order of output to 32 bit
  166. unsigned short r : 5;
  167. unsigned short a : 1;
  168. inline BGRA5551_t& operator=( const BGRA8888_t& in )
  169. {
  170. r = in.r >> 3;
  171. g = in.g >> 3;
  172. b = in.b >> 3;
  173. a = in.a >> 7;
  174. return *this;
  175. }
  176. };
  177.  
  178. struct BGRA4444_t
  179. {
  180. unsigned short b : 4; // order of names changes
  181. unsigned short g : 4; // byte order of output to 32 bit
  182. unsigned short r : 4;
  183. unsigned short a : 4;
  184. inline BGRA4444_t& operator=( const BGRA8888_t& in )
  185. {
  186. r = in.r >> 4;
  187. g = in.g >> 4;
  188. b = in.b >> 4;
  189. a = in.a >> 4;
  190. return *this;
  191. }
  192. };
  193.  
  194. struct RGBX5551_t
  195. {
  196. unsigned short r : 5;
  197. unsigned short g : 5;
  198. unsigned short b : 5;
  199. unsigned short x : 1;
  200. inline RGBX5551_t& operator=( const BGRA8888_t& in )
  201. {
  202. r = in.r >> 3;
  203. g = in.g >> 3;
  204. b = in.b >> 3;
  205. return *this;
  206. }
  207. };
  208.  
  209.  
  210.  
  211. //-----------------------------------------------------------------------------
  212. // some important constants
  213. //-----------------------------------------------------------------------------
  214. #define ARTWORK_GAMMA ( 2.2f )
  215. #define IMAGE_MAX_DIM ( 2048 )
  216.  
  217.  
  218. //-----------------------------------------------------------------------------
  219. // information about each image format
  220. //-----------------------------------------------------------------------------
  221. struct ImageFormatInfo_t
  222. {
  223. char* m_pName;
  224. int m_NumBytes;
  225. int m_NumRedBits;
  226. int m_NumGreeBits;
  227. int m_NumBlueBits;
  228. int m_NumAlphaBits;
  229. bool m_IsCompressed;
  230. };
  231.  
  232.  
  233. //-----------------------------------------------------------------------------
  234. // Various methods related to pixelmaps and color formats
  235. //-----------------------------------------------------------------------------
  236. namespace ImageLoader
  237. {
  238.  
  239. bool GetInfo( const char *fileName, int *width, int *height, enum ImageFormat *imageFormat, float *sourceGamma );
  240. int GetMemRequired( int width, int height, int depth, ImageFormat imageFormat, bool mipmap );
  241. int GetMipMapLevelByteOffset( int width, int height, enum ImageFormat imageFormat, int skipMipLevels );
  242. void GetMipMapLevelDimensions( int *width, int *height, int skipMipLevels );
  243. int GetNumMipMapLevels( int width, int height, int depth = 1 );
  244. bool Load( unsigned char *imageData, const char *fileName, int width, int height, enum ImageFormat imageFormat, float targetGamma, bool mipmap );
  245. bool Load( unsigned char *imageData, FILE *fp, int width, int height,
  246. enum ImageFormat imageFormat, float targetGamma, bool mipmap );
  247.  
  248. // convert from any image format to any other image format.
  249. // return false if the conversion cannot be performed.
  250. // Strides denote the number of bytes per each line,
  251. // by default assumes width * # of bytes per pixel
  252. bool ConvertImageFormat( unsigned char *src, enum ImageFormat srcImageFormat,
  253. unsigned char *dst, enum ImageFormat dstImageFormat,
  254. int width, int height, int srcStride = 0, int dstStride = 0 );
  255.  
  256. // Flags for ResampleRGBA8888
  257. enum
  258. {
  259. RESAMPLE_NORMALMAP = 0x1,
  260. RESAMPLE_ALPHATEST = 0x2,
  261. RESAMPLE_NICE_FILTER = 0x4,
  262. RESAMPLE_CLAMPS = 0x8,
  263. RESAMPLE_CLAMPT = 0x10,
  264. RESAMPLE_CLAMPU = 0x20,
  265. };
  266.  
  267. struct ResampleInfo_t
  268. {
  269. ResampleInfo_t() : m_flColorScale( 1.0f ), m_nFlags(0), m_flAlphaThreshhold(0.4f),
  270. m_flAlphaHiFreqThreshhold(0.4f), m_nSrcDepth(1), m_nDestDepth(1) {}
  271.  
  272. unsigned char *m_pSrc;
  273. unsigned char *m_pDest;
  274. int m_nSrcWidth;
  275. int m_nSrcHeight;
  276. int m_nSrcDepth;
  277. int m_nDestWidth;
  278. int m_nDestHeight;
  279. int m_nDestDepth;
  280. float m_flSrcGamma;
  281. float m_flDestGamma;
  282. float m_flColorScale;
  283. float m_flAlphaThreshhold;
  284. float m_flAlphaHiFreqThreshhold;
  285. int m_nFlags;
  286. };
  287.  
  288. bool ResampleRGBA8888( const ResampleInfo_t &info );
  289. bool ResampleRGBA16161616( const ResampleInfo_t &info );
  290. bool ResampleRGB323232F( const ResampleInfo_t &info );
  291.  
  292. void ConvertNormalMapRGBA8888ToDUDVMapUVLX8888( unsigned char *src, int width, int height,
  293. unsigned char *dst_ );
  294. void ConvertNormalMapRGBA8888ToDUDVMapUVWQ8888( unsigned char *src, int width, int height,
  295. unsigned char *dst_ );
  296. void ConvertNormalMapRGBA8888ToDUDVMapUV88( unsigned char *src, int width, int height,
  297. unsigned char *dst_ );
  298.  
  299. void ConvertIA88ImageToNormalMapRGBA8888( unsigned char *src, int width,
  300. int height, unsigned char *dst,
  301. float bumpScale );
  302.  
  303. void NormalizeNormalMapRGBA8888( unsigned char *src, int numTexels );
  304.  
  305.  
  306. //-----------------------------------------------------------------------------
  307. // Gamma correction
  308. //-----------------------------------------------------------------------------
  309. void GammaCorrectRGBA8888( unsigned char *src, unsigned char* dst,
  310. int width, int height, int depth, float srcGamma, float dstGamma );
  311.  
  312.  
  313. //-----------------------------------------------------------------------------
  314. // Makes a gamma table
  315. //-----------------------------------------------------------------------------
  316. void ConstructGammaTable( unsigned char* pTable, float srcGamma, float dstGamma );
  317.  
  318.  
  319. //-----------------------------------------------------------------------------
  320. // Gamma corrects using a previously constructed gamma table
  321. //-----------------------------------------------------------------------------
  322. void GammaCorrectRGBA8888( unsigned char* pSrc, unsigned char* pDst,
  323. int width, int height, int depth, unsigned char* pGammaTable );
  324.  
  325.  
  326. //-----------------------------------------------------------------------------
  327. // Generates a number of mipmap levels
  328. //-----------------------------------------------------------------------------
  329. void GenerateMipmapLevels( unsigned char* pSrc, unsigned char* pDst, int width,
  330. int height, int depth, ImageFormat imageFormat, float srcGamma, float dstGamma,
  331. int numLevels = 0 );
  332.  
  333.  
  334. //-----------------------------------------------------------------------------
  335. // operations on square images (src and dst can be the same)
  336. //-----------------------------------------------------------------------------
  337. bool RotateImageLeft( unsigned char *src, unsigned char *dst,
  338. int widthHeight, ImageFormat imageFormat );
  339. bool RotateImage180( unsigned char *src, unsigned char *dst,
  340. int widthHeight, ImageFormat imageFormat );
  341. bool FlipImageVertically( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 );
  342. bool FlipImageHorizontally( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 );
  343. bool SwapAxes( unsigned char *src,
  344. int widthHeight, ImageFormat imageFormat );
  345.  
  346.  
  347. //-----------------------------------------------------------------------------
  348. // Returns info about each image format
  349. //-----------------------------------------------------------------------------
  350. ImageFormatInfo_t const& ImageFormatInfo( ImageFormat fmt );
  351.  
  352.  
  353. //-----------------------------------------------------------------------------
  354. // Gets the name of the image format
  355. //-----------------------------------------------------------------------------
  356. inline char const* GetName( ImageFormat fmt )
  357. {
  358. return ImageFormatInfo(fmt).m_pName;
  359. }
  360.  
  361.  
  362. //-----------------------------------------------------------------------------
  363. // Gets the size of the image format in bytes
  364. //-----------------------------------------------------------------------------
  365. inline int SizeInBytes( ImageFormat fmt )
  366. {
  367. return ImageFormatInfo(fmt).m_NumBytes;
  368. }
  369.  
  370. //-----------------------------------------------------------------------------
  371. // Does the image format support transparency?
  372. //-----------------------------------------------------------------------------
  373. inline bool IsTransparent( ImageFormat fmt )
  374. {
  375. return ImageFormatInfo(fmt).m_NumAlphaBits > 0;
  376. }
  377.  
  378.  
  379. //-----------------------------------------------------------------------------
  380. // Is the image format compressed?
  381. //-----------------------------------------------------------------------------
  382. inline bool IsCompressed( ImageFormat fmt )
  383. {
  384. return ImageFormatInfo(fmt).m_IsCompressed;
  385. }
  386.  
  387. } // end namespace ImageLoader
  388.  
  389. #endif // IMAGEFORMAT_H
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement