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 source_alpha
= M_IMGDATA
->m_alpha
;
478 target_alpha
= image
.GetAlpha() ;
482 long x_delta
= (old_width
<<16) / width
;
483 long y_delta
= (old_height
<<16) / height
;
485 unsigned char* dest_pixel
= target_data
;
488 for ( long j
= 0; j
< height
; j
++ )
490 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
491 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
494 for ( long i
= 0; i
< width
; i
++ )
496 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
497 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
498 dest_pixel
[0] = src_pixel
[0];
499 dest_pixel
[1] = src_pixel
[1];
500 dest_pixel
[2] = src_pixel
[2];
503 *(target_alpha
++) = *src_alpha_pixel
;
511 // If the original image has a mask, apply the mask to the new image
512 if (M_IMGDATA
->m_hasMask
)
514 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
515 M_IMGDATA
->m_maskGreen
,
516 M_IMGDATA
->m_maskBlue
);
519 // In case this is a cursor, make sure the hotspot is scaled accordingly:
520 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
521 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
522 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
523 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
524 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
525 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
530 wxImage
wxImage::ResampleBox(int width
, int height
) const
532 // This function implements a simple pre-blur/box averaging method for
533 // downsampling that gives reasonably smooth results To scale the image
534 // down we will need to gather a grid of pixels of the size of the scale
535 // factor in each direction and then do an averaging of the pixels.
537 wxImage
ret_image(width
, height
, false);
539 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
540 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
542 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
543 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
545 // If we want good-looking results we need to pre-blur the image a bit first
546 wxImage
src_image(*this);
547 src_image
= src_image
.BlurHorizontal(scale_factor_x_2
);
548 src_image
= src_image
.BlurVertical(scale_factor_y_2
);
550 unsigned char* src_data
= src_image
.GetData();
551 unsigned char* src_alpha
= src_image
.GetAlpha();
552 unsigned char* dst_data
= ret_image
.GetData();
553 unsigned char* dst_alpha
= NULL
;
557 ret_image
.SetAlpha();
558 dst_alpha
= ret_image
.GetAlpha();
561 int averaged_pixels
, src_pixel_index
;
562 double sum_r
, sum_g
, sum_b
, sum_a
;
564 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
566 // Source pixel in the Y direction
567 int src_y
= (int)(y
* scale_factor_y
);
569 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
571 // Source pixel in the X direction
572 int src_x
= (int)(x
* scale_factor_x
);
574 // Box of pixels to average
576 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
578 for ( int j
= src_y
- scale_factor_y_2
+ 1;
579 j
<= int(src_y
+ scale_factor_y_2
);
582 // We don't care to average pixels that don't exist (edges)
583 if ( j
< 0 || j
> M_IMGDATA
->m_height
)
586 for ( int i
= src_x
- scale_factor_x_2
+ 1;
587 i
<= src_x
+ scale_factor_x_2
;
590 // Don't average edge pixels
591 if ( i
< 0 || i
> M_IMGDATA
->m_width
)
594 // Calculate the actual index in our source pixels
595 src_pixel_index
= src_y
* M_IMGDATA
->m_width
+ src_x
;
597 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
598 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
599 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
601 sum_a
+= src_alpha
[src_pixel_index
];
607 // Calculate the average from the sum and number of averaged pixels
608 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
609 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
610 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
613 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
620 // The following two local functions are for the B-spline weighting of the
621 // bicubic sampling algorithm
622 static inline double spline_cube(double value
)
624 return value
<= 0.0 ? 0.0 : value
* value
* value
;
627 static inline double spline_weight(double value
)
629 return (spline_cube(value
+ 2) -
630 4 * spline_cube(value
+ 1) +
631 6 * spline_cube(value
) -
632 4 * spline_cube(value
- 1)) / 6;
635 // This is the bicubic resampling algorithm
636 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
638 // This function implements a Bicubic B-Spline algorithm for resampling.
639 // This method is certainly a little slower than wxImage's default pixel
640 // replication method, however for most reasonably sized images not being
641 // upsampled too much on a fairly average CPU this difference is hardly
642 // noticeable and the results are far more pleasing to look at.
644 // This particular bicubic algorithm does pixel weighting according to a
645 // B-Spline that basically implements a Gaussian bell-like weighting
646 // kernel. Because of this method the results may appear a bit blurry when
647 // upsampling by large factors. This is basically because a slight
648 // gaussian blur is being performed to get the smooth look of the upsampled
651 // Edge pixels: 3-4 possible solutions
652 // - (Wrap/tile) Wrap the image, take the color value from the opposite
653 // side of the image.
654 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
655 // where n is nonpositive, will have the value of (2, 1).
656 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
657 // pixels which do have all neighbours.
658 // - (Clamp) Choose the nearest pixel along the border. This takes the
659 // border pixels and extends them out to infinity.
661 // NOTE: below the y_offset and x_offset variables are being set for edge
662 // pixels using the "Mirror" method mentioned above
666 ret_image
.Create(width
, height
, false);
668 unsigned char* src_data
= M_IMGDATA
->m_data
;
669 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
670 unsigned char* dst_data
= ret_image
.GetData();
671 unsigned char* dst_alpha
= NULL
;
675 ret_image
.SetAlpha();
676 dst_alpha
= ret_image
.GetAlpha();
679 for ( int dsty
= 0; dsty
< height
; dsty
++ )
681 // We need to calculate the source pixel to interpolate from - Y-axis
682 double srcpixy
= dsty
* M_IMGDATA
->m_height
/ height
;
683 double dy
= srcpixy
- (int)srcpixy
;
685 for ( int dstx
= 0; dstx
< width
; dstx
++ )
687 // X-axis of pixel to interpolate from
688 double srcpixx
= dstx
* M_IMGDATA
->m_width
/ width
;
689 double dx
= srcpixx
- (int)srcpixx
;
691 // Sums for each color channel
692 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
694 // Here we actually determine the RGBA values for the destination pixel
695 for ( int k
= -1; k
<= 2; k
++ )
698 int y_offset
= srcpixy
+ k
< 0.0
700 : srcpixy
+ k
>= M_IMGDATA
->m_height
701 ? M_IMGDATA
->m_height
- 1
702 : (int)(srcpixy
+ k
);
704 // Loop across the X axis
705 for ( int i
= -1; i
<= 2; i
++ )
708 int x_offset
= srcpixx
+ i
< 0.0
710 : srcpixx
+ i
>= M_IMGDATA
->m_width
711 ? M_IMGDATA
->m_width
- 1
712 : (int)(srcpixx
+ i
);
714 // Calculate the exact position where the source data
715 // should be pulled from based on the x_offset and y_offset
716 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
718 // Calculate the weight for the specified pixel according
719 // to the bicubic b-spline kernel we're using for
722 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
724 // Create a sum of all velues for each color channel
725 // adjusted for the pixel's calculated weight
726 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
727 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
728 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
730 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
734 // Put the data into the destination image. The summed values are
735 // of double data type and are rounded here for accuracy
736 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
737 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
738 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
742 *dst_alpha
++ = (unsigned char)sum_a
;
749 // Blur in the horizontal direction
750 wxImage
wxImage::BlurHorizontal(int blurRadius
)
753 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
755 unsigned char* src_data
= M_IMGDATA
->m_data
;
756 unsigned char* dst_data
= ret_image
.GetData();
757 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
758 unsigned char* dst_alpha
= NULL
;
760 // Check for a mask or alpha
761 if ( M_IMGDATA
->m_hasMask
)
763 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
764 M_IMGDATA
->m_maskGreen
,
765 M_IMGDATA
->m_maskBlue
);
771 ret_image
.SetAlpha();
772 dst_alpha
= ret_image
.GetAlpha();
776 // number of pixels we average over
777 const int blurArea
= blurRadius
*2 + 1;
779 // Horizontal blurring algorithm - average all pixels in the specified blur
780 // radius in the X or horizontal direction
781 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
783 // Variables used in the blurring algorithm
790 const unsigned char *src
;
793 // Calculate the average of all pixels in the blur radius for the first
795 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
797 // To deal with the pixels at the start of a row so it's not
798 // grabbing GOK values from memory at negative indices of the
799 // image's data or grabbing from the previous row
801 pixel_idx
= y
* M_IMGDATA
->m_width
;
803 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
805 src
= src_data
+ pixel_idx
*3;
810 sum_a
+= src_alpha
[pixel_idx
];
813 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
814 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
815 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
816 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
818 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
820 // Now average the values of the rest of the pixels by just moving the
821 // blur radius box along the row
822 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
824 // Take care of edge pixels on the left edge by essentially
825 // duplicating the edge pixel
826 if ( x
- blurRadius
- 1 < 0 )
827 pixel_idx
= y
* M_IMGDATA
->m_width
;
829 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
831 // Subtract the value of the pixel at the left side of the blur
833 src
= src_data
+ pixel_idx
*3;
838 sum_a
-= src_alpha
[pixel_idx
];
840 // Take care of edge pixels on the right edge
841 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
842 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
844 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
846 // Add the value of the pixel being added to the end of our box
847 src
= src_data
+ pixel_idx
*3;
852 sum_a
+= src_alpha
[pixel_idx
];
854 // Save off the averaged data
855 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
;
856 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
857 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
858 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
860 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
867 // Blur in the vertical direction
868 wxImage
wxImage::BlurVertical(int blurRadius
)
871 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
873 unsigned char* src_data
= M_IMGDATA
->m_data
;
874 unsigned char* dst_data
= ret_image
.GetData();
875 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
876 unsigned char* dst_alpha
= NULL
;
878 // Check for a mask or alpha
879 if ( M_IMGDATA
->m_hasMask
)
881 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
882 M_IMGDATA
->m_maskGreen
,
883 M_IMGDATA
->m_maskBlue
);
889 ret_image
.SetAlpha();
890 dst_alpha
= ret_image
.GetAlpha();
894 // number of pixels we average over
895 const int blurArea
= blurRadius
*2 + 1;
897 // Vertical blurring algorithm - same as horizontal but switched the
898 // opposite direction
899 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
901 // Variables used in the blurring algorithm
908 const unsigned char *src
;
911 // Calculate the average of all pixels in our blur radius box for the
912 // first pixel of the column
913 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
915 // To deal with the pixels at the start of a column so it's not
916 // grabbing GOK values from memory at negative indices of the
917 // image's data or grabbing from the previous column
921 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
923 src
= src_data
+ pixel_idx
*3;
928 sum_a
+= src_alpha
[pixel_idx
];
931 dst
= dst_data
+ x
*3;
932 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
933 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
934 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
936 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
938 // Now average the values of the rest of the pixels by just moving the
939 // box along the column from top to bottom
940 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
942 // Take care of pixels that would be beyond the top edge by
943 // duplicating the top edge pixel for the column
944 if ( y
- blurRadius
- 1 < 0 )
947 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
949 // Subtract the value of the pixel at the top of our blur radius box
950 src
= src_data
+ pixel_idx
*3;
955 sum_a
-= src_alpha
[pixel_idx
];
957 // Take care of the pixels that would be beyond the bottom edge of
958 // the image similar to the top edge
959 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
960 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
962 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
964 // Add the value of the pixel being added to the end of our box
965 src
= src_data
+ pixel_idx
*3;
970 sum_a
+= src_alpha
[pixel_idx
];
972 // Save off the averaged data
973 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
974 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
975 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
976 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
978 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
985 // The new blur function
986 wxImage
wxImage::Blur(int blurRadius
)
989 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
991 // Blur the image in each direction
992 ret_image
= BlurHorizontal(blurRadius
);
993 ret_image
= ret_image
.BlurVertical(blurRadius
);
998 wxImage
wxImage::Rotate90( bool clockwise
) const
1002 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1004 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
1006 unsigned char *data
= image
.GetData();
1008 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1010 unsigned char *source_data
= M_IMGDATA
->m_data
;
1011 unsigned char *target_data
;
1012 unsigned char *alpha_data
= 0 ;
1013 unsigned char *source_alpha
= 0 ;
1014 unsigned char *target_alpha
= 0 ;
1016 if (M_IMGDATA
->m_hasMask
)
1018 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1022 source_alpha
= M_IMGDATA
->m_alpha
;
1026 alpha_data
= image
.GetAlpha() ;
1030 long height
= M_IMGDATA
->m_height
;
1031 long width
= M_IMGDATA
->m_width
;
1033 for (long j
= 0; j
< height
; j
++)
1035 for (long i
= 0; i
< width
; i
++)
1039 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1041 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1045 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1047 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1049 memcpy( target_data
, source_data
, 3 );
1054 memcpy( target_alpha
, source_alpha
, 1 );
1063 wxImage
wxImage::Mirror( bool horizontally
) const
1067 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1069 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1071 unsigned char *data
= image
.GetData();
1072 unsigned char *alpha
= NULL
;
1074 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1076 if (M_IMGDATA
->m_alpha
!= NULL
) {
1078 alpha
= image
.GetAlpha();
1079 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1082 if (M_IMGDATA
->m_hasMask
)
1083 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1085 long height
= M_IMGDATA
->m_height
;
1086 long width
= M_IMGDATA
->m_width
;
1088 unsigned char *source_data
= M_IMGDATA
->m_data
;
1089 unsigned char *target_data
;
1093 for (long j
= 0; j
< height
; j
++)
1096 target_data
= data
-3;
1097 for (long i
= 0; i
< width
; i
++)
1099 memcpy( target_data
, source_data
, 3 );
1107 // src_alpha starts at the first pixel and increases by 1 after each step
1108 // (a step here is the copy of the alpha value of one pixel)
1109 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1110 // dest_alpha starts just beyond the first line, decreases before each step,
1111 // and after each line is finished, increases by 2 widths (skipping the line
1112 // just copied and the line that will be copied next)
1113 unsigned char *dest_alpha
= alpha
+ width
;
1115 for (long jj
= 0; jj
< height
; ++jj
)
1117 for (long i
= 0; i
< width
; ++i
) {
1118 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1120 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1126 for (long i
= 0; i
< height
; i
++)
1128 target_data
= data
+ 3*width
*(height
-1-i
);
1129 memcpy( target_data
, source_data
, (size_t)3*width
);
1130 source_data
+= 3*width
;
1135 // src_alpha starts at the first pixel and increases by 1 width after each step
1136 // (a step here is the copy of the alpha channel of an entire line)
1137 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1138 // dest_alpha starts just beyond the last line (beyond the whole image)
1139 // and decreases by 1 width before each step
1140 unsigned char *dest_alpha
= alpha
+ width
* height
;
1142 for (long jj
= 0; jj
< height
; ++jj
)
1144 dest_alpha
-= width
;
1145 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1154 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1158 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1160 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1161 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1162 image
, wxT("invalid subimage size") );
1164 const int subwidth
= rect
.GetWidth();
1165 const int subheight
= rect
.GetHeight();
1167 image
.Create( subwidth
, subheight
, false );
1169 const unsigned char *src_data
= GetData();
1170 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1171 unsigned char *subdata
= image
.GetData();
1172 unsigned char *subalpha
= NULL
;
1174 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1176 if (src_alpha
!= NULL
) {
1178 subalpha
= image
.GetAlpha();
1179 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1182 if (M_IMGDATA
->m_hasMask
)
1183 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1185 const int width
= GetWidth();
1186 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1188 src_data
+= 3 * pixsoff
;
1189 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1191 for (long j
= 0; j
< subheight
; ++j
)
1193 memcpy( subdata
, src_data
, 3 * subwidth
);
1194 subdata
+= 3 * subwidth
;
1195 src_data
+= 3 * width
;
1196 if (subalpha
!= NULL
) {
1197 memcpy( subalpha
, src_alpha
, subwidth
);
1198 subalpha
+= subwidth
;
1206 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1207 int r_
, int g_
, int b_
) const
1211 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1212 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1214 int width
= GetWidth(), height
= GetHeight();
1215 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1217 unsigned char r
= (unsigned char)r_
;
1218 unsigned char g
= (unsigned char)g_
;
1219 unsigned char b
= (unsigned char)b_
;
1220 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1222 GetOrFindMaskColour( &r
, &g
, &b
);
1223 image
.SetMaskColour(r
, g
, b
);
1226 image
.SetRGB(wxRect(), r
, g
, b
);
1228 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1229 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1231 finalRect
.width
-= pos
.x
;
1233 finalRect
.height
-= pos
.y
;
1235 subRect
.Intersect(finalRect
);
1237 if (!subRect
.IsEmpty())
1239 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1240 image
.Paste(*this, pos
.x
, pos
.y
);
1242 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1248 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1250 wxCHECK_RET( Ok(), wxT("invalid image") );
1251 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1257 int width
= image
.GetWidth();
1258 int height
= image
.GetHeight();
1271 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1272 width
= M_IMGDATA
->m_width
- (x
+xx
);
1273 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1274 height
= M_IMGDATA
->m_height
- (y
+yy
);
1276 if (width
< 1) return;
1277 if (height
< 1) return;
1279 if ((!HasMask() && !image
.HasMask()) ||
1280 (HasMask() && !image
.HasMask()) ||
1281 ((HasMask() && image
.HasMask() &&
1282 (GetMaskRed()==image
.GetMaskRed()) &&
1283 (GetMaskGreen()==image
.GetMaskGreen()) &&
1284 (GetMaskBlue()==image
.GetMaskBlue()))))
1287 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1288 int source_step
= image
.GetWidth()*3;
1290 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1291 int target_step
= M_IMGDATA
->m_width
*3;
1292 for (int j
= 0; j
< height
; j
++)
1294 memcpy( target_data
, source_data
, width
);
1295 source_data
+= source_step
;
1296 target_data
+= target_step
;
1301 if (!HasMask() && image
.HasMask())
1303 unsigned char r
= image
.GetMaskRed();
1304 unsigned char g
= image
.GetMaskGreen();
1305 unsigned char b
= image
.GetMaskBlue();
1308 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1309 int source_step
= image
.GetWidth()*3;
1311 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1312 int target_step
= M_IMGDATA
->m_width
*3;
1314 for (int j
= 0; j
< height
; j
++)
1316 for (int i
= 0; i
< width
; i
+=3)
1318 if ((source_data
[i
] != r
) &&
1319 (source_data
[i
+1] != g
) &&
1320 (source_data
[i
+2] != b
))
1322 memcpy( target_data
+i
, source_data
+i
, 3 );
1325 source_data
+= source_step
;
1326 target_data
+= target_step
;
1331 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1332 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1334 wxCHECK_RET( Ok(), wxT("invalid image") );
1338 unsigned char *data
= GetData();
1340 const int w
= GetWidth();
1341 const int h
= GetHeight();
1343 for (int j
= 0; j
< h
; j
++)
1344 for (int i
= 0; i
< w
; i
++)
1346 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1356 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1360 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1362 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1364 unsigned char *dest
= image
.GetData();
1366 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1368 unsigned char *src
= M_IMGDATA
->m_data
;
1369 bool hasMask
= M_IMGDATA
->m_hasMask
;
1370 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1371 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1372 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1375 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1377 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1378 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1380 // don't modify the mask
1381 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1383 memcpy(dest
, src
, 3);
1387 // calculate the luma
1388 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1389 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1393 // copy the alpha channel, if any
1396 const size_t alphaSize
= GetWidth() * GetHeight();
1397 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1398 memcpy(alpha
, GetAlpha(), alphaSize
);
1400 image
.SetAlpha(alpha
);
1406 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1410 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1412 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1414 unsigned char *data
= image
.GetData();
1416 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1418 if (M_IMGDATA
->m_hasMask
)
1420 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1421 M_IMGDATA
->m_maskBlue
== b
)
1422 image
.SetMaskColour( 255, 255, 255 );
1424 image
.SetMaskColour( 0, 0, 0 );
1427 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1429 unsigned char *srcd
= M_IMGDATA
->m_data
;
1430 unsigned char *tard
= image
.GetData();
1432 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1434 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1435 tard
[0] = tard
[1] = tard
[2] = 255;
1437 tard
[0] = tard
[1] = tard
[2] = 0;
1443 int wxImage::GetWidth() const
1445 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1447 return M_IMGDATA
->m_width
;
1450 int wxImage::GetHeight() const
1452 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1454 return M_IMGDATA
->m_height
;
1457 long wxImage::XYToIndex(int x
, int y
) const
1461 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1463 return y
*M_IMGDATA
->m_width
+ x
;
1469 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1471 long pos
= XYToIndex(x
, y
);
1472 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1478 M_IMGDATA
->m_data
[ pos
] = r
;
1479 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1480 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1483 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1485 wxCHECK_RET( Ok(), wxT("invalid image") );
1490 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1491 if ( rect
== wxRect() )
1497 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1498 imageRect
.Contains(rect
.GetBottomRight()),
1499 wxT("invalid bounding rectangle") );
1502 int x1
= rect
.GetLeft(),
1504 x2
= rect
.GetRight() + 1,
1505 y2
= rect
.GetBottom() + 1;
1507 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1508 int x
, y
, width
= GetWidth();
1509 for (y
= y1
; y
< y2
; y
++)
1511 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1512 for (x
= x1
; x
< x2
; x
++)
1521 unsigned char wxImage::GetRed( int x
, int y
) const
1523 long pos
= XYToIndex(x
, y
);
1524 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1528 return M_IMGDATA
->m_data
[pos
];
1531 unsigned char wxImage::GetGreen( int x
, int y
) const
1533 long pos
= XYToIndex(x
, y
);
1534 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1538 return M_IMGDATA
->m_data
[pos
+1];
1541 unsigned char wxImage::GetBlue( int x
, int y
) const
1543 long pos
= XYToIndex(x
, y
);
1544 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1548 return M_IMGDATA
->m_data
[pos
+2];
1551 bool wxImage::Ok() const
1553 // image of 0 width or height can't be considered ok - at least because it
1554 // causes crashes in ConvertToBitmap() if we don't catch it in time
1555 wxImageRefData
*data
= M_IMGDATA
;
1556 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1559 unsigned char *wxImage::GetData() const
1561 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1563 return M_IMGDATA
->m_data
;
1566 void wxImage::SetData( unsigned char *data
, bool static_data
)
1568 wxCHECK_RET( Ok(), wxT("invalid image") );
1570 wxImageRefData
*newRefData
= new wxImageRefData();
1572 newRefData
->m_width
= M_IMGDATA
->m_width
;
1573 newRefData
->m_height
= M_IMGDATA
->m_height
;
1574 newRefData
->m_data
= data
;
1575 newRefData
->m_ok
= true;
1576 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1577 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1578 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1579 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1580 newRefData
->m_static
= static_data
;
1584 m_refData
= newRefData
;
1587 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1589 wxImageRefData
*newRefData
= new wxImageRefData();
1593 newRefData
->m_width
= new_width
;
1594 newRefData
->m_height
= new_height
;
1595 newRefData
->m_data
= data
;
1596 newRefData
->m_ok
= true;
1597 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1598 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1599 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1600 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1604 newRefData
->m_width
= new_width
;
1605 newRefData
->m_height
= new_height
;
1606 newRefData
->m_data
= data
;
1607 newRefData
->m_ok
= true;
1609 newRefData
->m_static
= static_data
;
1613 m_refData
= newRefData
;
1616 // ----------------------------------------------------------------------------
1617 // alpha channel support
1618 // ----------------------------------------------------------------------------
1620 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1622 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1624 long pos
= XYToIndex(x
, y
);
1625 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1629 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1632 unsigned char wxImage::GetAlpha(int x
, int y
) const
1634 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1636 long pos
= XYToIndex(x
, y
);
1637 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1639 return M_IMGDATA
->m_alpha
[pos
];
1643 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1647 const int w
= M_IMGDATA
->m_width
;
1648 const int h
= M_IMGDATA
->m_height
;
1650 unsigned char *alpha
= GetAlpha();
1651 unsigned char *data
= GetData();
1653 for ( int y
= 0; y
< h
; y
++ )
1655 for ( int x
= 0; x
< w
; x
++ )
1667 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1669 wxCHECK_RET( Ok(), wxT("invalid image") );
1675 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1678 free(M_IMGDATA
->m_alpha
);
1679 M_IMGDATA
->m_alpha
= alpha
;
1680 M_IMGDATA
->m_staticAlpha
= static_data
;
1683 unsigned char *wxImage::GetAlpha() const
1685 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1687 return M_IMGDATA
->m_alpha
;
1690 void wxImage::InitAlpha()
1692 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1694 // initialize memory for alpha channel
1697 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1698 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1702 // use the mask to initialize the alpha channel.
1703 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1705 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1706 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1707 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1708 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1712 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1713 ? wxIMAGE_ALPHA_TRANSPARENT
1714 : wxIMAGE_ALPHA_OPAQUE
;
1717 M_IMGDATA
->m_hasMask
= false;
1721 // make the image fully opaque
1722 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1726 // ----------------------------------------------------------------------------
1728 // ----------------------------------------------------------------------------
1730 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1732 wxCHECK_RET( Ok(), wxT("invalid image") );
1736 M_IMGDATA
->m_maskRed
= r
;
1737 M_IMGDATA
->m_maskGreen
= g
;
1738 M_IMGDATA
->m_maskBlue
= b
;
1739 M_IMGDATA
->m_hasMask
= true;
1742 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1744 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1746 if (M_IMGDATA
->m_hasMask
)
1748 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1749 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1750 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1755 FindFirstUnusedColour(r
, g
, b
);
1760 unsigned char wxImage::GetMaskRed() const
1762 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1764 return M_IMGDATA
->m_maskRed
;
1767 unsigned char wxImage::GetMaskGreen() const
1769 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1771 return M_IMGDATA
->m_maskGreen
;
1774 unsigned char wxImage::GetMaskBlue() const
1776 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1778 return M_IMGDATA
->m_maskBlue
;
1781 void wxImage::SetMask( bool mask
)
1783 wxCHECK_RET( Ok(), wxT("invalid image") );
1787 M_IMGDATA
->m_hasMask
= mask
;
1790 bool wxImage::HasMask() const
1792 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1794 return M_IMGDATA
->m_hasMask
;
1797 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1799 long pos
= XYToIndex(x
, y
);
1800 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1803 if ( M_IMGDATA
->m_hasMask
)
1805 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1806 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1807 p
[1] == M_IMGDATA
->m_maskGreen
&&
1808 p
[2] == M_IMGDATA
->m_maskBlue
)
1815 if ( M_IMGDATA
->m_alpha
)
1817 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1819 // transparent enough
1828 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1829 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1831 // check that the images are the same size
1832 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1834 wxLogError( _("Image and mask have different sizes.") );
1838 // find unused colour
1839 unsigned char r
,g
,b
;
1840 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1842 wxLogError( _("No unused colour in image being masked.") );
1848 unsigned char *imgdata
= GetData();
1849 unsigned char *maskdata
= mask
.GetData();
1851 const int w
= GetWidth();
1852 const int h
= GetHeight();
1854 for (int j
= 0; j
< h
; j
++)
1856 for (int i
= 0; i
< w
; i
++)
1858 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1869 SetMaskColour(r
, g
, b
);
1875 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1880 unsigned char mr
, mg
, mb
;
1881 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1883 wxLogError( _("No unused colour in image being masked.") );
1890 SetMaskColour(mr
, mg
, mb
);
1892 unsigned char *imgdata
= GetData();
1893 unsigned char *alphadata
= GetAlpha();
1896 int h
= GetHeight();
1898 for (int y
= 0; y
< h
; y
++)
1900 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1902 if (*alphadata
< threshold
)
1911 free(M_IMGDATA
->m_alpha
);
1912 M_IMGDATA
->m_alpha
= NULL
;
1917 // ----------------------------------------------------------------------------
1918 // Palette functions
1919 // ----------------------------------------------------------------------------
1923 bool wxImage::HasPalette() const
1928 return M_IMGDATA
->m_palette
.Ok();
1931 const wxPalette
& wxImage::GetPalette() const
1933 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1935 return M_IMGDATA
->m_palette
;
1938 void wxImage::SetPalette(const wxPalette
& palette
)
1940 wxCHECK_RET( Ok(), wxT("invalid image") );
1944 M_IMGDATA
->m_palette
= palette
;
1947 #endif // wxUSE_PALETTE
1949 // ----------------------------------------------------------------------------
1950 // Option functions (arbitrary name/value mapping)
1951 // ----------------------------------------------------------------------------
1953 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1955 wxCHECK_RET( Ok(), wxT("invalid image") );
1959 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1960 if (idx
== wxNOT_FOUND
)
1962 M_IMGDATA
->m_optionNames
.Add(name
);
1963 M_IMGDATA
->m_optionValues
.Add(value
);
1967 M_IMGDATA
->m_optionNames
[idx
] = name
;
1968 M_IMGDATA
->m_optionValues
[idx
] = value
;
1972 void wxImage::SetOption(const wxString
& name
, int value
)
1975 valStr
.Printf(wxT("%d"), value
);
1976 SetOption(name
, valStr
);
1979 wxString
wxImage::GetOption(const wxString
& name
) const
1981 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1983 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1984 if (idx
== wxNOT_FOUND
)
1985 return wxEmptyString
;
1987 return M_IMGDATA
->m_optionValues
[idx
];
1990 int wxImage::GetOptionInt(const wxString
& name
) const
1992 return wxAtoi(GetOption(name
));
1995 bool wxImage::HasOption(const wxString
& name
) const
1997 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1999 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
2002 // ----------------------------------------------------------------------------
2004 // ----------------------------------------------------------------------------
2006 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2007 long WXUNUSED_UNLESS_STREAMS(type
),
2008 int WXUNUSED_UNLESS_STREAMS(index
) )
2010 #if HAS_FILE_STREAMS
2011 if (wxFileExists(filename
))
2013 wxImageFileInputStream
stream(filename
);
2014 wxBufferedInputStream
bstream( stream
);
2015 return LoadFile(bstream
, type
, index
);
2019 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2023 #else // !HAS_FILE_STREAMS
2025 #endif // HAS_FILE_STREAMS
2028 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2029 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2030 int WXUNUSED_UNLESS_STREAMS(index
) )
2032 #if HAS_FILE_STREAMS
2033 if (wxFileExists(filename
))
2035 wxImageFileInputStream
stream(filename
);
2036 wxBufferedInputStream
bstream( stream
);
2037 return LoadFile(bstream
, mimetype
, index
);
2041 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2045 #else // !HAS_FILE_STREAMS
2047 #endif // HAS_FILE_STREAMS
2052 bool wxImage::SaveFile( const wxString
& filename
) const
2054 wxString ext
= filename
.AfterLast('.').Lower();
2056 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
2059 SaveFile(filename
, pHandler
->GetType());
2063 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
2068 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2069 int WXUNUSED_UNLESS_STREAMS(type
) ) const
2071 #if HAS_FILE_STREAMS
2072 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2074 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2076 wxImageFileOutputStream
stream(filename
);
2078 if ( stream
.IsOk() )
2080 wxBufferedOutputStream
bstream( stream
);
2081 return SaveFile(bstream
, type
);
2083 #endif // HAS_FILE_STREAMS
2088 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2089 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2091 #if HAS_FILE_STREAMS
2092 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2094 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2096 wxImageFileOutputStream
stream(filename
);
2098 if ( stream
.IsOk() )
2100 wxBufferedOutputStream
bstream( stream
);
2101 return SaveFile(bstream
, mimetype
);
2103 #endif // HAS_FILE_STREAMS
2108 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2110 #if HAS_FILE_STREAMS
2111 wxImageFileInputStream
stream(name
);
2112 return CanRead(stream
);
2118 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2119 long WXUNUSED_UNLESS_STREAMS(type
) )
2121 #if HAS_FILE_STREAMS
2122 wxImageFileInputStream
stream(name
);
2124 return GetImageCount(stream
, type
);
2132 bool wxImage::CanRead( wxInputStream
&stream
)
2134 const wxList
& list
= GetHandlers();
2136 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2138 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2139 if (handler
->CanRead( stream
))
2146 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
2148 wxImageHandler
*handler
;
2150 if ( type
== wxBITMAP_TYPE_ANY
)
2152 wxList
&list
=GetHandlers();
2154 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2156 handler
=(wxImageHandler
*)node
->GetData();
2157 if ( handler
->CanRead(stream
) )
2158 return handler
->GetImageCount(stream
);
2162 wxLogWarning(_("No handler found for image type."));
2166 handler
= FindHandler(type
);
2170 wxLogWarning(_("No image handler for type %ld defined."), type
);
2174 if ( handler
->CanRead(stream
) )
2176 return handler
->GetImageCount(stream
);
2180 wxLogError(_("Image file is not of type %ld."), type
);
2185 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
2189 m_refData
= new wxImageRefData
;
2191 wxImageHandler
*handler
;
2193 if ( type
== wxBITMAP_TYPE_ANY
)
2195 wxList
&list
=GetHandlers();
2197 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2199 handler
=(wxImageHandler
*)node
->GetData();
2200 if ( handler
->CanRead(stream
) )
2201 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2205 wxLogWarning( _("No handler found for image type.") );
2209 handler
= FindHandler(type
);
2213 wxLogWarning( _("No image handler for type %ld defined."), type
);
2218 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2220 wxLogError(_("Image file is not of type %ld."), type
);
2224 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2227 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2231 m_refData
= new wxImageRefData
;
2233 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2237 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2242 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2244 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
2248 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
2251 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
2253 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2255 wxImageHandler
*handler
= FindHandler(type
);
2258 wxLogWarning( _("No image handler for type %d defined."), type
);
2263 return handler
->SaveFile( (wxImage
*)this, stream
);
2266 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2268 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2270 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2273 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2278 return handler
->SaveFile( (wxImage
*)this, stream
);
2280 #endif // wxUSE_STREAMS
2282 // ----------------------------------------------------------------------------
2283 // image I/O handlers
2284 // ----------------------------------------------------------------------------
2286 void wxImage::AddHandler( wxImageHandler
*handler
)
2288 // Check for an existing handler of the type being added.
2289 if (FindHandler( handler
->GetType() ) == 0)
2291 sm_handlers
.Append( handler
);
2295 // This is not documented behaviour, merely the simplest 'fix'
2296 // for preventing duplicate additions. If someone ever has
2297 // a good reason to add and remove duplicate handlers (and they
2298 // may) we should probably refcount the duplicates.
2299 // also an issue in InsertHandler below.
2301 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2302 handler
->GetName().c_str() );
2307 void wxImage::InsertHandler( wxImageHandler
*handler
)
2309 // Check for an existing handler of the type being added.
2310 if (FindHandler( handler
->GetType() ) == 0)
2312 sm_handlers
.Insert( handler
);
2316 // see AddHandler for additional comments.
2317 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2318 handler
->GetName().c_str() );
2323 bool wxImage::RemoveHandler( const wxString
& name
)
2325 wxImageHandler
*handler
= FindHandler(name
);
2328 sm_handlers
.DeleteObject(handler
);
2336 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2338 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2341 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2342 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2344 node
= node
->GetNext();
2349 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
2351 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2354 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2355 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2356 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
2358 node
= node
->GetNext();
2363 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
2365 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2368 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2369 if (handler
->GetType() == bitmapType
) return handler
;
2370 node
= node
->GetNext();
2375 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2377 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2380 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2381 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2382 node
= node
->GetNext();
2387 void wxImage::InitStandardHandlers()
2390 AddHandler(new wxBMPHandler
);
2391 #endif // wxUSE_STREAMS
2394 void wxImage::CleanUpHandlers()
2396 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2399 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2400 wxList::compatibility_iterator next
= node
->GetNext();
2405 sm_handlers
.Clear();
2408 wxString
wxImage::GetImageExtWildcard()
2412 wxList
& Handlers
= wxImage::GetHandlers();
2413 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2416 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2417 fmts
+= wxT("*.") + Handler
->GetExtension();
2418 Node
= Node
->GetNext();
2419 if ( Node
) fmts
+= wxT(";");
2422 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2425 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2427 const double red
= rgb
.red
/ 255.0,
2428 green
= rgb
.green
/ 255.0,
2429 blue
= rgb
.blue
/ 255.0;
2431 // find the min and max intensity (and remember which one was it for the
2433 double minimumRGB
= red
;
2434 if ( green
< minimumRGB
)
2436 if ( blue
< minimumRGB
)
2439 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2440 double maximumRGB
= red
;
2441 if ( green
> maximumRGB
)
2446 if ( blue
> maximumRGB
)
2452 const double value
= maximumRGB
;
2454 double hue
= 0.0, saturation
;
2455 const double deltaRGB
= maximumRGB
- minimumRGB
;
2456 if ( wxIsNullDouble(deltaRGB
) )
2458 // Gray has no color
2467 hue
= (green
- blue
) / deltaRGB
;
2471 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2475 hue
= 4.0 + (red
- green
) / deltaRGB
;
2479 wxFAIL_MSG(wxT("hue not specified"));
2488 saturation
= deltaRGB
/ maximumRGB
;
2491 return HSVValue(hue
, saturation
, value
);
2494 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2496 double red
, green
, blue
;
2498 if ( wxIsNullDouble(hsv
.saturation
) )
2507 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2508 int i
= (int)floor(hue
);
2509 double f
= hue
- i
; // fractional part of h
2510 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2516 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2521 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2529 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2534 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2539 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2547 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2552 return RGBValue((unsigned char)(red
* 255.0),
2553 (unsigned char)(green
* 255.0),
2554 (unsigned char)(blue
* 255.0));
2558 * Rotates the hue of each pixel of the image. angle is a double in the range
2559 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2561 void wxImage::RotateHue(double angle
)
2565 unsigned char *srcBytePtr
;
2566 unsigned char *dstBytePtr
;
2567 unsigned long count
;
2568 wxImage::HSVValue hsv
;
2569 wxImage::RGBValue rgb
;
2571 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2572 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2573 if ( count
> 0 && !wxIsNullDouble(angle
) )
2575 srcBytePtr
= M_IMGDATA
->m_data
;
2576 dstBytePtr
= srcBytePtr
;
2579 rgb
.red
= *srcBytePtr
++;
2580 rgb
.green
= *srcBytePtr
++;
2581 rgb
.blue
= *srcBytePtr
++;
2582 hsv
= RGBtoHSV(rgb
);
2584 hsv
.hue
= hsv
.hue
+ angle
;
2586 hsv
.hue
= hsv
.hue
- 1.0;
2587 else if (hsv
.hue
< 0.0)
2588 hsv
.hue
= hsv
.hue
+ 1.0;
2590 rgb
= HSVtoRGB(hsv
);
2591 *dstBytePtr
++ = rgb
.red
;
2592 *dstBytePtr
++ = rgb
.green
;
2593 *dstBytePtr
++ = rgb
.blue
;
2594 } while (--count
!= 0);
2598 //-----------------------------------------------------------------------------
2600 //-----------------------------------------------------------------------------
2602 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2605 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2610 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2615 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2620 bool wxImageHandler::CanRead( const wxString
& name
)
2622 if (wxFileExists(name
))
2624 wxImageFileInputStream
stream(name
);
2625 return CanRead(stream
);
2628 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2633 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2635 wxFileOffset posOld
= stream
.TellI();
2636 if ( posOld
== wxInvalidOffset
)
2638 // can't test unseekable stream
2642 bool ok
= DoCanRead(stream
);
2644 // restore the old position to be able to test other formats and so on
2645 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2647 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2649 // reading would fail anyhow as we're not at the right position
2656 #endif // wxUSE_STREAMS
2658 // ----------------------------------------------------------------------------
2659 // image histogram stuff
2660 // ----------------------------------------------------------------------------
2663 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2668 unsigned char g2
) const
2670 unsigned long key
= MakeKey(r2
, g2
, b2
);
2672 while ( find(key
) != end() )
2674 // color already used
2686 wxLogError(_("No unused colour in image.") );
2692 key
= MakeKey(r2
, g2
, b2
);
2706 wxImage::FindFirstUnusedColour(unsigned char *r
,
2711 unsigned char g2
) const
2713 wxImageHistogram histogram
;
2715 ComputeHistogram(histogram
);
2717 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2723 // Counts and returns the number of different colours. Optionally stops
2724 // when it exceeds 'stopafter' different colours. This is useful, for
2725 // example, to see if the image can be saved as 8-bit (256 colour or
2726 // less, in this case it would be invoked as CountColours(256)). Default
2727 // value for stopafter is -1 (don't care).
2729 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2733 unsigned char r
, g
, b
;
2735 unsigned long size
, nentries
, key
;
2738 size
= GetWidth() * GetHeight();
2741 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2746 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2748 if (h
.Get(key
) == NULL
)
2759 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2761 unsigned char *p
= GetData();
2762 unsigned long nentries
= 0;
2766 const unsigned long size
= GetWidth() * GetHeight();
2768 unsigned char r
, g
, b
;
2769 for ( unsigned long n
= 0; n
< size
; n
++ )
2775 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2777 if ( entry
.value
++ == 0 )
2778 entry
.index
= nentries
++;
2785 * Rotation code by Carlos Moreno
2788 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2789 // does exactly the same thing. And I also got rid of wxRotationPixel
2790 // bacause of potential problems in architectures where alignment
2791 // is an issue, so I had to rewrite parts of the code.
2793 static const double gs_Epsilon
= 1e-10;
2795 static inline int wxCint (double x
)
2797 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2801 // Auxiliary function to rotate a point (x,y) with respect to point p0
2802 // make it inline and use a straight return to facilitate optimization
2803 // also, the function receives the sine and cosine of the angle to avoid
2804 // repeating the time-consuming calls to these functions -- sin/cos can
2805 // be computed and stored in the calling function.
2807 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2809 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2810 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2813 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2815 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2818 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2821 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2823 bool has_alpha
= HasAlpha();
2825 // Create pointer-based array to accelerate access to wxImage's data
2826 unsigned char ** data
= new unsigned char * [GetHeight()];
2827 data
[0] = GetData();
2828 for (i
= 1; i
< GetHeight(); i
++)
2829 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2831 // Same for alpha channel
2832 unsigned char ** alpha
= NULL
;
2835 alpha
= new unsigned char * [GetHeight()];
2836 alpha
[0] = GetAlpha();
2837 for (i
= 1; i
< GetHeight(); i
++)
2838 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2841 // precompute coefficients for rotation formula
2842 // (sine and cosine of the angle)
2843 const double cos_angle
= cos(angle
);
2844 const double sin_angle
= sin(angle
);
2846 // Create new Image to store the result
2847 // First, find rectangle that covers the rotated image; to do that,
2848 // rotate the four corners
2850 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2852 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2853 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2854 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2855 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2857 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2858 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2859 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2860 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2862 // Create rotated image
2863 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2864 // With alpha channel
2868 if (offset_after_rotation
!= NULL
)
2870 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2873 // GRG: The rotated (destination) image is always accessed
2874 // sequentially, so there is no need for a pointer-based
2875 // array here (and in fact it would be slower).
2877 unsigned char * dst
= rotated
.GetData();
2879 unsigned char * alpha_dst
= NULL
;
2881 alpha_dst
= rotated
.GetAlpha();
2883 // GRG: if the original image has a mask, use its RGB values
2884 // as the blank pixel, else, fall back to default (black).
2886 unsigned char blank_r
= 0;
2887 unsigned char blank_g
= 0;
2888 unsigned char blank_b
= 0;
2892 blank_r
= GetMaskRed();
2893 blank_g
= GetMaskGreen();
2894 blank_b
= GetMaskBlue();
2895 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2898 // Now, for each point of the rotated image, find where it came from, by
2899 // performing an inverse rotation (a rotation of -angle) and getting the
2900 // pixel at those coordinates
2902 // GRG: I've taken the (interpolating) test out of the loops, so that
2903 // it is done only once, instead of repeating it for each pixel.
2908 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2910 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2912 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2914 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2915 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2917 // interpolate using the 4 enclosing grid-points. Those
2918 // points can be obtained using floor and ceiling of the
2919 // exact coordinates of the point
2922 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2924 x1
= wxCint(floor(src
.x
));
2925 x2
= wxCint(ceil(src
.x
));
2927 else // else means that x is near one of the borders (0 or width-1)
2929 x1
= x2
= wxCint (src
.x
);
2932 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2934 y1
= wxCint(floor(src
.y
));
2935 y2
= wxCint(ceil(src
.y
));
2939 y1
= y2
= wxCint (src
.y
);
2942 // get four points and the distances (square of the distance,
2943 // for efficiency reasons) for the interpolation formula
2945 // GRG: Do not calculate the points until they are
2946 // really needed -- this way we can calculate
2947 // just one, instead of four, if d1, d2, d3
2948 // or d4 are < gs_Epsilon
2950 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2951 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2952 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2953 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2955 // Now interpolate as a weighted average of the four surrounding
2956 // points, where the weights are the distances to each of those points
2958 // If the point is exactly at one point of the grid of the source
2959 // image, then don't interpolate -- just assign the pixel
2961 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2963 unsigned char *p
= data
[y1
] + (3 * x1
);
2969 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2971 else if (d2
< gs_Epsilon
)
2973 unsigned char *p
= data
[y1
] + (3 * x2
);
2979 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2981 else if (d3
< gs_Epsilon
)
2983 unsigned char *p
= data
[y2
] + (3 * x2
);
2989 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2991 else if (d4
< gs_Epsilon
)
2993 unsigned char *p
= data
[y2
] + (3 * x1
);
2999 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3003 // weights for the weighted average are proportional to the inverse of the distance
3004 unsigned char *v1
= data
[y1
] + (3 * x1
);
3005 unsigned char *v2
= data
[y1
] + (3 * x2
);
3006 unsigned char *v3
= data
[y2
] + (3 * x2
);
3007 unsigned char *v4
= data
[y2
] + (3 * x1
);
3009 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3013 *(dst
++) = (unsigned char)
3014 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3015 w3
* *(v3
++) + w4
* *(v4
++)) /
3016 (w1
+ w2
+ w3
+ w4
) );
3017 *(dst
++) = (unsigned char)
3018 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3019 w3
* *(v3
++) + w4
* *(v4
++)) /
3020 (w1
+ w2
+ w3
+ w4
) );
3021 *(dst
++) = (unsigned char)
3022 ( (w1
* *v1
+ w2
* *v2
+
3023 w3
* *v3
+ w4
* *v4
) /
3024 (w1
+ w2
+ w3
+ w4
) );
3028 v1
= alpha
[y1
] + (x1
);
3029 v2
= alpha
[y1
] + (x2
);
3030 v3
= alpha
[y2
] + (x2
);
3031 v4
= alpha
[y2
] + (x1
);
3033 *(alpha_dst
++) = (unsigned char)
3034 ( (w1
* *v1
+ w2
* *v2
+
3035 w3
* *v3
+ w4
* *v4
) /
3036 (w1
+ w2
+ w3
+ w4
) );
3052 else // not interpolating
3054 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
3056 for (x
= 0; x
< rotated
.GetWidth(); x
++)
3058 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3060 const int xs
= wxCint (src
.x
); // wxCint rounds to the
3061 const int ys
= wxCint (src
.y
); // closest integer
3063 if (0 <= xs
&& xs
< GetWidth() &&
3064 0 <= ys
&& ys
< GetHeight())
3066 unsigned char *p
= data
[ys
] + (3 * xs
);
3072 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3081 *(alpha_dst
++) = 255;
3099 // A module to allow wxImage initialization/cleanup
3100 // without calling these functions from app.cpp or from
3101 // the user's application.
3103 class wxImageModule
: public wxModule
3105 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3108 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
3109 void OnExit() { wxImage::CleanUpHandlers(); };
3112 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3115 #endif // wxUSE_IMAGE