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"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
35 #include "wx/xpmdecod.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 class wxImageRefData
: public wxObjectRefData
49 virtual ~wxImageRefData();
53 unsigned char *m_data
;
56 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
58 // alpha channel data, may be NULL for the formats without alpha support
59 unsigned char *m_alpha
;
63 // if true, m_data is pointer to static data and shouldn't be freed
66 // same as m_static but for m_alpha
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
93 m_staticAlpha
= false;
96 wxImageRefData::~wxImageRefData()
100 if ( !m_staticAlpha
)
104 wxList
wxImage::sm_handlers
;
108 //-----------------------------------------------------------------------------
110 #define M_IMGDATA ((wxImageRefData *)m_refData)
112 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
114 wxImage::wxImage( int width
, int height
, bool clear
)
116 Create( width
, height
, clear
);
119 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
121 Create( width
, height
, data
, static_data
);
124 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
126 Create( width
, height
, data
, alpha
, static_data
);
129 wxImage::wxImage( const wxString
& name
, long type
, int index
)
131 LoadFile( name
, type
, index
);
134 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
136 LoadFile( name
, mimetype
, index
);
140 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
142 LoadFile( stream
, type
, index
);
145 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
147 LoadFile( stream
, mimetype
, index
);
149 #endif // wxUSE_STREAMS
151 wxImage::wxImage( const wxImage
& image
)
157 wxImage::wxImage( const wxImage
* image
)
159 if (image
) Ref(*image
);
162 wxImage::wxImage( const char** xpmData
)
167 wxImage::wxImage( char** xpmData
)
169 Create((const char**) xpmData
);
172 bool wxImage::Create( const char** xpmData
)
177 wxXPMDecoder decoder
;
178 (*this) = decoder
.ReadData(xpmData
);
185 bool wxImage::Create( int width
, int height
, bool clear
)
189 m_refData
= new wxImageRefData();
191 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
192 if (!M_IMGDATA
->m_data
)
199 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
201 M_IMGDATA
->m_width
= width
;
202 M_IMGDATA
->m_height
= height
;
203 M_IMGDATA
->m_ok
= true;
208 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
212 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
214 m_refData
= new wxImageRefData();
216 M_IMGDATA
->m_data
= data
;
217 M_IMGDATA
->m_width
= width
;
218 M_IMGDATA
->m_height
= height
;
219 M_IMGDATA
->m_ok
= true;
220 M_IMGDATA
->m_static
= static_data
;
225 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
229 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
231 m_refData
= new wxImageRefData();
233 M_IMGDATA
->m_data
= data
;
234 M_IMGDATA
->m_alpha
= alpha
;
235 M_IMGDATA
->m_width
= width
;
236 M_IMGDATA
->m_height
= height
;
237 M_IMGDATA
->m_ok
= true;
238 M_IMGDATA
->m_static
= static_data
;
243 void wxImage::Destroy()
248 wxImage
wxImage::Copy() const
252 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
254 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
256 unsigned char *data
= image
.GetData();
258 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
260 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
261 image
.SetMask( M_IMGDATA
->m_hasMask
);
263 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
265 // also copy the image options
266 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
267 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
268 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
273 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
275 if( xFactor
== 1 && yFactor
== 1 )
280 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
282 // can't scale to/from 0 size
283 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
284 wxT("invalid new image size") );
286 long old_height
= M_IMGDATA
->m_height
,
287 old_width
= M_IMGDATA
->m_width
;
289 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
290 wxT("invalid old image size") );
292 long width
= old_width
/ xFactor
;
293 long height
= old_height
/ yFactor
;
295 image
.Create( width
, height
, false );
297 char unsigned *data
= image
.GetData();
299 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
301 bool hasMask
= false ;
302 unsigned char maskRed
= 0;
303 unsigned char maskGreen
= 0;
304 unsigned char maskBlue
=0 ;
306 unsigned char *source_data
= M_IMGDATA
->m_data
;
307 unsigned char *target_data
= data
;
308 unsigned char *source_alpha
= 0 ;
309 unsigned char *target_alpha
= 0 ;
310 if (M_IMGDATA
->m_hasMask
)
313 maskRed
= M_IMGDATA
->m_maskRed
;
314 maskGreen
= M_IMGDATA
->m_maskGreen
;
315 maskBlue
=M_IMGDATA
->m_maskBlue
;
317 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
318 M_IMGDATA
->m_maskGreen
,
319 M_IMGDATA
->m_maskBlue
);
323 source_alpha
= M_IMGDATA
->m_alpha
;
327 target_alpha
= image
.GetAlpha() ;
331 for (long y
= 0; y
< height
; y
++)
333 for (long x
= 0; x
< width
; x
++)
335 unsigned long avgRed
= 0 ;
336 unsigned long avgGreen
= 0;
337 unsigned long avgBlue
= 0;
338 unsigned long avgAlpha
= 0 ;
339 unsigned long counter
= 0 ;
341 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
343 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
344 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
346 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
347 unsigned char red
= pixel
[0] ;
348 unsigned char green
= pixel
[1] ;
349 unsigned char blue
= pixel
[2] ;
350 unsigned char alpha
= 255 ;
352 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
353 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
368 *(target_data
++) = M_IMGDATA
->m_maskRed
;
369 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
370 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
375 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
376 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
377 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
378 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
383 // In case this is a cursor, make sure the hotspot is scaled accordingly:
384 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
385 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
386 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
387 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
388 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
389 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
394 wxImage
wxImage::Scale( int width
, int height
) const
398 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
400 // can't scale to/from 0 size
401 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
402 wxT("invalid new image size") );
404 long old_height
= M_IMGDATA
->m_height
,
405 old_width
= M_IMGDATA
->m_width
;
406 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
407 wxT("invalid old image size") );
409 if ( old_width
% width
== 0 && old_width
>= width
&&
410 old_height
% height
== 0 && old_height
>= height
)
412 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
414 image
.Create( width
, height
, false );
416 unsigned char *data
= image
.GetData();
418 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
420 unsigned char *source_data
= M_IMGDATA
->m_data
;
421 unsigned char *target_data
= data
;
422 unsigned char *source_alpha
= 0 ;
423 unsigned char *target_alpha
= 0 ;
425 if (M_IMGDATA
->m_hasMask
)
427 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
428 M_IMGDATA
->m_maskGreen
,
429 M_IMGDATA
->m_maskBlue
);
433 source_alpha
= M_IMGDATA
->m_alpha
;
437 target_alpha
= image
.GetAlpha() ;
441 long x_delta
= (old_width
<<16) / width
;
442 long y_delta
= (old_height
<<16) / height
;
444 unsigned char* dest_pixel
= target_data
;
447 for ( long j
= 0; j
< height
; j
++ )
449 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
450 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
453 for ( long i
= 0; i
< width
; i
++ )
455 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
456 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
457 dest_pixel
[0] = src_pixel
[0];
458 dest_pixel
[1] = src_pixel
[1];
459 dest_pixel
[2] = src_pixel
[2];
462 *(target_alpha
++) = *src_alpha_pixel
;
469 // In case this is a cursor, make sure the hotspot is scaled accordingly:
470 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
471 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
472 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
473 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
474 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
475 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
480 wxImage
wxImage::Rotate90( bool clockwise
) const
484 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
486 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
488 unsigned char *data
= image
.GetData();
490 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
492 unsigned char *source_data
= M_IMGDATA
->m_data
;
493 unsigned char *target_data
;
494 unsigned char *alpha_data
= 0 ;
495 unsigned char *source_alpha
= 0 ;
496 unsigned char *target_alpha
= 0 ;
498 if (M_IMGDATA
->m_hasMask
)
500 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
504 source_alpha
= M_IMGDATA
->m_alpha
;
508 alpha_data
= image
.GetAlpha() ;
512 long height
= M_IMGDATA
->m_height
;
513 long width
= M_IMGDATA
->m_width
;
515 for (long j
= 0; j
< height
; j
++)
517 for (long i
= 0; i
< width
; i
++)
521 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
523 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
527 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
529 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
531 memcpy( target_data
, source_data
, 3 );
536 memcpy( target_alpha
, source_alpha
, 1 );
545 wxImage
wxImage::Mirror( bool horizontally
) const
549 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
551 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
553 unsigned char *data
= image
.GetData();
555 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
557 if (M_IMGDATA
->m_hasMask
)
558 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
560 long height
= M_IMGDATA
->m_height
;
561 long width
= M_IMGDATA
->m_width
;
563 unsigned char *source_data
= M_IMGDATA
->m_data
;
564 unsigned char *target_data
;
568 for (long j
= 0; j
< height
; j
++)
571 target_data
= data
-3;
572 for (long i
= 0; i
< width
; i
++)
574 memcpy( target_data
, source_data
, 3 );
582 for (long i
= 0; i
< height
; i
++)
584 target_data
= data
+ 3*width
*(height
-1-i
);
585 memcpy( target_data
, source_data
, (size_t)3*width
);
586 source_data
+= 3*width
;
593 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
597 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
599 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
600 image
, wxT("invalid subimage size") );
602 int subwidth
=rect
.GetWidth();
603 const int subheight
=rect
.GetHeight();
605 image
.Create( subwidth
, subheight
, false );
607 unsigned char *subdata
= image
.GetData(), *data
=GetData();
609 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
611 if (M_IMGDATA
->m_hasMask
)
612 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
614 const int subleft
=3*rect
.GetLeft();
615 const int width
=3*GetWidth();
618 data
+=rect
.GetTop()*width
+subleft
;
620 for (long j
= 0; j
< subheight
; ++j
)
622 memcpy( subdata
, data
, subwidth
);
630 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
631 int r_
, int g_
, int b_
) const
635 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
636 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
638 int width
= GetWidth(), height
= GetHeight();
639 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
641 unsigned char r
= (unsigned char)r_
;
642 unsigned char g
= (unsigned char)g_
;
643 unsigned char b
= (unsigned char)b_
;
644 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
646 GetOrFindMaskColour( &r
, &g
, &b
);
647 image
.SetMaskColour(r
, g
, b
);
650 image
.SetRGB(wxRect(), r
, g
, b
);
652 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
653 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
655 subRect
.Intersect(finalRect
);
657 if (!subRect
.IsEmpty())
659 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
660 image
.Paste(*this, pos
.x
, pos
.y
);
662 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
668 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
670 wxCHECK_RET( Ok(), wxT("invalid image") );
671 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
675 int width
= image
.GetWidth();
676 int height
= image
.GetHeight();
689 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
690 width
= M_IMGDATA
->m_width
- (x
+xx
);
691 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
692 height
= M_IMGDATA
->m_height
- (y
+yy
);
694 if (width
< 1) return;
695 if (height
< 1) return;
697 if ((!HasMask() && !image
.HasMask()) ||
698 (HasMask() && !image
.HasMask()) ||
699 ((HasMask() && image
.HasMask() &&
700 (GetMaskRed()==image
.GetMaskRed()) &&
701 (GetMaskGreen()==image
.GetMaskGreen()) &&
702 (GetMaskBlue()==image
.GetMaskBlue()))))
705 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
706 int source_step
= image
.GetWidth()*3;
708 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
709 int target_step
= M_IMGDATA
->m_width
*3;
710 for (int j
= 0; j
< height
; j
++)
712 memcpy( target_data
, source_data
, width
);
713 source_data
+= source_step
;
714 target_data
+= target_step
;
719 if (!HasMask() && image
.HasMask())
721 unsigned char r
= image
.GetMaskRed();
722 unsigned char g
= image
.GetMaskGreen();
723 unsigned char b
= image
.GetMaskBlue();
726 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
727 int source_step
= image
.GetWidth()*3;
729 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
730 int target_step
= M_IMGDATA
->m_width
*3;
732 for (int j
= 0; j
< height
; j
++)
734 for (int i
= 0; i
< width
; i
+=3)
736 if ((source_data
[i
] != r
) &&
737 (source_data
[i
+1] != g
) &&
738 (source_data
[i
+2] != b
))
740 memcpy( target_data
+i
, source_data
+i
, 3 );
743 source_data
+= source_step
;
744 target_data
+= target_step
;
749 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
750 unsigned char r2
, unsigned char g2
, unsigned char b2
)
752 wxCHECK_RET( Ok(), wxT("invalid image") );
754 unsigned char *data
= GetData();
756 const int w
= GetWidth();
757 const int h
= GetHeight();
759 for (int j
= 0; j
< h
; j
++)
760 for (int i
= 0; i
< w
; i
++)
762 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
772 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
776 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
778 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
780 unsigned char *dest
= image
.GetData();
782 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
784 unsigned char *src
= M_IMGDATA
->m_data
;
785 bool hasMask
= M_IMGDATA
->m_hasMask
;
786 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
787 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
788 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
791 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
793 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
794 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
796 // don't modify the mask
797 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
799 memcpy(dest
, src
, 3);
803 // calculate the luma
804 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
805 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
812 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
816 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
818 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
820 unsigned char *data
= image
.GetData();
822 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
824 if (M_IMGDATA
->m_hasMask
)
826 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
827 M_IMGDATA
->m_maskBlue
== b
)
828 image
.SetMaskColour( 255, 255, 255 );
830 image
.SetMaskColour( 0, 0, 0 );
833 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
835 unsigned char *srcd
= M_IMGDATA
->m_data
;
836 unsigned char *tard
= image
.GetData();
838 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
840 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
841 tard
[0] = tard
[1] = tard
[2] = 255;
843 tard
[0] = tard
[1] = tard
[2] = 0;
849 int wxImage::GetWidth() const
851 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
853 return M_IMGDATA
->m_width
;
856 int wxImage::GetHeight() const
858 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
860 return M_IMGDATA
->m_height
;
863 long wxImage::XYToIndex(int x
, int y
) const
867 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
869 return y
*M_IMGDATA
->m_width
+ x
;
875 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
877 long pos
= XYToIndex(x
, y
);
878 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
882 M_IMGDATA
->m_data
[ pos
] = r
;
883 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
884 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
887 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
889 wxCHECK_RET( Ok(), wxT("invalid image") );
892 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
893 if ( rect
== wxRect() )
899 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
900 imageRect
.Inside(rect
.GetBottomRight()),
901 wxT("invalid bounding rectangle") );
904 int x1
= rect
.GetLeft(),
906 x2
= rect
.GetRight() + 1,
907 y2
= rect
.GetBottom() + 1;
909 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
910 int x
, y
, width
= GetWidth();
911 for (y
= y1
; y
< y2
; y
++)
913 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
914 for (x
= x1
; x
< x2
; x
++)
923 unsigned char wxImage::GetRed( int x
, int y
) const
925 long pos
= XYToIndex(x
, y
);
926 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
930 return M_IMGDATA
->m_data
[pos
];
933 unsigned char wxImage::GetGreen( int x
, int y
) const
935 long pos
= XYToIndex(x
, y
);
936 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
940 return M_IMGDATA
->m_data
[pos
+1];
943 unsigned char wxImage::GetBlue( int x
, int y
) const
945 long pos
= XYToIndex(x
, y
);
946 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
950 return M_IMGDATA
->m_data
[pos
+2];
953 bool wxImage::Ok() const
955 // image of 0 width or height can't be considered ok - at least because it
956 // causes crashes in ConvertToBitmap() if we don't catch it in time
957 wxImageRefData
*data
= M_IMGDATA
;
958 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
961 unsigned char *wxImage::GetData() const
963 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
965 return M_IMGDATA
->m_data
;
968 void wxImage::SetData( unsigned char *data
, bool static_data
)
970 wxCHECK_RET( Ok(), wxT("invalid image") );
972 wxImageRefData
*newRefData
= new wxImageRefData();
974 newRefData
->m_width
= M_IMGDATA
->m_width
;
975 newRefData
->m_height
= M_IMGDATA
->m_height
;
976 newRefData
->m_data
= data
;
977 newRefData
->m_ok
= true;
978 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
979 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
980 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
981 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
982 newRefData
->m_static
= static_data
;
986 m_refData
= newRefData
;
989 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
991 wxImageRefData
*newRefData
= new wxImageRefData();
995 newRefData
->m_width
= new_width
;
996 newRefData
->m_height
= new_height
;
997 newRefData
->m_data
= data
;
998 newRefData
->m_ok
= true;
999 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1000 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1001 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1002 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1006 newRefData
->m_width
= new_width
;
1007 newRefData
->m_height
= new_height
;
1008 newRefData
->m_data
= data
;
1009 newRefData
->m_ok
= true;
1011 newRefData
->m_static
= static_data
;
1015 m_refData
= newRefData
;
1018 // ----------------------------------------------------------------------------
1019 // alpha channel support
1020 // ----------------------------------------------------------------------------
1022 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1024 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1026 long pos
= XYToIndex(x
, y
);
1027 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1029 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1032 unsigned char wxImage::GetAlpha(int x
, int y
) const
1034 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1036 long pos
= XYToIndex(x
, y
);
1037 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1039 return M_IMGDATA
->m_alpha
[pos
];
1043 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1047 const int w
= M_IMGDATA
->m_width
;
1048 const int h
= M_IMGDATA
->m_height
;
1050 unsigned char *alpha
= GetAlpha();
1051 unsigned char *data
= GetData();
1053 for ( int y
= 0; y
< h
; y
++ )
1055 for ( int x
= 0; x
< w
; x
++ )
1067 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1069 wxCHECK_RET( Ok(), wxT("invalid image") );
1073 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1076 free(M_IMGDATA
->m_alpha
);
1077 M_IMGDATA
->m_alpha
= alpha
;
1078 M_IMGDATA
->m_staticAlpha
= static_data
;
1081 unsigned char *wxImage::GetAlpha() const
1083 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1085 return M_IMGDATA
->m_alpha
;
1088 void wxImage::InitAlpha()
1090 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1092 // initialize memory for alpha channel
1095 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1096 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1100 // use the mask to initialize the alpha channel.
1101 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1103 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1104 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1105 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1106 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1110 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1111 ? wxIMAGE_ALPHA_TRANSPARENT
1112 : wxIMAGE_ALPHA_OPAQUE
;
1115 M_IMGDATA
->m_hasMask
= false;
1119 // make the image fully opaque
1120 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1124 // ----------------------------------------------------------------------------
1126 // ----------------------------------------------------------------------------
1128 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1130 wxCHECK_RET( Ok(), wxT("invalid image") );
1132 M_IMGDATA
->m_maskRed
= r
;
1133 M_IMGDATA
->m_maskGreen
= g
;
1134 M_IMGDATA
->m_maskBlue
= b
;
1135 M_IMGDATA
->m_hasMask
= true;
1138 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1140 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1142 if (M_IMGDATA
->m_hasMask
)
1144 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1145 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1146 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1151 FindFirstUnusedColour(r
, g
, b
);
1156 unsigned char wxImage::GetMaskRed() const
1158 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1160 return M_IMGDATA
->m_maskRed
;
1163 unsigned char wxImage::GetMaskGreen() const
1165 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1167 return M_IMGDATA
->m_maskGreen
;
1170 unsigned char wxImage::GetMaskBlue() const
1172 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1174 return M_IMGDATA
->m_maskBlue
;
1177 void wxImage::SetMask( bool mask
)
1179 wxCHECK_RET( Ok(), wxT("invalid image") );
1181 M_IMGDATA
->m_hasMask
= mask
;
1184 bool wxImage::HasMask() const
1186 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1188 return M_IMGDATA
->m_hasMask
;
1191 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1193 long pos
= XYToIndex(x
, y
);
1194 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1197 if ( M_IMGDATA
->m_hasMask
)
1199 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1200 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1201 p
[1] == M_IMGDATA
->m_maskGreen
&&
1202 p
[2] == M_IMGDATA
->m_maskBlue
)
1209 if ( M_IMGDATA
->m_alpha
)
1211 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1213 // transparent enough
1222 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1223 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1225 // check that the images are the same size
1226 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1228 wxLogError( _("Image and mask have different sizes.") );
1232 // find unused colour
1233 unsigned char r
,g
,b
;
1234 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1236 wxLogError( _("No unused colour in image being masked.") );
1240 unsigned char *imgdata
= GetData();
1241 unsigned char *maskdata
= mask
.GetData();
1243 const int w
= GetWidth();
1244 const int h
= GetHeight();
1246 for (int j
= 0; j
< h
; j
++)
1248 for (int i
= 0; i
< w
; i
++)
1250 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1261 SetMaskColour(r
, g
, b
);
1267 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1272 unsigned char mr
, mg
, mb
;
1273 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1275 wxLogError( _("No unused colour in image being masked.") );
1280 SetMaskColour(mr
, mg
, mb
);
1282 unsigned char *imgdata
= GetData();
1283 unsigned char *alphadata
= GetAlpha();
1286 int h
= GetHeight();
1288 for (int y
= 0; y
< h
; y
++)
1290 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1292 if (*alphadata
< threshold
)
1301 free(M_IMGDATA
->m_alpha
);
1302 M_IMGDATA
->m_alpha
= NULL
;
1307 // ----------------------------------------------------------------------------
1308 // Palette functions
1309 // ----------------------------------------------------------------------------
1313 bool wxImage::HasPalette() const
1318 return M_IMGDATA
->m_palette
.Ok();
1321 const wxPalette
& wxImage::GetPalette() const
1323 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1325 return M_IMGDATA
->m_palette
;
1328 void wxImage::SetPalette(const wxPalette
& palette
)
1330 wxCHECK_RET( Ok(), wxT("invalid image") );
1332 M_IMGDATA
->m_palette
= palette
;
1335 #endif // wxUSE_PALETTE
1337 // ----------------------------------------------------------------------------
1338 // Option functions (arbitrary name/value mapping)
1339 // ----------------------------------------------------------------------------
1341 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1343 wxCHECK_RET( Ok(), wxT("invalid image") );
1345 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1346 if (idx
== wxNOT_FOUND
)
1348 M_IMGDATA
->m_optionNames
.Add(name
);
1349 M_IMGDATA
->m_optionValues
.Add(value
);
1353 M_IMGDATA
->m_optionNames
[idx
] = name
;
1354 M_IMGDATA
->m_optionValues
[idx
] = value
;
1358 void wxImage::SetOption(const wxString
& name
, int value
)
1361 valStr
.Printf(wxT("%d"), value
);
1362 SetOption(name
, valStr
);
1365 wxString
wxImage::GetOption(const wxString
& name
) const
1367 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1369 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1370 if (idx
== wxNOT_FOUND
)
1371 return wxEmptyString
;
1373 return M_IMGDATA
->m_optionValues
[idx
];
1376 int wxImage::GetOptionInt(const wxString
& name
) const
1378 return wxAtoi(GetOption(name
));
1381 bool wxImage::HasOption(const wxString
& name
) const
1383 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1385 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1388 // ----------------------------------------------------------------------------
1390 // ----------------------------------------------------------------------------
1392 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1395 if (wxFileExists(filename
))
1397 wxFileInputStream
stream(filename
);
1398 wxBufferedInputStream
bstream( stream
);
1399 return LoadFile(bstream
, type
, index
);
1403 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1407 #else // !wxUSE_STREAMS
1409 #endif // wxUSE_STREAMS
1412 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1415 if (wxFileExists(filename
))
1417 wxFileInputStream
stream(filename
);
1418 wxBufferedInputStream
bstream( stream
);
1419 return LoadFile(bstream
, mimetype
, index
);
1423 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1427 #else // !wxUSE_STREAMS
1429 #endif // wxUSE_STREAMS
1434 bool wxImage::SaveFile( const wxString
& filename
) const
1436 wxString ext
= filename
.AfterLast('.').Lower();
1438 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1441 SaveFile(filename
, pHandler
->GetType());
1445 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1450 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1453 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1455 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1457 wxFileOutputStream
stream(filename
);
1459 if ( stream
.IsOk() )
1461 wxBufferedOutputStream
bstream( stream
);
1462 return SaveFile(bstream
, type
);
1464 #endif // wxUSE_STREAMS
1469 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1472 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1474 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1476 wxFileOutputStream
stream(filename
);
1478 if ( stream
.IsOk() )
1480 wxBufferedOutputStream
bstream( stream
);
1481 return SaveFile(bstream
, mimetype
);
1483 #endif // wxUSE_STREAMS
1488 bool wxImage::CanRead( const wxString
&name
)
1491 wxFileInputStream
stream(name
);
1492 return CanRead(stream
);
1498 int wxImage::GetImageCount( const wxString
&name
, long type
)
1501 wxFileInputStream
stream(name
);
1503 return GetImageCount(stream
, type
);
1511 bool wxImage::CanRead( wxInputStream
&stream
)
1513 const wxList
& list
= GetHandlers();
1515 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1517 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1518 if (handler
->CanRead( stream
))
1525 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1527 wxImageHandler
*handler
;
1529 if ( type
== wxBITMAP_TYPE_ANY
)
1531 wxList
&list
=GetHandlers();
1533 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1535 handler
=(wxImageHandler
*)node
->GetData();
1536 if ( handler
->CanRead(stream
) )
1537 return handler
->GetImageCount(stream
);
1541 wxLogWarning(_("No handler found for image type."));
1545 handler
= FindHandler(type
);
1549 wxLogWarning(_("No image handler for type %d defined."), type
);
1553 if ( handler
->CanRead(stream
) )
1555 return handler
->GetImageCount(stream
);
1559 wxLogError(_("Image file is not of type %d."), type
);
1564 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1568 m_refData
= new wxImageRefData
;
1570 wxImageHandler
*handler
;
1572 if ( type
== wxBITMAP_TYPE_ANY
)
1574 wxList
&list
=GetHandlers();
1576 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1578 handler
=(wxImageHandler
*)node
->GetData();
1579 if ( handler
->CanRead(stream
) )
1580 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1584 wxLogWarning( _("No handler found for image type.") );
1588 handler
= FindHandler(type
);
1592 wxLogWarning( _("No image handler for type %d defined."), type
);
1597 if (!handler
->CanRead(stream
))
1599 wxLogError(_("Image file is not of type %d."), type
);
1603 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1606 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1610 m_refData
= new wxImageRefData
;
1612 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1616 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1621 if (!handler
->CanRead(stream
))
1623 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1627 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1630 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1632 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1634 wxImageHandler
*handler
= FindHandler(type
);
1637 wxLogWarning( _("No image handler for type %d defined."), type
);
1642 return handler
->SaveFile( (wxImage
*)this, stream
);
1645 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1647 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1649 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1652 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1657 return handler
->SaveFile( (wxImage
*)this, stream
);
1659 #endif // wxUSE_STREAMS
1661 // ----------------------------------------------------------------------------
1662 // image I/O handlers
1663 // ----------------------------------------------------------------------------
1665 void wxImage::AddHandler( wxImageHandler
*handler
)
1667 // Check for an existing handler of the type being added.
1668 if (FindHandler( handler
->GetType() ) == 0)
1670 sm_handlers
.Append( handler
);
1674 // This is not documented behaviour, merely the simplest 'fix'
1675 // for preventing duplicate additions. If someone ever has
1676 // a good reason to add and remove duplicate handlers (and they
1677 // may) we should probably refcount the duplicates.
1678 // also an issue in InsertHandler below.
1680 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1681 handler
->GetName().c_str() );
1686 void wxImage::InsertHandler( wxImageHandler
*handler
)
1688 // Check for an existing handler of the type being added.
1689 if (FindHandler( handler
->GetType() ) == 0)
1691 sm_handlers
.Insert( handler
);
1695 // see AddHandler for additional comments.
1696 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1697 handler
->GetName().c_str() );
1702 bool wxImage::RemoveHandler( const wxString
& name
)
1704 wxImageHandler
*handler
= FindHandler(name
);
1707 sm_handlers
.DeleteObject(handler
);
1715 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1717 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1720 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1721 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1723 node
= node
->GetNext();
1728 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1730 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1733 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1734 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1735 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1737 node
= node
->GetNext();
1742 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1744 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1747 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1748 if (handler
->GetType() == bitmapType
) return handler
;
1749 node
= node
->GetNext();
1754 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1756 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1759 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1760 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1761 node
= node
->GetNext();
1766 void wxImage::InitStandardHandlers()
1769 AddHandler(new wxBMPHandler
);
1770 #endif // wxUSE_STREAMS
1773 void wxImage::CleanUpHandlers()
1775 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1778 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1779 wxList::compatibility_iterator next
= node
->GetNext();
1784 sm_handlers
.Clear();
1787 wxString
wxImage::GetImageExtWildcard()
1791 wxList
& Handlers
= wxImage::GetHandlers();
1792 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1795 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1796 fmts
+= wxT("*.") + Handler
->GetExtension();
1797 Node
= Node
->GetNext();
1798 if ( Node
) fmts
+= wxT(";");
1801 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1804 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1806 const double red
= rgb
.red
/ 255.0,
1807 green
= rgb
.green
/ 255.0,
1808 blue
= rgb
.blue
/ 255.0;
1810 // find the min and max intensity (and remember which one was it for the
1812 double minimumRGB
= red
;
1813 if ( green
< minimumRGB
)
1815 if ( blue
< minimumRGB
)
1818 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1819 double maximumRGB
= red
;
1820 if ( green
> maximumRGB
)
1825 if ( blue
> maximumRGB
)
1831 const double value
= maximumRGB
;
1833 double hue
= 0.0, saturation
;
1834 const double deltaRGB
= maximumRGB
- minimumRGB
;
1835 if ( wxIsNullDouble(deltaRGB
) )
1837 // Gray has no color
1846 hue
= (green
- blue
) / deltaRGB
;
1850 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1854 hue
= 4.0 + (red
- green
) / deltaRGB
;
1858 wxFAIL_MSG(wxT("hue not specified"));
1867 saturation
= deltaRGB
/ maximumRGB
;
1870 return HSVValue(hue
, saturation
, value
);
1873 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1875 double red
, green
, blue
;
1877 if ( wxIsNullDouble(hsv
.saturation
) )
1886 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1887 int i
= (int)floor(hue
);
1888 double f
= hue
- i
; // fractional part of h
1889 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1895 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1900 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1908 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1913 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1918 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1926 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1931 return RGBValue((unsigned char)(red
* 255.0),
1932 (unsigned char)(green
* 255.0),
1933 (unsigned char)(blue
* 255.0));
1937 * Rotates the hue of each pixel of the image. angle is a double in the range
1938 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1940 void wxImage::RotateHue(double angle
)
1942 unsigned char *srcBytePtr
;
1943 unsigned char *dstBytePtr
;
1944 unsigned long count
;
1945 wxImage::HSVValue hsv
;
1946 wxImage::RGBValue rgb
;
1948 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
1949 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1950 if ( count
> 0 && !wxIsNullDouble(angle
) )
1952 srcBytePtr
= M_IMGDATA
->m_data
;
1953 dstBytePtr
= srcBytePtr
;
1956 rgb
.red
= *srcBytePtr
++;
1957 rgb
.green
= *srcBytePtr
++;
1958 rgb
.blue
= *srcBytePtr
++;
1959 hsv
= RGBtoHSV(rgb
);
1961 hsv
.hue
= hsv
.hue
+ angle
;
1963 hsv
.hue
= hsv
.hue
- 1.0;
1964 else if (hsv
.hue
< 0.0)
1965 hsv
.hue
= hsv
.hue
+ 1.0;
1967 rgb
= HSVtoRGB(hsv
);
1968 *dstBytePtr
++ = rgb
.red
;
1969 *dstBytePtr
++ = rgb
.green
;
1970 *dstBytePtr
++ = rgb
.blue
;
1971 } while (--count
!= 0);
1975 //-----------------------------------------------------------------------------
1977 //-----------------------------------------------------------------------------
1979 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1982 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1987 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1992 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1997 bool wxImageHandler::CanRead( const wxString
& name
)
1999 if (wxFileExists(name
))
2001 wxFileInputStream
stream(name
);
2002 return CanRead(stream
);
2005 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2010 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2012 wxFileOffset posOld
= stream
.TellI();
2013 if ( posOld
== wxInvalidOffset
)
2015 // can't test unseekable stream
2019 bool ok
= DoCanRead(stream
);
2021 // restore the old position to be able to test other formats and so on
2022 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2024 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2026 // reading would fail anyhow as we're not at the right position
2033 #endif // wxUSE_STREAMS
2035 // ----------------------------------------------------------------------------
2036 // image histogram stuff
2037 // ----------------------------------------------------------------------------
2040 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2045 unsigned char g2
) const
2047 unsigned long key
= MakeKey(r2
, g2
, b2
);
2049 while ( find(key
) != end() )
2051 // color already used
2063 wxLogError(_("No unused colour in image.") );
2069 key
= MakeKey(r2
, g2
, b2
);
2083 wxImage::FindFirstUnusedColour(unsigned char *r
,
2088 unsigned char g2
) const
2090 wxImageHistogram histogram
;
2092 ComputeHistogram(histogram
);
2094 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2100 // Counts and returns the number of different colours. Optionally stops
2101 // when it exceeds 'stopafter' different colours. This is useful, for
2102 // example, to see if the image can be saved as 8-bit (256 colour or
2103 // less, in this case it would be invoked as CountColours(256)). Default
2104 // value for stopafter is -1 (don't care).
2106 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2110 unsigned char r
, g
, b
;
2112 unsigned long size
, nentries
, key
;
2115 size
= GetWidth() * GetHeight();
2118 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2123 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2125 if (h
.Get(key
) == NULL
)
2136 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2138 unsigned char *p
= GetData();
2139 unsigned long nentries
= 0;
2143 const unsigned long size
= GetWidth() * GetHeight();
2145 unsigned char r
, g
, b
;
2146 for ( unsigned long n
= 0; n
< size
; n
++ )
2152 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2154 if ( entry
.value
++ == 0 )
2155 entry
.index
= nentries
++;
2162 * Rotation code by Carlos Moreno
2165 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2166 // does exactly the same thing. And I also got rid of wxRotationPixel
2167 // bacause of potential problems in architectures where alignment
2168 // is an issue, so I had to rewrite parts of the code.
2170 static const double gs_Epsilon
= 1e-10;
2172 static inline int wxCint (double x
)
2174 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2178 // Auxiliary function to rotate a point (x,y) with respect to point p0
2179 // make it inline and use a straight return to facilitate optimization
2180 // also, the function receives the sine and cosine of the angle to avoid
2181 // repeating the time-consuming calls to these functions -- sin/cos can
2182 // be computed and stored in the calling function.
2184 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2186 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2187 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2190 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2192 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2195 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2198 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2200 bool has_alpha
= HasAlpha();
2202 // Create pointer-based array to accelerate access to wxImage's data
2203 unsigned char ** data
= new unsigned char * [GetHeight()];
2204 data
[0] = GetData();
2205 for (i
= 1; i
< GetHeight(); i
++)
2206 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2208 // Same for alpha channel
2209 unsigned char ** alpha
= NULL
;
2212 alpha
= new unsigned char * [GetHeight()];
2213 alpha
[0] = GetAlpha();
2214 for (i
= 1; i
< GetHeight(); i
++)
2215 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2218 // precompute coefficients for rotation formula
2219 // (sine and cosine of the angle)
2220 const double cos_angle
= cos(angle
);
2221 const double sin_angle
= sin(angle
);
2223 // Create new Image to store the result
2224 // First, find rectangle that covers the rotated image; to do that,
2225 // rotate the four corners
2227 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2229 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2230 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2231 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2232 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2234 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2235 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2236 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2237 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2239 // Create rotated image
2240 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2241 // With alpha channel
2245 if (offset_after_rotation
!= NULL
)
2247 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2250 // GRG: The rotated (destination) image is always accessed
2251 // sequentially, so there is no need for a pointer-based
2252 // array here (and in fact it would be slower).
2254 unsigned char * dst
= rotated
.GetData();
2256 unsigned char * alpha_dst
= NULL
;
2258 alpha_dst
= rotated
.GetAlpha();
2260 // GRG: if the original image has a mask, use its RGB values
2261 // as the blank pixel, else, fall back to default (black).
2263 unsigned char blank_r
= 0;
2264 unsigned char blank_g
= 0;
2265 unsigned char blank_b
= 0;
2269 blank_r
= GetMaskRed();
2270 blank_g
= GetMaskGreen();
2271 blank_b
= GetMaskBlue();
2272 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2275 // Now, for each point of the rotated image, find where it came from, by
2276 // performing an inverse rotation (a rotation of -angle) and getting the
2277 // pixel at those coordinates
2279 // GRG: I've taken the (interpolating) test out of the loops, so that
2280 // it is done only once, instead of repeating it for each pixel.
2285 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2287 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2289 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2291 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2292 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2294 // interpolate using the 4 enclosing grid-points. Those
2295 // points can be obtained using floor and ceiling of the
2296 // exact coordinates of the point
2299 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2301 x1
= wxCint(floor(src
.x
));
2302 x2
= wxCint(ceil(src
.x
));
2304 else // else means that x is near one of the borders (0 or width-1)
2306 x1
= x2
= wxCint (src
.x
);
2309 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2311 y1
= wxCint(floor(src
.y
));
2312 y2
= wxCint(ceil(src
.y
));
2316 y1
= y2
= wxCint (src
.y
);
2319 // get four points and the distances (square of the distance,
2320 // for efficiency reasons) for the interpolation formula
2322 // GRG: Do not calculate the points until they are
2323 // really needed -- this way we can calculate
2324 // just one, instead of four, if d1, d2, d3
2325 // or d4 are < gs_Epsilon
2327 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2328 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2329 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2330 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2332 // Now interpolate as a weighted average of the four surrounding
2333 // points, where the weights are the distances to each of those points
2335 // If the point is exactly at one point of the grid of the source
2336 // image, then don't interpolate -- just assign the pixel
2338 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2340 unsigned char *p
= data
[y1
] + (3 * x1
);
2346 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2348 else if (d2
< gs_Epsilon
)
2350 unsigned char *p
= data
[y1
] + (3 * x2
);
2356 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2358 else if (d3
< gs_Epsilon
)
2360 unsigned char *p
= data
[y2
] + (3 * x2
);
2366 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2368 else if (d4
< gs_Epsilon
)
2370 unsigned char *p
= data
[y2
] + (3 * x1
);
2376 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2380 // weights for the weighted average are proportional to the inverse of the distance
2381 unsigned char *v1
= data
[y1
] + (3 * x1
);
2382 unsigned char *v2
= data
[y1
] + (3 * x2
);
2383 unsigned char *v3
= data
[y2
] + (3 * x2
);
2384 unsigned char *v4
= data
[y2
] + (3 * x1
);
2386 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2390 *(dst
++) = (unsigned char)
2391 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2392 w3
* *(v3
++) + w4
* *(v4
++)) /
2393 (w1
+ w2
+ w3
+ w4
) );
2394 *(dst
++) = (unsigned char)
2395 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2396 w3
* *(v3
++) + w4
* *(v4
++)) /
2397 (w1
+ w2
+ w3
+ w4
) );
2398 *(dst
++) = (unsigned char)
2399 ( (w1
* *v1
+ w2
* *v2
+
2400 w3
* *v3
+ w4
* *v4
) /
2401 (w1
+ w2
+ w3
+ w4
) );
2405 v1
= alpha
[y1
] + (x1
);
2406 v2
= alpha
[y1
] + (x2
);
2407 v3
= alpha
[y2
] + (x2
);
2408 v4
= alpha
[y2
] + (x1
);
2410 *(alpha_dst
++) = (unsigned char)
2411 ( (w1
* *v1
+ w2
* *v2
+
2412 w3
* *v3
+ w4
* *v4
) /
2413 (w1
+ w2
+ w3
+ w4
) );
2429 else // not interpolating
2431 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2433 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2435 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2437 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2438 const int ys
= wxCint (src
.y
); // closest integer
2440 if (0 <= xs
&& xs
< GetWidth() &&
2441 0 <= ys
&& ys
< GetHeight())
2443 unsigned char *p
= data
[ys
] + (3 * xs
);
2449 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2458 *(alpha_dst
++) = 255;
2476 // A module to allow wxImage initialization/cleanup
2477 // without calling these functions from app.cpp or from
2478 // the user's application.
2480 class wxImageModule
: public wxModule
2482 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2485 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2486 void OnExit() { wxImage::CleanUpHandlers(); };
2489 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2492 #endif // wxUSE_IMAGE