1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/palette.h"
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
35 #include "wx/xpmdecod.h"
41 // make the code compile with either wxFile*Stream or wxFFile*Stream:
42 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
49 typedef wxFFileInputStream wxImageFileInputStream
;
50 typedef wxFFileOutputStream wxImageFileOutputStream
;
51 #endif // wxUSE_FILE/wxUSE_FFILE
52 #endif // HAS_FILE_STREAMS
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 class wxImageRefData
: public wxObjectRefData
62 virtual ~wxImageRefData();
66 unsigned char *m_data
;
69 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
71 // alpha channel data, may be NULL for the formats without alpha support
72 unsigned char *m_alpha
;
76 // if true, m_data is pointer to static data and shouldn't be freed
79 // same as m_static but for m_alpha
84 #endif // wxUSE_PALETTE
86 wxArrayString m_optionNames
;
87 wxArrayString m_optionValues
;
89 DECLARE_NO_COPY_CLASS(wxImageRefData
)
92 wxImageRefData::wxImageRefData()
97 m_alpha
= (unsigned char *) NULL
;
106 m_staticAlpha
= false;
109 wxImageRefData::~wxImageRefData()
113 if ( !m_staticAlpha
)
117 wxList
wxImage::sm_handlers
;
121 //-----------------------------------------------------------------------------
123 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
125 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
127 wxImage::wxImage( int width
, int height
, bool clear
)
129 Create( width
, height
, clear
);
132 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
134 Create( width
, height
, data
, static_data
);
137 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
139 Create( width
, height
, data
, alpha
, static_data
);
142 wxImage::wxImage( const wxString
& name
, long type
, int index
)
144 LoadFile( name
, type
, index
);
147 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
149 LoadFile( name
, mimetype
, index
);
153 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
155 LoadFile( stream
, type
, index
);
158 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
160 LoadFile( stream
, mimetype
, index
);
162 #endif // wxUSE_STREAMS
164 wxImage::wxImage( const char** xpmData
)
169 wxImage::wxImage( char** xpmData
)
171 Create((const char**) xpmData
);
174 bool wxImage::Create( const char** xpmData
)
179 wxXPMDecoder decoder
;
180 (*this) = decoder
.ReadData(xpmData
);
187 bool wxImage::Create( int width
, int height
, bool clear
)
191 m_refData
= new wxImageRefData();
193 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
194 if (!M_IMGDATA
->m_data
)
201 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
203 M_IMGDATA
->m_width
= width
;
204 M_IMGDATA
->m_height
= height
;
205 M_IMGDATA
->m_ok
= true;
210 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
214 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
216 m_refData
= new wxImageRefData();
218 M_IMGDATA
->m_data
= data
;
219 M_IMGDATA
->m_width
= width
;
220 M_IMGDATA
->m_height
= height
;
221 M_IMGDATA
->m_ok
= true;
222 M_IMGDATA
->m_static
= static_data
;
227 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
231 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
233 m_refData
= new wxImageRefData();
235 M_IMGDATA
->m_data
= data
;
236 M_IMGDATA
->m_alpha
= alpha
;
237 M_IMGDATA
->m_width
= width
;
238 M_IMGDATA
->m_height
= height
;
239 M_IMGDATA
->m_ok
= true;
240 M_IMGDATA
->m_static
= static_data
;
245 void wxImage::Destroy()
250 wxObjectRefData
* wxImage::CreateRefData() const
252 return new wxImageRefData
;
255 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
257 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
258 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
260 wxImageRefData
* refData_new
= new wxImageRefData
;
261 refData_new
->m_width
= refData
->m_width
;
262 refData_new
->m_height
= refData
->m_height
;
263 refData_new
->m_maskRed
= refData
->m_maskRed
;
264 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
265 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
266 refData_new
->m_hasMask
= refData
->m_hasMask
;
267 refData_new
->m_ok
= true;
268 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
269 if (refData
->m_alpha
!= NULL
)
271 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
272 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
275 refData_new
->m_data
= (unsigned char*)malloc(size
);
276 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
278 refData_new
->m_palette
= refData
->m_palette
;
280 refData_new
->m_optionNames
= refData
->m_optionNames
;
281 refData_new
->m_optionValues
= refData
->m_optionValues
;
285 wxImage
wxImage::Copy() const
289 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
291 image
.m_refData
= CloneRefData(m_refData
);
296 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
298 if( xFactor
== 1 && yFactor
== 1 )
303 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
305 // can't scale to/from 0 size
306 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
307 wxT("invalid new image size") );
309 long old_height
= M_IMGDATA
->m_height
,
310 old_width
= M_IMGDATA
->m_width
;
312 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
313 wxT("invalid old image size") );
315 long width
= old_width
/ xFactor
;
316 long height
= old_height
/ yFactor
;
318 image
.Create( width
, height
, false );
320 char unsigned *data
= image
.GetData();
322 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
324 bool hasMask
= false ;
325 unsigned char maskRed
= 0;
326 unsigned char maskGreen
= 0;
327 unsigned char maskBlue
=0 ;
329 unsigned char *source_data
= M_IMGDATA
->m_data
;
330 unsigned char *target_data
= data
;
331 unsigned char *source_alpha
= 0 ;
332 unsigned char *target_alpha
= 0 ;
333 if (M_IMGDATA
->m_hasMask
)
336 maskRed
= M_IMGDATA
->m_maskRed
;
337 maskGreen
= M_IMGDATA
->m_maskGreen
;
338 maskBlue
=M_IMGDATA
->m_maskBlue
;
340 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
341 M_IMGDATA
->m_maskGreen
,
342 M_IMGDATA
->m_maskBlue
);
346 source_alpha
= M_IMGDATA
->m_alpha
;
350 target_alpha
= image
.GetAlpha() ;
354 for (long y
= 0; y
< height
; y
++)
356 for (long x
= 0; x
< width
; x
++)
358 unsigned long avgRed
= 0 ;
359 unsigned long avgGreen
= 0;
360 unsigned long avgBlue
= 0;
361 unsigned long avgAlpha
= 0 ;
362 unsigned long counter
= 0 ;
364 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
366 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
367 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
369 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
370 unsigned char red
= pixel
[0] ;
371 unsigned char green
= pixel
[1] ;
372 unsigned char blue
= pixel
[2] ;
373 unsigned char alpha
= 255 ;
375 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
376 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
391 *(target_data
++) = M_IMGDATA
->m_maskRed
;
392 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
393 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
398 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
399 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
400 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
401 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
406 // In case this is a cursor, make sure the hotspot is scaled accordingly:
407 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
408 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
409 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
410 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
411 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
412 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
417 wxImage
wxImage::Scale( int width
, int height
, int quality
) const
421 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
423 // can't scale to/from 0 size
424 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
425 wxT("invalid new image size") );
427 long old_height
= M_IMGDATA
->m_height
,
428 old_width
= M_IMGDATA
->m_width
;
429 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
430 wxT("invalid old image size") );
432 // If the image's new width and height are the same as the original, no need to waste time or CPU cycles
433 if(old_width
== width
&& old_height
== height
)
436 // Scale the image (...or more appropriately, resample the image) using 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 a bicubic b-spline scaling method
448 image
= ResampleBicubic(width
, height
);
451 else // Default scaling method == simple pixel replication
453 if ( old_width
% width
== 0 && old_width
>= width
&&
454 old_height
% height
== 0 && old_height
>= height
)
456 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
458 image
.Create( width
, height
, false );
460 unsigned char *data
= image
.GetData();
462 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
464 unsigned char *source_data
= M_IMGDATA
->m_data
;
465 unsigned char *target_data
= data
;
466 unsigned char *source_alpha
= 0 ;
467 unsigned char *target_alpha
= 0 ;
469 if (M_IMGDATA
->m_hasMask
)
471 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
472 M_IMGDATA
->m_maskGreen
,
473 M_IMGDATA
->m_maskBlue
);
477 source_alpha
= M_IMGDATA
->m_alpha
;
481 target_alpha
= image
.GetAlpha() ;
485 long x_delta
= (old_width
<<16) / width
;
486 long y_delta
= (old_height
<<16) / height
;
488 unsigned char* dest_pixel
= target_data
;
491 for ( long j
= 0; j
< height
; j
++ )
493 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
494 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
497 for ( long i
= 0; i
< width
; i
++ )
499 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
500 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
501 dest_pixel
[0] = src_pixel
[0];
502 dest_pixel
[1] = src_pixel
[1];
503 dest_pixel
[2] = src_pixel
[2];
506 *(target_alpha
++) = *src_alpha_pixel
;
514 // In case this is a cursor, make sure the hotspot is scaled accordingly:
515 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
516 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
517 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
518 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
519 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
520 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
525 wxImage
wxImage::ResampleBox(int width
, int height
) const
527 // This function implements a simple pre-blur/box averaging method for downsampling that gives reasonably smooth results
528 // To scale the image down we will need to gather a grid of pixels of the size of the scale factor in each direction
529 // and then do an averaging of the pixels.
531 wxImage
ret_image(width
, height
, false);
533 double scale_factor_x
= double(M_IMGDATA
->m_width
) / width
;
534 double scale_factor_y
= double(M_IMGDATA
->m_height
) / height
;
536 // If we want good-looking results we need to pre-blur the image a bit first
537 wxImage
src_image(*this);
538 src_image
= src_image
.BlurHorizontal(scale_factor_x
/ 2);
539 src_image
= src_image
.BlurVertical(scale_factor_y
/ 2);
541 unsigned char* src_data
= src_image
.GetData();
542 unsigned char* src_alpha
= src_image
.GetAlpha();
543 unsigned char* dst_data
= ret_image
.GetData();
544 unsigned char* dst_alpha
= NULL
;
548 ret_image
.SetAlpha();
549 dst_alpha
= ret_image
.GetAlpha();
553 int averaged_pixels
, src_pixel_index
, src_x
, src_y
;
554 double sum_r
, sum_g
, sum_b
, sum_a
;
556 for(y
= 0; y
< height
; y
++) // Destination image - Y direction
558 // Source pixel in the Y direction
559 src_y
= y
* scale_factor_y
;
561 for(x
= 0; x
< width
; x
++) // Destination image - X direction
563 // Source pixel in the X direction
564 src_x
= x
* scale_factor_x
;
566 // Box of pixels to average
568 sum_r
= sum_g
= sum_b
= sum_a
= 0.0;
570 for(j
= src_y
- scale_factor_y
/ 2 + 1; j
<= int(src_y
+ scale_factor_y
/ 2); j
++) // Y direction
572 // We don't care to average pixels that don't exist (edges)
573 if(j
< 0 || j
> M_IMGDATA
->m_height
)
576 for(i
= src_x
- scale_factor_x
/ 2 + 1; i
<= int(src_x
+ scale_factor_x
/ 2); i
++) // X direction
578 // Don't average edge pixels
579 if(i
< 0 || i
> M_IMGDATA
->m_width
)
582 // Calculate the actual index in our source pixels
583 src_pixel_index
= src_y
* M_IMGDATA
->m_width
+ src_x
;
585 sum_r
+= src_data
[src_pixel_index
* 3 + 0];
586 sum_g
+= src_data
[src_pixel_index
* 3 + 1];
587 sum_b
+= src_data
[src_pixel_index
* 3 + 2];
589 sum_a
+= src_alpha
[src_pixel_index
];
595 // Calculate the average from the sum and number of averaged pixels
596 dst_data
[0] = int(sum_r
/ averaged_pixels
);
597 dst_data
[1] = int(sum_g
/ averaged_pixels
);
598 dst_data
[2] = int(sum_b
/ averaged_pixels
);
601 *dst_alpha
++ = sum_a
/ averaged_pixels
;
608 // The following two local functions are for the B-spline weighting of the bicubic sampling algorithm
609 static inline double spline_cube(double value
)
611 return value
<= 0.0 ? 0.0 : value
* value
* value
;
614 static inline double spline_weight(double value
)
616 return (spline_cube(value
+ 2) - 4 * spline_cube(value
+ 1) + 6 * spline_cube(value
) - 4 * spline_cube(value
- 1)) / 6;
619 // This is the bicubic resampling algorithm
620 wxImage
wxImage::ResampleBicubic(int width
, int height
) const
622 // This function implements a Bicubic B-Spline algorithm for resampling. This method is certainly a little slower than wxImage's default
623 // pixel replication method, however for most reasonably sized images not being upsampled too much on a fairly average CPU this
624 // difference is hardly noticeable and the results are far more pleasing to look at.
626 // This particular bicubic algorithm does pixel weighting according to a B-Spline that basically implements a Gaussian bell-like
627 // weighting kernel. Because of this method the results may appear a bit blurry when upsampling by large factors. This is basically
628 // because a slight gaussian blur is being performed to get the smooth look of the upsampled image.
630 // Edge pixels: 3-4 possible solutions
631 // - (Wrap/tile) Wrap the image, take the color value from the opposite side of the image.
632 // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n), where n is nonpositive, will have the value of (2, 1).
633 // - (Ignore) Simply ignore the edge pixels and apply the kernel only to pixels which do have all neighbours.
634 // - (Clamp) Choose the nearest pixel along the border. This takes the border pixels and extends them out to infinity.
636 // NOTE: below the y_offset and x_offset variables are being set for edge pixels using the "Mirror" method mentioned above
640 ret_image
.Create(width
, height
, false);
642 unsigned char* src_data
= M_IMGDATA
->m_data
;
643 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
644 unsigned char* dst_data
= ret_image
.GetData();
645 unsigned char* dst_alpha
= NULL
;
649 ret_image
.SetAlpha();
650 dst_alpha
= ret_image
.GetAlpha();
654 double srcpixx
, srcpixy
, dx
, dy
;
656 double sum_r
= 0, sum_g
= 0, sum_b
= 0, sum_a
= 0; // Sums for each color channel
657 int x_offset
= 0, y_offset
= 0;
659 long src_pixel_index
;
661 for(dsty
= 0; dsty
< height
; dsty
++)
663 // We need to calculate the source pixel to interpolate from - Y-axis
664 srcpixy
= double(dsty
) * M_IMGDATA
->m_height
/ height
;
665 dy
= srcpixy
- (int)srcpixy
;
667 for(dstx
= 0; dstx
< width
; dstx
++)
669 // X-axis of pixel to interpolate from
670 srcpixx
= double(dstx
) * M_IMGDATA
->m_width
/ width
;
671 dx
= srcpixx
- (int)srcpixx
;
673 // Clear all the RGBA sum values
674 sum_r
= sum_g
= sum_b
= sum_a
= 0;
676 // Here we actually determine the RGBA values for the destination pixel
677 for(k
= -1; k
<= 2; k
++)
680 y_offset
= srcpixy
+ double(k
) < 0.0 ? 0 : (srcpixy
+ double(k
) >= M_IMGDATA
->m_height
? M_IMGDATA
->m_height
- 1 : srcpixy
+ k
);
682 // Loop across the X axis
683 for(i
= -1; i
<= 2; i
++)
686 x_offset
= srcpixx
+ double(i
) < 0.0 ? 0 : (srcpixx
+ double(i
) >= M_IMGDATA
->m_width
? M_IMGDATA
->m_width
- 1 : srcpixx
+ i
);
688 // Calculate the exact position where the source data should be pulled from based on the x_offset and y_offset
689 src_pixel_index
= (y_offset
* M_IMGDATA
->m_width
) + x_offset
;
691 // Calculate the weight for the specified pixel according to the bicubic b-spline kernel we're using for interpolation
692 pixel_weight
= spline_weight(double(i
) - dx
) * spline_weight(double(k
) - dy
);
694 // Create a sum of all velues for each color channel adjusted for the pixel's calculated weight
695 sum_r
+= double(src_data
[src_pixel_index
* 3 + 0]) * pixel_weight
;
696 sum_g
+= double(src_data
[src_pixel_index
* 3 + 1]) * pixel_weight
;
697 sum_b
+= double(src_data
[src_pixel_index
* 3 + 2]) * pixel_weight
;
699 sum_a
+= double(src_alpha
[src_pixel_index
]) * pixel_weight
;
703 // Put the data into the destination image. The summed values are of double data type and are rounded here for accuracy
704 dst_data
[0] = int(sum_r
+ 0.5);
705 dst_data
[1] = int(sum_g
+ 0.5);
706 dst_data
[2] = int(sum_b
+ 0.5);
710 *dst_alpha
++ = sum_a
;
717 // Blur in the horizontal direction
718 wxImage
wxImage::BlurHorizontal(int blurRadius
)
721 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
723 unsigned char* src_data
= M_IMGDATA
->m_data
;
724 unsigned char* dst_data
= ret_image
.GetData();
725 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
726 unsigned char* dst_alpha
= NULL
;
728 // Check for a mask or alpha
729 if(M_IMGDATA
->m_hasMask
)
730 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
734 ret_image
.SetAlpha();
735 dst_alpha
= ret_image
.GetAlpha();
738 // Variables used in the blurring algorithm
741 long sum_r
, sum_g
, sum_b
, sum_a
;
744 // Horizontal blurring algorithm - average all pixels in the specified blur radius in the X or horizontal direction
745 for(y
= 0; y
< M_IMGDATA
->m_height
; y
++)
747 sum_r
= sum_g
= sum_b
= sum_a
= 0;
749 // Calculate the average of all pixels in the blur radius for the first pixel of the row
750 for(kernel_x
= -blurRadius
; kernel_x
<= blurRadius
; kernel_x
++)
752 // To deal with the pixels at the start of a row so it's not grabbing GOK values from memory at negative indices of the image's data or grabbing from the previous row
754 pixel_idx
= y
* M_IMGDATA
->m_width
;
756 pixel_idx
= kernel_x
+ y
* M_IMGDATA
->m_width
;
758 sum_r
+= src_data
[pixel_idx
* 3 + 0];
759 sum_g
+= src_data
[pixel_idx
* 3 + 1];
760 sum_b
+= src_data
[pixel_idx
* 3 + 2];
761 sum_a
+= src_alpha
? src_alpha
[pixel_idx
] : 0;
763 dst_data
[y
* M_IMGDATA
->m_width
* 3 + 0] = sum_r
/ (blurRadius
* 2 + 1);
764 dst_data
[y
* M_IMGDATA
->m_width
* 3 + 1] = sum_g
/ (blurRadius
* 2 + 1);
765 dst_data
[y
* M_IMGDATA
->m_width
* 3 + 2] = sum_b
/ (blurRadius
* 2 + 1);
767 dst_alpha
[y
* M_IMGDATA
->m_width
] = sum_a
/ (blurRadius
* 2 + 1);
769 // Now average the values of the rest of the pixels by just moving the blur radius box along the row
770 for(x
= 1; x
< M_IMGDATA
->m_width
; x
++)
772 // Take care of edge pixels on the left edge by essentially duplicating the edge pixel
773 if(x
- blurRadius
- 1 < 0)
774 pixel_idx
= y
* M_IMGDATA
->m_width
;
776 pixel_idx
= (x
- blurRadius
- 1) + y
* M_IMGDATA
->m_width
;
778 // Subtract the value of the pixel at the left side of the blur radius box
779 sum_r
-= src_data
[pixel_idx
* 3 + 0];
780 sum_g
-= src_data
[pixel_idx
* 3 + 1];
781 sum_b
-= src_data
[pixel_idx
* 3 + 2];
782 sum_a
-= src_alpha
? src_alpha
[pixel_idx
] : 0;
784 // Take care of edge pixels on the right edge
785 if(x
+ blurRadius
> M_IMGDATA
->m_width
- 1)
786 pixel_idx
= M_IMGDATA
->m_width
- 1 + y
* M_IMGDATA
->m_width
;
788 pixel_idx
= x
+ blurRadius
+ y
* M_IMGDATA
->m_width
;
790 // Add the value of the pixel being added to the end of our box
791 sum_r
+= src_data
[pixel_idx
* 3 + 0];
792 sum_g
+= src_data
[pixel_idx
* 3 + 1];
793 sum_b
+= src_data
[pixel_idx
* 3 + 2];
794 sum_a
+= src_alpha
? src_alpha
[pixel_idx
] : 0;
796 // Save off the averaged data
797 dst_data
[x
* 3 + y
* M_IMGDATA
->m_width
* 3 + 0] = sum_r
/ (blurRadius
* 2 + 1);
798 dst_data
[x
* 3 + y
* M_IMGDATA
->m_width
* 3 + 1] = sum_g
/ (blurRadius
* 2 + 1);
799 dst_data
[x
* 3 + y
* M_IMGDATA
->m_width
* 3 + 2] = sum_b
/ (blurRadius
* 2 + 1);
801 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = sum_a
/ (blurRadius
* 2 + 1);
808 // Blur in the vertical direction
809 wxImage
wxImage::BlurVertical(int blurRadius
)
812 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
814 unsigned char* src_data
= M_IMGDATA
->m_data
;
815 unsigned char* dst_data
= ret_image
.GetData();
816 unsigned char* src_alpha
= M_IMGDATA
->m_alpha
;
817 unsigned char* dst_alpha
= NULL
;
819 // Check for a mask or alpha
820 if(M_IMGDATA
->m_hasMask
)
821 ret_image
.SetMaskColour(M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
825 ret_image
.SetAlpha();
826 dst_alpha
= ret_image
.GetAlpha();
829 // Variables used in the blurring algorithm
832 long sum_r
, sum_g
, sum_b
, sum_a
;
835 // Vertical blurring algorithm - same as horizontal but switched the opposite direction
836 for(x
= 0; x
< M_IMGDATA
->m_width
; x
++)
838 sum_r
= sum_g
= sum_b
= sum_a
= 0;
840 // Calculate the average of all pixels in our blur radius box for the first pixel of the column
841 for(kernel_y
= -blurRadius
; kernel_y
<= blurRadius
; kernel_y
++)
843 // To deal with the pixels at the start of a column so it's not grabbing GOK values from memory at negative indices of the image's data or grabbing from the previous column
847 pixel_idx
= x
+ kernel_y
* M_IMGDATA
->m_width
;
849 sum_r
+= src_data
[pixel_idx
* 3 + 0];
850 sum_g
+= src_data
[pixel_idx
* 3 + 1];
851 sum_b
+= src_data
[pixel_idx
* 3 + 2];
852 sum_a
+= src_alpha
? src_alpha
[pixel_idx
] : 0;
854 dst_data
[x
* 3 + 0] = sum_r
/ (blurRadius
* 2 + 1);
855 dst_data
[x
* 3 + 1] = sum_g
/ (blurRadius
* 2 + 1);
856 dst_data
[x
* 3 + 2] = sum_b
/ (blurRadius
* 2 + 1);
858 dst_alpha
[x
] = sum_a
/ (blurRadius
* 2 + 1);
860 // Now average the values of the rest of the pixels by just moving the box along the column from top to bottom
861 for(y
= 1; y
< M_IMGDATA
->m_height
; y
++)
863 // Take care of pixels that would be beyond the top edge by duplicating the top edge pixel for the column
864 if(y
- blurRadius
- 1 < 0)
867 pixel_idx
= x
+ (y
- blurRadius
- 1) * M_IMGDATA
->m_width
;
869 // Subtract the value of the pixel at the top of our blur radius box
870 sum_r
-= src_data
[pixel_idx
* 3 + 0];
871 sum_g
-= src_data
[pixel_idx
* 3 + 1];
872 sum_b
-= src_data
[pixel_idx
* 3 + 2];
873 sum_a
-= src_alpha
? src_alpha
[pixel_idx
] : 0;
875 // Take care of the pixels that would be beyond the bottom edge of the image similar to the top edge
876 if(y
+ blurRadius
> M_IMGDATA
->m_height
- 1)
877 pixel_idx
= x
+ (M_IMGDATA
->m_height
- 1) * M_IMGDATA
->m_width
;
879 pixel_idx
= x
+ (blurRadius
+ y
) * M_IMGDATA
->m_width
;
881 // Add the value of the pixel being added to the end of our box
882 sum_r
+= src_data
[pixel_idx
* 3 + 0];
883 sum_g
+= src_data
[pixel_idx
* 3 + 1];
884 sum_b
+= src_data
[pixel_idx
* 3 + 2];
885 sum_a
+= src_alpha
? src_alpha
[pixel_idx
] : 0;
887 // Save off the averaged data
888 dst_data
[(x
+ y
* M_IMGDATA
->m_width
) * 3 + 0] = sum_r
/ (blurRadius
* 2 + 1);
889 dst_data
[(x
+ y
* M_IMGDATA
->m_width
) * 3 + 1] = sum_g
/ (blurRadius
* 2 + 1);
890 dst_data
[(x
+ y
* M_IMGDATA
->m_width
) * 3 + 2] = sum_b
/ (blurRadius
* 2 + 1);
892 dst_alpha
[x
+ y
* M_IMGDATA
->m_width
] = sum_a
/ (blurRadius
* 2 + 1);
899 // The new blur function
900 wxImage
wxImage::Blur(int blurRadius
)
903 ret_image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
905 // Blur the image in each direction
906 ret_image
= BlurHorizontal(blurRadius
);
907 ret_image
= ret_image
.BlurVertical(blurRadius
);
912 wxImage
wxImage::Rotate90( bool clockwise
) const
916 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
918 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
920 unsigned char *data
= image
.GetData();
922 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
924 unsigned char *source_data
= M_IMGDATA
->m_data
;
925 unsigned char *target_data
;
926 unsigned char *alpha_data
= 0 ;
927 unsigned char *source_alpha
= 0 ;
928 unsigned char *target_alpha
= 0 ;
930 if (M_IMGDATA
->m_hasMask
)
932 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
936 source_alpha
= M_IMGDATA
->m_alpha
;
940 alpha_data
= image
.GetAlpha() ;
944 long height
= M_IMGDATA
->m_height
;
945 long width
= M_IMGDATA
->m_width
;
947 for (long j
= 0; j
< height
; j
++)
949 for (long i
= 0; i
< width
; i
++)
953 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
955 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
959 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
961 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
963 memcpy( target_data
, source_data
, 3 );
968 memcpy( target_alpha
, source_alpha
, 1 );
977 wxImage
wxImage::Mirror( bool horizontally
) const
981 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
983 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
985 unsigned char *data
= image
.GetData();
986 unsigned char *alpha
= NULL
;
988 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
990 if (M_IMGDATA
->m_alpha
!= NULL
) {
992 alpha
= image
.GetAlpha();
993 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
996 if (M_IMGDATA
->m_hasMask
)
997 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
999 long height
= M_IMGDATA
->m_height
;
1000 long width
= M_IMGDATA
->m_width
;
1002 unsigned char *source_data
= M_IMGDATA
->m_data
;
1003 unsigned char *target_data
;
1007 for (long j
= 0; j
< height
; j
++)
1010 target_data
= data
-3;
1011 for (long i
= 0; i
< width
; i
++)
1013 memcpy( target_data
, source_data
, 3 );
1021 // src_alpha starts at the first pixel and increases by 1 after each step
1022 // (a step here is the copy of the alpha value of one pixel)
1023 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1024 // dest_alpha starts just beyond the first line, decreases before each step,
1025 // and after each line is finished, increases by 2 widths (skipping the line
1026 // just copied and the line that will be copied next)
1027 unsigned char *dest_alpha
= alpha
+ width
;
1029 for (long jj
= 0; jj
< height
; ++jj
)
1031 for (long i
= 0; i
< width
; ++i
) {
1032 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
1034 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
1040 for (long i
= 0; i
< height
; i
++)
1042 target_data
= data
+ 3*width
*(height
-1-i
);
1043 memcpy( target_data
, source_data
, (size_t)3*width
);
1044 source_data
+= 3*width
;
1049 // src_alpha starts at the first pixel and increases by 1 width after each step
1050 // (a step here is the copy of the alpha channel of an entire line)
1051 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1052 // dest_alpha starts just beyond the last line (beyond the whole image)
1053 // and decreases by 1 width before each step
1054 unsigned char *dest_alpha
= alpha
+ width
* height
;
1056 for (long jj
= 0; jj
< height
; ++jj
)
1058 dest_alpha
-= width
;
1059 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
1068 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
1072 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1074 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
1075 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
1076 image
, wxT("invalid subimage size") );
1078 const int subwidth
= rect
.GetWidth();
1079 const int subheight
= rect
.GetHeight();
1081 image
.Create( subwidth
, subheight
, false );
1083 const unsigned char *src_data
= GetData();
1084 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
1085 unsigned char *subdata
= image
.GetData();
1086 unsigned char *subalpha
= NULL
;
1088 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
1090 if (src_alpha
!= NULL
) {
1092 subalpha
= image
.GetAlpha();
1093 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
1096 if (M_IMGDATA
->m_hasMask
)
1097 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
1099 const int width
= GetWidth();
1100 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
1102 src_data
+= 3 * pixsoff
;
1103 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
1105 for (long j
= 0; j
< subheight
; ++j
)
1107 memcpy( subdata
, src_data
, 3 * subwidth
);
1108 subdata
+= 3 * subwidth
;
1109 src_data
+= 3 * width
;
1110 if (subalpha
!= NULL
) {
1111 memcpy( subalpha
, src_alpha
, subwidth
);
1112 subalpha
+= subwidth
;
1120 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
1121 int r_
, int g_
, int b_
) const
1125 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1126 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
1128 int width
= GetWidth(), height
= GetHeight();
1129 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
1131 unsigned char r
= (unsigned char)r_
;
1132 unsigned char g
= (unsigned char)g_
;
1133 unsigned char b
= (unsigned char)b_
;
1134 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
1136 GetOrFindMaskColour( &r
, &g
, &b
);
1137 image
.SetMaskColour(r
, g
, b
);
1140 image
.SetRGB(wxRect(), r
, g
, b
);
1142 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
1143 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
1145 finalRect
.width
-= pos
.x
;
1147 finalRect
.height
-= pos
.y
;
1149 subRect
.Intersect(finalRect
);
1151 if (!subRect
.IsEmpty())
1153 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
1154 image
.Paste(*this, pos
.x
, pos
.y
);
1156 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
1162 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
1164 wxCHECK_RET( Ok(), wxT("invalid image") );
1165 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1171 int width
= image
.GetWidth();
1172 int height
= image
.GetHeight();
1185 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
1186 width
= M_IMGDATA
->m_width
- (x
+xx
);
1187 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
1188 height
= M_IMGDATA
->m_height
- (y
+yy
);
1190 if (width
< 1) return;
1191 if (height
< 1) return;
1193 if ((!HasMask() && !image
.HasMask()) ||
1194 (HasMask() && !image
.HasMask()) ||
1195 ((HasMask() && image
.HasMask() &&
1196 (GetMaskRed()==image
.GetMaskRed()) &&
1197 (GetMaskGreen()==image
.GetMaskGreen()) &&
1198 (GetMaskBlue()==image
.GetMaskBlue()))))
1201 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1202 int source_step
= image
.GetWidth()*3;
1204 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1205 int target_step
= M_IMGDATA
->m_width
*3;
1206 for (int j
= 0; j
< height
; j
++)
1208 memcpy( target_data
, source_data
, width
);
1209 source_data
+= source_step
;
1210 target_data
+= target_step
;
1215 if (!HasMask() && image
.HasMask())
1217 unsigned char r
= image
.GetMaskRed();
1218 unsigned char g
= image
.GetMaskGreen();
1219 unsigned char b
= image
.GetMaskBlue();
1222 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
1223 int source_step
= image
.GetWidth()*3;
1225 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
1226 int target_step
= M_IMGDATA
->m_width
*3;
1228 for (int j
= 0; j
< height
; j
++)
1230 for (int i
= 0; i
< width
; i
+=3)
1232 if ((source_data
[i
] != r
) &&
1233 (source_data
[i
+1] != g
) &&
1234 (source_data
[i
+2] != b
))
1236 memcpy( target_data
+i
, source_data
+i
, 3 );
1239 source_data
+= source_step
;
1240 target_data
+= target_step
;
1245 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
1246 unsigned char r2
, unsigned char g2
, unsigned char b2
)
1248 wxCHECK_RET( Ok(), wxT("invalid image") );
1252 unsigned char *data
= GetData();
1254 const int w
= GetWidth();
1255 const int h
= GetHeight();
1257 for (int j
= 0; j
< h
; j
++)
1258 for (int i
= 0; i
< w
; i
++)
1260 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
1270 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
1274 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1276 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
1278 unsigned char *dest
= image
.GetData();
1280 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
1282 unsigned char *src
= M_IMGDATA
->m_data
;
1283 bool hasMask
= M_IMGDATA
->m_hasMask
;
1284 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
1285 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
1286 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
1289 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
1291 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1292 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
1294 // don't modify the mask
1295 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
1297 memcpy(dest
, src
, 3);
1301 // calculate the luma
1302 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
1303 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
1307 // copy the alpha channel, if any
1310 const size_t alphaSize
= GetWidth() * GetHeight();
1311 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
1312 memcpy(alpha
, GetAlpha(), alphaSize
);
1314 image
.SetAlpha(alpha
);
1320 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
1324 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
1326 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
1328 unsigned char *data
= image
.GetData();
1330 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
1332 if (M_IMGDATA
->m_hasMask
)
1334 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
1335 M_IMGDATA
->m_maskBlue
== b
)
1336 image
.SetMaskColour( 255, 255, 255 );
1338 image
.SetMaskColour( 0, 0, 0 );
1341 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
1343 unsigned char *srcd
= M_IMGDATA
->m_data
;
1344 unsigned char *tard
= image
.GetData();
1346 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
1348 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
1349 tard
[0] = tard
[1] = tard
[2] = 255;
1351 tard
[0] = tard
[1] = tard
[2] = 0;
1357 int wxImage::GetWidth() const
1359 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1361 return M_IMGDATA
->m_width
;
1364 int wxImage::GetHeight() const
1366 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1368 return M_IMGDATA
->m_height
;
1371 long wxImage::XYToIndex(int x
, int y
) const
1375 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
1377 return y
*M_IMGDATA
->m_width
+ x
;
1383 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
1385 long pos
= XYToIndex(x
, y
);
1386 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1392 M_IMGDATA
->m_data
[ pos
] = r
;
1393 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
1394 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
1397 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
1399 wxCHECK_RET( Ok(), wxT("invalid image") );
1404 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
1405 if ( rect
== wxRect() )
1411 wxCHECK_RET( imageRect
.Contains(rect
.GetTopLeft()) &&
1412 imageRect
.Contains(rect
.GetBottomRight()),
1413 wxT("invalid bounding rectangle") );
1416 int x1
= rect
.GetLeft(),
1418 x2
= rect
.GetRight() + 1,
1419 y2
= rect
.GetBottom() + 1;
1421 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1422 int x
, y
, width
= GetWidth();
1423 for (y
= y1
; y
< y2
; y
++)
1425 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1426 for (x
= x1
; x
< x2
; x
++)
1435 unsigned char wxImage::GetRed( int x
, int y
) const
1437 long pos
= XYToIndex(x
, y
);
1438 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1442 return M_IMGDATA
->m_data
[pos
];
1445 unsigned char wxImage::GetGreen( int x
, int y
) const
1447 long pos
= XYToIndex(x
, y
);
1448 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1452 return M_IMGDATA
->m_data
[pos
+1];
1455 unsigned char wxImage::GetBlue( int x
, int y
) const
1457 long pos
= XYToIndex(x
, y
);
1458 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1462 return M_IMGDATA
->m_data
[pos
+2];
1465 bool wxImage::Ok() const
1467 // image of 0 width or height can't be considered ok - at least because it
1468 // causes crashes in ConvertToBitmap() if we don't catch it in time
1469 wxImageRefData
*data
= M_IMGDATA
;
1470 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1473 unsigned char *wxImage::GetData() const
1475 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1477 return M_IMGDATA
->m_data
;
1480 void wxImage::SetData( unsigned char *data
, bool static_data
)
1482 wxCHECK_RET( Ok(), wxT("invalid image") );
1484 wxImageRefData
*newRefData
= new wxImageRefData();
1486 newRefData
->m_width
= M_IMGDATA
->m_width
;
1487 newRefData
->m_height
= M_IMGDATA
->m_height
;
1488 newRefData
->m_data
= data
;
1489 newRefData
->m_ok
= true;
1490 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1491 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1492 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1493 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1494 newRefData
->m_static
= static_data
;
1498 m_refData
= newRefData
;
1501 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1503 wxImageRefData
*newRefData
= new wxImageRefData();
1507 newRefData
->m_width
= new_width
;
1508 newRefData
->m_height
= new_height
;
1509 newRefData
->m_data
= data
;
1510 newRefData
->m_ok
= true;
1511 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1512 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1513 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1514 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1518 newRefData
->m_width
= new_width
;
1519 newRefData
->m_height
= new_height
;
1520 newRefData
->m_data
= data
;
1521 newRefData
->m_ok
= true;
1523 newRefData
->m_static
= static_data
;
1527 m_refData
= newRefData
;
1530 // ----------------------------------------------------------------------------
1531 // alpha channel support
1532 // ----------------------------------------------------------------------------
1534 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1536 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1538 long pos
= XYToIndex(x
, y
);
1539 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1543 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1546 unsigned char wxImage::GetAlpha(int x
, int y
) const
1548 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1550 long pos
= XYToIndex(x
, y
);
1551 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1553 return M_IMGDATA
->m_alpha
[pos
];
1557 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1561 const int w
= M_IMGDATA
->m_width
;
1562 const int h
= M_IMGDATA
->m_height
;
1564 unsigned char *alpha
= GetAlpha();
1565 unsigned char *data
= GetData();
1567 for ( int y
= 0; y
< h
; y
++ )
1569 for ( int x
= 0; x
< w
; x
++ )
1581 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1583 wxCHECK_RET( Ok(), wxT("invalid image") );
1589 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1592 free(M_IMGDATA
->m_alpha
);
1593 M_IMGDATA
->m_alpha
= alpha
;
1594 M_IMGDATA
->m_staticAlpha
= static_data
;
1597 unsigned char *wxImage::GetAlpha() const
1599 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1601 return M_IMGDATA
->m_alpha
;
1604 void wxImage::InitAlpha()
1606 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1608 // initialize memory for alpha channel
1611 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1612 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1616 // use the mask to initialize the alpha channel.
1617 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1619 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1620 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1621 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1622 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1626 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1627 ? wxIMAGE_ALPHA_TRANSPARENT
1628 : wxIMAGE_ALPHA_OPAQUE
;
1631 M_IMGDATA
->m_hasMask
= false;
1635 // make the image fully opaque
1636 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1640 // ----------------------------------------------------------------------------
1642 // ----------------------------------------------------------------------------
1644 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1646 wxCHECK_RET( Ok(), wxT("invalid image") );
1650 M_IMGDATA
->m_maskRed
= r
;
1651 M_IMGDATA
->m_maskGreen
= g
;
1652 M_IMGDATA
->m_maskBlue
= b
;
1653 M_IMGDATA
->m_hasMask
= true;
1656 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1658 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1660 if (M_IMGDATA
->m_hasMask
)
1662 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1663 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1664 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1669 FindFirstUnusedColour(r
, g
, b
);
1674 unsigned char wxImage::GetMaskRed() const
1676 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1678 return M_IMGDATA
->m_maskRed
;
1681 unsigned char wxImage::GetMaskGreen() const
1683 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1685 return M_IMGDATA
->m_maskGreen
;
1688 unsigned char wxImage::GetMaskBlue() const
1690 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1692 return M_IMGDATA
->m_maskBlue
;
1695 void wxImage::SetMask( bool mask
)
1697 wxCHECK_RET( Ok(), wxT("invalid image") );
1701 M_IMGDATA
->m_hasMask
= mask
;
1704 bool wxImage::HasMask() const
1706 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1708 return M_IMGDATA
->m_hasMask
;
1711 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1713 long pos
= XYToIndex(x
, y
);
1714 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1717 if ( M_IMGDATA
->m_hasMask
)
1719 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1720 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1721 p
[1] == M_IMGDATA
->m_maskGreen
&&
1722 p
[2] == M_IMGDATA
->m_maskBlue
)
1729 if ( M_IMGDATA
->m_alpha
)
1731 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1733 // transparent enough
1742 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1743 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1745 // check that the images are the same size
1746 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1748 wxLogError( _("Image and mask have different sizes.") );
1752 // find unused colour
1753 unsigned char r
,g
,b
;
1754 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1756 wxLogError( _("No unused colour in image being masked.") );
1762 unsigned char *imgdata
= GetData();
1763 unsigned char *maskdata
= mask
.GetData();
1765 const int w
= GetWidth();
1766 const int h
= GetHeight();
1768 for (int j
= 0; j
< h
; j
++)
1770 for (int i
= 0; i
< w
; i
++)
1772 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1783 SetMaskColour(r
, g
, b
);
1789 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1794 unsigned char mr
, mg
, mb
;
1795 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1797 wxLogError( _("No unused colour in image being masked.") );
1804 SetMaskColour(mr
, mg
, mb
);
1806 unsigned char *imgdata
= GetData();
1807 unsigned char *alphadata
= GetAlpha();
1810 int h
= GetHeight();
1812 for (int y
= 0; y
< h
; y
++)
1814 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1816 if (*alphadata
< threshold
)
1825 free(M_IMGDATA
->m_alpha
);
1826 M_IMGDATA
->m_alpha
= NULL
;
1831 // ----------------------------------------------------------------------------
1832 // Palette functions
1833 // ----------------------------------------------------------------------------
1837 bool wxImage::HasPalette() const
1842 return M_IMGDATA
->m_palette
.Ok();
1845 const wxPalette
& wxImage::GetPalette() const
1847 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1849 return M_IMGDATA
->m_palette
;
1852 void wxImage::SetPalette(const wxPalette
& palette
)
1854 wxCHECK_RET( Ok(), wxT("invalid image") );
1858 M_IMGDATA
->m_palette
= palette
;
1861 #endif // wxUSE_PALETTE
1863 // ----------------------------------------------------------------------------
1864 // Option functions (arbitrary name/value mapping)
1865 // ----------------------------------------------------------------------------
1867 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1869 wxCHECK_RET( Ok(), wxT("invalid image") );
1873 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1874 if (idx
== wxNOT_FOUND
)
1876 M_IMGDATA
->m_optionNames
.Add(name
);
1877 M_IMGDATA
->m_optionValues
.Add(value
);
1881 M_IMGDATA
->m_optionNames
[idx
] = name
;
1882 M_IMGDATA
->m_optionValues
[idx
] = value
;
1886 void wxImage::SetOption(const wxString
& name
, int value
)
1889 valStr
.Printf(wxT("%d"), value
);
1890 SetOption(name
, valStr
);
1893 wxString
wxImage::GetOption(const wxString
& name
) const
1895 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1897 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1898 if (idx
== wxNOT_FOUND
)
1899 return wxEmptyString
;
1901 return M_IMGDATA
->m_optionValues
[idx
];
1904 int wxImage::GetOptionInt(const wxString
& name
) const
1906 return wxAtoi(GetOption(name
));
1909 bool wxImage::HasOption(const wxString
& name
) const
1911 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1913 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1916 // ----------------------------------------------------------------------------
1918 // ----------------------------------------------------------------------------
1920 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1921 long WXUNUSED_UNLESS_STREAMS(type
),
1922 int WXUNUSED_UNLESS_STREAMS(index
) )
1924 #if HAS_FILE_STREAMS
1925 if (wxFileExists(filename
))
1927 wxImageFileInputStream
stream(filename
);
1928 wxBufferedInputStream
bstream( stream
);
1929 return LoadFile(bstream
, type
, index
);
1933 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1937 #else // !HAS_FILE_STREAMS
1939 #endif // HAS_FILE_STREAMS
1942 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1943 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
1944 int WXUNUSED_UNLESS_STREAMS(index
) )
1946 #if HAS_FILE_STREAMS
1947 if (wxFileExists(filename
))
1949 wxImageFileInputStream
stream(filename
);
1950 wxBufferedInputStream
bstream( stream
);
1951 return LoadFile(bstream
, mimetype
, index
);
1955 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1959 #else // !HAS_FILE_STREAMS
1961 #endif // HAS_FILE_STREAMS
1966 bool wxImage::SaveFile( const wxString
& filename
) const
1968 wxString ext
= filename
.AfterLast('.').Lower();
1970 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1973 SaveFile(filename
, pHandler
->GetType());
1977 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1982 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1983 int WXUNUSED_UNLESS_STREAMS(type
) ) const
1985 #if HAS_FILE_STREAMS
1986 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1988 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1990 wxImageFileOutputStream
stream(filename
);
1992 if ( stream
.IsOk() )
1994 wxBufferedOutputStream
bstream( stream
);
1995 return SaveFile(bstream
, type
);
1997 #endif // HAS_FILE_STREAMS
2002 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
2003 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
2005 #if HAS_FILE_STREAMS
2006 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2008 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
2010 wxImageFileOutputStream
stream(filename
);
2012 if ( stream
.IsOk() )
2014 wxBufferedOutputStream
bstream( stream
);
2015 return SaveFile(bstream
, mimetype
);
2017 #endif // HAS_FILE_STREAMS
2022 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
2024 #if HAS_FILE_STREAMS
2025 wxImageFileInputStream
stream(name
);
2026 return CanRead(stream
);
2032 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
2033 long WXUNUSED_UNLESS_STREAMS(type
) )
2035 #if HAS_FILE_STREAMS
2036 wxImageFileInputStream
stream(name
);
2038 return GetImageCount(stream
, type
);
2046 bool wxImage::CanRead( wxInputStream
&stream
)
2048 const wxList
& list
= GetHandlers();
2050 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2052 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
2053 if (handler
->CanRead( stream
))
2060 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
2062 wxImageHandler
*handler
;
2064 if ( type
== wxBITMAP_TYPE_ANY
)
2066 wxList
&list
=GetHandlers();
2068 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2070 handler
=(wxImageHandler
*)node
->GetData();
2071 if ( handler
->CanRead(stream
) )
2072 return handler
->GetImageCount(stream
);
2076 wxLogWarning(_("No handler found for image type."));
2080 handler
= FindHandler(type
);
2084 wxLogWarning(_("No image handler for type %ld defined."), type
);
2088 if ( handler
->CanRead(stream
) )
2090 return handler
->GetImageCount(stream
);
2094 wxLogError(_("Image file is not of type %ld."), type
);
2099 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
2103 m_refData
= new wxImageRefData
;
2105 wxImageHandler
*handler
;
2107 if ( type
== wxBITMAP_TYPE_ANY
)
2109 wxList
&list
=GetHandlers();
2111 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
2113 handler
=(wxImageHandler
*)node
->GetData();
2114 if ( handler
->CanRead(stream
) )
2115 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2119 wxLogWarning( _("No handler found for image type.") );
2123 handler
= FindHandler(type
);
2127 wxLogWarning( _("No image handler for type %ld defined."), type
);
2132 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2134 wxLogError(_("Image file is not of type %ld."), type
);
2138 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
2141 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
2145 m_refData
= new wxImageRefData
;
2147 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2151 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2156 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
2158 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
2162 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
2165 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
2167 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2169 wxImageHandler
*handler
= FindHandler(type
);
2172 wxLogWarning( _("No image handler for type %d defined."), type
);
2177 return handler
->SaveFile( (wxImage
*)this, stream
);
2180 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
2182 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
2184 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
2187 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
2192 return handler
->SaveFile( (wxImage
*)this, stream
);
2194 #endif // wxUSE_STREAMS
2196 // ----------------------------------------------------------------------------
2197 // image I/O handlers
2198 // ----------------------------------------------------------------------------
2200 void wxImage::AddHandler( wxImageHandler
*handler
)
2202 // Check for an existing handler of the type being added.
2203 if (FindHandler( handler
->GetType() ) == 0)
2205 sm_handlers
.Append( handler
);
2209 // This is not documented behaviour, merely the simplest 'fix'
2210 // for preventing duplicate additions. If someone ever has
2211 // a good reason to add and remove duplicate handlers (and they
2212 // may) we should probably refcount the duplicates.
2213 // also an issue in InsertHandler below.
2215 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
2216 handler
->GetName().c_str() );
2221 void wxImage::InsertHandler( wxImageHandler
*handler
)
2223 // Check for an existing handler of the type being added.
2224 if (FindHandler( handler
->GetType() ) == 0)
2226 sm_handlers
.Insert( handler
);
2230 // see AddHandler for additional comments.
2231 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
2232 handler
->GetName().c_str() );
2237 bool wxImage::RemoveHandler( const wxString
& name
)
2239 wxImageHandler
*handler
= FindHandler(name
);
2242 sm_handlers
.DeleteObject(handler
);
2250 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
2252 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2255 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2256 if (handler
->GetName().Cmp(name
) == 0) return handler
;
2258 node
= node
->GetNext();
2263 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
2265 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2268 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2269 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
2270 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
2272 node
= node
->GetNext();
2277 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
2279 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2282 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2283 if (handler
->GetType() == bitmapType
) return handler
;
2284 node
= node
->GetNext();
2289 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
2291 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2294 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2295 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
2296 node
= node
->GetNext();
2301 void wxImage::InitStandardHandlers()
2304 AddHandler(new wxBMPHandler
);
2305 #endif // wxUSE_STREAMS
2308 void wxImage::CleanUpHandlers()
2310 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
2313 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
2314 wxList::compatibility_iterator next
= node
->GetNext();
2319 sm_handlers
.Clear();
2322 wxString
wxImage::GetImageExtWildcard()
2326 wxList
& Handlers
= wxImage::GetHandlers();
2327 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
2330 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
2331 fmts
+= wxT("*.") + Handler
->GetExtension();
2332 Node
= Node
->GetNext();
2333 if ( Node
) fmts
+= wxT(";");
2336 return wxT("(") + fmts
+ wxT(")|") + fmts
;
2339 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
2341 const double red
= rgb
.red
/ 255.0,
2342 green
= rgb
.green
/ 255.0,
2343 blue
= rgb
.blue
/ 255.0;
2345 // find the min and max intensity (and remember which one was it for the
2347 double minimumRGB
= red
;
2348 if ( green
< minimumRGB
)
2350 if ( blue
< minimumRGB
)
2353 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
2354 double maximumRGB
= red
;
2355 if ( green
> maximumRGB
)
2360 if ( blue
> maximumRGB
)
2366 const double value
= maximumRGB
;
2368 double hue
= 0.0, saturation
;
2369 const double deltaRGB
= maximumRGB
- minimumRGB
;
2370 if ( wxIsNullDouble(deltaRGB
) )
2372 // Gray has no color
2381 hue
= (green
- blue
) / deltaRGB
;
2385 hue
= 2.0 + (blue
- red
) / deltaRGB
;
2389 hue
= 4.0 + (red
- green
) / deltaRGB
;
2393 wxFAIL_MSG(wxT("hue not specified"));
2402 saturation
= deltaRGB
/ maximumRGB
;
2405 return HSVValue(hue
, saturation
, value
);
2408 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2410 double red
, green
, blue
;
2412 if ( wxIsNullDouble(hsv
.saturation
) )
2421 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2422 int i
= (int)floor(hue
);
2423 double f
= hue
- i
; // fractional part of h
2424 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2430 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2435 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2443 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2448 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2453 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2461 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2466 return RGBValue((unsigned char)(red
* 255.0),
2467 (unsigned char)(green
* 255.0),
2468 (unsigned char)(blue
* 255.0));
2472 * Rotates the hue of each pixel of the image. angle is a double in the range
2473 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2475 void wxImage::RotateHue(double angle
)
2479 unsigned char *srcBytePtr
;
2480 unsigned char *dstBytePtr
;
2481 unsigned long count
;
2482 wxImage::HSVValue hsv
;
2483 wxImage::RGBValue rgb
;
2485 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2486 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2487 if ( count
> 0 && !wxIsNullDouble(angle
) )
2489 srcBytePtr
= M_IMGDATA
->m_data
;
2490 dstBytePtr
= srcBytePtr
;
2493 rgb
.red
= *srcBytePtr
++;
2494 rgb
.green
= *srcBytePtr
++;
2495 rgb
.blue
= *srcBytePtr
++;
2496 hsv
= RGBtoHSV(rgb
);
2498 hsv
.hue
= hsv
.hue
+ angle
;
2500 hsv
.hue
= hsv
.hue
- 1.0;
2501 else if (hsv
.hue
< 0.0)
2502 hsv
.hue
= hsv
.hue
+ 1.0;
2504 rgb
= HSVtoRGB(hsv
);
2505 *dstBytePtr
++ = rgb
.red
;
2506 *dstBytePtr
++ = rgb
.green
;
2507 *dstBytePtr
++ = rgb
.blue
;
2508 } while (--count
!= 0);
2512 //-----------------------------------------------------------------------------
2514 //-----------------------------------------------------------------------------
2516 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2519 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2524 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2529 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2534 bool wxImageHandler::CanRead( const wxString
& name
)
2536 if (wxFileExists(name
))
2538 wxImageFileInputStream
stream(name
);
2539 return CanRead(stream
);
2542 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2547 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2549 wxFileOffset posOld
= stream
.TellI();
2550 if ( posOld
== wxInvalidOffset
)
2552 // can't test unseekable stream
2556 bool ok
= DoCanRead(stream
);
2558 // restore the old position to be able to test other formats and so on
2559 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2561 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2563 // reading would fail anyhow as we're not at the right position
2570 #endif // wxUSE_STREAMS
2572 // ----------------------------------------------------------------------------
2573 // image histogram stuff
2574 // ----------------------------------------------------------------------------
2577 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2582 unsigned char g2
) const
2584 unsigned long key
= MakeKey(r2
, g2
, b2
);
2586 while ( find(key
) != end() )
2588 // color already used
2600 wxLogError(_("No unused colour in image.") );
2606 key
= MakeKey(r2
, g2
, b2
);
2620 wxImage::FindFirstUnusedColour(unsigned char *r
,
2625 unsigned char g2
) const
2627 wxImageHistogram histogram
;
2629 ComputeHistogram(histogram
);
2631 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2637 // Counts and returns the number of different colours. Optionally stops
2638 // when it exceeds 'stopafter' different colours. This is useful, for
2639 // example, to see if the image can be saved as 8-bit (256 colour or
2640 // less, in this case it would be invoked as CountColours(256)). Default
2641 // value for stopafter is -1 (don't care).
2643 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2647 unsigned char r
, g
, b
;
2649 unsigned long size
, nentries
, key
;
2652 size
= GetWidth() * GetHeight();
2655 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2660 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2662 if (h
.Get(key
) == NULL
)
2673 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2675 unsigned char *p
= GetData();
2676 unsigned long nentries
= 0;
2680 const unsigned long size
= GetWidth() * GetHeight();
2682 unsigned char r
, g
, b
;
2683 for ( unsigned long n
= 0; n
< size
; n
++ )
2689 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2691 if ( entry
.value
++ == 0 )
2692 entry
.index
= nentries
++;
2699 * Rotation code by Carlos Moreno
2702 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2703 // does exactly the same thing. And I also got rid of wxRotationPixel
2704 // bacause of potential problems in architectures where alignment
2705 // is an issue, so I had to rewrite parts of the code.
2707 static const double gs_Epsilon
= 1e-10;
2709 static inline int wxCint (double x
)
2711 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2715 // Auxiliary function to rotate a point (x,y) with respect to point p0
2716 // make it inline and use a straight return to facilitate optimization
2717 // also, the function receives the sine and cosine of the angle to avoid
2718 // repeating the time-consuming calls to these functions -- sin/cos can
2719 // be computed and stored in the calling function.
2721 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2723 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2724 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2727 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2729 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2732 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2735 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2737 bool has_alpha
= HasAlpha();
2739 // Create pointer-based array to accelerate access to wxImage's data
2740 unsigned char ** data
= new unsigned char * [GetHeight()];
2741 data
[0] = GetData();
2742 for (i
= 1; i
< GetHeight(); i
++)
2743 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2745 // Same for alpha channel
2746 unsigned char ** alpha
= NULL
;
2749 alpha
= new unsigned char * [GetHeight()];
2750 alpha
[0] = GetAlpha();
2751 for (i
= 1; i
< GetHeight(); i
++)
2752 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2755 // precompute coefficients for rotation formula
2756 // (sine and cosine of the angle)
2757 const double cos_angle
= cos(angle
);
2758 const double sin_angle
= sin(angle
);
2760 // Create new Image to store the result
2761 // First, find rectangle that covers the rotated image; to do that,
2762 // rotate the four corners
2764 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2766 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2767 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2768 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2769 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2771 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2772 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2773 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2774 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2776 // Create rotated image
2777 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2778 // With alpha channel
2782 if (offset_after_rotation
!= NULL
)
2784 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2787 // GRG: The rotated (destination) image is always accessed
2788 // sequentially, so there is no need for a pointer-based
2789 // array here (and in fact it would be slower).
2791 unsigned char * dst
= rotated
.GetData();
2793 unsigned char * alpha_dst
= NULL
;
2795 alpha_dst
= rotated
.GetAlpha();
2797 // GRG: if the original image has a mask, use its RGB values
2798 // as the blank pixel, else, fall back to default (black).
2800 unsigned char blank_r
= 0;
2801 unsigned char blank_g
= 0;
2802 unsigned char blank_b
= 0;
2806 blank_r
= GetMaskRed();
2807 blank_g
= GetMaskGreen();
2808 blank_b
= GetMaskBlue();
2809 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2812 // Now, for each point of the rotated image, find where it came from, by
2813 // performing an inverse rotation (a rotation of -angle) and getting the
2814 // pixel at those coordinates
2816 // GRG: I've taken the (interpolating) test out of the loops, so that
2817 // it is done only once, instead of repeating it for each pixel.
2822 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2824 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2826 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2828 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2829 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2831 // interpolate using the 4 enclosing grid-points. Those
2832 // points can be obtained using floor and ceiling of the
2833 // exact coordinates of the point
2836 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2838 x1
= wxCint(floor(src
.x
));
2839 x2
= wxCint(ceil(src
.x
));
2841 else // else means that x is near one of the borders (0 or width-1)
2843 x1
= x2
= wxCint (src
.x
);
2846 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2848 y1
= wxCint(floor(src
.y
));
2849 y2
= wxCint(ceil(src
.y
));
2853 y1
= y2
= wxCint (src
.y
);
2856 // get four points and the distances (square of the distance,
2857 // for efficiency reasons) for the interpolation formula
2859 // GRG: Do not calculate the points until they are
2860 // really needed -- this way we can calculate
2861 // just one, instead of four, if d1, d2, d3
2862 // or d4 are < gs_Epsilon
2864 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2865 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2866 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2867 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2869 // Now interpolate as a weighted average of the four surrounding
2870 // points, where the weights are the distances to each of those points
2872 // If the point is exactly at one point of the grid of the source
2873 // image, then don't interpolate -- just assign the pixel
2875 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2877 unsigned char *p
= data
[y1
] + (3 * x1
);
2883 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2885 else if (d2
< gs_Epsilon
)
2887 unsigned char *p
= data
[y1
] + (3 * x2
);
2893 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2895 else if (d3
< gs_Epsilon
)
2897 unsigned char *p
= data
[y2
] + (3 * x2
);
2903 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2905 else if (d4
< gs_Epsilon
)
2907 unsigned char *p
= data
[y2
] + (3 * x1
);
2913 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2917 // weights for the weighted average are proportional to the inverse of the distance
2918 unsigned char *v1
= data
[y1
] + (3 * x1
);
2919 unsigned char *v2
= data
[y1
] + (3 * x2
);
2920 unsigned char *v3
= data
[y2
] + (3 * x2
);
2921 unsigned char *v4
= data
[y2
] + (3 * x1
);
2923 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2927 *(dst
++) = (unsigned char)
2928 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2929 w3
* *(v3
++) + w4
* *(v4
++)) /
2930 (w1
+ w2
+ w3
+ w4
) );
2931 *(dst
++) = (unsigned char)
2932 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2933 w3
* *(v3
++) + w4
* *(v4
++)) /
2934 (w1
+ w2
+ w3
+ w4
) );
2935 *(dst
++) = (unsigned char)
2936 ( (w1
* *v1
+ w2
* *v2
+
2937 w3
* *v3
+ w4
* *v4
) /
2938 (w1
+ w2
+ w3
+ w4
) );
2942 v1
= alpha
[y1
] + (x1
);
2943 v2
= alpha
[y1
] + (x2
);
2944 v3
= alpha
[y2
] + (x2
);
2945 v4
= alpha
[y2
] + (x1
);
2947 *(alpha_dst
++) = (unsigned char)
2948 ( (w1
* *v1
+ w2
* *v2
+
2949 w3
* *v3
+ w4
* *v4
) /
2950 (w1
+ w2
+ w3
+ w4
) );
2966 else // not interpolating
2968 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2970 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2972 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2974 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2975 const int ys
= wxCint (src
.y
); // closest integer
2977 if (0 <= xs
&& xs
< GetWidth() &&
2978 0 <= ys
&& ys
< GetHeight())
2980 unsigned char *p
= data
[ys
] + (3 * xs
);
2986 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2995 *(alpha_dst
++) = 255;
3013 // A module to allow wxImage initialization/cleanup
3014 // without calling these functions from app.cpp or from
3015 // the user's application.
3017 class wxImageModule
: public wxModule
3019 DECLARE_DYNAMIC_CLASS(wxImageModule
)
3022 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
3023 void OnExit() { wxImage::CleanUpHandlers(); };
3026 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
3029 #endif // wxUSE_IMAGE