1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/palette.h"
29 #include "wx/colour.h"
32 #include "wx/wfstream.h"
33 #include "wx/xpmdecod.h"
38 // make the code compile with either wxFile*Stream or wxFFile*Stream:
39 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
43 typedef wxFFileInputStream wxImageFileInputStream
;
44 typedef wxFFileOutputStream wxImageFileOutputStream
;
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
48 #endif // wxUSE_FILE/wxUSE_FFILE
49 #endif // HAS_FILE_STREAMS
52 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 wxList
wxImage::sm_handlers
;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 class wxImageRefData
: public wxObjectRefData
70 virtual ~wxImageRefData();
75 unsigned char *m_data
;
78 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
80 // alpha channel data, may be NULL for the formats without alpha support
81 unsigned char *m_alpha
;
85 // if true, m_data is pointer to static data and shouldn't be freed
88 // same as m_static but for m_alpha
93 #endif // wxUSE_PALETTE
95 wxArrayString m_optionNames
;
96 wxArrayString m_optionValues
;
98 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
101 wxImageRefData::wxImageRefData()
105 m_type
= wxBITMAP_TYPE_INVALID
;
107 m_alpha
= (unsigned char *) NULL
;
116 m_staticAlpha
= false;
119 wxImageRefData::~wxImageRefData()
123 if ( !m_staticAlpha
)
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
134 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
136 bool wxImage::Create(const char* const* xpmData
)
141 wxXPMDecoder decoder
;
142 (*this) = decoder
.ReadData(xpmData
);
149 bool wxImage::Create( int width
, int height
, bool clear
)
153 m_refData
= new wxImageRefData();
155 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
156 if (!M_IMGDATA
->m_data
)
162 M_IMGDATA
->m_width
= width
;
163 M_IMGDATA
->m_height
= height
;
164 M_IMGDATA
->m_ok
= true;
174 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
178 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
180 m_refData
= new wxImageRefData();
182 M_IMGDATA
->m_data
= data
;
183 M_IMGDATA
->m_width
= width
;
184 M_IMGDATA
->m_height
= height
;
185 M_IMGDATA
->m_ok
= true;
186 M_IMGDATA
->m_static
= static_data
;
191 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
195 wxCHECK_MSG( data
, false, wxT("NULL data in wxImage::Create") );
197 m_refData
= new wxImageRefData();
199 M_IMGDATA
->m_data
= data
;
200 M_IMGDATA
->m_alpha
= alpha
;
201 M_IMGDATA
->m_width
= width
;
202 M_IMGDATA
->m_height
= height
;
203 M_IMGDATA
->m_ok
= true;
204 M_IMGDATA
->m_static
= static_data
;
205 M_IMGDATA
->m_staticAlpha
= static_data
;
210 void wxImage::Destroy()
215 void wxImage::Clear(unsigned char value
)
217 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
220 wxObjectRefData
* wxImage::CreateRefData() const
222 return new wxImageRefData
;
225 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
227 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
228 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
230 wxImageRefData
* refData_new
= new wxImageRefData
;
231 refData_new
->m_width
= refData
->m_width
;
232 refData_new
->m_height
= refData
->m_height
;
233 refData_new
->m_maskRed
= refData
->m_maskRed
;
234 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
235 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
236 refData_new
->m_hasMask
= refData
->m_hasMask
;
237 refData_new
->m_ok
= true;
238 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
239 if (refData
->m_alpha
!= NULL
)
241 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
242 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
245 refData_new
->m_data
= (unsigned char*)malloc(size
);
246 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
248 refData_new
->m_palette
= refData
->m_palette
;
250 refData_new
->m_optionNames
= refData
->m_optionNames
;
251 refData_new
->m_optionValues
= refData
->m_optionValues
;
255 // returns a new image with the same dimensions, alpha, and mask as *this
256 // if on_its_side is true, width and height are swapped
257 wxImage
wxImage::MakeEmptyClone(int flags
) const
261 wxCHECK_MSG( IsOk(), image
, wxS("invalid image") );
263 long height
= M_IMGDATA
->m_height
;
264 long width
= M_IMGDATA
->m_width
;
266 if ( flags
& Clone_SwapOrientation
)
267 wxSwap( width
, height
);
269 if ( !image
.Create( width
, height
, false ) )
271 wxFAIL_MSG( wxS("unable to create image") );
275 if ( M_IMGDATA
->m_alpha
)
278 wxCHECK2_MSG( image
.GetAlpha(), return wxImage(),
279 wxS("unable to create alpha channel") );
282 if ( M_IMGDATA
->m_hasMask
)
284 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
285 M_IMGDATA
->m_maskGreen
,
286 M_IMGDATA
->m_maskBlue
);
292 wxImage
wxImage::Copy() const
296 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
298 image
.m_refData
= CloneRefData(m_refData
);
303 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
305 if( xFactor
== 1 && yFactor
== 1 )
310 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
312 // can't scale to/from 0 size
313 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
314 wxT("invalid new image size") );
316 long old_height
= M_IMGDATA
->m_height
,
317 old_width
= M_IMGDATA
->m_width
;
319 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
320 wxT("invalid old image size") );
322 long width
= old_width
/ xFactor
;
323 long height
= old_height
/ yFactor
;
325 image
.Create( width
, height
, false );
327 char unsigned *data
= image
.GetData();
329 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
331 bool hasMask
= false ;
332 unsigned char maskRed
= 0;
333 unsigned char maskGreen
= 0;
334 unsigned char maskBlue
= 0 ;
336 const unsigned char *source_data
= M_IMGDATA
->m_data
;
337 unsigned char *target_data
= data
;
338 const unsigned char *source_alpha
= 0 ;
339 unsigned char *target_alpha
= 0 ;
340 if (M_IMGDATA
->m_hasMask
)
343 maskRed
= M_IMGDATA
->m_maskRed
;
344 maskGreen
= M_IMGDATA
->m_maskGreen
;
345 maskBlue
=M_IMGDATA
->m_maskBlue
;
347 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
348 M_IMGDATA
->m_maskGreen
,
349 M_IMGDATA
->m_maskBlue
);
353 source_alpha
= M_IMGDATA
->m_alpha
;
357 target_alpha
= image
.GetAlpha() ;
361 for (long y
= 0; y
< height
; y
++)
363 for (long x
= 0; x
< width
; x
++)
365 unsigned long avgRed
= 0 ;
366 unsigned long avgGreen
= 0;
367 unsigned long avgBlue
= 0;
368 unsigned long avgAlpha
= 0 ;
369 unsigned long counter
= 0 ;
371 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
373 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
374 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
376 const unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
377 unsigned char red
= pixel
[0] ;
378 unsigned char green
= pixel
[1] ;
379 unsigned char blue
= pixel
[2] ;
380 unsigned char alpha
= 255 ;
382 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
383 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
398 *(target_data
++) = M_IMGDATA
->m_maskRed
;
399 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
400 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
405 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
406 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
407 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
408 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
413 // In case this is a cursor, make sure the hotspot is scaled accordingly:
414 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
415 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
416 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
417 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
418 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
419 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
425 wxImage::Scale( int width
, int height
, wxImageResizeQuality quality
) const
429 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
431 // can't scale to/from 0 size
432 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
433 wxT("invalid new image size") );
435 long old_height
= M_IMGDATA
->m_height
,
436 old_width
= M_IMGDATA
->m_width
;
437 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
438 wxT("invalid old image size") );
440 // If the image's new width and height are the same as the original, no
441 // need to waste time or CPU cycles
442 if ( old_width
== width
&& old_height
== height
)
445 if (quality
== wxIMAGE_QUALITY_HIGH
)
447 quality
= (width
< old_width
&& height
< old_height
)
448 ? wxIMAGE_QUALITY_BOX_AVERAGE
449 : wxIMAGE_QUALITY_BICUBIC
;
452 // Resample the image using the method as specified.
455 case wxIMAGE_QUALITY_NEAREST
:
456 if ( old_width
% width
== 0 && old_width
>= width
&&
457 old_height
% height
== 0 && old_height
>= height
)
459 return ShrinkBy( old_width
/ width
, old_height
/ height
);
462 image
= ResampleNearest(width
, height
);
465 case wxIMAGE_QUALITY_BILINEAR
:
466 image
= ResampleBilinear(width
, height
);
469 case wxIMAGE_QUALITY_BICUBIC
:
470 image
= ResampleBicubic(width
, height
);
473 case wxIMAGE_QUALITY_BOX_AVERAGE
:
474 image
= ResampleBox(width
, height
);
478 // If the original image has a mask, apply the mask to the new image
479 if (M_IMGDATA
->m_hasMask
)
481 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
482 M_IMGDATA
->m_maskGreen
,
483 M_IMGDATA
->m_maskBlue
);
486 // In case this is a cursor, make sure the hotspot is scaled accordingly:
487 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
488 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
489 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
490 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
491 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
492 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
497 wxImage
wxImage::ResampleNearest(int width
, int height
) const
500 image
.Create( width
, height
, false );
502 unsigned char *data
= image
.GetData();
504 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
506 const unsigned char *source_data
= M_IMGDATA
->m_data
;
507 unsigned char *target_data
= data
;
508 const unsigned char *source_alpha
= 0 ;
509 unsigned char *target_alpha
= 0 ;
511 if ( !M_IMGDATA
->m_hasMask
)
513 source_alpha
= M_IMGDATA
->m_alpha
;
517 target_alpha
= image
.GetAlpha() ;
521 long old_height
= M_IMGDATA
->m_height
,
522 old_width
= M_IMGDATA
->m_width
;
523 long x_delta
= (old_width
<<16) / width
;
524 long y_delta
= (old_height
<<16) / height
;
526 unsigned char* dest_pixel
= target_data
;
529 for ( long j
= 0; j
< height
; j
++ )
531 const unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
532 const unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
535 for ( long i
= 0; i
< width
; i
++ )
537 const unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
538 const unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
539 dest_pixel
[0] = src_pixel
[0];
540 dest_pixel
[1] = src_pixel
[1];
541 dest_pixel
[2] = src_pixel
[2];
544 *(target_alpha
++) = *src_alpha_pixel
;
554 wxImage
wxImage::ResampleBox(int width
, int height
) const
556 // This function implements a simple pre-blur/box averaging method for
557 // downsampling that gives reasonably smooth results To scale the image
558 // down we will need to gather a grid of pixels of the size of the scale
559 // factor in each direction and then do an averaging of the pixels.
561 wxImage
ret_image(width
, height
, false);
563 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
564 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
566 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
567 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
569 const unsigned char* src_data
= M_IMGDATA
->m_data
;
570 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
571 unsigned char* dst_data
= ret_image
.GetData();
572 unsigned char* dst_alpha
= NULL
;
576 ret_image
.SetAlpha();
577 dst_alpha
= ret_image
.GetAlpha();
580 int averaged_pixels
, src_pixel_index
;
581 double sum_r
, sum_g
, sum_b
, sum_a
;
583 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
585 // Source pixel in the Y direction
586 int src_y
= (int)(y
* scale_factor_y
);
588 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
590 // Source pixel in the X direction
591 int src_x
= (int)(x
* scale_factor_x
);
593 // Box of pixels to average
595 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
597 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1), k
= j
;
598 j
<= int(src_y
+ scale_factor_y_2
) || j
< k
+ 2;
601 // We don't care to average pixels that don't exist (edges)
602 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
605 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1), e
= i
;
606 i
<= src_x
+ scale_factor_x_2
|| i
< e
+ 2;
609 // Don't average edge pixels
610 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
613 // Calculate the actual index in our source pixels
614 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
616 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
617 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
618 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
620 sum_a
+= src_alpha
[src_pixel_index
];
626 // Calculate the average from the sum and number of averaged pixels
627 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
628 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
629 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
632 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
639 wxImage
wxImage::ResampleBilinear(int width
, int height
) const
641 // This function implements a Bilinear algorithm for resampling.
642 wxImage
ret_image(width
, height
, false);
643 const unsigned char* src_data
= M_IMGDATA
->m_data
;
644 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
645 unsigned char* dst_data
= ret_image
.GetData();
646 unsigned char* dst_alpha
= NULL
;
650 ret_image
.SetAlpha();
651 dst_alpha
= ret_image
.GetAlpha();
653 double HFactor
= double(M_IMGDATA
->m_height
) / height
;
654 double WFactor
= double(M_IMGDATA
->m_width
) / width
;
656 int srcpixymax
= M_IMGDATA
->m_height
- 1;
657 int srcpixxmax
= M_IMGDATA
->m_width
- 1;
659 double srcpixy
, srcpixy1
, srcpixy2
, dy
, dy1
;
660 double srcpixx
, srcpixx1
, srcpixx2
, dx
, dx1
;
662 // initialize alpha values to avoid g++ warnings about possibly
663 // uninitialized variables
664 double r1
, g1
, b1
, a1
= 0;
665 double r2
, g2
, b2
, a2
= 0;
667 for ( int dsty
= 0; dsty
< height
; dsty
++ )
669 // We need to calculate the source pixel to interpolate from - Y-axis
670 srcpixy
= double(dsty
) * HFactor
;
671 srcpixy1
= int(srcpixy
);
672 srcpixy2
= ( srcpixy1
== srcpixymax
) ? srcpixy1
: srcpixy1
+ 1.0;
673 dy
= srcpixy
- (int)srcpixy
;
677 for ( int dstx
= 0; dstx
< width
; dstx
++ )
679 // X-axis of pixel to interpolate from
680 srcpixx
= double(dstx
) * WFactor
;
681 srcpixx1
= int(srcpixx
);
682 srcpixx2
= ( srcpixx1
== srcpixxmax
) ? srcpixx1
: srcpixx1
+ 1.0;
683 dx
= srcpixx
- (int)srcpixx
;
686 int x_offset1
= srcpixx1
< 0.0 ? 0 : srcpixx1
> srcpixxmax
? srcpixxmax
: (int)srcpixx1
;
687 int x_offset2
= srcpixx2
< 0.0 ? 0 : srcpixx2
> srcpixxmax
? srcpixxmax
: (int)srcpixx2
;
688 int y_offset1
= srcpixy1
< 0.0 ? 0 : srcpixy1
> srcpixymax
? srcpixymax
: (int)srcpixy1
;
689 int y_offset2
= srcpixy2
< 0.0 ? 0 : srcpixy2
> srcpixymax
? srcpixymax
: (int)srcpixy2
;
691 int src_pixel_index00
= y_offset1
* M_IMGDATA
->m_width
+ x_offset1
;
692 int src_pixel_index01
= y_offset1
* M_IMGDATA
->m_width
+ x_offset2
;
693 int src_pixel_index10
= y_offset2
* M_IMGDATA
->m_width
+ x_offset1
;
694 int src_pixel_index11
= y_offset2
* M_IMGDATA
->m_width
+ x_offset2
;
697 r1
= src_data
[src_pixel_index00
* 3 + 0] * dx1
+ src_data
[src_pixel_index01
* 3 + 0] * dx
;
698 g1
= src_data
[src_pixel_index00
* 3 + 1] * dx1
+ src_data
[src_pixel_index01
* 3 + 1] * dx
;
699 b1
= src_data
[src_pixel_index00
* 3 + 2] * dx1
+ src_data
[src_pixel_index01
* 3 + 2] * dx
;
701 a1
= src_alpha
[src_pixel_index00
] * dx1
+ src_alpha
[src_pixel_index01
] * dx
;
704 r2
= src_data
[src_pixel_index10
* 3 + 0] * dx1
+ src_data
[src_pixel_index11
* 3 + 0] * dx
;
705 g2
= src_data
[src_pixel_index10
* 3 + 1] * dx1
+ src_data
[src_pixel_index11
* 3 + 1] * dx
;
706 b2
= src_data
[src_pixel_index10
* 3 + 2] * dx1
+ src_data
[src_pixel_index11
* 3 + 2] * dx
;
708 a2
= src_alpha
[src_pixel_index10
] * dx1
+ src_alpha
[src_pixel_index11
] * dx
;
712 dst_data
[0] = static_cast<unsigned char>(r1
* dy1
+ r2
* dy
);
713 dst_data
[1] = static_cast<unsigned char>(g1
* dy1
+ g2
* dy
);
714 dst_data
[2] = static_cast<unsigned char>(b1
* dy1
+ b2
* dy
);
718 *dst_alpha
++ = static_cast<unsigned char>(a1
* dy1
+ a2
* dy
);
725 // The following two local functions are for the B-spline weighting of the
726 // bicubic sampling algorithm
727 static inline double spline_cube(double value
)
729 return value
<= 0.0 ? 0.0 : value
* value
* value
;
732 static inline double spline_weight(double value
)
734 return (spline_cube(value
+ 2) -
735 4 * spline_cube(value
+ 1) +
736 6 * spline_cube(value
) -
737 4 * spline_cube(value
- 1)) / 6;
740 // This is the bicubic resampling algorithm
741 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
743 // This function implements a Bicubic B-Spline algorithm for resampling.
744 // This method is certainly a little slower than wxImage's default pixel
745 // replication method, however for most reasonably sized images not being
746 // upsampled too much on a fairly average CPU this difference is hardly
747 // noticeable and the results are far more pleasing to look at.
749 // This particular bicubic algorithm does pixel weighting according to a
750 // B-Spline that basically implements a Gaussian bell-like weighting
751 // kernel. Because of this method the results may appear a bit blurry when
752 // upsampling by large factors. This is basically because a slight
753 // gaussian blur is being performed to get the smooth look of the upsampled
756 // Edge pixels: 3-4 possible solutions
757 // - (Wrap/tile) Wrap the image, take the color value from the opposite
758 // side of the image.
759 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
760 // where n is nonpositive, will have the value of (2, 1).
761 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
762 // pixels which do have all neighbours.
763 // - (Clamp) Choose the nearest pixel along the border. This takes the
764 // border pixels and extends them out to infinity.
766 // NOTE: below the y_offset and x_offset variables are being set for edge
767 // pixels using the "Mirror" method mentioned above
771 ret_image
.Create(width
, height
, false);
773 const unsigned char* src_data
= M_IMGDATA
->m_data
;
774 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
775 unsigned char* dst_data
= ret_image
.GetData();
776 unsigned char* dst_alpha
= NULL
;
780 ret_image
.SetAlpha();
781 dst_alpha
= ret_image
.GetAlpha();
784 for ( int dsty
= 0; dsty
< height
; dsty
++ )
786 // We need to calculate the source pixel to interpolate from - Y-axis
787 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
788 double dy
= srcpixy
- (int)srcpixy
;
790 for ( int dstx
= 0; dstx
< width
; dstx
++ )
792 // X-axis of pixel to interpolate from
793 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
794 double dx
= srcpixx
- (int)srcpixx
;
796 // Sums for each color channel
797 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
799 // Here we actually determine the RGBA values for the destination pixel
800 for ( int k
= -1; k
<= 2; k
++ )
803 int y_offset
= srcpixy
+ k
< 0.0
805 : srcpixy
+ k
>= M_IMGDATA
->m_height
806 ? M_IMGDATA
->m_height
- 1
807 : (int)(srcpixy
+ k
);
809 // Loop across the X axis
810 for ( int i
= -1; i
<= 2; i
++ )
813 int x_offset
= srcpixx
+ i
< 0.0
815 : srcpixx
+ i
>= M_IMGDATA
->m_width
816 ? M_IMGDATA
->m_width
- 1
817 : (int)(srcpixx
+ i
);
819 // Calculate the exact position where the source data
820 // should be pulled from based on the x_offset and y_offset
821 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
823 // Calculate the weight for the specified pixel according
824 // to the bicubic b-spline kernel we're using for
827 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
829 // Create a sum of all velues for each color channel
830 // adjusted for the pixel's calculated weight
831 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
832 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
833 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
835 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
839 // Put the data into the destination image. The summed values are
840 // of double data type and are rounded here for accuracy
841 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
842 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
843 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
847 *dst_alpha
++ = (unsigned char)sum_a
;
854 // Blur in the horizontal direction
855 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
857 wxImage
ret_image(MakeEmptyClone());
859 wxCHECK( ret_image
.IsOk(), ret_image
);
861 const unsigned char* src_data
= M_IMGDATA
->m_data
;
862 unsigned char* dst_data
= ret_image
.GetData();
863 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
864 unsigned char* dst_alpha
= ret_image
.GetAlpha();
866 // number of pixels we average over
867 const int blurArea
= blurRadius
*2 + 1;
869 // Horizontal blurring algorithm - average all pixels in the specified blur
870 // radius in the X or horizontal direction
871 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
873 // Variables used in the blurring algorithm
880 const unsigned char *src
;
883 // Calculate the average of all pixels in the blur radius for the first
885 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
887 // To deal with the pixels at the start of a row so it's not
888 // grabbing GOK values from memory at negative indices of the
889 // image's data or grabbing from the previous row
891 pixel_idx
= y
* M_IMGDATA
->m_width
;
893 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
895 src
= src_data
+ pixel_idx
*3;
900 sum_a
+= src_alpha
[pixel_idx
];
903 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
904 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
905 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
906 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
908 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
910 // Now average the values of the rest of the pixels by just moving the
911 // blur radius box along the row
912 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
914 // Take care of edge pixels on the left edge by essentially
915 // duplicating the edge pixel
916 if ( x
- blurRadius
- 1 < 0 )
917 pixel_idx
= y
* M_IMGDATA
->m_width
;
919 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
921 // Subtract the value of the pixel at the left side of the blur
923 src
= src_data
+ pixel_idx
*3;
928 sum_a
-= src_alpha
[pixel_idx
];
930 // Take care of edge pixels on the right edge
931 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
932 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
934 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
936 // Add the value of the pixel being added to the end of our box
937 src
= src_data
+ pixel_idx
*3;
942 sum_a
+= src_alpha
[pixel_idx
];
944 // Save off the averaged data
945 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
946 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
947 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
948 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
950 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
957 // Blur in the vertical direction
958 wxImage
wxImage::BlurVertical(int blurRadius
) const
960 wxImage
ret_image(MakeEmptyClone());
962 wxCHECK( ret_image
.IsOk(), ret_image
);
964 const unsigned char* src_data
= M_IMGDATA
->m_data
;
965 unsigned char* dst_data
= ret_image
.GetData();
966 const unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
967 unsigned char* dst_alpha
= ret_image
.GetAlpha();
969 // number of pixels we average over
970 const int blurArea
= blurRadius
*2 + 1;
972 // Vertical blurring algorithm - same as horizontal but switched the
973 // opposite direction
974 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
976 // Variables used in the blurring algorithm
983 const unsigned char *src
;
986 // Calculate the average of all pixels in our blur radius box for the
987 // first pixel of the column
988 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
990 // To deal with the pixels at the start of a column so it's not
991 // grabbing GOK values from memory at negative indices of the
992 // image's data or grabbing from the previous column
996 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
998 src
= src_data
+ pixel_idx
*3;
1003 sum_a
+= src_alpha
[pixel_idx
];
1006 dst
= dst_data
+ x
*3;
1007 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1008 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1009 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1011 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
1013 // Now average the values of the rest of the pixels by just moving the
1014 // box along the column from top to bottom
1015 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
1017 // Take care of pixels that would be beyond the top edge by
1018 // duplicating the top edge pixel for the column
1019 if ( y
- blurRadius
- 1 < 0 )
1022 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
1024 // Subtract the value of the pixel at the top of our blur radius box
1025 src
= src_data
+ pixel_idx
*3;
1030 sum_a
-= src_alpha
[pixel_idx
];
1032 // Take care of the pixels that would be beyond the bottom edge of
1033 // the image similar to the top edge
1034 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
1035 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
1037 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
1039 // Add the value of the pixel being added to the end of our box
1040 src
= src_data
+ pixel_idx
*3;
1045 sum_a
+= src_alpha
[pixel_idx
];
1047 // Save off the averaged data
1048 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
1049 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
1050 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
1051 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
1053 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
1060 // The new blur function
1061 wxImage
wxImage::Blur(int blurRadius
) const
1064 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1066 // Blur the image in each direction
1067 ret_image
= BlurHorizontal(blurRadius
);
1068 ret_image
= ret_image
.BlurVertical(blurRadius
);
1073 wxImage
wxImage::Rotate90( bool clockwise
) const
1075 wxImage
image(MakeEmptyClone(Clone_SwapOrientation
));
1077 wxCHECK( image
.IsOk(), image
);
1079 long height
= M_IMGDATA
->m_height
;
1080 long width
= M_IMGDATA
->m_width
;
1082 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1084 int hot_x
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_X
);
1085 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1086 clockwise
? hot_x
: width
- 1 - hot_x
);
1089 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1091 int hot_y
= GetOptionInt( wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
1092 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1093 clockwise
? height
- 1 - hot_y
: hot_y
);
1096 unsigned char *data
= image
.GetData();
1097 unsigned char *target_data
;
1099 // we rotate the image in 21-pixel (63-byte) wide strips
1100 // to make better use of cpu cache - memory transfers
1101 // (note: while much better than single-pixel "strips",
1102 // our vertical strips will still generally straddle 64-byte cachelines)
1103 for (long ii
= 0; ii
< width
; )
1105 long next_ii
= wxMin(ii
+ 21, width
);
1107 for (long j
= 0; j
< height
; j
++)
1109 const unsigned char *source_data
1110 = M_IMGDATA
->m_data
+ (j
*width
+ ii
)*3;
1112 for (long i
= ii
; i
< next_ii
; i
++)
1116 target_data
= data
+ ((i
+ 1)*height
- j
- 1)*3;
1120 target_data
= data
+ (height
*(width
- 1 - i
) + j
)*3;
1122 memcpy( target_data
, source_data
, 3 );
1130 const unsigned char *source_alpha
= M_IMGDATA
->m_alpha
;
1134 unsigned char *alpha_data
= image
.GetAlpha();
1135 unsigned char *target_alpha
= 0 ;
1137 for (long ii
= 0; ii
< width
; )
1139 long next_ii
= wxMin(ii
+ 64, width
);
1141 for (long j
= 0; j
< height
; j
++)
1143 source_alpha
= M_IMGDATA
->m_alpha
+ j
*width
+ ii
;
1145 for (long i
= ii
; i
< next_ii
; i
++)
1149 target_alpha
= alpha_data
+ (i
+1)*height
- j
- 1;
1153 target_alpha
= alpha_data
+ height
*(width
- i
- 1) + j
;
1156 *target_alpha
= *source_alpha
++;
1167 wxImage
wxImage::Rotate180() const
1169 wxImage
image(MakeEmptyClone());
1171 wxCHECK( image
.IsOk(), image
);
1173 long height
= M_IMGDATA
->m_height
;
1174 long width
= M_IMGDATA
->m_width
;
1176 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
1178 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
1179 width
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
));
1182 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
1184 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
1185 height
- 1 - GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
));
1188 unsigned char *data
= image
.GetData();
1189 unsigned char *alpha
= image
.GetAlpha();
1190 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1191 unsigned char *target_data
= data
+ width
* height
* 3;
1193 for (long j
= 0; j
< height
; j
++)
1195 for (long i
= 0; i
< width
; i
++)
1198 memcpy( target_data
, source_data
, 3 );
1205 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1206 unsigned char *dest_alpha
= alpha
+ width
* height
;
1208 for (long j
= 0; j
< height
; ++j
)
1210 for (long i
= 0; i
< width
; ++i
)
1212 *(--dest_alpha
) = *(src_alpha
++);
1220 wxImage
wxImage::Mirror( bool horizontally
) const
1222 wxImage
image(MakeEmptyClone());
1224 wxCHECK( image
.IsOk(), image
);
1226 long height
= M_IMGDATA
->m_height
;
1227 long width
= M_IMGDATA
->m_width
;
1229 unsigned char *data
= image
.GetData();
1230 unsigned char *alpha
= image
.GetAlpha();
1231 const unsigned char *source_data
= M_IMGDATA
->m_data
;
1232 unsigned char *target_data
;
1236 for (long j
= 0; j
< height
; j
++)
1239 target_data
= data
-3;
1240 for (long i
= 0; i
< width
; i
++)
1242 memcpy( target_data
, source_data
, 3 );
1250 // src_alpha starts at the first pixel and increases by 1 after each step
1251 // (a step here is the copy of the alpha value of one pixel)
1252 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1253 // dest_alpha starts just beyond the first line, decreases before each step,
1254 // and after each line is finished, increases by 2 widths (skipping the line
1255 // just copied and the line that will be copied next)
1256 unsigned char *dest_alpha
= alpha
+ width
;
1258 for (long jj
= 0; jj
< height
; ++jj
)
1260 for (long i
= 0; i
< width
; ++i
) {
1261 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1263 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1269 for (long i
= 0; i
< height
; i
++)
1271 target_data
= data
+ 3*width
*(height
-1-i
);
1272 memcpy( target_data
, source_data
, (size_t)3*width
);
1273 source_data
+= 3*width
;
1278 // src_alpha starts at the first pixel and increases by 1 width after each step
1279 // (a step here is the copy of the alpha channel of an entire line)
1280 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1281 // dest_alpha starts just beyond the last line (beyond the whole image)
1282 // and decreases by 1 width before each step
1283 unsigned char *dest_alpha
= alpha
+ width
* height
;
1285 for (long jj
= 0; jj
< height
; ++jj
)
1287 dest_alpha
-= width
;
1288 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1297 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1301 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1303 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1304 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1305 image
, wxT("invalid subimage size") );
1307 const int subwidth
= rect
.GetWidth();
1308 const int subheight
= rect
.GetHeight();
1310 image
.Create( subwidth
, subheight
, false );
1312 const unsigned char *src_data
= GetData();
1313 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1314 unsigned char *subdata
= image
.GetData();
1315 unsigned char *subalpha
= NULL
;
1317 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1321 subalpha
= image
.GetAlpha();
1322 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1325 if (M_IMGDATA
->m_hasMask
)
1326 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1328 const int width
= GetWidth();
1329 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1331 src_data
+= 3 * pixsoff
;
1332 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1334 for (long j
= 0; j
< subheight
; ++j
)
1336 memcpy( subdata
, src_data
, 3 * subwidth
);
1337 subdata
+= 3 * subwidth
;
1338 src_data
+= 3 * width
;
1339 if (subalpha
!= NULL
) {
1340 memcpy( subalpha
, src_alpha
, subwidth
);
1341 subalpha
+= subwidth
;
1349 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1350 int r_
, int g_
, int b_
) const
1354 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1355 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1357 int width
= GetWidth(), height
= GetHeight();
1358 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1360 unsigned char r
= (unsigned char)r_
;
1361 unsigned char g
= (unsigned char)g_
;
1362 unsigned char b
= (unsigned char)b_
;
1363 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1365 GetOrFindMaskColour( &r
, &g
, &b
);
1366 image
.SetMaskColour(r
, g
, b
);
1369 image
.SetRGB(wxRect(), r
, g
, b
);
1371 // we have two coordinate systems:
1372 // source: starting at 0,0 of source image
1373 // destination starting at 0,0 of destination image
1374 // Documentation says:
1375 // "The image is pasted into a new image [...] at the position pos relative
1376 // to the upper left of the new image." this means the transition rule is:
1377 // "dest coord" = "source coord" + pos;
1379 // calculate the intersection using source coordinates:
1380 wxRect
srcRect(0, 0, width
, height
);
1381 wxRect
dstRect(-pos
, size
);
1383 srcRect
.Intersect(dstRect
);
1385 if (!srcRect
.IsEmpty())
1387 // insertion point is needed in destination coordinates.
1388 // NB: it is not always "pos"!
1389 wxPoint ptInsert
= srcRect
.GetTopLeft() + pos
;
1391 if ((srcRect
.GetWidth() == width
) && (srcRect
.GetHeight() == height
))
1392 image
.Paste(*this, ptInsert
.x
, ptInsert
.y
);
1394 image
.Paste(GetSubImage(srcRect
), ptInsert
.x
, ptInsert
.y
);
1400 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1402 wxCHECK_RET( IsOk(), wxT("invalid image") );
1403 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1409 int width
= image
.GetWidth();
1410 int height
= image
.GetHeight();
1423 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1424 width
= M_IMGDATA
->m_width
- (x
+xx
);
1425 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1426 height
= M_IMGDATA
->m_height
- (y
+yy
);
1428 if (width
< 1) return;
1429 if (height
< 1) return;
1431 // If we can, copy the data using memcpy() as this is the fastest way. But
1432 // for this the image being pasted must have "compatible" mask with this
1433 // one meaning that either it must not have one at all or it must use the
1434 // same masked colour.
1435 if ( !image
.HasMask() ||
1437 (GetMaskRed()==image
.GetMaskRed()) &&
1438 (GetMaskGreen()==image
.GetMaskGreen()) &&
1439 (GetMaskBlue()==image
.GetMaskBlue()))) )
1441 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1442 int source_step
= image
.GetWidth()*3;
1444 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1445 int target_step
= M_IMGDATA
->m_width
*3;
1446 for (int j
= 0; j
< height
; j
++)
1448 memcpy( target_data
, source_data
, width
*3 );
1449 source_data
+= source_step
;
1450 target_data
+= target_step
;
1454 // Copy over the alpha channel from the original image
1455 if ( image
.HasAlpha() )
1460 const unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1461 int source_step
= image
.GetWidth();
1463 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1464 int target_step
= M_IMGDATA
->m_width
;
1466 for (int j
= 0; j
< height
; j
++,
1467 source_data
+= source_step
,
1468 target_data
+= target_step
)
1470 memcpy( target_data
, source_data
, width
);
1474 if (!HasMask() && image
.HasMask())
1476 unsigned char r
= image
.GetMaskRed();
1477 unsigned char g
= image
.GetMaskGreen();
1478 unsigned char b
= image
.GetMaskBlue();
1480 const unsigned char* source_data
= image
.GetData() + 3*(xx
+ yy
*image
.GetWidth());
1481 int source_step
= image
.GetWidth()*3;
1483 unsigned char* target_data
= GetData() + 3*((x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
);
1484 int target_step
= M_IMGDATA
->m_width
*3;
1486 for (int j
= 0; j
< height
; j
++)
1488 for (int i
= 0; i
< width
*3; i
+=3)
1490 if ((source_data
[i
] != r
) ||
1491 (source_data
[i
+1] != g
) ||
1492 (source_data
[i
+2] != b
))
1494 memcpy( target_data
+i
, source_data
+i
, 3 );
1497 source_data
+= source_step
;
1498 target_data
+= target_step
;
1503 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1504 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1506 wxCHECK_RET( IsOk(), wxT("invalid image") );
1510 unsigned char *data
= GetData();
1512 const int w
= GetWidth();
1513 const int h
= GetHeight();
1515 for (int j
= 0; j
< h
; j
++)
1516 for (int i
= 0; i
< w
; i
++)
1518 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1528 wxImage
wxImage::ConvertToGreyscale(void) const
1530 return ConvertToGreyscale(0.299, 0.587, 0.114);
1533 wxImage
wxImage::ConvertToGreyscale(double weight_r
, double weight_g
, double weight_b
) const
1535 wxImage
image(MakeEmptyClone());
1537 wxCHECK( image
.IsOk(), image
);
1539 const unsigned char *src
= M_IMGDATA
->m_data
;
1540 unsigned char *dest
= image
.GetData();
1542 const bool hasMask
= M_IMGDATA
->m_hasMask
;
1543 const unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1544 const unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1545 const unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1547 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1548 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1550 memcpy(dest
, src
, 3);
1551 // only modify non-masked pixels
1552 if ( !hasMask
|| src
[0] != maskRed
|| src
[1] != maskGreen
|| src
[2] != maskBlue
)
1554 wxColour::MakeGrey(dest
+ 0, dest
+ 1, dest
+ 2, weight_r
, weight_g
, weight_b
);
1558 // copy the alpha channel, if any
1559 if ( image
.HasAlpha() )
1561 memcpy( image
.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() );
1567 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1571 wxCHECK_MSG( IsOk(), image
, wxT("invalid image") );
1573 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1575 unsigned char *data
= image
.GetData();
1577 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1579 if (M_IMGDATA
->m_hasMask
)
1581 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1582 M_IMGDATA
->m_maskBlue
== b
)
1583 image
.SetMaskColour( 255, 255, 255 );
1585 image
.SetMaskColour( 0, 0, 0 );
1588 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1590 unsigned char *srcd
= M_IMGDATA
->m_data
;
1591 unsigned char *tard
= image
.GetData();
1593 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1595 bool on
= (srcd
[0] == r
) && (srcd
[1] == g
) && (srcd
[2] == b
);
1596 wxColourBase::MakeMono(tard
+ 0, tard
+ 1, tard
+ 2, on
);
1602 wxImage
wxImage::ConvertToDisabled(unsigned char brightness
) const
1604 wxImage image
= *this;
1606 unsigned char mr
= image
.GetMaskRed();
1607 unsigned char mg
= image
.GetMaskGreen();
1608 unsigned char mb
= image
.GetMaskBlue();
1610 int width
= image
.GetWidth();
1611 int height
= image
.GetHeight();
1612 bool has_mask
= image
.HasMask();
1614 for (int y
= height
-1; y
>= 0; --y
)
1616 for (int x
= width
-1; x
>= 0; --x
)
1618 unsigned char* data
= image
.GetData() + (y
*(width
*3))+(x
*3);
1619 unsigned char* r
= data
;
1620 unsigned char* g
= data
+1;
1621 unsigned char* b
= data
+2;
1623 if (has_mask
&& (*r
== mr
) && (*g
== mg
) && (*b
== mb
))
1626 wxColour::MakeDisabled(r
, g
, b
, brightness
);
1632 int wxImage::GetWidth() const
1634 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1636 return M_IMGDATA
->m_width
;
1639 int wxImage::GetHeight() const
1641 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1643 return M_IMGDATA
->m_height
;
1646 wxBitmapType
wxImage::GetType() const
1648 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1650 return M_IMGDATA
->m_type
;
1653 void wxImage::SetType(wxBitmapType type
)
1655 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1657 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1658 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1660 M_IMGDATA
->m_type
= type
;
1663 long wxImage::XYToIndex(int x
, int y
) const
1667 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1669 return y
*M_IMGDATA
->m_width
+ x
;
1675 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1677 long pos
= XYToIndex(x
, y
);
1678 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1684 M_IMGDATA
->m_data
[ pos
] = r
;
1685 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1686 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1689 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1691 wxCHECK_RET( IsOk(), wxT("invalid image") );
1696 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1697 if ( rect
== wxRect() )
1703 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1704 imageRect
.Contains(rect
.GetBottomRight()),
1705 wxT("invalid bounding rectangle") );
1708 int x1
= rect
.GetLeft(),
1710 x2
= rect
.GetRight() + 1,
1711 y2
= rect
.GetBottom() + 1;
1713 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1714 int x
, y
, width
= GetWidth();
1715 for (y
= y1
; y
< y2
; y
++)
1717 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1718 for (x
= x1
; x
< x2
; x
++)
1727 unsigned char wxImage::GetRed( int x
, int y
) const
1729 long pos
= XYToIndex(x
, y
);
1730 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1734 return M_IMGDATA
->m_data
[pos
];
1737 unsigned char wxImage::GetGreen( int x
, int y
) const
1739 long pos
= XYToIndex(x
, y
);
1740 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1744 return M_IMGDATA
->m_data
[pos
+1];
1747 unsigned char wxImage::GetBlue( int x
, int y
) const
1749 long pos
= XYToIndex(x
, y
);
1750 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1754 return M_IMGDATA
->m_data
[pos
+2];
1757 bool wxImage::IsOk() const
1759 // image of 0 width or height can't be considered ok - at least because it
1760 // causes crashes in ConvertToBitmap() if we don't catch it in time
1761 wxImageRefData
*data
= M_IMGDATA
;
1762 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1765 unsigned char *wxImage::GetData() const
1767 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
1769 return M_IMGDATA
->m_data
;
1772 void wxImage::SetData( unsigned char *data
, bool static_data
)
1774 wxCHECK_RET( IsOk(), wxT("invalid image") );
1776 wxImageRefData
*newRefData
= new wxImageRefData();
1778 newRefData
->m_width
= M_IMGDATA
->m_width
;
1779 newRefData
->m_height
= M_IMGDATA
->m_height
;
1780 newRefData
->m_data
= data
;
1781 newRefData
->m_ok
= true;
1782 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1783 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1784 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1785 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1786 newRefData
->m_static
= static_data
;
1790 m_refData
= newRefData
;
1793 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1795 wxImageRefData
*newRefData
= new wxImageRefData();
1799 newRefData
->m_width
= new_width
;
1800 newRefData
->m_height
= new_height
;
1801 newRefData
->m_data
= data
;
1802 newRefData
->m_ok
= true;
1803 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1804 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1805 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1806 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1810 newRefData
->m_width
= new_width
;
1811 newRefData
->m_height
= new_height
;
1812 newRefData
->m_data
= data
;
1813 newRefData
->m_ok
= true;
1815 newRefData
->m_static
= static_data
;
1819 m_refData
= newRefData
;
1822 // ----------------------------------------------------------------------------
1823 // alpha channel support
1824 // ----------------------------------------------------------------------------
1826 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1828 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1830 long pos
= XYToIndex(x
, y
);
1831 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1835 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1838 unsigned char wxImage::GetAlpha(int x
, int y
) const
1840 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1842 long pos
= XYToIndex(x
, y
);
1843 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1845 return M_IMGDATA
->m_alpha
[pos
];
1849 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1853 const int w
= M_IMGDATA
->m_width
;
1854 const int h
= M_IMGDATA
->m_height
;
1856 unsigned char *alpha
= GetAlpha();
1857 unsigned char *data
= GetData();
1859 for ( int y
= 0; y
< h
; y
++ )
1861 for ( int x
= 0; x
< w
; x
++ )
1873 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1875 wxCHECK_RET( IsOk(), wxT("invalid image") );
1881 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1884 if( !M_IMGDATA
->m_staticAlpha
)
1885 free(M_IMGDATA
->m_alpha
);
1887 M_IMGDATA
->m_alpha
= alpha
;
1888 M_IMGDATA
->m_staticAlpha
= static_data
;
1891 unsigned char *wxImage::GetAlpha() const
1893 wxCHECK_MSG( IsOk(), (unsigned char *)NULL
, wxT("invalid image") );
1895 return M_IMGDATA
->m_alpha
;
1898 void wxImage::InitAlpha()
1900 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1902 // initialize memory for alpha channel
1905 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1906 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1910 // use the mask to initialize the alpha channel.
1911 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1913 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1914 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1915 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1916 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1920 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1921 ? wxIMAGE_ALPHA_TRANSPARENT
1922 : wxIMAGE_ALPHA_OPAQUE
;
1925 M_IMGDATA
->m_hasMask
= false;
1929 // make the image fully opaque
1930 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1934 void wxImage::ClearAlpha()
1936 wxCHECK_RET( HasAlpha(), wxT("image already doesn't have an alpha channel") );
1938 if ( !M_IMGDATA
->m_staticAlpha
)
1939 free( M_IMGDATA
->m_alpha
);
1941 M_IMGDATA
->m_alpha
= NULL
;
1945 // ----------------------------------------------------------------------------
1947 // ----------------------------------------------------------------------------
1949 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1951 wxCHECK_RET( IsOk(), wxT("invalid image") );
1955 M_IMGDATA
->m_maskRed
= r
;
1956 M_IMGDATA
->m_maskGreen
= g
;
1957 M_IMGDATA
->m_maskBlue
= b
;
1958 M_IMGDATA
->m_hasMask
= true;
1961 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1963 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
1965 if (M_IMGDATA
->m_hasMask
)
1967 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1968 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1969 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1974 FindFirstUnusedColour(r
, g
, b
);
1979 unsigned char wxImage::GetMaskRed() const
1981 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1983 return M_IMGDATA
->m_maskRed
;
1986 unsigned char wxImage::GetMaskGreen() const
1988 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1990 return M_IMGDATA
->m_maskGreen
;
1993 unsigned char wxImage::GetMaskBlue() const
1995 wxCHECK_MSG( IsOk(), 0, wxT("invalid image") );
1997 return M_IMGDATA
->m_maskBlue
;
2000 void wxImage::SetMask( bool mask
)
2002 wxCHECK_RET( IsOk(), wxT("invalid image") );
2006 M_IMGDATA
->m_hasMask
= mask
;
2009 bool wxImage::HasMask() const
2011 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2013 return M_IMGDATA
->m_hasMask
;
2016 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
2018 long pos
= XYToIndex(x
, y
);
2019 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
2022 if ( M_IMGDATA
->m_hasMask
)
2024 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
2025 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
2026 p
[1] == M_IMGDATA
->m_maskGreen
&&
2027 p
[2] == M_IMGDATA
->m_maskBlue
)
2034 if ( M_IMGDATA
->m_alpha
)
2036 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
2038 // transparent enough
2047 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
2048 unsigned char mr
, unsigned char mg
, unsigned char mb
)
2050 // check that the images are the same size
2051 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
2053 wxLogError( _("Image and mask have different sizes.") );
2057 // find unused colour
2058 unsigned char r
,g
,b
;
2059 if (!FindFirstUnusedColour(&r
, &g
, &b
))
2061 wxLogError( _("No unused colour in image being masked.") );
2067 unsigned char *imgdata
= GetData();
2068 unsigned char *maskdata
= mask
.GetData();
2070 const int w
= GetWidth();
2071 const int h
= GetHeight();
2073 for (int j
= 0; j
< h
; j
++)
2075 for (int i
= 0; i
< w
; i
++)
2077 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
2088 SetMaskColour(r
, g
, b
);
2094 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
2099 unsigned char mr
, mg
, mb
;
2100 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
2102 wxLogError( _("No unused colour in image being masked.") );
2106 return ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
2109 bool wxImage::ConvertAlphaToMask(unsigned char mr
,
2112 unsigned char threshold
)
2120 SetMaskColour(mr
, mg
, mb
);
2122 unsigned char *imgdata
= GetData();
2123 unsigned char *alphadata
= GetAlpha();
2126 int h
= GetHeight();
2128 for (int y
= 0; y
< h
; y
++)
2130 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
2132 if (*alphadata
< threshold
)
2141 if ( !M_IMGDATA
->m_staticAlpha
)
2142 free(M_IMGDATA
->m_alpha
);
2144 M_IMGDATA
->m_alpha
= NULL
;
2145 M_IMGDATA
->m_staticAlpha
= false;
2150 // ----------------------------------------------------------------------------
2151 // Palette functions
2152 // ----------------------------------------------------------------------------
2156 bool wxImage::HasPalette() const
2161 return M_IMGDATA
->m_palette
.IsOk();
2164 const wxPalette
& wxImage::GetPalette() const
2166 wxCHECK_MSG( IsOk(), wxNullPalette
, wxT("invalid image") );
2168 return M_IMGDATA
->m_palette
;
2171 void wxImage::SetPalette(const wxPalette
& palette
)
2173 wxCHECK_RET( IsOk(), wxT("invalid image") );
2177 M_IMGDATA
->m_palette
= palette
;
2180 #endif // wxUSE_PALETTE
2182 // ----------------------------------------------------------------------------
2183 // Option functions (arbitrary name/value mapping)
2184 // ----------------------------------------------------------------------------
2186 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
2190 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2191 if ( idx
== wxNOT_FOUND
)
2193 M_IMGDATA
->m_optionNames
.Add(name
);
2194 M_IMGDATA
->m_optionValues
.Add(value
);
2198 M_IMGDATA
->m_optionNames
[idx
] = name
;
2199 M_IMGDATA
->m_optionValues
[idx
] = value
;
2203 void wxImage::SetOption(const wxString
& name
, int value
)
2206 valStr
.Printf(wxT("%d"), value
);
2207 SetOption(name
, valStr
);
2210 wxString
wxImage::GetOption(const wxString
& name
) const
2213 return wxEmptyString
;
2215 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2216 if ( idx
== wxNOT_FOUND
)
2217 return wxEmptyString
;
2219 return M_IMGDATA
->m_optionValues
[idx
];
2222 int wxImage::GetOptionInt(const wxString
& name
) const
2224 return wxAtoi(GetOption(name
));
2227 bool wxImage::HasOption(const wxString
& name
) const
2229 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2233 // ----------------------------------------------------------------------------
2235 // ----------------------------------------------------------------------------
2237 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2238 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2239 int WXUNUSED_UNLESS_STREAMS(index
) )
2241 #if HAS_FILE_STREAMS
2242 wxImageFileInputStream
stream(filename
);
2243 if ( stream
.IsOk() )
2245 wxBufferedInputStream
bstream( stream
);
2246 if ( LoadFile(bstream
, type
, index
) )
2250 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2251 #endif // HAS_FILE_STREAMS
2256 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2257 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2258 int WXUNUSED_UNLESS_STREAMS(index
) )
2260 #if HAS_FILE_STREAMS
2261 wxImageFileInputStream
stream(filename
);
2262 if ( stream
.IsOk() )
2264 wxBufferedInputStream
bstream( stream
);
2265 if ( LoadFile(bstream
, mimetype
, index
) )
2269 wxLogError(_("Failed to load image from file \"%s\"."), filename
);
2270 #endif // HAS_FILE_STREAMS
2276 bool wxImage::SaveFile( const wxString
& filename
) const
2278 wxString ext
= filename
.AfterLast('.').Lower();
2280 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2283 wxLogError(_("Can't save image to file '%s': unknown extension."),
2288 return SaveFile(filename
, handler
->GetType());
2291 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2292 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2294 #if HAS_FILE_STREAMS
2295 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2297 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2299 wxImageFileOutputStream
stream(filename
);
2301 if ( stream
.IsOk() )
2303 wxBufferedOutputStream
bstream( stream
);
2304 return SaveFile(bstream
, type
);
2306 #endif // HAS_FILE_STREAMS
2311 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2312 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2314 #if HAS_FILE_STREAMS
2315 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2317 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2319 wxImageFileOutputStream
stream(filename
);
2321 if ( stream
.IsOk() )
2323 wxBufferedOutputStream
bstream( stream
);
2324 return SaveFile(bstream
, mimetype
);
2326 #endif // HAS_FILE_STREAMS
2331 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2333 #if HAS_FILE_STREAMS
2334 wxImageFileInputStream
stream(name
);
2335 return CanRead(stream
);
2341 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2342 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2344 #if HAS_FILE_STREAMS
2345 wxImageFileInputStream
stream(name
);
2347 return GetImageCount(stream
, type
);
2355 bool wxImage::CanRead( wxInputStream
&stream
)
2357 const wxList
& list
= GetHandlers();
2359 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2361 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2362 if (handler
->CanRead( stream
))
2369 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2371 wxImageHandler
*handler
;
2373 if ( type
== wxBITMAP_TYPE_ANY
)
2375 const wxList
& list
= GetHandlers();
2377 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2379 node
= node
->GetNext() )
2381 handler
= (wxImageHandler
*)node
->GetData();
2382 if ( handler
->CanRead(stream
) )
2384 const int count
= handler
->GetImageCount(stream
);
2391 wxLogWarning(_("No handler found for image type."));
2395 handler
= FindHandler(type
);
2399 wxLogWarning(_("No image handler for type %d defined."), type
);
2403 if ( handler
->CanRead(stream
) )
2405 return handler
->GetImageCount(stream
);
2409 wxLogError(_("Image file is not of type %d."), type
);
2414 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2416 // save the options values which can be clobbered by the handler (e.g. many
2417 // of them call Destroy() before trying to load the file)
2418 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2419 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2421 // Preserve the original stream position if possible to rewind back to it
2422 // if we failed to load the file -- maybe the next handler that we try can
2423 // succeed after us then.
2424 wxFileOffset posOld
= wxInvalidOffset
;
2425 if ( stream
.IsSeekable() )
2426 posOld
= stream
.TellI();
2428 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2430 if ( posOld
!= wxInvalidOffset
)
2431 stream
.SeekI(posOld
);
2436 // rescale the image to the specified size if needed
2437 if ( maxWidth
|| maxHeight
)
2439 const unsigned widthOrig
= GetWidth(),
2440 heightOrig
= GetHeight();
2442 // this uses the same (trivial) algorithm as the JPEG handler
2443 unsigned width
= widthOrig
,
2444 height
= heightOrig
;
2445 while ( (maxWidth
&& width
> maxWidth
) ||
2446 (maxHeight
&& height
> maxHeight
) )
2452 if ( width
!= widthOrig
|| height
!= heightOrig
)
2454 // get the original size if it was set by the image handler
2455 // but also in order to restore it after Rescale
2456 int widthOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH
),
2457 heightOrigOption
= GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
2459 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2461 SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH
, widthOrigOption
? widthOrigOption
: widthOrig
);
2462 SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT
, heightOrigOption
? heightOrigOption
: heightOrig
);
2466 // Set this after Rescale, which currently does not preserve it
2467 M_IMGDATA
->m_type
= handler
.GetType();
2472 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2476 wxImageHandler
*handler
;
2478 if ( type
== wxBITMAP_TYPE_ANY
)
2480 if ( !stream
.IsSeekable() )
2482 // The error message about image data format being unknown below
2483 // would be misleading in this case as we are not even going to try
2484 // any handlers because CanRead() never does anything for not
2485 // seekable stream, so try to be more precise here.
2486 wxLogError(_("Can't automatically determine the image format "
2487 "for non-seekable input."));
2491 const wxList
& list
= GetHandlers();
2492 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2494 node
= node
->GetNext() )
2496 handler
= (wxImageHandler
*)node
->GetData();
2497 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2501 wxLogWarning( _("Unknown image data format.") );
2505 //else: have specific type
2507 handler
= FindHandler(type
);
2510 wxLogWarning( _("No image handler for type %d defined."), type
);
2514 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2516 wxLogError(_("This is not a %s."), handler
->GetName());
2520 return DoLoad(*handler
, stream
, index
);
2523 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2527 m_refData
= new wxImageRefData
;
2529 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2533 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2537 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2539 wxLogError(_("Image is not of type %s."), mimetype
);
2543 return DoLoad(*handler
, stream
, index
);
2546 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2548 wxImage
* const self
= const_cast<wxImage
*>(this);
2549 if ( !handler
.SaveFile(self
, stream
) )
2552 M_IMGDATA
->m_type
= handler
.GetType();
2556 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2558 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2560 wxImageHandler
*handler
= FindHandler(type
);
2563 wxLogWarning( _("No image handler for type %d defined."), type
);
2567 return DoSave(*handler
, stream
);
2570 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2572 wxCHECK_MSG( IsOk(), false, wxT("invalid image") );
2574 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2577 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2580 return DoSave(*handler
, stream
);
2583 #endif // wxUSE_STREAMS
2585 // ----------------------------------------------------------------------------
2586 // image I/O handlers
2587 // ----------------------------------------------------------------------------
2589 void wxImage::AddHandler( wxImageHandler
*handler
)
2591 // Check for an existing handler of the type being added.
2592 if (FindHandler( handler
->GetType() ) == 0)
2594 sm_handlers
.Append( handler
);
2598 // This is not documented behaviour, merely the simplest 'fix'
2599 // for preventing duplicate additions. If someone ever has
2600 // a good reason to add and remove duplicate handlers (and they
2601 // may) we should probably refcount the duplicates.
2602 // also an issue in InsertHandler below.
2604 wxLogDebug( wxT("Adding duplicate image handler for '%s'"),
2605 handler
->GetName().c_str() );
2610 void wxImage::InsertHandler( wxImageHandler
*handler
)
2612 // Check for an existing handler of the type being added.
2613 if (FindHandler( handler
->GetType() ) == 0)
2615 sm_handlers
.Insert( handler
);
2619 // see AddHandler for additional comments.
2620 wxLogDebug( wxT("Inserting duplicate image handler for '%s'"),
2621 handler
->GetName().c_str() );
2626 bool wxImage::RemoveHandler( const wxString
& name
)
2628 wxImageHandler
*handler
= FindHandler(name
);
2631 sm_handlers
.DeleteObject(handler
);
2639 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2641 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2644 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2645 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2647 node
= node
->GetNext();
2652 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2654 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2657 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2658 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2660 if (handler
->GetExtension() == extension
)
2662 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2665 node
= node
->GetNext();
2670 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2672 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2675 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2676 if (handler
->GetType() == bitmapType
) return handler
;
2677 node
= node
->GetNext();
2682 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2684 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2687 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2688 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2689 node
= node
->GetNext();
2694 void wxImage::InitStandardHandlers()
2697 AddHandler(new wxBMPHandler
);
2698 #endif // wxUSE_STREAMS
2701 void wxImage::CleanUpHandlers()
2703 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2706 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2707 wxList::compatibility_iterator next
= node
->GetNext();
2712 sm_handlers
.Clear();
2715 wxString
wxImage::GetImageExtWildcard()
2719 wxList
& Handlers
= wxImage::GetHandlers();
2720 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2723 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2724 fmts
+= wxT("*.") + Handler
->GetExtension();
2725 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2726 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2727 Node
= Node
->GetNext();
2728 if ( Node
) fmts
+= wxT(";");
2731 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2734 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2736 const double red
= rgb
.red
/ 255.0,
2737 green
= rgb
.green
/ 255.0,
2738 blue
= rgb
.blue
/ 255.0;
2740 // find the min and max intensity (and remember which one was it for the
2742 double minimumRGB
= red
;
2743 if ( green
< minimumRGB
)
2745 if ( blue
< minimumRGB
)
2748 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2749 double maximumRGB
= red
;
2750 if ( green
> maximumRGB
)
2755 if ( blue
> maximumRGB
)
2761 const double value
= maximumRGB
;
2763 double hue
= 0.0, saturation
;
2764 const double deltaRGB
= maximumRGB
- minimumRGB
;
2765 if ( wxIsNullDouble(deltaRGB
) )
2767 // Gray has no color
2776 hue
= (green
- blue
) / deltaRGB
;
2780 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2784 hue
= 4.0 + (red
- green
) / deltaRGB
;
2788 wxFAIL_MSG(wxT("hue not specified"));
2797 saturation
= deltaRGB
/ maximumRGB
;
2800 return HSVValue(hue
, saturation
, value
);
2803 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2805 double red
, green
, blue
;
2807 if ( wxIsNullDouble(hsv
.saturation
) )
2816 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2817 int i
= (int)floor(hue
);
2818 double f
= hue
- i
; // fractional part of h
2819 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2825 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2830 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2838 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2843 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2848 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2856 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2861 return RGBValue((unsigned char)(red
* 255.0),
2862 (unsigned char)(green
* 255.0),
2863 (unsigned char)(blue
* 255.0));
2867 * Rotates the hue of each pixel of the image. angle is a double in the range
2868 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2870 void wxImage::RotateHue(double angle
)
2874 unsigned char *srcBytePtr
;
2875 unsigned char *dstBytePtr
;
2876 unsigned long count
;
2877 wxImage::HSVValue hsv
;
2878 wxImage::RGBValue rgb
;
2880 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2881 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2882 if ( count
> 0 && !wxIsNullDouble(angle
) )
2884 srcBytePtr
= M_IMGDATA
->m_data
;
2885 dstBytePtr
= srcBytePtr
;
2888 rgb
.red
= *srcBytePtr
++;
2889 rgb
.green
= *srcBytePtr
++;
2890 rgb
.blue
= *srcBytePtr
++;
2891 hsv
= RGBtoHSV(rgb
);
2893 hsv
.hue
= hsv
.hue
+ angle
;
2895 hsv
.hue
= hsv
.hue
- 1.0;
2896 else if (hsv
.hue
< 0.0)
2897 hsv
.hue
= hsv
.hue
+ 1.0;
2899 rgb
= HSVtoRGB(hsv
);
2900 *dstBytePtr
++ = rgb
.red
;
2901 *dstBytePtr
++ = rgb
.green
;
2902 *dstBytePtr
++ = rgb
.blue
;
2903 } while (--count
!= 0);
2907 //-----------------------------------------------------------------------------
2909 //-----------------------------------------------------------------------------
2911 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2914 int wxImageHandler::GetImageCount( wxInputStream
& stream
)
2916 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2917 // wxImageHandler::CallDoCanRead
2919 if ( !stream
.IsSeekable() )
2920 return false; // can't test unseekable stream
2922 wxFileOffset posOld
= stream
.TellI();
2923 int n
= DoGetImageCount(stream
);
2925 // restore the old position to be able to test other formats and so on
2926 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2928 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2930 // reading would fail anyhow as we're not at the right position
2937 bool wxImageHandler::CanRead( const wxString
& name
)
2939 wxImageFileInputStream
stream(name
);
2940 if ( !stream
.IsOk() )
2942 wxLogError(_("Failed to check format of image file \"%s\"."), name
);
2947 return CanRead(stream
);
2950 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2952 // NOTE: this code is the same of wxAnimationDecoder::CanRead and
2953 // wxImageHandler::GetImageCount
2955 if ( !stream
.IsSeekable() )
2956 return false; // can't test unseekable stream
2958 wxFileOffset posOld
= stream
.TellI();
2959 bool ok
= DoCanRead(stream
);
2961 // restore the old position to be able to test other formats and so on
2962 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2964 wxLogDebug(wxT("Failed to rewind the stream in wxImageHandler!"));
2966 // reading would fail anyhow as we're not at the right position
2973 #endif // wxUSE_STREAMS
2977 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2979 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, wxT("NULL pointer") );
2981 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2982 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2984 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2985 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2987 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2990 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2992 else // no resolution options specified
2997 return wxIMAGE_RESOLUTION_NONE
;
3000 // get the resolution unit too
3001 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
3004 // this is the default
3005 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
3008 return (wxImageResolution
)resUnit
;
3011 // ----------------------------------------------------------------------------
3012 // image histogram stuff
3013 // ----------------------------------------------------------------------------
3016 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
3021 unsigned char g2
) const
3023 unsigned long key
= MakeKey(r2
, g2
, b2
);
3025 while ( find(key
) != end() )
3027 // color already used
3039 wxLogError(_("No unused colour in image.") );
3045 key
= MakeKey(r2
, g2
, b2
);
3059 wxImage::FindFirstUnusedColour(unsigned char *r
,
3064 unsigned char g2
) const
3066 wxImageHistogram histogram
;
3068 ComputeHistogram(histogram
);
3070 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
3076 // Counts and returns the number of different colours. Optionally stops
3077 // when it exceeds 'stopafter' different colours. This is useful, for
3078 // example, to see if the image can be saved as 8-bit (256 colour or
3079 // less, in this case it would be invoked as CountColours(256)). Default
3080 // value for stopafter is -1 (don't care).
3082 unsigned long wxImage::CountColours( unsigned long stopafter
) const
3086 unsigned char r
, g
, b
;
3088 unsigned long size
, nentries
, key
;
3091 size
= GetWidth() * GetHeight();
3094 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
3099 key
= wxImageHistogram::MakeKey(r
, g
, b
);
3101 if (h
.Get(key
) == NULL
)
3112 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
3114 unsigned char *p
= GetData();
3115 unsigned long nentries
= 0;
3119 const unsigned long size
= GetWidth() * GetHeight();
3121 unsigned char r
, g
, b
;
3122 for ( unsigned long n
= 0; n
< size
; n
++ )
3128 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
3130 if ( entry
.value
++ == 0 )
3131 entry
.index
= nentries
++;
3138 * Rotation code by Carlos Moreno
3141 static const double wxROTATE_EPSILON
= 1e-10;
3143 // Auxiliary function to rotate a point (x,y) with respect to point p0
3144 // make it inline and use a straight return to facilitate optimization
3145 // also, the function receives the sine and cosine of the angle to avoid
3146 // repeating the time-consuming calls to these functions -- sin/cos can
3147 // be computed and stored in the calling function.
3149 static inline wxRealPoint
3150 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
3151 const wxRealPoint
& p0
)
3153 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
3154 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
3157 static inline wxRealPoint
3158 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
3159 const wxRealPoint
& p0
)
3161 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
3164 wxImage
wxImage::Rotate(double angle
,
3165 const wxPoint
& centre_of_rotation
,
3167 wxPoint
*offset_after_rotation
) const
3169 // screen coordinates are a mirror image of "real" coordinates
3172 const bool has_alpha
= HasAlpha();
3174 const int w
= GetWidth();
3175 const int h
= GetHeight();
3179 // Create pointer-based array to accelerate access to wxImage's data
3180 unsigned char ** data
= new unsigned char * [h
];
3181 data
[0] = GetData();
3182 for (i
= 1; i
< h
; i
++)
3183 data
[i
] = data
[i
- 1] + (3 * w
);
3185 // Same for alpha channel
3186 unsigned char ** alpha
= NULL
;
3189 alpha
= new unsigned char * [h
];
3190 alpha
[0] = GetAlpha();
3191 for (i
= 1; i
< h
; i
++)
3192 alpha
[i
] = alpha
[i
- 1] + w
;
3195 // precompute coefficients for rotation formula
3196 const double cos_angle
= cos(angle
);
3197 const double sin_angle
= sin(angle
);
3199 // Create new Image to store the result
3200 // First, find rectangle that covers the rotated image; to do that,
3201 // rotate the four corners
3203 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
3205 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
3206 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
3207 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
3208 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
3210 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
3211 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
3212 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
3213 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
3215 // Create rotated image
3216 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
3217 // With alpha channel
3221 if (offset_after_rotation
!= NULL
)
3223 *offset_after_rotation
= wxPoint (x1a
, y1a
);
3226 // the rotated (destination) image is always accessed sequentially via this
3227 // pointer, there is no need for pointer-based arrays here
3228 unsigned char *dst
= rotated
.GetData();
3230 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
3232 // if the original image has a mask, use its RGB values as the blank pixel,
3233 // else, fall back to default (black).
3234 unsigned char blank_r
= 0;
3235 unsigned char blank_g
= 0;
3236 unsigned char blank_b
= 0;
3240 blank_r
= GetMaskRed();
3241 blank_g
= GetMaskGreen();
3242 blank_b
= GetMaskBlue();
3243 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3246 // Now, for each point of the rotated image, find where it came from, by
3247 // performing an inverse rotation (a rotation of -angle) and getting the
3248 // pixel at those coordinates
3250 const int rH
= rotated
.GetHeight();
3251 const int rW
= rotated
.GetWidth();
3253 // do the (interpolating) test outside of the loops, so that it is done
3254 // only once, instead of repeating it for each pixel.
3257 for (int y
= 0; y
< rH
; y
++)
3259 for (int x
= 0; x
< rW
; x
++)
3261 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3263 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3264 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3266 // interpolate using the 4 enclosing grid-points. Those
3267 // points can be obtained using floor and ceiling of the
3268 // exact coordinates of the point
3271 if (0 < src
.x
&& src
.x
< w
- 1)
3273 x1
= wxRound(floor(src
.x
));
3274 x2
= wxRound(ceil(src
.x
));
3276 else // else means that x is near one of the borders (0 or width-1)
3278 x1
= x2
= wxRound (src
.x
);
3281 if (0 < src
.y
&& src
.y
< h
- 1)
3283 y1
= wxRound(floor(src
.y
));
3284 y2
= wxRound(ceil(src
.y
));
3288 y1
= y2
= wxRound (src
.y
);
3291 // get four points and the distances (square of the distance,
3292 // for efficiency reasons) for the interpolation formula
3294 // GRG: Do not calculate the points until they are
3295 // really needed -- this way we can calculate
3296 // just one, instead of four, if d1, d2, d3
3297 // or d4 are < wxROTATE_EPSILON
3299 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3300 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3301 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3302 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3304 // Now interpolate as a weighted average of the four surrounding
3305 // points, where the weights are the distances to each of those points
3307 // If the point is exactly at one point of the grid of the source
3308 // image, then don't interpolate -- just assign the pixel
3310 // d1,d2,d3,d4 are positive -- no need for abs()
3311 if (d1
< wxROTATE_EPSILON
)
3313 unsigned char *p
= data
[y1
] + (3 * x1
);
3319 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3321 else if (d2
< wxROTATE_EPSILON
)
3323 unsigned char *p
= data
[y1
] + (3 * x2
);
3329 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3331 else if (d3
< wxROTATE_EPSILON
)
3333 unsigned char *p
= data
[y2
] + (3 * x2
);
3339 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3341 else if (d4
< wxROTATE_EPSILON
)
3343 unsigned char *p
= data
[y2
] + (3 * x1
);
3349 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3353 // weights for the weighted average are proportional to the inverse of the distance
3354 unsigned char *v1
= data
[y1
] + (3 * x1
);
3355 unsigned char *v2
= data
[y1
] + (3 * x2
);
3356 unsigned char *v3
= data
[y2
] + (3 * x2
);
3357 unsigned char *v4
= data
[y2
] + (3 * x1
);
3359 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3363 *(dst
++) = (unsigned char)
3364 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3365 w3
* *(v3
++) + w4
* *(v4
++)) /
3366 (w1
+ w2
+ w3
+ w4
) );
3367 *(dst
++) = (unsigned char)
3368 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3369 w3
* *(v3
++) + w4
* *(v4
++)) /
3370 (w1
+ w2
+ w3
+ w4
) );
3371 *(dst
++) = (unsigned char)
3372 ( (w1
* *v1
+ w2
* *v2
+
3373 w3
* *v3
+ w4
* *v4
) /
3374 (w1
+ w2
+ w3
+ w4
) );
3378 v1
= alpha
[y1
] + (x1
);
3379 v2
= alpha
[y1
] + (x2
);
3380 v3
= alpha
[y2
] + (x2
);
3381 v4
= alpha
[y2
] + (x1
);
3383 *(alpha_dst
++) = (unsigned char)
3384 ( (w1
* *v1
+ w2
* *v2
+
3385 w3
* *v3
+ w4
* *v4
) /
3386 (w1
+ w2
+ w3
+ w4
) );
3402 else // not interpolating
3404 for (int y
= 0; y
< rH
; y
++)
3406 for (int x
= 0; x
< rW
; x
++)
3408 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3410 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3411 const int ys
= wxRound (src
.y
); // closest integer
3413 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3415 unsigned char *p
= data
[ys
] + (3 * xs
);
3421 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3430 *(alpha_dst
++) = 255;
3446 // A module to allow wxImage initialization/cleanup
3447 // without calling these functions from app.cpp or from
3448 // the user's application.
3450 class wxImageModule
: public wxModule
3452 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3455 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3456 void OnExit() { wxImage::CleanUpHandlers(); }
3459 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3462 #endif // wxUSE_IMAGE