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