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/wfstream.h"
33 #include "wx/xpmdecod.h"
38 // make the code compile with either wxFile*Stream or wxFFile*Stream:
39 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
43 typedef wxFFileInputStream wxImageFileInputStream
;
44 typedef wxFFileOutputStream wxImageFileOutputStream
;
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
48 #endif // wxUSE_FILE/wxUSE_FFILE
49 #endif // HAS_FILE_STREAMS
52 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 wxList
wxImage::sm_handlers
;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 class wxImageRefData
: public wxObjectRefData
70 virtual ~wxImageRefData();
75 unsigned char *m_data
;
78 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
80 // alpha channel data, may be NULL for the formats without alpha support
81 unsigned char *m_alpha
;
85 // if true, m_data is pointer to static data and shouldn't be freed
88 // same as m_static but for m_alpha
93 #endif // wxUSE_PALETTE
95 wxArrayString m_optionNames
;
96 wxArrayString m_optionValues
;
98 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
101 wxImageRefData::wxImageRefData()
105 m_type
= wxBITMAP_TYPE_INVALID
;
107 m_alpha
= (unsigned char *) NULL
;
116 m_staticAlpha
= false;
119 wxImageRefData::~wxImageRefData()
123 if ( !m_staticAlpha
)
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
134 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
136 bool wxImage::Create(const char* const* xpmData
)
141 wxXPMDecoder decoder
;
142 (*this) = decoder
.ReadData(xpmData
);
149 bool wxImage::Create( int width
, int height
, bool clear
)
153 m_refData
= new wxImageRefData();
155 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
156 if (!M_IMGDATA
->m_data
)
162 M_IMGDATA
->m_width
= width
;
163 M_IMGDATA
->m_height
= height
;
164 M_IMGDATA
->m_ok
= true;
174 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
178 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
180 m_refData
= new wxImageRefData();
182 M_IMGDATA
->m_data
= data
;
183 M_IMGDATA
->m_width
= width
;
184 M_IMGDATA
->m_height
= height
;
185 M_IMGDATA
->m_ok
= true;
186 M_IMGDATA
->m_static
= static_data
;
191 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
195 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
197 m_refData
= new wxImageRefData();
199 M_IMGDATA
->m_data
= data
;
200 M_IMGDATA
->m_alpha
= alpha
;
201 M_IMGDATA
->m_width
= width
;
202 M_IMGDATA
->m_height
= height
;
203 M_IMGDATA
->m_ok
= true;
204 M_IMGDATA
->m_static
= static_data
;
205 M_IMGDATA
->m_staticAlpha
= static_data
;
210 void wxImage::Destroy()
215 void wxImage::Clear(unsigned char value
)
217 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
220 wxObjectRefData
* wxImage::CreateRefData() const
222 return new wxImageRefData
;
225 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
227 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
228 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
230 wxImageRefData
* refData_new
= new wxImageRefData
;
231 refData_new
->m_width
= refData
->m_width
;
232 refData_new
->m_height
= refData
->m_height
;
233 refData_new
->m_maskRed
= refData
->m_maskRed
;
234 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
235 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
236 refData_new
->m_hasMask
= refData
->m_hasMask
;
237 refData_new
->m_ok
= true;
238 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
239 if (refData
->m_alpha
!= NULL
)
241 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
242 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
245 refData_new
->m_data
= (unsigned char*)malloc(size
);
246 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
248 refData_new
->m_palette
= refData
->m_palette
;
250 refData_new
->m_optionNames
= refData
->m_optionNames
;
251 refData_new
->m_optionValues
= refData
->m_optionValues
;
255 // returns a new image with the same dimensions, alpha, and mask as *this
256 // if on_its_side is true, width and height are swapped
257 wxImage
wxImage::MakeEmptyClone(int flags
) const
261 wxCHECK_MSG( Ok(), image
, wxS("invalid image") );
263 long height
= M_IMGDATA
->m_height
;
264 long width
= M_IMGDATA
->m_width
;
266 if ( flags
& Clone_SwapOrientation
)
267 wxSwap( width
, height
);
269 if ( !image
.Create( width
, height
, false ) )
271 wxFAIL_MSG( wxS("unable to create image") );
275 if ( M_IMGDATA
->m_alpha
)
278 wxCHECK2_MSG( image
.GetAlpha(), return wxImage(),
279 wxS("unable to create alpha channel") );
282 if ( M_IMGDATA
->m_hasMask
)
284 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
285 M_IMGDATA
->m_maskGreen
,
286 M_IMGDATA
->m_maskBlue
);
292 wxImage
wxImage::Copy() const
296 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
298 image
.m_refData
= CloneRefData(m_refData
);
303 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
305 if( xFactor
== 1 && yFactor
== 1 )
310 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
312 // can't scale to/from 0 size
313 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
314 wxT("invalid new image size") );
316 long old_height
= M_IMGDATA
->m_height
,
317 old_width
= M_IMGDATA
->m_width
;
319 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
320 wxT("invalid old image size") );
322 long width
= old_width
/ xFactor
;
323 long height
= old_height
/ yFactor
;
325 image
.Create( width
, height
, false );
327 char unsigned *data
= image
.GetData();
329 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
331 bool hasMask
= false ;
332 unsigned char maskRed
= 0;
333 unsigned char maskGreen
= 0;
334 unsigned char maskBlue
= 0 ;
336 const unsigned char *source_data
= M_IMGDATA
->m_data
;
337 unsigned char *target_data
= data
;
338 const unsigned char *source_alpha
= 0 ;
339 unsigned char *target_alpha
= 0 ;
340 if (M_IMGDATA
->m_hasMask
)
343 maskRed
= M_IMGDATA
->m_maskRed
;
344 maskGreen
= M_IMGDATA
->m_maskGreen
;
345 maskBlue
=M_IMGDATA
->m_maskBlue
;
347 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
348 M_IMGDATA
->m_maskGreen
,
349 M_IMGDATA
->m_maskBlue
);
353 source_alpha
= M_IMGDATA
->m_alpha
;
357 target_alpha
= image
.GetAlpha() ;
361 for (long y
= 0; y
< height
; y
++)
363 for (long x
= 0; x
< width
; x
++)
365 unsigned long avgRed
= 0 ;
366 unsigned long avgGreen
= 0;
367 unsigned long avgBlue
= 0;
368 unsigned long avgAlpha
= 0 ;
369 unsigned long counter
= 0 ;
371 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
373 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
374 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
376 const unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
377 unsigned char red
= pixel
[0] ;
378 unsigned char green
= pixel
[1] ;
379 unsigned char blue
= pixel
[2] ;
380 unsigned char alpha
= 255 ;
382 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
383 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
398 *(target_data
++) = M_IMGDATA
->m_maskRed
;
399 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
400 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
405 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
406 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
407 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
408 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
413 // In case this is a cursor, make sure the hotspot is scaled accordingly:
414 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
415 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
416 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
417 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
418 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
419 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
425 wxImage::Scale( int width
, int height
, wxImageResizeQuality quality
) const
429 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
431 // can't scale to/from 0 size
432 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
433 wxT("invalid new image size") );
435 long old_height
= M_IMGDATA
->m_height
,
436 old_width
= M_IMGDATA
->m_width
;
437 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
438 wxT("invalid old image size") );
440 // If the image's new width and height are the same as the original, no
441 // need to waste time or CPU cycles
442 if ( old_width
== width
&& old_height
== height
)
445 // resample the image using either the nearest neighbourhood, bilinear or
446 // bicubic method as specified
449 case wxIMAGE_QUALITY_BICUBIC
:
450 case wxIMAGE_QUALITY_BILINEAR
:
451 // both of these algorithms should be used for up-sampling the
452 // image only, when down-sampling always use box averaging for best
454 if ( width
< old_width
&& height
< old_height
)
455 image
= ResampleBox(width
, height
);
456 else if ( quality
== wxIMAGE_QUALITY_BILINEAR
)
457 image
= ResampleBilinear(width
, height
);
458 else if ( quality
== wxIMAGE_QUALITY_BICUBIC
)
459 image
= ResampleBicubic(width
, height
);
462 case wxIMAGE_QUALITY_NEAREST
:
463 if ( old_width
% width
== 0 && old_width
>= width
&&
464 old_height
% height
== 0 && old_height
>= height
)
466 return ShrinkBy( old_width
/ width
, old_height
/ height
);
469 image
= ResampleNearest(width
, height
);
473 // If the original image has a mask, apply the mask to the new image
474 if (M_IMGDATA
->m_hasMask
)
476 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
477 M_IMGDATA
->m_maskGreen
,
478 M_IMGDATA
->m_maskBlue
);
481 // In case this is a cursor, make sure the hotspot is scaled accordingly:
482 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
483 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
484 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
485 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
486 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
487 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
492 wxImage
wxImage::ResampleNearest(int width
, int height
) const
495 image
.Create( width
, height
, false );
497 unsigned char *data
= image
.GetData();
499 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
501 const unsigned char *source_data
= M_IMGDATA
->m_data
;
502 unsigned char *target_data
= data
;
503 const unsigned char *source_alpha
= 0 ;
504 unsigned char *target_alpha
= 0 ;
506 if ( !M_IMGDATA
->m_hasMask
)
508 source_alpha
= M_IMGDATA
->m_alpha
;
512 target_alpha
= image
.GetAlpha() ;
516 long old_height
= M_IMGDATA
->m_height
,
517 old_width
= M_IMGDATA
->m_width
;
518 long x_delta
= (old_width
<<16) / width
;
519 long y_delta
= (old_height
<<16) / height
;
521 unsigned char* dest_pixel
= target_data
;
524 for ( long j
= 0; j
< height
; j
++ )
526 const unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
527 const unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
530 for ( long i
= 0; i
< width
; i
++ )
532 const unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
533 const unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
534 dest_pixel
[0] = src_pixel
[0];
535 dest_pixel
[1] = src_pixel
[1];
536 dest_pixel
[2] = src_pixel
[2];
539 *(target_alpha
++) = *src_alpha_pixel
;
549 wxImage
wxImage::ResampleBox(int width
, int height
) const
551 // This function implements a simple pre-blur/box averaging method for
552 // downsampling that gives reasonably smooth results To scale the image
553 // down we will need to gather a grid of pixels of the size of the scale
554 // factor in each direction and then do an averaging of the pixels.
556 wxImage
ret_image(width
, height
, false);
558 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
559 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
561 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
562 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
564 const unsigned char* src_data
= M_IMGDATA
->m_data
;
565 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
566 unsigned char* dst_data
= ret_image
.GetData();
567 unsigned char* dst_alpha
= NULL
;
571 ret_image
.SetAlpha();
572 dst_alpha
= ret_image
.GetAlpha();
575 int averaged_pixels
, src_pixel_index
;
576 double sum_r
, sum_g
, sum_b
, sum_a
;
578 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
580 // Source pixel in the Y direction
581 int src_y
= (int)(y
* scale_factor_y
);
583 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
585 // Source pixel in the X direction
586 int src_x
= (int)(x
* scale_factor_x
);
588 // Box of pixels to average
590 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
592 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
593 j
<= int(src_y
+ scale_factor_y_2
);
596 // We don't care to average pixels that don't exist (edges)
597 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
600 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
601 i
<= src_x
+ scale_factor_x_2
;
604 // Don't average edge pixels
605 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
608 // Calculate the actual index in our source pixels
609 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
611 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
612 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
613 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
615 sum_a
+= src_alpha
[src_pixel_index
];
621 // Calculate the average from the sum and number of averaged pixels
622 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
623 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
624 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
627 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
634 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
636 // This function implements a Bilinear algorithm for resampling.
637 wxImage
ret_image(width
, height
, false);
638 const unsigned char* src_data
= M_IMGDATA
->m_data
;
639 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
640 unsigned char* dst_data
= ret_image
.GetData();
641 unsigned char* dst_alpha
= NULL
;
645 ret_image
.SetAlpha();
646 dst_alpha
= ret_image
.GetAlpha();
648 double HFactor
= double(M_IMGDATA
->m_height
) / height
;
649 double WFactor
= double(M_IMGDATA
->m_width
) / width
;
651 int srcpixymax
= M_IMGDATA
->m_height
- 1;
652 int srcpixxmax
= M_IMGDATA
->m_width
- 1;
654 double srcpixy
, srcpixy1
, srcpixy2
, dy
, dy1
;
655 double srcpixx
, srcpixx1
, srcpixx2
, dx
, dx1
;
657 // initialize alpha values to avoid g++ warnings about possibly
658 // uninitialized variables
659 double r1
, g1
, b1
, a1
= 0;
660 double r2
, g2
, b2
, a2
= 0;
662 for ( int dsty
= 0; dsty
< height
; dsty
++ )
664 // We need to calculate the source pixel to interpolate from - Y-axis
665 srcpixy
= double(dsty
) * HFactor
;
666 srcpixy1
= int(srcpixy
);
667 srcpixy2
= ( srcpixy1
== srcpixymax
) ? srcpixy1
: srcpixy1
+ 1.0;
668 dy
= srcpixy
- (int)srcpixy
;
672 for ( int dstx
= 0; dstx
< width
; dstx
++ )
674 // X-axis of pixel to interpolate from
675 srcpixx
= double(dstx
) * WFactor
;
676 srcpixx1
= int(srcpixx
);
677 srcpixx2
= ( srcpixx1
== srcpixxmax
) ? srcpixx1
: srcpixx1
+ 1.0;
678 dx
= srcpixx
- (int)srcpixx
;
681 int x_offset1
= srcpixx1
< 0.0 ? 0 : srcpixx1
> srcpixxmax
? srcpixxmax
: (int)srcpixx1
;
682 int x_offset2
= srcpixx2
< 0.0 ? 0 : srcpixx2
> srcpixxmax
? srcpixxmax
: (int)srcpixx2
;
683 int y_offset1
= srcpixy1
< 0.0 ? 0 : srcpixy1
> srcpixymax
? srcpixymax
: (int)srcpixy1
;
684 int y_offset2
= srcpixy2
< 0.0 ? 0 : srcpixy2
> srcpixymax
? srcpixymax
: (int)srcpixy2
;
686 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
687 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
688 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
689 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
692 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
693 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
694 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
696 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
699 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
700 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
701 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
703 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
707 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
708 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
709 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
713 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
720 // The following two local functions are for the B-spline weighting of the
721 // bicubic sampling algorithm
722 static inline double spline_cube(double value
)
724 return value
<= 0.0 ? 0.0 : value
* value
* value
;
727 static inline double spline_weight(double value
)
729 return (spline_cube(value
+ 2) -
730 4 * spline_cube(value
+ 1) +
731 6 * spline_cube(value
) -
732 4 * spline_cube(value
- 1)) / 6;
735 // This is the bicubic resampling algorithm
736 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
738 // This function implements a Bicubic B-Spline algorithm for resampling.
739 // This method is certainly a little slower than wxImage's default pixel
740 // replication method, however for most reasonably sized images not being
741 // upsampled too much on a fairly average CPU this difference is hardly
742 // noticeable and the results are far more pleasing to look at.
744 // This particular bicubic algorithm does pixel weighting according to a
745 // B-Spline that basically implements a Gaussian bell-like weighting
746 // kernel. Because of this method the results may appear a bit blurry when
747 // upsampling by large factors. This is basically because a slight
748 // gaussian blur is being performed to get the smooth look of the upsampled
751 // Edge pixels: 3-4 possible solutions
752 // - (Wrap/tile) Wrap the image, take the color value from the opposite
753 // side of the image.
754 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
755 // where n is nonpositive, will have the value of (2, 1).
756 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
757 // pixels which do have all neighbours.
758 // - (Clamp) Choose the nearest pixel along the border. This takes the
759 // border pixels and extends them out to infinity.
761 // NOTE: below the y_offset and x_offset variables are being set for edge
762 // pixels using the "Mirror" method mentioned above
766 ret_image
.Create(width
, height
, false);
768 const unsigned char* src_data
= M_IMGDATA
->m_data
;
769 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
770 unsigned char* dst_data
= ret_image
.GetData();
771 unsigned char* dst_alpha
= NULL
;
775 ret_image
.SetAlpha();
776 dst_alpha
= ret_image
.GetAlpha();
779 for ( int dsty
= 0; dsty
< height
; dsty
++ )
781 // We need to calculate the source pixel to interpolate from - Y-axis
782 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
783 double dy
= srcpixy
- (int)srcpixy
;
785 for ( int dstx
= 0; dstx
< width
; dstx
++ )
787 // X-axis of pixel to interpolate from
788 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
789 double dx
= srcpixx
- (int)srcpixx
;
791 // Sums for each color channel
792 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
794 // Here we actually determine the RGBA values for the destination pixel
795 for ( int k
= -1; k
<= 2; k
++ )
798 int y_offset
= srcpixy
+ k
< 0.0
800 : srcpixy
+ k
>= M_IMGDATA
->m_height
801 ? M_IMGDATA
->m_height
- 1
802 : (int)(srcpixy
+ k
);
804 // Loop across the X axis
805 for ( int i
= -1; i
<= 2; i
++ )
808 int x_offset
= srcpixx
+ i
< 0.0
810 : srcpixx
+ i
>= M_IMGDATA
->m_width
811 ? M_IMGDATA
->m_width
- 1
812 : (int)(srcpixx
+ i
);
814 // Calculate the exact position where the source data
815 // should be pulled from based on the x_offset and y_offset
816 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
818 // Calculate the weight for the specified pixel according
819 // to the bicubic b-spline kernel we're using for
822 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
824 // Create a sum of all velues for each color channel
825 // adjusted for the pixel's calculated weight
826 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
827 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
828 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
830 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
834 // Put the data into the destination image. The summed values are
835 // of double data type and are rounded here for accuracy
836 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
837 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
838 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
842 *dst_alpha
++ = (unsigned char)sum_a
;
849 // Blur in the horizontal direction
850 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
852 wxImage
ret_image(MakeEmptyClone());
854 wxCHECK( ret_image
.Ok(), ret_image
);
856 const unsigned char* src_data
= M_IMGDATA
->m_data
;
857 unsigned char* dst_data
= ret_image
.GetData();
858 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
859 unsigned char* dst_alpha
= ret_image
.GetAlpha();
861 // number of pixels we average over
862 const int blurArea
= blurRadius
*2 + 1;
864 // Horizontal blurring algorithm - average all pixels in the specified blur
865 // radius in the X or horizontal direction
866 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
868 // Variables used in the blurring algorithm
875 const unsigned char *src
;
878 // Calculate the average of all pixels in the blur radius for the first
880 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
882 // To deal with the pixels at the start of a row so it's not
883 // grabbing GOK values from memory at negative indices of the
884 // image's data or grabbing from the previous row
886 pixel_idx
= y
* M_IMGDATA
->m_width
;
888 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
890 src
= src_data
+ pixel_idx
*3;
895 sum_a
+= src_alpha
[pixel_idx
];
898 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
899 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
900 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
901 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
903 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
905 // Now average the values of the rest of the pixels by just moving the
906 // blur radius box along the row
907 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
909 // Take care of edge pixels on the left edge by essentially
910 // duplicating the edge pixel
911 if ( x
- blurRadius
- 1 < 0 )
912 pixel_idx
= y
* M_IMGDATA
->m_width
;
914 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
916 // Subtract the value of the pixel at the left side of the blur
918 src
= src_data
+ pixel_idx
*3;
923 sum_a
-= src_alpha
[pixel_idx
];
925 // Take care of edge pixels on the right edge
926 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
927 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
929 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
931 // Add the value of the pixel being added to the end of our box
932 src
= src_data
+ pixel_idx
*3;
937 sum_a
+= src_alpha
[pixel_idx
];
939 // Save off the averaged data
940 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
941 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
942 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
943 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
945 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
952 // Blur in the vertical direction
953 wxImage
wxImage::BlurVertical(int blurRadius
) const
955 wxImage
ret_image(MakeEmptyClone());
957 wxCHECK( ret_image
.Ok(), ret_image
);
959 const unsigned char* src_data
= M_IMGDATA
->m_data
;
960 unsigned char* dst_data
= ret_image
.GetData();
961 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
962 unsigned char* dst_alpha
= ret_image
.GetAlpha();
964 // number of pixels we average over
965 const int blurArea
= blurRadius
*2 + 1;
967 // Vertical blurring algorithm - same as horizontal but switched the
968 // opposite direction
969 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
971 // Variables used in the blurring algorithm
978 const unsigned char *src
;
981 // Calculate the average of all pixels in our blur radius box for the
982 // first pixel of the column
983 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
985 // To deal with the pixels at the start of a column so it's not
986 // grabbing GOK values from memory at negative indices of the
987 // image's data or grabbing from the previous column
991 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
993 src
= src_data
+ pixel_idx
*3;
998 sum_a
+= src_alpha
[pixel_idx
];
1001 dst
= dst_data
+ x
*3;
1002 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1003 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1004 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1006 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
1008 // Now average the values of the rest of the pixels by just moving the
1009 // box along the column from top to bottom
1010 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1012 // Take care of pixels that would be beyond the top edge by
1013 // duplicating the top edge pixel for the column
1014 if ( y
- blurRadius
- 1 < 0 )
1017 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1019 // Subtract the value of the pixel at the top of our blur radius box
1020 src
= src_data
+ pixel_idx
*3;
1025 sum_a
-= src_alpha
[pixel_idx
];
1027 // Take care of the pixels that would be beyond the bottom edge of
1028 // the image similar to the top edge
1029 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1030 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1032 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1034 // Add the value of the pixel being added to the end of our box
1035 src
= src_data
+ pixel_idx
*3;
1040 sum_a
+= src_alpha
[pixel_idx
];
1042 // Save off the averaged data
1043 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1044 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1045 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1046 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1048 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1055 // The new blur function
1056 wxImage
wxImage::Blur(int blurRadius
) const
1059 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1061 // Blur the image in each direction
1062 ret_image
= BlurHorizontal(blurRadius
);
1063 ret_image
= ret_image
.BlurVertical(blurRadius
);
1068 wxImage
wxImage::Rotate90( bool clockwise
) const
1070 wxImage
image(MakeEmptyClone(Clone_SwapOrientation
));
1072 wxCHECK( image
.Ok(), image
);
1074 long height
= M_IMGDATA
->m_height
;
1075 long width
= M_IMGDATA
->m_width
;
1077 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1079 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1080 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1081 clockwise
? hot_x
: width
- 1 - hot_x
);
1084 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1086 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1087 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1088 clockwise
? height
- 1 - hot_y
: hot_y
);
1091 unsigned char *data
= image
.GetData();
1092 unsigned char *target_data
;
1094 // we rotate the image in 21-pixel (63-byte) wide strips
1095 // to make better use of cpu cache - memory transfers
1096 // (note: while much better than single-pixel "strips",
1097 // our vertical strips will still generally straddle cachelines)
1098 for (long ii
= 0; ii
< width
; )
1100 long next_ii
= wxMin(ii
+ 21, width
);
1102 for (long j
= 0; j
< height
; j
++)
1104 const unsigned char *source_data
1105 = M_IMGDATA
->m_data
+ (j
*width
+ ii
)*3;
1107 for (long i
= ii
; i
< next_ii
; i
++)
1111 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1115 target_data
= data
+ ((height
*(width
- 1 - i
)) + j
)*3;
1117 memcpy( target_data
, source_data
, 3 );
1125 const unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1129 unsigned char *alpha_data
= image
.GetAlpha();
1130 unsigned char *target_alpha
= 0 ;
1132 for (long j
= 0; j
< height
; j
++)
1134 for (long i
= 0; i
< width
; i
++)
1138 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1142 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1145 *target_alpha
= *source_alpha
++;
1153 wxImage
wxImage::Rotate180() const
1155 wxImage
image(MakeEmptyClone());
1157 wxCHECK( image
.Ok(), image
);
1159 long height
= M_IMGDATA
->m_height
;
1160 long width
= M_IMGDATA
->m_width
;
1162 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1164 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1165 width
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
));
1168 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1170 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1171 height
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
));
1174 unsigned char *data
= image
.GetData();
1175 unsigned char *alpha
= image
.GetAlpha();
1176 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1177 unsigned char *target_data
= data
+ width
* height
* 3;
1179 for (long j
= 0; j
< height
; j
++)
1181 for (long i
= 0; i
< width
; i
++)
1184 memcpy( target_data
, source_data
, 3 );
1191 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1192 unsigned char *dest_alpha
= alpha
+ width
* height
;
1194 for (long j
= 0; j
< height
; ++j
)
1196 for (long i
= 0; i
< width
; ++i
)
1198 *(--dest_alpha
) = *(src_alpha
++);
1206 wxImage
wxImage::Mirror( bool horizontally
) const
1208 wxImage
image(MakeEmptyClone());
1210 wxCHECK( image
.Ok(), image
);
1212 long height
= M_IMGDATA
->m_height
;
1213 long width
= M_IMGDATA
->m_width
;
1215 unsigned char *data
= image
.GetData();
1216 unsigned char *alpha
= image
.GetAlpha();
1217 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1218 unsigned char *target_data
;
1222 for (long j
= 0; j
< height
; j
++)
1225 target_data
= data
-3;
1226 for (long i
= 0; i
< width
; i
++)
1228 memcpy( target_data
, source_data
, 3 );
1236 // src_alpha starts at the first pixel and increases by 1 after each step
1237 // (a step here is the copy of the alpha value of one pixel)
1238 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1239 // dest_alpha starts just beyond the first line, decreases before each step,
1240 // and after each line is finished, increases by 2 widths (skipping the line
1241 // just copied and the line that will be copied next)
1242 unsigned char *dest_alpha
= alpha
+ width
;
1244 for (long jj
= 0; jj
< height
; ++jj
)
1246 for (long i
= 0; i
< width
; ++i
) {
1247 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1249 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1255 for (long i
= 0; i
< height
; i
++)
1257 target_data
= data
+ 3*width
*(height
-1-i
);
1258 memcpy( target_data
, source_data
, (size_t)3*width
);
1259 source_data
+= 3*width
;
1264 // src_alpha starts at the first pixel and increases by 1 width after each step
1265 // (a step here is the copy of the alpha channel of an entire line)
1266 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1267 // dest_alpha starts just beyond the last line (beyond the whole image)
1268 // and decreases by 1 width before each step
1269 unsigned char *dest_alpha
= alpha
+ width
* height
;
1271 for (long jj
= 0; jj
< height
; ++jj
)
1273 dest_alpha
-= width
;
1274 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1283 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1287 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1289 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1290 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1291 image
, wxT("invalid subimage size") );
1293 const int subwidth
= rect
.GetWidth();
1294 const int subheight
= rect
.GetHeight();
1296 image
.Create( subwidth
, subheight
, false );
1298 const unsigned char *src_data
= GetData();
1299 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1300 unsigned char *subdata
= image
.GetData();
1301 unsigned char *subalpha
= NULL
;
1303 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1307 subalpha
= image
.GetAlpha();
1308 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1311 if (M_IMGDATA
->m_hasMask
)
1312 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1314 const int width
= GetWidth();
1315 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1317 src_data
+= 3 * pixsoff
;
1318 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1320 for (long j
= 0; j
< subheight
; ++j
)
1322 memcpy( subdata
, src_data
, 3 * subwidth
);
1323 subdata
+= 3 * subwidth
;
1324 src_data
+= 3 * width
;
1325 if (subalpha
!= NULL
) {
1326 memcpy( subalpha
, src_alpha
, subwidth
);
1327 subalpha
+= subwidth
;
1335 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1336 int r_
, int g_
, int b_
) const
1340 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1341 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1343 int width
= GetWidth(), height
= GetHeight();
1344 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1346 unsigned char r
= (unsigned char)r_
;
1347 unsigned char g
= (unsigned char)g_
;
1348 unsigned char b
= (unsigned char)b_
;
1349 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1351 GetOrFindMaskColour( &r
, &g
, &b
);
1352 image
.SetMaskColour(r
, g
, b
);
1355 image
.SetRGB(wxRect(), r
, g
, b
);
1357 // we have two coordinate systems:
1358 // source: starting at 0,0 of source image
1359 // destination starting at 0,0 of destination image
1360 // Documentation says:
1361 // "The image is pasted into a new image [...] at the position pos relative
1362 // to the upper left of the new image." this means the transition rule is:
1363 // "dest coord" = "source coord" + pos;
1365 // calculate the intersection using source coordinates:
1366 wxRect
srcRect(0, 0, width
, height
);
1367 wxRect
dstRect(-pos
, size
);
1369 srcRect
.Intersect(dstRect
);
1371 if (!srcRect
.IsEmpty())
1373 // insertion point is needed in destination coordinates.
1374 // NB: it is not always "pos"!
1375 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1377 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1378 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1380 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1386 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1388 wxCHECK_RET( Ok(), wxT("invalid image") );
1389 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1395 int width
= image
.GetWidth();
1396 int height
= image
.GetHeight();
1409 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1410 width
= M_IMGDATA
->m_width
- (x
+xx
);
1411 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1412 height
= M_IMGDATA
->m_height
- (y
+yy
);
1414 if (width
< 1) return;
1415 if (height
< 1) return;
1417 if ((!HasMask() && !image
.HasMask()) ||
1418 (HasMask() && !image
.HasMask()) ||
1419 ((HasMask() && image
.HasMask() &&
1420 (GetMaskRed()==image
.GetMaskRed()) &&
1421 (GetMaskGreen()==image
.GetMaskGreen()) &&
1422 (GetMaskBlue()==image
.GetMaskBlue()))))
1424 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1425 int source_step
= image
.GetWidth()*3;
1427 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1428 int target_step
= M_IMGDATA
->m_width
*3;
1429 for (int j
= 0; j
< height
; j
++)
1431 memcpy( target_data
, source_data
, width
*3 );
1432 source_data
+= source_step
;
1433 target_data
+= target_step
;
1437 // Copy over the alpha channel from the original image
1438 if ( image
.HasAlpha() )
1443 const unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1444 int source_step
= image
.GetWidth();
1446 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1447 int target_step
= M_IMGDATA
->m_width
;
1449 for (int j
= 0; j
< height
; j
++,
1450 source_data
+= source_step
,
1451 target_data
+= target_step
)
1453 memcpy( target_data
, source_data
, width
);
1457 if (!HasMask() && image
.HasMask())
1459 unsigned char r
= image
.GetMaskRed();
1460 unsigned char g
= image
.GetMaskGreen();
1461 unsigned char b
= image
.GetMaskBlue();
1463 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1464 int source_step
= image
.GetWidth()*3;
1466 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1467 int target_step
= M_IMGDATA
->m_width
*3;
1469 for (int j
= 0; j
< height
; j
++)
1471 for (int i
= 0; i
< width
*3; i
+=3)
1473 if ((source_data
[i
] != r
) ||
1474 (source_data
[i
+1] != g
) ||
1475 (source_data
[i
+2] != b
))
1477 memcpy( target_data
+i
, source_data
+i
, 3 );
1480 source_data
+= source_step
;
1481 target_data
+= target_step
;
1486 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1487 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1489 wxCHECK_RET( Ok(), wxT("invalid image") );
1493 unsigned char *data
= GetData();
1495 const int w
= GetWidth();
1496 const int h
= GetHeight();
1498 for (int j
= 0; j
< h
; j
++)
1499 for (int i
= 0; i
< w
; i
++)
1501 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1511 wxImage
wxImage::ConvertToGreyscale(void) const
1513 return ConvertToGreyscale(0.299, 0.587, 0.114);
1516 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1518 wxImage
image(MakeEmptyClone());
1520 wxCHECK( image
.Ok(), image
);
1522 const unsigned char *src
= M_IMGDATA
->m_data
;
1523 unsigned char *dest
= image
.GetData();
1525 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1526 const unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1527 const unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1528 const unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1530 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1531 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1533 memcpy(dest
, src
, 3);
1534 // only modify non-masked pixels
1535 if ( !hasMask
|| src
[0] != maskRed
|| src
[1] != maskGreen
|| src
[2] != maskBlue
)
1537 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1541 // copy the alpha channel, if any
1542 if ( image
.HasAlpha() )
1544 memcpy( image
.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1550 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1554 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1556 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1558 unsigned char *data
= image
.GetData();
1560 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1562 if (M_IMGDATA
->m_hasMask
)
1564 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1565 M_IMGDATA
->m_maskBlue
== b
)
1566 image
.SetMaskColour( 255, 255, 255 );
1568 image
.SetMaskColour( 0, 0, 0 );
1571 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1573 unsigned char *srcd
= M_IMGDATA
->m_data
;
1574 unsigned char *tard
= image
.GetData();
1576 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1578 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1579 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1585 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1587 wxImage image
= *this;
1589 unsigned char mr
= image
.GetMaskRed();
1590 unsigned char mg
= image
.GetMaskGreen();
1591 unsigned char mb
= image
.GetMaskBlue();
1593 int width
= image
.GetWidth();
1594 int height
= image
.GetHeight();
1595 bool has_mask
= image
.HasMask();
1597 for (int y
= height
-1; y
>= 0; --y
)
1599 for (int x
= width
-1; x
>= 0; --x
)
1601 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1602 unsigned char* r
= data
;
1603 unsigned char* g
= data
+1;
1604 unsigned char* b
= data
+2;
1606 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1609 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1615 int wxImage::GetWidth() const
1617 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1619 return M_IMGDATA
->m_width
;
1622 int wxImage::GetHeight() const
1624 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1626 return M_IMGDATA
->m_height
;
1629 wxBitmapType
wxImage::GetType() const
1631 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1633 return M_IMGDATA
->m_type
;
1636 void wxImage::SetType(wxBitmapType type
)
1638 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1640 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1641 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1643 M_IMGDATA
->m_type
= type
;
1646 long wxImage::XYToIndex(int x
, int y
) const
1650 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1652 return y
*M_IMGDATA
->m_width
+ x
;
1658 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1660 long pos
= XYToIndex(x
, y
);
1661 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1667 M_IMGDATA
->m_data
[ pos
] = r
;
1668 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1669 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1672 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1674 wxCHECK_RET( Ok(), wxT("invalid image") );
1679 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1680 if ( rect
== wxRect() )
1686 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1687 imageRect
.Contains(rect
.GetBottomRight()),
1688 wxT("invalid bounding rectangle") );
1691 int x1
= rect
.GetLeft(),
1693 x2
= rect
.GetRight() + 1,
1694 y2
= rect
.GetBottom() + 1;
1696 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1697 int x
, y
, width
= GetWidth();
1698 for (y
= y1
; y
< y2
; y
++)
1700 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1701 for (x
= x1
; x
< x2
; x
++)
1710 unsigned char wxImage::GetRed( int x
, int y
) const
1712 long pos
= XYToIndex(x
, y
);
1713 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1717 return M_IMGDATA
->m_data
[pos
];
1720 unsigned char wxImage::GetGreen( int x
, int y
) const
1722 long pos
= XYToIndex(x
, y
);
1723 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1727 return M_IMGDATA
->m_data
[pos
+1];
1730 unsigned char wxImage::GetBlue( int x
, int y
) const
1732 long pos
= XYToIndex(x
, y
);
1733 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1737 return M_IMGDATA
->m_data
[pos
+2];
1740 bool wxImage::IsOk() const
1742 // image of 0 width or height can't be considered ok - at least because it
1743 // causes crashes in ConvertToBitmap() if we don't catch it in time
1744 wxImageRefData
*data
= M_IMGDATA
;
1745 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1748 unsigned char *wxImage::GetData() const
1750 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1752 return M_IMGDATA
->m_data
;
1755 void wxImage::SetData( unsigned char *data
, bool static_data
)
1757 wxCHECK_RET( Ok(), wxT("invalid image") );
1759 wxImageRefData
*newRefData
= new wxImageRefData();
1761 newRefData
->m_width
= M_IMGDATA
->m_width
;
1762 newRefData
->m_height
= M_IMGDATA
->m_height
;
1763 newRefData
->m_data
= data
;
1764 newRefData
->m_ok
= true;
1765 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1766 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1767 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1768 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1769 newRefData
->m_static
= static_data
;
1773 m_refData
= newRefData
;
1776 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1778 wxImageRefData
*newRefData
= new wxImageRefData();
1782 newRefData
->m_width
= new_width
;
1783 newRefData
->m_height
= new_height
;
1784 newRefData
->m_data
= data
;
1785 newRefData
->m_ok
= true;
1786 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1787 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1788 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1789 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1793 newRefData
->m_width
= new_width
;
1794 newRefData
->m_height
= new_height
;
1795 newRefData
->m_data
= data
;
1796 newRefData
->m_ok
= true;
1798 newRefData
->m_static
= static_data
;
1802 m_refData
= newRefData
;
1805 // ----------------------------------------------------------------------------
1806 // alpha channel support
1807 // ----------------------------------------------------------------------------
1809 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1811 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1813 long pos
= XYToIndex(x
, y
);
1814 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1818 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1821 unsigned char wxImage::GetAlpha(int x
, int y
) const
1823 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1825 long pos
= XYToIndex(x
, y
);
1826 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1828 return M_IMGDATA
->m_alpha
[pos
];
1832 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1836 const int w
= M_IMGDATA
->m_width
;
1837 const int h
= M_IMGDATA
->m_height
;
1839 unsigned char *alpha
= GetAlpha();
1840 unsigned char *data
= GetData();
1842 for ( int y
= 0; y
< h
; y
++ )
1844 for ( int x
= 0; x
< w
; x
++ )
1856 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1858 wxCHECK_RET( Ok(), wxT("invalid image") );
1864 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1867 if( !M_IMGDATA
->m_staticAlpha
)
1868 free(M_IMGDATA
->m_alpha
);
1870 M_IMGDATA
->m_alpha
= alpha
;
1871 M_IMGDATA
->m_staticAlpha
= static_data
;
1874 unsigned char *wxImage::GetAlpha() const
1876 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1878 return M_IMGDATA
->m_alpha
;
1881 void wxImage::InitAlpha()
1883 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1885 // initialize memory for alpha channel
1888 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1889 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1893 // use the mask to initialize the alpha channel.
1894 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1896 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1897 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1898 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1899 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1903 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1904 ? wxIMAGE_ALPHA_TRANSPARENT
1905 : wxIMAGE_ALPHA_OPAQUE
;
1908 M_IMGDATA
->m_hasMask
= false;
1912 // make the image fully opaque
1913 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1917 void wxImage::ClearAlpha()
1919 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1921 if ( !M_IMGDATA
->m_staticAlpha
)
1922 free( M_IMGDATA
->m_alpha
);
1924 M_IMGDATA
->m_alpha
= NULL
;
1928 // ----------------------------------------------------------------------------
1930 // ----------------------------------------------------------------------------
1932 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1934 wxCHECK_RET( Ok(), wxT("invalid image") );
1938 M_IMGDATA
->m_maskRed
= r
;
1939 M_IMGDATA
->m_maskGreen
= g
;
1940 M_IMGDATA
->m_maskBlue
= b
;
1941 M_IMGDATA
->m_hasMask
= true;
1944 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1946 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1948 if (M_IMGDATA
->m_hasMask
)
1950 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1951 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1952 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1957 FindFirstUnusedColour(r
, g
, b
);
1962 unsigned char wxImage::GetMaskRed() const
1964 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1966 return M_IMGDATA
->m_maskRed
;
1969 unsigned char wxImage::GetMaskGreen() const
1971 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1973 return M_IMGDATA
->m_maskGreen
;
1976 unsigned char wxImage::GetMaskBlue() const
1978 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1980 return M_IMGDATA
->m_maskBlue
;
1983 void wxImage::SetMask( bool mask
)
1985 wxCHECK_RET( Ok(), wxT("invalid image") );
1989 M_IMGDATA
->m_hasMask
= mask
;
1992 bool wxImage::HasMask() const
1994 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1996 return M_IMGDATA
->m_hasMask
;
1999 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
2001 long pos
= XYToIndex(x
, y
);
2002 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
2005 if ( M_IMGDATA
->m_hasMask
)
2007 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
2008 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
2009 p
[1] == M_IMGDATA
->m_maskGreen
&&
2010 p
[2] == M_IMGDATA
->m_maskBlue
)
2017 if ( M_IMGDATA
->m_alpha
)
2019 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2021 // transparent enough
2030 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2031 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2033 // check that the images are the same size
2034 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2036 wxLogError( _("Image and mask have different sizes.") );
2040 // find unused colour
2041 unsigned char r
,g
,b
;
2042 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2044 wxLogError( _("No unused colour in image being masked.") );
2050 unsigned char *imgdata
= GetData();
2051 unsigned char *maskdata
= mask
.GetData();
2053 const int w
= GetWidth();
2054 const int h
= GetHeight();
2056 for (int j
= 0; j
< h
; j
++)
2058 for (int i
= 0; i
< w
; i
++)
2060 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2071 SetMaskColour(r
, g
, b
);
2077 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2082 unsigned char mr
, mg
, mb
;
2083 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2085 wxLogError( _("No unused colour in image being masked.") );
2089 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2092 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2095 unsigned char threshold
)
2103 SetMaskColour(mr
, mg
, mb
);
2105 unsigned char *imgdata
= GetData();
2106 unsigned char *alphadata
= GetAlpha();
2109 int h
= GetHeight();
2111 for (int y
= 0; y
< h
; y
++)
2113 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2115 if (*alphadata
< threshold
)
2124 if ( !M_IMGDATA
->m_staticAlpha
)
2125 free(M_IMGDATA
->m_alpha
);
2127 M_IMGDATA
->m_alpha
= NULL
;
2128 M_IMGDATA
->m_staticAlpha
= false;
2133 // ----------------------------------------------------------------------------
2134 // Palette functions
2135 // ----------------------------------------------------------------------------
2139 bool wxImage::HasPalette() const
2144 return M_IMGDATA
->m_palette
.Ok();
2147 const wxPalette
& wxImage::GetPalette() const
2149 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
2151 return M_IMGDATA
->m_palette
;
2154 void wxImage::SetPalette(const wxPalette
& palette
)
2156 wxCHECK_RET( Ok(), wxT("invalid image") );
2160 M_IMGDATA
->m_palette
= palette
;
2163 #endif // wxUSE_PALETTE
2165 // ----------------------------------------------------------------------------
2166 // Option functions (arbitrary name/value mapping)
2167 // ----------------------------------------------------------------------------
2169 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2173 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2174 if ( idx
== wxNOT_FOUND
)
2176 M_IMGDATA
->m_optionNames
.Add(name
);
2177 M_IMGDATA
->m_optionValues
.Add(value
);
2181 M_IMGDATA
->m_optionNames
[idx
] = name
;
2182 M_IMGDATA
->m_optionValues
[idx
] = value
;
2186 void wxImage::SetOption(const wxString
& name
, int value
)
2189 valStr
.Printf(wxT("%d"), value
);
2190 SetOption(name
, valStr
);
2193 wxString
wxImage::GetOption(const wxString
& name
) const
2196 return wxEmptyString
;
2198 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2199 if ( idx
== wxNOT_FOUND
)
2200 return wxEmptyString
;
2202 return M_IMGDATA
->m_optionValues
[idx
];
2205 int wxImage::GetOptionInt(const wxString
& name
) const
2207 return wxAtoi(GetOption(name
));
2210 bool wxImage::HasOption(const wxString
& name
) const
2212 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2216 // ----------------------------------------------------------------------------
2218 // ----------------------------------------------------------------------------
2220 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2221 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2222 int WXUNUSED_UNLESS_STREAMS(index
) )
2224 #if HAS_FILE_STREAMS
2225 wxImageFileInputStream
stream(filename
);
2226 if ( stream
.IsOk() )
2228 wxBufferedInputStream
bstream( stream
);
2229 if ( LoadFile(bstream
, type
, index
) )
2233 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2234 #endif // HAS_FILE_STREAMS
2239 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2240 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2241 int WXUNUSED_UNLESS_STREAMS(index
) )
2243 #if HAS_FILE_STREAMS
2244 wxImageFileInputStream
stream(filename
);
2245 if ( stream
.IsOk() )
2247 wxBufferedInputStream
bstream( stream
);
2248 if ( LoadFile(bstream
, mimetype
, index
) )
2252 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2253 #endif // HAS_FILE_STREAMS
2259 bool wxImage::SaveFile( const wxString
& filename
) const
2261 wxString ext
= filename
.AfterLast('.').Lower();
2263 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2266 wxLogError(_("Can't save image to file '%s': unknown extension."),
2271 return SaveFile(filename
, handler
->GetType());
2274 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2275 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2277 #if HAS_FILE_STREAMS
2278 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2280 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2282 wxImageFileOutputStream
stream(filename
);
2284 if ( stream
.IsOk() )
2286 wxBufferedOutputStream
bstream( stream
);
2287 return SaveFile(bstream
, type
);
2289 #endif // HAS_FILE_STREAMS
2294 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2295 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2297 #if HAS_FILE_STREAMS
2298 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2300 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2302 wxImageFileOutputStream
stream(filename
);
2304 if ( stream
.IsOk() )
2306 wxBufferedOutputStream
bstream( stream
);
2307 return SaveFile(bstream
, mimetype
);
2309 #endif // HAS_FILE_STREAMS
2314 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2316 #if HAS_FILE_STREAMS
2317 wxImageFileInputStream
stream(name
);
2318 return CanRead(stream
);
2324 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2325 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2327 #if HAS_FILE_STREAMS
2328 wxImageFileInputStream
stream(name
);
2330 return GetImageCount(stream
, type
);
2338 bool wxImage::CanRead( wxInputStream
&stream
)
2340 const wxList
& list
= GetHandlers();
2342 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2344 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2345 if (handler
->CanRead( stream
))
2352 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2354 wxImageHandler
*handler
;
2356 if ( type
== wxBITMAP_TYPE_ANY
)
2358 const wxList
& list
= GetHandlers();
2360 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2362 node
= node
->GetNext() )
2364 handler
= (wxImageHandler
*)node
->GetData();
2365 if ( handler
->CanRead(stream
) )
2367 const int count
= handler
->GetImageCount(stream
);
2374 wxLogWarning(_("No handler found for image type."));
2378 handler
= FindHandler(type
);
2382 wxLogWarning(_("No image handler for type %d defined."), type
);
2386 if ( handler
->CanRead(stream
) )
2388 return handler
->GetImageCount(stream
);
2392 wxLogError(_("Image file is not of type %d."), type
);
2397 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2399 // save the options values which can be clobbered by the handler (e.g. many
2400 // of them call Destroy() before trying to load the file)
2401 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2402 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2404 // Preserve the original stream position if possible to rewind back to it
2405 // if we failed to load the file -- maybe the next handler that we try can
2406 // succeed after us then.
2407 wxFileOffset posOld
= wxInvalidOffset
;
2408 if ( stream
.IsSeekable() )
2409 posOld
= stream
.TellI();
2411 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2413 if ( posOld
!= wxInvalidOffset
)
2414 stream
.SeekI(posOld
);
2419 // rescale the image to the specified size if needed
2420 if ( maxWidth
|| maxHeight
)
2422 const unsigned widthOrig
= GetWidth(),
2423 heightOrig
= GetHeight();
2425 // this uses the same (trivial) algorithm as the JPEG handler
2426 unsigned width
= widthOrig
,
2427 height
= heightOrig
;
2428 while ( (maxWidth
&& width
> maxWidth
) ||
2429 (maxHeight
&& height
> maxHeight
) )
2435 if ( width
!= widthOrig
|| height
!= heightOrig
)
2436 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2439 // Set this after Rescale, which currently does not preserve it
2440 M_IMGDATA
->m_type
= handler
.GetType();
2445 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2449 wxImageHandler
*handler
;
2451 if ( type
== wxBITMAP_TYPE_ANY
)
2453 if ( !stream
.IsSeekable() )
2455 // The error message about image data format being unknown below
2456 // would be misleading in this case as we are not even going to try
2457 // any handlers because CanRead() never does anything for not
2458 // seekable stream, so try to be more precise here.
2459 wxLogError(_("Can't automatically determine the image format "
2460 "for non-seekable input."));
2464 const wxList
& list
= GetHandlers();
2465 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2467 node
= node
->GetNext() )
2469 handler
= (wxImageHandler
*)node
->GetData();
2470 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2474 wxLogWarning( _("Unknown image data format.") );
2478 //else: have specific type
2480 handler
= FindHandler(type
);
2483 wxLogWarning( _("No image handler for type %d defined."), type
);
2487 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2489 wxLogError(_("This is not a %s."), handler
->GetName());
2493 return DoLoad(*handler
, stream
, index
);
2496 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2500 m_refData
= new wxImageRefData
;
2502 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2506 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2510 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2512 wxLogError(_("Image is not of type %s."), mimetype
);
2516 return DoLoad(*handler
, stream
, index
);
2519 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2521 wxImage
* const self
= const_cast<wxImage
*>(this);
2522 if ( !handler
.SaveFile(self
, stream
) )
2525 M_IMGDATA
->m_type
= handler
.GetType();
2529 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2531 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2533 wxImageHandler
*handler
= FindHandler(type
);
2536 wxLogWarning( _("No image handler for type %d defined."), type
);
2540 return DoSave(*handler
, stream
);
2543 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2545 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2547 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2550 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2553 return DoSave(*handler
, stream
);
2556 #endif // wxUSE_STREAMS
2558 // ----------------------------------------------------------------------------
2559 // image I/O handlers
2560 // ----------------------------------------------------------------------------
2562 void wxImage::AddHandler( wxImageHandler
*handler
)
2564 // Check for an existing handler of the type being added.
2565 if (FindHandler( handler
->GetType() ) == 0)
2567 sm_handlers
.Append( handler
);
2571 // This is not documented behaviour, merely the simplest 'fix'
2572 // for preventing duplicate additions. If someone ever has
2573 // a good reason to add and remove duplicate handlers (and they
2574 // may) we should probably refcount the duplicates.
2575 // also an issue in InsertHandler below.
2577 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2578 handler
->GetName().c_str() );
2583 void wxImage::InsertHandler( wxImageHandler
*handler
)
2585 // Check for an existing handler of the type being added.
2586 if (FindHandler( handler
->GetType() ) == 0)
2588 sm_handlers
.Insert( handler
);
2592 // see AddHandler for additional comments.
2593 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2594 handler
->GetName().c_str() );
2599 bool wxImage::RemoveHandler( const wxString
& name
)
2601 wxImageHandler
*handler
= FindHandler(name
);
2604 sm_handlers
.DeleteObject(handler
);
2612 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2614 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2617 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2618 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2620 node
= node
->GetNext();
2625 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2627 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2630 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2631 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2633 if (handler
->GetExtension() == extension
)
2635 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2638 node
= node
->GetNext();
2643 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2645 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2648 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2649 if (handler
->GetType() == bitmapType
) return handler
;
2650 node
= node
->GetNext();
2655 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2657 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2660 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2661 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2662 node
= node
->GetNext();
2667 void wxImage::InitStandardHandlers()
2670 AddHandler(new wxBMPHandler
);
2671 #endif // wxUSE_STREAMS
2674 void wxImage::CleanUpHandlers()
2676 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2679 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2680 wxList::compatibility_iterator next
= node
->GetNext();
2685 sm_handlers
.Clear();
2688 wxString
wxImage::GetImageExtWildcard()
2692 wxList
& Handlers
= wxImage::GetHandlers();
2693 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2696 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2697 fmts
+= wxT("*.") + Handler
->GetExtension();
2698 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2699 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2700 Node
= Node
->GetNext();
2701 if ( Node
) fmts
+= wxT(";");
2704 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2707 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2709 const double red
= rgb
.red
/ 255.0,
2710 green
= rgb
.green
/ 255.0,
2711 blue
= rgb
.blue
/ 255.0;
2713 // find the min and max intensity (and remember which one was it for the
2715 double minimumRGB
= red
;
2716 if ( green
< minimumRGB
)
2718 if ( blue
< minimumRGB
)
2721 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2722 double maximumRGB
= red
;
2723 if ( green
> maximumRGB
)
2728 if ( blue
> maximumRGB
)
2734 const double value
= maximumRGB
;
2736 double hue
= 0.0, saturation
;
2737 const double deltaRGB
= maximumRGB
- minimumRGB
;
2738 if ( wxIsNullDouble(deltaRGB
) )
2740 // Gray has no color
2749 hue
= (green
- blue
) / deltaRGB
;
2753 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2757 hue
= 4.0 + (red
- green
) / deltaRGB
;
2761 wxFAIL_MSG(wxT("hue not specified"));
2770 saturation
= deltaRGB
/ maximumRGB
;
2773 return HSVValue(hue
, saturation
, value
);
2776 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2778 double red
, green
, blue
;
2780 if ( wxIsNullDouble(hsv
.saturation
) )
2789 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2790 int i
= (int)floor(hue
);
2791 double f
= hue
- i
; // fractional part of h
2792 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2798 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2803 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2811 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2816 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2821 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2829 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2834 return RGBValue((unsigned char)(red
* 255.0),
2835 (unsigned char)(green
* 255.0),
2836 (unsigned char)(blue
* 255.0));
2840 * Rotates the hue of each pixel of the image. angle is a double in the range
2841 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2843 void wxImage::RotateHue(double angle
)
2847 unsigned char *srcBytePtr
;
2848 unsigned char *dstBytePtr
;
2849 unsigned long count
;
2850 wxImage::HSVValue hsv
;
2851 wxImage::RGBValue rgb
;
2853 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2854 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2855 if ( count
> 0 && !wxIsNullDouble(angle
) )
2857 srcBytePtr
= M_IMGDATA
->m_data
;
2858 dstBytePtr
= srcBytePtr
;
2861 rgb
.red
= *srcBytePtr
++;
2862 rgb
.green
= *srcBytePtr
++;
2863 rgb
.blue
= *srcBytePtr
++;
2864 hsv
= RGBtoHSV(rgb
);
2866 hsv
.hue
= hsv
.hue
+ angle
;
2868 hsv
.hue
= hsv
.hue
- 1.0;
2869 else if (hsv
.hue
< 0.0)
2870 hsv
.hue
= hsv
.hue
+ 1.0;
2872 rgb
= HSVtoRGB(hsv
);
2873 *dstBytePtr
++ = rgb
.red
;
2874 *dstBytePtr
++ = rgb
.green
;
2875 *dstBytePtr
++ = rgb
.blue
;
2876 } while (--count
!= 0);
2880 //-----------------------------------------------------------------------------
2882 //-----------------------------------------------------------------------------
2884 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2887 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
2889 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2890 // wxImageHandler::CallDoCanRead
2892 if ( !stream
.IsSeekable() )
2893 return false; // can't test unseekable stream
2895 wxFileOffset posOld
= stream
.TellI();
2896 int n
= DoGetImageCount(stream
);
2898 // restore the old position to be able to test other formats and so on
2899 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2901 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2903 // reading would fail anyhow as we're not at the right position
2910 bool wxImageHandler::CanRead( const wxString
& name
)
2912 wxImageFileInputStream
stream(name
);
2913 if ( !stream
.IsOk() )
2915 wxLogError(_("Failed to check format of image file \"%s\"."), name
);
2920 return CanRead(stream
);
2923 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2925 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2926 // wxImageHandler::GetImageCount
2928 if ( !stream
.IsSeekable() )
2929 return false; // can't test unseekable stream
2931 wxFileOffset posOld
= stream
.TellI();
2932 bool ok
= DoCanRead(stream
);
2934 // restore the old position to be able to test other formats and so on
2935 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2937 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2939 // reading would fail anyhow as we're not at the right position
2946 #endif // wxUSE_STREAMS
2950 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2952 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
2954 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2955 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2957 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2958 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2960 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2963 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2965 else // no resolution options specified
2970 return wxIMAGE_RESOLUTION_NONE
;
2973 // get the resolution unit too
2974 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2977 // this is the default
2978 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2981 return (wxImageResolution
)resUnit
;
2984 // ----------------------------------------------------------------------------
2985 // image histogram stuff
2986 // ----------------------------------------------------------------------------
2989 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2994 unsigned char g2
) const
2996 unsigned long key
= MakeKey(r2
, g2
, b2
);
2998 while ( find(key
) != end() )
3000 // color already used
3012 wxLogError(_("No unused colour in image.") );
3018 key
= MakeKey(r2
, g2
, b2
);
3032 wxImage::FindFirstUnusedColour(unsigned char *r
,
3037 unsigned char g2
) const
3039 wxImageHistogram histogram
;
3041 ComputeHistogram(histogram
);
3043 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3049 // Counts and returns the number of different colours. Optionally stops
3050 // when it exceeds 'stopafter' different colours. This is useful, for
3051 // example, to see if the image can be saved as 8-bit (256 colour or
3052 // less, in this case it would be invoked as CountColours(256)). Default
3053 // value for stopafter is -1 (don't care).
3055 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3059 unsigned char r
, g
, b
;
3061 unsigned long size
, nentries
, key
;
3064 size
= GetWidth() * GetHeight();
3067 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3072 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3074 if (h
.Get(key
) == NULL
)
3085 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3087 unsigned char *p
= GetData();
3088 unsigned long nentries
= 0;
3092 const unsigned long size
= GetWidth() * GetHeight();
3094 unsigned char r
, g
, b
;
3095 for ( unsigned long n
= 0; n
< size
; n
++ )
3101 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3103 if ( entry
.value
++ == 0 )
3104 entry
.index
= nentries
++;
3111 * Rotation code by Carlos Moreno
3114 static const double wxROTATE_EPSILON
= 1e-10;
3116 // Auxiliary function to rotate a point (x,y) with respect to point p0
3117 // make it inline and use a straight return to facilitate optimization
3118 // also, the function receives the sine and cosine of the angle to avoid
3119 // repeating the time-consuming calls to these functions -- sin/cos can
3120 // be computed and stored in the calling function.
3122 static inline wxRealPoint
3123 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3124 const wxRealPoint
& p0
)
3126 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3127 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3130 static inline wxRealPoint
3131 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3132 const wxRealPoint
& p0
)
3134 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3137 wxImage
wxImage::Rotate(double angle
,
3138 const wxPoint
& centre_of_rotation
,
3140 wxPoint
*offset_after_rotation
) const
3142 // screen coordinates are a mirror image of "real" coordinates
3145 const bool has_alpha
= HasAlpha();
3147 const int w
= GetWidth();
3148 const int h
= GetHeight();
3152 // Create pointer-based array to accelerate access to wxImage's data
3153 unsigned char ** data
= new unsigned char * [h
];
3154 data
[0] = GetData();
3155 for (i
= 1; i
< h
; i
++)
3156 data
[i
] = data
[i
- 1] + (3 * w
);
3158 // Same for alpha channel
3159 unsigned char ** alpha
= NULL
;
3162 alpha
= new unsigned char * [h
];
3163 alpha
[0] = GetAlpha();
3164 for (i
= 1; i
< h
; i
++)
3165 alpha
[i
] = alpha
[i
- 1] + w
;
3168 // precompute coefficients for rotation formula
3169 const double cos_angle
= cos(angle
);
3170 const double sin_angle
= sin(angle
);
3172 // Create new Image to store the result
3173 // First, find rectangle that covers the rotated image; to do that,
3174 // rotate the four corners
3176 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3178 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3179 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3180 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3181 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3183 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3184 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3185 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3186 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3188 // Create rotated image
3189 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3190 // With alpha channel
3194 if (offset_after_rotation
!= NULL
)
3196 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3199 // the rotated (destination) image is always accessed sequentially via this
3200 // pointer, there is no need for pointer-based arrays here
3201 unsigned char *dst
= rotated
.GetData();
3203 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3205 // if the original image has a mask, use its RGB values as the blank pixel,
3206 // else, fall back to default (black).
3207 unsigned char blank_r
= 0;
3208 unsigned char blank_g
= 0;
3209 unsigned char blank_b
= 0;
3213 blank_r
= GetMaskRed();
3214 blank_g
= GetMaskGreen();
3215 blank_b
= GetMaskBlue();
3216 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3219 // Now, for each point of the rotated image, find where it came from, by
3220 // performing an inverse rotation (a rotation of -angle) and getting the
3221 // pixel at those coordinates
3223 const int rH
= rotated
.GetHeight();
3224 const int rW
= rotated
.GetWidth();
3226 // do the (interpolating) test outside of the loops, so that it is done
3227 // only once, instead of repeating it for each pixel.
3230 for (int y
= 0; y
< rH
; y
++)
3232 for (int x
= 0; x
< rW
; x
++)
3234 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3236 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3237 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3239 // interpolate using the 4 enclosing grid-points. Those
3240 // points can be obtained using floor and ceiling of the
3241 // exact coordinates of the point
3244 if (0 < src
.x
&& src
.x
< w
- 1)
3246 x1
= wxRound(floor(src
.x
));
3247 x2
= wxRound(ceil(src
.x
));
3249 else // else means that x is near one of the borders (0 or width-1)
3251 x1
= x2
= wxRound (src
.x
);
3254 if (0 < src
.y
&& src
.y
< h
- 1)
3256 y1
= wxRound(floor(src
.y
));
3257 y2
= wxRound(ceil(src
.y
));
3261 y1
= y2
= wxRound (src
.y
);
3264 // get four points and the distances (square of the distance,
3265 // for efficiency reasons) for the interpolation formula
3267 // GRG: Do not calculate the points until they are
3268 // really needed -- this way we can calculate
3269 // just one, instead of four, if d1, d2, d3
3270 // or d4 are < wxROTATE_EPSILON
3272 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3273 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3274 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3275 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3277 // Now interpolate as a weighted average of the four surrounding
3278 // points, where the weights are the distances to each of those points
3280 // If the point is exactly at one point of the grid of the source
3281 // image, then don't interpolate -- just assign the pixel
3283 // d1,d2,d3,d4 are positive -- no need for abs()
3284 if (d1
< wxROTATE_EPSILON
)
3286 unsigned char *p
= data
[y1
] + (3 * x1
);
3292 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3294 else if (d2
< wxROTATE_EPSILON
)
3296 unsigned char *p
= data
[y1
] + (3 * x2
);
3302 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3304 else if (d3
< wxROTATE_EPSILON
)
3306 unsigned char *p
= data
[y2
] + (3 * x2
);
3312 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3314 else if (d4
< wxROTATE_EPSILON
)
3316 unsigned char *p
= data
[y2
] + (3 * x1
);
3322 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3326 // weights for the weighted average are proportional to the inverse of the distance
3327 unsigned char *v1
= data
[y1
] + (3 * x1
);
3328 unsigned char *v2
= data
[y1
] + (3 * x2
);
3329 unsigned char *v3
= data
[y2
] + (3 * x2
);
3330 unsigned char *v4
= data
[y2
] + (3 * x1
);
3332 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3336 *(dst
++) = (unsigned char)
3337 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3338 w3
* *(v3
++) + w4
* *(v4
++)) /
3339 (w1
+ w2
+ w3
+ w4
) );
3340 *(dst
++) = (unsigned char)
3341 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3342 w3
* *(v3
++) + w4
* *(v4
++)) /
3343 (w1
+ w2
+ w3
+ w4
) );
3344 *(dst
++) = (unsigned char)
3345 ( (w1
* *v1
+ w2
* *v2
+
3346 w3
* *v3
+ w4
* *v4
) /
3347 (w1
+ w2
+ w3
+ w4
) );
3351 v1
= alpha
[y1
] + (x1
);
3352 v2
= alpha
[y1
] + (x2
);
3353 v3
= alpha
[y2
] + (x2
);
3354 v4
= alpha
[y2
] + (x1
);
3356 *(alpha_dst
++) = (unsigned char)
3357 ( (w1
* *v1
+ w2
* *v2
+
3358 w3
* *v3
+ w4
* *v4
) /
3359 (w1
+ w2
+ w3
+ w4
) );
3375 else // not interpolating
3377 for (int y
= 0; y
< rH
; y
++)
3379 for (int x
= 0; x
< rW
; x
++)
3381 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3383 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3384 const int ys
= wxRound (src
.y
); // closest integer
3386 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3388 unsigned char *p
= data
[ys
] + (3 * xs
);
3394 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3403 *(alpha_dst
++) = 255;
3419 // A module to allow wxImage initialization/cleanup
3420 // without calling these functions from app.cpp or from
3421 // the user's application.
3423 class wxImageModule
: public wxModule
3425 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3428 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3429 void OnExit() { wxImage::CleanUpHandlers(); }
3432 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3435 #endif // wxUSE_IMAGE