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