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