]>
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);
93 // call this to indicate that we should use the alpha channel
94 void UseAlpha() { m_bmp
.UseAlpha(); }
97 unsigned char *GetPixels() const { return m_pixels
; }
98 int GetWidth() const { return m_width
; }
99 int GetHeight() const { return m_height
; }
100 int GetByPP() const { return m_bypp
; }
101 int GetBPP() const { return 8*GetByPP(); }
102 int GetRowStride() const { return m_stride
; }
104 // private: -- public because accessed by the macros below but still mustn't be
107 // the bitmap we're associated with
110 // pointer to the start of the data
111 unsigned char *m_pixels
;
113 // the size of the image we address, in pixels
117 // number of bytes (NOT bits) per pixel, including alpha channel if any
120 // this parameter is the offset of the start of the (N+1)st row from the
121 // Nth one and can be different from m_bypp*width in some cases:
122 // a) the most usual one is to force 32/64 bit alignment of rows
123 // b) another one is for bottom-to-top images where it's negative
124 // c) finally, it could conceivably be 0 for the images with all
125 // lines being identical
129 // this is the type for the iterator over raw bitmap data
130 class wxRawBitmapIterator
136 // we must be associated/initialized with some bitmap data object
137 wxRawBitmapIterator(const wxRawBitmapData
& data
) : m_data(&data
)
139 m_ptr
= m_data
->GetPixels();
142 // default copy ctor, assignment operator and dtor are ok
148 // move x pixels to the right and y down
150 // note that the rows don't wrap!
151 void Offset(int x
, int y
)
153 m_ptr
+= m_data
->GetRowStride()*y
+ m_data
->GetByPP()*x
;
156 // move x pixels to the right (again, no row wrapping)
159 m_ptr
+= m_data
->GetByPP()*x
;
162 // move y rows to the bottom
165 m_ptr
+= m_data
->GetRowStride()*y
;
171 m_ptr
= m_data
->GetPixels();
174 // go to the given position
175 void MoveTo(int x
, int y
)
181 // same as OffsetX(1) for convenience
182 wxRawBitmapIterator
& operator++()
188 // postfix (hence less efficient) version
189 wxRawBitmapIterator
operator++(int)
191 wxRawBitmapIterator
p(*this);
199 // DIBs store data in BGR format, i.e. "little endian" RGB
206 #endif // __WXMSW__/!__WXMSW__
210 // access to invidividual colour components
211 unsigned char& Red() { return m_ptr
[RED
]; }
212 unsigned char& Green() { return m_ptr
[GREEN
]; }
213 unsigned char& Blue() { return m_ptr
[BLUE
]; }
214 unsigned char& Alpha() { return m_ptr
[ALPHA
]; }
216 // address the pixel contents directly
218 // warning: the format is platform dependent
219 wxUint32
& Data() { return *(wxUint32
*)m_ptr
; }
221 // private: -- don't access these fields directly, same as as above
222 unsigned char *m_ptr
;
224 const wxRawBitmapData
*m_data
;
228 // these macros are used to change the current location in the bitmap
229 // ------------------------------------------------------------------
231 // move x pixels to the right and y down
233 // note that the rows don't wrap!
234 #define wxBMP_OFFSET(p, x, y) \
235 p.m_ptr += p.m_data->m_stride * (y) + p.m_data->m_bypp * (x)
237 // move x pixels to the right (again, no row wrapping)
238 #define wxBMP_OFFSET_X(p, x) p.m_ptr += p.m_data->m_bypp * (x)
240 // move y rows to the bottom
241 #define wxBMP_OFFSET_Y(p, y) p.m_ptr += p.m_data->m_stride * (y)
245 // these macros are used to work with the pixel values
247 // all of them can be used as either lvalues or rvalues.
248 // ----------------------------------------------------
250 #define wxBMP_RED(p) (p.m_ptr[wxRawBitmapIterator::RED])
251 #define wxBMP_GREEN(p) (p.m_ptr[wxRawBitmapIterator::GREEN])
252 #define wxBMP_BLUE(p) (p.m_ptr[wxRawBitmapIterator::BLUE])
254 #define wxBMP_ALPHA(p) (p.m_ptr[wxRawBitmapIterator::ALPHA])
256 // these macros are most efficient but return the buffer contents in
257 // platform-specific format, e.g. RGB on all sane platforms and BGR under Win32
258 #define wxBMP_RGB(p) *(wxUint32 *)(p.m_ptr)
259 #define wxBMP_RGBA(p) *(wxUint32 *)(p.m_ptr)
261 #endif // _WX_RAWBMP_H_BASE_