1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/palette.h"
29 #include "wx/colour.h"
32 #include "wx/filefn.h"
33 #include "wx/wfstream.h"
34 #include "wx/xpmdecod.h"
39 // make the code compile with either wxFile*Stream or wxFFile*Stream:
40 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
44 typedef wxFFileInputStream wxImageFileInputStream
;
45 typedef wxFFileOutputStream wxImageFileOutputStream
;
47 typedef wxFileInputStream wxImageFileInputStream
;
48 typedef wxFileOutputStream wxImageFileOutputStream
;
49 #endif // wxUSE_FILE/wxUSE_FFILE
50 #endif // HAS_FILE_STREAMS
53 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 wxList
wxImage::sm_handlers
;
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 class wxImageRefData
: public wxObjectRefData
71 virtual ~wxImageRefData();
76 unsigned char *m_data
;
79 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
81 // alpha channel data, may be NULL for the formats without alpha support
82 unsigned char *m_alpha
;
86 // if true, m_data is pointer to static data and shouldn't be freed
89 // same as m_static but for m_alpha
94 #endif // wxUSE_PALETTE
96 wxArrayString m_optionNames
;
97 wxArrayString m_optionValues
;
99 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
102 wxImageRefData::wxImageRefData()
106 m_type
= wxBITMAP_TYPE_INVALID
;
108 m_alpha
= (unsigned char *) NULL
;
117 m_staticAlpha
= false;
120 wxImageRefData::~wxImageRefData()
124 if ( !m_staticAlpha
)
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
135 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
137 bool wxImage::Create(const char* const* xpmData
)
142 wxXPMDecoder decoder
;
143 (*this) = decoder
.ReadData(xpmData
);
150 bool wxImage::Create( int width
, int height
, bool clear
)
154 m_refData
= new wxImageRefData();
156 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
157 if (!M_IMGDATA
->m_data
)
163 M_IMGDATA
->m_width
= width
;
164 M_IMGDATA
->m_height
= height
;
165 M_IMGDATA
->m_ok
= true;
175 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
179 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
181 m_refData
= new wxImageRefData();
183 M_IMGDATA
->m_data
= data
;
184 M_IMGDATA
->m_width
= width
;
185 M_IMGDATA
->m_height
= height
;
186 M_IMGDATA
->m_ok
= true;
187 M_IMGDATA
->m_static
= static_data
;
192 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
196 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
198 m_refData
= new wxImageRefData();
200 M_IMGDATA
->m_data
= data
;
201 M_IMGDATA
->m_alpha
= alpha
;
202 M_IMGDATA
->m_width
= width
;
203 M_IMGDATA
->m_height
= height
;
204 M_IMGDATA
->m_ok
= true;
205 M_IMGDATA
->m_static
= static_data
;
206 M_IMGDATA
->m_staticAlpha
= static_data
;
211 void wxImage::Destroy()
216 void wxImage::Clear(unsigned char value
)
218 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
221 wxObjectRefData
* wxImage::CreateRefData() const
223 return new wxImageRefData
;
226 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
228 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
229 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
231 wxImageRefData
* refData_new
= new wxImageRefData
;
232 refData_new
->m_width
= refData
->m_width
;
233 refData_new
->m_height
= refData
->m_height
;
234 refData_new
->m_maskRed
= refData
->m_maskRed
;
235 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
236 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
237 refData_new
->m_hasMask
= refData
->m_hasMask
;
238 refData_new
->m_ok
= true;
239 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
240 if (refData
->m_alpha
!= NULL
)
242 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
243 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
246 refData_new
->m_data
= (unsigned char*)malloc(size
);
247 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
249 refData_new
->m_palette
= refData
->m_palette
;
251 refData_new
->m_optionNames
= refData
->m_optionNames
;
252 refData_new
->m_optionValues
= refData
->m_optionValues
;
256 // 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( Ok(), 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( Ok(), 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( Ok(), 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( Ok(), 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 either the nearest neighbourhood, bilinear or
447 // bicubic method as specified
450 case wxIMAGE_QUALITY_BICUBIC
:
451 case wxIMAGE_QUALITY_BILINEAR
:
452 // both of these algorithms should be used for up-sampling the
453 // image only, when down-sampling always use box averaging for best
455 if ( width
< old_width
&& height
< old_height
)
456 image
= ResampleBox(width
, height
);
457 else if ( quality
== wxIMAGE_QUALITY_BILINEAR
)
458 image
= ResampleBilinear(width
, height
);
459 else if ( quality
== wxIMAGE_QUALITY_BICUBIC
)
460 image
= ResampleBicubic(width
, height
);
463 case wxIMAGE_QUALITY_NEAREST
:
464 if ( old_width
% width
== 0 && old_width
>= width
&&
465 old_height
% height
== 0 && old_height
>= height
)
467 return ShrinkBy( old_width
/ width
, old_height
/ height
);
470 image
= ResampleNearest(width
, height
);
474 // If the original image has a mask, apply the mask to the new image
475 if (M_IMGDATA
->m_hasMask
)
477 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
478 M_IMGDATA
->m_maskGreen
,
479 M_IMGDATA
->m_maskBlue
);
482 // In case this is a cursor, make sure the hotspot is scaled accordingly:
483 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
484 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
485 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
486 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
487 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
488 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
493 wxImage
wxImage::ResampleNearest(int width
, int height
) const
496 image
.Create( width
, height
, false );
498 unsigned char *data
= image
.GetData();
500 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
502 const unsigned char *source_data
= M_IMGDATA
->m_data
;
503 unsigned char *target_data
= data
;
504 const unsigned char *source_alpha
= 0 ;
505 unsigned char *target_alpha
= 0 ;
507 if ( !M_IMGDATA
->m_hasMask
)
509 source_alpha
= M_IMGDATA
->m_alpha
;
513 target_alpha
= image
.GetAlpha() ;
517 long old_height
= M_IMGDATA
->m_height
,
518 old_width
= M_IMGDATA
->m_width
;
519 long x_delta
= (old_width
<<16) / width
;
520 long y_delta
= (old_height
<<16) / height
;
522 unsigned char* dest_pixel
= target_data
;
525 for ( long j
= 0; j
< height
; j
++ )
527 const unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
528 const unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
531 for ( long i
= 0; i
< width
; i
++ )
533 const unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
534 const unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
535 dest_pixel
[0] = src_pixel
[0];
536 dest_pixel
[1] = src_pixel
[1];
537 dest_pixel
[2] = src_pixel
[2];
540 *(target_alpha
++) = *src_alpha_pixel
;
550 wxImage
wxImage::ResampleBox(int width
, int height
) const
552 // This function implements a simple pre-blur/box averaging method for
553 // downsampling that gives reasonably smooth results To scale the image
554 // down we will need to gather a grid of pixels of the size of the scale
555 // factor in each direction and then do an averaging of the pixels.
557 wxImage
ret_image(width
, height
, false);
559 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
560 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
562 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
563 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
565 const unsigned char* src_data
= M_IMGDATA
->m_data
;
566 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
567 unsigned char* dst_data
= ret_image
.GetData();
568 unsigned char* dst_alpha
= NULL
;
572 ret_image
.SetAlpha();
573 dst_alpha
= ret_image
.GetAlpha();
576 int averaged_pixels
, src_pixel_index
;
577 double sum_r
, sum_g
, sum_b
, sum_a
;
579 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
581 // Source pixel in the Y direction
582 int src_y
= (int)(y
* scale_factor_y
);
584 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
586 // Source pixel in the X direction
587 int src_x
= (int)(x
* scale_factor_x
);
589 // Box of pixels to average
591 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
593 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
594 j
<= int(src_y
+ scale_factor_y_2
);
597 // We don't care to average pixels that don't exist (edges)
598 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
601 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
602 i
<= src_x
+ scale_factor_x_2
;
605 // Don't average edge pixels
606 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
609 // Calculate the actual index in our source pixels
610 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
612 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
613 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
614 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
616 sum_a
+= src_alpha
[src_pixel_index
];
622 // Calculate the average from the sum and number of averaged pixels
623 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
624 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
625 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
628 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
635 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
637 // This function implements a Bilinear algorithm for resampling.
638 wxImage
ret_image(width
, height
, false);
639 const unsigned char* src_data
= M_IMGDATA
->m_data
;
640 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
641 unsigned char* dst_data
= ret_image
.GetData();
642 unsigned char* dst_alpha
= NULL
;
646 ret_image
.SetAlpha();
647 dst_alpha
= ret_image
.GetAlpha();
649 double HFactor
= double(M_IMGDATA
->m_height
) / height
;
650 double WFactor
= double(M_IMGDATA
->m_width
) / width
;
652 int srcpixymax
= M_IMGDATA
->m_height
- 1;
653 int srcpixxmax
= M_IMGDATA
->m_width
- 1;
655 double srcpixy
, srcpixy1
, srcpixy2
, dy
, dy1
;
656 double srcpixx
, srcpixx1
, srcpixx2
, dx
, dx1
;
658 // initialize alpha values to avoid g++ warnings about possibly
659 // uninitialized variables
660 double r1
, g1
, b1
, a1
= 0;
661 double r2
, g2
, b2
, a2
= 0;
663 for ( int dsty
= 0; dsty
< height
; dsty
++ )
665 // We need to calculate the source pixel to interpolate from - Y-axis
666 srcpixy
= double(dsty
) * HFactor
;
667 srcpixy1
= int(srcpixy
);
668 srcpixy2
= ( srcpixy1
== srcpixymax
) ? srcpixy1
: srcpixy1
+ 1.0;
669 dy
= srcpixy
- (int)srcpixy
;
673 for ( int dstx
= 0; dstx
< width
; dstx
++ )
675 // X-axis of pixel to interpolate from
676 srcpixx
= double(dstx
) * WFactor
;
677 srcpixx1
= int(srcpixx
);
678 srcpixx2
= ( srcpixx1
== srcpixxmax
) ? srcpixx1
: srcpixx1
+ 1.0;
679 dx
= srcpixx
- (int)srcpixx
;
682 int x_offset1
= srcpixx1
< 0.0 ? 0 : srcpixx1
> srcpixxmax
? srcpixxmax
: (int)srcpixx1
;
683 int x_offset2
= srcpixx2
< 0.0 ? 0 : srcpixx2
> srcpixxmax
? srcpixxmax
: (int)srcpixx2
;
684 int y_offset1
= srcpixy1
< 0.0 ? 0 : srcpixy1
> srcpixymax
? srcpixymax
: (int)srcpixy1
;
685 int y_offset2
= srcpixy2
< 0.0 ? 0 : srcpixy2
> srcpixymax
? srcpixymax
: (int)srcpixy2
;
687 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
688 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
689 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
690 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
693 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
694 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
695 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
697 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
700 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
701 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
702 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
704 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
708 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
709 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
710 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
714 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
721 // The following two local functions are for the B-spline weighting of the
722 // bicubic sampling algorithm
723 static inline double spline_cube(double value
)
725 return value
<= 0.0 ? 0.0 : value
* value
* value
;
728 static inline double spline_weight(double value
)
730 return (spline_cube(value
+ 2) -
731 4 * spline_cube(value
+ 1) +
732 6 * spline_cube(value
) -
733 4 * spline_cube(value
- 1)) / 6;
736 // This is the bicubic resampling algorithm
737 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
739 // This function implements a Bicubic B-Spline algorithm for resampling.
740 // This method is certainly a little slower than wxImage's default pixel
741 // replication method, however for most reasonably sized images not being
742 // upsampled too much on a fairly average CPU this difference is hardly
743 // noticeable and the results are far more pleasing to look at.
745 // This particular bicubic algorithm does pixel weighting according to a
746 // B-Spline that basically implements a Gaussian bell-like weighting
747 // kernel. Because of this method the results may appear a bit blurry when
748 // upsampling by large factors. This is basically because a slight
749 // gaussian blur is being performed to get the smooth look of the upsampled
752 // Edge pixels: 3-4 possible solutions
753 // - (Wrap/tile) Wrap the image, take the color value from the opposite
754 // side of the image.
755 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
756 // where n is nonpositive, will have the value of (2, 1).
757 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
758 // pixels which do have all neighbours.
759 // - (Clamp) Choose the nearest pixel along the border. This takes the
760 // border pixels and extends them out to infinity.
762 // NOTE: below the y_offset and x_offset variables are being set for edge
763 // pixels using the "Mirror" method mentioned above
767 ret_image
.Create(width
, height
, false);
769 const unsigned char* src_data
= M_IMGDATA
->m_data
;
770 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
771 unsigned char* dst_data
= ret_image
.GetData();
772 unsigned char* dst_alpha
= NULL
;
776 ret_image
.SetAlpha();
777 dst_alpha
= ret_image
.GetAlpha();
780 for ( int dsty
= 0; dsty
< height
; dsty
++ )
782 // We need to calculate the source pixel to interpolate from - Y-axis
783 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
784 double dy
= srcpixy
- (int)srcpixy
;
786 for ( int dstx
= 0; dstx
< width
; dstx
++ )
788 // X-axis of pixel to interpolate from
789 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
790 double dx
= srcpixx
- (int)srcpixx
;
792 // Sums for each color channel
793 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
795 // Here we actually determine the RGBA values for the destination pixel
796 for ( int k
= -1; k
<= 2; k
++ )
799 int y_offset
= srcpixy
+ k
< 0.0
801 : srcpixy
+ k
>= M_IMGDATA
->m_height
802 ? M_IMGDATA
->m_height
- 1
803 : (int)(srcpixy
+ k
);
805 // Loop across the X axis
806 for ( int i
= -1; i
<= 2; i
++ )
809 int x_offset
= srcpixx
+ i
< 0.0
811 : srcpixx
+ i
>= M_IMGDATA
->m_width
812 ? M_IMGDATA
->m_width
- 1
813 : (int)(srcpixx
+ i
);
815 // Calculate the exact position where the source data
816 // should be pulled from based on the x_offset and y_offset
817 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
819 // Calculate the weight for the specified pixel according
820 // to the bicubic b-spline kernel we're using for
823 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
825 // Create a sum of all velues for each color channel
826 // adjusted for the pixel's calculated weight
827 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
828 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
829 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
831 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
835 // Put the data into the destination image. The summed values are
836 // of double data type and are rounded here for accuracy
837 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
838 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
839 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
843 *dst_alpha
++ = (unsigned char)sum_a
;
850 // Blur in the horizontal direction
851 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
853 wxImage
ret_image(MakeEmptyClone());
855 wxCHECK( ret_image
.Ok(), ret_image
);
857 const unsigned char* src_data
= M_IMGDATA
->m_data
;
858 unsigned char* dst_data
= ret_image
.GetData();
859 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
860 unsigned char* dst_alpha
= ret_image
.GetAlpha();
862 // number of pixels we average over
863 const int blurArea
= blurRadius
*2 + 1;
865 // Horizontal blurring algorithm - average all pixels in the specified blur
866 // radius in the X or horizontal direction
867 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
869 // Variables used in the blurring algorithm
876 const unsigned char *src
;
879 // Calculate the average of all pixels in the blur radius for the first
881 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
883 // To deal with the pixels at the start of a row so it's not
884 // grabbing GOK values from memory at negative indices of the
885 // image's data or grabbing from the previous row
887 pixel_idx
= y
* M_IMGDATA
->m_width
;
889 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
891 src
= src_data
+ pixel_idx
*3;
896 sum_a
+= src_alpha
[pixel_idx
];
899 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
900 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
901 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
902 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
904 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
906 // Now average the values of the rest of the pixels by just moving the
907 // blur radius box along the row
908 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
910 // Take care of edge pixels on the left edge by essentially
911 // duplicating the edge pixel
912 if ( x
- blurRadius
- 1 < 0 )
913 pixel_idx
= y
* M_IMGDATA
->m_width
;
915 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
917 // Subtract the value of the pixel at the left side of the blur
919 src
= src_data
+ pixel_idx
*3;
924 sum_a
-= src_alpha
[pixel_idx
];
926 // Take care of edge pixels on the right edge
927 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
928 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
930 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
932 // Add the value of the pixel being added to the end of our box
933 src
= src_data
+ pixel_idx
*3;
938 sum_a
+= src_alpha
[pixel_idx
];
940 // Save off the averaged data
941 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
942 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
943 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
944 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
946 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
953 // Blur in the vertical direction
954 wxImage
wxImage::BlurVertical(int blurRadius
) const
956 wxImage
ret_image(MakeEmptyClone());
958 wxCHECK( ret_image
.Ok(), ret_image
);
960 const unsigned char* src_data
= M_IMGDATA
->m_data
;
961 unsigned char* dst_data
= ret_image
.GetData();
962 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
963 unsigned char* dst_alpha
= ret_image
.GetAlpha();
965 // number of pixels we average over
966 const int blurArea
= blurRadius
*2 + 1;
968 // Vertical blurring algorithm - same as horizontal but switched the
969 // opposite direction
970 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
972 // Variables used in the blurring algorithm
979 const unsigned char *src
;
982 // Calculate the average of all pixels in our blur radius box for the
983 // first pixel of the column
984 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
986 // To deal with the pixels at the start of a column so it's not
987 // grabbing GOK values from memory at negative indices of the
988 // image's data or grabbing from the previous column
992 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
994 src
= src_data
+ pixel_idx
*3;
999 sum_a
+= src_alpha
[pixel_idx
];
1002 dst
= dst_data
+ x
*3;
1003 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1004 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1005 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1007 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
1009 // Now average the values of the rest of the pixels by just moving the
1010 // box along the column from top to bottom
1011 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1013 // Take care of pixels that would be beyond the top edge by
1014 // duplicating the top edge pixel for the column
1015 if ( y
- blurRadius
- 1 < 0 )
1018 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1020 // Subtract the value of the pixel at the top of our blur radius box
1021 src
= src_data
+ pixel_idx
*3;
1026 sum_a
-= src_alpha
[pixel_idx
];
1028 // Take care of the pixels that would be beyond the bottom edge of
1029 // the image similar to the top edge
1030 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1031 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1033 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1035 // Add the value of the pixel being added to the end of our box
1036 src
= src_data
+ pixel_idx
*3;
1041 sum_a
+= src_alpha
[pixel_idx
];
1043 // Save off the averaged data
1044 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1045 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1046 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1047 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1049 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1056 // The new blur function
1057 wxImage
wxImage::Blur(int blurRadius
) const
1060 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1062 // Blur the image in each direction
1063 ret_image
= BlurHorizontal(blurRadius
);
1064 ret_image
= ret_image
.BlurVertical(blurRadius
);
1069 wxImage
wxImage::Rotate90( bool clockwise
) const
1071 wxImage
image(MakeEmptyClone(Clone_SwapOrientation
));
1073 wxCHECK( image
.Ok(), image
);
1075 long height
= M_IMGDATA
->m_height
;
1076 long width
= M_IMGDATA
->m_width
;
1078 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1080 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1081 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1082 clockwise
? hot_x
: width
- 1 - hot_x
);
1085 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1087 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1088 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1089 clockwise
? height
- 1 - hot_y
: hot_y
);
1092 unsigned char *data
= image
.GetData();
1093 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1094 unsigned char *target_data
;
1095 unsigned char *alpha_data
= image
.GetAlpha();
1096 const unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1097 unsigned char *target_alpha
= 0 ;
1099 for (long j
= 0; j
< height
; j
++)
1101 for (long i
= 0; i
< width
; i
++)
1105 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1107 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1111 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1113 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1115 memcpy( target_data
, source_data
, 3 );
1120 memcpy( target_alpha
, source_alpha
, 1 );
1129 wxImage
wxImage::Rotate180() const
1131 wxImage
image(MakeEmptyClone());
1133 wxCHECK( image
.Ok(), image
);
1135 long height
= M_IMGDATA
->m_height
;
1136 long width
= M_IMGDATA
->m_width
;
1138 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1140 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1141 width
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
));
1144 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1146 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1147 height
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
));
1150 unsigned char *data
= image
.GetData();
1151 unsigned char *alpha
= image
.GetAlpha();
1152 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1153 unsigned char *target_data
= data
+ width
* height
* 3;
1155 for (long j
= 0; j
< height
; j
++)
1157 for (long i
= 0; i
< width
; i
++)
1160 memcpy( target_data
, source_data
, 3 );
1167 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1168 unsigned char *dest_alpha
= alpha
+ width
* height
;
1170 for (long j
= 0; j
< height
; ++j
)
1172 for (long i
= 0; i
< width
; ++i
)
1174 *(--dest_alpha
) = *(src_alpha
++);
1182 wxImage
wxImage::Mirror( bool horizontally
) const
1184 wxImage
image(MakeEmptyClone());
1186 wxCHECK( image
.Ok(), image
);
1188 long height
= M_IMGDATA
->m_height
;
1189 long width
= M_IMGDATA
->m_width
;
1191 unsigned char *data
= image
.GetData();
1192 unsigned char *alpha
= image
.GetAlpha();
1193 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1194 unsigned char *target_data
;
1198 for (long j
= 0; j
< height
; j
++)
1201 target_data
= data
-3;
1202 for (long i
= 0; i
< width
; i
++)
1204 memcpy( target_data
, source_data
, 3 );
1212 // src_alpha starts at the first pixel and increases by 1 after each step
1213 // (a step here is the copy of the alpha value of one pixel)
1214 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1215 // dest_alpha starts just beyond the first line, decreases before each step,
1216 // and after each line is finished, increases by 2 widths (skipping the line
1217 // just copied and the line that will be copied next)
1218 unsigned char *dest_alpha
= alpha
+ width
;
1220 for (long jj
= 0; jj
< height
; ++jj
)
1222 for (long i
= 0; i
< width
; ++i
) {
1223 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1225 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1231 for (long i
= 0; i
< height
; i
++)
1233 target_data
= data
+ 3*width
*(height
-1-i
);
1234 memcpy( target_data
, source_data
, (size_t)3*width
);
1235 source_data
+= 3*width
;
1240 // src_alpha starts at the first pixel and increases by 1 width after each step
1241 // (a step here is the copy of the alpha channel of an entire line)
1242 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1243 // dest_alpha starts just beyond the last line (beyond the whole image)
1244 // and decreases by 1 width before each step
1245 unsigned char *dest_alpha
= alpha
+ width
* height
;
1247 for (long jj
= 0; jj
< height
; ++jj
)
1249 dest_alpha
-= width
;
1250 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1259 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1263 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1265 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1266 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1267 image
, wxT("invalid subimage size") );
1269 const int subwidth
= rect
.GetWidth();
1270 const int subheight
= rect
.GetHeight();
1272 image
.Create( subwidth
, subheight
, false );
1274 const unsigned char *src_data
= GetData();
1275 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1276 unsigned char *subdata
= image
.GetData();
1277 unsigned char *subalpha
= NULL
;
1279 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1283 subalpha
= image
.GetAlpha();
1284 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1287 if (M_IMGDATA
->m_hasMask
)
1288 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1290 const int width
= GetWidth();
1291 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1293 src_data
+= 3 * pixsoff
;
1294 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1296 for (long j
= 0; j
< subheight
; ++j
)
1298 memcpy( subdata
, src_data
, 3 * subwidth
);
1299 subdata
+= 3 * subwidth
;
1300 src_data
+= 3 * width
;
1301 if (subalpha
!= NULL
) {
1302 memcpy( subalpha
, src_alpha
, subwidth
);
1303 subalpha
+= subwidth
;
1311 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1312 int r_
, int g_
, int b_
) const
1316 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1317 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1319 int width
= GetWidth(), height
= GetHeight();
1320 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1322 unsigned char r
= (unsigned char)r_
;
1323 unsigned char g
= (unsigned char)g_
;
1324 unsigned char b
= (unsigned char)b_
;
1325 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1327 GetOrFindMaskColour( &r
, &g
, &b
);
1328 image
.SetMaskColour(r
, g
, b
);
1331 image
.SetRGB(wxRect(), r
, g
, b
);
1333 // we have two coordinate systems:
1334 // source: starting at 0,0 of source image
1335 // destination starting at 0,0 of destination image
1336 // Documentation says:
1337 // "The image is pasted into a new image [...] at the position pos relative
1338 // to the upper left of the new image." this means the transition rule is:
1339 // "dest coord" = "source coord" + pos;
1341 // calculate the intersection using source coordinates:
1342 wxRect
srcRect(0, 0, width
, height
);
1343 wxRect
dstRect(-pos
, size
);
1345 srcRect
.Intersect(dstRect
);
1347 if (!srcRect
.IsEmpty())
1349 // insertion point is needed in destination coordinates.
1350 // NB: it is not always "pos"!
1351 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1353 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1354 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1356 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1362 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1364 wxCHECK_RET( Ok(), wxT("invalid image") );
1365 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1371 int width
= image
.GetWidth();
1372 int height
= image
.GetHeight();
1385 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1386 width
= M_IMGDATA
->m_width
- (x
+xx
);
1387 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1388 height
= M_IMGDATA
->m_height
- (y
+yy
);
1390 if (width
< 1) return;
1391 if (height
< 1) return;
1393 if ((!HasMask() && !image
.HasMask()) ||
1394 (HasMask() && !image
.HasMask()) ||
1395 ((HasMask() && image
.HasMask() &&
1396 (GetMaskRed()==image
.GetMaskRed()) &&
1397 (GetMaskGreen()==image
.GetMaskGreen()) &&
1398 (GetMaskBlue()==image
.GetMaskBlue()))))
1400 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1401 int source_step
= image
.GetWidth()*3;
1403 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1404 int target_step
= M_IMGDATA
->m_width
*3;
1405 for (int j
= 0; j
< height
; j
++)
1407 memcpy( target_data
, source_data
, width
*3 );
1408 source_data
+= source_step
;
1409 target_data
+= target_step
;
1413 // Copy over the alpha channel from the original image
1414 if ( image
.HasAlpha() )
1419 const unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1420 int source_step
= image
.GetWidth();
1422 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1423 int target_step
= M_IMGDATA
->m_width
;
1425 for (int j
= 0; j
< height
; j
++,
1426 source_data
+= source_step
,
1427 target_data
+= target_step
)
1429 memcpy( target_data
, source_data
, width
);
1433 if (!HasMask() && image
.HasMask())
1435 unsigned char r
= image
.GetMaskRed();
1436 unsigned char g
= image
.GetMaskGreen();
1437 unsigned char b
= image
.GetMaskBlue();
1439 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1440 int source_step
= image
.GetWidth()*3;
1442 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1443 int target_step
= M_IMGDATA
->m_width
*3;
1445 for (int j
= 0; j
< height
; j
++)
1447 for (int i
= 0; i
< width
*3; i
+=3)
1449 if ((source_data
[i
] != r
) ||
1450 (source_data
[i
+1] != g
) ||
1451 (source_data
[i
+2] != b
))
1453 memcpy( target_data
+i
, source_data
+i
, 3 );
1456 source_data
+= source_step
;
1457 target_data
+= target_step
;
1462 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1463 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1465 wxCHECK_RET( Ok(), wxT("invalid image") );
1469 unsigned char *data
= GetData();
1471 const int w
= GetWidth();
1472 const int h
= GetHeight();
1474 for (int j
= 0; j
< h
; j
++)
1475 for (int i
= 0; i
< w
; i
++)
1477 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1487 wxImage
wxImage::ConvertToGreyscale(void) const
1489 return ConvertToGreyscale(0.299, 0.587, 0.114);
1492 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1494 wxImage
image(MakeEmptyClone());
1496 wxCHECK( image
.Ok(), image
);
1498 const unsigned char *src
= M_IMGDATA
->m_data
;
1499 unsigned char *dest
= image
.GetData();
1501 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1502 const unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1503 const unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1504 const unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1506 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1507 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1509 memcpy(dest
, src
, 3);
1510 // only modify non-masked pixels
1511 if ( !hasMask
|| src
[0] != maskRed
|| src
[1] != maskGreen
|| src
[2] != maskBlue
)
1513 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1517 // copy the alpha channel, if any
1518 if ( image
.HasAlpha() )
1520 memcpy( image
.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1526 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1530 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1532 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1534 unsigned char *data
= image
.GetData();
1536 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1538 if (M_IMGDATA
->m_hasMask
)
1540 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1541 M_IMGDATA
->m_maskBlue
== b
)
1542 image
.SetMaskColour( 255, 255, 255 );
1544 image
.SetMaskColour( 0, 0, 0 );
1547 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1549 unsigned char *srcd
= M_IMGDATA
->m_data
;
1550 unsigned char *tard
= image
.GetData();
1552 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1554 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1555 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1561 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1563 wxImage image
= *this;
1565 unsigned char mr
= image
.GetMaskRed();
1566 unsigned char mg
= image
.GetMaskGreen();
1567 unsigned char mb
= image
.GetMaskBlue();
1569 int width
= image
.GetWidth();
1570 int height
= image
.GetHeight();
1571 bool has_mask
= image
.HasMask();
1573 for (int y
= height
-1; y
>= 0; --y
)
1575 for (int x
= width
-1; x
>= 0; --x
)
1577 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1578 unsigned char* r
= data
;
1579 unsigned char* g
= data
+1;
1580 unsigned char* b
= data
+2;
1582 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1585 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1591 int wxImage::GetWidth() const
1593 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1595 return M_IMGDATA
->m_width
;
1598 int wxImage::GetHeight() const
1600 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1602 return M_IMGDATA
->m_height
;
1605 wxBitmapType
wxImage::GetType() const
1607 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1609 return M_IMGDATA
->m_type
;
1612 void wxImage::SetType(wxBitmapType type
)
1614 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1616 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1617 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1619 M_IMGDATA
->m_type
= type
;
1622 long wxImage::XYToIndex(int x
, int y
) const
1626 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1628 return y
*M_IMGDATA
->m_width
+ x
;
1634 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1636 long pos
= XYToIndex(x
, y
);
1637 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1643 M_IMGDATA
->m_data
[ pos
] = r
;
1644 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1645 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1648 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1650 wxCHECK_RET( Ok(), wxT("invalid image") );
1655 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1656 if ( rect
== wxRect() )
1662 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1663 imageRect
.Contains(rect
.GetBottomRight()),
1664 wxT("invalid bounding rectangle") );
1667 int x1
= rect
.GetLeft(),
1669 x2
= rect
.GetRight() + 1,
1670 y2
= rect
.GetBottom() + 1;
1672 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1673 int x
, y
, width
= GetWidth();
1674 for (y
= y1
; y
< y2
; y
++)
1676 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1677 for (x
= x1
; x
< x2
; x
++)
1686 unsigned char wxImage::GetRed( int x
, int y
) const
1688 long pos
= XYToIndex(x
, y
);
1689 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1693 return M_IMGDATA
->m_data
[pos
];
1696 unsigned char wxImage::GetGreen( int x
, int y
) const
1698 long pos
= XYToIndex(x
, y
);
1699 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1703 return M_IMGDATA
->m_data
[pos
+1];
1706 unsigned char wxImage::GetBlue( int x
, int y
) const
1708 long pos
= XYToIndex(x
, y
);
1709 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1713 return M_IMGDATA
->m_data
[pos
+2];
1716 bool wxImage::IsOk() const
1718 // image of 0 width or height can't be considered ok - at least because it
1719 // causes crashes in ConvertToBitmap() if we don't catch it in time
1720 wxImageRefData
*data
= M_IMGDATA
;
1721 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1724 unsigned char *wxImage::GetData() const
1726 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1728 return M_IMGDATA
->m_data
;
1731 void wxImage::SetData( unsigned char *data
, bool static_data
)
1733 wxCHECK_RET( Ok(), wxT("invalid image") );
1735 wxImageRefData
*newRefData
= new wxImageRefData();
1737 newRefData
->m_width
= M_IMGDATA
->m_width
;
1738 newRefData
->m_height
= M_IMGDATA
->m_height
;
1739 newRefData
->m_data
= data
;
1740 newRefData
->m_ok
= true;
1741 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1742 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1743 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1744 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1745 newRefData
->m_static
= static_data
;
1749 m_refData
= newRefData
;
1752 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1754 wxImageRefData
*newRefData
= new wxImageRefData();
1758 newRefData
->m_width
= new_width
;
1759 newRefData
->m_height
= new_height
;
1760 newRefData
->m_data
= data
;
1761 newRefData
->m_ok
= true;
1762 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1763 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1764 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1765 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1769 newRefData
->m_width
= new_width
;
1770 newRefData
->m_height
= new_height
;
1771 newRefData
->m_data
= data
;
1772 newRefData
->m_ok
= true;
1774 newRefData
->m_static
= static_data
;
1778 m_refData
= newRefData
;
1781 // ----------------------------------------------------------------------------
1782 // alpha channel support
1783 // ----------------------------------------------------------------------------
1785 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1787 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1789 long pos
= XYToIndex(x
, y
);
1790 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1794 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1797 unsigned char wxImage::GetAlpha(int x
, int y
) const
1799 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1801 long pos
= XYToIndex(x
, y
);
1802 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1804 return M_IMGDATA
->m_alpha
[pos
];
1808 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1812 const int w
= M_IMGDATA
->m_width
;
1813 const int h
= M_IMGDATA
->m_height
;
1815 unsigned char *alpha
= GetAlpha();
1816 unsigned char *data
= GetData();
1818 for ( int y
= 0; y
< h
; y
++ )
1820 for ( int x
= 0; x
< w
; x
++ )
1832 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1834 wxCHECK_RET( Ok(), wxT("invalid image") );
1840 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1843 if( !M_IMGDATA
->m_staticAlpha
)
1844 free(M_IMGDATA
->m_alpha
);
1846 M_IMGDATA
->m_alpha
= alpha
;
1847 M_IMGDATA
->m_staticAlpha
= static_data
;
1850 unsigned char *wxImage::GetAlpha() const
1852 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1854 return M_IMGDATA
->m_alpha
;
1857 void wxImage::InitAlpha()
1859 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1861 // initialize memory for alpha channel
1864 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1865 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1869 // use the mask to initialize the alpha channel.
1870 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1872 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1873 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1874 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1875 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1879 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1880 ? wxIMAGE_ALPHA_TRANSPARENT
1881 : wxIMAGE_ALPHA_OPAQUE
;
1884 M_IMGDATA
->m_hasMask
= false;
1888 // make the image fully opaque
1889 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1893 void wxImage::ClearAlpha()
1895 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1897 if ( !M_IMGDATA
->m_staticAlpha
)
1898 free( M_IMGDATA
->m_alpha
);
1900 M_IMGDATA
->m_alpha
= NULL
;
1904 // ----------------------------------------------------------------------------
1906 // ----------------------------------------------------------------------------
1908 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1910 wxCHECK_RET( Ok(), wxT("invalid image") );
1914 M_IMGDATA
->m_maskRed
= r
;
1915 M_IMGDATA
->m_maskGreen
= g
;
1916 M_IMGDATA
->m_maskBlue
= b
;
1917 M_IMGDATA
->m_hasMask
= true;
1920 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1922 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1924 if (M_IMGDATA
->m_hasMask
)
1926 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1927 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1928 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1933 FindFirstUnusedColour(r
, g
, b
);
1938 unsigned char wxImage::GetMaskRed() const
1940 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1942 return M_IMGDATA
->m_maskRed
;
1945 unsigned char wxImage::GetMaskGreen() const
1947 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1949 return M_IMGDATA
->m_maskGreen
;
1952 unsigned char wxImage::GetMaskBlue() const
1954 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1956 return M_IMGDATA
->m_maskBlue
;
1959 void wxImage::SetMask( bool mask
)
1961 wxCHECK_RET( Ok(), wxT("invalid image") );
1965 M_IMGDATA
->m_hasMask
= mask
;
1968 bool wxImage::HasMask() const
1970 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1972 return M_IMGDATA
->m_hasMask
;
1975 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1977 long pos
= XYToIndex(x
, y
);
1978 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1981 if ( M_IMGDATA
->m_hasMask
)
1983 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1984 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1985 p
[1] == M_IMGDATA
->m_maskGreen
&&
1986 p
[2] == M_IMGDATA
->m_maskBlue
)
1993 if ( M_IMGDATA
->m_alpha
)
1995 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1997 // transparent enough
2006 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2007 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2009 // check that the images are the same size
2010 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2012 wxLogError( _("Image and mask have different sizes.") );
2016 // find unused colour
2017 unsigned char r
,g
,b
;
2018 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2020 wxLogError( _("No unused colour in image being masked.") );
2026 unsigned char *imgdata
= GetData();
2027 unsigned char *maskdata
= mask
.GetData();
2029 const int w
= GetWidth();
2030 const int h
= GetHeight();
2032 for (int j
= 0; j
< h
; j
++)
2034 for (int i
= 0; i
< w
; i
++)
2036 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2047 SetMaskColour(r
, g
, b
);
2053 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2058 unsigned char mr
, mg
, mb
;
2059 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2061 wxLogError( _("No unused colour in image being masked.") );
2065 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2068 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2071 unsigned char threshold
)
2079 SetMaskColour(mr
, mg
, mb
);
2081 unsigned char *imgdata
= GetData();
2082 unsigned char *alphadata
= GetAlpha();
2085 int h
= GetHeight();
2087 for (int y
= 0; y
< h
; y
++)
2089 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2091 if (*alphadata
< threshold
)
2100 if ( !M_IMGDATA
->m_staticAlpha
)
2101 free(M_IMGDATA
->m_alpha
);
2103 M_IMGDATA
->m_alpha
= NULL
;
2104 M_IMGDATA
->m_staticAlpha
= false;
2109 // ----------------------------------------------------------------------------
2110 // Palette functions
2111 // ----------------------------------------------------------------------------
2115 bool wxImage::HasPalette() const
2120 return M_IMGDATA
->m_palette
.Ok();
2123 const wxPalette
& wxImage::GetPalette() const
2125 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
2127 return M_IMGDATA
->m_palette
;
2130 void wxImage::SetPalette(const wxPalette
& palette
)
2132 wxCHECK_RET( Ok(), wxT("invalid image") );
2136 M_IMGDATA
->m_palette
= palette
;
2139 #endif // wxUSE_PALETTE
2141 // ----------------------------------------------------------------------------
2142 // Option functions (arbitrary name/value mapping)
2143 // ----------------------------------------------------------------------------
2145 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2149 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2150 if ( idx
== wxNOT_FOUND
)
2152 M_IMGDATA
->m_optionNames
.Add(name
);
2153 M_IMGDATA
->m_optionValues
.Add(value
);
2157 M_IMGDATA
->m_optionNames
[idx
] = name
;
2158 M_IMGDATA
->m_optionValues
[idx
] = value
;
2162 void wxImage::SetOption(const wxString
& name
, int value
)
2165 valStr
.Printf(wxT("%d"), value
);
2166 SetOption(name
, valStr
);
2169 wxString
wxImage::GetOption(const wxString
& name
) const
2172 return wxEmptyString
;
2174 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2175 if ( idx
== wxNOT_FOUND
)
2176 return wxEmptyString
;
2178 return M_IMGDATA
->m_optionValues
[idx
];
2181 int wxImage::GetOptionInt(const wxString
& name
) const
2183 return wxAtoi(GetOption(name
));
2186 bool wxImage::HasOption(const wxString
& name
) const
2188 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2192 // ----------------------------------------------------------------------------
2194 // ----------------------------------------------------------------------------
2196 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2197 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2198 int WXUNUSED_UNLESS_STREAMS(index
) )
2200 #if HAS_FILE_STREAMS
2201 if (wxFileExists(filename
))
2203 wxImageFileInputStream
stream(filename
);
2204 wxBufferedInputStream
bstream( stream
);
2205 return LoadFile(bstream
, type
, index
);
2209 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2213 #else // !HAS_FILE_STREAMS
2215 #endif // HAS_FILE_STREAMS
2218 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2219 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2220 int WXUNUSED_UNLESS_STREAMS(index
) )
2222 #if HAS_FILE_STREAMS
2223 if (wxFileExists(filename
))
2225 wxImageFileInputStream
stream(filename
);
2226 wxBufferedInputStream
bstream( stream
);
2227 return LoadFile(bstream
, mimetype
, index
);
2231 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2235 #else // !HAS_FILE_STREAMS
2237 #endif // HAS_FILE_STREAMS
2241 bool wxImage::SaveFile( const wxString
& filename
) const
2243 wxString ext
= filename
.AfterLast('.').Lower();
2245 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2248 wxLogError(_("Can't save image to file '%s': unknown extension."),
2253 return SaveFile(filename
, handler
->GetType());
2256 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2257 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2259 #if HAS_FILE_STREAMS
2260 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2262 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2264 wxImageFileOutputStream
stream(filename
);
2266 if ( stream
.IsOk() )
2268 wxBufferedOutputStream
bstream( stream
);
2269 return SaveFile(bstream
, type
);
2271 #endif // HAS_FILE_STREAMS
2276 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2277 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2279 #if HAS_FILE_STREAMS
2280 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2282 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2284 wxImageFileOutputStream
stream(filename
);
2286 if ( stream
.IsOk() )
2288 wxBufferedOutputStream
bstream( stream
);
2289 return SaveFile(bstream
, mimetype
);
2291 #endif // HAS_FILE_STREAMS
2296 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2298 #if HAS_FILE_STREAMS
2299 wxImageFileInputStream
stream(name
);
2300 return CanRead(stream
);
2306 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2307 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2309 #if HAS_FILE_STREAMS
2310 wxImageFileInputStream
stream(name
);
2312 return GetImageCount(stream
, type
);
2320 bool wxImage::CanRead( wxInputStream
&stream
)
2322 const wxList
& list
= GetHandlers();
2324 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2326 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2327 if (handler
->CanRead( stream
))
2334 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2336 wxImageHandler
*handler
;
2338 if ( type
== wxBITMAP_TYPE_ANY
)
2340 const wxList
& list
= GetHandlers();
2342 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2344 node
= node
->GetNext() )
2346 handler
= (wxImageHandler
*)node
->GetData();
2347 if ( handler
->CanRead(stream
) )
2349 const int count
= handler
->GetImageCount(stream
);
2356 wxLogWarning(_("No handler found for image type."));
2360 handler
= FindHandler(type
);
2364 wxLogWarning(_("No image handler for type %d defined."), type
);
2368 if ( handler
->CanRead(stream
) )
2370 return handler
->GetImageCount(stream
);
2374 wxLogError(_("Image file is not of type %d."), type
);
2379 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2381 // save the options values which can be clobbered by the handler (e.g. many
2382 // of them call Destroy() before trying to load the file)
2383 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2384 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2386 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2389 // rescale the image to the specified size if needed
2390 if ( maxWidth
|| maxHeight
)
2392 const unsigned widthOrig
= GetWidth(),
2393 heightOrig
= GetHeight();
2395 // this uses the same (trivial) algorithm as the JPEG handler
2396 unsigned width
= widthOrig
,
2397 height
= heightOrig
;
2398 while ( (maxWidth
&& width
> maxWidth
) ||
2399 (maxHeight
&& height
> maxHeight
) )
2405 if ( width
!= widthOrig
|| height
!= heightOrig
)
2406 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2409 // Set this after Rescale, which currently does not preserve it
2410 M_IMGDATA
->m_type
= handler
.GetType();
2415 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2419 wxImageHandler
*handler
;
2421 if ( type
== wxBITMAP_TYPE_ANY
)
2423 const wxList
& list
= GetHandlers();
2424 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2426 node
= node
->GetNext() )
2428 handler
= (wxImageHandler
*)node
->GetData();
2429 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2433 wxLogWarning( _("No handler found for image type.") );
2437 //else: have specific type
2439 handler
= FindHandler(type
);
2442 wxLogWarning( _("No image handler for type %d defined."), type
);
2446 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2448 wxLogError(_("Image file is not of type %d."), type
);
2452 return DoLoad(*handler
, stream
, index
);
2455 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2459 m_refData
= new wxImageRefData
;
2461 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2465 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2469 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2471 wxLogError(_("Image file is not of type %s."), mimetype
);
2475 return DoLoad(*handler
, stream
, index
);
2478 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2480 wxImage
* const self
= const_cast<wxImage
*>(this);
2481 if ( !handler
.SaveFile(self
, stream
) )
2484 M_IMGDATA
->m_type
= handler
.GetType();
2488 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2490 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2492 wxImageHandler
*handler
= FindHandler(type
);
2495 wxLogWarning( _("No image handler for type %d defined."), type
);
2499 return DoSave(*handler
, stream
);
2502 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2504 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2506 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2509 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2512 return DoSave(*handler
, stream
);
2515 #endif // wxUSE_STREAMS
2517 // ----------------------------------------------------------------------------
2518 // image I/O handlers
2519 // ----------------------------------------------------------------------------
2521 void wxImage::AddHandler( wxImageHandler
*handler
)
2523 // Check for an existing handler of the type being added.
2524 if (FindHandler( handler
->GetType() ) == 0)
2526 sm_handlers
.Append( handler
);
2530 // This is not documented behaviour, merely the simplest 'fix'
2531 // for preventing duplicate additions. If someone ever has
2532 // a good reason to add and remove duplicate handlers (and they
2533 // may) we should probably refcount the duplicates.
2534 // also an issue in InsertHandler below.
2536 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2537 handler
->GetName().c_str() );
2542 void wxImage::InsertHandler( wxImageHandler
*handler
)
2544 // Check for an existing handler of the type being added.
2545 if (FindHandler( handler
->GetType() ) == 0)
2547 sm_handlers
.Insert( handler
);
2551 // see AddHandler for additional comments.
2552 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2553 handler
->GetName().c_str() );
2558 bool wxImage::RemoveHandler( const wxString
& name
)
2560 wxImageHandler
*handler
= FindHandler(name
);
2563 sm_handlers
.DeleteObject(handler
);
2571 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2573 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2576 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2577 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2579 node
= node
->GetNext();
2584 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2586 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2589 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2590 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2592 if (handler
->GetExtension() == extension
)
2594 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2597 node
= node
->GetNext();
2602 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2604 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2607 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2608 if (handler
->GetType() == bitmapType
) return handler
;
2609 node
= node
->GetNext();
2614 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2616 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2619 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2620 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2621 node
= node
->GetNext();
2626 void wxImage::InitStandardHandlers()
2629 AddHandler(new wxBMPHandler
);
2630 #endif // wxUSE_STREAMS
2633 void wxImage::CleanUpHandlers()
2635 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2638 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2639 wxList::compatibility_iterator next
= node
->GetNext();
2644 sm_handlers
.Clear();
2647 wxString
wxImage::GetImageExtWildcard()
2651 wxList
& Handlers
= wxImage::GetHandlers();
2652 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2655 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2656 fmts
+= wxT("*.") + Handler
->GetExtension();
2657 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2658 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2659 Node
= Node
->GetNext();
2660 if ( Node
) fmts
+= wxT(";");
2663 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2666 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2668 const double red
= rgb
.red
/ 255.0,
2669 green
= rgb
.green
/ 255.0,
2670 blue
= rgb
.blue
/ 255.0;
2672 // find the min and max intensity (and remember which one was it for the
2674 double minimumRGB
= red
;
2675 if ( green
< minimumRGB
)
2677 if ( blue
< minimumRGB
)
2680 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2681 double maximumRGB
= red
;
2682 if ( green
> maximumRGB
)
2687 if ( blue
> maximumRGB
)
2693 const double value
= maximumRGB
;
2695 double hue
= 0.0, saturation
;
2696 const double deltaRGB
= maximumRGB
- minimumRGB
;
2697 if ( wxIsNullDouble(deltaRGB
) )
2699 // Gray has no color
2708 hue
= (green
- blue
) / deltaRGB
;
2712 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2716 hue
= 4.0 + (red
- green
) / deltaRGB
;
2720 wxFAIL_MSG(wxT("hue not specified"));
2729 saturation
= deltaRGB
/ maximumRGB
;
2732 return HSVValue(hue
, saturation
, value
);
2735 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2737 double red
, green
, blue
;
2739 if ( wxIsNullDouble(hsv
.saturation
) )
2748 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2749 int i
= (int)floor(hue
);
2750 double f
= hue
- i
; // fractional part of h
2751 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2757 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2762 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2770 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2775 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2780 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2788 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2793 return RGBValue((unsigned char)(red
* 255.0),
2794 (unsigned char)(green
* 255.0),
2795 (unsigned char)(blue
* 255.0));
2799 * Rotates the hue of each pixel of the image. angle is a double in the range
2800 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2802 void wxImage::RotateHue(double angle
)
2806 unsigned char *srcBytePtr
;
2807 unsigned char *dstBytePtr
;
2808 unsigned long count
;
2809 wxImage::HSVValue hsv
;
2810 wxImage::RGBValue rgb
;
2812 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2813 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2814 if ( count
> 0 && !wxIsNullDouble(angle
) )
2816 srcBytePtr
= M_IMGDATA
->m_data
;
2817 dstBytePtr
= srcBytePtr
;
2820 rgb
.red
= *srcBytePtr
++;
2821 rgb
.green
= *srcBytePtr
++;
2822 rgb
.blue
= *srcBytePtr
++;
2823 hsv
= RGBtoHSV(rgb
);
2825 hsv
.hue
= hsv
.hue
+ angle
;
2827 hsv
.hue
= hsv
.hue
- 1.0;
2828 else if (hsv
.hue
< 0.0)
2829 hsv
.hue
= hsv
.hue
+ 1.0;
2831 rgb
= HSVtoRGB(hsv
);
2832 *dstBytePtr
++ = rgb
.red
;
2833 *dstBytePtr
++ = rgb
.green
;
2834 *dstBytePtr
++ = rgb
.blue
;
2835 } while (--count
!= 0);
2839 //-----------------------------------------------------------------------------
2841 //-----------------------------------------------------------------------------
2843 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2846 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
2848 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2849 // wxImageHandler::CallDoCanRead
2851 if ( !stream
.IsSeekable() )
2852 return false; // can't test unseekable stream
2854 wxFileOffset posOld
= stream
.TellI();
2855 int n
= DoGetImageCount(stream
);
2857 // restore the old position to be able to test other formats and so on
2858 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2860 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2862 // reading would fail anyhow as we're not at the right position
2869 bool wxImageHandler::CanRead( const wxString
& name
)
2871 if (wxFileExists(name
))
2873 wxImageFileInputStream
stream(name
);
2874 return CanRead(stream
);
2877 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2882 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2884 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2885 // wxImageHandler::GetImageCount
2887 if ( !stream
.IsSeekable() )
2888 return false; // can't test unseekable stream
2890 wxFileOffset posOld
= stream
.TellI();
2891 bool ok
= DoCanRead(stream
);
2893 // restore the old position to be able to test other formats and so on
2894 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2896 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2898 // reading would fail anyhow as we're not at the right position
2905 #endif // wxUSE_STREAMS
2909 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2911 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
2913 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2914 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2916 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2917 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2919 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2922 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2924 else // no resolution options specified
2929 return wxIMAGE_RESOLUTION_NONE
;
2932 // get the resolution unit too
2933 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2936 // this is the default
2937 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2940 return (wxImageResolution
)resUnit
;
2943 // ----------------------------------------------------------------------------
2944 // image histogram stuff
2945 // ----------------------------------------------------------------------------
2948 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2953 unsigned char g2
) const
2955 unsigned long key
= MakeKey(r2
, g2
, b2
);
2957 while ( find(key
) != end() )
2959 // color already used
2971 wxLogError(_("No unused colour in image.") );
2977 key
= MakeKey(r2
, g2
, b2
);
2991 wxImage::FindFirstUnusedColour(unsigned char *r
,
2996 unsigned char g2
) const
2998 wxImageHistogram histogram
;
3000 ComputeHistogram(histogram
);
3002 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3008 // Counts and returns the number of different colours. Optionally stops
3009 // when it exceeds 'stopafter' different colours. This is useful, for
3010 // example, to see if the image can be saved as 8-bit (256 colour or
3011 // less, in this case it would be invoked as CountColours(256)). Default
3012 // value for stopafter is -1 (don't care).
3014 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3018 unsigned char r
, g
, b
;
3020 unsigned long size
, nentries
, key
;
3023 size
= GetWidth() * GetHeight();
3026 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3031 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3033 if (h
.Get(key
) == NULL
)
3044 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3046 unsigned char *p
= GetData();
3047 unsigned long nentries
= 0;
3051 const unsigned long size
= GetWidth() * GetHeight();
3053 unsigned char r
, g
, b
;
3054 for ( unsigned long n
= 0; n
< size
; n
++ )
3060 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3062 if ( entry
.value
++ == 0 )
3063 entry
.index
= nentries
++;
3070 * Rotation code by Carlos Moreno
3073 static const double wxROTATE_EPSILON
= 1e-10;
3075 // Auxiliary function to rotate a point (x,y) with respect to point p0
3076 // make it inline and use a straight return to facilitate optimization
3077 // also, the function receives the sine and cosine of the angle to avoid
3078 // repeating the time-consuming calls to these functions -- sin/cos can
3079 // be computed and stored in the calling function.
3081 static inline wxRealPoint
3082 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3083 const wxRealPoint
& p0
)
3085 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3086 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3089 static inline wxRealPoint
3090 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3091 const wxRealPoint
& p0
)
3093 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3096 wxImage
wxImage::Rotate(double angle
,
3097 const wxPoint
& centre_of_rotation
,
3099 wxPoint
*offset_after_rotation
) const
3101 // screen coordinates are a mirror image of "real" coordinates
3104 const bool has_alpha
= HasAlpha();
3106 const int w
= GetWidth();
3107 const int h
= GetHeight();
3111 // Create pointer-based array to accelerate access to wxImage's data
3112 unsigned char ** data
= new unsigned char * [h
];
3113 data
[0] = GetData();
3114 for (i
= 1; i
< h
; i
++)
3115 data
[i
] = data
[i
- 1] + (3 * w
);
3117 // Same for alpha channel
3118 unsigned char ** alpha
= NULL
;
3121 alpha
= new unsigned char * [h
];
3122 alpha
[0] = GetAlpha();
3123 for (i
= 1; i
< h
; i
++)
3124 alpha
[i
] = alpha
[i
- 1] + w
;
3127 // precompute coefficients for rotation formula
3128 const double cos_angle
= cos(angle
);
3129 const double sin_angle
= sin(angle
);
3131 // Create new Image to store the result
3132 // First, find rectangle that covers the rotated image; to do that,
3133 // rotate the four corners
3135 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3137 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3138 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3139 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3140 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3142 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3143 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3144 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3145 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3147 // Create rotated image
3148 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3149 // With alpha channel
3153 if (offset_after_rotation
!= NULL
)
3155 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3158 // the rotated (destination) image is always accessed sequentially via this
3159 // pointer, there is no need for pointer-based arrays here
3160 unsigned char *dst
= rotated
.GetData();
3162 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3164 // if the original image has a mask, use its RGB values as the blank pixel,
3165 // else, fall back to default (black).
3166 unsigned char blank_r
= 0;
3167 unsigned char blank_g
= 0;
3168 unsigned char blank_b
= 0;
3172 blank_r
= GetMaskRed();
3173 blank_g
= GetMaskGreen();
3174 blank_b
= GetMaskBlue();
3175 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3178 // Now, for each point of the rotated image, find where it came from, by
3179 // performing an inverse rotation (a rotation of -angle) and getting the
3180 // pixel at those coordinates
3182 const int rH
= rotated
.GetHeight();
3183 const int rW
= rotated
.GetWidth();
3185 // do the (interpolating) test outside of the loops, so that it is done
3186 // only once, instead of repeating it for each pixel.
3189 for (int y
= 0; y
< rH
; y
++)
3191 for (int x
= 0; x
< rW
; x
++)
3193 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3195 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3196 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3198 // interpolate using the 4 enclosing grid-points. Those
3199 // points can be obtained using floor and ceiling of the
3200 // exact coordinates of the point
3203 if (0 < src
.x
&& src
.x
< w
- 1)
3205 x1
= wxRound(floor(src
.x
));
3206 x2
= wxRound(ceil(src
.x
));
3208 else // else means that x is near one of the borders (0 or width-1)
3210 x1
= x2
= wxRound (src
.x
);
3213 if (0 < src
.y
&& src
.y
< h
- 1)
3215 y1
= wxRound(floor(src
.y
));
3216 y2
= wxRound(ceil(src
.y
));
3220 y1
= y2
= wxRound (src
.y
);
3223 // get four points and the distances (square of the distance,
3224 // for efficiency reasons) for the interpolation formula
3226 // GRG: Do not calculate the points until they are
3227 // really needed -- this way we can calculate
3228 // just one, instead of four, if d1, d2, d3
3229 // or d4 are < wxROTATE_EPSILON
3231 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3232 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3233 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3234 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3236 // Now interpolate as a weighted average of the four surrounding
3237 // points, where the weights are the distances to each of those points
3239 // If the point is exactly at one point of the grid of the source
3240 // image, then don't interpolate -- just assign the pixel
3242 // d1,d2,d3,d4 are positive -- no need for abs()
3243 if (d1
< wxROTATE_EPSILON
)
3245 unsigned char *p
= data
[y1
] + (3 * x1
);
3251 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3253 else if (d2
< wxROTATE_EPSILON
)
3255 unsigned char *p
= data
[y1
] + (3 * x2
);
3261 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3263 else if (d3
< wxROTATE_EPSILON
)
3265 unsigned char *p
= data
[y2
] + (3 * x2
);
3271 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3273 else if (d4
< wxROTATE_EPSILON
)
3275 unsigned char *p
= data
[y2
] + (3 * x1
);
3281 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3285 // weights for the weighted average are proportional to the inverse of the distance
3286 unsigned char *v1
= data
[y1
] + (3 * x1
);
3287 unsigned char *v2
= data
[y1
] + (3 * x2
);
3288 unsigned char *v3
= data
[y2
] + (3 * x2
);
3289 unsigned char *v4
= data
[y2
] + (3 * x1
);
3291 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3295 *(dst
++) = (unsigned char)
3296 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3297 w3
* *(v3
++) + w4
* *(v4
++)) /
3298 (w1
+ w2
+ w3
+ w4
) );
3299 *(dst
++) = (unsigned char)
3300 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3301 w3
* *(v3
++) + w4
* *(v4
++)) /
3302 (w1
+ w2
+ w3
+ w4
) );
3303 *(dst
++) = (unsigned char)
3304 ( (w1
* *v1
+ w2
* *v2
+
3305 w3
* *v3
+ w4
* *v4
) /
3306 (w1
+ w2
+ w3
+ w4
) );
3310 v1
= alpha
[y1
] + (x1
);
3311 v2
= alpha
[y1
] + (x2
);
3312 v3
= alpha
[y2
] + (x2
);
3313 v4
= alpha
[y2
] + (x1
);
3315 *(alpha_dst
++) = (unsigned char)
3316 ( (w1
* *v1
+ w2
* *v2
+
3317 w3
* *v3
+ w4
* *v4
) /
3318 (w1
+ w2
+ w3
+ w4
) );
3334 else // not interpolating
3336 for (int y
= 0; y
< rH
; y
++)
3338 for (int x
= 0; x
< rW
; x
++)
3340 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3342 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3343 const int ys
= wxRound (src
.y
); // closest integer
3345 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3347 unsigned char *p
= data
[ys
] + (3 * xs
);
3353 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3362 *(alpha_dst
++) = 255;
3378 // A module to allow wxImage initialization/cleanup
3379 // without calling these functions from app.cpp or from
3380 // the user's application.
3382 class wxImageModule
: public wxModule
3384 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3387 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3388 void OnExit() { wxImage::CleanUpHandlers(); }
3391 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3394 #endif // wxUSE_IMAGE