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 unsigned char *source_data
= M_IMGDATA
->m_data
;
338 unsigned char *target_data
= data
;
339 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 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 unsigned char *source_data
= M_IMGDATA
->m_data
;
503 unsigned char *target_data
= data
;
504 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 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
528 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
531 for ( long i
= 0; i
< width
; i
++ )
533 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
534 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 unsigned char* src_data
= M_IMGDATA
->m_data
;
566 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 unsigned char* src_data
= M_IMGDATA
->m_data
;
640 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 unsigned char* src_data
= M_IMGDATA
->m_data
;
770 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( false ));
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( false ));
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") );
1281 if (src_alpha
!= NULL
) {
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 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1401 int source_step
= image
.GetWidth()*3;
1403 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*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 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 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1440 int source_step
= image
.GetWidth()*3;
1442 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*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( false ));
1496 wxCHECK( image
.Ok(), image
);
1498 const unsigned char *src
= M_IMGDATA
->m_data
;
1499 unsigned char *dest
= image
.GetData();
1501 bool hasMask
= M_IMGDATA
->m_hasMask
;
1502 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1503 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1504 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 // don't modify the mask
1511 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1516 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1520 // copy the alpha channel, if any
1521 if ( image
.HasAlpha() )
1523 memcpy( image
.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1529 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1533 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1535 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1537 unsigned char *data
= image
.GetData();
1539 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1541 if (M_IMGDATA
->m_hasMask
)
1543 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1544 M_IMGDATA
->m_maskBlue
== b
)
1545 image
.SetMaskColour( 255, 255, 255 );
1547 image
.SetMaskColour( 0, 0, 0 );
1550 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1552 unsigned char *srcd
= M_IMGDATA
->m_data
;
1553 unsigned char *tard
= image
.GetData();
1555 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1557 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1558 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1564 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1566 wxImage image
= *this;
1568 unsigned char mr
= image
.GetMaskRed();
1569 unsigned char mg
= image
.GetMaskGreen();
1570 unsigned char mb
= image
.GetMaskBlue();
1572 int width
= image
.GetWidth();
1573 int height
= image
.GetHeight();
1574 bool has_mask
= image
.HasMask();
1576 for (int y
= height
-1; y
>= 0; --y
)
1578 for (int x
= width
-1; x
>= 0; --x
)
1580 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1581 unsigned char* r
= data
;
1582 unsigned char* g
= data
+1;
1583 unsigned char* b
= data
+2;
1585 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1588 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1594 int wxImage::GetWidth() const
1596 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1598 return M_IMGDATA
->m_width
;
1601 int wxImage::GetHeight() const
1603 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1605 return M_IMGDATA
->m_height
;
1608 wxBitmapType
wxImage::GetType() const
1610 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1612 return M_IMGDATA
->m_type
;
1615 void wxImage::SetType(wxBitmapType type
)
1617 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1619 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1620 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1622 M_IMGDATA
->m_type
= type
;
1625 long wxImage::XYToIndex(int x
, int y
) const
1629 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1631 return y
*M_IMGDATA
->m_width
+ x
;
1637 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1639 long pos
= XYToIndex(x
, y
);
1640 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1646 M_IMGDATA
->m_data
[ pos
] = r
;
1647 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1648 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1651 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1653 wxCHECK_RET( Ok(), wxT("invalid image") );
1658 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1659 if ( rect
== wxRect() )
1665 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1666 imageRect
.Contains(rect
.GetBottomRight()),
1667 wxT("invalid bounding rectangle") );
1670 int x1
= rect
.GetLeft(),
1672 x2
= rect
.GetRight() + 1,
1673 y2
= rect
.GetBottom() + 1;
1675 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1676 int x
, y
, width
= GetWidth();
1677 for (y
= y1
; y
< y2
; y
++)
1679 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1680 for (x
= x1
; x
< x2
; x
++)
1689 unsigned char wxImage::GetRed( int x
, int y
) const
1691 long pos
= XYToIndex(x
, y
);
1692 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1696 return M_IMGDATA
->m_data
[pos
];
1699 unsigned char wxImage::GetGreen( int x
, int y
) const
1701 long pos
= XYToIndex(x
, y
);
1702 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1706 return M_IMGDATA
->m_data
[pos
+1];
1709 unsigned char wxImage::GetBlue( int x
, int y
) const
1711 long pos
= XYToIndex(x
, y
);
1712 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1716 return M_IMGDATA
->m_data
[pos
+2];
1719 bool wxImage::IsOk() const
1721 // image of 0 width or height can't be considered ok - at least because it
1722 // causes crashes in ConvertToBitmap() if we don't catch it in time
1723 wxImageRefData
*data
= M_IMGDATA
;
1724 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1727 unsigned char *wxImage::GetData() const
1729 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1731 return M_IMGDATA
->m_data
;
1734 void wxImage::SetData( unsigned char *data
, bool static_data
)
1736 wxCHECK_RET( Ok(), wxT("invalid image") );
1738 wxImageRefData
*newRefData
= new wxImageRefData();
1740 newRefData
->m_width
= M_IMGDATA
->m_width
;
1741 newRefData
->m_height
= M_IMGDATA
->m_height
;
1742 newRefData
->m_data
= data
;
1743 newRefData
->m_ok
= true;
1744 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1745 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1746 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1747 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1748 newRefData
->m_static
= static_data
;
1752 m_refData
= newRefData
;
1755 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1757 wxImageRefData
*newRefData
= new wxImageRefData();
1761 newRefData
->m_width
= new_width
;
1762 newRefData
->m_height
= new_height
;
1763 newRefData
->m_data
= data
;
1764 newRefData
->m_ok
= true;
1765 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1766 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1767 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1768 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1772 newRefData
->m_width
= new_width
;
1773 newRefData
->m_height
= new_height
;
1774 newRefData
->m_data
= data
;
1775 newRefData
->m_ok
= true;
1777 newRefData
->m_static
= static_data
;
1781 m_refData
= newRefData
;
1784 // ----------------------------------------------------------------------------
1785 // alpha channel support
1786 // ----------------------------------------------------------------------------
1788 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1790 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1792 long pos
= XYToIndex(x
, y
);
1793 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1797 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1800 unsigned char wxImage::GetAlpha(int x
, int y
) const
1802 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1804 long pos
= XYToIndex(x
, y
);
1805 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1807 return M_IMGDATA
->m_alpha
[pos
];
1811 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1815 const int w
= M_IMGDATA
->m_width
;
1816 const int h
= M_IMGDATA
->m_height
;
1818 unsigned char *alpha
= GetAlpha();
1819 unsigned char *data
= GetData();
1821 for ( int y
= 0; y
< h
; y
++ )
1823 for ( int x
= 0; x
< w
; x
++ )
1835 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1837 wxCHECK_RET( Ok(), wxT("invalid image") );
1843 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1846 if( !M_IMGDATA
->m_staticAlpha
)
1847 free(M_IMGDATA
->m_alpha
);
1849 M_IMGDATA
->m_alpha
= alpha
;
1850 M_IMGDATA
->m_staticAlpha
= static_data
;
1853 unsigned char *wxImage::GetAlpha() const
1855 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1857 return M_IMGDATA
->m_alpha
;
1860 void wxImage::InitAlpha()
1862 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1864 // initialize memory for alpha channel
1867 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1868 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1872 // use the mask to initialize the alpha channel.
1873 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1875 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1876 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1877 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1878 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1882 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1883 ? wxIMAGE_ALPHA_TRANSPARENT
1884 : wxIMAGE_ALPHA_OPAQUE
;
1887 M_IMGDATA
->m_hasMask
= false;
1891 // make the image fully opaque
1892 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1896 void wxImage::ClearAlpha()
1898 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1900 if ( !M_IMGDATA
->m_staticAlpha
)
1901 free( M_IMGDATA
->m_alpha
);
1903 M_IMGDATA
->m_alpha
= NULL
;
1907 // ----------------------------------------------------------------------------
1909 // ----------------------------------------------------------------------------
1911 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1913 wxCHECK_RET( Ok(), wxT("invalid image") );
1917 M_IMGDATA
->m_maskRed
= r
;
1918 M_IMGDATA
->m_maskGreen
= g
;
1919 M_IMGDATA
->m_maskBlue
= b
;
1920 M_IMGDATA
->m_hasMask
= true;
1923 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1925 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1927 if (M_IMGDATA
->m_hasMask
)
1929 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1930 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1931 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1936 FindFirstUnusedColour(r
, g
, b
);
1941 unsigned char wxImage::GetMaskRed() const
1943 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1945 return M_IMGDATA
->m_maskRed
;
1948 unsigned char wxImage::GetMaskGreen() const
1950 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1952 return M_IMGDATA
->m_maskGreen
;
1955 unsigned char wxImage::GetMaskBlue() const
1957 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1959 return M_IMGDATA
->m_maskBlue
;
1962 void wxImage::SetMask( bool mask
)
1964 wxCHECK_RET( Ok(), wxT("invalid image") );
1968 M_IMGDATA
->m_hasMask
= mask
;
1971 bool wxImage::HasMask() const
1973 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1975 return M_IMGDATA
->m_hasMask
;
1978 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1980 long pos
= XYToIndex(x
, y
);
1981 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1984 if ( M_IMGDATA
->m_hasMask
)
1986 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1987 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1988 p
[1] == M_IMGDATA
->m_maskGreen
&&
1989 p
[2] == M_IMGDATA
->m_maskBlue
)
1996 if ( M_IMGDATA
->m_alpha
)
1998 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2000 // transparent enough
2009 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2010 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2012 // check that the images are the same size
2013 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2015 wxLogError( _("Image and mask have different sizes.") );
2019 // find unused colour
2020 unsigned char r
,g
,b
;
2021 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2023 wxLogError( _("No unused colour in image being masked.") );
2029 unsigned char *imgdata
= GetData();
2030 unsigned char *maskdata
= mask
.GetData();
2032 const int w
= GetWidth();
2033 const int h
= GetHeight();
2035 for (int j
= 0; j
< h
; j
++)
2037 for (int i
= 0; i
< w
; i
++)
2039 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2050 SetMaskColour(r
, g
, b
);
2056 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2061 unsigned char mr
, mg
, mb
;
2062 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2064 wxLogError( _("No unused colour in image being masked.") );
2068 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2071 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2074 unsigned char threshold
)
2082 SetMaskColour(mr
, mg
, mb
);
2084 unsigned char *imgdata
= GetData();
2085 unsigned char *alphadata
= GetAlpha();
2088 int h
= GetHeight();
2090 for (int y
= 0; y
< h
; y
++)
2092 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2094 if (*alphadata
< threshold
)
2103 if ( !M_IMGDATA
->m_staticAlpha
)
2104 free(M_IMGDATA
->m_alpha
);
2106 M_IMGDATA
->m_alpha
= NULL
;
2107 M_IMGDATA
->m_staticAlpha
= false;
2112 // ----------------------------------------------------------------------------
2113 // Palette functions
2114 // ----------------------------------------------------------------------------
2118 bool wxImage::HasPalette() const
2123 return M_IMGDATA
->m_palette
.Ok();
2126 const wxPalette
& wxImage::GetPalette() const
2128 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
2130 return M_IMGDATA
->m_palette
;
2133 void wxImage::SetPalette(const wxPalette
& palette
)
2135 wxCHECK_RET( Ok(), wxT("invalid image") );
2139 M_IMGDATA
->m_palette
= palette
;
2142 #endif // wxUSE_PALETTE
2144 // ----------------------------------------------------------------------------
2145 // Option functions (arbitrary name/value mapping)
2146 // ----------------------------------------------------------------------------
2148 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2152 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2153 if ( idx
== wxNOT_FOUND
)
2155 M_IMGDATA
->m_optionNames
.Add(name
);
2156 M_IMGDATA
->m_optionValues
.Add(value
);
2160 M_IMGDATA
->m_optionNames
[idx
] = name
;
2161 M_IMGDATA
->m_optionValues
[idx
] = value
;
2165 void wxImage::SetOption(const wxString
& name
, int value
)
2168 valStr
.Printf(wxT("%d"), value
);
2169 SetOption(name
, valStr
);
2172 wxString
wxImage::GetOption(const wxString
& name
) const
2175 return wxEmptyString
;
2177 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2178 if ( idx
== wxNOT_FOUND
)
2179 return wxEmptyString
;
2181 return M_IMGDATA
->m_optionValues
[idx
];
2184 int wxImage::GetOptionInt(const wxString
& name
) const
2186 return wxAtoi(GetOption(name
));
2189 bool wxImage::HasOption(const wxString
& name
) const
2191 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2195 // ----------------------------------------------------------------------------
2197 // ----------------------------------------------------------------------------
2199 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2200 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2201 int WXUNUSED_UNLESS_STREAMS(index
) )
2203 #if HAS_FILE_STREAMS
2204 if (wxFileExists(filename
))
2206 wxImageFileInputStream
stream(filename
);
2207 wxBufferedInputStream
bstream( stream
);
2208 return LoadFile(bstream
, type
, index
);
2212 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2216 #else // !HAS_FILE_STREAMS
2218 #endif // HAS_FILE_STREAMS
2221 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2222 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2223 int WXUNUSED_UNLESS_STREAMS(index
) )
2225 #if HAS_FILE_STREAMS
2226 if (wxFileExists(filename
))
2228 wxImageFileInputStream
stream(filename
);
2229 wxBufferedInputStream
bstream( stream
);
2230 return LoadFile(bstream
, mimetype
, index
);
2234 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2238 #else // !HAS_FILE_STREAMS
2240 #endif // HAS_FILE_STREAMS
2244 bool wxImage::SaveFile( const wxString
& filename
) const
2246 wxString ext
= filename
.AfterLast('.').Lower();
2248 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2251 wxLogError(_("Can't save image to file '%s': unknown extension."),
2256 return SaveFile(filename
, handler
->GetType());
2259 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2260 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2262 #if HAS_FILE_STREAMS
2263 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2265 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2267 wxImageFileOutputStream
stream(filename
);
2269 if ( stream
.IsOk() )
2271 wxBufferedOutputStream
bstream( stream
);
2272 return SaveFile(bstream
, type
);
2274 #endif // HAS_FILE_STREAMS
2279 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2280 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2282 #if HAS_FILE_STREAMS
2283 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2285 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2287 wxImageFileOutputStream
stream(filename
);
2289 if ( stream
.IsOk() )
2291 wxBufferedOutputStream
bstream( stream
);
2292 return SaveFile(bstream
, mimetype
);
2294 #endif // HAS_FILE_STREAMS
2299 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2301 #if HAS_FILE_STREAMS
2302 wxImageFileInputStream
stream(name
);
2303 return CanRead(stream
);
2309 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2310 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2312 #if HAS_FILE_STREAMS
2313 wxImageFileInputStream
stream(name
);
2315 return GetImageCount(stream
, type
);
2323 bool wxImage::CanRead( wxInputStream
&stream
)
2325 const wxList
& list
= GetHandlers();
2327 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2329 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2330 if (handler
->CanRead( stream
))
2337 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2339 wxImageHandler
*handler
;
2341 if ( type
== wxBITMAP_TYPE_ANY
)
2343 const wxList
& list
= GetHandlers();
2345 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2347 node
= node
->GetNext() )
2349 handler
= (wxImageHandler
*)node
->GetData();
2350 if ( handler
->CanRead(stream
) )
2352 const int count
= handler
->GetImageCount(stream
);
2359 wxLogWarning(_("No handler found for image type."));
2363 handler
= FindHandler(type
);
2367 wxLogWarning(_("No image handler for type %d defined."), type
);
2371 if ( handler
->CanRead(stream
) )
2373 return handler
->GetImageCount(stream
);
2377 wxLogError(_("Image file is not of type %d."), type
);
2382 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2384 // save the options values which can be clobbered by the handler (e.g. many
2385 // of them call Destroy() before trying to load the file)
2386 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2387 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2389 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2392 // rescale the image to the specified size if needed
2393 if ( maxWidth
|| maxHeight
)
2395 const unsigned widthOrig
= GetWidth(),
2396 heightOrig
= GetHeight();
2398 // this uses the same (trivial) algorithm as the JPEG handler
2399 unsigned width
= widthOrig
,
2400 height
= heightOrig
;
2401 while ( (maxWidth
&& width
> maxWidth
) ||
2402 (maxHeight
&& height
> maxHeight
) )
2408 if ( width
!= widthOrig
|| height
!= heightOrig
)
2409 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2412 // Set this after Rescale, which currently does not preserve it
2413 M_IMGDATA
->m_type
= handler
.GetType();
2418 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2422 wxImageHandler
*handler
;
2424 if ( type
== wxBITMAP_TYPE_ANY
)
2426 const wxList
& list
= GetHandlers();
2427 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2429 node
= node
->GetNext() )
2431 handler
= (wxImageHandler
*)node
->GetData();
2432 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2436 wxLogWarning( _("No handler found for image type.") );
2440 //else: have specific type
2442 handler
= FindHandler(type
);
2445 wxLogWarning( _("No image handler for type %d defined."), type
);
2449 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2451 wxLogError(_("Image file is not of type %d."), type
);
2455 return DoLoad(*handler
, stream
, index
);
2458 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2462 m_refData
= new wxImageRefData
;
2464 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2468 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2472 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2474 wxLogError(_("Image file is not of type %s."), mimetype
);
2478 return DoLoad(*handler
, stream
, index
);
2481 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2483 wxImage
* const self
= const_cast<wxImage
*>(this);
2484 if ( !handler
.SaveFile(self
, stream
) )
2487 M_IMGDATA
->m_type
= handler
.GetType();
2491 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2493 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2495 wxImageHandler
*handler
= FindHandler(type
);
2498 wxLogWarning( _("No image handler for type %d defined."), type
);
2502 return DoSave(*handler
, stream
);
2505 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2507 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2509 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2512 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2515 return DoSave(*handler
, stream
);
2518 #endif // wxUSE_STREAMS
2520 // ----------------------------------------------------------------------------
2521 // image I/O handlers
2522 // ----------------------------------------------------------------------------
2524 void wxImage::AddHandler( wxImageHandler
*handler
)
2526 // Check for an existing handler of the type being added.
2527 if (FindHandler( handler
->GetType() ) == 0)
2529 sm_handlers
.Append( handler
);
2533 // This is not documented behaviour, merely the simplest 'fix'
2534 // for preventing duplicate additions. If someone ever has
2535 // a good reason to add and remove duplicate handlers (and they
2536 // may) we should probably refcount the duplicates.
2537 // also an issue in InsertHandler below.
2539 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2540 handler
->GetName().c_str() );
2545 void wxImage::InsertHandler( wxImageHandler
*handler
)
2547 // Check for an existing handler of the type being added.
2548 if (FindHandler( handler
->GetType() ) == 0)
2550 sm_handlers
.Insert( handler
);
2554 // see AddHandler for additional comments.
2555 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2556 handler
->GetName().c_str() );
2561 bool wxImage::RemoveHandler( const wxString
& name
)
2563 wxImageHandler
*handler
= FindHandler(name
);
2566 sm_handlers
.DeleteObject(handler
);
2574 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2576 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2579 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2580 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2582 node
= node
->GetNext();
2587 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2589 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2592 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2593 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2595 if (handler
->GetExtension() == extension
)
2597 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2600 node
= node
->GetNext();
2605 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2607 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2610 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2611 if (handler
->GetType() == bitmapType
) return handler
;
2612 node
= node
->GetNext();
2617 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2619 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2622 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2623 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2624 node
= node
->GetNext();
2629 void wxImage::InitStandardHandlers()
2632 AddHandler(new wxBMPHandler
);
2633 #endif // wxUSE_STREAMS
2636 void wxImage::CleanUpHandlers()
2638 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2641 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2642 wxList::compatibility_iterator next
= node
->GetNext();
2647 sm_handlers
.Clear();
2650 wxString
wxImage::GetImageExtWildcard()
2654 wxList
& Handlers
= wxImage::GetHandlers();
2655 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2658 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2659 fmts
+= wxT("*.") + Handler
->GetExtension();
2660 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2661 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2662 Node
= Node
->GetNext();
2663 if ( Node
) fmts
+= wxT(";");
2666 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2669 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2671 const double red
= rgb
.red
/ 255.0,
2672 green
= rgb
.green
/ 255.0,
2673 blue
= rgb
.blue
/ 255.0;
2675 // find the min and max intensity (and remember which one was it for the
2677 double minimumRGB
= red
;
2678 if ( green
< minimumRGB
)
2680 if ( blue
< minimumRGB
)
2683 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2684 double maximumRGB
= red
;
2685 if ( green
> maximumRGB
)
2690 if ( blue
> maximumRGB
)
2696 const double value
= maximumRGB
;
2698 double hue
= 0.0, saturation
;
2699 const double deltaRGB
= maximumRGB
- minimumRGB
;
2700 if ( wxIsNullDouble(deltaRGB
) )
2702 // Gray has no color
2711 hue
= (green
- blue
) / deltaRGB
;
2715 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2719 hue
= 4.0 + (red
- green
) / deltaRGB
;
2723 wxFAIL_MSG(wxT("hue not specified"));
2732 saturation
= deltaRGB
/ maximumRGB
;
2735 return HSVValue(hue
, saturation
, value
);
2738 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2740 double red
, green
, blue
;
2742 if ( wxIsNullDouble(hsv
.saturation
) )
2751 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2752 int i
= (int)floor(hue
);
2753 double f
= hue
- i
; // fractional part of h
2754 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2760 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2765 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2773 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2778 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2783 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2791 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2796 return RGBValue((unsigned char)(red
* 255.0),
2797 (unsigned char)(green
* 255.0),
2798 (unsigned char)(blue
* 255.0));
2802 * Rotates the hue of each pixel of the image. angle is a double in the range
2803 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2805 void wxImage::RotateHue(double angle
)
2809 unsigned char *srcBytePtr
;
2810 unsigned char *dstBytePtr
;
2811 unsigned long count
;
2812 wxImage::HSVValue hsv
;
2813 wxImage::RGBValue rgb
;
2815 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2816 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2817 if ( count
> 0 && !wxIsNullDouble(angle
) )
2819 srcBytePtr
= M_IMGDATA
->m_data
;
2820 dstBytePtr
= srcBytePtr
;
2823 rgb
.red
= *srcBytePtr
++;
2824 rgb
.green
= *srcBytePtr
++;
2825 rgb
.blue
= *srcBytePtr
++;
2826 hsv
= RGBtoHSV(rgb
);
2828 hsv
.hue
= hsv
.hue
+ angle
;
2830 hsv
.hue
= hsv
.hue
- 1.0;
2831 else if (hsv
.hue
< 0.0)
2832 hsv
.hue
= hsv
.hue
+ 1.0;
2834 rgb
= HSVtoRGB(hsv
);
2835 *dstBytePtr
++ = rgb
.red
;
2836 *dstBytePtr
++ = rgb
.green
;
2837 *dstBytePtr
++ = rgb
.blue
;
2838 } while (--count
!= 0);
2842 //-----------------------------------------------------------------------------
2844 //-----------------------------------------------------------------------------
2846 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2849 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
2851 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2852 // wxImageHandler::CallDoCanRead
2854 if ( !stream
.IsSeekable() )
2855 return false; // can't test unseekable stream
2857 wxFileOffset posOld
= stream
.TellI();
2858 int n
= DoGetImageCount(stream
);
2860 // restore the old position to be able to test other formats and so on
2861 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2863 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2865 // reading would fail anyhow as we're not at the right position
2872 bool wxImageHandler::CanRead( const wxString
& name
)
2874 if (wxFileExists(name
))
2876 wxImageFileInputStream
stream(name
);
2877 return CanRead(stream
);
2880 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2885 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2887 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2888 // wxImageHandler::GetImageCount
2890 if ( !stream
.IsSeekable() )
2891 return false; // can't test unseekable stream
2893 wxFileOffset posOld
= stream
.TellI();
2894 bool ok
= DoCanRead(stream
);
2896 // restore the old position to be able to test other formats and so on
2897 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2899 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2901 // reading would fail anyhow as we're not at the right position
2908 #endif // wxUSE_STREAMS
2912 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2914 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
2916 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2917 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2919 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2920 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2922 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2925 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2927 else // no resolution options specified
2932 return wxIMAGE_RESOLUTION_NONE
;
2935 // get the resolution unit too
2936 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2939 // this is the default
2940 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2943 return (wxImageResolution
)resUnit
;
2946 // ----------------------------------------------------------------------------
2947 // image histogram stuff
2948 // ----------------------------------------------------------------------------
2951 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2956 unsigned char g2
) const
2958 unsigned long key
= MakeKey(r2
, g2
, b2
);
2960 while ( find(key
) != end() )
2962 // color already used
2974 wxLogError(_("No unused colour in image.") );
2980 key
= MakeKey(r2
, g2
, b2
);
2994 wxImage::FindFirstUnusedColour(unsigned char *r
,
2999 unsigned char g2
) const
3001 wxImageHistogram histogram
;
3003 ComputeHistogram(histogram
);
3005 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3011 // Counts and returns the number of different colours. Optionally stops
3012 // when it exceeds 'stopafter' different colours. This is useful, for
3013 // example, to see if the image can be saved as 8-bit (256 colour or
3014 // less, in this case it would be invoked as CountColours(256)). Default
3015 // value for stopafter is -1 (don't care).
3017 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3021 unsigned char r
, g
, b
;
3023 unsigned long size
, nentries
, key
;
3026 size
= GetWidth() * GetHeight();
3029 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3034 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3036 if (h
.Get(key
) == NULL
)
3047 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3049 unsigned char *p
= GetData();
3050 unsigned long nentries
= 0;
3054 const unsigned long size
= GetWidth() * GetHeight();
3056 unsigned char r
, g
, b
;
3057 for ( unsigned long n
= 0; n
< size
; n
++ )
3063 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3065 if ( entry
.value
++ == 0 )
3066 entry
.index
= nentries
++;
3073 * Rotation code by Carlos Moreno
3076 static const double wxROTATE_EPSILON
= 1e-10;
3078 // Auxiliary function to rotate a point (x,y) with respect to point p0
3079 // make it inline and use a straight return to facilitate optimization
3080 // also, the function receives the sine and cosine of the angle to avoid
3081 // repeating the time-consuming calls to these functions -- sin/cos can
3082 // be computed and stored in the calling function.
3084 static inline wxRealPoint
3085 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3086 const wxRealPoint
& p0
)
3088 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3089 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3092 static inline wxRealPoint
3093 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3094 const wxRealPoint
& p0
)
3096 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3099 wxImage
wxImage::Rotate(double angle
,
3100 const wxPoint
& centre_of_rotation
,
3102 wxPoint
*offset_after_rotation
) const
3104 // screen coordinates are a mirror image of "real" coordinates
3107 const bool has_alpha
= HasAlpha();
3109 const int w
= GetWidth();
3110 const int h
= GetHeight();
3114 // Create pointer-based array to accelerate access to wxImage's data
3115 unsigned char ** data
= new unsigned char * [h
];
3116 data
[0] = GetData();
3117 for (i
= 1; i
< h
; i
++)
3118 data
[i
] = data
[i
- 1] + (3 * w
);
3120 // Same for alpha channel
3121 unsigned char ** alpha
= NULL
;
3124 alpha
= new unsigned char * [h
];
3125 alpha
[0] = GetAlpha();
3126 for (i
= 1; i
< h
; i
++)
3127 alpha
[i
] = alpha
[i
- 1] + w
;
3130 // precompute coefficients for rotation formula
3131 const double cos_angle
= cos(angle
);
3132 const double sin_angle
= sin(angle
);
3134 // Create new Image to store the result
3135 // First, find rectangle that covers the rotated image; to do that,
3136 // rotate the four corners
3138 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3140 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3141 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3142 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3143 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3145 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3146 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3147 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3148 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3150 // Create rotated image
3151 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3152 // With alpha channel
3156 if (offset_after_rotation
!= NULL
)
3158 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3161 // the rotated (destination) image is always accessed sequentially via this
3162 // pointer, there is no need for pointer-based arrays here
3163 unsigned char *dst
= rotated
.GetData();
3165 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3167 // if the original image has a mask, use its RGB values as the blank pixel,
3168 // else, fall back to default (black).
3169 unsigned char blank_r
= 0;
3170 unsigned char blank_g
= 0;
3171 unsigned char blank_b
= 0;
3175 blank_r
= GetMaskRed();
3176 blank_g
= GetMaskGreen();
3177 blank_b
= GetMaskBlue();
3178 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3181 // Now, for each point of the rotated image, find where it came from, by
3182 // performing an inverse rotation (a rotation of -angle) and getting the
3183 // pixel at those coordinates
3185 const int rH
= rotated
.GetHeight();
3186 const int rW
= rotated
.GetWidth();
3188 // do the (interpolating) test outside of the loops, so that it is done
3189 // only once, instead of repeating it for each pixel.
3192 for (int y
= 0; y
< rH
; y
++)
3194 for (int x
= 0; x
< rW
; x
++)
3196 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3198 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3199 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3201 // interpolate using the 4 enclosing grid-points. Those
3202 // points can be obtained using floor and ceiling of the
3203 // exact coordinates of the point
3206 if (0 < src
.x
&& src
.x
< w
- 1)
3208 x1
= wxRound(floor(src
.x
));
3209 x2
= wxRound(ceil(src
.x
));
3211 else // else means that x is near one of the borders (0 or width-1)
3213 x1
= x2
= wxRound (src
.x
);
3216 if (0 < src
.y
&& src
.y
< h
- 1)
3218 y1
= wxRound(floor(src
.y
));
3219 y2
= wxRound(ceil(src
.y
));
3223 y1
= y2
= wxRound (src
.y
);
3226 // get four points and the distances (square of the distance,
3227 // for efficiency reasons) for the interpolation formula
3229 // GRG: Do not calculate the points until they are
3230 // really needed -- this way we can calculate
3231 // just one, instead of four, if d1, d2, d3
3232 // or d4 are < wxROTATE_EPSILON
3234 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3235 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3236 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3237 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3239 // Now interpolate as a weighted average of the four surrounding
3240 // points, where the weights are the distances to each of those points
3242 // If the point is exactly at one point of the grid of the source
3243 // image, then don't interpolate -- just assign the pixel
3245 // d1,d2,d3,d4 are positive -- no need for abs()
3246 if (d1
< wxROTATE_EPSILON
)
3248 unsigned char *p
= data
[y1
] + (3 * x1
);
3254 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3256 else if (d2
< wxROTATE_EPSILON
)
3258 unsigned char *p
= data
[y1
] + (3 * x2
);
3264 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3266 else if (d3
< wxROTATE_EPSILON
)
3268 unsigned char *p
= data
[y2
] + (3 * x2
);
3274 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3276 else if (d4
< wxROTATE_EPSILON
)
3278 unsigned char *p
= data
[y2
] + (3 * x1
);
3284 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3288 // weights for the weighted average are proportional to the inverse of the distance
3289 unsigned char *v1
= data
[y1
] + (3 * x1
);
3290 unsigned char *v2
= data
[y1
] + (3 * x2
);
3291 unsigned char *v3
= data
[y2
] + (3 * x2
);
3292 unsigned char *v4
= data
[y2
] + (3 * x1
);
3294 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3298 *(dst
++) = (unsigned char)
3299 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3300 w3
* *(v3
++) + w4
* *(v4
++)) /
3301 (w1
+ w2
+ w3
+ w4
) );
3302 *(dst
++) = (unsigned char)
3303 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3304 w3
* *(v3
++) + w4
* *(v4
++)) /
3305 (w1
+ w2
+ w3
+ w4
) );
3306 *(dst
++) = (unsigned char)
3307 ( (w1
* *v1
+ w2
* *v2
+
3308 w3
* *v3
+ w4
* *v4
) /
3309 (w1
+ w2
+ w3
+ w4
) );
3313 v1
= alpha
[y1
] + (x1
);
3314 v2
= alpha
[y1
] + (x2
);
3315 v3
= alpha
[y2
] + (x2
);
3316 v4
= alpha
[y2
] + (x1
);
3318 *(alpha_dst
++) = (unsigned char)
3319 ( (w1
* *v1
+ w2
* *v2
+
3320 w3
* *v3
+ w4
* *v4
) /
3321 (w1
+ w2
+ w3
+ w4
) );
3337 else // not interpolating
3339 for (int y
= 0; y
< rH
; y
++)
3341 for (int x
= 0; x
< rW
; x
++)
3343 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3345 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3346 const int ys
= wxRound (src
.y
); // closest integer
3348 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3350 unsigned char *p
= data
[ys
] + (3 * xs
);
3356 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3365 *(alpha_dst
++) = 255;
3381 // A module to allow wxImage initialization/cleanup
3382 // without calling these functions from app.cpp or from
3383 // the user's application.
3385 class wxImageModule
: public wxModule
3387 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3390 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3391 void OnExit() { wxImage::CleanUpHandlers(); }
3394 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3397 #endif // wxUSE_IMAGE