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 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
267 // also copy the alpha channel
271 unsigned char* alpha
= image
.GetAlpha();
272 memcpy( alpha
, GetAlpha(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
275 // also copy the image options
276 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
277 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
282 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
284 if( xFactor
== 1 && yFactor
== 1 )
289 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
291 // can't scale to/from 0 size
292 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
293 wxT("invalid new image size") );
295 long old_height
= M_IMGDATA
->m_height
,
296 old_width
= M_IMGDATA
->m_width
;
298 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
299 wxT("invalid old image size") );
301 long width
= old_width
/ xFactor
;
302 long height
= old_height
/ yFactor
;
304 image
.Create( width
, height
, false );
306 char unsigned *data
= image
.GetData();
308 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
310 bool hasMask
= false ;
311 unsigned char maskRed
= 0;
312 unsigned char maskGreen
= 0;
313 unsigned char maskBlue
=0 ;
315 unsigned char *source_data
= M_IMGDATA
->m_data
;
316 unsigned char *target_data
= data
;
317 unsigned char *source_alpha
= 0 ;
318 unsigned char *target_alpha
= 0 ;
319 if (M_IMGDATA
->m_hasMask
)
322 maskRed
= M_IMGDATA
->m_maskRed
;
323 maskGreen
= M_IMGDATA
->m_maskGreen
;
324 maskBlue
=M_IMGDATA
->m_maskBlue
;
326 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
327 M_IMGDATA
->m_maskGreen
,
328 M_IMGDATA
->m_maskBlue
);
332 source_alpha
= M_IMGDATA
->m_alpha
;
336 target_alpha
= image
.GetAlpha() ;
340 for (long y
= 0; y
< height
; y
++)
342 for (long x
= 0; x
< width
; x
++)
344 unsigned long avgRed
= 0 ;
345 unsigned long avgGreen
= 0;
346 unsigned long avgBlue
= 0;
347 unsigned long avgAlpha
= 0 ;
348 unsigned long counter
= 0 ;
350 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
352 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
353 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
355 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
356 unsigned char red
= pixel
[0] ;
357 unsigned char green
= pixel
[1] ;
358 unsigned char blue
= pixel
[2] ;
359 unsigned char alpha
= 255 ;
361 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
362 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
377 *(target_data
++) = M_IMGDATA
->m_maskRed
;
378 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
379 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
384 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
385 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
386 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
387 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
392 // In case this is a cursor, make sure the hotspot is scaled accordingly:
393 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
394 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
395 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
396 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
397 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
398 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
403 wxImage
wxImage::Scale( int width
, int height
) const
407 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
409 // can't scale to/from 0 size
410 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
411 wxT("invalid new image size") );
413 long old_height
= M_IMGDATA
->m_height
,
414 old_width
= M_IMGDATA
->m_width
;
415 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
416 wxT("invalid old image size") );
418 if ( old_width
% width
== 0 && old_width
>= width
&&
419 old_height
% height
== 0 && old_height
>= height
)
421 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
423 image
.Create( width
, height
, false );
425 unsigned char *data
= image
.GetData();
427 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
429 unsigned char *source_data
= M_IMGDATA
->m_data
;
430 unsigned char *target_data
= data
;
431 unsigned char *source_alpha
= 0 ;
432 unsigned char *target_alpha
= 0 ;
434 if (M_IMGDATA
->m_hasMask
)
436 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
437 M_IMGDATA
->m_maskGreen
,
438 M_IMGDATA
->m_maskBlue
);
442 source_alpha
= M_IMGDATA
->m_alpha
;
446 target_alpha
= image
.GetAlpha() ;
450 long x_delta
= (old_width
<<16) / width
;
451 long y_delta
= (old_height
<<16) / height
;
453 unsigned char* dest_pixel
= target_data
;
456 for ( long j
= 0; j
< height
; j
++ )
458 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
459 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
462 for ( long i
= 0; i
< width
; i
++ )
464 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
465 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
466 dest_pixel
[0] = src_pixel
[0];
467 dest_pixel
[1] = src_pixel
[1];
468 dest_pixel
[2] = src_pixel
[2];
471 *(target_alpha
++) = *src_alpha_pixel
;
478 // In case this is a cursor, make sure the hotspot is scaled accordingly:
479 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
480 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
481 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
482 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
483 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
484 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
489 wxImage
wxImage::Rotate90( bool clockwise
) const
493 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
495 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
497 unsigned char *data
= image
.GetData();
499 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
501 unsigned char *source_data
= M_IMGDATA
->m_data
;
502 unsigned char *target_data
;
503 unsigned char *alpha_data
= 0 ;
504 unsigned char *source_alpha
= 0 ;
505 unsigned char *target_alpha
= 0 ;
507 if (M_IMGDATA
->m_hasMask
)
509 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
513 source_alpha
= M_IMGDATA
->m_alpha
;
517 alpha_data
= image
.GetAlpha() ;
521 long height
= M_IMGDATA
->m_height
;
522 long width
= M_IMGDATA
->m_width
;
524 for (long j
= 0; j
< height
; j
++)
526 for (long i
= 0; i
< width
; i
++)
530 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
532 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
536 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
538 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
540 memcpy( target_data
, source_data
, 3 );
545 memcpy( target_alpha
, source_alpha
, 1 );
554 wxImage
wxImage::Mirror( bool horizontally
) const
558 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
560 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
562 unsigned char *data
= image
.GetData();
564 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
566 if (M_IMGDATA
->m_hasMask
)
567 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
569 long height
= M_IMGDATA
->m_height
;
570 long width
= M_IMGDATA
->m_width
;
572 unsigned char *source_data
= M_IMGDATA
->m_data
;
573 unsigned char *target_data
;
577 for (long j
= 0; j
< height
; j
++)
580 target_data
= data
-3;
581 for (long i
= 0; i
< width
; i
++)
583 memcpy( target_data
, source_data
, 3 );
591 for (long i
= 0; i
< height
; i
++)
593 target_data
= data
+ 3*width
*(height
-1-i
);
594 memcpy( target_data
, source_data
, (size_t)3*width
);
595 source_data
+= 3*width
;
602 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
606 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
608 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
609 image
, wxT("invalid subimage size") );
611 int subwidth
=rect
.GetWidth();
612 const int subheight
=rect
.GetHeight();
614 image
.Create( subwidth
, subheight
, false );
616 unsigned char *subdata
= image
.GetData(), *data
=GetData();
618 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
620 if (M_IMGDATA
->m_hasMask
)
621 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
623 const int subleft
=3*rect
.GetLeft();
624 const int width
=3*GetWidth();
627 data
+=rect
.GetTop()*width
+subleft
;
629 for (long j
= 0; j
< subheight
; ++j
)
631 memcpy( subdata
, data
, subwidth
);
639 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
640 int r_
, int g_
, int b_
) const
644 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
645 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
647 int width
= GetWidth(), height
= GetHeight();
648 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
650 unsigned char r
= (unsigned char)r_
;
651 unsigned char g
= (unsigned char)g_
;
652 unsigned char b
= (unsigned char)b_
;
653 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
655 GetOrFindMaskColour( &r
, &g
, &b
);
656 image
.SetMaskColour(r
, g
, b
);
659 image
.SetRGB(wxRect(), r
, g
, b
);
661 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
662 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
664 subRect
.Intersect(finalRect
);
666 if (!subRect
.IsEmpty())
668 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
669 image
.Paste(*this, pos
.x
, pos
.y
);
671 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
677 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
679 wxCHECK_RET( Ok(), wxT("invalid image") );
680 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
684 int width
= image
.GetWidth();
685 int height
= image
.GetHeight();
698 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
699 width
= M_IMGDATA
->m_width
- (x
+xx
);
700 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
701 height
= M_IMGDATA
->m_height
- (y
+yy
);
703 if (width
< 1) return;
704 if (height
< 1) return;
706 if ((!HasMask() && !image
.HasMask()) ||
707 (HasMask() && !image
.HasMask()) ||
708 ((HasMask() && image
.HasMask() &&
709 (GetMaskRed()==image
.GetMaskRed()) &&
710 (GetMaskGreen()==image
.GetMaskGreen()) &&
711 (GetMaskBlue()==image
.GetMaskBlue()))))
714 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
715 int source_step
= image
.GetWidth()*3;
717 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
718 int target_step
= M_IMGDATA
->m_width
*3;
719 for (int j
= 0; j
< height
; j
++)
721 memcpy( target_data
, source_data
, width
);
722 source_data
+= source_step
;
723 target_data
+= target_step
;
728 if (!HasMask() && image
.HasMask())
730 unsigned char r
= image
.GetMaskRed();
731 unsigned char g
= image
.GetMaskGreen();
732 unsigned char b
= image
.GetMaskBlue();
735 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
736 int source_step
= image
.GetWidth()*3;
738 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
739 int target_step
= M_IMGDATA
->m_width
*3;
741 for (int j
= 0; j
< height
; j
++)
743 for (int i
= 0; i
< width
; i
+=3)
745 if ((source_data
[i
] != r
) &&
746 (source_data
[i
+1] != g
) &&
747 (source_data
[i
+2] != b
))
749 memcpy( target_data
+i
, source_data
+i
, 3 );
752 source_data
+= source_step
;
753 target_data
+= target_step
;
758 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
759 unsigned char r2
, unsigned char g2
, unsigned char b2
)
761 wxCHECK_RET( Ok(), wxT("invalid image") );
763 unsigned char *data
= GetData();
765 const int w
= GetWidth();
766 const int h
= GetHeight();
768 for (int j
= 0; j
< h
; j
++)
769 for (int i
= 0; i
< w
; i
++)
771 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
781 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
785 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
787 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
789 unsigned char *dest
= image
.GetData();
791 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
793 unsigned char *src
= M_IMGDATA
->m_data
;
794 bool hasMask
= M_IMGDATA
->m_hasMask
;
795 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
796 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
797 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
800 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
802 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
803 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
805 // don't modify the mask
806 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
808 memcpy(dest
, src
, 3);
812 // calculate the luma
813 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
814 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
821 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
825 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
827 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
829 unsigned char *data
= image
.GetData();
831 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
833 if (M_IMGDATA
->m_hasMask
)
835 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
836 M_IMGDATA
->m_maskBlue
== b
)
837 image
.SetMaskColour( 255, 255, 255 );
839 image
.SetMaskColour( 0, 0, 0 );
842 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
844 unsigned char *srcd
= M_IMGDATA
->m_data
;
845 unsigned char *tard
= image
.GetData();
847 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
849 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
850 tard
[0] = tard
[1] = tard
[2] = 255;
852 tard
[0] = tard
[1] = tard
[2] = 0;
858 int wxImage::GetWidth() const
860 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
862 return M_IMGDATA
->m_width
;
865 int wxImage::GetHeight() const
867 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
869 return M_IMGDATA
->m_height
;
872 long wxImage::XYToIndex(int x
, int y
) const
876 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
878 return y
*M_IMGDATA
->m_width
+ x
;
884 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
886 long pos
= XYToIndex(x
, y
);
887 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
891 M_IMGDATA
->m_data
[ pos
] = r
;
892 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
893 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
896 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
898 wxCHECK_RET( Ok(), wxT("invalid image") );
901 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
902 if ( rect
== wxRect() )
908 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
909 imageRect
.Inside(rect
.GetBottomRight()),
910 wxT("invalid bounding rectangle") );
913 int x1
= rect
.GetLeft(),
915 x2
= rect
.GetRight() + 1,
916 y2
= rect
.GetBottom() + 1;
918 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
919 int x
, y
, width
= GetWidth();
920 for (y
= y1
; y
< y2
; y
++)
922 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
923 for (x
= x1
; x
< x2
; x
++)
932 unsigned char wxImage::GetRed( int x
, int y
) const
934 long pos
= XYToIndex(x
, y
);
935 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
939 return M_IMGDATA
->m_data
[pos
];
942 unsigned char wxImage::GetGreen( int x
, int y
) const
944 long pos
= XYToIndex(x
, y
);
945 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
949 return M_IMGDATA
->m_data
[pos
+1];
952 unsigned char wxImage::GetBlue( int x
, int y
) const
954 long pos
= XYToIndex(x
, y
);
955 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
959 return M_IMGDATA
->m_data
[pos
+2];
962 bool wxImage::Ok() const
964 // image of 0 width or height can't be considered ok - at least because it
965 // causes crashes in ConvertToBitmap() if we don't catch it in time
966 wxImageRefData
*data
= M_IMGDATA
;
967 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
970 unsigned char *wxImage::GetData() const
972 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
974 return M_IMGDATA
->m_data
;
977 void wxImage::SetData( unsigned char *data
, bool static_data
)
979 wxCHECK_RET( Ok(), wxT("invalid image") );
981 wxImageRefData
*newRefData
= new wxImageRefData();
983 newRefData
->m_width
= M_IMGDATA
->m_width
;
984 newRefData
->m_height
= M_IMGDATA
->m_height
;
985 newRefData
->m_data
= data
;
986 newRefData
->m_ok
= true;
987 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
988 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
989 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
990 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
991 newRefData
->m_static
= static_data
;
995 m_refData
= newRefData
;
998 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1000 wxImageRefData
*newRefData
= new wxImageRefData();
1004 newRefData
->m_width
= new_width
;
1005 newRefData
->m_height
= new_height
;
1006 newRefData
->m_data
= data
;
1007 newRefData
->m_ok
= true;
1008 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1009 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1010 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1011 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1015 newRefData
->m_width
= new_width
;
1016 newRefData
->m_height
= new_height
;
1017 newRefData
->m_data
= data
;
1018 newRefData
->m_ok
= true;
1020 newRefData
->m_static
= static_data
;
1024 m_refData
= newRefData
;
1027 // ----------------------------------------------------------------------------
1028 // alpha channel support
1029 // ----------------------------------------------------------------------------
1031 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1033 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1035 long pos
= XYToIndex(x
, y
);
1036 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1038 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1041 unsigned char wxImage::GetAlpha(int x
, int y
) const
1043 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1045 long pos
= XYToIndex(x
, y
);
1046 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1048 return M_IMGDATA
->m_alpha
[pos
];
1052 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1056 const int w
= M_IMGDATA
->m_width
;
1057 const int h
= M_IMGDATA
->m_height
;
1059 unsigned char *alpha
= GetAlpha();
1060 unsigned char *data
= GetData();
1062 for ( int y
= 0; y
< h
; y
++ )
1064 for ( int x
= 0; x
< w
; x
++ )
1076 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1078 wxCHECK_RET( Ok(), wxT("invalid image") );
1082 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1085 free(M_IMGDATA
->m_alpha
);
1086 M_IMGDATA
->m_alpha
= alpha
;
1087 M_IMGDATA
->m_staticAlpha
= static_data
;
1090 unsigned char *wxImage::GetAlpha() const
1092 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1094 return M_IMGDATA
->m_alpha
;
1097 void wxImage::InitAlpha()
1099 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1101 // initialize memory for alpha channel
1104 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1105 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1109 // use the mask to initialize the alpha channel.
1110 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1112 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1113 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1114 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1115 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1119 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1120 ? wxIMAGE_ALPHA_TRANSPARENT
1121 : wxIMAGE_ALPHA_OPAQUE
;
1124 M_IMGDATA
->m_hasMask
= false;
1128 // make the image fully opaque
1129 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1133 // ----------------------------------------------------------------------------
1135 // ----------------------------------------------------------------------------
1137 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1139 wxCHECK_RET( Ok(), wxT("invalid image") );
1141 M_IMGDATA
->m_maskRed
= r
;
1142 M_IMGDATA
->m_maskGreen
= g
;
1143 M_IMGDATA
->m_maskBlue
= b
;
1144 M_IMGDATA
->m_hasMask
= true;
1147 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1149 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1151 if (M_IMGDATA
->m_hasMask
)
1153 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1154 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1155 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1160 FindFirstUnusedColour(r
, g
, b
);
1165 unsigned char wxImage::GetMaskRed() const
1167 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1169 return M_IMGDATA
->m_maskRed
;
1172 unsigned char wxImage::GetMaskGreen() const
1174 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1176 return M_IMGDATA
->m_maskGreen
;
1179 unsigned char wxImage::GetMaskBlue() const
1181 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1183 return M_IMGDATA
->m_maskBlue
;
1186 void wxImage::SetMask( bool mask
)
1188 wxCHECK_RET( Ok(), wxT("invalid image") );
1190 M_IMGDATA
->m_hasMask
= mask
;
1193 bool wxImage::HasMask() const
1195 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1197 return M_IMGDATA
->m_hasMask
;
1200 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1202 long pos
= XYToIndex(x
, y
);
1203 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1206 if ( M_IMGDATA
->m_hasMask
)
1208 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1209 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1210 p
[1] == M_IMGDATA
->m_maskGreen
&&
1211 p
[2] == M_IMGDATA
->m_maskBlue
)
1218 if ( M_IMGDATA
->m_alpha
)
1220 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1222 // transparent enough
1231 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1232 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1234 // check that the images are the same size
1235 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1237 wxLogError( _("Image and mask have different sizes.") );
1241 // find unused colour
1242 unsigned char r
,g
,b
;
1243 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1245 wxLogError( _("No unused colour in image being masked.") );
1249 unsigned char *imgdata
= GetData();
1250 unsigned char *maskdata
= mask
.GetData();
1252 const int w
= GetWidth();
1253 const int h
= GetHeight();
1255 for (int j
= 0; j
< h
; j
++)
1257 for (int i
= 0; i
< w
; i
++)
1259 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1270 SetMaskColour(r
, g
, b
);
1276 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1281 unsigned char mr
, mg
, mb
;
1282 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1284 wxLogError( _("No unused colour in image being masked.") );
1289 SetMaskColour(mr
, mg
, mb
);
1291 unsigned char *imgdata
= GetData();
1292 unsigned char *alphadata
= GetAlpha();
1295 int h
= GetHeight();
1297 for (int y
= 0; y
< h
; y
++)
1299 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1301 if (*alphadata
< threshold
)
1310 free(M_IMGDATA
->m_alpha
);
1311 M_IMGDATA
->m_alpha
= NULL
;
1316 // ----------------------------------------------------------------------------
1317 // Palette functions
1318 // ----------------------------------------------------------------------------
1322 bool wxImage::HasPalette() const
1327 return M_IMGDATA
->m_palette
.Ok();
1330 const wxPalette
& wxImage::GetPalette() const
1332 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1334 return M_IMGDATA
->m_palette
;
1337 void wxImage::SetPalette(const wxPalette
& palette
)
1339 wxCHECK_RET( Ok(), wxT("invalid image") );
1341 M_IMGDATA
->m_palette
= palette
;
1344 #endif // wxUSE_PALETTE
1346 // ----------------------------------------------------------------------------
1347 // Option functions (arbitrary name/value mapping)
1348 // ----------------------------------------------------------------------------
1350 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1352 wxCHECK_RET( Ok(), wxT("invalid image") );
1354 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1355 if (idx
== wxNOT_FOUND
)
1357 M_IMGDATA
->m_optionNames
.Add(name
);
1358 M_IMGDATA
->m_optionValues
.Add(value
);
1362 M_IMGDATA
->m_optionNames
[idx
] = name
;
1363 M_IMGDATA
->m_optionValues
[idx
] = value
;
1367 void wxImage::SetOption(const wxString
& name
, int value
)
1370 valStr
.Printf(wxT("%d"), value
);
1371 SetOption(name
, valStr
);
1374 wxString
wxImage::GetOption(const wxString
& name
) const
1376 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1378 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1379 if (idx
== wxNOT_FOUND
)
1380 return wxEmptyString
;
1382 return M_IMGDATA
->m_optionValues
[idx
];
1385 int wxImage::GetOptionInt(const wxString
& name
) const
1387 return wxAtoi(GetOption(name
));
1390 bool wxImage::HasOption(const wxString
& name
) const
1392 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1394 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1397 // ----------------------------------------------------------------------------
1399 // ----------------------------------------------------------------------------
1401 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1404 if (wxFileExists(filename
))
1406 wxFileInputStream
stream(filename
);
1407 wxBufferedInputStream
bstream( stream
);
1408 return LoadFile(bstream
, type
, index
);
1412 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1416 #else // !wxUSE_STREAMS
1418 #endif // wxUSE_STREAMS
1421 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1424 if (wxFileExists(filename
))
1426 wxFileInputStream
stream(filename
);
1427 wxBufferedInputStream
bstream( stream
);
1428 return LoadFile(bstream
, mimetype
, index
);
1432 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1436 #else // !wxUSE_STREAMS
1438 #endif // wxUSE_STREAMS
1443 bool wxImage::SaveFile( const wxString
& filename
) const
1445 wxString ext
= filename
.AfterLast('.').Lower();
1447 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1450 SaveFile(filename
, pHandler
->GetType());
1454 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1459 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1462 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1464 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1466 wxFileOutputStream
stream(filename
);
1468 if ( stream
.IsOk() )
1470 wxBufferedOutputStream
bstream( stream
);
1471 return SaveFile(bstream
, type
);
1473 #endif // wxUSE_STREAMS
1478 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1481 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1483 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1485 wxFileOutputStream
stream(filename
);
1487 if ( stream
.IsOk() )
1489 wxBufferedOutputStream
bstream( stream
);
1490 return SaveFile(bstream
, mimetype
);
1492 #endif // wxUSE_STREAMS
1497 bool wxImage::CanRead( const wxString
&name
)
1500 wxFileInputStream
stream(name
);
1501 return CanRead(stream
);
1507 int wxImage::GetImageCount( const wxString
&name
, long type
)
1510 wxFileInputStream
stream(name
);
1512 return GetImageCount(stream
, type
);
1520 bool wxImage::CanRead( wxInputStream
&stream
)
1522 const wxList
& list
= GetHandlers();
1524 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1526 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1527 if (handler
->CanRead( stream
))
1534 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1536 wxImageHandler
*handler
;
1538 if ( type
== wxBITMAP_TYPE_ANY
)
1540 wxList
&list
=GetHandlers();
1542 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1544 handler
=(wxImageHandler
*)node
->GetData();
1545 if ( handler
->CanRead(stream
) )
1546 return handler
->GetImageCount(stream
);
1550 wxLogWarning(_("No handler found for image type."));
1554 handler
= FindHandler(type
);
1558 wxLogWarning(_("No image handler for type %d defined."), type
);
1562 if ( handler
->CanRead(stream
) )
1564 return handler
->GetImageCount(stream
);
1568 wxLogError(_("Image file is not of type %d."), type
);
1573 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1577 m_refData
= new wxImageRefData
;
1579 wxImageHandler
*handler
;
1581 if ( type
== wxBITMAP_TYPE_ANY
)
1583 wxList
&list
=GetHandlers();
1585 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1587 handler
=(wxImageHandler
*)node
->GetData();
1588 if ( handler
->CanRead(stream
) )
1589 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1593 wxLogWarning( _("No handler found for image type.") );
1597 handler
= FindHandler(type
);
1601 wxLogWarning( _("No image handler for type %d defined."), type
);
1606 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1608 wxLogError(_("Image file is not of type %d."), type
);
1612 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1615 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1619 m_refData
= new wxImageRefData
;
1621 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1625 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1630 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1632 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1636 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1639 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1641 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1643 wxImageHandler
*handler
= FindHandler(type
);
1646 wxLogWarning( _("No image handler for type %d defined."), type
);
1651 return handler
->SaveFile( (wxImage
*)this, stream
);
1654 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1656 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1658 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1661 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1666 return handler
->SaveFile( (wxImage
*)this, stream
);
1668 #endif // wxUSE_STREAMS
1670 // ----------------------------------------------------------------------------
1671 // image I/O handlers
1672 // ----------------------------------------------------------------------------
1674 void wxImage::AddHandler( wxImageHandler
*handler
)
1676 // Check for an existing handler of the type being added.
1677 if (FindHandler( handler
->GetType() ) == 0)
1679 sm_handlers
.Append( handler
);
1683 // This is not documented behaviour, merely the simplest 'fix'
1684 // for preventing duplicate additions. If someone ever has
1685 // a good reason to add and remove duplicate handlers (and they
1686 // may) we should probably refcount the duplicates.
1687 // also an issue in InsertHandler below.
1689 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1690 handler
->GetName().c_str() );
1695 void wxImage::InsertHandler( wxImageHandler
*handler
)
1697 // Check for an existing handler of the type being added.
1698 if (FindHandler( handler
->GetType() ) == 0)
1700 sm_handlers
.Insert( handler
);
1704 // see AddHandler for additional comments.
1705 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1706 handler
->GetName().c_str() );
1711 bool wxImage::RemoveHandler( const wxString
& name
)
1713 wxImageHandler
*handler
= FindHandler(name
);
1716 sm_handlers
.DeleteObject(handler
);
1724 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1726 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1729 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1730 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1732 node
= node
->GetNext();
1737 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1739 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1742 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1743 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1744 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1746 node
= node
->GetNext();
1751 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1753 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1756 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1757 if (handler
->GetType() == bitmapType
) return handler
;
1758 node
= node
->GetNext();
1763 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1765 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1768 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1769 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1770 node
= node
->GetNext();
1775 void wxImage::InitStandardHandlers()
1778 AddHandler(new wxBMPHandler
);
1779 #endif // wxUSE_STREAMS
1782 void wxImage::CleanUpHandlers()
1784 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1787 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1788 wxList::compatibility_iterator next
= node
->GetNext();
1793 sm_handlers
.Clear();
1796 wxString
wxImage::GetImageExtWildcard()
1800 wxList
& Handlers
= wxImage::GetHandlers();
1801 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1804 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1805 fmts
+= wxT("*.") + Handler
->GetExtension();
1806 Node
= Node
->GetNext();
1807 if ( Node
) fmts
+= wxT(";");
1810 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1813 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1815 const double red
= rgb
.red
/ 255.0,
1816 green
= rgb
.green
/ 255.0,
1817 blue
= rgb
.blue
/ 255.0;
1819 // find the min and max intensity (and remember which one was it for the
1821 double minimumRGB
= red
;
1822 if ( green
< minimumRGB
)
1824 if ( blue
< minimumRGB
)
1827 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1828 double maximumRGB
= red
;
1829 if ( green
> maximumRGB
)
1834 if ( blue
> maximumRGB
)
1840 const double value
= maximumRGB
;
1842 double hue
= 0.0, saturation
;
1843 const double deltaRGB
= maximumRGB
- minimumRGB
;
1844 if ( wxIsNullDouble(deltaRGB
) )
1846 // Gray has no color
1855 hue
= (green
- blue
) / deltaRGB
;
1859 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1863 hue
= 4.0 + (red
- green
) / deltaRGB
;
1867 wxFAIL_MSG(wxT("hue not specified"));
1876 saturation
= deltaRGB
/ maximumRGB
;
1879 return HSVValue(hue
, saturation
, value
);
1882 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1884 double red
, green
, blue
;
1886 if ( wxIsNullDouble(hsv
.saturation
) )
1895 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1896 int i
= (int)floor(hue
);
1897 double f
= hue
- i
; // fractional part of h
1898 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1904 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1909 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1917 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1922 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1927 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1935 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1940 return RGBValue((unsigned char)(red
* 255.0),
1941 (unsigned char)(green
* 255.0),
1942 (unsigned char)(blue
* 255.0));
1946 * Rotates the hue of each pixel of the image. angle is a double in the range
1947 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1949 void wxImage::RotateHue(double angle
)
1951 unsigned char *srcBytePtr
;
1952 unsigned char *dstBytePtr
;
1953 unsigned long count
;
1954 wxImage::HSVValue hsv
;
1955 wxImage::RGBValue rgb
;
1957 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
1958 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1959 if ( count
> 0 && !wxIsNullDouble(angle
) )
1961 srcBytePtr
= M_IMGDATA
->m_data
;
1962 dstBytePtr
= srcBytePtr
;
1965 rgb
.red
= *srcBytePtr
++;
1966 rgb
.green
= *srcBytePtr
++;
1967 rgb
.blue
= *srcBytePtr
++;
1968 hsv
= RGBtoHSV(rgb
);
1970 hsv
.hue
= hsv
.hue
+ angle
;
1972 hsv
.hue
= hsv
.hue
- 1.0;
1973 else if (hsv
.hue
< 0.0)
1974 hsv
.hue
= hsv
.hue
+ 1.0;
1976 rgb
= HSVtoRGB(hsv
);
1977 *dstBytePtr
++ = rgb
.red
;
1978 *dstBytePtr
++ = rgb
.green
;
1979 *dstBytePtr
++ = rgb
.blue
;
1980 } while (--count
!= 0);
1984 //-----------------------------------------------------------------------------
1986 //-----------------------------------------------------------------------------
1988 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1991 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1996 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2001 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2006 bool wxImageHandler::CanRead( const wxString
& name
)
2008 if (wxFileExists(name
))
2010 wxFileInputStream
stream(name
);
2011 return CanRead(stream
);
2014 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2019 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2021 wxFileOffset posOld
= stream
.TellI();
2022 if ( posOld
== wxInvalidOffset
)
2024 // can't test unseekable stream
2028 bool ok
= DoCanRead(stream
);
2030 // restore the old position to be able to test other formats and so on
2031 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2033 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2035 // reading would fail anyhow as we're not at the right position
2042 #endif // wxUSE_STREAMS
2044 // ----------------------------------------------------------------------------
2045 // image histogram stuff
2046 // ----------------------------------------------------------------------------
2049 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2054 unsigned char g2
) const
2056 unsigned long key
= MakeKey(r2
, g2
, b2
);
2058 while ( find(key
) != end() )
2060 // color already used
2072 wxLogError(_("No unused colour in image.") );
2078 key
= MakeKey(r2
, g2
, b2
);
2092 wxImage::FindFirstUnusedColour(unsigned char *r
,
2097 unsigned char g2
) const
2099 wxImageHistogram histogram
;
2101 ComputeHistogram(histogram
);
2103 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2109 // Counts and returns the number of different colours. Optionally stops
2110 // when it exceeds 'stopafter' different colours. This is useful, for
2111 // example, to see if the image can be saved as 8-bit (256 colour or
2112 // less, in this case it would be invoked as CountColours(256)). Default
2113 // value for stopafter is -1 (don't care).
2115 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2119 unsigned char r
, g
, b
;
2121 unsigned long size
, nentries
, key
;
2124 size
= GetWidth() * GetHeight();
2127 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2132 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2134 if (h
.Get(key
) == NULL
)
2145 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2147 unsigned char *p
= GetData();
2148 unsigned long nentries
= 0;
2152 const unsigned long size
= GetWidth() * GetHeight();
2154 unsigned char r
, g
, b
;
2155 for ( unsigned long n
= 0; n
< size
; n
++ )
2161 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2163 if ( entry
.value
++ == 0 )
2164 entry
.index
= nentries
++;
2171 * Rotation code by Carlos Moreno
2174 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2175 // does exactly the same thing. And I also got rid of wxRotationPixel
2176 // bacause of potential problems in architectures where alignment
2177 // is an issue, so I had to rewrite parts of the code.
2179 static const double gs_Epsilon
= 1e-10;
2181 static inline int wxCint (double x
)
2183 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2187 // Auxiliary function to rotate a point (x,y) with respect to point p0
2188 // make it inline and use a straight return to facilitate optimization
2189 // also, the function receives the sine and cosine of the angle to avoid
2190 // repeating the time-consuming calls to these functions -- sin/cos can
2191 // be computed and stored in the calling function.
2193 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2195 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2196 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2199 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2201 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2204 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2207 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2209 bool has_alpha
= HasAlpha();
2211 // Create pointer-based array to accelerate access to wxImage's data
2212 unsigned char ** data
= new unsigned char * [GetHeight()];
2213 data
[0] = GetData();
2214 for (i
= 1; i
< GetHeight(); i
++)
2215 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2217 // Same for alpha channel
2218 unsigned char ** alpha
= NULL
;
2221 alpha
= new unsigned char * [GetHeight()];
2222 alpha
[0] = GetAlpha();
2223 for (i
= 1; i
< GetHeight(); i
++)
2224 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2227 // precompute coefficients for rotation formula
2228 // (sine and cosine of the angle)
2229 const double cos_angle
= cos(angle
);
2230 const double sin_angle
= sin(angle
);
2232 // Create new Image to store the result
2233 // First, find rectangle that covers the rotated image; to do that,
2234 // rotate the four corners
2236 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2238 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2239 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2240 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2241 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2243 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2244 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2245 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2246 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2248 // Create rotated image
2249 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2250 // With alpha channel
2254 if (offset_after_rotation
!= NULL
)
2256 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2259 // GRG: The rotated (destination) image is always accessed
2260 // sequentially, so there is no need for a pointer-based
2261 // array here (and in fact it would be slower).
2263 unsigned char * dst
= rotated
.GetData();
2265 unsigned char * alpha_dst
= NULL
;
2267 alpha_dst
= rotated
.GetAlpha();
2269 // GRG: if the original image has a mask, use its RGB values
2270 // as the blank pixel, else, fall back to default (black).
2272 unsigned char blank_r
= 0;
2273 unsigned char blank_g
= 0;
2274 unsigned char blank_b
= 0;
2278 blank_r
= GetMaskRed();
2279 blank_g
= GetMaskGreen();
2280 blank_b
= GetMaskBlue();
2281 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2284 // Now, for each point of the rotated image, find where it came from, by
2285 // performing an inverse rotation (a rotation of -angle) and getting the
2286 // pixel at those coordinates
2288 // GRG: I've taken the (interpolating) test out of the loops, so that
2289 // it is done only once, instead of repeating it for each pixel.
2294 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2296 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2298 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2300 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2301 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2303 // interpolate using the 4 enclosing grid-points. Those
2304 // points can be obtained using floor and ceiling of the
2305 // exact coordinates of the point
2308 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2310 x1
= wxCint(floor(src
.x
));
2311 x2
= wxCint(ceil(src
.x
));
2313 else // else means that x is near one of the borders (0 or width-1)
2315 x1
= x2
= wxCint (src
.x
);
2318 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2320 y1
= wxCint(floor(src
.y
));
2321 y2
= wxCint(ceil(src
.y
));
2325 y1
= y2
= wxCint (src
.y
);
2328 // get four points and the distances (square of the distance,
2329 // for efficiency reasons) for the interpolation formula
2331 // GRG: Do not calculate the points until they are
2332 // really needed -- this way we can calculate
2333 // just one, instead of four, if d1, d2, d3
2334 // or d4 are < gs_Epsilon
2336 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2337 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2338 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2339 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2341 // Now interpolate as a weighted average of the four surrounding
2342 // points, where the weights are the distances to each of those points
2344 // If the point is exactly at one point of the grid of the source
2345 // image, then don't interpolate -- just assign the pixel
2347 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2349 unsigned char *p
= data
[y1
] + (3 * x1
);
2355 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2357 else if (d2
< gs_Epsilon
)
2359 unsigned char *p
= data
[y1
] + (3 * x2
);
2365 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2367 else if (d3
< gs_Epsilon
)
2369 unsigned char *p
= data
[y2
] + (3 * x2
);
2375 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2377 else if (d4
< gs_Epsilon
)
2379 unsigned char *p
= data
[y2
] + (3 * x1
);
2385 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2389 // weights for the weighted average are proportional to the inverse of the distance
2390 unsigned char *v1
= data
[y1
] + (3 * x1
);
2391 unsigned char *v2
= data
[y1
] + (3 * x2
);
2392 unsigned char *v3
= data
[y2
] + (3 * x2
);
2393 unsigned char *v4
= data
[y2
] + (3 * x1
);
2395 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2399 *(dst
++) = (unsigned char)
2400 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2401 w3
* *(v3
++) + w4
* *(v4
++)) /
2402 (w1
+ w2
+ w3
+ w4
) );
2403 *(dst
++) = (unsigned char)
2404 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2405 w3
* *(v3
++) + w4
* *(v4
++)) /
2406 (w1
+ w2
+ w3
+ w4
) );
2407 *(dst
++) = (unsigned char)
2408 ( (w1
* *v1
+ w2
* *v2
+
2409 w3
* *v3
+ w4
* *v4
) /
2410 (w1
+ w2
+ w3
+ w4
) );
2414 v1
= alpha
[y1
] + (x1
);
2415 v2
= alpha
[y1
] + (x2
);
2416 v3
= alpha
[y2
] + (x2
);
2417 v4
= alpha
[y2
] + (x1
);
2419 *(alpha_dst
++) = (unsigned char)
2420 ( (w1
* *v1
+ w2
* *v2
+
2421 w3
* *v3
+ w4
* *v4
) /
2422 (w1
+ w2
+ w3
+ w4
) );
2438 else // not interpolating
2440 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2442 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2444 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2446 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2447 const int ys
= wxCint (src
.y
); // closest integer
2449 if (0 <= xs
&& xs
< GetWidth() &&
2450 0 <= ys
&& ys
< GetHeight())
2452 unsigned char *p
= data
[ys
] + (3 * xs
);
2458 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2467 *(alpha_dst
++) = 255;
2485 // A module to allow wxImage initialization/cleanup
2486 // without calling these functions from app.cpp or from
2487 // the user's application.
2489 class wxImageModule
: public wxModule
2491 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2494 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2495 void OnExit() { wxImage::CleanUpHandlers(); };
2498 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2501 #endif // wxUSE_IMAGE