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 wxFileInputStream wxImageFileInputStream
;
44 typedef wxFileOutputStream wxImageFileOutputStream
;
46 typedef wxFFileInputStream wxImageFileInputStream
;
47 typedef wxFFileOutputStream 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 // If we want good-looking results we need to pre-blur the image a bit first
542 wxImage
src_image(*this);
543 src_image
= src_image
.BlurHorizontal(scale_factor_x_2
);
544 src_image
= src_image
.BlurVertical(scale_factor_y_2
);
546 unsigned char* src_data
= src_image
.GetData();
547 unsigned char* src_alpha
= src_image
.GetAlpha();
548 unsigned char* dst_data
= ret_image
.GetData();
549 unsigned char* dst_alpha
= NULL
;
553 ret_image
.SetAlpha();
554 dst_alpha
= ret_image
.GetAlpha();
557 int averaged_pixels
, src_pixel_index
;
558 double sum_r
, sum_g
, sum_b
, sum_a
;
560 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
562 // Source pixel in the Y direction
563 int src_y
= (int)(y
* scale_factor_y
);
565 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
567 // Source pixel in the X direction
568 int src_x
= (int)(x
* scale_factor_x
);
570 // Box of pixels to average
572 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
574 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
575 j
<= int(src_y
+ scale_factor_y_2
);
578 // We don't care to average pixels that don't exist (edges)
579 if ( j
< 0 || j
> M_IMGDATA
->m_height
)
582 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
583 i
<= src_x
+ scale_factor_x_2
;
586 // Don't average edge pixels
587 if ( i
< 0 || i
> M_IMGDATA
->m_width
)
590 // Calculate the actual index in our source pixels
591 src_pixel_index
= src_y
* M_IMGDATA
->m_width
+ src_x
;
593 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
594 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
595 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
597 sum_a
+= src_alpha
[src_pixel_index
];
603 // Calculate the average from the sum and number of averaged pixels
604 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
605 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
606 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
609 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
616 // The following two local functions are for the B-spline weighting of the
617 // bicubic sampling algorithm
618 static inline double spline_cube(double value
)
620 return value
<= 0.0 ? 0.0 : value
* value
* value
;
623 static inline double spline_weight(double value
)
625 return (spline_cube(value
+ 2) -
626 4 * spline_cube(value
+ 1) +
627 6 * spline_cube(value
) -
628 4 * spline_cube(value
- 1)) / 6;
631 // This is the bicubic resampling algorithm
632 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
634 // This function implements a Bicubic B-Spline algorithm for resampling.
635 // This method is certainly a little slower than wxImage's default pixel
636 // replication method, however for most reasonably sized images not being
637 // upsampled too much on a fairly average CPU this difference is hardly
638 // noticeable and the results are far more pleasing to look at.
640 // This particular bicubic algorithm does pixel weighting according to a
641 // B-Spline that basically implements a Gaussian bell-like weighting
642 // kernel. Because of this method the results may appear a bit blurry when
643 // upsampling by large factors. This is basically because a slight
644 // gaussian blur is being performed to get the smooth look of the upsampled
647 // Edge pixels: 3-4 possible solutions
648 // - (Wrap/tile) Wrap the image, take the color value from the opposite
649 // side of the image.
650 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
651 // where n is nonpositive, will have the value of (2, 1).
652 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
653 // pixels which do have all neighbours.
654 // - (Clamp) Choose the nearest pixel along the border. This takes the
655 // border pixels and extends them out to infinity.
657 // NOTE: below the y_offset and x_offset variables are being set for edge
658 // pixels using the "Mirror" method mentioned above
662 ret_image
.Create(width
, height
, false);
664 unsigned char* src_data
= M_IMGDATA
->m_data
;
665 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
666 unsigned char* dst_data
= ret_image
.GetData();
667 unsigned char* dst_alpha
= NULL
;
671 ret_image
.SetAlpha();
672 dst_alpha
= ret_image
.GetAlpha();
675 for ( int dsty
= 0; dsty
< height
; dsty
++ )
677 // We need to calculate the source pixel to interpolate from - Y-axis
678 double srcpixy
= dsty
* M_IMGDATA
->m_height
/ height
;
679 double dy
= srcpixy
- (int)srcpixy
;
681 for ( int dstx
= 0; dstx
< width
; dstx
++ )
683 // X-axis of pixel to interpolate from
684 double srcpixx
= dstx
* M_IMGDATA
->m_width
/ width
;
685 double dx
= srcpixx
- (int)srcpixx
;
687 // Sums for each color channel
688 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
690 // Here we actually determine the RGBA values for the destination pixel
691 for ( int k
= -1; k
<= 2; k
++ )
694 int y_offset
= srcpixy
+ k
< 0.0
696 : srcpixy
+ k
>= M_IMGDATA
->m_height
697 ? M_IMGDATA
->m_height
- 1
698 : (int)(srcpixy
+ k
);
700 // Loop across the X axis
701 for ( int i
= -1; i
<= 2; i
++ )
704 int x_offset
= srcpixx
+ i
< 0.0
706 : srcpixx
+ i
>= M_IMGDATA
->m_width
707 ? M_IMGDATA
->m_width
- 1
708 : (int)(srcpixx
+ i
);
710 // Calculate the exact position where the source data
711 // should be pulled from based on the x_offset and y_offset
712 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
714 // Calculate the weight for the specified pixel according
715 // to the bicubic b-spline kernel we're using for
718 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
720 // Create a sum of all velues for each color channel
721 // adjusted for the pixel's calculated weight
722 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
723 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
724 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
726 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
730 // Put the data into the destination image. The summed values are
731 // of double data type and are rounded here for accuracy
732 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
733 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
734 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
738 *dst_alpha
++ = (unsigned char)sum_a
;
745 // Blur in the horizontal direction
746 wxImage
wxImage::BlurHorizontal(int blurRadius
)
749 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
751 unsigned char* src_data
= M_IMGDATA
->m_data
;
752 unsigned char* dst_data
= ret_image
.GetData();
753 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
754 unsigned char* dst_alpha
= NULL
;
756 // Check for a mask or alpha
757 if ( M_IMGDATA
->m_hasMask
)
759 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
760 M_IMGDATA
->m_maskGreen
,
761 M_IMGDATA
->m_maskBlue
);
767 ret_image
.SetAlpha();
768 dst_alpha
= ret_image
.GetAlpha();
772 // number of pixels we average over
773 const int blurArea
= blurRadius
*2 + 1;
775 // Horizontal blurring algorithm - average all pixels in the specified blur
776 // radius in the X or horizontal direction
777 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
779 // Variables used in the blurring algorithm
786 const unsigned char *src
;
789 // Calculate the average of all pixels in the blur radius for the first
791 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
793 // To deal with the pixels at the start of a row so it's not
794 // grabbing GOK values from memory at negative indices of the
795 // image's data or grabbing from the previous row
797 pixel_idx
= y
* M_IMGDATA
->m_width
;
799 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
801 src
= src_data
+ pixel_idx
*3;
806 sum_a
+= src_alpha
[pixel_idx
];
809 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
810 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
811 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
812 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
814 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
816 // Now average the values of the rest of the pixels by just moving the
817 // blur radius box along the row
818 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
820 // Take care of edge pixels on the left edge by essentially
821 // duplicating the edge pixel
822 if ( x
- blurRadius
- 1 < 0 )
823 pixel_idx
= y
* M_IMGDATA
->m_width
;
825 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
827 // Subtract the value of the pixel at the left side of the blur
829 src
= src_data
+ pixel_idx
*3;
834 sum_a
-= src_alpha
[pixel_idx
];
836 // Take care of edge pixels on the right edge
837 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
838 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
840 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
842 // Add the value of the pixel being added to the end of our box
843 src
= src_data
+ pixel_idx
*3;
848 sum_a
+= src_alpha
[pixel_idx
];
850 // Save off the averaged data
851 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
852 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
853 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
854 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
856 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
863 // Blur in the vertical direction
864 wxImage
wxImage::BlurVertical(int blurRadius
)
867 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
869 unsigned char* src_data
= M_IMGDATA
->m_data
;
870 unsigned char* dst_data
= ret_image
.GetData();
871 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
872 unsigned char* dst_alpha
= NULL
;
874 // Check for a mask or alpha
875 if ( M_IMGDATA
->m_hasMask
)
877 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
878 M_IMGDATA
->m_maskGreen
,
879 M_IMGDATA
->m_maskBlue
);
885 ret_image
.SetAlpha();
886 dst_alpha
= ret_image
.GetAlpha();
890 // number of pixels we average over
891 const int blurArea
= blurRadius
*2 + 1;
893 // Vertical blurring algorithm - same as horizontal but switched the
894 // opposite direction
895 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
897 // Variables used in the blurring algorithm
904 const unsigned char *src
;
907 // Calculate the average of all pixels in our blur radius box for the
908 // first pixel of the column
909 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
911 // To deal with the pixels at the start of a column so it's not
912 // grabbing GOK values from memory at negative indices of the
913 // image's data or grabbing from the previous column
917 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
919 src
= src_data
+ pixel_idx
*3;
924 sum_a
+= src_alpha
[pixel_idx
];
927 dst
= dst_data
+ x
*3;
928 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
929 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
930 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
932 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
934 // Now average the values of the rest of the pixels by just moving the
935 // box along the column from top to bottom
936 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
938 // Take care of pixels that would be beyond the top edge by
939 // duplicating the top edge pixel for the column
940 if ( y
- blurRadius
- 1 < 0 )
943 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
945 // Subtract the value of the pixel at the top of our blur radius box
946 src
= src_data
+ pixel_idx
*3;
951 sum_a
-= src_alpha
[pixel_idx
];
953 // Take care of the pixels that would be beyond the bottom edge of
954 // the image similar to the top edge
955 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
956 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
958 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
960 // Add the value of the pixel being added to the end of our box
961 src
= src_data
+ pixel_idx
*3;
966 sum_a
+= src_alpha
[pixel_idx
];
968 // Save off the averaged data
969 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
970 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
971 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
972 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
974 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
981 // The new blur function
982 wxImage
wxImage::Blur(int blurRadius
)
985 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
987 // Blur the image in each direction
988 ret_image
= BlurHorizontal(blurRadius
);
989 ret_image
= ret_image
.BlurVertical(blurRadius
);
994 wxImage
wxImage::Rotate90( bool clockwise
) const
998 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1000 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
1002 unsigned char *data
= image
.GetData();
1004 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1006 unsigned char *source_data
= M_IMGDATA
->m_data
;
1007 unsigned char *target_data
;
1008 unsigned char *alpha_data
= 0 ;
1009 unsigned char *source_alpha
= 0 ;
1010 unsigned char *target_alpha
= 0 ;
1012 if (M_IMGDATA
->m_hasMask
)
1014 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1018 source_alpha
= M_IMGDATA
->m_alpha
;
1022 alpha_data
= image
.GetAlpha() ;
1026 long height
= M_IMGDATA
->m_height
;
1027 long width
= M_IMGDATA
->m_width
;
1029 for (long j
= 0; j
< height
; j
++)
1031 for (long i
= 0; i
< width
; i
++)
1035 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1037 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1041 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1043 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1045 memcpy( target_data
, source_data
, 3 );
1050 memcpy( target_alpha
, source_alpha
, 1 );
1059 wxImage
wxImage::Mirror( bool horizontally
) const
1063 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1065 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1067 unsigned char *data
= image
.GetData();
1068 unsigned char *alpha
= NULL
;
1070 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1072 if (M_IMGDATA
->m_alpha
!= NULL
) {
1074 alpha
= image
.GetAlpha();
1075 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1078 if (M_IMGDATA
->m_hasMask
)
1079 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1081 long height
= M_IMGDATA
->m_height
;
1082 long width
= M_IMGDATA
->m_width
;
1084 unsigned char *source_data
= M_IMGDATA
->m_data
;
1085 unsigned char *target_data
;
1089 for (long j
= 0; j
< height
; j
++)
1092 target_data
= data
-3;
1093 for (long i
= 0; i
< width
; i
++)
1095 memcpy( target_data
, source_data
, 3 );
1103 // src_alpha starts at the first pixel and increases by 1 after each step
1104 // (a step here is the copy of the alpha value of one pixel)
1105 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1106 // dest_alpha starts just beyond the first line, decreases before each step,
1107 // and after each line is finished, increases by 2 widths (skipping the line
1108 // just copied and the line that will be copied next)
1109 unsigned char *dest_alpha
= alpha
+ width
;
1111 for (long jj
= 0; jj
< height
; ++jj
)
1113 for (long i
= 0; i
< width
; ++i
) {
1114 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1116 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1122 for (long i
= 0; i
< height
; i
++)
1124 target_data
= data
+ 3*width
*(height
-1-i
);
1125 memcpy( target_data
, source_data
, (size_t)3*width
);
1126 source_data
+= 3*width
;
1131 // src_alpha starts at the first pixel and increases by 1 width after each step
1132 // (a step here is the copy of the alpha channel of an entire line)
1133 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1134 // dest_alpha starts just beyond the last line (beyond the whole image)
1135 // and decreases by 1 width before each step
1136 unsigned char *dest_alpha
= alpha
+ width
* height
;
1138 for (long jj
= 0; jj
< height
; ++jj
)
1140 dest_alpha
-= width
;
1141 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1150 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1154 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1156 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1157 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1158 image
, wxT("invalid subimage size") );
1160 const int subwidth
= rect
.GetWidth();
1161 const int subheight
= rect
.GetHeight();
1163 image
.Create( subwidth
, subheight
, false );
1165 const unsigned char *src_data
= GetData();
1166 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1167 unsigned char *subdata
= image
.GetData();
1168 unsigned char *subalpha
= NULL
;
1170 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1172 if (src_alpha
!= NULL
) {
1174 subalpha
= image
.GetAlpha();
1175 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1178 if (M_IMGDATA
->m_hasMask
)
1179 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1181 const int width
= GetWidth();
1182 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1184 src_data
+= 3 * pixsoff
;
1185 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1187 for (long j
= 0; j
< subheight
; ++j
)
1189 memcpy( subdata
, src_data
, 3 * subwidth
);
1190 subdata
+= 3 * subwidth
;
1191 src_data
+= 3 * width
;
1192 if (subalpha
!= NULL
) {
1193 memcpy( subalpha
, src_alpha
, subwidth
);
1194 subalpha
+= subwidth
;
1202 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1203 int r_
, int g_
, int b_
) const
1207 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1208 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1210 int width
= GetWidth(), height
= GetHeight();
1211 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1213 unsigned char r
= (unsigned char)r_
;
1214 unsigned char g
= (unsigned char)g_
;
1215 unsigned char b
= (unsigned char)b_
;
1216 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1218 GetOrFindMaskColour( &r
, &g
, &b
);
1219 image
.SetMaskColour(r
, g
, b
);
1222 image
.SetRGB(wxRect(), r
, g
, b
);
1224 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1225 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1227 finalRect
.width
-= pos
.x
;
1229 finalRect
.height
-= pos
.y
;
1231 subRect
.Intersect(finalRect
);
1233 if (!subRect
.IsEmpty())
1235 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1236 image
.Paste(*this, pos
.x
, pos
.y
);
1238 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1244 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1246 wxCHECK_RET( Ok(), wxT("invalid image") );
1247 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1253 int width
= image
.GetWidth();
1254 int height
= image
.GetHeight();
1267 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1268 width
= M_IMGDATA
->m_width
- (x
+xx
);
1269 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1270 height
= M_IMGDATA
->m_height
- (y
+yy
);
1272 if (width
< 1) return;
1273 if (height
< 1) return;
1275 if ((!HasMask() && !image
.HasMask()) ||
1276 (HasMask() && !image
.HasMask()) ||
1277 ((HasMask() && image
.HasMask() &&
1278 (GetMaskRed()==image
.GetMaskRed()) &&
1279 (GetMaskGreen()==image
.GetMaskGreen()) &&
1280 (GetMaskBlue()==image
.GetMaskBlue()))))
1283 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1284 int source_step
= image
.GetWidth()*3;
1286 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1287 int target_step
= M_IMGDATA
->m_width
*3;
1288 for (int j
= 0; j
< height
; j
++)
1290 memcpy( target_data
, source_data
, width
);
1291 source_data
+= source_step
;
1292 target_data
+= target_step
;
1297 if (!HasMask() && image
.HasMask())
1299 unsigned char r
= image
.GetMaskRed();
1300 unsigned char g
= image
.GetMaskGreen();
1301 unsigned char b
= image
.GetMaskBlue();
1304 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1305 int source_step
= image
.GetWidth()*3;
1307 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1308 int target_step
= M_IMGDATA
->m_width
*3;
1310 for (int j
= 0; j
< height
; j
++)
1312 for (int i
= 0; i
< width
; i
+=3)
1314 if ((source_data
[i
] != r
) ||
1315 (source_data
[i
+1] != g
) ||
1316 (source_data
[i
+2] != b
))
1318 memcpy( target_data
+i
, source_data
+i
, 3 );
1321 source_data
+= source_step
;
1322 target_data
+= target_step
;
1327 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1328 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1330 wxCHECK_RET( Ok(), wxT("invalid image") );
1334 unsigned char *data
= GetData();
1336 const int w
= GetWidth();
1337 const int h
= GetHeight();
1339 for (int j
= 0; j
< h
; j
++)
1340 for (int i
= 0; i
< w
; i
++)
1342 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1352 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1356 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1358 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1360 unsigned char *dest
= image
.GetData();
1362 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1364 unsigned char *src
= M_IMGDATA
->m_data
;
1365 bool hasMask
= M_IMGDATA
->m_hasMask
;
1366 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1367 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1368 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1371 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1373 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1374 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1376 // don't modify the mask
1377 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1379 memcpy(dest
, src
, 3);
1383 // calculate the luma
1384 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1385 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1389 // copy the alpha channel, if any
1392 const size_t alphaSize
= GetWidth() * GetHeight();
1393 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1394 memcpy(alpha
, GetAlpha(), alphaSize
);
1396 image
.SetAlpha(alpha
);
1402 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1406 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1408 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1410 unsigned char *data
= image
.GetData();
1412 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1414 if (M_IMGDATA
->m_hasMask
)
1416 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1417 M_IMGDATA
->m_maskBlue
== b
)
1418 image
.SetMaskColour( 255, 255, 255 );
1420 image
.SetMaskColour( 0, 0, 0 );
1423 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1425 unsigned char *srcd
= M_IMGDATA
->m_data
;
1426 unsigned char *tard
= image
.GetData();
1428 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1430 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1431 tard
[0] = tard
[1] = tard
[2] = 255;
1433 tard
[0] = tard
[1] = tard
[2] = 0;
1439 int wxImage::GetWidth() const
1441 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1443 return M_IMGDATA
->m_width
;
1446 int wxImage::GetHeight() const
1448 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1450 return M_IMGDATA
->m_height
;
1453 long wxImage::XYToIndex(int x
, int y
) const
1457 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1459 return y
*M_IMGDATA
->m_width
+ x
;
1465 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1467 long pos
= XYToIndex(x
, y
);
1468 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1474 M_IMGDATA
->m_data
[ pos
] = r
;
1475 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1476 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1479 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1481 wxCHECK_RET( Ok(), wxT("invalid image") );
1486 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1487 if ( rect
== wxRect() )
1493 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1494 imageRect
.Contains(rect
.GetBottomRight()),
1495 wxT("invalid bounding rectangle") );
1498 int x1
= rect
.GetLeft(),
1500 x2
= rect
.GetRight() + 1,
1501 y2
= rect
.GetBottom() + 1;
1503 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1504 int x
, y
, width
= GetWidth();
1505 for (y
= y1
; y
< y2
; y
++)
1507 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1508 for (x
= x1
; x
< x2
; x
++)
1517 unsigned char wxImage::GetRed( int x
, int y
) const
1519 long pos
= XYToIndex(x
, y
);
1520 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1524 return M_IMGDATA
->m_data
[pos
];
1527 unsigned char wxImage::GetGreen( int x
, int y
) const
1529 long pos
= XYToIndex(x
, y
);
1530 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1534 return M_IMGDATA
->m_data
[pos
+1];
1537 unsigned char wxImage::GetBlue( int x
, int y
) const
1539 long pos
= XYToIndex(x
, y
);
1540 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1544 return M_IMGDATA
->m_data
[pos
+2];
1547 bool wxImage::IsOk() const
1549 // image of 0 width or height can't be considered ok - at least because it
1550 // causes crashes in ConvertToBitmap() if we don't catch it in time
1551 wxImageRefData
*data
= M_IMGDATA
;
1552 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1555 unsigned char *wxImage::GetData() const
1557 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1559 return M_IMGDATA
->m_data
;
1562 void wxImage::SetData( unsigned char *data
, bool static_data
)
1564 wxCHECK_RET( Ok(), wxT("invalid image") );
1566 wxImageRefData
*newRefData
= new wxImageRefData();
1568 newRefData
->m_width
= M_IMGDATA
->m_width
;
1569 newRefData
->m_height
= M_IMGDATA
->m_height
;
1570 newRefData
->m_data
= data
;
1571 newRefData
->m_ok
= true;
1572 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1573 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1574 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1575 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1576 newRefData
->m_static
= static_data
;
1580 m_refData
= newRefData
;
1583 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1585 wxImageRefData
*newRefData
= new wxImageRefData();
1589 newRefData
->m_width
= new_width
;
1590 newRefData
->m_height
= new_height
;
1591 newRefData
->m_data
= data
;
1592 newRefData
->m_ok
= true;
1593 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1594 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1595 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1596 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1600 newRefData
->m_width
= new_width
;
1601 newRefData
->m_height
= new_height
;
1602 newRefData
->m_data
= data
;
1603 newRefData
->m_ok
= true;
1605 newRefData
->m_static
= static_data
;
1609 m_refData
= newRefData
;
1612 // ----------------------------------------------------------------------------
1613 // alpha channel support
1614 // ----------------------------------------------------------------------------
1616 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1618 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1620 long pos
= XYToIndex(x
, y
);
1621 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1625 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1628 unsigned char wxImage::GetAlpha(int x
, int y
) const
1630 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1632 long pos
= XYToIndex(x
, y
);
1633 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1635 return M_IMGDATA
->m_alpha
[pos
];
1639 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1643 const int w
= M_IMGDATA
->m_width
;
1644 const int h
= M_IMGDATA
->m_height
;
1646 unsigned char *alpha
= GetAlpha();
1647 unsigned char *data
= GetData();
1649 for ( int y
= 0; y
< h
; y
++ )
1651 for ( int x
= 0; x
< w
; x
++ )
1663 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1665 wxCHECK_RET( Ok(), wxT("invalid image") );
1671 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1674 free(M_IMGDATA
->m_alpha
);
1675 M_IMGDATA
->m_alpha
= alpha
;
1676 M_IMGDATA
->m_staticAlpha
= static_data
;
1679 unsigned char *wxImage::GetAlpha() const
1681 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1683 return M_IMGDATA
->m_alpha
;
1686 void wxImage::InitAlpha()
1688 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1690 // initialize memory for alpha channel
1693 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1694 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1698 // use the mask to initialize the alpha channel.
1699 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1701 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1702 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1703 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1704 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1708 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1709 ? wxIMAGE_ALPHA_TRANSPARENT
1710 : wxIMAGE_ALPHA_OPAQUE
;
1713 M_IMGDATA
->m_hasMask
= false;
1717 // make the image fully opaque
1718 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1722 // ----------------------------------------------------------------------------
1724 // ----------------------------------------------------------------------------
1726 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1728 wxCHECK_RET( Ok(), wxT("invalid image") );
1732 M_IMGDATA
->m_maskRed
= r
;
1733 M_IMGDATA
->m_maskGreen
= g
;
1734 M_IMGDATA
->m_maskBlue
= b
;
1735 M_IMGDATA
->m_hasMask
= true;
1738 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1740 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1742 if (M_IMGDATA
->m_hasMask
)
1744 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1745 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1746 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1751 FindFirstUnusedColour(r
, g
, b
);
1756 unsigned char wxImage::GetMaskRed() const
1758 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1760 return M_IMGDATA
->m_maskRed
;
1763 unsigned char wxImage::GetMaskGreen() const
1765 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1767 return M_IMGDATA
->m_maskGreen
;
1770 unsigned char wxImage::GetMaskBlue() const
1772 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1774 return M_IMGDATA
->m_maskBlue
;
1777 void wxImage::SetMask( bool mask
)
1779 wxCHECK_RET( Ok(), wxT("invalid image") );
1783 M_IMGDATA
->m_hasMask
= mask
;
1786 bool wxImage::HasMask() const
1788 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1790 return M_IMGDATA
->m_hasMask
;
1793 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1795 long pos
= XYToIndex(x
, y
);
1796 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1799 if ( M_IMGDATA
->m_hasMask
)
1801 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1802 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1803 p
[1] == M_IMGDATA
->m_maskGreen
&&
1804 p
[2] == M_IMGDATA
->m_maskBlue
)
1811 if ( M_IMGDATA
->m_alpha
)
1813 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1815 // transparent enough
1824 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1825 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1827 // check that the images are the same size
1828 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1830 wxLogError( _("Image and mask have different sizes.") );
1834 // find unused colour
1835 unsigned char r
,g
,b
;
1836 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1838 wxLogError( _("No unused colour in image being masked.") );
1844 unsigned char *imgdata
= GetData();
1845 unsigned char *maskdata
= mask
.GetData();
1847 const int w
= GetWidth();
1848 const int h
= GetHeight();
1850 for (int j
= 0; j
< h
; j
++)
1852 for (int i
= 0; i
< w
; i
++)
1854 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1865 SetMaskColour(r
, g
, b
);
1871 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1876 unsigned char mr
, mg
, mb
;
1877 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1879 wxLogError( _("No unused colour in image being masked.") );
1886 SetMaskColour(mr
, mg
, mb
);
1888 unsigned char *imgdata
= GetData();
1889 unsigned char *alphadata
= GetAlpha();
1892 int h
= GetHeight();
1894 for (int y
= 0; y
< h
; y
++)
1896 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1898 if (*alphadata
< threshold
)
1907 free(M_IMGDATA
->m_alpha
);
1908 M_IMGDATA
->m_alpha
= NULL
;
1913 // ----------------------------------------------------------------------------
1914 // Palette functions
1915 // ----------------------------------------------------------------------------
1919 bool wxImage::HasPalette() const
1924 return M_IMGDATA
->m_palette
.Ok();
1927 const wxPalette
& wxImage::GetPalette() const
1929 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1931 return M_IMGDATA
->m_palette
;
1934 void wxImage::SetPalette(const wxPalette
& palette
)
1936 wxCHECK_RET( Ok(), wxT("invalid image") );
1940 M_IMGDATA
->m_palette
= palette
;
1943 #endif // wxUSE_PALETTE
1945 // ----------------------------------------------------------------------------
1946 // Option functions (arbitrary name/value mapping)
1947 // ----------------------------------------------------------------------------
1949 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1951 wxCHECK_RET( Ok(), wxT("invalid image") );
1955 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1956 if (idx
== wxNOT_FOUND
)
1958 M_IMGDATA
->m_optionNames
.Add(name
);
1959 M_IMGDATA
->m_optionValues
.Add(value
);
1963 M_IMGDATA
->m_optionNames
[idx
] = name
;
1964 M_IMGDATA
->m_optionValues
[idx
] = value
;
1968 void wxImage::SetOption(const wxString
& name
, int value
)
1971 valStr
.Printf(wxT("%d"), value
);
1972 SetOption(name
, valStr
);
1975 wxString
wxImage::GetOption(const wxString
& name
) const
1977 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1979 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1980 if (idx
== wxNOT_FOUND
)
1981 return wxEmptyString
;
1983 return M_IMGDATA
->m_optionValues
[idx
];
1986 int wxImage::GetOptionInt(const wxString
& name
) const
1988 return wxAtoi(GetOption(name
));
1991 bool wxImage::HasOption(const wxString
& name
) const
1993 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1995 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1998 // ----------------------------------------------------------------------------
2000 // ----------------------------------------------------------------------------
2002 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2003 long WXUNUSED_UNLESS_STREAMS(type
),
2004 int WXUNUSED_UNLESS_STREAMS(index
) )
2006 #if HAS_FILE_STREAMS
2007 if (wxFileExists(filename
))
2009 wxImageFileInputStream
stream(filename
);
2010 wxBufferedInputStream
bstream( stream
);
2011 return LoadFile(bstream
, type
, index
);
2015 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2019 #else // !HAS_FILE_STREAMS
2021 #endif // HAS_FILE_STREAMS
2024 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2025 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2026 int WXUNUSED_UNLESS_STREAMS(index
) )
2028 #if HAS_FILE_STREAMS
2029 if (wxFileExists(filename
))
2031 wxImageFileInputStream
stream(filename
);
2032 wxBufferedInputStream
bstream( stream
);
2033 return LoadFile(bstream
, mimetype
, index
);
2037 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2041 #else // !HAS_FILE_STREAMS
2043 #endif // HAS_FILE_STREAMS
2048 bool wxImage::SaveFile( const wxString
& filename
) const
2050 wxString ext
= filename
.AfterLast('.').Lower();
2052 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
2055 SaveFile(filename
, pHandler
->GetType());
2059 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
2064 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2065 int WXUNUSED_UNLESS_STREAMS(type
) ) const
2067 #if HAS_FILE_STREAMS
2068 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2070 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2072 wxImageFileOutputStream
stream(filename
);
2074 if ( stream
.IsOk() )
2076 wxBufferedOutputStream
bstream( stream
);
2077 return SaveFile(bstream
, type
);
2079 #endif // HAS_FILE_STREAMS
2084 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2085 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2087 #if HAS_FILE_STREAMS
2088 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2090 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2092 wxImageFileOutputStream
stream(filename
);
2094 if ( stream
.IsOk() )
2096 wxBufferedOutputStream
bstream( stream
);
2097 return SaveFile(bstream
, mimetype
);
2099 #endif // HAS_FILE_STREAMS
2104 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2106 #if HAS_FILE_STREAMS
2107 wxImageFileInputStream
stream(name
);
2108 return CanRead(stream
);
2114 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2115 long WXUNUSED_UNLESS_STREAMS(type
) )
2117 #if HAS_FILE_STREAMS
2118 wxImageFileInputStream
stream(name
);
2120 return GetImageCount(stream
, type
);
2128 bool wxImage::CanRead( wxInputStream
&stream
)
2130 const wxList
& list
= GetHandlers();
2132 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2134 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2135 if (handler
->CanRead( stream
))
2142 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
2144 wxImageHandler
*handler
;
2146 if ( type
== wxBITMAP_TYPE_ANY
)
2148 wxList
&list
=GetHandlers();
2150 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2152 handler
=(wxImageHandler
*)node
->GetData();
2153 if ( handler
->CanRead(stream
) )
2154 return handler
->GetImageCount(stream
);
2158 wxLogWarning(_("No handler found for image type."));
2162 handler
= FindHandler(type
);
2166 wxLogWarning(_("No image handler for type %ld defined."), type
);
2170 if ( handler
->CanRead(stream
) )
2172 return handler
->GetImageCount(stream
);
2176 wxLogError(_("Image file is not of type %ld."), type
);
2181 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
2185 m_refData
= new wxImageRefData
;
2187 wxImageHandler
*handler
;
2189 if ( type
== wxBITMAP_TYPE_ANY
)
2191 wxList
&list
=GetHandlers();
2193 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2195 handler
=(wxImageHandler
*)node
->GetData();
2196 if ( handler
->CanRead(stream
) )
2197 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2201 wxLogWarning( _("No handler found for image type.") );
2205 handler
= FindHandler(type
);
2209 wxLogWarning( _("No image handler for type %ld defined."), type
);
2214 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2216 wxLogError(_("Image file is not of type %ld."), type
);
2220 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2223 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2227 m_refData
= new wxImageRefData
;
2229 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2233 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2238 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2240 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
2244 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
2247 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
2249 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2251 wxImageHandler
*handler
= FindHandler(type
);
2254 wxLogWarning( _("No image handler for type %d defined."), type
);
2259 return handler
->SaveFile( (wxImage
*)this, stream
);
2262 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2264 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2266 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2269 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2274 return handler
->SaveFile( (wxImage
*)this, stream
);
2276 #endif // wxUSE_STREAMS
2278 // ----------------------------------------------------------------------------
2279 // image I/O handlers
2280 // ----------------------------------------------------------------------------
2282 void wxImage::AddHandler( wxImageHandler
*handler
)
2284 // Check for an existing handler of the type being added.
2285 if (FindHandler( handler
->GetType() ) == 0)
2287 sm_handlers
.Append( handler
);
2291 // This is not documented behaviour, merely the simplest 'fix'
2292 // for preventing duplicate additions. If someone ever has
2293 // a good reason to add and remove duplicate handlers (and they
2294 // may) we should probably refcount the duplicates.
2295 // also an issue in InsertHandler below.
2297 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2298 handler
->GetName().c_str() );
2303 void wxImage::InsertHandler( wxImageHandler
*handler
)
2305 // Check for an existing handler of the type being added.
2306 if (FindHandler( handler
->GetType() ) == 0)
2308 sm_handlers
.Insert( handler
);
2312 // see AddHandler for additional comments.
2313 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2314 handler
->GetName().c_str() );
2319 bool wxImage::RemoveHandler( const wxString
& name
)
2321 wxImageHandler
*handler
= FindHandler(name
);
2324 sm_handlers
.DeleteObject(handler
);
2332 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2334 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2337 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2338 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2340 node
= node
->GetNext();
2345 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
2347 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2350 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2351 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2352 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
2354 node
= node
->GetNext();
2359 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
2361 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2364 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2365 if (handler
->GetType() == bitmapType
) return handler
;
2366 node
= node
->GetNext();
2371 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2373 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2376 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2377 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2378 node
= node
->GetNext();
2383 void wxImage::InitStandardHandlers()
2386 AddHandler(new wxBMPHandler
);
2387 #endif // wxUSE_STREAMS
2390 void wxImage::CleanUpHandlers()
2392 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2395 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2396 wxList::compatibility_iterator next
= node
->GetNext();
2401 sm_handlers
.Clear();
2404 wxString
wxImage::GetImageExtWildcard()
2408 wxList
& Handlers
= wxImage::GetHandlers();
2409 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2412 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2413 fmts
+= wxT("*.") + Handler
->GetExtension();
2414 Node
= Node
->GetNext();
2415 if ( Node
) fmts
+= wxT(";");
2418 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2421 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2423 const double red
= rgb
.red
/ 255.0,
2424 green
= rgb
.green
/ 255.0,
2425 blue
= rgb
.blue
/ 255.0;
2427 // find the min and max intensity (and remember which one was it for the
2429 double minimumRGB
= red
;
2430 if ( green
< minimumRGB
)
2432 if ( blue
< minimumRGB
)
2435 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2436 double maximumRGB
= red
;
2437 if ( green
> maximumRGB
)
2442 if ( blue
> maximumRGB
)
2448 const double value
= maximumRGB
;
2450 double hue
= 0.0, saturation
;
2451 const double deltaRGB
= maximumRGB
- minimumRGB
;
2452 if ( wxIsNullDouble(deltaRGB
) )
2454 // Gray has no color
2463 hue
= (green
- blue
) / deltaRGB
;
2467 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2471 hue
= 4.0 + (red
- green
) / deltaRGB
;
2475 wxFAIL_MSG(wxT("hue not specified"));
2484 saturation
= deltaRGB
/ maximumRGB
;
2487 return HSVValue(hue
, saturation
, value
);
2490 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2492 double red
, green
, blue
;
2494 if ( wxIsNullDouble(hsv
.saturation
) )
2503 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2504 int i
= (int)floor(hue
);
2505 double f
= hue
- i
; // fractional part of h
2506 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2512 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2517 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2525 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2530 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2535 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2543 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2548 return RGBValue((unsigned char)(red
* 255.0),
2549 (unsigned char)(green
* 255.0),
2550 (unsigned char)(blue
* 255.0));
2554 * Rotates the hue of each pixel of the image. angle is a double in the range
2555 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2557 void wxImage::RotateHue(double angle
)
2561 unsigned char *srcBytePtr
;
2562 unsigned char *dstBytePtr
;
2563 unsigned long count
;
2564 wxImage::HSVValue hsv
;
2565 wxImage::RGBValue rgb
;
2567 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2568 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2569 if ( count
> 0 && !wxIsNullDouble(angle
) )
2571 srcBytePtr
= M_IMGDATA
->m_data
;
2572 dstBytePtr
= srcBytePtr
;
2575 rgb
.red
= *srcBytePtr
++;
2576 rgb
.green
= *srcBytePtr
++;
2577 rgb
.blue
= *srcBytePtr
++;
2578 hsv
= RGBtoHSV(rgb
);
2580 hsv
.hue
= hsv
.hue
+ angle
;
2582 hsv
.hue
= hsv
.hue
- 1.0;
2583 else if (hsv
.hue
< 0.0)
2584 hsv
.hue
= hsv
.hue
+ 1.0;
2586 rgb
= HSVtoRGB(hsv
);
2587 *dstBytePtr
++ = rgb
.red
;
2588 *dstBytePtr
++ = rgb
.green
;
2589 *dstBytePtr
++ = rgb
.blue
;
2590 } while (--count
!= 0);
2594 //-----------------------------------------------------------------------------
2596 //-----------------------------------------------------------------------------
2598 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2601 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2606 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2611 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2616 bool wxImageHandler::CanRead( const wxString
& name
)
2618 if (wxFileExists(name
))
2620 wxImageFileInputStream
stream(name
);
2621 return CanRead(stream
);
2624 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2629 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2631 wxFileOffset posOld
= stream
.TellI();
2632 if ( posOld
== wxInvalidOffset
)
2634 // can't test unseekable stream
2638 bool ok
= DoCanRead(stream
);
2640 // restore the old position to be able to test other formats and so on
2641 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2643 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2645 // reading would fail anyhow as we're not at the right position
2652 #endif // wxUSE_STREAMS
2654 // ----------------------------------------------------------------------------
2655 // image histogram stuff
2656 // ----------------------------------------------------------------------------
2659 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2664 unsigned char g2
) const
2666 unsigned long key
= MakeKey(r2
, g2
, b2
);
2668 while ( find(key
) != end() )
2670 // color already used
2682 wxLogError(_("No unused colour in image.") );
2688 key
= MakeKey(r2
, g2
, b2
);
2702 wxImage::FindFirstUnusedColour(unsigned char *r
,
2707 unsigned char g2
) const
2709 wxImageHistogram histogram
;
2711 ComputeHistogram(histogram
);
2713 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2719 // Counts and returns the number of different colours. Optionally stops
2720 // when it exceeds 'stopafter' different colours. This is useful, for
2721 // example, to see if the image can be saved as 8-bit (256 colour or
2722 // less, in this case it would be invoked as CountColours(256)). Default
2723 // value for stopafter is -1 (don't care).
2725 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2729 unsigned char r
, g
, b
;
2731 unsigned long size
, nentries
, key
;
2734 size
= GetWidth() * GetHeight();
2737 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2742 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2744 if (h
.Get(key
) == NULL
)
2755 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2757 unsigned char *p
= GetData();
2758 unsigned long nentries
= 0;
2762 const unsigned long size
= GetWidth() * GetHeight();
2764 unsigned char r
, g
, b
;
2765 for ( unsigned long n
= 0; n
< size
; n
++ )
2771 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2773 if ( entry
.value
++ == 0 )
2774 entry
.index
= nentries
++;
2781 * Rotation code by Carlos Moreno
2784 static const double wxROTATE_EPSILON
= 1e-10;
2786 // Auxiliary function to rotate a point (x,y) with respect to point p0
2787 // make it inline and use a straight return to facilitate optimization
2788 // also, the function receives the sine and cosine of the angle to avoid
2789 // repeating the time-consuming calls to these functions -- sin/cos can
2790 // be computed and stored in the calling function.
2792 static inline wxRealPoint
2793 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2794 const wxRealPoint
& p0
)
2796 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2797 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2800 static inline wxRealPoint
2801 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2802 const wxRealPoint
& p0
)
2804 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2807 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2810 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2812 bool has_alpha
= HasAlpha();
2814 // Create pointer-based array to accelerate access to wxImage's data
2815 unsigned char ** data
= new unsigned char * [GetHeight()];
2816 data
[0] = GetData();
2817 for (i
= 1; i
< GetHeight(); i
++)
2818 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2820 // Same for alpha channel
2821 unsigned char ** alpha
= NULL
;
2824 alpha
= new unsigned char * [GetHeight()];
2825 alpha
[0] = GetAlpha();
2826 for (i
= 1; i
< GetHeight(); i
++)
2827 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2830 // precompute coefficients for rotation formula
2831 // (sine and cosine of the angle)
2832 const double cos_angle
= cos(angle
);
2833 const double sin_angle
= sin(angle
);
2835 // Create new Image to store the result
2836 // First, find rectangle that covers the rotated image; to do that,
2837 // rotate the four corners
2839 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2841 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2842 wxRealPoint p2
= wxRotatePoint (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2843 wxRealPoint p3
= wxRotatePoint (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2844 wxRealPoint p4
= wxRotatePoint (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2846 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2847 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2848 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2849 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2851 // Create rotated image
2852 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2853 // With alpha channel
2857 if (offset_after_rotation
!= NULL
)
2859 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2862 // GRG: The rotated (destination) image is always accessed
2863 // sequentially, so there is no need for a pointer-based
2864 // array here (and in fact it would be slower).
2866 unsigned char * dst
= rotated
.GetData();
2868 unsigned char * alpha_dst
= NULL
;
2870 alpha_dst
= rotated
.GetAlpha();
2872 // GRG: if the original image has a mask, use its RGB values
2873 // as the blank pixel, else, fall back to default (black).
2875 unsigned char blank_r
= 0;
2876 unsigned char blank_g
= 0;
2877 unsigned char blank_b
= 0;
2881 blank_r
= GetMaskRed();
2882 blank_g
= GetMaskGreen();
2883 blank_b
= GetMaskBlue();
2884 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2887 // Now, for each point of the rotated image, find where it came from, by
2888 // performing an inverse rotation (a rotation of -angle) and getting the
2889 // pixel at those coordinates
2891 // GRG: I've taken the (interpolating) test out of the loops, so that
2892 // it is done only once, instead of repeating it for each pixel.
2897 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2899 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2901 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2903 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2904 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2906 // interpolate using the 4 enclosing grid-points. Those
2907 // points can be obtained using floor and ceiling of the
2908 // exact coordinates of the point
2911 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2913 x1
= wxRound(floor(src
.x
));
2914 x2
= wxRound(ceil(src
.x
));
2916 else // else means that x is near one of the borders (0 or width-1)
2918 x1
= x2
= wxRound (src
.x
);
2921 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2923 y1
= wxRound(floor(src
.y
));
2924 y2
= wxRound(ceil(src
.y
));
2928 y1
= y2
= wxRound (src
.y
);
2931 // get four points and the distances (square of the distance,
2932 // for efficiency reasons) for the interpolation formula
2934 // GRG: Do not calculate the points until they are
2935 // really needed -- this way we can calculate
2936 // just one, instead of four, if d1, d2, d3
2937 // or d4 are < wxROTATE_EPSILON
2939 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2940 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2941 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2942 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2944 // Now interpolate as a weighted average of the four surrounding
2945 // points, where the weights are the distances to each of those points
2947 // If the point is exactly at one point of the grid of the source
2948 // image, then don't interpolate -- just assign the pixel
2950 // d1,d2,d3,d4 are positive -- no need for abs()
2951 if (d1
< wxROTATE_EPSILON
)
2953 unsigned char *p
= data
[y1
] + (3 * x1
);
2959 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2961 else if (d2
< wxROTATE_EPSILON
)
2963 unsigned char *p
= data
[y1
] + (3 * x2
);
2969 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2971 else if (d3
< wxROTATE_EPSILON
)
2973 unsigned char *p
= data
[y2
] + (3 * x2
);
2979 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2981 else if (d4
< wxROTATE_EPSILON
)
2983 unsigned char *p
= data
[y2
] + (3 * x1
);
2989 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2993 // weights for the weighted average are proportional to the inverse of the distance
2994 unsigned char *v1
= data
[y1
] + (3 * x1
);
2995 unsigned char *v2
= data
[y1
] + (3 * x2
);
2996 unsigned char *v3
= data
[y2
] + (3 * x2
);
2997 unsigned char *v4
= data
[y2
] + (3 * x1
);
2999 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3003 *(dst
++) = (unsigned char)
3004 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3005 w3
* *(v3
++) + w4
* *(v4
++)) /
3006 (w1
+ w2
+ w3
+ w4
) );
3007 *(dst
++) = (unsigned char)
3008 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3009 w3
* *(v3
++) + w4
* *(v4
++)) /
3010 (w1
+ w2
+ w3
+ w4
) );
3011 *(dst
++) = (unsigned char)
3012 ( (w1
* *v1
+ w2
* *v2
+
3013 w3
* *v3
+ w4
* *v4
) /
3014 (w1
+ w2
+ w3
+ w4
) );
3018 v1
= alpha
[y1
] + (x1
);
3019 v2
= alpha
[y1
] + (x2
);
3020 v3
= alpha
[y2
] + (x2
);
3021 v4
= alpha
[y2
] + (x1
);
3023 *(alpha_dst
++) = (unsigned char)
3024 ( (w1
* *v1
+ w2
* *v2
+
3025 w3
* *v3
+ w4
* *v4
) /
3026 (w1
+ w2
+ w3
+ w4
) );
3042 else // not interpolating
3044 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
3046 for (x
= 0; x
< rotated
.GetWidth(); x
++)
3048 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3050 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3051 const int ys
= wxRound (src
.y
); // closest integer
3053 if (0 <= xs
&& xs
< GetWidth() &&
3054 0 <= ys
&& ys
< GetHeight())
3056 unsigned char *p
= data
[ys
] + (3 * xs
);
3062 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3071 *(alpha_dst
++) = 255;
3089 // A module to allow wxImage initialization/cleanup
3090 // without calling these functions from app.cpp or from
3091 // the user's application.
3093 class wxImageModule
: public wxModule
3095 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3098 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
3099 void OnExit() { wxImage::CleanUpHandlers(); };
3102 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3105 #endif // wxUSE_IMAGE