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 wx_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
;
243 void wxImage::Destroy()
248 wxObjectRefData
* wxImage::CreateRefData() const
250 return new wxImageRefData
;
253 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
255 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
256 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
258 wxImageRefData
* refData_new
= new wxImageRefData
;
259 refData_new
->m_width
= refData
->m_width
;
260 refData_new
->m_height
= refData
->m_height
;
261 refData_new
->m_maskRed
= refData
->m_maskRed
;
262 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
263 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
264 refData_new
->m_hasMask
= refData
->m_hasMask
;
265 refData_new
->m_ok
= true;
266 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
267 if (refData
->m_alpha
!= NULL
)
269 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
270 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
273 refData_new
->m_data
= (unsigned char*)malloc(size
);
274 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
276 refData_new
->m_palette
= refData
->m_palette
;
278 refData_new
->m_optionNames
= refData
->m_optionNames
;
279 refData_new
->m_optionValues
= refData
->m_optionValues
;
283 wxImage
wxImage::Copy() const
287 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
289 image
.m_refData
= CloneRefData(m_refData
);
294 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
296 if( xFactor
== 1 && yFactor
== 1 )
301 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
303 // can't scale to/from 0 size
304 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
305 wxT("invalid new image size") );
307 long old_height
= M_IMGDATA
->m_height
,
308 old_width
= M_IMGDATA
->m_width
;
310 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
311 wxT("invalid old image size") );
313 long width
= old_width
/ xFactor
;
314 long height
= old_height
/ yFactor
;
316 image
.Create( width
, height
, false );
318 char unsigned *data
= image
.GetData();
320 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
322 bool hasMask
= false ;
323 unsigned char maskRed
= 0;
324 unsigned char maskGreen
= 0;
325 unsigned char maskBlue
=0 ;
327 unsigned char *source_data
= M_IMGDATA
->m_data
;
328 unsigned char *target_data
= data
;
329 unsigned char *source_alpha
= 0 ;
330 unsigned char *target_alpha
= 0 ;
331 if (M_IMGDATA
->m_hasMask
)
334 maskRed
= M_IMGDATA
->m_maskRed
;
335 maskGreen
= M_IMGDATA
->m_maskGreen
;
336 maskBlue
=M_IMGDATA
->m_maskBlue
;
338 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
339 M_IMGDATA
->m_maskGreen
,
340 M_IMGDATA
->m_maskBlue
);
344 source_alpha
= M_IMGDATA
->m_alpha
;
348 target_alpha
= image
.GetAlpha() ;
352 for (long y
= 0; y
< height
; y
++)
354 for (long x
= 0; x
< width
; x
++)
356 unsigned long avgRed
= 0 ;
357 unsigned long avgGreen
= 0;
358 unsigned long avgBlue
= 0;
359 unsigned long avgAlpha
= 0 ;
360 unsigned long counter
= 0 ;
362 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
364 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
365 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
367 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
368 unsigned char red
= pixel
[0] ;
369 unsigned char green
= pixel
[1] ;
370 unsigned char blue
= pixel
[2] ;
371 unsigned char alpha
= 255 ;
373 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
374 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
389 *(target_data
++) = M_IMGDATA
->m_maskRed
;
390 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
391 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
396 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
397 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
398 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
399 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
404 // In case this is a cursor, make sure the hotspot is scaled accordingly:
405 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
406 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
407 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
408 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
409 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
410 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
415 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
419 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
421 // can't scale to/from 0 size
422 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
423 wxT("invalid new image size") );
425 long old_height
= M_IMGDATA
->m_height
,
426 old_width
= M_IMGDATA
->m_width
;
427 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
428 wxT("invalid old image size") );
430 // If the image's new width and height are the same as the original, no
431 // need to waste time or CPU cycles
432 if ( old_width
== width
&& old_height
== height
)
435 // Scale the image (...or more appropriately, resample the image) using
436 // either the high-quality or normal method as specified
437 if ( quality
== wxIMAGE_QUALITY_HIGH
)
439 // We need to check whether we are downsampling or upsampling the image
440 if ( width
< old_width
&& height
< old_height
)
442 // Downsample the image using the box averaging method for best results
443 image
= ResampleBox(width
, height
);
447 // For upsampling or other random/wierd image dimensions we'll use
448 // a bicubic b-spline scaling method
449 image
= ResampleBicubic(width
, height
);
452 else // Default scaling method == simple pixel replication
454 if ( old_width
% width
== 0 && old_width
>= width
&&
455 old_height
% height
== 0 && old_height
>= height
)
457 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
459 image
.Create( width
, height
, false );
461 unsigned char *data
= image
.GetData();
463 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
465 unsigned char *source_data
= M_IMGDATA
->m_data
;
466 unsigned char *target_data
= data
;
467 unsigned char *source_alpha
= 0 ;
468 unsigned char *target_alpha
= 0 ;
470 if ( !M_IMGDATA
->m_hasMask
)
472 source_alpha
= M_IMGDATA
->m_alpha
;
476 target_alpha
= image
.GetAlpha() ;
480 long x_delta
= (old_width
<<16) / width
;
481 long y_delta
= (old_height
<<16) / height
;
483 unsigned char* dest_pixel
= target_data
;
486 for ( long j
= 0; j
< height
; j
++ )
488 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
489 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
492 for ( long i
= 0; i
< width
; i
++ )
494 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
495 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
496 dest_pixel
[0] = src_pixel
[0];
497 dest_pixel
[1] = src_pixel
[1];
498 dest_pixel
[2] = src_pixel
[2];
501 *(target_alpha
++) = *src_alpha_pixel
;
509 // If the original image has a mask, apply the mask to the new image
510 if (M_IMGDATA
->m_hasMask
)
512 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
513 M_IMGDATA
->m_maskGreen
,
514 M_IMGDATA
->m_maskBlue
);
517 // In case this is a cursor, make sure the hotspot is scaled accordingly:
518 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
519 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
520 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
521 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
522 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
523 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
528 wxImage
wxImage::ResampleBox(int width
, int height
) const
530 // This function implements a simple pre-blur/box averaging method for
531 // downsampling that gives reasonably smooth results To scale the image
532 // down we will need to gather a grid of pixels of the size of the scale
533 // factor in each direction and then do an averaging of the pixels.
535 wxImage
ret_image(width
, height
, false);
537 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
538 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
540 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
541 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
543 unsigned char* src_data
= M_IMGDATA
->m_data
;
544 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
545 unsigned char* dst_data
= ret_image
.GetData();
546 unsigned char* dst_alpha
= NULL
;
550 ret_image
.SetAlpha();
551 dst_alpha
= ret_image
.GetAlpha();
554 int averaged_pixels
, src_pixel_index
;
555 double sum_r
, sum_g
, sum_b
, sum_a
;
557 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
559 // Source pixel in the Y direction
560 int src_y
= (int)(y
* scale_factor_y
);
562 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
564 // Source pixel in the X direction
565 int src_x
= (int)(x
* scale_factor_x
);
567 // Box of pixels to average
569 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
571 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
572 j
<= int(src_y
+ scale_factor_y_2
);
575 // We don't care to average pixels that don't exist (edges)
576 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
579 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
580 i
<= src_x
+ scale_factor_x_2
;
583 // Don't average edge pixels
584 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
587 // Calculate the actual index in our source pixels
588 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
590 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
591 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
592 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
594 sum_a
+= src_alpha
[src_pixel_index
];
600 // Calculate the average from the sum and number of averaged pixels
601 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
602 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
603 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
606 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
613 // The following two local functions are for the B-spline weighting of the
614 // bicubic sampling algorithm
615 static inline double spline_cube(double value
)
617 return value
<= 0.0 ? 0.0 : value
* value
* value
;
620 static inline double spline_weight(double value
)
622 return (spline_cube(value
+ 2) -
623 4 * spline_cube(value
+ 1) +
624 6 * spline_cube(value
) -
625 4 * spline_cube(value
- 1)) / 6;
628 // This is the bicubic resampling algorithm
629 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
631 // This function implements a Bicubic B-Spline algorithm for resampling.
632 // This method is certainly a little slower than wxImage's default pixel
633 // replication method, however for most reasonably sized images not being
634 // upsampled too much on a fairly average CPU this difference is hardly
635 // noticeable and the results are far more pleasing to look at.
637 // This particular bicubic algorithm does pixel weighting according to a
638 // B-Spline that basically implements a Gaussian bell-like weighting
639 // kernel. Because of this method the results may appear a bit blurry when
640 // upsampling by large factors. This is basically because a slight
641 // gaussian blur is being performed to get the smooth look of the upsampled
644 // Edge pixels: 3-4 possible solutions
645 // - (Wrap/tile) Wrap the image, take the color value from the opposite
646 // side of the image.
647 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
648 // where n is nonpositive, will have the value of (2, 1).
649 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
650 // pixels which do have all neighbours.
651 // - (Clamp) Choose the nearest pixel along the border. This takes the
652 // border pixels and extends them out to infinity.
654 // NOTE: below the y_offset and x_offset variables are being set for edge
655 // pixels using the "Mirror" method mentioned above
659 ret_image
.Create(width
, height
, false);
661 unsigned char* src_data
= M_IMGDATA
->m_data
;
662 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
663 unsigned char* dst_data
= ret_image
.GetData();
664 unsigned char* dst_alpha
= NULL
;
668 ret_image
.SetAlpha();
669 dst_alpha
= ret_image
.GetAlpha();
672 for ( int dsty
= 0; dsty
< height
; dsty
++ )
674 // We need to calculate the source pixel to interpolate from - Y-axis
675 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
676 double dy
= srcpixy
- (int)srcpixy
;
678 for ( int dstx
= 0; dstx
< width
; dstx
++ )
680 // X-axis of pixel to interpolate from
681 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
682 double dx
= srcpixx
- (int)srcpixx
;
684 // Sums for each color channel
685 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
687 // Here we actually determine the RGBA values for the destination pixel
688 for ( int k
= -1; k
<= 2; k
++ )
691 int y_offset
= srcpixy
+ k
< 0.0
693 : srcpixy
+ k
>= M_IMGDATA
->m_height
694 ? M_IMGDATA
->m_height
- 1
695 : (int)(srcpixy
+ k
);
697 // Loop across the X axis
698 for ( int i
= -1; i
<= 2; i
++ )
701 int x_offset
= srcpixx
+ i
< 0.0
703 : srcpixx
+ i
>= M_IMGDATA
->m_width
704 ? M_IMGDATA
->m_width
- 1
705 : (int)(srcpixx
+ i
);
707 // Calculate the exact position where the source data
708 // should be pulled from based on the x_offset and y_offset
709 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
711 // Calculate the weight for the specified pixel according
712 // to the bicubic b-spline kernel we're using for
715 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
717 // Create a sum of all velues for each color channel
718 // adjusted for the pixel's calculated weight
719 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
720 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
721 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
723 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
727 // Put the data into the destination image. The summed values are
728 // of double data type and are rounded here for accuracy
729 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
730 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
731 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
735 *dst_alpha
++ = (unsigned char)sum_a
;
742 // Blur in the horizontal direction
743 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
746 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
748 unsigned char* src_data
= M_IMGDATA
->m_data
;
749 unsigned char* dst_data
= ret_image
.GetData();
750 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
751 unsigned char* dst_alpha
= NULL
;
753 // Check for a mask or alpha
756 ret_image
.SetAlpha();
757 dst_alpha
= ret_image
.GetAlpha();
759 else if ( M_IMGDATA
->m_hasMask
)
761 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
762 M_IMGDATA
->m_maskGreen
,
763 M_IMGDATA
->m_maskBlue
);
766 // number of pixels we average over
767 const int blurArea
= blurRadius
*2 + 1;
769 // Horizontal blurring algorithm - average all pixels in the specified blur
770 // radius in the X or horizontal direction
771 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
773 // Variables used in the blurring algorithm
780 const unsigned char *src
;
783 // Calculate the average of all pixels in the blur radius for the first
785 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
787 // To deal with the pixels at the start of a row so it's not
788 // grabbing GOK values from memory at negative indices of the
789 // image's data or grabbing from the previous row
791 pixel_idx
= y
* M_IMGDATA
->m_width
;
793 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
795 src
= src_data
+ pixel_idx
*3;
800 sum_a
+= src_alpha
[pixel_idx
];
803 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
804 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
805 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
806 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
808 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
810 // Now average the values of the rest of the pixels by just moving the
811 // blur radius box along the row
812 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
814 // Take care of edge pixels on the left edge by essentially
815 // duplicating the edge pixel
816 if ( x
- blurRadius
- 1 < 0 )
817 pixel_idx
= y
* M_IMGDATA
->m_width
;
819 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
821 // Subtract the value of the pixel at the left side of the blur
823 src
= src_data
+ pixel_idx
*3;
828 sum_a
-= src_alpha
[pixel_idx
];
830 // Take care of edge pixels on the right edge
831 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
832 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
834 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
836 // Add the value of the pixel being added to the end of our box
837 src
= src_data
+ pixel_idx
*3;
842 sum_a
+= src_alpha
[pixel_idx
];
844 // Save off the averaged data
845 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
846 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
847 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
848 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
850 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
857 // Blur in the vertical direction
858 wxImage
wxImage::BlurVertical(int blurRadius
) const
861 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
863 unsigned char* src_data
= M_IMGDATA
->m_data
;
864 unsigned char* dst_data
= ret_image
.GetData();
865 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
866 unsigned char* dst_alpha
= NULL
;
868 // Check for a mask or alpha
871 ret_image
.SetAlpha();
872 dst_alpha
= ret_image
.GetAlpha();
874 else if ( M_IMGDATA
->m_hasMask
)
876 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
877 M_IMGDATA
->m_maskGreen
,
878 M_IMGDATA
->m_maskBlue
);
881 // number of pixels we average over
882 const int blurArea
= blurRadius
*2 + 1;
884 // Vertical blurring algorithm - same as horizontal but switched the
885 // opposite direction
886 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
888 // Variables used in the blurring algorithm
895 const unsigned char *src
;
898 // Calculate the average of all pixels in our blur radius box for the
899 // first pixel of the column
900 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
902 // To deal with the pixels at the start of a column so it's not
903 // grabbing GOK values from memory at negative indices of the
904 // image's data or grabbing from the previous column
908 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
910 src
= src_data
+ pixel_idx
*3;
915 sum_a
+= src_alpha
[pixel_idx
];
918 dst
= dst_data
+ x
*3;
919 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
920 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
921 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
923 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
925 // Now average the values of the rest of the pixels by just moving the
926 // box along the column from top to bottom
927 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
929 // Take care of pixels that would be beyond the top edge by
930 // duplicating the top edge pixel for the column
931 if ( y
- blurRadius
- 1 < 0 )
934 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
936 // Subtract the value of the pixel at the top of our blur radius box
937 src
= src_data
+ pixel_idx
*3;
942 sum_a
-= src_alpha
[pixel_idx
];
944 // Take care of the pixels that would be beyond the bottom edge of
945 // the image similar to the top edge
946 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
947 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
949 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
951 // Add the value of the pixel being added to the end of our box
952 src
= src_data
+ pixel_idx
*3;
957 sum_a
+= src_alpha
[pixel_idx
];
959 // Save off the averaged data
960 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
961 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
962 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
963 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
965 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
972 // The new blur function
973 wxImage
wxImage::Blur(int blurRadius
) const
976 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
978 // Blur the image in each direction
979 ret_image
= BlurHorizontal(blurRadius
);
980 ret_image
= ret_image
.BlurVertical(blurRadius
);
985 wxImage
wxImage::Rotate90( bool clockwise
) const
989 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
991 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
993 unsigned char *data
= image
.GetData();
995 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
997 unsigned char *source_data
= M_IMGDATA
->m_data
;
998 unsigned char *target_data
;
999 unsigned char *alpha_data
= 0 ;
1000 unsigned char *source_alpha
= 0 ;
1001 unsigned char *target_alpha
= 0 ;
1003 if (M_IMGDATA
->m_hasMask
)
1005 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1009 source_alpha
= M_IMGDATA
->m_alpha
;
1013 alpha_data
= image
.GetAlpha() ;
1017 long height
= M_IMGDATA
->m_height
;
1018 long width
= M_IMGDATA
->m_width
;
1020 for (long j
= 0; j
< height
; j
++)
1022 for (long i
= 0; i
< width
; i
++)
1026 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1028 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1032 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1034 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1036 memcpy( target_data
, source_data
, 3 );
1041 memcpy( target_alpha
, source_alpha
, 1 );
1050 wxImage
wxImage::Mirror( bool horizontally
) const
1054 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1056 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1058 unsigned char *data
= image
.GetData();
1059 unsigned char *alpha
= NULL
;
1061 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1063 if (M_IMGDATA
->m_alpha
!= NULL
) {
1065 alpha
= image
.GetAlpha();
1066 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1069 if (M_IMGDATA
->m_hasMask
)
1070 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1072 long height
= M_IMGDATA
->m_height
;
1073 long width
= M_IMGDATA
->m_width
;
1075 unsigned char *source_data
= M_IMGDATA
->m_data
;
1076 unsigned char *target_data
;
1080 for (long j
= 0; j
< height
; j
++)
1083 target_data
= data
-3;
1084 for (long i
= 0; i
< width
; i
++)
1086 memcpy( target_data
, source_data
, 3 );
1094 // src_alpha starts at the first pixel and increases by 1 after each step
1095 // (a step here is the copy of the alpha value of one pixel)
1096 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1097 // dest_alpha starts just beyond the first line, decreases before each step,
1098 // and after each line is finished, increases by 2 widths (skipping the line
1099 // just copied and the line that will be copied next)
1100 unsigned char *dest_alpha
= alpha
+ width
;
1102 for (long jj
= 0; jj
< height
; ++jj
)
1104 for (long i
= 0; i
< width
; ++i
) {
1105 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1107 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1113 for (long i
= 0; i
< height
; i
++)
1115 target_data
= data
+ 3*width
*(height
-1-i
);
1116 memcpy( target_data
, source_data
, (size_t)3*width
);
1117 source_data
+= 3*width
;
1122 // src_alpha starts at the first pixel and increases by 1 width after each step
1123 // (a step here is the copy of the alpha channel of an entire line)
1124 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1125 // dest_alpha starts just beyond the last line (beyond the whole image)
1126 // and decreases by 1 width before each step
1127 unsigned char *dest_alpha
= alpha
+ width
* height
;
1129 for (long jj
= 0; jj
< height
; ++jj
)
1131 dest_alpha
-= width
;
1132 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1141 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1145 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1147 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1148 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1149 image
, wxT("invalid subimage size") );
1151 const int subwidth
= rect
.GetWidth();
1152 const int subheight
= rect
.GetHeight();
1154 image
.Create( subwidth
, subheight
, false );
1156 const unsigned char *src_data
= GetData();
1157 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1158 unsigned char *subdata
= image
.GetData();
1159 unsigned char *subalpha
= NULL
;
1161 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1163 if (src_alpha
!= NULL
) {
1165 subalpha
= image
.GetAlpha();
1166 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1169 if (M_IMGDATA
->m_hasMask
)
1170 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1172 const int width
= GetWidth();
1173 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1175 src_data
+= 3 * pixsoff
;
1176 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1178 for (long j
= 0; j
< subheight
; ++j
)
1180 memcpy( subdata
, src_data
, 3 * subwidth
);
1181 subdata
+= 3 * subwidth
;
1182 src_data
+= 3 * width
;
1183 if (subalpha
!= NULL
) {
1184 memcpy( subalpha
, src_alpha
, subwidth
);
1185 subalpha
+= subwidth
;
1193 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1194 int r_
, int g_
, int b_
) const
1198 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1199 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1201 int width
= GetWidth(), height
= GetHeight();
1202 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1204 unsigned char r
= (unsigned char)r_
;
1205 unsigned char g
= (unsigned char)g_
;
1206 unsigned char b
= (unsigned char)b_
;
1207 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1209 GetOrFindMaskColour( &r
, &g
, &b
);
1210 image
.SetMaskColour(r
, g
, b
);
1213 image
.SetRGB(wxRect(), r
, g
, b
);
1215 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1216 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1218 finalRect
.width
-= pos
.x
;
1220 finalRect
.height
-= pos
.y
;
1222 subRect
.Intersect(finalRect
);
1224 if (!subRect
.IsEmpty())
1226 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1227 image
.Paste(*this, pos
.x
, pos
.y
);
1229 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1235 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1237 wxCHECK_RET( Ok(), wxT("invalid image") );
1238 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1244 int width
= image
.GetWidth();
1245 int height
= image
.GetHeight();
1258 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1259 width
= M_IMGDATA
->m_width
- (x
+xx
);
1260 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1261 height
= M_IMGDATA
->m_height
- (y
+yy
);
1263 if (width
< 1) return;
1264 if (height
< 1) return;
1266 if ((!HasMask() && !image
.HasMask()) ||
1267 (HasMask() && !image
.HasMask()) ||
1268 ((HasMask() && image
.HasMask() &&
1269 (GetMaskRed()==image
.GetMaskRed()) &&
1270 (GetMaskGreen()==image
.GetMaskGreen()) &&
1271 (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
);
1282 source_data
+= source_step
;
1283 target_data
+= target_step
;
1288 // Copy over the alpha channel from the original image
1289 if ( image
.HasAlpha() )
1294 unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1295 int source_step
= image
.GetWidth();
1297 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1298 int target_step
= M_IMGDATA
->m_width
;
1300 for (int j
= 0; j
< height
; j
++,
1301 source_data
+= source_step
,
1302 target_data
+= target_step
)
1304 memcpy( target_data
, source_data
, width
);
1308 if (!HasMask() && image
.HasMask())
1310 unsigned char r
= image
.GetMaskRed();
1311 unsigned char g
= image
.GetMaskGreen();
1312 unsigned char b
= image
.GetMaskBlue();
1315 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1316 int source_step
= image
.GetWidth()*3;
1318 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1319 int target_step
= M_IMGDATA
->m_width
*3;
1321 for (int j
= 0; j
< height
; j
++)
1323 for (int i
= 0; i
< width
; i
+=3)
1325 if ((source_data
[i
] != r
) ||
1326 (source_data
[i
+1] != g
) ||
1327 (source_data
[i
+2] != b
))
1329 memcpy( target_data
+i
, source_data
+i
, 3 );
1332 source_data
+= source_step
;
1333 target_data
+= target_step
;
1338 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1339 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1341 wxCHECK_RET( Ok(), wxT("invalid image") );
1345 unsigned char *data
= GetData();
1347 const int w
= GetWidth();
1348 const int h
= GetHeight();
1350 for (int j
= 0; j
< h
; j
++)
1351 for (int i
= 0; i
< w
; i
++)
1353 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1363 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1367 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1369 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1371 unsigned char *dest
= image
.GetData();
1373 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1375 unsigned char *src
= M_IMGDATA
->m_data
;
1376 bool hasMask
= M_IMGDATA
->m_hasMask
;
1377 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1378 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1379 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1382 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1384 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1385 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1387 // don't modify the mask
1388 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1390 memcpy(dest
, src
, 3);
1394 // calculate the luma
1395 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1396 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1400 // copy the alpha channel, if any
1403 const size_t alphaSize
= GetWidth() * GetHeight();
1404 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1405 memcpy(alpha
, GetAlpha(), alphaSize
);
1407 image
.SetAlpha(alpha
);
1413 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1417 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1419 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1421 unsigned char *data
= image
.GetData();
1423 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1425 if (M_IMGDATA
->m_hasMask
)
1427 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1428 M_IMGDATA
->m_maskBlue
== b
)
1429 image
.SetMaskColour( 255, 255, 255 );
1431 image
.SetMaskColour( 0, 0, 0 );
1434 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1436 unsigned char *srcd
= M_IMGDATA
->m_data
;
1437 unsigned char *tard
= image
.GetData();
1439 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1441 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1442 tard
[0] = tard
[1] = tard
[2] = 255;
1444 tard
[0] = tard
[1] = tard
[2] = 0;
1450 int wxImage::GetWidth() const
1452 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1454 return M_IMGDATA
->m_width
;
1457 int wxImage::GetHeight() const
1459 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1461 return M_IMGDATA
->m_height
;
1464 wxBitmapType
wxImage::GetType() const
1466 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1468 return M_IMGDATA
->m_type
;
1471 long wxImage::XYToIndex(int x
, int y
) const
1475 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1477 return y
*M_IMGDATA
->m_width
+ x
;
1483 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1485 long pos
= XYToIndex(x
, y
);
1486 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1492 M_IMGDATA
->m_data
[ pos
] = r
;
1493 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1494 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1497 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1499 wxCHECK_RET( Ok(), wxT("invalid image") );
1504 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1505 if ( rect
== wxRect() )
1511 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1512 imageRect
.Contains(rect
.GetBottomRight()),
1513 wxT("invalid bounding rectangle") );
1516 int x1
= rect
.GetLeft(),
1518 x2
= rect
.GetRight() + 1,
1519 y2
= rect
.GetBottom() + 1;
1521 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1522 int x
, y
, width
= GetWidth();
1523 for (y
= y1
; y
< y2
; y
++)
1525 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1526 for (x
= x1
; x
< x2
; x
++)
1535 unsigned char wxImage::GetRed( int x
, int y
) const
1537 long pos
= XYToIndex(x
, y
);
1538 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1542 return M_IMGDATA
->m_data
[pos
];
1545 unsigned char wxImage::GetGreen( int x
, int y
) const
1547 long pos
= XYToIndex(x
, y
);
1548 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1552 return M_IMGDATA
->m_data
[pos
+1];
1555 unsigned char wxImage::GetBlue( int x
, int y
) const
1557 long pos
= XYToIndex(x
, y
);
1558 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1562 return M_IMGDATA
->m_data
[pos
+2];
1565 bool wxImage::IsOk() const
1567 // image of 0 width or height can't be considered ok - at least because it
1568 // causes crashes in ConvertToBitmap() if we don't catch it in time
1569 wxImageRefData
*data
= M_IMGDATA
;
1570 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1573 unsigned char *wxImage::GetData() const
1575 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1577 return M_IMGDATA
->m_data
;
1580 void wxImage::SetData( unsigned char *data
, bool static_data
)
1582 wxCHECK_RET( Ok(), wxT("invalid image") );
1584 wxImageRefData
*newRefData
= new wxImageRefData();
1586 newRefData
->m_width
= M_IMGDATA
->m_width
;
1587 newRefData
->m_height
= M_IMGDATA
->m_height
;
1588 newRefData
->m_data
= data
;
1589 newRefData
->m_ok
= true;
1590 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1591 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1592 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1593 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1594 newRefData
->m_static
= static_data
;
1598 m_refData
= newRefData
;
1601 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1603 wxImageRefData
*newRefData
= new wxImageRefData();
1607 newRefData
->m_width
= new_width
;
1608 newRefData
->m_height
= new_height
;
1609 newRefData
->m_data
= data
;
1610 newRefData
->m_ok
= true;
1611 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1612 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1613 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1614 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1618 newRefData
->m_width
= new_width
;
1619 newRefData
->m_height
= new_height
;
1620 newRefData
->m_data
= data
;
1621 newRefData
->m_ok
= true;
1623 newRefData
->m_static
= static_data
;
1627 m_refData
= newRefData
;
1630 // ----------------------------------------------------------------------------
1631 // alpha channel support
1632 // ----------------------------------------------------------------------------
1634 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1636 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1638 long pos
= XYToIndex(x
, y
);
1639 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1643 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1646 unsigned char wxImage::GetAlpha(int x
, int y
) const
1648 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1650 long pos
= XYToIndex(x
, y
);
1651 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1653 return M_IMGDATA
->m_alpha
[pos
];
1657 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1661 const int w
= M_IMGDATA
->m_width
;
1662 const int h
= M_IMGDATA
->m_height
;
1664 unsigned char *alpha
= GetAlpha();
1665 unsigned char *data
= GetData();
1667 for ( int y
= 0; y
< h
; y
++ )
1669 for ( int x
= 0; x
< w
; x
++ )
1681 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1683 wxCHECK_RET( Ok(), wxT("invalid image") );
1689 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1692 if( !M_IMGDATA
->m_staticAlpha
)
1693 free(M_IMGDATA
->m_alpha
);
1695 M_IMGDATA
->m_alpha
= alpha
;
1696 M_IMGDATA
->m_staticAlpha
= static_data
;
1699 unsigned char *wxImage::GetAlpha() const
1701 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1703 return M_IMGDATA
->m_alpha
;
1706 void wxImage::InitAlpha()
1708 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1710 // initialize memory for alpha channel
1713 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1714 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1718 // use the mask to initialize the alpha channel.
1719 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1721 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1722 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1723 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1724 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1728 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1729 ? wxIMAGE_ALPHA_TRANSPARENT
1730 : wxIMAGE_ALPHA_OPAQUE
;
1733 M_IMGDATA
->m_hasMask
= false;
1737 // make the image fully opaque
1738 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1742 // ----------------------------------------------------------------------------
1744 // ----------------------------------------------------------------------------
1746 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1748 wxCHECK_RET( Ok(), wxT("invalid image") );
1752 M_IMGDATA
->m_maskRed
= r
;
1753 M_IMGDATA
->m_maskGreen
= g
;
1754 M_IMGDATA
->m_maskBlue
= b
;
1755 M_IMGDATA
->m_hasMask
= true;
1758 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1760 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1762 if (M_IMGDATA
->m_hasMask
)
1764 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1765 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1766 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1771 FindFirstUnusedColour(r
, g
, b
);
1776 unsigned char wxImage::GetMaskRed() const
1778 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1780 return M_IMGDATA
->m_maskRed
;
1783 unsigned char wxImage::GetMaskGreen() const
1785 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1787 return M_IMGDATA
->m_maskGreen
;
1790 unsigned char wxImage::GetMaskBlue() const
1792 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1794 return M_IMGDATA
->m_maskBlue
;
1797 void wxImage::SetMask( bool mask
)
1799 wxCHECK_RET( Ok(), wxT("invalid image") );
1803 M_IMGDATA
->m_hasMask
= mask
;
1806 bool wxImage::HasMask() const
1808 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1810 return M_IMGDATA
->m_hasMask
;
1813 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1815 long pos
= XYToIndex(x
, y
);
1816 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1819 if ( M_IMGDATA
->m_hasMask
)
1821 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1822 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1823 p
[1] == M_IMGDATA
->m_maskGreen
&&
1824 p
[2] == M_IMGDATA
->m_maskBlue
)
1831 if ( M_IMGDATA
->m_alpha
)
1833 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1835 // transparent enough
1844 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1845 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1847 // check that the images are the same size
1848 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1850 wxLogError( _("Image and mask have different sizes.") );
1854 // find unused colour
1855 unsigned char r
,g
,b
;
1856 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1858 wxLogError( _("No unused colour in image being masked.") );
1864 unsigned char *imgdata
= GetData();
1865 unsigned char *maskdata
= mask
.GetData();
1867 const int w
= GetWidth();
1868 const int h
= GetHeight();
1870 for (int j
= 0; j
< h
; j
++)
1872 for (int i
= 0; i
< w
; i
++)
1874 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1885 SetMaskColour(r
, g
, b
);
1891 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1896 unsigned char mr
, mg
, mb
;
1897 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1899 wxLogError( _("No unused colour in image being masked.") );
1906 SetMaskColour(mr
, mg
, mb
);
1908 unsigned char *imgdata
= GetData();
1909 unsigned char *alphadata
= GetAlpha();
1912 int h
= GetHeight();
1914 for (int y
= 0; y
< h
; y
++)
1916 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1918 if (*alphadata
< threshold
)
1927 if( !M_IMGDATA
->m_staticAlpha
)
1928 free(M_IMGDATA
->m_alpha
);
1930 M_IMGDATA
->m_alpha
= NULL
;
1931 M_IMGDATA
->m_staticAlpha
= false;
1936 // ----------------------------------------------------------------------------
1937 // Palette functions
1938 // ----------------------------------------------------------------------------
1942 bool wxImage::HasPalette() const
1947 return M_IMGDATA
->m_palette
.Ok();
1950 const wxPalette
& wxImage::GetPalette() const
1952 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1954 return M_IMGDATA
->m_palette
;
1957 void wxImage::SetPalette(const wxPalette
& palette
)
1959 wxCHECK_RET( Ok(), wxT("invalid image") );
1963 M_IMGDATA
->m_palette
= palette
;
1966 #endif // wxUSE_PALETTE
1968 // ----------------------------------------------------------------------------
1969 // Option functions (arbitrary name/value mapping)
1970 // ----------------------------------------------------------------------------
1972 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1974 wxCHECK_RET( Ok(), wxT("invalid image") );
1978 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1979 if (idx
== wxNOT_FOUND
)
1981 M_IMGDATA
->m_optionNames
.Add(name
);
1982 M_IMGDATA
->m_optionValues
.Add(value
);
1986 M_IMGDATA
->m_optionNames
[idx
] = name
;
1987 M_IMGDATA
->m_optionValues
[idx
] = value
;
1991 void wxImage::SetOption(const wxString
& name
, int value
)
1994 valStr
.Printf(wxT("%d"), value
);
1995 SetOption(name
, valStr
);
1998 wxString
wxImage::GetOption(const wxString
& name
) const
2000 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
2002 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
2003 if (idx
== wxNOT_FOUND
)
2004 return wxEmptyString
;
2006 return M_IMGDATA
->m_optionValues
[idx
];
2009 int wxImage::GetOptionInt(const wxString
& name
) const
2011 return wxAtoi(GetOption(name
));
2014 bool wxImage::HasOption(const wxString
& name
) const
2016 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2018 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
2021 // ----------------------------------------------------------------------------
2023 // ----------------------------------------------------------------------------
2025 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2026 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2027 int WXUNUSED_UNLESS_STREAMS(index
) )
2029 #if HAS_FILE_STREAMS
2030 if (wxFileExists(filename
))
2032 wxImageFileInputStream
stream(filename
);
2033 wxBufferedInputStream
bstream( stream
);
2034 return LoadFile(bstream
, type
, index
);
2038 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2042 #else // !HAS_FILE_STREAMS
2044 #endif // HAS_FILE_STREAMS
2047 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2048 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2049 int WXUNUSED_UNLESS_STREAMS(index
) )
2051 #if HAS_FILE_STREAMS
2052 if (wxFileExists(filename
))
2054 wxImageFileInputStream
stream(filename
);
2055 wxBufferedInputStream
bstream( stream
);
2056 return LoadFile(bstream
, mimetype
, index
);
2060 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2064 #else // !HAS_FILE_STREAMS
2066 #endif // HAS_FILE_STREAMS
2070 bool wxImage::SaveFile( const wxString
& filename
) const
2072 wxString ext
= filename
.AfterLast('.').Lower();
2074 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2077 wxLogError(_("Can't save image to file '%s': unknown extension."),
2082 return SaveFile(filename
, handler
->GetType());
2085 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2086 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2088 #if HAS_FILE_STREAMS
2089 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2091 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2093 wxImageFileOutputStream
stream(filename
);
2095 if ( stream
.IsOk() )
2097 wxBufferedOutputStream
bstream( stream
);
2098 return SaveFile(bstream
, type
);
2100 #endif // HAS_FILE_STREAMS
2105 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2106 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2108 #if HAS_FILE_STREAMS
2109 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2111 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2113 wxImageFileOutputStream
stream(filename
);
2115 if ( stream
.IsOk() )
2117 wxBufferedOutputStream
bstream( stream
);
2118 return SaveFile(bstream
, mimetype
);
2120 #endif // HAS_FILE_STREAMS
2125 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2127 #if HAS_FILE_STREAMS
2128 wxImageFileInputStream
stream(name
);
2129 return CanRead(stream
);
2135 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2136 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2138 #if HAS_FILE_STREAMS
2139 wxImageFileInputStream
stream(name
);
2141 return GetImageCount(stream
, type
);
2149 bool wxImage::CanRead( wxInputStream
&stream
)
2151 const wxList
& list
= GetHandlers();
2153 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2155 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2156 if (handler
->CanRead( stream
))
2163 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2165 wxImageHandler
*handler
;
2167 if ( type
== wxBITMAP_TYPE_ANY
)
2169 const wxList
& list
= GetHandlers();
2171 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2173 node
= node
->GetNext() )
2175 handler
= (wxImageHandler
*)node
->GetData();
2176 if ( handler
->CanRead(stream
) )
2178 const int count
= handler
->GetImageCount(stream
);
2185 wxLogWarning(_("No handler found for image type."));
2189 handler
= FindHandler(type
);
2193 wxLogWarning(_("No image handler for type %ld defined."), type
);
2197 if ( handler
->CanRead(stream
) )
2199 return handler
->GetImageCount(stream
);
2203 wxLogError(_("Image file is not of type %ld."), type
);
2208 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2210 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2213 M_IMGDATA
->m_type
= handler
.GetType();
2217 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2221 m_refData
= new wxImageRefData
;
2223 wxImageHandler
*handler
;
2225 if ( type
== wxBITMAP_TYPE_ANY
)
2227 const wxList
& list
= GetHandlers();
2228 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2230 node
= node
->GetNext() )
2232 handler
= (wxImageHandler
*)node
->GetData();
2233 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2237 wxLogWarning( _("No handler found for image type.") );
2241 //else: have specific type
2243 handler
= FindHandler(type
);
2246 wxLogWarning( _("No image handler for type %ld defined."), type
);
2250 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2252 wxLogError(_("Image file is not of type %ld."), type
);
2256 return DoLoad(*handler
, stream
, index
);
2259 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2263 m_refData
= new wxImageRefData
;
2265 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2269 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2273 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2275 wxLogError(_("Image file is not of type %s."), mimetype
);
2279 return DoLoad(*handler
, stream
, index
);
2282 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2284 wxImage
* const self
= wx_const_cast(wxImage
*, this);
2285 if ( !handler
.SaveFile(self
, stream
) )
2288 M_IMGDATA
->m_type
= handler
.GetType();
2292 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2294 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2296 wxImageHandler
*handler
= FindHandler(type
);
2299 wxLogWarning( _("No image handler for type %d defined."), type
);
2303 return DoSave(*handler
, stream
);
2306 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2308 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2310 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2313 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2316 return DoSave(*handler
, stream
);
2319 #endif // wxUSE_STREAMS
2321 // ----------------------------------------------------------------------------
2322 // image I/O handlers
2323 // ----------------------------------------------------------------------------
2325 void wxImage::AddHandler( wxImageHandler
*handler
)
2327 // Check for an existing handler of the type being added.
2328 if (FindHandler( handler
->GetType() ) == 0)
2330 sm_handlers
.Append( handler
);
2334 // This is not documented behaviour, merely the simplest 'fix'
2335 // for preventing duplicate additions. If someone ever has
2336 // a good reason to add and remove duplicate handlers (and they
2337 // may) we should probably refcount the duplicates.
2338 // also an issue in InsertHandler below.
2340 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2341 handler
->GetName().c_str() );
2346 void wxImage::InsertHandler( wxImageHandler
*handler
)
2348 // Check for an existing handler of the type being added.
2349 if (FindHandler( handler
->GetType() ) == 0)
2351 sm_handlers
.Insert( handler
);
2355 // see AddHandler for additional comments.
2356 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2357 handler
->GetName().c_str() );
2362 bool wxImage::RemoveHandler( const wxString
& name
)
2364 wxImageHandler
*handler
= FindHandler(name
);
2367 sm_handlers
.DeleteObject(handler
);
2375 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2377 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2380 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2381 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2383 node
= node
->GetNext();
2388 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2390 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2393 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2394 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2395 ( (bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
)) )
2399 node
= node
->GetNext();
2404 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2406 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2409 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2410 if (handler
->GetType() == bitmapType
) return handler
;
2411 node
= node
->GetNext();
2416 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2418 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2421 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2422 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2423 node
= node
->GetNext();
2428 void wxImage::InitStandardHandlers()
2431 AddHandler(new wxBMPHandler
);
2432 #endif // wxUSE_STREAMS
2435 void wxImage::CleanUpHandlers()
2437 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2440 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2441 wxList::compatibility_iterator next
= node
->GetNext();
2446 sm_handlers
.Clear();
2449 wxString
wxImage::GetImageExtWildcard()
2453 wxList
& Handlers
= wxImage::GetHandlers();
2454 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2457 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2458 fmts
+= wxT("*.") + Handler
->GetExtension();
2459 Node
= Node
->GetNext();
2460 if ( Node
) fmts
+= wxT(";");
2463 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2466 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2468 const double red
= rgb
.red
/ 255.0,
2469 green
= rgb
.green
/ 255.0,
2470 blue
= rgb
.blue
/ 255.0;
2472 // find the min and max intensity (and remember which one was it for the
2474 double minimumRGB
= red
;
2475 if ( green
< minimumRGB
)
2477 if ( blue
< minimumRGB
)
2480 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2481 double maximumRGB
= red
;
2482 if ( green
> maximumRGB
)
2487 if ( blue
> maximumRGB
)
2493 const double value
= maximumRGB
;
2495 double hue
= 0.0, saturation
;
2496 const double deltaRGB
= maximumRGB
- minimumRGB
;
2497 if ( wxIsNullDouble(deltaRGB
) )
2499 // Gray has no color
2508 hue
= (green
- blue
) / deltaRGB
;
2512 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2516 hue
= 4.0 + (red
- green
) / deltaRGB
;
2520 wxFAIL_MSG(wxT("hue not specified"));
2529 saturation
= deltaRGB
/ maximumRGB
;
2532 return HSVValue(hue
, saturation
, value
);
2535 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2537 double red
, green
, blue
;
2539 if ( wxIsNullDouble(hsv
.saturation
) )
2548 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2549 int i
= (int)floor(hue
);
2550 double f
= hue
- i
; // fractional part of h
2551 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2557 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2562 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2570 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2575 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2580 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2588 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2593 return RGBValue((unsigned char)(red
* 255.0),
2594 (unsigned char)(green
* 255.0),
2595 (unsigned char)(blue
* 255.0));
2599 * Rotates the hue of each pixel of the image. angle is a double in the range
2600 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2602 void wxImage::RotateHue(double angle
)
2606 unsigned char *srcBytePtr
;
2607 unsigned char *dstBytePtr
;
2608 unsigned long count
;
2609 wxImage::HSVValue hsv
;
2610 wxImage::RGBValue rgb
;
2612 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2613 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2614 if ( count
> 0 && !wxIsNullDouble(angle
) )
2616 srcBytePtr
= M_IMGDATA
->m_data
;
2617 dstBytePtr
= srcBytePtr
;
2620 rgb
.red
= *srcBytePtr
++;
2621 rgb
.green
= *srcBytePtr
++;
2622 rgb
.blue
= *srcBytePtr
++;
2623 hsv
= RGBtoHSV(rgb
);
2625 hsv
.hue
= hsv
.hue
+ angle
;
2627 hsv
.hue
= hsv
.hue
- 1.0;
2628 else if (hsv
.hue
< 0.0)
2629 hsv
.hue
= hsv
.hue
+ 1.0;
2631 rgb
= HSVtoRGB(hsv
);
2632 *dstBytePtr
++ = rgb
.red
;
2633 *dstBytePtr
++ = rgb
.green
;
2634 *dstBytePtr
++ = rgb
.blue
;
2635 } while (--count
!= 0);
2639 //-----------------------------------------------------------------------------
2641 //-----------------------------------------------------------------------------
2643 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2646 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2651 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2656 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2661 bool wxImageHandler::CanRead( const wxString
& name
)
2663 if (wxFileExists(name
))
2665 wxImageFileInputStream
stream(name
);
2666 return CanRead(stream
);
2669 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2674 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2676 wxFileOffset posOld
= stream
.TellI();
2677 if ( posOld
== wxInvalidOffset
)
2679 // can't test unseekable stream
2683 bool ok
= DoCanRead(stream
);
2685 // restore the old position to be able to test other formats and so on
2686 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2688 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2690 // reading would fail anyhow as we're not at the right position
2697 #endif // wxUSE_STREAMS
2701 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2703 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, _T("NULL pointer") );
2705 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2706 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2708 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2709 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2711 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2714 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2716 else // no resolution options specified
2721 return wxIMAGE_RESOLUTION_NONE
;
2724 // get the resolution unit too
2725 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2728 // this is the default
2729 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2732 return (wxImageResolution
)resUnit
;
2735 // ----------------------------------------------------------------------------
2736 // image histogram stuff
2737 // ----------------------------------------------------------------------------
2740 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2745 unsigned char g2
) const
2747 unsigned long key
= MakeKey(r2
, g2
, b2
);
2749 while ( find(key
) != end() )
2751 // color already used
2763 wxLogError(_("No unused colour in image.") );
2769 key
= MakeKey(r2
, g2
, b2
);
2783 wxImage::FindFirstUnusedColour(unsigned char *r
,
2788 unsigned char g2
) const
2790 wxImageHistogram histogram
;
2792 ComputeHistogram(histogram
);
2794 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2800 // Counts and returns the number of different colours. Optionally stops
2801 // when it exceeds 'stopafter' different colours. This is useful, for
2802 // example, to see if the image can be saved as 8-bit (256 colour or
2803 // less, in this case it would be invoked as CountColours(256)). Default
2804 // value for stopafter is -1 (don't care).
2806 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2810 unsigned char r
, g
, b
;
2812 unsigned long size
, nentries
, key
;
2815 size
= GetWidth() * GetHeight();
2818 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2823 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2825 if (h
.Get(key
) == NULL
)
2836 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2838 unsigned char *p
= GetData();
2839 unsigned long nentries
= 0;
2843 const unsigned long size
= GetWidth() * GetHeight();
2845 unsigned char r
, g
, b
;
2846 for ( unsigned long n
= 0; n
< size
; n
++ )
2852 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2854 if ( entry
.value
++ == 0 )
2855 entry
.index
= nentries
++;
2862 * Rotation code by Carlos Moreno
2865 static const double wxROTATE_EPSILON
= 1e-10;
2867 // Auxiliary function to rotate a point (x,y) with respect to point p0
2868 // make it inline and use a straight return to facilitate optimization
2869 // also, the function receives the sine and cosine of the angle to avoid
2870 // repeating the time-consuming calls to these functions -- sin/cos can
2871 // be computed and stored in the calling function.
2873 static inline wxRealPoint
2874 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2875 const wxRealPoint
& p0
)
2877 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2878 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2881 static inline wxRealPoint
2882 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2883 const wxRealPoint
& p0
)
2885 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2888 wxImage
wxImage::Rotate(double angle
,
2889 const wxPoint
& centre_of_rotation
,
2891 wxPoint
*offset_after_rotation
) const
2893 // screen coordinates are a mirror image of "real" coordinates
2896 const bool has_alpha
= HasAlpha();
2898 const int w
= GetWidth();
2899 const int h
= GetHeight();
2903 // Create pointer-based array to accelerate access to wxImage's data
2904 unsigned char ** data
= new unsigned char * [h
];
2905 data
[0] = GetData();
2906 for (i
= 1; i
< h
; i
++)
2907 data
[i
] = data
[i
- 1] + (3 * w
);
2909 // Same for alpha channel
2910 unsigned char ** alpha
= NULL
;
2913 alpha
= new unsigned char * [h
];
2914 alpha
[0] = GetAlpha();
2915 for (i
= 1; i
< h
; i
++)
2916 alpha
[i
] = alpha
[i
- 1] + w
;
2919 // precompute coefficients for rotation formula
2920 const double cos_angle
= cos(angle
);
2921 const double sin_angle
= sin(angle
);
2923 // Create new Image to store the result
2924 // First, find rectangle that covers the rotated image; to do that,
2925 // rotate the four corners
2927 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2929 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2930 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
2931 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
2932 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
2934 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2935 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2936 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2937 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2939 // Create rotated image
2940 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2941 // With alpha channel
2945 if (offset_after_rotation
!= NULL
)
2947 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2950 // the rotated (destination) image is always accessed sequentially via this
2951 // pointer, there is no need for pointer-based arrays here
2952 unsigned char *dst
= rotated
.GetData();
2954 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
2956 // if the original image has a mask, use its RGB values as the blank pixel,
2957 // else, fall back to default (black).
2958 unsigned char blank_r
= 0;
2959 unsigned char blank_g
= 0;
2960 unsigned char blank_b
= 0;
2964 blank_r
= GetMaskRed();
2965 blank_g
= GetMaskGreen();
2966 blank_b
= GetMaskBlue();
2967 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2970 // Now, for each point of the rotated image, find where it came from, by
2971 // performing an inverse rotation (a rotation of -angle) and getting the
2972 // pixel at those coordinates
2974 const int rH
= rotated
.GetHeight();
2975 const int rW
= rotated
.GetWidth();
2977 // do the (interpolating) test outside of the loops, so that it is done
2978 // only once, instead of repeating it for each pixel.
2981 for (int y
= 0; y
< rH
; y
++)
2983 for (int x
= 0; x
< rW
; x
++)
2985 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2987 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
2988 -0.25 < src
.y
&& src
.y
< h
- 0.75)
2990 // interpolate using the 4 enclosing grid-points. Those
2991 // points can be obtained using floor and ceiling of the
2992 // exact coordinates of the point
2995 if (0 < src
.x
&& src
.x
< w
- 1)
2997 x1
= wxRound(floor(src
.x
));
2998 x2
= wxRound(ceil(src
.x
));
3000 else // else means that x is near one of the borders (0 or width-1)
3002 x1
= x2
= wxRound (src
.x
);
3005 if (0 < src
.y
&& src
.y
< h
- 1)
3007 y1
= wxRound(floor(src
.y
));
3008 y2
= wxRound(ceil(src
.y
));
3012 y1
= y2
= wxRound (src
.y
);
3015 // get four points and the distances (square of the distance,
3016 // for efficiency reasons) for the interpolation formula
3018 // GRG: Do not calculate the points until they are
3019 // really needed -- this way we can calculate
3020 // just one, instead of four, if d1, d2, d3
3021 // or d4 are < wxROTATE_EPSILON
3023 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3024 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3025 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3026 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3028 // Now interpolate as a weighted average of the four surrounding
3029 // points, where the weights are the distances to each of those points
3031 // If the point is exactly at one point of the grid of the source
3032 // image, then don't interpolate -- just assign the pixel
3034 // d1,d2,d3,d4 are positive -- no need for abs()
3035 if (d1
< wxROTATE_EPSILON
)
3037 unsigned char *p
= data
[y1
] + (3 * x1
);
3043 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3045 else if (d2
< wxROTATE_EPSILON
)
3047 unsigned char *p
= data
[y1
] + (3 * x2
);
3053 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3055 else if (d3
< wxROTATE_EPSILON
)
3057 unsigned char *p
= data
[y2
] + (3 * x2
);
3063 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3065 else if (d4
< wxROTATE_EPSILON
)
3067 unsigned char *p
= data
[y2
] + (3 * x1
);
3073 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3077 // weights for the weighted average are proportional to the inverse of the distance
3078 unsigned char *v1
= data
[y1
] + (3 * x1
);
3079 unsigned char *v2
= data
[y1
] + (3 * x2
);
3080 unsigned char *v3
= data
[y2
] + (3 * x2
);
3081 unsigned char *v4
= data
[y2
] + (3 * x1
);
3083 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3087 *(dst
++) = (unsigned char)
3088 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3089 w3
* *(v3
++) + w4
* *(v4
++)) /
3090 (w1
+ w2
+ w3
+ w4
) );
3091 *(dst
++) = (unsigned char)
3092 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3093 w3
* *(v3
++) + w4
* *(v4
++)) /
3094 (w1
+ w2
+ w3
+ w4
) );
3095 *(dst
++) = (unsigned char)
3096 ( (w1
* *v1
+ w2
* *v2
+
3097 w3
* *v3
+ w4
* *v4
) /
3098 (w1
+ w2
+ w3
+ w4
) );
3102 v1
= alpha
[y1
] + (x1
);
3103 v2
= alpha
[y1
] + (x2
);
3104 v3
= alpha
[y2
] + (x2
);
3105 v4
= alpha
[y2
] + (x1
);
3107 *(alpha_dst
++) = (unsigned char)
3108 ( (w1
* *v1
+ w2
* *v2
+
3109 w3
* *v3
+ w4
* *v4
) /
3110 (w1
+ w2
+ w3
+ w4
) );
3126 else // not interpolating
3128 for (int y
= 0; y
< rH
; y
++)
3130 for (int x
= 0; x
< rW
; x
++)
3132 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3134 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3135 const int ys
= wxRound (src
.y
); // closest integer
3137 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3139 unsigned char *p
= data
[ys
] + (3 * xs
);
3145 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3154 *(alpha_dst
++) = 255;
3170 // A module to allow wxImage initialization/cleanup
3171 // without calling these functions from app.cpp or from
3172 // the user's application.
3174 class wxImageModule
: public wxModule
3176 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3179 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3180 void OnExit() { wxImage::CleanUpHandlers(); }
3183 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3186 #endif // wxUSE_IMAGE