1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: macros for fast, raw bitmap data access
4 // Author: Eric Kidd, Vadim Zeitlin
8 // Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
16 #include "wx/bitmap.h"
18 // ----------------------------------------------------------------------------
21 // We need to access our raw bitmap data (1) portably and (2) efficiently.
22 // We do this using a two-dimensional "iteration" interface. Performance
23 // is extremely important here: these functions will be called hundreds
24 // of thousands of times in a row, and even small inefficiencies will
25 // make applications seem slow.
27 // We can't always rely on inline functions, because not all compilers actually
28 // bother to inline them unless we crank the optimization levels way up.
29 // Therefore, we also provide macros to wring maximum speed out of compiler
30 // unconditionally (e.g. even in debug builds). Of course, if the performance
31 // isn't absolutely crucial for you you shouldn't be using them but the inline
33 // ----------------------------------------------------------------------------
38 typedef wxPixelData<wxBitmap, wxNativePixelFormat> PixelData;
44 ... raw access to bitmap data unavailable, do something else ...
48 if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
50 ... complain: the bitmap it too small ...
54 PixelData::Iterator p(data);
56 // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
57 p.Offset(data, 10, 10);
59 for ( int y = 0; y < 10; ++y )
61 PixelData::Iterator rowStart = p;
63 for ( int x = 0; x < 10; ++x, ++p )
76 Note: we do not use WXDLLIMPEXP_CORE with classes in this file because VC++ has
77 problems with exporting inner class defined inside a specialization of a
78 template class from a DLL. Besides, as all the methods are inline it's not
79 really necessary to put them in DLL at all.
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
87 wxPixelFormat is a template class describing the bitmap data format. It
88 contains the constants describing the format of pixel data, but does not
89 describe how the entire bitmap is stored (i.e. top-to-bottom,
90 bottom-to-top, ...). It is also a "traits"-like class, i.e. it only
91 contains some constants and maybe static methods but nothing more, so it
92 can be safely used without incurring any overhead as all accesses to it are
95 Current limitations: we don't support RAGABA and ARAGAB formats supported
96 by Mac OS X. If there is sufficient interest, these classes could be
97 extended to deal with them. Neither do we support alpha channel having
98 different representation from the RGB ones (happens under QNX/Photon I
99 think), but again this could be achieved with some small extra effort.
101 Template parameters are:
102 - type of a single pixel component
103 - size of the single pixel in bits
104 - indices of red, green and blue pixel components inside the pixel
105 - index of the alpha component or -1 if none
106 - type which can contain the full pixel value (all channels)
109 template <class Channel
,
110 size_t Bpp
, int R
, int G
, int B
, int A
= -1,
111 class Pixel
= wxUint32
>
115 // iterator over pixels is usually of type "ChannelType *"
116 typedef Channel ChannelType
;
118 // the type which may hold the entire pixel value
119 typedef Pixel PixelType
;
121 // NB: using static ints initialized inside the class declaration is not
122 // portable as it doesn't work with VC++ 6, so we must use enums
124 // size of one pixel in bits
125 enum { BitsPerPixel
= Bpp
};
127 // size of one pixel in ChannelType units (usually bytes)
128 enum { SizePixel
= Bpp
/ (8 * sizeof(Channel
)) };
130 // the channels indices inside the pixel
139 // true if we have an alpha channel (together with the other channels, this
140 // doesn't cover the case of wxImage which stores alpha separately)
141 enum { HasAlpha
= A
!= -1 };
144 // some "predefined" pixel formats
145 // -------------------------------
147 // wxImage format is common to all platforms
148 typedef wxPixelFormat
<unsigned char, 24, 0, 1, 2> wxImagePixelFormat
;
150 // the (most common) native bitmap format without alpha support
151 #if defined(__WXMSW__)
152 // under MSW the RGB components are reversed, they're in BGR order
153 typedef wxPixelFormat
<unsigned char, 24, 2, 1, 0> wxNativePixelFormat
;
155 #define wxPIXEL_FORMAT_ALPHA 3
156 #elif defined(__WXMAC__)
157 // under Mac, first component is unused but still present, hence we use
159 typedef wxPixelFormat
<unsigned char, 32, 1, 2, 3> wxNativePixelFormat
;
161 #define wxPIXEL_FORMAT_ALPHA 0
162 #elif defined(__WXCOCOA__)
163 // Cocoa is standard RGB or RGBA (normally it is RGBA)
164 typedef wxPixelFormat
<unsigned char, 24, 0, 1, 2> wxNativePixelFormat
;
166 #define wxPIXEL_FORMAT_ALPHA 3
167 #elif defined(__WXGTK__)
168 // Under GTK+ 2.X we use GdkPixbuf, which is standard RGB or RGBA
169 typedef wxPixelFormat
<unsigned char, 24, 0, 1, 2> wxNativePixelFormat
;
171 #define wxPIXEL_FORMAT_ALPHA 3
172 #elif defined(__WXDFB__)
173 // Under DirectFB, RGB components are reversed, they're in BGR order
174 typedef wxPixelFormat
<unsigned char, 24, 2, 1, 0> wxNativePixelFormat
;
176 #define wxPIXEL_FORMAT_ALPHA 3
179 // the (most common) native format for bitmaps with alpha channel
180 #ifdef wxPIXEL_FORMAT_ALPHA
181 typedef wxPixelFormat
<unsigned char, 32,
182 wxNativePixelFormat::RED
,
183 wxNativePixelFormat::GREEN
,
184 wxNativePixelFormat::BLUE
,
185 wxPIXEL_FORMAT_ALPHA
> wxAlphaPixelFormat
;
186 #endif // wxPIXEL_FORMAT_ALPHA
188 // we also define the (default/best) pixel format for the given class: this is
189 // used as default value for the pixel format in wxPixelIterator template
190 template <class T
> struct wxPixelFormatFor
;
193 // wxPixelFormatFor is only defined for wxImage, attempt to use it with other
194 // classes (wxBitmap...) will result in compile errors which is exactly what we
197 struct wxPixelFormatFor
<wxImage
>
199 typedef wxImagePixelFormat Format
;
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
208 wxPixelDataBase is just a helper for wxPixelData: it contains things common
209 to both wxImage and wxBitmap specializations.
211 class wxPixelDataBase
214 // origin of the rectangular region we represent
215 wxPoint
GetOrigin() const { return m_ptOrigin
; }
217 // width and height of the region we represent
218 int GetWidth() const { return m_width
; }
219 int GetHeight() const { return m_height
; }
221 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
223 // the distance between two rows
224 int GetRowStride() const { return m_stride
; }
226 // private: -- see comment in the beginning of the file
228 // the origin of this image inside the bigger bitmap (usually (0, 0))
231 // the size of the image we address, in pixels
235 // this parameter is the offset of the start of the (N+1)st row from the
236 // Nth one and can be different from m_bypp*width in some cases:
237 // a) the most usual one is to force 32/64 bit alignment of rows
238 // b) another one is for bottom-to-top images where it's negative
239 // c) finally, it could conceivably be 0 for the images with all
240 // lines being identical
244 // ctor is protected because this class is only meant to be used as the
245 // base class by wxPixelData
255 wxPixelData represents the entire bitmap data, i.e. unlike
256 wxPixelFormat (which it uses) it also stores the global bitmap
257 characteristics such as its size, inter-row separation and so on.
259 Because of this it can be used to move the pixel iterators (which don't
260 have enough information about the bitmap themselves). This may seem a bit
261 unnatural but must be done in this way to keep the iterator objects as
262 small as possible for maximum efficiency as otherwise they wouldn't be put
263 into the CPU registers by the compiler any more.
265 Implementation note: we use the standard workaround for lack of partial
266 template specialization support in VC (both 6 and 7): instead of partly
267 specializing the class Foo<T, U> for some T we introduce FooOut<T> and
268 FooIn<U> nested in it, make Foo<T, U> equivalent to FooOut<T>::FooIn<U> and
269 fully specialize FooOut.
271 Also note that this class doesn't have any default definition because we
272 can't really do anything without knowing the exact image class. We do
273 provide wxPixelDataBase to make it simpler to write new wxPixelData
277 // we need to define this skeleton template to mollify VC++
278 template <class Image
>
279 struct wxPixelDataOut
281 template <class PixelFormat
>
290 // wxPixelData specialization for wxImage: this is the simplest case as we
291 // don't have to care about different pixel formats here
293 struct wxPixelDataOut
<wxImage
>
295 // NB: this is a template class even though it doesn't use its template
296 // parameter because otherwise wxPixelData couldn't compile
297 template <class dummyPixelFormat
>
298 class wxPixelDataIn
: public wxPixelDataBase
301 // the type of the class we're working with
302 typedef wxImage ImageType
;
304 // the iterator which should be used for working with data in this
309 // the pixel format we use
310 typedef wxImagePixelFormat PixelFormat
;
312 // the type of the pixel components
313 typedef typename
PixelFormat::ChannelType ChannelType
;
315 // the pixel data we're working with
317 wxPixelDataOut
<wxImage
>::wxPixelDataIn
<PixelFormat
> PixelData
;
320 void Reset(const PixelData
& data
)
322 *this = data
.GetPixels();
325 // creates the iterator pointing to the beginning of data
326 Iterator(PixelData
& data
)
331 // creates the iterator initially pointing to the image origin
332 Iterator(const wxImage
& image
)
334 m_pRGB
= image
.GetData();
336 if ( image
.HasAlpha() )
338 m_pAlpha
= image
.GetAlpha();
340 else // alpha is not used at all
346 // true if the iterator is valid
347 bool IsOk() const { return m_pRGB
!= NULL
; }
353 // advance the iterator to the next pixel, prefix version
354 Iterator
& operator++()
356 m_pRGB
+= PixelFormat::SizePixel
;
363 // postfix (hence less efficient -- don't use it unless you
364 // absolutely must) version
365 Iterator
operator++(int)
372 // move x pixels to the right and y down
374 // note that the rows don't wrap!
375 void Offset(const PixelData
& data
, int x
, int y
)
377 m_pRGB
+= data
.GetRowStride()*y
+ PixelFormat::SizePixel
*x
;
379 m_pAlpha
+= data
.GetWidth() + x
;
382 // move x pixels to the right (again, no row wrapping)
383 void OffsetX(const PixelData
& WXUNUSED(data
), int x
)
385 m_pRGB
+= PixelFormat::SizePixel
*x
;
390 // move y rows to the bottom
391 void OffsetY(const PixelData
& data
, int y
)
393 m_pRGB
+= data
.GetRowStride()*y
;
395 m_pAlpha
+= data
.GetWidth();
398 // go to the given position
399 void MoveTo(const PixelData
& data
, int x
, int y
)
409 // access to individual colour components
410 ChannelType
& Red() { return m_pRGB
[PixelFormat::RED
]; }
411 ChannelType
& Green() { return m_pRGB
[PixelFormat::GREEN
]; }
412 ChannelType
& Blue() { return m_pRGB
[PixelFormat::BLUE
]; }
413 ChannelType
& Alpha() { return *m_pAlpha
; }
415 // address the pixel contents directly (always RGB, without alpha)
416 typename
PixelFormat::PixelType
& Data()
417 { return *(typename
PixelFormat::PixelType
*)m_pRGB
; }
419 // private: -- see comment in the beginning of the file
421 // pointer into RGB buffer
422 unsigned char *m_pRGB
;
424 // pointer into alpha buffer or NULL if alpha isn't used
425 unsigned char *m_pAlpha
;
428 // initializes us with the data of the given image
429 wxPixelDataIn(ImageType
& image
) : m_image(image
), m_pixels(image
)
431 m_width
= image
.GetWidth();
432 m_height
= image
.GetHeight();
433 m_stride
= Iterator::PixelFormat::SizePixel
* m_width
;
436 // initializes us with the given region of the specified image
437 wxPixelDataIn(ImageType
& image
,
439 const wxSize
& sz
) : m_image(image
), m_pixels(image
)
441 m_stride
= Iterator::PixelFormat::SizePixel
* m_width
;
446 // initializes us with the given region of the specified image
447 wxPixelDataIn(ImageType
& image
,
448 const wxRect
& rect
) : m_image(image
), m_pixels(image
)
450 m_stride
= Iterator::PixelFormat::SizePixel
* m_width
;
452 InitRect(rect
.GetPosition(), rect
.GetSize());
455 // we evaluate to true only if we could get access to bitmap data
457 operator bool() const { return m_pixels
.IsOk(); }
459 // get the iterator pointing to the origin
460 Iterator
GetPixels() const { return m_pixels
; }
463 void InitRect(const wxPoint
& pt
, const wxSize
& sz
)
469 m_pixels
.Offset(*this, pt
.x
, pt
.y
);
472 // the image we're working with
475 // the iterator pointing to the image origin
482 // wxPixelData specialization for wxBitmap: here things are more interesting as
483 // we also have to support different pixel formats
485 struct wxPixelDataOut
<wxBitmap
>
487 template <class Format
>
488 class wxPixelDataIn
: public wxPixelDataBase
491 // the type of the class we're working with
492 typedef wxBitmap ImageType
;
497 // the pixel format we use
498 typedef Format PixelFormat
;
500 // the type of the pixel components
501 typedef typename
PixelFormat::ChannelType ChannelType
;
503 // the pixel data we're working with
504 typedef wxPixelDataOut
<wxBitmap
>::wxPixelDataIn
<Format
> PixelData
;
508 void Reset(const PixelData
& data
)
510 *this = data
.GetPixels();
513 // initializes the iterator to point to the origin of the given
515 Iterator(PixelData
& data
)
520 // initializes the iterator to point to the origin of the given
522 Iterator(wxBitmap
& bmp
, PixelData
& data
)
524 // using cast here is ugly but it should be safe as
525 // GetRawData() real return type should be consistent with
526 // BitsPerPixel (which is in turn defined by ChannelType) and
527 // this is the only thing we can do without making GetRawData()
528 // a template function which is undesirable
529 m_ptr
= (ChannelType
*)
530 bmp
.GetRawData(data
, PixelFormat::BitsPerPixel
);
533 // default constructor
539 // return true if this iterator is valid
540 bool IsOk() const { return m_ptr
!= NULL
; }
546 // advance the iterator to the next pixel, prefix version
547 Iterator
& operator++()
549 m_ptr
+= PixelFormat::SizePixel
;
554 // postfix (hence less efficient -- don't use it unless you
555 // absolutely must) version
556 Iterator
operator++(int)
563 // move x pixels to the right and y down
565 // note that the rows don't wrap!
566 void Offset(const PixelData
& data
, int x
, int y
)
568 m_ptr
+= data
.GetRowStride()*y
+ PixelFormat::SizePixel
*x
;
571 // move x pixels to the right (again, no row wrapping)
572 void OffsetX(const PixelData
& WXUNUSED(data
), int x
)
574 m_ptr
+= PixelFormat::SizePixel
*x
;
577 // move y rows to the bottom
578 void OffsetY(const PixelData
& data
, int y
)
580 m_ptr
+= data
.GetRowStride()*y
;
583 // go to the given position
584 void MoveTo(const PixelData
& data
, int x
, int y
)
594 // access to invidividual colour components
595 ChannelType
& Red() { return m_ptr
[PixelFormat::RED
]; }
596 ChannelType
& Green() { return m_ptr
[PixelFormat::GREEN
]; }
597 ChannelType
& Blue() { return m_ptr
[PixelFormat::BLUE
]; }
598 ChannelType
& Alpha() { return m_ptr
[PixelFormat::ALPHA
]; }
600 // address the pixel contents directly
602 // warning: the format is platform dependent
603 typename
PixelFormat::PixelType
& Data()
604 { return *(typename
PixelFormat::PixelType
*)m_ptr
; }
606 // private: -- see comment in the beginning of the file
608 // for efficiency reasons this class should not have any other
609 // fields, otherwise it won't be put into a CPU register (as it
610 // should inside the inner loops) by some compilers, notably gcc
614 // ctor associates this pointer with a bitmap and locks the bitmap for
615 // raw access, it will be unlocked only by our dtor and so these
616 // objects should normally be only created on the stack, i.e. have
618 wxPixelDataIn(wxBitmap
& bmp
) : m_bmp(bmp
), m_pixels(bmp
, *this)
622 wxPixelDataIn(wxBitmap
& bmp
, const wxRect
& rect
)
623 : m_bmp(bmp
), m_pixels(bmp
, *this)
625 InitRect(rect
.GetPosition(), rect
.GetSize());
628 wxPixelDataIn(wxBitmap
& bmp
, const wxPoint
& pt
, const wxSize
& sz
)
629 : m_bmp(bmp
), m_pixels(bmp
, *this)
634 // we evaluate to true only if we could get access to bitmap data
636 operator bool() const { return m_pixels
.IsOk(); }
638 // get the iterator pointing to the origin
639 Iterator
GetPixels() const { return m_pixels
; }
641 // dtor unlocks the bitmap
644 if ( m_pixels
.IsOk() )
646 #if defined(__WXMSW__) || defined(__WXMAC__)
647 // this is a hack to mark wxBitmap as using alpha channel
648 if ( Format::HasAlpha
)
651 m_bmp
.UngetRawData(*this);
653 // else: don't call UngetRawData() if GetRawData() failed
656 #if WXWIN_COMPATIBILITY_2_8
657 // not needed anymore, calls to it should be simply removed
658 wxDEPRECATED_INLINE( void UseAlpha(), wxEMPTY_PARAMETER_VALUE
)
661 // private: -- see comment in the beginning of the file
663 // the bitmap we're associated with
666 // the iterator pointing to the image origin
670 void InitRect(const wxPoint
& pt
, const wxSize
& sz
)
672 m_pixels
.Offset(*this, pt
.x
, pt
.y
);
683 // FIXME-VC6: VC6 doesn't like typename in default template parameters while
684 // it is necessary with standard-conforming compilers, remove this
685 // #define and just use typename when we drop VC6 support
686 #if defined(__VISUALC__) && !wxCHECK_VISUALC_VERSION(7)
687 #define wxTYPENAME_IN_TEMPLATE_DEFAULT_PARAM
689 #define wxTYPENAME_IN_TEMPLATE_DEFAULT_PARAM typename
692 template <class Image
,
693 class PixelFormat
= wxTYPENAME_IN_TEMPLATE_DEFAULT_PARAM
694 wxPixelFormatFor
<Image
>::Format
>
696 public wxPixelDataOut
<Image
>::template wxPixelDataIn
<PixelFormat
>
700 typename wxPixelDataOut
<Image
>::template wxPixelDataIn
<PixelFormat
>
703 wxPixelData(Image
& image
) : Base(image
) { }
705 wxPixelData(Image
& i
, const wxRect
& rect
) : Base(i
, rect
) { }
707 wxPixelData(Image
& i
, const wxPoint
& pt
, const wxSize
& sz
)
713 // some "predefined" pixel data classes
715 typedef wxPixelData
<wxImage
> wxImagePixelData
;
718 typedef wxPixelData
<wxBitmap
, wxNativePixelFormat
> wxNativePixelData
;
719 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> wxAlphaPixelData
;
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
728 wxPixel::Iterator represents something which points to the pixel data and
729 allows us to iterate over it. In the simplest case of wxBitmap it is,
730 indeed, just a pointer, but it can be something more complicated and,
731 moreover, you are free to specialize it for other image classes and bitmap
734 Note that although it would have been much more intuitive to have a real
735 class here instead of what we have now, this class would need two template
736 parameters, and this can't be done because we'd need compiler support for
737 partial template specialization then and neither VC6 nor VC7 provide it.
739 template < class Image
, class PixelFormat
= wxPixelFormatFor
<Image
> >
740 struct wxPixelIterator
: public wxPixelData
<Image
, PixelFormat
>::Iterator
744 #endif // _WX_RAWBMP_H_