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"
35 #include "wx/xpmdecod.h"
41 // make the code compile with either wxFile*Stream or wxFFile*Stream:
42 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
49 typedef wxFFileInputStream wxImageFileInputStream
;
50 typedef wxFFileOutputStream wxImageFileOutputStream
;
51 #endif // wxUSE_FILE/wxUSE_FFILE
52 #endif // HAS_FILE_STREAMS
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 class wxImageRefData
: public wxObjectRefData
62 virtual ~wxImageRefData();
66 unsigned char *m_data
;
69 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
71 // alpha channel data, may be NULL for the formats without alpha support
72 unsigned char *m_alpha
;
76 // if true, m_data is pointer to static data and shouldn't be freed
79 // same as m_static but for m_alpha
84 #endif // wxUSE_PALETTE
86 wxArrayString m_optionNames
;
87 wxArrayString m_optionValues
;
89 DECLARE_NO_COPY_CLASS(wxImageRefData
)
92 wxImageRefData::wxImageRefData()
97 m_alpha
= (unsigned char *) NULL
;
106 m_staticAlpha
= false;
109 wxImageRefData::~wxImageRefData()
113 if ( !m_staticAlpha
)
117 wxList
wxImage::sm_handlers
;
121 //-----------------------------------------------------------------------------
123 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
125 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
127 wxImage::wxImage( int width
, int height
, bool clear
)
129 Create( width
, height
, clear
);
132 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
134 Create( width
, height
, data
, static_data
);
137 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
139 Create( width
, height
, data
, alpha
, static_data
);
142 wxImage::wxImage( const wxString
& name
, long type
, int index
)
144 LoadFile( name
, type
, index
);
147 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
149 LoadFile( name
, mimetype
, index
);
153 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
155 LoadFile( stream
, type
, index
);
158 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
160 LoadFile( stream
, mimetype
, index
);
162 #endif // wxUSE_STREAMS
164 wxImage::wxImage( const char** xpmData
)
169 wxImage::wxImage( char** xpmData
)
171 Create((const char**) xpmData
);
174 bool wxImage::Create( const char** xpmData
)
179 wxXPMDecoder decoder
;
180 (*this) = decoder
.ReadData(xpmData
);
187 bool wxImage::Create( int width
, int height
, bool clear
)
191 m_refData
= new wxImageRefData();
193 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
194 if (!M_IMGDATA
->m_data
)
201 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
203 M_IMGDATA
->m_width
= width
;
204 M_IMGDATA
->m_height
= height
;
205 M_IMGDATA
->m_ok
= true;
210 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
214 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
216 m_refData
= new wxImageRefData();
218 M_IMGDATA
->m_data
= data
;
219 M_IMGDATA
->m_width
= width
;
220 M_IMGDATA
->m_height
= height
;
221 M_IMGDATA
->m_ok
= true;
222 M_IMGDATA
->m_static
= static_data
;
227 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
231 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
233 m_refData
= new wxImageRefData();
235 M_IMGDATA
->m_data
= data
;
236 M_IMGDATA
->m_alpha
= alpha
;
237 M_IMGDATA
->m_width
= width
;
238 M_IMGDATA
->m_height
= height
;
239 M_IMGDATA
->m_ok
= true;
240 M_IMGDATA
->m_static
= static_data
;
245 void wxImage::Destroy()
250 wxObjectRefData
* wxImage::CreateRefData() const
252 return new wxImageRefData
;
255 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
257 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
258 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
260 wxImageRefData
* refData_new
= new wxImageRefData
;
261 refData_new
->m_width
= refData
->m_width
;
262 refData_new
->m_height
= refData
->m_height
;
263 refData_new
->m_maskRed
= refData
->m_maskRed
;
264 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
265 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
266 refData_new
->m_hasMask
= refData
->m_hasMask
;
267 refData_new
->m_ok
= true;
268 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
269 if (refData
->m_alpha
!= NULL
)
271 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
272 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
275 refData_new
->m_data
= (unsigned char*)malloc(size
);
276 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
278 refData_new
->m_palette
= refData
->m_palette
;
280 refData_new
->m_optionNames
= refData
->m_optionNames
;
281 refData_new
->m_optionValues
= refData
->m_optionValues
;
285 wxImage
wxImage::Copy() const
289 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
291 image
.m_refData
= CloneRefData(m_refData
);
296 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
298 if( xFactor
== 1 && yFactor
== 1 )
303 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
305 // can't scale to/from 0 size
306 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
307 wxT("invalid new image size") );
309 long old_height
= M_IMGDATA
->m_height
,
310 old_width
= M_IMGDATA
->m_width
;
312 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
313 wxT("invalid old image size") );
315 long width
= old_width
/ xFactor
;
316 long height
= old_height
/ yFactor
;
318 image
.Create( width
, height
, false );
320 char unsigned *data
= image
.GetData();
322 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
324 bool hasMask
= false ;
325 unsigned char maskRed
= 0;
326 unsigned char maskGreen
= 0;
327 unsigned char maskBlue
=0 ;
329 unsigned char *source_data
= M_IMGDATA
->m_data
;
330 unsigned char *target_data
= data
;
331 unsigned char *source_alpha
= 0 ;
332 unsigned char *target_alpha
= 0 ;
333 if (M_IMGDATA
->m_hasMask
)
336 maskRed
= M_IMGDATA
->m_maskRed
;
337 maskGreen
= M_IMGDATA
->m_maskGreen
;
338 maskBlue
=M_IMGDATA
->m_maskBlue
;
340 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
341 M_IMGDATA
->m_maskGreen
,
342 M_IMGDATA
->m_maskBlue
);
346 source_alpha
= M_IMGDATA
->m_alpha
;
350 target_alpha
= image
.GetAlpha() ;
354 for (long y
= 0; y
< height
; y
++)
356 for (long x
= 0; x
< width
; x
++)
358 unsigned long avgRed
= 0 ;
359 unsigned long avgGreen
= 0;
360 unsigned long avgBlue
= 0;
361 unsigned long avgAlpha
= 0 ;
362 unsigned long counter
= 0 ;
364 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
366 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
367 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
369 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
370 unsigned char red
= pixel
[0] ;
371 unsigned char green
= pixel
[1] ;
372 unsigned char blue
= pixel
[2] ;
373 unsigned char alpha
= 255 ;
375 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
376 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
391 *(target_data
++) = M_IMGDATA
->m_maskRed
;
392 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
393 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
398 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
399 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
400 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
401 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
406 // In case this is a cursor, make sure the hotspot is scaled accordingly:
407 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
408 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
409 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
410 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
411 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
412 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
417 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
421 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
423 // can't scale to/from 0 size
424 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
425 wxT("invalid new image size") );
427 long old_height
= M_IMGDATA
->m_height
,
428 old_width
= M_IMGDATA
->m_width
;
429 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
430 wxT("invalid old image size") );
432 // If the image's new width and height are the same as the original, no
433 // need to waste time or CPU cycles
434 if ( old_width
== width
&& old_height
== height
)
437 // Scale the image (...or more appropriately, resample the image) using
438 // either the high-quality or normal method as specified
439 if ( quality
== wxIMAGE_QUALITY_HIGH
)
441 // We need to check whether we are downsampling or upsampling the image
442 if ( width
< old_width
&& height
< old_height
)
444 // Downsample the image using the box averaging method for best results
445 image
= ResampleBox(width
, height
);
449 // For upsampling or other random/wierd image dimensions we'll use
450 // a bicubic b-spline scaling method
451 image
= ResampleBicubic(width
, height
);
454 else // Default scaling method == simple pixel replication
456 if ( old_width
% width
== 0 && old_width
>= width
&&
457 old_height
% height
== 0 && old_height
>= height
)
459 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
461 image
.Create( width
, height
, false );
463 unsigned char *data
= image
.GetData();
465 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
467 unsigned char *source_data
= M_IMGDATA
->m_data
;
468 unsigned char *target_data
= data
;
469 unsigned char *source_alpha
= 0 ;
470 unsigned char *target_alpha
= 0 ;
472 if (M_IMGDATA
->m_hasMask
)
474 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
475 M_IMGDATA
->m_maskGreen
,
476 M_IMGDATA
->m_maskBlue
);
480 source_alpha
= M_IMGDATA
->m_alpha
;
484 target_alpha
= image
.GetAlpha() ;
488 long x_delta
= (old_width
<<16) / width
;
489 long y_delta
= (old_height
<<16) / height
;
491 unsigned char* dest_pixel
= target_data
;
494 for ( long j
= 0; j
< height
; j
++ )
496 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
497 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
500 for ( long i
= 0; i
< width
; i
++ )
502 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
503 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
504 dest_pixel
[0] = src_pixel
[0];
505 dest_pixel
[1] = src_pixel
[1];
506 dest_pixel
[2] = src_pixel
[2];
509 *(target_alpha
++) = *src_alpha_pixel
;
517 // In case this is a cursor, make sure the hotspot is scaled accordingly:
518 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
519 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
520 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
521 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
522 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
523 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
528 wxImage
wxImage::ResampleBox(int width
, int height
) const
530 // This function implements a simple pre-blur/box averaging method for
531 // downsampling that gives reasonably smooth results To scale the image
532 // down we will need to gather a grid of pixels of the size of the scale
533 // factor in each direction and then do an averaging of the pixels.
535 wxImage
ret_image(width
, height
, false);
537 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
538 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
540 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
541 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
543 // If we want good-looking results we need to pre-blur the image a bit first
544 wxImage
src_image(*this);
545 src_image
= src_image
.BlurHorizontal(scale_factor_x_2
);
546 src_image
= src_image
.BlurVertical(scale_factor_y_2
);
548 unsigned char* src_data
= src_image
.GetData();
549 unsigned char* src_alpha
= src_image
.GetAlpha();
550 unsigned char* dst_data
= ret_image
.GetData();
551 unsigned char* dst_alpha
= NULL
;
555 ret_image
.SetAlpha();
556 dst_alpha
= ret_image
.GetAlpha();
559 int averaged_pixels
, src_pixel_index
;
560 double sum_r
, sum_g
, sum_b
, sum_a
;
562 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
564 // Source pixel in the Y direction
565 int src_y
= (int)(y
* scale_factor_y
);
567 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
569 // Source pixel in the X direction
570 int src_x
= (int)(x
* scale_factor_x
);
572 // Box of pixels to average
574 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
576 for ( int j
= src_y
- scale_factor_y_2
+ 1;
577 j
<= int(src_y
+ scale_factor_y_2
);
580 // We don't care to average pixels that don't exist (edges)
581 if ( j
< 0 || j
> M_IMGDATA
->m_height
)
584 for ( int i
= src_x
- scale_factor_x_2
+ 1;
585 i
<= src_x
+ scale_factor_x_2
;
588 // Don't average edge pixels
589 if ( i
< 0 || i
> M_IMGDATA
->m_width
)
592 // Calculate the actual index in our source pixels
593 src_pixel_index
= src_y
* M_IMGDATA
->m_width
+ src_x
;
595 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
596 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
597 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
599 sum_a
+= src_alpha
[src_pixel_index
];
605 // Calculate the average from the sum and number of averaged pixels
606 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
607 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
608 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
611 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
618 // The following two local functions are for the B-spline weighting of the
619 // bicubic sampling algorithm
620 static inline double spline_cube(double value
)
622 return value
<= 0.0 ? 0.0 : value
* value
* value
;
625 static inline double spline_weight(double value
)
627 return (spline_cube(value
+ 2) -
628 4 * spline_cube(value
+ 1) +
629 6 * spline_cube(value
) -
630 4 * spline_cube(value
- 1)) / 6;
633 // This is the bicubic resampling algorithm
634 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
636 // This function implements a Bicubic B-Spline algorithm for resampling.
637 // This method is certainly a little slower than wxImage's default pixel
638 // replication method, however for most reasonably sized images not being
639 // upsampled too much on a fairly average CPU this difference is hardly
640 // noticeable and the results are far more pleasing to look at.
642 // This particular bicubic algorithm does pixel weighting according to a
643 // B-Spline that basically implements a Gaussian bell-like weighting
644 // kernel. Because of this method the results may appear a bit blurry when
645 // upsampling by large factors. This is basically because a slight
646 // gaussian blur is being performed to get the smooth look of the upsampled
649 // Edge pixels: 3-4 possible solutions
650 // - (Wrap/tile) Wrap the image, take the color value from the opposite
651 // side of the image.
652 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
653 // where n is nonpositive, will have the value of (2, 1).
654 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
655 // pixels which do have all neighbours.
656 // - (Clamp) Choose the nearest pixel along the border. This takes the
657 // border pixels and extends them out to infinity.
659 // NOTE: below the y_offset and x_offset variables are being set for edge
660 // pixels using the "Mirror" method mentioned above
664 ret_image
.Create(width
, height
, false);
666 unsigned char* src_data
= M_IMGDATA
->m_data
;
667 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
668 unsigned char* dst_data
= ret_image
.GetData();
669 unsigned char* dst_alpha
= NULL
;
673 ret_image
.SetAlpha();
674 dst_alpha
= ret_image
.GetAlpha();
677 for ( int dsty
= 0; dsty
< height
; dsty
++ )
679 // We need to calculate the source pixel to interpolate from - Y-axis
680 double srcpixy
= dsty
* M_IMGDATA
->m_height
/ height
;
681 double dy
= srcpixy
- (int)srcpixy
;
683 for ( int dstx
= 0; dstx
< width
; dstx
++ )
685 // X-axis of pixel to interpolate from
686 double srcpixx
= dstx
* M_IMGDATA
->m_width
/ width
;
687 double dx
= srcpixx
- (int)srcpixx
;
689 // Sums for each color channel
690 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
692 // Here we actually determine the RGBA values for the destination pixel
693 for ( int k
= -1; k
<= 2; k
++ )
696 int y_offset
= srcpixy
+ k
< 0.0
698 : srcpixy
+ k
>= M_IMGDATA
->m_height
699 ? M_IMGDATA
->m_height
- 1
700 : (int)(srcpixy
+ k
);
702 // Loop across the X axis
703 for ( int i
= -1; i
<= 2; i
++ )
706 int x_offset
= srcpixx
+ i
< 0.0
708 : srcpixx
+ i
>= M_IMGDATA
->m_width
709 ? M_IMGDATA
->m_width
- 1
710 : (int)(srcpixx
+ i
);
712 // Calculate the exact position where the source data
713 // should be pulled from based on the x_offset and y_offset
714 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
716 // Calculate the weight for the specified pixel according
717 // to the bicubic b-spline kernel we're using for
720 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
722 // Create a sum of all velues for each color channel
723 // adjusted for the pixel's calculated weight
724 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
725 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
726 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
728 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
732 // Put the data into the destination image. The summed values are
733 // of double data type and are rounded here for accuracy
734 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
735 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
736 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
740 *dst_alpha
++ = (unsigned char)sum_a
;
747 // Blur in the horizontal direction
748 wxImage
wxImage::BlurHorizontal(int blurRadius
)
751 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
753 unsigned char* src_data
= M_IMGDATA
->m_data
;
754 unsigned char* dst_data
= ret_image
.GetData();
755 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
756 unsigned char* dst_alpha
= NULL
;
758 // Check for a mask or alpha
759 if ( M_IMGDATA
->m_hasMask
)
761 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
762 M_IMGDATA
->m_maskGreen
,
763 M_IMGDATA
->m_maskBlue
);
769 ret_image
.SetAlpha();
770 dst_alpha
= ret_image
.GetAlpha();
774 // number of pixels we average over
775 const int blurArea
= blurRadius
*2 + 1;
777 // Horizontal blurring algorithm - average all pixels in the specified blur
778 // radius in the X or horizontal direction
779 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
781 // Variables used in the blurring algorithm
788 const unsigned char *src
;
791 // Calculate the average of all pixels in the blur radius for the first
793 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
795 // To deal with the pixels at the start of a row so it's not
796 // grabbing GOK values from memory at negative indices of the
797 // image's data or grabbing from the previous row
799 pixel_idx
= y
* M_IMGDATA
->m_width
;
801 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
803 src
= src_data
+ pixel_idx
*3;
808 sum_a
+= src_alpha
[pixel_idx
];
811 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
812 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
813 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
814 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
816 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
818 // Now average the values of the rest of the pixels by just moving the
819 // blur radius box along the row
820 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
822 // Take care of edge pixels on the left edge by essentially
823 // duplicating the edge pixel
824 if ( x
- blurRadius
- 1 < 0 )
825 pixel_idx
= y
* M_IMGDATA
->m_width
;
827 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
829 // Subtract the value of the pixel at the left side of the blur
831 src
= src_data
+ pixel_idx
*3;
836 sum_a
-= src_alpha
[pixel_idx
];
838 // Take care of edge pixels on the right edge
839 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
840 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
842 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
844 // Add the value of the pixel being added to the end of our box
845 src
= src_data
+ pixel_idx
*3;
850 sum_a
+= src_alpha
[pixel_idx
];
852 // Save off the averaged data
853 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
;
854 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
855 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
856 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
858 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
865 // Blur in the vertical direction
866 wxImage
wxImage::BlurVertical(int blurRadius
)
869 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
871 unsigned char* src_data
= M_IMGDATA
->m_data
;
872 unsigned char* dst_data
= ret_image
.GetData();
873 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
874 unsigned char* dst_alpha
= NULL
;
876 // Check for a mask or alpha
877 if ( M_IMGDATA
->m_hasMask
)
879 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
880 M_IMGDATA
->m_maskGreen
,
881 M_IMGDATA
->m_maskBlue
);
887 ret_image
.SetAlpha();
888 dst_alpha
= ret_image
.GetAlpha();
892 // number of pixels we average over
893 const int blurArea
= blurRadius
*2 + 1;
895 // Vertical blurring algorithm - same as horizontal but switched the
896 // opposite direction
897 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
899 // Variables used in the blurring algorithm
906 const unsigned char *src
;
909 // Calculate the average of all pixels in our blur radius box for the
910 // first pixel of the column
911 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
913 // To deal with the pixels at the start of a column so it's not
914 // grabbing GOK values from memory at negative indices of the
915 // image's data or grabbing from the previous column
919 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
921 src
= src_data
+ pixel_idx
*3;
926 sum_a
+= src_alpha
[pixel_idx
];
929 dst
= dst_data
+ x
*3;
930 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
931 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
932 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
934 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
936 // Now average the values of the rest of the pixels by just moving the
937 // box along the column from top to bottom
938 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
940 // Take care of pixels that would be beyond the top edge by
941 // duplicating the top edge pixel for the column
942 if ( y
- blurRadius
- 1 < 0 )
945 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
947 // Subtract the value of the pixel at the top of our blur radius box
948 src
= src_data
+ pixel_idx
*3;
953 sum_a
-= src_alpha
[pixel_idx
];
955 // Take care of the pixels that would be beyond the bottom edge of
956 // the image similar to the top edge
957 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
958 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
960 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
962 // Add the value of the pixel being added to the end of our box
963 src
= src_data
+ pixel_idx
*3;
968 sum_a
+= src_alpha
[pixel_idx
];
970 // Save off the averaged data
971 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
972 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
973 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
974 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
976 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
983 // The new blur function
984 wxImage
wxImage::Blur(int blurRadius
)
987 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
989 // Blur the image in each direction
990 ret_image
= BlurHorizontal(blurRadius
);
991 ret_image
= ret_image
.BlurVertical(blurRadius
);
996 wxImage
wxImage::Rotate90( bool clockwise
) const
1000 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1002 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
1004 unsigned char *data
= image
.GetData();
1006 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1008 unsigned char *source_data
= M_IMGDATA
->m_data
;
1009 unsigned char *target_data
;
1010 unsigned char *alpha_data
= 0 ;
1011 unsigned char *source_alpha
= 0 ;
1012 unsigned char *target_alpha
= 0 ;
1014 if (M_IMGDATA
->m_hasMask
)
1016 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1020 source_alpha
= M_IMGDATA
->m_alpha
;
1024 alpha_data
= image
.GetAlpha() ;
1028 long height
= M_IMGDATA
->m_height
;
1029 long width
= M_IMGDATA
->m_width
;
1031 for (long j
= 0; j
< height
; j
++)
1033 for (long i
= 0; i
< width
; i
++)
1037 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1039 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1043 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1045 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1047 memcpy( target_data
, source_data
, 3 );
1052 memcpy( target_alpha
, source_alpha
, 1 );
1061 wxImage
wxImage::Mirror( bool horizontally
) const
1065 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1067 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1069 unsigned char *data
= image
.GetData();
1070 unsigned char *alpha
= NULL
;
1072 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1074 if (M_IMGDATA
->m_alpha
!= NULL
) {
1076 alpha
= image
.GetAlpha();
1077 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1080 if (M_IMGDATA
->m_hasMask
)
1081 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1083 long height
= M_IMGDATA
->m_height
;
1084 long width
= M_IMGDATA
->m_width
;
1086 unsigned char *source_data
= M_IMGDATA
->m_data
;
1087 unsigned char *target_data
;
1091 for (long j
= 0; j
< height
; j
++)
1094 target_data
= data
-3;
1095 for (long i
= 0; i
< width
; i
++)
1097 memcpy( target_data
, source_data
, 3 );
1105 // src_alpha starts at the first pixel and increases by 1 after each step
1106 // (a step here is the copy of the alpha value of one pixel)
1107 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1108 // dest_alpha starts just beyond the first line, decreases before each step,
1109 // and after each line is finished, increases by 2 widths (skipping the line
1110 // just copied and the line that will be copied next)
1111 unsigned char *dest_alpha
= alpha
+ width
;
1113 for (long jj
= 0; jj
< height
; ++jj
)
1115 for (long i
= 0; i
< width
; ++i
) {
1116 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1118 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1124 for (long i
= 0; i
< height
; i
++)
1126 target_data
= data
+ 3*width
*(height
-1-i
);
1127 memcpy( target_data
, source_data
, (size_t)3*width
);
1128 source_data
+= 3*width
;
1133 // src_alpha starts at the first pixel and increases by 1 width after each step
1134 // (a step here is the copy of the alpha channel of an entire line)
1135 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1136 // dest_alpha starts just beyond the last line (beyond the whole image)
1137 // and decreases by 1 width before each step
1138 unsigned char *dest_alpha
= alpha
+ width
* height
;
1140 for (long jj
= 0; jj
< height
; ++jj
)
1142 dest_alpha
-= width
;
1143 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1152 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1156 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1158 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1159 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1160 image
, wxT("invalid subimage size") );
1162 const int subwidth
= rect
.GetWidth();
1163 const int subheight
= rect
.GetHeight();
1165 image
.Create( subwidth
, subheight
, false );
1167 const unsigned char *src_data
= GetData();
1168 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1169 unsigned char *subdata
= image
.GetData();
1170 unsigned char *subalpha
= NULL
;
1172 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1174 if (src_alpha
!= NULL
) {
1176 subalpha
= image
.GetAlpha();
1177 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1180 if (M_IMGDATA
->m_hasMask
)
1181 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1183 const int width
= GetWidth();
1184 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1186 src_data
+= 3 * pixsoff
;
1187 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1189 for (long j
= 0; j
< subheight
; ++j
)
1191 memcpy( subdata
, src_data
, 3 * subwidth
);
1192 subdata
+= 3 * subwidth
;
1193 src_data
+= 3 * width
;
1194 if (subalpha
!= NULL
) {
1195 memcpy( subalpha
, src_alpha
, subwidth
);
1196 subalpha
+= subwidth
;
1204 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1205 int r_
, int g_
, int b_
) const
1209 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1210 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1212 int width
= GetWidth(), height
= GetHeight();
1213 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1215 unsigned char r
= (unsigned char)r_
;
1216 unsigned char g
= (unsigned char)g_
;
1217 unsigned char b
= (unsigned char)b_
;
1218 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1220 GetOrFindMaskColour( &r
, &g
, &b
);
1221 image
.SetMaskColour(r
, g
, b
);
1224 image
.SetRGB(wxRect(), r
, g
, b
);
1226 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1227 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1229 finalRect
.width
-= pos
.x
;
1231 finalRect
.height
-= pos
.y
;
1233 subRect
.Intersect(finalRect
);
1235 if (!subRect
.IsEmpty())
1237 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1238 image
.Paste(*this, pos
.x
, pos
.y
);
1240 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1246 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1248 wxCHECK_RET( Ok(), wxT("invalid image") );
1249 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1255 int width
= image
.GetWidth();
1256 int height
= image
.GetHeight();
1269 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1270 width
= M_IMGDATA
->m_width
- (x
+xx
);
1271 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1272 height
= M_IMGDATA
->m_height
- (y
+yy
);
1274 if (width
< 1) return;
1275 if (height
< 1) return;
1277 if ((!HasMask() && !image
.HasMask()) ||
1278 (HasMask() && !image
.HasMask()) ||
1279 ((HasMask() && image
.HasMask() &&
1280 (GetMaskRed()==image
.GetMaskRed()) &&
1281 (GetMaskGreen()==image
.GetMaskGreen()) &&
1282 (GetMaskBlue()==image
.GetMaskBlue()))))
1285 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1286 int source_step
= image
.GetWidth()*3;
1288 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1289 int target_step
= M_IMGDATA
->m_width
*3;
1290 for (int j
= 0; j
< height
; j
++)
1292 memcpy( target_data
, source_data
, width
);
1293 source_data
+= source_step
;
1294 target_data
+= target_step
;
1299 if (!HasMask() && image
.HasMask())
1301 unsigned char r
= image
.GetMaskRed();
1302 unsigned char g
= image
.GetMaskGreen();
1303 unsigned char b
= image
.GetMaskBlue();
1306 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1307 int source_step
= image
.GetWidth()*3;
1309 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1310 int target_step
= M_IMGDATA
->m_width
*3;
1312 for (int j
= 0; j
< height
; j
++)
1314 for (int i
= 0; i
< width
; i
+=3)
1316 if ((source_data
[i
] != r
) &&
1317 (source_data
[i
+1] != g
) &&
1318 (source_data
[i
+2] != b
))
1320 memcpy( target_data
+i
, source_data
+i
, 3 );
1323 source_data
+= source_step
;
1324 target_data
+= target_step
;
1329 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1330 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1332 wxCHECK_RET( Ok(), wxT("invalid image") );
1336 unsigned char *data
= GetData();
1338 const int w
= GetWidth();
1339 const int h
= GetHeight();
1341 for (int j
= 0; j
< h
; j
++)
1342 for (int i
= 0; i
< w
; i
++)
1344 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1354 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1358 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1360 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1362 unsigned char *dest
= image
.GetData();
1364 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1366 unsigned char *src
= M_IMGDATA
->m_data
;
1367 bool hasMask
= M_IMGDATA
->m_hasMask
;
1368 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1369 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1370 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1373 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1375 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1376 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1378 // don't modify the mask
1379 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1381 memcpy(dest
, src
, 3);
1385 // calculate the luma
1386 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1387 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1391 // copy the alpha channel, if any
1394 const size_t alphaSize
= GetWidth() * GetHeight();
1395 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1396 memcpy(alpha
, GetAlpha(), alphaSize
);
1398 image
.SetAlpha(alpha
);
1404 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1408 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1410 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1412 unsigned char *data
= image
.GetData();
1414 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1416 if (M_IMGDATA
->m_hasMask
)
1418 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1419 M_IMGDATA
->m_maskBlue
== b
)
1420 image
.SetMaskColour( 255, 255, 255 );
1422 image
.SetMaskColour( 0, 0, 0 );
1425 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1427 unsigned char *srcd
= M_IMGDATA
->m_data
;
1428 unsigned char *tard
= image
.GetData();
1430 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1432 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1433 tard
[0] = tard
[1] = tard
[2] = 255;
1435 tard
[0] = tard
[1] = tard
[2] = 0;
1441 int wxImage::GetWidth() const
1443 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1445 return M_IMGDATA
->m_width
;
1448 int wxImage::GetHeight() const
1450 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1452 return M_IMGDATA
->m_height
;
1455 long wxImage::XYToIndex(int x
, int y
) const
1459 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1461 return y
*M_IMGDATA
->m_width
+ x
;
1467 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1469 long pos
= XYToIndex(x
, y
);
1470 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1476 M_IMGDATA
->m_data
[ pos
] = r
;
1477 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1478 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1481 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1483 wxCHECK_RET( Ok(), wxT("invalid image") );
1488 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1489 if ( rect
== wxRect() )
1495 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1496 imageRect
.Contains(rect
.GetBottomRight()),
1497 wxT("invalid bounding rectangle") );
1500 int x1
= rect
.GetLeft(),
1502 x2
= rect
.GetRight() + 1,
1503 y2
= rect
.GetBottom() + 1;
1505 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1506 int x
, y
, width
= GetWidth();
1507 for (y
= y1
; y
< y2
; y
++)
1509 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1510 for (x
= x1
; x
< x2
; x
++)
1519 unsigned char wxImage::GetRed( int x
, int y
) const
1521 long pos
= XYToIndex(x
, y
);
1522 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1526 return M_IMGDATA
->m_data
[pos
];
1529 unsigned char wxImage::GetGreen( int x
, int y
) const
1531 long pos
= XYToIndex(x
, y
);
1532 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1536 return M_IMGDATA
->m_data
[pos
+1];
1539 unsigned char wxImage::GetBlue( int x
, int y
) const
1541 long pos
= XYToIndex(x
, y
);
1542 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1546 return M_IMGDATA
->m_data
[pos
+2];
1549 bool wxImage::Ok() const
1551 // image of 0 width or height can't be considered ok - at least because it
1552 // causes crashes in ConvertToBitmap() if we don't catch it in time
1553 wxImageRefData
*data
= M_IMGDATA
;
1554 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1557 unsigned char *wxImage::GetData() const
1559 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1561 return M_IMGDATA
->m_data
;
1564 void wxImage::SetData( unsigned char *data
, bool static_data
)
1566 wxCHECK_RET( Ok(), wxT("invalid image") );
1568 wxImageRefData
*newRefData
= new wxImageRefData();
1570 newRefData
->m_width
= M_IMGDATA
->m_width
;
1571 newRefData
->m_height
= M_IMGDATA
->m_height
;
1572 newRefData
->m_data
= data
;
1573 newRefData
->m_ok
= true;
1574 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1575 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1576 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1577 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1578 newRefData
->m_static
= static_data
;
1582 m_refData
= newRefData
;
1585 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1587 wxImageRefData
*newRefData
= new wxImageRefData();
1591 newRefData
->m_width
= new_width
;
1592 newRefData
->m_height
= new_height
;
1593 newRefData
->m_data
= data
;
1594 newRefData
->m_ok
= true;
1595 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1596 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1597 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1598 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1602 newRefData
->m_width
= new_width
;
1603 newRefData
->m_height
= new_height
;
1604 newRefData
->m_data
= data
;
1605 newRefData
->m_ok
= true;
1607 newRefData
->m_static
= static_data
;
1611 m_refData
= newRefData
;
1614 // ----------------------------------------------------------------------------
1615 // alpha channel support
1616 // ----------------------------------------------------------------------------
1618 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1620 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1622 long pos
= XYToIndex(x
, y
);
1623 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1627 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1630 unsigned char wxImage::GetAlpha(int x
, int y
) const
1632 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1634 long pos
= XYToIndex(x
, y
);
1635 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1637 return M_IMGDATA
->m_alpha
[pos
];
1641 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1645 const int w
= M_IMGDATA
->m_width
;
1646 const int h
= M_IMGDATA
->m_height
;
1648 unsigned char *alpha
= GetAlpha();
1649 unsigned char *data
= GetData();
1651 for ( int y
= 0; y
< h
; y
++ )
1653 for ( int x
= 0; x
< w
; x
++ )
1665 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1667 wxCHECK_RET( Ok(), wxT("invalid image") );
1673 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1676 free(M_IMGDATA
->m_alpha
);
1677 M_IMGDATA
->m_alpha
= alpha
;
1678 M_IMGDATA
->m_staticAlpha
= static_data
;
1681 unsigned char *wxImage::GetAlpha() const
1683 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1685 return M_IMGDATA
->m_alpha
;
1688 void wxImage::InitAlpha()
1690 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1692 // initialize memory for alpha channel
1695 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1696 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1700 // use the mask to initialize the alpha channel.
1701 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1703 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1704 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1705 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1706 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1710 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1711 ? wxIMAGE_ALPHA_TRANSPARENT
1712 : wxIMAGE_ALPHA_OPAQUE
;
1715 M_IMGDATA
->m_hasMask
= false;
1719 // make the image fully opaque
1720 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1724 // ----------------------------------------------------------------------------
1726 // ----------------------------------------------------------------------------
1728 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1730 wxCHECK_RET( Ok(), wxT("invalid image") );
1734 M_IMGDATA
->m_maskRed
= r
;
1735 M_IMGDATA
->m_maskGreen
= g
;
1736 M_IMGDATA
->m_maskBlue
= b
;
1737 M_IMGDATA
->m_hasMask
= true;
1740 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1742 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1744 if (M_IMGDATA
->m_hasMask
)
1746 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1747 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1748 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1753 FindFirstUnusedColour(r
, g
, b
);
1758 unsigned char wxImage::GetMaskRed() const
1760 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1762 return M_IMGDATA
->m_maskRed
;
1765 unsigned char wxImage::GetMaskGreen() const
1767 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1769 return M_IMGDATA
->m_maskGreen
;
1772 unsigned char wxImage::GetMaskBlue() const
1774 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1776 return M_IMGDATA
->m_maskBlue
;
1779 void wxImage::SetMask( bool mask
)
1781 wxCHECK_RET( Ok(), wxT("invalid image") );
1785 M_IMGDATA
->m_hasMask
= mask
;
1788 bool wxImage::HasMask() const
1790 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1792 return M_IMGDATA
->m_hasMask
;
1795 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1797 long pos
= XYToIndex(x
, y
);
1798 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1801 if ( M_IMGDATA
->m_hasMask
)
1803 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1804 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1805 p
[1] == M_IMGDATA
->m_maskGreen
&&
1806 p
[2] == M_IMGDATA
->m_maskBlue
)
1813 if ( M_IMGDATA
->m_alpha
)
1815 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1817 // transparent enough
1826 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1827 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1829 // check that the images are the same size
1830 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1832 wxLogError( _("Image and mask have different sizes.") );
1836 // find unused colour
1837 unsigned char r
,g
,b
;
1838 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1840 wxLogError( _("No unused colour in image being masked.") );
1846 unsigned char *imgdata
= GetData();
1847 unsigned char *maskdata
= mask
.GetData();
1849 const int w
= GetWidth();
1850 const int h
= GetHeight();
1852 for (int j
= 0; j
< h
; j
++)
1854 for (int i
= 0; i
< w
; i
++)
1856 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1867 SetMaskColour(r
, g
, b
);
1873 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1878 unsigned char mr
, mg
, mb
;
1879 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1881 wxLogError( _("No unused colour in image being masked.") );
1888 SetMaskColour(mr
, mg
, mb
);
1890 unsigned char *imgdata
= GetData();
1891 unsigned char *alphadata
= GetAlpha();
1894 int h
= GetHeight();
1896 for (int y
= 0; y
< h
; y
++)
1898 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1900 if (*alphadata
< threshold
)
1909 free(M_IMGDATA
->m_alpha
);
1910 M_IMGDATA
->m_alpha
= NULL
;
1915 // ----------------------------------------------------------------------------
1916 // Palette functions
1917 // ----------------------------------------------------------------------------
1921 bool wxImage::HasPalette() const
1926 return M_IMGDATA
->m_palette
.Ok();
1929 const wxPalette
& wxImage::GetPalette() const
1931 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1933 return M_IMGDATA
->m_palette
;
1936 void wxImage::SetPalette(const wxPalette
& palette
)
1938 wxCHECK_RET( Ok(), wxT("invalid image") );
1942 M_IMGDATA
->m_palette
= palette
;
1945 #endif // wxUSE_PALETTE
1947 // ----------------------------------------------------------------------------
1948 // Option functions (arbitrary name/value mapping)
1949 // ----------------------------------------------------------------------------
1951 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1953 wxCHECK_RET( Ok(), wxT("invalid image") );
1957 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1958 if (idx
== wxNOT_FOUND
)
1960 M_IMGDATA
->m_optionNames
.Add(name
);
1961 M_IMGDATA
->m_optionValues
.Add(value
);
1965 M_IMGDATA
->m_optionNames
[idx
] = name
;
1966 M_IMGDATA
->m_optionValues
[idx
] = value
;
1970 void wxImage::SetOption(const wxString
& name
, int value
)
1973 valStr
.Printf(wxT("%d"), value
);
1974 SetOption(name
, valStr
);
1977 wxString
wxImage::GetOption(const wxString
& name
) const
1979 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1981 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1982 if (idx
== wxNOT_FOUND
)
1983 return wxEmptyString
;
1985 return M_IMGDATA
->m_optionValues
[idx
];
1988 int wxImage::GetOptionInt(const wxString
& name
) const
1990 return wxAtoi(GetOption(name
));
1993 bool wxImage::HasOption(const wxString
& name
) const
1995 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1997 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
2000 // ----------------------------------------------------------------------------
2002 // ----------------------------------------------------------------------------
2004 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2005 long WXUNUSED_UNLESS_STREAMS(type
),
2006 int WXUNUSED_UNLESS_STREAMS(index
) )
2008 #if HAS_FILE_STREAMS
2009 if (wxFileExists(filename
))
2011 wxImageFileInputStream
stream(filename
);
2012 wxBufferedInputStream
bstream( stream
);
2013 return LoadFile(bstream
, type
, index
);
2017 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2021 #else // !HAS_FILE_STREAMS
2023 #endif // HAS_FILE_STREAMS
2026 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2027 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2028 int WXUNUSED_UNLESS_STREAMS(index
) )
2030 #if HAS_FILE_STREAMS
2031 if (wxFileExists(filename
))
2033 wxImageFileInputStream
stream(filename
);
2034 wxBufferedInputStream
bstream( stream
);
2035 return LoadFile(bstream
, mimetype
, index
);
2039 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2043 #else // !HAS_FILE_STREAMS
2045 #endif // HAS_FILE_STREAMS
2050 bool wxImage::SaveFile( const wxString
& filename
) const
2052 wxString ext
= filename
.AfterLast('.').Lower();
2054 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
2057 SaveFile(filename
, pHandler
->GetType());
2061 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
2066 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2067 int WXUNUSED_UNLESS_STREAMS(type
) ) const
2069 #if HAS_FILE_STREAMS
2070 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2072 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2074 wxImageFileOutputStream
stream(filename
);
2076 if ( stream
.IsOk() )
2078 wxBufferedOutputStream
bstream( stream
);
2079 return SaveFile(bstream
, type
);
2081 #endif // HAS_FILE_STREAMS
2086 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2087 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2089 #if HAS_FILE_STREAMS
2090 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2092 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2094 wxImageFileOutputStream
stream(filename
);
2096 if ( stream
.IsOk() )
2098 wxBufferedOutputStream
bstream( stream
);
2099 return SaveFile(bstream
, mimetype
);
2101 #endif // HAS_FILE_STREAMS
2106 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2108 #if HAS_FILE_STREAMS
2109 wxImageFileInputStream
stream(name
);
2110 return CanRead(stream
);
2116 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2117 long WXUNUSED_UNLESS_STREAMS(type
) )
2119 #if HAS_FILE_STREAMS
2120 wxImageFileInputStream
stream(name
);
2122 return GetImageCount(stream
, type
);
2130 bool wxImage::CanRead( wxInputStream
&stream
)
2132 const wxList
& list
= GetHandlers();
2134 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2136 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2137 if (handler
->CanRead( stream
))
2144 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
2146 wxImageHandler
*handler
;
2148 if ( type
== wxBITMAP_TYPE_ANY
)
2150 wxList
&list
=GetHandlers();
2152 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2154 handler
=(wxImageHandler
*)node
->GetData();
2155 if ( handler
->CanRead(stream
) )
2156 return handler
->GetImageCount(stream
);
2160 wxLogWarning(_("No handler found for image type."));
2164 handler
= FindHandler(type
);
2168 wxLogWarning(_("No image handler for type %ld defined."), type
);
2172 if ( handler
->CanRead(stream
) )
2174 return handler
->GetImageCount(stream
);
2178 wxLogError(_("Image file is not of type %ld."), type
);
2183 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
2187 m_refData
= new wxImageRefData
;
2189 wxImageHandler
*handler
;
2191 if ( type
== wxBITMAP_TYPE_ANY
)
2193 wxList
&list
=GetHandlers();
2195 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2197 handler
=(wxImageHandler
*)node
->GetData();
2198 if ( handler
->CanRead(stream
) )
2199 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2203 wxLogWarning( _("No handler found for image type.") );
2207 handler
= FindHandler(type
);
2211 wxLogWarning( _("No image handler for type %ld defined."), type
);
2216 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2218 wxLogError(_("Image file is not of type %ld."), type
);
2222 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2225 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2229 m_refData
= new wxImageRefData
;
2231 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2235 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2240 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2242 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
2246 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
2249 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
2251 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2253 wxImageHandler
*handler
= FindHandler(type
);
2256 wxLogWarning( _("No image handler for type %d defined."), type
);
2261 return handler
->SaveFile( (wxImage
*)this, stream
);
2264 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2266 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2268 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2271 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2276 return handler
->SaveFile( (wxImage
*)this, stream
);
2278 #endif // wxUSE_STREAMS
2280 // ----------------------------------------------------------------------------
2281 // image I/O handlers
2282 // ----------------------------------------------------------------------------
2284 void wxImage::AddHandler( wxImageHandler
*handler
)
2286 // Check for an existing handler of the type being added.
2287 if (FindHandler( handler
->GetType() ) == 0)
2289 sm_handlers
.Append( handler
);
2293 // This is not documented behaviour, merely the simplest 'fix'
2294 // for preventing duplicate additions. If someone ever has
2295 // a good reason to add and remove duplicate handlers (and they
2296 // may) we should probably refcount the duplicates.
2297 // also an issue in InsertHandler below.
2299 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2300 handler
->GetName().c_str() );
2305 void wxImage::InsertHandler( wxImageHandler
*handler
)
2307 // Check for an existing handler of the type being added.
2308 if (FindHandler( handler
->GetType() ) == 0)
2310 sm_handlers
.Insert( handler
);
2314 // see AddHandler for additional comments.
2315 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2316 handler
->GetName().c_str() );
2321 bool wxImage::RemoveHandler( const wxString
& name
)
2323 wxImageHandler
*handler
= FindHandler(name
);
2326 sm_handlers
.DeleteObject(handler
);
2334 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2336 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2339 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2340 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2342 node
= node
->GetNext();
2347 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
2349 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2352 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2353 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2354 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
2356 node
= node
->GetNext();
2361 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
2363 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2366 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2367 if (handler
->GetType() == bitmapType
) return handler
;
2368 node
= node
->GetNext();
2373 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2375 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2378 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2379 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2380 node
= node
->GetNext();
2385 void wxImage::InitStandardHandlers()
2388 AddHandler(new wxBMPHandler
);
2389 #endif // wxUSE_STREAMS
2392 void wxImage::CleanUpHandlers()
2394 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2397 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2398 wxList::compatibility_iterator next
= node
->GetNext();
2403 sm_handlers
.Clear();
2406 wxString
wxImage::GetImageExtWildcard()
2410 wxList
& Handlers
= wxImage::GetHandlers();
2411 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2414 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2415 fmts
+= wxT("*.") + Handler
->GetExtension();
2416 Node
= Node
->GetNext();
2417 if ( Node
) fmts
+= wxT(";");
2420 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2423 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2425 const double red
= rgb
.red
/ 255.0,
2426 green
= rgb
.green
/ 255.0,
2427 blue
= rgb
.blue
/ 255.0;
2429 // find the min and max intensity (and remember which one was it for the
2431 double minimumRGB
= red
;
2432 if ( green
< minimumRGB
)
2434 if ( blue
< minimumRGB
)
2437 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2438 double maximumRGB
= red
;
2439 if ( green
> maximumRGB
)
2444 if ( blue
> maximumRGB
)
2450 const double value
= maximumRGB
;
2452 double hue
= 0.0, saturation
;
2453 const double deltaRGB
= maximumRGB
- minimumRGB
;
2454 if ( wxIsNullDouble(deltaRGB
) )
2456 // Gray has no color
2465 hue
= (green
- blue
) / deltaRGB
;
2469 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2473 hue
= 4.0 + (red
- green
) / deltaRGB
;
2477 wxFAIL_MSG(wxT("hue not specified"));
2486 saturation
= deltaRGB
/ maximumRGB
;
2489 return HSVValue(hue
, saturation
, value
);
2492 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2494 double red
, green
, blue
;
2496 if ( wxIsNullDouble(hsv
.saturation
) )
2505 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2506 int i
= (int)floor(hue
);
2507 double f
= hue
- i
; // fractional part of h
2508 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2514 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2519 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2527 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2532 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2537 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2545 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2550 return RGBValue((unsigned char)(red
* 255.0),
2551 (unsigned char)(green
* 255.0),
2552 (unsigned char)(blue
* 255.0));
2556 * Rotates the hue of each pixel of the image. angle is a double in the range
2557 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2559 void wxImage::RotateHue(double angle
)
2563 unsigned char *srcBytePtr
;
2564 unsigned char *dstBytePtr
;
2565 unsigned long count
;
2566 wxImage::HSVValue hsv
;
2567 wxImage::RGBValue rgb
;
2569 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2570 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2571 if ( count
> 0 && !wxIsNullDouble(angle
) )
2573 srcBytePtr
= M_IMGDATA
->m_data
;
2574 dstBytePtr
= srcBytePtr
;
2577 rgb
.red
= *srcBytePtr
++;
2578 rgb
.green
= *srcBytePtr
++;
2579 rgb
.blue
= *srcBytePtr
++;
2580 hsv
= RGBtoHSV(rgb
);
2582 hsv
.hue
= hsv
.hue
+ angle
;
2584 hsv
.hue
= hsv
.hue
- 1.0;
2585 else if (hsv
.hue
< 0.0)
2586 hsv
.hue
= hsv
.hue
+ 1.0;
2588 rgb
= HSVtoRGB(hsv
);
2589 *dstBytePtr
++ = rgb
.red
;
2590 *dstBytePtr
++ = rgb
.green
;
2591 *dstBytePtr
++ = rgb
.blue
;
2592 } while (--count
!= 0);
2596 //-----------------------------------------------------------------------------
2598 //-----------------------------------------------------------------------------
2600 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2603 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2608 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2613 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2618 bool wxImageHandler::CanRead( const wxString
& name
)
2620 if (wxFileExists(name
))
2622 wxImageFileInputStream
stream(name
);
2623 return CanRead(stream
);
2626 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2631 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2633 wxFileOffset posOld
= stream
.TellI();
2634 if ( posOld
== wxInvalidOffset
)
2636 // can't test unseekable stream
2640 bool ok
= DoCanRead(stream
);
2642 // restore the old position to be able to test other formats and so on
2643 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2645 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2647 // reading would fail anyhow as we're not at the right position
2654 #endif // wxUSE_STREAMS
2656 // ----------------------------------------------------------------------------
2657 // image histogram stuff
2658 // ----------------------------------------------------------------------------
2661 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2666 unsigned char g2
) const
2668 unsigned long key
= MakeKey(r2
, g2
, b2
);
2670 while ( find(key
) != end() )
2672 // color already used
2684 wxLogError(_("No unused colour in image.") );
2690 key
= MakeKey(r2
, g2
, b2
);
2704 wxImage::FindFirstUnusedColour(unsigned char *r
,
2709 unsigned char g2
) const
2711 wxImageHistogram histogram
;
2713 ComputeHistogram(histogram
);
2715 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2721 // Counts and returns the number of different colours. Optionally stops
2722 // when it exceeds 'stopafter' different colours. This is useful, for
2723 // example, to see if the image can be saved as 8-bit (256 colour or
2724 // less, in this case it would be invoked as CountColours(256)). Default
2725 // value for stopafter is -1 (don't care).
2727 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2731 unsigned char r
, g
, b
;
2733 unsigned long size
, nentries
, key
;
2736 size
= GetWidth() * GetHeight();
2739 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2744 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2746 if (h
.Get(key
) == NULL
)
2757 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2759 unsigned char *p
= GetData();
2760 unsigned long nentries
= 0;
2764 const unsigned long size
= GetWidth() * GetHeight();
2766 unsigned char r
, g
, b
;
2767 for ( unsigned long n
= 0; n
< size
; n
++ )
2773 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2775 if ( entry
.value
++ == 0 )
2776 entry
.index
= nentries
++;
2783 * Rotation code by Carlos Moreno
2786 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2787 // does exactly the same thing. And I also got rid of wxRotationPixel
2788 // bacause of potential problems in architectures where alignment
2789 // is an issue, so I had to rewrite parts of the code.
2791 static const double gs_Epsilon
= 1e-10;
2793 static inline int wxCint (double x
)
2795 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2799 // Auxiliary function to rotate a point (x,y) with respect to point p0
2800 // make it inline and use a straight return to facilitate optimization
2801 // also, the function receives the sine and cosine of the angle to avoid
2802 // repeating the time-consuming calls to these functions -- sin/cos can
2803 // be computed and stored in the calling function.
2805 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2807 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2808 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2811 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2813 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2816 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2819 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2821 bool has_alpha
= HasAlpha();
2823 // Create pointer-based array to accelerate access to wxImage's data
2824 unsigned char ** data
= new unsigned char * [GetHeight()];
2825 data
[0] = GetData();
2826 for (i
= 1; i
< GetHeight(); i
++)
2827 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2829 // Same for alpha channel
2830 unsigned char ** alpha
= NULL
;
2833 alpha
= new unsigned char * [GetHeight()];
2834 alpha
[0] = GetAlpha();
2835 for (i
= 1; i
< GetHeight(); i
++)
2836 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2839 // precompute coefficients for rotation formula
2840 // (sine and cosine of the angle)
2841 const double cos_angle
= cos(angle
);
2842 const double sin_angle
= sin(angle
);
2844 // Create new Image to store the result
2845 // First, find rectangle that covers the rotated image; to do that,
2846 // rotate the four corners
2848 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2850 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2851 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2852 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2853 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2855 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2856 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2857 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2858 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2860 // Create rotated image
2861 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2862 // With alpha channel
2866 if (offset_after_rotation
!= NULL
)
2868 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2871 // GRG: The rotated (destination) image is always accessed
2872 // sequentially, so there is no need for a pointer-based
2873 // array here (and in fact it would be slower).
2875 unsigned char * dst
= rotated
.GetData();
2877 unsigned char * alpha_dst
= NULL
;
2879 alpha_dst
= rotated
.GetAlpha();
2881 // GRG: if the original image has a mask, use its RGB values
2882 // as the blank pixel, else, fall back to default (black).
2884 unsigned char blank_r
= 0;
2885 unsigned char blank_g
= 0;
2886 unsigned char blank_b
= 0;
2890 blank_r
= GetMaskRed();
2891 blank_g
= GetMaskGreen();
2892 blank_b
= GetMaskBlue();
2893 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2896 // Now, for each point of the rotated image, find where it came from, by
2897 // performing an inverse rotation (a rotation of -angle) and getting the
2898 // pixel at those coordinates
2900 // GRG: I've taken the (interpolating) test out of the loops, so that
2901 // it is done only once, instead of repeating it for each pixel.
2906 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2908 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2910 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2912 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2913 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2915 // interpolate using the 4 enclosing grid-points. Those
2916 // points can be obtained using floor and ceiling of the
2917 // exact coordinates of the point
2920 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2922 x1
= wxCint(floor(src
.x
));
2923 x2
= wxCint(ceil(src
.x
));
2925 else // else means that x is near one of the borders (0 or width-1)
2927 x1
= x2
= wxCint (src
.x
);
2930 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2932 y1
= wxCint(floor(src
.y
));
2933 y2
= wxCint(ceil(src
.y
));
2937 y1
= y2
= wxCint (src
.y
);
2940 // get four points and the distances (square of the distance,
2941 // for efficiency reasons) for the interpolation formula
2943 // GRG: Do not calculate the points until they are
2944 // really needed -- this way we can calculate
2945 // just one, instead of four, if d1, d2, d3
2946 // or d4 are < gs_Epsilon
2948 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2949 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2950 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2951 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2953 // Now interpolate as a weighted average of the four surrounding
2954 // points, where the weights are the distances to each of those points
2956 // If the point is exactly at one point of the grid of the source
2957 // image, then don't interpolate -- just assign the pixel
2959 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2961 unsigned char *p
= data
[y1
] + (3 * x1
);
2967 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2969 else if (d2
< gs_Epsilon
)
2971 unsigned char *p
= data
[y1
] + (3 * x2
);
2977 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2979 else if (d3
< gs_Epsilon
)
2981 unsigned char *p
= data
[y2
] + (3 * x2
);
2987 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2989 else if (d4
< gs_Epsilon
)
2991 unsigned char *p
= data
[y2
] + (3 * x1
);
2997 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3001 // weights for the weighted average are proportional to the inverse of the distance
3002 unsigned char *v1
= data
[y1
] + (3 * x1
);
3003 unsigned char *v2
= data
[y1
] + (3 * x2
);
3004 unsigned char *v3
= data
[y2
] + (3 * x2
);
3005 unsigned char *v4
= data
[y2
] + (3 * x1
);
3007 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3011 *(dst
++) = (unsigned char)
3012 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3013 w3
* *(v3
++) + w4
* *(v4
++)) /
3014 (w1
+ w2
+ w3
+ w4
) );
3015 *(dst
++) = (unsigned char)
3016 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3017 w3
* *(v3
++) + w4
* *(v4
++)) /
3018 (w1
+ w2
+ w3
+ w4
) );
3019 *(dst
++) = (unsigned char)
3020 ( (w1
* *v1
+ w2
* *v2
+
3021 w3
* *v3
+ w4
* *v4
) /
3022 (w1
+ w2
+ w3
+ w4
) );
3026 v1
= alpha
[y1
] + (x1
);
3027 v2
= alpha
[y1
] + (x2
);
3028 v3
= alpha
[y2
] + (x2
);
3029 v4
= alpha
[y2
] + (x1
);
3031 *(alpha_dst
++) = (unsigned char)
3032 ( (w1
* *v1
+ w2
* *v2
+
3033 w3
* *v3
+ w4
* *v4
) /
3034 (w1
+ w2
+ w3
+ w4
) );
3050 else // not interpolating
3052 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
3054 for (x
= 0; x
< rotated
.GetWidth(); x
++)
3056 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3058 const int xs
= wxCint (src
.x
); // wxCint rounds to the
3059 const int ys
= wxCint (src
.y
); // closest integer
3061 if (0 <= xs
&& xs
< GetWidth() &&
3062 0 <= ys
&& ys
< GetHeight())
3064 unsigned char *p
= data
[ys
] + (3 * xs
);
3070 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3079 *(alpha_dst
++) = 255;
3097 // A module to allow wxImage initialization/cleanup
3098 // without calling these functions from app.cpp or from
3099 // the user's application.
3101 class wxImageModule
: public wxModule
3103 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3106 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
3107 void OnExit() { wxImage::CleanUpHandlers(); };
3110 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3113 #endif // wxUSE_IMAGE