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