]> git.saurik.com Git - wxWidgets.git/blob - src/common/image.cpp
Fix wxIMAGE_QUALITY_HIGH definition.
[wxWidgets.git] / src / common / image.cpp
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 IsOk();
144 #else
145 wxUnusedVar(xpmData);
146 return false;
147 #endif
148 }
149
150 bool wxImage::Create( int width, int height, bool clear )
151 {
152 UnRef();
153
154 m_refData = new wxImageRefData();
155
156 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
157 if (!M_IMGDATA->m_data)
158 {
159 UnRef();
160 return false;
161 }
162
163 M_IMGDATA->m_width = width;
164 M_IMGDATA->m_height = height;
165 M_IMGDATA->m_ok = true;
166
167 if (clear)
168 {
169 Clear();
170 }
171
172 return true;
173 }
174
175 bool wxImage::Create( int width, int height, unsigned char* data, bool static_data )
176 {
177 UnRef();
178
179 wxCHECK_MSG( data, false, wxT("NULL data in wxImage::Create") );
180
181 m_refData = new wxImageRefData();
182
183 M_IMGDATA->m_data = data;
184 M_IMGDATA->m_width = width;
185 M_IMGDATA->m_height = height;
186 M_IMGDATA->m_ok = true;
187 M_IMGDATA->m_static = static_data;
188
189 return true;
190 }
191
192 bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
193 {
194 UnRef();
195
196 wxCHECK_MSG( data, false, wxT("NULL data in wxImage::Create") );
197
198 m_refData = new wxImageRefData();
199
200 M_IMGDATA->m_data = data;
201 M_IMGDATA->m_alpha = alpha;
202 M_IMGDATA->m_width = width;
203 M_IMGDATA->m_height = height;
204 M_IMGDATA->m_ok = true;
205 M_IMGDATA->m_static = static_data;
206 M_IMGDATA->m_staticAlpha = static_data;
207
208 return true;
209 }
210
211 void wxImage::Destroy()
212 {
213 UnRef();
214 }
215
216 void wxImage::Clear(unsigned char value)
217 {
218 memset(M_IMGDATA->m_data, value, M_IMGDATA->m_width*M_IMGDATA->m_height*3);
219 }
220
221 wxObjectRefData* wxImage::CreateRefData() const
222 {
223 return new wxImageRefData;
224 }
225
226 wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const
227 {
228 const wxImageRefData* refData = static_cast<const wxImageRefData*>(that);
229 wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") );
230
231 wxImageRefData* refData_new = new wxImageRefData;
232 refData_new->m_width = refData->m_width;
233 refData_new->m_height = refData->m_height;
234 refData_new->m_maskRed = refData->m_maskRed;
235 refData_new->m_maskGreen = refData->m_maskGreen;
236 refData_new->m_maskBlue = refData->m_maskBlue;
237 refData_new->m_hasMask = refData->m_hasMask;
238 refData_new->m_ok = true;
239 unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height);
240 if (refData->m_alpha != NULL)
241 {
242 refData_new->m_alpha = (unsigned char*)malloc(size);
243 memcpy(refData_new->m_alpha, refData->m_alpha, size);
244 }
245 size *= 3;
246 refData_new->m_data = (unsigned char*)malloc(size);
247 memcpy(refData_new->m_data, refData->m_data, size);
248 #if wxUSE_PALETTE
249 refData_new->m_palette = refData->m_palette;
250 #endif
251 refData_new->m_optionNames = refData->m_optionNames;
252 refData_new->m_optionValues = refData->m_optionValues;
253 return refData_new;
254 }
255
256 // returns a new image with the same dimensions, alpha, and mask as *this
257 // if on_its_side is true, width and height are swapped
258 wxImage wxImage::MakeEmptyClone(int flags) const
259 {
260 wxImage image;
261
262 wxCHECK_MSG( IsOk(), image, wxS("invalid image") );
263
264 long height = M_IMGDATA->m_height;
265 long width = M_IMGDATA->m_width;
266
267 if ( flags & Clone_SwapOrientation )
268 wxSwap( width, height );
269
270 if ( !image.Create( width, height, false ) )
271 {
272 wxFAIL_MSG( wxS("unable to create image") );
273 return image;
274 }
275
276 if ( M_IMGDATA->m_alpha )
277 {
278 image.SetAlpha();
279 wxCHECK2_MSG( image.GetAlpha(), return wxImage(),
280 wxS("unable to create alpha channel") );
281 }
282
283 if ( M_IMGDATA->m_hasMask )
284 {
285 image.SetMaskColour( M_IMGDATA->m_maskRed,
286 M_IMGDATA->m_maskGreen,
287 M_IMGDATA->m_maskBlue );
288 }
289
290 return image;
291 }
292
293 wxImage wxImage::Copy() const
294 {
295 wxImage image;
296
297 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
298
299 image.m_refData = CloneRefData(m_refData);
300
301 return image;
302 }
303
304 wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
305 {
306 if( xFactor == 1 && yFactor == 1 )
307 return *this;
308
309 wxImage image;
310
311 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
312
313 // can't scale to/from 0 size
314 wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image,
315 wxT("invalid new image size") );
316
317 long old_height = M_IMGDATA->m_height,
318 old_width = M_IMGDATA->m_width;
319
320 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
321 wxT("invalid old image size") );
322
323 long width = old_width / xFactor ;
324 long height = old_height / yFactor ;
325
326 image.Create( width, height, false );
327
328 char unsigned *data = image.GetData();
329
330 wxCHECK_MSG( data, image, wxT("unable to create image") );
331
332 bool hasMask = false ;
333 unsigned char maskRed = 0;
334 unsigned char maskGreen = 0;
335 unsigned char maskBlue = 0 ;
336
337 const unsigned char *source_data = M_IMGDATA->m_data;
338 unsigned char *target_data = data;
339 const unsigned char *source_alpha = 0 ;
340 unsigned char *target_alpha = 0 ;
341 if (M_IMGDATA->m_hasMask)
342 {
343 hasMask = true ;
344 maskRed = M_IMGDATA->m_maskRed;
345 maskGreen = M_IMGDATA->m_maskGreen;
346 maskBlue =M_IMGDATA->m_maskBlue ;
347
348 image.SetMaskColour( M_IMGDATA->m_maskRed,
349 M_IMGDATA->m_maskGreen,
350 M_IMGDATA->m_maskBlue );
351 }
352 else
353 {
354 source_alpha = M_IMGDATA->m_alpha ;
355 if ( source_alpha )
356 {
357 image.SetAlpha() ;
358 target_alpha = image.GetAlpha() ;
359 }
360 }
361
362 for (long y = 0; y < height; y++)
363 {
364 for (long x = 0; x < width; x++)
365 {
366 unsigned long avgRed = 0 ;
367 unsigned long avgGreen = 0;
368 unsigned long avgBlue = 0;
369 unsigned long avgAlpha = 0 ;
370 unsigned long counter = 0 ;
371 // determine average
372 for ( int y1 = 0 ; y1 < yFactor ; ++y1 )
373 {
374 long y_offset = (y * yFactor + y1) * old_width;
375 for ( int x1 = 0 ; x1 < xFactor ; ++x1 )
376 {
377 const unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ;
378 unsigned char red = pixel[0] ;
379 unsigned char green = pixel[1] ;
380 unsigned char blue = pixel[2] ;
381 unsigned char alpha = 255 ;
382 if ( source_alpha )
383 alpha = *(source_alpha + y_offset + x * xFactor + x1) ;
384 if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
385 {
386 if ( alpha > 0 )
387 {
388 avgRed += red ;
389 avgGreen += green ;
390 avgBlue += blue ;
391 }
392 avgAlpha += alpha ;
393 counter++ ;
394 }
395 }
396 }
397 if ( counter == 0 )
398 {
399 *(target_data++) = M_IMGDATA->m_maskRed ;
400 *(target_data++) = M_IMGDATA->m_maskGreen ;
401 *(target_data++) = M_IMGDATA->m_maskBlue ;
402 }
403 else
404 {
405 if ( source_alpha )
406 *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ;
407 *(target_data++) = (unsigned char)(avgRed / counter);
408 *(target_data++) = (unsigned char)(avgGreen / counter);
409 *(target_data++) = (unsigned char)(avgBlue / counter);
410 }
411 }
412 }
413
414 // In case this is a cursor, make sure the hotspot is scaled accordingly:
415 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
416 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
417 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor);
418 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
419 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
420 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor);
421
422 return image;
423 }
424
425 wxImage
426 wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const
427 {
428 wxImage image;
429
430 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
431
432 // can't scale to/from 0 size
433 wxCHECK_MSG( (width > 0) && (height > 0), image,
434 wxT("invalid new image size") );
435
436 long old_height = M_IMGDATA->m_height,
437 old_width = M_IMGDATA->m_width;
438 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
439 wxT("invalid old image size") );
440
441 // If the image's new width and height are the same as the original, no
442 // need to waste time or CPU cycles
443 if ( old_width == width && old_height == height )
444 return *this;
445
446 // Resample the image using the method as specified.
447 switch ( quality )
448 {
449 case wxIMAGE_QUALITY_NEAREST:
450 if ( old_width % width == 0 && old_width >= width &&
451 old_height % height == 0 && old_height >= height )
452 {
453 return ShrinkBy( old_width / width , old_height / height );
454 }
455
456 image = ResampleNearest(width, height);
457 break;
458
459 case wxIMAGE_QUALITY_BILINEAR:
460 image = ResampleBilinear(width, height);
461 break;
462
463 case wxIMAGE_QUALITY_BICUBIC:
464 image = ResampleBicubic(width, height);
465 break;
466
467 case wxIMAGE_QUALITY_BOX_AVERAGE:
468 image = ResampleBox(width, height);
469 break;
470
471 case wxIMAGE_QUALITY_HIGH:
472 image = width < old_width && height < old_height
473 ? ResampleBox(width, height)
474 : ResampleBicubic(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.IsOk(), 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.IsOk(), 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.IsOk(), 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 64-byte 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 ii = 0; ii < width; )
1138 {
1139 long next_ii = wxMin(ii + 64, width);
1140
1141 for (long j = 0; j < height; j++)
1142 {
1143 source_alpha = M_IMGDATA->m_alpha + j*width + ii;
1144
1145 for (long i = ii; i < next_ii; i++)
1146 {
1147 if ( clockwise )
1148 {
1149 target_alpha = alpha_data + (i+1)*height - j - 1;
1150 }
1151 else
1152 {
1153 target_alpha = alpha_data + height*(width - i - 1) + j;
1154 }
1155
1156 *target_alpha = *source_alpha++;
1157 }
1158 }
1159
1160 ii = next_ii;
1161 }
1162 }
1163
1164 return image;
1165 }
1166
1167 wxImage wxImage::Rotate180() const
1168 {
1169 wxImage image(MakeEmptyClone());
1170
1171 wxCHECK( image.IsOk(), image );
1172
1173 long height = M_IMGDATA->m_height;
1174 long width = M_IMGDATA->m_width;
1175
1176 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
1177 {
1178 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
1179 width - 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X));
1180 }
1181
1182 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
1183 {
1184 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
1185 height - 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y));
1186 }
1187
1188 unsigned char *data = image.GetData();
1189 unsigned char *alpha = image.GetAlpha();
1190 const unsigned char *source_data = M_IMGDATA->m_data;
1191 unsigned char *target_data = data + width * height * 3;
1192
1193 for (long j = 0; j < height; j++)
1194 {
1195 for (long i = 0; i < width; i++)
1196 {
1197 target_data -= 3;
1198 memcpy( target_data, source_data, 3 );
1199 source_data += 3;
1200 }
1201 }
1202
1203 if ( alpha )
1204 {
1205 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
1206 unsigned char *dest_alpha = alpha + width * height;
1207
1208 for (long j = 0; j < height; ++j)
1209 {
1210 for (long i = 0; i < width; ++i)
1211 {
1212 *(--dest_alpha) = *(src_alpha++);
1213 }
1214 }
1215 }
1216
1217 return image;
1218 }
1219
1220 wxImage wxImage::Mirror( bool horizontally ) const
1221 {
1222 wxImage image(MakeEmptyClone());
1223
1224 wxCHECK( image.IsOk(), image );
1225
1226 long height = M_IMGDATA->m_height;
1227 long width = M_IMGDATA->m_width;
1228
1229 unsigned char *data = image.GetData();
1230 unsigned char *alpha = image.GetAlpha();
1231 const unsigned char *source_data = M_IMGDATA->m_data;
1232 unsigned char *target_data;
1233
1234 if (horizontally)
1235 {
1236 for (long j = 0; j < height; j++)
1237 {
1238 data += width*3;
1239 target_data = data-3;
1240 for (long i = 0; i < width; i++)
1241 {
1242 memcpy( target_data, source_data, 3 );
1243 source_data += 3;
1244 target_data -= 3;
1245 }
1246 }
1247
1248 if (alpha != NULL)
1249 {
1250 // src_alpha starts at the first pixel and increases by 1 after each step
1251 // (a step here is the copy of the alpha value of one pixel)
1252 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
1253 // dest_alpha starts just beyond the first line, decreases before each step,
1254 // and after each line is finished, increases by 2 widths (skipping the line
1255 // just copied and the line that will be copied next)
1256 unsigned char *dest_alpha = alpha + width;
1257
1258 for (long jj = 0; jj < height; ++jj)
1259 {
1260 for (long i = 0; i < width; ++i) {
1261 *(--dest_alpha) = *(src_alpha++); // copy one pixel
1262 }
1263 dest_alpha += 2 * width; // advance beyond the end of the next line
1264 }
1265 }
1266 }
1267 else
1268 {
1269 for (long i = 0; i < height; i++)
1270 {
1271 target_data = data + 3*width*(height-1-i);
1272 memcpy( target_data, source_data, (size_t)3*width );
1273 source_data += 3*width;
1274 }
1275
1276 if ( alpha )
1277 {
1278 // src_alpha starts at the first pixel and increases by 1 width after each step
1279 // (a step here is the copy of the alpha channel of an entire line)
1280 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
1281 // dest_alpha starts just beyond the last line (beyond the whole image)
1282 // and decreases by 1 width before each step
1283 unsigned char *dest_alpha = alpha + width * height;
1284
1285 for (long jj = 0; jj < height; ++jj)
1286 {
1287 dest_alpha -= width;
1288 memcpy( dest_alpha, src_alpha, (size_t)width );
1289 src_alpha += width;
1290 }
1291 }
1292 }
1293
1294 return image;
1295 }
1296
1297 wxImage wxImage::GetSubImage( const wxRect &rect ) const
1298 {
1299 wxImage image;
1300
1301 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
1302
1303 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) &&
1304 (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
1305 image, wxT("invalid subimage size") );
1306
1307 const int subwidth = rect.GetWidth();
1308 const int subheight = rect.GetHeight();
1309
1310 image.Create( subwidth, subheight, false );
1311
1312 const unsigned char *src_data = GetData();
1313 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
1314 unsigned char *subdata = image.GetData();
1315 unsigned char *subalpha = NULL;
1316
1317 wxCHECK_MSG( subdata, image, wxT("unable to create image") );
1318
1319 if ( src_alpha ) {
1320 image.SetAlpha();
1321 subalpha = image.GetAlpha();
1322 wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel"));
1323 }
1324
1325 if (M_IMGDATA->m_hasMask)
1326 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
1327
1328 const int width = GetWidth();
1329 const int pixsoff = rect.GetLeft() + width * rect.GetTop();
1330
1331 src_data += 3 * pixsoff;
1332 src_alpha += pixsoff; // won't be used if was NULL, so this is ok
1333
1334 for (long j = 0; j < subheight; ++j)
1335 {
1336 memcpy( subdata, src_data, 3 * subwidth );
1337 subdata += 3 * subwidth;
1338 src_data += 3 * width;
1339 if (subalpha != NULL) {
1340 memcpy( subalpha, src_alpha, subwidth );
1341 subalpha += subwidth;
1342 src_alpha += width;
1343 }
1344 }
1345
1346 return image;
1347 }
1348
1349 wxImage wxImage::Size( const wxSize& size, const wxPoint& pos,
1350 int r_, int g_, int b_ ) const
1351 {
1352 wxImage image;
1353
1354 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
1355 wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") );
1356
1357 int width = GetWidth(), height = GetHeight();
1358 image.Create(size.GetWidth(), size.GetHeight(), false);
1359
1360 unsigned char r = (unsigned char)r_;
1361 unsigned char g = (unsigned char)g_;
1362 unsigned char b = (unsigned char)b_;
1363 if ((r_ == -1) && (g_ == -1) && (b_ == -1))
1364 {
1365 GetOrFindMaskColour( &r, &g, &b );
1366 image.SetMaskColour(r, g, b);
1367 }
1368
1369 image.SetRGB(wxRect(), r, g, b);
1370
1371 // we have two coordinate systems:
1372 // source: starting at 0,0 of source image
1373 // destination starting at 0,0 of destination image
1374 // Documentation says:
1375 // "The image is pasted into a new image [...] at the position pos relative
1376 // to the upper left of the new image." this means the transition rule is:
1377 // "dest coord" = "source coord" + pos;
1378
1379 // calculate the intersection using source coordinates:
1380 wxRect srcRect(0, 0, width, height);
1381 wxRect dstRect(-pos, size);
1382
1383 srcRect.Intersect(dstRect);
1384
1385 if (!srcRect.IsEmpty())
1386 {
1387 // insertion point is needed in destination coordinates.
1388 // NB: it is not always "pos"!
1389 wxPoint ptInsert = srcRect.GetTopLeft() + pos;
1390
1391 if ((srcRect.GetWidth() == width) && (srcRect.GetHeight() == height))
1392 image.Paste(*this, ptInsert.x, ptInsert.y);
1393 else
1394 image.Paste(GetSubImage(srcRect), ptInsert.x, ptInsert.y);
1395 }
1396
1397 return image;
1398 }
1399
1400 void wxImage::Paste( const wxImage &image, int x, int y )
1401 {
1402 wxCHECK_RET( IsOk(), wxT("invalid image") );
1403 wxCHECK_RET( image.IsOk(), wxT("invalid image") );
1404
1405 AllocExclusive();
1406
1407 int xx = 0;
1408 int yy = 0;
1409 int width = image.GetWidth();
1410 int height = image.GetHeight();
1411
1412 if (x < 0)
1413 {
1414 xx = -x;
1415 width += x;
1416 }
1417 if (y < 0)
1418 {
1419 yy = -y;
1420 height += y;
1421 }
1422
1423 if ((x+xx)+width > M_IMGDATA->m_width)
1424 width = M_IMGDATA->m_width - (x+xx);
1425 if ((y+yy)+height > M_IMGDATA->m_height)
1426 height = M_IMGDATA->m_height - (y+yy);
1427
1428 if (width < 1) return;
1429 if (height < 1) return;
1430
1431 // If we can, copy the data using memcpy() as this is the fastest way. But
1432 // for this the image being pasted must have "compatible" mask with this
1433 // one meaning that either it must not have one at all or it must use the
1434 // same masked colour.
1435 if ( !image.HasMask() ||
1436 ((HasMask() &&
1437 (GetMaskRed()==image.GetMaskRed()) &&
1438 (GetMaskGreen()==image.GetMaskGreen()) &&
1439 (GetMaskBlue()==image.GetMaskBlue()))) )
1440 {
1441 const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth());
1442 int source_step = image.GetWidth()*3;
1443
1444 unsigned char* target_data = GetData() + 3*((x+xx) + (y+yy)*M_IMGDATA->m_width);
1445 int target_step = M_IMGDATA->m_width*3;
1446 for (int j = 0; j < height; j++)
1447 {
1448 memcpy( target_data, source_data, width*3 );
1449 source_data += source_step;
1450 target_data += target_step;
1451 }
1452 }
1453
1454 // Copy over the alpha channel from the original image
1455 if ( image.HasAlpha() )
1456 {
1457 if ( !HasAlpha() )
1458 InitAlpha();
1459
1460 const unsigned char* source_data = image.GetAlpha() + xx + yy*image.GetWidth();
1461 int source_step = image.GetWidth();
1462
1463 unsigned char* target_data = GetAlpha() + (x+xx) + (y+yy)*M_IMGDATA->m_width;
1464 int target_step = M_IMGDATA->m_width;
1465
1466 for (int j = 0; j < height; j++,
1467 source_data += source_step,
1468 target_data += target_step)
1469 {
1470 memcpy( target_data, source_data, width );
1471 }
1472 }
1473
1474 if (!HasMask() && image.HasMask())
1475 {
1476 unsigned char r = image.GetMaskRed();
1477 unsigned char g = image.GetMaskGreen();
1478 unsigned char b = image.GetMaskBlue();
1479
1480 const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth());
1481 int source_step = image.GetWidth()*3;
1482
1483 unsigned char* target_data = GetData() + 3*((x+xx) + (y+yy)*M_IMGDATA->m_width);
1484 int target_step = M_IMGDATA->m_width*3;
1485
1486 for (int j = 0; j < height; j++)
1487 {
1488 for (int i = 0; i < width*3; i+=3)
1489 {
1490 if ((source_data[i] != r) ||
1491 (source_data[i+1] != g) ||
1492 (source_data[i+2] != b))
1493 {
1494 memcpy( target_data+i, source_data+i, 3 );
1495 }
1496 }
1497 source_data += source_step;
1498 target_data += target_step;
1499 }
1500 }
1501 }
1502
1503 void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1,
1504 unsigned char r2, unsigned char g2, unsigned char b2 )
1505 {
1506 wxCHECK_RET( IsOk(), wxT("invalid image") );
1507
1508 AllocExclusive();
1509
1510 unsigned char *data = GetData();
1511
1512 const int w = GetWidth();
1513 const int h = GetHeight();
1514
1515 for (int j = 0; j < h; j++)
1516 for (int i = 0; i < w; i++)
1517 {
1518 if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1))
1519 {
1520 data[0] = r2;
1521 data[1] = g2;
1522 data[2] = b2;
1523 }
1524 data += 3;
1525 }
1526 }
1527
1528 wxImage wxImage::ConvertToGreyscale(void) const
1529 {
1530 return ConvertToGreyscale(0.299, 0.587, 0.114);
1531 }
1532
1533 wxImage wxImage::ConvertToGreyscale(double weight_r, double weight_g, double weight_b) const
1534 {
1535 wxImage image(MakeEmptyClone());
1536
1537 wxCHECK( image.IsOk(), image );
1538
1539 const unsigned char *src = M_IMGDATA->m_data;
1540 unsigned char *dest = image.GetData();
1541
1542 const bool hasMask = M_IMGDATA->m_hasMask;
1543 const unsigned char maskRed = M_IMGDATA->m_maskRed;
1544 const unsigned char maskGreen = M_IMGDATA->m_maskGreen;
1545 const unsigned char maskBlue = M_IMGDATA->m_maskBlue;
1546
1547 const long size = M_IMGDATA->m_width * M_IMGDATA->m_height;
1548 for ( long i = 0; i < size; i++, src += 3, dest += 3 )
1549 {
1550 memcpy(dest, src, 3);
1551 // only modify non-masked pixels
1552 if ( !hasMask || src[0] != maskRed || src[1] != maskGreen || src[2] != maskBlue )
1553 {
1554 wxColour::MakeGrey(dest + 0, dest + 1, dest + 2, weight_r, weight_g, weight_b);
1555 }
1556 }
1557
1558 // copy the alpha channel, if any
1559 if ( image.HasAlpha() )
1560 {
1561 memcpy( image.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1562 }
1563
1564 return image;
1565 }
1566
1567 wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const
1568 {
1569 wxImage image;
1570
1571 wxCHECK_MSG( IsOk(), image, wxT("invalid image") );
1572
1573 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
1574
1575 unsigned char *data = image.GetData();
1576
1577 wxCHECK_MSG( data, image, wxT("unable to create image") );
1578
1579 if (M_IMGDATA->m_hasMask)
1580 {
1581 if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g &&
1582 M_IMGDATA->m_maskBlue == b)
1583 image.SetMaskColour( 255, 255, 255 );
1584 else
1585 image.SetMaskColour( 0, 0, 0 );
1586 }
1587
1588 long size = M_IMGDATA->m_height * M_IMGDATA->m_width;
1589
1590 unsigned char *srcd = M_IMGDATA->m_data;
1591 unsigned char *tard = image.GetData();
1592
1593 for ( long i = 0; i < size; i++, srcd += 3, tard += 3 )
1594 {
1595 bool on = (srcd[0] == r) && (srcd[1] == g) && (srcd[2] == b);
1596 wxColourBase::MakeMono(tard + 0, tard + 1, tard + 2, on);
1597 }
1598
1599 return image;
1600 }
1601
1602 wxImage wxImage::ConvertToDisabled(unsigned char brightness) const
1603 {
1604 wxImage image = *this;
1605
1606 unsigned char mr = image.GetMaskRed();
1607 unsigned char mg = image.GetMaskGreen();
1608 unsigned char mb = image.GetMaskBlue();
1609
1610 int width = image.GetWidth();
1611 int height = image.GetHeight();
1612 bool has_mask = image.HasMask();
1613
1614 for (int y = height-1; y >= 0; --y)
1615 {
1616 for (int x = width-1; x >= 0; --x)
1617 {
1618 unsigned char* data = image.GetData() + (y*(width*3))+(x*3);
1619 unsigned char* r = data;
1620 unsigned char* g = data+1;
1621 unsigned char* b = data+2;
1622
1623 if (has_mask && (*r == mr) && (*g == mg) && (*b == mb))
1624 continue;
1625
1626 wxColour::MakeDisabled(r, g, b, brightness);
1627 }
1628 }
1629 return image;
1630 }
1631
1632 int wxImage::GetWidth() const
1633 {
1634 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1635
1636 return M_IMGDATA->m_width;
1637 }
1638
1639 int wxImage::GetHeight() const
1640 {
1641 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1642
1643 return M_IMGDATA->m_height;
1644 }
1645
1646 wxBitmapType wxImage::GetType() const
1647 {
1648 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID, wxT("invalid image") );
1649
1650 return M_IMGDATA->m_type;
1651 }
1652
1653 void wxImage::SetType(wxBitmapType type)
1654 {
1655 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1656
1657 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1658 wxASSERT_MSG( type != wxBITMAP_TYPE_MAX, "invalid bitmap type" );
1659
1660 M_IMGDATA->m_type = type;
1661 }
1662
1663 long wxImage::XYToIndex(int x, int y) const
1664 {
1665 if ( IsOk() &&
1666 x >= 0 && y >= 0 &&
1667 x < M_IMGDATA->m_width && y < M_IMGDATA->m_height )
1668 {
1669 return y*M_IMGDATA->m_width + x;
1670 }
1671
1672 return -1;
1673 }
1674
1675 void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
1676 {
1677 long pos = XYToIndex(x, y);
1678 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
1679
1680 AllocExclusive();
1681
1682 pos *= 3;
1683
1684 M_IMGDATA->m_data[ pos ] = r;
1685 M_IMGDATA->m_data[ pos+1 ] = g;
1686 M_IMGDATA->m_data[ pos+2 ] = b;
1687 }
1688
1689 void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b )
1690 {
1691 wxCHECK_RET( IsOk(), wxT("invalid image") );
1692
1693 AllocExclusive();
1694
1695 wxRect rect(rect_);
1696 wxRect imageRect(0, 0, GetWidth(), GetHeight());
1697 if ( rect == wxRect() )
1698 {
1699 rect = imageRect;
1700 }
1701 else
1702 {
1703 wxCHECK_RET( imageRect.Contains(rect.GetTopLeft()) &&
1704 imageRect.Contains(rect.GetBottomRight()),
1705 wxT("invalid bounding rectangle") );
1706 }
1707
1708 int x1 = rect.GetLeft(),
1709 y1 = rect.GetTop(),
1710 x2 = rect.GetRight() + 1,
1711 y2 = rect.GetBottom() + 1;
1712
1713 unsigned char *data wxDUMMY_INITIALIZE(NULL);
1714 int x, y, width = GetWidth();
1715 for (y = y1; y < y2; y++)
1716 {
1717 data = M_IMGDATA->m_data + (y*width + x1)*3;
1718 for (x = x1; x < x2; x++)
1719 {
1720 *data++ = r;
1721 *data++ = g;
1722 *data++ = b;
1723 }
1724 }
1725 }
1726
1727 unsigned char wxImage::GetRed( int x, int y ) const
1728 {
1729 long pos = XYToIndex(x, y);
1730 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1731
1732 pos *= 3;
1733
1734 return M_IMGDATA->m_data[pos];
1735 }
1736
1737 unsigned char wxImage::GetGreen( int x, int y ) const
1738 {
1739 long pos = XYToIndex(x, y);
1740 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1741
1742 pos *= 3;
1743
1744 return M_IMGDATA->m_data[pos+1];
1745 }
1746
1747 unsigned char wxImage::GetBlue( int x, int y ) const
1748 {
1749 long pos = XYToIndex(x, y);
1750 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1751
1752 pos *= 3;
1753
1754 return M_IMGDATA->m_data[pos+2];
1755 }
1756
1757 bool wxImage::IsOk() const
1758 {
1759 // image of 0 width or height can't be considered ok - at least because it
1760 // causes crashes in ConvertToBitmap() if we don't catch it in time
1761 wxImageRefData *data = M_IMGDATA;
1762 return data && data->m_ok && data->m_width && data->m_height;
1763 }
1764
1765 unsigned char *wxImage::GetData() const
1766 {
1767 wxCHECK_MSG( IsOk(), (unsigned char *)NULL, wxT("invalid image") );
1768
1769 return M_IMGDATA->m_data;
1770 }
1771
1772 void wxImage::SetData( unsigned char *data, bool static_data )
1773 {
1774 wxCHECK_RET( IsOk(), wxT("invalid image") );
1775
1776 wxImageRefData *newRefData = new wxImageRefData();
1777
1778 newRefData->m_width = M_IMGDATA->m_width;
1779 newRefData->m_height = M_IMGDATA->m_height;
1780 newRefData->m_data = data;
1781 newRefData->m_ok = true;
1782 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1783 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1784 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1785 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
1786 newRefData->m_static = static_data;
1787
1788 UnRef();
1789
1790 m_refData = newRefData;
1791 }
1792
1793 void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data )
1794 {
1795 wxImageRefData *newRefData = new wxImageRefData();
1796
1797 if (m_refData)
1798 {
1799 newRefData->m_width = new_width;
1800 newRefData->m_height = new_height;
1801 newRefData->m_data = data;
1802 newRefData->m_ok = true;
1803 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1804 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1805 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1806 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
1807 }
1808 else
1809 {
1810 newRefData->m_width = new_width;
1811 newRefData->m_height = new_height;
1812 newRefData->m_data = data;
1813 newRefData->m_ok = true;
1814 }
1815 newRefData->m_static = static_data;
1816
1817 UnRef();
1818
1819 m_refData = newRefData;
1820 }
1821
1822 // ----------------------------------------------------------------------------
1823 // alpha channel support
1824 // ----------------------------------------------------------------------------
1825
1826 void wxImage::SetAlpha(int x, int y, unsigned char alpha)
1827 {
1828 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1829
1830 long pos = XYToIndex(x, y);
1831 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
1832
1833 AllocExclusive();
1834
1835 M_IMGDATA->m_alpha[pos] = alpha;
1836 }
1837
1838 unsigned char wxImage::GetAlpha(int x, int y) const
1839 {
1840 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1841
1842 long pos = XYToIndex(x, y);
1843 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
1844
1845 return M_IMGDATA->m_alpha[pos];
1846 }
1847
1848 bool
1849 wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b)
1850 {
1851 SetAlpha(NULL);
1852
1853 const int w = M_IMGDATA->m_width;
1854 const int h = M_IMGDATA->m_height;
1855
1856 unsigned char *alpha = GetAlpha();
1857 unsigned char *data = GetData();
1858
1859 for ( int y = 0; y < h; y++ )
1860 {
1861 for ( int x = 0; x < w; x++ )
1862 {
1863 *alpha++ = *data;
1864 *data++ = r;
1865 *data++ = g;
1866 *data++ = b;
1867 }
1868 }
1869
1870 return true;
1871 }
1872
1873 void wxImage::SetAlpha( unsigned char *alpha, bool static_data )
1874 {
1875 wxCHECK_RET( IsOk(), wxT("invalid image") );
1876
1877 AllocExclusive();
1878
1879 if ( !alpha )
1880 {
1881 alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
1882 }
1883
1884 if( !M_IMGDATA->m_staticAlpha )
1885 free(M_IMGDATA->m_alpha);
1886
1887 M_IMGDATA->m_alpha = alpha;
1888 M_IMGDATA->m_staticAlpha = static_data;
1889 }
1890
1891 unsigned char *wxImage::GetAlpha() const
1892 {
1893 wxCHECK_MSG( IsOk(), (unsigned char *)NULL, wxT("invalid image") );
1894
1895 return M_IMGDATA->m_alpha;
1896 }
1897
1898 void wxImage::InitAlpha()
1899 {
1900 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1901
1902 // initialize memory for alpha channel
1903 SetAlpha();
1904
1905 unsigned char *alpha = M_IMGDATA->m_alpha;
1906 const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
1907
1908 if ( HasMask() )
1909 {
1910 // use the mask to initialize the alpha channel.
1911 const unsigned char * const alphaEnd = alpha + lenAlpha;
1912
1913 const unsigned char mr = M_IMGDATA->m_maskRed;
1914 const unsigned char mg = M_IMGDATA->m_maskGreen;
1915 const unsigned char mb = M_IMGDATA->m_maskBlue;
1916 for ( unsigned char *src = M_IMGDATA->m_data;
1917 alpha < alphaEnd;
1918 src += 3, alpha++ )
1919 {
1920 *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
1921 ? wxIMAGE_ALPHA_TRANSPARENT
1922 : wxIMAGE_ALPHA_OPAQUE;
1923 }
1924
1925 M_IMGDATA->m_hasMask = false;
1926 }
1927 else // no mask
1928 {
1929 // make the image fully opaque
1930 memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha);
1931 }
1932 }
1933
1934 void wxImage::ClearAlpha()
1935 {
1936 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1937
1938 if ( !M_IMGDATA->m_staticAlpha )
1939 free( M_IMGDATA->m_alpha );
1940
1941 M_IMGDATA->m_alpha = NULL;
1942 }
1943
1944
1945 // ----------------------------------------------------------------------------
1946 // mask support
1947 // ----------------------------------------------------------------------------
1948
1949 void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
1950 {
1951 wxCHECK_RET( IsOk(), wxT("invalid image") );
1952
1953 AllocExclusive();
1954
1955 M_IMGDATA->m_maskRed = r;
1956 M_IMGDATA->m_maskGreen = g;
1957 M_IMGDATA->m_maskBlue = b;
1958 M_IMGDATA->m_hasMask = true;
1959 }
1960
1961 bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const
1962 {
1963 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
1964
1965 if (M_IMGDATA->m_hasMask)
1966 {
1967 if (r) *r = M_IMGDATA->m_maskRed;
1968 if (g) *g = M_IMGDATA->m_maskGreen;
1969 if (b) *b = M_IMGDATA->m_maskBlue;
1970 return true;
1971 }
1972 else
1973 {
1974 FindFirstUnusedColour(r, g, b);
1975 return false;
1976 }
1977 }
1978
1979 unsigned char wxImage::GetMaskRed() const
1980 {
1981 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1982
1983 return M_IMGDATA->m_maskRed;
1984 }
1985
1986 unsigned char wxImage::GetMaskGreen() const
1987 {
1988 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1989
1990 return M_IMGDATA->m_maskGreen;
1991 }
1992
1993 unsigned char wxImage::GetMaskBlue() const
1994 {
1995 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1996
1997 return M_IMGDATA->m_maskBlue;
1998 }
1999
2000 void wxImage::SetMask( bool mask )
2001 {
2002 wxCHECK_RET( IsOk(), wxT("invalid image") );
2003
2004 AllocExclusive();
2005
2006 M_IMGDATA->m_hasMask = mask;
2007 }
2008
2009 bool wxImage::HasMask() const
2010 {
2011 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2012
2013 return M_IMGDATA->m_hasMask;
2014 }
2015
2016 bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const
2017 {
2018 long pos = XYToIndex(x, y);
2019 wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") );
2020
2021 // check mask
2022 if ( M_IMGDATA->m_hasMask )
2023 {
2024 const unsigned char *p = M_IMGDATA->m_data + 3*pos;
2025 if ( p[0] == M_IMGDATA->m_maskRed &&
2026 p[1] == M_IMGDATA->m_maskGreen &&
2027 p[2] == M_IMGDATA->m_maskBlue )
2028 {
2029 return true;
2030 }
2031 }
2032
2033 // then check alpha
2034 if ( M_IMGDATA->m_alpha )
2035 {
2036 if ( M_IMGDATA->m_alpha[pos] < threshold )
2037 {
2038 // transparent enough
2039 return true;
2040 }
2041 }
2042
2043 // not transparent
2044 return false;
2045 }
2046
2047 bool wxImage::SetMaskFromImage(const wxImage& mask,
2048 unsigned char mr, unsigned char mg, unsigned char mb)
2049 {
2050 // check that the images are the same size
2051 if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
2052 {
2053 wxLogError( _("Image and mask have different sizes.") );
2054 return false;
2055 }
2056
2057 // find unused colour
2058 unsigned char r,g,b ;
2059 if (!FindFirstUnusedColour(&r, &g, &b))
2060 {
2061 wxLogError( _("No unused colour in image being masked.") );
2062 return false ;
2063 }
2064
2065 AllocExclusive();
2066
2067 unsigned char *imgdata = GetData();
2068 unsigned char *maskdata = mask.GetData();
2069
2070 const int w = GetWidth();
2071 const int h = GetHeight();
2072
2073 for (int j = 0; j < h; j++)
2074 {
2075 for (int i = 0; i < w; i++)
2076 {
2077 if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb))
2078 {
2079 imgdata[0] = r;
2080 imgdata[1] = g;
2081 imgdata[2] = b;
2082 }
2083 imgdata += 3;
2084 maskdata += 3;
2085 }
2086 }
2087
2088 SetMaskColour(r, g, b);
2089 SetMask(true);
2090
2091 return true;
2092 }
2093
2094 bool wxImage::ConvertAlphaToMask(unsigned char threshold)
2095 {
2096 if ( !HasAlpha() )
2097 return false;
2098
2099 unsigned char mr, mg, mb;
2100 if ( !FindFirstUnusedColour(&mr, &mg, &mb) )
2101 {
2102 wxLogError( _("No unused colour in image being masked.") );
2103 return false;
2104 }
2105
2106 return ConvertAlphaToMask(mr, mg, mb, threshold);
2107 }
2108
2109 bool wxImage::ConvertAlphaToMask(unsigned char mr,
2110 unsigned char mg,
2111 unsigned char mb,
2112 unsigned char threshold)
2113 {
2114 if ( !HasAlpha() )
2115 return false;
2116
2117 AllocExclusive();
2118
2119 SetMask(true);
2120 SetMaskColour(mr, mg, mb);
2121
2122 unsigned char *imgdata = GetData();
2123 unsigned char *alphadata = GetAlpha();
2124
2125 int w = GetWidth();
2126 int h = GetHeight();
2127
2128 for (int y = 0; y < h; y++)
2129 {
2130 for (int x = 0; x < w; x++, imgdata += 3, alphadata++)
2131 {
2132 if (*alphadata < threshold)
2133 {
2134 imgdata[0] = mr;
2135 imgdata[1] = mg;
2136 imgdata[2] = mb;
2137 }
2138 }
2139 }
2140
2141 if ( !M_IMGDATA->m_staticAlpha )
2142 free(M_IMGDATA->m_alpha);
2143
2144 M_IMGDATA->m_alpha = NULL;
2145 M_IMGDATA->m_staticAlpha = false;
2146
2147 return true;
2148 }
2149
2150 // ----------------------------------------------------------------------------
2151 // Palette functions
2152 // ----------------------------------------------------------------------------
2153
2154 #if wxUSE_PALETTE
2155
2156 bool wxImage::HasPalette() const
2157 {
2158 if (!IsOk())
2159 return false;
2160
2161 return M_IMGDATA->m_palette.IsOk();
2162 }
2163
2164 const wxPalette& wxImage::GetPalette() const
2165 {
2166 wxCHECK_MSG( IsOk(), wxNullPalette, wxT("invalid image") );
2167
2168 return M_IMGDATA->m_palette;
2169 }
2170
2171 void wxImage::SetPalette(const wxPalette& palette)
2172 {
2173 wxCHECK_RET( IsOk(), wxT("invalid image") );
2174
2175 AllocExclusive();
2176
2177 M_IMGDATA->m_palette = palette;
2178 }
2179
2180 #endif // wxUSE_PALETTE
2181
2182 // ----------------------------------------------------------------------------
2183 // Option functions (arbitrary name/value mapping)
2184 // ----------------------------------------------------------------------------
2185
2186 void wxImage::SetOption(const wxString& name, const wxString& value)
2187 {
2188 AllocExclusive();
2189
2190 int idx = M_IMGDATA->m_optionNames.Index(name, false);
2191 if ( idx == wxNOT_FOUND )
2192 {
2193 M_IMGDATA->m_optionNames.Add(name);
2194 M_IMGDATA->m_optionValues.Add(value);
2195 }
2196 else
2197 {
2198 M_IMGDATA->m_optionNames[idx] = name;
2199 M_IMGDATA->m_optionValues[idx] = value;
2200 }
2201 }
2202
2203 void wxImage::SetOption(const wxString& name, int value)
2204 {
2205 wxString valStr;
2206 valStr.Printf(wxT("%d"), value);
2207 SetOption(name, valStr);
2208 }
2209
2210 wxString wxImage::GetOption(const wxString& name) const
2211 {
2212 if ( !M_IMGDATA )
2213 return wxEmptyString;
2214
2215 int idx = M_IMGDATA->m_optionNames.Index(name, false);
2216 if ( idx == wxNOT_FOUND )
2217 return wxEmptyString;
2218 else
2219 return M_IMGDATA->m_optionValues[idx];
2220 }
2221
2222 int wxImage::GetOptionInt(const wxString& name) const
2223 {
2224 return wxAtoi(GetOption(name));
2225 }
2226
2227 bool wxImage::HasOption(const wxString& name) const
2228 {
2229 return M_IMGDATA ? M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND
2230 : false;
2231 }
2232
2233 // ----------------------------------------------------------------------------
2234 // image I/O
2235 // ----------------------------------------------------------------------------
2236
2237 // Under Windows we can load wxImage not only from files but also from
2238 // resources.
2239 #if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE
2240 #define HAS_LOAD_FROM_RESOURCE
2241 #endif
2242
2243 #ifdef HAS_LOAD_FROM_RESOURCE
2244
2245 #include "wx/msw/dib.h"
2246 #include "wx/msw/private.h"
2247
2248 static wxImage LoadImageFromResource(const wxString &name, wxBitmapType type)
2249 {
2250 AutoHBITMAP
2251 hBitmap,
2252 hMask;
2253
2254 if ( type == wxBITMAP_TYPE_BMP_RESOURCE )
2255 {
2256 hBitmap.Init( ::LoadBitmap(wxGetInstance(), name.t_str()) );
2257
2258 if ( !hBitmap )
2259 {
2260 wxLogError(_("Failed to load bitmap \"%s\" from resources."), name);
2261 }
2262 }
2263 else if ( type == wxBITMAP_TYPE_ICO_RESOURCE )
2264 {
2265 const HICON hIcon = ::LoadIcon(wxGetInstance(), name.t_str());
2266
2267 if ( !hIcon )
2268 {
2269 wxLogError(_("Failed to load icon \"%s\" from resources."), name);
2270 }
2271 else
2272 {
2273 ICONINFO info;
2274 if ( !::GetIconInfo(hIcon, &info) )
2275 {
2276 wxLogLastError(wxT("GetIconInfo"));
2277 return wxImage();
2278 }
2279
2280 hBitmap.Init(info.hbmColor);
2281 hMask.Init(info.hbmMask);
2282 }
2283 }
2284 else if ( type == wxBITMAP_TYPE_CUR_RESOURCE )
2285 {
2286 wxLogDebug(wxS("Loading cursors from resources is not implemented."));
2287 }
2288 else
2289 {
2290 wxFAIL_MSG(wxS("Invalid bitmap resource type."));
2291 }
2292
2293 if ( !hBitmap )
2294 return wxImage();
2295
2296 wxImage image = wxDIB(hBitmap).ConvertToImage();
2297 if ( hMask )
2298 {
2299 const wxImage mask = wxDIB(hMask).ConvertToImage();
2300 image.SetMaskFromImage(mask, 255, 255, 255);
2301 }
2302 else
2303 {
2304 // Light gray colour is a default mask
2305 image.SetMaskColour(0xc0, 0xc0, 0xc0);
2306 }
2307
2308 // We could have already loaded alpha from the resources, but if not,
2309 // initialize it now using the mask.
2310 if ( !image.HasAlpha() )
2311 image.InitAlpha();
2312
2313 return image;
2314 }
2315
2316 #endif // HAS_LOAD_FROM_RESOURCE
2317
2318 bool wxImage::LoadFile( const wxString& filename,
2319 wxBitmapType type,
2320 int WXUNUSED_UNLESS_STREAMS(index) )
2321 {
2322 #ifdef HAS_LOAD_FROM_RESOURCE
2323 if ( type == wxBITMAP_TYPE_BMP_RESOURCE
2324 || type == wxBITMAP_TYPE_ICO_RESOURCE
2325 || type == wxBITMAP_TYPE_CUR_RESOURCE)
2326 {
2327 const wxImage image = ::LoadImageFromResource(filename, type);
2328 if ( image.IsOk() )
2329 {
2330 *this = image;
2331 return true;
2332 }
2333 }
2334 #endif // HAS_LOAD_FROM_RESOURCE
2335
2336 #if HAS_FILE_STREAMS
2337 wxImageFileInputStream stream(filename);
2338 if ( stream.IsOk() )
2339 {
2340 wxBufferedInputStream bstream( stream );
2341 if ( LoadFile(bstream, type, index) )
2342 return true;
2343 }
2344
2345 wxLogError(_("Failed to load image from file \"%s\"."), filename);
2346 #endif // HAS_FILE_STREAMS
2347
2348 return false;
2349 }
2350
2351 bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
2352 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype),
2353 int WXUNUSED_UNLESS_STREAMS(index) )
2354 {
2355 #if HAS_FILE_STREAMS
2356 wxImageFileInputStream stream(filename);
2357 if ( stream.IsOk() )
2358 {
2359 wxBufferedInputStream bstream( stream );
2360 if ( LoadFile(bstream, mimetype, index) )
2361 return true;
2362 }
2363
2364 wxLogError(_("Failed to load image from file \"%s\"."), filename);
2365 #endif // HAS_FILE_STREAMS
2366
2367 return false;
2368 }
2369
2370
2371 bool wxImage::SaveFile( const wxString& filename ) const
2372 {
2373 wxString ext = filename.AfterLast('.').Lower();
2374
2375 wxImageHandler *handler = FindHandler(ext, wxBITMAP_TYPE_ANY);
2376 if ( !handler)
2377 {
2378 wxLogError(_("Can't save image to file '%s': unknown extension."),
2379 filename);
2380 return false;
2381 }
2382
2383 return SaveFile(filename, handler->GetType());
2384 }
2385
2386 bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
2387 wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) const
2388 {
2389 #if HAS_FILE_STREAMS
2390 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2391
2392 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
2393
2394 wxImageFileOutputStream stream(filename);
2395
2396 if ( stream.IsOk() )
2397 {
2398 wxBufferedOutputStream bstream( stream );
2399 return SaveFile(bstream, type);
2400 }
2401 #endif // HAS_FILE_STREAMS
2402
2403 return false;
2404 }
2405
2406 bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
2407 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const
2408 {
2409 #if HAS_FILE_STREAMS
2410 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2411
2412 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
2413
2414 wxImageFileOutputStream stream(filename);
2415
2416 if ( stream.IsOk() )
2417 {
2418 wxBufferedOutputStream bstream( stream );
2419 return SaveFile(bstream, mimetype);
2420 }
2421 #endif // HAS_FILE_STREAMS
2422
2423 return false;
2424 }
2425
2426 bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) )
2427 {
2428 #if HAS_FILE_STREAMS
2429 wxImageFileInputStream stream(name);
2430 return CanRead(stream);
2431 #else
2432 return false;
2433 #endif
2434 }
2435
2436 int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name),
2437 wxBitmapType WXUNUSED_UNLESS_STREAMS(type) )
2438 {
2439 #if HAS_FILE_STREAMS
2440 wxImageFileInputStream stream(name);
2441 if (stream.IsOk())
2442 return GetImageCount(stream, type);
2443 #endif
2444
2445 return 0;
2446 }
2447
2448 #if wxUSE_STREAMS
2449
2450 bool wxImage::CanRead( wxInputStream &stream )
2451 {
2452 const wxList& list = GetHandlers();
2453
2454 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
2455 {
2456 wxImageHandler *handler=(wxImageHandler*)node->GetData();
2457 if (handler->CanRead( stream ))
2458 return true;
2459 }
2460
2461 return false;
2462 }
2463
2464 int wxImage::GetImageCount( wxInputStream &stream, wxBitmapType type )
2465 {
2466 wxImageHandler *handler;
2467
2468 if ( type == wxBITMAP_TYPE_ANY )
2469 {
2470 const wxList& list = GetHandlers();
2471
2472 for ( wxList::compatibility_iterator node = list.GetFirst();
2473 node;
2474 node = node->GetNext() )
2475 {
2476 handler = (wxImageHandler*)node->GetData();
2477 if ( handler->CanRead(stream) )
2478 {
2479 const int count = handler->GetImageCount(stream);
2480 if ( count >= 0 )
2481 return count;
2482 }
2483
2484 }
2485
2486 wxLogWarning(_("No handler found for image type."));
2487 return 0;
2488 }
2489
2490 handler = FindHandler(type);
2491
2492 if ( !handler )
2493 {
2494 wxLogWarning(_("No image handler for type %d defined."), type);
2495 return false;
2496 }
2497
2498 if ( handler->CanRead(stream) )
2499 {
2500 return handler->GetImageCount(stream);
2501 }
2502 else
2503 {
2504 wxLogError(_("Image file is not of type %d."), type);
2505 return 0;
2506 }
2507 }
2508
2509 bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index)
2510 {
2511 // save the options values which can be clobbered by the handler (e.g. many
2512 // of them call Destroy() before trying to load the file)
2513 const unsigned maxWidth = GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH),
2514 maxHeight = GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT);
2515
2516 // Preserve the original stream position if possible to rewind back to it
2517 // if we failed to load the file -- maybe the next handler that we try can
2518 // succeed after us then.
2519 wxFileOffset posOld = wxInvalidOffset;
2520 if ( stream.IsSeekable() )
2521 posOld = stream.TellI();
2522
2523 if ( !handler.LoadFile(this, stream, true/*verbose*/, index) )
2524 {
2525 if ( posOld != wxInvalidOffset )
2526 stream.SeekI(posOld);
2527
2528 return false;
2529 }
2530
2531 // rescale the image to the specified size if needed
2532 if ( maxWidth || maxHeight )
2533 {
2534 const unsigned widthOrig = GetWidth(),
2535 heightOrig = GetHeight();
2536
2537 // this uses the same (trivial) algorithm as the JPEG handler
2538 unsigned width = widthOrig,
2539 height = heightOrig;
2540 while ( (maxWidth && width > maxWidth) ||
2541 (maxHeight && height > maxHeight) )
2542 {
2543 width /= 2;
2544 height /= 2;
2545 }
2546
2547 if ( width != widthOrig || height != heightOrig )
2548 {
2549 // get the original size if it was set by the image handler
2550 // but also in order to restore it after Rescale
2551 int widthOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH),
2552 heightOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT);
2553
2554 Rescale(width, height, wxIMAGE_QUALITY_HIGH);
2555
2556 SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH, widthOrigOption ? widthOrigOption : widthOrig);
2557 SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT, heightOrigOption ? heightOrigOption : heightOrig);
2558 }
2559 }
2560
2561 // Set this after Rescale, which currently does not preserve it
2562 M_IMGDATA->m_type = handler.GetType();
2563
2564 return true;
2565 }
2566
2567 bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index )
2568 {
2569 AllocExclusive();
2570
2571 wxImageHandler *handler;
2572
2573 if ( type == wxBITMAP_TYPE_ANY )
2574 {
2575 if ( !stream.IsSeekable() )
2576 {
2577 // The error message about image data format being unknown below
2578 // would be misleading in this case as we are not even going to try
2579 // any handlers because CanRead() never does anything for not
2580 // seekable stream, so try to be more precise here.
2581 wxLogError(_("Can't automatically determine the image format "
2582 "for non-seekable input."));
2583 return false;
2584 }
2585
2586 const wxList& list = GetHandlers();
2587 for ( wxList::compatibility_iterator node = list.GetFirst();
2588 node;
2589 node = node->GetNext() )
2590 {
2591 handler = (wxImageHandler*)node->GetData();
2592 if ( handler->CanRead(stream) && DoLoad(*handler, stream, index) )
2593 return true;
2594 }
2595
2596 wxLogWarning( _("Unknown image data format.") );
2597
2598 return false;
2599 }
2600 //else: have specific type
2601
2602 handler = FindHandler(type);
2603 if ( !handler )
2604 {
2605 wxLogWarning( _("No image handler for type %d defined."), type );
2606 return false;
2607 }
2608
2609 if ( stream.IsSeekable() && !handler->CanRead(stream) )
2610 {
2611 wxLogError(_("This is not a %s."), handler->GetName());
2612 return false;
2613 }
2614
2615 return DoLoad(*handler, stream, index);
2616 }
2617
2618 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
2619 {
2620 UnRef();
2621
2622 m_refData = new wxImageRefData;
2623
2624 wxImageHandler *handler = FindHandlerMime(mimetype);
2625
2626 if ( !handler )
2627 {
2628 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
2629 return false;
2630 }
2631
2632 if ( stream.IsSeekable() && !handler->CanRead(stream) )
2633 {
2634 wxLogError(_("Image is not of type %s."), mimetype);
2635 return false;
2636 }
2637
2638 return DoLoad(*handler, stream, index);
2639 }
2640
2641 bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const
2642 {
2643 wxImage * const self = const_cast<wxImage *>(this);
2644 if ( !handler.SaveFile(self, stream) )
2645 return false;
2646
2647 M_IMGDATA->m_type = handler.GetType();
2648 return true;
2649 }
2650
2651 bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const
2652 {
2653 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2654
2655 wxImageHandler *handler = FindHandler(type);
2656 if ( !handler )
2657 {
2658 wxLogWarning( _("No image handler for type %d defined."), type );
2659 return false;
2660 }
2661
2662 return DoSave(*handler, stream);
2663 }
2664
2665 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
2666 {
2667 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2668
2669 wxImageHandler *handler = FindHandlerMime(mimetype);
2670 if ( !handler )
2671 {
2672 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
2673 return false;
2674 }
2675
2676 return DoSave(*handler, stream);
2677 }
2678
2679 #endif // wxUSE_STREAMS
2680
2681 // ----------------------------------------------------------------------------
2682 // image I/O handlers
2683 // ----------------------------------------------------------------------------
2684
2685 void wxImage::AddHandler( wxImageHandler *handler )
2686 {
2687 // Check for an existing handler of the type being added.
2688 if (FindHandler( handler->GetType() ) == 0)
2689 {
2690 sm_handlers.Append( handler );
2691 }
2692 else
2693 {
2694 // This is not documented behaviour, merely the simplest 'fix'
2695 // for preventing duplicate additions. If someone ever has
2696 // a good reason to add and remove duplicate handlers (and they
2697 // may) we should probably refcount the duplicates.
2698 // also an issue in InsertHandler below.
2699
2700 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2701 handler->GetName().c_str() );
2702 delete handler;
2703 }
2704 }
2705
2706 void wxImage::InsertHandler( wxImageHandler *handler )
2707 {
2708 // Check for an existing handler of the type being added.
2709 if (FindHandler( handler->GetType() ) == 0)
2710 {
2711 sm_handlers.Insert( handler );
2712 }
2713 else
2714 {
2715 // see AddHandler for additional comments.
2716 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2717 handler->GetName().c_str() );
2718 delete handler;
2719 }
2720 }
2721
2722 bool wxImage::RemoveHandler( const wxString& name )
2723 {
2724 wxImageHandler *handler = FindHandler(name);
2725 if (handler)
2726 {
2727 sm_handlers.DeleteObject(handler);
2728 delete handler;
2729 return true;
2730 }
2731 else
2732 return false;
2733 }
2734
2735 wxImageHandler *wxImage::FindHandler( const wxString& name )
2736 {
2737 wxList::compatibility_iterator node = sm_handlers.GetFirst();
2738 while (node)
2739 {
2740 wxImageHandler *handler = (wxImageHandler*)node->GetData();
2741 if (handler->GetName().Cmp(name) == 0) return handler;
2742
2743 node = node->GetNext();
2744 }
2745 return NULL;
2746 }
2747
2748 wxImageHandler *wxImage::FindHandler( const wxString& extension, wxBitmapType bitmapType )
2749 {
2750 wxList::compatibility_iterator node = sm_handlers.GetFirst();
2751 while (node)
2752 {
2753 wxImageHandler *handler = (wxImageHandler*)node->GetData();
2754 if ((bitmapType == wxBITMAP_TYPE_ANY) || (handler->GetType() == bitmapType))
2755 {
2756 if (handler->GetExtension() == extension)
2757 return handler;
2758 if (handler->GetAltExtensions().Index(extension, false) != wxNOT_FOUND)
2759 return handler;
2760 }
2761 node = node->GetNext();
2762 }
2763 return NULL;
2764 }
2765
2766 wxImageHandler *wxImage::FindHandler(wxBitmapType bitmapType )
2767 {
2768 wxList::compatibility_iterator node = sm_handlers.GetFirst();
2769 while (node)
2770 {
2771 wxImageHandler *handler = (wxImageHandler *)node->GetData();
2772 if (handler->GetType() == bitmapType) return handler;
2773 node = node->GetNext();
2774 }
2775 return NULL;
2776 }
2777
2778 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
2779 {
2780 wxList::compatibility_iterator node = sm_handlers.GetFirst();
2781 while (node)
2782 {
2783 wxImageHandler *handler = (wxImageHandler *)node->GetData();
2784 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
2785 node = node->GetNext();
2786 }
2787 return NULL;
2788 }
2789
2790 void wxImage::InitStandardHandlers()
2791 {
2792 #if wxUSE_STREAMS
2793 AddHandler(new wxBMPHandler);
2794 #endif // wxUSE_STREAMS
2795 }
2796
2797 void wxImage::CleanUpHandlers()
2798 {
2799 wxList::compatibility_iterator node = sm_handlers.GetFirst();
2800 while (node)
2801 {
2802 wxImageHandler *handler = (wxImageHandler *)node->GetData();
2803 wxList::compatibility_iterator next = node->GetNext();
2804 delete handler;
2805 node = next;
2806 }
2807
2808 sm_handlers.Clear();
2809 }
2810
2811 wxString wxImage::GetImageExtWildcard()
2812 {
2813 wxString fmts;
2814
2815 wxList& Handlers = wxImage::GetHandlers();
2816 wxList::compatibility_iterator Node = Handlers.GetFirst();
2817 while ( Node )
2818 {
2819 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
2820 fmts += wxT("*.") + Handler->GetExtension();
2821 for (size_t i = 0; i < Handler->GetAltExtensions().size(); i++)
2822 fmts += wxT(";*.") + Handler->GetAltExtensions()[i];
2823 Node = Node->GetNext();
2824 if ( Node ) fmts += wxT(";");
2825 }
2826
2827 return wxT("(") + fmts + wxT(")|") + fmts;
2828 }
2829
2830 wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb)
2831 {
2832 const double red = rgb.red / 255.0,
2833 green = rgb.green / 255.0,
2834 blue = rgb.blue / 255.0;
2835
2836 // find the min and max intensity (and remember which one was it for the
2837 // latter)
2838 double minimumRGB = red;
2839 if ( green < minimumRGB )
2840 minimumRGB = green;
2841 if ( blue < minimumRGB )
2842 minimumRGB = blue;
2843
2844 enum { RED, GREEN, BLUE } chMax = RED;
2845 double maximumRGB = red;
2846 if ( green > maximumRGB )
2847 {
2848 chMax = GREEN;
2849 maximumRGB = green;
2850 }
2851 if ( blue > maximumRGB )
2852 {
2853 chMax = BLUE;
2854 maximumRGB = blue;
2855 }
2856
2857 const double value = maximumRGB;
2858
2859 double hue = 0.0, saturation;
2860 const double deltaRGB = maximumRGB - minimumRGB;
2861 if ( wxIsNullDouble(deltaRGB) )
2862 {
2863 // Gray has no color
2864 hue = 0.0;
2865 saturation = 0.0;
2866 }
2867 else
2868 {
2869 switch ( chMax )
2870 {
2871 case RED:
2872 hue = (green - blue) / deltaRGB;
2873 break;
2874
2875 case GREEN:
2876 hue = 2.0 + (blue - red) / deltaRGB;
2877 break;
2878
2879 case BLUE:
2880 hue = 4.0 + (red - green) / deltaRGB;
2881 break;
2882 }
2883
2884 hue /= 6.0;
2885
2886 if ( hue < 0.0 )
2887 hue += 1.0;
2888
2889 saturation = deltaRGB / maximumRGB;
2890 }
2891
2892 return HSVValue(hue, saturation, value);
2893 }
2894
2895 wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv)
2896 {
2897 double red, green, blue;
2898
2899 if ( wxIsNullDouble(hsv.saturation) )
2900 {
2901 // Grey
2902 red = hsv.value;
2903 green = hsv.value;
2904 blue = hsv.value;
2905 }
2906 else // not grey
2907 {
2908 double hue = hsv.hue * 6.0; // sector 0 to 5
2909 int i = (int)floor(hue);
2910 double f = hue - i; // fractional part of h
2911 double p = hsv.value * (1.0 - hsv.saturation);
2912
2913 switch (i)
2914 {
2915 case 0:
2916 red = hsv.value;
2917 green = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2918 blue = p;
2919 break;
2920
2921 case 1:
2922 red = hsv.value * (1.0 - hsv.saturation * f);
2923 green = hsv.value;
2924 blue = p;
2925 break;
2926
2927 case 2:
2928 red = p;
2929 green = hsv.value;
2930 blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2931 break;
2932
2933 case 3:
2934 red = p;
2935 green = hsv.value * (1.0 - hsv.saturation * f);
2936 blue = hsv.value;
2937 break;
2938
2939 case 4:
2940 red = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2941 green = p;
2942 blue = hsv.value;
2943 break;
2944
2945 default: // case 5:
2946 red = hsv.value;
2947 green = p;
2948 blue = hsv.value * (1.0 - hsv.saturation * f);
2949 break;
2950 }
2951 }
2952
2953 return RGBValue((unsigned char)(red * 255.0),
2954 (unsigned char)(green * 255.0),
2955 (unsigned char)(blue * 255.0));
2956 }
2957
2958 /*
2959 * Rotates the hue of each pixel of the image. angle is a double in the range
2960 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2961 */
2962 void wxImage::RotateHue(double angle)
2963 {
2964 AllocExclusive();
2965
2966 unsigned char *srcBytePtr;
2967 unsigned char *dstBytePtr;
2968 unsigned long count;
2969 wxImage::HSVValue hsv;
2970 wxImage::RGBValue rgb;
2971
2972 wxASSERT (angle >= -1.0 && angle <= 1.0);
2973 count = M_IMGDATA->m_width * M_IMGDATA->m_height;
2974 if ( count > 0 && !wxIsNullDouble(angle) )
2975 {
2976 srcBytePtr = M_IMGDATA->m_data;
2977 dstBytePtr = srcBytePtr;
2978 do
2979 {
2980 rgb.red = *srcBytePtr++;
2981 rgb.green = *srcBytePtr++;
2982 rgb.blue = *srcBytePtr++;
2983 hsv = RGBtoHSV(rgb);
2984
2985 hsv.hue = hsv.hue + angle;
2986 if (hsv.hue > 1.0)
2987 hsv.hue = hsv.hue - 1.0;
2988 else if (hsv.hue < 0.0)
2989 hsv.hue = hsv.hue + 1.0;
2990
2991 rgb = HSVtoRGB(hsv);
2992 *dstBytePtr++ = rgb.red;
2993 *dstBytePtr++ = rgb.green;
2994 *dstBytePtr++ = rgb.blue;
2995 } while (--count != 0);
2996 }
2997 }
2998
2999 //-----------------------------------------------------------------------------
3000 // wxImageHandler
3001 //-----------------------------------------------------------------------------
3002
3003 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
3004
3005 #if wxUSE_STREAMS
3006 int wxImageHandler::GetImageCount( wxInputStream& stream )
3007 {
3008 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3009 // wxImageHandler::CallDoCanRead
3010
3011 if ( !stream.IsSeekable() )
3012 return false; // can't test unseekable stream
3013
3014 wxFileOffset posOld = stream.TellI();
3015 int n = DoGetImageCount(stream);
3016
3017 // restore the old position to be able to test other formats and so on
3018 if ( stream.SeekI(posOld) == wxInvalidOffset )
3019 {
3020 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3021
3022 // reading would fail anyhow as we're not at the right position
3023 return false;
3024 }
3025
3026 return n;
3027 }
3028
3029 bool wxImageHandler::CanRead( const wxString& name )
3030 {
3031 wxImageFileInputStream stream(name);
3032 if ( !stream.IsOk() )
3033 {
3034 wxLogError(_("Failed to check format of image file \"%s\"."), name);
3035
3036 return false;
3037 }
3038
3039 return CanRead(stream);
3040 }
3041
3042 bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
3043 {
3044 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3045 // wxImageHandler::GetImageCount
3046
3047 if ( !stream.IsSeekable() )
3048 return false; // can't test unseekable stream
3049
3050 wxFileOffset posOld = stream.TellI();
3051 bool ok = DoCanRead(stream);
3052
3053 // restore the old position to be able to test other formats and so on
3054 if ( stream.SeekI(posOld) == wxInvalidOffset )
3055 {
3056 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3057
3058 // reading would fail anyhow as we're not at the right position
3059 return false;
3060 }
3061
3062 return ok;
3063 }
3064
3065 #endif // wxUSE_STREAMS
3066
3067 /* static */
3068 wxImageResolution
3069 wxImageHandler::GetResolutionFromOptions(const wxImage& image, int *x, int *y)
3070 {
3071 wxCHECK_MSG( x && y, wxIMAGE_RESOLUTION_NONE, wxT("NULL pointer") );
3072
3073 if ( image.HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
3074 image.HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
3075 {
3076 *x = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
3077 *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
3078 }
3079 else if ( image.HasOption(wxIMAGE_OPTION_RESOLUTION) )
3080 {
3081 *x =
3082 *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
3083 }
3084 else // no resolution options specified
3085 {
3086 *x =
3087 *y = 0;
3088
3089 return wxIMAGE_RESOLUTION_NONE;
3090 }
3091
3092 // get the resolution unit too
3093 int resUnit = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
3094 if ( !resUnit )
3095 {
3096 // this is the default
3097 resUnit = wxIMAGE_RESOLUTION_INCHES;
3098 }
3099
3100 return (wxImageResolution)resUnit;
3101 }
3102
3103 // ----------------------------------------------------------------------------
3104 // image histogram stuff
3105 // ----------------------------------------------------------------------------
3106
3107 bool
3108 wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
3109 unsigned char *g,
3110 unsigned char *b,
3111 unsigned char r2,
3112 unsigned char b2,
3113 unsigned char g2) const
3114 {
3115 unsigned long key = MakeKey(r2, g2, b2);
3116
3117 while ( find(key) != end() )
3118 {
3119 // color already used
3120 r2++;
3121 if ( r2 >= 255 )
3122 {
3123 r2 = 0;
3124 g2++;
3125 if ( g2 >= 255 )
3126 {
3127 g2 = 0;
3128 b2++;
3129 if ( b2 >= 255 )
3130 {
3131 wxLogError(_("No unused colour in image.") );
3132 return false;
3133 }
3134 }
3135 }
3136
3137 key = MakeKey(r2, g2, b2);
3138 }
3139
3140 if ( r )
3141 *r = r2;
3142 if ( g )
3143 *g = g2;
3144 if ( b )
3145 *b = b2;
3146
3147 return true;
3148 }
3149
3150 bool
3151 wxImage::FindFirstUnusedColour(unsigned char *r,
3152 unsigned char *g,
3153 unsigned char *b,
3154 unsigned char r2,
3155 unsigned char b2,
3156 unsigned char g2) const
3157 {
3158 wxImageHistogram histogram;
3159
3160 ComputeHistogram(histogram);
3161
3162 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
3163 }
3164
3165
3166
3167 // GRG, Dic/99
3168 // Counts and returns the number of different colours. Optionally stops
3169 // when it exceeds 'stopafter' different colours. This is useful, for
3170 // example, to see if the image can be saved as 8-bit (256 colour or
3171 // less, in this case it would be invoked as CountColours(256)). Default
3172 // value for stopafter is -1 (don't care).
3173 //
3174 unsigned long wxImage::CountColours( unsigned long stopafter ) const
3175 {
3176 wxHashTable h;
3177 wxObject dummy;
3178 unsigned char r, g, b;
3179 unsigned char *p;
3180 unsigned long size, nentries, key;
3181
3182 p = GetData();
3183 size = GetWidth() * GetHeight();
3184 nentries = 0;
3185
3186 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
3187 {
3188 r = *(p++);
3189 g = *(p++);
3190 b = *(p++);
3191 key = wxImageHistogram::MakeKey(r, g, b);
3192
3193 if (h.Get(key) == NULL)
3194 {
3195 h.Put(key, &dummy);
3196 nentries++;
3197 }
3198 }
3199
3200 return nentries;
3201 }
3202
3203
3204 unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
3205 {
3206 unsigned char *p = GetData();
3207 unsigned long nentries = 0;
3208
3209 h.clear();
3210
3211 const unsigned long size = GetWidth() * GetHeight();
3212
3213 unsigned char r, g, b;
3214 for ( unsigned long n = 0; n < size; n++ )
3215 {
3216 r = *p++;
3217 g = *p++;
3218 b = *p++;
3219
3220 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
3221
3222 if ( entry.value++ == 0 )
3223 entry.index = nentries++;
3224 }
3225
3226 return nentries;
3227 }
3228
3229 /*
3230 * Rotation code by Carlos Moreno
3231 */
3232
3233 static const double wxROTATE_EPSILON = 1e-10;
3234
3235 // Auxiliary function to rotate a point (x,y) with respect to point p0
3236 // make it inline and use a straight return to facilitate optimization
3237 // also, the function receives the sine and cosine of the angle to avoid
3238 // repeating the time-consuming calls to these functions -- sin/cos can
3239 // be computed and stored in the calling function.
3240
3241 static inline wxRealPoint
3242 wxRotatePoint(const wxRealPoint& p, double cos_angle, double sin_angle,
3243 const wxRealPoint& p0)
3244 {
3245 return wxRealPoint(p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
3246 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
3247 }
3248
3249 static inline wxRealPoint
3250 wxRotatePoint(double x, double y, double cos_angle, double sin_angle,
3251 const wxRealPoint & p0)
3252 {
3253 return wxRotatePoint (wxRealPoint(x,y), cos_angle, sin_angle, p0);
3254 }
3255
3256 wxImage wxImage::Rotate(double angle,
3257 const wxPoint& centre_of_rotation,
3258 bool interpolating,
3259 wxPoint *offset_after_rotation) const
3260 {
3261 // screen coordinates are a mirror image of "real" coordinates
3262 angle = -angle;
3263
3264 const bool has_alpha = HasAlpha();
3265
3266 const int w = GetWidth();
3267 const int h = GetHeight();
3268
3269 int i;
3270
3271 // Create pointer-based array to accelerate access to wxImage's data
3272 unsigned char ** data = new unsigned char * [h];
3273 data[0] = GetData();
3274 for (i = 1; i < h; i++)
3275 data[i] = data[i - 1] + (3 * w);
3276
3277 // Same for alpha channel
3278 unsigned char ** alpha = NULL;
3279 if (has_alpha)
3280 {
3281 alpha = new unsigned char * [h];
3282 alpha[0] = GetAlpha();
3283 for (i = 1; i < h; i++)
3284 alpha[i] = alpha[i - 1] + w;
3285 }
3286
3287 // precompute coefficients for rotation formula
3288 const double cos_angle = cos(angle);
3289 const double sin_angle = sin(angle);
3290
3291 // Create new Image to store the result
3292 // First, find rectangle that covers the rotated image; to do that,
3293 // rotate the four corners
3294
3295 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
3296
3297 wxRealPoint p1 = wxRotatePoint (0, 0, cos_angle, sin_angle, p0);
3298 wxRealPoint p2 = wxRotatePoint (0, h, cos_angle, sin_angle, p0);
3299 wxRealPoint p3 = wxRotatePoint (w, 0, cos_angle, sin_angle, p0);
3300 wxRealPoint p4 = wxRotatePoint (w, h, cos_angle, sin_angle, p0);
3301
3302 int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
3303 int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
3304 int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
3305 int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
3306
3307 // Create rotated image
3308 wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false);
3309 // With alpha channel
3310 if (has_alpha)
3311 rotated.SetAlpha();
3312
3313 if (offset_after_rotation != NULL)
3314 {
3315 *offset_after_rotation = wxPoint (x1a, y1a);
3316 }
3317
3318 // the rotated (destination) image is always accessed sequentially via this
3319 // pointer, there is no need for pointer-based arrays here
3320 unsigned char *dst = rotated.GetData();
3321
3322 unsigned char *alpha_dst = has_alpha ? rotated.GetAlpha() : NULL;
3323
3324 // if the original image has a mask, use its RGB values as the blank pixel,
3325 // else, fall back to default (black).
3326 unsigned char blank_r = 0;
3327 unsigned char blank_g = 0;
3328 unsigned char blank_b = 0;
3329
3330 if (HasMask())
3331 {
3332 blank_r = GetMaskRed();
3333 blank_g = GetMaskGreen();
3334 blank_b = GetMaskBlue();
3335 rotated.SetMaskColour( blank_r, blank_g, blank_b );
3336 }
3337
3338 // Now, for each point of the rotated image, find where it came from, by
3339 // performing an inverse rotation (a rotation of -angle) and getting the
3340 // pixel at those coordinates
3341
3342 const int rH = rotated.GetHeight();
3343 const int rW = rotated.GetWidth();
3344
3345 // do the (interpolating) test outside of the loops, so that it is done
3346 // only once, instead of repeating it for each pixel.
3347 if (interpolating)
3348 {
3349 for (int y = 0; y < rH; y++)
3350 {
3351 for (int x = 0; x < rW; x++)
3352 {
3353 wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
3354
3355 if (-0.25 < src.x && src.x < w - 0.75 &&
3356 -0.25 < src.y && src.y < h - 0.75)
3357 {
3358 // interpolate using the 4 enclosing grid-points. Those
3359 // points can be obtained using floor and ceiling of the
3360 // exact coordinates of the point
3361 int x1, y1, x2, y2;
3362
3363 if (0 < src.x && src.x < w - 1)
3364 {
3365 x1 = wxRound(floor(src.x));
3366 x2 = wxRound(ceil(src.x));
3367 }
3368 else // else means that x is near one of the borders (0 or width-1)
3369 {
3370 x1 = x2 = wxRound (src.x);
3371 }
3372
3373 if (0 < src.y && src.y < h - 1)
3374 {
3375 y1 = wxRound(floor(src.y));
3376 y2 = wxRound(ceil(src.y));
3377 }
3378 else
3379 {
3380 y1 = y2 = wxRound (src.y);
3381 }
3382
3383 // get four points and the distances (square of the distance,
3384 // for efficiency reasons) for the interpolation formula
3385
3386 // GRG: Do not calculate the points until they are
3387 // really needed -- this way we can calculate
3388 // just one, instead of four, if d1, d2, d3
3389 // or d4 are < wxROTATE_EPSILON
3390
3391 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
3392 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
3393 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
3394 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
3395
3396 // Now interpolate as a weighted average of the four surrounding
3397 // points, where the weights are the distances to each of those points
3398
3399 // If the point is exactly at one point of the grid of the source
3400 // image, then don't interpolate -- just assign the pixel
3401
3402 // d1,d2,d3,d4 are positive -- no need for abs()
3403 if (d1 < wxROTATE_EPSILON)
3404 {
3405 unsigned char *p = data[y1] + (3 * x1);
3406 *(dst++) = *(p++);
3407 *(dst++) = *(p++);
3408 *(dst++) = *p;
3409
3410 if (has_alpha)
3411 *(alpha_dst++) = *(alpha[y1] + x1);
3412 }
3413 else if (d2 < wxROTATE_EPSILON)
3414 {
3415 unsigned char *p = data[y1] + (3 * x2);
3416 *(dst++) = *(p++);
3417 *(dst++) = *(p++);
3418 *(dst++) = *p;
3419
3420 if (has_alpha)
3421 *(alpha_dst++) = *(alpha[y1] + x2);
3422 }
3423 else if (d3 < wxROTATE_EPSILON)
3424 {
3425 unsigned char *p = data[y2] + (3 * x2);
3426 *(dst++) = *(p++);
3427 *(dst++) = *(p++);
3428 *(dst++) = *p;
3429
3430 if (has_alpha)
3431 *(alpha_dst++) = *(alpha[y2] + x2);
3432 }
3433 else if (d4 < wxROTATE_EPSILON)
3434 {
3435 unsigned char *p = data[y2] + (3 * x1);
3436 *(dst++) = *(p++);
3437 *(dst++) = *(p++);
3438 *(dst++) = *p;
3439
3440 if (has_alpha)
3441 *(alpha_dst++) = *(alpha[y2] + x1);
3442 }
3443 else
3444 {
3445 // weights for the weighted average are proportional to the inverse of the distance
3446 unsigned char *v1 = data[y1] + (3 * x1);
3447 unsigned char *v2 = data[y1] + (3 * x2);
3448 unsigned char *v3 = data[y2] + (3 * x2);
3449 unsigned char *v4 = data[y2] + (3 * x1);
3450
3451 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
3452
3453 // GRG: Unrolled.
3454
3455 *(dst++) = (unsigned char)
3456 ( (w1 * *(v1++) + w2 * *(v2++) +
3457 w3 * *(v3++) + w4 * *(v4++)) /
3458 (w1 + w2 + w3 + w4) );
3459 *(dst++) = (unsigned char)
3460 ( (w1 * *(v1++) + w2 * *(v2++) +
3461 w3 * *(v3++) + w4 * *(v4++)) /
3462 (w1 + w2 + w3 + w4) );
3463 *(dst++) = (unsigned char)
3464 ( (w1 * *v1 + w2 * *v2 +
3465 w3 * *v3 + w4 * *v4) /
3466 (w1 + w2 + w3 + w4) );
3467
3468 if (has_alpha)
3469 {
3470 v1 = alpha[y1] + (x1);
3471 v2 = alpha[y1] + (x2);
3472 v3 = alpha[y2] + (x2);
3473 v4 = alpha[y2] + (x1);
3474
3475 *(alpha_dst++) = (unsigned char)
3476 ( (w1 * *v1 + w2 * *v2 +
3477 w3 * *v3 + w4 * *v4) /
3478 (w1 + w2 + w3 + w4) );
3479 }
3480 }
3481 }
3482 else
3483 {
3484 *(dst++) = blank_r;
3485 *(dst++) = blank_g;
3486 *(dst++) = blank_b;
3487
3488 if (has_alpha)
3489 *(alpha_dst++) = 0;
3490 }
3491 }
3492 }
3493 }
3494 else // not interpolating
3495 {
3496 for (int y = 0; y < rH; y++)
3497 {
3498 for (int x = 0; x < rW; x++)
3499 {
3500 wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
3501
3502 const int xs = wxRound (src.x); // wxRound rounds to the
3503 const int ys = wxRound (src.y); // closest integer
3504
3505 if (0 <= xs && xs < w && 0 <= ys && ys < h)
3506 {
3507 unsigned char *p = data[ys] + (3 * xs);
3508 *(dst++) = *(p++);
3509 *(dst++) = *(p++);
3510 *(dst++) = *p;
3511
3512 if (has_alpha)
3513 *(alpha_dst++) = *(alpha[ys] + (xs));
3514 }
3515 else
3516 {
3517 *(dst++) = blank_r;
3518 *(dst++) = blank_g;
3519 *(dst++) = blank_b;
3520
3521 if (has_alpha)
3522 *(alpha_dst++) = 255;
3523 }
3524 }
3525 }
3526 }
3527
3528 delete [] data;
3529 delete [] alpha;
3530
3531 return rotated;
3532 }
3533
3534
3535
3536
3537
3538 // A module to allow wxImage initialization/cleanup
3539 // without calling these functions from app.cpp or from
3540 // the user's application.
3541
3542 class wxImageModule: public wxModule
3543 {
3544 DECLARE_DYNAMIC_CLASS(wxImageModule)
3545 public:
3546 wxImageModule() {}
3547 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3548 void OnExit() { wxImage::CleanUpHandlers(); }
3549 };
3550
3551 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
3552
3553
3554 #endif // wxUSE_IMAGE