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