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
)
199 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
201 M_IMGDATA
->m_width
= width
;
202 M_IMGDATA
->m_height
= height
;
203 M_IMGDATA
->m_ok
= true;
208 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
212 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
214 m_refData
= new wxImageRefData();
216 M_IMGDATA
->m_data
= data
;
217 M_IMGDATA
->m_width
= width
;
218 M_IMGDATA
->m_height
= height
;
219 M_IMGDATA
->m_ok
= true;
220 M_IMGDATA
->m_static
= static_data
;
225 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
229 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
231 m_refData
= new wxImageRefData();
233 M_IMGDATA
->m_data
= data
;
234 M_IMGDATA
->m_alpha
= alpha
;
235 M_IMGDATA
->m_width
= width
;
236 M_IMGDATA
->m_height
= height
;
237 M_IMGDATA
->m_ok
= true;
238 M_IMGDATA
->m_static
= static_data
;
239 M_IMGDATA
->m_staticAlpha
= static_data
;
244 void wxImage::Destroy()
249 wxObjectRefData
* wxImage::CreateRefData() const
251 return new wxImageRefData
;
254 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
256 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
257 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
259 wxImageRefData
* refData_new
= new wxImageRefData
;
260 refData_new
->m_width
= refData
->m_width
;
261 refData_new
->m_height
= refData
->m_height
;
262 refData_new
->m_maskRed
= refData
->m_maskRed
;
263 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
264 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
265 refData_new
->m_hasMask
= refData
->m_hasMask
;
266 refData_new
->m_ok
= true;
267 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
268 if (refData
->m_alpha
!= NULL
)
270 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
271 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
274 refData_new
->m_data
= (unsigned char*)malloc(size
);
275 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
277 refData_new
->m_palette
= refData
->m_palette
;
279 refData_new
->m_optionNames
= refData
->m_optionNames
;
280 refData_new
->m_optionValues
= refData
->m_optionValues
;
284 wxImage
wxImage::Copy() const
288 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
290 image
.m_refData
= CloneRefData(m_refData
);
295 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
297 if( xFactor
== 1 && yFactor
== 1 )
302 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
304 // can't scale to/from 0 size
305 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
306 wxT("invalid new image size") );
308 long old_height
= M_IMGDATA
->m_height
,
309 old_width
= M_IMGDATA
->m_width
;
311 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
312 wxT("invalid old image size") );
314 long width
= old_width
/ xFactor
;
315 long height
= old_height
/ yFactor
;
317 image
.Create( width
, height
, false );
319 char unsigned *data
= image
.GetData();
321 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
323 bool hasMask
= false ;
324 unsigned char maskRed
= 0;
325 unsigned char maskGreen
= 0;
326 unsigned char maskBlue
=0 ;
328 unsigned char *source_data
= M_IMGDATA
->m_data
;
329 unsigned char *target_data
= data
;
330 unsigned char *source_alpha
= 0 ;
331 unsigned char *target_alpha
= 0 ;
332 if (M_IMGDATA
->m_hasMask
)
335 maskRed
= M_IMGDATA
->m_maskRed
;
336 maskGreen
= M_IMGDATA
->m_maskGreen
;
337 maskBlue
=M_IMGDATA
->m_maskBlue
;
339 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
340 M_IMGDATA
->m_maskGreen
,
341 M_IMGDATA
->m_maskBlue
);
345 source_alpha
= M_IMGDATA
->m_alpha
;
349 target_alpha
= image
.GetAlpha() ;
353 for (long y
= 0; y
< height
; y
++)
355 for (long x
= 0; x
< width
; x
++)
357 unsigned long avgRed
= 0 ;
358 unsigned long avgGreen
= 0;
359 unsigned long avgBlue
= 0;
360 unsigned long avgAlpha
= 0 ;
361 unsigned long counter
= 0 ;
363 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
365 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
366 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
368 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
369 unsigned char red
= pixel
[0] ;
370 unsigned char green
= pixel
[1] ;
371 unsigned char blue
= pixel
[2] ;
372 unsigned char alpha
= 255 ;
374 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
375 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
390 *(target_data
++) = M_IMGDATA
->m_maskRed
;
391 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
392 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
397 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
398 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
399 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
400 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
405 // In case this is a cursor, make sure the hotspot is scaled accordingly:
406 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
407 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
408 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
409 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
410 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
411 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
416 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
420 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
422 // can't scale to/from 0 size
423 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
424 wxT("invalid new image size") );
426 long old_height
= M_IMGDATA
->m_height
,
427 old_width
= M_IMGDATA
->m_width
;
428 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
429 wxT("invalid old image size") );
431 // If the image's new width and height are the same as the original, no
432 // need to waste time or CPU cycles
433 if ( old_width
== width
&& old_height
== height
)
436 // Scale the image (...or more appropriately, resample the image) using
437 // either the high-quality or normal method as specified
438 if ( quality
== wxIMAGE_QUALITY_HIGH
)
440 // We need to check whether we are downsampling or upsampling the image
441 if ( width
< old_width
&& height
< old_height
)
443 // Downsample the image using the box averaging method for best results
444 image
= ResampleBox(width
, height
);
448 // For upsampling or other random/wierd image dimensions we'll use
449 // a bicubic b-spline scaling method
450 image
= ResampleBicubic(width
, height
);
453 else // Default scaling method == simple pixel replication
455 if ( old_width
% width
== 0 && old_width
>= width
&&
456 old_height
% height
== 0 && old_height
>= height
)
458 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
460 image
.Create( width
, height
, false );
462 unsigned char *data
= image
.GetData();
464 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
466 unsigned char *source_data
= M_IMGDATA
->m_data
;
467 unsigned char *target_data
= data
;
468 unsigned char *source_alpha
= 0 ;
469 unsigned char *target_alpha
= 0 ;
471 if ( !M_IMGDATA
->m_hasMask
)
473 source_alpha
= M_IMGDATA
->m_alpha
;
477 target_alpha
= image
.GetAlpha() ;
481 long x_delta
= (old_width
<<16) / width
;
482 long y_delta
= (old_height
<<16) / height
;
484 unsigned char* dest_pixel
= target_data
;
487 for ( long j
= 0; j
< height
; j
++ )
489 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
490 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
493 for ( long i
= 0; i
< width
; i
++ )
495 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
496 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
497 dest_pixel
[0] = src_pixel
[0];
498 dest_pixel
[1] = src_pixel
[1];
499 dest_pixel
[2] = src_pixel
[2];
502 *(target_alpha
++) = *src_alpha_pixel
;
510 // If the original image has a mask, apply the mask to the new image
511 if (M_IMGDATA
->m_hasMask
)
513 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
514 M_IMGDATA
->m_maskGreen
,
515 M_IMGDATA
->m_maskBlue
);
518 // In case this is a cursor, make sure the hotspot is scaled accordingly:
519 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
520 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
521 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
522 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
523 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
524 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
529 wxImage
wxImage::ResampleBox(int width
, int height
) const
531 // This function implements a simple pre-blur/box averaging method for
532 // downsampling that gives reasonably smooth results To scale the image
533 // down we will need to gather a grid of pixels of the size of the scale
534 // factor in each direction and then do an averaging of the pixels.
536 wxImage
ret_image(width
, height
, false);
538 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
539 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
541 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
542 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
544 unsigned char* src_data
= M_IMGDATA
->m_data
;
545 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
546 unsigned char* dst_data
= ret_image
.GetData();
547 unsigned char* dst_alpha
= NULL
;
551 ret_image
.SetAlpha();
552 dst_alpha
= ret_image
.GetAlpha();
555 int averaged_pixels
, src_pixel_index
;
556 double sum_r
, sum_g
, sum_b
, sum_a
;
558 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
560 // Source pixel in the Y direction
561 int src_y
= (int)(y
* scale_factor_y
);
563 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
565 // Source pixel in the X direction
566 int src_x
= (int)(x
* scale_factor_x
);
568 // Box of pixels to average
570 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
572 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
573 j
<= int(src_y
+ scale_factor_y_2
);
576 // We don't care to average pixels that don't exist (edges)
577 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
580 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
581 i
<= src_x
+ scale_factor_x_2
;
584 // Don't average edge pixels
585 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
588 // Calculate the actual index in our source pixels
589 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
591 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
592 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
593 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
595 sum_a
+= src_alpha
[src_pixel_index
];
601 // Calculate the average from the sum and number of averaged pixels
602 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
603 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
604 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
607 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
614 // The following two local functions are for the B-spline weighting of the
615 // bicubic sampling algorithm
616 static inline double spline_cube(double value
)
618 return value
<= 0.0 ? 0.0 : value
* value
* value
;
621 static inline double spline_weight(double value
)
623 return (spline_cube(value
+ 2) -
624 4 * spline_cube(value
+ 1) +
625 6 * spline_cube(value
) -
626 4 * spline_cube(value
- 1)) / 6;
629 // This is the bicubic resampling algorithm
630 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
632 // This function implements a Bicubic B-Spline algorithm for resampling.
633 // This method is certainly a little slower than wxImage's default pixel
634 // replication method, however for most reasonably sized images not being
635 // upsampled too much on a fairly average CPU this difference is hardly
636 // noticeable and the results are far more pleasing to look at.
638 // This particular bicubic algorithm does pixel weighting according to a
639 // B-Spline that basically implements a Gaussian bell-like weighting
640 // kernel. Because of this method the results may appear a bit blurry when
641 // upsampling by large factors. This is basically because a slight
642 // gaussian blur is being performed to get the smooth look of the upsampled
645 // Edge pixels: 3-4 possible solutions
646 // - (Wrap/tile) Wrap the image, take the color value from the opposite
647 // side of the image.
648 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
649 // where n is nonpositive, will have the value of (2, 1).
650 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
651 // pixels which do have all neighbours.
652 // - (Clamp) Choose the nearest pixel along the border. This takes the
653 // border pixels and extends them out to infinity.
655 // NOTE: below the y_offset and x_offset variables are being set for edge
656 // pixels using the "Mirror" method mentioned above
660 ret_image
.Create(width
, height
, false);
662 unsigned char* src_data
= M_IMGDATA
->m_data
;
663 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
664 unsigned char* dst_data
= ret_image
.GetData();
665 unsigned char* dst_alpha
= NULL
;
669 ret_image
.SetAlpha();
670 dst_alpha
= ret_image
.GetAlpha();
673 for ( int dsty
= 0; dsty
< height
; dsty
++ )
675 // We need to calculate the source pixel to interpolate from - Y-axis
676 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
677 double dy
= srcpixy
- (int)srcpixy
;
679 for ( int dstx
= 0; dstx
< width
; dstx
++ )
681 // X-axis of pixel to interpolate from
682 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
683 double dx
= srcpixx
- (int)srcpixx
;
685 // Sums for each color channel
686 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
688 // Here we actually determine the RGBA values for the destination pixel
689 for ( int k
= -1; k
<= 2; k
++ )
692 int y_offset
= srcpixy
+ k
< 0.0
694 : srcpixy
+ k
>= M_IMGDATA
->m_height
695 ? M_IMGDATA
->m_height
- 1
696 : (int)(srcpixy
+ k
);
698 // Loop across the X axis
699 for ( int i
= -1; i
<= 2; i
++ )
702 int x_offset
= srcpixx
+ i
< 0.0
704 : srcpixx
+ i
>= M_IMGDATA
->m_width
705 ? M_IMGDATA
->m_width
- 1
706 : (int)(srcpixx
+ i
);
708 // Calculate the exact position where the source data
709 // should be pulled from based on the x_offset and y_offset
710 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
712 // Calculate the weight for the specified pixel according
713 // to the bicubic b-spline kernel we're using for
716 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
718 // Create a sum of all velues for each color channel
719 // adjusted for the pixel's calculated weight
720 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
721 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
722 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
724 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
728 // Put the data into the destination image. The summed values are
729 // of double data type and are rounded here for accuracy
730 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
731 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
732 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
736 *dst_alpha
++ = (unsigned char)sum_a
;
743 // Blur in the horizontal direction
744 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
747 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
749 unsigned char* src_data
= M_IMGDATA
->m_data
;
750 unsigned char* dst_data
= ret_image
.GetData();
751 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
752 unsigned char* dst_alpha
= NULL
;
754 // Check for a mask or alpha
757 ret_image
.SetAlpha();
758 dst_alpha
= ret_image
.GetAlpha();
760 else if ( M_IMGDATA
->m_hasMask
)
762 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
763 M_IMGDATA
->m_maskGreen
,
764 M_IMGDATA
->m_maskBlue
);
767 // number of pixels we average over
768 const int blurArea
= blurRadius
*2 + 1;
770 // Horizontal blurring algorithm - average all pixels in the specified blur
771 // radius in the X or horizontal direction
772 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
774 // Variables used in the blurring algorithm
781 const unsigned char *src
;
784 // Calculate the average of all pixels in the blur radius for the first
786 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
788 // To deal with the pixels at the start of a row so it's not
789 // grabbing GOK values from memory at negative indices of the
790 // image's data or grabbing from the previous row
792 pixel_idx
= y
* M_IMGDATA
->m_width
;
794 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
796 src
= src_data
+ pixel_idx
*3;
801 sum_a
+= src_alpha
[pixel_idx
];
804 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
805 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
806 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
807 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
809 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
811 // Now average the values of the rest of the pixels by just moving the
812 // blur radius box along the row
813 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
815 // Take care of edge pixels on the left edge by essentially
816 // duplicating the edge pixel
817 if ( x
- blurRadius
- 1 < 0 )
818 pixel_idx
= y
* M_IMGDATA
->m_width
;
820 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
822 // Subtract the value of the pixel at the left side of the blur
824 src
= src_data
+ pixel_idx
*3;
829 sum_a
-= src_alpha
[pixel_idx
];
831 // Take care of edge pixels on the right edge
832 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
833 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
835 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
837 // Add the value of the pixel being added to the end of our box
838 src
= src_data
+ pixel_idx
*3;
843 sum_a
+= src_alpha
[pixel_idx
];
845 // Save off the averaged data
846 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
847 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
848 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
849 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
851 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
858 // Blur in the vertical direction
859 wxImage
wxImage::BlurVertical(int blurRadius
) const
862 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
864 unsigned char* src_data
= M_IMGDATA
->m_data
;
865 unsigned char* dst_data
= ret_image
.GetData();
866 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
867 unsigned char* dst_alpha
= NULL
;
869 // Check for a mask or alpha
872 ret_image
.SetAlpha();
873 dst_alpha
= ret_image
.GetAlpha();
875 else if ( M_IMGDATA
->m_hasMask
)
877 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
878 M_IMGDATA
->m_maskGreen
,
879 M_IMGDATA
->m_maskBlue
);
882 // number of pixels we average over
883 const int blurArea
= blurRadius
*2 + 1;
885 // Vertical blurring algorithm - same as horizontal but switched the
886 // opposite direction
887 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
889 // Variables used in the blurring algorithm
896 const unsigned char *src
;
899 // Calculate the average of all pixels in our blur radius box for the
900 // first pixel of the column
901 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
903 // To deal with the pixels at the start of a column so it's not
904 // grabbing GOK values from memory at negative indices of the
905 // image's data or grabbing from the previous column
909 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
911 src
= src_data
+ pixel_idx
*3;
916 sum_a
+= src_alpha
[pixel_idx
];
919 dst
= dst_data
+ x
*3;
920 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
921 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
922 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
924 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
926 // Now average the values of the rest of the pixels by just moving the
927 // box along the column from top to bottom
928 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
930 // Take care of pixels that would be beyond the top edge by
931 // duplicating the top edge pixel for the column
932 if ( y
- blurRadius
- 1 < 0 )
935 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
937 // Subtract the value of the pixel at the top of our blur radius box
938 src
= src_data
+ pixel_idx
*3;
943 sum_a
-= src_alpha
[pixel_idx
];
945 // Take care of the pixels that would be beyond the bottom edge of
946 // the image similar to the top edge
947 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
948 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
950 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
952 // Add the value of the pixel being added to the end of our box
953 src
= src_data
+ pixel_idx
*3;
958 sum_a
+= src_alpha
[pixel_idx
];
960 // Save off the averaged data
961 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
962 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
963 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
964 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
966 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
973 // The new blur function
974 wxImage
wxImage::Blur(int blurRadius
) const
977 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
979 // Blur the image in each direction
980 ret_image
= BlurHorizontal(blurRadius
);
981 ret_image
= ret_image
.BlurVertical(blurRadius
);
986 wxImage
wxImage::Rotate90( bool clockwise
) const
990 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
992 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
994 unsigned char *data
= image
.GetData();
996 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
998 unsigned char *source_data
= M_IMGDATA
->m_data
;
999 unsigned char *target_data
;
1000 unsigned char *alpha_data
= 0 ;
1001 unsigned char *source_alpha
= 0 ;
1002 unsigned char *target_alpha
= 0 ;
1004 if (M_IMGDATA
->m_hasMask
)
1006 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1010 source_alpha
= M_IMGDATA
->m_alpha
;
1014 alpha_data
= image
.GetAlpha() ;
1018 long height
= M_IMGDATA
->m_height
;
1019 long width
= M_IMGDATA
->m_width
;
1021 for (long j
= 0; j
< height
; j
++)
1023 for (long i
= 0; i
< width
; i
++)
1027 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1029 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1033 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1035 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1037 memcpy( target_data
, source_data
, 3 );
1042 memcpy( target_alpha
, source_alpha
, 1 );
1051 wxImage
wxImage::Mirror( bool horizontally
) const
1055 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1057 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1059 unsigned char *data
= image
.GetData();
1060 unsigned char *alpha
= NULL
;
1062 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1064 if (M_IMGDATA
->m_alpha
!= NULL
) {
1066 alpha
= image
.GetAlpha();
1067 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1070 if (M_IMGDATA
->m_hasMask
)
1071 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1073 long height
= M_IMGDATA
->m_height
;
1074 long width
= M_IMGDATA
->m_width
;
1076 unsigned char *source_data
= M_IMGDATA
->m_data
;
1077 unsigned char *target_data
;
1081 for (long j
= 0; j
< height
; j
++)
1084 target_data
= data
-3;
1085 for (long i
= 0; i
< width
; i
++)
1087 memcpy( target_data
, source_data
, 3 );
1095 // src_alpha starts at the first pixel and increases by 1 after each step
1096 // (a step here is the copy of the alpha value of one pixel)
1097 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1098 // dest_alpha starts just beyond the first line, decreases before each step,
1099 // and after each line is finished, increases by 2 widths (skipping the line
1100 // just copied and the line that will be copied next)
1101 unsigned char *dest_alpha
= alpha
+ width
;
1103 for (long jj
= 0; jj
< height
; ++jj
)
1105 for (long i
= 0; i
< width
; ++i
) {
1106 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1108 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1114 for (long i
= 0; i
< height
; i
++)
1116 target_data
= data
+ 3*width
*(height
-1-i
);
1117 memcpy( target_data
, source_data
, (size_t)3*width
);
1118 source_data
+= 3*width
;
1123 // src_alpha starts at the first pixel and increases by 1 width after each step
1124 // (a step here is the copy of the alpha channel of an entire line)
1125 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1126 // dest_alpha starts just beyond the last line (beyond the whole image)
1127 // and decreases by 1 width before each step
1128 unsigned char *dest_alpha
= alpha
+ width
* height
;
1130 for (long jj
= 0; jj
< height
; ++jj
)
1132 dest_alpha
-= width
;
1133 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1142 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1146 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1148 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1149 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1150 image
, wxT("invalid subimage size") );
1152 const int subwidth
= rect
.GetWidth();
1153 const int subheight
= rect
.GetHeight();
1155 image
.Create( subwidth
, subheight
, false );
1157 const unsigned char *src_data
= GetData();
1158 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1159 unsigned char *subdata
= image
.GetData();
1160 unsigned char *subalpha
= NULL
;
1162 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1164 if (src_alpha
!= NULL
) {
1166 subalpha
= image
.GetAlpha();
1167 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1170 if (M_IMGDATA
->m_hasMask
)
1171 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1173 const int width
= GetWidth();
1174 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1176 src_data
+= 3 * pixsoff
;
1177 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1179 for (long j
= 0; j
< subheight
; ++j
)
1181 memcpy( subdata
, src_data
, 3 * subwidth
);
1182 subdata
+= 3 * subwidth
;
1183 src_data
+= 3 * width
;
1184 if (subalpha
!= NULL
) {
1185 memcpy( subalpha
, src_alpha
, subwidth
);
1186 subalpha
+= subwidth
;
1194 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1195 int r_
, int g_
, int b_
) const
1199 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1200 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1202 int width
= GetWidth(), height
= GetHeight();
1203 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1205 unsigned char r
= (unsigned char)r_
;
1206 unsigned char g
= (unsigned char)g_
;
1207 unsigned char b
= (unsigned char)b_
;
1208 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1210 GetOrFindMaskColour( &r
, &g
, &b
);
1211 image
.SetMaskColour(r
, g
, b
);
1214 image
.SetRGB(wxRect(), r
, g
, b
);
1216 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1217 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1219 finalRect
.width
-= pos
.x
;
1221 finalRect
.height
-= pos
.y
;
1223 subRect
.Intersect(finalRect
);
1225 if (!subRect
.IsEmpty())
1227 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1228 image
.Paste(*this, pos
.x
, pos
.y
);
1230 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1236 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1238 wxCHECK_RET( Ok(), wxT("invalid image") );
1239 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1245 int width
= image
.GetWidth();
1246 int height
= image
.GetHeight();
1259 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1260 width
= M_IMGDATA
->m_width
- (x
+xx
);
1261 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1262 height
= M_IMGDATA
->m_height
- (y
+yy
);
1264 if (width
< 1) return;
1265 if (height
< 1) return;
1267 if ((!HasMask() && !image
.HasMask()) ||
1268 (HasMask() && !image
.HasMask()) ||
1269 ((HasMask() && image
.HasMask() &&
1270 (GetMaskRed()==image
.GetMaskRed()) &&
1271 (GetMaskGreen()==image
.GetMaskGreen()) &&
1272 (GetMaskBlue()==image
.GetMaskBlue()))))
1274 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1275 int source_step
= image
.GetWidth()*3;
1277 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1278 int target_step
= M_IMGDATA
->m_width
*3;
1279 for (int j
= 0; j
< height
; j
++)
1281 memcpy( target_data
, source_data
, width
*3 );
1282 source_data
+= source_step
;
1283 target_data
+= target_step
;
1287 // Copy over the alpha channel from the original image
1288 if ( image
.HasAlpha() )
1293 unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1294 int source_step
= image
.GetWidth();
1296 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1297 int target_step
= M_IMGDATA
->m_width
;
1299 for (int j
= 0; j
< height
; j
++,
1300 source_data
+= source_step
,
1301 target_data
+= target_step
)
1303 memcpy( target_data
, source_data
, width
);
1307 if (!HasMask() && image
.HasMask())
1309 unsigned char r
= image
.GetMaskRed();
1310 unsigned char g
= image
.GetMaskGreen();
1311 unsigned char b
= image
.GetMaskBlue();
1313 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1314 int source_step
= image
.GetWidth()*3;
1316 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1317 int target_step
= M_IMGDATA
->m_width
*3;
1319 for (int j
= 0; j
< height
; j
++)
1321 for (int i
= 0; i
< width
*3; i
+=3)
1323 if ((source_data
[i
] != r
) ||
1324 (source_data
[i
+1] != g
) ||
1325 (source_data
[i
+2] != b
))
1327 memcpy( target_data
+i
, source_data
+i
, 3 );
1330 source_data
+= source_step
;
1331 target_data
+= target_step
;
1336 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1337 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1339 wxCHECK_RET( Ok(), wxT("invalid image") );
1343 unsigned char *data
= GetData();
1345 const int w
= GetWidth();
1346 const int h
= GetHeight();
1348 for (int j
= 0; j
< h
; j
++)
1349 for (int i
= 0; i
< w
; i
++)
1351 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1361 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1365 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1367 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1369 unsigned char *dest
= image
.GetData();
1371 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1373 unsigned char *src
= M_IMGDATA
->m_data
;
1374 bool hasMask
= M_IMGDATA
->m_hasMask
;
1375 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1376 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1377 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1380 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1382 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1383 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1385 // don't modify the mask
1386 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1388 memcpy(dest
, src
, 3);
1392 // calculate the luma
1393 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1394 dest
[0] = dest
[1] = dest
[2] = static_cast<unsigned char>(luma
);
1398 // copy the alpha channel, if any
1401 const size_t alphaSize
= GetWidth() * GetHeight();
1402 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1403 memcpy(alpha
, GetAlpha(), alphaSize
);
1405 image
.SetAlpha(alpha
);
1411 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1415 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1417 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1419 unsigned char *data
= image
.GetData();
1421 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1423 if (M_IMGDATA
->m_hasMask
)
1425 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1426 M_IMGDATA
->m_maskBlue
== b
)
1427 image
.SetMaskColour( 255, 255, 255 );
1429 image
.SetMaskColour( 0, 0, 0 );
1432 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1434 unsigned char *srcd
= M_IMGDATA
->m_data
;
1435 unsigned char *tard
= image
.GetData();
1437 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1439 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1440 tard
[0] = tard
[1] = tard
[2] = 255;
1442 tard
[0] = tard
[1] = tard
[2] = 0;
1448 int wxImage::GetWidth() const
1450 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1452 return M_IMGDATA
->m_width
;
1455 int wxImage::GetHeight() const
1457 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1459 return M_IMGDATA
->m_height
;
1462 wxBitmapType
wxImage::GetType() const
1464 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1466 return M_IMGDATA
->m_type
;
1469 void wxImage::SetType(wxBitmapType type
)
1471 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1473 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1474 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1476 M_IMGDATA
->m_type
= type
;
1479 long wxImage::XYToIndex(int x
, int y
) const
1483 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1485 return y
*M_IMGDATA
->m_width
+ x
;
1491 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1493 long pos
= XYToIndex(x
, y
);
1494 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1500 M_IMGDATA
->m_data
[ pos
] = r
;
1501 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1502 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1505 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1507 wxCHECK_RET( Ok(), wxT("invalid image") );
1512 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1513 if ( rect
== wxRect() )
1519 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1520 imageRect
.Contains(rect
.GetBottomRight()),
1521 wxT("invalid bounding rectangle") );
1524 int x1
= rect
.GetLeft(),
1526 x2
= rect
.GetRight() + 1,
1527 y2
= rect
.GetBottom() + 1;
1529 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1530 int x
, y
, width
= GetWidth();
1531 for (y
= y1
; y
< y2
; y
++)
1533 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1534 for (x
= x1
; x
< x2
; x
++)
1543 unsigned char wxImage::GetRed( int x
, int y
) const
1545 long pos
= XYToIndex(x
, y
);
1546 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1550 return M_IMGDATA
->m_data
[pos
];
1553 unsigned char wxImage::GetGreen( int x
, int y
) const
1555 long pos
= XYToIndex(x
, y
);
1556 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1560 return M_IMGDATA
->m_data
[pos
+1];
1563 unsigned char wxImage::GetBlue( int x
, int y
) const
1565 long pos
= XYToIndex(x
, y
);
1566 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1570 return M_IMGDATA
->m_data
[pos
+2];
1573 bool wxImage::IsOk() const
1575 // image of 0 width or height can't be considered ok - at least because it
1576 // causes crashes in ConvertToBitmap() if we don't catch it in time
1577 wxImageRefData
*data
= M_IMGDATA
;
1578 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1581 unsigned char *wxImage::GetData() const
1583 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1585 return M_IMGDATA
->m_data
;
1588 void wxImage::SetData( unsigned char *data
, bool static_data
)
1590 wxCHECK_RET( Ok(), wxT("invalid image") );
1592 wxImageRefData
*newRefData
= new wxImageRefData();
1594 newRefData
->m_width
= M_IMGDATA
->m_width
;
1595 newRefData
->m_height
= M_IMGDATA
->m_height
;
1596 newRefData
->m_data
= data
;
1597 newRefData
->m_ok
= true;
1598 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1599 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1600 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1601 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1602 newRefData
->m_static
= static_data
;
1606 m_refData
= newRefData
;
1609 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1611 wxImageRefData
*newRefData
= new wxImageRefData();
1615 newRefData
->m_width
= new_width
;
1616 newRefData
->m_height
= new_height
;
1617 newRefData
->m_data
= data
;
1618 newRefData
->m_ok
= true;
1619 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1620 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1621 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1622 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1626 newRefData
->m_width
= new_width
;
1627 newRefData
->m_height
= new_height
;
1628 newRefData
->m_data
= data
;
1629 newRefData
->m_ok
= true;
1631 newRefData
->m_static
= static_data
;
1635 m_refData
= newRefData
;
1638 // ----------------------------------------------------------------------------
1639 // alpha channel support
1640 // ----------------------------------------------------------------------------
1642 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1644 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1646 long pos
= XYToIndex(x
, y
);
1647 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1651 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1654 unsigned char wxImage::GetAlpha(int x
, int y
) const
1656 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1658 long pos
= XYToIndex(x
, y
);
1659 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1661 return M_IMGDATA
->m_alpha
[pos
];
1665 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1669 const int w
= M_IMGDATA
->m_width
;
1670 const int h
= M_IMGDATA
->m_height
;
1672 unsigned char *alpha
= GetAlpha();
1673 unsigned char *data
= GetData();
1675 for ( int y
= 0; y
< h
; y
++ )
1677 for ( int x
= 0; x
< w
; x
++ )
1689 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1691 wxCHECK_RET( Ok(), wxT("invalid image") );
1697 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1700 if( !M_IMGDATA
->m_staticAlpha
)
1701 free(M_IMGDATA
->m_alpha
);
1703 M_IMGDATA
->m_alpha
= alpha
;
1704 M_IMGDATA
->m_staticAlpha
= static_data
;
1707 unsigned char *wxImage::GetAlpha() const
1709 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1711 return M_IMGDATA
->m_alpha
;
1714 void wxImage::InitAlpha()
1716 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1718 // initialize memory for alpha channel
1721 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1722 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1726 // use the mask to initialize the alpha channel.
1727 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1729 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1730 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1731 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1732 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1736 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1737 ? wxIMAGE_ALPHA_TRANSPARENT
1738 : wxIMAGE_ALPHA_OPAQUE
;
1741 M_IMGDATA
->m_hasMask
= false;
1745 // make the image fully opaque
1746 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1750 // ----------------------------------------------------------------------------
1752 // ----------------------------------------------------------------------------
1754 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1756 wxCHECK_RET( Ok(), wxT("invalid image") );
1760 M_IMGDATA
->m_maskRed
= r
;
1761 M_IMGDATA
->m_maskGreen
= g
;
1762 M_IMGDATA
->m_maskBlue
= b
;
1763 M_IMGDATA
->m_hasMask
= true;
1766 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1768 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1770 if (M_IMGDATA
->m_hasMask
)
1772 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1773 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1774 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1779 FindFirstUnusedColour(r
, g
, b
);
1784 unsigned char wxImage::GetMaskRed() const
1786 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1788 return M_IMGDATA
->m_maskRed
;
1791 unsigned char wxImage::GetMaskGreen() const
1793 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1795 return M_IMGDATA
->m_maskGreen
;
1798 unsigned char wxImage::GetMaskBlue() const
1800 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1802 return M_IMGDATA
->m_maskBlue
;
1805 void wxImage::SetMask( bool mask
)
1807 wxCHECK_RET( Ok(), wxT("invalid image") );
1811 M_IMGDATA
->m_hasMask
= mask
;
1814 bool wxImage::HasMask() const
1816 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1818 return M_IMGDATA
->m_hasMask
;
1821 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1823 long pos
= XYToIndex(x
, y
);
1824 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1827 if ( M_IMGDATA
->m_hasMask
)
1829 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1830 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1831 p
[1] == M_IMGDATA
->m_maskGreen
&&
1832 p
[2] == M_IMGDATA
->m_maskBlue
)
1839 if ( M_IMGDATA
->m_alpha
)
1841 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1843 // transparent enough
1852 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1853 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1855 // check that the images are the same size
1856 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1858 wxLogError( _("Image and mask have different sizes.") );
1862 // find unused colour
1863 unsigned char r
,g
,b
;
1864 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1866 wxLogError( _("No unused colour in image being masked.") );
1872 unsigned char *imgdata
= GetData();
1873 unsigned char *maskdata
= mask
.GetData();
1875 const int w
= GetWidth();
1876 const int h
= GetHeight();
1878 for (int j
= 0; j
< h
; j
++)
1880 for (int i
= 0; i
< w
; i
++)
1882 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1893 SetMaskColour(r
, g
, b
);
1899 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1904 unsigned char mr
, mg
, mb
;
1905 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1907 wxLogError( _("No unused colour in image being masked.") );
1914 SetMaskColour(mr
, mg
, mb
);
1916 unsigned char *imgdata
= GetData();
1917 unsigned char *alphadata
= GetAlpha();
1920 int h
= GetHeight();
1922 for (int y
= 0; y
< h
; y
++)
1924 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1926 if (*alphadata
< threshold
)
1935 if( !M_IMGDATA
->m_staticAlpha
)
1936 free(M_IMGDATA
->m_alpha
);
1938 M_IMGDATA
->m_alpha
= NULL
;
1939 M_IMGDATA
->m_staticAlpha
= false;
1944 // ----------------------------------------------------------------------------
1945 // Palette functions
1946 // ----------------------------------------------------------------------------
1950 bool wxImage::HasPalette() const
1955 return M_IMGDATA
->m_palette
.Ok();
1958 const wxPalette
& wxImage::GetPalette() const
1960 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1962 return M_IMGDATA
->m_palette
;
1965 void wxImage::SetPalette(const wxPalette
& palette
)
1967 wxCHECK_RET( Ok(), wxT("invalid image") );
1971 M_IMGDATA
->m_palette
= palette
;
1974 #endif // wxUSE_PALETTE
1976 // ----------------------------------------------------------------------------
1977 // Option functions (arbitrary name/value mapping)
1978 // ----------------------------------------------------------------------------
1980 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1984 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1985 if ( idx
== wxNOT_FOUND
)
1987 M_IMGDATA
->m_optionNames
.Add(name
);
1988 M_IMGDATA
->m_optionValues
.Add(value
);
1992 M_IMGDATA
->m_optionNames
[idx
] = name
;
1993 M_IMGDATA
->m_optionValues
[idx
] = value
;
1997 void wxImage::SetOption(const wxString
& name
, int value
)
2000 valStr
.Printf(wxT("%d"), value
);
2001 SetOption(name
, valStr
);
2004 wxString
wxImage::GetOption(const wxString
& name
) const
2007 return wxEmptyString
;
2009 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2010 if ( idx
== wxNOT_FOUND
)
2011 return wxEmptyString
;
2013 return M_IMGDATA
->m_optionValues
[idx
];
2016 int wxImage::GetOptionInt(const wxString
& name
) const
2018 return wxAtoi(GetOption(name
));
2021 bool wxImage::HasOption(const wxString
& name
) const
2023 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2027 // ----------------------------------------------------------------------------
2029 // ----------------------------------------------------------------------------
2031 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2032 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2033 int WXUNUSED_UNLESS_STREAMS(index
) )
2035 #if HAS_FILE_STREAMS
2036 if (wxFileExists(filename
))
2038 wxImageFileInputStream
stream(filename
);
2039 wxBufferedInputStream
bstream( stream
);
2040 return LoadFile(bstream
, type
, index
);
2044 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2048 #else // !HAS_FILE_STREAMS
2050 #endif // HAS_FILE_STREAMS
2053 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2054 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2055 int WXUNUSED_UNLESS_STREAMS(index
) )
2057 #if HAS_FILE_STREAMS
2058 if (wxFileExists(filename
))
2060 wxImageFileInputStream
stream(filename
);
2061 wxBufferedInputStream
bstream( stream
);
2062 return LoadFile(bstream
, mimetype
, index
);
2066 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2070 #else // !HAS_FILE_STREAMS
2072 #endif // HAS_FILE_STREAMS
2076 bool wxImage::SaveFile( const wxString
& filename
) const
2078 wxString ext
= filename
.AfterLast('.').Lower();
2080 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2083 wxLogError(_("Can't save image to file '%s': unknown extension."),
2088 return SaveFile(filename
, handler
->GetType());
2091 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2092 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2094 #if HAS_FILE_STREAMS
2095 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2097 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2099 wxImageFileOutputStream
stream(filename
);
2101 if ( stream
.IsOk() )
2103 wxBufferedOutputStream
bstream( stream
);
2104 return SaveFile(bstream
, type
);
2106 #endif // HAS_FILE_STREAMS
2111 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2112 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2114 #if HAS_FILE_STREAMS
2115 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2117 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2119 wxImageFileOutputStream
stream(filename
);
2121 if ( stream
.IsOk() )
2123 wxBufferedOutputStream
bstream( stream
);
2124 return SaveFile(bstream
, mimetype
);
2126 #endif // HAS_FILE_STREAMS
2131 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2133 #if HAS_FILE_STREAMS
2134 wxImageFileInputStream
stream(name
);
2135 return CanRead(stream
);
2141 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2142 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2144 #if HAS_FILE_STREAMS
2145 wxImageFileInputStream
stream(name
);
2147 return GetImageCount(stream
, type
);
2155 bool wxImage::CanRead( wxInputStream
&stream
)
2157 const wxList
& list
= GetHandlers();
2159 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2161 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2162 if (handler
->CanRead( stream
))
2169 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2171 wxImageHandler
*handler
;
2173 if ( type
== wxBITMAP_TYPE_ANY
)
2175 const wxList
& list
= GetHandlers();
2177 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2179 node
= node
->GetNext() )
2181 handler
= (wxImageHandler
*)node
->GetData();
2182 if ( handler
->CanRead(stream
) )
2184 const int count
= handler
->GetImageCount(stream
);
2191 wxLogWarning(_("No handler found for image type."));
2195 handler
= FindHandler(type
);
2199 wxLogWarning(_("No image handler for type %ld defined."), type
);
2203 if ( handler
->CanRead(stream
) )
2205 return handler
->GetImageCount(stream
);
2209 wxLogError(_("Image file is not of type %ld."), type
);
2214 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2216 // save the options values which can be clobbered by the handler (e.g. many
2217 // of them call Destroy() before trying to load the file)
2218 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2219 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2221 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2224 M_IMGDATA
->m_type
= handler
.GetType();
2226 // rescale the image to the specified size if needed
2227 if ( maxWidth
|| maxHeight
)
2229 const unsigned widthOrig
= GetWidth(),
2230 heightOrig
= GetHeight();
2232 // this uses the same (trivial) algorithm as the JPEG handler
2233 unsigned width
= widthOrig
,
2234 height
= heightOrig
;
2235 while ( (maxWidth
&& width
> maxWidth
) ||
2236 (maxHeight
&& height
> maxHeight
) )
2242 if ( width
!= widthOrig
|| height
!= heightOrig
)
2243 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2249 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2253 wxImageHandler
*handler
;
2255 if ( type
== wxBITMAP_TYPE_ANY
)
2257 const wxList
& list
= GetHandlers();
2258 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2260 node
= node
->GetNext() )
2262 handler
= (wxImageHandler
*)node
->GetData();
2263 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2267 wxLogWarning( _("No handler found for image type.") );
2271 //else: have specific type
2273 handler
= FindHandler(type
);
2276 wxLogWarning( _("No image handler for type %ld defined."), type
);
2280 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2282 wxLogError(_("Image file is not of type %ld."), type
);
2286 return DoLoad(*handler
, stream
, index
);
2289 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2293 m_refData
= new wxImageRefData
;
2295 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2299 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2303 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2305 wxLogError(_("Image file is not of type %s."), mimetype
);
2309 return DoLoad(*handler
, stream
, index
);
2312 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2314 wxImage
* const self
= const_cast<wxImage
*>(this);
2315 if ( !handler
.SaveFile(self
, stream
) )
2318 M_IMGDATA
->m_type
= handler
.GetType();
2322 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2324 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2326 wxImageHandler
*handler
= FindHandler(type
);
2329 wxLogWarning( _("No image handler for type %d defined."), type
);
2333 return DoSave(*handler
, stream
);
2336 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2338 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2340 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2343 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2346 return DoSave(*handler
, stream
);
2349 #endif // wxUSE_STREAMS
2351 // ----------------------------------------------------------------------------
2352 // image I/O handlers
2353 // ----------------------------------------------------------------------------
2355 void wxImage::AddHandler( wxImageHandler
*handler
)
2357 // Check for an existing handler of the type being added.
2358 if (FindHandler( handler
->GetType() ) == 0)
2360 sm_handlers
.Append( handler
);
2364 // This is not documented behaviour, merely the simplest 'fix'
2365 // for preventing duplicate additions. If someone ever has
2366 // a good reason to add and remove duplicate handlers (and they
2367 // may) we should probably refcount the duplicates.
2368 // also an issue in InsertHandler below.
2370 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2371 handler
->GetName().c_str() );
2376 void wxImage::InsertHandler( wxImageHandler
*handler
)
2378 // Check for an existing handler of the type being added.
2379 if (FindHandler( handler
->GetType() ) == 0)
2381 sm_handlers
.Insert( handler
);
2385 // see AddHandler for additional comments.
2386 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2387 handler
->GetName().c_str() );
2392 bool wxImage::RemoveHandler( const wxString
& name
)
2394 wxImageHandler
*handler
= FindHandler(name
);
2397 sm_handlers
.DeleteObject(handler
);
2405 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2407 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2410 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2411 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2413 node
= node
->GetNext();
2418 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2420 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2423 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2424 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2425 ( (bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
)) )
2429 node
= node
->GetNext();
2434 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2436 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2439 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2440 if (handler
->GetType() == bitmapType
) return handler
;
2441 node
= node
->GetNext();
2446 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2448 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2451 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2452 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2453 node
= node
->GetNext();
2458 void wxImage::InitStandardHandlers()
2461 AddHandler(new wxBMPHandler
);
2462 #endif // wxUSE_STREAMS
2465 void wxImage::CleanUpHandlers()
2467 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2470 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2471 wxList::compatibility_iterator next
= node
->GetNext();
2476 sm_handlers
.Clear();
2479 wxString
wxImage::GetImageExtWildcard()
2483 wxList
& Handlers
= wxImage::GetHandlers();
2484 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2487 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2488 fmts
+= wxT("*.") + Handler
->GetExtension();
2489 Node
= Node
->GetNext();
2490 if ( Node
) fmts
+= wxT(";");
2493 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2496 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2498 const double red
= rgb
.red
/ 255.0,
2499 green
= rgb
.green
/ 255.0,
2500 blue
= rgb
.blue
/ 255.0;
2502 // find the min and max intensity (and remember which one was it for the
2504 double minimumRGB
= red
;
2505 if ( green
< minimumRGB
)
2507 if ( blue
< minimumRGB
)
2510 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2511 double maximumRGB
= red
;
2512 if ( green
> maximumRGB
)
2517 if ( blue
> maximumRGB
)
2523 const double value
= maximumRGB
;
2525 double hue
= 0.0, saturation
;
2526 const double deltaRGB
= maximumRGB
- minimumRGB
;
2527 if ( wxIsNullDouble(deltaRGB
) )
2529 // Gray has no color
2538 hue
= (green
- blue
) / deltaRGB
;
2542 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2546 hue
= 4.0 + (red
- green
) / deltaRGB
;
2550 wxFAIL_MSG(wxT("hue not specified"));
2559 saturation
= deltaRGB
/ maximumRGB
;
2562 return HSVValue(hue
, saturation
, value
);
2565 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2567 double red
, green
, blue
;
2569 if ( wxIsNullDouble(hsv
.saturation
) )
2578 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2579 int i
= (int)floor(hue
);
2580 double f
= hue
- i
; // fractional part of h
2581 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2587 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2592 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2600 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2605 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2610 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2618 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2623 return RGBValue((unsigned char)(red
* 255.0),
2624 (unsigned char)(green
* 255.0),
2625 (unsigned char)(blue
* 255.0));
2629 * Rotates the hue of each pixel of the image. angle is a double in the range
2630 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2632 void wxImage::RotateHue(double angle
)
2636 unsigned char *srcBytePtr
;
2637 unsigned char *dstBytePtr
;
2638 unsigned long count
;
2639 wxImage::HSVValue hsv
;
2640 wxImage::RGBValue rgb
;
2642 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2643 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2644 if ( count
> 0 && !wxIsNullDouble(angle
) )
2646 srcBytePtr
= M_IMGDATA
->m_data
;
2647 dstBytePtr
= srcBytePtr
;
2650 rgb
.red
= *srcBytePtr
++;
2651 rgb
.green
= *srcBytePtr
++;
2652 rgb
.blue
= *srcBytePtr
++;
2653 hsv
= RGBtoHSV(rgb
);
2655 hsv
.hue
= hsv
.hue
+ angle
;
2657 hsv
.hue
= hsv
.hue
- 1.0;
2658 else if (hsv
.hue
< 0.0)
2659 hsv
.hue
= hsv
.hue
+ 1.0;
2661 rgb
= HSVtoRGB(hsv
);
2662 *dstBytePtr
++ = rgb
.red
;
2663 *dstBytePtr
++ = rgb
.green
;
2664 *dstBytePtr
++ = rgb
.blue
;
2665 } while (--count
!= 0);
2669 //-----------------------------------------------------------------------------
2671 //-----------------------------------------------------------------------------
2673 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2676 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2681 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2686 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2691 bool wxImageHandler::CanRead( const wxString
& name
)
2693 if (wxFileExists(name
))
2695 wxImageFileInputStream
stream(name
);
2696 return CanRead(stream
);
2699 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2704 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2706 wxFileOffset posOld
= stream
.TellI();
2707 if ( posOld
== wxInvalidOffset
)
2709 // can't test unseekable stream
2713 bool ok
= DoCanRead(stream
);
2715 // restore the old position to be able to test other formats and so on
2716 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2718 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2720 // reading would fail anyhow as we're not at the right position
2727 #endif // wxUSE_STREAMS
2731 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2733 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, _T("NULL pointer") );
2735 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2736 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2738 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2739 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2741 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2744 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2746 else // no resolution options specified
2751 return wxIMAGE_RESOLUTION_NONE
;
2754 // get the resolution unit too
2755 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2758 // this is the default
2759 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2762 return (wxImageResolution
)resUnit
;
2765 // ----------------------------------------------------------------------------
2766 // image histogram stuff
2767 // ----------------------------------------------------------------------------
2770 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2775 unsigned char g2
) const
2777 unsigned long key
= MakeKey(r2
, g2
, b2
);
2779 while ( find(key
) != end() )
2781 // color already used
2793 wxLogError(_("No unused colour in image.") );
2799 key
= MakeKey(r2
, g2
, b2
);
2813 wxImage::FindFirstUnusedColour(unsigned char *r
,
2818 unsigned char g2
) const
2820 wxImageHistogram histogram
;
2822 ComputeHistogram(histogram
);
2824 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2830 // Counts and returns the number of different colours. Optionally stops
2831 // when it exceeds 'stopafter' different colours. This is useful, for
2832 // example, to see if the image can be saved as 8-bit (256 colour or
2833 // less, in this case it would be invoked as CountColours(256)). Default
2834 // value for stopafter is -1 (don't care).
2836 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2840 unsigned char r
, g
, b
;
2842 unsigned long size
, nentries
, key
;
2845 size
= GetWidth() * GetHeight();
2848 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2853 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2855 if (h
.Get(key
) == NULL
)
2866 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2868 unsigned char *p
= GetData();
2869 unsigned long nentries
= 0;
2873 const unsigned long size
= GetWidth() * GetHeight();
2875 unsigned char r
, g
, b
;
2876 for ( unsigned long n
= 0; n
< size
; n
++ )
2882 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2884 if ( entry
.value
++ == 0 )
2885 entry
.index
= nentries
++;
2892 * Rotation code by Carlos Moreno
2895 static const double wxROTATE_EPSILON
= 1e-10;
2897 // Auxiliary function to rotate a point (x,y) with respect to point p0
2898 // make it inline and use a straight return to facilitate optimization
2899 // also, the function receives the sine and cosine of the angle to avoid
2900 // repeating the time-consuming calls to these functions -- sin/cos can
2901 // be computed and stored in the calling function.
2903 static inline wxRealPoint
2904 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2905 const wxRealPoint
& p0
)
2907 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2908 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2911 static inline wxRealPoint
2912 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2913 const wxRealPoint
& p0
)
2915 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2918 wxImage
wxImage::Rotate(double angle
,
2919 const wxPoint
& centre_of_rotation
,
2921 wxPoint
*offset_after_rotation
) const
2923 // screen coordinates are a mirror image of "real" coordinates
2926 const bool has_alpha
= HasAlpha();
2928 const int w
= GetWidth();
2929 const int h
= GetHeight();
2933 // Create pointer-based array to accelerate access to wxImage's data
2934 unsigned char ** data
= new unsigned char * [h
];
2935 data
[0] = GetData();
2936 for (i
= 1; i
< h
; i
++)
2937 data
[i
] = data
[i
- 1] + (3 * w
);
2939 // Same for alpha channel
2940 unsigned char ** alpha
= NULL
;
2943 alpha
= new unsigned char * [h
];
2944 alpha
[0] = GetAlpha();
2945 for (i
= 1; i
< h
; i
++)
2946 alpha
[i
] = alpha
[i
- 1] + w
;
2949 // precompute coefficients for rotation formula
2950 const double cos_angle
= cos(angle
);
2951 const double sin_angle
= sin(angle
);
2953 // Create new Image to store the result
2954 // First, find rectangle that covers the rotated image; to do that,
2955 // rotate the four corners
2957 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2959 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2960 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
2961 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
2962 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
2964 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2965 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2966 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2967 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2969 // Create rotated image
2970 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2971 // With alpha channel
2975 if (offset_after_rotation
!= NULL
)
2977 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2980 // the rotated (destination) image is always accessed sequentially via this
2981 // pointer, there is no need for pointer-based arrays here
2982 unsigned char *dst
= rotated
.GetData();
2984 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
2986 // if the original image has a mask, use its RGB values as the blank pixel,
2987 // else, fall back to default (black).
2988 unsigned char blank_r
= 0;
2989 unsigned char blank_g
= 0;
2990 unsigned char blank_b
= 0;
2994 blank_r
= GetMaskRed();
2995 blank_g
= GetMaskGreen();
2996 blank_b
= GetMaskBlue();
2997 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
3000 // Now, for each point of the rotated image, find where it came from, by
3001 // performing an inverse rotation (a rotation of -angle) and getting the
3002 // pixel at those coordinates
3004 const int rH
= rotated
.GetHeight();
3005 const int rW
= rotated
.GetWidth();
3007 // do the (interpolating) test outside of the loops, so that it is done
3008 // only once, instead of repeating it for each pixel.
3011 for (int y
= 0; y
< rH
; y
++)
3013 for (int x
= 0; x
< rW
; x
++)
3015 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3017 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3018 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3020 // interpolate using the 4 enclosing grid-points. Those
3021 // points can be obtained using floor and ceiling of the
3022 // exact coordinates of the point
3025 if (0 < src
.x
&& src
.x
< w
- 1)
3027 x1
= wxRound(floor(src
.x
));
3028 x2
= wxRound(ceil(src
.x
));
3030 else // else means that x is near one of the borders (0 or width-1)
3032 x1
= x2
= wxRound (src
.x
);
3035 if (0 < src
.y
&& src
.y
< h
- 1)
3037 y1
= wxRound(floor(src
.y
));
3038 y2
= wxRound(ceil(src
.y
));
3042 y1
= y2
= wxRound (src
.y
);
3045 // get four points and the distances (square of the distance,
3046 // for efficiency reasons) for the interpolation formula
3048 // GRG: Do not calculate the points until they are
3049 // really needed -- this way we can calculate
3050 // just one, instead of four, if d1, d2, d3
3051 // or d4 are < wxROTATE_EPSILON
3053 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3054 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3055 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3056 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3058 // Now interpolate as a weighted average of the four surrounding
3059 // points, where the weights are the distances to each of those points
3061 // If the point is exactly at one point of the grid of the source
3062 // image, then don't interpolate -- just assign the pixel
3064 // d1,d2,d3,d4 are positive -- no need for abs()
3065 if (d1
< wxROTATE_EPSILON
)
3067 unsigned char *p
= data
[y1
] + (3 * x1
);
3073 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3075 else if (d2
< wxROTATE_EPSILON
)
3077 unsigned char *p
= data
[y1
] + (3 * x2
);
3083 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3085 else if (d3
< wxROTATE_EPSILON
)
3087 unsigned char *p
= data
[y2
] + (3 * x2
);
3093 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3095 else if (d4
< wxROTATE_EPSILON
)
3097 unsigned char *p
= data
[y2
] + (3 * x1
);
3103 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3107 // weights for the weighted average are proportional to the inverse of the distance
3108 unsigned char *v1
= data
[y1
] + (3 * x1
);
3109 unsigned char *v2
= data
[y1
] + (3 * x2
);
3110 unsigned char *v3
= data
[y2
] + (3 * x2
);
3111 unsigned char *v4
= data
[y2
] + (3 * x1
);
3113 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3117 *(dst
++) = (unsigned char)
3118 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3119 w3
* *(v3
++) + w4
* *(v4
++)) /
3120 (w1
+ w2
+ w3
+ w4
) );
3121 *(dst
++) = (unsigned char)
3122 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3123 w3
* *(v3
++) + w4
* *(v4
++)) /
3124 (w1
+ w2
+ w3
+ w4
) );
3125 *(dst
++) = (unsigned char)
3126 ( (w1
* *v1
+ w2
* *v2
+
3127 w3
* *v3
+ w4
* *v4
) /
3128 (w1
+ w2
+ w3
+ w4
) );
3132 v1
= alpha
[y1
] + (x1
);
3133 v2
= alpha
[y1
] + (x2
);
3134 v3
= alpha
[y2
] + (x2
);
3135 v4
= alpha
[y2
] + (x1
);
3137 *(alpha_dst
++) = (unsigned char)
3138 ( (w1
* *v1
+ w2
* *v2
+
3139 w3
* *v3
+ w4
* *v4
) /
3140 (w1
+ w2
+ w3
+ w4
) );
3156 else // not interpolating
3158 for (int y
= 0; y
< rH
; y
++)
3160 for (int x
= 0; x
< rW
; x
++)
3162 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3164 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3165 const int ys
= wxRound (src
.y
); // closest integer
3167 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3169 unsigned char *p
= data
[ys
] + (3 * xs
);
3175 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3184 *(alpha_dst
++) = 255;
3200 // A module to allow wxImage initialization/cleanup
3201 // without calling these functions from app.cpp or from
3202 // the user's application.
3204 class wxImageModule
: public wxModule
3206 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3209 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3210 void OnExit() { wxImage::CleanUpHandlers(); }
3213 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3216 #endif // wxUSE_IMAGE