1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
39 #include "wx/xpmdecod.h"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxImageRefData
: public wxObjectRefData
53 virtual ~wxImageRefData();
57 unsigned char *m_data
;
60 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 // alpha channel data, may be NULL for the formats without alpha support
63 unsigned char *m_alpha
;
67 // if true, m_data is pointer to static data and shouldn't be freed
70 // same as m_static but for m_alpha
75 #endif // wxUSE_PALETTE
77 wxArrayString m_optionNames
;
78 wxArrayString m_optionValues
;
80 DECLARE_NO_COPY_CLASS(wxImageRefData
)
83 wxImageRefData::wxImageRefData()
88 m_alpha
= (unsigned char *) NULL
;
97 m_staticAlpha
= false;
100 wxImageRefData::~wxImageRefData()
104 if ( !m_staticAlpha
)
108 wxList
wxImage::sm_handlers
;
112 //-----------------------------------------------------------------------------
114 #define M_IMGDATA ((wxImageRefData *)m_refData)
116 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
118 wxImage::wxImage( int width
, int height
, bool clear
)
120 Create( width
, height
, clear
);
123 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
125 Create( width
, height
, data
, static_data
);
128 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
130 Create( width
, height
, data
, alpha
, static_data
);
133 wxImage::wxImage( const wxString
& name
, long type
, int index
)
135 LoadFile( name
, type
, index
);
138 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
140 LoadFile( name
, mimetype
, index
);
144 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
146 LoadFile( stream
, type
, index
);
149 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
151 LoadFile( stream
, mimetype
, index
);
153 #endif // wxUSE_STREAMS
155 wxImage::wxImage( const wxImage
& image
)
161 wxImage::wxImage( const wxImage
* image
)
163 if (image
) Ref(*image
);
166 wxImage::wxImage( const char** xpmData
)
171 wxImage::wxImage( char** xpmData
)
173 Create((const char**) xpmData
);
176 bool wxImage::Create( const char** xpmData
)
181 wxXPMDecoder decoder
;
182 (*this) = decoder
.ReadData(xpmData
);
189 bool wxImage::Create( int width
, int height
, bool clear
)
193 m_refData
= new wxImageRefData();
195 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
196 if (!M_IMGDATA
->m_data
)
203 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
205 M_IMGDATA
->m_width
= width
;
206 M_IMGDATA
->m_height
= height
;
207 M_IMGDATA
->m_ok
= true;
212 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
216 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
218 m_refData
= new wxImageRefData();
220 M_IMGDATA
->m_data
= data
;
221 M_IMGDATA
->m_width
= width
;
222 M_IMGDATA
->m_height
= height
;
223 M_IMGDATA
->m_ok
= true;
224 M_IMGDATA
->m_static
= static_data
;
229 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
233 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
235 m_refData
= new wxImageRefData();
237 M_IMGDATA
->m_data
= data
;
238 M_IMGDATA
->m_alpha
= alpha
;
239 M_IMGDATA
->m_width
= width
;
240 M_IMGDATA
->m_height
= height
;
241 M_IMGDATA
->m_ok
= true;
242 M_IMGDATA
->m_static
= static_data
;
247 void wxImage::Destroy()
252 wxImage
wxImage::Copy() const
256 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
258 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
260 unsigned char *data
= image
.GetData();
262 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
264 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
265 image
.SetMask( M_IMGDATA
->m_hasMask
);
267 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
269 // also copy the image options
270 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
271 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
272 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
277 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
279 if( xFactor
== 1 && yFactor
== 1 )
284 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
286 // can't scale to/from 0 size
287 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
288 wxT("invalid new image size") );
290 long old_height
= M_IMGDATA
->m_height
,
291 old_width
= M_IMGDATA
->m_width
;
293 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
294 wxT("invalid old image size") );
296 long width
= old_width
/ xFactor
;
297 long height
= old_height
/ yFactor
;
299 image
.Create( width
, height
, false );
301 char unsigned *data
= image
.GetData();
303 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
305 bool hasMask
= false ;
306 unsigned char maskRed
= 0;
307 unsigned char maskGreen
= 0;
308 unsigned char maskBlue
=0 ;
310 unsigned char *source_data
= M_IMGDATA
->m_data
;
311 unsigned char *target_data
= data
;
312 unsigned char *source_alpha
= 0 ;
313 unsigned char *target_alpha
= 0 ;
314 if (M_IMGDATA
->m_hasMask
)
317 maskRed
= M_IMGDATA
->m_maskRed
;
318 maskGreen
= M_IMGDATA
->m_maskGreen
;
319 maskBlue
=M_IMGDATA
->m_maskBlue
;
321 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
322 M_IMGDATA
->m_maskGreen
,
323 M_IMGDATA
->m_maskBlue
);
327 source_alpha
= M_IMGDATA
->m_alpha
;
331 target_alpha
= image
.GetAlpha() ;
335 for (long y
= 0; y
< height
; y
++)
337 for (long x
= 0; x
< width
; x
++)
339 unsigned long avgRed
= 0 ;
340 unsigned long avgGreen
= 0;
341 unsigned long avgBlue
= 0;
342 unsigned long avgAlpha
= 0 ;
343 unsigned long counter
= 0 ;
345 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
347 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
348 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
350 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
351 unsigned char red
= pixel
[0] ;
352 unsigned char green
= pixel
[1] ;
353 unsigned char blue
= pixel
[2] ;
354 unsigned char alpha
= 255 ;
356 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
357 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
372 *(target_data
++) = M_IMGDATA
->m_maskRed
;
373 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
374 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
379 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
380 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
381 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
382 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
387 // In case this is a cursor, make sure the hotspot is scalled accordingly:
388 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
389 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
390 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
391 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
392 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
393 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
398 wxImage
wxImage::Scale( int width
, int height
) const
402 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
404 // can't scale to/from 0 size
405 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
406 wxT("invalid new image size") );
408 long old_height
= M_IMGDATA
->m_height
,
409 old_width
= M_IMGDATA
->m_width
;
410 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
411 wxT("invalid old image size") );
413 if ( old_width
% width
== 0 && old_width
>= width
&&
414 old_height
% height
== 0 && old_height
>= height
)
416 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
418 image
.Create( width
, height
, false );
420 unsigned char *data
= image
.GetData();
422 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
424 unsigned char *source_data
= M_IMGDATA
->m_data
;
425 unsigned char *target_data
= data
;
426 unsigned char *source_alpha
= 0 ;
427 unsigned char *target_alpha
= 0 ;
429 if (M_IMGDATA
->m_hasMask
)
431 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
432 M_IMGDATA
->m_maskGreen
,
433 M_IMGDATA
->m_maskBlue
);
437 source_alpha
= M_IMGDATA
->m_alpha
;
441 target_alpha
= image
.GetAlpha() ;
445 long x_delta
= (old_width
<<16) / width
;
446 long y_delta
= (old_height
<<16) / height
;
448 unsigned char* dest_pixel
= target_data
;
451 for ( long j
= 0; j
< height
; j
++ )
453 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
454 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
457 for ( long i
= 0; i
< width
; i
++ )
459 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
460 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
461 dest_pixel
[0] = src_pixel
[0];
462 dest_pixel
[1] = src_pixel
[1];
463 dest_pixel
[2] = src_pixel
[2];
466 *(target_alpha
++) = *src_alpha_pixel
;
473 // In case this is a cursor, make sure the hotspot is scalled accordingly:
474 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
475 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
476 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
477 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
478 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
479 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
484 wxImage
wxImage::Rotate90( bool clockwise
) const
488 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
490 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
492 unsigned char *data
= image
.GetData();
494 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
496 unsigned char *source_data
= M_IMGDATA
->m_data
;
497 unsigned char *target_data
;
498 unsigned char *alpha_data
= 0 ;
499 unsigned char *source_alpha
= 0 ;
500 unsigned char *target_alpha
= 0 ;
502 if (M_IMGDATA
->m_hasMask
)
504 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
508 source_alpha
= M_IMGDATA
->m_alpha
;
512 alpha_data
= image
.GetAlpha() ;
516 long height
= M_IMGDATA
->m_height
;
517 long width
= M_IMGDATA
->m_width
;
519 for (long j
= 0; j
< height
; j
++)
521 for (long i
= 0; i
< width
; i
++)
525 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
527 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
531 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
533 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
535 memcpy( target_data
, source_data
, 3 );
540 memcpy( target_alpha
, source_alpha
, 1 );
549 wxImage
wxImage::Mirror( bool horizontally
) const
553 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
555 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
557 unsigned char *data
= image
.GetData();
559 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
561 if (M_IMGDATA
->m_hasMask
)
562 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
564 long height
= M_IMGDATA
->m_height
;
565 long width
= M_IMGDATA
->m_width
;
567 unsigned char *source_data
= M_IMGDATA
->m_data
;
568 unsigned char *target_data
;
572 for (long j
= 0; j
< height
; j
++)
575 target_data
= data
-3;
576 for (long i
= 0; i
< width
; i
++)
578 memcpy( target_data
, source_data
, 3 );
586 for (long i
= 0; i
< height
; i
++)
588 target_data
= data
+ 3*width
*(height
-1-i
);
589 memcpy( target_data
, source_data
, (size_t)3*width
);
590 source_data
+= 3*width
;
597 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
601 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
603 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
604 image
, wxT("invalid subimage size") );
606 int subwidth
=rect
.GetWidth();
607 const int subheight
=rect
.GetHeight();
609 image
.Create( subwidth
, subheight
, false );
611 unsigned char *subdata
= image
.GetData(), *data
=GetData();
613 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
615 if (M_IMGDATA
->m_hasMask
)
616 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
618 const int subleft
=3*rect
.GetLeft();
619 const int width
=3*GetWidth();
622 data
+=rect
.GetTop()*width
+subleft
;
624 for (long j
= 0; j
< subheight
; ++j
)
626 memcpy( subdata
, data
, subwidth
);
634 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
635 int r_
, int g_
, int b_
) const
639 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
640 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
642 int width
= GetWidth(), height
= GetHeight();
643 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
645 unsigned char r
= (unsigned char)r_
;
646 unsigned char g
= (unsigned char)g_
;
647 unsigned char b
= (unsigned char)b_
;
648 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
650 GetOrFindMaskColour( &r
, &g
, &b
);
651 image
.SetMaskColour(r
, g
, b
);
654 image
.SetRGB(wxRect(), r
, g
, b
);
656 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
657 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
659 subRect
.Intersect(finalRect
);
661 if (!subRect
.IsEmpty())
663 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
664 image
.Paste(*this, pos
.x
, pos
.y
);
666 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
672 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
674 wxCHECK_RET( Ok(), wxT("invalid image") );
675 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
679 int width
= image
.GetWidth();
680 int height
= image
.GetHeight();
693 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
694 width
= M_IMGDATA
->m_width
- (x
+xx
);
695 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
696 height
= M_IMGDATA
->m_height
- (y
+yy
);
698 if (width
< 1) return;
699 if (height
< 1) return;
701 if ((!HasMask() && !image
.HasMask()) ||
702 (HasMask() && !image
.HasMask()) ||
703 ((HasMask() && image
.HasMask() &&
704 (GetMaskRed()==image
.GetMaskRed()) &&
705 (GetMaskGreen()==image
.GetMaskGreen()) &&
706 (GetMaskBlue()==image
.GetMaskBlue()))))
709 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
710 int source_step
= image
.GetWidth()*3;
712 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
713 int target_step
= M_IMGDATA
->m_width
*3;
714 for (int j
= 0; j
< height
; j
++)
716 memcpy( target_data
, source_data
, width
);
717 source_data
+= source_step
;
718 target_data
+= target_step
;
723 if (!HasMask() && image
.HasMask())
725 unsigned char r
= image
.GetMaskRed();
726 unsigned char g
= image
.GetMaskGreen();
727 unsigned char b
= image
.GetMaskBlue();
730 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
731 int source_step
= image
.GetWidth()*3;
733 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
734 int target_step
= M_IMGDATA
->m_width
*3;
736 for (int j
= 0; j
< height
; j
++)
738 for (int i
= 0; i
< width
; i
+=3)
740 if ((source_data
[i
] != r
) &&
741 (source_data
[i
+1] != g
) &&
742 (source_data
[i
+2] != b
))
744 memcpy( target_data
+i
, source_data
+i
, 3 );
747 source_data
+= source_step
;
748 target_data
+= target_step
;
753 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
754 unsigned char r2
, unsigned char g2
, unsigned char b2
)
756 wxCHECK_RET( Ok(), wxT("invalid image") );
758 unsigned char *data
= GetData();
760 const int w
= GetWidth();
761 const int h
= GetHeight();
763 for (int j
= 0; j
< h
; j
++)
764 for (int i
= 0; i
< w
; i
++)
766 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
776 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
780 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
782 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
784 unsigned char *data
= image
.GetData();
786 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
788 if (M_IMGDATA
->m_hasMask
)
790 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
791 M_IMGDATA
->m_maskBlue
== b
)
792 image
.SetMaskColour( 255, 255, 255 );
794 image
.SetMaskColour( 0, 0, 0 );
797 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
799 unsigned char *srcd
= M_IMGDATA
->m_data
;
800 unsigned char *tard
= image
.GetData();
802 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
804 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
805 tard
[0] = tard
[1] = tard
[2] = 255;
807 tard
[0] = tard
[1] = tard
[2] = 0;
813 int wxImage::GetWidth() const
815 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
817 return M_IMGDATA
->m_width
;
820 int wxImage::GetHeight() const
822 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
824 return M_IMGDATA
->m_height
;
827 long wxImage::XYToIndex(int x
, int y
) const
831 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
833 return y
*M_IMGDATA
->m_width
+ x
;
839 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
841 long pos
= XYToIndex(x
, y
);
842 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
846 M_IMGDATA
->m_data
[ pos
] = r
;
847 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
848 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
851 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
853 wxCHECK_RET( Ok(), wxT("invalid image") );
856 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
857 if ( rect
== wxRect() )
863 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
864 imageRect
.Inside(rect
.GetBottomRight()),
865 wxT("invalid bounding rectangle") );
868 int x1
= rect
.GetLeft(),
870 x2
= rect
.GetRight() + 1,
871 y2
= rect
.GetBottom() + 1;
873 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
874 int x
, y
, width
= GetWidth();
875 for (y
= y1
; y
< y2
; y
++)
877 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
878 for (x
= x1
; x
< x2
; x
++)
887 unsigned char wxImage::GetRed( int x
, int y
) const
889 long pos
= XYToIndex(x
, y
);
890 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
894 return M_IMGDATA
->m_data
[pos
];
897 unsigned char wxImage::GetGreen( int x
, int y
) const
899 long pos
= XYToIndex(x
, y
);
900 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
904 return M_IMGDATA
->m_data
[pos
+1];
907 unsigned char wxImage::GetBlue( int x
, int y
) const
909 long pos
= XYToIndex(x
, y
);
910 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
914 return M_IMGDATA
->m_data
[pos
+2];
917 bool wxImage::Ok() const
919 // image of 0 width or height can't be considered ok - at least because it
920 // causes crashes in ConvertToBitmap() if we don't catch it in time
921 wxImageRefData
*data
= M_IMGDATA
;
922 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
925 unsigned char *wxImage::GetData() const
927 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
929 return M_IMGDATA
->m_data
;
932 void wxImage::SetData( unsigned char *data
, bool static_data
)
934 wxCHECK_RET( Ok(), wxT("invalid image") );
936 wxImageRefData
*newRefData
= new wxImageRefData();
938 newRefData
->m_width
= M_IMGDATA
->m_width
;
939 newRefData
->m_height
= M_IMGDATA
->m_height
;
940 newRefData
->m_data
= data
;
941 newRefData
->m_ok
= true;
942 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
943 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
944 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
945 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
946 newRefData
->m_static
= static_data
;
950 m_refData
= newRefData
;
953 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
955 wxImageRefData
*newRefData
= new wxImageRefData();
959 newRefData
->m_width
= new_width
;
960 newRefData
->m_height
= new_height
;
961 newRefData
->m_data
= data
;
962 newRefData
->m_ok
= true;
963 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
964 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
965 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
966 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
970 newRefData
->m_width
= new_width
;
971 newRefData
->m_height
= new_height
;
972 newRefData
->m_data
= data
;
973 newRefData
->m_ok
= true;
975 newRefData
->m_static
= static_data
;
979 m_refData
= newRefData
;
982 // ----------------------------------------------------------------------------
983 // alpha channel support
984 // ----------------------------------------------------------------------------
986 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
988 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
990 long pos
= XYToIndex(x
, y
);
991 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
993 M_IMGDATA
->m_alpha
[pos
] = alpha
;
996 unsigned char wxImage::GetAlpha(int x
, int y
) const
998 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1000 long pos
= XYToIndex(x
, y
);
1001 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1003 return M_IMGDATA
->m_alpha
[pos
];
1007 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1011 const int w
= M_IMGDATA
->m_width
;
1012 const int h
= M_IMGDATA
->m_height
;
1014 unsigned char *alpha
= GetAlpha();
1015 unsigned char *data
= GetData();
1017 for ( int y
= 0; y
< h
; y
++ )
1019 for ( int x
= 0; x
< w
; x
++ )
1031 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1033 wxCHECK_RET( Ok(), wxT("invalid image") );
1037 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1040 free(M_IMGDATA
->m_alpha
);
1041 M_IMGDATA
->m_alpha
= alpha
;
1042 M_IMGDATA
->m_staticAlpha
= static_data
;
1045 unsigned char *wxImage::GetAlpha() const
1047 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1049 return M_IMGDATA
->m_alpha
;
1052 void wxImage::InitAlpha()
1054 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1056 // initialize memory for alpha channel
1059 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1060 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1064 // use the mask to initialize the alpha channel.
1065 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1067 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1068 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1069 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1070 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1074 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1075 ? wxIMAGE_ALPHA_TRANSPARENT
1076 : wxIMAGE_ALPHA_OPAQUE
;
1079 M_IMGDATA
->m_hasMask
= false;
1083 // make the image fully opaque
1084 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1088 // ----------------------------------------------------------------------------
1090 // ----------------------------------------------------------------------------
1092 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1094 wxCHECK_RET( Ok(), wxT("invalid image") );
1096 M_IMGDATA
->m_maskRed
= r
;
1097 M_IMGDATA
->m_maskGreen
= g
;
1098 M_IMGDATA
->m_maskBlue
= b
;
1099 M_IMGDATA
->m_hasMask
= true;
1102 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1104 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1106 if (M_IMGDATA
->m_hasMask
)
1108 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1109 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1110 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1115 FindFirstUnusedColour(r
, g
, b
);
1120 unsigned char wxImage::GetMaskRed() const
1122 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1124 return M_IMGDATA
->m_maskRed
;
1127 unsigned char wxImage::GetMaskGreen() const
1129 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1131 return M_IMGDATA
->m_maskGreen
;
1134 unsigned char wxImage::GetMaskBlue() const
1136 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1138 return M_IMGDATA
->m_maskBlue
;
1141 void wxImage::SetMask( bool mask
)
1143 wxCHECK_RET( Ok(), wxT("invalid image") );
1145 M_IMGDATA
->m_hasMask
= mask
;
1148 bool wxImage::HasMask() const
1150 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1152 return M_IMGDATA
->m_hasMask
;
1155 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1157 long pos
= XYToIndex(x
, y
);
1158 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1161 if ( M_IMGDATA
->m_hasMask
)
1163 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1164 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1165 p
[1] == M_IMGDATA
->m_maskGreen
&&
1166 p
[2] == M_IMGDATA
->m_maskBlue
)
1173 if ( M_IMGDATA
->m_alpha
)
1175 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1177 // transparent enough
1186 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1187 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1189 // check that the images are the same size
1190 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1192 wxLogError( _("Image and mask have different sizes.") );
1196 // find unused colour
1197 unsigned char r
,g
,b
;
1198 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1200 wxLogError( _("No unused colour in image being masked.") );
1204 unsigned char *imgdata
= GetData();
1205 unsigned char *maskdata
= mask
.GetData();
1207 const int w
= GetWidth();
1208 const int h
= GetHeight();
1210 for (int j
= 0; j
< h
; j
++)
1212 for (int i
= 0; i
< w
; i
++)
1214 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1225 SetMaskColour(r
, g
, b
);
1231 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1236 unsigned char mr
, mg
, mb
;
1237 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1239 wxLogError( _("No unused colour in image being masked.") );
1244 SetMaskColour(mr
, mg
, mb
);
1246 unsigned char *imgdata
= GetData();
1247 unsigned char *alphadata
= GetAlpha();
1250 int h
= GetHeight();
1252 for (int y
= 0; y
< h
; y
++)
1254 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1256 if (*alphadata
< threshold
)
1265 free(M_IMGDATA
->m_alpha
);
1266 M_IMGDATA
->m_alpha
= NULL
;
1271 // ----------------------------------------------------------------------------
1272 // Palette functions
1273 // ----------------------------------------------------------------------------
1277 bool wxImage::HasPalette() const
1282 return M_IMGDATA
->m_palette
.Ok();
1285 const wxPalette
& wxImage::GetPalette() const
1287 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1289 return M_IMGDATA
->m_palette
;
1292 void wxImage::SetPalette(const wxPalette
& palette
)
1294 wxCHECK_RET( Ok(), wxT("invalid image") );
1296 M_IMGDATA
->m_palette
= palette
;
1299 #endif // wxUSE_PALETTE
1301 // ----------------------------------------------------------------------------
1302 // Option functions (arbitrary name/value mapping)
1303 // ----------------------------------------------------------------------------
1305 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1307 wxCHECK_RET( Ok(), wxT("invalid image") );
1309 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1310 if (idx
== wxNOT_FOUND
)
1312 M_IMGDATA
->m_optionNames
.Add(name
);
1313 M_IMGDATA
->m_optionValues
.Add(value
);
1317 M_IMGDATA
->m_optionNames
[idx
] = name
;
1318 M_IMGDATA
->m_optionValues
[idx
] = value
;
1322 void wxImage::SetOption(const wxString
& name
, int value
)
1325 valStr
.Printf(wxT("%d"), value
);
1326 SetOption(name
, valStr
);
1329 wxString
wxImage::GetOption(const wxString
& name
) const
1331 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1333 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1334 if (idx
== wxNOT_FOUND
)
1335 return wxEmptyString
;
1337 return M_IMGDATA
->m_optionValues
[idx
];
1340 int wxImage::GetOptionInt(const wxString
& name
) const
1342 return wxAtoi(GetOption(name
));
1345 bool wxImage::HasOption(const wxString
& name
) const
1347 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1349 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1352 // ----------------------------------------------------------------------------
1354 // ----------------------------------------------------------------------------
1356 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1359 if (wxFileExists(filename
))
1361 wxFileInputStream
stream(filename
);
1362 wxBufferedInputStream
bstream( stream
);
1363 return LoadFile(bstream
, type
, index
);
1367 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1371 #else // !wxUSE_STREAMS
1373 #endif // wxUSE_STREAMS
1376 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1379 if (wxFileExists(filename
))
1381 wxFileInputStream
stream(filename
);
1382 wxBufferedInputStream
bstream( stream
);
1383 return LoadFile(bstream
, mimetype
, index
);
1387 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1391 #else // !wxUSE_STREAMS
1393 #endif // wxUSE_STREAMS
1398 bool wxImage::SaveFile( const wxString
& filename
) const
1400 wxString ext
= filename
.AfterLast('.').Lower();
1402 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1405 SaveFile(filename
, pHandler
->GetType());
1409 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1414 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1417 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1419 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1421 wxFileOutputStream
stream(filename
);
1423 if ( stream
.IsOk() )
1425 wxBufferedOutputStream
bstream( stream
);
1426 return SaveFile(bstream
, type
);
1428 #endif // wxUSE_STREAMS
1433 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1436 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1438 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1440 wxFileOutputStream
stream(filename
);
1442 if ( stream
.IsOk() )
1444 wxBufferedOutputStream
bstream( stream
);
1445 return SaveFile(bstream
, mimetype
);
1447 #endif // wxUSE_STREAMS
1452 bool wxImage::CanRead( const wxString
&name
)
1455 wxFileInputStream
stream(name
);
1456 return CanRead(stream
);
1462 int wxImage::GetImageCount( const wxString
&name
, long type
)
1465 wxFileInputStream
stream(name
);
1467 return GetImageCount(stream
, type
);
1475 bool wxImage::CanRead( wxInputStream
&stream
)
1477 const wxList
& list
= GetHandlers();
1479 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1481 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1482 if (handler
->CanRead( stream
))
1489 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1491 wxImageHandler
*handler
;
1493 if ( type
== wxBITMAP_TYPE_ANY
)
1495 wxList
&list
=GetHandlers();
1497 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1499 handler
=(wxImageHandler
*)node
->GetData();
1500 if ( handler
->CanRead(stream
) )
1501 return handler
->GetImageCount(stream
);
1505 wxLogWarning(_("No handler found for image type."));
1509 handler
= FindHandler(type
);
1513 wxLogWarning(_("No image handler for type %d defined."), type
);
1517 if ( handler
->CanRead(stream
) )
1519 return handler
->GetImageCount(stream
);
1523 wxLogError(_("Image file is not of type %d."), type
);
1528 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1532 m_refData
= new wxImageRefData
;
1534 wxImageHandler
*handler
;
1536 if ( type
== wxBITMAP_TYPE_ANY
)
1538 wxList
&list
=GetHandlers();
1540 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1542 handler
=(wxImageHandler
*)node
->GetData();
1543 if ( handler
->CanRead(stream
) )
1544 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1548 wxLogWarning( _("No handler found for image type.") );
1552 handler
= FindHandler(type
);
1556 wxLogWarning( _("No image handler for type %d defined."), type
);
1561 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1564 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1568 m_refData
= new wxImageRefData
;
1570 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1574 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1579 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1582 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1584 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1586 wxImageHandler
*handler
= FindHandler(type
);
1589 wxLogWarning( _("No image handler for type %d defined."), type
);
1594 return handler
->SaveFile( (wxImage
*)this, stream
);
1597 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1599 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1601 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1604 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1609 return handler
->SaveFile( (wxImage
*)this, stream
);
1611 #endif // wxUSE_STREAMS
1613 // ----------------------------------------------------------------------------
1614 // image I/O handlers
1615 // ----------------------------------------------------------------------------
1617 void wxImage::AddHandler( wxImageHandler
*handler
)
1619 // Check for an existing handler of the type being added.
1620 if (FindHandler( handler
->GetType() ) == 0)
1622 sm_handlers
.Append( handler
);
1626 // This is not documented behaviour, merely the simplest 'fix'
1627 // for preventing duplicate additions. If someone ever has
1628 // a good reason to add and remove duplicate handlers (and they
1629 // may) we should probably refcount the duplicates.
1630 // also an issue in InsertHandler below.
1632 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1633 handler
->GetName().c_str() );
1638 void wxImage::InsertHandler( wxImageHandler
*handler
)
1640 // Check for an existing handler of the type being added.
1641 if (FindHandler( handler
->GetType() ) == 0)
1643 sm_handlers
.Insert( handler
);
1647 // see AddHandler for additional comments.
1648 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1649 handler
->GetName().c_str() );
1654 bool wxImage::RemoveHandler( const wxString
& name
)
1656 wxImageHandler
*handler
= FindHandler(name
);
1659 sm_handlers
.DeleteObject(handler
);
1667 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1669 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1672 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1673 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1675 node
= node
->GetNext();
1680 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1682 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1685 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1686 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1687 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1689 node
= node
->GetNext();
1694 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1696 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1699 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1700 if (handler
->GetType() == bitmapType
) return handler
;
1701 node
= node
->GetNext();
1706 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1708 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1711 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1712 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1713 node
= node
->GetNext();
1718 void wxImage::InitStandardHandlers()
1721 AddHandler(new wxBMPHandler
);
1722 #endif // wxUSE_STREAMS
1725 void wxImage::CleanUpHandlers()
1727 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1730 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1731 wxList::compatibility_iterator next
= node
->GetNext();
1736 sm_handlers
.Clear();
1739 wxString
wxImage::GetImageExtWildcard()
1743 wxList
& Handlers
= wxImage::GetHandlers();
1744 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1747 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1748 fmts
+= wxT("*.") + Handler
->GetExtension();
1749 Node
= Node
->GetNext();
1750 if ( Node
) fmts
+= wxT(";");
1753 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1756 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1758 double hue
, saturation
, value
;
1760 const double red
= rgb
.red
/ 255.0,
1761 green
= rgb
.green
/ 255.0,
1762 blue
= rgb
.blue
/ 255.0;
1764 double minimumRGB
= red
;
1765 if (green
< minimumRGB
)
1768 if (blue
< minimumRGB
)
1771 double maximumRGB
= red
;
1772 if (green
> maximumRGB
)
1775 if (blue
> maximumRGB
)
1780 if (maximumRGB
== minimumRGB
)
1782 // Gray has no color
1788 double deltaRGB
= maximumRGB
- minimumRGB
;
1790 saturation
= deltaRGB
/ maximumRGB
;
1792 if ( red
== maximumRGB
)
1793 hue
= (green
- blue
) / deltaRGB
;
1794 else if (green
== maximumRGB
)
1795 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1797 hue
= 4.0 + (red
- green
) / deltaRGB
;
1805 return HSVValue(hue
, saturation
, value
);
1808 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1810 double red
, green
, blue
;
1812 if ( hsv
.saturation
== 0.0 )
1814 red
= hsv
.value
; //Grey
1820 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1821 int i
= (int)floor(hue
);
1822 double f
= hue
- i
; // fractional part of h
1823 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1829 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1834 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1842 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1847 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1852 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1860 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1865 return RGBValue((unsigned char)(red
* 255.0),
1866 (unsigned char)(green
* 255.0),
1867 (unsigned char)(blue
* 255.0));
1871 * Rotates the hue of each pixel of the image. angle is a double in the range
1872 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1874 void wxImage::RotateHue(double angle
)
1876 unsigned char *srcBytePtr
;
1877 unsigned char *dstBytePtr
;
1878 unsigned long count
;
1879 wxImage::HSVValue hsv
;
1880 wxImage::RGBValue rgb
;
1882 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
1883 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1884 if (count
> 0 && angle
!= 0.0)
1886 srcBytePtr
= M_IMGDATA
->m_data
;
1887 dstBytePtr
= srcBytePtr
;
1890 rgb
.red
= *srcBytePtr
++;
1891 rgb
.green
= *srcBytePtr
++;
1892 rgb
.blue
= *srcBytePtr
++;
1893 hsv
= RGBtoHSV(rgb
);
1895 hsv
.hue
= hsv
.hue
+ angle
;
1897 hsv
.hue
= hsv
.hue
- 1.0;
1898 else if (hsv
.hue
< 0.0)
1899 hsv
.hue
= hsv
.hue
+ 1.0;
1901 rgb
= HSVtoRGB(hsv
);
1902 *dstBytePtr
++ = rgb
.red
;
1903 *dstBytePtr
++ = rgb
.green
;
1904 *dstBytePtr
++ = rgb
.blue
;
1905 } while (--count
!= 0);
1909 //-----------------------------------------------------------------------------
1911 //-----------------------------------------------------------------------------
1913 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1916 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1921 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1926 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1931 bool wxImageHandler::CanRead( const wxString
& name
)
1933 if (wxFileExists(name
))
1935 wxFileInputStream
stream(name
);
1936 return CanRead(stream
);
1939 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1944 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1946 wxFileOffset posOld
= stream
.TellI();
1947 if ( posOld
== wxInvalidOffset
)
1949 // can't test unseekable stream
1953 bool ok
= DoCanRead(stream
);
1955 // restore the old position to be able to test other formats and so on
1956 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1958 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1960 // reading would fail anyhow as we're not at the right position
1967 #endif // wxUSE_STREAMS
1969 // ----------------------------------------------------------------------------
1970 // image histogram stuff
1971 // ----------------------------------------------------------------------------
1974 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1979 unsigned char g2
) const
1981 unsigned long key
= MakeKey(r2
, g2
, b2
);
1983 while ( find(key
) != end() )
1985 // color already used
1997 wxLogError(_("No unused colour in image.") );
2003 key
= MakeKey(r2
, g2
, b2
);
2017 wxImage::FindFirstUnusedColour(unsigned char *r
,
2022 unsigned char g2
) const
2024 wxImageHistogram histogram
;
2026 ComputeHistogram(histogram
);
2028 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2034 // Counts and returns the number of different colours. Optionally stops
2035 // when it exceeds 'stopafter' different colours. This is useful, for
2036 // example, to see if the image can be saved as 8-bit (256 colour or
2037 // less, in this case it would be invoked as CountColours(256)). Default
2038 // value for stopafter is -1 (don't care).
2040 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2044 unsigned char r
, g
, b
;
2046 unsigned long size
, nentries
, key
;
2049 size
= GetWidth() * GetHeight();
2052 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2057 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2059 if (h
.Get(key
) == NULL
)
2070 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2072 unsigned char *p
= GetData();
2073 unsigned long nentries
= 0;
2077 const unsigned long size
= GetWidth() * GetHeight();
2079 unsigned char r
, g
, b
;
2080 for ( unsigned long n
= 0; n
< size
; n
++ )
2086 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2088 if ( entry
.value
++ == 0 )
2089 entry
.index
= nentries
++;
2096 * Rotation code by Carlos Moreno
2099 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2100 // does exactly the same thing. And I also got rid of wxRotationPixel
2101 // bacause of potential problems in architectures where alignment
2102 // is an issue, so I had to rewrite parts of the code.
2104 static const double gs_Epsilon
= 1e-10;
2106 static inline int wxCint (double x
)
2108 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2112 // Auxiliary function to rotate a point (x,y) with respect to point p0
2113 // make it inline and use a straight return to facilitate optimization
2114 // also, the function receives the sine and cosine of the angle to avoid
2115 // repeating the time-consuming calls to these functions -- sin/cos can
2116 // be computed and stored in the calling function.
2118 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2120 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2121 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2124 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2126 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2129 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2132 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2134 bool has_alpha
= HasAlpha();
2136 // Create pointer-based array to accelerate access to wxImage's data
2137 unsigned char ** data
= new unsigned char * [GetHeight()];
2138 data
[0] = GetData();
2139 for (i
= 1; i
< GetHeight(); i
++)
2140 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2142 // Same for alpha channel
2143 unsigned char ** alpha
= NULL
;
2146 alpha
= new unsigned char * [GetHeight()];
2147 alpha
[0] = GetAlpha();
2148 for (i
= 1; i
< GetHeight(); i
++)
2149 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2152 // precompute coefficients for rotation formula
2153 // (sine and cosine of the angle)
2154 const double cos_angle
= cos(angle
);
2155 const double sin_angle
= sin(angle
);
2157 // Create new Image to store the result
2158 // First, find rectangle that covers the rotated image; to do that,
2159 // rotate the four corners
2161 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2163 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2164 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2165 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2166 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2168 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2169 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2170 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2171 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2173 // Create rotated image
2174 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
2175 // With alpha channel
2179 if (offset_after_rotation
!= NULL
)
2181 *offset_after_rotation
= wxPoint (x1
, y1
);
2184 // GRG: The rotated (destination) image is always accessed
2185 // sequentially, so there is no need for a pointer-based
2186 // array here (and in fact it would be slower).
2188 unsigned char * dst
= rotated
.GetData();
2190 unsigned char * alpha_dst
= NULL
;
2192 alpha_dst
= rotated
.GetAlpha();
2194 // GRG: if the original image has a mask, use its RGB values
2195 // as the blank pixel, else, fall back to default (black).
2197 unsigned char blank_r
= 0;
2198 unsigned char blank_g
= 0;
2199 unsigned char blank_b
= 0;
2203 blank_r
= GetMaskRed();
2204 blank_g
= GetMaskGreen();
2205 blank_b
= GetMaskBlue();
2206 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2209 // Now, for each point of the rotated image, find where it came from, by
2210 // performing an inverse rotation (a rotation of -angle) and getting the
2211 // pixel at those coordinates
2213 // GRG: I've taken the (interpolating) test out of the loops, so that
2214 // it is done only once, instead of repeating it for each pixel.
2219 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2221 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2223 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2225 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2226 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2228 // interpolate using the 4 enclosing grid-points. Those
2229 // points can be obtained using floor and ceiling of the
2230 // exact coordinates of the point
2231 // C.M. 2000-02-17: when the point is near the border, special care is required.
2235 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2237 x1
= wxCint(floor(src
.x
));
2238 x2
= wxCint(ceil(src
.x
));
2240 else // else means that x is near one of the borders (0 or width-1)
2242 x1
= x2
= wxCint (src
.x
);
2245 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2247 y1
= wxCint(floor(src
.y
));
2248 y2
= wxCint(ceil(src
.y
));
2252 y1
= y2
= wxCint (src
.y
);
2255 // get four points and the distances (square of the distance,
2256 // for efficiency reasons) for the interpolation formula
2258 // GRG: Do not calculate the points until they are
2259 // really needed -- this way we can calculate
2260 // just one, instead of four, if d1, d2, d3
2261 // or d4 are < gs_Epsilon
2263 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2264 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2265 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2266 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2268 // Now interpolate as a weighted average of the four surrounding
2269 // points, where the weights are the distances to each of those points
2271 // If the point is exactly at one point of the grid of the source
2272 // image, then don't interpolate -- just assign the pixel
2274 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2276 unsigned char *p
= data
[y1
] + (3 * x1
);
2283 unsigned char *p
= alpha
[y1
] + x1
;
2284 *(alpha_dst
++) = *p
;
2287 else if (d2
< gs_Epsilon
)
2289 unsigned char *p
= data
[y1
] + (3 * x2
);
2296 unsigned char *p
= alpha
[y1
] + x2
;
2297 *(alpha_dst
++) = *p
;
2300 else if (d3
< gs_Epsilon
)
2302 unsigned char *p
= data
[y2
] + (3 * x2
);
2309 unsigned char *p
= alpha
[y2
] + x2
;
2310 *(alpha_dst
++) = *p
;
2313 else if (d4
< gs_Epsilon
)
2315 unsigned char *p
= data
[y2
] + (3 * x1
);
2322 unsigned char *p
= alpha
[y2
] + x1
;
2323 *(alpha_dst
++) = *p
;
2328 // weights for the weighted average are proportional to the inverse of the distance
2329 unsigned char *v1
= data
[y1
] + (3 * x1
);
2330 unsigned char *v2
= data
[y1
] + (3 * x2
);
2331 unsigned char *v3
= data
[y2
] + (3 * x2
);
2332 unsigned char *v4
= data
[y2
] + (3 * x1
);
2334 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2338 *(dst
++) = (unsigned char)
2339 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2340 w3
* *(v3
++) + w4
* *(v4
++)) /
2341 (w1
+ w2
+ w3
+ w4
) );
2342 *(dst
++) = (unsigned char)
2343 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2344 w3
* *(v3
++) + w4
* *(v4
++)) /
2345 (w1
+ w2
+ w3
+ w4
) );
2346 *(dst
++) = (unsigned char)
2347 ( (w1
* *v1
+ w2
* *v2
+
2348 w3
* *v3
+ w4
* *v4
) /
2349 (w1
+ w2
+ w3
+ w4
) );
2353 unsigned char *v1
= alpha
[y1
] + (x1
);
2354 unsigned char *v2
= alpha
[y1
] + (x2
);
2355 unsigned char *v3
= alpha
[y2
] + (x2
);
2356 unsigned char *v4
= alpha
[y2
] + (x1
);
2358 *(alpha_dst
++) = (unsigned char)
2359 ( (w1
* *v1
+ w2
* *v2
+
2360 w3
* *v3
+ w4
* *v4
) /
2361 (w1
+ w2
+ w3
+ w4
) );
2377 else // not interpolating
2379 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2381 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2383 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2385 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2386 const int ys
= wxCint (src
.y
); // closest integer
2388 if (0 <= xs
&& xs
< GetWidth() &&
2389 0 <= ys
&& ys
< GetHeight())
2391 unsigned char *p
= data
[ys
] + (3 * xs
);
2398 unsigned char *p
= alpha
[ys
] + (xs
);
2399 *(alpha_dst
++) = *p
;
2409 *(alpha_dst
++) = 255;
2427 // A module to allow wxImage initialization/cleanup
2428 // without calling these functions from app.cpp or from
2429 // the user's application.
2431 class wxImageModule
: public wxModule
2433 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2436 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2437 void OnExit() { wxImage::CleanUpHandlers(); };
2440 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2443 #endif // wxUSE_IMAGE