1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
28 #include "wx/filefn.h"
29 #include "wx/wfstream.h"
31 #include "wx/module.h"
36 #include "wx/xpmdecod.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxImageRefData
: public wxObjectRefData
50 virtual ~wxImageRefData();
54 unsigned char *m_data
;
57 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
59 // alpha channel data, may be NULL for the formats without alpha support
60 unsigned char *m_alpha
;
64 // if true, m_data is pointer to static data and shouldn't be freed
67 // same as m_static but for m_alpha
72 #endif // wxUSE_PALETTE
74 wxArrayString m_optionNames
;
75 wxArrayString m_optionValues
;
77 DECLARE_NO_COPY_CLASS(wxImageRefData
)
80 wxImageRefData::wxImageRefData()
85 m_alpha
= (unsigned char *) NULL
;
94 m_staticAlpha
= false;
97 wxImageRefData::~wxImageRefData()
101 if ( !m_staticAlpha
)
105 wxList
wxImage::sm_handlers
;
109 //-----------------------------------------------------------------------------
111 #define M_IMGDATA ((wxImageRefData *)m_refData)
113 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
115 wxImage::wxImage( int width
, int height
, bool clear
)
117 Create( width
, height
, clear
);
120 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
122 Create( width
, height
, data
, static_data
);
125 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
127 Create( width
, height
, data
, alpha
, static_data
);
130 wxImage::wxImage( const wxString
& name
, long type
, int index
)
132 LoadFile( name
, type
, index
);
135 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
137 LoadFile( name
, mimetype
, index
);
141 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
143 LoadFile( stream
, type
, index
);
146 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
148 LoadFile( stream
, mimetype
, index
);
150 #endif // wxUSE_STREAMS
152 wxImage::wxImage( const char** xpmData
)
157 wxImage::wxImage( char** xpmData
)
159 Create((const char**) xpmData
);
162 bool wxImage::Create( const char** xpmData
)
167 wxXPMDecoder decoder
;
168 (*this) = decoder
.ReadData(xpmData
);
175 bool wxImage::Create( int width
, int height
, bool clear
)
179 m_refData
= new wxImageRefData();
181 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
182 if (!M_IMGDATA
->m_data
)
189 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
191 M_IMGDATA
->m_width
= width
;
192 M_IMGDATA
->m_height
= height
;
193 M_IMGDATA
->m_ok
= true;
198 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
202 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
204 m_refData
= new wxImageRefData();
206 M_IMGDATA
->m_data
= data
;
207 M_IMGDATA
->m_width
= width
;
208 M_IMGDATA
->m_height
= height
;
209 M_IMGDATA
->m_ok
= true;
210 M_IMGDATA
->m_static
= static_data
;
215 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
219 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
221 m_refData
= new wxImageRefData();
223 M_IMGDATA
->m_data
= data
;
224 M_IMGDATA
->m_alpha
= alpha
;
225 M_IMGDATA
->m_width
= width
;
226 M_IMGDATA
->m_height
= height
;
227 M_IMGDATA
->m_ok
= true;
228 M_IMGDATA
->m_static
= static_data
;
233 void wxImage::Destroy()
238 wxImage
wxImage::Copy() const
242 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
244 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
246 unsigned char *data
= image
.GetData();
248 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
250 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
251 image
.SetMask( M_IMGDATA
->m_hasMask
);
253 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
255 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
257 // also copy the alpha channel
261 unsigned char* alpha
= image
.GetAlpha();
262 memcpy( alpha
, GetAlpha(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
265 // also copy the image options
266 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
267 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
272 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
274 if( xFactor
== 1 && yFactor
== 1 )
279 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
281 // can't scale to/from 0 size
282 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
283 wxT("invalid new image size") );
285 long old_height
= M_IMGDATA
->m_height
,
286 old_width
= M_IMGDATA
->m_width
;
288 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
289 wxT("invalid old image size") );
291 long width
= old_width
/ xFactor
;
292 long height
= old_height
/ yFactor
;
294 image
.Create( width
, height
, false );
296 char unsigned *data
= image
.GetData();
298 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
300 bool hasMask
= false ;
301 unsigned char maskRed
= 0;
302 unsigned char maskGreen
= 0;
303 unsigned char maskBlue
=0 ;
305 unsigned char *source_data
= M_IMGDATA
->m_data
;
306 unsigned char *target_data
= data
;
307 unsigned char *source_alpha
= 0 ;
308 unsigned char *target_alpha
= 0 ;
309 if (M_IMGDATA
->m_hasMask
)
312 maskRed
= M_IMGDATA
->m_maskRed
;
313 maskGreen
= M_IMGDATA
->m_maskGreen
;
314 maskBlue
=M_IMGDATA
->m_maskBlue
;
316 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
317 M_IMGDATA
->m_maskGreen
,
318 M_IMGDATA
->m_maskBlue
);
322 source_alpha
= M_IMGDATA
->m_alpha
;
326 target_alpha
= image
.GetAlpha() ;
330 for (long y
= 0; y
< height
; y
++)
332 for (long x
= 0; x
< width
; x
++)
334 unsigned long avgRed
= 0 ;
335 unsigned long avgGreen
= 0;
336 unsigned long avgBlue
= 0;
337 unsigned long avgAlpha
= 0 ;
338 unsigned long counter
= 0 ;
340 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
342 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
343 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
345 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
346 unsigned char red
= pixel
[0] ;
347 unsigned char green
= pixel
[1] ;
348 unsigned char blue
= pixel
[2] ;
349 unsigned char alpha
= 255 ;
351 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
352 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
367 *(target_data
++) = M_IMGDATA
->m_maskRed
;
368 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
369 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
374 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
375 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
376 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
377 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
382 // In case this is a cursor, make sure the hotspot is scaled accordingly:
383 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
384 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
385 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
386 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
387 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
388 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
393 wxImage
wxImage::Scale( int width
, int height
) const
397 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
399 // can't scale to/from 0 size
400 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
401 wxT("invalid new image size") );
403 long old_height
= M_IMGDATA
->m_height
,
404 old_width
= M_IMGDATA
->m_width
;
405 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
406 wxT("invalid old image size") );
408 if ( old_width
% width
== 0 && old_width
>= width
&&
409 old_height
% height
== 0 && old_height
>= height
)
411 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
413 image
.Create( width
, height
, false );
415 unsigned char *data
= image
.GetData();
417 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
419 unsigned char *source_data
= M_IMGDATA
->m_data
;
420 unsigned char *target_data
= data
;
421 unsigned char *source_alpha
= 0 ;
422 unsigned char *target_alpha
= 0 ;
424 if (M_IMGDATA
->m_hasMask
)
426 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
427 M_IMGDATA
->m_maskGreen
,
428 M_IMGDATA
->m_maskBlue
);
432 source_alpha
= M_IMGDATA
->m_alpha
;
436 target_alpha
= image
.GetAlpha() ;
440 long x_delta
= (old_width
<<16) / width
;
441 long y_delta
= (old_height
<<16) / height
;
443 unsigned char* dest_pixel
= target_data
;
446 for ( long j
= 0; j
< height
; j
++ )
448 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
449 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
452 for ( long i
= 0; i
< width
; i
++ )
454 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
455 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
456 dest_pixel
[0] = src_pixel
[0];
457 dest_pixel
[1] = src_pixel
[1];
458 dest_pixel
[2] = src_pixel
[2];
461 *(target_alpha
++) = *src_alpha_pixel
;
468 // In case this is a cursor, make sure the hotspot is scaled accordingly:
469 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
470 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
471 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
472 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
473 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
474 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
479 wxImage
wxImage::Rotate90( bool clockwise
) const
483 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
485 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
487 unsigned char *data
= image
.GetData();
489 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
491 unsigned char *source_data
= M_IMGDATA
->m_data
;
492 unsigned char *target_data
;
493 unsigned char *alpha_data
= 0 ;
494 unsigned char *source_alpha
= 0 ;
495 unsigned char *target_alpha
= 0 ;
497 if (M_IMGDATA
->m_hasMask
)
499 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
503 source_alpha
= M_IMGDATA
->m_alpha
;
507 alpha_data
= image
.GetAlpha() ;
511 long height
= M_IMGDATA
->m_height
;
512 long width
= M_IMGDATA
->m_width
;
514 for (long j
= 0; j
< height
; j
++)
516 for (long i
= 0; i
< width
; i
++)
520 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
522 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
526 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
528 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
530 memcpy( target_data
, source_data
, 3 );
535 memcpy( target_alpha
, source_alpha
, 1 );
544 wxImage
wxImage::Mirror( bool horizontally
) const
548 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
550 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
552 unsigned char *data
= image
.GetData();
553 unsigned char *alpha
= NULL
;
555 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
557 if (M_IMGDATA
->m_alpha
!= NULL
) {
559 alpha
= image
.GetAlpha();
560 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
563 if (M_IMGDATA
->m_hasMask
)
564 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
566 long height
= M_IMGDATA
->m_height
;
567 long width
= M_IMGDATA
->m_width
;
569 unsigned char *source_data
= M_IMGDATA
->m_data
;
570 unsigned char *target_data
;
574 for (long j
= 0; j
< height
; j
++)
577 target_data
= data
-3;
578 for (long i
= 0; i
< width
; i
++)
580 memcpy( target_data
, source_data
, 3 );
588 // src_alpha starts at the first pixel and increases by 1 after each step
589 // (a step here is the copy of the alpha value of one pixel)
590 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
591 // dest_alpha starts just beyond the first line, decreases before each step,
592 // and after each line is finished, increases by 2 widths (skipping the line
593 // just copied and the line that will be copied next)
594 unsigned char *dest_alpha
= alpha
+ width
;
596 for (long jj
= 0; jj
< height
; ++jj
)
598 for (long i
= 0; i
< width
; ++i
) {
599 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
601 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
607 for (long i
= 0; i
< height
; i
++)
609 target_data
= data
+ 3*width
*(height
-1-i
);
610 memcpy( target_data
, source_data
, (size_t)3*width
);
611 source_data
+= 3*width
;
616 // src_alpha starts at the first pixel and increases by 1 width after each step
617 // (a step here is the copy of the alpha channel of an entire line)
618 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
619 // dest_alpha starts just beyond the last line (beyond the whole image)
620 // and decreases by 1 width before each step
621 unsigned char *dest_alpha
= alpha
+ width
* height
;
623 for (long jj
= 0; jj
< height
; ++jj
)
626 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
635 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
639 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
641 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
642 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
643 image
, wxT("invalid subimage size") );
645 const int subwidth
= rect
.GetWidth();
646 const int subheight
= rect
.GetHeight();
648 image
.Create( subwidth
, subheight
, false );
650 const unsigned char *src_data
= GetData();
651 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
652 unsigned char *subdata
= image
.GetData();
653 unsigned char *subalpha
= NULL
;
655 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
657 if (src_alpha
!= NULL
) {
659 subalpha
= image
.GetAlpha();
660 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
663 if (M_IMGDATA
->m_hasMask
)
664 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
666 const int width
= GetWidth();
667 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
669 src_data
+= 3 * pixsoff
;
670 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
672 for (long j
= 0; j
< subheight
; ++j
)
674 memcpy( subdata
, src_data
, 3 * subwidth
);
675 subdata
+= 3 * subwidth
;
676 src_data
+= 3 * width
;
677 if (subalpha
!= NULL
) {
678 memcpy( subalpha
, src_alpha
, subwidth
);
679 subalpha
+= subwidth
;
687 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
688 int r_
, int g_
, int b_
) const
692 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
693 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
695 int width
= GetWidth(), height
= GetHeight();
696 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
698 unsigned char r
= (unsigned char)r_
;
699 unsigned char g
= (unsigned char)g_
;
700 unsigned char b
= (unsigned char)b_
;
701 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
703 GetOrFindMaskColour( &r
, &g
, &b
);
704 image
.SetMaskColour(r
, g
, b
);
707 image
.SetRGB(wxRect(), r
, g
, b
);
709 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
710 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
712 subRect
.Intersect(finalRect
);
714 if (!subRect
.IsEmpty())
716 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
717 image
.Paste(*this, pos
.x
, pos
.y
);
719 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
725 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
727 wxCHECK_RET( Ok(), wxT("invalid image") );
728 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
732 int width
= image
.GetWidth();
733 int height
= image
.GetHeight();
746 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
747 width
= M_IMGDATA
->m_width
- (x
+xx
);
748 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
749 height
= M_IMGDATA
->m_height
- (y
+yy
);
751 if (width
< 1) return;
752 if (height
< 1) return;
754 if ((!HasMask() && !image
.HasMask()) ||
755 (HasMask() && !image
.HasMask()) ||
756 ((HasMask() && image
.HasMask() &&
757 (GetMaskRed()==image
.GetMaskRed()) &&
758 (GetMaskGreen()==image
.GetMaskGreen()) &&
759 (GetMaskBlue()==image
.GetMaskBlue()))))
762 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
763 int source_step
= image
.GetWidth()*3;
765 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
766 int target_step
= M_IMGDATA
->m_width
*3;
767 for (int j
= 0; j
< height
; j
++)
769 memcpy( target_data
, source_data
, width
);
770 source_data
+= source_step
;
771 target_data
+= target_step
;
776 if (!HasMask() && image
.HasMask())
778 unsigned char r
= image
.GetMaskRed();
779 unsigned char g
= image
.GetMaskGreen();
780 unsigned char b
= image
.GetMaskBlue();
783 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
784 int source_step
= image
.GetWidth()*3;
786 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
787 int target_step
= M_IMGDATA
->m_width
*3;
789 for (int j
= 0; j
< height
; j
++)
791 for (int i
= 0; i
< width
; i
+=3)
793 if ((source_data
[i
] != r
) &&
794 (source_data
[i
+1] != g
) &&
795 (source_data
[i
+2] != b
))
797 memcpy( target_data
+i
, source_data
+i
, 3 );
800 source_data
+= source_step
;
801 target_data
+= target_step
;
806 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
807 unsigned char r2
, unsigned char g2
, unsigned char b2
)
809 wxCHECK_RET( Ok(), wxT("invalid image") );
811 unsigned char *data
= GetData();
813 const int w
= GetWidth();
814 const int h
= GetHeight();
816 for (int j
= 0; j
< h
; j
++)
817 for (int i
= 0; i
< w
; i
++)
819 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
829 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
833 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
835 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
837 unsigned char *dest
= image
.GetData();
839 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
841 unsigned char *src
= M_IMGDATA
->m_data
;
842 bool hasMask
= M_IMGDATA
->m_hasMask
;
843 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
844 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
845 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
848 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
850 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
851 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
853 // don't modify the mask
854 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
856 memcpy(dest
, src
, 3);
860 // calculate the luma
861 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
862 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
869 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
873 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
875 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
877 unsigned char *data
= image
.GetData();
879 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
881 if (M_IMGDATA
->m_hasMask
)
883 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
884 M_IMGDATA
->m_maskBlue
== b
)
885 image
.SetMaskColour( 255, 255, 255 );
887 image
.SetMaskColour( 0, 0, 0 );
890 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
892 unsigned char *srcd
= M_IMGDATA
->m_data
;
893 unsigned char *tard
= image
.GetData();
895 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
897 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
898 tard
[0] = tard
[1] = tard
[2] = 255;
900 tard
[0] = tard
[1] = tard
[2] = 0;
906 int wxImage::GetWidth() const
908 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
910 return M_IMGDATA
->m_width
;
913 int wxImage::GetHeight() const
915 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
917 return M_IMGDATA
->m_height
;
920 long wxImage::XYToIndex(int x
, int y
) const
924 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
926 return y
*M_IMGDATA
->m_width
+ x
;
932 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
934 long pos
= XYToIndex(x
, y
);
935 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
939 M_IMGDATA
->m_data
[ pos
] = r
;
940 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
941 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
944 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
946 wxCHECK_RET( Ok(), wxT("invalid image") );
949 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
950 if ( rect
== wxRect() )
956 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
957 imageRect
.Inside(rect
.GetBottomRight()),
958 wxT("invalid bounding rectangle") );
961 int x1
= rect
.GetLeft(),
963 x2
= rect
.GetRight() + 1,
964 y2
= rect
.GetBottom() + 1;
966 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
967 int x
, y
, width
= GetWidth();
968 for (y
= y1
; y
< y2
; y
++)
970 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
971 for (x
= x1
; x
< x2
; x
++)
980 unsigned char wxImage::GetRed( int x
, int y
) const
982 long pos
= XYToIndex(x
, y
);
983 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
987 return M_IMGDATA
->m_data
[pos
];
990 unsigned char wxImage::GetGreen( int x
, int y
) const
992 long pos
= XYToIndex(x
, y
);
993 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
997 return M_IMGDATA
->m_data
[pos
+1];
1000 unsigned char wxImage::GetBlue( int x
, int y
) const
1002 long pos
= XYToIndex(x
, y
);
1003 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1007 return M_IMGDATA
->m_data
[pos
+2];
1010 bool wxImage::Ok() const
1012 // image of 0 width or height can't be considered ok - at least because it
1013 // causes crashes in ConvertToBitmap() if we don't catch it in time
1014 wxImageRefData
*data
= M_IMGDATA
;
1015 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1018 unsigned char *wxImage::GetData() const
1020 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1022 return M_IMGDATA
->m_data
;
1025 void wxImage::SetData( unsigned char *data
, bool static_data
)
1027 wxCHECK_RET( Ok(), wxT("invalid image") );
1029 wxImageRefData
*newRefData
= new wxImageRefData();
1031 newRefData
->m_width
= M_IMGDATA
->m_width
;
1032 newRefData
->m_height
= M_IMGDATA
->m_height
;
1033 newRefData
->m_data
= data
;
1034 newRefData
->m_ok
= true;
1035 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1036 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1037 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1038 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1039 newRefData
->m_static
= static_data
;
1043 m_refData
= newRefData
;
1046 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1048 wxImageRefData
*newRefData
= new wxImageRefData();
1052 newRefData
->m_width
= new_width
;
1053 newRefData
->m_height
= new_height
;
1054 newRefData
->m_data
= data
;
1055 newRefData
->m_ok
= true;
1056 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1057 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1058 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1059 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1063 newRefData
->m_width
= new_width
;
1064 newRefData
->m_height
= new_height
;
1065 newRefData
->m_data
= data
;
1066 newRefData
->m_ok
= true;
1068 newRefData
->m_static
= static_data
;
1072 m_refData
= newRefData
;
1075 // ----------------------------------------------------------------------------
1076 // alpha channel support
1077 // ----------------------------------------------------------------------------
1079 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1081 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1083 long pos
= XYToIndex(x
, y
);
1084 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1086 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1089 unsigned char wxImage::GetAlpha(int x
, int y
) const
1091 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1093 long pos
= XYToIndex(x
, y
);
1094 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1096 return M_IMGDATA
->m_alpha
[pos
];
1100 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1104 const int w
= M_IMGDATA
->m_width
;
1105 const int h
= M_IMGDATA
->m_height
;
1107 unsigned char *alpha
= GetAlpha();
1108 unsigned char *data
= GetData();
1110 for ( int y
= 0; y
< h
; y
++ )
1112 for ( int x
= 0; x
< w
; x
++ )
1124 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1126 wxCHECK_RET( Ok(), wxT("invalid image") );
1130 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1133 free(M_IMGDATA
->m_alpha
);
1134 M_IMGDATA
->m_alpha
= alpha
;
1135 M_IMGDATA
->m_staticAlpha
= static_data
;
1138 unsigned char *wxImage::GetAlpha() const
1140 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1142 return M_IMGDATA
->m_alpha
;
1145 void wxImage::InitAlpha()
1147 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1149 // initialize memory for alpha channel
1152 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1153 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1157 // use the mask to initialize the alpha channel.
1158 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1160 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1161 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1162 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1163 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1167 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1168 ? wxIMAGE_ALPHA_TRANSPARENT
1169 : wxIMAGE_ALPHA_OPAQUE
;
1172 M_IMGDATA
->m_hasMask
= false;
1176 // make the image fully opaque
1177 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1181 // ----------------------------------------------------------------------------
1183 // ----------------------------------------------------------------------------
1185 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1187 wxCHECK_RET( Ok(), wxT("invalid image") );
1189 M_IMGDATA
->m_maskRed
= r
;
1190 M_IMGDATA
->m_maskGreen
= g
;
1191 M_IMGDATA
->m_maskBlue
= b
;
1192 M_IMGDATA
->m_hasMask
= true;
1195 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1197 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1199 if (M_IMGDATA
->m_hasMask
)
1201 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1202 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1203 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1208 FindFirstUnusedColour(r
, g
, b
);
1213 unsigned char wxImage::GetMaskRed() const
1215 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1217 return M_IMGDATA
->m_maskRed
;
1220 unsigned char wxImage::GetMaskGreen() const
1222 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1224 return M_IMGDATA
->m_maskGreen
;
1227 unsigned char wxImage::GetMaskBlue() const
1229 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1231 return M_IMGDATA
->m_maskBlue
;
1234 void wxImage::SetMask( bool mask
)
1236 wxCHECK_RET( Ok(), wxT("invalid image") );
1238 M_IMGDATA
->m_hasMask
= mask
;
1241 bool wxImage::HasMask() const
1243 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1245 return M_IMGDATA
->m_hasMask
;
1248 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1250 long pos
= XYToIndex(x
, y
);
1251 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1254 if ( M_IMGDATA
->m_hasMask
)
1256 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1257 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1258 p
[1] == M_IMGDATA
->m_maskGreen
&&
1259 p
[2] == M_IMGDATA
->m_maskBlue
)
1266 if ( M_IMGDATA
->m_alpha
)
1268 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1270 // transparent enough
1279 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1280 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1282 // check that the images are the same size
1283 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1285 wxLogError( _("Image and mask have different sizes.") );
1289 // find unused colour
1290 unsigned char r
,g
,b
;
1291 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1293 wxLogError( _("No unused colour in image being masked.") );
1297 unsigned char *imgdata
= GetData();
1298 unsigned char *maskdata
= mask
.GetData();
1300 const int w
= GetWidth();
1301 const int h
= GetHeight();
1303 for (int j
= 0; j
< h
; j
++)
1305 for (int i
= 0; i
< w
; i
++)
1307 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1318 SetMaskColour(r
, g
, b
);
1324 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1329 unsigned char mr
, mg
, mb
;
1330 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1332 wxLogError( _("No unused colour in image being masked.") );
1337 SetMaskColour(mr
, mg
, mb
);
1339 unsigned char *imgdata
= GetData();
1340 unsigned char *alphadata
= GetAlpha();
1343 int h
= GetHeight();
1345 for (int y
= 0; y
< h
; y
++)
1347 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1349 if (*alphadata
< threshold
)
1358 free(M_IMGDATA
->m_alpha
);
1359 M_IMGDATA
->m_alpha
= NULL
;
1364 // ----------------------------------------------------------------------------
1365 // Palette functions
1366 // ----------------------------------------------------------------------------
1370 bool wxImage::HasPalette() const
1375 return M_IMGDATA
->m_palette
.Ok();
1378 const wxPalette
& wxImage::GetPalette() const
1380 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1382 return M_IMGDATA
->m_palette
;
1385 void wxImage::SetPalette(const wxPalette
& palette
)
1387 wxCHECK_RET( Ok(), wxT("invalid image") );
1389 M_IMGDATA
->m_palette
= palette
;
1392 #endif // wxUSE_PALETTE
1394 // ----------------------------------------------------------------------------
1395 // Option functions (arbitrary name/value mapping)
1396 // ----------------------------------------------------------------------------
1398 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1400 wxCHECK_RET( Ok(), wxT("invalid image") );
1402 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1403 if (idx
== wxNOT_FOUND
)
1405 M_IMGDATA
->m_optionNames
.Add(name
);
1406 M_IMGDATA
->m_optionValues
.Add(value
);
1410 M_IMGDATA
->m_optionNames
[idx
] = name
;
1411 M_IMGDATA
->m_optionValues
[idx
] = value
;
1415 void wxImage::SetOption(const wxString
& name
, int value
)
1418 valStr
.Printf(wxT("%d"), value
);
1419 SetOption(name
, valStr
);
1422 wxString
wxImage::GetOption(const wxString
& name
) const
1424 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1426 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1427 if (idx
== wxNOT_FOUND
)
1428 return wxEmptyString
;
1430 return M_IMGDATA
->m_optionValues
[idx
];
1433 int wxImage::GetOptionInt(const wxString
& name
) const
1435 return wxAtoi(GetOption(name
));
1438 bool wxImage::HasOption(const wxString
& name
) const
1440 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1442 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1445 // ----------------------------------------------------------------------------
1447 // ----------------------------------------------------------------------------
1449 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1452 if (wxFileExists(filename
))
1454 wxFileInputStream
stream(filename
);
1455 wxBufferedInputStream
bstream( stream
);
1456 return LoadFile(bstream
, type
, index
);
1460 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1464 #else // !wxUSE_STREAMS
1466 #endif // wxUSE_STREAMS
1469 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1472 if (wxFileExists(filename
))
1474 wxFileInputStream
stream(filename
);
1475 wxBufferedInputStream
bstream( stream
);
1476 return LoadFile(bstream
, mimetype
, index
);
1480 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1484 #else // !wxUSE_STREAMS
1486 #endif // wxUSE_STREAMS
1491 bool wxImage::SaveFile( const wxString
& filename
) const
1493 wxString ext
= filename
.AfterLast('.').Lower();
1495 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1498 SaveFile(filename
, pHandler
->GetType());
1502 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1507 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1510 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1512 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1514 wxFileOutputStream
stream(filename
);
1516 if ( stream
.IsOk() )
1518 wxBufferedOutputStream
bstream( stream
);
1519 return SaveFile(bstream
, type
);
1521 #endif // wxUSE_STREAMS
1526 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1529 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1531 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1533 wxFileOutputStream
stream(filename
);
1535 if ( stream
.IsOk() )
1537 wxBufferedOutputStream
bstream( stream
);
1538 return SaveFile(bstream
, mimetype
);
1540 #endif // wxUSE_STREAMS
1545 bool wxImage::CanRead( const wxString
&name
)
1548 wxFileInputStream
stream(name
);
1549 return CanRead(stream
);
1555 int wxImage::GetImageCount( const wxString
&name
, long type
)
1558 wxFileInputStream
stream(name
);
1560 return GetImageCount(stream
, type
);
1568 bool wxImage::CanRead( wxInputStream
&stream
)
1570 const wxList
& list
= GetHandlers();
1572 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1574 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1575 if (handler
->CanRead( stream
))
1582 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1584 wxImageHandler
*handler
;
1586 if ( type
== wxBITMAP_TYPE_ANY
)
1588 wxList
&list
=GetHandlers();
1590 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1592 handler
=(wxImageHandler
*)node
->GetData();
1593 if ( handler
->CanRead(stream
) )
1594 return handler
->GetImageCount(stream
);
1598 wxLogWarning(_("No handler found for image type."));
1602 handler
= FindHandler(type
);
1606 wxLogWarning(_("No image handler for type %d defined."), type
);
1610 if ( handler
->CanRead(stream
) )
1612 return handler
->GetImageCount(stream
);
1616 wxLogError(_("Image file is not of type %d."), type
);
1621 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1625 m_refData
= new wxImageRefData
;
1627 wxImageHandler
*handler
;
1629 if ( type
== wxBITMAP_TYPE_ANY
)
1631 wxList
&list
=GetHandlers();
1633 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1635 handler
=(wxImageHandler
*)node
->GetData();
1636 if ( handler
->CanRead(stream
) )
1637 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1641 wxLogWarning( _("No handler found for image type.") );
1645 handler
= FindHandler(type
);
1649 wxLogWarning( _("No image handler for type %d defined."), type
);
1654 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1656 wxLogError(_("Image file is not of type %d."), type
);
1660 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1663 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1667 m_refData
= new wxImageRefData
;
1669 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1673 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1678 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1680 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1684 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1687 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1689 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1691 wxImageHandler
*handler
= FindHandler(type
);
1694 wxLogWarning( _("No image handler for type %d defined."), type
);
1699 return handler
->SaveFile( (wxImage
*)this, stream
);
1702 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1704 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1706 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1709 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1714 return handler
->SaveFile( (wxImage
*)this, stream
);
1716 #endif // wxUSE_STREAMS
1718 // ----------------------------------------------------------------------------
1719 // image I/O handlers
1720 // ----------------------------------------------------------------------------
1722 void wxImage::AddHandler( wxImageHandler
*handler
)
1724 // Check for an existing handler of the type being added.
1725 if (FindHandler( handler
->GetType() ) == 0)
1727 sm_handlers
.Append( handler
);
1731 // This is not documented behaviour, merely the simplest 'fix'
1732 // for preventing duplicate additions. If someone ever has
1733 // a good reason to add and remove duplicate handlers (and they
1734 // may) we should probably refcount the duplicates.
1735 // also an issue in InsertHandler below.
1737 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1738 handler
->GetName().c_str() );
1743 void wxImage::InsertHandler( wxImageHandler
*handler
)
1745 // Check for an existing handler of the type being added.
1746 if (FindHandler( handler
->GetType() ) == 0)
1748 sm_handlers
.Insert( handler
);
1752 // see AddHandler for additional comments.
1753 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1754 handler
->GetName().c_str() );
1759 bool wxImage::RemoveHandler( const wxString
& name
)
1761 wxImageHandler
*handler
= FindHandler(name
);
1764 sm_handlers
.DeleteObject(handler
);
1772 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1774 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1777 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1778 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1780 node
= node
->GetNext();
1785 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1787 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1790 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1791 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1792 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1794 node
= node
->GetNext();
1799 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1801 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1804 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1805 if (handler
->GetType() == bitmapType
) return handler
;
1806 node
= node
->GetNext();
1811 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1813 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1816 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1817 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1818 node
= node
->GetNext();
1823 void wxImage::InitStandardHandlers()
1826 AddHandler(new wxBMPHandler
);
1827 #endif // wxUSE_STREAMS
1830 void wxImage::CleanUpHandlers()
1832 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1835 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1836 wxList::compatibility_iterator next
= node
->GetNext();
1841 sm_handlers
.Clear();
1844 wxString
wxImage::GetImageExtWildcard()
1848 wxList
& Handlers
= wxImage::GetHandlers();
1849 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1852 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1853 fmts
+= wxT("*.") + Handler
->GetExtension();
1854 Node
= Node
->GetNext();
1855 if ( Node
) fmts
+= wxT(";");
1858 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1861 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1863 const double red
= rgb
.red
/ 255.0,
1864 green
= rgb
.green
/ 255.0,
1865 blue
= rgb
.blue
/ 255.0;
1867 // find the min and max intensity (and remember which one was it for the
1869 double minimumRGB
= red
;
1870 if ( green
< minimumRGB
)
1872 if ( blue
< minimumRGB
)
1875 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1876 double maximumRGB
= red
;
1877 if ( green
> maximumRGB
)
1882 if ( blue
> maximumRGB
)
1888 const double value
= maximumRGB
;
1890 double hue
= 0.0, saturation
;
1891 const double deltaRGB
= maximumRGB
- minimumRGB
;
1892 if ( wxIsNullDouble(deltaRGB
) )
1894 // Gray has no color
1903 hue
= (green
- blue
) / deltaRGB
;
1907 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1911 hue
= 4.0 + (red
- green
) / deltaRGB
;
1915 wxFAIL_MSG(wxT("hue not specified"));
1924 saturation
= deltaRGB
/ maximumRGB
;
1927 return HSVValue(hue
, saturation
, value
);
1930 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1932 double red
, green
, blue
;
1934 if ( wxIsNullDouble(hsv
.saturation
) )
1943 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1944 int i
= (int)floor(hue
);
1945 double f
= hue
- i
; // fractional part of h
1946 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1952 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1957 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1965 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1970 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1975 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1983 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1988 return RGBValue((unsigned char)(red
* 255.0),
1989 (unsigned char)(green
* 255.0),
1990 (unsigned char)(blue
* 255.0));
1994 * Rotates the hue of each pixel of the image. angle is a double in the range
1995 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1997 void wxImage::RotateHue(double angle
)
1999 unsigned char *srcBytePtr
;
2000 unsigned char *dstBytePtr
;
2001 unsigned long count
;
2002 wxImage::HSVValue hsv
;
2003 wxImage::RGBValue rgb
;
2005 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2006 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2007 if ( count
> 0 && !wxIsNullDouble(angle
) )
2009 srcBytePtr
= M_IMGDATA
->m_data
;
2010 dstBytePtr
= srcBytePtr
;
2013 rgb
.red
= *srcBytePtr
++;
2014 rgb
.green
= *srcBytePtr
++;
2015 rgb
.blue
= *srcBytePtr
++;
2016 hsv
= RGBtoHSV(rgb
);
2018 hsv
.hue
= hsv
.hue
+ angle
;
2020 hsv
.hue
= hsv
.hue
- 1.0;
2021 else if (hsv
.hue
< 0.0)
2022 hsv
.hue
= hsv
.hue
+ 1.0;
2024 rgb
= HSVtoRGB(hsv
);
2025 *dstBytePtr
++ = rgb
.red
;
2026 *dstBytePtr
++ = rgb
.green
;
2027 *dstBytePtr
++ = rgb
.blue
;
2028 } while (--count
!= 0);
2032 //-----------------------------------------------------------------------------
2034 //-----------------------------------------------------------------------------
2036 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2039 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2044 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2049 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2054 bool wxImageHandler::CanRead( const wxString
& name
)
2056 if (wxFileExists(name
))
2058 wxFileInputStream
stream(name
);
2059 return CanRead(stream
);
2062 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2067 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2069 wxFileOffset posOld
= stream
.TellI();
2070 if ( posOld
== wxInvalidOffset
)
2072 // can't test unseekable stream
2076 bool ok
= DoCanRead(stream
);
2078 // restore the old position to be able to test other formats and so on
2079 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2081 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2083 // reading would fail anyhow as we're not at the right position
2090 #endif // wxUSE_STREAMS
2092 // ----------------------------------------------------------------------------
2093 // image histogram stuff
2094 // ----------------------------------------------------------------------------
2097 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2102 unsigned char g2
) const
2104 unsigned long key
= MakeKey(r2
, g2
, b2
);
2106 while ( find(key
) != end() )
2108 // color already used
2120 wxLogError(_("No unused colour in image.") );
2126 key
= MakeKey(r2
, g2
, b2
);
2140 wxImage::FindFirstUnusedColour(unsigned char *r
,
2145 unsigned char g2
) const
2147 wxImageHistogram histogram
;
2149 ComputeHistogram(histogram
);
2151 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2157 // Counts and returns the number of different colours. Optionally stops
2158 // when it exceeds 'stopafter' different colours. This is useful, for
2159 // example, to see if the image can be saved as 8-bit (256 colour or
2160 // less, in this case it would be invoked as CountColours(256)). Default
2161 // value for stopafter is -1 (don't care).
2163 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2167 unsigned char r
, g
, b
;
2169 unsigned long size
, nentries
, key
;
2172 size
= GetWidth() * GetHeight();
2175 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2180 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2182 if (h
.Get(key
) == NULL
)
2193 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2195 unsigned char *p
= GetData();
2196 unsigned long nentries
= 0;
2200 const unsigned long size
= GetWidth() * GetHeight();
2202 unsigned char r
, g
, b
;
2203 for ( unsigned long n
= 0; n
< size
; n
++ )
2209 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2211 if ( entry
.value
++ == 0 )
2212 entry
.index
= nentries
++;
2219 * Rotation code by Carlos Moreno
2222 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2223 // does exactly the same thing. And I also got rid of wxRotationPixel
2224 // bacause of potential problems in architectures where alignment
2225 // is an issue, so I had to rewrite parts of the code.
2227 static const double gs_Epsilon
= 1e-10;
2229 static inline int wxCint (double x
)
2231 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2235 // Auxiliary function to rotate a point (x,y) with respect to point p0
2236 // make it inline and use a straight return to facilitate optimization
2237 // also, the function receives the sine and cosine of the angle to avoid
2238 // repeating the time-consuming calls to these functions -- sin/cos can
2239 // be computed and stored in the calling function.
2241 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2243 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2244 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2247 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2249 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2252 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2255 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2257 bool has_alpha
= HasAlpha();
2259 // Create pointer-based array to accelerate access to wxImage's data
2260 unsigned char ** data
= new unsigned char * [GetHeight()];
2261 data
[0] = GetData();
2262 for (i
= 1; i
< GetHeight(); i
++)
2263 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2265 // Same for alpha channel
2266 unsigned char ** alpha
= NULL
;
2269 alpha
= new unsigned char * [GetHeight()];
2270 alpha
[0] = GetAlpha();
2271 for (i
= 1; i
< GetHeight(); i
++)
2272 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2275 // precompute coefficients for rotation formula
2276 // (sine and cosine of the angle)
2277 const double cos_angle
= cos(angle
);
2278 const double sin_angle
= sin(angle
);
2280 // Create new Image to store the result
2281 // First, find rectangle that covers the rotated image; to do that,
2282 // rotate the four corners
2284 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2286 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2287 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2288 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2289 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2291 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2292 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2293 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2294 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2296 // Create rotated image
2297 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2298 // With alpha channel
2302 if (offset_after_rotation
!= NULL
)
2304 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2307 // GRG: The rotated (destination) image is always accessed
2308 // sequentially, so there is no need for a pointer-based
2309 // array here (and in fact it would be slower).
2311 unsigned char * dst
= rotated
.GetData();
2313 unsigned char * alpha_dst
= NULL
;
2315 alpha_dst
= rotated
.GetAlpha();
2317 // GRG: if the original image has a mask, use its RGB values
2318 // as the blank pixel, else, fall back to default (black).
2320 unsigned char blank_r
= 0;
2321 unsigned char blank_g
= 0;
2322 unsigned char blank_b
= 0;
2326 blank_r
= GetMaskRed();
2327 blank_g
= GetMaskGreen();
2328 blank_b
= GetMaskBlue();
2329 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2332 // Now, for each point of the rotated image, find where it came from, by
2333 // performing an inverse rotation (a rotation of -angle) and getting the
2334 // pixel at those coordinates
2336 // GRG: I've taken the (interpolating) test out of the loops, so that
2337 // it is done only once, instead of repeating it for each pixel.
2342 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2344 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2346 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2348 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2349 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2351 // interpolate using the 4 enclosing grid-points. Those
2352 // points can be obtained using floor and ceiling of the
2353 // exact coordinates of the point
2356 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2358 x1
= wxCint(floor(src
.x
));
2359 x2
= wxCint(ceil(src
.x
));
2361 else // else means that x is near one of the borders (0 or width-1)
2363 x1
= x2
= wxCint (src
.x
);
2366 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2368 y1
= wxCint(floor(src
.y
));
2369 y2
= wxCint(ceil(src
.y
));
2373 y1
= y2
= wxCint (src
.y
);
2376 // get four points and the distances (square of the distance,
2377 // for efficiency reasons) for the interpolation formula
2379 // GRG: Do not calculate the points until they are
2380 // really needed -- this way we can calculate
2381 // just one, instead of four, if d1, d2, d3
2382 // or d4 are < gs_Epsilon
2384 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2385 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2386 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2387 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2389 // Now interpolate as a weighted average of the four surrounding
2390 // points, where the weights are the distances to each of those points
2392 // If the point is exactly at one point of the grid of the source
2393 // image, then don't interpolate -- just assign the pixel
2395 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2397 unsigned char *p
= data
[y1
] + (3 * x1
);
2403 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2405 else if (d2
< gs_Epsilon
)
2407 unsigned char *p
= data
[y1
] + (3 * x2
);
2413 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2415 else if (d3
< gs_Epsilon
)
2417 unsigned char *p
= data
[y2
] + (3 * x2
);
2423 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2425 else if (d4
< gs_Epsilon
)
2427 unsigned char *p
= data
[y2
] + (3 * x1
);
2433 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2437 // weights for the weighted average are proportional to the inverse of the distance
2438 unsigned char *v1
= data
[y1
] + (3 * x1
);
2439 unsigned char *v2
= data
[y1
] + (3 * x2
);
2440 unsigned char *v3
= data
[y2
] + (3 * x2
);
2441 unsigned char *v4
= data
[y2
] + (3 * x1
);
2443 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2447 *(dst
++) = (unsigned char)
2448 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2449 w3
* *(v3
++) + w4
* *(v4
++)) /
2450 (w1
+ w2
+ w3
+ w4
) );
2451 *(dst
++) = (unsigned char)
2452 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2453 w3
* *(v3
++) + w4
* *(v4
++)) /
2454 (w1
+ w2
+ w3
+ w4
) );
2455 *(dst
++) = (unsigned char)
2456 ( (w1
* *v1
+ w2
* *v2
+
2457 w3
* *v3
+ w4
* *v4
) /
2458 (w1
+ w2
+ w3
+ w4
) );
2462 v1
= alpha
[y1
] + (x1
);
2463 v2
= alpha
[y1
] + (x2
);
2464 v3
= alpha
[y2
] + (x2
);
2465 v4
= alpha
[y2
] + (x1
);
2467 *(alpha_dst
++) = (unsigned char)
2468 ( (w1
* *v1
+ w2
* *v2
+
2469 w3
* *v3
+ w4
* *v4
) /
2470 (w1
+ w2
+ w3
+ w4
) );
2486 else // not interpolating
2488 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2490 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2492 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2494 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2495 const int ys
= wxCint (src
.y
); // closest integer
2497 if (0 <= xs
&& xs
< GetWidth() &&
2498 0 <= ys
&& ys
< GetHeight())
2500 unsigned char *p
= data
[ys
] + (3 * xs
);
2506 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2515 *(alpha_dst
++) = 255;
2533 // A module to allow wxImage initialization/cleanup
2534 // without calling these functions from app.cpp or from
2535 // the user's application.
2537 class wxImageModule
: public wxModule
2539 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2542 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2543 void OnExit() { wxImage::CleanUpHandlers(); };
2546 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2549 #endif // wxUSE_IMAGE