]> git.saurik.com Git - wxWidgets.git/blob - include/wx/rawbmp.h
Build/distrib updates
[wxWidgets.git] / include / wx / rawbmp.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/rawbmp.h
3 // Purpose: macros for fast, raw bitmap data access
4 // Author: Eric Kidd, Vadim Zeitlin
5 // Modified by:
6 // Created: 10.03.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_RAWBMP_H_BASE_
13 #define _WX_RAWBMP_H_BASE_
14
15 #include "wx/image.h"
16
17 // ----------------------------------------------------------------------------
18 // Abstract Pixel API
19 //
20 // We need to access our raw bitmap data (1) portably and (2) efficiently.
21 // We do this using a two-dimensional "iteration" interface. Performance
22 // is extremely important here: these functions will be called hundreds
23 // of thousands of times in a row, and even small inefficiencies will
24 // make applications seem slow.
25 //
26 // We can't always rely on inline functions, because not all compilers actually
27 // bother to inline them unless we crank the optimization levels way up.
28 // Therefore, we also provide macros to wring maximum speed out of compiler
29 // unconditionally (e.g. even in debug builds). Of course, if the performance
30 // isn't absolutely crucial for you you shouldn't be using them but the inline
31 // functions instead.
32 // ----------------------------------------------------------------------------
33
34 /*
35 Usage example:
36
37 typedef wxPixelData<wxBitmap, wxNativePixelFormat> PixelData;
38
39 wxBitmap bmp;
40 PixelData data(bmp);
41 if ( !data )
42 {
43 ... raw access to bitmap data unavailable, do something else ...
44 return;
45 }
46
47 if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
48 {
49 ... complain: the bitmap it too small ...
50 return;
51 }
52
53 PixelData::Iterator p(data);
54
55 // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
56 p.Offset(data, 10, 10);
57
58 for ( int y = 0; y < 10; ++y )
59 {
60 PixelData::Iterator rowStart = p;
61
62 for ( int x = 0; x < 10; ++x, ++p )
63 {
64 p.Red() = r;
65 p.Green() = g;
66 p.Blue() = b;
67 }
68
69 p = rowStart;
70 p.OffsetY(data, 1);
71 }
72 */
73
74 #ifdef __VISUALC__
75 // VC++ gives an absolutely harmless warning for wxPixelData<wxBitmap> ctor
76 #pragma warning(disable: 4355) // 'this' used in initializer list
77 #endif
78
79 // ----------------------------------------------------------------------------
80 // wxPixelFormat
81 // ----------------------------------------------------------------------------
82
83 /*
84 wxPixelFormat is a template class describing the bitmap data format. It
85 contains the constants describing the format of pixel data, but does not
86 describe how the entire bitmap is stored (i.e. top-to-bottom,
87 bottom-to-top, ...). It is also a "traits"-like class, i.e. it only
88 contains some constants and maybe static methods but nothing more, so it
89 can be safely used without incurring any overhead as all accesses to it are
90 done at compile-time.
91
92 Current limitations: we don't support RAGABA and ARAGAB formats supported
93 by Mac OS X. If there is sufficient interest, these classes could be
94 extended to deal with them. Neither do we support alpha channel having
95 different representation from the RGB ones (happens under QNX/Photon I
96 think), but again this could be achieved with some small extra effort.
97
98 Template parameters are:
99 - type of a single pixel component
100 - size of the single pixel in bits
101 - indices of red, green and blue pixel components inside the pixel
102 - index of the alpha component or -1 if none
103 - type which can contain the full pixel value (all channels)
104 */
105
106 template <class Channel,
107 size_t Bpp, int R, int G, int B, int A = -1,
108 class Pixel = wxUint32>
109
110 struct WXDLLEXPORT wxPixelFormat
111 {
112 // iterator over pixels is usually of type "ChannelType *"
113 typedef Channel ChannelType;
114
115 // the type which may hold the entire pixel value
116 typedef Pixel PixelType;
117
118 // NB: using static ints initialized inside the class declaration is not
119 // portable as it doesn't work with VC++ 6, so we must use enums
120
121 // size of one pixel in bits
122 enum { BitsPerPixel = Bpp };
123
124 // size of one pixel in ChannelType units (usually bytes)
125 enum { SizePixel = Bpp / (8 * sizeof(Channel)) };
126
127 // the channels indices inside the pixel
128 enum
129 {
130 RED = R,
131 GREEN = G,
132 BLUE = B,
133 ALPHA = A
134 };
135
136 // true if we have an alpha channel (together with the other channels, this
137 // doesn't cover the case of wxImage which stores alpha separately)
138 enum { HasAlpha = A != -1 };
139 };
140
141 // some "predefined" pixel formats
142 // -------------------------------
143
144 // wxImage format is common to all platforms
145 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxImagePixelFormat;
146
147 // the (most common) native bitmap format without alpha support
148 #if defined(__WXMSW__)
149 // under MSW the RGB components are inversed, they're in BGR order
150 typedef wxPixelFormat<unsigned char, 24, 2, 1, 0> wxNativePixelFormat;
151
152 #define wxPIXEL_FORMAT_ALPHA 3
153 #elif defined(__WXMAC__)
154 // under Mac, first component is unused but still present, hence we use
155 // 32bpp, not 24
156 typedef wxPixelFormat<unsigned char, 32, 1, 2, 3> wxNativePixelFormat;
157
158 #define wxPIXEL_FORMAT_ALPHA 0
159 #elif defined(__WXCOCOA__)
160 // Cocoa is standard RGB or RGBA (normally it is RGBA)
161 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
162
163 #define wxPIXEL_FORMAT_ALPHA 3
164 #elif defined(__WXGTK__)
165 // Under GTK+ 2.X we use GdkPixbuf, which should be RGBA
166 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
167
168 #define wxPIXEL_FORMAT_ALPHA 3
169 #endif
170
171 // the (most common) native format for bitmaps with alpha channel
172 #ifdef wxPIXEL_FORMAT_ALPHA
173 typedef wxPixelFormat<unsigned char, 32,
174 wxNativePixelFormat::RED,
175 wxNativePixelFormat::GREEN,
176 wxNativePixelFormat::BLUE,
177 wxPIXEL_FORMAT_ALPHA> wxAlphaPixelFormat;
178 #endif // wxPIXEL_FORMAT_ALPHA
179
180 // we also define the (default/best) pixel format for the given class: this is
181 // used as default value for the pixel format in wxPixelIterator template
182 template <class T> struct wxPixelFormatFor;
183
184 #if wxUSE_IMAGE
185 // wxPixelFormatFor is only defined for wxImage, attempt to use it with other
186 // classes (wxBitmap...) will result in compile errors which is exactly what we
187 // want
188 template <>
189 struct WXDLLEXPORT wxPixelFormatFor<wxImage>
190 {
191 typedef wxImagePixelFormat Format;
192 };
193 #endif //wxUSE_IMAGE
194
195 // ----------------------------------------------------------------------------
196 // wxPixelData
197 // ----------------------------------------------------------------------------
198
199 /*
200 wxPixelDataBase is just a helper for wxPixelData: it contains things common
201 to both wxImage and wxBitmap specializations.
202 */
203 class WXDLLEXPORT wxPixelDataBase
204 {
205 public:
206 // origin of the rectangular region we represent
207 wxPoint GetOrigin() const { return m_ptOrigin; }
208
209 // width and height of the region we represent
210 int GetWidth() const { return m_width; }
211 int GetHeight() const { return m_height; }
212
213 wxSize GetSize() const { return wxSize(m_width, m_height); }
214
215 // the distance between two rows
216 int GetRowStride() const { return m_stride; }
217
218 // private: -- see comment in the beginning of the file
219
220 // the origin of this image inside the bigger bitmap (usually (0, 0))
221 wxPoint m_ptOrigin;
222
223 // the size of the image we address, in pixels
224 int m_width,
225 m_height;
226
227 // this parameter is the offset of the start of the (N+1)st row from the
228 // Nth one and can be different from m_bypp*width in some cases:
229 // a) the most usual one is to force 32/64 bit alignment of rows
230 // b) another one is for bottom-to-top images where it's negative
231 // c) finally, it could conceivably be 0 for the images with all
232 // lines being identical
233 int m_stride;
234
235 protected:
236 // ctor is protected because this class is only meant to be used as the
237 // base class by wxPixelData
238 wxPixelDataBase()
239 {
240 m_width =
241 m_height =
242 m_stride = 0;
243 }
244 };
245
246 /*
247 wxPixelData represents the entire bitmap data, i.e. unlike
248 wxPixelFormat (which it uses) it also stores the global bitmap
249 characteristics such as its size, inter-row separation and so on.
250
251 Because of this it can be used to move the pixel iterators (which don't
252 have enough information about the bitmap themselves). This may seem a bit
253 unnatural but must be done in this way to keep the iterator objects as
254 small as possible for maximum efficiency as otherwise they wouldn't be put
255 into the CPU registers by the compiler any more.
256
257 Implementation note: we use the standard workaround for lack of partial
258 template specialization support in VC (both 6 and 7): instead of partly
259 specializing the class Foo<T, U> for some T we introduce FooOut<T> and
260 FooIn<U> nested in it, make Foo<T, U> equivalent to FooOut<T>::FooIn<U> and
261 fully specialize FooOut.
262
263 Also note that this class doesn't have any default definition because we
264 can't really do anything without knowing the exact image class. We do
265 provide wxPixelDataBase to make it simpler to write new wxPixelData
266 specializations.
267 */
268
269 // we need to define this skeleton template to mollify VC++
270 template <class Image>
271 struct WXDLLEXPORT wxPixelDataOut
272 {
273 template <class PixelFormat>
274 class WXDLLEXPORT wxPixelDataIn
275 {
276 public:
277 class Iterator { };
278 };
279 };
280
281 #if wxUSE_IMAGE
282 // wxPixelData specialization for wxImage: this is the simplest case as we
283 // don't have to care about different pixel formats here
284 template <>
285 struct WXDLLEXPORT wxPixelDataOut<wxImage>
286 {
287 // NB: this is a template class even though it doesn't use its template
288 // parameter because otherwise wxPixelData couldn't compile
289 template <class dummyPixelFormat>
290 class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
291 {
292 public:
293 // the type of the class we're working with
294 typedef wxImage ImageType;
295
296 // the iterator which should be used for working with data in this
297 // format
298 class Iterator
299 {
300 public:
301 // the pixel format we use
302 typedef wxImagePixelFormat PixelFormat;
303
304 // the type of the pixel components
305 typedef typename dummyPixelFormat::ChannelType ChannelType;
306
307 // the pixel data we're working with
308 typedef
309 wxPixelDataOut<wxImage>::wxPixelDataIn<PixelFormat> PixelData;
310
311 // go back to (0, 0)
312 void Reset(const PixelData& data)
313 {
314 *this = data.GetPixels();
315 }
316
317 // creates the iterator pointing to the beginning of data
318 Iterator(PixelData& data)
319 {
320 Reset(data);
321 }
322
323 // creates the iterator initially pointing to the image origin
324 Iterator(const wxImage& image)
325 {
326 m_pRGB = image.GetData();
327
328 if ( image.HasAlpha() )
329 {
330 m_pAlpha = image.GetAlpha();
331 }
332 else // alpha is not used at all
333 {
334 m_pAlpha = NULL;
335 }
336 }
337
338 // true if the iterator is valid
339 bool IsOk() const { return m_pRGB != NULL; }
340
341
342 // navigation
343 // ----------
344
345 // advance the iterator to the next pixel, prefix version
346 Iterator& operator++()
347 {
348 m_pRGB += PixelFormat::SizePixel;
349 if ( m_pAlpha )
350 ++m_pAlpha;
351
352 return *this;
353 }
354
355 // postfix (hence less efficient -- don't use it unless you
356 // absolutely must) version
357 Iterator operator++(int)
358 {
359 Iterator p(*this);
360 ++*this;
361 return p;
362 }
363
364 // move x pixels to the right and y down
365 //
366 // note that the rows don't wrap!
367 void Offset(const PixelData& data, int x, int y)
368 {
369 m_pRGB += data.GetRowStride()*y + PixelFormat::SizePixel*x;
370 if ( m_pAlpha )
371 m_pAlpha += data.GetWidth() + x;
372 }
373
374 // move x pixels to the right (again, no row wrapping)
375 void OffsetX(const PixelData& WXUNUSED(data), int x)
376 {
377 m_pRGB += PixelFormat::SizePixel*x;
378 if ( m_pAlpha )
379 m_pAlpha += x;
380 }
381
382 // move y rows to the bottom
383 void OffsetY(const PixelData& data, int y)
384 {
385 m_pRGB += data.GetRowStride()*y;
386 if ( m_pAlpha )
387 m_pAlpha += data.GetWidth();
388 }
389
390 // go to the given position
391 void MoveTo(const PixelData& data, int x, int y)
392 {
393 Reset(data);
394 Offset(data, x, y);
395 }
396
397
398 // data access
399 // -----------
400
401 // access to invidividual colour components
402 ChannelType& Red() { return m_pRGB[PixelFormat::RED]; }
403 ChannelType& Green() { return m_pRGB[PixelFormat::GREEN]; }
404 ChannelType& Blue() { return m_pRGB[PixelFormat::BLUE]; }
405 ChannelType& Alpha() { return *m_pAlpha; }
406
407 // private: -- see comment in the beginning of the file
408
409 // pointer into RGB buffer
410 unsigned char *m_pRGB;
411
412 // pointer into alpha buffer or NULL if alpha isn't used
413 unsigned char *m_pAlpha;
414 };
415
416 // initializes us with the data of the given image
417 wxPixelDataIn(ImageType& image) : m_image(image), m_pixels(image)
418 {
419 m_width = image.GetWidth();
420 m_height = image.GetHeight();
421 m_stride = Iterator::SizePixel * m_width;
422 }
423
424 // initializes us with the given region of the specified image
425 wxPixelDataIn(ImageType& image,
426 const wxPoint& pt,
427 const wxSize& sz) : m_image(image), m_pixels(image)
428 {
429 m_stride = Iterator::SizePixel * m_width;
430
431 InitRect(pt, sz);
432 }
433
434 // initializes us with the given region of the specified image
435 wxPixelDataIn(ImageType& image,
436 const wxRect& rect) : m_image(image), m_pixels(image)
437 {
438 m_stride = Iterator::SizePixel * m_width;
439
440 InitRect(rect.GetPosition(), rect.GetSize());
441 }
442
443 // we evaluate to true only if we could get access to bitmap data
444 // successfully
445 operator bool() const { return m_pixels.IsOk(); }
446
447 // get the iterator pointing to the origin
448 Iterator GetPixels() const { return m_pixels; }
449
450 private:
451 void InitRect(const wxPoint& pt, const wxSize& sz)
452 {
453 m_width = sz.x;
454 m_height = sz.y;
455
456 m_ptOrigin = pt;
457 m_pixels.Offset(*this, pt.x, pt.y);
458 }
459
460 // the image we're working with
461 ImageType& m_image;
462
463 // the iterator pointing to the image origin
464 Iterator m_pixels;
465 };
466 };
467 #endif //wxUSE_IMAGE
468
469 #if wxUSE_GUI
470 // wxPixelData specialization for wxBitmap: here things are more interesting as
471 // we also have to support different pixel formats
472 template <>
473 struct WXDLLEXPORT wxPixelDataOut<wxBitmap>
474 {
475 template <class Format>
476 class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
477 {
478 public:
479 // the type of the class we're working with
480 typedef wxBitmap ImageType;
481
482 class WXDLLEXPORT Iterator
483 {
484 public:
485 // the pixel format we use
486 typedef Format PixelFormat;
487
488 // the type of the pixel components
489 typedef typename PixelFormat::ChannelType ChannelType;
490
491 // the pixel data we're working with
492 typedef wxPixelDataOut<wxBitmap>::wxPixelDataIn<Format> PixelData;
493
494
495 // go back to (0, 0)
496 void Reset(const PixelData& data)
497 {
498 *this = data.GetPixels();
499 }
500
501 // initializes the iterator to point to the origin of the given
502 // pixel data
503 Iterator(PixelData& data)
504 {
505 Reset(data);
506 }
507
508 // initializes the iterator to point to the origin of the given
509 // bitmap
510 Iterator(wxBitmap& bmp, PixelData& data)
511 {
512 // using cast here is ugly but it should be safe as
513 // GetRawData() real return type should be consistent with
514 // BitsPerPixel (which is in turn defined by ChannelType) and
515 // this is the only thing we can do without making GetRawData()
516 // a template function which is undesirable
517 m_ptr = (ChannelType *)
518 bmp.GetRawData(data, PixelFormat::BitsPerPixel);
519 }
520
521 // return true if this iterator is valid
522 bool IsOk() const { return m_ptr != NULL; }
523
524
525 // navigation
526 // ----------
527
528 // advance the iterator to the next pixel, prefix version
529 Iterator& operator++()
530 {
531 m_ptr += PixelFormat::SizePixel;
532
533 return *this;
534 }
535
536 // postfix (hence less efficient -- don't use it unless you
537 // absolutely must) version
538 Iterator operator++(int)
539 {
540 Iterator p(*this);
541 ++*this;
542 return p;
543 }
544
545 // move x pixels to the right and y down
546 //
547 // note that the rows don't wrap!
548 void Offset(const PixelData& data, int x, int y)
549 {
550 m_ptr += data.GetRowStride()*y + PixelFormat::SizePixel*x;
551 }
552
553 // move x pixels to the right (again, no row wrapping)
554 void OffsetX(const PixelData& WXUNUSED(data), int x)
555 {
556 m_ptr += PixelFormat::SizePixel*x;
557 }
558
559 // move y rows to the bottom
560 void OffsetY(const PixelData& data, int y)
561 {
562 m_ptr += data.GetRowStride()*y;
563 }
564
565 // go to the given position
566 void MoveTo(const PixelData& data, int x, int y)
567 {
568 Reset(data);
569 Offset(data, x, y);
570 }
571
572
573 // data access
574 // -----------
575
576 // access to invidividual colour components
577 ChannelType& Red() { return m_ptr[PixelFormat::RED]; }
578 ChannelType& Green() { return m_ptr[PixelFormat::GREEN]; }
579 ChannelType& Blue() { return m_ptr[PixelFormat::BLUE]; }
580 ChannelType& Alpha() { return m_ptr[PixelFormat::ALPHA]; }
581
582 // address the pixel contents directly
583 //
584 // warning: the format is platform dependent
585 typename PixelFormat::PixelType& Data()
586 { return *(typename PixelFormat::PixelType *)m_ptr; }
587
588 // private: -- see comment in the beginning of the file
589
590 // for efficiency reasons this class should not have any other
591 // fields, otherwise it won't be put into a CPU register (as it
592 // should inside the inner loops) by some compilers, notably gcc
593 ChannelType *m_ptr;
594 };
595
596 // ctor associates this pointer with a bitmap and locks the bitmap for
597 // raw access, it will be unlocked only by our dtor and so these
598 // objects should normally be only created on the stack, i.e. have
599 // limited life-time
600 wxPixelDataIn(wxBitmap& bmp) : m_bmp(bmp), m_pixels(bmp, *this)
601 {
602 }
603
604 wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
605 : m_bmp(bmp), m_pixels(bmp, *this)
606 {
607 InitRect(rect.GetPosition(), rect.GetSize());
608 }
609
610 wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
611 : m_bmp(bmp), m_pixels(bmp, *this)
612 {
613 InitRect(pt, sz);
614 }
615
616 // we evaluate to true only if we could get access to bitmap data
617 // successfully
618 operator bool() const { return m_pixels.IsOk(); }
619
620 // get the iterator pointing to the origin
621 Iterator GetPixels() const { return m_pixels; }
622
623 // dtor unlocks the bitmap
624 ~wxPixelDataIn()
625 {
626 m_bmp.UngetRawData(*this);
627 }
628
629 // call this to indicate that we should use the alpha channel
630 void UseAlpha() { m_bmp.UseAlpha(); }
631
632 // private: -- see comment in the beginning of the file
633
634 // the bitmap we're associated with
635 wxBitmap m_bmp;
636
637 // the iterator pointing to the image origin
638 Iterator m_pixels;
639
640 private:
641 void InitRect(const wxPoint& pt, const wxSize& sz)
642 {
643 m_pixels.Offset(*this, pt.x, pt.y);
644
645 m_ptOrigin = pt;
646 m_width = sz.x;
647 m_height = sz.y;
648 }
649 };
650 };
651 #endif //wxUSE_GUI
652
653 #ifdef __VISUALC__
654 // typedef-name 'foo' used as synonym for class-name 'bar'
655 // (VC++ gives this warning each time wxPixelData::Base is used but it
656 // doesn't make any sense here -- what's wrong with using typedef instead
657 // of class, this is what it is here for!)
658 #pragma warning(disable: 4097)
659 #endif // __VISUALC__
660
661 template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
662 class wxPixelData :
663 public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
664 {
665 public:
666 typedef
667 typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
668 Base;
669
670 wxPixelData(Image& image) : Base(image) { }
671
672 wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
673
674 wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
675 : Base(i, pt, sz)
676 {
677 }
678 };
679
680
681 // some "predefined" pixel data classes
682 #if wxUSE_IMAGE
683 typedef wxPixelData<wxImage> wxImagePixelData;
684 #endif //wxUSE_IMAGE
685 #if wxUSE_GUI
686 typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
687 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
688 #endif //wxUSE_GUI
689
690 // ----------------------------------------------------------------------------
691 // wxPixelIterator
692 // ----------------------------------------------------------------------------
693
694 /*
695 wxPixel::Iterator represents something which points to the pixel data and
696 allows us to iterate over it. In the simplest case of wxBitmap it is,
697 indeed, just a pointer, but it can be something more complicated and,
698 moreover, you are free to specialize it for other image classes and bitmap
699 formats.
700
701 Note that although it would have been much more intuitive to have a real
702 class here instead of what we have now, this class would need two template
703 parameters, and this can't be done because we'd need compiler support for
704 partial template specialization then and neither VC6 nor VC7 provide it.
705 */
706 template < class Image, class PixelFormat = wxPixelFormatFor<Image> >
707 struct WXDLLEXPORT wxPixelIterator : wxPixelData<Image, PixelFormat>::Iterator
708 {
709 };
710
711 #ifdef __VISUALC__
712 #pragma warning(default: 4355)
713 #pragma warning(default: 4097)
714 #endif
715
716 #endif // _WX_RAWBMP_H_BASE_
717