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