]> git.saurik.com Git - wxWidgets.git/blob - include/wx/rawbmp.h
added wxImage::RotateHue() and RGB <-> HSV conversions (patch 1227108)
[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(push)
77 #pragma warning(disable: 4355) // 'this' used in initializer list
78 #endif
79
80 /*
81 Note: we do not use WXDLLEXPORT with classes in this file because VC++ has
82 problems with exporting inner class defined inside a specialization of a
83 template class from a DLL. Besides, as all the methods are inline it's not
84 really necessary to put them in DLL at all.
85 */
86
87 // ----------------------------------------------------------------------------
88 // wxPixelFormat
89 // ----------------------------------------------------------------------------
90
91 /*
92 wxPixelFormat is a template class describing the bitmap data format. It
93 contains the constants describing the format of pixel data, but does not
94 describe how the entire bitmap is stored (i.e. top-to-bottom,
95 bottom-to-top, ...). It is also a "traits"-like class, i.e. it only
96 contains some constants and maybe static methods but nothing more, so it
97 can be safely used without incurring any overhead as all accesses to it are
98 done at compile-time.
99
100 Current limitations: we don't support RAGABA and ARAGAB formats supported
101 by Mac OS X. If there is sufficient interest, these classes could be
102 extended to deal with them. Neither do we support alpha channel having
103 different representation from the RGB ones (happens under QNX/Photon I
104 think), but again this could be achieved with some small extra effort.
105
106 Template parameters are:
107 - type of a single pixel component
108 - size of the single pixel in bits
109 - indices of red, green and blue pixel components inside the pixel
110 - index of the alpha component or -1 if none
111 - type which can contain the full pixel value (all channels)
112 */
113
114 template <class Channel,
115 size_t Bpp, int R, int G, int B, int A = -1,
116 class Pixel = wxUint32>
117
118 struct wxPixelFormat
119 {
120 // iterator over pixels is usually of type "ChannelType *"
121 typedef Channel ChannelType;
122
123 // the type which may hold the entire pixel value
124 typedef Pixel PixelType;
125
126 // NB: using static ints initialized inside the class declaration is not
127 // portable as it doesn't work with VC++ 6, so we must use enums
128
129 // size of one pixel in bits
130 enum { BitsPerPixel = Bpp };
131
132 // size of one pixel in ChannelType units (usually bytes)
133 enum { SizePixel = Bpp / (8 * sizeof(Channel)) };
134
135 // the channels indices inside the pixel
136 enum
137 {
138 RED = R,
139 GREEN = G,
140 BLUE = B,
141 ALPHA = A
142 };
143
144 // true if we have an alpha channel (together with the other channels, this
145 // doesn't cover the case of wxImage which stores alpha separately)
146 enum { HasAlpha = A != -1 };
147 };
148
149 // some "predefined" pixel formats
150 // -------------------------------
151
152 // wxImage format is common to all platforms
153 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxImagePixelFormat;
154
155 // the (most common) native bitmap format without alpha support
156 #if defined(__WXMSW__)
157 // under MSW the RGB components are reversed, they're in BGR order
158 typedef wxPixelFormat<unsigned char, 24, 2, 1, 0> wxNativePixelFormat;
159
160 #define wxPIXEL_FORMAT_ALPHA 3
161 #elif defined(__WXMAC__)
162 // under Mac, first component is unused but still present, hence we use
163 // 32bpp, not 24
164 typedef wxPixelFormat<unsigned char, 32, 1, 2, 3> wxNativePixelFormat;
165
166 #define wxPIXEL_FORMAT_ALPHA 0
167 #elif defined(__WXCOCOA__)
168 // Cocoa is standard RGB or RGBA (normally it is RGBA)
169 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
170
171 #define wxPIXEL_FORMAT_ALPHA 3
172 #elif defined(__WXGTK__)
173 // Under GTK+ 2.X we use GdkPixbuf, which should be RGBA
174 typedef wxPixelFormat<unsigned char, 24, 0, 1, 2> wxNativePixelFormat;
175
176 #define wxPIXEL_FORMAT_ALPHA 3
177 #endif
178
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
187
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;
191
192 #if wxUSE_IMAGE
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
195 // want
196 template <>
197 struct wxPixelFormatFor<wxImage>
198 {
199 typedef wxImagePixelFormat Format;
200 };
201 #endif //wxUSE_IMAGE
202
203 // ----------------------------------------------------------------------------
204 // wxPixelData
205 // ----------------------------------------------------------------------------
206
207 /*
208 wxPixelDataBase is just a helper for wxPixelData: it contains things common
209 to both wxImage and wxBitmap specializations.
210 */
211 class wxPixelDataBase
212 {
213 public:
214 // origin of the rectangular region we represent
215 wxPoint GetOrigin() const { return m_ptOrigin; }
216
217 // width and height of the region we represent
218 int GetWidth() const { return m_width; }
219 int GetHeight() const { return m_height; }
220
221 wxSize GetSize() const { return wxSize(m_width, m_height); }
222
223 // the distance between two rows
224 int GetRowStride() const { return m_stride; }
225
226 // private: -- see comment in the beginning of the file
227
228 // the origin of this image inside the bigger bitmap (usually (0, 0))
229 wxPoint m_ptOrigin;
230
231 // the size of the image we address, in pixels
232 int m_width,
233 m_height;
234
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
241 int m_stride;
242
243 protected:
244 // ctor is protected because this class is only meant to be used as the
245 // base class by wxPixelData
246 wxPixelDataBase()
247 {
248 m_width =
249 m_height =
250 m_stride = 0;
251 }
252 };
253
254 /*
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.
258
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.
264
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.
270
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
274 specializations.
275 */
276
277 // we need to define this skeleton template to mollify VC++
278 template <class Image>
279 struct wxPixelDataOut
280 {
281 template <class PixelFormat>
282 class wxPixelDataIn
283 {
284 public:
285 class Iterator { };
286 };
287 };
288
289 #if wxUSE_IMAGE
290 // wxPixelData specialization for wxImage: this is the simplest case as we
291 // don't have to care about different pixel formats here
292 template <>
293 struct wxPixelDataOut<wxImage>
294 {
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
299 {
300 public:
301 // the type of the class we're working with
302 typedef wxImage ImageType;
303
304 // the iterator which should be used for working with data in this
305 // format
306 class Iterator
307 {
308 public:
309 // the pixel format we use
310 typedef wxImagePixelFormat PixelFormat;
311
312 // the type of the pixel components
313 typedef typename dummyPixelFormat::ChannelType ChannelType;
314
315 // the pixel data we're working with
316 typedef
317 wxPixelDataOut<wxImage>::wxPixelDataIn<PixelFormat> PixelData;
318
319 // go back to (0, 0)
320 void Reset(const PixelData& data)
321 {
322 *this = data.GetPixels();
323 }
324
325 // creates the iterator pointing to the beginning of data
326 Iterator(PixelData& data)
327 {
328 Reset(data);
329 }
330
331 // creates the iterator initially pointing to the image origin
332 Iterator(const wxImage& image)
333 {
334 m_pRGB = image.GetData();
335
336 if ( image.HasAlpha() )
337 {
338 m_pAlpha = image.GetAlpha();
339 }
340 else // alpha is not used at all
341 {
342 m_pAlpha = NULL;
343 }
344 }
345
346 // true if the iterator is valid
347 bool IsOk() const { return m_pRGB != NULL; }
348
349
350 // navigation
351 // ----------
352
353 // advance the iterator to the next pixel, prefix version
354 Iterator& operator++()
355 {
356 m_pRGB += PixelFormat::SizePixel;
357 if ( m_pAlpha )
358 ++m_pAlpha;
359
360 return *this;
361 }
362
363 // postfix (hence less efficient -- don't use it unless you
364 // absolutely must) version
365 Iterator operator++(int)
366 {
367 Iterator p(*this);
368 ++*this;
369 return p;
370 }
371
372 // move x pixels to the right and y down
373 //
374 // note that the rows don't wrap!
375 void Offset(const PixelData& data, int x, int y)
376 {
377 m_pRGB += data.GetRowStride()*y + PixelFormat::SizePixel*x;
378 if ( m_pAlpha )
379 m_pAlpha += data.GetWidth() + x;
380 }
381
382 // move x pixels to the right (again, no row wrapping)
383 void OffsetX(const PixelData& WXUNUSED(data), int x)
384 {
385 m_pRGB += PixelFormat::SizePixel*x;
386 if ( m_pAlpha )
387 m_pAlpha += x;
388 }
389
390 // move y rows to the bottom
391 void OffsetY(const PixelData& data, int y)
392 {
393 m_pRGB += data.GetRowStride()*y;
394 if ( m_pAlpha )
395 m_pAlpha += data.GetWidth();
396 }
397
398 // go to the given position
399 void MoveTo(const PixelData& data, int x, int y)
400 {
401 Reset(data);
402 Offset(data, x, y);
403 }
404
405
406 // data access
407 // -----------
408
409 // access to invidividual 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; }
414
415 // private: -- see comment in the beginning of the file
416
417 // pointer into RGB buffer
418 unsigned char *m_pRGB;
419
420 // pointer into alpha buffer or NULL if alpha isn't used
421 unsigned char *m_pAlpha;
422 };
423
424 // initializes us with the data of the given image
425 wxPixelDataIn(ImageType& image) : m_image(image), m_pixels(image)
426 {
427 m_width = image.GetWidth();
428 m_height = image.GetHeight();
429 m_stride = Iterator::SizePixel * m_width;
430 }
431
432 // initializes us with the given region of the specified image
433 wxPixelDataIn(ImageType& image,
434 const wxPoint& pt,
435 const wxSize& sz) : m_image(image), m_pixels(image)
436 {
437 m_stride = Iterator::SizePixel * m_width;
438
439 InitRect(pt, sz);
440 }
441
442 // initializes us with the given region of the specified image
443 wxPixelDataIn(ImageType& image,
444 const wxRect& rect) : m_image(image), m_pixels(image)
445 {
446 m_stride = Iterator::SizePixel * m_width;
447
448 InitRect(rect.GetPosition(), rect.GetSize());
449 }
450
451 // we evaluate to true only if we could get access to bitmap data
452 // successfully
453 operator bool() const { return m_pixels.IsOk(); }
454
455 // get the iterator pointing to the origin
456 Iterator GetPixels() const { return m_pixels; }
457
458 private:
459 void InitRect(const wxPoint& pt, const wxSize& sz)
460 {
461 m_width = sz.x;
462 m_height = sz.y;
463
464 m_ptOrigin = pt;
465 m_pixels.Offset(*this, pt.x, pt.y);
466 }
467
468 // the image we're working with
469 ImageType& m_image;
470
471 // the iterator pointing to the image origin
472 Iterator m_pixels;
473 };
474 };
475 #endif //wxUSE_IMAGE
476
477 #if wxUSE_GUI
478 // wxPixelData specialization for wxBitmap: here things are more interesting as
479 // we also have to support different pixel formats
480 template <>
481 struct wxPixelDataOut<wxBitmap>
482 {
483 template <class Format>
484 class wxPixelDataIn : public wxPixelDataBase
485 {
486 public:
487 // the type of the class we're working with
488 typedef wxBitmap ImageType;
489
490 class Iterator
491 {
492 public:
493 // the pixel format we use
494 typedef Format PixelFormat;
495
496 // the type of the pixel components
497 typedef typename PixelFormat::ChannelType ChannelType;
498
499 // the pixel data we're working with
500 typedef wxPixelDataOut<wxBitmap>::wxPixelDataIn<Format> PixelData;
501
502
503 // go back to (0, 0)
504 void Reset(const PixelData& data)
505 {
506 *this = data.GetPixels();
507 }
508
509 // initializes the iterator to point to the origin of the given
510 // pixel data
511 Iterator(PixelData& data)
512 {
513 Reset(data);
514 }
515
516 // initializes the iterator to point to the origin of the given
517 // bitmap
518 Iterator(wxBitmap& bmp, PixelData& data)
519 {
520 // using cast here is ugly but it should be safe as
521 // GetRawData() real return type should be consistent with
522 // BitsPerPixel (which is in turn defined by ChannelType) and
523 // this is the only thing we can do without making GetRawData()
524 // a template function which is undesirable
525 m_ptr = (ChannelType *)
526 bmp.GetRawData(data, PixelFormat::BitsPerPixel);
527 }
528
529 // return true if this iterator is valid
530 bool IsOk() const { return m_ptr != NULL; }
531
532
533 // navigation
534 // ----------
535
536 // advance the iterator to the next pixel, prefix version
537 Iterator& operator++()
538 {
539 m_ptr += PixelFormat::SizePixel;
540
541 return *this;
542 }
543
544 // postfix (hence less efficient -- don't use it unless you
545 // absolutely must) version
546 Iterator operator++(int)
547 {
548 Iterator p(*this);
549 ++*this;
550 return p;
551 }
552
553 // move x pixels to the right and y down
554 //
555 // note that the rows don't wrap!
556 void Offset(const PixelData& data, int x, int y)
557 {
558 m_ptr += data.GetRowStride()*y + PixelFormat::SizePixel*x;
559 }
560
561 // move x pixels to the right (again, no row wrapping)
562 void OffsetX(const PixelData& WXUNUSED(data), int x)
563 {
564 m_ptr += PixelFormat::SizePixel*x;
565 }
566
567 // move y rows to the bottom
568 void OffsetY(const PixelData& data, int y)
569 {
570 m_ptr += data.GetRowStride()*y;
571 }
572
573 // go to the given position
574 void MoveTo(const PixelData& data, int x, int y)
575 {
576 Reset(data);
577 Offset(data, x, y);
578 }
579
580
581 // data access
582 // -----------
583
584 // access to invidividual colour components
585 ChannelType& Red() { return m_ptr[PixelFormat::RED]; }
586 ChannelType& Green() { return m_ptr[PixelFormat::GREEN]; }
587 ChannelType& Blue() { return m_ptr[PixelFormat::BLUE]; }
588 ChannelType& Alpha() { return m_ptr[PixelFormat::ALPHA]; }
589
590 // address the pixel contents directly
591 //
592 // warning: the format is platform dependent
593 typename PixelFormat::PixelType& Data()
594 { return *(typename PixelFormat::PixelType *)m_ptr; }
595
596 // private: -- see comment in the beginning of the file
597
598 // for efficiency reasons this class should not have any other
599 // fields, otherwise it won't be put into a CPU register (as it
600 // should inside the inner loops) by some compilers, notably gcc
601 ChannelType *m_ptr;
602 };
603
604 // ctor associates this pointer with a bitmap and locks the bitmap for
605 // raw access, it will be unlocked only by our dtor and so these
606 // objects should normally be only created on the stack, i.e. have
607 // limited life-time
608 wxPixelDataIn(wxBitmap& bmp) : m_bmp(bmp), m_pixels(bmp, *this)
609 {
610 }
611
612 wxPixelDataIn(wxBitmap& bmp, const wxRect& rect)
613 : m_bmp(bmp), m_pixels(bmp, *this)
614 {
615 InitRect(rect.GetPosition(), rect.GetSize());
616 }
617
618 wxPixelDataIn(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz)
619 : m_bmp(bmp), m_pixels(bmp, *this)
620 {
621 InitRect(pt, sz);
622 }
623
624 // we evaluate to true only if we could get access to bitmap data
625 // successfully
626 operator bool() const { return m_pixels.IsOk(); }
627
628 // get the iterator pointing to the origin
629 Iterator GetPixels() const { return m_pixels; }
630
631 // dtor unlocks the bitmap
632 ~wxPixelDataIn()
633 {
634 m_bmp.UngetRawData(*this);
635 }
636
637 // call this to indicate that we should use the alpha channel
638 void UseAlpha() { m_bmp.UseAlpha(); }
639
640 // private: -- see comment in the beginning of the file
641
642 // the bitmap we're associated with
643 wxBitmap m_bmp;
644
645 // the iterator pointing to the image origin
646 Iterator m_pixels;
647
648 private:
649 void InitRect(const wxPoint& pt, const wxSize& sz)
650 {
651 m_pixels.Offset(*this, pt.x, pt.y);
652
653 m_ptOrigin = pt;
654 m_width = sz.x;
655 m_height = sz.y;
656 }
657 };
658 };
659 #endif //wxUSE_GUI
660
661 #ifdef __VISUALC__
662 // typedef-name 'foo' used as synonym for class-name 'bar'
663 // (VC++ gives this warning each time wxPixelData::Base is used but it
664 // doesn't make any sense here -- what's wrong with using typedef instead
665 // of class, this is what it is here for!)
666 #pragma warning(disable: 4097)
667 #endif // __VISUALC__
668
669 template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
670 class wxPixelData :
671 public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
672 {
673 public:
674 typedef
675 typename wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
676 Base;
677
678 wxPixelData(Image& image) : Base(image) { }
679
680 wxPixelData(Image& i, const wxRect& rect) : Base(i, rect) { }
681
682 wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz)
683 : Base(i, pt, sz)
684 {
685 }
686 };
687
688
689 // some "predefined" pixel data classes
690 #if wxUSE_IMAGE
691 typedef wxPixelData<wxImage> wxImagePixelData;
692 #endif //wxUSE_IMAGE
693 #if wxUSE_GUI
694 typedef wxPixelData<wxBitmap, wxNativePixelFormat> wxNativePixelData;
695 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> wxAlphaPixelData;
696
697 #endif //wxUSE_GUI
698
699 // ----------------------------------------------------------------------------
700 // wxPixelIterator
701 // ----------------------------------------------------------------------------
702
703 /*
704 wxPixel::Iterator represents something which points to the pixel data and
705 allows us to iterate over it. In the simplest case of wxBitmap it is,
706 indeed, just a pointer, but it can be something more complicated and,
707 moreover, you are free to specialize it for other image classes and bitmap
708 formats.
709
710 Note that although it would have been much more intuitive to have a real
711 class here instead of what we have now, this class would need two template
712 parameters, and this can't be done because we'd need compiler support for
713 partial template specialization then and neither VC6 nor VC7 provide it.
714 */
715 template < class Image, class PixelFormat = wxPixelFormatFor<Image> >
716 struct wxPixelIterator : public wxPixelData<Image, PixelFormat>::Iterator
717 {
718 };
719
720 #ifdef __VISUALC__
721 #pragma warning(pop)
722 #endif
723
724 #endif // _WX_RAWBMP_H_BASE_
725