1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
25 #include "wx/module.h"
26 #include "wx/palette.h"
28 #include "wx/colour.h"
31 #include "wx/wfstream.h"
32 #include "wx/xpmdecod.h"
37 // make the code compile with either wxFile*Stream or wxFFile*Stream:
38 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
42 typedef wxFFileInputStream wxImageFileInputStream
;
43 typedef wxFFileOutputStream wxImageFileOutputStream
;
45 typedef wxFileInputStream wxImageFileInputStream
;
46 typedef wxFileOutputStream wxImageFileOutputStream
;
47 #endif // wxUSE_FILE/wxUSE_FFILE
48 #endif // HAS_FILE_STREAMS
51 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 wxList
wxImage::sm_handlers
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 class wxImageRefData
: public wxObjectRefData
69 virtual ~wxImageRefData();
74 unsigned char *m_data
;
77 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
79 // alpha channel data, may be NULL for the formats without alpha support
80 unsigned char *m_alpha
;
84 // if true, m_data is pointer to static data and shouldn't be freed
87 // same as m_static but for m_alpha
92 #endif // wxUSE_PALETTE
94 wxArrayString m_optionNames
;
95 wxArrayString m_optionValues
;
97 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
100 wxImageRefData::wxImageRefData()
104 m_type
= wxBITMAP_TYPE_INVALID
;
106 m_alpha
= (unsigned char *) NULL
;
115 m_staticAlpha
= false;
118 wxImageRefData::~wxImageRefData()
122 if ( !m_staticAlpha
)
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
133 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
135 bool wxImage::Create(const char* const* xpmData
)
140 wxXPMDecoder decoder
;
141 (*this) = decoder
.ReadData(xpmData
);
144 wxUnusedVar(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( IsOk(), 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( IsOk(), 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( IsOk(), 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( IsOk(), 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 the method as specified.
448 case wxIMAGE_QUALITY_NEAREST
:
449 if ( old_width
% width
== 0 && old_width
>= width
&&
450 old_height
% height
== 0 && old_height
>= height
)
452 return ShrinkBy( old_width
/ width
, old_height
/ height
);
455 image
= ResampleNearest(width
, height
);
458 case wxIMAGE_QUALITY_BILINEAR
:
459 image
= ResampleBilinear(width
, height
);
462 case wxIMAGE_QUALITY_BICUBIC
:
463 image
= ResampleBicubic(width
, height
);
466 case wxIMAGE_QUALITY_BOX_AVERAGE
:
467 image
= ResampleBox(width
, height
);
470 case wxIMAGE_QUALITY_HIGH
:
471 image
= width
< old_width
&& height
< old_height
472 ? ResampleBox(width
, height
)
473 : ResampleBicubic(width
, height
);
477 // If the original image has a mask, apply the mask to the new image
478 if (M_IMGDATA
->m_hasMask
)
480 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
481 M_IMGDATA
->m_maskGreen
,
482 M_IMGDATA
->m_maskBlue
);
485 // In case this is a cursor, make sure the hotspot is scaled accordingly:
486 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
487 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
488 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
489 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
490 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
491 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
496 wxImage
wxImage::ResampleNearest(int width
, int height
) const
499 image
.Create( width
, height
, false );
501 unsigned char *data
= image
.GetData();
503 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
505 const unsigned char *source_data
= M_IMGDATA
->m_data
;
506 unsigned char *target_data
= data
;
507 const unsigned char *source_alpha
= 0 ;
508 unsigned char *target_alpha
= 0 ;
510 if ( !M_IMGDATA
->m_hasMask
)
512 source_alpha
= M_IMGDATA
->m_alpha
;
516 target_alpha
= image
.GetAlpha() ;
520 long old_height
= M_IMGDATA
->m_height
,
521 old_width
= M_IMGDATA
->m_width
;
522 long x_delta
= (old_width
<<16) / width
;
523 long y_delta
= (old_height
<<16) / height
;
525 unsigned char* dest_pixel
= target_data
;
528 for ( long j
= 0; j
< height
; j
++ )
530 const unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
531 const unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
534 for ( long i
= 0; i
< width
; i
++ )
536 const unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
537 const unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
538 dest_pixel
[0] = src_pixel
[0];
539 dest_pixel
[1] = src_pixel
[1];
540 dest_pixel
[2] = src_pixel
[2];
543 *(target_alpha
++) = *src_alpha_pixel
;
562 inline int BoxBetween(int value
, int low
, int high
)
564 return wxMax(wxMin(value
, high
), low
);
567 void ResampleBoxPrecalc(wxVector
<BoxPrecalc
>& boxes
, int oldDim
)
569 const int newDim
= boxes
.size();
570 const double scale_factor_1
= double(oldDim
) / newDim
;
571 const int scale_factor_2
= (int)(scale_factor_1
/ 2);
573 for ( int dst
= 0; dst
< newDim
; ++dst
)
575 // Source pixel in the Y direction
576 const int src_p
= int(dst
* scale_factor_1
);
578 BoxPrecalc
& precalc
= boxes
[dst
];
579 precalc
.boxStart
= BoxBetween(int(src_p
- scale_factor_1
/2.0 + 1),
581 precalc
.boxEnd
= BoxBetween(wxMax(precalc
.boxStart
+ 1,
582 int(src_p
+ scale_factor_2
)),
587 } // anonymous namespace
589 wxImage
wxImage::ResampleBox(int width
, int height
) const
591 // This function implements a simple pre-blur/box averaging method for
592 // downsampling that gives reasonably smooth results To scale the image
593 // down we will need to gather a grid of pixels of the size of the scale
594 // factor in each direction and then do an averaging of the pixels.
596 wxImage
ret_image(width
, height
, false);
598 wxVector
<BoxPrecalc
> vPrecalcs(height
);
599 wxVector
<BoxPrecalc
> hPrecalcs(width
);
601 ResampleBoxPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
602 ResampleBoxPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
605 const unsigned char* src_data
= M_IMGDATA
->m_data
;
606 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
607 unsigned char* dst_data
= ret_image
.GetData();
608 unsigned char* dst_alpha
= NULL
;
612 ret_image
.SetAlpha();
613 dst_alpha
= ret_image
.GetAlpha();
616 int averaged_pixels
, src_pixel_index
;
617 double sum_r
, sum_g
, sum_b
, sum_a
;
619 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
621 // Source pixel in the Y direction
622 const BoxPrecalc
& vPrecalc
= vPrecalcs
[y
];
624 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
626 // Source pixel in the X direction
627 const BoxPrecalc
& hPrecalc
= hPrecalcs
[x
];
629 // Box of pixels to average
631 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
633 for ( int j
= vPrecalc
.boxStart
; j
<= vPrecalc
.boxEnd
; ++j
)
635 for ( int i
= hPrecalc
.boxStart
; i
<= hPrecalc
.boxEnd
; ++i
)
637 // Calculate the actual index in our source pixels
638 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
640 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
641 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
642 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
644 sum_a
+= src_alpha
[src_pixel_index
];
650 // Calculate the average from the sum and number of averaged pixels
651 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
652 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
653 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
656 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
666 struct BilinearPrecalc
674 void ResampleBilinearPrecalc(wxVector
<BilinearPrecalc
>& precalcs
, int oldDim
)
676 const int newDim
= precalcs
.size();
677 const double scale_factor
= double(oldDim
) / newDim
;
678 const int srcpixmax
= oldDim
- 1;
680 for ( int dsty
= 0; dsty
< newDim
; dsty
++ )
682 // We need to calculate the source pixel to interpolate from - Y-axis
683 double srcpix
= double(dsty
) * scale_factor
;
684 double srcpix1
= int(srcpix
);
685 double srcpix2
= srcpix1
== srcpixmax
? srcpix1
: srcpix1
+ 1.0;
687 BilinearPrecalc
& precalc
= precalcs
[dsty
];
689 precalc
.dd
= srcpix
- (int)srcpix
;
690 precalc
.dd1
= 1.0 - precalc
.dd
;
691 precalc
.offset1
= srcpix1
< 0.0
693 : srcpix1
> srcpixmax
696 precalc
.offset2
= srcpix2
< 0.0
698 : srcpix2
> srcpixmax
704 } // anonymous namespace
706 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
708 // This function implements a Bilinear algorithm for resampling.
709 wxImage
ret_image(width
, height
, false);
710 const unsigned char* src_data
= M_IMGDATA
->m_data
;
711 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
712 unsigned char* dst_data
= ret_image
.GetData();
713 unsigned char* dst_alpha
= NULL
;
717 ret_image
.SetAlpha();
718 dst_alpha
= ret_image
.GetAlpha();
721 wxVector
<BilinearPrecalc
> vPrecalcs(height
);
722 wxVector
<BilinearPrecalc
> hPrecalcs(width
);
723 ResampleBilinearPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
724 ResampleBilinearPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
726 // initialize alpha values to avoid g++ warnings about possibly
727 // uninitialized variables
728 double r1
, g1
, b1
, a1
= 0;
729 double r2
, g2
, b2
, a2
= 0;
731 for ( int dsty
= 0; dsty
< height
; dsty
++ )
733 // We need to calculate the source pixel to interpolate from - Y-axis
734 const BilinearPrecalc
& vPrecalc
= vPrecalcs
[dsty
];
735 const int y_offset1
= vPrecalc
.offset1
;
736 const int y_offset2
= vPrecalc
.offset2
;
737 const double dy
= vPrecalc
.dd
;
738 const double dy1
= vPrecalc
.dd1
;
741 for ( int dstx
= 0; dstx
< width
; dstx
++ )
743 // X-axis of pixel to interpolate from
744 const BilinearPrecalc
& hPrecalc
= hPrecalcs
[dstx
];
746 const int x_offset1
= hPrecalc
.offset1
;
747 const int x_offset2
= hPrecalc
.offset2
;
748 const double dx
= hPrecalc
.dd
;
749 const double dx1
= hPrecalc
.dd1
;
751 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
752 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
753 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
754 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
757 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
758 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
759 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
761 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
764 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
765 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
766 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
768 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
772 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
773 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
774 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
778 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
785 // The following two local functions are for the B-spline weighting of the
786 // bicubic sampling algorithm
787 static inline double spline_cube(double value
)
789 return value
<= 0.0 ? 0.0 : value
* value
* value
;
792 static inline double spline_weight(double value
)
794 return (spline_cube(value
+ 2) -
795 4 * spline_cube(value
+ 1) +
796 6 * spline_cube(value
) -
797 4 * spline_cube(value
- 1)) / 6;
804 struct BicubicPrecalc
810 void ResampleBicubicPrecalc(wxVector
<BicubicPrecalc
> &aWeight
, int oldDim
)
812 const int newDim
= aWeight
.size();
813 for ( int dstd
= 0; dstd
< newDim
; dstd
++ )
815 // We need to calculate the source pixel to interpolate from - Y-axis
816 const double srcpixd
= static_cast<double>(dstd
* oldDim
) / newDim
;
817 const double dd
= srcpixd
- static_cast<int>(srcpixd
);
819 BicubicPrecalc
&precalc
= aWeight
[dstd
];
821 for ( int k
= -1; k
<= 2; k
++ )
823 precalc
.offset
[k
+ 1] = srcpixd
+ k
< 0.0
825 : srcpixd
+ k
>= oldDim
827 : static_cast<int>(srcpixd
+ k
);
829 precalc
.weight
[k
+ 1] = spline_weight(k
- dd
);
834 } // anonymous namespace
836 // This is the bicubic resampling algorithm
837 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
839 // This function implements a Bicubic B-Spline algorithm for resampling.
840 // This method is certainly a little slower than wxImage's default pixel
841 // replication method, however for most reasonably sized images not being
842 // upsampled too much on a fairly average CPU this difference is hardly
843 // noticeable and the results are far more pleasing to look at.
845 // This particular bicubic algorithm does pixel weighting according to a
846 // B-Spline that basically implements a Gaussian bell-like weighting
847 // kernel. Because of this method the results may appear a bit blurry when
848 // upsampling by large factors. This is basically because a slight
849 // gaussian blur is being performed to get the smooth look of the upsampled
852 // Edge pixels: 3-4 possible solutions
853 // - (Wrap/tile) Wrap the image, take the color value from the opposite
854 // side of the image.
855 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
856 // where n is nonpositive, will have the value of (2, 1).
857 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
858 // pixels which do have all neighbours.
859 // - (Clamp) Choose the nearest pixel along the border. This takes the
860 // border pixels and extends them out to infinity.
862 // NOTE: below the y_offset and x_offset variables are being set for edge
863 // pixels using the "Mirror" method mentioned above
867 ret_image
.Create(width
, height
, false);
869 const unsigned char* src_data
= M_IMGDATA
->m_data
;
870 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
871 unsigned char* dst_data
= ret_image
.GetData();
872 unsigned char* dst_alpha
= NULL
;
876 ret_image
.SetAlpha();
877 dst_alpha
= ret_image
.GetAlpha();
880 // Precalculate weights
881 wxVector
<BicubicPrecalc
> vPrecalcs(height
);
882 wxVector
<BicubicPrecalc
> hPrecalcs(width
);
884 ResampleBicubicPrecalc(vPrecalcs
, M_IMGDATA
->m_height
);
885 ResampleBicubicPrecalc(hPrecalcs
, M_IMGDATA
->m_width
);
887 for ( int dsty
= 0; dsty
< height
; dsty
++ )
889 // We need to calculate the source pixel to interpolate from - Y-axis
890 const BicubicPrecalc
& vPrecalc
= vPrecalcs
[dsty
];
892 for ( int dstx
= 0; dstx
< width
; dstx
++ )
894 // X-axis of pixel to interpolate from
895 const BicubicPrecalc
& hPrecalc
= hPrecalcs
[dstx
];
897 // Sums for each color channel
898 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
900 // Here we actually determine the RGBA values for the destination pixel
901 for ( int k
= -1; k
<= 2; k
++ )
904 const int y_offset
= vPrecalc
.offset
[k
+ 1];
906 // Loop across the X axis
907 for ( int i
= -1; i
<= 2; i
++ )
910 const int x_offset
= hPrecalc
.offset
[i
+ 1];
912 // Calculate the exact position where the source data
913 // should be pulled from based on the x_offset and y_offset
914 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
916 // Calculate the weight for the specified pixel according
917 // to the bicubic b-spline kernel we're using for
920 pixel_weight
= vPrecalc
.weight
[k
+ 1] * hPrecalc
.weight
[i
+ 1];
922 // Create a sum of all velues for each color channel
923 // adjusted for the pixel's calculated weight
924 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
925 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
926 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
928 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
932 // Put the data into the destination image. The summed values are
933 // of double data type and are rounded here for accuracy
934 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
935 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
936 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
940 *dst_alpha
++ = (unsigned char)sum_a
;
947 // Blur in the horizontal direction
948 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
950 wxImage
ret_image(MakeEmptyClone());
952 wxCHECK( ret_image
.IsOk(), ret_image
);
954 const unsigned char* src_data
= M_IMGDATA
->m_data
;
955 unsigned char* dst_data
= ret_image
.GetData();
956 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
957 unsigned char* dst_alpha
= ret_image
.GetAlpha();
959 // number of pixels we average over
960 const int blurArea
= blurRadius
*2 + 1;
962 // Horizontal blurring algorithm - average all pixels in the specified blur
963 // radius in the X or horizontal direction
964 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
966 // Variables used in the blurring algorithm
973 const unsigned char *src
;
976 // Calculate the average of all pixels in the blur radius for the first
978 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
980 // To deal with the pixels at the start of a row so it's not
981 // grabbing GOK values from memory at negative indices of the
982 // image's data or grabbing from the previous row
984 pixel_idx
= y
* M_IMGDATA
->m_width
;
986 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
988 src
= src_data
+ pixel_idx
*3;
993 sum_a
+= src_alpha
[pixel_idx
];
996 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
997 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
998 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
999 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1001 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1003 // Now average the values of the rest of the pixels by just moving the
1004 // blur radius box along the row
1005 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
1007 // Take care of edge pixels on the left edge by essentially
1008 // duplicating the edge pixel
1009 if ( x
- blurRadius
- 1 < 0 )
1010 pixel_idx
= y
* M_IMGDATA
->m_width
;
1012 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
1014 // Subtract the value of the pixel at the left side of the blur
1016 src
= src_data
+ pixel_idx
*3;
1021 sum_a
-= src_alpha
[pixel_idx
];
1023 // Take care of edge pixels on the right edge
1024 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
1025 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
1027 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
1029 // Add the value of the pixel being added to the end of our box
1030 src
= src_data
+ pixel_idx
*3;
1035 sum_a
+= src_alpha
[pixel_idx
];
1037 // Save off the averaged data
1038 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
1039 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1040 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1041 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1043 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1050 // Blur in the vertical direction
1051 wxImage
wxImage::BlurVertical(int blurRadius
) const
1053 wxImage
ret_image(MakeEmptyClone());
1055 wxCHECK( ret_image
.IsOk(), ret_image
);
1057 const unsigned char* src_data
= M_IMGDATA
->m_data
;
1058 unsigned char* dst_data
= ret_image
.GetData();
1059 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
1060 unsigned char* dst_alpha
= ret_image
.GetAlpha();
1062 // number of pixels we average over
1063 const int blurArea
= blurRadius
*2 + 1;
1065 // Vertical blurring algorithm - same as horizontal but switched the
1066 // opposite direction
1067 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
1069 // Variables used in the blurring algorithm
1076 const unsigned char *src
;
1079 // Calculate the average of all pixels in our blur radius box for the
1080 // first pixel of the column
1081 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
1083 // To deal with the pixels at the start of a column so it's not
1084 // grabbing GOK values from memory at negative indices of the
1085 // image's data or grabbing from the previous column
1089 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
1091 src
= src_data
+ pixel_idx
*3;
1096 sum_a
+= src_alpha
[pixel_idx
];
1099 dst
= dst_data
+ x
*3;
1100 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1101 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1102 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1104 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
1106 // Now average the values of the rest of the pixels by just moving the
1107 // box along the column from top to bottom
1108 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1110 // Take care of pixels that would be beyond the top edge by
1111 // duplicating the top edge pixel for the column
1112 if ( y
- blurRadius
- 1 < 0 )
1115 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1117 // Subtract the value of the pixel at the top of our blur radius box
1118 src
= src_data
+ pixel_idx
*3;
1123 sum_a
-= src_alpha
[pixel_idx
];
1125 // Take care of the pixels that would be beyond the bottom edge of
1126 // the image similar to the top edge
1127 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1128 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1130 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1132 // Add the value of the pixel being added to the end of our box
1133 src
= src_data
+ pixel_idx
*3;
1138 sum_a
+= src_alpha
[pixel_idx
];
1140 // Save off the averaged data
1141 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1142 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1143 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1144 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1146 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1153 // The new blur function
1154 wxImage
wxImage::Blur(int blurRadius
) const
1157 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1159 // Blur the image in each direction
1160 ret_image
= BlurHorizontal(blurRadius
);
1161 ret_image
= ret_image
.BlurVertical(blurRadius
);
1166 wxImage
wxImage::Rotate90( bool clockwise
) const
1168 wxImage
image(MakeEmptyClone(Clone_SwapOrientation
));
1170 wxCHECK( image
.IsOk(), image
);
1172 long height
= M_IMGDATA
->m_height
;
1173 long width
= M_IMGDATA
->m_width
;
1175 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1177 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1178 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1179 clockwise
? hot_x
: width
- 1 - hot_x
);
1182 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1184 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1185 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1186 clockwise
? height
- 1 - hot_y
: hot_y
);
1189 unsigned char *data
= image
.GetData();
1190 unsigned char *target_data
;
1192 // we rotate the image in 21-pixel (63-byte) wide strips
1193 // to make better use of cpu cache - memory transfers
1194 // (note: while much better than single-pixel "strips",
1195 // our vertical strips will still generally straddle 64-byte cachelines)
1196 for (long ii
= 0; ii
< width
; )
1198 long next_ii
= wxMin(ii
+ 21, width
);
1200 for (long j
= 0; j
< height
; j
++)
1202 const unsigned char *source_data
1203 = M_IMGDATA
->m_data
+ (j
*width
+ ii
)*3;
1205 for (long i
= ii
; i
< next_ii
; i
++)
1209 target_data
= data
+ ((i
+ 1)*height
- j
- 1)*3;
1213 target_data
= data
+ (height
*(width
- 1 - i
) + j
)*3;
1215 memcpy( target_data
, source_data
, 3 );
1223 const unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1227 unsigned char *alpha_data
= image
.GetAlpha();
1228 unsigned char *target_alpha
= 0 ;
1230 for (long ii
= 0; ii
< width
; )
1232 long next_ii
= wxMin(ii
+ 64, width
);
1234 for (long j
= 0; j
< height
; j
++)
1236 source_alpha
= M_IMGDATA
->m_alpha
+ j
*width
+ ii
;
1238 for (long i
= ii
; i
< next_ii
; i
++)
1242 target_alpha
= alpha_data
+ (i
+1)*height
- j
- 1;
1246 target_alpha
= alpha_data
+ height
*(width
- i
- 1) + j
;
1249 *target_alpha
= *source_alpha
++;
1260 wxImage
wxImage::Rotate180() const
1262 wxImage
image(MakeEmptyClone());
1264 wxCHECK( image
.IsOk(), image
);
1266 long height
= M_IMGDATA
->m_height
;
1267 long width
= M_IMGDATA
->m_width
;
1269 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1271 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1272 width
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
));
1275 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1277 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1278 height
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
));
1281 unsigned char *data
= image
.GetData();
1282 unsigned char *alpha
= image
.GetAlpha();
1283 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1284 unsigned char *target_data
= data
+ width
* height
* 3;
1286 for (long j
= 0; j
< height
; j
++)
1288 for (long i
= 0; i
< width
; i
++)
1291 memcpy( target_data
, source_data
, 3 );
1298 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1299 unsigned char *dest_alpha
= alpha
+ width
* height
;
1301 for (long j
= 0; j
< height
; ++j
)
1303 for (long i
= 0; i
< width
; ++i
)
1305 *(--dest_alpha
) = *(src_alpha
++);
1313 wxImage
wxImage::Mirror( bool horizontally
) const
1315 wxImage
image(MakeEmptyClone());
1317 wxCHECK( image
.IsOk(), image
);
1319 long height
= M_IMGDATA
->m_height
;
1320 long width
= M_IMGDATA
->m_width
;
1322 unsigned char *data
= image
.GetData();
1323 unsigned char *alpha
= image
.GetAlpha();
1324 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1325 unsigned char *target_data
;
1329 for (long j
= 0; j
< height
; j
++)
1332 target_data
= data
-3;
1333 for (long i
= 0; i
< width
; i
++)
1335 memcpy( target_data
, source_data
, 3 );
1343 // src_alpha starts at the first pixel and increases by 1 after each step
1344 // (a step here is the copy of the alpha value of one pixel)
1345 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1346 // dest_alpha starts just beyond the first line, decreases before each step,
1347 // and after each line is finished, increases by 2 widths (skipping the line
1348 // just copied and the line that will be copied next)
1349 unsigned char *dest_alpha
= alpha
+ width
;
1351 for (long jj
= 0; jj
< height
; ++jj
)
1353 for (long i
= 0; i
< width
; ++i
) {
1354 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1356 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1362 for (long i
= 0; i
< height
; i
++)
1364 target_data
= data
+ 3*width
*(height
-1-i
);
1365 memcpy( target_data
, source_data
, (size_t)3*width
);
1366 source_data
+= 3*width
;
1371 // src_alpha starts at the first pixel and increases by 1 width after each step
1372 // (a step here is the copy of the alpha channel of an entire line)
1373 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1374 // dest_alpha starts just beyond the last line (beyond the whole image)
1375 // and decreases by 1 width before each step
1376 unsigned char *dest_alpha
= alpha
+ width
* height
;
1378 for (long jj
= 0; jj
< height
; ++jj
)
1380 dest_alpha
-= width
;
1381 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1390 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1394 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1396 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1397 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1398 image
, wxT("invalid subimage size") );
1400 const int subwidth
= rect
.GetWidth();
1401 const int subheight
= rect
.GetHeight();
1403 image
.Create( subwidth
, subheight
, false );
1405 const unsigned char *src_data
= GetData();
1406 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1407 unsigned char *subdata
= image
.GetData();
1408 unsigned char *subalpha
= NULL
;
1410 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1414 subalpha
= image
.GetAlpha();
1415 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1418 if (M_IMGDATA
->m_hasMask
)
1419 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1421 const int width
= GetWidth();
1422 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1424 src_data
+= 3 * pixsoff
;
1425 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1427 for (long j
= 0; j
< subheight
; ++j
)
1429 memcpy( subdata
, src_data
, 3 * subwidth
);
1430 subdata
+= 3 * subwidth
;
1431 src_data
+= 3 * width
;
1432 if (subalpha
!= NULL
) {
1433 memcpy( subalpha
, src_alpha
, subwidth
);
1434 subalpha
+= subwidth
;
1442 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1443 int r_
, int g_
, int b_
) const
1447 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1448 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1450 int width
= GetWidth(), height
= GetHeight();
1451 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1453 unsigned char r
= (unsigned char)r_
;
1454 unsigned char g
= (unsigned char)g_
;
1455 unsigned char b
= (unsigned char)b_
;
1456 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1458 GetOrFindMaskColour( &r
, &g
, &b
);
1459 image
.SetMaskColour(r
, g
, b
);
1462 image
.SetRGB(wxRect(), r
, g
, b
);
1464 // we have two coordinate systems:
1465 // source: starting at 0,0 of source image
1466 // destination starting at 0,0 of destination image
1467 // Documentation says:
1468 // "The image is pasted into a new image [...] at the position pos relative
1469 // to the upper left of the new image." this means the transition rule is:
1470 // "dest coord" = "source coord" + pos;
1472 // calculate the intersection using source coordinates:
1473 wxRect
srcRect(0, 0, width
, height
);
1474 wxRect
dstRect(-pos
, size
);
1476 srcRect
.Intersect(dstRect
);
1478 if (!srcRect
.IsEmpty())
1480 // insertion point is needed in destination coordinates.
1481 // NB: it is not always "pos"!
1482 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1484 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1485 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1487 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1493 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1495 wxCHECK_RET( IsOk(), wxT("invalid image") );
1496 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1502 int width
= image
.GetWidth();
1503 int height
= image
.GetHeight();
1516 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1517 width
= M_IMGDATA
->m_width
- (x
+xx
);
1518 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1519 height
= M_IMGDATA
->m_height
- (y
+yy
);
1521 if (width
< 1) return;
1522 if (height
< 1) return;
1524 // If we can, copy the data using memcpy() as this is the fastest way. But
1525 // for this the image being pasted must have "compatible" mask with this
1526 // one meaning that either it must not have one at all or it must use the
1527 // same masked colour.
1528 if ( !image
.HasMask() ||
1530 (GetMaskRed()==image
.GetMaskRed()) &&
1531 (GetMaskGreen()==image
.GetMaskGreen()) &&
1532 (GetMaskBlue()==image
.GetMaskBlue()))) )
1534 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1535 int source_step
= image
.GetWidth()*3;
1537 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1538 int target_step
= M_IMGDATA
->m_width
*3;
1539 for (int j
= 0; j
< height
; j
++)
1541 memcpy( target_data
, source_data
, width
*3 );
1542 source_data
+= source_step
;
1543 target_data
+= target_step
;
1547 // Copy over the alpha channel from the original image
1548 if ( image
.HasAlpha() )
1553 const unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1554 int source_step
= image
.GetWidth();
1556 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1557 int target_step
= M_IMGDATA
->m_width
;
1559 for (int j
= 0; j
< height
; j
++,
1560 source_data
+= source_step
,
1561 target_data
+= target_step
)
1563 memcpy( target_data
, source_data
, width
);
1567 if (!HasMask() && image
.HasMask())
1569 unsigned char r
= image
.GetMaskRed();
1570 unsigned char g
= image
.GetMaskGreen();
1571 unsigned char b
= image
.GetMaskBlue();
1573 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1574 int source_step
= image
.GetWidth()*3;
1576 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1577 int target_step
= M_IMGDATA
->m_width
*3;
1579 for (int j
= 0; j
< height
; j
++)
1581 for (int i
= 0; i
< width
*3; i
+=3)
1583 if ((source_data
[i
] != r
) ||
1584 (source_data
[i
+1] != g
) ||
1585 (source_data
[i
+2] != b
))
1587 memcpy( target_data
+i
, source_data
+i
, 3 );
1590 source_data
+= source_step
;
1591 target_data
+= target_step
;
1596 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1597 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1599 wxCHECK_RET( IsOk(), wxT("invalid image") );
1603 unsigned char *data
= GetData();
1605 const int w
= GetWidth();
1606 const int h
= GetHeight();
1608 for (int j
= 0; j
< h
; j
++)
1609 for (int i
= 0; i
< w
; i
++)
1611 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1621 wxImage
wxImage::ConvertToGreyscale(void) const
1623 return ConvertToGreyscale(0.299, 0.587, 0.114);
1626 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1629 wxCHECK_MSG(IsOk(), image
, "invalid image");
1631 const int w
= M_IMGDATA
->m_width
;
1632 const int h
= M_IMGDATA
->m_height
;
1633 size_t size
= size_t(w
) * h
;
1634 image
.Create(w
, h
, false);
1635 const unsigned char* alpha
= M_IMGDATA
->m_alpha
;
1639 memcpy(image
.GetAlpha(), alpha
, size
);
1641 const unsigned char mask_r
= M_IMGDATA
->m_maskRed
;
1642 const unsigned char mask_g
= M_IMGDATA
->m_maskGreen
;
1643 const unsigned char mask_b
= M_IMGDATA
->m_maskBlue
;
1644 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1646 image
.SetMaskColour(mask_r
, mask_g
, mask_b
);
1648 const unsigned char* src
= M_IMGDATA
->m_data
;
1649 unsigned char* dst
= image
.GetData();
1652 unsigned char r
= *src
++;
1653 unsigned char g
= *src
++;
1654 unsigned char b
= *src
++;
1655 if (!hasMask
|| r
!= mask_r
|| g
!= mask_g
|| b
!= mask_b
)
1656 wxColour::MakeGrey(&r
, &g
, &b
, weight_r
, weight_g
, weight_b
);
1664 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1668 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1670 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1672 unsigned char *data
= image
.GetData();
1674 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1676 if (M_IMGDATA
->m_hasMask
)
1678 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1679 M_IMGDATA
->m_maskBlue
== b
)
1680 image
.SetMaskColour( 255, 255, 255 );
1682 image
.SetMaskColour( 0, 0, 0 );
1685 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1687 unsigned char *srcd
= M_IMGDATA
->m_data
;
1688 unsigned char *tard
= image
.GetData();
1690 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1692 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1693 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1699 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1702 wxCHECK_MSG(IsOk(), image
, "invalid image");
1704 const int w
= M_IMGDATA
->m_width
;
1705 const int h
= M_IMGDATA
->m_height
;
1706 size_t size
= size_t(w
) * h
;
1707 image
.Create(w
, h
, false);
1708 const unsigned char* alpha
= M_IMGDATA
->m_alpha
;
1712 memcpy(image
.GetAlpha(), alpha
, size
);
1714 const unsigned char mask_r
= M_IMGDATA
->m_maskRed
;
1715 const unsigned char mask_g
= M_IMGDATA
->m_maskGreen
;
1716 const unsigned char mask_b
= M_IMGDATA
->m_maskBlue
;
1717 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1719 image
.SetMaskColour(mask_r
, mask_g
, mask_b
);
1721 const unsigned char* src
= M_IMGDATA
->m_data
;
1722 unsigned char* dst
= image
.GetData();
1725 unsigned char r
= *src
++;
1726 unsigned char g
= *src
++;
1727 unsigned char b
= *src
++;
1728 if (!hasMask
|| r
!= mask_r
|| g
!= mask_g
|| b
!= mask_b
)
1729 wxColour::MakeDisabled(&r
, &g
, &b
, brightness
);
1737 int wxImage::GetWidth() const
1739 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1741 return M_IMGDATA
->m_width
;
1744 int wxImage::GetHeight() const
1746 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1748 return M_IMGDATA
->m_height
;
1751 wxBitmapType
wxImage::GetType() const
1753 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1755 return M_IMGDATA
->m_type
;
1758 void wxImage::SetType(wxBitmapType type
)
1760 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1762 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1763 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1765 M_IMGDATA
->m_type
= type
;
1768 long wxImage::XYToIndex(int x
, int y
) const
1772 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1774 return y
*M_IMGDATA
->m_width
+ x
;
1780 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1782 long pos
= XYToIndex(x
, y
);
1783 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1789 M_IMGDATA
->m_data
[ pos
] = r
;
1790 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1791 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1794 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1796 wxCHECK_RET( IsOk(), wxT("invalid image") );
1801 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1802 if ( rect
== wxRect() )
1808 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1809 imageRect
.Contains(rect
.GetBottomRight()),
1810 wxT("invalid bounding rectangle") );
1813 int x1
= rect
.GetLeft(),
1815 x2
= rect
.GetRight() + 1,
1816 y2
= rect
.GetBottom() + 1;
1818 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1819 int x
, y
, width
= GetWidth();
1820 for (y
= y1
; y
< y2
; y
++)
1822 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1823 for (x
= x1
; x
< x2
; x
++)
1832 unsigned char wxImage::GetRed( int x
, int y
) const
1834 long pos
= XYToIndex(x
, y
);
1835 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1839 return M_IMGDATA
->m_data
[pos
];
1842 unsigned char wxImage::GetGreen( int x
, int y
) const
1844 long pos
= XYToIndex(x
, y
);
1845 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1849 return M_IMGDATA
->m_data
[pos
+1];
1852 unsigned char wxImage::GetBlue( int x
, int y
) const
1854 long pos
= XYToIndex(x
, y
);
1855 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1859 return M_IMGDATA
->m_data
[pos
+2];
1862 bool wxImage::IsOk() const
1864 // image of 0 width or height can't be considered ok - at least because it
1865 // causes crashes in ConvertToBitmap() if we don't catch it in time
1866 wxImageRefData
*data
= M_IMGDATA
;
1867 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1870 unsigned char *wxImage::GetData() const
1872 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
1874 return M_IMGDATA
->m_data
;
1877 void wxImage::SetData( unsigned char *data
, bool static_data
)
1879 wxCHECK_RET( IsOk(), wxT("invalid image") );
1881 wxImageRefData
*newRefData
= new wxImageRefData();
1883 newRefData
->m_width
= M_IMGDATA
->m_width
;
1884 newRefData
->m_height
= M_IMGDATA
->m_height
;
1885 newRefData
->m_data
= data
;
1886 newRefData
->m_ok
= true;
1887 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1888 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1889 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1890 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1891 newRefData
->m_static
= static_data
;
1895 m_refData
= newRefData
;
1898 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1900 wxImageRefData
*newRefData
= new wxImageRefData();
1904 newRefData
->m_width
= new_width
;
1905 newRefData
->m_height
= new_height
;
1906 newRefData
->m_data
= data
;
1907 newRefData
->m_ok
= true;
1908 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1909 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1910 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1911 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1915 newRefData
->m_width
= new_width
;
1916 newRefData
->m_height
= new_height
;
1917 newRefData
->m_data
= data
;
1918 newRefData
->m_ok
= true;
1920 newRefData
->m_static
= static_data
;
1924 m_refData
= newRefData
;
1927 // ----------------------------------------------------------------------------
1928 // alpha channel support
1929 // ----------------------------------------------------------------------------
1931 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1933 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1935 long pos
= XYToIndex(x
, y
);
1936 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1940 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1943 unsigned char wxImage::GetAlpha(int x
, int y
) const
1945 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1947 long pos
= XYToIndex(x
, y
);
1948 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1950 return M_IMGDATA
->m_alpha
[pos
];
1954 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1958 const int w
= M_IMGDATA
->m_width
;
1959 const int h
= M_IMGDATA
->m_height
;
1961 unsigned char *alpha
= GetAlpha();
1962 unsigned char *data
= GetData();
1964 for ( int y
= 0; y
< h
; y
++ )
1966 for ( int x
= 0; x
< w
; x
++ )
1978 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1980 wxCHECK_RET( IsOk(), wxT("invalid image") );
1986 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1989 if( !M_IMGDATA
->m_staticAlpha
)
1990 free(M_IMGDATA
->m_alpha
);
1992 M_IMGDATA
->m_alpha
= alpha
;
1993 M_IMGDATA
->m_staticAlpha
= static_data
;
1996 unsigned char *wxImage::GetAlpha() const
1998 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
2000 return M_IMGDATA
->m_alpha
;
2003 void wxImage::InitAlpha()
2005 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
2007 // initialize memory for alpha channel
2010 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
2011 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2015 // use the mask to initialize the alpha channel.
2016 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
2018 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
2019 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
2020 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
2021 for ( unsigned char *src
= M_IMGDATA
->m_data
;
2025 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
2026 ? wxIMAGE_ALPHA_TRANSPARENT
2027 : wxIMAGE_ALPHA_OPAQUE
;
2030 M_IMGDATA
->m_hasMask
= false;
2034 // make the image fully opaque
2035 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
2039 void wxImage::ClearAlpha()
2041 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
2043 if ( !M_IMGDATA
->m_staticAlpha
)
2044 free( M_IMGDATA
->m_alpha
);
2046 M_IMGDATA
->m_alpha
= NULL
;
2050 // ----------------------------------------------------------------------------
2052 // ----------------------------------------------------------------------------
2054 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
2056 wxCHECK_RET( IsOk(), wxT("invalid image") );
2060 M_IMGDATA
->m_maskRed
= r
;
2061 M_IMGDATA
->m_maskGreen
= g
;
2062 M_IMGDATA
->m_maskBlue
= b
;
2063 M_IMGDATA
->m_hasMask
= true;
2066 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
2068 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2070 if (M_IMGDATA
->m_hasMask
)
2072 if (r
) *r
= M_IMGDATA
->m_maskRed
;
2073 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
2074 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
2079 FindFirstUnusedColour(r
, g
, b
);
2084 unsigned char wxImage::GetMaskRed() const
2086 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2088 return M_IMGDATA
->m_maskRed
;
2091 unsigned char wxImage::GetMaskGreen() const
2093 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2095 return M_IMGDATA
->m_maskGreen
;
2098 unsigned char wxImage::GetMaskBlue() const
2100 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
2102 return M_IMGDATA
->m_maskBlue
;
2105 void wxImage::SetMask( bool mask
)
2107 wxCHECK_RET( IsOk(), wxT("invalid image") );
2111 M_IMGDATA
->m_hasMask
= mask
;
2114 bool wxImage::HasMask() const
2116 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2118 return M_IMGDATA
->m_hasMask
;
2121 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
2123 long pos
= XYToIndex(x
, y
);
2124 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
2127 if ( M_IMGDATA
->m_hasMask
)
2129 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
2130 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
2131 p
[1] == M_IMGDATA
->m_maskGreen
&&
2132 p
[2] == M_IMGDATA
->m_maskBlue
)
2139 if ( M_IMGDATA
->m_alpha
)
2141 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2143 // transparent enough
2152 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2153 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2155 // check that the images are the same size
2156 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2158 wxLogError( _("Image and mask have different sizes.") );
2162 // find unused colour
2163 unsigned char r
,g
,b
;
2164 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2166 wxLogError( _("No unused colour in image being masked.") );
2172 unsigned char *imgdata
= GetData();
2173 unsigned char *maskdata
= mask
.GetData();
2175 const int w
= GetWidth();
2176 const int h
= GetHeight();
2178 for (int j
= 0; j
< h
; j
++)
2180 for (int i
= 0; i
< w
; i
++)
2182 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2193 SetMaskColour(r
, g
, b
);
2199 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2204 unsigned char mr
, mg
, mb
;
2205 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2207 wxLogError( _("No unused colour in image being masked.") );
2211 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2214 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2217 unsigned char threshold
)
2225 SetMaskColour(mr
, mg
, mb
);
2227 unsigned char *imgdata
= GetData();
2228 unsigned char *alphadata
= GetAlpha();
2231 int h
= GetHeight();
2233 for (int y
= 0; y
< h
; y
++)
2235 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2237 if (*alphadata
< threshold
)
2246 if ( !M_IMGDATA
->m_staticAlpha
)
2247 free(M_IMGDATA
->m_alpha
);
2249 M_IMGDATA
->m_alpha
= NULL
;
2250 M_IMGDATA
->m_staticAlpha
= false;
2255 // ----------------------------------------------------------------------------
2256 // Palette functions
2257 // ----------------------------------------------------------------------------
2261 bool wxImage::HasPalette() const
2266 return M_IMGDATA
->m_palette
.IsOk();
2269 const wxPalette
& wxImage::GetPalette() const
2271 wxCHECK_MSG( IsOk(), wxNullPalette
, wxT("invalid image") );
2273 return M_IMGDATA
->m_palette
;
2276 void wxImage::SetPalette(const wxPalette
& palette
)
2278 wxCHECK_RET( IsOk(), wxT("invalid image") );
2282 M_IMGDATA
->m_palette
= palette
;
2285 #endif // wxUSE_PALETTE
2287 // ----------------------------------------------------------------------------
2288 // Option functions (arbitrary name/value mapping)
2289 // ----------------------------------------------------------------------------
2291 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2295 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2296 if ( idx
== wxNOT_FOUND
)
2298 M_IMGDATA
->m_optionNames
.Add(name
);
2299 M_IMGDATA
->m_optionValues
.Add(value
);
2303 M_IMGDATA
->m_optionNames
[idx
] = name
;
2304 M_IMGDATA
->m_optionValues
[idx
] = value
;
2308 void wxImage::SetOption(const wxString
& name
, int value
)
2311 valStr
.Printf(wxT("%d"), value
);
2312 SetOption(name
, valStr
);
2315 wxString
wxImage::GetOption(const wxString
& name
) const
2318 return wxEmptyString
;
2320 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2321 if ( idx
== wxNOT_FOUND
)
2322 return wxEmptyString
;
2324 return M_IMGDATA
->m_optionValues
[idx
];
2327 int wxImage::GetOptionInt(const wxString
& name
) const
2329 return wxAtoi(GetOption(name
));
2332 bool wxImage::HasOption(const wxString
& name
) const
2334 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2338 // ----------------------------------------------------------------------------
2340 // ----------------------------------------------------------------------------
2342 // Under Windows we can load wxImage not only from files but also from
2344 #if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE
2345 #define HAS_LOAD_FROM_RESOURCE
2348 #ifdef HAS_LOAD_FROM_RESOURCE
2350 #include "wx/msw/dib.h"
2351 #include "wx/msw/private.h"
2353 static wxImage
LoadImageFromResource(const wxString
&name
, wxBitmapType type
)
2359 if ( type
== wxBITMAP_TYPE_BMP_RESOURCE
)
2361 hBitmap
.Init( ::LoadBitmap(wxGetInstance(), name
.t_str()) );
2365 wxLogError(_("Failed to load bitmap \"%s\" from resources."), name
);
2368 else if ( type
== wxBITMAP_TYPE_ICO_RESOURCE
)
2370 const HICON hIcon
= ::LoadIcon(wxGetInstance(), name
.t_str());
2374 wxLogError(_("Failed to load icon \"%s\" from resources."), name
);
2379 if ( !::GetIconInfo(hIcon
, &info
) )
2381 wxLogLastError(wxT("GetIconInfo"));
2385 hBitmap
.Init(info
.hbmColor
);
2386 hMask
.Init(info
.hbmMask
);
2389 else if ( type
== wxBITMAP_TYPE_CUR_RESOURCE
)
2391 wxLogDebug(wxS("Loading cursors from resources is not implemented."));
2395 wxFAIL_MSG(wxS("Invalid bitmap resource type."));
2401 wxImage image
= wxDIB(hBitmap
).ConvertToImage();
2404 const wxImage mask
= wxDIB(hMask
).ConvertToImage();
2405 image
.SetMaskFromImage(mask
, 255, 255, 255);
2409 // Light gray colour is a default mask
2410 image
.SetMaskColour(0xc0, 0xc0, 0xc0);
2413 // We could have already loaded alpha from the resources, but if not,
2414 // initialize it now using the mask.
2415 if ( !image
.HasAlpha() )
2421 #endif // HAS_LOAD_FROM_RESOURCE
2423 bool wxImage::LoadFile( const wxString
& filename
,
2425 int WXUNUSED_UNLESS_STREAMS(index
) )
2427 #ifdef HAS_LOAD_FROM_RESOURCE
2428 if ( type
== wxBITMAP_TYPE_BMP_RESOURCE
2429 || type
== wxBITMAP_TYPE_ICO_RESOURCE
2430 || type
== wxBITMAP_TYPE_CUR_RESOURCE
)
2432 const wxImage image
= ::LoadImageFromResource(filename
, type
);
2439 #endif // HAS_LOAD_FROM_RESOURCE
2441 #if HAS_FILE_STREAMS
2442 wxImageFileInputStream
stream(filename
);
2443 if ( stream
.IsOk() )
2445 wxBufferedInputStream
bstream( stream
);
2446 if ( LoadFile(bstream
, type
, index
) )
2450 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2451 #endif // HAS_FILE_STREAMS
2456 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2457 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2458 int WXUNUSED_UNLESS_STREAMS(index
) )
2460 #if HAS_FILE_STREAMS
2461 wxImageFileInputStream
stream(filename
);
2462 if ( stream
.IsOk() )
2464 wxBufferedInputStream
bstream( stream
);
2465 if ( LoadFile(bstream
, mimetype
, index
) )
2469 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2470 #endif // HAS_FILE_STREAMS
2476 bool wxImage::SaveFile( const wxString
& filename
) const
2478 wxString ext
= filename
.AfterLast('.').Lower();
2480 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2483 wxLogError(_("Can't save image to file '%s': unknown extension."),
2488 return SaveFile(filename
, handler
->GetType());
2491 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2492 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2494 #if HAS_FILE_STREAMS
2495 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2497 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2499 wxImageFileOutputStream
stream(filename
);
2501 if ( stream
.IsOk() )
2503 wxBufferedOutputStream
bstream( stream
);
2504 return SaveFile(bstream
, type
);
2506 #endif // HAS_FILE_STREAMS
2511 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2512 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2514 #if HAS_FILE_STREAMS
2515 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2517 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2519 wxImageFileOutputStream
stream(filename
);
2521 if ( stream
.IsOk() )
2523 wxBufferedOutputStream
bstream( stream
);
2524 return SaveFile(bstream
, mimetype
);
2526 #endif // HAS_FILE_STREAMS
2531 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2533 #if HAS_FILE_STREAMS
2534 wxImageFileInputStream
stream(name
);
2535 return CanRead(stream
);
2541 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2542 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2544 #if HAS_FILE_STREAMS
2545 wxImageFileInputStream
stream(name
);
2547 return GetImageCount(stream
, type
);
2555 bool wxImage::CanRead( wxInputStream
&stream
)
2557 const wxList
& list
= GetHandlers();
2559 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2561 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2562 if (handler
->CanRead( stream
))
2569 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2571 wxImageHandler
*handler
;
2573 if ( type
== wxBITMAP_TYPE_ANY
)
2575 const wxList
& list
= GetHandlers();
2577 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2579 node
= node
->GetNext() )
2581 handler
= (wxImageHandler
*)node
->GetData();
2582 if ( handler
->CanRead(stream
) )
2584 const int count
= handler
->GetImageCount(stream
);
2591 wxLogWarning(_("No handler found for image type."));
2595 handler
= FindHandler(type
);
2599 wxLogWarning(_("No image handler for type %d defined."), type
);
2603 if ( handler
->CanRead(stream
) )
2605 return handler
->GetImageCount(stream
);
2609 wxLogError(_("Image file is not of type %d."), type
);
2614 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2616 // save the options values which can be clobbered by the handler (e.g. many
2617 // of them call Destroy() before trying to load the file)
2618 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2619 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2621 // Preserve the original stream position if possible to rewind back to it
2622 // if we failed to load the file -- maybe the next handler that we try can
2623 // succeed after us then.
2624 wxFileOffset posOld
= wxInvalidOffset
;
2625 if ( stream
.IsSeekable() )
2626 posOld
= stream
.TellI();
2628 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2630 if ( posOld
!= wxInvalidOffset
)
2631 stream
.SeekI(posOld
);
2636 // rescale the image to the specified size if needed
2637 if ( maxWidth
|| maxHeight
)
2639 const unsigned widthOrig
= GetWidth(),
2640 heightOrig
= GetHeight();
2642 // this uses the same (trivial) algorithm as the JPEG handler
2643 unsigned width
= widthOrig
,
2644 height
= heightOrig
;
2645 while ( (maxWidth
&& width
> maxWidth
) ||
2646 (maxHeight
&& height
> maxHeight
) )
2652 if ( width
!= widthOrig
|| height
!= heightOrig
)
2654 // get the original size if it was set by the image handler
2655 // but also in order to restore it after Rescale
2656 int widthOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH
),
2657 heightOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
2659 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2661 SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH
, widthOrigOption
? widthOrigOption
: widthOrig
);
2662 SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT
, heightOrigOption
? heightOrigOption
: heightOrig
);
2666 // Set this after Rescale, which currently does not preserve it
2667 M_IMGDATA
->m_type
= handler
.GetType();
2672 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2676 wxImageHandler
*handler
;
2678 if ( type
== wxBITMAP_TYPE_ANY
)
2680 if ( !stream
.IsSeekable() )
2682 // The error message about image data format being unknown below
2683 // would be misleading in this case as we are not even going to try
2684 // any handlers because CanRead() never does anything for not
2685 // seekable stream, so try to be more precise here.
2686 wxLogError(_("Can't automatically determine the image format "
2687 "for non-seekable input."));
2691 const wxList
& list
= GetHandlers();
2692 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2694 node
= node
->GetNext() )
2696 handler
= (wxImageHandler
*)node
->GetData();
2697 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2701 wxLogWarning( _("Unknown image data format.") );
2705 //else: have specific type
2707 handler
= FindHandler(type
);
2710 wxLogWarning( _("No image handler for type %d defined."), type
);
2714 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2716 wxLogError(_("This is not a %s."), handler
->GetName());
2720 return DoLoad(*handler
, stream
, index
);
2723 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2727 m_refData
= new wxImageRefData
;
2729 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2733 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2737 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2739 wxLogError(_("Image is not of type %s."), mimetype
);
2743 return DoLoad(*handler
, stream
, index
);
2746 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2748 wxImage
* const self
= const_cast<wxImage
*>(this);
2749 if ( !handler
.SaveFile(self
, stream
) )
2752 M_IMGDATA
->m_type
= handler
.GetType();
2756 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2758 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2760 wxImageHandler
*handler
= FindHandler(type
);
2763 wxLogWarning( _("No image handler for type %d defined."), type
);
2767 return DoSave(*handler
, stream
);
2770 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2772 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2774 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2777 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2781 return DoSave(*handler
, stream
);
2784 #endif // wxUSE_STREAMS
2786 // ----------------------------------------------------------------------------
2787 // image I/O handlers
2788 // ----------------------------------------------------------------------------
2790 void wxImage::AddHandler( wxImageHandler
*handler
)
2792 // Check for an existing handler of the type being added.
2793 if (FindHandler( handler
->GetType() ) == 0)
2795 sm_handlers
.Append( handler
);
2799 // This is not documented behaviour, merely the simplest 'fix'
2800 // for preventing duplicate additions. If someone ever has
2801 // a good reason to add and remove duplicate handlers (and they
2802 // may) we should probably refcount the duplicates.
2803 // also an issue in InsertHandler below.
2805 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2806 handler
->GetName().c_str() );
2811 void wxImage::InsertHandler( wxImageHandler
*handler
)
2813 // Check for an existing handler of the type being added.
2814 if (FindHandler( handler
->GetType() ) == 0)
2816 sm_handlers
.Insert( handler
);
2820 // see AddHandler for additional comments.
2821 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2822 handler
->GetName().c_str() );
2827 bool wxImage::RemoveHandler( const wxString
& name
)
2829 wxImageHandler
*handler
= FindHandler(name
);
2832 sm_handlers
.DeleteObject(handler
);
2840 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2842 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2845 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2846 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2848 node
= node
->GetNext();
2853 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2855 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2858 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2859 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2861 if (handler
->GetExtension() == extension
)
2863 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2866 node
= node
->GetNext();
2871 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2873 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2876 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2877 if (handler
->GetType() == bitmapType
) return handler
;
2878 node
= node
->GetNext();
2883 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2885 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2888 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2889 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2890 node
= node
->GetNext();
2895 void wxImage::InitStandardHandlers()
2898 AddHandler(new wxBMPHandler
);
2899 #endif // wxUSE_STREAMS
2902 void wxImage::CleanUpHandlers()
2904 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2907 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2908 wxList::compatibility_iterator next
= node
->GetNext();
2913 sm_handlers
.Clear();
2916 wxString
wxImage::GetImageExtWildcard()
2920 wxList
& Handlers
= wxImage::GetHandlers();
2921 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2924 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2925 fmts
+= wxT("*.") + Handler
->GetExtension();
2926 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2927 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2928 Node
= Node
->GetNext();
2929 if ( Node
) fmts
+= wxT(";");
2932 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2935 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2937 const double red
= rgb
.red
/ 255.0,
2938 green
= rgb
.green
/ 255.0,
2939 blue
= rgb
.blue
/ 255.0;
2941 // find the min and max intensity (and remember which one was it for the
2943 double minimumRGB
= red
;
2944 if ( green
< minimumRGB
)
2946 if ( blue
< minimumRGB
)
2949 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2950 double maximumRGB
= red
;
2951 if ( green
> maximumRGB
)
2956 if ( blue
> maximumRGB
)
2962 const double value
= maximumRGB
;
2964 double hue
= 0.0, saturation
;
2965 const double deltaRGB
= maximumRGB
- minimumRGB
;
2966 if ( wxIsNullDouble(deltaRGB
) )
2968 // Gray has no color
2977 hue
= (green
- blue
) / deltaRGB
;
2981 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2985 hue
= 4.0 + (red
- green
) / deltaRGB
;
2994 saturation
= deltaRGB
/ maximumRGB
;
2997 return HSVValue(hue
, saturation
, value
);
3000 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
3002 double red
, green
, blue
;
3004 if ( wxIsNullDouble(hsv
.saturation
) )
3013 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
3014 int i
= (int)floor(hue
);
3015 double f
= hue
- i
; // fractional part of h
3016 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
3022 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3027 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3035 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3040 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3045 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
3053 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
3058 return RGBValue((unsigned char)(red
* 255.0),
3059 (unsigned char)(green
* 255.0),
3060 (unsigned char)(blue
* 255.0));
3064 * Rotates the hue of each pixel of the image. angle is a double in the range
3065 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
3067 void wxImage::RotateHue(double angle
)
3071 unsigned char *srcBytePtr
;
3072 unsigned char *dstBytePtr
;
3073 unsigned long count
;
3074 wxImage::HSVValue hsv
;
3075 wxImage::RGBValue rgb
;
3077 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
3078 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
3079 if ( count
> 0 && !wxIsNullDouble(angle
) )
3081 srcBytePtr
= M_IMGDATA
->m_data
;
3082 dstBytePtr
= srcBytePtr
;
3085 rgb
.red
= *srcBytePtr
++;
3086 rgb
.green
= *srcBytePtr
++;
3087 rgb
.blue
= *srcBytePtr
++;
3088 hsv
= RGBtoHSV(rgb
);
3090 hsv
.hue
= hsv
.hue
+ angle
;
3092 hsv
.hue
= hsv
.hue
- 1.0;
3093 else if (hsv
.hue
< 0.0)
3094 hsv
.hue
= hsv
.hue
+ 1.0;
3096 rgb
= HSVtoRGB(hsv
);
3097 *dstBytePtr
++ = rgb
.red
;
3098 *dstBytePtr
++ = rgb
.green
;
3099 *dstBytePtr
++ = rgb
.blue
;
3100 } while (--count
!= 0);
3104 //-----------------------------------------------------------------------------
3106 //-----------------------------------------------------------------------------
3108 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
3111 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
3113 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3114 // wxImageHandler::CallDoCanRead
3116 if ( !stream
.IsSeekable() )
3117 return false; // can't test unseekable stream
3119 wxFileOffset posOld
= stream
.TellI();
3120 int n
= DoGetImageCount(stream
);
3122 // restore the old position to be able to test other formats and so on
3123 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
3125 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3127 // reading would fail anyhow as we're not at the right position
3134 bool wxImageHandler::CanRead( const wxString
& name
)
3136 wxImageFileInputStream
stream(name
);
3137 if ( !stream
.IsOk() )
3139 wxLogError(_("Failed to check format of image file \"%s\"."), name
);
3144 return CanRead(stream
);
3147 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
3149 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
3150 // wxImageHandler::GetImageCount
3152 if ( !stream
.IsSeekable() )
3153 return false; // can't test unseekable stream
3155 wxFileOffset posOld
= stream
.TellI();
3156 bool ok
= DoCanRead(stream
);
3158 // restore the old position to be able to test other formats and so on
3159 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
3161 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
3163 // reading would fail anyhow as we're not at the right position
3170 #endif // wxUSE_STREAMS
3174 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
3176 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
3178 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
3179 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
3181 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
3182 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
3184 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
3187 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
3189 else // no resolution options specified
3194 return wxIMAGE_RESOLUTION_NONE
;
3197 // get the resolution unit too
3198 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
3201 // this is the default
3202 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
3205 return (wxImageResolution
)resUnit
;
3208 // ----------------------------------------------------------------------------
3209 // image histogram stuff
3210 // ----------------------------------------------------------------------------
3213 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
3218 unsigned char g2
) const
3220 unsigned long key
= MakeKey(r2
, g2
, b2
);
3222 while ( find(key
) != end() )
3224 // color already used
3236 wxLogError(_("No unused colour in image.") );
3242 key
= MakeKey(r2
, g2
, b2
);
3256 wxImage::FindFirstUnusedColour(unsigned char *r
,
3261 unsigned char g2
) const
3263 wxImageHistogram histogram
;
3265 ComputeHistogram(histogram
);
3267 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3273 // Counts and returns the number of different colours. Optionally stops
3274 // when it exceeds 'stopafter' different colours. This is useful, for
3275 // example, to see if the image can be saved as 8-bit (256 colour or
3276 // less, in this case it would be invoked as CountColours(256)). Default
3277 // value for stopafter is -1 (don't care).
3279 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3283 unsigned char r
, g
, b
;
3285 unsigned long size
, nentries
, key
;
3288 size
= GetWidth() * GetHeight();
3291 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3296 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3298 if (h
.Get(key
) == NULL
)
3309 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3311 unsigned char *p
= GetData();
3312 unsigned long nentries
= 0;
3316 const unsigned long size
= GetWidth() * GetHeight();
3318 unsigned char r
, g
, b
;
3319 for ( unsigned long n
= 0; n
< size
; n
++ )
3325 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3327 if ( entry
.value
++ == 0 )
3328 entry
.index
= nentries
++;
3335 * Rotation code by Carlos Moreno
3338 static const double wxROTATE_EPSILON
= 1e-10;
3340 // Auxiliary function to rotate a point (x,y) with respect to point p0
3341 // make it inline and use a straight return to facilitate optimization
3342 // also, the function receives the sine and cosine of the angle to avoid
3343 // repeating the time-consuming calls to these functions -- sin/cos can
3344 // be computed and stored in the calling function.
3346 static inline wxRealPoint
3347 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3348 const wxRealPoint
& p0
)
3350 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3351 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3354 static inline wxRealPoint
3355 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3356 const wxRealPoint
& p0
)
3358 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3361 wxImage
wxImage::Rotate(double angle
,
3362 const wxPoint
& centre_of_rotation
,
3364 wxPoint
*offset_after_rotation
) const
3366 // screen coordinates are a mirror image of "real" coordinates
3369 const bool has_alpha
= HasAlpha();
3371 const int w
= GetWidth();
3372 const int h
= GetHeight();
3376 // Create pointer-based array to accelerate access to wxImage's data
3377 unsigned char ** data
= new unsigned char * [h
];
3378 data
[0] = GetData();
3379 for (i
= 1; i
< h
; i
++)
3380 data
[i
] = data
[i
- 1] + (3 * w
);
3382 // Same for alpha channel
3383 unsigned char ** alpha
= NULL
;
3386 alpha
= new unsigned char * [h
];
3387 alpha
[0] = GetAlpha();
3388 for (i
= 1; i
< h
; i
++)
3389 alpha
[i
] = alpha
[i
- 1] + w
;
3392 // precompute coefficients for rotation formula
3393 const double cos_angle
= cos(angle
);
3394 const double sin_angle
= sin(angle
);
3396 // Create new Image to store the result
3397 // First, find rectangle that covers the rotated image; to do that,
3398 // rotate the four corners
3400 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3402 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3403 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3404 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3405 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3407 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3408 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3409 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3410 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3412 // Create rotated image
3413 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3414 // With alpha channel
3418 if (offset_after_rotation
!= NULL
)
3420 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3423 // the rotated (destination) image is always accessed sequentially via this
3424 // pointer, there is no need for pointer-based arrays here
3425 unsigned char *dst
= rotated
.GetData();
3427 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3429 // if the original image has a mask, use its RGB values as the blank pixel,
3430 // else, fall back to default (black).
3431 unsigned char blank_r
= 0;
3432 unsigned char blank_g
= 0;
3433 unsigned char blank_b
= 0;
3437 blank_r
= GetMaskRed();
3438 blank_g
= GetMaskGreen();
3439 blank_b
= GetMaskBlue();
3440 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3443 // Now, for each point of the rotated image, find where it came from, by
3444 // performing an inverse rotation (a rotation of -angle) and getting the
3445 // pixel at those coordinates
3447 const int rH
= rotated
.GetHeight();
3448 const int rW
= rotated
.GetWidth();
3450 // do the (interpolating) test outside of the loops, so that it is done
3451 // only once, instead of repeating it for each pixel.
3454 for (int y
= 0; y
< rH
; y
++)
3456 for (int x
= 0; x
< rW
; x
++)
3458 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3460 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3461 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3463 // interpolate using the 4 enclosing grid-points. Those
3464 // points can be obtained using floor and ceiling of the
3465 // exact coordinates of the point
3468 if (0 < src
.x
&& src
.x
< w
- 1)
3470 x1
= wxRound(floor(src
.x
));
3471 x2
= wxRound(ceil(src
.x
));
3473 else // else means that x is near one of the borders (0 or width-1)
3475 x1
= x2
= wxRound (src
.x
);
3478 if (0 < src
.y
&& src
.y
< h
- 1)
3480 y1
= wxRound(floor(src
.y
));
3481 y2
= wxRound(ceil(src
.y
));
3485 y1
= y2
= wxRound (src
.y
);
3488 // get four points and the distances (square of the distance,
3489 // for efficiency reasons) for the interpolation formula
3491 // GRG: Do not calculate the points until they are
3492 // really needed -- this way we can calculate
3493 // just one, instead of four, if d1, d2, d3
3494 // or d4 are < wxROTATE_EPSILON
3496 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3497 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3498 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3499 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3501 // Now interpolate as a weighted average of the four surrounding
3502 // points, where the weights are the distances to each of those points
3504 // If the point is exactly at one point of the grid of the source
3505 // image, then don't interpolate -- just assign the pixel
3507 // d1,d2,d3,d4 are positive -- no need for abs()
3508 if (d1
< wxROTATE_EPSILON
)
3510 unsigned char *p
= data
[y1
] + (3 * x1
);
3516 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3518 else if (d2
< wxROTATE_EPSILON
)
3520 unsigned char *p
= data
[y1
] + (3 * x2
);
3526 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3528 else if (d3
< wxROTATE_EPSILON
)
3530 unsigned char *p
= data
[y2
] + (3 * x2
);
3536 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3538 else if (d4
< wxROTATE_EPSILON
)
3540 unsigned char *p
= data
[y2
] + (3 * x1
);
3546 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3550 // weights for the weighted average are proportional to the inverse of the distance
3551 unsigned char *v1
= data
[y1
] + (3 * x1
);
3552 unsigned char *v2
= data
[y1
] + (3 * x2
);
3553 unsigned char *v3
= data
[y2
] + (3 * x2
);
3554 unsigned char *v4
= data
[y2
] + (3 * x1
);
3556 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3560 *(dst
++) = (unsigned char)
3561 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3562 w3
* *(v3
++) + w4
* *(v4
++)) /
3563 (w1
+ w2
+ w3
+ w4
) );
3564 *(dst
++) = (unsigned char)
3565 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3566 w3
* *(v3
++) + w4
* *(v4
++)) /
3567 (w1
+ w2
+ w3
+ w4
) );
3568 *(dst
++) = (unsigned char)
3569 ( (w1
* *v1
+ w2
* *v2
+
3570 w3
* *v3
+ w4
* *v4
) /
3571 (w1
+ w2
+ w3
+ w4
) );
3575 v1
= alpha
[y1
] + (x1
);
3576 v2
= alpha
[y1
] + (x2
);
3577 v3
= alpha
[y2
] + (x2
);
3578 v4
= alpha
[y2
] + (x1
);
3580 *(alpha_dst
++) = (unsigned char)
3581 ( (w1
* *v1
+ w2
* *v2
+
3582 w3
* *v3
+ w4
* *v4
) /
3583 (w1
+ w2
+ w3
+ w4
) );
3599 else // not interpolating
3601 for (int y
= 0; y
< rH
; y
++)
3603 for (int x
= 0; x
< rW
; x
++)
3605 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3607 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3608 const int ys
= wxRound (src
.y
); // closest integer
3610 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3612 unsigned char *p
= data
[ys
] + (3 * xs
);
3618 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3627 *(alpha_dst
++) = 255;
3643 // A module to allow wxImage initialization/cleanup
3644 // without calling these functions from app.cpp or from
3645 // the user's application.
3647 class wxImageModule
: public wxModule
3649 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3652 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3653 void OnExit() { wxImage::CleanUpHandlers(); }
3656 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3659 #endif // wxUSE_IMAGE