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.

142 lines
3.7 KiB

  1. //========= Copyright 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. // A Bitmap
  19. //-----------------------------------------------------------------------------
  20. struct Bitmap_t
  21. {
  22. Bitmap_t() { Reset(); }
  23. ~Bitmap_t() { Clear(); }
  24. //
  25. // Accessors
  26. //
  27. inline int Width() const { return m_nWidth; }
  28. inline int Height() const { return m_nHeight; }
  29. inline ImageFormat Format() const { return m_ImageFormat; }
  30. inline unsigned char *GetBits() const { return m_pBits; }
  31. inline int Stride() const { return m_nStride; }
  32. inline bool GetOwnsBuffer() const { return m_bOwnsBuffer; }
  33. /// Allocate the buffer. Discards existing data, freeing it if we own it
  34. void Init( int nWidth, int nHeight, ImageFormat imageFormat, int nStride = 0 );
  35. /// Set the bitmap to the specified buffer. Any existing data is discarded/freed
  36. /// as appropriate.
  37. void SetBuffer( int nWidth, int nHeight, ImageFormat imageFormat, unsigned char *pBits, bool bAssumeOwnership, int nStride = 0 );
  38. /// Sets / releases ownershp of the buffer. This does not otherwise alter the
  39. /// state of the bitmap.
  40. void SetOwnsBuffer( bool bOwnsBuffer )
  41. {
  42. Assert( m_pBits );
  43. m_bOwnsBuffer = bOwnsBuffer;
  44. }
  45. /// Free up all memory and reset to default state
  46. void Clear();
  47. /// Return true if we have a valid size and buffer
  48. bool IsValid() const;
  49. /// Get pointer to raw pixel data.
  50. unsigned char *GetPixel( int x, int y );
  51. const unsigned char *GetPixel( int x, int y ) const;
  52. /// Get pixel value at specified coordinates
  53. Color GetColor( int x, int y ) const;
  54. /// Set pixel value at specified coordinates
  55. void SetColor( int x, int y, Color c );
  56. /// Set this bitmap to be a logical copy of the specified
  57. /// bitmap. No memory is allocated or copied, just copying
  58. /// some pointers. We can also optionally transfer ownership
  59. /// of the buffer.
  60. void MakeLogicalCopyOf( Bitmap_t &src, bool bTransferBufferOwnership = false );
  61. /// Set this bitmap to be a cropped rectangle from the given bitmap.
  62. /// The source pointer can be NULL or point to this, which means to do
  63. /// the crop in place.
  64. void Crop( int x0, int y0, int nWidth, int nHeight, const Bitmap_t *pImgSource = NULL );
  65. /// Blit a rectangle of pixel data into this image.
  66. void SetPixelData( const Bitmap_t &src, int nSrcX1, int nSrcY1, int nCopySizeX, int nCopySizeY, int nDestX1, int nDestY1 );
  67. /// Blit the entire source image into this image, at the specified offset.
  68. /// the rectangle is clipped if necessary
  69. void SetPixelData( const Bitmap_t &src, int nDestX1 = 0, int nDestY1 = 0 );
  70. private:
  71. void Reset();
  72. /// Dimensions
  73. int m_nWidth;
  74. int m_nHeight;
  75. /// Size, in bytes, of one pixel
  76. int m_nPixelSize;
  77. /// Image row stride, in bytes
  78. int m_nStride;
  79. // Do we own this buffer?
  80. bool m_bOwnsBuffer;
  81. /// Pixel format
  82. ImageFormat m_ImageFormat;
  83. /// Bitmap data. Must be allocated with malloc/free. Don't use
  84. /// new/delete
  85. unsigned char *m_pBits;
  86. };
  87. inline void Bitmap_t::Reset()
  88. {
  89. m_nWidth = 0;
  90. m_nHeight = 0;
  91. m_ImageFormat = IMAGE_FORMAT_UNKNOWN;
  92. m_pBits = NULL;
  93. m_nPixelSize = 0;
  94. m_bOwnsBuffer = false;
  95. m_nStride = 0;
  96. }
  97. inline unsigned char *Bitmap_t::GetPixel( int x, int y )
  98. {
  99. if ( !m_pBits )
  100. return NULL;
  101. return m_pBits + (y*m_nStride) + x* m_nPixelSize;
  102. }
  103. inline const unsigned char *Bitmap_t::GetPixel( int x, int y ) const
  104. {
  105. if ( !m_pBits )
  106. return NULL;
  107. return m_pBits + (y*m_nStride) + x* m_nPixelSize;
  108. }
  109. #endif // BITMAP_H