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.

172 lines
5.0 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef BITMAP_H
  9. #define BITMAP_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "bitmap/imageformat.h"
  14. #include "color.h"
  15. #include "dbg.h"
  16. class CUtlBuffer;
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations
  19. //-----------------------------------------------------------------------------
  20. class CUtlBuffer;
  21. //-----------------------------------------------------------------------------
  22. // A Bitmap
  23. //-----------------------------------------------------------------------------
  24. struct Bitmap_t
  25. {
  26. Bitmap_t() { Reset(); }
  27. ~Bitmap_t() { Clear(); }
  28. //
  29. // Accessors
  30. //
  31. inline int Width() const { return m_nWidth; }
  32. inline int Height() const { return m_nHeight; }
  33. inline ImageFormat Format() const { return m_ImageFormat; }
  34. inline unsigned char *GetBits() const { return m_pBits; }
  35. inline int Stride() const { return m_nStride; }
  36. inline bool GetOwnsBuffer() const { return m_bOwnsBuffer; }
  37. /// Allocate the buffer. Discards existing data, freeing it if we own it
  38. void Init( int nWidth, int nHeight, ImageFormat imageFormat, int nStride = 0 );
  39. /// Set the bitmap to the specified buffer. Any existing data is discarded/freed
  40. /// as appropriate.
  41. void SetBuffer( int nWidth, int nHeight, ImageFormat imageFormat, unsigned char *pBits, bool bAssumeOwnership, int nStride = 0 );
  42. /// Sets / releases ownershp of the buffer. This does not otherwise alter the
  43. /// state of the bitmap.
  44. void SetOwnsBuffer( bool bOwnsBuffer )
  45. {
  46. Assert( m_pBits );
  47. m_bOwnsBuffer = bOwnsBuffer;
  48. }
  49. /// Free up all memory and reset to default state
  50. void Clear();
  51. /// Return true if we have a valid size and buffer
  52. bool IsValid() const;
  53. /// Get pointer to raw pixel data.
  54. unsigned char *GetPixel( int x, int y );
  55. const unsigned char *GetPixel( int x, int y ) const;
  56. /// Get pixel value at specified coordinates
  57. Color GetColor( int x, int y ) const;
  58. /// Set pixel value at specified coordinates
  59. void SetColor( int x, int y, Color c );
  60. /// Set this bitmap to be a logical copy of the specified
  61. /// bitmap. No memory is allocated or copied, just copying
  62. /// some pointers. We can also optionally transfer ownership
  63. /// of the buffer.
  64. void MakeLogicalCopyOf( Bitmap_t &src, bool bTransferBufferOwnership = false );
  65. /// Set this bitmap to be a cropped rectangle from the given bitmap.
  66. /// The source pointer can be NULL or point to this, which means to do
  67. /// the crop in place.
  68. void Crop( int x0, int y0, int nWidth, int nHeight, const Bitmap_t *pImgSource = NULL );
  69. /// Blit a rectangle of pixel data into this image.
  70. void SetPixelData( const Bitmap_t &src, int nSrcX1, int nSrcY1, int nCopySizeX, int nCopySizeY, int nDestX1, int nDestY1 );
  71. /// Blit the entire source image into this image, at the specified offset.
  72. /// the rectangle is clipped if necessary
  73. void SetPixelData( const Bitmap_t &src, int nDestX1 = 0, int nDestY1 = 0 );
  74. private:
  75. void Reset();
  76. /// Dimensions
  77. int m_nWidth;
  78. int m_nHeight;
  79. /// Size, in bytes, of one pixel
  80. int m_nPixelSize;
  81. /// Image row stride, in bytes
  82. int m_nStride;
  83. // Do we own this buffer?
  84. bool m_bOwnsBuffer;
  85. /// Pixel format
  86. ImageFormat m_ImageFormat;
  87. /// Bitmap data. Must be allocated with malloc/free. Don't use
  88. /// new/delete
  89. unsigned char *m_pBits;
  90. };
  91. inline void Bitmap_t::Reset()
  92. {
  93. m_nWidth = 0;
  94. m_nHeight = 0;
  95. m_ImageFormat = IMAGE_FORMAT_UNKNOWN;
  96. m_pBits = NULL;
  97. m_nPixelSize = 0;
  98. m_bOwnsBuffer = false;
  99. m_nStride = 0;
  100. }
  101. inline unsigned char *Bitmap_t::GetPixel( int x, int y )
  102. {
  103. if ( !m_pBits )
  104. return NULL;
  105. return m_pBits + (y*m_nStride) + x* m_nPixelSize;
  106. }
  107. inline const unsigned char *Bitmap_t::GetPixel( int x, int y ) const
  108. {
  109. if ( !m_pBits )
  110. return NULL;
  111. return m_pBits + (y*m_nStride) + x* m_nPixelSize;
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Loads a bitmap from an arbitrary file: could be a TGA, PSD, or PFM.
  115. // LoadBitmap autodetects which type, and returns it
  116. //-----------------------------------------------------------------------------
  117. enum BitmapFileType_t
  118. {
  119. BITMAP_FILE_TYPE_UNKNOWN = -1,
  120. BITMAP_FILE_TYPE_PSD = 0,
  121. BITMAP_FILE_TYPE_TGA,
  122. BITMAP_FILE_TYPE_PFM,
  123. };
  124. BitmapFileType_t LoadBitmapFile( CUtlBuffer &buf, Bitmap_t *pBitmap );
  125. //-----------------------------------------------------------------------------
  126. // PFM file loading related methods
  127. //-----------------------------------------------------------------------------
  128. bool PFMReadFileR32F( CUtlBuffer &fileBuffer, Bitmap_t &bitmap, float pfmScale );
  129. bool PFMReadFileRGB323232F( CUtlBuffer &fileBuffer, Bitmap_t &bitmap, float pfmScale );
  130. bool PFMReadFileRGBA32323232F( CUtlBuffer &fileBuffer, Bitmap_t &bitmap, float pfmScale );
  131. bool PFMGetInfo_AndAdvanceToTextureBits( CUtlBuffer &pfmBuffer, int &nWidth, int &nHeight, ImageFormat &imageFormat );
  132. #endif // BITMAP_H