]> git.saurik.com Git - wxWidgets.git/blob - include/wx/rawbmp.h
made FindWindow() member functions const (this makes it possible to use XRCCTRL(...
[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 #endif
165
166 // the (most common) native format for bitmaps with alpha channel
167 #ifdef wxPIXEL_FORMAT_ALPHA
168 typedef wxPixelFormat<unsigned char, 32,
169 wxNativePixelFormat::RED,
170 wxNativePixelFormat::GREEN,
171 wxNativePixelFormat::BLUE,
172 wxPIXEL_FORMAT_ALPHA> wxAlphaPixelFormat;
173 #endif // wxPIXEL_FORMAT_ALPHA
174
175 // we also define the (default/best) pixel format for the given class: this is
176 // used as default value for the pixel format in wxPixelIterator template
177 template <class T> struct wxPixelFormatFor;
178
179 #if wxUSE_IMAGE
180 // wxPixelFormatFor is only defined for wxImage, attempt to use it with other
181 // classes (wxBitmap...) will result in compile errors which is exactly what we
182 // want
183 template <>
184 struct WXDLLEXPORT wxPixelFormatFor<wxImage>
185 {
186 typedef wxImagePixelFormat Format;
187 };
188 #endif //wxUSE_IMAGE
189
190 // ----------------------------------------------------------------------------
191 // wxPixelData
192 // ----------------------------------------------------------------------------
193
194 /*
195 wxPixelDataBase is just a helper for wxPixelData: it contains things common
196 to both wxImage and wxBitmap specializations.
197 */
198 class WXDLLEXPORT wxPixelDataBase
199 {
200 public:
201 // origin of the rectangular region we represent
202 wxPoint GetOrigin() const { return m_ptOrigin; }
203
204 // width and height of the region we represent
205 int GetWidth() const { return m_width; }
206 int GetHeight() const { return m_height; }
207
208 wxSize GetSize() const { return wxSize(m_width, m_height); }
209
210 // the distance between two rows
211 int GetRowStride() const { return m_stride; }
212
213 // private: -- see comment in the beginning of the file
214
215 // the origin of this image inside the bigger bitmap (usually (0, 0))
216 wxPoint m_ptOrigin;
217
218 // the size of the image we address, in pixels
219 int m_width,
220 m_height;
221
222 // this parameter is the offset of the start of the (N+1)st row from the
223 // Nth one and can be different from m_bypp*width in some cases:
224 // a) the most usual one is to force 32/64 bit alignment of rows
225 // b) another one is for bottom-to-top images where it's negative
226 // c) finally, it could conceivably be 0 for the images with all
227 // lines being identical
228 int m_stride;
229
230 protected:
231 // ctor is protected because this class is only meant to be used as the
232 // base class by wxPixelData
233 wxPixelDataBase()
234 {
235 m_width =
236 m_height =
237 m_stride = 0;
238 }
239 };
240
241 /*
242 wxPixelData represents the entire bitmap data, i.e. unlike
243 wxPixelFormat (which it uses) it also stores the global bitmap
244 characteristics such as its size, inter-row separation and so on.
245
246 Because of this it can be used to move the pixel iterators (which don't
247 have enough information about the bitmap themselves). This may seem a bit
248 unnatural but must be done in this way to keep the iterator objects as
249 small as possible for maximum efficiency as otherwise they wouldn't be put
250 into the CPU registers by the compiler any more.
251
252 Implementation note: we use the standard workaround for lack of partial
253 template specialization support in VC (both 6 and 7): instead of partly
254 specializing the class Foo<T, U> for some T we introduce FooOut<T> and
255 FooIn<U> nested in it, make Foo<T, U> equivalent to FooOut<T>::FooIn<U> and
256 fully specialize FooOut.
257
258 Also note that this class doesn't have any default definition because we
259 can't really do anything without knowing the exact image class. We do
260 provide wxPixelDataBase to make it simpler to write new wxPixelData
261 specializations.
262 */
263
264 // we need to define this skeleton template to mollify VC++
265 template <class Image>
266 struct WXDLLEXPORT wxPixelDataOut
267 {
268 template <class PixelFormat>
269 class WXDLLEXPORT wxPixelDataIn
270 {
271 public:
272 class Iterator { };
273 };
274 };
275
276 #if wxUSE_IMAGE
277 // wxPixelData specialization for wxImage: this is the simplest case as we
278 // don't have to care about different pixel formats here
279 template <>
280 struct WXDLLEXPORT wxPixelDataOut<wxImage>
281 {
282 // NB: this is a template class even though it doesn't use its template
283 // parameter because otherwise wxPixelData couldn't compile
284 template <class dummyPixelFormat>
285 class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
286 {
287 // the type of the class we're working with
288 typedef wxImage ImageType;
289
290 // the iterator which should be used for working with data in this
291 // format
292 class Iterator
293 {
294 public:
295 // the pixel format we use
296 typedef wxImagePixelFormat PixelFormat;
297
298 // the type of the pixel components
299 typedef typename dummyPixelFormat::ChannelType ChannelType;
300
301 // the pixel data we're working with
302 typedef
303 wxPixelDataOut<wxImage>::wxPixelDataIn<PixelFormat> PixelData;
304
305 // go back to (0, 0)
306 void Reset(const PixelData& data)
307 {
308 *this = data.GetPixels();
309 }
310
311 // creates the iterator pointing to the beginning of data
312 Iterator(PixelData& data)
313 {
314 Reset(data);
315 }
316
317 // creates the iterator initially pointing to the image origin
318 Iterator(const wxImage& image)
319 {
320 m_pRGB = image.GetData();
321
322 if ( image.HasAlpha() )
323 {
324 m_pAlpha = image.GetAlpha();
325 }
326 else // alpha is not used at all
327 {
328 m_pAlpha = NULL;
329 }
330 }
331
332 // true if the iterator is valid
333 bool IsOk() const { return m_pRGB != NULL; }
334
335
336 // navigation
337 // ----------
338
339 // advance the iterator to the next pixel, prefix version
340 Iterator& operator++()
341 {
342 m_pRGB += PixelFormat::SizePixel;
343 if ( m_pAlpha )
344 ++m_pAlpha;
345
346 return *this;
347 }
348
349 // postfix (hence less efficient -- don't use it unless you
350 // absolutely must) version
351 Iterator operator++(int)
352 {
353 Iterator p(*this);
354 ++*this;
355 return p;
356 }
357
358 // move x pixels to the right and y down
359 //
360 // note that the rows don't wrap!
361 void Offset(const PixelData& data, int x, int y)
362 {
363 m_pRGB += data.GetRowStride()*y + PixelFormat::SizePixel*x;
364 if ( m_pAlpha )
365 m_pAlpha += data.GetWidth() + x;
366 }
367
368 // move x pixels to the right (again, no row wrapping)
369 void OffsetX(const PixelData& WXUNUSED(data), int x)
370 {
371 m_pRGB += PixelFormat::SizePixel*x;
372 if ( m_pAlpha )
373 m_pAlpha += x;
374 }
375
376 // move y rows to the bottom
377 void OffsetY(const PixelData& data, int y)
378 {
379 m_pRGB += data.GetRowStride()*y;
380 if ( m_pAlpha )
381 m_pAlpha += data.GetWidth();
382 }
383
384 // go to the given position
385 void MoveTo(const PixelData& data, int x, int y)
386 {
387 Reset(data);
388 Offset(data, x, y);
389 }
390
391
392 // data access
393 // -----------
394
395 // access to invidividual colour components
396 ChannelType& Red() { return m_pRGB[PixelFormat::RED]; }
397 ChannelType& Green() { return m_pRGB[PixelFormat::GREEN]; }
398 ChannelType& Blue() { return m_pRGB[PixelFormat::BLUE]; }
399 ChannelType& Alpha() { return *m_pAlpha; }
400
401 // private: -- see comment in the beginning of the file
402
403 // pointer into RGB buffer
404 unsigned char *m_pRGB;
405
406 // pointer into alpha buffer or NULL if alpha isn't used
407 unsigned char *m_pAlpha;
408 };
409
410 // initializes us with the data of the given image
411 wxPixelDataIn(ImageType& image) : m_image(image), m_pixels(image)
412 {
413 m_width = image.GetWidth();
414 m_height = image.GetHeight();
415 m_stride = Iterator::SizePixel * m_width;
416 }
417
418 // initializes us with the given region of the specified image
419 wxPixelDataIn(ImageType& image,
420 const wxPoint& pt,
421 const wxSize& sz) : m_image(image), m_pixels(image)
422 {
423 m_stride = Iterator::SizePixel * m_width;
424
425 InitRect(pt, sz);
426 }
427
428 // initializes us with the given region of the specified image
429 wxPixelDataIn(ImageType& image,
430 const wxRect& rect) : m_image(image), m_pixels(image)
431 {
432 m_stride = Iterator::SizePixel * m_width;
433
434 InitRect(rect.GetPosition(), rect.GetSize());
435 }
436
437 // we evaluate to true only if we could get access to bitmap data
438 // successfully
439 operator bool() const { return m_pixels.IsOk(); }
440
441 // get the iterator pointing to the origin
442 Iterator GetPixels() const { return m_pixels; }
443
444 private:
445 void InitRect(const wxPoint& pt, const wxSize& sz)
446 {
447 m_width = sz.x;
448 m_height = sz.y;
449
450 m_ptOrigin = pt;
451 m_pixels.Offset(*this, pt.x, pt.y);
452 }
453
454 // the image we're working with
455 ImageType& m_image;
456
457 // the iterator pointing to the image origin
458 Iterator m_pixels;
459 };
460 };
461 #endif //wxUSE_IMAGE
462
463 #if wxUSE_GUI
464 // wxPixelData specialization for wxBitmap: here things are more interesting as
465 // we also have to support different pixel formats
466 template <>
467 struct WXDLLEXPORT wxPixelDataOut<wxBitmap>
468 {
469 template <class Format>
470 class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
471 {
472 public:
473 // the type of the class we're working with
474 typedef wxBitmap ImageType;
475
476 class WXDLLEXPORT Iterator
477 {
478 public:
479 // the pixel format we use
480 typedef Format PixelFormat;
481
482 // the type of the pixel components
483 typedef typename PixelFormat::ChannelType ChannelType;
484
485 // the pixel data we're working with
486 typedef wxPixelDataOut<wxBitmap>::wxPixelDataIn<Format> PixelData;
487
488
489 // go back to (0, 0)
490 void Reset(const PixelData& data)
491 {
492 *this = data.GetPixels();
493 }
494
495 // initializes the iterator to point to the origin of the given
496 // pixel data
497 Iterator(PixelData& data)
498 {
499 Reset(data);
500 }
501
502 // initializes the iterator to point to the origin of the given
503 // bitmap
504 Iterator(wxBitmap& bmp, PixelData& data)
505 {
506 // using cast here is ugly but it should be safe as
507 // GetRawData() real return type should be consistent with
508 // BitsPerPixel (which is in turn defined by ChannelType) and
509 // this is the only thing we can do without making GetRawData()
510 // a template function which is undesirable
511 m_ptr = (ChannelType *)
512 bmp.GetRawData(data, PixelFormat::BitsPerPixel);
513 }
514
515 // return true if this iterator is valid
516 bool IsOk() const { return m_ptr != NULL; }
517
518
519 // navigation
520 // ----------
521
522 // advance the iterator to the next pixel, prefix version
523 Iterator& operator++()
524 {
525 m_ptr += PixelFormat::SizePixel;
526
527 return *this;
528 }
529
530 // postfix (hence less efficient -- don't use it unless you
531 // absolutely must) version
532 Iterator operator++(int)
533 {
534 Iterator p(*this);
535 ++*this;
536 return p;
537 }
538
539 // move x pixels to the right and y down
540 //
541 // note that the rows don't wrap!
542 void Offset(const PixelData& data, int x, int y)
543 {
544 m_ptr += data.GetRowStride()*y + PixelFormat::SizePixel*x;
545 }
546
547 // move x pixels to the right (again, no row wrapping)
548 void OffsetX(const PixelData& WXUNUSED(data), int x)
549 {
550 m_ptr += PixelFormat::SizePixel*x;
551 }
552
553 // move y rows to the bottom
554 void OffsetY(const PixelData& data, int y)
555 {
556 m_ptr += data.GetRowStride()*y;
557 }
558
559 // go to the given position
560 void MoveTo(const PixelData& data, int x, int y)
561 {
562 Reset(data);
563 Offset(data, x, y);
564 }
565
566
567 // data access
568 // -----------
569
570 // access to invidividual colour components
571 ChannelType& Red() { return m_ptr[PixelFormat::RED]; }
572 ChannelType& Green() { return m_ptr[PixelFormat::GREEN]; }
573 ChannelType& Blue() { return m_ptr[PixelFormat::BLUE]; }
574 ChannelType& Alpha() { return m_ptr[PixelFormat::ALPHA]; }
575
576 // address the pixel contents directly
577 //
578 // warning: the format is platform dependent
579 typename PixelFormat::PixelType& Data()
580 { return *(typename PixelFormat::PixelType *)m_ptr; }
581
582 // private: -- see comment in the beginning of the file
583
584 // for efficiency reasons this class should not have any other
585 // fields, otherwise it won't be put into a CPU register (as it
586 // should inside the inner loops) by some compilers, notably gcc
587 ChannelType *m_ptr;
588 };
589
590 // ctor associates this pointer with a bitmap and locks the bitmap for
591 // raw access, it will be unlocked only by our dtor and so these
592 // objects should normally be only created on the stack, i.e. have
593 // limited life-time
594 wxPixelDataIn(wxBitmap& bmp) : m_bmp(bmp), m_pixels(bmp, *this)
595 {
596 }
597
598 wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
599 : m_bmp(bmp), m_pixels(bmp, *this)
600 {
601 InitRect(rect.GetPosition(), rect.GetSize());
602 }
603
604 wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
605 : m_bmp(bmp), m_pixels(bmp, *this)
606 {
607 InitRect(pt, sz);
608 }
609
610 // we evaluate to true only if we could get access to bitmap data
611 // successfully
612 operator bool() const { return m_pixels.IsOk(); }
613
614 // get the iterator pointing to the origin
615 Iterator GetPixels() const { return m_pixels; }
616
617 // dtor unlocks the bitmap
618 ~wxPixelDataIn()
619 {
620 m_bmp.UngetRawData(*this);
621 }
622
623 // call this to indicate that we should use the alpha channel
624 void UseAlpha() { m_bmp.UseAlpha(); }
625
626 // private: -- see comment in the beginning of the file
627
628 // the bitmap we're associated with
629 wxBitmap m_bmp;
630
631 // the iterator pointing to the image origin
632 Iterator m_pixels;
633
634 private:
635 void InitRect(const wxPoint& pt, const wxSize& sz)
636 {
637 m_pixels.Offset(*this, pt.x, pt.y);
638
639 m_ptOrigin = pt;
640 m_width = sz.x;
641 m_height = sz.y;
642 }
643 };
644 };
645 #endif //wxUSE_GUI
646
647 #ifdef __VISUALC__
648 // typedef-name 'foo' used as synonym for class-name 'bar'
649 // (VC++ gives this warning each time wxPixelData::Base is used but it
650 // doesn't make any sense here -- what's wrong with using typedef instead
651 // of class, this is what it is here for!)
652 #pragma warning(disable: 4097)
653 #endif // __VISUALC__
654
655 template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
656 class wxPixelData :
657 public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
658 {
659 public:
660 typedef
661 typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
662 Base;
663
664 wxPixelData(Image& image) : Base(image) { }
665
666 wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
667
668 wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
669 : Base(i, pt, sz)
670 {
671 }
672 };
673
674
675 // some "predefined" pixel data classes
676 #if wxUSE_IMAGE
677 typedef wxPixelData<wxImage> wxImagePixelData;
678 #endif //wxUSE_IMAGE
679 #if wxUSE_GUI
680 typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
681 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
682 #endif //wxUSE_GUI
683
684 // ----------------------------------------------------------------------------
685 // wxPixelIterator
686 // ----------------------------------------------------------------------------
687
688 /*
689 wxPixel::Iterator represents something which points to the pixel data and
690 allows us to iterate over it. In the simplest case of wxBitmap it is,
691 indeed, just a pointer, but it can be something more complicated and,
692 moreover, you are free to specialize it for other image classes and bitmap
693 formats.
694
695 Note that although it would have been much more intuitive to have a real
696 class here instead of what we have now, this class would need two template
697 parameters, and this can't be done because we'd need compiler support for
698 partial template specialization then and neither VC6 nor VC7 provide it.
699 */
700 template < class Image, class PixelFormat = wxPixelFormatFor<Image> >
701 struct WXDLLEXPORT wxPixelIterator : wxPixelData<Image, PixelFormat>::Iterator
702 {
703 };
704
705 #ifdef __VISUALC__
706 #pragma warning(default: 4355)
707 #pragma warning(default: 4097)
708 #endif
709
710 #endif // _WX_RAWBMP_H_BASE_
711