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