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