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