1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/palette.h"
29 #include "wx/colour.h"
32 #include "wx/filefn.h"
33 #include "wx/wfstream.h"
34 #include "wx/xpmdecod.h"
39 // make the code compile with either wxFile*Stream or wxFFile*Stream:
40 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
44 typedef wxFFileInputStream wxImageFileInputStream
;
45 typedef wxFFileOutputStream wxImageFileOutputStream
;
47 typedef wxFileInputStream wxImageFileInputStream
;
48 typedef wxFileOutputStream wxImageFileOutputStream
;
49 #endif // wxUSE_FILE/wxUSE_FFILE
50 #endif // HAS_FILE_STREAMS
53 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 wxList
wxImage::sm_handlers
;
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 class wxImageRefData
: public wxObjectRefData
71 virtual ~wxImageRefData();
76 unsigned char *m_data
;
79 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
81 // alpha channel data, may be NULL for the formats without alpha support
82 unsigned char *m_alpha
;
86 // if true, m_data is pointer to static data and shouldn't be freed
89 // same as m_static but for m_alpha
94 #endif // wxUSE_PALETTE
96 wxArrayString m_optionNames
;
97 wxArrayString m_optionValues
;
99 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
102 wxImageRefData::wxImageRefData()
106 m_type
= wxBITMAP_TYPE_INVALID
;
108 m_alpha
= (unsigned char *) NULL
;
117 m_staticAlpha
= false;
120 wxImageRefData::~wxImageRefData()
124 if ( !m_staticAlpha
)
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
135 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
137 bool wxImage::Create(const char* const* xpmData
)
142 wxXPMDecoder decoder
;
143 (*this) = decoder
.ReadData(xpmData
);
150 bool wxImage::Create( int width
, int height
, bool clear
)
154 m_refData
= new wxImageRefData();
156 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
157 if (!M_IMGDATA
->m_data
)
163 M_IMGDATA
->m_width
= width
;
164 M_IMGDATA
->m_height
= height
;
165 M_IMGDATA
->m_ok
= true;
175 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
179 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
181 m_refData
= new wxImageRefData();
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
;
192 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
196 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
198 m_refData
= new wxImageRefData();
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
;
211 void wxImage::Destroy()
216 void wxImage::Clear(unsigned char value
)
218 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
221 wxObjectRefData
* wxImage::CreateRefData() const
223 return new wxImageRefData
;
226 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
228 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
229 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
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
)
242 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
243 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
246 refData_new
->m_data
= (unsigned char*)malloc(size
);
247 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
249 refData_new
->m_palette
= refData
->m_palette
;
251 refData_new
->m_optionNames
= refData
->m_optionNames
;
252 refData_new
->m_optionValues
= refData
->m_optionValues
;
256 wxImage
wxImage::Copy() const
260 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
262 image
.m_refData
= CloneRefData(m_refData
);
267 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
269 if( xFactor
== 1 && yFactor
== 1 )
274 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
276 // can't scale to/from 0 size
277 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
278 wxT("invalid new image size") );
280 long old_height
= M_IMGDATA
->m_height
,
281 old_width
= M_IMGDATA
->m_width
;
283 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
284 wxT("invalid old image size") );
286 long width
= old_width
/ xFactor
;
287 long height
= old_height
/ yFactor
;
289 image
.Create( width
, height
, false );
291 char unsigned *data
= image
.GetData();
293 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
295 bool hasMask
= false ;
296 unsigned char maskRed
= 0;
297 unsigned char maskGreen
= 0;
298 unsigned char maskBlue
=0 ;
300 unsigned char *source_data
= M_IMGDATA
->m_data
;
301 unsigned char *target_data
= data
;
302 unsigned char *source_alpha
= 0 ;
303 unsigned char *target_alpha
= 0 ;
304 if (M_IMGDATA
->m_hasMask
)
307 maskRed
= M_IMGDATA
->m_maskRed
;
308 maskGreen
= M_IMGDATA
->m_maskGreen
;
309 maskBlue
=M_IMGDATA
->m_maskBlue
;
311 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
312 M_IMGDATA
->m_maskGreen
,
313 M_IMGDATA
->m_maskBlue
);
317 source_alpha
= M_IMGDATA
->m_alpha
;
321 target_alpha
= image
.GetAlpha() ;
325 for (long y
= 0; y
< height
; y
++)
327 for (long x
= 0; x
< width
; x
++)
329 unsigned long avgRed
= 0 ;
330 unsigned long avgGreen
= 0;
331 unsigned long avgBlue
= 0;
332 unsigned long avgAlpha
= 0 ;
333 unsigned long counter
= 0 ;
335 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
337 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
338 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
340 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
341 unsigned char red
= pixel
[0] ;
342 unsigned char green
= pixel
[1] ;
343 unsigned char blue
= pixel
[2] ;
344 unsigned char alpha
= 255 ;
346 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
347 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
362 *(target_data
++) = M_IMGDATA
->m_maskRed
;
363 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
364 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
369 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
370 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
371 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
372 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
377 // In case this is a cursor, make sure the hotspot is scaled accordingly:
378 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
379 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
380 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
381 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
382 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
383 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
389 wxImage::Scale( int width
, int height
, wxImageResizeQuality quality
) const
393 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
395 // can't scale to/from 0 size
396 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
397 wxT("invalid new image size") );
399 long old_height
= M_IMGDATA
->m_height
,
400 old_width
= M_IMGDATA
->m_width
;
401 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
402 wxT("invalid old image size") );
404 // If the image's new width and height are the same as the original, no
405 // need to waste time or CPU cycles
406 if ( old_width
== width
&& old_height
== height
)
409 // resample the image using either the nearest neighbourhood, bilinear or
410 // bicubic method as specified
413 case wxIMAGE_QUALITY_BICUBIC
:
414 case wxIMAGE_QUALITY_BILINEAR
:
415 // both of these algorithms should be used for up-sampling the
416 // image only, when down-sampling always use box averaging for best
418 if ( width
< old_width
&& height
< old_height
)
419 image
= ResampleBox(width
, height
);
420 else if ( quality
== wxIMAGE_QUALITY_BILINEAR
)
421 image
= ResampleBilinear(width
, height
);
422 else if ( quality
== wxIMAGE_QUALITY_BICUBIC
)
423 image
= ResampleBicubic(width
, height
);
426 case wxIMAGE_QUALITY_NEAREST
:
427 if ( old_width
% width
== 0 && old_width
>= width
&&
428 old_height
% height
== 0 && old_height
>= height
)
430 return ShrinkBy( old_width
/ width
, old_height
/ height
);
433 image
= ResampleNearest(width
, height
);
437 // If the original image has a mask, apply the mask to the new image
438 if (M_IMGDATA
->m_hasMask
)
440 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
441 M_IMGDATA
->m_maskGreen
,
442 M_IMGDATA
->m_maskBlue
);
445 // In case this is a cursor, make sure the hotspot is scaled accordingly:
446 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
447 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
448 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
449 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
450 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
451 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
456 wxImage
wxImage::ResampleNearest(int width
, int height
) const
459 image
.Create( width
, height
, false );
461 unsigned char *data
= image
.GetData();
463 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
465 unsigned char *source_data
= M_IMGDATA
->m_data
;
466 unsigned char *target_data
= data
;
467 unsigned char *source_alpha
= 0 ;
468 unsigned char *target_alpha
= 0 ;
470 if ( !M_IMGDATA
->m_hasMask
)
472 source_alpha
= M_IMGDATA
->m_alpha
;
476 target_alpha
= image
.GetAlpha() ;
480 long old_height
= M_IMGDATA
->m_height
,
481 old_width
= M_IMGDATA
->m_width
;
482 long x_delta
= (old_width
<<16) / width
;
483 long y_delta
= (old_height
<<16) / height
;
485 unsigned char* dest_pixel
= target_data
;
488 for ( long j
= 0; j
< height
; j
++ )
490 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
491 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
494 for ( long i
= 0; i
< width
; i
++ )
496 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
497 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
498 dest_pixel
[0] = src_pixel
[0];
499 dest_pixel
[1] = src_pixel
[1];
500 dest_pixel
[2] = src_pixel
[2];
503 *(target_alpha
++) = *src_alpha_pixel
;
513 wxImage
wxImage::ResampleBox(int width
, int height
) const
515 // This function implements a simple pre-blur/box averaging method for
516 // downsampling that gives reasonably smooth results To scale the image
517 // down we will need to gather a grid of pixels of the size of the scale
518 // factor in each direction and then do an averaging of the pixels.
520 wxImage
ret_image(width
, height
, false);
522 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
523 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
525 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
526 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
528 unsigned char* src_data
= M_IMGDATA
->m_data
;
529 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
530 unsigned char* dst_data
= ret_image
.GetData();
531 unsigned char* dst_alpha
= NULL
;
535 ret_image
.SetAlpha();
536 dst_alpha
= ret_image
.GetAlpha();
539 int averaged_pixels
, src_pixel_index
;
540 double sum_r
, sum_g
, sum_b
, sum_a
;
542 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
544 // Source pixel in the Y direction
545 int src_y
= (int)(y
* scale_factor_y
);
547 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
549 // Source pixel in the X direction
550 int src_x
= (int)(x
* scale_factor_x
);
552 // Box of pixels to average
554 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
556 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
557 j
<= int(src_y
+ scale_factor_y_2
);
560 // We don't care to average pixels that don't exist (edges)
561 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
564 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
565 i
<= src_x
+ scale_factor_x_2
;
568 // Don't average edge pixels
569 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
572 // Calculate the actual index in our source pixels
573 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
575 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
576 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
577 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
579 sum_a
+= src_alpha
[src_pixel_index
];
585 // Calculate the average from the sum and number of averaged pixels
586 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
587 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
588 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
591 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
598 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
600 // This function implements a Bilinear algorithm for resampling.
601 wxImage
ret_image(width
, height
, false);
602 unsigned char* src_data
= M_IMGDATA
->m_data
;
603 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
604 unsigned char* dst_data
= ret_image
.GetData();
605 unsigned char* dst_alpha
= NULL
;
609 ret_image
.SetAlpha();
610 dst_alpha
= ret_image
.GetAlpha();
612 double HFactor
= double(M_IMGDATA
->m_height
) / height
;
613 double WFactor
= double(M_IMGDATA
->m_width
) / width
;
615 int srcpixymax
= M_IMGDATA
->m_height
- 1;
616 int srcpixxmax
= M_IMGDATA
->m_width
- 1;
618 double srcpixy
, srcpixy1
, srcpixy2
, dy
, dy1
;
619 double srcpixx
, srcpixx1
, srcpixx2
, dx
, dx1
;
621 // initialize alpha values to avoid g++ warnings about possibly
622 // uninitialized variables
623 double r1
, g1
, b1
, a1
= 0;
624 double r2
, g2
, b2
, a2
= 0;
626 for ( int dsty
= 0; dsty
< height
; dsty
++ )
628 // We need to calculate the source pixel to interpolate from - Y-axis
629 srcpixy
= double(dsty
) * HFactor
;
630 srcpixy1
= int(srcpixy
);
631 srcpixy2
= ( srcpixy1
== srcpixymax
) ? srcpixy1
: srcpixy1
+ 1.0;
632 dy
= srcpixy
- (int)srcpixy
;
636 for ( int dstx
= 0; dstx
< width
; dstx
++ )
638 // X-axis of pixel to interpolate from
639 srcpixx
= double(dstx
) * WFactor
;
640 srcpixx1
= int(srcpixx
);
641 srcpixx2
= ( srcpixx1
== srcpixxmax
) ? srcpixx1
: srcpixx1
+ 1.0;
642 dx
= srcpixx
- (int)srcpixx
;
645 int x_offset1
= srcpixx1
< 0.0 ? 0 : srcpixx1
> srcpixxmax
? srcpixxmax
: (int)srcpixx1
;
646 int x_offset2
= srcpixx2
< 0.0 ? 0 : srcpixx2
> srcpixxmax
? srcpixxmax
: (int)srcpixx2
;
647 int y_offset1
= srcpixy1
< 0.0 ? 0 : srcpixy1
> srcpixymax
? srcpixymax
: (int)srcpixy1
;
648 int y_offset2
= srcpixy2
< 0.0 ? 0 : srcpixy2
> srcpixymax
? srcpixymax
: (int)srcpixy2
;
650 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
651 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
652 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
653 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
656 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
657 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
658 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
660 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
663 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
664 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
665 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
667 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
671 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
672 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
673 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
677 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
684 // The following two local functions are for the B-spline weighting of the
685 // bicubic sampling algorithm
686 static inline double spline_cube(double value
)
688 return value
<= 0.0 ? 0.0 : value
* value
* value
;
691 static inline double spline_weight(double value
)
693 return (spline_cube(value
+ 2) -
694 4 * spline_cube(value
+ 1) +
695 6 * spline_cube(value
) -
696 4 * spline_cube(value
- 1)) / 6;
699 // This is the bicubic resampling algorithm
700 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
702 // This function implements a Bicubic B-Spline algorithm for resampling.
703 // This method is certainly a little slower than wxImage's default pixel
704 // replication method, however for most reasonably sized images not being
705 // upsampled too much on a fairly average CPU this difference is hardly
706 // noticeable and the results are far more pleasing to look at.
708 // This particular bicubic algorithm does pixel weighting according to a
709 // B-Spline that basically implements a Gaussian bell-like weighting
710 // kernel. Because of this method the results may appear a bit blurry when
711 // upsampling by large factors. This is basically because a slight
712 // gaussian blur is being performed to get the smooth look of the upsampled
715 // Edge pixels: 3-4 possible solutions
716 // - (Wrap/tile) Wrap the image, take the color value from the opposite
717 // side of the image.
718 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
719 // where n is nonpositive, will have the value of (2, 1).
720 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
721 // pixels which do have all neighbours.
722 // - (Clamp) Choose the nearest pixel along the border. This takes the
723 // border pixels and extends them out to infinity.
725 // NOTE: below the y_offset and x_offset variables are being set for edge
726 // pixels using the "Mirror" method mentioned above
730 ret_image
.Create(width
, height
, false);
732 unsigned char* src_data
= M_IMGDATA
->m_data
;
733 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
734 unsigned char* dst_data
= ret_image
.GetData();
735 unsigned char* dst_alpha
= NULL
;
739 ret_image
.SetAlpha();
740 dst_alpha
= ret_image
.GetAlpha();
743 for ( int dsty
= 0; dsty
< height
; dsty
++ )
745 // We need to calculate the source pixel to interpolate from - Y-axis
746 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
747 double dy
= srcpixy
- (int)srcpixy
;
749 for ( int dstx
= 0; dstx
< width
; dstx
++ )
751 // X-axis of pixel to interpolate from
752 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
753 double dx
= srcpixx
- (int)srcpixx
;
755 // Sums for each color channel
756 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
758 // Here we actually determine the RGBA values for the destination pixel
759 for ( int k
= -1; k
<= 2; k
++ )
762 int y_offset
= srcpixy
+ k
< 0.0
764 : srcpixy
+ k
>= M_IMGDATA
->m_height
765 ? M_IMGDATA
->m_height
- 1
766 : (int)(srcpixy
+ k
);
768 // Loop across the X axis
769 for ( int i
= -1; i
<= 2; i
++ )
772 int x_offset
= srcpixx
+ i
< 0.0
774 : srcpixx
+ i
>= M_IMGDATA
->m_width
775 ? M_IMGDATA
->m_width
- 1
776 : (int)(srcpixx
+ i
);
778 // Calculate the exact position where the source data
779 // should be pulled from based on the x_offset and y_offset
780 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
782 // Calculate the weight for the specified pixel according
783 // to the bicubic b-spline kernel we're using for
786 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
788 // Create a sum of all velues for each color channel
789 // adjusted for the pixel's calculated weight
790 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
791 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
792 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
794 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
798 // Put the data into the destination image. The summed values are
799 // of double data type and are rounded here for accuracy
800 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
801 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
802 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
806 *dst_alpha
++ = (unsigned char)sum_a
;
813 // Blur in the horizontal direction
814 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
817 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
819 unsigned char* src_data
= M_IMGDATA
->m_data
;
820 unsigned char* dst_data
= ret_image
.GetData();
821 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
822 unsigned char* dst_alpha
= NULL
;
824 // Check for a mask or alpha
827 ret_image
.SetAlpha();
828 dst_alpha
= ret_image
.GetAlpha();
830 else if ( M_IMGDATA
->m_hasMask
)
832 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
833 M_IMGDATA
->m_maskGreen
,
834 M_IMGDATA
->m_maskBlue
);
837 // number of pixels we average over
838 const int blurArea
= blurRadius
*2 + 1;
840 // Horizontal blurring algorithm - average all pixels in the specified blur
841 // radius in the X or horizontal direction
842 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
844 // Variables used in the blurring algorithm
851 const unsigned char *src
;
854 // Calculate the average of all pixels in the blur radius for the first
856 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
858 // To deal with the pixels at the start of a row so it's not
859 // grabbing GOK values from memory at negative indices of the
860 // image's data or grabbing from the previous row
862 pixel_idx
= y
* M_IMGDATA
->m_width
;
864 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
866 src
= src_data
+ pixel_idx
*3;
871 sum_a
+= src_alpha
[pixel_idx
];
874 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
875 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
876 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
877 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
879 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
881 // Now average the values of the rest of the pixels by just moving the
882 // blur radius box along the row
883 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
885 // Take care of edge pixels on the left edge by essentially
886 // duplicating the edge pixel
887 if ( x
- blurRadius
- 1 < 0 )
888 pixel_idx
= y
* M_IMGDATA
->m_width
;
890 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
892 // Subtract the value of the pixel at the left side of the blur
894 src
= src_data
+ pixel_idx
*3;
899 sum_a
-= src_alpha
[pixel_idx
];
901 // Take care of edge pixels on the right edge
902 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
903 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
905 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
907 // Add the value of the pixel being added to the end of our box
908 src
= src_data
+ pixel_idx
*3;
913 sum_a
+= src_alpha
[pixel_idx
];
915 // Save off the averaged data
916 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
917 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
918 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
919 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
921 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
928 // Blur in the vertical direction
929 wxImage
wxImage::BlurVertical(int blurRadius
) const
932 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
934 unsigned char* src_data
= M_IMGDATA
->m_data
;
935 unsigned char* dst_data
= ret_image
.GetData();
936 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
937 unsigned char* dst_alpha
= NULL
;
939 // Check for a mask or alpha
942 ret_image
.SetAlpha();
943 dst_alpha
= ret_image
.GetAlpha();
945 else if ( M_IMGDATA
->m_hasMask
)
947 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
948 M_IMGDATA
->m_maskGreen
,
949 M_IMGDATA
->m_maskBlue
);
952 // number of pixels we average over
953 const int blurArea
= blurRadius
*2 + 1;
955 // Vertical blurring algorithm - same as horizontal but switched the
956 // opposite direction
957 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
959 // Variables used in the blurring algorithm
966 const unsigned char *src
;
969 // Calculate the average of all pixels in our blur radius box for the
970 // first pixel of the column
971 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
973 // To deal with the pixels at the start of a column so it's not
974 // grabbing GOK values from memory at negative indices of the
975 // image's data or grabbing from the previous column
979 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
981 src
= src_data
+ pixel_idx
*3;
986 sum_a
+= src_alpha
[pixel_idx
];
989 dst
= dst_data
+ x
*3;
990 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
991 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
992 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
994 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
996 // Now average the values of the rest of the pixels by just moving the
997 // box along the column from top to bottom
998 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1000 // Take care of pixels that would be beyond the top edge by
1001 // duplicating the top edge pixel for the column
1002 if ( y
- blurRadius
- 1 < 0 )
1005 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1007 // Subtract the value of the pixel at the top of our blur radius box
1008 src
= src_data
+ pixel_idx
*3;
1013 sum_a
-= src_alpha
[pixel_idx
];
1015 // Take care of the pixels that would be beyond the bottom edge of
1016 // the image similar to the top edge
1017 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1018 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1020 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1022 // Add the value of the pixel being added to the end of our box
1023 src
= src_data
+ pixel_idx
*3;
1028 sum_a
+= src_alpha
[pixel_idx
];
1030 // Save off the averaged data
1031 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1032 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1033 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1034 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1036 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1043 // The new blur function
1044 wxImage
wxImage::Blur(int blurRadius
) const
1047 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1049 // Blur the image in each direction
1050 ret_image
= BlurHorizontal(blurRadius
);
1051 ret_image
= ret_image
.BlurVertical(blurRadius
);
1056 wxImage
wxImage::Rotate90( bool clockwise
) const
1060 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1062 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
1064 unsigned char *data
= image
.GetData();
1066 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1068 unsigned char *source_data
= M_IMGDATA
->m_data
;
1069 unsigned char *target_data
;
1070 unsigned char *alpha_data
= 0 ;
1071 unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1072 unsigned char *target_alpha
= 0 ;
1077 alpha_data
= image
.GetAlpha();
1078 wxCHECK_MSG( alpha_data
, image
, wxS("unable to create alpha channel") );
1081 if ( M_IMGDATA
->m_hasMask
)
1082 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1084 long height
= M_IMGDATA
->m_height
;
1085 long width
= M_IMGDATA
->m_width
;
1087 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1089 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1090 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1091 clockwise
? hot_x
: width
- 1 - hot_x
);
1094 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1096 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1097 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1098 clockwise
? height
- 1 - hot_y
: hot_y
);
1101 for (long j
= 0; j
< height
; j
++)
1103 for (long i
= 0; i
< width
; i
++)
1107 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1109 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1113 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1115 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1117 memcpy( target_data
, source_data
, 3 );
1122 memcpy( target_alpha
, source_alpha
, 1 );
1131 wxImage
wxImage::Rotate180() const
1135 wxCHECK_MSG( Ok(), image
, wxS("invalid image") );
1137 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1139 unsigned char *data
= image
.GetData();
1140 unsigned char *alpha
= NULL
;
1142 wxCHECK_MSG( data
, image
, wxS("unable to create image") );
1144 if ( M_IMGDATA
->m_alpha
!= NULL
)
1147 alpha
= image
.GetAlpha();
1148 wxCHECK_MSG( alpha
, image
, wxS("unable to create alpha channel") );
1151 if ( M_IMGDATA
->m_hasMask
)
1152 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1154 long height
= M_IMGDATA
->m_height
;
1155 long width
= M_IMGDATA
->m_width
;
1157 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1158 unsigned char *target_data
= data
+ width
* height
* 3;
1160 for (long j
= 0; j
< height
; j
++)
1162 for (long i
= 0; i
< width
; i
++)
1165 memcpy( target_data
, source_data
, 3 );
1172 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1173 unsigned char *dest_alpha
= alpha
+ width
* height
;
1175 for (long j
= 0; j
< height
; ++j
)
1177 for (long i
= 0; i
< width
; ++i
)
1179 *(--dest_alpha
) = *(src_alpha
++);
1187 wxImage
wxImage::Mirror( bool horizontally
) const
1191 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1193 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1195 unsigned char *data
= image
.GetData();
1196 unsigned char *alpha
= NULL
;
1198 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1200 if (M_IMGDATA
->m_alpha
!= NULL
) {
1202 alpha
= image
.GetAlpha();
1203 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1206 if (M_IMGDATA
->m_hasMask
)
1207 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1209 long height
= M_IMGDATA
->m_height
;
1210 long width
= M_IMGDATA
->m_width
;
1212 unsigned char *source_data
= M_IMGDATA
->m_data
;
1213 unsigned char *target_data
;
1217 for (long j
= 0; j
< height
; j
++)
1220 target_data
= data
-3;
1221 for (long i
= 0; i
< width
; i
++)
1223 memcpy( target_data
, source_data
, 3 );
1231 // src_alpha starts at the first pixel and increases by 1 after each step
1232 // (a step here is the copy of the alpha value of one pixel)
1233 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1234 // dest_alpha starts just beyond the first line, decreases before each step,
1235 // and after each line is finished, increases by 2 widths (skipping the line
1236 // just copied and the line that will be copied next)
1237 unsigned char *dest_alpha
= alpha
+ width
;
1239 for (long jj
= 0; jj
< height
; ++jj
)
1241 for (long i
= 0; i
< width
; ++i
) {
1242 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1244 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1250 for (long i
= 0; i
< height
; i
++)
1252 target_data
= data
+ 3*width
*(height
-1-i
);
1253 memcpy( target_data
, source_data
, (size_t)3*width
);
1254 source_data
+= 3*width
;
1259 // src_alpha starts at the first pixel and increases by 1 width after each step
1260 // (a step here is the copy of the alpha channel of an entire line)
1261 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1262 // dest_alpha starts just beyond the last line (beyond the whole image)
1263 // and decreases by 1 width before each step
1264 unsigned char *dest_alpha
= alpha
+ width
* height
;
1266 for (long jj
= 0; jj
< height
; ++jj
)
1268 dest_alpha
-= width
;
1269 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1278 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1282 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1284 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1285 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1286 image
, wxT("invalid subimage size") );
1288 const int subwidth
= rect
.GetWidth();
1289 const int subheight
= rect
.GetHeight();
1291 image
.Create( subwidth
, subheight
, false );
1293 const unsigned char *src_data
= GetData();
1294 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1295 unsigned char *subdata
= image
.GetData();
1296 unsigned char *subalpha
= NULL
;
1298 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1300 if (src_alpha
!= NULL
) {
1302 subalpha
= image
.GetAlpha();
1303 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1306 if (M_IMGDATA
->m_hasMask
)
1307 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1309 const int width
= GetWidth();
1310 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1312 src_data
+= 3 * pixsoff
;
1313 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1315 for (long j
= 0; j
< subheight
; ++j
)
1317 memcpy( subdata
, src_data
, 3 * subwidth
);
1318 subdata
+= 3 * subwidth
;
1319 src_data
+= 3 * width
;
1320 if (subalpha
!= NULL
) {
1321 memcpy( subalpha
, src_alpha
, subwidth
);
1322 subalpha
+= subwidth
;
1330 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1331 int r_
, int g_
, int b_
) const
1335 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1336 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1338 int width
= GetWidth(), height
= GetHeight();
1339 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1341 unsigned char r
= (unsigned char)r_
;
1342 unsigned char g
= (unsigned char)g_
;
1343 unsigned char b
= (unsigned char)b_
;
1344 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1346 GetOrFindMaskColour( &r
, &g
, &b
);
1347 image
.SetMaskColour(r
, g
, b
);
1350 image
.SetRGB(wxRect(), r
, g
, b
);
1352 // we have two coordinate systems:
1353 // source: starting at 0,0 of source image
1354 // destination starting at 0,0 of destination image
1355 // Documentation says:
1356 // "The image is pasted into a new image [...] at the position pos relative
1357 // to the upper left of the new image." this means the transition rule is:
1358 // "dest coord" = "source coord" + pos;
1360 // calculate the intersection using source coordinates:
1361 wxRect
srcRect(0, 0, width
, height
);
1362 wxRect
dstRect(-pos
, size
);
1364 srcRect
.Intersect(dstRect
);
1366 if (!srcRect
.IsEmpty())
1368 // insertion point is needed in destination coordinates.
1369 // NB: it is not always "pos"!
1370 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1372 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1373 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1375 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1381 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1383 wxCHECK_RET( Ok(), wxT("invalid image") );
1384 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1390 int width
= image
.GetWidth();
1391 int height
= image
.GetHeight();
1404 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1405 width
= M_IMGDATA
->m_width
- (x
+xx
);
1406 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1407 height
= M_IMGDATA
->m_height
- (y
+yy
);
1409 if (width
< 1) return;
1410 if (height
< 1) return;
1412 if ((!HasMask() && !image
.HasMask()) ||
1413 (HasMask() && !image
.HasMask()) ||
1414 ((HasMask() && image
.HasMask() &&
1415 (GetMaskRed()==image
.GetMaskRed()) &&
1416 (GetMaskGreen()==image
.GetMaskGreen()) &&
1417 (GetMaskBlue()==image
.GetMaskBlue()))))
1419 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1420 int source_step
= image
.GetWidth()*3;
1422 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1423 int target_step
= M_IMGDATA
->m_width
*3;
1424 for (int j
= 0; j
< height
; j
++)
1426 memcpy( target_data
, source_data
, width
*3 );
1427 source_data
+= source_step
;
1428 target_data
+= target_step
;
1432 // Copy over the alpha channel from the original image
1433 if ( image
.HasAlpha() )
1438 unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1439 int source_step
= image
.GetWidth();
1441 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1442 int target_step
= M_IMGDATA
->m_width
;
1444 for (int j
= 0; j
< height
; j
++,
1445 source_data
+= source_step
,
1446 target_data
+= target_step
)
1448 memcpy( target_data
, source_data
, width
);
1452 if (!HasMask() && image
.HasMask())
1454 unsigned char r
= image
.GetMaskRed();
1455 unsigned char g
= image
.GetMaskGreen();
1456 unsigned char b
= image
.GetMaskBlue();
1458 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1459 int source_step
= image
.GetWidth()*3;
1461 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1462 int target_step
= M_IMGDATA
->m_width
*3;
1464 for (int j
= 0; j
< height
; j
++)
1466 for (int i
= 0; i
< width
*3; i
+=3)
1468 if ((source_data
[i
] != r
) ||
1469 (source_data
[i
+1] != g
) ||
1470 (source_data
[i
+2] != b
))
1472 memcpy( target_data
+i
, source_data
+i
, 3 );
1475 source_data
+= source_step
;
1476 target_data
+= target_step
;
1481 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1482 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1484 wxCHECK_RET( Ok(), wxT("invalid image") );
1488 unsigned char *data
= GetData();
1490 const int w
= GetWidth();
1491 const int h
= GetHeight();
1493 for (int j
= 0; j
< h
; j
++)
1494 for (int i
= 0; i
< w
; i
++)
1496 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1506 wxImage
wxImage::ConvertToGreyscale(void) const
1508 return ConvertToGreyscale(0.299, 0.587, 0.114);
1511 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1515 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1517 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1519 unsigned char *dest
= image
.GetData();
1521 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1523 unsigned char *src
= M_IMGDATA
->m_data
;
1524 bool hasMask
= M_IMGDATA
->m_hasMask
;
1525 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1526 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1527 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1530 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1532 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1533 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1535 memcpy(dest
, src
, 3);
1536 // don't modify the mask
1537 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1542 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1546 // copy the alpha channel, if any
1549 const size_t alphaSize
= GetWidth() * GetHeight();
1550 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1551 memcpy(alpha
, GetAlpha(), alphaSize
);
1553 image
.SetAlpha(alpha
);
1559 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1563 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1565 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1567 unsigned char *data
= image
.GetData();
1569 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1571 if (M_IMGDATA
->m_hasMask
)
1573 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1574 M_IMGDATA
->m_maskBlue
== b
)
1575 image
.SetMaskColour( 255, 255, 255 );
1577 image
.SetMaskColour( 0, 0, 0 );
1580 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1582 unsigned char *srcd
= M_IMGDATA
->m_data
;
1583 unsigned char *tard
= image
.GetData();
1585 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1587 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1588 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1594 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1596 wxImage image
= *this;
1598 unsigned char mr
= image
.GetMaskRed();
1599 unsigned char mg
= image
.GetMaskGreen();
1600 unsigned char mb
= image
.GetMaskBlue();
1602 int width
= image
.GetWidth();
1603 int height
= image
.GetHeight();
1604 bool has_mask
= image
.HasMask();
1606 for (int y
= height
-1; y
>= 0; --y
)
1608 for (int x
= width
-1; x
>= 0; --x
)
1610 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1611 unsigned char* r
= data
;
1612 unsigned char* g
= data
+1;
1613 unsigned char* b
= data
+2;
1615 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1618 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1624 int wxImage::GetWidth() const
1626 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1628 return M_IMGDATA
->m_width
;
1631 int wxImage::GetHeight() const
1633 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1635 return M_IMGDATA
->m_height
;
1638 wxBitmapType
wxImage::GetType() const
1640 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1642 return M_IMGDATA
->m_type
;
1645 void wxImage::SetType(wxBitmapType type
)
1647 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1649 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1650 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1652 M_IMGDATA
->m_type
= type
;
1655 long wxImage::XYToIndex(int x
, int y
) const
1659 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1661 return y
*M_IMGDATA
->m_width
+ x
;
1667 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1669 long pos
= XYToIndex(x
, y
);
1670 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1676 M_IMGDATA
->m_data
[ pos
] = r
;
1677 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1678 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1681 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1683 wxCHECK_RET( Ok(), wxT("invalid image") );
1688 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1689 if ( rect
== wxRect() )
1695 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1696 imageRect
.Contains(rect
.GetBottomRight()),
1697 wxT("invalid bounding rectangle") );
1700 int x1
= rect
.GetLeft(),
1702 x2
= rect
.GetRight() + 1,
1703 y2
= rect
.GetBottom() + 1;
1705 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1706 int x
, y
, width
= GetWidth();
1707 for (y
= y1
; y
< y2
; y
++)
1709 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1710 for (x
= x1
; x
< x2
; x
++)
1719 unsigned char wxImage::GetRed( int x
, int y
) const
1721 long pos
= XYToIndex(x
, y
);
1722 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1726 return M_IMGDATA
->m_data
[pos
];
1729 unsigned char wxImage::GetGreen( int x
, int y
) const
1731 long pos
= XYToIndex(x
, y
);
1732 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1736 return M_IMGDATA
->m_data
[pos
+1];
1739 unsigned char wxImage::GetBlue( int x
, int y
) const
1741 long pos
= XYToIndex(x
, y
);
1742 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1746 return M_IMGDATA
->m_data
[pos
+2];
1749 bool wxImage::IsOk() const
1751 // image of 0 width or height can't be considered ok - at least because it
1752 // causes crashes in ConvertToBitmap() if we don't catch it in time
1753 wxImageRefData
*data
= M_IMGDATA
;
1754 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1757 unsigned char *wxImage::GetData() const
1759 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1761 return M_IMGDATA
->m_data
;
1764 void wxImage::SetData( unsigned char *data
, bool static_data
)
1766 wxCHECK_RET( Ok(), wxT("invalid image") );
1768 wxImageRefData
*newRefData
= new wxImageRefData();
1770 newRefData
->m_width
= M_IMGDATA
->m_width
;
1771 newRefData
->m_height
= M_IMGDATA
->m_height
;
1772 newRefData
->m_data
= data
;
1773 newRefData
->m_ok
= true;
1774 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1775 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1776 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1777 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1778 newRefData
->m_static
= static_data
;
1782 m_refData
= newRefData
;
1785 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1787 wxImageRefData
*newRefData
= new wxImageRefData();
1791 newRefData
->m_width
= new_width
;
1792 newRefData
->m_height
= new_height
;
1793 newRefData
->m_data
= data
;
1794 newRefData
->m_ok
= true;
1795 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1796 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1797 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1798 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1802 newRefData
->m_width
= new_width
;
1803 newRefData
->m_height
= new_height
;
1804 newRefData
->m_data
= data
;
1805 newRefData
->m_ok
= true;
1807 newRefData
->m_static
= static_data
;
1811 m_refData
= newRefData
;
1814 // ----------------------------------------------------------------------------
1815 // alpha channel support
1816 // ----------------------------------------------------------------------------
1818 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1820 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1822 long pos
= XYToIndex(x
, y
);
1823 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1827 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1830 unsigned char wxImage::GetAlpha(int x
, int y
) const
1832 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1834 long pos
= XYToIndex(x
, y
);
1835 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1837 return M_IMGDATA
->m_alpha
[pos
];
1841 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1845 const int w
= M_IMGDATA
->m_width
;
1846 const int h
= M_IMGDATA
->m_height
;
1848 unsigned char *alpha
= GetAlpha();
1849 unsigned char *data
= GetData();
1851 for ( int y
= 0; y
< h
; y
++ )
1853 for ( int x
= 0; x
< w
; x
++ )
1865 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1867 wxCHECK_RET( Ok(), wxT("invalid image") );
1873 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1876 if( !M_IMGDATA
->m_staticAlpha
)
1877 free(M_IMGDATA
->m_alpha
);
1879 M_IMGDATA
->m_alpha
= alpha
;
1880 M_IMGDATA
->m_staticAlpha
= static_data
;
1883 unsigned char *wxImage::GetAlpha() const
1885 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1887 return M_IMGDATA
->m_alpha
;
1890 void wxImage::InitAlpha()
1892 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1894 // initialize memory for alpha channel
1897 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1898 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1902 // use the mask to initialize the alpha channel.
1903 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1905 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1906 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1907 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1908 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1912 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1913 ? wxIMAGE_ALPHA_TRANSPARENT
1914 : wxIMAGE_ALPHA_OPAQUE
;
1917 M_IMGDATA
->m_hasMask
= false;
1921 // make the image fully opaque
1922 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1926 void wxImage::ClearAlpha()
1928 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1930 if ( !M_IMGDATA
->m_staticAlpha
)
1931 free( M_IMGDATA
->m_alpha
);
1933 M_IMGDATA
->m_alpha
= NULL
;
1937 // ----------------------------------------------------------------------------
1939 // ----------------------------------------------------------------------------
1941 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1943 wxCHECK_RET( Ok(), wxT("invalid image") );
1947 M_IMGDATA
->m_maskRed
= r
;
1948 M_IMGDATA
->m_maskGreen
= g
;
1949 M_IMGDATA
->m_maskBlue
= b
;
1950 M_IMGDATA
->m_hasMask
= true;
1953 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1955 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1957 if (M_IMGDATA
->m_hasMask
)
1959 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1960 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1961 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1966 FindFirstUnusedColour(r
, g
, b
);
1971 unsigned char wxImage::GetMaskRed() const
1973 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1975 return M_IMGDATA
->m_maskRed
;
1978 unsigned char wxImage::GetMaskGreen() const
1980 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1982 return M_IMGDATA
->m_maskGreen
;
1985 unsigned char wxImage::GetMaskBlue() const
1987 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1989 return M_IMGDATA
->m_maskBlue
;
1992 void wxImage::SetMask( bool mask
)
1994 wxCHECK_RET( Ok(), wxT("invalid image") );
1998 M_IMGDATA
->m_hasMask
= mask
;
2001 bool wxImage::HasMask() const
2003 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2005 return M_IMGDATA
->m_hasMask
;
2008 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
2010 long pos
= XYToIndex(x
, y
);
2011 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
2014 if ( M_IMGDATA
->m_hasMask
)
2016 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
2017 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
2018 p
[1] == M_IMGDATA
->m_maskGreen
&&
2019 p
[2] == M_IMGDATA
->m_maskBlue
)
2026 if ( M_IMGDATA
->m_alpha
)
2028 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2030 // transparent enough
2039 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2040 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2042 // check that the images are the same size
2043 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2045 wxLogError( _("Image and mask have different sizes.") );
2049 // find unused colour
2050 unsigned char r
,g
,b
;
2051 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2053 wxLogError( _("No unused colour in image being masked.") );
2059 unsigned char *imgdata
= GetData();
2060 unsigned char *maskdata
= mask
.GetData();
2062 const int w
= GetWidth();
2063 const int h
= GetHeight();
2065 for (int j
= 0; j
< h
; j
++)
2067 for (int i
= 0; i
< w
; i
++)
2069 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2080 SetMaskColour(r
, g
, b
);
2086 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2091 unsigned char mr
, mg
, mb
;
2092 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2094 wxLogError( _("No unused colour in image being masked.") );
2098 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2101 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2104 unsigned char threshold
)
2112 SetMaskColour(mr
, mg
, mb
);
2114 unsigned char *imgdata
= GetData();
2115 unsigned char *alphadata
= GetAlpha();
2118 int h
= GetHeight();
2120 for (int y
= 0; y
< h
; y
++)
2122 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2124 if (*alphadata
< threshold
)
2133 if ( !M_IMGDATA
->m_staticAlpha
)
2134 free(M_IMGDATA
->m_alpha
);
2136 M_IMGDATA
->m_alpha
= NULL
;
2137 M_IMGDATA
->m_staticAlpha
= false;
2142 // ----------------------------------------------------------------------------
2143 // Palette functions
2144 // ----------------------------------------------------------------------------
2148 bool wxImage::HasPalette() const
2153 return M_IMGDATA
->m_palette
.Ok();
2156 const wxPalette
& wxImage::GetPalette() const
2158 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
2160 return M_IMGDATA
->m_palette
;
2163 void wxImage::SetPalette(const wxPalette
& palette
)
2165 wxCHECK_RET( Ok(), wxT("invalid image") );
2169 M_IMGDATA
->m_palette
= palette
;
2172 #endif // wxUSE_PALETTE
2174 // ----------------------------------------------------------------------------
2175 // Option functions (arbitrary name/value mapping)
2176 // ----------------------------------------------------------------------------
2178 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2182 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2183 if ( idx
== wxNOT_FOUND
)
2185 M_IMGDATA
->m_optionNames
.Add(name
);
2186 M_IMGDATA
->m_optionValues
.Add(value
);
2190 M_IMGDATA
->m_optionNames
[idx
] = name
;
2191 M_IMGDATA
->m_optionValues
[idx
] = value
;
2195 void wxImage::SetOption(const wxString
& name
, int value
)
2198 valStr
.Printf(wxT("%d"), value
);
2199 SetOption(name
, valStr
);
2202 wxString
wxImage::GetOption(const wxString
& name
) const
2205 return wxEmptyString
;
2207 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2208 if ( idx
== wxNOT_FOUND
)
2209 return wxEmptyString
;
2211 return M_IMGDATA
->m_optionValues
[idx
];
2214 int wxImage::GetOptionInt(const wxString
& name
) const
2216 return wxAtoi(GetOption(name
));
2219 bool wxImage::HasOption(const wxString
& name
) const
2221 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2225 // ----------------------------------------------------------------------------
2227 // ----------------------------------------------------------------------------
2229 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2230 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2231 int WXUNUSED_UNLESS_STREAMS(index
) )
2233 #if HAS_FILE_STREAMS
2234 if (wxFileExists(filename
))
2236 wxImageFileInputStream
stream(filename
);
2237 wxBufferedInputStream
bstream( stream
);
2238 return LoadFile(bstream
, type
, index
);
2242 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2246 #else // !HAS_FILE_STREAMS
2248 #endif // HAS_FILE_STREAMS
2251 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2252 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2253 int WXUNUSED_UNLESS_STREAMS(index
) )
2255 #if HAS_FILE_STREAMS
2256 if (wxFileExists(filename
))
2258 wxImageFileInputStream
stream(filename
);
2259 wxBufferedInputStream
bstream( stream
);
2260 return LoadFile(bstream
, mimetype
, index
);
2264 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2268 #else // !HAS_FILE_STREAMS
2270 #endif // HAS_FILE_STREAMS
2274 bool wxImage::SaveFile( const wxString
& filename
) const
2276 wxString ext
= filename
.AfterLast('.').Lower();
2278 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2281 wxLogError(_("Can't save image to file '%s': unknown extension."),
2286 return SaveFile(filename
, handler
->GetType());
2289 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2290 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2292 #if HAS_FILE_STREAMS
2293 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2295 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2297 wxImageFileOutputStream
stream(filename
);
2299 if ( stream
.IsOk() )
2301 wxBufferedOutputStream
bstream( stream
);
2302 return SaveFile(bstream
, type
);
2304 #endif // HAS_FILE_STREAMS
2309 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2310 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2312 #if HAS_FILE_STREAMS
2313 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2315 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2317 wxImageFileOutputStream
stream(filename
);
2319 if ( stream
.IsOk() )
2321 wxBufferedOutputStream
bstream( stream
);
2322 return SaveFile(bstream
, mimetype
);
2324 #endif // HAS_FILE_STREAMS
2329 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2331 #if HAS_FILE_STREAMS
2332 wxImageFileInputStream
stream(name
);
2333 return CanRead(stream
);
2339 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2340 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2342 #if HAS_FILE_STREAMS
2343 wxImageFileInputStream
stream(name
);
2345 return GetImageCount(stream
, type
);
2353 bool wxImage::CanRead( wxInputStream
&stream
)
2355 const wxList
& list
= GetHandlers();
2357 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2359 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2360 if (handler
->CanRead( stream
))
2367 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2369 wxImageHandler
*handler
;
2371 if ( type
== wxBITMAP_TYPE_ANY
)
2373 const wxList
& list
= GetHandlers();
2375 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2377 node
= node
->GetNext() )
2379 handler
= (wxImageHandler
*)node
->GetData();
2380 if ( handler
->CanRead(stream
) )
2382 const int count
= handler
->GetImageCount(stream
);
2389 wxLogWarning(_("No handler found for image type."));
2393 handler
= FindHandler(type
);
2397 wxLogWarning(_("No image handler for type %d defined."), type
);
2401 if ( handler
->CanRead(stream
) )
2403 return handler
->GetImageCount(stream
);
2407 wxLogError(_("Image file is not of type %d."), type
);
2412 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2414 // save the options values which can be clobbered by the handler (e.g. many
2415 // of them call Destroy() before trying to load the file)
2416 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2417 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2419 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2422 // rescale the image to the specified size if needed
2423 if ( maxWidth
|| maxHeight
)
2425 const unsigned widthOrig
= GetWidth(),
2426 heightOrig
= GetHeight();
2428 // this uses the same (trivial) algorithm as the JPEG handler
2429 unsigned width
= widthOrig
,
2430 height
= heightOrig
;
2431 while ( (maxWidth
&& width
> maxWidth
) ||
2432 (maxHeight
&& height
> maxHeight
) )
2438 if ( width
!= widthOrig
|| height
!= heightOrig
)
2439 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2442 // Set this after Rescale, which currently does not preserve it
2443 M_IMGDATA
->m_type
= handler
.GetType();
2448 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2452 wxImageHandler
*handler
;
2454 if ( type
== wxBITMAP_TYPE_ANY
)
2456 const wxList
& list
= GetHandlers();
2457 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2459 node
= node
->GetNext() )
2461 handler
= (wxImageHandler
*)node
->GetData();
2462 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2466 wxLogWarning( _("No handler found for image type.") );
2470 //else: have specific type
2472 handler
= FindHandler(type
);
2475 wxLogWarning( _("No image handler for type %d defined."), type
);
2479 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2481 wxLogError(_("Image file is not of type %d."), type
);
2485 return DoLoad(*handler
, stream
, index
);
2488 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2492 m_refData
= new wxImageRefData
;
2494 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2498 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2502 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2504 wxLogError(_("Image file is not of type %s."), mimetype
);
2508 return DoLoad(*handler
, stream
, index
);
2511 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2513 wxImage
* const self
= const_cast<wxImage
*>(this);
2514 if ( !handler
.SaveFile(self
, stream
) )
2517 M_IMGDATA
->m_type
= handler
.GetType();
2521 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2523 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2525 wxImageHandler
*handler
= FindHandler(type
);
2528 wxLogWarning( _("No image handler for type %d defined."), type
);
2532 return DoSave(*handler
, stream
);
2535 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2537 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2539 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2542 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2545 return DoSave(*handler
, stream
);
2548 #endif // wxUSE_STREAMS
2550 // ----------------------------------------------------------------------------
2551 // image I/O handlers
2552 // ----------------------------------------------------------------------------
2554 void wxImage::AddHandler( wxImageHandler
*handler
)
2556 // Check for an existing handler of the type being added.
2557 if (FindHandler( handler
->GetType() ) == 0)
2559 sm_handlers
.Append( handler
);
2563 // This is not documented behaviour, merely the simplest 'fix'
2564 // for preventing duplicate additions. If someone ever has
2565 // a good reason to add and remove duplicate handlers (and they
2566 // may) we should probably refcount the duplicates.
2567 // also an issue in InsertHandler below.
2569 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2570 handler
->GetName().c_str() );
2575 void wxImage::InsertHandler( wxImageHandler
*handler
)
2577 // Check for an existing handler of the type being added.
2578 if (FindHandler( handler
->GetType() ) == 0)
2580 sm_handlers
.Insert( handler
);
2584 // see AddHandler for additional comments.
2585 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2586 handler
->GetName().c_str() );
2591 bool wxImage::RemoveHandler( const wxString
& name
)
2593 wxImageHandler
*handler
= FindHandler(name
);
2596 sm_handlers
.DeleteObject(handler
);
2604 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2606 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2609 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2610 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2612 node
= node
->GetNext();
2617 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2619 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2622 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2623 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2625 if (handler
->GetExtension() == extension
)
2627 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2630 node
= node
->GetNext();
2635 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2637 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2640 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2641 if (handler
->GetType() == bitmapType
) return handler
;
2642 node
= node
->GetNext();
2647 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2649 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2652 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2653 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2654 node
= node
->GetNext();
2659 void wxImage::InitStandardHandlers()
2662 AddHandler(new wxBMPHandler
);
2663 #endif // wxUSE_STREAMS
2666 void wxImage::CleanUpHandlers()
2668 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2671 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2672 wxList::compatibility_iterator next
= node
->GetNext();
2677 sm_handlers
.Clear();
2680 wxString
wxImage::GetImageExtWildcard()
2684 wxList
& Handlers
= wxImage::GetHandlers();
2685 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2688 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2689 fmts
+= wxT("*.") + Handler
->GetExtension();
2690 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2691 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2692 Node
= Node
->GetNext();
2693 if ( Node
) fmts
+= wxT(";");
2696 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2699 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2701 const double red
= rgb
.red
/ 255.0,
2702 green
= rgb
.green
/ 255.0,
2703 blue
= rgb
.blue
/ 255.0;
2705 // find the min and max intensity (and remember which one was it for the
2707 double minimumRGB
= red
;
2708 if ( green
< minimumRGB
)
2710 if ( blue
< minimumRGB
)
2713 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2714 double maximumRGB
= red
;
2715 if ( green
> maximumRGB
)
2720 if ( blue
> maximumRGB
)
2726 const double value
= maximumRGB
;
2728 double hue
= 0.0, saturation
;
2729 const double deltaRGB
= maximumRGB
- minimumRGB
;
2730 if ( wxIsNullDouble(deltaRGB
) )
2732 // Gray has no color
2741 hue
= (green
- blue
) / deltaRGB
;
2745 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2749 hue
= 4.0 + (red
- green
) / deltaRGB
;
2753 wxFAIL_MSG(wxT("hue not specified"));
2762 saturation
= deltaRGB
/ maximumRGB
;
2765 return HSVValue(hue
, saturation
, value
);
2768 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2770 double red
, green
, blue
;
2772 if ( wxIsNullDouble(hsv
.saturation
) )
2781 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2782 int i
= (int)floor(hue
);
2783 double f
= hue
- i
; // fractional part of h
2784 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2790 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2795 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2803 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2808 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2813 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2821 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2826 return RGBValue((unsigned char)(red
* 255.0),
2827 (unsigned char)(green
* 255.0),
2828 (unsigned char)(blue
* 255.0));
2832 * Rotates the hue of each pixel of the image. angle is a double in the range
2833 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2835 void wxImage::RotateHue(double angle
)
2839 unsigned char *srcBytePtr
;
2840 unsigned char *dstBytePtr
;
2841 unsigned long count
;
2842 wxImage::HSVValue hsv
;
2843 wxImage::RGBValue rgb
;
2845 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2846 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2847 if ( count
> 0 && !wxIsNullDouble(angle
) )
2849 srcBytePtr
= M_IMGDATA
->m_data
;
2850 dstBytePtr
= srcBytePtr
;
2853 rgb
.red
= *srcBytePtr
++;
2854 rgb
.green
= *srcBytePtr
++;
2855 rgb
.blue
= *srcBytePtr
++;
2856 hsv
= RGBtoHSV(rgb
);
2858 hsv
.hue
= hsv
.hue
+ angle
;
2860 hsv
.hue
= hsv
.hue
- 1.0;
2861 else if (hsv
.hue
< 0.0)
2862 hsv
.hue
= hsv
.hue
+ 1.0;
2864 rgb
= HSVtoRGB(hsv
);
2865 *dstBytePtr
++ = rgb
.red
;
2866 *dstBytePtr
++ = rgb
.green
;
2867 *dstBytePtr
++ = rgb
.blue
;
2868 } while (--count
!= 0);
2872 //-----------------------------------------------------------------------------
2874 //-----------------------------------------------------------------------------
2876 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2879 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
2881 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2882 // wxImageHandler::CallDoCanRead
2884 if ( !stream
.IsSeekable() )
2885 return false; // can't test unseekable stream
2887 wxFileOffset posOld
= stream
.TellI();
2888 int n
= DoGetImageCount(stream
);
2890 // restore the old position to be able to test other formats and so on
2891 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2893 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2895 // reading would fail anyhow as we're not at the right position
2902 bool wxImageHandler::CanRead( const wxString
& name
)
2904 if (wxFileExists(name
))
2906 wxImageFileInputStream
stream(name
);
2907 return CanRead(stream
);
2910 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2915 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2917 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2918 // wxImageHandler::GetImageCount
2920 if ( !stream
.IsSeekable() )
2921 return false; // can't test unseekable stream
2923 wxFileOffset posOld
= stream
.TellI();
2924 bool ok
= DoCanRead(stream
);
2926 // restore the old position to be able to test other formats and so on
2927 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2929 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2931 // reading would fail anyhow as we're not at the right position
2938 #endif // wxUSE_STREAMS
2942 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2944 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
2946 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2947 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2949 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2950 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2952 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2955 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2957 else // no resolution options specified
2962 return wxIMAGE_RESOLUTION_NONE
;
2965 // get the resolution unit too
2966 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2969 // this is the default
2970 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2973 return (wxImageResolution
)resUnit
;
2976 // ----------------------------------------------------------------------------
2977 // image histogram stuff
2978 // ----------------------------------------------------------------------------
2981 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2986 unsigned char g2
) const
2988 unsigned long key
= MakeKey(r2
, g2
, b2
);
2990 while ( find(key
) != end() )
2992 // color already used
3004 wxLogError(_("No unused colour in image.") );
3010 key
= MakeKey(r2
, g2
, b2
);
3024 wxImage::FindFirstUnusedColour(unsigned char *r
,
3029 unsigned char g2
) const
3031 wxImageHistogram histogram
;
3033 ComputeHistogram(histogram
);
3035 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3041 // Counts and returns the number of different colours. Optionally stops
3042 // when it exceeds 'stopafter' different colours. This is useful, for
3043 // example, to see if the image can be saved as 8-bit (256 colour or
3044 // less, in this case it would be invoked as CountColours(256)). Default
3045 // value for stopafter is -1 (don't care).
3047 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3051 unsigned char r
, g
, b
;
3053 unsigned long size
, nentries
, key
;
3056 size
= GetWidth() * GetHeight();
3059 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3064 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3066 if (h
.Get(key
) == NULL
)
3077 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3079 unsigned char *p
= GetData();
3080 unsigned long nentries
= 0;
3084 const unsigned long size
= GetWidth() * GetHeight();
3086 unsigned char r
, g
, b
;
3087 for ( unsigned long n
= 0; n
< size
; n
++ )
3093 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3095 if ( entry
.value
++ == 0 )
3096 entry
.index
= nentries
++;
3103 * Rotation code by Carlos Moreno
3106 static const double wxROTATE_EPSILON
= 1e-10;
3108 // Auxiliary function to rotate a point (x,y) with respect to point p0
3109 // make it inline and use a straight return to facilitate optimization
3110 // also, the function receives the sine and cosine of the angle to avoid
3111 // repeating the time-consuming calls to these functions -- sin/cos can
3112 // be computed and stored in the calling function.
3114 static inline wxRealPoint
3115 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3116 const wxRealPoint
& p0
)
3118 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3119 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3122 static inline wxRealPoint
3123 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3124 const wxRealPoint
& p0
)
3126 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3129 wxImage
wxImage::Rotate(double angle
,
3130 const wxPoint
& centre_of_rotation
,
3132 wxPoint
*offset_after_rotation
) const
3134 // screen coordinates are a mirror image of "real" coordinates
3137 const bool has_alpha
= HasAlpha();
3139 const int w
= GetWidth();
3140 const int h
= GetHeight();
3144 // Create pointer-based array to accelerate access to wxImage's data
3145 unsigned char ** data
= new unsigned char * [h
];
3146 data
[0] = GetData();
3147 for (i
= 1; i
< h
; i
++)
3148 data
[i
] = data
[i
- 1] + (3 * w
);
3150 // Same for alpha channel
3151 unsigned char ** alpha
= NULL
;
3154 alpha
= new unsigned char * [h
];
3155 alpha
[0] = GetAlpha();
3156 for (i
= 1; i
< h
; i
++)
3157 alpha
[i
] = alpha
[i
- 1] + w
;
3160 // precompute coefficients for rotation formula
3161 const double cos_angle
= cos(angle
);
3162 const double sin_angle
= sin(angle
);
3164 // Create new Image to store the result
3165 // First, find rectangle that covers the rotated image; to do that,
3166 // rotate the four corners
3168 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3170 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3171 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3172 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3173 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3175 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3176 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3177 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3178 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3180 // Create rotated image
3181 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3182 // With alpha channel
3186 if (offset_after_rotation
!= NULL
)
3188 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3191 // the rotated (destination) image is always accessed sequentially via this
3192 // pointer, there is no need for pointer-based arrays here
3193 unsigned char *dst
= rotated
.GetData();
3195 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3197 // if the original image has a mask, use its RGB values as the blank pixel,
3198 // else, fall back to default (black).
3199 unsigned char blank_r
= 0;
3200 unsigned char blank_g
= 0;
3201 unsigned char blank_b
= 0;
3205 blank_r
= GetMaskRed();
3206 blank_g
= GetMaskGreen();
3207 blank_b
= GetMaskBlue();
3208 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3211 // Now, for each point of the rotated image, find where it came from, by
3212 // performing an inverse rotation (a rotation of -angle) and getting the
3213 // pixel at those coordinates
3215 const int rH
= rotated
.GetHeight();
3216 const int rW
= rotated
.GetWidth();
3218 // do the (interpolating) test outside of the loops, so that it is done
3219 // only once, instead of repeating it for each pixel.
3222 for (int y
= 0; y
< rH
; y
++)
3224 for (int x
= 0; x
< rW
; x
++)
3226 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3228 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3229 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3231 // interpolate using the 4 enclosing grid-points. Those
3232 // points can be obtained using floor and ceiling of the
3233 // exact coordinates of the point
3236 if (0 < src
.x
&& src
.x
< w
- 1)
3238 x1
= wxRound(floor(src
.x
));
3239 x2
= wxRound(ceil(src
.x
));
3241 else // else means that x is near one of the borders (0 or width-1)
3243 x1
= x2
= wxRound (src
.x
);
3246 if (0 < src
.y
&& src
.y
< h
- 1)
3248 y1
= wxRound(floor(src
.y
));
3249 y2
= wxRound(ceil(src
.y
));
3253 y1
= y2
= wxRound (src
.y
);
3256 // get four points and the distances (square of the distance,
3257 // for efficiency reasons) for the interpolation formula
3259 // GRG: Do not calculate the points until they are
3260 // really needed -- this way we can calculate
3261 // just one, instead of four, if d1, d2, d3
3262 // or d4 are < wxROTATE_EPSILON
3264 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3265 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3266 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3267 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3269 // Now interpolate as a weighted average of the four surrounding
3270 // points, where the weights are the distances to each of those points
3272 // If the point is exactly at one point of the grid of the source
3273 // image, then don't interpolate -- just assign the pixel
3275 // d1,d2,d3,d4 are positive -- no need for abs()
3276 if (d1
< wxROTATE_EPSILON
)
3278 unsigned char *p
= data
[y1
] + (3 * x1
);
3284 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3286 else if (d2
< wxROTATE_EPSILON
)
3288 unsigned char *p
= data
[y1
] + (3 * x2
);
3294 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3296 else if (d3
< wxROTATE_EPSILON
)
3298 unsigned char *p
= data
[y2
] + (3 * x2
);
3304 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3306 else if (d4
< wxROTATE_EPSILON
)
3308 unsigned char *p
= data
[y2
] + (3 * x1
);
3314 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3318 // weights for the weighted average are proportional to the inverse of the distance
3319 unsigned char *v1
= data
[y1
] + (3 * x1
);
3320 unsigned char *v2
= data
[y1
] + (3 * x2
);
3321 unsigned char *v3
= data
[y2
] + (3 * x2
);
3322 unsigned char *v4
= data
[y2
] + (3 * x1
);
3324 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3328 *(dst
++) = (unsigned char)
3329 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3330 w3
* *(v3
++) + w4
* *(v4
++)) /
3331 (w1
+ w2
+ w3
+ w4
) );
3332 *(dst
++) = (unsigned char)
3333 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3334 w3
* *(v3
++) + w4
* *(v4
++)) /
3335 (w1
+ w2
+ w3
+ w4
) );
3336 *(dst
++) = (unsigned char)
3337 ( (w1
* *v1
+ w2
* *v2
+
3338 w3
* *v3
+ w4
* *v4
) /
3339 (w1
+ w2
+ w3
+ w4
) );
3343 v1
= alpha
[y1
] + (x1
);
3344 v2
= alpha
[y1
] + (x2
);
3345 v3
= alpha
[y2
] + (x2
);
3346 v4
= alpha
[y2
] + (x1
);
3348 *(alpha_dst
++) = (unsigned char)
3349 ( (w1
* *v1
+ w2
* *v2
+
3350 w3
* *v3
+ w4
* *v4
) /
3351 (w1
+ w2
+ w3
+ w4
) );
3367 else // not interpolating
3369 for (int y
= 0; y
< rH
; y
++)
3371 for (int x
= 0; x
< rW
; x
++)
3373 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3375 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3376 const int ys
= wxRound (src
.y
); // closest integer
3378 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3380 unsigned char *p
= data
[ys
] + (3 * xs
);
3386 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3395 *(alpha_dst
++) = 255;
3411 // A module to allow wxImage initialization/cleanup
3412 // without calling these functions from app.cpp or from
3413 // the user's application.
3415 class wxImageModule
: public wxModule
3417 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3420 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3421 void OnExit() { wxImage::CleanUpHandlers(); }
3424 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3427 #endif // wxUSE_IMAGE