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 wxList
wxImage::sm_handlers
;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 class wxImageRefData
: public wxObjectRefData
70 virtual ~wxImageRefData();
75 unsigned char *m_data
;
78 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
80 // alpha channel data, may be NULL for the formats without alpha support
81 unsigned char *m_alpha
;
85 // if true, m_data is pointer to static data and shouldn't be freed
88 // same as m_static but for m_alpha
93 #endif // wxUSE_PALETTE
95 wxArrayString m_optionNames
;
96 wxArrayString m_optionValues
;
98 wxDECLARE_NO_COPY_CLASS(wxImageRefData
);
101 wxImageRefData::wxImageRefData()
105 m_type
= wxBITMAP_TYPE_INVALID
;
107 m_alpha
= (unsigned char *) NULL
;
116 m_staticAlpha
= false;
119 wxImageRefData::~wxImageRefData()
123 if ( !m_staticAlpha
)
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 #define M_IMGDATA static_cast<wxImageRefData*>(m_refData)
134 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
136 bool wxImage::Create(const char* const* xpmData
)
141 wxXPMDecoder decoder
;
142 (*this) = decoder
.ReadData(xpmData
);
149 bool wxImage::Create( int width
, int height
, bool clear
)
153 m_refData
= new wxImageRefData();
155 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
156 if (!M_IMGDATA
->m_data
)
162 M_IMGDATA
->m_width
= width
;
163 M_IMGDATA
->m_height
= height
;
164 M_IMGDATA
->m_ok
= true;
174 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
178 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
180 m_refData
= new wxImageRefData();
182 M_IMGDATA
->m_data
= data
;
183 M_IMGDATA
->m_width
= width
;
184 M_IMGDATA
->m_height
= height
;
185 M_IMGDATA
->m_ok
= true;
186 M_IMGDATA
->m_static
= static_data
;
191 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
195 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
197 m_refData
= new wxImageRefData();
199 M_IMGDATA
->m_data
= data
;
200 M_IMGDATA
->m_alpha
= alpha
;
201 M_IMGDATA
->m_width
= width
;
202 M_IMGDATA
->m_height
= height
;
203 M_IMGDATA
->m_ok
= true;
204 M_IMGDATA
->m_static
= static_data
;
205 M_IMGDATA
->m_staticAlpha
= static_data
;
210 void wxImage::Destroy()
215 void wxImage::Clear(unsigned char value
)
217 memset(M_IMGDATA
->m_data
, value
, M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
220 wxObjectRefData
* wxImage::CreateRefData() const
222 return new wxImageRefData
;
225 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
227 const wxImageRefData
* refData
= static_cast<const wxImageRefData
*>(that
);
228 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
230 wxImageRefData
* refData_new
= new wxImageRefData
;
231 refData_new
->m_width
= refData
->m_width
;
232 refData_new
->m_height
= refData
->m_height
;
233 refData_new
->m_maskRed
= refData
->m_maskRed
;
234 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
235 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
236 refData_new
->m_hasMask
= refData
->m_hasMask
;
237 refData_new
->m_ok
= true;
238 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
239 if (refData
->m_alpha
!= NULL
)
241 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
242 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
245 refData_new
->m_data
= (unsigned char*)malloc(size
);
246 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
248 refData_new
->m_palette
= refData
->m_palette
;
250 refData_new
->m_optionNames
= refData
->m_optionNames
;
251 refData_new
->m_optionValues
= refData
->m_optionValues
;
255 wxImage
wxImage::Copy() const
259 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
261 image
.m_refData
= CloneRefData(m_refData
);
266 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
268 if( xFactor
== 1 && yFactor
== 1 )
273 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
275 // can't scale to/from 0 size
276 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
277 wxT("invalid new image size") );
279 long old_height
= M_IMGDATA
->m_height
,
280 old_width
= M_IMGDATA
->m_width
;
282 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
283 wxT("invalid old image size") );
285 long width
= old_width
/ xFactor
;
286 long height
= old_height
/ yFactor
;
288 image
.Create( width
, height
, false );
290 char unsigned *data
= image
.GetData();
292 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
294 bool hasMask
= false ;
295 unsigned char maskRed
= 0;
296 unsigned char maskGreen
= 0;
297 unsigned char maskBlue
=0 ;
299 unsigned char *source_data
= M_IMGDATA
->m_data
;
300 unsigned char *target_data
= data
;
301 unsigned char *source_alpha
= 0 ;
302 unsigned char *target_alpha
= 0 ;
303 if (M_IMGDATA
->m_hasMask
)
306 maskRed
= M_IMGDATA
->m_maskRed
;
307 maskGreen
= M_IMGDATA
->m_maskGreen
;
308 maskBlue
=M_IMGDATA
->m_maskBlue
;
310 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
311 M_IMGDATA
->m_maskGreen
,
312 M_IMGDATA
->m_maskBlue
);
316 source_alpha
= M_IMGDATA
->m_alpha
;
320 target_alpha
= image
.GetAlpha() ;
324 for (long y
= 0; y
< height
; y
++)
326 for (long x
= 0; x
< width
; x
++)
328 unsigned long avgRed
= 0 ;
329 unsigned long avgGreen
= 0;
330 unsigned long avgBlue
= 0;
331 unsigned long avgAlpha
= 0 ;
332 unsigned long counter
= 0 ;
334 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
336 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
337 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
339 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
340 unsigned char red
= pixel
[0] ;
341 unsigned char green
= pixel
[1] ;
342 unsigned char blue
= pixel
[2] ;
343 unsigned char alpha
= 255 ;
345 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
346 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
361 *(target_data
++) = M_IMGDATA
->m_maskRed
;
362 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
363 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
368 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
369 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
370 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
371 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
376 // In case this is a cursor, make sure the hotspot is scaled accordingly:
377 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
378 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
379 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
380 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
381 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
382 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
387 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
391 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
393 // can't scale to/from 0 size
394 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
395 wxT("invalid new image size") );
397 long old_height
= M_IMGDATA
->m_height
,
398 old_width
= M_IMGDATA
->m_width
;
399 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
400 wxT("invalid old image size") );
402 // If the image's new width and height are the same as the original, no
403 // need to waste time or CPU cycles
404 if ( old_width
== width
&& old_height
== height
)
407 // Scale the image (...or more appropriately, resample the image) using
408 // either the high-quality or normal method as specified
409 if ( quality
== wxIMAGE_QUALITY_HIGH
)
411 // We need to check whether we are downsampling or upsampling the image
412 if ( width
< old_width
&& height
< old_height
)
414 // Downsample the image using the box averaging method for best results
415 image
= ResampleBox(width
, height
);
419 // For upsampling or other random/wierd image dimensions we'll use
420 // a bicubic b-spline scaling method
421 image
= ResampleBicubic(width
, height
);
424 else // Default scaling method == simple pixel replication
426 if ( old_width
% width
== 0 && old_width
>= width
&&
427 old_height
% height
== 0 && old_height
>= height
)
429 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
431 image
.Create( width
, height
, false );
433 unsigned char *data
= image
.GetData();
435 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
437 unsigned char *source_data
= M_IMGDATA
->m_data
;
438 unsigned char *target_data
= data
;
439 unsigned char *source_alpha
= 0 ;
440 unsigned char *target_alpha
= 0 ;
442 if ( !M_IMGDATA
->m_hasMask
)
444 source_alpha
= M_IMGDATA
->m_alpha
;
448 target_alpha
= image
.GetAlpha() ;
452 long x_delta
= (old_width
<<16) / width
;
453 long y_delta
= (old_height
<<16) / height
;
455 unsigned char* dest_pixel
= target_data
;
458 for ( long j
= 0; j
< height
; j
++ )
460 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
461 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
464 for ( long i
= 0; i
< width
; i
++ )
466 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
467 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
468 dest_pixel
[0] = src_pixel
[0];
469 dest_pixel
[1] = src_pixel
[1];
470 dest_pixel
[2] = src_pixel
[2];
473 *(target_alpha
++) = *src_alpha_pixel
;
481 // If the original image has a mask, apply the mask to the new image
482 if (M_IMGDATA
->m_hasMask
)
484 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
485 M_IMGDATA
->m_maskGreen
,
486 M_IMGDATA
->m_maskBlue
);
489 // In case this is a cursor, make sure the hotspot is scaled accordingly:
490 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
491 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
492 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
493 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
494 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
495 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
500 wxImage
wxImage::ResampleBox(int width
, int height
) const
502 // This function implements a simple pre-blur/box averaging method for
503 // downsampling that gives reasonably smooth results To scale the image
504 // down we will need to gather a grid of pixels of the size of the scale
505 // factor in each direction and then do an averaging of the pixels.
507 wxImage
ret_image(width
, height
, false);
509 const double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
510 const double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
512 const int scale_factor_x_2
= (int)(scale_factor_x
/ 2);
513 const int scale_factor_y_2
= (int)(scale_factor_y
/ 2);
515 unsigned char* src_data
= M_IMGDATA
->m_data
;
516 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
517 unsigned char* dst_data
= ret_image
.GetData();
518 unsigned char* dst_alpha
= NULL
;
522 ret_image
.SetAlpha();
523 dst_alpha
= ret_image
.GetAlpha();
526 int averaged_pixels
, src_pixel_index
;
527 double sum_r
, sum_g
, sum_b
, sum_a
;
529 for ( int y
= 0; y
< height
; y
++ ) // Destination image - Y direction
531 // Source pixel in the Y direction
532 int src_y
= (int)(y
* scale_factor_y
);
534 for ( int x
= 0; x
< width
; x
++ ) // Destination image - X direction
536 // Source pixel in the X direction
537 int src_x
= (int)(x
* scale_factor_x
);
539 // Box of pixels to average
541 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
543 for ( int j
= int(src_y
- scale_factor_y
/2.0 + 1);
544 j
<= int(src_y
+ scale_factor_y_2
);
547 // We don't care to average pixels that don't exist (edges)
548 if ( j
< 0 || j
> M_IMGDATA
->m_height
- 1 )
551 for ( int i
= int(src_x
- scale_factor_x
/2.0 + 1);
552 i
<= src_x
+ scale_factor_x_2
;
555 // Don't average edge pixels
556 if ( i
< 0 || i
> M_IMGDATA
->m_width
- 1 )
559 // Calculate the actual index in our source pixels
560 src_pixel_index
= j
* M_IMGDATA
->m_width
+ i
;
562 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
563 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
564 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
566 sum_a
+= src_alpha
[src_pixel_index
];
572 // Calculate the average from the sum and number of averaged pixels
573 dst_data
[0] = (unsigned char)(sum_r
/ averaged_pixels
);
574 dst_data
[1] = (unsigned char)(sum_g
/ averaged_pixels
);
575 dst_data
[2] = (unsigned char)(sum_b
/ averaged_pixels
);
578 *dst_alpha
++ = (unsigned char)(sum_a
/ averaged_pixels
);
585 // The following two local functions are for the B-spline weighting of the
586 // bicubic sampling algorithm
587 static inline double spline_cube(double value
)
589 return value
<= 0.0 ? 0.0 : value
* value
* value
;
592 static inline double spline_weight(double value
)
594 return (spline_cube(value
+ 2) -
595 4 * spline_cube(value
+ 1) +
596 6 * spline_cube(value
) -
597 4 * spline_cube(value
- 1)) / 6;
600 // This is the bicubic resampling algorithm
601 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
603 // This function implements a Bicubic B-Spline algorithm for resampling.
604 // This method is certainly a little slower than wxImage's default pixel
605 // replication method, however for most reasonably sized images not being
606 // upsampled too much on a fairly average CPU this difference is hardly
607 // noticeable and the results are far more pleasing to look at.
609 // This particular bicubic algorithm does pixel weighting according to a
610 // B-Spline that basically implements a Gaussian bell-like weighting
611 // kernel. Because of this method the results may appear a bit blurry when
612 // upsampling by large factors. This is basically because a slight
613 // gaussian blur is being performed to get the smooth look of the upsampled
616 // Edge pixels: 3-4 possible solutions
617 // - (Wrap/tile) Wrap the image, take the color value from the opposite
618 // side of the image.
619 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n),
620 // where n is nonpositive, will have the value of (2, 1).
621 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to
622 // pixels which do have all neighbours.
623 // - (Clamp) Choose the nearest pixel along the border. This takes the
624 // border pixels and extends them out to infinity.
626 // NOTE: below the y_offset and x_offset variables are being set for edge
627 // pixels using the "Mirror" method mentioned above
631 ret_image
.Create(width
, height
, false);
633 unsigned char* src_data
= M_IMGDATA
->m_data
;
634 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
635 unsigned char* dst_data
= ret_image
.GetData();
636 unsigned char* dst_alpha
= NULL
;
640 ret_image
.SetAlpha();
641 dst_alpha
= ret_image
.GetAlpha();
644 for ( int dsty
= 0; dsty
< height
; dsty
++ )
646 // We need to calculate the source pixel to interpolate from - Y-axis
647 double srcpixy
= double(dsty
* M_IMGDATA
->m_height
) / height
;
648 double dy
= srcpixy
- (int)srcpixy
;
650 for ( int dstx
= 0; dstx
< width
; dstx
++ )
652 // X-axis of pixel to interpolate from
653 double srcpixx
= double(dstx
* M_IMGDATA
->m_width
) / width
;
654 double dx
= srcpixx
- (int)srcpixx
;
656 // Sums for each color channel
657 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0;
659 // Here we actually determine the RGBA values for the destination pixel
660 for ( int k
= -1; k
<= 2; k
++ )
663 int y_offset
= srcpixy
+ k
< 0.0
665 : srcpixy
+ k
>= M_IMGDATA
->m_height
666 ? M_IMGDATA
->m_height
- 1
667 : (int)(srcpixy
+ k
);
669 // Loop across the X axis
670 for ( int i
= -1; i
<= 2; i
++ )
673 int x_offset
= srcpixx
+ i
< 0.0
675 : srcpixx
+ i
>= M_IMGDATA
->m_width
676 ? M_IMGDATA
->m_width
- 1
677 : (int)(srcpixx
+ i
);
679 // Calculate the exact position where the source data
680 // should be pulled from based on the x_offset and y_offset
681 int src_pixel_index
= y_offset
*M_IMGDATA
->m_width
+ x_offset
;
683 // Calculate the weight for the specified pixel according
684 // to the bicubic b-spline kernel we're using for
687 pixel_weight
= spline_weight(i
- dx
)*spline_weight(k
- dy
);
689 // Create a sum of all velues for each color channel
690 // adjusted for the pixel's calculated weight
691 sum_r
+= src_data
[src_pixel_index
* 3 + 0] * pixel_weight
;
692 sum_g
+= src_data
[src_pixel_index
* 3 + 1] * pixel_weight
;
693 sum_b
+= src_data
[src_pixel_index
* 3 + 2] * pixel_weight
;
695 sum_a
+= src_alpha
[src_pixel_index
] * pixel_weight
;
699 // Put the data into the destination image. The summed values are
700 // of double data type and are rounded here for accuracy
701 dst_data
[0] = (unsigned char)(sum_r
+ 0.5);
702 dst_data
[1] = (unsigned char)(sum_g
+ 0.5);
703 dst_data
[2] = (unsigned char)(sum_b
+ 0.5);
707 *dst_alpha
++ = (unsigned char)sum_a
;
714 // Blur in the horizontal direction
715 wxImage
wxImage::BlurHorizontal(int blurRadius
) const
718 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
720 unsigned char* src_data
= M_IMGDATA
->m_data
;
721 unsigned char* dst_data
= ret_image
.GetData();
722 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
723 unsigned char* dst_alpha
= NULL
;
725 // Check for a mask or alpha
728 ret_image
.SetAlpha();
729 dst_alpha
= ret_image
.GetAlpha();
731 else if ( M_IMGDATA
->m_hasMask
)
733 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
734 M_IMGDATA
->m_maskGreen
,
735 M_IMGDATA
->m_maskBlue
);
738 // number of pixels we average over
739 const int blurArea
= blurRadius
*2 + 1;
741 // Horizontal blurring algorithm - average all pixels in the specified blur
742 // radius in the X or horizontal direction
743 for ( int y
= 0; y
< M_IMGDATA
->m_height
; y
++ )
745 // Variables used in the blurring algorithm
752 const unsigned char *src
;
755 // Calculate the average of all pixels in the blur radius for the first
757 for ( int kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++ )
759 // To deal with the pixels at the start of a row so it's not
760 // grabbing GOK values from memory at negative indices of the
761 // image's data or grabbing from the previous row
763 pixel_idx
= y
* M_IMGDATA
->m_width
;
765 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
767 src
= src_data
+ pixel_idx
*3;
772 sum_a
+= src_alpha
[pixel_idx
];
775 dst
= dst_data
+ y
* M_IMGDATA
->m_width
*3;
776 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
777 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
778 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
780 dst_alpha
[y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
782 // Now average the values of the rest of the pixels by just moving the
783 // blur radius box along the row
784 for ( int x
= 1; x
< M_IMGDATA
->m_width
; x
++ )
786 // Take care of edge pixels on the left edge by essentially
787 // duplicating the edge pixel
788 if ( x
- blurRadius
- 1 < 0 )
789 pixel_idx
= y
* M_IMGDATA
->m_width
;
791 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
793 // Subtract the value of the pixel at the left side of the blur
795 src
= src_data
+ pixel_idx
*3;
800 sum_a
-= src_alpha
[pixel_idx
];
802 // Take care of edge pixels on the right edge
803 if ( x
+ blurRadius
> M_IMGDATA
->m_width
- 1 )
804 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
806 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
808 // Add the value of the pixel being added to the end of our box
809 src
= src_data
+ pixel_idx
*3;
814 sum_a
+= src_alpha
[pixel_idx
];
816 // Save off the averaged data
817 dst
= dst_data
+ x
*3 + y
*M_IMGDATA
->m_width
*3;
818 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
819 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
820 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
822 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
829 // Blur in the vertical direction
830 wxImage
wxImage::BlurVertical(int blurRadius
) const
833 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
835 unsigned char* src_data
= M_IMGDATA
->m_data
;
836 unsigned char* dst_data
= ret_image
.GetData();
837 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
838 unsigned char* dst_alpha
= NULL
;
840 // Check for a mask or alpha
843 ret_image
.SetAlpha();
844 dst_alpha
= ret_image
.GetAlpha();
846 else if ( M_IMGDATA
->m_hasMask
)
848 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
,
849 M_IMGDATA
->m_maskGreen
,
850 M_IMGDATA
->m_maskBlue
);
853 // number of pixels we average over
854 const int blurArea
= blurRadius
*2 + 1;
856 // Vertical blurring algorithm - same as horizontal but switched the
857 // opposite direction
858 for ( int x
= 0; x
< M_IMGDATA
->m_width
; x
++ )
860 // Variables used in the blurring algorithm
867 const unsigned char *src
;
870 // Calculate the average of all pixels in our blur radius box for the
871 // first pixel of the column
872 for ( int kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++ )
874 // To deal with the pixels at the start of a column so it's not
875 // grabbing GOK values from memory at negative indices of the
876 // image's data or grabbing from the previous column
880 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
882 src
= src_data
+ pixel_idx
*3;
887 sum_a
+= src_alpha
[pixel_idx
];
890 dst
= dst_data
+ x
*3;
891 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
892 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
893 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
895 dst_alpha
[x
] = (unsigned char)(sum_a
/ blurArea
);
897 // Now average the values of the rest of the pixels by just moving the
898 // box along the column from top to bottom
899 for ( int y
= 1; y
< M_IMGDATA
->m_height
; y
++ )
901 // Take care of pixels that would be beyond the top edge by
902 // duplicating the top edge pixel for the column
903 if ( y
- blurRadius
- 1 < 0 )
906 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
908 // Subtract the value of the pixel at the top of our blur radius box
909 src
= src_data
+ pixel_idx
*3;
914 sum_a
-= src_alpha
[pixel_idx
];
916 // Take care of the pixels that would be beyond the bottom edge of
917 // the image similar to the top edge
918 if ( y
+ blurRadius
> M_IMGDATA
->m_height
- 1 )
919 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
921 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
923 // Add the value of the pixel being added to the end of our box
924 src
= src_data
+ pixel_idx
*3;
929 sum_a
+= src_alpha
[pixel_idx
];
931 // Save off the averaged data
932 dst
= dst_data
+ (x
+ y
* M_IMGDATA
->m_width
) * 3;
933 dst
[0] = (unsigned char)(sum_r
/ blurArea
);
934 dst
[1] = (unsigned char)(sum_g
/ blurArea
);
935 dst
[2] = (unsigned char)(sum_b
/ blurArea
);
937 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = (unsigned char)(sum_a
/ blurArea
);
944 // The new blur function
945 wxImage
wxImage::Blur(int blurRadius
) const
948 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
950 // Blur the image in each direction
951 ret_image
= BlurHorizontal(blurRadius
);
952 ret_image
= ret_image
.BlurVertical(blurRadius
);
957 wxImage
wxImage::Rotate90( bool clockwise
) const
961 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
963 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
965 unsigned char *data
= image
.GetData();
967 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
969 unsigned char *source_data
= M_IMGDATA
->m_data
;
970 unsigned char *target_data
;
971 unsigned char *alpha_data
= 0 ;
972 unsigned char *source_alpha
= 0 ;
973 unsigned char *target_alpha
= 0 ;
975 if (M_IMGDATA
->m_hasMask
)
977 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
981 source_alpha
= M_IMGDATA
->m_alpha
;
985 alpha_data
= image
.GetAlpha() ;
989 long height
= M_IMGDATA
->m_height
;
990 long width
= M_IMGDATA
->m_width
;
992 for (long j
= 0; j
< height
; j
++)
994 for (long i
= 0; i
< width
; i
++)
998 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
1000 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
1004 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
1006 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
1008 memcpy( target_data
, source_data
, 3 );
1013 memcpy( target_alpha
, source_alpha
, 1 );
1022 wxImage
wxImage::Mirror( bool horizontally
) const
1026 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1028 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1030 unsigned char *data
= image
.GetData();
1031 unsigned char *alpha
= NULL
;
1033 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1035 if (M_IMGDATA
->m_alpha
!= NULL
) {
1037 alpha
= image
.GetAlpha();
1038 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
1041 if (M_IMGDATA
->m_hasMask
)
1042 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1044 long height
= M_IMGDATA
->m_height
;
1045 long width
= M_IMGDATA
->m_width
;
1047 unsigned char *source_data
= M_IMGDATA
->m_data
;
1048 unsigned char *target_data
;
1052 for (long j
= 0; j
< height
; j
++)
1055 target_data
= data
-3;
1056 for (long i
= 0; i
< width
; i
++)
1058 memcpy( target_data
, source_data
, 3 );
1066 // src_alpha starts at the first pixel and increases by 1 after each step
1067 // (a step here is the copy of the alpha value of one pixel)
1068 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1069 // dest_alpha starts just beyond the first line, decreases before each step,
1070 // and after each line is finished, increases by 2 widths (skipping the line
1071 // just copied and the line that will be copied next)
1072 unsigned char *dest_alpha
= alpha
+ width
;
1074 for (long jj
= 0; jj
< height
; ++jj
)
1076 for (long i
= 0; i
< width
; ++i
) {
1077 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1079 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1085 for (long i
= 0; i
< height
; i
++)
1087 target_data
= data
+ 3*width
*(height
-1-i
);
1088 memcpy( target_data
, source_data
, (size_t)3*width
);
1089 source_data
+= 3*width
;
1094 // src_alpha starts at the first pixel and increases by 1 width after each step
1095 // (a step here is the copy of the alpha channel of an entire line)
1096 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1097 // dest_alpha starts just beyond the last line (beyond the whole image)
1098 // and decreases by 1 width before each step
1099 unsigned char *dest_alpha
= alpha
+ width
* height
;
1101 for (long jj
= 0; jj
< height
; ++jj
)
1103 dest_alpha
-= width
;
1104 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1113 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1117 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1119 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1120 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1121 image
, wxT("invalid subimage size") );
1123 const int subwidth
= rect
.GetWidth();
1124 const int subheight
= rect
.GetHeight();
1126 image
.Create( subwidth
, subheight
, false );
1128 const unsigned char *src_data
= GetData();
1129 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1130 unsigned char *subdata
= image
.GetData();
1131 unsigned char *subalpha
= NULL
;
1133 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1135 if (src_alpha
!= NULL
) {
1137 subalpha
= image
.GetAlpha();
1138 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1141 if (M_IMGDATA
->m_hasMask
)
1142 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1144 const int width
= GetWidth();
1145 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1147 src_data
+= 3 * pixsoff
;
1148 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1150 for (long j
= 0; j
< subheight
; ++j
)
1152 memcpy( subdata
, src_data
, 3 * subwidth
);
1153 subdata
+= 3 * subwidth
;
1154 src_data
+= 3 * width
;
1155 if (subalpha
!= NULL
) {
1156 memcpy( subalpha
, src_alpha
, subwidth
);
1157 subalpha
+= subwidth
;
1165 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1166 int r_
, int g_
, int b_
) const
1170 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1171 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1173 int width
= GetWidth(), height
= GetHeight();
1174 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1176 unsigned char r
= (unsigned char)r_
;
1177 unsigned char g
= (unsigned char)g_
;
1178 unsigned char b
= (unsigned char)b_
;
1179 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1181 GetOrFindMaskColour( &r
, &g
, &b
);
1182 image
.SetMaskColour(r
, g
, b
);
1185 image
.SetRGB(wxRect(), r
, g
, b
);
1187 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1188 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1190 finalRect
.width
-= pos
.x
;
1192 finalRect
.height
-= pos
.y
;
1194 subRect
.Intersect(finalRect
);
1196 if (!subRect
.IsEmpty())
1198 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1199 image
.Paste(*this, pos
.x
, pos
.y
);
1201 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1207 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1209 wxCHECK_RET( Ok(), wxT("invalid image") );
1210 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1216 int width
= image
.GetWidth();
1217 int height
= image
.GetHeight();
1230 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1231 width
= M_IMGDATA
->m_width
- (x
+xx
);
1232 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1233 height
= M_IMGDATA
->m_height
- (y
+yy
);
1235 if (width
< 1) return;
1236 if (height
< 1) return;
1238 if ((!HasMask() && !image
.HasMask()) ||
1239 (HasMask() && !image
.HasMask()) ||
1240 ((HasMask() && image
.HasMask() &&
1241 (GetMaskRed()==image
.GetMaskRed()) &&
1242 (GetMaskGreen()==image
.GetMaskGreen()) &&
1243 (GetMaskBlue()==image
.GetMaskBlue()))))
1245 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1246 int source_step
= image
.GetWidth()*3;
1248 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1249 int target_step
= M_IMGDATA
->m_width
*3;
1250 for (int j
= 0; j
< height
; j
++)
1252 memcpy( target_data
, source_data
, width
*3 );
1253 source_data
+= source_step
;
1254 target_data
+= target_step
;
1258 // Copy over the alpha channel from the original image
1259 if ( image
.HasAlpha() )
1264 unsigned char* source_data
= image
.GetAlpha() + xx
+ yy
*image
.GetWidth();
1265 int source_step
= image
.GetWidth();
1267 unsigned char* target_data
= GetAlpha() + (x
+xx
) + (y
+yy
)*M_IMGDATA
->m_width
;
1268 int target_step
= M_IMGDATA
->m_width
;
1270 for (int j
= 0; j
< height
; j
++,
1271 source_data
+= source_step
,
1272 target_data
+= target_step
)
1274 memcpy( target_data
, source_data
, width
);
1278 if (!HasMask() && image
.HasMask())
1280 unsigned char r
= image
.GetMaskRed();
1281 unsigned char g
= image
.GetMaskGreen();
1282 unsigned char b
= image
.GetMaskBlue();
1284 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1285 int source_step
= image
.GetWidth()*3;
1287 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1288 int target_step
= M_IMGDATA
->m_width
*3;
1290 for (int j
= 0; j
< height
; j
++)
1292 for (int i
= 0; i
< width
*3; i
+=3)
1294 if ((source_data
[i
] != r
) ||
1295 (source_data
[i
+1] != g
) ||
1296 (source_data
[i
+2] != b
))
1298 memcpy( target_data
+i
, source_data
+i
, 3 );
1301 source_data
+= source_step
;
1302 target_data
+= target_step
;
1307 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1308 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1310 wxCHECK_RET( Ok(), wxT("invalid image") );
1314 unsigned char *data
= GetData();
1316 const int w
= GetWidth();
1317 const int h
= GetHeight();
1319 for (int j
= 0; j
< h
; j
++)
1320 for (int i
= 0; i
< w
; i
++)
1322 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1332 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1336 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1338 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1340 unsigned char *dest
= image
.GetData();
1342 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1344 unsigned char *src
= M_IMGDATA
->m_data
;
1345 bool hasMask
= M_IMGDATA
->m_hasMask
;
1346 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1347 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1348 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1351 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1353 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1354 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1356 // don't modify the mask
1357 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1359 memcpy(dest
, src
, 3);
1363 // calculate the luma
1364 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1365 dest
[0] = dest
[1] = dest
[2] = static_cast<unsigned char>(luma
);
1369 // copy the alpha channel, if any
1372 const size_t alphaSize
= GetWidth() * GetHeight();
1373 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1374 memcpy(alpha
, GetAlpha(), alphaSize
);
1376 image
.SetAlpha(alpha
);
1382 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1386 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1388 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1390 unsigned char *data
= image
.GetData();
1392 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1394 if (M_IMGDATA
->m_hasMask
)
1396 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1397 M_IMGDATA
->m_maskBlue
== b
)
1398 image
.SetMaskColour( 255, 255, 255 );
1400 image
.SetMaskColour( 0, 0, 0 );
1403 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1405 unsigned char *srcd
= M_IMGDATA
->m_data
;
1406 unsigned char *tard
= image
.GetData();
1408 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1410 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1411 tard
[0] = tard
[1] = tard
[2] = 255;
1413 tard
[0] = tard
[1] = tard
[2] = 0;
1419 int wxImage::GetWidth() const
1421 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1423 return M_IMGDATA
->m_width
;
1426 int wxImage::GetHeight() const
1428 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1430 return M_IMGDATA
->m_height
;
1433 wxBitmapType
wxImage::GetType() const
1435 wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID
, wxT("invalid image") );
1437 return M_IMGDATA
->m_type
;
1440 void wxImage::SetType(wxBitmapType type
)
1442 wxCHECK_RET( IsOk(), "must create the image before setting its type");
1444 // type can be wxBITMAP_TYPE_INVALID to reset the image type to default
1445 wxASSERT_MSG( type
!= wxBITMAP_TYPE_MAX
, "invalid bitmap type" );
1447 M_IMGDATA
->m_type
= type
;
1450 long wxImage::XYToIndex(int x
, int y
) const
1454 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1456 return y
*M_IMGDATA
->m_width
+ x
;
1462 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1464 long pos
= XYToIndex(x
, y
);
1465 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1471 M_IMGDATA
->m_data
[ pos
] = r
;
1472 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1473 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1476 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1478 wxCHECK_RET( Ok(), wxT("invalid image") );
1483 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1484 if ( rect
== wxRect() )
1490 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1491 imageRect
.Contains(rect
.GetBottomRight()),
1492 wxT("invalid bounding rectangle") );
1495 int x1
= rect
.GetLeft(),
1497 x2
= rect
.GetRight() + 1,
1498 y2
= rect
.GetBottom() + 1;
1500 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1501 int x
, y
, width
= GetWidth();
1502 for (y
= y1
; y
< y2
; y
++)
1504 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1505 for (x
= x1
; x
< x2
; x
++)
1514 unsigned char wxImage::GetRed( int x
, int y
) const
1516 long pos
= XYToIndex(x
, y
);
1517 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1521 return M_IMGDATA
->m_data
[pos
];
1524 unsigned char wxImage::GetGreen( int x
, int y
) const
1526 long pos
= XYToIndex(x
, y
);
1527 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1531 return M_IMGDATA
->m_data
[pos
+1];
1534 unsigned char wxImage::GetBlue( int x
, int y
) const
1536 long pos
= XYToIndex(x
, y
);
1537 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1541 return M_IMGDATA
->m_data
[pos
+2];
1544 bool wxImage::IsOk() const
1546 // image of 0 width or height can't be considered ok - at least because it
1547 // causes crashes in ConvertToBitmap() if we don't catch it in time
1548 wxImageRefData
*data
= M_IMGDATA
;
1549 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1552 unsigned char *wxImage::GetData() const
1554 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1556 return M_IMGDATA
->m_data
;
1559 void wxImage::SetData( unsigned char *data
, bool static_data
)
1561 wxCHECK_RET( Ok(), wxT("invalid image") );
1563 wxImageRefData
*newRefData
= new wxImageRefData();
1565 newRefData
->m_width
= M_IMGDATA
->m_width
;
1566 newRefData
->m_height
= M_IMGDATA
->m_height
;
1567 newRefData
->m_data
= data
;
1568 newRefData
->m_ok
= true;
1569 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1570 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1571 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1572 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1573 newRefData
->m_static
= static_data
;
1577 m_refData
= newRefData
;
1580 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1582 wxImageRefData
*newRefData
= new wxImageRefData();
1586 newRefData
->m_width
= new_width
;
1587 newRefData
->m_height
= new_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
;
1597 newRefData
->m_width
= new_width
;
1598 newRefData
->m_height
= new_height
;
1599 newRefData
->m_data
= data
;
1600 newRefData
->m_ok
= true;
1602 newRefData
->m_static
= static_data
;
1606 m_refData
= newRefData
;
1609 // ----------------------------------------------------------------------------
1610 // alpha channel support
1611 // ----------------------------------------------------------------------------
1613 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1615 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1617 long pos
= XYToIndex(x
, y
);
1618 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1622 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1625 unsigned char wxImage::GetAlpha(int x
, int y
) const
1627 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1629 long pos
= XYToIndex(x
, y
);
1630 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1632 return M_IMGDATA
->m_alpha
[pos
];
1636 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1640 const int w
= M_IMGDATA
->m_width
;
1641 const int h
= M_IMGDATA
->m_height
;
1643 unsigned char *alpha
= GetAlpha();
1644 unsigned char *data
= GetData();
1646 for ( int y
= 0; y
< h
; y
++ )
1648 for ( int x
= 0; x
< w
; x
++ )
1660 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1662 wxCHECK_RET( Ok(), wxT("invalid image") );
1668 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1671 if( !M_IMGDATA
->m_staticAlpha
)
1672 free(M_IMGDATA
->m_alpha
);
1674 M_IMGDATA
->m_alpha
= alpha
;
1675 M_IMGDATA
->m_staticAlpha
= static_data
;
1678 unsigned char *wxImage::GetAlpha() const
1680 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1682 return M_IMGDATA
->m_alpha
;
1685 void wxImage::InitAlpha()
1687 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1689 // initialize memory for alpha channel
1692 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1693 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1697 // use the mask to initialize the alpha channel.
1698 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1700 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1701 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1702 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1703 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1707 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1708 ? wxIMAGE_ALPHA_TRANSPARENT
1709 : wxIMAGE_ALPHA_OPAQUE
;
1712 M_IMGDATA
->m_hasMask
= false;
1716 // make the image fully opaque
1717 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1721 // ----------------------------------------------------------------------------
1723 // ----------------------------------------------------------------------------
1725 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1727 wxCHECK_RET( Ok(), wxT("invalid image") );
1731 M_IMGDATA
->m_maskRed
= r
;
1732 M_IMGDATA
->m_maskGreen
= g
;
1733 M_IMGDATA
->m_maskBlue
= b
;
1734 M_IMGDATA
->m_hasMask
= true;
1737 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1739 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1741 if (M_IMGDATA
->m_hasMask
)
1743 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1744 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1745 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1750 FindFirstUnusedColour(r
, g
, b
);
1755 unsigned char wxImage::GetMaskRed() const
1757 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1759 return M_IMGDATA
->m_maskRed
;
1762 unsigned char wxImage::GetMaskGreen() const
1764 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1766 return M_IMGDATA
->m_maskGreen
;
1769 unsigned char wxImage::GetMaskBlue() const
1771 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1773 return M_IMGDATA
->m_maskBlue
;
1776 void wxImage::SetMask( bool mask
)
1778 wxCHECK_RET( Ok(), wxT("invalid image") );
1782 M_IMGDATA
->m_hasMask
= mask
;
1785 bool wxImage::HasMask() const
1787 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1789 return M_IMGDATA
->m_hasMask
;
1792 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1794 long pos
= XYToIndex(x
, y
);
1795 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1798 if ( M_IMGDATA
->m_hasMask
)
1800 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1801 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1802 p
[1] == M_IMGDATA
->m_maskGreen
&&
1803 p
[2] == M_IMGDATA
->m_maskBlue
)
1810 if ( M_IMGDATA
->m_alpha
)
1812 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1814 // transparent enough
1823 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1824 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1826 // check that the images are the same size
1827 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1829 wxLogError( _("Image and mask have different sizes.") );
1833 // find unused colour
1834 unsigned char r
,g
,b
;
1835 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1837 wxLogError( _("No unused colour in image being masked.") );
1843 unsigned char *imgdata
= GetData();
1844 unsigned char *maskdata
= mask
.GetData();
1846 const int w
= GetWidth();
1847 const int h
= GetHeight();
1849 for (int j
= 0; j
< h
; j
++)
1851 for (int i
= 0; i
< w
; i
++)
1853 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1864 SetMaskColour(r
, g
, b
);
1870 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1875 unsigned char mr
, mg
, mb
;
1876 if ( !FindFirstUnusedColour(&mr
, &mg
, &mb
) )
1878 wxLogError( _("No unused colour in image being masked.") );
1882 ConvertAlphaToMask(mr
, mg
, mb
, threshold
);
1886 void wxImage::ConvertAlphaToMask(unsigned char mr
,
1889 unsigned char threshold
)
1897 SetMaskColour(mr
, mg
, mb
);
1899 unsigned char *imgdata
= GetData();
1900 unsigned char *alphadata
= GetAlpha();
1903 int h
= GetHeight();
1905 for (int y
= 0; y
< h
; y
++)
1907 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1909 if (*alphadata
< threshold
)
1918 if ( !M_IMGDATA
->m_staticAlpha
)
1919 free(M_IMGDATA
->m_alpha
);
1921 M_IMGDATA
->m_alpha
= NULL
;
1922 M_IMGDATA
->m_staticAlpha
= false;
1925 // ----------------------------------------------------------------------------
1926 // Palette functions
1927 // ----------------------------------------------------------------------------
1931 bool wxImage::HasPalette() const
1936 return M_IMGDATA
->m_palette
.Ok();
1939 const wxPalette
& wxImage::GetPalette() const
1941 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1943 return M_IMGDATA
->m_palette
;
1946 void wxImage::SetPalette(const wxPalette
& palette
)
1948 wxCHECK_RET( Ok(), wxT("invalid image") );
1952 M_IMGDATA
->m_palette
= palette
;
1955 #endif // wxUSE_PALETTE
1957 // ----------------------------------------------------------------------------
1958 // Option functions (arbitrary name/value mapping)
1959 // ----------------------------------------------------------------------------
1961 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1965 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1966 if ( idx
== wxNOT_FOUND
)
1968 M_IMGDATA
->m_optionNames
.Add(name
);
1969 M_IMGDATA
->m_optionValues
.Add(value
);
1973 M_IMGDATA
->m_optionNames
[idx
] = name
;
1974 M_IMGDATA
->m_optionValues
[idx
] = value
;
1978 void wxImage::SetOption(const wxString
& name
, int value
)
1981 valStr
.Printf(wxT("%d"), value
);
1982 SetOption(name
, valStr
);
1985 wxString
wxImage::GetOption(const wxString
& name
) const
1988 return wxEmptyString
;
1990 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1991 if ( idx
== wxNOT_FOUND
)
1992 return wxEmptyString
;
1994 return M_IMGDATA
->m_optionValues
[idx
];
1997 int wxImage::GetOptionInt(const wxString
& name
) const
1999 return wxAtoi(GetOption(name
));
2002 bool wxImage::HasOption(const wxString
& name
) const
2004 return M_IMGDATA
? M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
2008 // ----------------------------------------------------------------------------
2010 // ----------------------------------------------------------------------------
2012 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2013 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
),
2014 int WXUNUSED_UNLESS_STREAMS(index
) )
2016 #if HAS_FILE_STREAMS
2017 if (wxFileExists(filename
))
2019 wxImageFileInputStream
stream(filename
);
2020 wxBufferedInputStream
bstream( stream
);
2021 return LoadFile(bstream
, type
, index
);
2025 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2029 #else // !HAS_FILE_STREAMS
2031 #endif // HAS_FILE_STREAMS
2034 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2035 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
2036 int WXUNUSED_UNLESS_STREAMS(index
) )
2038 #if HAS_FILE_STREAMS
2039 if (wxFileExists(filename
))
2041 wxImageFileInputStream
stream(filename
);
2042 wxBufferedInputStream
bstream( stream
);
2043 return LoadFile(bstream
, mimetype
, index
);
2047 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
2051 #else // !HAS_FILE_STREAMS
2053 #endif // HAS_FILE_STREAMS
2057 bool wxImage::SaveFile( const wxString
& filename
) const
2059 wxString ext
= filename
.AfterLast('.').Lower();
2061 wxImageHandler
*handler
= FindHandler(ext
, wxBITMAP_TYPE_ANY
);
2064 wxLogError(_("Can't save image to file '%s': unknown extension."),
2069 return SaveFile(filename
, handler
->GetType());
2072 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2073 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) ) const
2075 #if HAS_FILE_STREAMS
2076 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2078 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2080 wxImageFileOutputStream
stream(filename
);
2082 if ( stream
.IsOk() )
2084 wxBufferedOutputStream
bstream( stream
);
2085 return SaveFile(bstream
, type
);
2087 #endif // HAS_FILE_STREAMS
2092 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2093 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2095 #if HAS_FILE_STREAMS
2096 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2098 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2100 wxImageFileOutputStream
stream(filename
);
2102 if ( stream
.IsOk() )
2104 wxBufferedOutputStream
bstream( stream
);
2105 return SaveFile(bstream
, mimetype
);
2107 #endif // HAS_FILE_STREAMS
2112 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2114 #if HAS_FILE_STREAMS
2115 wxImageFileInputStream
stream(name
);
2116 return CanRead(stream
);
2122 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2123 wxBitmapType
WXUNUSED_UNLESS_STREAMS(type
) )
2125 #if HAS_FILE_STREAMS
2126 wxImageFileInputStream
stream(name
);
2128 return GetImageCount(stream
, type
);
2136 bool wxImage::CanRead( wxInputStream
&stream
)
2138 const wxList
& list
= GetHandlers();
2140 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2142 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2143 if (handler
->CanRead( stream
))
2150 int wxImage::GetImageCount( wxInputStream
&stream
, wxBitmapType type
)
2152 wxImageHandler
*handler
;
2154 if ( type
== wxBITMAP_TYPE_ANY
)
2156 const wxList
& list
= GetHandlers();
2158 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2160 node
= node
->GetNext() )
2162 handler
= (wxImageHandler
*)node
->GetData();
2163 if ( handler
->CanRead(stream
) )
2165 const int count
= handler
->GetImageCount(stream
);
2172 wxLogWarning(_("No handler found for image type."));
2176 handler
= FindHandler(type
);
2180 wxLogWarning(_("No image handler for type %ld defined."), type
);
2184 if ( handler
->CanRead(stream
) )
2186 return handler
->GetImageCount(stream
);
2190 wxLogError(_("Image file is not of type %ld."), type
);
2195 bool wxImage::DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
)
2197 // save the options values which can be clobbered by the handler (e.g. many
2198 // of them call Destroy() before trying to load the file)
2199 const unsigned maxWidth
= GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH
),
2200 maxHeight
= GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT
);
2202 if ( !handler
.LoadFile(this, stream
, true/*verbose*/, index
) )
2205 M_IMGDATA
->m_type
= handler
.GetType();
2207 // rescale the image to the specified size if needed
2208 if ( maxWidth
|| maxHeight
)
2210 const unsigned widthOrig
= GetWidth(),
2211 heightOrig
= GetHeight();
2213 // this uses the same (trivial) algorithm as the JPEG handler
2214 unsigned width
= widthOrig
,
2215 height
= heightOrig
;
2216 while ( (maxWidth
&& width
> maxWidth
) ||
2217 (maxHeight
&& height
> maxHeight
) )
2223 if ( width
!= widthOrig
|| height
!= heightOrig
)
2224 Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
2230 bool wxImage::LoadFile( wxInputStream
& stream
, wxBitmapType type
, int index
)
2234 wxImageHandler
*handler
;
2236 if ( type
== wxBITMAP_TYPE_ANY
)
2238 const wxList
& list
= GetHandlers();
2239 for ( wxList::compatibility_iterator node
= list
.GetFirst();
2241 node
= node
->GetNext() )
2243 handler
= (wxImageHandler
*)node
->GetData();
2244 if ( handler
->CanRead(stream
) && DoLoad(*handler
, stream
, index
) )
2248 wxLogWarning( _("No handler found for image type.") );
2252 //else: have specific type
2254 handler
= FindHandler(type
);
2257 wxLogWarning( _("No image handler for type %ld defined."), type
);
2261 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2263 wxLogError(_("Image file is not of type %ld."), type
);
2267 return DoLoad(*handler
, stream
, index
);
2270 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2274 m_refData
= new wxImageRefData
;
2276 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2280 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2284 if ( stream
.IsSeekable() && !handler
->CanRead(stream
) )
2286 wxLogError(_("Image file is not of type %s."), mimetype
);
2290 return DoLoad(*handler
, stream
, index
);
2293 bool wxImage::DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const
2295 wxImage
* const self
= const_cast<wxImage
*>(this);
2296 if ( !handler
.SaveFile(self
, stream
) )
2299 M_IMGDATA
->m_type
= handler
.GetType();
2303 bool wxImage::SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const
2305 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2307 wxImageHandler
*handler
= FindHandler(type
);
2310 wxLogWarning( _("No image handler for type %d defined."), type
);
2314 return DoSave(*handler
, stream
);
2317 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2319 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2321 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2324 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2327 return DoSave(*handler
, stream
);
2330 #endif // wxUSE_STREAMS
2332 // ----------------------------------------------------------------------------
2333 // image I/O handlers
2334 // ----------------------------------------------------------------------------
2336 void wxImage::AddHandler( wxImageHandler
*handler
)
2338 // Check for an existing handler of the type being added.
2339 if (FindHandler( handler
->GetType() ) == 0)
2341 sm_handlers
.Append( handler
);
2345 // This is not documented behaviour, merely the simplest 'fix'
2346 // for preventing duplicate additions. If someone ever has
2347 // a good reason to add and remove duplicate handlers (and they
2348 // may) we should probably refcount the duplicates.
2349 // also an issue in InsertHandler below.
2351 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2352 handler
->GetName().c_str() );
2357 void wxImage::InsertHandler( wxImageHandler
*handler
)
2359 // Check for an existing handler of the type being added.
2360 if (FindHandler( handler
->GetType() ) == 0)
2362 sm_handlers
.Insert( handler
);
2366 // see AddHandler for additional comments.
2367 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2368 handler
->GetName().c_str() );
2373 bool wxImage::RemoveHandler( const wxString
& name
)
2375 wxImageHandler
*handler
= FindHandler(name
);
2378 sm_handlers
.DeleteObject(handler
);
2386 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2388 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2391 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2392 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2394 node
= node
->GetNext();
2399 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, wxBitmapType bitmapType
)
2401 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2404 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2405 if ((bitmapType
== wxBITMAP_TYPE_ANY
) || (handler
->GetType() == bitmapType
))
2407 if (handler
->GetExtension() == extension
)
2409 if (handler
->GetAltExtensions().Index(extension
, false) != wxNOT_FOUND
)
2412 node
= node
->GetNext();
2417 wxImageHandler
*wxImage::FindHandler(wxBitmapType bitmapType
)
2419 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2422 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2423 if (handler
->GetType() == bitmapType
) return handler
;
2424 node
= node
->GetNext();
2429 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2431 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2434 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2435 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2436 node
= node
->GetNext();
2441 void wxImage::InitStandardHandlers()
2444 AddHandler(new wxBMPHandler
);
2445 #endif // wxUSE_STREAMS
2448 void wxImage::CleanUpHandlers()
2450 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2453 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2454 wxList::compatibility_iterator next
= node
->GetNext();
2459 sm_handlers
.Clear();
2462 wxString
wxImage::GetImageExtWildcard()
2466 wxList
& Handlers
= wxImage::GetHandlers();
2467 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2470 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2471 fmts
+= wxT("*.") + Handler
->GetExtension();
2472 for (size_t i
= 0; i
< Handler
->GetAltExtensions().size(); i
++)
2473 fmts
+= wxT(";*.") + Handler
->GetAltExtensions()[i
];
2474 Node
= Node
->GetNext();
2475 if ( Node
) fmts
+= wxT(";");
2478 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2481 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2483 const double red
= rgb
.red
/ 255.0,
2484 green
= rgb
.green
/ 255.0,
2485 blue
= rgb
.blue
/ 255.0;
2487 // find the min and max intensity (and remember which one was it for the
2489 double minimumRGB
= red
;
2490 if ( green
< minimumRGB
)
2492 if ( blue
< minimumRGB
)
2495 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2496 double maximumRGB
= red
;
2497 if ( green
> maximumRGB
)
2502 if ( blue
> maximumRGB
)
2508 const double value
= maximumRGB
;
2510 double hue
= 0.0, saturation
;
2511 const double deltaRGB
= maximumRGB
- minimumRGB
;
2512 if ( wxIsNullDouble(deltaRGB
) )
2514 // Gray has no color
2523 hue
= (green
- blue
) / deltaRGB
;
2527 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2531 hue
= 4.0 + (red
- green
) / deltaRGB
;
2535 wxFAIL_MSG(wxT("hue not specified"));
2544 saturation
= deltaRGB
/ maximumRGB
;
2547 return HSVValue(hue
, saturation
, value
);
2550 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2552 double red
, green
, blue
;
2554 if ( wxIsNullDouble(hsv
.saturation
) )
2563 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2564 int i
= (int)floor(hue
);
2565 double f
= hue
- i
; // fractional part of h
2566 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2572 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2577 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2585 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2590 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2595 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2603 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2608 return RGBValue((unsigned char)(red
* 255.0),
2609 (unsigned char)(green
* 255.0),
2610 (unsigned char)(blue
* 255.0));
2614 * Rotates the hue of each pixel of the image. angle is a double in the range
2615 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2617 void wxImage::RotateHue(double angle
)
2621 unsigned char *srcBytePtr
;
2622 unsigned char *dstBytePtr
;
2623 unsigned long count
;
2624 wxImage::HSVValue hsv
;
2625 wxImage::RGBValue rgb
;
2627 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2628 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2629 if ( count
> 0 && !wxIsNullDouble(angle
) )
2631 srcBytePtr
= M_IMGDATA
->m_data
;
2632 dstBytePtr
= srcBytePtr
;
2635 rgb
.red
= *srcBytePtr
++;
2636 rgb
.green
= *srcBytePtr
++;
2637 rgb
.blue
= *srcBytePtr
++;
2638 hsv
= RGBtoHSV(rgb
);
2640 hsv
.hue
= hsv
.hue
+ angle
;
2642 hsv
.hue
= hsv
.hue
- 1.0;
2643 else if (hsv
.hue
< 0.0)
2644 hsv
.hue
= hsv
.hue
+ 1.0;
2646 rgb
= HSVtoRGB(hsv
);
2647 *dstBytePtr
++ = rgb
.red
;
2648 *dstBytePtr
++ = rgb
.green
;
2649 *dstBytePtr
++ = rgb
.blue
;
2650 } while (--count
!= 0);
2654 //-----------------------------------------------------------------------------
2656 //-----------------------------------------------------------------------------
2658 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2661 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2666 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2671 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2676 bool wxImageHandler::CanRead( const wxString
& name
)
2678 if (wxFileExists(name
))
2680 wxImageFileInputStream
stream(name
);
2681 return CanRead(stream
);
2684 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2689 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2691 wxFileOffset posOld
= stream
.TellI();
2692 if ( posOld
== wxInvalidOffset
)
2694 // can't test unseekable stream
2698 bool ok
= DoCanRead(stream
);
2700 // restore the old position to be able to test other formats and so on
2701 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2703 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2705 // reading would fail anyhow as we're not at the right position
2712 #endif // wxUSE_STREAMS
2716 wxImageHandler::GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
)
2718 wxCHECK_MSG( x
&& y
, wxIMAGE_RESOLUTION_NONE
, _T("NULL pointer") );
2720 if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
2721 image
.HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
2723 *x
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
);
2724 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
2726 else if ( image
.HasOption(wxIMAGE_OPTION_RESOLUTION
) )
2729 *y
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTION
);
2731 else // no resolution options specified
2736 return wxIMAGE_RESOLUTION_NONE
;
2739 // get the resolution unit too
2740 int resUnit
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
);
2743 // this is the default
2744 resUnit
= wxIMAGE_RESOLUTION_INCHES
;
2747 return (wxImageResolution
)resUnit
;
2750 // ----------------------------------------------------------------------------
2751 // image histogram stuff
2752 // ----------------------------------------------------------------------------
2755 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2760 unsigned char g2
) const
2762 unsigned long key
= MakeKey(r2
, g2
, b2
);
2764 while ( find(key
) != end() )
2766 // color already used
2778 wxLogError(_("No unused colour in image.") );
2784 key
= MakeKey(r2
, g2
, b2
);
2798 wxImage::FindFirstUnusedColour(unsigned char *r
,
2803 unsigned char g2
) const
2805 wxImageHistogram histogram
;
2807 ComputeHistogram(histogram
);
2809 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2815 // Counts and returns the number of different colours. Optionally stops
2816 // when it exceeds 'stopafter' different colours. This is useful, for
2817 // example, to see if the image can be saved as 8-bit (256 colour or
2818 // less, in this case it would be invoked as CountColours(256)). Default
2819 // value for stopafter is -1 (don't care).
2821 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2825 unsigned char r
, g
, b
;
2827 unsigned long size
, nentries
, key
;
2830 size
= GetWidth() * GetHeight();
2833 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2838 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2840 if (h
.Get(key
) == NULL
)
2851 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2853 unsigned char *p
= GetData();
2854 unsigned long nentries
= 0;
2858 const unsigned long size
= GetWidth() * GetHeight();
2860 unsigned char r
, g
, b
;
2861 for ( unsigned long n
= 0; n
< size
; n
++ )
2867 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2869 if ( entry
.value
++ == 0 )
2870 entry
.index
= nentries
++;
2877 * Rotation code by Carlos Moreno
2880 static const double wxROTATE_EPSILON
= 1e-10;
2882 // Auxiliary function to rotate a point (x,y) with respect to point p0
2883 // make it inline and use a straight return to facilitate optimization
2884 // also, the function receives the sine and cosine of the angle to avoid
2885 // repeating the time-consuming calls to these functions -- sin/cos can
2886 // be computed and stored in the calling function.
2888 static inline wxRealPoint
2889 wxRotatePoint(const wxRealPoint
& p
, double cos_angle
, double sin_angle
,
2890 const wxRealPoint
& p0
)
2892 return wxRealPoint(p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2893 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2896 static inline wxRealPoint
2897 wxRotatePoint(double x
, double y
, double cos_angle
, double sin_angle
,
2898 const wxRealPoint
& p0
)
2900 return wxRotatePoint (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2903 wxImage
wxImage::Rotate(double angle
,
2904 const wxPoint
& centre_of_rotation
,
2906 wxPoint
*offset_after_rotation
) const
2908 // screen coordinates are a mirror image of "real" coordinates
2911 const bool has_alpha
= HasAlpha();
2913 const int w
= GetWidth();
2914 const int h
= GetHeight();
2918 // Create pointer-based array to accelerate access to wxImage's data
2919 unsigned char ** data
= new unsigned char * [h
];
2920 data
[0] = GetData();
2921 for (i
= 1; i
< h
; i
++)
2922 data
[i
] = data
[i
- 1] + (3 * w
);
2924 // Same for alpha channel
2925 unsigned char ** alpha
= NULL
;
2928 alpha
= new unsigned char * [h
];
2929 alpha
[0] = GetAlpha();
2930 for (i
= 1; i
< h
; i
++)
2931 alpha
[i
] = alpha
[i
- 1] + w
;
2934 // precompute coefficients for rotation formula
2935 const double cos_angle
= cos(angle
);
2936 const double sin_angle
= sin(angle
);
2938 // Create new Image to store the result
2939 // First, find rectangle that covers the rotated image; to do that,
2940 // rotate the four corners
2942 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2944 wxRealPoint p1
= wxRotatePoint (0, 0, cos_angle
, sin_angle
, p0
);
2945 wxRealPoint p2
= wxRotatePoint (0, h
, cos_angle
, sin_angle
, p0
);
2946 wxRealPoint p3
= wxRotatePoint (w
, 0, cos_angle
, sin_angle
, p0
);
2947 wxRealPoint p4
= wxRotatePoint (w
, h
, cos_angle
, sin_angle
, p0
);
2949 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2950 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2951 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2952 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2954 // Create rotated image
2955 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2956 // With alpha channel
2960 if (offset_after_rotation
!= NULL
)
2962 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2965 // the rotated (destination) image is always accessed sequentially via this
2966 // pointer, there is no need for pointer-based arrays here
2967 unsigned char *dst
= rotated
.GetData();
2969 unsigned char *alpha_dst
= has_alpha
? rotated
.GetAlpha() : NULL
;
2971 // if the original image has a mask, use its RGB values as the blank pixel,
2972 // else, fall back to default (black).
2973 unsigned char blank_r
= 0;
2974 unsigned char blank_g
= 0;
2975 unsigned char blank_b
= 0;
2979 blank_r
= GetMaskRed();
2980 blank_g
= GetMaskGreen();
2981 blank_b
= GetMaskBlue();
2982 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2985 // Now, for each point of the rotated image, find where it came from, by
2986 // performing an inverse rotation (a rotation of -angle) and getting the
2987 // pixel at those coordinates
2989 const int rH
= rotated
.GetHeight();
2990 const int rW
= rotated
.GetWidth();
2992 // do the (interpolating) test outside of the loops, so that it is done
2993 // only once, instead of repeating it for each pixel.
2996 for (int y
= 0; y
< rH
; y
++)
2998 for (int x
= 0; x
< rW
; x
++)
3000 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3002 if (-0.25 < src
.x
&& src
.x
< w
- 0.75 &&
3003 -0.25 < src
.y
&& src
.y
< h
- 0.75)
3005 // interpolate using the 4 enclosing grid-points. Those
3006 // points can be obtained using floor and ceiling of the
3007 // exact coordinates of the point
3010 if (0 < src
.x
&& src
.x
< w
- 1)
3012 x1
= wxRound(floor(src
.x
));
3013 x2
= wxRound(ceil(src
.x
));
3015 else // else means that x is near one of the borders (0 or width-1)
3017 x1
= x2
= wxRound (src
.x
);
3020 if (0 < src
.y
&& src
.y
< h
- 1)
3022 y1
= wxRound(floor(src
.y
));
3023 y2
= wxRound(ceil(src
.y
));
3027 y1
= y2
= wxRound (src
.y
);
3030 // get four points and the distances (square of the distance,
3031 // for efficiency reasons) for the interpolation formula
3033 // GRG: Do not calculate the points until they are
3034 // really needed -- this way we can calculate
3035 // just one, instead of four, if d1, d2, d3
3036 // or d4 are < wxROTATE_EPSILON
3038 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
3039 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
3040 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
3041 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
3043 // Now interpolate as a weighted average of the four surrounding
3044 // points, where the weights are the distances to each of those points
3046 // If the point is exactly at one point of the grid of the source
3047 // image, then don't interpolate -- just assign the pixel
3049 // d1,d2,d3,d4 are positive -- no need for abs()
3050 if (d1
< wxROTATE_EPSILON
)
3052 unsigned char *p
= data
[y1
] + (3 * x1
);
3058 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
3060 else if (d2
< wxROTATE_EPSILON
)
3062 unsigned char *p
= data
[y1
] + (3 * x2
);
3068 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
3070 else if (d3
< wxROTATE_EPSILON
)
3072 unsigned char *p
= data
[y2
] + (3 * x2
);
3078 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
3080 else if (d4
< wxROTATE_EPSILON
)
3082 unsigned char *p
= data
[y2
] + (3 * x1
);
3088 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
3092 // weights for the weighted average are proportional to the inverse of the distance
3093 unsigned char *v1
= data
[y1
] + (3 * x1
);
3094 unsigned char *v2
= data
[y1
] + (3 * x2
);
3095 unsigned char *v3
= data
[y2
] + (3 * x2
);
3096 unsigned char *v4
= data
[y2
] + (3 * x1
);
3098 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
3102 *(dst
++) = (unsigned char)
3103 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3104 w3
* *(v3
++) + w4
* *(v4
++)) /
3105 (w1
+ w2
+ w3
+ w4
) );
3106 *(dst
++) = (unsigned char)
3107 ( (w1
* *(v1
++) + w2
* *(v2
++) +
3108 w3
* *(v3
++) + w4
* *(v4
++)) /
3109 (w1
+ w2
+ w3
+ w4
) );
3110 *(dst
++) = (unsigned char)
3111 ( (w1
* *v1
+ w2
* *v2
+
3112 w3
* *v3
+ w4
* *v4
) /
3113 (w1
+ w2
+ w3
+ w4
) );
3117 v1
= alpha
[y1
] + (x1
);
3118 v2
= alpha
[y1
] + (x2
);
3119 v3
= alpha
[y2
] + (x2
);
3120 v4
= alpha
[y2
] + (x1
);
3122 *(alpha_dst
++) = (unsigned char)
3123 ( (w1
* *v1
+ w2
* *v2
+
3124 w3
* *v3
+ w4
* *v4
) /
3125 (w1
+ w2
+ w3
+ w4
) );
3141 else // not interpolating
3143 for (int y
= 0; y
< rH
; y
++)
3145 for (int x
= 0; x
< rW
; x
++)
3147 wxRealPoint src
= wxRotatePoint (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
3149 const int xs
= wxRound (src
.x
); // wxRound rounds to the
3150 const int ys
= wxRound (src
.y
); // closest integer
3152 if (0 <= xs
&& xs
< w
&& 0 <= ys
&& ys
< h
)
3154 unsigned char *p
= data
[ys
] + (3 * xs
);
3160 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
3169 *(alpha_dst
++) = 255;
3185 // A module to allow wxImage initialization/cleanup
3186 // without calling these functions from app.cpp or from
3187 // the user's application.
3189 class wxImageModule
: public wxModule
3191 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3194 bool OnInit() { wxImage::InitStandardHandlers(); return true; }
3195 void OnExit() { wxImage::CleanUpHandlers(); }
3198 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3201 #endif // wxUSE_IMAGE