Counter Strike : Global Offensive Source Code
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.

217 lines
6.1 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // A class representing an image
  4. //
  5. //=============================================================================
  6. #ifndef DMEIMAGE_H
  7. #define DMEIMAGE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattributevar.h"
  13. #include "bitmap/imageformat.h"
  14. #include "bitmap/floatbitmap.h"
  15. //-----------------------------------------------------------------------------
  16. // A class representing an image (2d or 3d bitmap)
  17. //-----------------------------------------------------------------------------
  18. class CDmeImage : public CDmElement
  19. {
  20. DEFINE_ELEMENT( CDmeImage, CDmElement );
  21. public:
  22. virtual void OnAttributeChanged( CDmAttribute *pAttribute );
  23. virtual void OnElementUnserialized();
  24. virtual void OnElementSerialized();
  25. public:
  26. // Initializes the buffer, but doesn't allocate space
  27. void Init( int nWidth, int nHeight, int nDepth, ImageFormat fmt, float flGamma );
  28. // Gets dimensions
  29. int Width() const;
  30. int Height() const;
  31. int Depth() const;
  32. // Methods related to image format
  33. ImageFormat Format() const;
  34. const char *FormatName() const;
  35. // Methods related to gamma
  36. float Gamma() const;
  37. // returns the size of one row
  38. int RowSizeInBytes( ) const;
  39. // returns the size of one z slice
  40. int ZSliceSizeInBytes( ) const;
  41. // returns the total size of the image
  42. int SizeInBytes( ) const;
  43. // Sets the storage mode. False = the bits are put in the attribute.
  44. // True = the bits are put in the float bitmap
  45. void SetFloatBitmapStorageMode( bool bFloatBitmap, bool bDiscardContents = false );
  46. bool IsUsingFloatBitmapStorageMode() const;
  47. bool HasImageData() const;
  48. // Used for computation
  49. // void BeginComputation();
  50. // void EndComputation();
  51. // Copies the image from the src in whatever storage form they are currently in
  52. // Potentially color converting
  53. void CopyFrom( CDmeImage *pSrcImage, ImageFormat fmt = IMAGE_FORMAT_UNKNOWN );
  54. // Color converts the image into the destination format.
  55. // Has no immediate effect if the image is in 'float bitmap' mode.
  56. // Instead, it will cause it to use this format when it eventually
  57. // reconverts back to 'attribute' mode.
  58. // NOTE: Doesn't work to convert to a compressed format.
  59. void ConvertFormat( ImageFormat fmt );
  60. // Reinterprets the image as a new color format; no work is done
  61. // The old + new color formats must be the same size in bytes
  62. void ReinterpetFormat( ImageFormat fmt );
  63. //
  64. // NOTE: The following methods operate on the bits attribute
  65. //
  66. // Used for bit modification
  67. CUtlBinaryBlock &BeginModification( );
  68. void EndModification( );
  69. // returns a pointer to the image bits buffer
  70. const void *ImageBits();
  71. // Copies bits into the image bits buffer
  72. void SetImageBits( const void *pBits, int nSize );
  73. // Compresses an image into this image
  74. void CompressImage( CDmeImage *pSrcImage, ImageFormat fmt );
  75. //
  76. // NOTE: The following methods operate on the float-bitmap version of the bits attribute
  77. //
  78. // returns a pointer to the image bits buffer as a float bitmap
  79. const FloatBitMap_t *FloatBitmap();
  80. // Allows you to directly manipulate the float bitmap
  81. FloatBitMap_t &BeginFloatBitmapModification( );
  82. void EndFloatBitmapModification( );
  83. // Creates an image 1/4 size of the source using a box filter
  84. void QuarterSize( CDmeImage *pSrcImage );
  85. // Downsample using nice filter (NOTE: Dest bitmap needs to have been initialized w/ final size)
  86. void DownsampleNiceFiltered( const DownsampleInfo_t& info, CDmeImage *pSrcImage );
  87. // Sets the color of every pixel
  88. void Clear( float r, float g, float b, float a );
  89. private:
  90. enum StorageMode_t
  91. {
  92. DMEIMAGE_STORAGE_NONE = 0,
  93. DMEIMAGE_STORAGE_FLOAT_BITMAP,
  94. DMEIMAGE_STORAGE_ATTRIBUTE,
  95. };
  96. CDmaVar<int> m_nWidth;
  97. CDmaVar<int> m_nHeight;
  98. CDmaVar<int> m_nDepth;
  99. CDmaVar<int> m_nFormat;
  100. CDmaVar<float> m_flGamma;
  101. CDmaBinaryBlock m_Bits;
  102. // Used for computation
  103. DmAttributeModifyHandle_t m_hModify;
  104. StorageMode_t m_Mode;
  105. bool m_bInModification;
  106. bool m_bInFloatBitmapModification;
  107. bool m_bIgnoreChangedBitsAttribute;
  108. FloatBitMap_t m_ComputeBits;
  109. };
  110. //-----------------------------------------------------------------------------
  111. // Gets dimensions
  112. //-----------------------------------------------------------------------------
  113. inline int CDmeImage::Width() const
  114. {
  115. return ( m_Mode != DMEIMAGE_STORAGE_FLOAT_BITMAP ) ? m_nWidth : m_ComputeBits.NumCols();
  116. }
  117. inline int CDmeImage::Height() const
  118. {
  119. return ( m_Mode != DMEIMAGE_STORAGE_FLOAT_BITMAP ) ? m_nHeight : m_ComputeBits.NumRows();
  120. }
  121. inline int CDmeImage::Depth() const
  122. {
  123. return ( m_Mode != DMEIMAGE_STORAGE_FLOAT_BITMAP ) ? m_nDepth : m_ComputeBits.NumSlices();
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Methods related to gamma
  127. //-----------------------------------------------------------------------------
  128. inline float CDmeImage::Gamma() const
  129. {
  130. return m_flGamma;
  131. }
  132. //-----------------------------------------------------------------------------
  133. // returns a pointer to the image bits buffer
  134. //-----------------------------------------------------------------------------
  135. inline const void *CDmeImage::ImageBits()
  136. {
  137. SetFloatBitmapStorageMode( false );
  138. return m_Bits.Get();
  139. }
  140. inline bool CDmeImage::IsUsingFloatBitmapStorageMode() const
  141. {
  142. return ( m_Mode == DMEIMAGE_STORAGE_FLOAT_BITMAP );
  143. }
  144. inline bool CDmeImage::HasImageData() const
  145. {
  146. return ( m_Mode != DMEIMAGE_STORAGE_NONE );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // An array of images (used for cubemaps or texture arrays)
  150. //-----------------------------------------------------------------------------
  151. class CDmeImageArray : public CDmElement
  152. {
  153. DEFINE_ELEMENT( CDmeImageArray, CDmElement );
  154. public:
  155. int ImageCount() const;
  156. CDmeImage *GetImage( int nIndex ) const;
  157. CDmeImage *AddImage( );
  158. void AddImage( CDmeImage *pImage );
  159. // Gets dimensions
  160. int Width() const;
  161. int Height() const;
  162. int Depth() const;
  163. ImageFormat Format() const;
  164. bool IsConsistent( int nWidth, int nHeight, int nDepth, ImageFormat fmt ) const;
  165. private:
  166. CDmaElementArray< CDmeImage > m_Images;
  167. };
  168. #endif // DMEIMAGE_H