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"
31 #include "wx/filefn.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 class wxImageRefData
: public wxObjectRefData
63 virtual ~wxImageRefData();
67 unsigned char *m_data
;
70 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
72 // alpha channel data, may be NULL for the formats without alpha support
73 unsigned char *m_alpha
;
77 // if true, m_data is pointer to static data and shouldn't be freed
80 // same as m_static but for m_alpha
85 #endif // wxUSE_PALETTE
87 wxArrayString m_optionNames
;
88 wxArrayString m_optionValues
;
90 DECLARE_NO_COPY_CLASS(wxImageRefData
)
93 wxImageRefData::wxImageRefData()
98 m_alpha
= (unsigned char *) NULL
;
107 m_staticAlpha
= false;
110 wxImageRefData::~wxImageRefData()
114 if ( !m_staticAlpha
)
118 wxList
wxImage::sm_handlers
;
122 //-----------------------------------------------------------------------------
124 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
126 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
128 wxImage::wxImage( int width
, int height
, bool clear
)
130 Create( width
, height
, clear
);
133 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
135 Create( width
, height
, data
, static_data
);
138 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
140 Create( width
, height
, data
, alpha
, static_data
);
143 wxImage::wxImage( const wxString
& name
, long type
, int index
)
145 LoadFile( name
, type
, index
);
148 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
150 LoadFile( name
, mimetype
, index
);
154 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
156 LoadFile( stream
, type
, index
);
159 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
161 LoadFile( stream
, mimetype
, index
);
163 #endif // wxUSE_STREAMS
165 wxImage::wxImage(const char* const* xpmData
)
170 bool wxImage::Create(const char* const* xpmData
)
175 wxXPMDecoder decoder
;
176 (*this) = decoder
.ReadData(xpmData
);
183 bool wxImage::Create( int width
, int height
, bool clear
)
187 m_refData
= new wxImageRefData();
189 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
190 if (!M_IMGDATA
->m_data
)
197 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
199 M_IMGDATA
->m_width
= width
;
200 M_IMGDATA
->m_height
= height
;
201 M_IMGDATA
->m_ok
= true;
206 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
210 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
212 m_refData
= new wxImageRefData();
214 M_IMGDATA
->m_data
= data
;
215 M_IMGDATA
->m_width
= width
;
216 M_IMGDATA
->m_height
= height
;
217 M_IMGDATA
->m_ok
= true;
218 M_IMGDATA
->m_static
= static_data
;
223 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
227 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
229 m_refData
= new wxImageRefData();
231 M_IMGDATA
->m_data
= data
;
232 M_IMGDATA
->m_alpha
= alpha
;
233 M_IMGDATA
->m_width
= width
;
234 M_IMGDATA
->m_height
= height
;
235 M_IMGDATA
->m_ok
= true;
236 M_IMGDATA
->m_static
= static_data
;
241 void wxImage::Destroy()
246 wxObjectRefData
* wxImage::CreateRefData() const
248 return new wxImageRefData
;
251 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
253 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
254 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
256 wxImageRefData
* refData_new
= new wxImageRefData
;
257 refData_new
->m_width
= refData
->m_width
;
258 refData_new
->m_height
= refData
->m_height
;
259 refData_new
->m_maskRed
= refData
->m_maskRed
;
260 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
261 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
262 refData_new
->m_hasMask
= refData
->m_hasMask
;
263 refData_new
->m_ok
= true;
264 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
265 if (refData
->m_alpha
!= NULL
)
267 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
268 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
271 refData_new
->m_data
= (unsigned char*)malloc(size
);
272 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
274 refData_new
->m_palette
= refData
->m_palette
;
276 refData_new
->m_optionNames
= refData
->m_optionNames
;
277 refData_new
->m_optionValues
= refData
->m_optionValues
;
281 wxImage
wxImage::Copy() const
285 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
287 image
.m_refData
= CloneRefData(m_refData
);
292 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
294 if( xFactor
== 1 && yFactor
== 1 )
299 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
301 // can't scale to/from 0 size
302 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
303 wxT("invalid new image size") );
305 long old_height
= M_IMGDATA
->m_height
,
306 old_width
= M_IMGDATA
->m_width
;
308 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
309 wxT("invalid old image size") );
311 long width
= old_width
/ xFactor
;
312 long height
= old_height
/ yFactor
;
314 image
.Create( width
, height
, false );
316 char unsigned *data
= image
.GetData();
318 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
320 bool hasMask
= false ;
321 unsigned char maskRed
= 0;
322 unsigned char maskGreen
= 0;
323 unsigned char maskBlue
=0 ;
325 unsigned char *source_data
= M_IMGDATA
->m_data
;
326 unsigned char *target_data
= data
;
327 unsigned char *source_alpha
= 0 ;
328 unsigned char *target_alpha
= 0 ;
329 if (M_IMGDATA
->m_hasMask
)
332 maskRed
= M_IMGDATA
->m_maskRed
;
333 maskGreen
= M_IMGDATA
->m_maskGreen
;
334 maskBlue
=M_IMGDATA
->m_maskBlue
;
336 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
337 M_IMGDATA
->m_maskGreen
,
338 M_IMGDATA
->m_maskBlue
);
342 source_alpha
= M_IMGDATA
->m_alpha
;
346 target_alpha
= image
.GetAlpha() ;
350 for (long y
= 0; y
< height
; y
++)
352 for (long x
= 0; x
< width
; x
++)
354 unsigned long avgRed
= 0 ;
355 unsigned long avgGreen
= 0;
356 unsigned long avgBlue
= 0;
357 unsigned long avgAlpha
= 0 ;
358 unsigned long counter
= 0 ;
360 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
362 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
363 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
365 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
366 unsigned char red
= pixel
[0] ;
367 unsigned char green
= pixel
[1] ;
368 unsigned char blue
= pixel
[2] ;
369 unsigned char alpha
= 255 ;
371 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
372 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
387 *(target_data
++) = M_IMGDATA
->m_maskRed
;
388 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
389 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
394 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
395 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
396 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
397 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
402 // In case this is a cursor, make sure the hotspot is scaled accordingly:
403 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
404 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
405 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
406 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
407 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
408 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
413 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
417 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
419 // can't scale to/from 0 size
420 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
421 wxT("invalid new image size") );
423 long old_height
= M_IMGDATA
->m_height
,
424 old_width
= M_IMGDATA
->m_width
;
425 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
426 wxT("invalid old image size") );
428 // If the image's new width and height are the same as the original, no
429 // need to waste time or CPU cycles
430 if ( old_width
== width
&& old_height
== height
)
433 // Scale the image (...or more appropriately, resample the image) using
434 // either the high-quality or normal method as specified
435 if ( quality
== wxIMAGE_QUALITY_HIGH
)
437 // We need to check whether we are downsampling or upsampling the image
438 if ( width
< old_width
&& height
< old_height
)
440 // Downsample the image using the box averaging method for best results
441 image
= ResampleBox(width
, height
);
445 // For upsampling or other random/wierd image dimensions we'll use
446 // a bicubic b-spline scaling method
447 image
= ResampleBicubic(width
, height
);
450 else // Default scaling method == simple pixel replication
452 if ( old_width
% width
== 0 && old_width
>= width
&&
453 old_height
% height
== 0 && old_height
>= height
)
455 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
457 image
.Create( width
, height
, false );
459 unsigned char *data
= image
.GetData();
461 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
463 unsigned char *source_data
= M_IMGDATA
->m_data
;
464 unsigned char *target_data
= data
;
465 unsigned char *source_alpha
= 0 ;
466 unsigned char *target_alpha
= 0 ;
468 if ( !M_IMGDATA
->m_hasMask
)
470 source_alpha
= M_IMGDATA
->m_alpha
;
474 target_alpha
= image
.GetAlpha() ;
478 long x_delta
= (old_width
<<16) / width
;
479 long y_delta
= (old_height
<<16) / height
;
481 unsigned char* dest_pixel
= target_data
;
484 for ( long j
= 0; j
< height
; j
++ )
486 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
487 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
490 for ( long i
= 0; i
< width
; i
++ )
492 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
493 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
494 dest_pixel
[0] = src_pixel
[0];
495 dest_pixel
[1] = src_pixel
[1];
496 dest_pixel
[2] = src_pixel
[2];
499 *(target_alpha
++) = *src_alpha_pixel
;
507 // If the original image has a mask, apply the mask to the new image
508 if (M_IMGDATA
->m_hasMask
)
510 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
511 M_IMGDATA
->m_maskGreen
,
512 M_IMGDATA
->m_maskBlue
);
515 // In case this is a cursor, make sure the hotspot is scaled accordingly:
516 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
517 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
518 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
519 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
520 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
521 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
526 wxImage
wxImage::ResampleBox(int width
, int height
) const
528 // This function implements a simple pre-blur/box averaging method for
529 // downsampling that gives reasonably smooth results To scale the image
530 // down we will need to gather a grid of pixels of the size of the scale
531 // factor in each direction and then do an averaging of the pixels.
533 wxImage
ret_image(width
, height
, false);
535 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
536 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
538 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
539 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
541 unsigned char* src_data
= M_IMGDATA
->m_data
;
542 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
543 unsigned char* dst_data
= ret_image
.GetData();
544 unsigned char* dst_alpha
= NULL
;
548 ret_image
.SetAlpha();
549 dst_alpha
= ret_image
.GetAlpha();
552 int averaged_pixels
, src_pixel_index
;
553 double sum_r
, sum_g
, sum_b
, sum_a
;
555 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
557 // Source pixel in the Y direction
558 int src_y
= (int)(y
* scale_factor_y
);
560 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
562 // Source pixel in the X direction
563 int src_x
= (int)(x
* scale_factor_x
);
565 // Box of pixels to average
567 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
569 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
570 j
<= int(src_y
+ scale_factor_y_2
);
573 // We don't care to average pixels that don't exist (edges)
574 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
577 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
578 i
<= src_x
+ scale_factor_x_2
;
581 // Don't average edge pixels
582 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
585 // Calculate the actual index in our source pixels
586 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
588 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
589 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
590 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
592 sum_a
+= src_alpha
[src_pixel_index
];
598 // Calculate the average from the sum and number of averaged pixels
599 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
600 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
601 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
604 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
611 // The following two local functions are for the B-spline weighting of the
612 // bicubic sampling algorithm
613 static inline double spline_cube(double value
)
615 return value
<= 0.0 ? 0.0 : value
* value
* value
;
618 static inline double spline_weight(double value
)
620 return (spline_cube(value
+ 2) -
621 4 * spline_cube(value
+ 1) +
622 6 * spline_cube(value
) -
623 4 * spline_cube(value
- 1)) / 6;
626 // This is the bicubic resampling algorithm
627 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
629 // This function implements a Bicubic B-Spline algorithm for resampling.
630 // This method is certainly a little slower than wxImage's default pixel
631 // replication method, however for most reasonably sized images not being
632 // upsampled too much on a fairly average CPU this difference is hardly
633 // noticeable and the results are far more pleasing to look at.
635 // This particular bicubic algorithm does pixel weighting according to a
636 // B-Spline that basically implements a Gaussian bell-like weighting
637 // kernel. Because of this method the results may appear a bit blurry when
638 // upsampling by large factors. This is basically because a slight
639 // gaussian blur is being performed to get the smooth look of the upsampled
642 // Edge pixels: 3-4 possible solutions
643 // - (Wrap/tile) Wrap the image, take the color value from the opposite
644 // side of the image.
645 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
646 // where n is nonpositive, will have the value of (2, 1).
647 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
648 // pixels which do have all neighbours.
649 // - (Clamp) Choose the nearest pixel along the border. This takes the
650 // border pixels and extends them out to infinity.
652 // NOTE: below the y_offset and x_offset variables are being set for edge
653 // pixels using the "Mirror" method mentioned above
657 ret_image
.Create(width
, height
, false);
659 unsigned char* src_data
= M_IMGDATA
->m_data
;
660 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
661 unsigned char* dst_data
= ret_image
.GetData();
662 unsigned char* dst_alpha
= NULL
;
666 ret_image
.SetAlpha();
667 dst_alpha
= ret_image
.GetAlpha();
670 for ( int dsty
= 0; dsty
< height
; dsty
++ )
672 // We need to calculate the source pixel to interpolate from - Y-axis
673 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
674 double dy
= srcpixy
- (int)srcpixy
;
676 for ( int dstx
= 0; dstx
< width
; dstx
++ )
678 // X-axis of pixel to interpolate from
679 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
680 double dx
= srcpixx
- (int)srcpixx
;
682 // Sums for each color channel
683 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
685 // Here we actually determine the RGBA values for the destination pixel
686 for ( int k
= -1; k
<= 2; k
++ )
689 int y_offset
= srcpixy
+ k
< 0.0
691 : srcpixy
+ k
>= M_IMGDATA
->m_height
692 ? M_IMGDATA
->m_height
- 1
693 : (int)(srcpixy
+ k
);
695 // Loop across the X axis
696 for ( int i
= -1; i
<= 2; i
++ )
699 int x_offset
= srcpixx
+ i
< 0.0
701 : srcpixx
+ i
>= M_IMGDATA
->m_width
702 ? M_IMGDATA
->m_width
- 1
703 : (int)(srcpixx
+ i
);
705 // Calculate the exact position where the source data
706 // should be pulled from based on the x_offset and y_offset
707 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
709 // Calculate the weight for the specified pixel according
710 // to the bicubic b-spline kernel we're using for
713 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
715 // Create a sum of all velues for each color channel
716 // adjusted for the pixel's calculated weight
717 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
718 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
719 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
721 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
725 // Put the data into the destination image. The summed values are
726 // of double data type and are rounded here for accuracy
727 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
728 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
729 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
733 *dst_alpha
++ = (unsigned char)sum_a
;
740 // Blur in the horizontal direction
741 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
744 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
746 unsigned char* src_data
= M_IMGDATA
->m_data
;
747 unsigned char* dst_data
= ret_image
.GetData();
748 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
749 unsigned char* dst_alpha
= NULL
;
751 // Check for a mask or alpha
754 ret_image
.SetAlpha();
755 dst_alpha
= ret_image
.GetAlpha();
757 else if ( M_IMGDATA
->m_hasMask
)
759 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
760 M_IMGDATA
->m_maskGreen
,
761 M_IMGDATA
->m_maskBlue
);
764 // number of pixels we average over
765 const int blurArea
= blurRadius
*2 + 1;
767 // Horizontal blurring algorithm - average all pixels in the specified blur
768 // radius in the X or horizontal direction
769 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
771 // Variables used in the blurring algorithm
778 const unsigned char *src
;
781 // Calculate the average of all pixels in the blur radius for the first
783 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
785 // To deal with the pixels at the start of a row so it's not
786 // grabbing GOK values from memory at negative indices of the
787 // image's data or grabbing from the previous row
789 pixel_idx
= y
* M_IMGDATA
->m_width
;
791 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
793 src
= src_data
+ pixel_idx
*3;
798 sum_a
+= src_alpha
[pixel_idx
];
801 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
802 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
803 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
804 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
806 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
808 // Now average the values of the rest of the pixels by just moving the
809 // blur radius box along the row
810 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
812 // Take care of edge pixels on the left edge by essentially
813 // duplicating the edge pixel
814 if ( x
- blurRadius
- 1 < 0 )
815 pixel_idx
= y
* M_IMGDATA
->m_width
;
817 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
819 // Subtract the value of the pixel at the left side of the blur
821 src
= src_data
+ pixel_idx
*3;
826 sum_a
-= src_alpha
[pixel_idx
];
828 // Take care of edge pixels on the right edge
829 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
830 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
832 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
834 // Add the value of the pixel being added to the end of our box
835 src
= src_data
+ pixel_idx
*3;
840 sum_a
+= src_alpha
[pixel_idx
];
842 // Save off the averaged data
843 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
844 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
845 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
846 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
848 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
855 // Blur in the vertical direction
856 wxImage
wxImage::BlurVertical(int blurRadius
) const
859 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
861 unsigned char* src_data
= M_IMGDATA
->m_data
;
862 unsigned char* dst_data
= ret_image
.GetData();
863 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
864 unsigned char* dst_alpha
= NULL
;
866 // Check for a mask or alpha
869 ret_image
.SetAlpha();
870 dst_alpha
= ret_image
.GetAlpha();
872 else if ( M_IMGDATA
->m_hasMask
)
874 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
875 M_IMGDATA
->m_maskGreen
,
876 M_IMGDATA
->m_maskBlue
);
879 // number of pixels we average over
880 const int blurArea
= blurRadius
*2 + 1;
882 // Vertical blurring algorithm - same as horizontal but switched the
883 // opposite direction
884 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
886 // Variables used in the blurring algorithm
893 const unsigned char *src
;
896 // Calculate the average of all pixels in our blur radius box for the
897 // first pixel of the column
898 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
900 // To deal with the pixels at the start of a column so it's not
901 // grabbing GOK values from memory at negative indices of the
902 // image's data or grabbing from the previous column
906 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
908 src
= src_data
+ pixel_idx
*3;
913 sum_a
+= src_alpha
[pixel_idx
];
916 dst
= dst_data
+ x
*3;
917 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
918 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
919 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
921 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
923 // Now average the values of the rest of the pixels by just moving the
924 // box along the column from top to bottom
925 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
927 // Take care of pixels that would be beyond the top edge by
928 // duplicating the top edge pixel for the column
929 if ( y
- blurRadius
- 1 < 0 )
932 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
934 // Subtract the value of the pixel at the top of our blur radius box
935 src
= src_data
+ pixel_idx
*3;
940 sum_a
-= src_alpha
[pixel_idx
];
942 // Take care of the pixels that would be beyond the bottom edge of
943 // the image similar to the top edge
944 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
945 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
947 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
949 // Add the value of the pixel being added to the end of our box
950 src
= src_data
+ pixel_idx
*3;
955 sum_a
+= src_alpha
[pixel_idx
];
957 // Save off the averaged data
958 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
959 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
960 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
961 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
963 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
970 // The new blur function
971 wxImage
wxImage::Blur(int blurRadius
) const
974 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
976 // Blur the image in each direction
977 ret_image
= BlurHorizontal(blurRadius
);
978 ret_image
= ret_image
.BlurVertical(blurRadius
);
983 wxImage
wxImage::Rotate90( bool clockwise
) const
987 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
989 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
991 unsigned char *data
= image
.GetData();
993 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
995 unsigned char *source_data
= M_IMGDATA
->m_data
;
996 unsigned char *target_data
;
997 unsigned char *alpha_data
= 0 ;
998 unsigned char *source_alpha
= 0 ;
999 unsigned char *target_alpha
= 0 ;
1001 if (M_IMGDATA
->m_hasMask
)
1003 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1007 source_alpha
= M_IMGDATA
->m_alpha
;
1011 alpha_data
= image
.GetAlpha() ;
1015 long height
= M_IMGDATA
->m_height
;
1016 long width
= M_IMGDATA
->m_width
;
1018 for (long j
= 0; j
< height
; j
++)
1020 for (long i
= 0; i
< width
; i
++)
1024 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1026 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1030 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1032 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1034 memcpy( target_data
, source_data
, 3 );
1039 memcpy( target_alpha
, source_alpha
, 1 );
1048 wxImage
wxImage::Mirror( bool horizontally
) const
1052 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1054 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1056 unsigned char *data
= image
.GetData();
1057 unsigned char *alpha
= NULL
;
1059 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1061 if (M_IMGDATA
->m_alpha
!= NULL
) {
1063 alpha
= image
.GetAlpha();
1064 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1067 if (M_IMGDATA
->m_hasMask
)
1068 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1070 long height
= M_IMGDATA
->m_height
;
1071 long width
= M_IMGDATA
->m_width
;
1073 unsigned char *source_data
= M_IMGDATA
->m_data
;
1074 unsigned char *target_data
;
1078 for (long j
= 0; j
< height
; j
++)
1081 target_data
= data
-3;
1082 for (long i
= 0; i
< width
; i
++)
1084 memcpy( target_data
, source_data
, 3 );
1092 // src_alpha starts at the first pixel and increases by 1 after each step
1093 // (a step here is the copy of the alpha value of one pixel)
1094 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1095 // dest_alpha starts just beyond the first line, decreases before each step,
1096 // and after each line is finished, increases by 2 widths (skipping the line
1097 // just copied and the line that will be copied next)
1098 unsigned char *dest_alpha
= alpha
+ width
;
1100 for (long jj
= 0; jj
< height
; ++jj
)
1102 for (long i
= 0; i
< width
; ++i
) {
1103 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1105 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1111 for (long i
= 0; i
< height
; i
++)
1113 target_data
= data
+ 3*width
*(height
-1-i
);
1114 memcpy( target_data
, source_data
, (size_t)3*width
);
1115 source_data
+= 3*width
;
1120 // src_alpha starts at the first pixel and increases by 1 width after each step
1121 // (a step here is the copy of the alpha channel of an entire line)
1122 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1123 // dest_alpha starts just beyond the last line (beyond the whole image)
1124 // and decreases by 1 width before each step
1125 unsigned char *dest_alpha
= alpha
+ width
* height
;
1127 for (long jj
= 0; jj
< height
; ++jj
)
1129 dest_alpha
-= width
;
1130 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1139 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1143 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1145 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1146 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1147 image
, wxT("invalid subimage size") );
1149 const int subwidth
= rect
.GetWidth();
1150 const int subheight
= rect
.GetHeight();
1152 image
.Create( subwidth
, subheight
, false );
1154 const unsigned char *src_data
= GetData();
1155 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1156 unsigned char *subdata
= image
.GetData();
1157 unsigned char *subalpha
= NULL
;
1159 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1161 if (src_alpha
!= NULL
) {
1163 subalpha
= image
.GetAlpha();
1164 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1167 if (M_IMGDATA
->m_hasMask
)
1168 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1170 const int width
= GetWidth();
1171 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1173 src_data
+= 3 * pixsoff
;
1174 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1176 for (long j
= 0; j
< subheight
; ++j
)
1178 memcpy( subdata
, src_data
, 3 * subwidth
);
1179 subdata
+= 3 * subwidth
;
1180 src_data
+= 3 * width
;
1181 if (subalpha
!= NULL
) {
1182 memcpy( subalpha
, src_alpha
, subwidth
);
1183 subalpha
+= subwidth
;
1191 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1192 int r_
, int g_
, int b_
) const
1196 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1197 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1199 int width
= GetWidth(), height
= GetHeight();
1200 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1202 unsigned char r
= (unsigned char)r_
;
1203 unsigned char g
= (unsigned char)g_
;
1204 unsigned char b
= (unsigned char)b_
;
1205 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1207 GetOrFindMaskColour( &r
, &g
, &b
);
1208 image
.SetMaskColour(r
, g
, b
);
1211 image
.SetRGB(wxRect(), r
, g
, b
);
1213 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1214 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1216 finalRect
.width
-= pos
.x
;
1218 finalRect
.height
-= pos
.y
;
1220 subRect
.Intersect(finalRect
);
1222 if (!subRect
.IsEmpty())
1224 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1225 image
.Paste(*this, pos
.x
, pos
.y
);
1227 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1233 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1235 wxCHECK_RET( Ok(), wxT("invalid image") );
1236 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1242 int width
= image
.GetWidth();
1243 int height
= image
.GetHeight();
1256 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1257 width
= M_IMGDATA
->m_width
- (x
+xx
);
1258 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1259 height
= M_IMGDATA
->m_height
- (y
+yy
);
1261 if (width
< 1) return;
1262 if (height
< 1) return;
1264 if ((!HasMask() && !image
.HasMask()) ||
1265 (HasMask() && !image
.HasMask()) ||
1266 ((HasMask() && image
.HasMask() &&
1267 (GetMaskRed()==image
.GetMaskRed()) &&
1268 (GetMaskGreen()==image
.GetMaskGreen()) &&
1269 (GetMaskBlue()==image
.GetMaskBlue()))))
1272 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1273 int source_step
= image
.GetWidth()*3;
1275 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1276 int target_step
= M_IMGDATA
->m_width
*3;
1277 for (int j
= 0; j
< height
; j
++)
1279 memcpy( target_data
, source_data
, width
);
1280 source_data
+= source_step
;
1281 target_data
+= target_step
;
1286 if (!HasMask() && image
.HasMask())
1288 unsigned char r
= image
.GetMaskRed();
1289 unsigned char g
= image
.GetMaskGreen();
1290 unsigned char b
= image
.GetMaskBlue();
1293 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1294 int source_step
= image
.GetWidth()*3;
1296 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1297 int target_step
= M_IMGDATA
->m_width
*3;
1299 for (int j
= 0; j
< height
; j
++)
1301 for (int i
= 0; i
< width
; i
+=3)
1303 if ((source_data
[i
] != r
) ||
1304 (source_data
[i
+1] != g
) ||
1305 (source_data
[i
+2] != b
))
1307 memcpy( target_data
+i
, source_data
+i
, 3 );
1310 source_data
+= source_step
;
1311 target_data
+= target_step
;
1316 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1317 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1319 wxCHECK_RET( Ok(), wxT("invalid image") );
1323 unsigned char *data
= GetData();
1325 const int w
= GetWidth();
1326 const int h
= GetHeight();
1328 for (int j
= 0; j
< h
; j
++)
1329 for (int i
= 0; i
< w
; i
++)
1331 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1341 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1345 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1347 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1349 unsigned char *dest
= image
.GetData();
1351 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1353 unsigned char *src
= M_IMGDATA
->m_data
;
1354 bool hasMask
= M_IMGDATA
->m_hasMask
;
1355 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1356 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1357 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1360 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1362 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1363 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1365 // don't modify the mask
1366 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1368 memcpy(dest
, src
, 3);
1372 // calculate the luma
1373 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1374 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1378 // copy the alpha channel, if any
1381 const size_t alphaSize
= GetWidth() * GetHeight();
1382 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1383 memcpy(alpha
, GetAlpha(), alphaSize
);
1385 image
.SetAlpha(alpha
);
1391 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1395 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1397 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1399 unsigned char *data
= image
.GetData();
1401 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1403 if (M_IMGDATA
->m_hasMask
)
1405 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1406 M_IMGDATA
->m_maskBlue
== b
)
1407 image
.SetMaskColour( 255, 255, 255 );
1409 image
.SetMaskColour( 0, 0, 0 );
1412 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1414 unsigned char *srcd
= M_IMGDATA
->m_data
;
1415 unsigned char *tard
= image
.GetData();
1417 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1419 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1420 tard
[0] = tard
[1] = tard
[2] = 255;
1422 tard
[0] = tard
[1] = tard
[2] = 0;
1428 int wxImage::GetWidth() const
1430 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1432 return M_IMGDATA
->m_width
;
1435 int wxImage::GetHeight() const
1437 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1439 return M_IMGDATA
->m_height
;
1442 long wxImage::XYToIndex(int x
, int y
) const
1446 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1448 return y
*M_IMGDATA
->m_width
+ x
;
1454 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1456 long pos
= XYToIndex(x
, y
);
1457 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1463 M_IMGDATA
->m_data
[ pos
] = r
;
1464 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1465 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1468 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1470 wxCHECK_RET( Ok(), wxT("invalid image") );
1475 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1476 if ( rect
== wxRect() )
1482 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1483 imageRect
.Contains(rect
.GetBottomRight()),
1484 wxT("invalid bounding rectangle") );
1487 int x1
= rect
.GetLeft(),
1489 x2
= rect
.GetRight() + 1,
1490 y2
= rect
.GetBottom() + 1;
1492 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1493 int x
, y
, width
= GetWidth();
1494 for (y
= y1
; y
< y2
; y
++)
1496 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1497 for (x
= x1
; x
< x2
; x
++)
1506 unsigned char wxImage::GetRed( int x
, int y
) const
1508 long pos
= XYToIndex(x
, y
);
1509 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1513 return M_IMGDATA
->m_data
[pos
];
1516 unsigned char wxImage::GetGreen( int x
, int y
) const
1518 long pos
= XYToIndex(x
, y
);
1519 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1523 return M_IMGDATA
->m_data
[pos
+1];
1526 unsigned char wxImage::GetBlue( int x
, int y
) const
1528 long pos
= XYToIndex(x
, y
);
1529 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1533 return M_IMGDATA
->m_data
[pos
+2];
1536 bool wxImage::IsOk() const
1538 // image of 0 width or height can't be considered ok - at least because it
1539 // causes crashes in ConvertToBitmap() if we don't catch it in time
1540 wxImageRefData
*data
= M_IMGDATA
;
1541 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1544 unsigned char *wxImage::GetData() const
1546 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1548 return M_IMGDATA
->m_data
;
1551 void wxImage::SetData( unsigned char *data
, bool static_data
)
1553 wxCHECK_RET( Ok(), wxT("invalid image") );
1555 wxImageRefData
*newRefData
= new wxImageRefData();
1557 newRefData
->m_width
= M_IMGDATA
->m_width
;
1558 newRefData
->m_height
= M_IMGDATA
->m_height
;
1559 newRefData
->m_data
= data
;
1560 newRefData
->m_ok
= true;
1561 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1562 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1563 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1564 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1565 newRefData
->m_static
= static_data
;
1569 m_refData
= newRefData
;
1572 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1574 wxImageRefData
*newRefData
= new wxImageRefData();
1578 newRefData
->m_width
= new_width
;
1579 newRefData
->m_height
= new_height
;
1580 newRefData
->m_data
= data
;
1581 newRefData
->m_ok
= true;
1582 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1583 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1584 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1585 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1589 newRefData
->m_width
= new_width
;
1590 newRefData
->m_height
= new_height
;
1591 newRefData
->m_data
= data
;
1592 newRefData
->m_ok
= true;
1594 newRefData
->m_static
= static_data
;
1598 m_refData
= newRefData
;
1601 // ----------------------------------------------------------------------------
1602 // alpha channel support
1603 // ----------------------------------------------------------------------------
1605 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1607 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1609 long pos
= XYToIndex(x
, y
);
1610 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1614 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1617 unsigned char wxImage::GetAlpha(int x
, int y
) const
1619 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1621 long pos
= XYToIndex(x
, y
);
1622 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1624 return M_IMGDATA
->m_alpha
[pos
];
1628 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1632 const int w
= M_IMGDATA
->m_width
;
1633 const int h
= M_IMGDATA
->m_height
;
1635 unsigned char *alpha
= GetAlpha();
1636 unsigned char *data
= GetData();
1638 for ( int y
= 0; y
< h
; y
++ )
1640 for ( int x
= 0; x
< w
; x
++ )
1652 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1654 wxCHECK_RET( Ok(), wxT("invalid image") );
1660 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1663 if( !M_IMGDATA
->m_staticAlpha
)
1664 free(M_IMGDATA
->m_alpha
);
1666 M_IMGDATA
->m_alpha
= alpha
;
1667 M_IMGDATA
->m_staticAlpha
= static_data
;
1670 unsigned char *wxImage::GetAlpha() const
1672 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1674 return M_IMGDATA
->m_alpha
;
1677 void wxImage::InitAlpha()
1679 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1681 // initialize memory for alpha channel
1684 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1685 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1689 // use the mask to initialize the alpha channel.
1690 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1692 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1693 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1694 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1695 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1699 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1700 ? wxIMAGE_ALPHA_TRANSPARENT
1701 : wxIMAGE_ALPHA_OPAQUE
;
1704 M_IMGDATA
->m_hasMask
= false;
1708 // make the image fully opaque
1709 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1713 // ----------------------------------------------------------------------------
1715 // ----------------------------------------------------------------------------
1717 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1719 wxCHECK_RET( Ok(), wxT("invalid image") );
1723 M_IMGDATA
->m_maskRed
= r
;
1724 M_IMGDATA
->m_maskGreen
= g
;
1725 M_IMGDATA
->m_maskBlue
= b
;
1726 M_IMGDATA
->m_hasMask
= true;
1729 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1731 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1733 if (M_IMGDATA
->m_hasMask
)
1735 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1736 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1737 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1742 FindFirstUnusedColour(r
, g
, b
);
1747 unsigned char wxImage::GetMaskRed() const
1749 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1751 return M_IMGDATA
->m_maskRed
;
1754 unsigned char wxImage::GetMaskGreen() const
1756 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1758 return M_IMGDATA
->m_maskGreen
;
1761 unsigned char wxImage::GetMaskBlue() const
1763 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1765 return M_IMGDATA
->m_maskBlue
;
1768 void wxImage::SetMask( bool mask
)
1770 wxCHECK_RET( Ok(), wxT("invalid image") );
1774 M_IMGDATA
->m_hasMask
= mask
;
1777 bool wxImage::HasMask() const
1779 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1781 return M_IMGDATA
->m_hasMask
;
1784 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1786 long pos
= XYToIndex(x
, y
);
1787 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1790 if ( M_IMGDATA
->m_hasMask
)
1792 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1793 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1794 p
[1] == M_IMGDATA
->m_maskGreen
&&
1795 p
[2] == M_IMGDATA
->m_maskBlue
)
1802 if ( M_IMGDATA
->m_alpha
)
1804 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1806 // transparent enough
1815 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1816 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1818 // check that the images are the same size
1819 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1821 wxLogError( _("Image and mask have different sizes.") );
1825 // find unused colour
1826 unsigned char r
,g
,b
;
1827 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1829 wxLogError( _("No unused colour in image being masked.") );
1835 unsigned char *imgdata
= GetData();
1836 unsigned char *maskdata
= mask
.GetData();
1838 const int w
= GetWidth();
1839 const int h
= GetHeight();
1841 for (int j
= 0; j
< h
; j
++)
1843 for (int i
= 0; i
< w
; i
++)
1845 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1856 SetMaskColour(r
, g
, b
);
1862 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1867 unsigned char mr
, mg
, mb
;
1868 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1870 wxLogError( _("No unused colour in image being masked.") );
1877 SetMaskColour(mr
, mg
, mb
);
1879 unsigned char *imgdata
= GetData();
1880 unsigned char *alphadata
= GetAlpha();
1883 int h
= GetHeight();
1885 for (int y
= 0; y
< h
; y
++)
1887 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1889 if (*alphadata
< threshold
)
1898 if( !M_IMGDATA
->m_staticAlpha
)
1899 free(M_IMGDATA
->m_alpha
);
1901 M_IMGDATA
->m_alpha
= NULL
;
1902 M_IMGDATA
->m_staticAlpha
= false;
1907 // ----------------------------------------------------------------------------
1908 // Palette functions
1909 // ----------------------------------------------------------------------------
1913 bool wxImage::HasPalette() const
1918 return M_IMGDATA
->m_palette
.Ok();
1921 const wxPalette
& wxImage::GetPalette() const
1923 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1925 return M_IMGDATA
->m_palette
;
1928 void wxImage::SetPalette(const wxPalette
& palette
)
1930 wxCHECK_RET( Ok(), wxT("invalid image") );
1934 M_IMGDATA
->m_palette
= palette
;
1937 #endif // wxUSE_PALETTE
1939 // ----------------------------------------------------------------------------
1940 // Option functions (arbitrary name/value mapping)
1941 // ----------------------------------------------------------------------------
1943 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1945 wxCHECK_RET( Ok(), wxT("invalid image") );
1949 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1950 if (idx
== wxNOT_FOUND
)
1952 M_IMGDATA
->m_optionNames
.Add(name
);
1953 M_IMGDATA
->m_optionValues
.Add(value
);
1957 M_IMGDATA
->m_optionNames
[idx
] = name
;
1958 M_IMGDATA
->m_optionValues
[idx
] = value
;
1962 void wxImage::SetOption(const wxString
& name
, int value
)
1965 valStr
.Printf(wxT("%d"), value
);
1966 SetOption(name
, valStr
);
1969 wxString
wxImage::GetOption(const wxString
& name
) const
1971 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1973 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1974 if (idx
== wxNOT_FOUND
)
1975 return wxEmptyString
;
1977 return M_IMGDATA
->m_optionValues
[idx
];
1980 int wxImage::GetOptionInt(const wxString
& name
) const
1982 return wxAtoi(GetOption(name
));
1985 bool wxImage::HasOption(const wxString
& name
) const
1987 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1989 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1992 // ----------------------------------------------------------------------------
1994 // ----------------------------------------------------------------------------
1996 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1997 long WXUNUSED_UNLESS_STREAMS(type
),
1998 int WXUNUSED_UNLESS_STREAMS(index
) )
2000 #if HAS_FILE_STREAMS
2001 if (wxFileExists(filename
))
2003 wxImageFileInputStream
stream(filename
);
2004 wxBufferedInputStream
bstream( stream
);
2005 return LoadFile(bstream
, type
, index
);
2009 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2013 #else // !HAS_FILE_STREAMS
2015 #endif // HAS_FILE_STREAMS
2018 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2019 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2020 int WXUNUSED_UNLESS_STREAMS(index
) )
2022 #if HAS_FILE_STREAMS
2023 if (wxFileExists(filename
))
2025 wxImageFileInputStream
stream(filename
);
2026 wxBufferedInputStream
bstream( stream
);
2027 return LoadFile(bstream
, mimetype
, index
);
2031 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2035 #else // !HAS_FILE_STREAMS
2037 #endif // HAS_FILE_STREAMS
2042 bool wxImage::SaveFile( const wxString
& filename
) const
2044 wxString ext
= filename
.AfterLast('.').Lower();
2046 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
2049 SaveFile(filename
, pHandler
->GetType());
2053 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
2058 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2059 int WXUNUSED_UNLESS_STREAMS(type
) ) const
2061 #if HAS_FILE_STREAMS
2062 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2064 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2066 wxImageFileOutputStream
stream(filename
);
2068 if ( stream
.IsOk() )
2070 wxBufferedOutputStream
bstream( stream
);
2071 return SaveFile(bstream
, type
);
2073 #endif // HAS_FILE_STREAMS
2078 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2079 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2081 #if HAS_FILE_STREAMS
2082 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2084 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2086 wxImageFileOutputStream
stream(filename
);
2088 if ( stream
.IsOk() )
2090 wxBufferedOutputStream
bstream( stream
);
2091 return SaveFile(bstream
, mimetype
);
2093 #endif // HAS_FILE_STREAMS
2098 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2100 #if HAS_FILE_STREAMS
2101 wxImageFileInputStream
stream(name
);
2102 return CanRead(stream
);
2108 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2109 long WXUNUSED_UNLESS_STREAMS(type
) )
2111 #if HAS_FILE_STREAMS
2112 wxImageFileInputStream
stream(name
);
2114 return GetImageCount(stream
, type
);
2122 bool wxImage::CanRead( wxInputStream
&stream
)
2124 const wxList
& list
= GetHandlers();
2126 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2128 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2129 if (handler
->CanRead( stream
))
2136 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
2138 wxImageHandler
*handler
;
2140 if ( type
== wxBITMAP_TYPE_ANY
)
2142 wxList
&list
=GetHandlers();
2144 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2146 handler
=(wxImageHandler
*)node
->GetData();
2147 if ( handler
->CanRead(stream
) )
2148 return handler
->GetImageCount(stream
);
2152 wxLogWarning(_("No handler found for image type."));
2156 handler
= FindHandler(type
);
2160 wxLogWarning(_("No image handler for type %ld defined."), type
);
2164 if ( handler
->CanRead(stream
) )
2166 return handler
->GetImageCount(stream
);
2170 wxLogError(_("Image file is not of type %ld."), type
);
2175 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
2179 m_refData
= new wxImageRefData
;
2181 wxImageHandler
*handler
;
2183 if ( type
== wxBITMAP_TYPE_ANY
)
2185 wxList
&list
=GetHandlers();
2187 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2189 handler
=(wxImageHandler
*)node
->GetData();
2190 if ( handler
->CanRead(stream
) )
2191 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2195 wxLogWarning( _("No handler found for image type.") );
2199 handler
= FindHandler(type
);
2203 wxLogWarning( _("No image handler for type %ld defined."), type
);
2208 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2210 wxLogError(_("Image file is not of type %ld."), type
);
2214 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2217 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2221 m_refData
= new wxImageRefData
;
2223 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2227 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2232 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2234 wxLogError(_("Image file is not of type %s."), mimetype
);
2238 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
2241 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
2243 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2245 wxImageHandler
*handler
= FindHandler(type
);
2248 wxLogWarning( _("No image handler for type %d defined."), type
);
2253 return handler
->SaveFile( (wxImage
*)this, stream
);
2256 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2258 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2260 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2263 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2268 return handler
->SaveFile( (wxImage
*)this, stream
);
2270 #endif // wxUSE_STREAMS
2272 // ----------------------------------------------------------------------------
2273 // image I/O handlers
2274 // ----------------------------------------------------------------------------
2276 void wxImage::AddHandler( wxImageHandler
*handler
)
2278 // Check for an existing handler of the type being added.
2279 if (FindHandler( handler
->GetType() ) == 0)
2281 sm_handlers
.Append( handler
);
2285 // This is not documented behaviour, merely the simplest 'fix'
2286 // for preventing duplicate additions. If someone ever has
2287 // a good reason to add and remove duplicate handlers (and they
2288 // may) we should probably refcount the duplicates.
2289 // also an issue in InsertHandler below.
2291 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2292 handler
->GetName().c_str() );
2297 void wxImage::InsertHandler( wxImageHandler
*handler
)
2299 // Check for an existing handler of the type being added.
2300 if (FindHandler( handler
->GetType() ) == 0)
2302 sm_handlers
.Insert( handler
);
2306 // see AddHandler for additional comments.
2307 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2308 handler
->GetName().c_str() );
2313 bool wxImage::RemoveHandler( const wxString
& name
)
2315 wxImageHandler
*handler
= FindHandler(name
);
2318 sm_handlers
.DeleteObject(handler
);
2326 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2328 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2331 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2332 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2334 node
= node
->GetNext();
2339 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
2341 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2344 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2345 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2346 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
2348 node
= node
->GetNext();
2353 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
2355 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2358 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2359 if (handler
->GetType() == bitmapType
) return handler
;
2360 node
= node
->GetNext();
2365 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2367 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2370 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2371 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2372 node
= node
->GetNext();
2377 void wxImage::InitStandardHandlers()
2380 AddHandler(new wxBMPHandler
);
2381 #endif // wxUSE_STREAMS
2384 void wxImage::CleanUpHandlers()
2386 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2389 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2390 wxList::compatibility_iterator next
= node
->GetNext();
2395 sm_handlers
.Clear();
2398 wxString
wxImage::GetImageExtWildcard()
2402 wxList
& Handlers
= wxImage::GetHandlers();
2403 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2406 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2407 fmts
+= wxT("*.") + Handler
->GetExtension();
2408 Node
= Node
->GetNext();
2409 if ( Node
) fmts
+= wxT(";");
2412 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2415 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2417 const double red
= rgb
.red
/ 255.0,
2418 green
= rgb
.green
/ 255.0,
2419 blue
= rgb
.blue
/ 255.0;
2421 // find the min and max intensity (and remember which one was it for the
2423 double minimumRGB
= red
;
2424 if ( green
< minimumRGB
)
2426 if ( blue
< minimumRGB
)
2429 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2430 double maximumRGB
= red
;
2431 if ( green
> maximumRGB
)
2436 if ( blue
> maximumRGB
)
2442 const double value
= maximumRGB
;
2444 double hue
= 0.0, saturation
;
2445 const double deltaRGB
= maximumRGB
- minimumRGB
;
2446 if ( wxIsNullDouble(deltaRGB
) )
2448 // Gray has no color
2457 hue
= (green
- blue
) / deltaRGB
;
2461 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2465 hue
= 4.0 + (red
- green
) / deltaRGB
;
2469 wxFAIL_MSG(wxT("hue not specified"));
2478 saturation
= deltaRGB
/ maximumRGB
;
2481 return HSVValue(hue
, saturation
, value
);
2484 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2486 double red
, green
, blue
;
2488 if ( wxIsNullDouble(hsv
.saturation
) )
2497 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2498 int i
= (int)floor(hue
);
2499 double f
= hue
- i
; // fractional part of h
2500 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2506 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2511 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2519 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2524 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2529 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2537 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2542 return RGBValue((unsigned char)(red
* 255.0),
2543 (unsigned char)(green
* 255.0),
2544 (unsigned char)(blue
* 255.0));
2548 * Rotates the hue of each pixel of the image. angle is a double in the range
2549 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2551 void wxImage::RotateHue(double angle
)
2555 unsigned char *srcBytePtr
;
2556 unsigned char *dstBytePtr
;
2557 unsigned long count
;
2558 wxImage::HSVValue hsv
;
2559 wxImage::RGBValue rgb
;
2561 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2562 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2563 if ( count
> 0 && !wxIsNullDouble(angle
) )
2565 srcBytePtr
= M_IMGDATA
->m_data
;
2566 dstBytePtr
= srcBytePtr
;
2569 rgb
.red
= *srcBytePtr
++;
2570 rgb
.green
= *srcBytePtr
++;
2571 rgb
.blue
= *srcBytePtr
++;
2572 hsv
= RGBtoHSV(rgb
);
2574 hsv
.hue
= hsv
.hue
+ angle
;
2576 hsv
.hue
= hsv
.hue
- 1.0;
2577 else if (hsv
.hue
< 0.0)
2578 hsv
.hue
= hsv
.hue
+ 1.0;
2580 rgb
= HSVtoRGB(hsv
);
2581 *dstBytePtr
++ = rgb
.red
;
2582 *dstBytePtr
++ = rgb
.green
;
2583 *dstBytePtr
++ = rgb
.blue
;
2584 } while (--count
!= 0);
2588 //-----------------------------------------------------------------------------
2590 //-----------------------------------------------------------------------------
2592 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2595 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2600 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2605 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2610 bool wxImageHandler::CanRead( const wxString
& name
)
2612 if (wxFileExists(name
))
2614 wxImageFileInputStream
stream(name
);
2615 return CanRead(stream
);
2618 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2623 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2625 wxFileOffset posOld
= stream
.TellI();
2626 if ( posOld
== wxInvalidOffset
)
2628 // can't test unseekable stream
2632 bool ok
= DoCanRead(stream
);
2634 // restore the old position to be able to test other formats and so on
2635 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2637 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2639 // reading would fail anyhow as we're not at the right position
2646 #endif // wxUSE_STREAMS
2650 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2652 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, _T("NULL pointer") );
2654 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2655 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2657 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2658 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2660 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2663 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2665 else // no resolution options specified
2670 return wxIMAGE_RESOLUTION_NONE
;
2673 // get the resolution unit too
2674 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2677 // this is the default
2678 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2681 return (wxImageResolution
)resUnit
;
2684 // ----------------------------------------------------------------------------
2685 // image histogram stuff
2686 // ----------------------------------------------------------------------------
2689 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2694 unsigned char g2
) const
2696 unsigned long key
= MakeKey(r2
, g2
, b2
);
2698 while ( find(key
) != end() )
2700 // color already used
2712 wxLogError(_("No unused colour in image.") );
2718 key
= MakeKey(r2
, g2
, b2
);
2732 wxImage::FindFirstUnusedColour(unsigned char *r
,
2737 unsigned char g2
) const
2739 wxImageHistogram histogram
;
2741 ComputeHistogram(histogram
);
2743 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2749 // Counts and returns the number of different colours. Optionally stops
2750 // when it exceeds 'stopafter' different colours. This is useful, for
2751 // example, to see if the image can be saved as 8-bit (256 colour or
2752 // less, in this case it would be invoked as CountColours(256)). Default
2753 // value for stopafter is -1 (don't care).
2755 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2759 unsigned char r
, g
, b
;
2761 unsigned long size
, nentries
, key
;
2764 size
= GetWidth() * GetHeight();
2767 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2772 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2774 if (h
.Get(key
) == NULL
)
2785 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2787 unsigned char *p
= GetData();
2788 unsigned long nentries
= 0;
2792 const unsigned long size
= GetWidth() * GetHeight();
2794 unsigned char r
, g
, b
;
2795 for ( unsigned long n
= 0; n
< size
; n
++ )
2801 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2803 if ( entry
.value
++ == 0 )
2804 entry
.index
= nentries
++;
2811 * Rotation code by Carlos Moreno
2814 static const double wxROTATE_EPSILON
= 1e-10;
2816 // Auxiliary function to rotate a point (x,y) with respect to point p0
2817 // make it inline and use a straight return to facilitate optimization
2818 // also, the function receives the sine and cosine of the angle to avoid
2819 // repeating the time-consuming calls to these functions -- sin/cos can
2820 // be computed and stored in the calling function.
2822 static inline wxRealPoint
2823 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2824 const wxRealPoint
& p0
)
2826 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2827 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2830 static inline wxRealPoint
2831 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2832 const wxRealPoint
& p0
)
2834 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2837 wxImage
wxImage::Rotate(double angle
,
2838 const wxPoint
& centre_of_rotation
,
2840 wxPoint
*offset_after_rotation
) const
2842 // screen coordinates are a mirror image of "real" coordinates
2845 const bool has_alpha
= HasAlpha();
2847 const int w
= GetWidth();
2848 const int h
= GetHeight();
2852 // Create pointer-based array to accelerate access to wxImage's data
2853 unsigned char ** data
= new unsigned char * [h
];
2854 data
[0] = GetData();
2855 for (i
= 1; i
< h
; i
++)
2856 data
[i
] = data
[i
- 1] + (3 * w
);
2858 // Same for alpha channel
2859 unsigned char ** alpha
= NULL
;
2862 alpha
= new unsigned char * [h
];
2863 alpha
[0] = GetAlpha();
2864 for (i
= 1; i
< h
; i
++)
2865 alpha
[i
] = alpha
[i
- 1] + w
;
2868 // precompute coefficients for rotation formula
2869 const double cos_angle
= cos(angle
);
2870 const double sin_angle
= sin(angle
);
2872 // Create new Image to store the result
2873 // First, find rectangle that covers the rotated image; to do that,
2874 // rotate the four corners
2876 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2878 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2879 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
2880 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
2881 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
2883 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2884 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2885 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2886 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2888 // Create rotated image
2889 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2890 // With alpha channel
2894 if (offset_after_rotation
!= NULL
)
2896 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2899 // the rotated (destination) image is always accessed sequentially via this
2900 // pointer, there is no need for pointer-based arrays here
2901 unsigned char *dst
= rotated
.GetData();
2903 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
2905 // if the original image has a mask, use its RGB values as the blank pixel,
2906 // else, fall back to default (black).
2907 unsigned char blank_r
= 0;
2908 unsigned char blank_g
= 0;
2909 unsigned char blank_b
= 0;
2913 blank_r
= GetMaskRed();
2914 blank_g
= GetMaskGreen();
2915 blank_b
= GetMaskBlue();
2916 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2919 // Now, for each point of the rotated image, find where it came from, by
2920 // performing an inverse rotation (a rotation of -angle) and getting the
2921 // pixel at those coordinates
2923 const int rH
= rotated
.GetHeight();
2924 const int rW
= rotated
.GetWidth();
2926 // do the (interpolating) test outside of the loops, so that it is done
2927 // only once, instead of repeating it for each pixel.
2930 for (int y
= 0; y
< rH
; y
++)
2932 for (int x
= 0; x
< rW
; x
++)
2934 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2936 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
2937 -0.25 < src
.y
&& src
.y
< h
- 0.75)
2939 // interpolate using the 4 enclosing grid-points. Those
2940 // points can be obtained using floor and ceiling of the
2941 // exact coordinates of the point
2944 if (0 < src
.x
&& src
.x
< w
- 1)
2946 x1
= wxRound(floor(src
.x
));
2947 x2
= wxRound(ceil(src
.x
));
2949 else // else means that x is near one of the borders (0 or width-1)
2951 x1
= x2
= wxRound (src
.x
);
2954 if (0 < src
.y
&& src
.y
< h
- 1)
2956 y1
= wxRound(floor(src
.y
));
2957 y2
= wxRound(ceil(src
.y
));
2961 y1
= y2
= wxRound (src
.y
);
2964 // get four points and the distances (square of the distance,
2965 // for efficiency reasons) for the interpolation formula
2967 // GRG: Do not calculate the points until they are
2968 // really needed -- this way we can calculate
2969 // just one, instead of four, if d1, d2, d3
2970 // or d4 are < wxROTATE_EPSILON
2972 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2973 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2974 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2975 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2977 // Now interpolate as a weighted average of the four surrounding
2978 // points, where the weights are the distances to each of those points
2980 // If the point is exactly at one point of the grid of the source
2981 // image, then don't interpolate -- just assign the pixel
2983 // d1,d2,d3,d4 are positive -- no need for abs()
2984 if (d1
< wxROTATE_EPSILON
)
2986 unsigned char *p
= data
[y1
] + (3 * x1
);
2992 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2994 else if (d2
< wxROTATE_EPSILON
)
2996 unsigned char *p
= data
[y1
] + (3 * x2
);
3002 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3004 else if (d3
< wxROTATE_EPSILON
)
3006 unsigned char *p
= data
[y2
] + (3 * x2
);
3012 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3014 else if (d4
< wxROTATE_EPSILON
)
3016 unsigned char *p
= data
[y2
] + (3 * x1
);
3022 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3026 // weights for the weighted average are proportional to the inverse of the distance
3027 unsigned char *v1
= data
[y1
] + (3 * x1
);
3028 unsigned char *v2
= data
[y1
] + (3 * x2
);
3029 unsigned char *v3
= data
[y2
] + (3 * x2
);
3030 unsigned char *v4
= data
[y2
] + (3 * x1
);
3032 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3036 *(dst
++) = (unsigned char)
3037 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3038 w3
* *(v3
++) + w4
* *(v4
++)) /
3039 (w1
+ w2
+ w3
+ w4
) );
3040 *(dst
++) = (unsigned char)
3041 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3042 w3
* *(v3
++) + w4
* *(v4
++)) /
3043 (w1
+ w2
+ w3
+ w4
) );
3044 *(dst
++) = (unsigned char)
3045 ( (w1
* *v1
+ w2
* *v2
+
3046 w3
* *v3
+ w4
* *v4
) /
3047 (w1
+ w2
+ w3
+ w4
) );
3051 v1
= alpha
[y1
] + (x1
);
3052 v2
= alpha
[y1
] + (x2
);
3053 v3
= alpha
[y2
] + (x2
);
3054 v4
= alpha
[y2
] + (x1
);
3056 *(alpha_dst
++) = (unsigned char)
3057 ( (w1
* *v1
+ w2
* *v2
+
3058 w3
* *v3
+ w4
* *v4
) /
3059 (w1
+ w2
+ w3
+ w4
) );
3075 else // not interpolating
3077 for (int y
= 0; y
< rH
; y
++)
3079 for (int x
= 0; x
< rW
; x
++)
3081 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3083 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3084 const int ys
= wxRound (src
.y
); // closest integer
3086 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3088 unsigned char *p
= data
[ys
] + (3 * xs
);
3094 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3103 *(alpha_dst
++) = 255;
3119 // A module to allow wxImage initialization/cleanup
3120 // without calling these functions from app.cpp or from
3121 // the user's application.
3123 class wxImageModule
: public wxModule
3125 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3128 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3129 void OnExit() { wxImage::CleanUpHandlers(); }
3132 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3135 #endif // wxUSE_IMAGE