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
);
145 wxUnusedVar(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 // returns a new image with the same dimensions, alpha, and mask as *this
257 // if on_its_side is true, width and height are swapped
258 wxImage
wxImage::MakeEmptyClone(int flags
) const
262 wxCHECK_MSG( IsOk(), image
, wxS("invalid image") );
264 long height
= M_IMGDATA
->m_height
;
265 long width
= M_IMGDATA
->m_width
;
267 if ( flags
& Clone_SwapOrientation
)
268 wxSwap( width
, height
);
270 if ( !image
.Create( width
, height
, false ) )
272 wxFAIL_MSG( wxS("unable to create image") );
276 if ( M_IMGDATA
->m_alpha
)
279 wxCHECK2_MSG( image
.GetAlpha(), return wxImage(),
280 wxS("unable to create alpha channel") );
283 if ( M_IMGDATA
->m_hasMask
)
285 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
286 M_IMGDATA
->m_maskGreen
,
287 M_IMGDATA
->m_maskBlue
);
293 wxImage
wxImage::Copy() const
297 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
299 image
.m_refData
= CloneRefData(m_refData
);
304 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
306 if( xFactor
== 1 && yFactor
== 1 )
311 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
313 // can't scale to/from 0 size
314 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
315 wxT("invalid new image size") );
317 long old_height
= M_IMGDATA
->m_height
,
318 old_width
= M_IMGDATA
->m_width
;
320 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
321 wxT("invalid old image size") );
323 long width
= old_width
/ xFactor
;
324 long height
= old_height
/ yFactor
;
326 image
.Create( width
, height
, false );
328 char unsigned *data
= image
.GetData();
330 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
332 bool hasMask
= false ;
333 unsigned char maskRed
= 0;
334 unsigned char maskGreen
= 0;
335 unsigned char maskBlue
= 0 ;
337 const unsigned char *source_data
= M_IMGDATA
->m_data
;
338 unsigned char *target_data
= data
;
339 const unsigned char *source_alpha
= 0 ;
340 unsigned char *target_alpha
= 0 ;
341 if (M_IMGDATA
->m_hasMask
)
344 maskRed
= M_IMGDATA
->m_maskRed
;
345 maskGreen
= M_IMGDATA
->m_maskGreen
;
346 maskBlue
=M_IMGDATA
->m_maskBlue
;
348 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
349 M_IMGDATA
->m_maskGreen
,
350 M_IMGDATA
->m_maskBlue
);
354 source_alpha
= M_IMGDATA
->m_alpha
;
358 target_alpha
= image
.GetAlpha() ;
362 for (long y
= 0; y
< height
; y
++)
364 for (long x
= 0; x
< width
; x
++)
366 unsigned long avgRed
= 0 ;
367 unsigned long avgGreen
= 0;
368 unsigned long avgBlue
= 0;
369 unsigned long avgAlpha
= 0 ;
370 unsigned long counter
= 0 ;
372 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
374 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
375 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
377 const unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
378 unsigned char red
= pixel
[0] ;
379 unsigned char green
= pixel
[1] ;
380 unsigned char blue
= pixel
[2] ;
381 unsigned char alpha
= 255 ;
383 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
384 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
399 *(target_data
++) = M_IMGDATA
->m_maskRed
;
400 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
401 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
406 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
407 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
408 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
409 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
414 // In case this is a cursor, make sure the hotspot is scaled accordingly:
415 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
416 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
417 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
418 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
419 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
420 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
426 wxImage::Scale( int width
, int height
, wxImageResizeQuality quality
) const
430 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
432 // can't scale to/from 0 size
433 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
434 wxT("invalid new image size") );
436 long old_height
= M_IMGDATA
->m_height
,
437 old_width
= M_IMGDATA
->m_width
;
438 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
439 wxT("invalid old image size") );
441 // If the image's new width and height are the same as the original, no
442 // need to waste time or CPU cycles
443 if ( old_width
== width
&& old_height
== height
)
446 // Resample the image using the method as specified.
449 case wxIMAGE_QUALITY_NEAREST
:
450 if ( old_width
% width
== 0 && old_width
>= width
&&
451 old_height
% height
== 0 && old_height
>= height
)
453 return ShrinkBy( old_width
/ width
, old_height
/ height
);
456 image
= ResampleNearest(width
, height
);
459 case wxIMAGE_QUALITY_BILINEAR
:
460 image
= ResampleBilinear(width
, height
);
463 case wxIMAGE_QUALITY_BICUBIC
:
464 image
= ResampleBicubic(width
, height
);
467 case wxIMAGE_QUALITY_BOX_AVERAGE
:
468 image
= ResampleBox(width
, height
);
471 case wxIMAGE_QUALITY_HIGH
:
472 image
= width
< old_width
&& height
< old_height
473 ? ResampleBox(width
, height
)
474 : ResampleBicubic(width
, height
);
478 // If the original image has a mask, apply the mask to the new image
479 if (M_IMGDATA
->m_hasMask
)
481 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
482 M_IMGDATA
->m_maskGreen
,
483 M_IMGDATA
->m_maskBlue
);
486 // In case this is a cursor, make sure the hotspot is scaled accordingly:
487 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
488 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
489 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
490 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
491 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
492 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
497 wxImage
wxImage::ResampleNearest(int width
, int height
) const
500 image
.Create( width
, height
, false );
502 unsigned char *data
= image
.GetData();
504 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
506 const unsigned char *source_data
= M_IMGDATA
->m_data
;
507 unsigned char *target_data
= data
;
508 const unsigned char *source_alpha
= 0 ;
509 unsigned char *target_alpha
= 0 ;
511 if ( !M_IMGDATA
->m_hasMask
)
513 source_alpha
= M_IMGDATA
->m_alpha
;
517 target_alpha
= image
.GetAlpha() ;
521 long old_height
= M_IMGDATA
->m_height
,
522 old_width
= M_IMGDATA
->m_width
;
523 long x_delta
= (old_width
<<16) / width
;
524 long y_delta
= (old_height
<<16) / height
;
526 unsigned char* dest_pixel
= target_data
;
529 for ( long j
= 0; j
< height
; j
++ )
531 const unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
532 const unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
535 for ( long i
= 0; i
< width
; i
++ )
537 const unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
538 const unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
539 dest_pixel
[0] = src_pixel
[0];
540 dest_pixel
[1] = src_pixel
[1];
541 dest_pixel
[2] = src_pixel
[2];
544 *(target_alpha
++) = *src_alpha_pixel
;
563 inline int BoxBetween(int value
, int low
, int high
)
565 return wxMax(wxMin(value
, high
), low
);
568 void ResampleBoxPrecalc(wxVector
<BoxPrecalc
>& boxes
, int oldDim
)
570 const int newDim
= boxes
.size();
571 const double scale_factor_1
= double(oldDim
) / newDim
;
572 const int scale_factor_2
= (int)(scale_factor_1
/ 2);
574 for ( int dst
= 0; dst
< newDim
; ++dst
)
576 // Source pixel in the Y direction
577 const int src_p
= int(dst
* scale_factor_1
);
579 BoxPrecalc
& precalc
= boxes
[dst
];
580 precalc
.boxStart
= BoxBetween(int(src_p
- scale_factor_1
/2.0 + 1),
582 precalc
.boxEnd
= BoxBetween(wxMax(precalc
.boxStart
+ 1,
583 int(src_p
+ scale_factor_2
)),
588 } // anonymous namespace
590 wxImage
wxImage::ResampleBox(int width
, int height
) const
592 // This function implements a simple pre-blur/box averaging method for
593 // downsampling that gives reasonably smooth results To scale the image
594 // down we will need to gather a grid of pixels of the size of the scale
595 // factor in each direction and then do an averaging of the pixels.
597 wxImage
ret_image(width
, height
, false);
599 wxVector
<BoxPrecalc
> vPrecalcs(height
);
600 wxVector
<BoxPrecalc
> hPrecalcs(width
);
602 ResampleBoxPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
603 ResampleBoxPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
606 const unsigned char* src_data
= M_IMGDATA
->m_data
;
607 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
608 unsigned char* dst_data
= ret_image
.GetData();
609 unsigned char* dst_alpha
= NULL
;
613 ret_image
.SetAlpha();
614 dst_alpha
= ret_image
.GetAlpha();
617 int averaged_pixels
, src_pixel_index
;
618 double sum_r
, sum_g
, sum_b
, sum_a
;
620 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
622 // Source pixel in the Y direction
623 const BoxPrecalc
& vPrecalc
= vPrecalcs
[y
];
625 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
627 // Source pixel in the X direction
628 const BoxPrecalc
& hPrecalc
= hPrecalcs
[x
];
630 // Box of pixels to average
632 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
634 for ( int j
= vPrecalc
.boxStart
; j
<= vPrecalc
.boxEnd
; ++j
)
636 for ( int i
= hPrecalc
.boxStart
; i
<= hPrecalc
.boxEnd
; ++i
)
638 // Calculate the actual index in our source pixels
639 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
641 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
642 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
643 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
645 sum_a
+= src_alpha
[src_pixel_index
];
651 // Calculate the average from the sum and number of averaged pixels
652 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
653 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
654 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
657 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
667 struct BilinearPrecalc
675 void ResampleBilinearPrecalc(wxVector
<BilinearPrecalc
>& precalcs
, int oldDim
)
677 const int newDim
= precalcs
.size();
678 const double scale_factor
= double(oldDim
) / newDim
;
679 const int srcpixmax
= oldDim
- 1;
681 for ( int dsty
= 0; dsty
< newDim
; dsty
++ )
683 // We need to calculate the source pixel to interpolate from - Y-axis
684 double srcpix
= double(dsty
) * scale_factor
;
685 double srcpix1
= int(srcpix
);
686 double srcpix2
= srcpix1
== srcpixmax
? srcpix1
: srcpix1
+ 1.0;
688 BilinearPrecalc
& precalc
= precalcs
[dsty
];
690 precalc
.dd
= srcpix
- (int)srcpix
;
691 precalc
.dd1
= 1.0 - precalc
.dd
;
692 precalc
.offset1
= srcpix1
< 0.0
694 : srcpix1
> srcpixmax
697 precalc
.offset2
= srcpix2
< 0.0
699 : srcpix2
> srcpixmax
705 } // anonymous namespace
707 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
709 // This function implements a Bilinear algorithm for resampling.
710 wxImage
ret_image(width
, height
, false);
711 const unsigned char* src_data
= M_IMGDATA
->m_data
;
712 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
713 unsigned char* dst_data
= ret_image
.GetData();
714 unsigned char* dst_alpha
= NULL
;
718 ret_image
.SetAlpha();
719 dst_alpha
= ret_image
.GetAlpha();
722 wxVector
<BilinearPrecalc
> vPrecalcs(height
);
723 wxVector
<BilinearPrecalc
> hPrecalcs(width
);
724 ResampleBilinearPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
725 ResampleBilinearPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
727 // initialize alpha values to avoid g++ warnings about possibly
728 // uninitialized variables
729 double r1
, g1
, b1
, a1
= 0;
730 double r2
, g2
, b2
, a2
= 0;
732 for ( int dsty
= 0; dsty
< height
; dsty
++ )
734 // We need to calculate the source pixel to interpolate from - Y-axis
735 const BilinearPrecalc
& vPrecalc
= vPrecalcs
[dsty
];
736 const int y_offset1
= vPrecalc
.offset1
;
737 const int y_offset2
= vPrecalc
.offset2
;
738 const double dy
= vPrecalc
.dd
;
739 const double dy1
= vPrecalc
.dd1
;
742 for ( int dstx
= 0; dstx
< width
; dstx
++ )
744 // X-axis of pixel to interpolate from
745 const BilinearPrecalc
& hPrecalc
= hPrecalcs
[dstx
];
747 const int x_offset1
= hPrecalc
.offset1
;
748 const int x_offset2
= hPrecalc
.offset2
;
749 const double dx
= hPrecalc
.dd
;
750 const double dx1
= hPrecalc
.dd1
;
752 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
753 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
754 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
755 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
758 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
759 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
760 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
762 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
765 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
766 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
767 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
769 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
773 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
774 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
775 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
779 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
786 // The following two local functions are for the B-spline weighting of the
787 // bicubic sampling algorithm
788 static inline double spline_cube(double value
)
790 return value
<= 0.0 ? 0.0 : value
* value
* value
;
793 static inline double spline_weight(double value
)
795 return (spline_cube(value
+ 2) -
796 4 * spline_cube(value
+ 1) +
797 6 * spline_cube(value
) -
798 4 * spline_cube(value
- 1)) / 6;
805 struct BicubicPrecalc
811 void ResampleBicubicPrecalc(wxVector
<BicubicPrecalc
> &aWeight
, int oldDim
)
813 const int newDim
= aWeight
.size();
814 for ( int dstd
= 0; dstd
< newDim
; dstd
++ )
816 // We need to calculate the source pixel to interpolate from - Y-axis
817 const double srcpixd
= static_cast<double>(dstd
* oldDim
) / newDim
;
818 const double dd
= srcpixd
- static_cast<int>(srcpixd
);
820 BicubicPrecalc
&precalc
= aWeight
[dstd
];
822 for ( int k
= -1; k
<= 2; k
++ )
824 precalc
.offset
[k
+ 1] = srcpixd
+ k
< 0.0
826 : srcpixd
+ k
>= oldDim
828 : static_cast<int>(srcpixd
+ k
);
830 precalc
.weight
[k
+ 1] = spline_weight(k
- dd
);
835 } // anonymous namespace
837 // This is the bicubic resampling algorithm
838 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
840 // This function implements a Bicubic B-Spline algorithm for resampling.
841 // This method is certainly a little slower than wxImage's default pixel
842 // replication method, however for most reasonably sized images not being
843 // upsampled too much on a fairly average CPU this difference is hardly
844 // noticeable and the results are far more pleasing to look at.
846 // This particular bicubic algorithm does pixel weighting according to a
847 // B-Spline that basically implements a Gaussian bell-like weighting
848 // kernel. Because of this method the results may appear a bit blurry when
849 // upsampling by large factors. This is basically because a slight
850 // gaussian blur is being performed to get the smooth look of the upsampled
853 // Edge pixels: 3-4 possible solutions
854 // - (Wrap/tile) Wrap the image, take the color value from the opposite
855 // side of the image.
856 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
857 // where n is nonpositive, will have the value of (2, 1).
858 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
859 // pixels which do have all neighbours.
860 // - (Clamp) Choose the nearest pixel along the border. This takes the
861 // border pixels and extends them out to infinity.
863 // NOTE: below the y_offset and x_offset variables are being set for edge
864 // pixels using the "Mirror" method mentioned above
868 ret_image
.Create(width
, height
, false);
870 const unsigned char* src_data
= M_IMGDATA
->m_data
;
871 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
872 unsigned char* dst_data
= ret_image
.GetData();
873 unsigned char* dst_alpha
= NULL
;
877 ret_image
.SetAlpha();
878 dst_alpha
= ret_image
.GetAlpha();
881 // Precalculate weights
882 wxVector
<BicubicPrecalc
> vPrecalcs(height
);
883 wxVector
<BicubicPrecalc
> hPrecalcs(width
);
885 ResampleBicubicPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
886 ResampleBicubicPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
888 for ( int dsty
= 0; dsty
< height
; dsty
++ )
890 // We need to calculate the source pixel to interpolate from - Y-axis
891 const BicubicPrecalc
& vPrecalc
= vPrecalcs
[dsty
];
893 for ( int dstx
= 0; dstx
< width
; dstx
++ )
895 // X-axis of pixel to interpolate from
896 const BicubicPrecalc
& hPrecalc
= hPrecalcs
[dstx
];
898 // Sums for each color channel
899 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
901 // Here we actually determine the RGBA values for the destination pixel
902 for ( int k
= -1; k
<= 2; k
++ )
905 const int y_offset
= vPrecalc
.offset
[k
+ 1];
907 // Loop across the X axis
908 for ( int i
= -1; i
<= 2; i
++ )
911 const int x_offset
= hPrecalc
.offset
[i
+ 1];
913 // Calculate the exact position where the source data
914 // should be pulled from based on the x_offset and y_offset
915 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
917 // Calculate the weight for the specified pixel according
918 // to the bicubic b-spline kernel we're using for
921 pixel_weight
= vPrecalc
.weight
[k
+ 1] * hPrecalc
.weight
[i
+ 1];
923 // Create a sum of all velues for each color channel
924 // adjusted for the pixel's calculated weight
925 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
926 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
927 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
929 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
933 // Put the data into the destination image. The summed values are
934 // of double data type and are rounded here for accuracy
935 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
936 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
937 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
941 *dst_alpha
++ = (unsigned char)sum_a
;
948 // Blur in the horizontal direction
949 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
951 wxImage
ret_image(MakeEmptyClone());
953 wxCHECK( ret_image
.IsOk(), ret_image
);
955 const unsigned char* src_data
= M_IMGDATA
->m_data
;
956 unsigned char* dst_data
= ret_image
.GetData();
957 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
958 unsigned char* dst_alpha
= ret_image
.GetAlpha();
960 // number of pixels we average over
961 const int blurArea
= blurRadius
*2 + 1;
963 // Horizontal blurring algorithm - average all pixels in the specified blur
964 // radius in the X or horizontal direction
965 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
967 // Variables used in the blurring algorithm
974 const unsigned char *src
;
977 // Calculate the average of all pixels in the blur radius for the first
979 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
981 // To deal with the pixels at the start of a row so it's not
982 // grabbing GOK values from memory at negative indices of the
983 // image's data or grabbing from the previous row
985 pixel_idx
= y
* M_IMGDATA
->m_width
;
987 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
989 src
= src_data
+ pixel_idx
*3;
994 sum_a
+= src_alpha
[pixel_idx
];
997 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
998 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
999 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1000 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1002 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1004 // Now average the values of the rest of the pixels by just moving the
1005 // blur radius box along the row
1006 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
1008 // Take care of edge pixels on the left edge by essentially
1009 // duplicating the edge pixel
1010 if ( x
- blurRadius
- 1 < 0 )
1011 pixel_idx
= y
* M_IMGDATA
->m_width
;
1013 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
1015 // Subtract the value of the pixel at the left side of the blur
1017 src
= src_data
+ pixel_idx
*3;
1022 sum_a
-= src_alpha
[pixel_idx
];
1024 // Take care of edge pixels on the right edge
1025 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
1026 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
1028 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
1030 // Add the value of the pixel being added to the end of our box
1031 src
= src_data
+ pixel_idx
*3;
1036 sum_a
+= src_alpha
[pixel_idx
];
1038 // Save off the averaged data
1039 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
1040 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1041 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1042 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1044 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1051 // Blur in the vertical direction
1052 wxImage
wxImage::BlurVertical(int blurRadius
) const
1054 wxImage
ret_image(MakeEmptyClone());
1056 wxCHECK( ret_image
.IsOk(), ret_image
);
1058 const unsigned char* src_data
= M_IMGDATA
->m_data
;
1059 unsigned char* dst_data
= ret_image
.GetData();
1060 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
1061 unsigned char* dst_alpha
= ret_image
.GetAlpha();
1063 // number of pixels we average over
1064 const int blurArea
= blurRadius
*2 + 1;
1066 // Vertical blurring algorithm - same as horizontal but switched the
1067 // opposite direction
1068 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
1070 // Variables used in the blurring algorithm
1077 const unsigned char *src
;
1080 // Calculate the average of all pixels in our blur radius box for the
1081 // first pixel of the column
1082 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
1084 // To deal with the pixels at the start of a column so it's not
1085 // grabbing GOK values from memory at negative indices of the
1086 // image's data or grabbing from the previous column
1090 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
1092 src
= src_data
+ pixel_idx
*3;
1097 sum_a
+= src_alpha
[pixel_idx
];
1100 dst
= dst_data
+ x
*3;
1101 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1102 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1103 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1105 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
1107 // Now average the values of the rest of the pixels by just moving the
1108 // box along the column from top to bottom
1109 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1111 // Take care of pixels that would be beyond the top edge by
1112 // duplicating the top edge pixel for the column
1113 if ( y
- blurRadius
- 1 < 0 )
1116 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1118 // Subtract the value of the pixel at the top of our blur radius box
1119 src
= src_data
+ pixel_idx
*3;
1124 sum_a
-= src_alpha
[pixel_idx
];
1126 // Take care of the pixels that would be beyond the bottom edge of
1127 // the image similar to the top edge
1128 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1129 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1131 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1133 // Add the value of the pixel being added to the end of our box
1134 src
= src_data
+ pixel_idx
*3;
1139 sum_a
+= src_alpha
[pixel_idx
];
1141 // Save off the averaged data
1142 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1143 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1144 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1145 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1147 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1154 // The new blur function
1155 wxImage
wxImage::Blur(int blurRadius
) const
1158 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1160 // Blur the image in each direction
1161 ret_image
= BlurHorizontal(blurRadius
);
1162 ret_image
= ret_image
.BlurVertical(blurRadius
);
1167 wxImage
wxImage::Rotate90( bool clockwise
) const
1169 wxImage
image(MakeEmptyClone(Clone_SwapOrientation
));
1171 wxCHECK( image
.IsOk(), image
);
1173 long height
= M_IMGDATA
->m_height
;
1174 long width
= M_IMGDATA
->m_width
;
1176 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1178 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1179 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1180 clockwise
? hot_x
: width
- 1 - hot_x
);
1183 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1185 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1186 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1187 clockwise
? height
- 1 - hot_y
: hot_y
);
1190 unsigned char *data
= image
.GetData();
1191 unsigned char *target_data
;
1193 // we rotate the image in 21-pixel (63-byte) wide strips
1194 // to make better use of cpu cache - memory transfers
1195 // (note: while much better than single-pixel "strips",
1196 // our vertical strips will still generally straddle 64-byte cachelines)
1197 for (long ii
= 0; ii
< width
; )
1199 long next_ii
= wxMin(ii
+ 21, width
);
1201 for (long j
= 0; j
< height
; j
++)
1203 const unsigned char *source_data
1204 = M_IMGDATA
->m_data
+ (j
*width
+ ii
)*3;
1206 for (long i
= ii
; i
< next_ii
; i
++)
1210 target_data
= data
+ ((i
+ 1)*height
- j
- 1)*3;
1214 target_data
= data
+ (height
*(width
- 1 - i
) + j
)*3;
1216 memcpy( target_data
, source_data
, 3 );
1224 const unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1228 unsigned char *alpha_data
= image
.GetAlpha();
1229 unsigned char *target_alpha
= 0 ;
1231 for (long ii
= 0; ii
< width
; )
1233 long next_ii
= wxMin(ii
+ 64, width
);
1235 for (long j
= 0; j
< height
; j
++)
1237 source_alpha
= M_IMGDATA
->m_alpha
+ j
*width
+ ii
;
1239 for (long i
= ii
; i
< next_ii
; i
++)
1243 target_alpha
= alpha_data
+ (i
+1)*height
- j
- 1;
1247 target_alpha
= alpha_data
+ height
*(width
- i
- 1) + j
;
1250 *target_alpha
= *source_alpha
++;
1261 wxImage
wxImage::Rotate180() const
1263 wxImage
image(MakeEmptyClone());
1265 wxCHECK( image
.IsOk(), image
);
1267 long height
= M_IMGDATA
->m_height
;
1268 long width
= M_IMGDATA
->m_width
;
1270 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1272 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1273 width
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
));
1276 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1278 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1279 height
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
));
1282 unsigned char *data
= image
.GetData();
1283 unsigned char *alpha
= image
.GetAlpha();
1284 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1285 unsigned char *target_data
= data
+ width
* height
* 3;
1287 for (long j
= 0; j
< height
; j
++)
1289 for (long i
= 0; i
< width
; i
++)
1292 memcpy( target_data
, source_data
, 3 );
1299 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1300 unsigned char *dest_alpha
= alpha
+ width
* height
;
1302 for (long j
= 0; j
< height
; ++j
)
1304 for (long i
= 0; i
< width
; ++i
)
1306 *(--dest_alpha
) = *(src_alpha
++);
1314 wxImage
wxImage::Mirror( bool horizontally
) const
1316 wxImage
image(MakeEmptyClone());
1318 wxCHECK( image
.IsOk(), image
);
1320 long height
= M_IMGDATA
->m_height
;
1321 long width
= M_IMGDATA
->m_width
;
1323 unsigned char *data
= image
.GetData();
1324 unsigned char *alpha
= image
.GetAlpha();
1325 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1326 unsigned char *target_data
;
1330 for (long j
= 0; j
< height
; j
++)
1333 target_data
= data
-3;
1334 for (long i
= 0; i
< width
; i
++)
1336 memcpy( target_data
, source_data
, 3 );
1344 // src_alpha starts at the first pixel and increases by 1 after each step
1345 // (a step here is the copy of the alpha value of one pixel)
1346 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1347 // dest_alpha starts just beyond the first line, decreases before each step,
1348 // and after each line is finished, increases by 2 widths (skipping the line
1349 // just copied and the line that will be copied next)
1350 unsigned char *dest_alpha
= alpha
+ width
;
1352 for (long jj
= 0; jj
< height
; ++jj
)
1354 for (long i
= 0; i
< width
; ++i
) {
1355 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1357 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1363 for (long i
= 0; i
< height
; i
++)
1365 target_data
= data
+ 3*width
*(height
-1-i
);
1366 memcpy( target_data
, source_data
, (size_t)3*width
);
1367 source_data
+= 3*width
;
1372 // src_alpha starts at the first pixel and increases by 1 width after each step
1373 // (a step here is the copy of the alpha channel of an entire line)
1374 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1375 // dest_alpha starts just beyond the last line (beyond the whole image)
1376 // and decreases by 1 width before each step
1377 unsigned char *dest_alpha
= alpha
+ width
* height
;
1379 for (long jj
= 0; jj
< height
; ++jj
)
1381 dest_alpha
-= width
;
1382 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1391 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1395 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1397 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1398 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1399 image
, wxT("invalid subimage size") );
1401 const int subwidth
= rect
.GetWidth();
1402 const int subheight
= rect
.GetHeight();
1404 image
.Create( subwidth
, subheight
, false );
1406 const unsigned char *src_data
= GetData();
1407 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1408 unsigned char *subdata
= image
.GetData();
1409 unsigned char *subalpha
= NULL
;
1411 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1415 subalpha
= image
.GetAlpha();
1416 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1419 if (M_IMGDATA
->m_hasMask
)
1420 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1422 const int width
= GetWidth();
1423 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1425 src_data
+= 3 * pixsoff
;
1426 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1428 for (long j
= 0; j
< subheight
; ++j
)
1430 memcpy( subdata
, src_data
, 3 * subwidth
);
1431 subdata
+= 3 * subwidth
;
1432 src_data
+= 3 * width
;
1433 if (subalpha
!= NULL
) {
1434 memcpy( subalpha
, src_alpha
, subwidth
);
1435 subalpha
+= subwidth
;
1443 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1444 int r_
, int g_
, int b_
) const
1448 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1449 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1451 int width
= GetWidth(), height
= GetHeight();
1452 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1454 unsigned char r
= (unsigned char)r_
;
1455 unsigned char g
= (unsigned char)g_
;
1456 unsigned char b
= (unsigned char)b_
;
1457 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1459 GetOrFindMaskColour( &r
, &g
, &b
);
1460 image
.SetMaskColour(r
, g
, b
);
1463 image
.SetRGB(wxRect(), r
, g
, b
);
1465 // we have two coordinate systems:
1466 // source: starting at 0,0 of source image
1467 // destination starting at 0,0 of destination image
1468 // Documentation says:
1469 // "The image is pasted into a new image [...] at the position pos relative
1470 // to the upper left of the new image." this means the transition rule is:
1471 // "dest coord" = "source coord" + pos;
1473 // calculate the intersection using source coordinates:
1474 wxRect
srcRect(0, 0, width
, height
);
1475 wxRect
dstRect(-pos
, size
);
1477 srcRect
.Intersect(dstRect
);
1479 if (!srcRect
.IsEmpty())
1481 // insertion point is needed in destination coordinates.
1482 // NB: it is not always "pos"!
1483 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1485 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1486 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1488 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1494 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1496 wxCHECK_RET( IsOk(), wxT("invalid image") );
1497 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1503 int width
= image
.GetWidth();
1504 int height
= image
.GetHeight();
1517 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1518 width
= M_IMGDATA
->m_width
- (x
+xx
);
1519 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1520 height
= M_IMGDATA
->m_height
- (y
+yy
);
1522 if (width
< 1) return;
1523 if (height
< 1) return;
1525 // If we can, copy the data using memcpy() as this is the fastest way. But
1526 // for this the image being pasted must have "compatible" mask with this
1527 // one meaning that either it must not have one at all or it must use the
1528 // same masked colour.
1529 if ( !image
.HasMask() ||
1531 (GetMaskRed()==image
.GetMaskRed()) &&
1532 (GetMaskGreen()==image
.GetMaskGreen()) &&
1533 (GetMaskBlue()==image
.GetMaskBlue()))) )
1535 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1536 int source_step
= image
.GetWidth()*3;
1538 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1539 int target_step
= M_IMGDATA
->m_width
*3;
1540 for (int j
= 0; j
< height
; j
++)
1542 memcpy( target_data
, source_data
, width
*3 );
1543 source_data
+= source_step
;
1544 target_data
+= target_step
;
1548 // Copy over the alpha channel from the original image
1549 if ( image
.HasAlpha() )
1554 const unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1555 int source_step
= image
.GetWidth();
1557 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1558 int target_step
= M_IMGDATA
->m_width
;
1560 for (int j
= 0; j
< height
; j
++,
1561 source_data
+= source_step
,
1562 target_data
+= target_step
)
1564 memcpy( target_data
, source_data
, width
);
1568 if (!HasMask() && image
.HasMask())
1570 unsigned char r
= image
.GetMaskRed();
1571 unsigned char g
= image
.GetMaskGreen();
1572 unsigned char b
= image
.GetMaskBlue();
1574 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1575 int source_step
= image
.GetWidth()*3;
1577 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1578 int target_step
= M_IMGDATA
->m_width
*3;
1580 for (int j
= 0; j
< height
; j
++)
1582 for (int i
= 0; i
< width
*3; i
+=3)
1584 if ((source_data
[i
] != r
) ||
1585 (source_data
[i
+1] != g
) ||
1586 (source_data
[i
+2] != b
))
1588 memcpy( target_data
+i
, source_data
+i
, 3 );
1591 source_data
+= source_step
;
1592 target_data
+= target_step
;
1597 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1598 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1600 wxCHECK_RET( IsOk(), wxT("invalid image") );
1604 unsigned char *data
= GetData();
1606 const int w
= GetWidth();
1607 const int h
= GetHeight();
1609 for (int j
= 0; j
< h
; j
++)
1610 for (int i
= 0; i
< w
; i
++)
1612 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1622 wxImage
wxImage::ConvertToGreyscale(void) const
1624 return ConvertToGreyscale(0.299, 0.587, 0.114);
1627 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1629 wxImage
image(MakeEmptyClone());
1631 wxCHECK( image
.IsOk(), image
);
1633 const unsigned char *src
= M_IMGDATA
->m_data
;
1634 unsigned char *dest
= image
.GetData();
1636 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1637 const unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1638 const unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1639 const unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1641 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1642 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1644 memcpy(dest
, src
, 3);
1645 // only modify non-masked pixels
1646 if ( !hasMask
|| src
[0] != maskRed
|| src
[1] != maskGreen
|| src
[2] != maskBlue
)
1648 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1652 // copy the alpha channel, if any
1653 if ( image
.HasAlpha() )
1655 memcpy( image
.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1661 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1665 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1667 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1669 unsigned char *data
= image
.GetData();
1671 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1673 if (M_IMGDATA
->m_hasMask
)
1675 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1676 M_IMGDATA
->m_maskBlue
== b
)
1677 image
.SetMaskColour( 255, 255, 255 );
1679 image
.SetMaskColour( 0, 0, 0 );
1682 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1684 unsigned char *srcd
= M_IMGDATA
->m_data
;
1685 unsigned char *tard
= image
.GetData();
1687 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1689 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1690 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1696 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1698 wxImage image
= *this;
1700 unsigned char mr
= image
.GetMaskRed();
1701 unsigned char mg
= image
.GetMaskGreen();
1702 unsigned char mb
= image
.GetMaskBlue();
1704 int width
= image
.GetWidth();
1705 int height
= image
.GetHeight();
1706 bool has_mask
= image
.HasMask();
1708 for (int y
= height
-1; y
>= 0; --y
)
1710 for (int x
= width
-1; x
>= 0; --x
)
1712 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1713 unsigned char* r
= data
;
1714 unsigned char* g
= data
+1;
1715 unsigned char* b
= data
+2;
1717 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1720 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1726 int wxImage::GetWidth() const
1728 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1730 return M_IMGDATA
->m_width
;
1733 int wxImage::GetHeight() const
1735 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1737 return M_IMGDATA
->m_height
;
1740 wxBitmapType
wxImage::GetType() const
1742 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1744 return M_IMGDATA
->m_type
;
1747 void wxImage::SetType(wxBitmapType type
)
1749 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1751 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1752 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1754 M_IMGDATA
->m_type
= type
;
1757 long wxImage::XYToIndex(int x
, int y
) const
1761 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1763 return y
*M_IMGDATA
->m_width
+ x
;
1769 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1771 long pos
= XYToIndex(x
, y
);
1772 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1778 M_IMGDATA
->m_data
[ pos
] = r
;
1779 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1780 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1783 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1785 wxCHECK_RET( IsOk(), wxT("invalid image") );
1790 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1791 if ( rect
== wxRect() )
1797 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1798 imageRect
.Contains(rect
.GetBottomRight()),
1799 wxT("invalid bounding rectangle") );
1802 int x1
= rect
.GetLeft(),
1804 x2
= rect
.GetRight() + 1,
1805 y2
= rect
.GetBottom() + 1;
1807 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1808 int x
, y
, width
= GetWidth();
1809 for (y
= y1
; y
< y2
; y
++)
1811 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1812 for (x
= x1
; x
< x2
; x
++)
1821 unsigned char wxImage::GetRed( int x
, int y
) const
1823 long pos
= XYToIndex(x
, y
);
1824 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1828 return M_IMGDATA
->m_data
[pos
];
1831 unsigned char wxImage::GetGreen( int x
, int y
) const
1833 long pos
= XYToIndex(x
, y
);
1834 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1838 return M_IMGDATA
->m_data
[pos
+1];
1841 unsigned char wxImage::GetBlue( int x
, int y
) const
1843 long pos
= XYToIndex(x
, y
);
1844 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1848 return M_IMGDATA
->m_data
[pos
+2];
1851 bool wxImage::IsOk() const
1853 // image of 0 width or height can't be considered ok - at least because it
1854 // causes crashes in ConvertToBitmap() if we don't catch it in time
1855 wxImageRefData
*data
= M_IMGDATA
;
1856 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1859 unsigned char *wxImage::GetData() const
1861 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
1863 return M_IMGDATA
->m_data
;
1866 void wxImage::SetData( unsigned char *data
, bool static_data
)
1868 wxCHECK_RET( IsOk(), wxT("invalid image") );
1870 wxImageRefData
*newRefData
= new wxImageRefData();
1872 newRefData
->m_width
= M_IMGDATA
->m_width
;
1873 newRefData
->m_height
= M_IMGDATA
->m_height
;
1874 newRefData
->m_data
= data
;
1875 newRefData
->m_ok
= true;
1876 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1877 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1878 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1879 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1880 newRefData
->m_static
= static_data
;
1884 m_refData
= newRefData
;
1887 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1889 wxImageRefData
*newRefData
= new wxImageRefData();
1893 newRefData
->m_width
= new_width
;
1894 newRefData
->m_height
= new_height
;
1895 newRefData
->m_data
= data
;
1896 newRefData
->m_ok
= true;
1897 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1898 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1899 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1900 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1904 newRefData
->m_width
= new_width
;
1905 newRefData
->m_height
= new_height
;
1906 newRefData
->m_data
= data
;
1907 newRefData
->m_ok
= true;
1909 newRefData
->m_static
= static_data
;
1913 m_refData
= newRefData
;
1916 // ----------------------------------------------------------------------------
1917 // alpha channel support
1918 // ----------------------------------------------------------------------------
1920 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1922 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1924 long pos
= XYToIndex(x
, y
);
1925 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1929 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1932 unsigned char wxImage::GetAlpha(int x
, int y
) const
1934 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1936 long pos
= XYToIndex(x
, y
);
1937 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1939 return M_IMGDATA
->m_alpha
[pos
];
1943 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1947 const int w
= M_IMGDATA
->m_width
;
1948 const int h
= M_IMGDATA
->m_height
;
1950 unsigned char *alpha
= GetAlpha();
1951 unsigned char *data
= GetData();
1953 for ( int y
= 0; y
< h
; y
++ )
1955 for ( int x
= 0; x
< w
; x
++ )
1967 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1969 wxCHECK_RET( IsOk(), wxT("invalid image") );
1975 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1978 if( !M_IMGDATA
->m_staticAlpha
)
1979 free(M_IMGDATA
->m_alpha
);
1981 M_IMGDATA
->m_alpha
= alpha
;
1982 M_IMGDATA
->m_staticAlpha
= static_data
;
1985 unsigned char *wxImage::GetAlpha() const
1987 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
1989 return M_IMGDATA
->m_alpha
;
1992 void wxImage::InitAlpha()
1994 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1996 // initialize memory for alpha channel
1999 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
2000 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2004 // use the mask to initialize the alpha channel.
2005 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
2007 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
2008 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
2009 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
2010 for ( unsigned char *src
= M_IMGDATA
->m_data
;
2014 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
2015 ? wxIMAGE_ALPHA_TRANSPARENT
2016 : wxIMAGE_ALPHA_OPAQUE
;
2019 M_IMGDATA
->m_hasMask
= false;
2023 // make the image fully opaque
2024 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
2028 void wxImage::ClearAlpha()
2030 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
2032 if ( !M_IMGDATA
->m_staticAlpha
)
2033 free( M_IMGDATA
->m_alpha
);
2035 M_IMGDATA
->m_alpha
= NULL
;
2039 // ----------------------------------------------------------------------------
2041 // ----------------------------------------------------------------------------
2043 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
2045 wxCHECK_RET( IsOk(), wxT("invalid image") );
2049 M_IMGDATA
->m_maskRed
= r
;
2050 M_IMGDATA
->m_maskGreen
= g
;
2051 M_IMGDATA
->m_maskBlue
= b
;
2052 M_IMGDATA
->m_hasMask
= true;
2055 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
2057 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2059 if (M_IMGDATA
->m_hasMask
)
2061 if (r
) *r
= M_IMGDATA
->m_maskRed
;
2062 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
2063 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
2068 FindFirstUnusedColour(r
, g
, b
);
2073 unsigned char wxImage::GetMaskRed() const
2075 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2077 return M_IMGDATA
->m_maskRed
;
2080 unsigned char wxImage::GetMaskGreen() const
2082 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2084 return M_IMGDATA
->m_maskGreen
;
2087 unsigned char wxImage::GetMaskBlue() const
2089 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2091 return M_IMGDATA
->m_maskBlue
;
2094 void wxImage::SetMask( bool mask
)
2096 wxCHECK_RET( IsOk(), wxT("invalid image") );
2100 M_IMGDATA
->m_hasMask
= mask
;
2103 bool wxImage::HasMask() const
2105 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2107 return M_IMGDATA
->m_hasMask
;
2110 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
2112 long pos
= XYToIndex(x
, y
);
2113 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
2116 if ( M_IMGDATA
->m_hasMask
)
2118 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
2119 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
2120 p
[1] == M_IMGDATA
->m_maskGreen
&&
2121 p
[2] == M_IMGDATA
->m_maskBlue
)
2128 if ( M_IMGDATA
->m_alpha
)
2130 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2132 // transparent enough
2141 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2142 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2144 // check that the images are the same size
2145 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2147 wxLogError( _("Image and mask have different sizes.") );
2151 // find unused colour
2152 unsigned char r
,g
,b
;
2153 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2155 wxLogError( _("No unused colour in image being masked.") );
2161 unsigned char *imgdata
= GetData();
2162 unsigned char *maskdata
= mask
.GetData();
2164 const int w
= GetWidth();
2165 const int h
= GetHeight();
2167 for (int j
= 0; j
< h
; j
++)
2169 for (int i
= 0; i
< w
; i
++)
2171 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2182 SetMaskColour(r
, g
, b
);
2188 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2193 unsigned char mr
, mg
, mb
;
2194 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2196 wxLogError( _("No unused colour in image being masked.") );
2200 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2203 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2206 unsigned char threshold
)
2214 SetMaskColour(mr
, mg
, mb
);
2216 unsigned char *imgdata
= GetData();
2217 unsigned char *alphadata
= GetAlpha();
2220 int h
= GetHeight();
2222 for (int y
= 0; y
< h
; y
++)
2224 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2226 if (*alphadata
< threshold
)
2235 if ( !M_IMGDATA
->m_staticAlpha
)
2236 free(M_IMGDATA
->m_alpha
);
2238 M_IMGDATA
->m_alpha
= NULL
;
2239 M_IMGDATA
->m_staticAlpha
= false;
2244 // ----------------------------------------------------------------------------
2245 // Palette functions
2246 // ----------------------------------------------------------------------------
2250 bool wxImage::HasPalette() const
2255 return M_IMGDATA
->m_palette
.IsOk();
2258 const wxPalette
& wxImage::GetPalette() const
2260 wxCHECK_MSG( IsOk(), wxNullPalette
, wxT("invalid image") );
2262 return M_IMGDATA
->m_palette
;
2265 void wxImage::SetPalette(const wxPalette
& palette
)
2267 wxCHECK_RET( IsOk(), wxT("invalid image") );
2271 M_IMGDATA
->m_palette
= palette
;
2274 #endif // wxUSE_PALETTE
2276 // ----------------------------------------------------------------------------
2277 // Option functions (arbitrary name/value mapping)
2278 // ----------------------------------------------------------------------------
2280 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2284 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2285 if ( idx
== wxNOT_FOUND
)
2287 M_IMGDATA
->m_optionNames
.Add(name
);
2288 M_IMGDATA
->m_optionValues
.Add(value
);
2292 M_IMGDATA
->m_optionNames
[idx
] = name
;
2293 M_IMGDATA
->m_optionValues
[idx
] = value
;
2297 void wxImage::SetOption(const wxString
& name
, int value
)
2300 valStr
.Printf(wxT("%d"), value
);
2301 SetOption(name
, valStr
);
2304 wxString
wxImage::GetOption(const wxString
& name
) const
2307 return wxEmptyString
;
2309 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2310 if ( idx
== wxNOT_FOUND
)
2311 return wxEmptyString
;
2313 return M_IMGDATA
->m_optionValues
[idx
];
2316 int wxImage::GetOptionInt(const wxString
& name
) const
2318 return wxAtoi(GetOption(name
));
2321 bool wxImage::HasOption(const wxString
& name
) const
2323 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2327 // ----------------------------------------------------------------------------
2329 // ----------------------------------------------------------------------------
2331 // Under Windows we can load wxImage not only from files but also from
2333 #if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE
2334 #define HAS_LOAD_FROM_RESOURCE
2337 #ifdef HAS_LOAD_FROM_RESOURCE
2339 #include "wx/msw/dib.h"
2340 #include "wx/msw/private.h"
2342 static wxImage
LoadImageFromResource(const wxString
&name
, wxBitmapType type
)
2348 if ( type
== wxBITMAP_TYPE_BMP_RESOURCE
)
2350 hBitmap
.Init( ::LoadBitmap(wxGetInstance(), name
.t_str()) );
2354 wxLogError(_("Failed to load bitmap \"%s\" from resources."), name
);
2357 else if ( type
== wxBITMAP_TYPE_ICO_RESOURCE
)
2359 const HICON hIcon
= ::LoadIcon(wxGetInstance(), name
.t_str());
2363 wxLogError(_("Failed to load icon \"%s\" from resources."), name
);
2368 if ( !::GetIconInfo(hIcon
, &info
) )
2370 wxLogLastError(wxT("GetIconInfo"));
2374 hBitmap
.Init(info
.hbmColor
);
2375 hMask
.Init(info
.hbmMask
);
2378 else if ( type
== wxBITMAP_TYPE_CUR_RESOURCE
)
2380 wxLogDebug(wxS("Loading cursors from resources is not implemented."));
2384 wxFAIL_MSG(wxS("Invalid bitmap resource type."));
2390 wxImage image
= wxDIB(hBitmap
).ConvertToImage();
2393 const wxImage mask
= wxDIB(hMask
).ConvertToImage();
2394 image
.SetMaskFromImage(mask
, 255, 255, 255);
2398 // Light gray colour is a default mask
2399 image
.SetMaskColour(0xc0, 0xc0, 0xc0);
2402 // We could have already loaded alpha from the resources, but if not,
2403 // initialize it now using the mask.
2404 if ( !image
.HasAlpha() )
2410 #endif // HAS_LOAD_FROM_RESOURCE
2412 bool wxImage::LoadFile( const wxString
& filename
,
2414 int WXUNUSED_UNLESS_STREAMS(index
) )
2416 #ifdef HAS_LOAD_FROM_RESOURCE
2417 if ( type
== wxBITMAP_TYPE_BMP_RESOURCE
2418 || type
== wxBITMAP_TYPE_ICO_RESOURCE
2419 || type
== wxBITMAP_TYPE_CUR_RESOURCE
)
2421 const wxImage image
= ::LoadImageFromResource(filename
, type
);
2428 #endif // HAS_LOAD_FROM_RESOURCE
2430 #if HAS_FILE_STREAMS
2431 wxImageFileInputStream
stream(filename
);
2432 if ( stream
.IsOk() )
2434 wxBufferedInputStream
bstream( stream
);
2435 if ( LoadFile(bstream
, type
, index
) )
2439 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2440 #endif // HAS_FILE_STREAMS
2445 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2446 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2447 int WXUNUSED_UNLESS_STREAMS(index
) )
2449 #if HAS_FILE_STREAMS
2450 wxImageFileInputStream
stream(filename
);
2451 if ( stream
.IsOk() )
2453 wxBufferedInputStream
bstream( stream
);
2454 if ( LoadFile(bstream
, mimetype
, index
) )
2458 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2459 #endif // HAS_FILE_STREAMS
2465 bool wxImage::SaveFile( const wxString
& filename
) const
2467 wxString ext
= filename
.AfterLast('.').Lower();
2469 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2472 wxLogError(_("Can't save image to file '%s': unknown extension."),
2477 return SaveFile(filename
, handler
->GetType());
2480 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2481 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2483 #if HAS_FILE_STREAMS
2484 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2486 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2488 wxImageFileOutputStream
stream(filename
);
2490 if ( stream
.IsOk() )
2492 wxBufferedOutputStream
bstream( stream
);
2493 return SaveFile(bstream
, type
);
2495 #endif // HAS_FILE_STREAMS
2500 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2501 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2503 #if HAS_FILE_STREAMS
2504 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2506 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2508 wxImageFileOutputStream
stream(filename
);
2510 if ( stream
.IsOk() )
2512 wxBufferedOutputStream
bstream( stream
);
2513 return SaveFile(bstream
, mimetype
);
2515 #endif // HAS_FILE_STREAMS
2520 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2522 #if HAS_FILE_STREAMS
2523 wxImageFileInputStream
stream(name
);
2524 return CanRead(stream
);
2530 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2531 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2533 #if HAS_FILE_STREAMS
2534 wxImageFileInputStream
stream(name
);
2536 return GetImageCount(stream
, type
);
2544 bool wxImage::CanRead( wxInputStream
&stream
)
2546 const wxList
& list
= GetHandlers();
2548 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2550 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2551 if (handler
->CanRead( stream
))
2558 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2560 wxImageHandler
*handler
;
2562 if ( type
== wxBITMAP_TYPE_ANY
)
2564 const wxList
& list
= GetHandlers();
2566 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2568 node
= node
->GetNext() )
2570 handler
= (wxImageHandler
*)node
->GetData();
2571 if ( handler
->CanRead(stream
) )
2573 const int count
= handler
->GetImageCount(stream
);
2580 wxLogWarning(_("No handler found for image type."));
2584 handler
= FindHandler(type
);
2588 wxLogWarning(_("No image handler for type %d defined."), type
);
2592 if ( handler
->CanRead(stream
) )
2594 return handler
->GetImageCount(stream
);
2598 wxLogError(_("Image file is not of type %d."), type
);
2603 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2605 // save the options values which can be clobbered by the handler (e.g. many
2606 // of them call Destroy() before trying to load the file)
2607 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2608 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2610 // Preserve the original stream position if possible to rewind back to it
2611 // if we failed to load the file -- maybe the next handler that we try can
2612 // succeed after us then.
2613 wxFileOffset posOld
= wxInvalidOffset
;
2614 if ( stream
.IsSeekable() )
2615 posOld
= stream
.TellI();
2617 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2619 if ( posOld
!= wxInvalidOffset
)
2620 stream
.SeekI(posOld
);
2625 // rescale the image to the specified size if needed
2626 if ( maxWidth
|| maxHeight
)
2628 const unsigned widthOrig
= GetWidth(),
2629 heightOrig
= GetHeight();
2631 // this uses the same (trivial) algorithm as the JPEG handler
2632 unsigned width
= widthOrig
,
2633 height
= heightOrig
;
2634 while ( (maxWidth
&& width
> maxWidth
) ||
2635 (maxHeight
&& height
> maxHeight
) )
2641 if ( width
!= widthOrig
|| height
!= heightOrig
)
2643 // get the original size if it was set by the image handler
2644 // but also in order to restore it after Rescale
2645 int widthOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH
),
2646 heightOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
2648 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2650 SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH
, widthOrigOption
? widthOrigOption
: widthOrig
);
2651 SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT
, heightOrigOption
? heightOrigOption
: heightOrig
);
2655 // Set this after Rescale, which currently does not preserve it
2656 M_IMGDATA
->m_type
= handler
.GetType();
2661 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2665 wxImageHandler
*handler
;
2667 if ( type
== wxBITMAP_TYPE_ANY
)
2669 if ( !stream
.IsSeekable() )
2671 // The error message about image data format being unknown below
2672 // would be misleading in this case as we are not even going to try
2673 // any handlers because CanRead() never does anything for not
2674 // seekable stream, so try to be more precise here.
2675 wxLogError(_("Can't automatically determine the image format "
2676 "for non-seekable input."));
2680 const wxList
& list
= GetHandlers();
2681 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2683 node
= node
->GetNext() )
2685 handler
= (wxImageHandler
*)node
->GetData();
2686 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2690 wxLogWarning( _("Unknown image data format.") );
2694 //else: have specific type
2696 handler
= FindHandler(type
);
2699 wxLogWarning( _("No image handler for type %d defined."), type
);
2703 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2705 wxLogError(_("This is not a %s."), handler
->GetName());
2709 return DoLoad(*handler
, stream
, index
);
2712 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2716 m_refData
= new wxImageRefData
;
2718 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2722 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2726 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2728 wxLogError(_("Image is not of type %s."), mimetype
);
2732 return DoLoad(*handler
, stream
, index
);
2735 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2737 wxImage
* const self
= const_cast<wxImage
*>(this);
2738 if ( !handler
.SaveFile(self
, stream
) )
2741 M_IMGDATA
->m_type
= handler
.GetType();
2745 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2747 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2749 wxImageHandler
*handler
= FindHandler(type
);
2752 wxLogWarning( _("No image handler for type %d defined."), type
);
2756 return DoSave(*handler
, stream
);
2759 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2761 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2763 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2766 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2770 return DoSave(*handler
, stream
);
2773 #endif // wxUSE_STREAMS
2775 // ----------------------------------------------------------------------------
2776 // image I/O handlers
2777 // ----------------------------------------------------------------------------
2779 void wxImage::AddHandler( wxImageHandler
*handler
)
2781 // Check for an existing handler of the type being added.
2782 if (FindHandler( handler
->GetType() ) == 0)
2784 sm_handlers
.Append( handler
);
2788 // This is not documented behaviour, merely the simplest 'fix'
2789 // for preventing duplicate additions. If someone ever has
2790 // a good reason to add and remove duplicate handlers (and they
2791 // may) we should probably refcount the duplicates.
2792 // also an issue in InsertHandler below.
2794 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2795 handler
->GetName().c_str() );
2800 void wxImage::InsertHandler( wxImageHandler
*handler
)
2802 // Check for an existing handler of the type being added.
2803 if (FindHandler( handler
->GetType() ) == 0)
2805 sm_handlers
.Insert( handler
);
2809 // see AddHandler for additional comments.
2810 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2811 handler
->GetName().c_str() );
2816 bool wxImage::RemoveHandler( const wxString
& name
)
2818 wxImageHandler
*handler
= FindHandler(name
);
2821 sm_handlers
.DeleteObject(handler
);
2829 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2831 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2834 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2835 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2837 node
= node
->GetNext();
2842 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2844 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2847 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2848 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2850 if (handler
->GetExtension() == extension
)
2852 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2855 node
= node
->GetNext();
2860 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2862 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2865 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2866 if (handler
->GetType() == bitmapType
) return handler
;
2867 node
= node
->GetNext();
2872 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2874 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2877 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2878 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2879 node
= node
->GetNext();
2884 void wxImage::InitStandardHandlers()
2887 AddHandler(new wxBMPHandler
);
2888 #endif // wxUSE_STREAMS
2891 void wxImage::CleanUpHandlers()
2893 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2896 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2897 wxList::compatibility_iterator next
= node
->GetNext();
2902 sm_handlers
.Clear();
2905 wxString
wxImage::GetImageExtWildcard()
2909 wxList
& Handlers
= wxImage::GetHandlers();
2910 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2913 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2914 fmts
+= wxT("*.") + Handler
->GetExtension();
2915 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2916 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2917 Node
= Node
->GetNext();
2918 if ( Node
) fmts
+= wxT(";");
2921 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2924 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2926 const double red
= rgb
.red
/ 255.0,
2927 green
= rgb
.green
/ 255.0,
2928 blue
= rgb
.blue
/ 255.0;
2930 // find the min and max intensity (and remember which one was it for the
2932 double minimumRGB
= red
;
2933 if ( green
< minimumRGB
)
2935 if ( blue
< minimumRGB
)
2938 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2939 double maximumRGB
= red
;
2940 if ( green
> maximumRGB
)
2945 if ( blue
> maximumRGB
)
2951 const double value
= maximumRGB
;
2953 double hue
= 0.0, saturation
;
2954 const double deltaRGB
= maximumRGB
- minimumRGB
;
2955 if ( wxIsNullDouble(deltaRGB
) )
2957 // Gray has no color
2966 hue
= (green
- blue
) / deltaRGB
;
2970 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2974 hue
= 4.0 + (red
- green
) / deltaRGB
;
2983 saturation
= deltaRGB
/ maximumRGB
;
2986 return HSVValue(hue
, saturation
, value
);
2989 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2991 double red
, green
, blue
;
2993 if ( wxIsNullDouble(hsv
.saturation
) )
3002 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
3003 int i
= (int)floor(hue
);
3004 double f
= hue
- i
; // fractional part of h
3005 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
3011 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3016 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3024 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3029 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3034 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3042 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3047 return RGBValue((unsigned char)(red
* 255.0),
3048 (unsigned char)(green
* 255.0),
3049 (unsigned char)(blue
* 255.0));
3053 * Rotates the hue of each pixel of the image. angle is a double in the range
3054 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
3056 void wxImage::RotateHue(double angle
)
3060 unsigned char *srcBytePtr
;
3061 unsigned char *dstBytePtr
;
3062 unsigned long count
;
3063 wxImage::HSVValue hsv
;
3064 wxImage::RGBValue rgb
;
3066 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
3067 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
3068 if ( count
> 0 && !wxIsNullDouble(angle
) )
3070 srcBytePtr
= M_IMGDATA
->m_data
;
3071 dstBytePtr
= srcBytePtr
;
3074 rgb
.red
= *srcBytePtr
++;
3075 rgb
.green
= *srcBytePtr
++;
3076 rgb
.blue
= *srcBytePtr
++;
3077 hsv
= RGBtoHSV(rgb
);
3079 hsv
.hue
= hsv
.hue
+ angle
;
3081 hsv
.hue
= hsv
.hue
- 1.0;
3082 else if (hsv
.hue
< 0.0)
3083 hsv
.hue
= hsv
.hue
+ 1.0;
3085 rgb
= HSVtoRGB(hsv
);
3086 *dstBytePtr
++ = rgb
.red
;
3087 *dstBytePtr
++ = rgb
.green
;
3088 *dstBytePtr
++ = rgb
.blue
;
3089 } while (--count
!= 0);
3093 //-----------------------------------------------------------------------------
3095 //-----------------------------------------------------------------------------
3097 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
3100 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
3102 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3103 // wxImageHandler::CallDoCanRead
3105 if ( !stream
.IsSeekable() )
3106 return false; // can't test unseekable stream
3108 wxFileOffset posOld
= stream
.TellI();
3109 int n
= DoGetImageCount(stream
);
3111 // restore the old position to be able to test other formats and so on
3112 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
3114 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3116 // reading would fail anyhow as we're not at the right position
3123 bool wxImageHandler::CanRead( const wxString
& name
)
3125 wxImageFileInputStream
stream(name
);
3126 if ( !stream
.IsOk() )
3128 wxLogError(_("Failed to check format of image file \"%s\"."), name
);
3133 return CanRead(stream
);
3136 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
3138 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3139 // wxImageHandler::GetImageCount
3141 if ( !stream
.IsSeekable() )
3142 return false; // can't test unseekable stream
3144 wxFileOffset posOld
= stream
.TellI();
3145 bool ok
= DoCanRead(stream
);
3147 // restore the old position to be able to test other formats and so on
3148 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
3150 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3152 // reading would fail anyhow as we're not at the right position
3159 #endif // wxUSE_STREAMS
3163 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
3165 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
3167 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
3168 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
3170 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
3171 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
3173 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
3176 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
3178 else // no resolution options specified
3183 return wxIMAGE_RESOLUTION_NONE
;
3186 // get the resolution unit too
3187 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
3190 // this is the default
3191 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
3194 return (wxImageResolution
)resUnit
;
3197 // ----------------------------------------------------------------------------
3198 // image histogram stuff
3199 // ----------------------------------------------------------------------------
3202 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
3207 unsigned char g2
) const
3209 unsigned long key
= MakeKey(r2
, g2
, b2
);
3211 while ( find(key
) != end() )
3213 // color already used
3225 wxLogError(_("No unused colour in image.") );
3231 key
= MakeKey(r2
, g2
, b2
);
3245 wxImage::FindFirstUnusedColour(unsigned char *r
,
3250 unsigned char g2
) const
3252 wxImageHistogram histogram
;
3254 ComputeHistogram(histogram
);
3256 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3262 // Counts and returns the number of different colours. Optionally stops
3263 // when it exceeds 'stopafter' different colours. This is useful, for
3264 // example, to see if the image can be saved as 8-bit (256 colour or
3265 // less, in this case it would be invoked as CountColours(256)). Default
3266 // value for stopafter is -1 (don't care).
3268 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3272 unsigned char r
, g
, b
;
3274 unsigned long size
, nentries
, key
;
3277 size
= GetWidth() * GetHeight();
3280 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3285 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3287 if (h
.Get(key
) == NULL
)
3298 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3300 unsigned char *p
= GetData();
3301 unsigned long nentries
= 0;
3305 const unsigned long size
= GetWidth() * GetHeight();
3307 unsigned char r
, g
, b
;
3308 for ( unsigned long n
= 0; n
< size
; n
++ )
3314 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3316 if ( entry
.value
++ == 0 )
3317 entry
.index
= nentries
++;
3324 * Rotation code by Carlos Moreno
3327 static const double wxROTATE_EPSILON
= 1e-10;
3329 // Auxiliary function to rotate a point (x,y) with respect to point p0
3330 // make it inline and use a straight return to facilitate optimization
3331 // also, the function receives the sine and cosine of the angle to avoid
3332 // repeating the time-consuming calls to these functions -- sin/cos can
3333 // be computed and stored in the calling function.
3335 static inline wxRealPoint
3336 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3337 const wxRealPoint
& p0
)
3339 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3340 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3343 static inline wxRealPoint
3344 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3345 const wxRealPoint
& p0
)
3347 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3350 wxImage
wxImage::Rotate(double angle
,
3351 const wxPoint
& centre_of_rotation
,
3353 wxPoint
*offset_after_rotation
) const
3355 // screen coordinates are a mirror image of "real" coordinates
3358 const bool has_alpha
= HasAlpha();
3360 const int w
= GetWidth();
3361 const int h
= GetHeight();
3365 // Create pointer-based array to accelerate access to wxImage's data
3366 unsigned char ** data
= new unsigned char * [h
];
3367 data
[0] = GetData();
3368 for (i
= 1; i
< h
; i
++)
3369 data
[i
] = data
[i
- 1] + (3 * w
);
3371 // Same for alpha channel
3372 unsigned char ** alpha
= NULL
;
3375 alpha
= new unsigned char * [h
];
3376 alpha
[0] = GetAlpha();
3377 for (i
= 1; i
< h
; i
++)
3378 alpha
[i
] = alpha
[i
- 1] + w
;
3381 // precompute coefficients for rotation formula
3382 const double cos_angle
= cos(angle
);
3383 const double sin_angle
= sin(angle
);
3385 // Create new Image to store the result
3386 // First, find rectangle that covers the rotated image; to do that,
3387 // rotate the four corners
3389 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3391 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3392 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3393 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3394 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3396 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3397 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3398 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3399 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3401 // Create rotated image
3402 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3403 // With alpha channel
3407 if (offset_after_rotation
!= NULL
)
3409 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3412 // the rotated (destination) image is always accessed sequentially via this
3413 // pointer, there is no need for pointer-based arrays here
3414 unsigned char *dst
= rotated
.GetData();
3416 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3418 // if the original image has a mask, use its RGB values as the blank pixel,
3419 // else, fall back to default (black).
3420 unsigned char blank_r
= 0;
3421 unsigned char blank_g
= 0;
3422 unsigned char blank_b
= 0;
3426 blank_r
= GetMaskRed();
3427 blank_g
= GetMaskGreen();
3428 blank_b
= GetMaskBlue();
3429 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3432 // Now, for each point of the rotated image, find where it came from, by
3433 // performing an inverse rotation (a rotation of -angle) and getting the
3434 // pixel at those coordinates
3436 const int rH
= rotated
.GetHeight();
3437 const int rW
= rotated
.GetWidth();
3439 // do the (interpolating) test outside of the loops, so that it is done
3440 // only once, instead of repeating it for each pixel.
3443 for (int y
= 0; y
< rH
; y
++)
3445 for (int x
= 0; x
< rW
; x
++)
3447 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3449 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3450 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3452 // interpolate using the 4 enclosing grid-points. Those
3453 // points can be obtained using floor and ceiling of the
3454 // exact coordinates of the point
3457 if (0 < src
.x
&& src
.x
< w
- 1)
3459 x1
= wxRound(floor(src
.x
));
3460 x2
= wxRound(ceil(src
.x
));
3462 else // else means that x is near one of the borders (0 or width-1)
3464 x1
= x2
= wxRound (src
.x
);
3467 if (0 < src
.y
&& src
.y
< h
- 1)
3469 y1
= wxRound(floor(src
.y
));
3470 y2
= wxRound(ceil(src
.y
));
3474 y1
= y2
= wxRound (src
.y
);
3477 // get four points and the distances (square of the distance,
3478 // for efficiency reasons) for the interpolation formula
3480 // GRG: Do not calculate the points until they are
3481 // really needed -- this way we can calculate
3482 // just one, instead of four, if d1, d2, d3
3483 // or d4 are < wxROTATE_EPSILON
3485 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3486 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3487 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3488 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3490 // Now interpolate as a weighted average of the four surrounding
3491 // points, where the weights are the distances to each of those points
3493 // If the point is exactly at one point of the grid of the source
3494 // image, then don't interpolate -- just assign the pixel
3496 // d1,d2,d3,d4 are positive -- no need for abs()
3497 if (d1
< wxROTATE_EPSILON
)
3499 unsigned char *p
= data
[y1
] + (3 * x1
);
3505 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3507 else if (d2
< wxROTATE_EPSILON
)
3509 unsigned char *p
= data
[y1
] + (3 * x2
);
3515 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3517 else if (d3
< wxROTATE_EPSILON
)
3519 unsigned char *p
= data
[y2
] + (3 * x2
);
3525 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3527 else if (d4
< wxROTATE_EPSILON
)
3529 unsigned char *p
= data
[y2
] + (3 * x1
);
3535 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3539 // weights for the weighted average are proportional to the inverse of the distance
3540 unsigned char *v1
= data
[y1
] + (3 * x1
);
3541 unsigned char *v2
= data
[y1
] + (3 * x2
);
3542 unsigned char *v3
= data
[y2
] + (3 * x2
);
3543 unsigned char *v4
= data
[y2
] + (3 * x1
);
3545 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3549 *(dst
++) = (unsigned char)
3550 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3551 w3
* *(v3
++) + w4
* *(v4
++)) /
3552 (w1
+ w2
+ w3
+ w4
) );
3553 *(dst
++) = (unsigned char)
3554 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3555 w3
* *(v3
++) + w4
* *(v4
++)) /
3556 (w1
+ w2
+ w3
+ w4
) );
3557 *(dst
++) = (unsigned char)
3558 ( (w1
* *v1
+ w2
* *v2
+
3559 w3
* *v3
+ w4
* *v4
) /
3560 (w1
+ w2
+ w3
+ w4
) );
3564 v1
= alpha
[y1
] + (x1
);
3565 v2
= alpha
[y1
] + (x2
);
3566 v3
= alpha
[y2
] + (x2
);
3567 v4
= alpha
[y2
] + (x1
);
3569 *(alpha_dst
++) = (unsigned char)
3570 ( (w1
* *v1
+ w2
* *v2
+
3571 w3
* *v3
+ w4
* *v4
) /
3572 (w1
+ w2
+ w3
+ w4
) );
3588 else // not interpolating
3590 for (int y
= 0; y
< rH
; y
++)
3592 for (int x
= 0; x
< rW
; x
++)
3594 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3596 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3597 const int ys
= wxRound (src
.y
); // closest integer
3599 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3601 unsigned char *p
= data
[ys
] + (3 * xs
);
3607 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3616 *(alpha_dst
++) = 255;
3632 // A module to allow wxImage initialization/cleanup
3633 // without calling these functions from app.cpp or from
3634 // the user's application.
3636 class wxImageModule
: public wxModule
3638 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3641 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3642 void OnExit() { wxImage::CleanUpHandlers(); }
3645 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3648 #endif // wxUSE_IMAGE