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