]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/rawbmp.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: macros for fast, raw bitmap data access
4 // Author: Eric Kidd, Vadim Zeitlin
8 // Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_RAWBMP_H_BASE_
13 #define _WX_RAWBMP_H_BASE_
15 // ----------------------------------------------------------------------------
18 // We need to access our raw bitmap data (1) portably and (2) efficiently.
19 // We do this using a two-dimensional "iteration" interface. Performance
20 // is extremely important here: these functions will be called hundreds
21 // of thousands of times in a row, and even small inefficiencies will
22 // make applications seem slow.
24 // We can't always rely on inline functions, because not all compilers actually
25 // bother to inline them unless we crank the optimization levels way up.
26 // Therefore, we also provide macros to wring maximum speed out of compiler
27 // unconditionally (e.g. even in debug builds). Of course, if the performance
28 // isn't absolutely crucial for you you shouldn't be using them but the inline
30 // ----------------------------------------------------------------------------
36 wxRawBitmapData data(bitmap);
39 ... raw access to bitmap data unavailable, do something else ...
43 if ( data.m_width < 20 || data.m_height < 20 )
45 ... complain: the bitmap it too small ...
49 wxRawBitmapIterator p(data);
51 // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
54 for ( int y = 0; y < 10; ++y )
56 wxRawBitmapIterator rowStart = p;
58 for ( int x = 0; x < 10; ++x, ++p )
70 // this struct represents a pointer to raw bitmap data
74 // ctor associates this pointer with a bitmap and locks the bitmap for raw
75 // access, it will be unlocked only by our dtor and so these objects should
76 // normally be only created on the stack, i.e. have limited life-time
77 wxRawBitmapData(wxBitmap bmp
) : m_bmp(bmp
)
79 if ( !bmp
.GetRawData(this) )
83 // we evaluate to true only if we could get access to bitmap data
85 operator bool() const { return m_pixels
!= NULL
; }
87 // dtor unlocks the bitmap
90 m_bmp
.UngetRawData(this);
94 unsigned char *GetPixels() const { return m_pixels
; }
95 int GetWidth() const { return m_width
; }
96 int GetHeight() const { return m_height
; }
97 int GetByPP() const { return m_bypp
; }
98 int GetBPP() const { return 8*GetByPP(); }
99 int GetRowStride() const { return m_stride
; }
101 // private: -- public because accessed by the macros below but still mustn't be
104 // the bitmap we're associated with
107 // pointer to the start of the data
108 unsigned char *m_pixels
;
110 // the size of the image we address, in pixels
114 // number of bytes (NOT bits) per pixel, including alpha channel if any
117 // this parameter is the offset of the start of the (N+1)st row from the
118 // Nth one and can be different from m_bypp*width in some cases:
119 // a) the most usual one is to force 32/64 bit alignment of rows
120 // b) another one is for bottom-to-top images where it's negative
121 // c) finally, it could conceivably be 0 for the images with all
122 // lines being identical
126 // this is the type for the iterator over raw bitmap data
127 class wxRawBitmapIterator
133 // we must be associated/initialized with some bitmap data object
134 wxRawBitmapIterator(const wxRawBitmapData
& data
) : m_data(&data
)
136 m_ptr
= m_data
->GetPixels();
139 // default copy ctor, assignment operator and dtor are ok
145 // move x pixels to the right and y down
147 // note that the rows don't wrap!
148 void Offset(int x
, int y
)
150 m_ptr
+= m_data
->GetRowStride()*y
+ m_data
->GetByPP()*x
;
153 // move x pixels to the right (again, no row wrapping)
156 m_ptr
+= m_data
->GetByPP()*x
;
159 // move y rows to the bottom
162 m_ptr
+= m_data
->GetRowStride()*y
;
168 m_ptr
= m_data
->GetPixels();
171 // go to the given position
172 void MoveTo(int x
, int y
)
178 // same as OffsetX(1) for convenience
179 wxRawBitmapIterator
& operator++()
185 // postfix (hence less efficient) version
186 wxRawBitmapIterator
operator++(int)
188 wxRawBitmapIterator
p(*this);
196 // DIBs store data in BGR format, i.e. "little endian" RGB
203 #endif // __WXMSW__/!__WXMSW__
207 // access to invidividual colour components
208 unsigned char& Red() { return m_ptr
[RED
]; }
209 unsigned char& Green() { return m_ptr
[GREEN
]; }
210 unsigned char& Blue() { return m_ptr
[BLUE
]; }
211 unsigned char& Alpha() { return m_ptr
[ALPHA
]; }
213 // address the pixel contents directly
215 // warning: the format is platform dependent
216 wxUint32
& Data() { return *(wxUint32
*)m_ptr
; }
218 // private: -- don't access these fields directly, same as as above
219 unsigned char *m_ptr
;
221 const wxRawBitmapData
*m_data
;
225 // these macros are used to change the current location in the bitmap
226 // ------------------------------------------------------------------
228 // move x pixels to the right and y down
230 // note that the rows don't wrap!
231 #define wxBMP_OFFSET(p, x, y) \
232 p.m_ptr += p.m_data->m_stride * (y) + p.m_data->m_bypp * (x)
234 // move x pixels to the right (again, no row wrapping)
235 #define wxBMP_OFFSET_X(p, x) p.m_ptr += p.m_data->m_bypp * (x)
237 // move y rows to the bottom
238 #define wxBMP_OFFSET_Y(p, y) p.m_ptr += p.m_data->m_stride * (y)
242 // these macros are used to work with the pixel values
244 // all of them can be used as either lvalues or rvalues.
245 // ----------------------------------------------------
247 #define wxBMP_RED(p) (p.m_ptr[wxRawBitmapIterator::RED])
248 #define wxBMP_GREEN(p) (p.m_ptr[wxRawBitmapIterator::GREEN])
249 #define wxBMP_BLUE(p) (p.m_ptr[wxRawBitmapIterator::BLUE])
251 #define wxBMP_ALPHA(p) (p.m_ptr[wxRawBitmapIterator::ALPHA])
253 // these macros are most efficient but return the buffer contents in
254 // platform-specific format, e.g. RGB on all sane platforms and BGR under Win32
255 #define wxBMP_RGB(p) *(wxUint32 *)(p.m_ptr)
256 #define wxBMP_RGBA(p) *(wxUint32 *)(p.m_ptr)
258 #endif // _WX_RAWBMP_H_BASE_