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