]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/image.cpp | |
3 | // Purpose: wxImage | |
4 | // Author: Robert Roebling | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #if wxUSE_IMAGE | |
18 | ||
19 | #include "wx/image.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/log.h" | |
23 | #include "wx/hash.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/math.h" | |
26 | #include "wx/module.h" | |
27 | #include "wx/palette.h" | |
28 | #include "wx/intl.h" | |
29 | #include "wx/colour.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/wfstream.h" | |
33 | #include "wx/xpmdecod.h" | |
34 | ||
35 | // For memcpy | |
36 | #include <string.h> | |
37 | ||
38 | // make the code compile with either wxFile*Stream or wxFFile*Stream: | |
39 | #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE)) | |
40 | ||
41 | #if HAS_FILE_STREAMS | |
42 | #if wxUSE_FFILE | |
43 | typedef wxFFileInputStream wxImageFileInputStream; | |
44 | typedef wxFFileOutputStream wxImageFileOutputStream; | |
45 | #elif wxUSE_FILE | |
46 | typedef wxFileInputStream wxImageFileInputStream; | |
47 | typedef wxFileOutputStream wxImageFileOutputStream; | |
48 | #endif // wxUSE_FILE/wxUSE_FFILE | |
49 | #endif // HAS_FILE_STREAMS | |
50 | ||
51 | #if wxUSE_VARIANT | |
52 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage,WXDLLEXPORT) | |
53 | #endif | |
54 | ||
55 | //----------------------------------------------------------------------------- | |
56 | // global data | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | wxList wxImage::sm_handlers; | |
60 | wxImage wxNullImage; | |
61 | ||
62 | //----------------------------------------------------------------------------- | |
63 | // wxImageRefData | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
66 | class wxImageRefData: public wxObjectRefData | |
67 | { | |
68 | public: | |
69 | wxImageRefData(); | |
70 | virtual ~wxImageRefData(); | |
71 | ||
72 | int m_width; | |
73 | int m_height; | |
74 | wxBitmapType m_type; | |
75 | unsigned char *m_data; | |
76 | ||
77 | bool m_hasMask; | |
78 | unsigned char m_maskRed,m_maskGreen,m_maskBlue; | |
79 | ||
80 | // alpha channel data, may be NULL for the formats without alpha support | |
81 | unsigned char *m_alpha; | |
82 | ||
83 | bool m_ok; | |
84 | ||
85 | // if true, m_data is pointer to static data and shouldn't be freed | |
86 | bool m_static; | |
87 | ||
88 | // same as m_static but for m_alpha | |
89 | bool m_staticAlpha; | |
90 | ||
91 | #if wxUSE_PALETTE | |
92 | wxPalette m_palette; | |
93 | #endif // wxUSE_PALETTE | |
94 | ||
95 | wxArrayString m_optionNames; | |
96 | wxArrayString m_optionValues; | |
97 | ||
98 | wxDECLARE_NO_COPY_CLASS(wxImageRefData); | |
99 | }; | |
100 | ||
101 | wxImageRefData::wxImageRefData() | |
102 | { | |
103 | m_width = 0; | |
104 | m_height = 0; | |
105 | m_type = wxBITMAP_TYPE_INVALID; | |
106 | m_data = | |
107 | m_alpha = (unsigned char *) NULL; | |
108 | ||
109 | m_maskRed = 0; | |
110 | m_maskGreen = 0; | |
111 | m_maskBlue = 0; | |
112 | m_hasMask = false; | |
113 | ||
114 | m_ok = false; | |
115 | m_static = | |
116 | m_staticAlpha = false; | |
117 | } | |
118 | ||
119 | wxImageRefData::~wxImageRefData() | |
120 | { | |
121 | if ( !m_static ) | |
122 | free( m_data ); | |
123 | if ( !m_staticAlpha ) | |
124 | free( m_alpha ); | |
125 | } | |
126 | ||
127 | ||
128 | //----------------------------------------------------------------------------- | |
129 | // wxImage | |
130 | //----------------------------------------------------------------------------- | |
131 | ||
132 | #define M_IMGDATA static_cast<wxImageRefData*>(m_refData) | |
133 | ||
134 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) | |
135 | ||
136 | bool wxImage::Create(const char* const* xpmData) | |
137 | { | |
138 | #if wxUSE_XPM | |
139 | UnRef(); | |
140 | ||
141 | wxXPMDecoder decoder; | |
142 | (*this) = decoder.ReadData(xpmData); | |
143 | return Ok(); | |
144 | #else | |
145 | return false; | |
146 | #endif | |
147 | } | |
148 | ||
149 | bool wxImage::Create( int width, int height, bool clear ) | |
150 | { | |
151 | UnRef(); | |
152 | ||
153 | m_refData = new wxImageRefData(); | |
154 | ||
155 | M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 ); | |
156 | if (!M_IMGDATA->m_data) | |
157 | { | |
158 | UnRef(); | |
159 | return false; | |
160 | } | |
161 | ||
162 | M_IMGDATA->m_width = width; | |
163 | M_IMGDATA->m_height = height; | |
164 | M_IMGDATA->m_ok = true; | |
165 | ||
166 | if (clear) | |
167 | { | |
168 | Clear(); | |
169 | } | |
170 | ||
171 | return true; | |
172 | } | |
173 | ||
174 | bool wxImage::Create( int width, int height, unsigned char* data, bool static_data ) | |
175 | { | |
176 | UnRef(); | |
177 | ||
178 | wxCHECK_MSG( data, false, wxT("NULL data in wxImage::Create") ); | |
179 | ||
180 | m_refData = new wxImageRefData(); | |
181 | ||
182 | M_IMGDATA->m_data = data; | |
183 | M_IMGDATA->m_width = width; | |
184 | M_IMGDATA->m_height = height; | |
185 | M_IMGDATA->m_ok = true; | |
186 | M_IMGDATA->m_static = static_data; | |
187 | ||
188 | return true; | |
189 | } | |
190 | ||
191 | bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) | |
192 | { | |
193 | UnRef(); | |
194 | ||
195 | wxCHECK_MSG( data, false, wxT("NULL data in wxImage::Create") ); | |
196 | ||
197 | m_refData = new wxImageRefData(); | |
198 | ||
199 | M_IMGDATA->m_data = data; | |
200 | M_IMGDATA->m_alpha = alpha; | |
201 | M_IMGDATA->m_width = width; | |
202 | M_IMGDATA->m_height = height; | |
203 | M_IMGDATA->m_ok = true; | |
204 | M_IMGDATA->m_static = static_data; | |
205 | M_IMGDATA->m_staticAlpha = static_data; | |
206 | ||
207 | return true; | |
208 | } | |
209 | ||
210 | void wxImage::Destroy() | |
211 | { | |
212 | UnRef(); | |
213 | } | |
214 | ||
215 | void wxImage::Clear(unsigned char value) | |
216 | { | |
217 | memset(M_IMGDATA->m_data, value, M_IMGDATA->m_width*M_IMGDATA->m_height*3); | |
218 | } | |
219 | ||
220 | wxObjectRefData* wxImage::CreateRefData() const | |
221 | { | |
222 | return new wxImageRefData; | |
223 | } | |
224 | ||
225 | wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const | |
226 | { | |
227 | const wxImageRefData* refData = static_cast<const wxImageRefData*>(that); | |
228 | wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") ); | |
229 | ||
230 | wxImageRefData* refData_new = new wxImageRefData; | |
231 | refData_new->m_width = refData->m_width; | |
232 | refData_new->m_height = refData->m_height; | |
233 | refData_new->m_maskRed = refData->m_maskRed; | |
234 | refData_new->m_maskGreen = refData->m_maskGreen; | |
235 | refData_new->m_maskBlue = refData->m_maskBlue; | |
236 | refData_new->m_hasMask = refData->m_hasMask; | |
237 | refData_new->m_ok = true; | |
238 | unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height); | |
239 | if (refData->m_alpha != NULL) | |
240 | { | |
241 | refData_new->m_alpha = (unsigned char*)malloc(size); | |
242 | memcpy(refData_new->m_alpha, refData->m_alpha, size); | |
243 | } | |
244 | size *= 3; | |
245 | refData_new->m_data = (unsigned char*)malloc(size); | |
246 | memcpy(refData_new->m_data, refData->m_data, size); | |
247 | #if wxUSE_PALETTE | |
248 | refData_new->m_palette = refData->m_palette; | |
249 | #endif | |
250 | refData_new->m_optionNames = refData->m_optionNames; | |
251 | refData_new->m_optionValues = refData->m_optionValues; | |
252 | return refData_new; | |
253 | } | |
254 | ||
255 | // returns a new image with the same dimensions, alpha, and mask as *this | |
256 | // if on_its_side is true, width and height are swapped | |
257 | wxImage wxImage::MakeEmptyClone(int flags) const | |
258 | { | |
259 | wxImage image; | |
260 | ||
261 | wxCHECK_MSG( Ok(), image, wxS("invalid image") ); | |
262 | ||
263 | long height = M_IMGDATA->m_height; | |
264 | long width = M_IMGDATA->m_width; | |
265 | ||
266 | if ( flags & Clone_SwapOrientation ) | |
267 | wxSwap( width, height ); | |
268 | ||
269 | if ( !image.Create( width, height, false ) ) | |
270 | { | |
271 | wxFAIL_MSG( wxS("unable to create image") ); | |
272 | return image; | |
273 | } | |
274 | ||
275 | if ( M_IMGDATA->m_alpha ) | |
276 | { | |
277 | image.SetAlpha(); | |
278 | wxCHECK2_MSG( image.GetAlpha(), return wxImage(), | |
279 | wxS("unable to create alpha channel") ); | |
280 | } | |
281 | ||
282 | if ( M_IMGDATA->m_hasMask ) | |
283 | { | |
284 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
285 | M_IMGDATA->m_maskGreen, | |
286 | M_IMGDATA->m_maskBlue ); | |
287 | } | |
288 | ||
289 | return image; | |
290 | } | |
291 | ||
292 | wxImage wxImage::Copy() const | |
293 | { | |
294 | wxImage image; | |
295 | ||
296 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
297 | ||
298 | image.m_refData = CloneRefData(m_refData); | |
299 | ||
300 | return image; | |
301 | } | |
302 | ||
303 | wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const | |
304 | { | |
305 | if( xFactor == 1 && yFactor == 1 ) | |
306 | return *this; | |
307 | ||
308 | wxImage image; | |
309 | ||
310 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
311 | ||
312 | // can't scale to/from 0 size | |
313 | wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image, | |
314 | wxT("invalid new image size") ); | |
315 | ||
316 | long old_height = M_IMGDATA->m_height, | |
317 | old_width = M_IMGDATA->m_width; | |
318 | ||
319 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
320 | wxT("invalid old image size") ); | |
321 | ||
322 | long width = old_width / xFactor ; | |
323 | long height = old_height / yFactor ; | |
324 | ||
325 | image.Create( width, height, false ); | |
326 | ||
327 | char unsigned *data = image.GetData(); | |
328 | ||
329 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
330 | ||
331 | bool hasMask = false ; | |
332 | unsigned char maskRed = 0; | |
333 | unsigned char maskGreen = 0; | |
334 | unsigned char maskBlue = 0 ; | |
335 | ||
336 | const unsigned char *source_data = M_IMGDATA->m_data; | |
337 | unsigned char *target_data = data; | |
338 | const unsigned char *source_alpha = 0 ; | |
339 | unsigned char *target_alpha = 0 ; | |
340 | if (M_IMGDATA->m_hasMask) | |
341 | { | |
342 | hasMask = true ; | |
343 | maskRed = M_IMGDATA->m_maskRed; | |
344 | maskGreen = M_IMGDATA->m_maskGreen; | |
345 | maskBlue =M_IMGDATA->m_maskBlue ; | |
346 | ||
347 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
348 | M_IMGDATA->m_maskGreen, | |
349 | M_IMGDATA->m_maskBlue ); | |
350 | } | |
351 | else | |
352 | { | |
353 | source_alpha = M_IMGDATA->m_alpha ; | |
354 | if ( source_alpha ) | |
355 | { | |
356 | image.SetAlpha() ; | |
357 | target_alpha = image.GetAlpha() ; | |
358 | } | |
359 | } | |
360 | ||
361 | for (long y = 0; y < height; y++) | |
362 | { | |
363 | for (long x = 0; x < width; x++) | |
364 | { | |
365 | unsigned long avgRed = 0 ; | |
366 | unsigned long avgGreen = 0; | |
367 | unsigned long avgBlue = 0; | |
368 | unsigned long avgAlpha = 0 ; | |
369 | unsigned long counter = 0 ; | |
370 | // determine average | |
371 | for ( int y1 = 0 ; y1 < yFactor ; ++y1 ) | |
372 | { | |
373 | long y_offset = (y * yFactor + y1) * old_width; | |
374 | for ( int x1 = 0 ; x1 < xFactor ; ++x1 ) | |
375 | { | |
376 | const unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ; | |
377 | unsigned char red = pixel[0] ; | |
378 | unsigned char green = pixel[1] ; | |
379 | unsigned char blue = pixel[2] ; | |
380 | unsigned char alpha = 255 ; | |
381 | if ( source_alpha ) | |
382 | alpha = *(source_alpha + y_offset + x * xFactor + x1) ; | |
383 | if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue ) | |
384 | { | |
385 | if ( alpha > 0 ) | |
386 | { | |
387 | avgRed += red ; | |
388 | avgGreen += green ; | |
389 | avgBlue += blue ; | |
390 | } | |
391 | avgAlpha += alpha ; | |
392 | counter++ ; | |
393 | } | |
394 | } | |
395 | } | |
396 | if ( counter == 0 ) | |
397 | { | |
398 | *(target_data++) = M_IMGDATA->m_maskRed ; | |
399 | *(target_data++) = M_IMGDATA->m_maskGreen ; | |
400 | *(target_data++) = M_IMGDATA->m_maskBlue ; | |
401 | } | |
402 | else | |
403 | { | |
404 | if ( source_alpha ) | |
405 | *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ; | |
406 | *(target_data++) = (unsigned char)(avgRed / counter); | |
407 | *(target_data++) = (unsigned char)(avgGreen / counter); | |
408 | *(target_data++) = (unsigned char)(avgBlue / counter); | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | // In case this is a cursor, make sure the hotspot is scaled accordingly: | |
414 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
415 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
416 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor); | |
417 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
418 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
419 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor); | |
420 | ||
421 | return image; | |
422 | } | |
423 | ||
424 | wxImage | |
425 | wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const | |
426 | { | |
427 | wxImage image; | |
428 | ||
429 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
430 | ||
431 | // can't scale to/from 0 size | |
432 | wxCHECK_MSG( (width > 0) && (height > 0), image, | |
433 | wxT("invalid new image size") ); | |
434 | ||
435 | long old_height = M_IMGDATA->m_height, | |
436 | old_width = M_IMGDATA->m_width; | |
437 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
438 | wxT("invalid old image size") ); | |
439 | ||
440 | // If the image's new width and height are the same as the original, no | |
441 | // need to waste time or CPU cycles | |
442 | if ( old_width == width && old_height == height ) | |
443 | return *this; | |
444 | ||
445 | if (quality == wxIMAGE_QUALITY_HIGH) | |
446 | { | |
447 | quality = (width < old_width && height < old_height) | |
448 | ? wxIMAGE_QUALITY_BOX_AVERAGE | |
449 | : wxIMAGE_QUALITY_BICUBIC; | |
450 | } | |
451 | ||
452 | // Resample the image using the method as specified. | |
453 | switch ( quality ) | |
454 | { | |
455 | case wxIMAGE_QUALITY_NEAREST: | |
456 | if ( old_width % width == 0 && old_width >= width && | |
457 | old_height % height == 0 && old_height >= height ) | |
458 | { | |
459 | return ShrinkBy( old_width / width , old_height / height ); | |
460 | } | |
461 | ||
462 | image = ResampleNearest(width, height); | |
463 | break; | |
464 | ||
465 | case wxIMAGE_QUALITY_BILINEAR: | |
466 | image = ResampleBilinear(width, height); | |
467 | break; | |
468 | ||
469 | case wxIMAGE_QUALITY_BICUBIC: | |
470 | image = ResampleBicubic(width, height); | |
471 | break; | |
472 | ||
473 | case wxIMAGE_QUALITY_BOX_AVERAGE: | |
474 | image = ResampleBox(width, height); | |
475 | break; | |
476 | } | |
477 | ||
478 | // If the original image has a mask, apply the mask to the new image | |
479 | if (M_IMGDATA->m_hasMask) | |
480 | { | |
481 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
482 | M_IMGDATA->m_maskGreen, | |
483 | M_IMGDATA->m_maskBlue ); | |
484 | } | |
485 | ||
486 | // In case this is a cursor, make sure the hotspot is scaled accordingly: | |
487 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
488 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
489 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); | |
490 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
491 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
492 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); | |
493 | ||
494 | return image; | |
495 | } | |
496 | ||
497 | wxImage wxImage::ResampleNearest(int width, int height) const | |
498 | { | |
499 | wxImage image; | |
500 | image.Create( width, height, false ); | |
501 | ||
502 | unsigned char *data = image.GetData(); | |
503 | ||
504 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
505 | ||
506 | const unsigned char *source_data = M_IMGDATA->m_data; | |
507 | unsigned char *target_data = data; | |
508 | const unsigned char *source_alpha = 0 ; | |
509 | unsigned char *target_alpha = 0 ; | |
510 | ||
511 | if ( !M_IMGDATA->m_hasMask ) | |
512 | { | |
513 | source_alpha = M_IMGDATA->m_alpha ; | |
514 | if ( source_alpha ) | |
515 | { | |
516 | image.SetAlpha() ; | |
517 | target_alpha = image.GetAlpha() ; | |
518 | } | |
519 | } | |
520 | ||
521 | long old_height = M_IMGDATA->m_height, | |
522 | old_width = M_IMGDATA->m_width; | |
523 | long x_delta = (old_width<<16) / width; | |
524 | long y_delta = (old_height<<16) / height; | |
525 | ||
526 | unsigned char* dest_pixel = target_data; | |
527 | ||
528 | long y = 0; | |
529 | for ( long j = 0; j < height; j++ ) | |
530 | { | |
531 | const unsigned char* src_line = &source_data[(y>>16)*old_width*3]; | |
532 | const unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ; | |
533 | ||
534 | long x = 0; | |
535 | for ( long i = 0; i < width; i++ ) | |
536 | { | |
537 | const unsigned char* src_pixel = &src_line[(x>>16)*3]; | |
538 | const unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ; | |
539 | dest_pixel[0] = src_pixel[0]; | |
540 | dest_pixel[1] = src_pixel[1]; | |
541 | dest_pixel[2] = src_pixel[2]; | |
542 | dest_pixel += 3; | |
543 | if ( source_alpha ) | |
544 | *(target_alpha++) = *src_alpha_pixel ; | |
545 | x += x_delta; | |
546 | } | |
547 | ||
548 | y += y_delta; | |
549 | } | |
550 | ||
551 | return image; | |
552 | } | |
553 | ||
554 | wxImage wxImage::ResampleBox(int width, int height) const | |
555 | { | |
556 | // This function implements a simple pre-blur/box averaging method for | |
557 | // downsampling that gives reasonably smooth results To scale the image | |
558 | // down we will need to gather a grid of pixels of the size of the scale | |
559 | // factor in each direction and then do an averaging of the pixels. | |
560 | ||
561 | wxImage ret_image(width, height, false); | |
562 | ||
563 | const double scale_factor_x = double(M_IMGDATA->m_width) / width; | |
564 | const double scale_factor_y = double(M_IMGDATA->m_height) / height; | |
565 | ||
566 | const int scale_factor_x_2 = (int)(scale_factor_x / 2); | |
567 | const int scale_factor_y_2 = (int)(scale_factor_y / 2); | |
568 | ||
569 | const unsigned char* src_data = M_IMGDATA->m_data; | |
570 | const unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
571 | unsigned char* dst_data = ret_image.GetData(); | |
572 | unsigned char* dst_alpha = NULL; | |
573 | ||
574 | if ( src_alpha ) | |
575 | { | |
576 | ret_image.SetAlpha(); | |
577 | dst_alpha = ret_image.GetAlpha(); | |
578 | } | |
579 | ||
580 | int averaged_pixels, src_pixel_index; | |
581 | double sum_r, sum_g, sum_b, sum_a; | |
582 | ||
583 | for ( int y = 0; y < height; y++ ) // Destination image - Y direction | |
584 | { | |
585 | // Source pixel in the Y direction | |
586 | int src_y = (int)(y * scale_factor_y); | |
587 | ||
588 | for ( int x = 0; x < width; x++ ) // Destination image - X direction | |
589 | { | |
590 | // Source pixel in the X direction | |
591 | int src_x = (int)(x * scale_factor_x); | |
592 | ||
593 | // Box of pixels to average | |
594 | averaged_pixels = 0; | |
595 | sum_r = sum_g = sum_b = sum_a = 0.0; | |
596 | ||
597 | for ( int j = int(src_y - scale_factor_y/2.0 + 1), k = j; | |
598 | j <= int(src_y + scale_factor_y_2) || j < k + 2; | |
599 | j++ ) | |
600 | { | |
601 | // We don't care to average pixels that don't exist (edges) | |
602 | if ( j < 0 || j > M_IMGDATA->m_height - 1 ) | |
603 | continue; | |
604 | ||
605 | for ( int i = int(src_x - scale_factor_x/2.0 + 1), e = i; | |
606 | i <= src_x + scale_factor_x_2 || i < e + 2; | |
607 | i++ ) | |
608 | { | |
609 | // Don't average edge pixels | |
610 | if ( i < 0 || i > M_IMGDATA->m_width - 1 ) | |
611 | continue; | |
612 | ||
613 | // Calculate the actual index in our source pixels | |
614 | src_pixel_index = j * M_IMGDATA->m_width + i; | |
615 | ||
616 | sum_r += src_data[src_pixel_index * 3 + 0]; | |
617 | sum_g += src_data[src_pixel_index * 3 + 1]; | |
618 | sum_b += src_data[src_pixel_index * 3 + 2]; | |
619 | if ( src_alpha ) | |
620 | sum_a += src_alpha[src_pixel_index]; | |
621 | ||
622 | averaged_pixels++; | |
623 | } | |
624 | } | |
625 | ||
626 | // Calculate the average from the sum and number of averaged pixels | |
627 | dst_data[0] = (unsigned char)(sum_r / averaged_pixels); | |
628 | dst_data[1] = (unsigned char)(sum_g / averaged_pixels); | |
629 | dst_data[2] = (unsigned char)(sum_b / averaged_pixels); | |
630 | dst_data += 3; | |
631 | if ( src_alpha ) | |
632 | *dst_alpha++ = (unsigned char)(sum_a / averaged_pixels); | |
633 | } | |
634 | } | |
635 | ||
636 | return ret_image; | |
637 | } | |
638 | ||
639 | wxImage wxImage::ResampleBilinear(int width, int height) const | |
640 | { | |
641 | // This function implements a Bilinear algorithm for resampling. | |
642 | wxImage ret_image(width, height, false); | |
643 | const unsigned char* src_data = M_IMGDATA->m_data; | |
644 | const unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
645 | unsigned char* dst_data = ret_image.GetData(); | |
646 | unsigned char* dst_alpha = NULL; | |
647 | ||
648 | if ( src_alpha ) | |
649 | { | |
650 | ret_image.SetAlpha(); | |
651 | dst_alpha = ret_image.GetAlpha(); | |
652 | } | |
653 | double HFactor = double(M_IMGDATA->m_height) / height; | |
654 | double WFactor = double(M_IMGDATA->m_width) / width; | |
655 | ||
656 | int srcpixymax = M_IMGDATA->m_height - 1; | |
657 | int srcpixxmax = M_IMGDATA->m_width - 1; | |
658 | ||
659 | double srcpixy, srcpixy1, srcpixy2, dy, dy1; | |
660 | double srcpixx, srcpixx1, srcpixx2, dx, dx1; | |
661 | ||
662 | // initialize alpha values to avoid g++ warnings about possibly | |
663 | // uninitialized variables | |
664 | double r1, g1, b1, a1 = 0; | |
665 | double r2, g2, b2, a2 = 0; | |
666 | ||
667 | for ( int dsty = 0; dsty < height; dsty++ ) | |
668 | { | |
669 | // We need to calculate the source pixel to interpolate from - Y-axis | |
670 | srcpixy = double(dsty) * HFactor; | |
671 | srcpixy1 = int(srcpixy); | |
672 | srcpixy2 = ( srcpixy1 == srcpixymax ) ? srcpixy1 : srcpixy1 + 1.0; | |
673 | dy = srcpixy - (int)srcpixy; | |
674 | dy1 = 1.0 - dy; | |
675 | ||
676 | ||
677 | for ( int dstx = 0; dstx < width; dstx++ ) | |
678 | { | |
679 | // X-axis of pixel to interpolate from | |
680 | srcpixx = double(dstx) * WFactor; | |
681 | srcpixx1 = int(srcpixx); | |
682 | srcpixx2 = ( srcpixx1 == srcpixxmax ) ? srcpixx1 : srcpixx1 + 1.0; | |
683 | dx = srcpixx - (int)srcpixx; | |
684 | dx1 = 1.0 - dx; | |
685 | ||
686 | int x_offset1 = srcpixx1 < 0.0 ? 0 : srcpixx1 > srcpixxmax ? srcpixxmax : (int)srcpixx1; | |
687 | int x_offset2 = srcpixx2 < 0.0 ? 0 : srcpixx2 > srcpixxmax ? srcpixxmax : (int)srcpixx2; | |
688 | int y_offset1 = srcpixy1 < 0.0 ? 0 : srcpixy1 > srcpixymax ? srcpixymax : (int)srcpixy1; | |
689 | int y_offset2 = srcpixy2 < 0.0 ? 0 : srcpixy2 > srcpixymax ? srcpixymax : (int)srcpixy2; | |
690 | ||
691 | int src_pixel_index00 = y_offset1 * M_IMGDATA->m_width + x_offset1; | |
692 | int src_pixel_index01 = y_offset1 * M_IMGDATA->m_width + x_offset2; | |
693 | int src_pixel_index10 = y_offset2 * M_IMGDATA->m_width + x_offset1; | |
694 | int src_pixel_index11 = y_offset2 * M_IMGDATA->m_width + x_offset2; | |
695 | ||
696 | // first line | |
697 | r1 = src_data[src_pixel_index00 * 3 + 0] * dx1 + src_data[src_pixel_index01 * 3 + 0] * dx; | |
698 | g1 = src_data[src_pixel_index00 * 3 + 1] * dx1 + src_data[src_pixel_index01 * 3 + 1] * dx; | |
699 | b1 = src_data[src_pixel_index00 * 3 + 2] * dx1 + src_data[src_pixel_index01 * 3 + 2] * dx; | |
700 | if ( src_alpha ) | |
701 | a1 = src_alpha[src_pixel_index00] * dx1 + src_alpha[src_pixel_index01] * dx; | |
702 | ||
703 | // second line | |
704 | r2 = src_data[src_pixel_index10 * 3 + 0] * dx1 + src_data[src_pixel_index11 * 3 + 0] * dx; | |
705 | g2 = src_data[src_pixel_index10 * 3 + 1] * dx1 + src_data[src_pixel_index11 * 3 + 1] * dx; | |
706 | b2 = src_data[src_pixel_index10 * 3 + 2] * dx1 + src_data[src_pixel_index11 * 3 + 2] * dx; | |
707 | if ( src_alpha ) | |
708 | a2 = src_alpha[src_pixel_index10] * dx1 + src_alpha[src_pixel_index11] * dx; | |
709 | ||
710 | // result lines | |
711 | ||
712 | dst_data[0] = static_cast<unsigned char>(r1 * dy1 + r2 * dy); | |
713 | dst_data[1] = static_cast<unsigned char>(g1 * dy1 + g2 * dy); | |
714 | dst_data[2] = static_cast<unsigned char>(b1 * dy1 + b2 * dy); | |
715 | dst_data += 3; | |
716 | ||
717 | if ( src_alpha ) | |
718 | *dst_alpha++ = static_cast<unsigned char>(a1 * dy1 + a2 * dy); | |
719 | } | |
720 | } | |
721 | ||
722 | return ret_image; | |
723 | } | |
724 | ||
725 | // The following two local functions are for the B-spline weighting of the | |
726 | // bicubic sampling algorithm | |
727 | static inline double spline_cube(double value) | |
728 | { | |
729 | return value <= 0.0 ? 0.0 : value * value * value; | |
730 | } | |
731 | ||
732 | static inline double spline_weight(double value) | |
733 | { | |
734 | return (spline_cube(value + 2) - | |
735 | 4 * spline_cube(value + 1) + | |
736 | 6 * spline_cube(value) - | |
737 | 4 * spline_cube(value - 1)) / 6; | |
738 | } | |
739 | ||
740 | // This is the bicubic resampling algorithm | |
741 | wxImage wxImage::ResampleBicubic(int width, int height) const | |
742 | { | |
743 | // This function implements a Bicubic B-Spline algorithm for resampling. | |
744 | // This method is certainly a little slower than wxImage's default pixel | |
745 | // replication method, however for most reasonably sized images not being | |
746 | // upsampled too much on a fairly average CPU this difference is hardly | |
747 | // noticeable and the results are far more pleasing to look at. | |
748 | // | |
749 | // This particular bicubic algorithm does pixel weighting according to a | |
750 | // B-Spline that basically implements a Gaussian bell-like weighting | |
751 | // kernel. Because of this method the results may appear a bit blurry when | |
752 | // upsampling by large factors. This is basically because a slight | |
753 | // gaussian blur is being performed to get the smooth look of the upsampled | |
754 | // image. | |
755 | ||
756 | // Edge pixels: 3-4 possible solutions | |
757 | // - (Wrap/tile) Wrap the image, take the color value from the opposite | |
758 | // side of the image. | |
759 | // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n), | |
760 | // where n is nonpositive, will have the value of (2, 1). | |
761 | // - (Ignore) Simply ignore the edge pixels and apply the kernel only to | |
762 | // pixels which do have all neighbours. | |
763 | // - (Clamp) Choose the nearest pixel along the border. This takes the | |
764 | // border pixels and extends them out to infinity. | |
765 | // | |
766 | // NOTE: below the y_offset and x_offset variables are being set for edge | |
767 | // pixels using the "Mirror" method mentioned above | |
768 | ||
769 | wxImage ret_image; | |
770 | ||
771 | ret_image.Create(width, height, false); | |
772 | ||
773 | const unsigned char* src_data = M_IMGDATA->m_data; | |
774 | const unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
775 | unsigned char* dst_data = ret_image.GetData(); | |
776 | unsigned char* dst_alpha = NULL; | |
777 | ||
778 | if ( src_alpha ) | |
779 | { | |
780 | ret_image.SetAlpha(); | |
781 | dst_alpha = ret_image.GetAlpha(); | |
782 | } | |
783 | ||
784 | for ( int dsty = 0; dsty < height; dsty++ ) | |
785 | { | |
786 | // We need to calculate the source pixel to interpolate from - Y-axis | |
787 | double srcpixy = double(dsty * M_IMGDATA->m_height) / height; | |
788 | double dy = srcpixy - (int)srcpixy; | |
789 | ||
790 | for ( int dstx = 0; dstx < width; dstx++ ) | |
791 | { | |
792 | // X-axis of pixel to interpolate from | |
793 | double srcpixx = double(dstx * M_IMGDATA->m_width) / width; | |
794 | double dx = srcpixx - (int)srcpixx; | |
795 | ||
796 | // Sums for each color channel | |
797 | double sum_r = 0, sum_g = 0, sum_b = 0, sum_a = 0; | |
798 | ||
799 | // Here we actually determine the RGBA values for the destination pixel | |
800 | for ( int k = -1; k <= 2; k++ ) | |
801 | { | |
802 | // Y offset | |
803 | int y_offset = srcpixy + k < 0.0 | |
804 | ? 0 | |
805 | : srcpixy + k >= M_IMGDATA->m_height | |
806 | ? M_IMGDATA->m_height - 1 | |
807 | : (int)(srcpixy + k); | |
808 | ||
809 | // Loop across the X axis | |
810 | for ( int i = -1; i <= 2; i++ ) | |
811 | { | |
812 | // X offset | |
813 | int x_offset = srcpixx + i < 0.0 | |
814 | ? 0 | |
815 | : srcpixx + i >= M_IMGDATA->m_width | |
816 | ? M_IMGDATA->m_width - 1 | |
817 | : (int)(srcpixx + i); | |
818 | ||
819 | // Calculate the exact position where the source data | |
820 | // should be pulled from based on the x_offset and y_offset | |
821 | int src_pixel_index = y_offset*M_IMGDATA->m_width + x_offset; | |
822 | ||
823 | // Calculate the weight for the specified pixel according | |
824 | // to the bicubic b-spline kernel we're using for | |
825 | // interpolation | |
826 | double | |
827 | pixel_weight = spline_weight(i - dx)*spline_weight(k - dy); | |
828 | ||
829 | // Create a sum of all velues for each color channel | |
830 | // adjusted for the pixel's calculated weight | |
831 | sum_r += src_data[src_pixel_index * 3 + 0] * pixel_weight; | |
832 | sum_g += src_data[src_pixel_index * 3 + 1] * pixel_weight; | |
833 | sum_b += src_data[src_pixel_index * 3 + 2] * pixel_weight; | |
834 | if ( src_alpha ) | |
835 | sum_a += src_alpha[src_pixel_index] * pixel_weight; | |
836 | } | |
837 | } | |
838 | ||
839 | // Put the data into the destination image. The summed values are | |
840 | // of double data type and are rounded here for accuracy | |
841 | dst_data[0] = (unsigned char)(sum_r + 0.5); | |
842 | dst_data[1] = (unsigned char)(sum_g + 0.5); | |
843 | dst_data[2] = (unsigned char)(sum_b + 0.5); | |
844 | dst_data += 3; | |
845 | ||
846 | if ( src_alpha ) | |
847 | *dst_alpha++ = (unsigned char)sum_a; | |
848 | } | |
849 | } | |
850 | ||
851 | return ret_image; | |
852 | } | |
853 | ||
854 | // Blur in the horizontal direction | |
855 | wxImage wxImage::BlurHorizontal(int blurRadius) const | |
856 | { | |
857 | wxImage ret_image(MakeEmptyClone()); | |
858 | ||
859 | wxCHECK( ret_image.Ok(), ret_image ); | |
860 | ||
861 | const unsigned char* src_data = M_IMGDATA->m_data; | |
862 | unsigned char* dst_data = ret_image.GetData(); | |
863 | const unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
864 | unsigned char* dst_alpha = ret_image.GetAlpha(); | |
865 | ||
866 | // number of pixels we average over | |
867 | const int blurArea = blurRadius*2 + 1; | |
868 | ||
869 | // Horizontal blurring algorithm - average all pixels in the specified blur | |
870 | // radius in the X or horizontal direction | |
871 | for ( int y = 0; y < M_IMGDATA->m_height; y++ ) | |
872 | { | |
873 | // Variables used in the blurring algorithm | |
874 | long sum_r = 0, | |
875 | sum_g = 0, | |
876 | sum_b = 0, | |
877 | sum_a = 0; | |
878 | ||
879 | long pixel_idx; | |
880 | const unsigned char *src; | |
881 | unsigned char *dst; | |
882 | ||
883 | // Calculate the average of all pixels in the blur radius for the first | |
884 | // pixel of the row | |
885 | for ( int kernel_x = -blurRadius; kernel_x <= blurRadius; kernel_x++ ) | |
886 | { | |
887 | // To deal with the pixels at the start of a row so it's not | |
888 | // grabbing GOK values from memory at negative indices of the | |
889 | // image's data or grabbing from the previous row | |
890 | if ( kernel_x < 0 ) | |
891 | pixel_idx = y * M_IMGDATA->m_width; | |
892 | else | |
893 | pixel_idx = kernel_x + y * M_IMGDATA->m_width; | |
894 | ||
895 | src = src_data + pixel_idx*3; | |
896 | sum_r += src[0]; | |
897 | sum_g += src[1]; | |
898 | sum_b += src[2]; | |
899 | if ( src_alpha ) | |
900 | sum_a += src_alpha[pixel_idx]; | |
901 | } | |
902 | ||
903 | dst = dst_data + y * M_IMGDATA->m_width*3; | |
904 | dst[0] = (unsigned char)(sum_r / blurArea); | |
905 | dst[1] = (unsigned char)(sum_g / blurArea); | |
906 | dst[2] = (unsigned char)(sum_b / blurArea); | |
907 | if ( src_alpha ) | |
908 | dst_alpha[y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); | |
909 | ||
910 | // Now average the values of the rest of the pixels by just moving the | |
911 | // blur radius box along the row | |
912 | for ( int x = 1; x < M_IMGDATA->m_width; x++ ) | |
913 | { | |
914 | // Take care of edge pixels on the left edge by essentially | |
915 | // duplicating the edge pixel | |
916 | if ( x - blurRadius - 1 < 0 ) | |
917 | pixel_idx = y * M_IMGDATA->m_width; | |
918 | else | |
919 | pixel_idx = (x - blurRadius - 1) + y * M_IMGDATA->m_width; | |
920 | ||
921 | // Subtract the value of the pixel at the left side of the blur | |
922 | // radius box | |
923 | src = src_data + pixel_idx*3; | |
924 | sum_r -= src[0]; | |
925 | sum_g -= src[1]; | |
926 | sum_b -= src[2]; | |
927 | if ( src_alpha ) | |
928 | sum_a -= src_alpha[pixel_idx]; | |
929 | ||
930 | // Take care of edge pixels on the right edge | |
931 | if ( x + blurRadius > M_IMGDATA->m_width - 1 ) | |
932 | pixel_idx = M_IMGDATA->m_width - 1 + y * M_IMGDATA->m_width; | |
933 | else | |
934 | pixel_idx = x + blurRadius + y * M_IMGDATA->m_width; | |
935 | ||
936 | // Add the value of the pixel being added to the end of our box | |
937 | src = src_data + pixel_idx*3; | |
938 | sum_r += src[0]; | |
939 | sum_g += src[1]; | |
940 | sum_b += src[2]; | |
941 | if ( src_alpha ) | |
942 | sum_a += src_alpha[pixel_idx]; | |
943 | ||
944 | // Save off the averaged data | |
945 | dst = dst_data + x*3 + y*M_IMGDATA->m_width*3; | |
946 | dst[0] = (unsigned char)(sum_r / blurArea); | |
947 | dst[1] = (unsigned char)(sum_g / blurArea); | |
948 | dst[2] = (unsigned char)(sum_b / blurArea); | |
949 | if ( src_alpha ) | |
950 | dst_alpha[x + y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); | |
951 | } | |
952 | } | |
953 | ||
954 | return ret_image; | |
955 | } | |
956 | ||
957 | // Blur in the vertical direction | |
958 | wxImage wxImage::BlurVertical(int blurRadius) const | |
959 | { | |
960 | wxImage ret_image(MakeEmptyClone()); | |
961 | ||
962 | wxCHECK( ret_image.Ok(), ret_image ); | |
963 | ||
964 | const unsigned char* src_data = M_IMGDATA->m_data; | |
965 | unsigned char* dst_data = ret_image.GetData(); | |
966 | const unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
967 | unsigned char* dst_alpha = ret_image.GetAlpha(); | |
968 | ||
969 | // number of pixels we average over | |
970 | const int blurArea = blurRadius*2 + 1; | |
971 | ||
972 | // Vertical blurring algorithm - same as horizontal but switched the | |
973 | // opposite direction | |
974 | for ( int x = 0; x < M_IMGDATA->m_width; x++ ) | |
975 | { | |
976 | // Variables used in the blurring algorithm | |
977 | long sum_r = 0, | |
978 | sum_g = 0, | |
979 | sum_b = 0, | |
980 | sum_a = 0; | |
981 | ||
982 | long pixel_idx; | |
983 | const unsigned char *src; | |
984 | unsigned char *dst; | |
985 | ||
986 | // Calculate the average of all pixels in our blur radius box for the | |
987 | // first pixel of the column | |
988 | for ( int kernel_y = -blurRadius; kernel_y <= blurRadius; kernel_y++ ) | |
989 | { | |
990 | // To deal with the pixels at the start of a column so it's not | |
991 | // grabbing GOK values from memory at negative indices of the | |
992 | // image's data or grabbing from the previous column | |
993 | if ( kernel_y < 0 ) | |
994 | pixel_idx = x; | |
995 | else | |
996 | pixel_idx = x + kernel_y * M_IMGDATA->m_width; | |
997 | ||
998 | src = src_data + pixel_idx*3; | |
999 | sum_r += src[0]; | |
1000 | sum_g += src[1]; | |
1001 | sum_b += src[2]; | |
1002 | if ( src_alpha ) | |
1003 | sum_a += src_alpha[pixel_idx]; | |
1004 | } | |
1005 | ||
1006 | dst = dst_data + x*3; | |
1007 | dst[0] = (unsigned char)(sum_r / blurArea); | |
1008 | dst[1] = (unsigned char)(sum_g / blurArea); | |
1009 | dst[2] = (unsigned char)(sum_b / blurArea); | |
1010 | if ( src_alpha ) | |
1011 | dst_alpha[x] = (unsigned char)(sum_a / blurArea); | |
1012 | ||
1013 | // Now average the values of the rest of the pixels by just moving the | |
1014 | // box along the column from top to bottom | |
1015 | for ( int y = 1; y < M_IMGDATA->m_height; y++ ) | |
1016 | { | |
1017 | // Take care of pixels that would be beyond the top edge by | |
1018 | // duplicating the top edge pixel for the column | |
1019 | if ( y - blurRadius - 1 < 0 ) | |
1020 | pixel_idx = x; | |
1021 | else | |
1022 | pixel_idx = x + (y - blurRadius - 1) * M_IMGDATA->m_width; | |
1023 | ||
1024 | // Subtract the value of the pixel at the top of our blur radius box | |
1025 | src = src_data + pixel_idx*3; | |
1026 | sum_r -= src[0]; | |
1027 | sum_g -= src[1]; | |
1028 | sum_b -= src[2]; | |
1029 | if ( src_alpha ) | |
1030 | sum_a -= src_alpha[pixel_idx]; | |
1031 | ||
1032 | // Take care of the pixels that would be beyond the bottom edge of | |
1033 | // the image similar to the top edge | |
1034 | if ( y + blurRadius > M_IMGDATA->m_height - 1 ) | |
1035 | pixel_idx = x + (M_IMGDATA->m_height - 1) * M_IMGDATA->m_width; | |
1036 | else | |
1037 | pixel_idx = x + (blurRadius + y) * M_IMGDATA->m_width; | |
1038 | ||
1039 | // Add the value of the pixel being added to the end of our box | |
1040 | src = src_data + pixel_idx*3; | |
1041 | sum_r += src[0]; | |
1042 | sum_g += src[1]; | |
1043 | sum_b += src[2]; | |
1044 | if ( src_alpha ) | |
1045 | sum_a += src_alpha[pixel_idx]; | |
1046 | ||
1047 | // Save off the averaged data | |
1048 | dst = dst_data + (x + y * M_IMGDATA->m_width) * 3; | |
1049 | dst[0] = (unsigned char)(sum_r / blurArea); | |
1050 | dst[1] = (unsigned char)(sum_g / blurArea); | |
1051 | dst[2] = (unsigned char)(sum_b / blurArea); | |
1052 | if ( src_alpha ) | |
1053 | dst_alpha[x + y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | return ret_image; | |
1058 | } | |
1059 | ||
1060 | // The new blur function | |
1061 | wxImage wxImage::Blur(int blurRadius) const | |
1062 | { | |
1063 | wxImage ret_image; | |
1064 | ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
1065 | ||
1066 | // Blur the image in each direction | |
1067 | ret_image = BlurHorizontal(blurRadius); | |
1068 | ret_image = ret_image.BlurVertical(blurRadius); | |
1069 | ||
1070 | return ret_image; | |
1071 | } | |
1072 | ||
1073 | wxImage wxImage::Rotate90( bool clockwise ) const | |
1074 | { | |
1075 | wxImage image(MakeEmptyClone(Clone_SwapOrientation)); | |
1076 | ||
1077 | wxCHECK( image.Ok(), image ); | |
1078 | ||
1079 | long height = M_IMGDATA->m_height; | |
1080 | long width = M_IMGDATA->m_width; | |
1081 | ||
1082 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
1083 | { | |
1084 | int hot_x = GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X ); | |
1085 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
1086 | clockwise ? hot_x : width - 1 - hot_x); | |
1087 | } | |
1088 | ||
1089 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
1090 | { | |
1091 | int hot_y = GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y ); | |
1092 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
1093 | clockwise ? height - 1 - hot_y : hot_y); | |
1094 | } | |
1095 | ||
1096 | unsigned char *data = image.GetData(); | |
1097 | unsigned char *target_data; | |
1098 | ||
1099 | // we rotate the image in 21-pixel (63-byte) wide strips | |
1100 | // to make better use of cpu cache - memory transfers | |
1101 | // (note: while much better than single-pixel "strips", | |
1102 | // our vertical strips will still generally straddle cachelines) | |
1103 | for (long ii = 0; ii < width; ) | |
1104 | { | |
1105 | long next_ii = wxMin(ii + 21, width); | |
1106 | ||
1107 | for (long j = 0; j < height; j++) | |
1108 | { | |
1109 | const unsigned char *source_data | |
1110 | = M_IMGDATA->m_data + (j*width + ii)*3; | |
1111 | ||
1112 | for (long i = ii; i < next_ii; i++) | |
1113 | { | |
1114 | if ( clockwise ) | |
1115 | { | |
1116 | target_data = data + (((i+1)*height) - j - 1)*3; | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | target_data = data + ((height*(width - 1 - i)) + j)*3; | |
1121 | } | |
1122 | memcpy( target_data, source_data, 3 ); | |
1123 | source_data += 3; | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | ii = next_ii; | |
1128 | } | |
1129 | ||
1130 | const unsigned char *source_alpha = M_IMGDATA->m_alpha; | |
1131 | ||
1132 | if ( source_alpha ) | |
1133 | { | |
1134 | unsigned char *alpha_data = image.GetAlpha(); | |
1135 | unsigned char *target_alpha = 0 ; | |
1136 | ||
1137 | for (long j = 0; j < height; j++) | |
1138 | { | |
1139 | for (long i = 0; i < width; i++) | |
1140 | { | |
1141 | if ( clockwise ) | |
1142 | { | |
1143 | target_alpha = alpha_data + (((i+1)*height) - j - 1); | |
1144 | } | |
1145 | else | |
1146 | { | |
1147 | target_alpha = alpha_data + ((height*(width-1)) + j - (i*height)); | |
1148 | } | |
1149 | ||
1150 | *target_alpha = *source_alpha++; | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | return image; | |
1156 | } | |
1157 | ||
1158 | wxImage wxImage::Rotate180() const | |
1159 | { | |
1160 | wxImage image(MakeEmptyClone()); | |
1161 | ||
1162 | wxCHECK( image.Ok(), image ); | |
1163 | ||
1164 | long height = M_IMGDATA->m_height; | |
1165 | long width = M_IMGDATA->m_width; | |
1166 | ||
1167 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) | |
1168 | { | |
1169 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
1170 | width - 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)); | |
1171 | } | |
1172 | ||
1173 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
1174 | { | |
1175 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
1176 | height - 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)); | |
1177 | } | |
1178 | ||
1179 | unsigned char *data = image.GetData(); | |
1180 | unsigned char *alpha = image.GetAlpha(); | |
1181 | const unsigned char *source_data = M_IMGDATA->m_data; | |
1182 | unsigned char *target_data = data + width * height * 3; | |
1183 | ||
1184 | for (long j = 0; j < height; j++) | |
1185 | { | |
1186 | for (long i = 0; i < width; i++) | |
1187 | { | |
1188 | target_data -= 3; | |
1189 | memcpy( target_data, source_data, 3 ); | |
1190 | source_data += 3; | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | if ( alpha ) | |
1195 | { | |
1196 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1197 | unsigned char *dest_alpha = alpha + width * height; | |
1198 | ||
1199 | for (long j = 0; j < height; ++j) | |
1200 | { | |
1201 | for (long i = 0; i < width; ++i) | |
1202 | { | |
1203 | *(--dest_alpha) = *(src_alpha++); | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | return image; | |
1209 | } | |
1210 | ||
1211 | wxImage wxImage::Mirror( bool horizontally ) const | |
1212 | { | |
1213 | wxImage image(MakeEmptyClone()); | |
1214 | ||
1215 | wxCHECK( image.Ok(), image ); | |
1216 | ||
1217 | long height = M_IMGDATA->m_height; | |
1218 | long width = M_IMGDATA->m_width; | |
1219 | ||
1220 | unsigned char *data = image.GetData(); | |
1221 | unsigned char *alpha = image.GetAlpha(); | |
1222 | const unsigned char *source_data = M_IMGDATA->m_data; | |
1223 | unsigned char *target_data; | |
1224 | ||
1225 | if (horizontally) | |
1226 | { | |
1227 | for (long j = 0; j < height; j++) | |
1228 | { | |
1229 | data += width*3; | |
1230 | target_data = data-3; | |
1231 | for (long i = 0; i < width; i++) | |
1232 | { | |
1233 | memcpy( target_data, source_data, 3 ); | |
1234 | source_data += 3; | |
1235 | target_data -= 3; | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | if (alpha != NULL) | |
1240 | { | |
1241 | // src_alpha starts at the first pixel and increases by 1 after each step | |
1242 | // (a step here is the copy of the alpha value of one pixel) | |
1243 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1244 | // dest_alpha starts just beyond the first line, decreases before each step, | |
1245 | // and after each line is finished, increases by 2 widths (skipping the line | |
1246 | // just copied and the line that will be copied next) | |
1247 | unsigned char *dest_alpha = alpha + width; | |
1248 | ||
1249 | for (long jj = 0; jj < height; ++jj) | |
1250 | { | |
1251 | for (long i = 0; i < width; ++i) { | |
1252 | *(--dest_alpha) = *(src_alpha++); // copy one pixel | |
1253 | } | |
1254 | dest_alpha += 2 * width; // advance beyond the end of the next line | |
1255 | } | |
1256 | } | |
1257 | } | |
1258 | else | |
1259 | { | |
1260 | for (long i = 0; i < height; i++) | |
1261 | { | |
1262 | target_data = data + 3*width*(height-1-i); | |
1263 | memcpy( target_data, source_data, (size_t)3*width ); | |
1264 | source_data += 3*width; | |
1265 | } | |
1266 | ||
1267 | if ( alpha ) | |
1268 | { | |
1269 | // src_alpha starts at the first pixel and increases by 1 width after each step | |
1270 | // (a step here is the copy of the alpha channel of an entire line) | |
1271 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1272 | // dest_alpha starts just beyond the last line (beyond the whole image) | |
1273 | // and decreases by 1 width before each step | |
1274 | unsigned char *dest_alpha = alpha + width * height; | |
1275 | ||
1276 | for (long jj = 0; jj < height; ++jj) | |
1277 | { | |
1278 | dest_alpha -= width; | |
1279 | memcpy( dest_alpha, src_alpha, (size_t)width ); | |
1280 | src_alpha += width; | |
1281 | } | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | return image; | |
1286 | } | |
1287 | ||
1288 | wxImage wxImage::GetSubImage( const wxRect &rect ) const | |
1289 | { | |
1290 | wxImage image; | |
1291 | ||
1292 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1293 | ||
1294 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && | |
1295 | (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()), | |
1296 | image, wxT("invalid subimage size") ); | |
1297 | ||
1298 | const int subwidth = rect.GetWidth(); | |
1299 | const int subheight = rect.GetHeight(); | |
1300 | ||
1301 | image.Create( subwidth, subheight, false ); | |
1302 | ||
1303 | const unsigned char *src_data = GetData(); | |
1304 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1305 | unsigned char *subdata = image.GetData(); | |
1306 | unsigned char *subalpha = NULL; | |
1307 | ||
1308 | wxCHECK_MSG( subdata, image, wxT("unable to create image") ); | |
1309 | ||
1310 | if ( src_alpha ) { | |
1311 | image.SetAlpha(); | |
1312 | subalpha = image.GetAlpha(); | |
1313 | wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel")); | |
1314 | } | |
1315 | ||
1316 | if (M_IMGDATA->m_hasMask) | |
1317 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
1318 | ||
1319 | const int width = GetWidth(); | |
1320 | const int pixsoff = rect.GetLeft() + width * rect.GetTop(); | |
1321 | ||
1322 | src_data += 3 * pixsoff; | |
1323 | src_alpha += pixsoff; // won't be used if was NULL, so this is ok | |
1324 | ||
1325 | for (long j = 0; j < subheight; ++j) | |
1326 | { | |
1327 | memcpy( subdata, src_data, 3 * subwidth ); | |
1328 | subdata += 3 * subwidth; | |
1329 | src_data += 3 * width; | |
1330 | if (subalpha != NULL) { | |
1331 | memcpy( subalpha, src_alpha, subwidth ); | |
1332 | subalpha += subwidth; | |
1333 | src_alpha += width; | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | return image; | |
1338 | } | |
1339 | ||
1340 | wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, | |
1341 | int r_, int g_, int b_ ) const | |
1342 | { | |
1343 | wxImage image; | |
1344 | ||
1345 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1346 | wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") ); | |
1347 | ||
1348 | int width = GetWidth(), height = GetHeight(); | |
1349 | image.Create(size.GetWidth(), size.GetHeight(), false); | |
1350 | ||
1351 | unsigned char r = (unsigned char)r_; | |
1352 | unsigned char g = (unsigned char)g_; | |
1353 | unsigned char b = (unsigned char)b_; | |
1354 | if ((r_ == -1) && (g_ == -1) && (b_ == -1)) | |
1355 | { | |
1356 | GetOrFindMaskColour( &r, &g, &b ); | |
1357 | image.SetMaskColour(r, g, b); | |
1358 | } | |
1359 | ||
1360 | image.SetRGB(wxRect(), r, g, b); | |
1361 | ||
1362 | // we have two coordinate systems: | |
1363 | // source: starting at 0,0 of source image | |
1364 | // destination starting at 0,0 of destination image | |
1365 | // Documentation says: | |
1366 | // "The image is pasted into a new image [...] at the position pos relative | |
1367 | // to the upper left of the new image." this means the transition rule is: | |
1368 | // "dest coord" = "source coord" + pos; | |
1369 | ||
1370 | // calculate the intersection using source coordinates: | |
1371 | wxRect srcRect(0, 0, width, height); | |
1372 | wxRect dstRect(-pos, size); | |
1373 | ||
1374 | srcRect.Intersect(dstRect); | |
1375 | ||
1376 | if (!srcRect.IsEmpty()) | |
1377 | { | |
1378 | // insertion point is needed in destination coordinates. | |
1379 | // NB: it is not always "pos"! | |
1380 | wxPoint ptInsert = srcRect.GetTopLeft() + pos; | |
1381 | ||
1382 | if ((srcRect.GetWidth() == width) && (srcRect.GetHeight() == height)) | |
1383 | image.Paste(*this, ptInsert.x, ptInsert.y); | |
1384 | else | |
1385 | image.Paste(GetSubImage(srcRect), ptInsert.x, ptInsert.y); | |
1386 | } | |
1387 | ||
1388 | return image; | |
1389 | } | |
1390 | ||
1391 | void wxImage::Paste( const wxImage &image, int x, int y ) | |
1392 | { | |
1393 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1394 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
1395 | ||
1396 | AllocExclusive(); | |
1397 | ||
1398 | int xx = 0; | |
1399 | int yy = 0; | |
1400 | int width = image.GetWidth(); | |
1401 | int height = image.GetHeight(); | |
1402 | ||
1403 | if (x < 0) | |
1404 | { | |
1405 | xx = -x; | |
1406 | width += x; | |
1407 | } | |
1408 | if (y < 0) | |
1409 | { | |
1410 | yy = -y; | |
1411 | height += y; | |
1412 | } | |
1413 | ||
1414 | if ((x+xx)+width > M_IMGDATA->m_width) | |
1415 | width = M_IMGDATA->m_width - (x+xx); | |
1416 | if ((y+yy)+height > M_IMGDATA->m_height) | |
1417 | height = M_IMGDATA->m_height - (y+yy); | |
1418 | ||
1419 | if (width < 1) return; | |
1420 | if (height < 1) return; | |
1421 | ||
1422 | if ((!HasMask() && !image.HasMask()) || | |
1423 | (HasMask() && !image.HasMask()) || | |
1424 | ((HasMask() && image.HasMask() && | |
1425 | (GetMaskRed()==image.GetMaskRed()) && | |
1426 | (GetMaskGreen()==image.GetMaskGreen()) && | |
1427 | (GetMaskBlue()==image.GetMaskBlue())))) | |
1428 | { | |
1429 | const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth()); | |
1430 | int source_step = image.GetWidth()*3; | |
1431 | ||
1432 | unsigned char* target_data = GetData() + 3*((x+xx) + (y+yy)*M_IMGDATA->m_width); | |
1433 | int target_step = M_IMGDATA->m_width*3; | |
1434 | for (int j = 0; j < height; j++) | |
1435 | { | |
1436 | memcpy( target_data, source_data, width*3 ); | |
1437 | source_data += source_step; | |
1438 | target_data += target_step; | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | // Copy over the alpha channel from the original image | |
1443 | if ( image.HasAlpha() ) | |
1444 | { | |
1445 | if ( !HasAlpha() ) | |
1446 | InitAlpha(); | |
1447 | ||
1448 | const unsigned char* source_data = image.GetAlpha() + xx + yy*image.GetWidth(); | |
1449 | int source_step = image.GetWidth(); | |
1450 | ||
1451 | unsigned char* target_data = GetAlpha() + (x+xx) + (y+yy)*M_IMGDATA->m_width; | |
1452 | int target_step = M_IMGDATA->m_width; | |
1453 | ||
1454 | for (int j = 0; j < height; j++, | |
1455 | source_data += source_step, | |
1456 | target_data += target_step) | |
1457 | { | |
1458 | memcpy( target_data, source_data, width ); | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | if (!HasMask() && image.HasMask()) | |
1463 | { | |
1464 | unsigned char r = image.GetMaskRed(); | |
1465 | unsigned char g = image.GetMaskGreen(); | |
1466 | unsigned char b = image.GetMaskBlue(); | |
1467 | ||
1468 | const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth()); | |
1469 | int source_step = image.GetWidth()*3; | |
1470 | ||
1471 | unsigned char* target_data = GetData() + 3*((x+xx) + (y+yy)*M_IMGDATA->m_width); | |
1472 | int target_step = M_IMGDATA->m_width*3; | |
1473 | ||
1474 | for (int j = 0; j < height; j++) | |
1475 | { | |
1476 | for (int i = 0; i < width*3; i+=3) | |
1477 | { | |
1478 | if ((source_data[i] != r) || | |
1479 | (source_data[i+1] != g) || | |
1480 | (source_data[i+2] != b)) | |
1481 | { | |
1482 | memcpy( target_data+i, source_data+i, 3 ); | |
1483 | } | |
1484 | } | |
1485 | source_data += source_step; | |
1486 | target_data += target_step; | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, | |
1492 | unsigned char r2, unsigned char g2, unsigned char b2 ) | |
1493 | { | |
1494 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1495 | ||
1496 | AllocExclusive(); | |
1497 | ||
1498 | unsigned char *data = GetData(); | |
1499 | ||
1500 | const int w = GetWidth(); | |
1501 | const int h = GetHeight(); | |
1502 | ||
1503 | for (int j = 0; j < h; j++) | |
1504 | for (int i = 0; i < w; i++) | |
1505 | { | |
1506 | if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) | |
1507 | { | |
1508 | data[0] = r2; | |
1509 | data[1] = g2; | |
1510 | data[2] = b2; | |
1511 | } | |
1512 | data += 3; | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | wxImage wxImage::ConvertToGreyscale(void) const | |
1517 | { | |
1518 | return ConvertToGreyscale(0.299, 0.587, 0.114); | |
1519 | } | |
1520 | ||
1521 | wxImage wxImage::ConvertToGreyscale(double weight_r, double weight_g, double weight_b) const | |
1522 | { | |
1523 | wxImage image(MakeEmptyClone()); | |
1524 | ||
1525 | wxCHECK( image.Ok(), image ); | |
1526 | ||
1527 | const unsigned char *src = M_IMGDATA->m_data; | |
1528 | unsigned char *dest = image.GetData(); | |
1529 | ||
1530 | const bool hasMask = M_IMGDATA->m_hasMask; | |
1531 | const unsigned char maskRed = M_IMGDATA->m_maskRed; | |
1532 | const unsigned char maskGreen = M_IMGDATA->m_maskGreen; | |
1533 | const unsigned char maskBlue = M_IMGDATA->m_maskBlue; | |
1534 | ||
1535 | const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1536 | for ( long i = 0; i < size; i++, src += 3, dest += 3 ) | |
1537 | { | |
1538 | memcpy(dest, src, 3); | |
1539 | // only modify non-masked pixels | |
1540 | if ( !hasMask || src[0] != maskRed || src[1] != maskGreen || src[2] != maskBlue ) | |
1541 | { | |
1542 | wxColour::MakeGrey(dest + 0, dest + 1, dest + 2, weight_r, weight_g, weight_b); | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | // copy the alpha channel, if any | |
1547 | if ( image.HasAlpha() ) | |
1548 | { | |
1549 | memcpy( image.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() ); | |
1550 | } | |
1551 | ||
1552 | return image; | |
1553 | } | |
1554 | ||
1555 | wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const | |
1556 | { | |
1557 | wxImage image; | |
1558 | ||
1559 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1560 | ||
1561 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); | |
1562 | ||
1563 | unsigned char *data = image.GetData(); | |
1564 | ||
1565 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
1566 | ||
1567 | if (M_IMGDATA->m_hasMask) | |
1568 | { | |
1569 | if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g && | |
1570 | M_IMGDATA->m_maskBlue == b) | |
1571 | image.SetMaskColour( 255, 255, 255 ); | |
1572 | else | |
1573 | image.SetMaskColour( 0, 0, 0 ); | |
1574 | } | |
1575 | ||
1576 | long size = M_IMGDATA->m_height * M_IMGDATA->m_width; | |
1577 | ||
1578 | unsigned char *srcd = M_IMGDATA->m_data; | |
1579 | unsigned char *tard = image.GetData(); | |
1580 | ||
1581 | for ( long i = 0; i < size; i++, srcd += 3, tard += 3 ) | |
1582 | { | |
1583 | bool on = (srcd[0] == r) && (srcd[1] == g) && (srcd[2] == b); | |
1584 | wxColourBase::MakeMono(tard + 0, tard + 1, tard + 2, on); | |
1585 | } | |
1586 | ||
1587 | return image; | |
1588 | } | |
1589 | ||
1590 | wxImage wxImage::ConvertToDisabled(unsigned char brightness) const | |
1591 | { | |
1592 | wxImage image = *this; | |
1593 | ||
1594 | unsigned char mr = image.GetMaskRed(); | |
1595 | unsigned char mg = image.GetMaskGreen(); | |
1596 | unsigned char mb = image.GetMaskBlue(); | |
1597 | ||
1598 | int width = image.GetWidth(); | |
1599 | int height = image.GetHeight(); | |
1600 | bool has_mask = image.HasMask(); | |
1601 | ||
1602 | for (int y = height-1; y >= 0; --y) | |
1603 | { | |
1604 | for (int x = width-1; x >= 0; --x) | |
1605 | { | |
1606 | unsigned char* data = image.GetData() + (y*(width*3))+(x*3); | |
1607 | unsigned char* r = data; | |
1608 | unsigned char* g = data+1; | |
1609 | unsigned char* b = data+2; | |
1610 | ||
1611 | if (has_mask && (*r == mr) && (*g == mg) && (*b == mb)) | |
1612 | continue; | |
1613 | ||
1614 | wxColour::MakeDisabled(r, g, b, brightness); | |
1615 | } | |
1616 | } | |
1617 | return image; | |
1618 | } | |
1619 | ||
1620 | int wxImage::GetWidth() const | |
1621 | { | |
1622 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1623 | ||
1624 | return M_IMGDATA->m_width; | |
1625 | } | |
1626 | ||
1627 | int wxImage::GetHeight() const | |
1628 | { | |
1629 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1630 | ||
1631 | return M_IMGDATA->m_height; | |
1632 | } | |
1633 | ||
1634 | wxBitmapType wxImage::GetType() const | |
1635 | { | |
1636 | wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID, wxT("invalid image") ); | |
1637 | ||
1638 | return M_IMGDATA->m_type; | |
1639 | } | |
1640 | ||
1641 | void wxImage::SetType(wxBitmapType type) | |
1642 | { | |
1643 | wxCHECK_RET( IsOk(), "must create the image before setting its type"); | |
1644 | ||
1645 | // type can be wxBITMAP_TYPE_INVALID to reset the image type to default | |
1646 | wxASSERT_MSG( type != wxBITMAP_TYPE_MAX, "invalid bitmap type" ); | |
1647 | ||
1648 | M_IMGDATA->m_type = type; | |
1649 | } | |
1650 | ||
1651 | long wxImage::XYToIndex(int x, int y) const | |
1652 | { | |
1653 | if ( Ok() && | |
1654 | x >= 0 && y >= 0 && | |
1655 | x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) | |
1656 | { | |
1657 | return y*M_IMGDATA->m_width + x; | |
1658 | } | |
1659 | ||
1660 | return -1; | |
1661 | } | |
1662 | ||
1663 | void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) | |
1664 | { | |
1665 | long pos = XYToIndex(x, y); | |
1666 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
1667 | ||
1668 | AllocExclusive(); | |
1669 | ||
1670 | pos *= 3; | |
1671 | ||
1672 | M_IMGDATA->m_data[ pos ] = r; | |
1673 | M_IMGDATA->m_data[ pos+1 ] = g; | |
1674 | M_IMGDATA->m_data[ pos+2 ] = b; | |
1675 | } | |
1676 | ||
1677 | void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b ) | |
1678 | { | |
1679 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1680 | ||
1681 | AllocExclusive(); | |
1682 | ||
1683 | wxRect rect(rect_); | |
1684 | wxRect imageRect(0, 0, GetWidth(), GetHeight()); | |
1685 | if ( rect == wxRect() ) | |
1686 | { | |
1687 | rect = imageRect; | |
1688 | } | |
1689 | else | |
1690 | { | |
1691 | wxCHECK_RET( imageRect.Contains(rect.GetTopLeft()) && | |
1692 | imageRect.Contains(rect.GetBottomRight()), | |
1693 | wxT("invalid bounding rectangle") ); | |
1694 | } | |
1695 | ||
1696 | int x1 = rect.GetLeft(), | |
1697 | y1 = rect.GetTop(), | |
1698 | x2 = rect.GetRight() + 1, | |
1699 | y2 = rect.GetBottom() + 1; | |
1700 | ||
1701 | unsigned char *data wxDUMMY_INITIALIZE(NULL); | |
1702 | int x, y, width = GetWidth(); | |
1703 | for (y = y1; y < y2; y++) | |
1704 | { | |
1705 | data = M_IMGDATA->m_data + (y*width + x1)*3; | |
1706 | for (x = x1; x < x2; x++) | |
1707 | { | |
1708 | *data++ = r; | |
1709 | *data++ = g; | |
1710 | *data++ = b; | |
1711 | } | |
1712 | } | |
1713 | } | |
1714 | ||
1715 | unsigned char wxImage::GetRed( int x, int y ) const | |
1716 | { | |
1717 | long pos = XYToIndex(x, y); | |
1718 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1719 | ||
1720 | pos *= 3; | |
1721 | ||
1722 | return M_IMGDATA->m_data[pos]; | |
1723 | } | |
1724 | ||
1725 | unsigned char wxImage::GetGreen( int x, int y ) const | |
1726 | { | |
1727 | long pos = XYToIndex(x, y); | |
1728 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1729 | ||
1730 | pos *= 3; | |
1731 | ||
1732 | return M_IMGDATA->m_data[pos+1]; | |
1733 | } | |
1734 | ||
1735 | unsigned char wxImage::GetBlue( int x, int y ) const | |
1736 | { | |
1737 | long pos = XYToIndex(x, y); | |
1738 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1739 | ||
1740 | pos *= 3; | |
1741 | ||
1742 | return M_IMGDATA->m_data[pos+2]; | |
1743 | } | |
1744 | ||
1745 | bool wxImage::IsOk() const | |
1746 | { | |
1747 | // image of 0 width or height can't be considered ok - at least because it | |
1748 | // causes crashes in ConvertToBitmap() if we don't catch it in time | |
1749 | wxImageRefData *data = M_IMGDATA; | |
1750 | return data && data->m_ok && data->m_width && data->m_height; | |
1751 | } | |
1752 | ||
1753 | unsigned char *wxImage::GetData() const | |
1754 | { | |
1755 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1756 | ||
1757 | return M_IMGDATA->m_data; | |
1758 | } | |
1759 | ||
1760 | void wxImage::SetData( unsigned char *data, bool static_data ) | |
1761 | { | |
1762 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1763 | ||
1764 | wxImageRefData *newRefData = new wxImageRefData(); | |
1765 | ||
1766 | newRefData->m_width = M_IMGDATA->m_width; | |
1767 | newRefData->m_height = M_IMGDATA->m_height; | |
1768 | newRefData->m_data = data; | |
1769 | newRefData->m_ok = true; | |
1770 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; | |
1771 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1772 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1773 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1774 | newRefData->m_static = static_data; | |
1775 | ||
1776 | UnRef(); | |
1777 | ||
1778 | m_refData = newRefData; | |
1779 | } | |
1780 | ||
1781 | void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data ) | |
1782 | { | |
1783 | wxImageRefData *newRefData = new wxImageRefData(); | |
1784 | ||
1785 | if (m_refData) | |
1786 | { | |
1787 | newRefData->m_width = new_width; | |
1788 | newRefData->m_height = new_height; | |
1789 | newRefData->m_data = data; | |
1790 | newRefData->m_ok = true; | |
1791 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; | |
1792 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1793 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1794 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1795 | } | |
1796 | else | |
1797 | { | |
1798 | newRefData->m_width = new_width; | |
1799 | newRefData->m_height = new_height; | |
1800 | newRefData->m_data = data; | |
1801 | newRefData->m_ok = true; | |
1802 | } | |
1803 | newRefData->m_static = static_data; | |
1804 | ||
1805 | UnRef(); | |
1806 | ||
1807 | m_refData = newRefData; | |
1808 | } | |
1809 | ||
1810 | // ---------------------------------------------------------------------------- | |
1811 | // alpha channel support | |
1812 | // ---------------------------------------------------------------------------- | |
1813 | ||
1814 | void wxImage::SetAlpha(int x, int y, unsigned char alpha) | |
1815 | { | |
1816 | wxCHECK_RET( HasAlpha(), wxT("no alpha channel") ); | |
1817 | ||
1818 | long pos = XYToIndex(x, y); | |
1819 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
1820 | ||
1821 | AllocExclusive(); | |
1822 | ||
1823 | M_IMGDATA->m_alpha[pos] = alpha; | |
1824 | } | |
1825 | ||
1826 | unsigned char wxImage::GetAlpha(int x, int y) const | |
1827 | { | |
1828 | wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") ); | |
1829 | ||
1830 | long pos = XYToIndex(x, y); | |
1831 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
1832 | ||
1833 | return M_IMGDATA->m_alpha[pos]; | |
1834 | } | |
1835 | ||
1836 | bool | |
1837 | wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) | |
1838 | { | |
1839 | SetAlpha(NULL); | |
1840 | ||
1841 | const int w = M_IMGDATA->m_width; | |
1842 | const int h = M_IMGDATA->m_height; | |
1843 | ||
1844 | unsigned char *alpha = GetAlpha(); | |
1845 | unsigned char *data = GetData(); | |
1846 | ||
1847 | for ( int y = 0; y < h; y++ ) | |
1848 | { | |
1849 | for ( int x = 0; x < w; x++ ) | |
1850 | { | |
1851 | *alpha++ = *data; | |
1852 | *data++ = r; | |
1853 | *data++ = g; | |
1854 | *data++ = b; | |
1855 | } | |
1856 | } | |
1857 | ||
1858 | return true; | |
1859 | } | |
1860 | ||
1861 | void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) | |
1862 | { | |
1863 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1864 | ||
1865 | AllocExclusive(); | |
1866 | ||
1867 | if ( !alpha ) | |
1868 | { | |
1869 | alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height); | |
1870 | } | |
1871 | ||
1872 | if( !M_IMGDATA->m_staticAlpha ) | |
1873 | free(M_IMGDATA->m_alpha); | |
1874 | ||
1875 | M_IMGDATA->m_alpha = alpha; | |
1876 | M_IMGDATA->m_staticAlpha = static_data; | |
1877 | } | |
1878 | ||
1879 | unsigned char *wxImage::GetAlpha() const | |
1880 | { | |
1881 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1882 | ||
1883 | return M_IMGDATA->m_alpha; | |
1884 | } | |
1885 | ||
1886 | void wxImage::InitAlpha() | |
1887 | { | |
1888 | wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") ); | |
1889 | ||
1890 | // initialize memory for alpha channel | |
1891 | SetAlpha(); | |
1892 | ||
1893 | unsigned char *alpha = M_IMGDATA->m_alpha; | |
1894 | const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1895 | ||
1896 | if ( HasMask() ) | |
1897 | { | |
1898 | // use the mask to initialize the alpha channel. | |
1899 | const unsigned char * const alphaEnd = alpha + lenAlpha; | |
1900 | ||
1901 | const unsigned char mr = M_IMGDATA->m_maskRed; | |
1902 | const unsigned char mg = M_IMGDATA->m_maskGreen; | |
1903 | const unsigned char mb = M_IMGDATA->m_maskBlue; | |
1904 | for ( unsigned char *src = M_IMGDATA->m_data; | |
1905 | alpha < alphaEnd; | |
1906 | src += 3, alpha++ ) | |
1907 | { | |
1908 | *alpha = (src[0] == mr && src[1] == mg && src[2] == mb) | |
1909 | ? wxIMAGE_ALPHA_TRANSPARENT | |
1910 | : wxIMAGE_ALPHA_OPAQUE; | |
1911 | } | |
1912 | ||
1913 | M_IMGDATA->m_hasMask = false; | |
1914 | } | |
1915 | else // no mask | |
1916 | { | |
1917 | // make the image fully opaque | |
1918 | memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha); | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | void wxImage::ClearAlpha() | |
1923 | { | |
1924 | wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") ); | |
1925 | ||
1926 | if ( !M_IMGDATA->m_staticAlpha ) | |
1927 | free( M_IMGDATA->m_alpha ); | |
1928 | ||
1929 | M_IMGDATA->m_alpha = NULL; | |
1930 | } | |
1931 | ||
1932 | ||
1933 | // ---------------------------------------------------------------------------- | |
1934 | // mask support | |
1935 | // ---------------------------------------------------------------------------- | |
1936 | ||
1937 | void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) | |
1938 | { | |
1939 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1940 | ||
1941 | AllocExclusive(); | |
1942 | ||
1943 | M_IMGDATA->m_maskRed = r; | |
1944 | M_IMGDATA->m_maskGreen = g; | |
1945 | M_IMGDATA->m_maskBlue = b; | |
1946 | M_IMGDATA->m_hasMask = true; | |
1947 | } | |
1948 | ||
1949 | bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const | |
1950 | { | |
1951 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1952 | ||
1953 | if (M_IMGDATA->m_hasMask) | |
1954 | { | |
1955 | if (r) *r = M_IMGDATA->m_maskRed; | |
1956 | if (g) *g = M_IMGDATA->m_maskGreen; | |
1957 | if (b) *b = M_IMGDATA->m_maskBlue; | |
1958 | return true; | |
1959 | } | |
1960 | else | |
1961 | { | |
1962 | FindFirstUnusedColour(r, g, b); | |
1963 | return false; | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | unsigned char wxImage::GetMaskRed() const | |
1968 | { | |
1969 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1970 | ||
1971 | return M_IMGDATA->m_maskRed; | |
1972 | } | |
1973 | ||
1974 | unsigned char wxImage::GetMaskGreen() const | |
1975 | { | |
1976 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1977 | ||
1978 | return M_IMGDATA->m_maskGreen; | |
1979 | } | |
1980 | ||
1981 | unsigned char wxImage::GetMaskBlue() const | |
1982 | { | |
1983 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1984 | ||
1985 | return M_IMGDATA->m_maskBlue; | |
1986 | } | |
1987 | ||
1988 | void wxImage::SetMask( bool mask ) | |
1989 | { | |
1990 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1991 | ||
1992 | AllocExclusive(); | |
1993 | ||
1994 | M_IMGDATA->m_hasMask = mask; | |
1995 | } | |
1996 | ||
1997 | bool wxImage::HasMask() const | |
1998 | { | |
1999 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
2000 | ||
2001 | return M_IMGDATA->m_hasMask; | |
2002 | } | |
2003 | ||
2004 | bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const | |
2005 | { | |
2006 | long pos = XYToIndex(x, y); | |
2007 | wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") ); | |
2008 | ||
2009 | // check mask | |
2010 | if ( M_IMGDATA->m_hasMask ) | |
2011 | { | |
2012 | const unsigned char *p = M_IMGDATA->m_data + 3*pos; | |
2013 | if ( p[0] == M_IMGDATA->m_maskRed && | |
2014 | p[1] == M_IMGDATA->m_maskGreen && | |
2015 | p[2] == M_IMGDATA->m_maskBlue ) | |
2016 | { | |
2017 | return true; | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | // then check alpha | |
2022 | if ( M_IMGDATA->m_alpha ) | |
2023 | { | |
2024 | if ( M_IMGDATA->m_alpha[pos] < threshold ) | |
2025 | { | |
2026 | // transparent enough | |
2027 | return true; | |
2028 | } | |
2029 | } | |
2030 | ||
2031 | // not transparent | |
2032 | return false; | |
2033 | } | |
2034 | ||
2035 | bool wxImage::SetMaskFromImage(const wxImage& mask, | |
2036 | unsigned char mr, unsigned char mg, unsigned char mb) | |
2037 | { | |
2038 | // check that the images are the same size | |
2039 | if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) ) | |
2040 | { | |
2041 | wxLogError( _("Image and mask have different sizes.") ); | |
2042 | return false; | |
2043 | } | |
2044 | ||
2045 | // find unused colour | |
2046 | unsigned char r,g,b ; | |
2047 | if (!FindFirstUnusedColour(&r, &g, &b)) | |
2048 | { | |
2049 | wxLogError( _("No unused colour in image being masked.") ); | |
2050 | return false ; | |
2051 | } | |
2052 | ||
2053 | AllocExclusive(); | |
2054 | ||
2055 | unsigned char *imgdata = GetData(); | |
2056 | unsigned char *maskdata = mask.GetData(); | |
2057 | ||
2058 | const int w = GetWidth(); | |
2059 | const int h = GetHeight(); | |
2060 | ||
2061 | for (int j = 0; j < h; j++) | |
2062 | { | |
2063 | for (int i = 0; i < w; i++) | |
2064 | { | |
2065 | if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb)) | |
2066 | { | |
2067 | imgdata[0] = r; | |
2068 | imgdata[1] = g; | |
2069 | imgdata[2] = b; | |
2070 | } | |
2071 | imgdata += 3; | |
2072 | maskdata += 3; | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | SetMaskColour(r, g, b); | |
2077 | SetMask(true); | |
2078 | ||
2079 | return true; | |
2080 | } | |
2081 | ||
2082 | bool wxImage::ConvertAlphaToMask(unsigned char threshold) | |
2083 | { | |
2084 | if ( !HasAlpha() ) | |
2085 | return false; | |
2086 | ||
2087 | unsigned char mr, mg, mb; | |
2088 | if ( !FindFirstUnusedColour(&mr, &mg, &mb) ) | |
2089 | { | |
2090 | wxLogError( _("No unused colour in image being masked.") ); | |
2091 | return false; | |
2092 | } | |
2093 | ||
2094 | return ConvertAlphaToMask(mr, mg, mb, threshold); | |
2095 | } | |
2096 | ||
2097 | bool wxImage::ConvertAlphaToMask(unsigned char mr, | |
2098 | unsigned char mg, | |
2099 | unsigned char mb, | |
2100 | unsigned char threshold) | |
2101 | { | |
2102 | if ( !HasAlpha() ) | |
2103 | return false; | |
2104 | ||
2105 | AllocExclusive(); | |
2106 | ||
2107 | SetMask(true); | |
2108 | SetMaskColour(mr, mg, mb); | |
2109 | ||
2110 | unsigned char *imgdata = GetData(); | |
2111 | unsigned char *alphadata = GetAlpha(); | |
2112 | ||
2113 | int w = GetWidth(); | |
2114 | int h = GetHeight(); | |
2115 | ||
2116 | for (int y = 0; y < h; y++) | |
2117 | { | |
2118 | for (int x = 0; x < w; x++, imgdata += 3, alphadata++) | |
2119 | { | |
2120 | if (*alphadata < threshold) | |
2121 | { | |
2122 | imgdata[0] = mr; | |
2123 | imgdata[1] = mg; | |
2124 | imgdata[2] = mb; | |
2125 | } | |
2126 | } | |
2127 | } | |
2128 | ||
2129 | if ( !M_IMGDATA->m_staticAlpha ) | |
2130 | free(M_IMGDATA->m_alpha); | |
2131 | ||
2132 | M_IMGDATA->m_alpha = NULL; | |
2133 | M_IMGDATA->m_staticAlpha = false; | |
2134 | ||
2135 | return true; | |
2136 | } | |
2137 | ||
2138 | // ---------------------------------------------------------------------------- | |
2139 | // Palette functions | |
2140 | // ---------------------------------------------------------------------------- | |
2141 | ||
2142 | #if wxUSE_PALETTE | |
2143 | ||
2144 | bool wxImage::HasPalette() const | |
2145 | { | |
2146 | if (!Ok()) | |
2147 | return false; | |
2148 | ||
2149 | return M_IMGDATA->m_palette.Ok(); | |
2150 | } | |
2151 | ||
2152 | const wxPalette& wxImage::GetPalette() const | |
2153 | { | |
2154 | wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); | |
2155 | ||
2156 | return M_IMGDATA->m_palette; | |
2157 | } | |
2158 | ||
2159 | void wxImage::SetPalette(const wxPalette& palette) | |
2160 | { | |
2161 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
2162 | ||
2163 | AllocExclusive(); | |
2164 | ||
2165 | M_IMGDATA->m_palette = palette; | |
2166 | } | |
2167 | ||
2168 | #endif // wxUSE_PALETTE | |
2169 | ||
2170 | // ---------------------------------------------------------------------------- | |
2171 | // Option functions (arbitrary name/value mapping) | |
2172 | // ---------------------------------------------------------------------------- | |
2173 | ||
2174 | void wxImage::SetOption(const wxString& name, const wxString& value) | |
2175 | { | |
2176 | AllocExclusive(); | |
2177 | ||
2178 | int idx = M_IMGDATA->m_optionNames.Index(name, false); | |
2179 | if ( idx == wxNOT_FOUND ) | |
2180 | { | |
2181 | M_IMGDATA->m_optionNames.Add(name); | |
2182 | M_IMGDATA->m_optionValues.Add(value); | |
2183 | } | |
2184 | else | |
2185 | { | |
2186 | M_IMGDATA->m_optionNames[idx] = name; | |
2187 | M_IMGDATA->m_optionValues[idx] = value; | |
2188 | } | |
2189 | } | |
2190 | ||
2191 | void wxImage::SetOption(const wxString& name, int value) | |
2192 | { | |
2193 | wxString valStr; | |
2194 | valStr.Printf(wxT("%d"), value); | |
2195 | SetOption(name, valStr); | |
2196 | } | |
2197 | ||
2198 | wxString wxImage::GetOption(const wxString& name) const | |
2199 | { | |
2200 | if ( !M_IMGDATA ) | |
2201 | return wxEmptyString; | |
2202 | ||
2203 | int idx = M_IMGDATA->m_optionNames.Index(name, false); | |
2204 | if ( idx == wxNOT_FOUND ) | |
2205 | return wxEmptyString; | |
2206 | else | |
2207 | return M_IMGDATA->m_optionValues[idx]; | |
2208 | } | |
2209 | ||
2210 | int wxImage::GetOptionInt(const wxString& name) const | |
2211 | { | |
2212 | return wxAtoi(GetOption(name)); | |
2213 | } | |
2214 | ||
2215 | bool wxImage::HasOption(const wxString& name) const | |
2216 | { | |
2217 | return M_IMGDATA ? M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND | |
2218 | : false; | |
2219 | } | |
2220 | ||
2221 | // ---------------------------------------------------------------------------- | |
2222 | // image I/O | |
2223 | // ---------------------------------------------------------------------------- | |
2224 | ||
2225 | bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), | |
2226 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type), | |
2227 | int WXUNUSED_UNLESS_STREAMS(index) ) | |
2228 | { | |
2229 | #if HAS_FILE_STREAMS | |
2230 | wxImageFileInputStream stream(filename); | |
2231 | if ( stream.IsOk() ) | |
2232 | { | |
2233 | wxBufferedInputStream bstream( stream ); | |
2234 | if ( LoadFile(bstream, type, index) ) | |
2235 | return true; | |
2236 | } | |
2237 | ||
2238 | wxLogError(_("Failed to load image from file \"%s\"."), filename); | |
2239 | #endif // HAS_FILE_STREAMS | |
2240 | ||
2241 | return false; | |
2242 | } | |
2243 | ||
2244 | bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), | |
2245 | const wxString& WXUNUSED_UNLESS_STREAMS(mimetype), | |
2246 | int WXUNUSED_UNLESS_STREAMS(index) ) | |
2247 | { | |
2248 | #if HAS_FILE_STREAMS | |
2249 | wxImageFileInputStream stream(filename); | |
2250 | if ( stream.IsOk() ) | |
2251 | { | |
2252 | wxBufferedInputStream bstream( stream ); | |
2253 | if ( LoadFile(bstream, mimetype, index) ) | |
2254 | return true; | |
2255 | } | |
2256 | ||
2257 | wxLogError(_("Failed to load image from file \"%s\"."), filename); | |
2258 | #endif // HAS_FILE_STREAMS | |
2259 | ||
2260 | return false; | |
2261 | } | |
2262 | ||
2263 | ||
2264 | bool wxImage::SaveFile( const wxString& filename ) const | |
2265 | { | |
2266 | wxString ext = filename.AfterLast('.').Lower(); | |
2267 | ||
2268 | wxImageHandler *handler = FindHandler(ext, wxBITMAP_TYPE_ANY); | |
2269 | if ( !handler) | |
2270 | { | |
2271 | wxLogError(_("Can't save image to file '%s': unknown extension."), | |
2272 | filename); | |
2273 | return false; | |
2274 | } | |
2275 | ||
2276 | return SaveFile(filename, handler->GetType()); | |
2277 | } | |
2278 | ||
2279 | bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), | |
2280 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) const | |
2281 | { | |
2282 | #if HAS_FILE_STREAMS | |
2283 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
2284 | ||
2285 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); | |
2286 | ||
2287 | wxImageFileOutputStream stream(filename); | |
2288 | ||
2289 | if ( stream.IsOk() ) | |
2290 | { | |
2291 | wxBufferedOutputStream bstream( stream ); | |
2292 | return SaveFile(bstream, type); | |
2293 | } | |
2294 | #endif // HAS_FILE_STREAMS | |
2295 | ||
2296 | return false; | |
2297 | } | |
2298 | ||
2299 | bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), | |
2300 | const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const | |
2301 | { | |
2302 | #if HAS_FILE_STREAMS | |
2303 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
2304 | ||
2305 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); | |
2306 | ||
2307 | wxImageFileOutputStream stream(filename); | |
2308 | ||
2309 | if ( stream.IsOk() ) | |
2310 | { | |
2311 | wxBufferedOutputStream bstream( stream ); | |
2312 | return SaveFile(bstream, mimetype); | |
2313 | } | |
2314 | #endif // HAS_FILE_STREAMS | |
2315 | ||
2316 | return false; | |
2317 | } | |
2318 | ||
2319 | bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) ) | |
2320 | { | |
2321 | #if HAS_FILE_STREAMS | |
2322 | wxImageFileInputStream stream(name); | |
2323 | return CanRead(stream); | |
2324 | #else | |
2325 | return false; | |
2326 | #endif | |
2327 | } | |
2328 | ||
2329 | int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name), | |
2330 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) | |
2331 | { | |
2332 | #if HAS_FILE_STREAMS | |
2333 | wxImageFileInputStream stream(name); | |
2334 | if (stream.Ok()) | |
2335 | return GetImageCount(stream, type); | |
2336 | #endif | |
2337 | ||
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | #if wxUSE_STREAMS | |
2342 | ||
2343 | bool wxImage::CanRead( wxInputStream &stream ) | |
2344 | { | |
2345 | const wxList& list = GetHandlers(); | |
2346 | ||
2347 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) | |
2348 | { | |
2349 | wxImageHandler *handler=(wxImageHandler*)node->GetData(); | |
2350 | if (handler->CanRead( stream )) | |
2351 | return true; | |
2352 | } | |
2353 | ||
2354 | return false; | |
2355 | } | |
2356 | ||
2357 | int wxImage::GetImageCount( wxInputStream &stream, wxBitmapType type ) | |
2358 | { | |
2359 | wxImageHandler *handler; | |
2360 | ||
2361 | if ( type == wxBITMAP_TYPE_ANY ) | |
2362 | { | |
2363 | const wxList& list = GetHandlers(); | |
2364 | ||
2365 | for ( wxList::compatibility_iterator node = list.GetFirst(); | |
2366 | node; | |
2367 | node = node->GetNext() ) | |
2368 | { | |
2369 | handler = (wxImageHandler*)node->GetData(); | |
2370 | if ( handler->CanRead(stream) ) | |
2371 | { | |
2372 | const int count = handler->GetImageCount(stream); | |
2373 | if ( count >= 0 ) | |
2374 | return count; | |
2375 | } | |
2376 | ||
2377 | } | |
2378 | ||
2379 | wxLogWarning(_("No handler found for image type.")); | |
2380 | return 0; | |
2381 | } | |
2382 | ||
2383 | handler = FindHandler(type); | |
2384 | ||
2385 | if ( !handler ) | |
2386 | { | |
2387 | wxLogWarning(_("No image handler for type %d defined."), type); | |
2388 | return false; | |
2389 | } | |
2390 | ||
2391 | if ( handler->CanRead(stream) ) | |
2392 | { | |
2393 | return handler->GetImageCount(stream); | |
2394 | } | |
2395 | else | |
2396 | { | |
2397 | wxLogError(_("Image file is not of type %d."), type); | |
2398 | return 0; | |
2399 | } | |
2400 | } | |
2401 | ||
2402 | bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index) | |
2403 | { | |
2404 | // save the options values which can be clobbered by the handler (e.g. many | |
2405 | // of them call Destroy() before trying to load the file) | |
2406 | const unsigned maxWidth = GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH), | |
2407 | maxHeight = GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT); | |
2408 | ||
2409 | // Preserve the original stream position if possible to rewind back to it | |
2410 | // if we failed to load the file -- maybe the next handler that we try can | |
2411 | // succeed after us then. | |
2412 | wxFileOffset posOld = wxInvalidOffset; | |
2413 | if ( stream.IsSeekable() ) | |
2414 | posOld = stream.TellI(); | |
2415 | ||
2416 | if ( !handler.LoadFile(this, stream, true/*verbose*/, index) ) | |
2417 | { | |
2418 | if ( posOld != wxInvalidOffset ) | |
2419 | stream.SeekI(posOld); | |
2420 | ||
2421 | return false; | |
2422 | } | |
2423 | ||
2424 | // rescale the image to the specified size if needed | |
2425 | if ( maxWidth || maxHeight ) | |
2426 | { | |
2427 | const unsigned widthOrig = GetWidth(), | |
2428 | heightOrig = GetHeight(); | |
2429 | ||
2430 | // this uses the same (trivial) algorithm as the JPEG handler | |
2431 | unsigned width = widthOrig, | |
2432 | height = heightOrig; | |
2433 | while ( (maxWidth && width > maxWidth) || | |
2434 | (maxHeight && height > maxHeight) ) | |
2435 | { | |
2436 | width /= 2; | |
2437 | height /= 2; | |
2438 | } | |
2439 | ||
2440 | if ( width != widthOrig || height != heightOrig ) | |
2441 | Rescale(width, height, wxIMAGE_QUALITY_HIGH); | |
2442 | } | |
2443 | ||
2444 | // Set this after Rescale, which currently does not preserve it | |
2445 | M_IMGDATA->m_type = handler.GetType(); | |
2446 | ||
2447 | return true; | |
2448 | } | |
2449 | ||
2450 | bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) | |
2451 | { | |
2452 | AllocExclusive(); | |
2453 | ||
2454 | wxImageHandler *handler; | |
2455 | ||
2456 | if ( type == wxBITMAP_TYPE_ANY ) | |
2457 | { | |
2458 | if ( !stream.IsSeekable() ) | |
2459 | { | |
2460 | // The error message about image data format being unknown below | |
2461 | // would be misleading in this case as we are not even going to try | |
2462 | // any handlers because CanRead() never does anything for not | |
2463 | // seekable stream, so try to be more precise here. | |
2464 | wxLogError(_("Can't automatically determine the image format " | |
2465 | "for non-seekable input.")); | |
2466 | return false; | |
2467 | } | |
2468 | ||
2469 | const wxList& list = GetHandlers(); | |
2470 | for ( wxList::compatibility_iterator node = list.GetFirst(); | |
2471 | node; | |
2472 | node = node->GetNext() ) | |
2473 | { | |
2474 | handler = (wxImageHandler*)node->GetData(); | |
2475 | if ( handler->CanRead(stream) && DoLoad(*handler, stream, index) ) | |
2476 | return true; | |
2477 | } | |
2478 | ||
2479 | wxLogWarning( _("Unknown image data format.") ); | |
2480 | ||
2481 | return false; | |
2482 | } | |
2483 | //else: have specific type | |
2484 | ||
2485 | handler = FindHandler(type); | |
2486 | if ( !handler ) | |
2487 | { | |
2488 | wxLogWarning( _("No image handler for type %d defined."), type ); | |
2489 | return false; | |
2490 | } | |
2491 | ||
2492 | if ( stream.IsSeekable() && !handler->CanRead(stream) ) | |
2493 | { | |
2494 | wxLogError(_("This is not a %s."), handler->GetName()); | |
2495 | return false; | |
2496 | } | |
2497 | ||
2498 | return DoLoad(*handler, stream, index); | |
2499 | } | |
2500 | ||
2501 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index ) | |
2502 | { | |
2503 | UnRef(); | |
2504 | ||
2505 | m_refData = new wxImageRefData; | |
2506 | ||
2507 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
2508 | ||
2509 | if ( !handler ) | |
2510 | { | |
2511 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); | |
2512 | return false; | |
2513 | } | |
2514 | ||
2515 | if ( stream.IsSeekable() && !handler->CanRead(stream) ) | |
2516 | { | |
2517 | wxLogError(_("Image is not of type %s."), mimetype); | |
2518 | return false; | |
2519 | } | |
2520 | ||
2521 | return DoLoad(*handler, stream, index); | |
2522 | } | |
2523 | ||
2524 | bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const | |
2525 | { | |
2526 | wxImage * const self = const_cast<wxImage *>(this); | |
2527 | if ( !handler.SaveFile(self, stream) ) | |
2528 | return false; | |
2529 | ||
2530 | M_IMGDATA->m_type = handler.GetType(); | |
2531 | return true; | |
2532 | } | |
2533 | ||
2534 | bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const | |
2535 | { | |
2536 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
2537 | ||
2538 | wxImageHandler *handler = FindHandler(type); | |
2539 | if ( !handler ) | |
2540 | { | |
2541 | wxLogWarning( _("No image handler for type %d defined."), type ); | |
2542 | return false; | |
2543 | } | |
2544 | ||
2545 | return DoSave(*handler, stream); | |
2546 | } | |
2547 | ||
2548 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const | |
2549 | { | |
2550 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
2551 | ||
2552 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
2553 | if ( !handler ) | |
2554 | { | |
2555 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); | |
2556 | } | |
2557 | ||
2558 | return DoSave(*handler, stream); | |
2559 | } | |
2560 | ||
2561 | #endif // wxUSE_STREAMS | |
2562 | ||
2563 | // ---------------------------------------------------------------------------- | |
2564 | // image I/O handlers | |
2565 | // ---------------------------------------------------------------------------- | |
2566 | ||
2567 | void wxImage::AddHandler( wxImageHandler *handler ) | |
2568 | { | |
2569 | // Check for an existing handler of the type being added. | |
2570 | if (FindHandler( handler->GetType() ) == 0) | |
2571 | { | |
2572 | sm_handlers.Append( handler ); | |
2573 | } | |
2574 | else | |
2575 | { | |
2576 | // This is not documented behaviour, merely the simplest 'fix' | |
2577 | // for preventing duplicate additions. If someone ever has | |
2578 | // a good reason to add and remove duplicate handlers (and they | |
2579 | // may) we should probably refcount the duplicates. | |
2580 | // also an issue in InsertHandler below. | |
2581 | ||
2582 | wxLogDebug( wxT("Adding duplicate image handler for '%s'"), | |
2583 | handler->GetName().c_str() ); | |
2584 | delete handler; | |
2585 | } | |
2586 | } | |
2587 | ||
2588 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
2589 | { | |
2590 | // Check for an existing handler of the type being added. | |
2591 | if (FindHandler( handler->GetType() ) == 0) | |
2592 | { | |
2593 | sm_handlers.Insert( handler ); | |
2594 | } | |
2595 | else | |
2596 | { | |
2597 | // see AddHandler for additional comments. | |
2598 | wxLogDebug( wxT("Inserting duplicate image handler for '%s'"), | |
2599 | handler->GetName().c_str() ); | |
2600 | delete handler; | |
2601 | } | |
2602 | } | |
2603 | ||
2604 | bool wxImage::RemoveHandler( const wxString& name ) | |
2605 | { | |
2606 | wxImageHandler *handler = FindHandler(name); | |
2607 | if (handler) | |
2608 | { | |
2609 | sm_handlers.DeleteObject(handler); | |
2610 | delete handler; | |
2611 | return true; | |
2612 | } | |
2613 | else | |
2614 | return false; | |
2615 | } | |
2616 | ||
2617 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
2618 | { | |
2619 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
2620 | while (node) | |
2621 | { | |
2622 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); | |
2623 | if (handler->GetName().Cmp(name) == 0) return handler; | |
2624 | ||
2625 | node = node->GetNext(); | |
2626 | } | |
2627 | return NULL; | |
2628 | } | |
2629 | ||
2630 | wxImageHandler *wxImage::FindHandler( const wxString& extension, wxBitmapType bitmapType ) | |
2631 | { | |
2632 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
2633 | while (node) | |
2634 | { | |
2635 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); | |
2636 | if ((bitmapType == wxBITMAP_TYPE_ANY) || (handler->GetType() == bitmapType)) | |
2637 | { | |
2638 | if (handler->GetExtension() == extension) | |
2639 | return handler; | |
2640 | if (handler->GetAltExtensions().Index(extension, false) != wxNOT_FOUND) | |
2641 | return handler; | |
2642 | } | |
2643 | node = node->GetNext(); | |
2644 | } | |
2645 | return NULL; | |
2646 | } | |
2647 | ||
2648 | wxImageHandler *wxImage::FindHandler(wxBitmapType bitmapType ) | |
2649 | { | |
2650 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
2651 | while (node) | |
2652 | { | |
2653 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
2654 | if (handler->GetType() == bitmapType) return handler; | |
2655 | node = node->GetNext(); | |
2656 | } | |
2657 | return NULL; | |
2658 | } | |
2659 | ||
2660 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) | |
2661 | { | |
2662 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
2663 | while (node) | |
2664 | { | |
2665 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
2666 | if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler; | |
2667 | node = node->GetNext(); | |
2668 | } | |
2669 | return NULL; | |
2670 | } | |
2671 | ||
2672 | void wxImage::InitStandardHandlers() | |
2673 | { | |
2674 | #if wxUSE_STREAMS | |
2675 | AddHandler(new wxBMPHandler); | |
2676 | #endif // wxUSE_STREAMS | |
2677 | } | |
2678 | ||
2679 | void wxImage::CleanUpHandlers() | |
2680 | { | |
2681 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); | |
2682 | while (node) | |
2683 | { | |
2684 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); | |
2685 | wxList::compatibility_iterator next = node->GetNext(); | |
2686 | delete handler; | |
2687 | node = next; | |
2688 | } | |
2689 | ||
2690 | sm_handlers.Clear(); | |
2691 | } | |
2692 | ||
2693 | wxString wxImage::GetImageExtWildcard() | |
2694 | { | |
2695 | wxString fmts; | |
2696 | ||
2697 | wxList& Handlers = wxImage::GetHandlers(); | |
2698 | wxList::compatibility_iterator Node = Handlers.GetFirst(); | |
2699 | while ( Node ) | |
2700 | { | |
2701 | wxImageHandler* Handler = (wxImageHandler*)Node->GetData(); | |
2702 | fmts += wxT("*.") + Handler->GetExtension(); | |
2703 | for (size_t i = 0; i < Handler->GetAltExtensions().size(); i++) | |
2704 | fmts += wxT(";*.") + Handler->GetAltExtensions()[i]; | |
2705 | Node = Node->GetNext(); | |
2706 | if ( Node ) fmts += wxT(";"); | |
2707 | } | |
2708 | ||
2709 | return wxT("(") + fmts + wxT(")|") + fmts; | |
2710 | } | |
2711 | ||
2712 | wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb) | |
2713 | { | |
2714 | const double red = rgb.red / 255.0, | |
2715 | green = rgb.green / 255.0, | |
2716 | blue = rgb.blue / 255.0; | |
2717 | ||
2718 | // find the min and max intensity (and remember which one was it for the | |
2719 | // latter) | |
2720 | double minimumRGB = red; | |
2721 | if ( green < minimumRGB ) | |
2722 | minimumRGB = green; | |
2723 | if ( blue < minimumRGB ) | |
2724 | minimumRGB = blue; | |
2725 | ||
2726 | enum { RED, GREEN, BLUE } chMax = RED; | |
2727 | double maximumRGB = red; | |
2728 | if ( green > maximumRGB ) | |
2729 | { | |
2730 | chMax = GREEN; | |
2731 | maximumRGB = green; | |
2732 | } | |
2733 | if ( blue > maximumRGB ) | |
2734 | { | |
2735 | chMax = BLUE; | |
2736 | maximumRGB = blue; | |
2737 | } | |
2738 | ||
2739 | const double value = maximumRGB; | |
2740 | ||
2741 | double hue = 0.0, saturation; | |
2742 | const double deltaRGB = maximumRGB - minimumRGB; | |
2743 | if ( wxIsNullDouble(deltaRGB) ) | |
2744 | { | |
2745 | // Gray has no color | |
2746 | hue = 0.0; | |
2747 | saturation = 0.0; | |
2748 | } | |
2749 | else | |
2750 | { | |
2751 | switch ( chMax ) | |
2752 | { | |
2753 | case RED: | |
2754 | hue = (green - blue) / deltaRGB; | |
2755 | break; | |
2756 | ||
2757 | case GREEN: | |
2758 | hue = 2.0 + (blue - red) / deltaRGB; | |
2759 | break; | |
2760 | ||
2761 | case BLUE: | |
2762 | hue = 4.0 + (red - green) / deltaRGB; | |
2763 | break; | |
2764 | ||
2765 | default: | |
2766 | wxFAIL_MSG(wxT("hue not specified")); | |
2767 | break; | |
2768 | } | |
2769 | ||
2770 | hue /= 6.0; | |
2771 | ||
2772 | if ( hue < 0.0 ) | |
2773 | hue += 1.0; | |
2774 | ||
2775 | saturation = deltaRGB / maximumRGB; | |
2776 | } | |
2777 | ||
2778 | return HSVValue(hue, saturation, value); | |
2779 | } | |
2780 | ||
2781 | wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv) | |
2782 | { | |
2783 | double red, green, blue; | |
2784 | ||
2785 | if ( wxIsNullDouble(hsv.saturation) ) | |
2786 | { | |
2787 | // Grey | |
2788 | red = hsv.value; | |
2789 | green = hsv.value; | |
2790 | blue = hsv.value; | |
2791 | } | |
2792 | else // not grey | |
2793 | { | |
2794 | double hue = hsv.hue * 6.0; // sector 0 to 5 | |
2795 | int i = (int)floor(hue); | |
2796 | double f = hue - i; // fractional part of h | |
2797 | double p = hsv.value * (1.0 - hsv.saturation); | |
2798 | ||
2799 | switch (i) | |
2800 | { | |
2801 | case 0: | |
2802 | red = hsv.value; | |
2803 | green = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2804 | blue = p; | |
2805 | break; | |
2806 | ||
2807 | case 1: | |
2808 | red = hsv.value * (1.0 - hsv.saturation * f); | |
2809 | green = hsv.value; | |
2810 | blue = p; | |
2811 | break; | |
2812 | ||
2813 | case 2: | |
2814 | red = p; | |
2815 | green = hsv.value; | |
2816 | blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2817 | break; | |
2818 | ||
2819 | case 3: | |
2820 | red = p; | |
2821 | green = hsv.value * (1.0 - hsv.saturation * f); | |
2822 | blue = hsv.value; | |
2823 | break; | |
2824 | ||
2825 | case 4: | |
2826 | red = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2827 | green = p; | |
2828 | blue = hsv.value; | |
2829 | break; | |
2830 | ||
2831 | default: // case 5: | |
2832 | red = hsv.value; | |
2833 | green = p; | |
2834 | blue = hsv.value * (1.0 - hsv.saturation * f); | |
2835 | break; | |
2836 | } | |
2837 | } | |
2838 | ||
2839 | return RGBValue((unsigned char)(red * 255.0), | |
2840 | (unsigned char)(green * 255.0), | |
2841 | (unsigned char)(blue * 255.0)); | |
2842 | } | |
2843 | ||
2844 | /* | |
2845 | * Rotates the hue of each pixel of the image. angle is a double in the range | |
2846 | * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees | |
2847 | */ | |
2848 | void wxImage::RotateHue(double angle) | |
2849 | { | |
2850 | AllocExclusive(); | |
2851 | ||
2852 | unsigned char *srcBytePtr; | |
2853 | unsigned char *dstBytePtr; | |
2854 | unsigned long count; | |
2855 | wxImage::HSVValue hsv; | |
2856 | wxImage::RGBValue rgb; | |
2857 | ||
2858 | wxASSERT (angle >= -1.0 && angle <= 1.0); | |
2859 | count = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
2860 | if ( count > 0 && !wxIsNullDouble(angle) ) | |
2861 | { | |
2862 | srcBytePtr = M_IMGDATA->m_data; | |
2863 | dstBytePtr = srcBytePtr; | |
2864 | do | |
2865 | { | |
2866 | rgb.red = *srcBytePtr++; | |
2867 | rgb.green = *srcBytePtr++; | |
2868 | rgb.blue = *srcBytePtr++; | |
2869 | hsv = RGBtoHSV(rgb); | |
2870 | ||
2871 | hsv.hue = hsv.hue + angle; | |
2872 | if (hsv.hue > 1.0) | |
2873 | hsv.hue = hsv.hue - 1.0; | |
2874 | else if (hsv.hue < 0.0) | |
2875 | hsv.hue = hsv.hue + 1.0; | |
2876 | ||
2877 | rgb = HSVtoRGB(hsv); | |
2878 | *dstBytePtr++ = rgb.red; | |
2879 | *dstBytePtr++ = rgb.green; | |
2880 | *dstBytePtr++ = rgb.blue; | |
2881 | } while (--count != 0); | |
2882 | } | |
2883 | } | |
2884 | ||
2885 | //----------------------------------------------------------------------------- | |
2886 | // wxImageHandler | |
2887 | //----------------------------------------------------------------------------- | |
2888 | ||
2889 | IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject) | |
2890 | ||
2891 | #if wxUSE_STREAMS | |
2892 | int wxImageHandler::GetImageCount( wxInputStream& stream ) | |
2893 | { | |
2894 | // NOTE: this code is the same of wxAnimationDecoder::CanRead and | |
2895 | // wxImageHandler::CallDoCanRead | |
2896 | ||
2897 | if ( !stream.IsSeekable() ) | |
2898 | return false; // can't test unseekable stream | |
2899 | ||
2900 | wxFileOffset posOld = stream.TellI(); | |
2901 | int n = DoGetImageCount(stream); | |
2902 | ||
2903 | // restore the old position to be able to test other formats and so on | |
2904 | if ( stream.SeekI(posOld) == wxInvalidOffset ) | |
2905 | { | |
2906 | wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!")); | |
2907 | ||
2908 | // reading would fail anyhow as we're not at the right position | |
2909 | return false; | |
2910 | } | |
2911 | ||
2912 | return n; | |
2913 | } | |
2914 | ||
2915 | bool wxImageHandler::CanRead( const wxString& name ) | |
2916 | { | |
2917 | wxImageFileInputStream stream(name); | |
2918 | if ( !stream.IsOk() ) | |
2919 | { | |
2920 | wxLogError(_("Failed to check format of image file \"%s\"."), name); | |
2921 | ||
2922 | return false; | |
2923 | } | |
2924 | ||
2925 | return CanRead(stream); | |
2926 | } | |
2927 | ||
2928 | bool wxImageHandler::CallDoCanRead(wxInputStream& stream) | |
2929 | { | |
2930 | // NOTE: this code is the same of wxAnimationDecoder::CanRead and | |
2931 | // wxImageHandler::GetImageCount | |
2932 | ||
2933 | if ( !stream.IsSeekable() ) | |
2934 | return false; // can't test unseekable stream | |
2935 | ||
2936 | wxFileOffset posOld = stream.TellI(); | |
2937 | bool ok = DoCanRead(stream); | |
2938 | ||
2939 | // restore the old position to be able to test other formats and so on | |
2940 | if ( stream.SeekI(posOld) == wxInvalidOffset ) | |
2941 | { | |
2942 | wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!")); | |
2943 | ||
2944 | // reading would fail anyhow as we're not at the right position | |
2945 | return false; | |
2946 | } | |
2947 | ||
2948 | return ok; | |
2949 | } | |
2950 | ||
2951 | #endif // wxUSE_STREAMS | |
2952 | ||
2953 | /* static */ | |
2954 | wxImageResolution | |
2955 | wxImageHandler::GetResolutionFromOptions(const wxImage& image, int *x, int *y) | |
2956 | { | |
2957 | wxCHECK_MSG( x && y, wxIMAGE_RESOLUTION_NONE, wxT("NULL pointer") ); | |
2958 | ||
2959 | if ( image.HasOption(wxIMAGE_OPTION_RESOLUTIONX) && | |
2960 | image.HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
2961 | { | |
2962 | *x = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX); | |
2963 | *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); | |
2964 | } | |
2965 | else if ( image.HasOption(wxIMAGE_OPTION_RESOLUTION) ) | |
2966 | { | |
2967 | *x = | |
2968 | *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTION); | |
2969 | } | |
2970 | else // no resolution options specified | |
2971 | { | |
2972 | *x = | |
2973 | *y = 0; | |
2974 | ||
2975 | return wxIMAGE_RESOLUTION_NONE; | |
2976 | } | |
2977 | ||
2978 | // get the resolution unit too | |
2979 | int resUnit = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT); | |
2980 | if ( !resUnit ) | |
2981 | { | |
2982 | // this is the default | |
2983 | resUnit = wxIMAGE_RESOLUTION_INCHES; | |
2984 | } | |
2985 | ||
2986 | return (wxImageResolution)resUnit; | |
2987 | } | |
2988 | ||
2989 | // ---------------------------------------------------------------------------- | |
2990 | // image histogram stuff | |
2991 | // ---------------------------------------------------------------------------- | |
2992 | ||
2993 | bool | |
2994 | wxImageHistogram::FindFirstUnusedColour(unsigned char *r, | |
2995 | unsigned char *g, | |
2996 | unsigned char *b, | |
2997 | unsigned char r2, | |
2998 | unsigned char b2, | |
2999 | unsigned char g2) const | |
3000 | { | |
3001 | unsigned long key = MakeKey(r2, g2, b2); | |
3002 | ||
3003 | while ( find(key) != end() ) | |
3004 | { | |
3005 | // color already used | |
3006 | r2++; | |
3007 | if ( r2 >= 255 ) | |
3008 | { | |
3009 | r2 = 0; | |
3010 | g2++; | |
3011 | if ( g2 >= 255 ) | |
3012 | { | |
3013 | g2 = 0; | |
3014 | b2++; | |
3015 | if ( b2 >= 255 ) | |
3016 | { | |
3017 | wxLogError(_("No unused colour in image.") ); | |
3018 | return false; | |
3019 | } | |
3020 | } | |
3021 | } | |
3022 | ||
3023 | key = MakeKey(r2, g2, b2); | |
3024 | } | |
3025 | ||
3026 | if ( r ) | |
3027 | *r = r2; | |
3028 | if ( g ) | |
3029 | *g = g2; | |
3030 | if ( b ) | |
3031 | *b = b2; | |
3032 | ||
3033 | return true; | |
3034 | } | |
3035 | ||
3036 | bool | |
3037 | wxImage::FindFirstUnusedColour(unsigned char *r, | |
3038 | unsigned char *g, | |
3039 | unsigned char *b, | |
3040 | unsigned char r2, | |
3041 | unsigned char b2, | |
3042 | unsigned char g2) const | |
3043 | { | |
3044 | wxImageHistogram histogram; | |
3045 | ||
3046 | ComputeHistogram(histogram); | |
3047 | ||
3048 | return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2); | |
3049 | } | |
3050 | ||
3051 | ||
3052 | ||
3053 | // GRG, Dic/99 | |
3054 | // Counts and returns the number of different colours. Optionally stops | |
3055 | // when it exceeds 'stopafter' different colours. This is useful, for | |
3056 | // example, to see if the image can be saved as 8-bit (256 colour or | |
3057 | // less, in this case it would be invoked as CountColours(256)). Default | |
3058 | // value for stopafter is -1 (don't care). | |
3059 | // | |
3060 | unsigned long wxImage::CountColours( unsigned long stopafter ) const | |
3061 | { | |
3062 | wxHashTable h; | |
3063 | wxObject dummy; | |
3064 | unsigned char r, g, b; | |
3065 | unsigned char *p; | |
3066 | unsigned long size, nentries, key; | |
3067 | ||
3068 | p = GetData(); | |
3069 | size = GetWidth() * GetHeight(); | |
3070 | nentries = 0; | |
3071 | ||
3072 | for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++) | |
3073 | { | |
3074 | r = *(p++); | |
3075 | g = *(p++); | |
3076 | b = *(p++); | |
3077 | key = wxImageHistogram::MakeKey(r, g, b); | |
3078 | ||
3079 | if (h.Get(key) == NULL) | |
3080 | { | |
3081 | h.Put(key, &dummy); | |
3082 | nentries++; | |
3083 | } | |
3084 | } | |
3085 | ||
3086 | return nentries; | |
3087 | } | |
3088 | ||
3089 | ||
3090 | unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const | |
3091 | { | |
3092 | unsigned char *p = GetData(); | |
3093 | unsigned long nentries = 0; | |
3094 | ||
3095 | h.clear(); | |
3096 | ||
3097 | const unsigned long size = GetWidth() * GetHeight(); | |
3098 | ||
3099 | unsigned char r, g, b; | |
3100 | for ( unsigned long n = 0; n < size; n++ ) | |
3101 | { | |
3102 | r = *p++; | |
3103 | g = *p++; | |
3104 | b = *p++; | |
3105 | ||
3106 | wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)]; | |
3107 | ||
3108 | if ( entry.value++ == 0 ) | |
3109 | entry.index = nentries++; | |
3110 | } | |
3111 | ||
3112 | return nentries; | |
3113 | } | |
3114 | ||
3115 | /* | |
3116 | * Rotation code by Carlos Moreno | |
3117 | */ | |
3118 | ||
3119 | static const double wxROTATE_EPSILON = 1e-10; | |
3120 | ||
3121 | // Auxiliary function to rotate a point (x,y) with respect to point p0 | |
3122 | // make it inline and use a straight return to facilitate optimization | |
3123 | // also, the function receives the sine and cosine of the angle to avoid | |
3124 | // repeating the time-consuming calls to these functions -- sin/cos can | |
3125 | // be computed and stored in the calling function. | |
3126 | ||
3127 | static inline wxRealPoint | |
3128 | wxRotatePoint(const wxRealPoint& p, double cos_angle, double sin_angle, | |
3129 | const wxRealPoint& p0) | |
3130 | { | |
3131 | return wxRealPoint(p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle, | |
3132 | p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle); | |
3133 | } | |
3134 | ||
3135 | static inline wxRealPoint | |
3136 | wxRotatePoint(double x, double y, double cos_angle, double sin_angle, | |
3137 | const wxRealPoint & p0) | |
3138 | { | |
3139 | return wxRotatePoint (wxRealPoint(x,y), cos_angle, sin_angle, p0); | |
3140 | } | |
3141 | ||
3142 | wxImage wxImage::Rotate(double angle, | |
3143 | const wxPoint& centre_of_rotation, | |
3144 | bool interpolating, | |
3145 | wxPoint *offset_after_rotation) const | |
3146 | { | |
3147 | // screen coordinates are a mirror image of "real" coordinates | |
3148 | angle = -angle; | |
3149 | ||
3150 | const bool has_alpha = HasAlpha(); | |
3151 | ||
3152 | const int w = GetWidth(); | |
3153 | const int h = GetHeight(); | |
3154 | ||
3155 | int i; | |
3156 | ||
3157 | // Create pointer-based array to accelerate access to wxImage's data | |
3158 | unsigned char ** data = new unsigned char * [h]; | |
3159 | data[0] = GetData(); | |
3160 | for (i = 1; i < h; i++) | |
3161 | data[i] = data[i - 1] + (3 * w); | |
3162 | ||
3163 | // Same for alpha channel | |
3164 | unsigned char ** alpha = NULL; | |
3165 | if (has_alpha) | |
3166 | { | |
3167 | alpha = new unsigned char * [h]; | |
3168 | alpha[0] = GetAlpha(); | |
3169 | for (i = 1; i < h; i++) | |
3170 | alpha[i] = alpha[i - 1] + w; | |
3171 | } | |
3172 | ||
3173 | // precompute coefficients for rotation formula | |
3174 | const double cos_angle = cos(angle); | |
3175 | const double sin_angle = sin(angle); | |
3176 | ||
3177 | // Create new Image to store the result | |
3178 | // First, find rectangle that covers the rotated image; to do that, | |
3179 | // rotate the four corners | |
3180 | ||
3181 | const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y); | |
3182 | ||
3183 | wxRealPoint p1 = wxRotatePoint (0, 0, cos_angle, sin_angle, p0); | |
3184 | wxRealPoint p2 = wxRotatePoint (0, h, cos_angle, sin_angle, p0); | |
3185 | wxRealPoint p3 = wxRotatePoint (w, 0, cos_angle, sin_angle, p0); | |
3186 | wxRealPoint p4 = wxRotatePoint (w, h, cos_angle, sin_angle, p0); | |
3187 | ||
3188 | int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x))); | |
3189 | int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y))); | |
3190 | int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x))); | |
3191 | int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y))); | |
3192 | ||
3193 | // Create rotated image | |
3194 | wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false); | |
3195 | // With alpha channel | |
3196 | if (has_alpha) | |
3197 | rotated.SetAlpha(); | |
3198 | ||
3199 | if (offset_after_rotation != NULL) | |
3200 | { | |
3201 | *offset_after_rotation = wxPoint (x1a, y1a); | |
3202 | } | |
3203 | ||
3204 | // the rotated (destination) image is always accessed sequentially via this | |
3205 | // pointer, there is no need for pointer-based arrays here | |
3206 | unsigned char *dst = rotated.GetData(); | |
3207 | ||
3208 | unsigned char *alpha_dst = has_alpha ? rotated.GetAlpha() : NULL; | |
3209 | ||
3210 | // if the original image has a mask, use its RGB values as the blank pixel, | |
3211 | // else, fall back to default (black). | |
3212 | unsigned char blank_r = 0; | |
3213 | unsigned char blank_g = 0; | |
3214 | unsigned char blank_b = 0; | |
3215 | ||
3216 | if (HasMask()) | |
3217 | { | |
3218 | blank_r = GetMaskRed(); | |
3219 | blank_g = GetMaskGreen(); | |
3220 | blank_b = GetMaskBlue(); | |
3221 | rotated.SetMaskColour( blank_r, blank_g, blank_b ); | |
3222 | } | |
3223 | ||
3224 | // Now, for each point of the rotated image, find where it came from, by | |
3225 | // performing an inverse rotation (a rotation of -angle) and getting the | |
3226 | // pixel at those coordinates | |
3227 | ||
3228 | const int rH = rotated.GetHeight(); | |
3229 | const int rW = rotated.GetWidth(); | |
3230 | ||
3231 | // do the (interpolating) test outside of the loops, so that it is done | |
3232 | // only once, instead of repeating it for each pixel. | |
3233 | if (interpolating) | |
3234 | { | |
3235 | for (int y = 0; y < rH; y++) | |
3236 | { | |
3237 | for (int x = 0; x < rW; x++) | |
3238 | { | |
3239 | wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0); | |
3240 | ||
3241 | if (-0.25 < src.x && src.x < w - 0.75 && | |
3242 | -0.25 < src.y && src.y < h - 0.75) | |
3243 | { | |
3244 | // interpolate using the 4 enclosing grid-points. Those | |
3245 | // points can be obtained using floor and ceiling of the | |
3246 | // exact coordinates of the point | |
3247 | int x1, y1, x2, y2; | |
3248 | ||
3249 | if (0 < src.x && src.x < w - 1) | |
3250 | { | |
3251 | x1 = wxRound(floor(src.x)); | |
3252 | x2 = wxRound(ceil(src.x)); | |
3253 | } | |
3254 | else // else means that x is near one of the borders (0 or width-1) | |
3255 | { | |
3256 | x1 = x2 = wxRound (src.x); | |
3257 | } | |
3258 | ||
3259 | if (0 < src.y && src.y < h - 1) | |
3260 | { | |
3261 | y1 = wxRound(floor(src.y)); | |
3262 | y2 = wxRound(ceil(src.y)); | |
3263 | } | |
3264 | else | |
3265 | { | |
3266 | y1 = y2 = wxRound (src.y); | |
3267 | } | |
3268 | ||
3269 | // get four points and the distances (square of the distance, | |
3270 | // for efficiency reasons) for the interpolation formula | |
3271 | ||
3272 | // GRG: Do not calculate the points until they are | |
3273 | // really needed -- this way we can calculate | |
3274 | // just one, instead of four, if d1, d2, d3 | |
3275 | // or d4 are < wxROTATE_EPSILON | |
3276 | ||
3277 | const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1); | |
3278 | const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1); | |
3279 | const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2); | |
3280 | const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2); | |
3281 | ||
3282 | // Now interpolate as a weighted average of the four surrounding | |
3283 | // points, where the weights are the distances to each of those points | |
3284 | ||
3285 | // If the point is exactly at one point of the grid of the source | |
3286 | // image, then don't interpolate -- just assign the pixel | |
3287 | ||
3288 | // d1,d2,d3,d4 are positive -- no need for abs() | |
3289 | if (d1 < wxROTATE_EPSILON) | |
3290 | { | |
3291 | unsigned char *p = data[y1] + (3 * x1); | |
3292 | *(dst++) = *(p++); | |
3293 | *(dst++) = *(p++); | |
3294 | *(dst++) = *p; | |
3295 | ||
3296 | if (has_alpha) | |
3297 | *(alpha_dst++) = *(alpha[y1] + x1); | |
3298 | } | |
3299 | else if (d2 < wxROTATE_EPSILON) | |
3300 | { | |
3301 | unsigned char *p = data[y1] + (3 * x2); | |
3302 | *(dst++) = *(p++); | |
3303 | *(dst++) = *(p++); | |
3304 | *(dst++) = *p; | |
3305 | ||
3306 | if (has_alpha) | |
3307 | *(alpha_dst++) = *(alpha[y1] + x2); | |
3308 | } | |
3309 | else if (d3 < wxROTATE_EPSILON) | |
3310 | { | |
3311 | unsigned char *p = data[y2] + (3 * x2); | |
3312 | *(dst++) = *(p++); | |
3313 | *(dst++) = *(p++); | |
3314 | *(dst++) = *p; | |
3315 | ||
3316 | if (has_alpha) | |
3317 | *(alpha_dst++) = *(alpha[y2] + x2); | |
3318 | } | |
3319 | else if (d4 < wxROTATE_EPSILON) | |
3320 | { | |
3321 | unsigned char *p = data[y2] + (3 * x1); | |
3322 | *(dst++) = *(p++); | |
3323 | *(dst++) = *(p++); | |
3324 | *(dst++) = *p; | |
3325 | ||
3326 | if (has_alpha) | |
3327 | *(alpha_dst++) = *(alpha[y2] + x1); | |
3328 | } | |
3329 | else | |
3330 | { | |
3331 | // weights for the weighted average are proportional to the inverse of the distance | |
3332 | unsigned char *v1 = data[y1] + (3 * x1); | |
3333 | unsigned char *v2 = data[y1] + (3 * x2); | |
3334 | unsigned char *v3 = data[y2] + (3 * x2); | |
3335 | unsigned char *v4 = data[y2] + (3 * x1); | |
3336 | ||
3337 | const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4; | |
3338 | ||
3339 | // GRG: Unrolled. | |
3340 | ||
3341 | *(dst++) = (unsigned char) | |
3342 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
3343 | w3 * *(v3++) + w4 * *(v4++)) / | |
3344 | (w1 + w2 + w3 + w4) ); | |
3345 | *(dst++) = (unsigned char) | |
3346 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
3347 | w3 * *(v3++) + w4 * *(v4++)) / | |
3348 | (w1 + w2 + w3 + w4) ); | |
3349 | *(dst++) = (unsigned char) | |
3350 | ( (w1 * *v1 + w2 * *v2 + | |
3351 | w3 * *v3 + w4 * *v4) / | |
3352 | (w1 + w2 + w3 + w4) ); | |
3353 | ||
3354 | if (has_alpha) | |
3355 | { | |
3356 | v1 = alpha[y1] + (x1); | |
3357 | v2 = alpha[y1] + (x2); | |
3358 | v3 = alpha[y2] + (x2); | |
3359 | v4 = alpha[y2] + (x1); | |
3360 | ||
3361 | *(alpha_dst++) = (unsigned char) | |
3362 | ( (w1 * *v1 + w2 * *v2 + | |
3363 | w3 * *v3 + w4 * *v4) / | |
3364 | (w1 + w2 + w3 + w4) ); | |
3365 | } | |
3366 | } | |
3367 | } | |
3368 | else | |
3369 | { | |
3370 | *(dst++) = blank_r; | |
3371 | *(dst++) = blank_g; | |
3372 | *(dst++) = blank_b; | |
3373 | ||
3374 | if (has_alpha) | |
3375 | *(alpha_dst++) = 0; | |
3376 | } | |
3377 | } | |
3378 | } | |
3379 | } | |
3380 | else // not interpolating | |
3381 | { | |
3382 | for (int y = 0; y < rH; y++) | |
3383 | { | |
3384 | for (int x = 0; x < rW; x++) | |
3385 | { | |
3386 | wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0); | |
3387 | ||
3388 | const int xs = wxRound (src.x); // wxRound rounds to the | |
3389 | const int ys = wxRound (src.y); // closest integer | |
3390 | ||
3391 | if (0 <= xs && xs < w && 0 <= ys && ys < h) | |
3392 | { | |
3393 | unsigned char *p = data[ys] + (3 * xs); | |
3394 | *(dst++) = *(p++); | |
3395 | *(dst++) = *(p++); | |
3396 | *(dst++) = *p; | |
3397 | ||
3398 | if (has_alpha) | |
3399 | *(alpha_dst++) = *(alpha[ys] + (xs)); | |
3400 | } | |
3401 | else | |
3402 | { | |
3403 | *(dst++) = blank_r; | |
3404 | *(dst++) = blank_g; | |
3405 | *(dst++) = blank_b; | |
3406 | ||
3407 | if (has_alpha) | |
3408 | *(alpha_dst++) = 255; | |
3409 | } | |
3410 | } | |
3411 | } | |
3412 | } | |
3413 | ||
3414 | delete [] data; | |
3415 | delete [] alpha; | |
3416 | ||
3417 | return rotated; | |
3418 | } | |
3419 | ||
3420 | ||
3421 | ||
3422 | ||
3423 | ||
3424 | // A module to allow wxImage initialization/cleanup | |
3425 | // without calling these functions from app.cpp or from | |
3426 | // the user's application. | |
3427 | ||
3428 | class wxImageModule: public wxModule | |
3429 | { | |
3430 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
3431 | public: | |
3432 | wxImageModule() {} | |
3433 | bool OnInit() { wxImage::InitStandardHandlers(); return true; } | |
3434 | void OnExit() { wxImage::CleanUpHandlers(); } | |
3435 | }; | |
3436 | ||
3437 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) | |
3438 | ||
3439 | ||
3440 | #endif // wxUSE_IMAGE |