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