]> git.saurik.com Git - wxWidgets.git/blob - include/wx/rawbmp.h
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[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 // ----------------------------------------------------------------------------
16 // Abstract Pixel API
17 //
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.
23 //
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
29 // functions instead.
30 // ----------------------------------------------------------------------------
31
32 /*
33 Usage example:
34
35 typedef wxPixelData<wxBitmap, wxNativePixelFormat> PixelData;
36
37 wxBitmap bmp;
38 PixelData data(bmp);
39 if ( !data )
40 {
41 ... raw access to bitmap data unavailable, do something else ...
42 return;
43 }
44
45 if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
46 {
47 ... complain: the bitmap it too small ...
48 return;
49 }
50
51 PixelData::Iterator p(data);
52
53 // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
54 p.Offset(data, 10, 10);
55
56 for ( int y = 0; y < 10; ++y )
57 {
58 PixelData::Iterator rowStart = p;
59
60 for ( int x = 0; x < 10; ++x, ++p )
61 {
62 p.Red() = r;
63 p.Green() = g;
64 p.Blue() = b;
65 }
66
67 p = rowStart;
68 p.OffsetY(data, 1);
69 }
70 */
71
72 #ifdef __VISUALC__
73 // VC++ gives an absolutely harmless warning for wxPixelData<wxBitmap> ctor
74 #pragma warning(disable: 4355) // 'this' used in initializer list
75 #endif
76
77 // ----------------------------------------------------------------------------
78 // wxPixelFormat
79 // ----------------------------------------------------------------------------
80
81 /*
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
88 done at compile-time.
89
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.
95
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)
102 */
103
104 template <class Channel,
105 size_t Bpp, int R, int G, int B, int A = -1,
106 class Pixel = wxUint32>
107
108 struct WXDLLEXPORT wxPixelFormat
109 {
110 // iterator over pixels is usually of type "ChannelType *"
111 typedef Channel ChannelType;
112
113 // the type which may hold the entire pixel value
114 typedef Pixel PixelType;
115
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
118
119 // size of one pixel in bits
120 enum { BitsPerPixel = Bpp };
121
122 // size of one pixel in ChannelType units (usually bytes)
123 enum { SizePixel = Bpp / (8 * sizeof(Channel)) };
124
125 // the channels indices inside the pixel
126 enum
127 {
128 RED = R,
129 GREEN = G,
130 BLUE = B,
131 ALPHA = A
132 };
133
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 };
137 };
138
139 // some "predefined" pixel formats
140 // -------------------------------
141
142 // wxImage format is common to all platforms
143 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxImagePixelFormat;
144
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;
149
150 #define wxPIXEL_FORMAT_ALPHA 3
151 #elif defined(__WXMAC__)
152 // under Mac, first component is unused but still present, hence we use
153 // 32bpp, not 24
154 typedef wxPixelFormat<unsigned char, 32, 1, 2, 3> wxNativePixelFormat;
155
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;
160
161 #define wxPIXEL_FORMAT_ALPHA 3
162 #endif
163
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
172
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;
176
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
179 // want
180 template <>
181 struct WXDLLEXPORT wxPixelFormatFor<wxImage>
182 {
183 typedef wxImagePixelFormat Format;
184 };
185
186 // ----------------------------------------------------------------------------
187 // wxPixelData
188 // ----------------------------------------------------------------------------
189
190 /*
191 wxPixelDataBase is just a helper for wxPixelData: it contains things common
192 to both wxImage and wxBitmap specializations.
193 */
194 class WXDLLEXPORT wxPixelDataBase
195 {
196 public:
197 // origin of the rectangular region we represent
198 wxPoint GetOrigin() const { return m_ptOrigin; }
199
200 // width and height of the region we represent
201 int GetWidth() const { return m_width; }
202 int GetHeight() const { return m_height; }
203
204 wxSize GetSize() const { return wxSize(m_width, m_height); }
205
206 // the distance between two rows
207 int GetRowStride() const { return m_stride; }
208
209 // private: -- see comment in the beginning of the file
210
211 // the origin of this image inside the bigger bitmap (usually (0, 0))
212 wxPoint m_ptOrigin;
213
214 // the size of the image we address, in pixels
215 int m_width,
216 m_height;
217
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
224 int m_stride;
225
226 protected:
227 // ctor is protected because this class is only meant to be used as the
228 // base class by wxPixelData
229 wxPixelDataBase()
230 {
231 m_width =
232 m_height =
233 m_stride = 0;
234 }
235 };
236
237 /*
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.
241
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.
247
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.
253
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
257 specializations.
258 */
259
260 // we need to define this skeleton template to mollify VC++
261 template <class Image>
262 struct WXDLLEXPORT wxPixelDataOut
263 {
264 template <class PixelFormat>
265 class WXDLLEXPORT wxPixelDataIn
266 {
267 public:
268 class Iterator { };
269 };
270 };
271
272 // wxPixelData specialization for wxImage: this is the simplest case as we
273 // don't have to care about different pixel formats here
274 template <>
275 struct WXDLLEXPORT wxPixelDataOut<wxImage>
276 {
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
281 {
282 // the type of the class we're working with
283 typedef wxImage ImageType;
284
285 // the iterator which should be used for working with data in this
286 // format
287 class Iterator
288 {
289 public:
290 // the pixel format we use
291 typedef wxImagePixelFormat PixelFormat;
292
293 // the type of the pixel components
294 typedef typename dummyPixelFormat::ChannelType ChannelType;
295
296 // the pixel data we're working with
297 typedef
298 wxPixelDataOut<wxImage>::wxPixelDataIn<PixelFormat> PixelData;
299
300 // go back to (0, 0)
301 void Reset(const PixelData& data)
302 {
303 *this = data.GetPixels();
304 }
305
306 // creates the iterator pointing to the beginning of data
307 Iterator(PixelData& data)
308 {
309 Reset(data);
310 }
311
312 // creates the iterator initially pointing to the image origin
313 Iterator(const wxImage& image)
314 {
315 m_pRGB = image.GetData();
316
317 if ( image.HasAlpha() )
318 {
319 m_pAlpha = image.GetAlpha();
320 }
321 else // alpha is not used at all
322 {
323 m_pAlpha = NULL;
324 }
325 }
326
327 // true if the iterator is valid
328 bool IsOk() const { return m_pRGB != NULL; }
329
330
331 // navigation
332 // ----------
333
334 // advance the iterator to the next pixel, prefix version
335 Iterator& operator++()
336 {
337 m_pRGB += PixelFormat::SizePixel;
338 if ( m_pAlpha )
339 ++m_pAlpha;
340
341 return *this;
342 }
343
344 // postfix (hence less efficient -- don't use it unless you
345 // absolutely must) version
346 Iterator operator++(int)
347 {
348 Iterator p(*this);
349 ++*this;
350 return p;
351 }
352
353 // move x pixels to the right and y down
354 //
355 // note that the rows don't wrap!
356 void Offset(const PixelData& data, int x, int y)
357 {
358 m_pRGB += data.GetRowStride()*y + PixelFormat::SizePixel*x;
359 if ( m_pAlpha )
360 m_pAlpha += data.GetWidth() + x;
361 }
362
363 // move x pixels to the right (again, no row wrapping)
364 void OffsetX(const PixelData& WXUNUSED(data), int x)
365 {
366 m_pRGB += PixelFormat::SizePixel*x;
367 if ( m_pAlpha )
368 m_pAlpha += x;
369 }
370
371 // move y rows to the bottom
372 void OffsetY(const PixelData& data, int y)
373 {
374 m_pRGB += data.GetRowStride()*y;
375 if ( m_pAlpha )
376 m_pAlpha += data.GetWidth();
377 }
378
379 // go to the given position
380 void MoveTo(const PixelData& data, int x, int y)
381 {
382 Reset(data);
383 Offset(data, x, y);
384 }
385
386
387 // data access
388 // -----------
389
390 // access to invidividual colour components
391 ChannelType& Red() { return m_pRGB[PixelFormat::RED]; }
392 ChannelType& Green() { return m_pRGB[PixelFormat::GREEN]; }
393 ChannelType& Blue() { return m_pRGB[PixelFormat::BLUE]; }
394 ChannelType& Alpha() { return *m_pAlpha; }
395
396 // private: -- see comment in the beginning of the file
397
398 // pointer into RGB buffer
399 unsigned char *m_pRGB;
400
401 // pointer into alpha buffer or NULL if alpha isn't used
402 unsigned char *m_pAlpha;
403 };
404
405 // initializes us with the data of the given image
406 wxPixelDataIn(ImageType& image) : m_image(image), m_pixels(image)
407 {
408 m_width = image.GetWidth();
409 m_height = image.GetHeight();
410 m_stride = Iterator::SizePixel * m_width;
411 }
412
413 // initializes us with the given region of the specified image
414 wxPixelDataIn(ImageType& image,
415 const wxPoint& pt,
416 const wxSize& sz) : m_image(image), m_pixels(image)
417 {
418 m_stride = Iterator::SizePixel * m_width;
419
420 InitRect(pt, sz);
421 }
422
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)
426 {
427 m_stride = Iterator::SizePixel * m_width;
428
429 InitRect(rect.GetPosition(), rect.GetSize());
430 }
431
432 // we evaluate to true only if we could get access to bitmap data
433 // successfully
434 operator bool() const { return m_pixels.IsOk(); }
435
436 // get the iterator pointing to the origin
437 Iterator GetPixels() const { return m_pixels; }
438
439 private:
440 void InitRect(const wxPoint& pt, const wxSize& sz)
441 {
442 m_width = sz.x;
443 m_height = sz.y;
444
445 m_ptOrigin = pt;
446 m_pixels.Offset(*this, pt.x, pt.y);
447 }
448
449 // the image we're working with
450 ImageType& m_image;
451
452 // the iterator pointing to the image origin
453 Iterator m_pixels;
454 };
455 };
456
457 // wxPixelData specialization for wxBitmap: here things are more interesting as
458 // we also have to support different pixel formats
459 template <>
460 struct WXDLLEXPORT wxPixelDataOut<wxBitmap>
461 {
462 template <class Format>
463 class WXDLLEXPORT wxPixelDataIn : public wxPixelDataBase
464 {
465 public:
466 // the type of the class we're working with
467 typedef wxBitmap ImageType;
468
469 class Iterator
470 {
471 public:
472 // the pixel format we use
473 typedef Format PixelFormat;
474
475 // the type of the pixel components
476 typedef typename PixelFormat::ChannelType ChannelType;
477
478 // the pixel data we're working with
479 typedef wxPixelDataOut<wxBitmap>::wxPixelDataIn<Format> PixelData;
480
481
482 // go back to (0, 0)
483 void Reset(const PixelData& data)
484 {
485 *this = data.GetPixels();
486 }
487
488 // initializes the iterator to point to the origin of the given
489 // pixel data
490 Iterator(PixelData& data)
491 {
492 Reset(data);
493 }
494
495 // initializes the iterator to point to the origin of the given
496 // bitmap
497 Iterator(wxBitmap& bmp, PixelData& data)
498 {
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);
506 }
507
508 // return true if this iterator is valid
509 bool IsOk() const { return m_ptr != NULL; }
510
511
512 // navigation
513 // ----------
514
515 // advance the iterator to the next pixel, prefix version
516 Iterator& operator++()
517 {
518 m_ptr += PixelFormat::SizePixel;
519
520 return *this;
521 }
522
523 // postfix (hence less efficient -- don't use it unless you
524 // absolutely must) version
525 Iterator operator++(int)
526 {
527 Iterator p(*this);
528 ++*this;
529 return p;
530 }
531
532 // move x pixels to the right and y down
533 //
534 // note that the rows don't wrap!
535 void Offset(const PixelData& data, int x, int y)
536 {
537 m_ptr += data.GetRowStride()*y + PixelFormat::SizePixel*x;
538 }
539
540 // move x pixels to the right (again, no row wrapping)
541 void OffsetX(const PixelData& WXUNUSED(data), int x)
542 {
543 m_ptr += PixelFormat::SizePixel*x;
544 }
545
546 // move y rows to the bottom
547 void OffsetY(const PixelData& data, int y)
548 {
549 m_ptr += data.GetRowStride()*y;
550 }
551
552 // go to the given position
553 void MoveTo(const PixelData& data, int x, int y)
554 {
555 Reset(data);
556 Offset(data, x, y);
557 }
558
559
560 // data access
561 // -----------
562
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]; }
568
569 // address the pixel contents directly
570 //
571 // warning: the format is platform dependent
572 typename PixelFormat::PixelType& Data()
573 { return *(typename PixelFormat::PixelType *)m_ptr; }
574
575 // private: -- see comment in the beginning of the file
576
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
580 ChannelType *m_ptr;
581 };
582
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
586 // limited life-time
587 wxPixelDataIn(wxBitmap& bmp) : m_bmp(bmp), m_pixels(bmp, *this)
588 {
589 }
590
591 wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
592 : m_bmp(bmp), m_pixels(bmp, *this)
593 {
594 InitRect(rect.GetPosition(), rect.GetSize());
595 }
596
597 wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
598 : m_bmp(bmp), m_pixels(bmp, *this)
599 {
600 InitRect(pt, sz);
601 }
602
603 // we evaluate to true only if we could get access to bitmap data
604 // successfully
605 operator bool() const { return m_pixels.IsOk(); }
606
607 // get the iterator pointing to the origin
608 Iterator GetPixels() const { return m_pixels; }
609
610 // dtor unlocks the bitmap
611 ~wxPixelDataIn()
612 {
613 m_bmp.UngetRawData(*this);
614 }
615
616 // call this to indicate that we should use the alpha channel
617 void UseAlpha() { m_bmp.UseAlpha(); }
618
619 // private: -- see comment in the beginning of the file
620
621 // the bitmap we're associated with
622 wxBitmap m_bmp;
623
624 // the iterator pointing to the image origin
625 Iterator m_pixels;
626
627 private:
628 void InitRect(const wxPoint& pt, const wxSize& sz)
629 {
630 m_pixels.Offset(*this, pt.x, pt.y);
631
632 m_ptOrigin = pt;
633 m_width = sz.x;
634 m_height = sz.y;
635 }
636 };
637 };
638
639 #ifdef __VISUALC__
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__
646
647 template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
648 class wxPixelData :
649 public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
650 {
651 public:
652 typedef
653 typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
654 Base;
655
656 wxPixelData(Image& image) : Base(image) { }
657
658 wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
659
660 wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
661 : Base(i, pt, sz)
662 {
663 }
664 };
665
666
667 // some "predefined" pixel data classes
668 typedef wxPixelData<wxImage> wxImagePixelData;
669 typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
670 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
671
672 // ----------------------------------------------------------------------------
673 // wxPixelIterator
674 // ----------------------------------------------------------------------------
675
676 /*
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
681 formats.
682
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.
687 */
688 template < class Image, class PixelFormat = wxPixelFormatFor<Image> >
689 struct WXDLLEXPORT wxPixelIterator : wxPixelData<Image, PixelFormat>::Iterator
690 {
691 };
692
693 #ifdef __VISUALC__
694 #pragma warning(default: 4355)
695 #pragma warning(default: 4097)
696 #endif
697
698 #endif // _WX_RAWBMP_H_BASE_
699