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 // ----------------------------------------------------------------------------
35 typedef wxPixelData<wxBitmap, wxNativePixelFormat> PixelData;
41 ... raw access to bitmap data unavailable, do something else ...
45 if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
47 ... complain: the bitmap it too small ...
51 PixelData::Iterator p(data);
53 // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
54 p.Offset(data, 10, 10);
56 for ( int y = 0; y < 10; ++y )
58 PixelData::Iterator rowStart = p;
60 for ( int x = 0; x < 10; ++x, ++p )
73 // VC++ gives an absolutely harmless warning for wxPixelData<wxBitmap> ctor
74 #pragma warning(disable: 4355) // 'this' used in initializer list
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
82 wxPixelFormat is a template class describing the bitmap data format. It
83 contains the constants describing the format of pixel data, but does not
84 describe how the entire bitmap is stored (i.e. top-to-bottom,
85 bottom-to-top, ...). It is also a "traits"-like class, i.e. it only
86 contains some constants and maybe static methods but nothing more, so it
87 can be safely used without incurring any overhead as all accesses to it are
90 Current limitations: we don't support RAGABA and ARAGAB formats supported
91 by Mac OS X. If there is sufficient interest, these classes could be
92 extended to deal with them. Neither do we support alpha channel having
93 different representation from the RGB ones (happens under QNX/Photon I
94 think), but again this could be achieved with some small extra effort.
96 Template parameters are:
97 - type of a single pixel component
98 - size of the single pixel in bits
99 - indices of red, green and blue pixel components inside the pixel
100 - index of the alpha component or -1 if none
101 - type which can contain the full pixel value (all channels)
104 template <class Channel
,
105 size_t Bpp
, int R
, int G
, int B
, int A
= -1,
106 class Pixel
= wxUint32
>
108 struct WXDLLEXPORT wxPixelFormat
110 // iterator over pixels is usually of type "ChannelType *"
111 typedef Channel ChannelType
;
113 // the type which may hold the entire pixel value
114 typedef Pixel PixelType
;
116 // NB: using static ints initialized inside the class declaration is not
117 // portable as it doesn't work with VC++ 6, so we must use enums
119 // size of one pixel in bits
120 enum { BitsPerPixel
= Bpp
};
122 // size of one pixel in ChannelType units (usually bytes)
123 enum { SizePixel
= Bpp
/ (8 * sizeof(Channel
)) };
125 // the channels indices inside the pixel
134 // true if we have an alpha channel (together with the other channels, this
135 // doesn't cover the case of wxImage which stores alpha separately)
136 enum { HasAlpha
= A
!= -1 };
139 // some "predefined" pixel formats
140 // -------------------------------
142 // wxImage format is common to all platforms
143 typedef wxPixelFormat
<unsigned char, 24, 0, 1, 2> wxImagePixelFormat
;
145 // the (most common) native bitmap format without alpha support
146 #if defined(__WXMSW__)
147 // under MSW the RGB components are inversed, they're in BGR order
148 typedef wxPixelFormat
<unsigned char, 24, 2, 1, 0> wxNativePixelFormat
;
150 #define wxPIXEL_FORMAT_ALPHA 3
151 #elif defined(__WXMAC__)
152 // under Mac, first component is unused but still present, hence we use
154 typedef wxPixelFormat
<unsigned char, 32, 1, 2, 3> wxNativePixelFormat
;
156 #define wxPIXEL_FORMAT_ALPHA 0
157 #elif defined(__WXCOCOA__)
158 // Cocoa is standard RGB or RGBA (normally it is RGBA)
159 typedef wxPixelFormat
<unsigned char, 24, 0, 1, 2> wxNativePixelFormat
;
161 #define wxPIXEL_FORMAT_ALPHA 3
164 // the (most common) native format for bitmaps with alpha channel
165 #ifdef wxPIXEL_FORMAT_ALPHA
166 typedef wxPixelFormat
<unsigned char, 32,
167 wxNativePixelFormat::RED
,
168 wxNativePixelFormat::GREEN
,
169 wxNativePixelFormat::BLUE
,
170 wxPIXEL_FORMAT_ALPHA
> wxAlphaPixelFormat
;
171 #endif // wxPIXEL_FORMAT_ALPHA
173 // we also define the (default/best) pixel format for the given class: this is
174 // used as default value for the pixel format in wxPixelIterator template
175 template <class T
> struct wxPixelFormatFor
;
177 // wxPixelFormatFor is only defined for wxImage, attempt to use it with other
178 // classes (wxBitmap...) will result in compile errors which is exactly what we
181 struct WXDLLEXPORT wxPixelFormatFor
<wxImage
>
183 typedef wxImagePixelFormat Format
;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 wxPixelDataBase is just a helper for wxPixelData: it contains things common
192 to both wxImage and wxBitmap specializations.
194 class WXDLLEXPORT wxPixelDataBase
197 // origin of the rectangular region we represent
198 wxPoint
GetOrigin() const { return m_ptOrigin
; }
200 // width and height of the region we represent
201 int GetWidth() const { return m_width
; }
202 int GetHeight() const { return m_height
; }
204 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
206 // the distance between two rows
207 int GetRowStride() const { return m_stride
; }
209 // private: -- see comment in the beginning of the file
211 // the origin of this image inside the bigger bitmap (usually (0, 0))
214 // the size of the image we address, in pixels
218 // this parameter is the offset of the start of the (N+1)st row from the
219 // Nth one and can be different from m_bypp*width in some cases:
220 // a) the most usual one is to force 32/64 bit alignment of rows
221 // b) another one is for bottom-to-top images where it's negative
222 // c) finally, it could conceivably be 0 for the images with all
223 // lines being identical
227 // ctor is protected because this class is only meant to be used as the
228 // base class by wxPixelData
238 wxPixelData represents the entire bitmap data, i.e. unlike
239 wxPixelFormat (which it uses) it also stores the global bitmap
240 characteristics such as its size, inter-row separation and so on.
242 Because of this it can be used to move the pixel iterators (which don't
243 have enough information about the bitmap themselves). This may seem a bit
244 unnatural but must be done in this way to keep the iterator objects as
245 small as possible for maximum efficiency as otherwise they wouldn't be put
246 into the CPU registers by the compiler any more.
248 Implementation note: we use the standard workaround for lack of partial
249 template specialization support in VC (both 6 and 7): instead of partly
250 specializing the class Foo<T, U> for some T we introduce FooOut<T> and
251 FooIn<U> nested in it, make Foo<T, U> equivalent to FooOut<T>::FooIn<U> and
252 fully specialize FooOut.
254 Also note that this class doesn't have any default definition because we
255 can't really do anything without knowing the exact image class. We do
256 provide wxPixelDataBase to make it simpler to write new wxPixelData
260 // we need to define this skeleton template to mollify VC++
261 template <class Image
>
262 struct WXDLLEXPORT wxPixelDataOut
264 template <class PixelFormat
>
265 class WXDLLEXPORT wxPixelDataIn
272 // wxPixelData specialization for wxImage: this is the simplest case as we
273 // don't have to care about different pixel formats here
275 struct WXDLLEXPORT wxPixelDataOut
<wxImage
>
277 // NB: this is a template class even though it doesn't use its template
278 // parameter because otherwise wxPixelData couldn't compile
279 template <class dummyPixelFormat
>
280 class WXDLLEXPORT wxPixelDataIn
: public wxPixelDataBase
282 // the type of the class we're working with
283 typedef wxImage ImageType
;
285 // the iterator which should be used for working with data in this
290 // the pixel format we use
291 typedef wxImagePixelFormat PixelFormat
;
293 // the type of the pixel components
294 typedef typename
dummyPixelFormat::ChannelType ChannelType
;
296 // the pixel data we're working with
298 wxPixelDataOut
<wxImage
>::wxPixelDataIn
<PixelFormat
> PixelData
;
301 void Reset(const PixelData
& data
)
303 *this = data
.GetPixels();
306 // creates the iterator pointing to the beginning of data
307 Iterator(PixelData
& data
)
312 // creates the iterator initially pointing to the image origin
313 Iterator(const wxImage
& image
)
315 m_pRGB
= image
.GetData();
317 if ( image
.HasAlpha() )
319 m_pAlpha
= image
.GetAlpha();
321 else // alpha is not used at all
327 // true if the iterator is valid
328 bool IsOk() const { return m_pRGB
!= NULL
; }
334 // advance the iterator to the next pixel, prefix version
335 Iterator
& operator++()
337 m_pRGB
+= PixelFormat::SizePixel
;
344 // postfix (hence less efficient -- don't use it unless you
345 // absolutely must) version
346 Iterator
operator++(int)
353 // move x pixels to the right and y down
355 // note that the rows don't wrap!
356 void Offset(const PixelData
& data
, int x
, int y
)
358 m_pRGB
+= data
.GetRowStride()*y
+ PixelFormat::SizePixel
*x
;
360 m_pAlpha
+= data
.GetWidth() + x
;
363 // move x pixels to the right (again, no row wrapping)
364 void OffsetX(const PixelData
& WXUNUSED(data
), int x
)
366 m_pRGB
+= PixelFormat::SizePixel
*x
;
371 // move y rows to the bottom
372 void OffsetY(const PixelData
& data
, int y
)
374 m_pRGB
+= data
.GetRowStride()*y
;
376 m_pAlpha
+= data
.GetWidth();
379 // go to the given position
380 void MoveTo(const PixelData
& data
, int x
, int y
)
390 // access to invidividual colour components
391 ChannelType
& Red() { return m_ptr
[PixelFormat::RED
]; }
392 ChannelType
& Green() { return m_ptr
[PixelFormat::GREEN
]; }
393 ChannelType
& Blue() { return m_ptr
[PixelFormat::BLUE
]; }
394 ChannelType
& Alpha() { return *m_pAlpha
; }
396 // private: -- see comment in the beginning of the file
398 // pointer into RGB buffer
399 unsigned char *m_pRGB
;
401 // pointer into alpha buffer or NULL if alpha isn't used
402 unsigned char *m_pAlpha
;
405 // initializes us with the data of the given image
406 wxPixelDataIn(ImageType
& image
) : m_image(image
), m_pixels(image
)
408 m_width
= image
.GetWidth();
409 m_height
= image
.GetHeight();
410 m_stride
= Iterator::SizePixel
* m_width
;
413 // initializes us with the given region of the specified image
414 wxPixelDataIn(ImageType
& image
,
416 const wxSize
& sz
) : m_image(image
), m_pixels(image
)
418 m_stride
= Iterator::SizePixel
* m_width
;
423 // initializes us with the given region of the specified image
424 wxPixelDataIn(ImageType
& image
,
425 const wxRect
& rect
) : m_image(image
), m_pixels(image
)
427 m_stride
= Iterator::SizePixel
* m_width
;
429 InitRect(rect
.GetPositions(), rect
.GetSize());
432 // we evaluate to true only if we could get access to bitmap data
434 operator bool() const { return m_pixels
.IsOk(); }
436 // get the iterator pointing to the origin
437 Iterator
GetPixels() const { return m_pixels
; }
440 void InitRect(const wxPoint
& pt
, const wxSize
& sz
)
446 m_pixels
.Offset(*this, pt
.x
, pt
.y
);
449 // the image we're working with
452 // the iterator pointing to the image origin
457 // wxPixelData specialization for wxBitmap: here things are more interesting as
458 // we also have to support different pixel formats
460 struct WXDLLEXPORT wxPixelDataOut
<wxBitmap
>
462 template <class Format
>
463 class WXDLLEXPORT wxPixelDataIn
: public wxPixelDataBase
466 // the type of the class we're working with
467 typedef wxBitmap ImageType
;
472 // the pixel format we use
473 typedef Format PixelFormat
;
475 // the type of the pixel components
476 typedef typename
PixelFormat::ChannelType ChannelType
;
478 // the pixel data we're working with
479 typedef wxPixelDataOut
<wxBitmap
>::wxPixelDataIn
<Format
> PixelData
;
483 void Reset(const PixelData
& data
)
485 *this = data
.GetPixels();
488 // initializes the iterator to point to the origin of the given
490 Iterator(PixelData
& data
)
495 // initializes the iterator to point to the origin of the given
497 Iterator(wxBitmap
& bmp
, PixelData
& data
)
499 // using cast here is ugly but it should be safe as
500 // GetRawData() real return type should be consistent with
501 // BitsPerPixel (which is in turn defined by ChannelType) and
502 // this is the only thing we can do without making GetRawData()
503 // a template function which is undesirable
504 m_ptr
= (ChannelType
*)
505 bmp
.GetRawData(data
, PixelFormat::BitsPerPixel
);
508 // return true if this iterator is valid
509 bool IsOk() const { return m_ptr
!= NULL
; }
515 // advance the iterator to the next pixel, prefix version
516 Iterator
& operator++()
518 m_ptr
+= PixelFormat::SizePixel
;
523 // postfix (hence less efficient -- don't use it unless you
524 // absolutely must) version
525 Iterator
operator++(int)
532 // move x pixels to the right and y down
534 // note that the rows don't wrap!
535 void Offset(const PixelData
& data
, int x
, int y
)
537 m_ptr
+= data
.GetRowStride()*y
+ PixelFormat::SizePixel
*x
;
540 // move x pixels to the right (again, no row wrapping)
541 void OffsetX(const PixelData
& WXUNUSED(data
), int x
)
543 m_ptr
+= PixelFormat::SizePixel
*x
;
546 // move y rows to the bottom
547 void OffsetY(const PixelData
& data
, int y
)
549 m_ptr
+= data
.GetRowStride()*y
;
552 // go to the given position
553 void MoveTo(const PixelData
& data
, int x
, int y
)
563 // access to invidividual colour components
564 ChannelType
& Red() { return m_ptr
[PixelFormat::RED
]; }
565 ChannelType
& Green() { return m_ptr
[PixelFormat::GREEN
]; }
566 ChannelType
& Blue() { return m_ptr
[PixelFormat::BLUE
]; }
567 ChannelType
& Alpha() { return m_ptr
[PixelFormat::ALPHA
]; }
569 // address the pixel contents directly
571 // warning: the format is platform dependent
572 typename
PixelFormat::PixelType
& Data()
573 { return *(typename
PixelFormat::PixelType
*)m_ptr
; }
575 // private: -- see comment in the beginning of the file
577 // for efficiency reasons this class should not have any other
578 // fields, otherwise it won't be put into a CPU register (as it
579 // should inside the inner loops) by some compilers, notably gcc
583 // ctor associates this pointer with a bitmap and locks the bitmap for
584 // raw access, it will be unlocked only by our dtor and so these
585 // objects should normally be only created on the stack, i.e. have
587 wxPixelDataIn(wxBitmap
& bmp
) : m_bmp(bmp
), m_pixels(bmp
, *this)
591 wxPixelDataIn(wxBitmap
& bmp
, const wxRect
& rect
)
592 : m_bmp(bmp
), m_pixels(bmp
, *this)
594 InitRect(rect
.GetPosition(), rect
.GetSize());
597 wxPixelDataIn(wxBitmap
& bmp
, const wxPoint
& pt
, const wxSize
& sz
)
598 : m_bmp(bmp
), m_pixels(bmp
, *this)
603 // we evaluate to true only if we could get access to bitmap data
605 operator bool() const { return m_pixels
.IsOk(); }
607 // get the iterator pointing to the origin
608 Iterator
GetPixels() const { return m_pixels
; }
610 // dtor unlocks the bitmap
613 m_bmp
.UngetRawData(*this);
616 // call this to indicate that we should use the alpha channel
617 void UseAlpha() { m_bmp
.UseAlpha(); }
619 // private: -- see comment in the beginning of the file
621 // the bitmap we're associated with
624 // the iterator pointing to the image origin
628 void InitRect(const wxPoint
& pt
, const wxSize
& sz
)
630 m_pixels
.Offset(*this, pt
.x
, pt
.y
);
640 // typedef-name 'foo' used as synonym for class-name 'bar'
641 // (VC++ gives this warning each time wxPixelData::Base is used but it
642 // doesn't make any sense here -- what's wrong with using typedef instead
643 // of class, this is what it is here for!)
644 #pragma warning(disable: 4097)
645 #endif // __VISUALC__
647 template <class Image
, class PixelFormat
= wxPixelFormatFor
<Image
> >
649 public wxPixelDataOut
<Image
>::template wxPixelDataIn
<PixelFormat
>
653 typename wxPixelDataOut
<Image
>::template wxPixelDataIn
<PixelFormat
>
656 wxPixelData(Image
& image
) : Base(image
) { }
658 wxPixelData(Image
& i
, const wxRect
& rect
) : Base(i
, rect
) { }
660 wxPixelData(Image
& i
, const wxPoint
& pt
, const wxSize
& sz
)
667 // some "predefined" pixel data classes
668 typedef wxPixelData
<wxImage
> wxImagePixelData
;
669 typedef wxPixelData
<wxBitmap
, wxNativePixelFormat
> wxNativePixelData
;
670 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> wxAlphaPixelData
;
672 // ----------------------------------------------------------------------------
674 // ----------------------------------------------------------------------------
677 wxPixel::Iterator represents something which points to the pixel data and
678 allows us to iterate over it. In the simplest case of wxBitmap it is,
679 indeed, just a pointer, but it can be something more complicated and,
680 moreover, you are free to specialize it for other image classes and bitmap
683 Note that although it would have been much more intuitive to have a real
684 class here instead of what we have now, this class would need two template
685 parameters, and this can't be done because we'd need compiler support for
686 partial template specialization then and neither VC6 nor VC7 provide it.
688 template < class Image
, class PixelFormat
= wxPixelFormatFor
<Image
> >
689 struct WXDLLEXPORT wxPixelIterator
: wxPixelData
<Image
, PixelFormat
>::Iterator
694 #pragma warning(default: 4355)
695 #pragma warning(default: 4097)
698 #endif // _WX_RAWBMP_H_BASE_