1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/palette.h"
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
33 #include "wx/xpmdecod.h"
38 // make the code compile with either wxFile*Stream or wxFFile*Stream:
39 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
43 typedef wxFFileInputStream wxImageFileInputStream
;
44 typedef wxFFileOutputStream wxImageFileOutputStream
;
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
48 #endif // wxUSE_FILE/wxUSE_FFILE
49 #endif // HAS_FILE_STREAMS
52 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage
,WXDLLEXPORT
)
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 class wxImageRefData
: public wxObjectRefData
63 virtual ~wxImageRefData();
68 unsigned char *m_data
;
71 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
73 // alpha channel data, may be NULL for the formats without alpha support
74 unsigned char *m_alpha
;
78 // if true, m_data is pointer to static data and shouldn't be freed
81 // same as m_static but for m_alpha
86 #endif // wxUSE_PALETTE
88 wxArrayString m_optionNames
;
89 wxArrayString m_optionValues
;
91 DECLARE_NO_COPY_CLASS(wxImageRefData
)
94 wxImageRefData::wxImageRefData()
98 m_type
= wxBITMAP_TYPE_INVALID
;
100 m_alpha
= (unsigned char *) NULL
;
109 m_staticAlpha
= false;
112 wxImageRefData::~wxImageRefData()
116 if ( !m_staticAlpha
)
120 wxList
wxImage::sm_handlers
;
124 //-----------------------------------------------------------------------------
126 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
128 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
130 wxImage::wxImage( int width
, int height
, bool clear
)
132 Create( width
, height
, clear
);
135 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
137 Create( width
, height
, data
, static_data
);
140 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
142 Create( width
, height
, data
, alpha
, static_data
);
145 wxImage::wxImage( const wxString
& name
, wxBitmapType type
, int index
)
147 LoadFile( name
, type
, index
);
150 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
152 LoadFile( name
, mimetype
, index
);
156 wxImage::wxImage( wxInputStream
& stream
, wxBitmapType type
, int index
)
158 LoadFile( stream
, type
, index
);
161 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
163 LoadFile( stream
, mimetype
, index
);
165 #endif // wxUSE_STREAMS
167 wxImage::wxImage(const char* const* xpmData
)
172 bool wxImage::Create(const char* const* xpmData
)
177 wxXPMDecoder decoder
;
178 (*this) = decoder
.ReadData(xpmData
);
185 bool wxImage::Create( int width
, int height
, bool clear
)
189 m_refData
= new wxImageRefData();
191 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
192 if (!M_IMGDATA
->m_data
)
198 M_IMGDATA
->m_width
= width
;
199 M_IMGDATA
->m_height
= height
;
200 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
;
241 M_IMGDATA
->m_staticAlpha
= static_data
;
246 void wxImage::Destroy()
251 void wxImage::Clear(unsigned char value
)
253 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
256 wxObjectRefData
* wxImage::CreateRefData() const
258 return new wxImageRefData
;
261 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
263 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
264 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
266 wxImageRefData
* refData_new
= new wxImageRefData
;
267 refData_new
->m_width
= refData
->m_width
;
268 refData_new
->m_height
= refData
->m_height
;
269 refData_new
->m_maskRed
= refData
->m_maskRed
;
270 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
271 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
272 refData_new
->m_hasMask
= refData
->m_hasMask
;
273 refData_new
->m_ok
= true;
274 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
275 if (refData
->m_alpha
!= NULL
)
277 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
278 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
281 refData_new
->m_data
= (unsigned char*)malloc(size
);
282 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
284 refData_new
->m_palette
= refData
->m_palette
;
286 refData_new
->m_optionNames
= refData
->m_optionNames
;
287 refData_new
->m_optionValues
= refData
->m_optionValues
;
291 wxImage
wxImage::Copy() const
295 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
297 image
.m_refData
= CloneRefData(m_refData
);
302 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
304 if( xFactor
== 1 && yFactor
== 1 )
309 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
311 // can't scale to/from 0 size
312 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
313 wxT("invalid new image size") );
315 long old_height
= M_IMGDATA
->m_height
,
316 old_width
= M_IMGDATA
->m_width
;
318 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
319 wxT("invalid old image size") );
321 long width
= old_width
/ xFactor
;
322 long height
= old_height
/ yFactor
;
324 image
.Create( width
, height
, false );
326 char unsigned *data
= image
.GetData();
328 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
330 bool hasMask
= false ;
331 unsigned char maskRed
= 0;
332 unsigned char maskGreen
= 0;
333 unsigned char maskBlue
=0 ;
335 unsigned char *source_data
= M_IMGDATA
->m_data
;
336 unsigned char *target_data
= data
;
337 unsigned char *source_alpha
= 0 ;
338 unsigned char *target_alpha
= 0 ;
339 if (M_IMGDATA
->m_hasMask
)
342 maskRed
= M_IMGDATA
->m_maskRed
;
343 maskGreen
= M_IMGDATA
->m_maskGreen
;
344 maskBlue
=M_IMGDATA
->m_maskBlue
;
346 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
347 M_IMGDATA
->m_maskGreen
,
348 M_IMGDATA
->m_maskBlue
);
352 source_alpha
= M_IMGDATA
->m_alpha
;
356 target_alpha
= image
.GetAlpha() ;
360 for (long y
= 0; y
< height
; y
++)
362 for (long x
= 0; x
< width
; x
++)
364 unsigned long avgRed
= 0 ;
365 unsigned long avgGreen
= 0;
366 unsigned long avgBlue
= 0;
367 unsigned long avgAlpha
= 0 ;
368 unsigned long counter
= 0 ;
370 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
372 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
373 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
375 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
376 unsigned char red
= pixel
[0] ;
377 unsigned char green
= pixel
[1] ;
378 unsigned char blue
= pixel
[2] ;
379 unsigned char alpha
= 255 ;
381 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
382 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
397 *(target_data
++) = M_IMGDATA
->m_maskRed
;
398 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
399 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
404 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
405 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
406 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
407 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
412 // In case this is a cursor, make sure the hotspot is scaled accordingly:
413 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
414 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
415 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
416 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
417 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
418 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
423 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
427 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
429 // can't scale to/from 0 size
430 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
431 wxT("invalid new image size") );
433 long old_height
= M_IMGDATA
->m_height
,
434 old_width
= M_IMGDATA
->m_width
;
435 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
436 wxT("invalid old image size") );
438 // If the image's new width and height are the same as the original, no
439 // need to waste time or CPU cycles
440 if ( old_width
== width
&& old_height
== height
)
443 // Scale the image (...or more appropriately, resample the image) using
444 // either the high-quality or normal method as specified
445 if ( quality
== wxIMAGE_QUALITY_HIGH
)
447 // We need to check whether we are downsampling or upsampling the image
448 if ( width
< old_width
&& height
< old_height
)
450 // Downsample the image using the box averaging method for best results
451 image
= ResampleBox(width
, height
);
455 // For upsampling or other random/wierd image dimensions we'll use
456 // a bicubic b-spline scaling method
457 image
= ResampleBicubic(width
, height
);
460 else // Default scaling method == simple pixel replication
462 if ( old_width
% width
== 0 && old_width
>= width
&&
463 old_height
% height
== 0 && old_height
>= height
)
465 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
467 image
.Create( width
, height
, false );
469 unsigned char *data
= image
.GetData();
471 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
473 unsigned char *source_data
= M_IMGDATA
->m_data
;
474 unsigned char *target_data
= data
;
475 unsigned char *source_alpha
= 0 ;
476 unsigned char *target_alpha
= 0 ;
478 if ( !M_IMGDATA
->m_hasMask
)
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 // If the original image has a mask, apply the mask to the new image
518 if (M_IMGDATA
->m_hasMask
)
520 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
521 M_IMGDATA
->m_maskGreen
,
522 M_IMGDATA
->m_maskBlue
);
525 // In case this is a cursor, make sure the hotspot is scaled accordingly:
526 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
527 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
528 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
529 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
530 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
531 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
536 wxImage
wxImage::ResampleBox(int width
, int height
) const
538 // This function implements a simple pre-blur/box averaging method for
539 // downsampling that gives reasonably smooth results To scale the image
540 // down we will need to gather a grid of pixels of the size of the scale
541 // factor in each direction and then do an averaging of the pixels.
543 wxImage
ret_image(width
, height
, false);
545 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
546 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
548 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
549 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
551 unsigned char* src_data
= M_IMGDATA
->m_data
;
552 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
553 unsigned char* dst_data
= ret_image
.GetData();
554 unsigned char* dst_alpha
= NULL
;
558 ret_image
.SetAlpha();
559 dst_alpha
= ret_image
.GetAlpha();
562 int averaged_pixels
, src_pixel_index
;
563 double sum_r
, sum_g
, sum_b
, sum_a
;
565 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
567 // Source pixel in the Y direction
568 int src_y
= (int)(y
* scale_factor_y
);
570 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
572 // Source pixel in the X direction
573 int src_x
= (int)(x
* scale_factor_x
);
575 // Box of pixels to average
577 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
579 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
580 j
<= int(src_y
+ scale_factor_y_2
);
583 // We don't care to average pixels that don't exist (edges)
584 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
587 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
588 i
<= src_x
+ scale_factor_x_2
;
591 // Don't average edge pixels
592 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
595 // Calculate the actual index in our source pixels
596 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
598 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
599 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
600 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
602 sum_a
+= src_alpha
[src_pixel_index
];
608 // Calculate the average from the sum and number of averaged pixels
609 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
610 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
611 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
614 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
621 // The following two local functions are for the B-spline weighting of the
622 // bicubic sampling algorithm
623 static inline double spline_cube(double value
)
625 return value
<= 0.0 ? 0.0 : value
* value
* value
;
628 static inline double spline_weight(double value
)
630 return (spline_cube(value
+ 2) -
631 4 * spline_cube(value
+ 1) +
632 6 * spline_cube(value
) -
633 4 * spline_cube(value
- 1)) / 6;
636 // This is the bicubic resampling algorithm
637 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
639 // This function implements a Bicubic B-Spline algorithm for resampling.
640 // This method is certainly a little slower than wxImage's default pixel
641 // replication method, however for most reasonably sized images not being
642 // upsampled too much on a fairly average CPU this difference is hardly
643 // noticeable and the results are far more pleasing to look at.
645 // This particular bicubic algorithm does pixel weighting according to a
646 // B-Spline that basically implements a Gaussian bell-like weighting
647 // kernel. Because of this method the results may appear a bit blurry when
648 // upsampling by large factors. This is basically because a slight
649 // gaussian blur is being performed to get the smooth look of the upsampled
652 // Edge pixels: 3-4 possible solutions
653 // - (Wrap/tile) Wrap the image, take the color value from the opposite
654 // side of the image.
655 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
656 // where n is nonpositive, will have the value of (2, 1).
657 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
658 // pixels which do have all neighbours.
659 // - (Clamp) Choose the nearest pixel along the border. This takes the
660 // border pixels and extends them out to infinity.
662 // NOTE: below the y_offset and x_offset variables are being set for edge
663 // pixels using the "Mirror" method mentioned above
667 ret_image
.Create(width
, height
, false);
669 unsigned char* src_data
= M_IMGDATA
->m_data
;
670 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
671 unsigned char* dst_data
= ret_image
.GetData();
672 unsigned char* dst_alpha
= NULL
;
676 ret_image
.SetAlpha();
677 dst_alpha
= ret_image
.GetAlpha();
680 for ( int dsty
= 0; dsty
< height
; dsty
++ )
682 // We need to calculate the source pixel to interpolate from - Y-axis
683 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
684 double dy
= srcpixy
- (int)srcpixy
;
686 for ( int dstx
= 0; dstx
< width
; dstx
++ )
688 // X-axis of pixel to interpolate from
689 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
690 double dx
= srcpixx
- (int)srcpixx
;
692 // Sums for each color channel
693 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
695 // Here we actually determine the RGBA values for the destination pixel
696 for ( int k
= -1; k
<= 2; k
++ )
699 int y_offset
= srcpixy
+ k
< 0.0
701 : srcpixy
+ k
>= M_IMGDATA
->m_height
702 ? M_IMGDATA
->m_height
- 1
703 : (int)(srcpixy
+ k
);
705 // Loop across the X axis
706 for ( int i
= -1; i
<= 2; i
++ )
709 int x_offset
= srcpixx
+ i
< 0.0
711 : srcpixx
+ i
>= M_IMGDATA
->m_width
712 ? M_IMGDATA
->m_width
- 1
713 : (int)(srcpixx
+ i
);
715 // Calculate the exact position where the source data
716 // should be pulled from based on the x_offset and y_offset
717 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
719 // Calculate the weight for the specified pixel according
720 // to the bicubic b-spline kernel we're using for
723 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
725 // Create a sum of all velues for each color channel
726 // adjusted for the pixel's calculated weight
727 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
728 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
729 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
731 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
735 // Put the data into the destination image. The summed values are
736 // of double data type and are rounded here for accuracy
737 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
738 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
739 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
743 *dst_alpha
++ = (unsigned char)sum_a
;
750 // Blur in the horizontal direction
751 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
754 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
756 unsigned char* src_data
= M_IMGDATA
->m_data
;
757 unsigned char* dst_data
= ret_image
.GetData();
758 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
759 unsigned char* dst_alpha
= NULL
;
761 // Check for a mask or alpha
764 ret_image
.SetAlpha();
765 dst_alpha
= ret_image
.GetAlpha();
767 else if ( M_IMGDATA
->m_hasMask
)
769 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
770 M_IMGDATA
->m_maskGreen
,
771 M_IMGDATA
->m_maskBlue
);
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
*3;
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
) const
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
879 ret_image
.SetAlpha();
880 dst_alpha
= ret_image
.GetAlpha();
882 else if ( M_IMGDATA
->m_hasMask
)
884 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
885 M_IMGDATA
->m_maskGreen
,
886 M_IMGDATA
->m_maskBlue
);
889 // number of pixels we average over
890 const int blurArea
= blurRadius
*2 + 1;
892 // Vertical blurring algorithm - same as horizontal but switched the
893 // opposite direction
894 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
896 // Variables used in the blurring algorithm
903 const unsigned char *src
;
906 // Calculate the average of all pixels in our blur radius box for the
907 // first pixel of the column
908 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
910 // To deal with the pixels at the start of a column so it's not
911 // grabbing GOK values from memory at negative indices of the
912 // image's data or grabbing from the previous column
916 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
918 src
= src_data
+ pixel_idx
*3;
923 sum_a
+= src_alpha
[pixel_idx
];
926 dst
= dst_data
+ x
*3;
927 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
928 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
929 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
931 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
933 // Now average the values of the rest of the pixels by just moving the
934 // box along the column from top to bottom
935 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
937 // Take care of pixels that would be beyond the top edge by
938 // duplicating the top edge pixel for the column
939 if ( y
- blurRadius
- 1 < 0 )
942 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
944 // Subtract the value of the pixel at the top of our blur radius box
945 src
= src_data
+ pixel_idx
*3;
950 sum_a
-= src_alpha
[pixel_idx
];
952 // Take care of the pixels that would be beyond the bottom edge of
953 // the image similar to the top edge
954 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
955 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
957 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
959 // Add the value of the pixel being added to the end of our box
960 src
= src_data
+ pixel_idx
*3;
965 sum_a
+= src_alpha
[pixel_idx
];
967 // Save off the averaged data
968 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
969 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
970 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
971 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
973 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
980 // The new blur function
981 wxImage
wxImage::Blur(int blurRadius
) const
984 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
986 // Blur the image in each direction
987 ret_image
= BlurHorizontal(blurRadius
);
988 ret_image
= ret_image
.BlurVertical(blurRadius
);
993 wxImage
wxImage::Rotate90( bool clockwise
) const
997 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
999 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
1001 unsigned char *data
= image
.GetData();
1003 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1005 unsigned char *source_data
= M_IMGDATA
->m_data
;
1006 unsigned char *target_data
;
1007 unsigned char *alpha_data
= 0 ;
1008 unsigned char *source_alpha
= 0 ;
1009 unsigned char *target_alpha
= 0 ;
1011 if (M_IMGDATA
->m_hasMask
)
1013 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1017 source_alpha
= M_IMGDATA
->m_alpha
;
1021 alpha_data
= image
.GetAlpha() ;
1025 long height
= M_IMGDATA
->m_height
;
1026 long width
= M_IMGDATA
->m_width
;
1028 for (long j
= 0; j
< height
; j
++)
1030 for (long i
= 0; i
< width
; i
++)
1034 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1036 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1040 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1042 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1044 memcpy( target_data
, source_data
, 3 );
1049 memcpy( target_alpha
, source_alpha
, 1 );
1058 wxImage
wxImage::Mirror( bool horizontally
) const
1062 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1064 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1066 unsigned char *data
= image
.GetData();
1067 unsigned char *alpha
= NULL
;
1069 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1071 if (M_IMGDATA
->m_alpha
!= NULL
) {
1073 alpha
= image
.GetAlpha();
1074 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1077 if (M_IMGDATA
->m_hasMask
)
1078 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1080 long height
= M_IMGDATA
->m_height
;
1081 long width
= M_IMGDATA
->m_width
;
1083 unsigned char *source_data
= M_IMGDATA
->m_data
;
1084 unsigned char *target_data
;
1088 for (long j
= 0; j
< height
; j
++)
1091 target_data
= data
-3;
1092 for (long i
= 0; i
< width
; i
++)
1094 memcpy( target_data
, source_data
, 3 );
1102 // src_alpha starts at the first pixel and increases by 1 after each step
1103 // (a step here is the copy of the alpha value of one pixel)
1104 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1105 // dest_alpha starts just beyond the first line, decreases before each step,
1106 // and after each line is finished, increases by 2 widths (skipping the line
1107 // just copied and the line that will be copied next)
1108 unsigned char *dest_alpha
= alpha
+ width
;
1110 for (long jj
= 0; jj
< height
; ++jj
)
1112 for (long i
= 0; i
< width
; ++i
) {
1113 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1115 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1121 for (long i
= 0; i
< height
; i
++)
1123 target_data
= data
+ 3*width
*(height
-1-i
);
1124 memcpy( target_data
, source_data
, (size_t)3*width
);
1125 source_data
+= 3*width
;
1130 // src_alpha starts at the first pixel and increases by 1 width after each step
1131 // (a step here is the copy of the alpha channel of an entire line)
1132 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1133 // dest_alpha starts just beyond the last line (beyond the whole image)
1134 // and decreases by 1 width before each step
1135 unsigned char *dest_alpha
= alpha
+ width
* height
;
1137 for (long jj
= 0; jj
< height
; ++jj
)
1139 dest_alpha
-= width
;
1140 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1149 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1153 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1155 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1156 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1157 image
, wxT("invalid subimage size") );
1159 const int subwidth
= rect
.GetWidth();
1160 const int subheight
= rect
.GetHeight();
1162 image
.Create( subwidth
, subheight
, false );
1164 const unsigned char *src_data
= GetData();
1165 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1166 unsigned char *subdata
= image
.GetData();
1167 unsigned char *subalpha
= NULL
;
1169 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1171 if (src_alpha
!= NULL
) {
1173 subalpha
= image
.GetAlpha();
1174 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1177 if (M_IMGDATA
->m_hasMask
)
1178 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1180 const int width
= GetWidth();
1181 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1183 src_data
+= 3 * pixsoff
;
1184 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1186 for (long j
= 0; j
< subheight
; ++j
)
1188 memcpy( subdata
, src_data
, 3 * subwidth
);
1189 subdata
+= 3 * subwidth
;
1190 src_data
+= 3 * width
;
1191 if (subalpha
!= NULL
) {
1192 memcpy( subalpha
, src_alpha
, subwidth
);
1193 subalpha
+= subwidth
;
1201 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1202 int r_
, int g_
, int b_
) const
1206 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1207 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1209 int width
= GetWidth(), height
= GetHeight();
1210 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1212 unsigned char r
= (unsigned char)r_
;
1213 unsigned char g
= (unsigned char)g_
;
1214 unsigned char b
= (unsigned char)b_
;
1215 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1217 GetOrFindMaskColour( &r
, &g
, &b
);
1218 image
.SetMaskColour(r
, g
, b
);
1221 image
.SetRGB(wxRect(), r
, g
, b
);
1223 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1224 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1226 finalRect
.width
-= pos
.x
;
1228 finalRect
.height
-= pos
.y
;
1230 subRect
.Intersect(finalRect
);
1232 if (!subRect
.IsEmpty())
1234 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1235 image
.Paste(*this, pos
.x
, pos
.y
);
1237 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1243 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1245 wxCHECK_RET( Ok(), wxT("invalid image") );
1246 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1252 int width
= image
.GetWidth();
1253 int height
= image
.GetHeight();
1266 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1267 width
= M_IMGDATA
->m_width
- (x
+xx
);
1268 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1269 height
= M_IMGDATA
->m_height
- (y
+yy
);
1271 if (width
< 1) return;
1272 if (height
< 1) return;
1274 if ((!HasMask() && !image
.HasMask()) ||
1275 (HasMask() && !image
.HasMask()) ||
1276 ((HasMask() && image
.HasMask() &&
1277 (GetMaskRed()==image
.GetMaskRed()) &&
1278 (GetMaskGreen()==image
.GetMaskGreen()) &&
1279 (GetMaskBlue()==image
.GetMaskBlue()))))
1281 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1282 int source_step
= image
.GetWidth()*3;
1284 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1285 int target_step
= M_IMGDATA
->m_width
*3;
1286 for (int j
= 0; j
< height
; j
++)
1288 memcpy( target_data
, source_data
, width
*3 );
1289 source_data
+= source_step
;
1290 target_data
+= target_step
;
1294 // Copy over the alpha channel from the original image
1295 if ( image
.HasAlpha() )
1300 unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1301 int source_step
= image
.GetWidth();
1303 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1304 int target_step
= M_IMGDATA
->m_width
;
1306 for (int j
= 0; j
< height
; j
++,
1307 source_data
+= source_step
,
1308 target_data
+= target_step
)
1310 memcpy( target_data
, source_data
, width
);
1314 if (!HasMask() && image
.HasMask())
1316 unsigned char r
= image
.GetMaskRed();
1317 unsigned char g
= image
.GetMaskGreen();
1318 unsigned char b
= image
.GetMaskBlue();
1320 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1321 int source_step
= image
.GetWidth()*3;
1323 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1324 int target_step
= M_IMGDATA
->m_width
*3;
1326 for (int j
= 0; j
< height
; j
++)
1328 for (int i
= 0; i
< width
*3; i
+=3)
1330 if ((source_data
[i
] != r
) ||
1331 (source_data
[i
+1] != g
) ||
1332 (source_data
[i
+2] != b
))
1334 memcpy( target_data
+i
, source_data
+i
, 3 );
1337 source_data
+= source_step
;
1338 target_data
+= target_step
;
1343 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1344 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1346 wxCHECK_RET( Ok(), wxT("invalid image") );
1350 unsigned char *data
= GetData();
1352 const int w
= GetWidth();
1353 const int h
= GetHeight();
1355 for (int j
= 0; j
< h
; j
++)
1356 for (int i
= 0; i
< w
; i
++)
1358 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1368 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1372 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1374 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1376 unsigned char *dest
= image
.GetData();
1378 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1380 unsigned char *src
= M_IMGDATA
->m_data
;
1381 bool hasMask
= M_IMGDATA
->m_hasMask
;
1382 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1383 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1384 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1387 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1389 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1390 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1392 // don't modify the mask
1393 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1395 memcpy(dest
, src
, 3);
1399 // calculate the luma
1400 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1401 dest
[0] = dest
[1] = dest
[2] = static_cast<unsigned char>(luma
);
1405 // copy the alpha channel, if any
1408 const size_t alphaSize
= GetWidth() * GetHeight();
1409 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1410 memcpy(alpha
, GetAlpha(), alphaSize
);
1412 image
.SetAlpha(alpha
);
1418 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1422 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1424 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1426 unsigned char *data
= image
.GetData();
1428 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1430 if (M_IMGDATA
->m_hasMask
)
1432 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1433 M_IMGDATA
->m_maskBlue
== b
)
1434 image
.SetMaskColour( 255, 255, 255 );
1436 image
.SetMaskColour( 0, 0, 0 );
1439 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1441 unsigned char *srcd
= M_IMGDATA
->m_data
;
1442 unsigned char *tard
= image
.GetData();
1444 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1446 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1447 tard
[0] = tard
[1] = tard
[2] = 255;
1449 tard
[0] = tard
[1] = tard
[2] = 0;
1455 int wxImage::GetWidth() const
1457 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1459 return M_IMGDATA
->m_width
;
1462 int wxImage::GetHeight() const
1464 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1466 return M_IMGDATA
->m_height
;
1469 wxBitmapType
wxImage::GetType() const
1471 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1473 return M_IMGDATA
->m_type
;
1476 void wxImage::SetType(wxBitmapType type
)
1478 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1480 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1481 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1483 M_IMGDATA
->m_type
= type
;
1486 long wxImage::XYToIndex(int x
, int y
) const
1490 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1492 return y
*M_IMGDATA
->m_width
+ x
;
1498 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1500 long pos
= XYToIndex(x
, y
);
1501 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1507 M_IMGDATA
->m_data
[ pos
] = r
;
1508 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1509 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1512 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1514 wxCHECK_RET( Ok(), wxT("invalid image") );
1519 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1520 if ( rect
== wxRect() )
1526 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1527 imageRect
.Contains(rect
.GetBottomRight()),
1528 wxT("invalid bounding rectangle") );
1531 int x1
= rect
.GetLeft(),
1533 x2
= rect
.GetRight() + 1,
1534 y2
= rect
.GetBottom() + 1;
1536 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1537 int x
, y
, width
= GetWidth();
1538 for (y
= y1
; y
< y2
; y
++)
1540 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1541 for (x
= x1
; x
< x2
; x
++)
1550 unsigned char wxImage::GetRed( int x
, int y
) const
1552 long pos
= XYToIndex(x
, y
);
1553 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1557 return M_IMGDATA
->m_data
[pos
];
1560 unsigned char wxImage::GetGreen( int x
, int y
) const
1562 long pos
= XYToIndex(x
, y
);
1563 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1567 return M_IMGDATA
->m_data
[pos
+1];
1570 unsigned char wxImage::GetBlue( int x
, int y
) const
1572 long pos
= XYToIndex(x
, y
);
1573 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1577 return M_IMGDATA
->m_data
[pos
+2];
1580 bool wxImage::IsOk() const
1582 // image of 0 width or height can't be considered ok - at least because it
1583 // causes crashes in ConvertToBitmap() if we don't catch it in time
1584 wxImageRefData
*data
= M_IMGDATA
;
1585 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1588 unsigned char *wxImage::GetData() const
1590 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1592 return M_IMGDATA
->m_data
;
1595 void wxImage::SetData( unsigned char *data
, bool static_data
)
1597 wxCHECK_RET( Ok(), wxT("invalid image") );
1599 wxImageRefData
*newRefData
= new wxImageRefData();
1601 newRefData
->m_width
= M_IMGDATA
->m_width
;
1602 newRefData
->m_height
= M_IMGDATA
->m_height
;
1603 newRefData
->m_data
= data
;
1604 newRefData
->m_ok
= true;
1605 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1606 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1607 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1608 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1609 newRefData
->m_static
= static_data
;
1613 m_refData
= newRefData
;
1616 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1618 wxImageRefData
*newRefData
= new wxImageRefData();
1622 newRefData
->m_width
= new_width
;
1623 newRefData
->m_height
= new_height
;
1624 newRefData
->m_data
= data
;
1625 newRefData
->m_ok
= true;
1626 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1627 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1628 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1629 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1633 newRefData
->m_width
= new_width
;
1634 newRefData
->m_height
= new_height
;
1635 newRefData
->m_data
= data
;
1636 newRefData
->m_ok
= true;
1638 newRefData
->m_static
= static_data
;
1642 m_refData
= newRefData
;
1645 // ----------------------------------------------------------------------------
1646 // alpha channel support
1647 // ----------------------------------------------------------------------------
1649 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1651 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1653 long pos
= XYToIndex(x
, y
);
1654 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1658 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1661 unsigned char wxImage::GetAlpha(int x
, int y
) const
1663 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1665 long pos
= XYToIndex(x
, y
);
1666 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1668 return M_IMGDATA
->m_alpha
[pos
];
1672 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1676 const int w
= M_IMGDATA
->m_width
;
1677 const int h
= M_IMGDATA
->m_height
;
1679 unsigned char *alpha
= GetAlpha();
1680 unsigned char *data
= GetData();
1682 for ( int y
= 0; y
< h
; y
++ )
1684 for ( int x
= 0; x
< w
; x
++ )
1696 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1698 wxCHECK_RET( Ok(), wxT("invalid image") );
1704 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1707 if( !M_IMGDATA
->m_staticAlpha
)
1708 free(M_IMGDATA
->m_alpha
);
1710 M_IMGDATA
->m_alpha
= alpha
;
1711 M_IMGDATA
->m_staticAlpha
= static_data
;
1714 unsigned char *wxImage::GetAlpha() const
1716 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1718 return M_IMGDATA
->m_alpha
;
1721 void wxImage::InitAlpha()
1723 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1725 // initialize memory for alpha channel
1728 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1729 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1733 // use the mask to initialize the alpha channel.
1734 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1736 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1737 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1738 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1739 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1743 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1744 ? wxIMAGE_ALPHA_TRANSPARENT
1745 : wxIMAGE_ALPHA_OPAQUE
;
1748 M_IMGDATA
->m_hasMask
= false;
1752 // make the image fully opaque
1753 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1757 // ----------------------------------------------------------------------------
1759 // ----------------------------------------------------------------------------
1761 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1763 wxCHECK_RET( Ok(), wxT("invalid image") );
1767 M_IMGDATA
->m_maskRed
= r
;
1768 M_IMGDATA
->m_maskGreen
= g
;
1769 M_IMGDATA
->m_maskBlue
= b
;
1770 M_IMGDATA
->m_hasMask
= true;
1773 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1775 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1777 if (M_IMGDATA
->m_hasMask
)
1779 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1780 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1781 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1786 FindFirstUnusedColour(r
, g
, b
);
1791 unsigned char wxImage::GetMaskRed() const
1793 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1795 return M_IMGDATA
->m_maskRed
;
1798 unsigned char wxImage::GetMaskGreen() const
1800 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1802 return M_IMGDATA
->m_maskGreen
;
1805 unsigned char wxImage::GetMaskBlue() const
1807 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1809 return M_IMGDATA
->m_maskBlue
;
1812 void wxImage::SetMask( bool mask
)
1814 wxCHECK_RET( Ok(), wxT("invalid image") );
1818 M_IMGDATA
->m_hasMask
= mask
;
1821 bool wxImage::HasMask() const
1823 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1825 return M_IMGDATA
->m_hasMask
;
1828 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1830 long pos
= XYToIndex(x
, y
);
1831 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1834 if ( M_IMGDATA
->m_hasMask
)
1836 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1837 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1838 p
[1] == M_IMGDATA
->m_maskGreen
&&
1839 p
[2] == M_IMGDATA
->m_maskBlue
)
1846 if ( M_IMGDATA
->m_alpha
)
1848 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1850 // transparent enough
1859 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1860 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1862 // check that the images are the same size
1863 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1865 wxLogError( _("Image and mask have different sizes.") );
1869 // find unused colour
1870 unsigned char r
,g
,b
;
1871 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1873 wxLogError( _("No unused colour in image being masked.") );
1879 unsigned char *imgdata
= GetData();
1880 unsigned char *maskdata
= mask
.GetData();
1882 const int w
= GetWidth();
1883 const int h
= GetHeight();
1885 for (int j
= 0; j
< h
; j
++)
1887 for (int i
= 0; i
< w
; i
++)
1889 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1900 SetMaskColour(r
, g
, b
);
1906 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1911 unsigned char mr
, mg
, mb
;
1912 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1914 wxLogError( _("No unused colour in image being masked.") );
1921 SetMaskColour(mr
, mg
, mb
);
1923 unsigned char *imgdata
= GetData();
1924 unsigned char *alphadata
= GetAlpha();
1927 int h
= GetHeight();
1929 for (int y
= 0; y
< h
; y
++)
1931 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1933 if (*alphadata
< threshold
)
1942 if( !M_IMGDATA
->m_staticAlpha
)
1943 free(M_IMGDATA
->m_alpha
);
1945 M_IMGDATA
->m_alpha
= NULL
;
1946 M_IMGDATA
->m_staticAlpha
= false;
1951 // ----------------------------------------------------------------------------
1952 // Palette functions
1953 // ----------------------------------------------------------------------------
1957 bool wxImage::HasPalette() const
1962 return M_IMGDATA
->m_palette
.Ok();
1965 const wxPalette
& wxImage::GetPalette() const
1967 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1969 return M_IMGDATA
->m_palette
;
1972 void wxImage::SetPalette(const wxPalette
& palette
)
1974 wxCHECK_RET( Ok(), wxT("invalid image") );
1978 M_IMGDATA
->m_palette
= palette
;
1981 #endif // wxUSE_PALETTE
1983 // ----------------------------------------------------------------------------
1984 // Option functions (arbitrary name/value mapping)
1985 // ----------------------------------------------------------------------------
1987 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1991 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1992 if ( idx
== wxNOT_FOUND
)
1994 M_IMGDATA
->m_optionNames
.Add(name
);
1995 M_IMGDATA
->m_optionValues
.Add(value
);
1999 M_IMGDATA
->m_optionNames
[idx
] = name
;
2000 M_IMGDATA
->m_optionValues
[idx
] = value
;
2004 void wxImage::SetOption(const wxString
& name
, int value
)
2007 valStr
.Printf(wxT("%d"), value
);
2008 SetOption(name
, valStr
);
2011 wxString
wxImage::GetOption(const wxString
& name
) const
2014 return wxEmptyString
;
2016 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2017 if ( idx
== wxNOT_FOUND
)
2018 return wxEmptyString
;
2020 return M_IMGDATA
->m_optionValues
[idx
];
2023 int wxImage::GetOptionInt(const wxString
& name
) const
2025 return wxAtoi(GetOption(name
));
2028 bool wxImage::HasOption(const wxString
& name
) const
2030 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2034 // ----------------------------------------------------------------------------
2036 // ----------------------------------------------------------------------------
2038 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2039 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2040 int WXUNUSED_UNLESS_STREAMS(index
) )
2042 #if HAS_FILE_STREAMS
2043 if (wxFileExists(filename
))
2045 wxImageFileInputStream
stream(filename
);
2046 wxBufferedInputStream
bstream( stream
);
2047 return LoadFile(bstream
, type
, index
);
2051 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2055 #else // !HAS_FILE_STREAMS
2057 #endif // HAS_FILE_STREAMS
2060 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2061 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2062 int WXUNUSED_UNLESS_STREAMS(index
) )
2064 #if HAS_FILE_STREAMS
2065 if (wxFileExists(filename
))
2067 wxImageFileInputStream
stream(filename
);
2068 wxBufferedInputStream
bstream( stream
);
2069 return LoadFile(bstream
, mimetype
, index
);
2073 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2077 #else // !HAS_FILE_STREAMS
2079 #endif // HAS_FILE_STREAMS
2083 bool wxImage::SaveFile( const wxString
& filename
) const
2085 wxString ext
= filename
.AfterLast('.').Lower();
2087 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2090 wxLogError(_("Can't save image to file '%s': unknown extension."),
2095 return SaveFile(filename
, handler
->GetType());
2098 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2099 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2101 #if HAS_FILE_STREAMS
2102 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2104 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2106 wxImageFileOutputStream
stream(filename
);
2108 if ( stream
.IsOk() )
2110 wxBufferedOutputStream
bstream( stream
);
2111 return SaveFile(bstream
, type
);
2113 #endif // HAS_FILE_STREAMS
2118 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2119 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2121 #if HAS_FILE_STREAMS
2122 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2124 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2126 wxImageFileOutputStream
stream(filename
);
2128 if ( stream
.IsOk() )
2130 wxBufferedOutputStream
bstream( stream
);
2131 return SaveFile(bstream
, mimetype
);
2133 #endif // HAS_FILE_STREAMS
2138 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2140 #if HAS_FILE_STREAMS
2141 wxImageFileInputStream
stream(name
);
2142 return CanRead(stream
);
2148 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2149 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2151 #if HAS_FILE_STREAMS
2152 wxImageFileInputStream
stream(name
);
2154 return GetImageCount(stream
, type
);
2162 bool wxImage::CanRead( wxInputStream
&stream
)
2164 const wxList
& list
= GetHandlers();
2166 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2168 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2169 if (handler
->CanRead( stream
))
2176 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2178 wxImageHandler
*handler
;
2180 if ( type
== wxBITMAP_TYPE_ANY
)
2182 const wxList
& list
= GetHandlers();
2184 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2186 node
= node
->GetNext() )
2188 handler
= (wxImageHandler
*)node
->GetData();
2189 if ( handler
->CanRead(stream
) )
2191 const int count
= handler
->GetImageCount(stream
);
2198 wxLogWarning(_("No handler found for image type."));
2202 handler
= FindHandler(type
);
2206 wxLogWarning(_("No image handler for type %ld defined."), type
);
2210 if ( handler
->CanRead(stream
) )
2212 return handler
->GetImageCount(stream
);
2216 wxLogError(_("Image file is not of type %ld."), type
);
2221 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2223 // save the options values which can be clobbered by the handler (e.g. many
2224 // of them call Destroy() before trying to load the file)
2225 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2226 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2228 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2231 M_IMGDATA
->m_type
= handler
.GetType();
2233 // rescale the image to the specified size if needed
2234 if ( maxWidth
|| maxHeight
)
2236 const unsigned widthOrig
= GetWidth(),
2237 heightOrig
= GetHeight();
2239 // this uses the same (trivial) algorithm as the JPEG handler
2240 unsigned width
= widthOrig
,
2241 height
= heightOrig
;
2242 while ( (maxWidth
&& width
> maxWidth
) ||
2243 (maxHeight
&& height
> maxHeight
) )
2249 if ( width
!= widthOrig
|| height
!= heightOrig
)
2250 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2256 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2260 wxImageHandler
*handler
;
2262 if ( type
== wxBITMAP_TYPE_ANY
)
2264 const wxList
& list
= GetHandlers();
2265 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2267 node
= node
->GetNext() )
2269 handler
= (wxImageHandler
*)node
->GetData();
2270 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2274 wxLogWarning( _("No handler found for image type.") );
2278 //else: have specific type
2280 handler
= FindHandler(type
);
2283 wxLogWarning( _("No image handler for type %ld defined."), type
);
2287 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2289 wxLogError(_("Image file is not of type %ld."), type
);
2293 return DoLoad(*handler
, stream
, index
);
2296 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2300 m_refData
= new wxImageRefData
;
2302 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2306 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2310 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2312 wxLogError(_("Image file is not of type %s."), mimetype
);
2316 return DoLoad(*handler
, stream
, index
);
2319 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2321 wxImage
* const self
= const_cast<wxImage
*>(this);
2322 if ( !handler
.SaveFile(self
, stream
) )
2325 M_IMGDATA
->m_type
= handler
.GetType();
2329 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2331 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2333 wxImageHandler
*handler
= FindHandler(type
);
2336 wxLogWarning( _("No image handler for type %d defined."), type
);
2340 return DoSave(*handler
, stream
);
2343 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2345 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2347 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2350 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2353 return DoSave(*handler
, stream
);
2356 #endif // wxUSE_STREAMS
2358 // ----------------------------------------------------------------------------
2359 // image I/O handlers
2360 // ----------------------------------------------------------------------------
2362 void wxImage::AddHandler( wxImageHandler
*handler
)
2364 // Check for an existing handler of the type being added.
2365 if (FindHandler( handler
->GetType() ) == 0)
2367 sm_handlers
.Append( handler
);
2371 // This is not documented behaviour, merely the simplest 'fix'
2372 // for preventing duplicate additions. If someone ever has
2373 // a good reason to add and remove duplicate handlers (and they
2374 // may) we should probably refcount the duplicates.
2375 // also an issue in InsertHandler below.
2377 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2378 handler
->GetName().c_str() );
2383 void wxImage::InsertHandler( wxImageHandler
*handler
)
2385 // Check for an existing handler of the type being added.
2386 if (FindHandler( handler
->GetType() ) == 0)
2388 sm_handlers
.Insert( handler
);
2392 // see AddHandler for additional comments.
2393 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2394 handler
->GetName().c_str() );
2399 bool wxImage::RemoveHandler( const wxString
& name
)
2401 wxImageHandler
*handler
= FindHandler(name
);
2404 sm_handlers
.DeleteObject(handler
);
2412 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2414 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2417 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2418 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2420 node
= node
->GetNext();
2425 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2427 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2430 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2431 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2432 ( (bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
)) )
2436 node
= node
->GetNext();
2441 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2443 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2446 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2447 if (handler
->GetType() == bitmapType
) return handler
;
2448 node
= node
->GetNext();
2453 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2455 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2458 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2459 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2460 node
= node
->GetNext();
2465 void wxImage::InitStandardHandlers()
2468 AddHandler(new wxBMPHandler
);
2469 #endif // wxUSE_STREAMS
2472 void wxImage::CleanUpHandlers()
2474 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2477 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2478 wxList::compatibility_iterator next
= node
->GetNext();
2483 sm_handlers
.Clear();
2486 wxString
wxImage::GetImageExtWildcard()
2490 wxList
& Handlers
= wxImage::GetHandlers();
2491 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2494 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2495 fmts
+= wxT("*.") + Handler
->GetExtension();
2496 Node
= Node
->GetNext();
2497 if ( Node
) fmts
+= wxT(";");
2500 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2503 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2505 const double red
= rgb
.red
/ 255.0,
2506 green
= rgb
.green
/ 255.0,
2507 blue
= rgb
.blue
/ 255.0;
2509 // find the min and max intensity (and remember which one was it for the
2511 double minimumRGB
= red
;
2512 if ( green
< minimumRGB
)
2514 if ( blue
< minimumRGB
)
2517 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2518 double maximumRGB
= red
;
2519 if ( green
> maximumRGB
)
2524 if ( blue
> maximumRGB
)
2530 const double value
= maximumRGB
;
2532 double hue
= 0.0, saturation
;
2533 const double deltaRGB
= maximumRGB
- minimumRGB
;
2534 if ( wxIsNullDouble(deltaRGB
) )
2536 // Gray has no color
2545 hue
= (green
- blue
) / deltaRGB
;
2549 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2553 hue
= 4.0 + (red
- green
) / deltaRGB
;
2557 wxFAIL_MSG(wxT("hue not specified"));
2566 saturation
= deltaRGB
/ maximumRGB
;
2569 return HSVValue(hue
, saturation
, value
);
2572 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2574 double red
, green
, blue
;
2576 if ( wxIsNullDouble(hsv
.saturation
) )
2585 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2586 int i
= (int)floor(hue
);
2587 double f
= hue
- i
; // fractional part of h
2588 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2594 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2599 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2607 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2612 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2617 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2625 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2630 return RGBValue((unsigned char)(red
* 255.0),
2631 (unsigned char)(green
* 255.0),
2632 (unsigned char)(blue
* 255.0));
2636 * Rotates the hue of each pixel of the image. angle is a double in the range
2637 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2639 void wxImage::RotateHue(double angle
)
2643 unsigned char *srcBytePtr
;
2644 unsigned char *dstBytePtr
;
2645 unsigned long count
;
2646 wxImage::HSVValue hsv
;
2647 wxImage::RGBValue rgb
;
2649 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2650 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2651 if ( count
> 0 && !wxIsNullDouble(angle
) )
2653 srcBytePtr
= M_IMGDATA
->m_data
;
2654 dstBytePtr
= srcBytePtr
;
2657 rgb
.red
= *srcBytePtr
++;
2658 rgb
.green
= *srcBytePtr
++;
2659 rgb
.blue
= *srcBytePtr
++;
2660 hsv
= RGBtoHSV(rgb
);
2662 hsv
.hue
= hsv
.hue
+ angle
;
2664 hsv
.hue
= hsv
.hue
- 1.0;
2665 else if (hsv
.hue
< 0.0)
2666 hsv
.hue
= hsv
.hue
+ 1.0;
2668 rgb
= HSVtoRGB(hsv
);
2669 *dstBytePtr
++ = rgb
.red
;
2670 *dstBytePtr
++ = rgb
.green
;
2671 *dstBytePtr
++ = rgb
.blue
;
2672 } while (--count
!= 0);
2676 //-----------------------------------------------------------------------------
2678 //-----------------------------------------------------------------------------
2680 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2683 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2688 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2693 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2698 bool wxImageHandler::CanRead( const wxString
& name
)
2700 if (wxFileExists(name
))
2702 wxImageFileInputStream
stream(name
);
2703 return CanRead(stream
);
2706 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2711 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2713 wxFileOffset posOld
= stream
.TellI();
2714 if ( posOld
== wxInvalidOffset
)
2716 // can't test unseekable stream
2720 bool ok
= DoCanRead(stream
);
2722 // restore the old position to be able to test other formats and so on
2723 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2725 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2727 // reading would fail anyhow as we're not at the right position
2734 #endif // wxUSE_STREAMS
2738 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2740 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, _T("NULL pointer") );
2742 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2743 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2745 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2746 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2748 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2751 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2753 else // no resolution options specified
2758 return wxIMAGE_RESOLUTION_NONE
;
2761 // get the resolution unit too
2762 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2765 // this is the default
2766 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2769 return (wxImageResolution
)resUnit
;
2772 // ----------------------------------------------------------------------------
2773 // image histogram stuff
2774 // ----------------------------------------------------------------------------
2777 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2782 unsigned char g2
) const
2784 unsigned long key
= MakeKey(r2
, g2
, b2
);
2786 while ( find(key
) != end() )
2788 // color already used
2800 wxLogError(_("No unused colour in image.") );
2806 key
= MakeKey(r2
, g2
, b2
);
2820 wxImage::FindFirstUnusedColour(unsigned char *r
,
2825 unsigned char g2
) const
2827 wxImageHistogram histogram
;
2829 ComputeHistogram(histogram
);
2831 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2837 // Counts and returns the number of different colours. Optionally stops
2838 // when it exceeds 'stopafter' different colours. This is useful, for
2839 // example, to see if the image can be saved as 8-bit (256 colour or
2840 // less, in this case it would be invoked as CountColours(256)). Default
2841 // value for stopafter is -1 (don't care).
2843 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2847 unsigned char r
, g
, b
;
2849 unsigned long size
, nentries
, key
;
2852 size
= GetWidth() * GetHeight();
2855 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2860 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2862 if (h
.Get(key
) == NULL
)
2873 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2875 unsigned char *p
= GetData();
2876 unsigned long nentries
= 0;
2880 const unsigned long size
= GetWidth() * GetHeight();
2882 unsigned char r
, g
, b
;
2883 for ( unsigned long n
= 0; n
< size
; n
++ )
2889 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2891 if ( entry
.value
++ == 0 )
2892 entry
.index
= nentries
++;
2899 * Rotation code by Carlos Moreno
2902 static const double wxROTATE_EPSILON
= 1e-10;
2904 // Auxiliary function to rotate a point (x,y) with respect to point p0
2905 // make it inline and use a straight return to facilitate optimization
2906 // also, the function receives the sine and cosine of the angle to avoid
2907 // repeating the time-consuming calls to these functions -- sin/cos can
2908 // be computed and stored in the calling function.
2910 static inline wxRealPoint
2911 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2912 const wxRealPoint
& p0
)
2914 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2915 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2918 static inline wxRealPoint
2919 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2920 const wxRealPoint
& p0
)
2922 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2925 wxImage
wxImage::Rotate(double angle
,
2926 const wxPoint
& centre_of_rotation
,
2928 wxPoint
*offset_after_rotation
) const
2930 // screen coordinates are a mirror image of "real" coordinates
2933 const bool has_alpha
= HasAlpha();
2935 const int w
= GetWidth();
2936 const int h
= GetHeight();
2940 // Create pointer-based array to accelerate access to wxImage's data
2941 unsigned char ** data
= new unsigned char * [h
];
2942 data
[0] = GetData();
2943 for (i
= 1; i
< h
; i
++)
2944 data
[i
] = data
[i
- 1] + (3 * w
);
2946 // Same for alpha channel
2947 unsigned char ** alpha
= NULL
;
2950 alpha
= new unsigned char * [h
];
2951 alpha
[0] = GetAlpha();
2952 for (i
= 1; i
< h
; i
++)
2953 alpha
[i
] = alpha
[i
- 1] + w
;
2956 // precompute coefficients for rotation formula
2957 const double cos_angle
= cos(angle
);
2958 const double sin_angle
= sin(angle
);
2960 // Create new Image to store the result
2961 // First, find rectangle that covers the rotated image; to do that,
2962 // rotate the four corners
2964 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2966 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2967 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
2968 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
2969 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
2971 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2972 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2973 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2974 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2976 // Create rotated image
2977 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2978 // With alpha channel
2982 if (offset_after_rotation
!= NULL
)
2984 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2987 // the rotated (destination) image is always accessed sequentially via this
2988 // pointer, there is no need for pointer-based arrays here
2989 unsigned char *dst
= rotated
.GetData();
2991 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
2993 // if the original image has a mask, use its RGB values as the blank pixel,
2994 // else, fall back to default (black).
2995 unsigned char blank_r
= 0;
2996 unsigned char blank_g
= 0;
2997 unsigned char blank_b
= 0;
3001 blank_r
= GetMaskRed();
3002 blank_g
= GetMaskGreen();
3003 blank_b
= GetMaskBlue();
3004 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3007 // Now, for each point of the rotated image, find where it came from, by
3008 // performing an inverse rotation (a rotation of -angle) and getting the
3009 // pixel at those coordinates
3011 const int rH
= rotated
.GetHeight();
3012 const int rW
= rotated
.GetWidth();
3014 // do the (interpolating) test outside of the loops, so that it is done
3015 // only once, instead of repeating it for each pixel.
3018 for (int y
= 0; y
< rH
; y
++)
3020 for (int x
= 0; x
< rW
; x
++)
3022 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3024 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3025 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3027 // interpolate using the 4 enclosing grid-points. Those
3028 // points can be obtained using floor and ceiling of the
3029 // exact coordinates of the point
3032 if (0 < src
.x
&& src
.x
< w
- 1)
3034 x1
= wxRound(floor(src
.x
));
3035 x2
= wxRound(ceil(src
.x
));
3037 else // else means that x is near one of the borders (0 or width-1)
3039 x1
= x2
= wxRound (src
.x
);
3042 if (0 < src
.y
&& src
.y
< h
- 1)
3044 y1
= wxRound(floor(src
.y
));
3045 y2
= wxRound(ceil(src
.y
));
3049 y1
= y2
= wxRound (src
.y
);
3052 // get four points and the distances (square of the distance,
3053 // for efficiency reasons) for the interpolation formula
3055 // GRG: Do not calculate the points until they are
3056 // really needed -- this way we can calculate
3057 // just one, instead of four, if d1, d2, d3
3058 // or d4 are < wxROTATE_EPSILON
3060 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3061 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3062 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3063 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3065 // Now interpolate as a weighted average of the four surrounding
3066 // points, where the weights are the distances to each of those points
3068 // If the point is exactly at one point of the grid of the source
3069 // image, then don't interpolate -- just assign the pixel
3071 // d1,d2,d3,d4 are positive -- no need for abs()
3072 if (d1
< wxROTATE_EPSILON
)
3074 unsigned char *p
= data
[y1
] + (3 * x1
);
3080 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3082 else if (d2
< wxROTATE_EPSILON
)
3084 unsigned char *p
= data
[y1
] + (3 * x2
);
3090 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3092 else if (d3
< wxROTATE_EPSILON
)
3094 unsigned char *p
= data
[y2
] + (3 * x2
);
3100 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3102 else if (d4
< wxROTATE_EPSILON
)
3104 unsigned char *p
= data
[y2
] + (3 * x1
);
3110 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3114 // weights for the weighted average are proportional to the inverse of the distance
3115 unsigned char *v1
= data
[y1
] + (3 * x1
);
3116 unsigned char *v2
= data
[y1
] + (3 * x2
);
3117 unsigned char *v3
= data
[y2
] + (3 * x2
);
3118 unsigned char *v4
= data
[y2
] + (3 * x1
);
3120 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3124 *(dst
++) = (unsigned char)
3125 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3126 w3
* *(v3
++) + w4
* *(v4
++)) /
3127 (w1
+ w2
+ w3
+ w4
) );
3128 *(dst
++) = (unsigned char)
3129 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3130 w3
* *(v3
++) + w4
* *(v4
++)) /
3131 (w1
+ w2
+ w3
+ w4
) );
3132 *(dst
++) = (unsigned char)
3133 ( (w1
* *v1
+ w2
* *v2
+
3134 w3
* *v3
+ w4
* *v4
) /
3135 (w1
+ w2
+ w3
+ w4
) );
3139 v1
= alpha
[y1
] + (x1
);
3140 v2
= alpha
[y1
] + (x2
);
3141 v3
= alpha
[y2
] + (x2
);
3142 v4
= alpha
[y2
] + (x1
);
3144 *(alpha_dst
++) = (unsigned char)
3145 ( (w1
* *v1
+ w2
* *v2
+
3146 w3
* *v3
+ w4
* *v4
) /
3147 (w1
+ w2
+ w3
+ w4
) );
3163 else // not interpolating
3165 for (int y
= 0; y
< rH
; y
++)
3167 for (int x
= 0; x
< rW
; x
++)
3169 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3171 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3172 const int ys
= wxRound (src
.y
); // closest integer
3174 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3176 unsigned char *p
= data
[ys
] + (3 * xs
);
3182 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3191 *(alpha_dst
++) = 255;
3207 // A module to allow wxImage initialization/cleanup
3208 // without calling these functions from app.cpp or from
3209 // the user's application.
3211 class wxImageModule
: public wxModule
3213 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3216 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3217 void OnExit() { wxImage::CleanUpHandlers(); }
3220 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3223 #endif // wxUSE_IMAGE