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"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #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 wx_static_cast(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 wxObjectRefData
* wxImage::CreateRefData() const
240 return new wxImageRefData
;
243 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
245 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
246 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
248 wxImageRefData
* refData_new
= new wxImageRefData
;
249 refData_new
->m_width
= refData
->m_width
;
250 refData_new
->m_height
= refData
->m_height
;
251 refData_new
->m_maskRed
= refData
->m_maskRed
;
252 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
253 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
254 refData_new
->m_hasMask
= refData
->m_hasMask
;
255 refData_new
->m_ok
= true;
256 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
257 if (refData
->m_alpha
!= NULL
)
259 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
260 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
263 refData_new
->m_data
= (unsigned char*)malloc(size
);
264 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
266 refData_new
->m_palette
= refData
->m_palette
;
268 refData_new
->m_optionNames
= refData
->m_optionNames
;
269 refData_new
->m_optionValues
= refData
->m_optionValues
;
273 wxImage
wxImage::Copy() const
277 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
279 image
.m_refData
= CloneRefData(m_refData
);
284 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
286 if( xFactor
== 1 && yFactor
== 1 )
291 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
293 // can't scale to/from 0 size
294 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
295 wxT("invalid new image size") );
297 long old_height
= M_IMGDATA
->m_height
,
298 old_width
= M_IMGDATA
->m_width
;
300 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
301 wxT("invalid old image size") );
303 long width
= old_width
/ xFactor
;
304 long height
= old_height
/ yFactor
;
306 image
.Create( width
, height
, false );
308 char unsigned *data
= image
.GetData();
310 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
312 bool hasMask
= false ;
313 unsigned char maskRed
= 0;
314 unsigned char maskGreen
= 0;
315 unsigned char maskBlue
=0 ;
317 unsigned char *source_data
= M_IMGDATA
->m_data
;
318 unsigned char *target_data
= data
;
319 unsigned char *source_alpha
= 0 ;
320 unsigned char *target_alpha
= 0 ;
321 if (M_IMGDATA
->m_hasMask
)
324 maskRed
= M_IMGDATA
->m_maskRed
;
325 maskGreen
= M_IMGDATA
->m_maskGreen
;
326 maskBlue
=M_IMGDATA
->m_maskBlue
;
328 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
329 M_IMGDATA
->m_maskGreen
,
330 M_IMGDATA
->m_maskBlue
);
334 source_alpha
= M_IMGDATA
->m_alpha
;
338 target_alpha
= image
.GetAlpha() ;
342 for (long y
= 0; y
< height
; y
++)
344 for (long x
= 0; x
< width
; x
++)
346 unsigned long avgRed
= 0 ;
347 unsigned long avgGreen
= 0;
348 unsigned long avgBlue
= 0;
349 unsigned long avgAlpha
= 0 ;
350 unsigned long counter
= 0 ;
352 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
354 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
355 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
357 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
358 unsigned char red
= pixel
[0] ;
359 unsigned char green
= pixel
[1] ;
360 unsigned char blue
= pixel
[2] ;
361 unsigned char alpha
= 255 ;
363 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
364 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
379 *(target_data
++) = M_IMGDATA
->m_maskRed
;
380 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
381 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
386 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
387 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
388 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
389 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
394 // In case this is a cursor, make sure the hotspot is scaled accordingly:
395 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
396 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
397 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
398 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
399 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
400 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
405 wxImage
wxImage::Scale( int width
, int height
) const
409 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
411 // can't scale to/from 0 size
412 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
413 wxT("invalid new image size") );
415 long old_height
= M_IMGDATA
->m_height
,
416 old_width
= M_IMGDATA
->m_width
;
417 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
418 wxT("invalid old image size") );
420 if ( old_width
% width
== 0 && old_width
>= width
&&
421 old_height
% height
== 0 && old_height
>= height
)
423 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
425 image
.Create( width
, height
, false );
427 unsigned char *data
= image
.GetData();
429 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
431 unsigned char *source_data
= M_IMGDATA
->m_data
;
432 unsigned char *target_data
= data
;
433 unsigned char *source_alpha
= 0 ;
434 unsigned char *target_alpha
= 0 ;
436 if (M_IMGDATA
->m_hasMask
)
438 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
439 M_IMGDATA
->m_maskGreen
,
440 M_IMGDATA
->m_maskBlue
);
444 source_alpha
= M_IMGDATA
->m_alpha
;
448 target_alpha
= image
.GetAlpha() ;
452 long x_delta
= (old_width
<<16) / width
;
453 long y_delta
= (old_height
<<16) / height
;
455 unsigned char* dest_pixel
= target_data
;
458 for ( long j
= 0; j
< height
; j
++ )
460 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
461 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
464 for ( long i
= 0; i
< width
; i
++ )
466 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
467 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
468 dest_pixel
[0] = src_pixel
[0];
469 dest_pixel
[1] = src_pixel
[1];
470 dest_pixel
[2] = src_pixel
[2];
473 *(target_alpha
++) = *src_alpha_pixel
;
480 // In case this is a cursor, make sure the hotspot is scaled accordingly:
481 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
482 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
483 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
484 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
485 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
486 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
491 wxImage
wxImage::Rotate90( bool clockwise
) const
495 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
497 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
499 unsigned char *data
= image
.GetData();
501 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
503 unsigned char *source_data
= M_IMGDATA
->m_data
;
504 unsigned char *target_data
;
505 unsigned char *alpha_data
= 0 ;
506 unsigned char *source_alpha
= 0 ;
507 unsigned char *target_alpha
= 0 ;
509 if (M_IMGDATA
->m_hasMask
)
511 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
515 source_alpha
= M_IMGDATA
->m_alpha
;
519 alpha_data
= image
.GetAlpha() ;
523 long height
= M_IMGDATA
->m_height
;
524 long width
= M_IMGDATA
->m_width
;
526 for (long j
= 0; j
< height
; j
++)
528 for (long i
= 0; i
< width
; i
++)
532 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
534 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
538 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
540 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
542 memcpy( target_data
, source_data
, 3 );
547 memcpy( target_alpha
, source_alpha
, 1 );
556 wxImage
wxImage::Mirror( bool horizontally
) const
560 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
562 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
564 unsigned char *data
= image
.GetData();
565 unsigned char *alpha
= NULL
;
567 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
569 if (M_IMGDATA
->m_alpha
!= NULL
) {
571 alpha
= image
.GetAlpha();
572 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
575 if (M_IMGDATA
->m_hasMask
)
576 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
578 long height
= M_IMGDATA
->m_height
;
579 long width
= M_IMGDATA
->m_width
;
581 unsigned char *source_data
= M_IMGDATA
->m_data
;
582 unsigned char *target_data
;
586 for (long j
= 0; j
< height
; j
++)
589 target_data
= data
-3;
590 for (long i
= 0; i
< width
; i
++)
592 memcpy( target_data
, source_data
, 3 );
600 // src_alpha starts at the first pixel and increases by 1 after each step
601 // (a step here is the copy of the alpha value of one pixel)
602 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
603 // dest_alpha starts just beyond the first line, decreases before each step,
604 // and after each line is finished, increases by 2 widths (skipping the line
605 // just copied and the line that will be copied next)
606 unsigned char *dest_alpha
= alpha
+ width
;
608 for (long jj
= 0; jj
< height
; ++jj
)
610 for (long i
= 0; i
< width
; ++i
) {
611 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
613 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
619 for (long i
= 0; i
< height
; i
++)
621 target_data
= data
+ 3*width
*(height
-1-i
);
622 memcpy( target_data
, source_data
, (size_t)3*width
);
623 source_data
+= 3*width
;
628 // src_alpha starts at the first pixel and increases by 1 width after each step
629 // (a step here is the copy of the alpha channel of an entire line)
630 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
631 // dest_alpha starts just beyond the last line (beyond the whole image)
632 // and decreases by 1 width before each step
633 unsigned char *dest_alpha
= alpha
+ width
* height
;
635 for (long jj
= 0; jj
< height
; ++jj
)
638 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
647 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
651 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
653 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
654 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
655 image
, wxT("invalid subimage size") );
657 const int subwidth
= rect
.GetWidth();
658 const int subheight
= rect
.GetHeight();
660 image
.Create( subwidth
, subheight
, false );
662 const unsigned char *src_data
= GetData();
663 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
664 unsigned char *subdata
= image
.GetData();
665 unsigned char *subalpha
= NULL
;
667 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
669 if (src_alpha
!= NULL
) {
671 subalpha
= image
.GetAlpha();
672 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
675 if (M_IMGDATA
->m_hasMask
)
676 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
678 const int width
= GetWidth();
679 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
681 src_data
+= 3 * pixsoff
;
682 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
684 for (long j
= 0; j
< subheight
; ++j
)
686 memcpy( subdata
, src_data
, 3 * subwidth
);
687 subdata
+= 3 * subwidth
;
688 src_data
+= 3 * width
;
689 if (subalpha
!= NULL
) {
690 memcpy( subalpha
, src_alpha
, subwidth
);
691 subalpha
+= subwidth
;
699 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
700 int r_
, int g_
, int b_
) const
704 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
705 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
707 int width
= GetWidth(), height
= GetHeight();
708 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
710 unsigned char r
= (unsigned char)r_
;
711 unsigned char g
= (unsigned char)g_
;
712 unsigned char b
= (unsigned char)b_
;
713 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
715 GetOrFindMaskColour( &r
, &g
, &b
);
716 image
.SetMaskColour(r
, g
, b
);
719 image
.SetRGB(wxRect(), r
, g
, b
);
721 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
722 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
724 finalRect
.width
-= pos
.x
;
726 finalRect
.height
-= pos
.y
;
728 subRect
.Intersect(finalRect
);
730 if (!subRect
.IsEmpty())
732 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
733 image
.Paste(*this, pos
.x
, pos
.y
);
735 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
741 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
743 wxCHECK_RET( Ok(), wxT("invalid image") );
744 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
750 int width
= image
.GetWidth();
751 int height
= image
.GetHeight();
764 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
765 width
= M_IMGDATA
->m_width
- (x
+xx
);
766 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
767 height
= M_IMGDATA
->m_height
- (y
+yy
);
769 if (width
< 1) return;
770 if (height
< 1) return;
772 if ((!HasMask() && !image
.HasMask()) ||
773 (HasMask() && !image
.HasMask()) ||
774 ((HasMask() && image
.HasMask() &&
775 (GetMaskRed()==image
.GetMaskRed()) &&
776 (GetMaskGreen()==image
.GetMaskGreen()) &&
777 (GetMaskBlue()==image
.GetMaskBlue()))))
780 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
781 int source_step
= image
.GetWidth()*3;
783 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
784 int target_step
= M_IMGDATA
->m_width
*3;
785 for (int j
= 0; j
< height
; j
++)
787 memcpy( target_data
, source_data
, width
);
788 source_data
+= source_step
;
789 target_data
+= target_step
;
794 if (!HasMask() && image
.HasMask())
796 unsigned char r
= image
.GetMaskRed();
797 unsigned char g
= image
.GetMaskGreen();
798 unsigned char b
= image
.GetMaskBlue();
801 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
802 int source_step
= image
.GetWidth()*3;
804 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
805 int target_step
= M_IMGDATA
->m_width
*3;
807 for (int j
= 0; j
< height
; j
++)
809 for (int i
= 0; i
< width
; i
+=3)
811 if ((source_data
[i
] != r
) &&
812 (source_data
[i
+1] != g
) &&
813 (source_data
[i
+2] != b
))
815 memcpy( target_data
+i
, source_data
+i
, 3 );
818 source_data
+= source_step
;
819 target_data
+= target_step
;
824 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
825 unsigned char r2
, unsigned char g2
, unsigned char b2
)
827 wxCHECK_RET( Ok(), wxT("invalid image") );
831 unsigned char *data
= GetData();
833 const int w
= GetWidth();
834 const int h
= GetHeight();
836 for (int j
= 0; j
< h
; j
++)
837 for (int i
= 0; i
< w
; i
++)
839 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
849 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
853 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
855 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
857 unsigned char *dest
= image
.GetData();
859 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
861 unsigned char *src
= M_IMGDATA
->m_data
;
862 bool hasMask
= M_IMGDATA
->m_hasMask
;
863 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
864 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
865 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
868 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
870 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
871 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
873 // don't modify the mask
874 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
876 memcpy(dest
, src
, 3);
880 // calculate the luma
881 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
882 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
886 // copy the alpha channel, if any
889 const size_t alphaSize
= GetWidth() * GetHeight();
890 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
891 memcpy(alpha
, GetAlpha(), alphaSize
);
893 image
.SetAlpha(alpha
);
899 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
903 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
905 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
907 unsigned char *data
= image
.GetData();
909 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
911 if (M_IMGDATA
->m_hasMask
)
913 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
914 M_IMGDATA
->m_maskBlue
== b
)
915 image
.SetMaskColour( 255, 255, 255 );
917 image
.SetMaskColour( 0, 0, 0 );
920 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
922 unsigned char *srcd
= M_IMGDATA
->m_data
;
923 unsigned char *tard
= image
.GetData();
925 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
927 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
928 tard
[0] = tard
[1] = tard
[2] = 255;
930 tard
[0] = tard
[1] = tard
[2] = 0;
936 int wxImage::GetWidth() const
938 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
940 return M_IMGDATA
->m_width
;
943 int wxImage::GetHeight() const
945 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
947 return M_IMGDATA
->m_height
;
950 long wxImage::XYToIndex(int x
, int y
) const
954 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
956 return y
*M_IMGDATA
->m_width
+ x
;
962 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
964 long pos
= XYToIndex(x
, y
);
965 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
971 M_IMGDATA
->m_data
[ pos
] = r
;
972 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
973 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
976 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
978 wxCHECK_RET( Ok(), wxT("invalid image") );
983 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
984 if ( rect
== wxRect() )
990 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
991 imageRect
.Inside(rect
.GetBottomRight()),
992 wxT("invalid bounding rectangle") );
995 int x1
= rect
.GetLeft(),
997 x2
= rect
.GetRight() + 1,
998 y2
= rect
.GetBottom() + 1;
1000 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1001 int x
, y
, width
= GetWidth();
1002 for (y
= y1
; y
< y2
; y
++)
1004 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1005 for (x
= x1
; x
< x2
; x
++)
1014 unsigned char wxImage::GetRed( int x
, int y
) const
1016 long pos
= XYToIndex(x
, y
);
1017 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1021 return M_IMGDATA
->m_data
[pos
];
1024 unsigned char wxImage::GetGreen( int x
, int y
) const
1026 long pos
= XYToIndex(x
, y
);
1027 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1031 return M_IMGDATA
->m_data
[pos
+1];
1034 unsigned char wxImage::GetBlue( int x
, int y
) const
1036 long pos
= XYToIndex(x
, y
);
1037 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1041 return M_IMGDATA
->m_data
[pos
+2];
1044 bool wxImage::Ok() const
1046 // image of 0 width or height can't be considered ok - at least because it
1047 // causes crashes in ConvertToBitmap() if we don't catch it in time
1048 wxImageRefData
*data
= M_IMGDATA
;
1049 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1052 unsigned char *wxImage::GetData() const
1054 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1056 return M_IMGDATA
->m_data
;
1059 void wxImage::SetData( unsigned char *data
, bool static_data
)
1061 wxCHECK_RET( Ok(), wxT("invalid image") );
1063 wxImageRefData
*newRefData
= new wxImageRefData();
1065 newRefData
->m_width
= M_IMGDATA
->m_width
;
1066 newRefData
->m_height
= M_IMGDATA
->m_height
;
1067 newRefData
->m_data
= data
;
1068 newRefData
->m_ok
= true;
1069 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1070 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1071 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1072 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1073 newRefData
->m_static
= static_data
;
1077 m_refData
= newRefData
;
1080 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1082 wxImageRefData
*newRefData
= new wxImageRefData();
1086 newRefData
->m_width
= new_width
;
1087 newRefData
->m_height
= new_height
;
1088 newRefData
->m_data
= data
;
1089 newRefData
->m_ok
= true;
1090 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1091 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1092 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1093 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1097 newRefData
->m_width
= new_width
;
1098 newRefData
->m_height
= new_height
;
1099 newRefData
->m_data
= data
;
1100 newRefData
->m_ok
= true;
1102 newRefData
->m_static
= static_data
;
1106 m_refData
= newRefData
;
1109 // ----------------------------------------------------------------------------
1110 // alpha channel support
1111 // ----------------------------------------------------------------------------
1113 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1115 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1117 long pos
= XYToIndex(x
, y
);
1118 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1122 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1125 unsigned char wxImage::GetAlpha(int x
, int y
) const
1127 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1129 long pos
= XYToIndex(x
, y
);
1130 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1132 return M_IMGDATA
->m_alpha
[pos
];
1136 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1140 const int w
= M_IMGDATA
->m_width
;
1141 const int h
= M_IMGDATA
->m_height
;
1143 unsigned char *alpha
= GetAlpha();
1144 unsigned char *data
= GetData();
1146 for ( int y
= 0; y
< h
; y
++ )
1148 for ( int x
= 0; x
< w
; x
++ )
1160 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1162 wxCHECK_RET( Ok(), wxT("invalid image") );
1168 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1171 free(M_IMGDATA
->m_alpha
);
1172 M_IMGDATA
->m_alpha
= alpha
;
1173 M_IMGDATA
->m_staticAlpha
= static_data
;
1176 unsigned char *wxImage::GetAlpha() const
1178 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1180 return M_IMGDATA
->m_alpha
;
1183 void wxImage::InitAlpha()
1185 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1187 // initialize memory for alpha channel
1190 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1191 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1195 // use the mask to initialize the alpha channel.
1196 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1198 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1199 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1200 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1201 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1205 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1206 ? wxIMAGE_ALPHA_TRANSPARENT
1207 : wxIMAGE_ALPHA_OPAQUE
;
1210 M_IMGDATA
->m_hasMask
= false;
1214 // make the image fully opaque
1215 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1219 // ----------------------------------------------------------------------------
1221 // ----------------------------------------------------------------------------
1223 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1225 wxCHECK_RET( Ok(), wxT("invalid image") );
1229 M_IMGDATA
->m_maskRed
= r
;
1230 M_IMGDATA
->m_maskGreen
= g
;
1231 M_IMGDATA
->m_maskBlue
= b
;
1232 M_IMGDATA
->m_hasMask
= true;
1235 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1237 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1239 if (M_IMGDATA
->m_hasMask
)
1241 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1242 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1243 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1248 FindFirstUnusedColour(r
, g
, b
);
1253 unsigned char wxImage::GetMaskRed() const
1255 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1257 return M_IMGDATA
->m_maskRed
;
1260 unsigned char wxImage::GetMaskGreen() const
1262 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1264 return M_IMGDATA
->m_maskGreen
;
1267 unsigned char wxImage::GetMaskBlue() const
1269 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1271 return M_IMGDATA
->m_maskBlue
;
1274 void wxImage::SetMask( bool mask
)
1276 wxCHECK_RET( Ok(), wxT("invalid image") );
1280 M_IMGDATA
->m_hasMask
= mask
;
1283 bool wxImage::HasMask() const
1285 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1287 return M_IMGDATA
->m_hasMask
;
1290 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1292 long pos
= XYToIndex(x
, y
);
1293 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1296 if ( M_IMGDATA
->m_hasMask
)
1298 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1299 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1300 p
[1] == M_IMGDATA
->m_maskGreen
&&
1301 p
[2] == M_IMGDATA
->m_maskBlue
)
1308 if ( M_IMGDATA
->m_alpha
)
1310 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1312 // transparent enough
1321 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1322 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1324 // check that the images are the same size
1325 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1327 wxLogError( _("Image and mask have different sizes.") );
1331 // find unused colour
1332 unsigned char r
,g
,b
;
1333 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1335 wxLogError( _("No unused colour in image being masked.") );
1341 unsigned char *imgdata
= GetData();
1342 unsigned char *maskdata
= mask
.GetData();
1344 const int w
= GetWidth();
1345 const int h
= GetHeight();
1347 for (int j
= 0; j
< h
; j
++)
1349 for (int i
= 0; i
< w
; i
++)
1351 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1362 SetMaskColour(r
, g
, b
);
1368 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1373 unsigned char mr
, mg
, mb
;
1374 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1376 wxLogError( _("No unused colour in image being masked.") );
1383 SetMaskColour(mr
, mg
, mb
);
1385 unsigned char *imgdata
= GetData();
1386 unsigned char *alphadata
= GetAlpha();
1389 int h
= GetHeight();
1391 for (int y
= 0; y
< h
; y
++)
1393 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1395 if (*alphadata
< threshold
)
1404 free(M_IMGDATA
->m_alpha
);
1405 M_IMGDATA
->m_alpha
= NULL
;
1410 // ----------------------------------------------------------------------------
1411 // Palette functions
1412 // ----------------------------------------------------------------------------
1416 bool wxImage::HasPalette() const
1421 return M_IMGDATA
->m_palette
.Ok();
1424 const wxPalette
& wxImage::GetPalette() const
1426 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1428 return M_IMGDATA
->m_palette
;
1431 void wxImage::SetPalette(const wxPalette
& palette
)
1433 wxCHECK_RET( Ok(), wxT("invalid image") );
1437 M_IMGDATA
->m_palette
= palette
;
1440 #endif // wxUSE_PALETTE
1442 // ----------------------------------------------------------------------------
1443 // Option functions (arbitrary name/value mapping)
1444 // ----------------------------------------------------------------------------
1446 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1448 wxCHECK_RET( Ok(), wxT("invalid image") );
1452 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1453 if (idx
== wxNOT_FOUND
)
1455 M_IMGDATA
->m_optionNames
.Add(name
);
1456 M_IMGDATA
->m_optionValues
.Add(value
);
1460 M_IMGDATA
->m_optionNames
[idx
] = name
;
1461 M_IMGDATA
->m_optionValues
[idx
] = value
;
1465 void wxImage::SetOption(const wxString
& name
, int value
)
1468 valStr
.Printf(wxT("%d"), value
);
1469 SetOption(name
, valStr
);
1472 wxString
wxImage::GetOption(const wxString
& name
) const
1474 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1476 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1477 if (idx
== wxNOT_FOUND
)
1478 return wxEmptyString
;
1480 return M_IMGDATA
->m_optionValues
[idx
];
1483 int wxImage::GetOptionInt(const wxString
& name
) const
1485 return wxAtoi(GetOption(name
));
1488 bool wxImage::HasOption(const wxString
& name
) const
1490 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1492 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1499 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1500 long WXUNUSED_UNLESS_STREAMS(type
),
1501 int WXUNUSED_UNLESS_STREAMS(index
) )
1504 if (wxFileExists(filename
))
1506 wxFileInputStream
stream(filename
);
1507 wxBufferedInputStream
bstream( stream
);
1508 return LoadFile(bstream
, type
, index
);
1512 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1516 #else // !wxUSE_STREAMS
1518 #endif // wxUSE_STREAMS
1521 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1522 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
1523 int WXUNUSED_UNLESS_STREAMS(index
) )
1526 if (wxFileExists(filename
))
1528 wxFileInputStream
stream(filename
);
1529 wxBufferedInputStream
bstream( stream
);
1530 return LoadFile(bstream
, mimetype
, index
);
1534 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1538 #else // !wxUSE_STREAMS
1540 #endif // wxUSE_STREAMS
1545 bool wxImage::SaveFile( const wxString
& filename
) const
1547 wxString ext
= filename
.AfterLast('.').Lower();
1549 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1552 SaveFile(filename
, pHandler
->GetType());
1556 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1561 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1562 int WXUNUSED_UNLESS_STREAMS(type
) ) const
1565 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1567 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1569 wxFileOutputStream
stream(filename
);
1571 if ( stream
.IsOk() )
1573 wxBufferedOutputStream
bstream( stream
);
1574 return SaveFile(bstream
, type
);
1576 #endif // wxUSE_STREAMS
1581 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1582 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
1585 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1587 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1589 wxFileOutputStream
stream(filename
);
1591 if ( stream
.IsOk() )
1593 wxBufferedOutputStream
bstream( stream
);
1594 return SaveFile(bstream
, mimetype
);
1596 #endif // wxUSE_STREAMS
1601 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
1604 wxFileInputStream
stream(name
);
1605 return CanRead(stream
);
1611 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
1612 long WXUNUSED_UNLESS_STREAMS(type
) )
1615 wxFileInputStream
stream(name
);
1617 return GetImageCount(stream
, type
);
1625 bool wxImage::CanRead( wxInputStream
&stream
)
1627 const wxList
& list
= GetHandlers();
1629 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1631 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1632 if (handler
->CanRead( stream
))
1639 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1641 wxImageHandler
*handler
;
1643 if ( type
== wxBITMAP_TYPE_ANY
)
1645 wxList
&list
=GetHandlers();
1647 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1649 handler
=(wxImageHandler
*)node
->GetData();
1650 if ( handler
->CanRead(stream
) )
1651 return handler
->GetImageCount(stream
);
1655 wxLogWarning(_("No handler found for image type."));
1659 handler
= FindHandler(type
);
1663 wxLogWarning(_("No image handler for type %d defined."), type
);
1667 if ( handler
->CanRead(stream
) )
1669 return handler
->GetImageCount(stream
);
1673 wxLogError(_("Image file is not of type %d."), type
);
1678 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1682 m_refData
= new wxImageRefData
;
1684 wxImageHandler
*handler
;
1686 if ( type
== wxBITMAP_TYPE_ANY
)
1688 wxList
&list
=GetHandlers();
1690 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1692 handler
=(wxImageHandler
*)node
->GetData();
1693 if ( handler
->CanRead(stream
) )
1694 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1698 wxLogWarning( _("No handler found for image type.") );
1702 handler
= FindHandler(type
);
1706 wxLogWarning( _("No image handler for type %d defined."), type
);
1711 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1713 wxLogError(_("Image file is not of type %d."), type
);
1717 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1720 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1724 m_refData
= new wxImageRefData
;
1726 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1730 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1735 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1737 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1741 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1744 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1746 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1748 wxImageHandler
*handler
= FindHandler(type
);
1751 wxLogWarning( _("No image handler for type %d defined."), type
);
1756 return handler
->SaveFile( (wxImage
*)this, stream
);
1759 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1761 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1763 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1766 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1771 return handler
->SaveFile( (wxImage
*)this, stream
);
1773 #endif // wxUSE_STREAMS
1775 // ----------------------------------------------------------------------------
1776 // image I/O handlers
1777 // ----------------------------------------------------------------------------
1779 void wxImage::AddHandler( wxImageHandler
*handler
)
1781 // Check for an existing handler of the type being added.
1782 if (FindHandler( handler
->GetType() ) == 0)
1784 sm_handlers
.Append( handler
);
1788 // This is not documented behaviour, merely the simplest 'fix'
1789 // for preventing duplicate additions. If someone ever has
1790 // a good reason to add and remove duplicate handlers (and they
1791 // may) we should probably refcount the duplicates.
1792 // also an issue in InsertHandler below.
1794 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1795 handler
->GetName().c_str() );
1800 void wxImage::InsertHandler( wxImageHandler
*handler
)
1802 // Check for an existing handler of the type being added.
1803 if (FindHandler( handler
->GetType() ) == 0)
1805 sm_handlers
.Insert( handler
);
1809 // see AddHandler for additional comments.
1810 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1811 handler
->GetName().c_str() );
1816 bool wxImage::RemoveHandler( const wxString
& name
)
1818 wxImageHandler
*handler
= FindHandler(name
);
1821 sm_handlers
.DeleteObject(handler
);
1829 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1831 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1834 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1835 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1837 node
= node
->GetNext();
1842 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1844 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1847 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1848 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1849 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1851 node
= node
->GetNext();
1856 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1858 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1861 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1862 if (handler
->GetType() == bitmapType
) return handler
;
1863 node
= node
->GetNext();
1868 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1870 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1873 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1874 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1875 node
= node
->GetNext();
1880 void wxImage::InitStandardHandlers()
1883 AddHandler(new wxBMPHandler
);
1884 #endif // wxUSE_STREAMS
1887 void wxImage::CleanUpHandlers()
1889 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1892 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1893 wxList::compatibility_iterator next
= node
->GetNext();
1898 sm_handlers
.Clear();
1901 wxString
wxImage::GetImageExtWildcard()
1905 wxList
& Handlers
= wxImage::GetHandlers();
1906 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1909 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1910 fmts
+= wxT("*.") + Handler
->GetExtension();
1911 Node
= Node
->GetNext();
1912 if ( Node
) fmts
+= wxT(";");
1915 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1918 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1920 const double red
= rgb
.red
/ 255.0,
1921 green
= rgb
.green
/ 255.0,
1922 blue
= rgb
.blue
/ 255.0;
1924 // find the min and max intensity (and remember which one was it for the
1926 double minimumRGB
= red
;
1927 if ( green
< minimumRGB
)
1929 if ( blue
< minimumRGB
)
1932 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1933 double maximumRGB
= red
;
1934 if ( green
> maximumRGB
)
1939 if ( blue
> maximumRGB
)
1945 const double value
= maximumRGB
;
1947 double hue
= 0.0, saturation
;
1948 const double deltaRGB
= maximumRGB
- minimumRGB
;
1949 if ( wxIsNullDouble(deltaRGB
) )
1951 // Gray has no color
1960 hue
= (green
- blue
) / deltaRGB
;
1964 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1968 hue
= 4.0 + (red
- green
) / deltaRGB
;
1972 wxFAIL_MSG(wxT("hue not specified"));
1981 saturation
= deltaRGB
/ maximumRGB
;
1984 return HSVValue(hue
, saturation
, value
);
1987 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1989 double red
, green
, blue
;
1991 if ( wxIsNullDouble(hsv
.saturation
) )
2000 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2001 int i
= (int)floor(hue
);
2002 double f
= hue
- i
; // fractional part of h
2003 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2009 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2014 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2022 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2027 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2032 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2040 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2045 return RGBValue((unsigned char)(red
* 255.0),
2046 (unsigned char)(green
* 255.0),
2047 (unsigned char)(blue
* 255.0));
2051 * Rotates the hue of each pixel of the image. angle is a double in the range
2052 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2054 void wxImage::RotateHue(double angle
)
2058 unsigned char *srcBytePtr
;
2059 unsigned char *dstBytePtr
;
2060 unsigned long count
;
2061 wxImage::HSVValue hsv
;
2062 wxImage::RGBValue rgb
;
2064 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2065 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2066 if ( count
> 0 && !wxIsNullDouble(angle
) )
2068 srcBytePtr
= M_IMGDATA
->m_data
;
2069 dstBytePtr
= srcBytePtr
;
2072 rgb
.red
= *srcBytePtr
++;
2073 rgb
.green
= *srcBytePtr
++;
2074 rgb
.blue
= *srcBytePtr
++;
2075 hsv
= RGBtoHSV(rgb
);
2077 hsv
.hue
= hsv
.hue
+ angle
;
2079 hsv
.hue
= hsv
.hue
- 1.0;
2080 else if (hsv
.hue
< 0.0)
2081 hsv
.hue
= hsv
.hue
+ 1.0;
2083 rgb
= HSVtoRGB(hsv
);
2084 *dstBytePtr
++ = rgb
.red
;
2085 *dstBytePtr
++ = rgb
.green
;
2086 *dstBytePtr
++ = rgb
.blue
;
2087 } while (--count
!= 0);
2091 //-----------------------------------------------------------------------------
2093 //-----------------------------------------------------------------------------
2095 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2098 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2103 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2108 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2113 bool wxImageHandler::CanRead( const wxString
& name
)
2115 if (wxFileExists(name
))
2117 wxFileInputStream
stream(name
);
2118 return CanRead(stream
);
2121 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2126 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2128 wxFileOffset posOld
= stream
.TellI();
2129 if ( posOld
== wxInvalidOffset
)
2131 // can't test unseekable stream
2135 bool ok
= DoCanRead(stream
);
2137 // restore the old position to be able to test other formats and so on
2138 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2140 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2142 // reading would fail anyhow as we're not at the right position
2149 #endif // wxUSE_STREAMS
2151 // ----------------------------------------------------------------------------
2152 // image histogram stuff
2153 // ----------------------------------------------------------------------------
2156 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2161 unsigned char g2
) const
2163 unsigned long key
= MakeKey(r2
, g2
, b2
);
2165 while ( find(key
) != end() )
2167 // color already used
2179 wxLogError(_("No unused colour in image.") );
2185 key
= MakeKey(r2
, g2
, b2
);
2199 wxImage::FindFirstUnusedColour(unsigned char *r
,
2204 unsigned char g2
) const
2206 wxImageHistogram histogram
;
2208 ComputeHistogram(histogram
);
2210 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2216 // Counts and returns the number of different colours. Optionally stops
2217 // when it exceeds 'stopafter' different colours. This is useful, for
2218 // example, to see if the image can be saved as 8-bit (256 colour or
2219 // less, in this case it would be invoked as CountColours(256)). Default
2220 // value for stopafter is -1 (don't care).
2222 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2226 unsigned char r
, g
, b
;
2228 unsigned long size
, nentries
, key
;
2231 size
= GetWidth() * GetHeight();
2234 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2239 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2241 if (h
.Get(key
) == NULL
)
2252 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2254 unsigned char *p
= GetData();
2255 unsigned long nentries
= 0;
2259 const unsigned long size
= GetWidth() * GetHeight();
2261 unsigned char r
, g
, b
;
2262 for ( unsigned long n
= 0; n
< size
; n
++ )
2268 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2270 if ( entry
.value
++ == 0 )
2271 entry
.index
= nentries
++;
2278 * Rotation code by Carlos Moreno
2281 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2282 // does exactly the same thing. And I also got rid of wxRotationPixel
2283 // bacause of potential problems in architectures where alignment
2284 // is an issue, so I had to rewrite parts of the code.
2286 static const double gs_Epsilon
= 1e-10;
2288 static inline int wxCint (double x
)
2290 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2294 // Auxiliary function to rotate a point (x,y) with respect to point p0
2295 // make it inline and use a straight return to facilitate optimization
2296 // also, the function receives the sine and cosine of the angle to avoid
2297 // repeating the time-consuming calls to these functions -- sin/cos can
2298 // be computed and stored in the calling function.
2300 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2302 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2303 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2306 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2308 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2311 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2314 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2316 bool has_alpha
= HasAlpha();
2318 // Create pointer-based array to accelerate access to wxImage's data
2319 unsigned char ** data
= new unsigned char * [GetHeight()];
2320 data
[0] = GetData();
2321 for (i
= 1; i
< GetHeight(); i
++)
2322 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2324 // Same for alpha channel
2325 unsigned char ** alpha
= NULL
;
2328 alpha
= new unsigned char * [GetHeight()];
2329 alpha
[0] = GetAlpha();
2330 for (i
= 1; i
< GetHeight(); i
++)
2331 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2334 // precompute coefficients for rotation formula
2335 // (sine and cosine of the angle)
2336 const double cos_angle
= cos(angle
);
2337 const double sin_angle
= sin(angle
);
2339 // Create new Image to store the result
2340 // First, find rectangle that covers the rotated image; to do that,
2341 // rotate the four corners
2343 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2345 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2346 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2347 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2348 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2350 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2351 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2352 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2353 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2355 // Create rotated image
2356 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2357 // With alpha channel
2361 if (offset_after_rotation
!= NULL
)
2363 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2366 // GRG: The rotated (destination) image is always accessed
2367 // sequentially, so there is no need for a pointer-based
2368 // array here (and in fact it would be slower).
2370 unsigned char * dst
= rotated
.GetData();
2372 unsigned char * alpha_dst
= NULL
;
2374 alpha_dst
= rotated
.GetAlpha();
2376 // GRG: if the original image has a mask, use its RGB values
2377 // as the blank pixel, else, fall back to default (black).
2379 unsigned char blank_r
= 0;
2380 unsigned char blank_g
= 0;
2381 unsigned char blank_b
= 0;
2385 blank_r
= GetMaskRed();
2386 blank_g
= GetMaskGreen();
2387 blank_b
= GetMaskBlue();
2388 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2391 // Now, for each point of the rotated image, find where it came from, by
2392 // performing an inverse rotation (a rotation of -angle) and getting the
2393 // pixel at those coordinates
2395 // GRG: I've taken the (interpolating) test out of the loops, so that
2396 // it is done only once, instead of repeating it for each pixel.
2401 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2403 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2405 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2407 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2408 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2410 // interpolate using the 4 enclosing grid-points. Those
2411 // points can be obtained using floor and ceiling of the
2412 // exact coordinates of the point
2415 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2417 x1
= wxCint(floor(src
.x
));
2418 x2
= wxCint(ceil(src
.x
));
2420 else // else means that x is near one of the borders (0 or width-1)
2422 x1
= x2
= wxCint (src
.x
);
2425 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2427 y1
= wxCint(floor(src
.y
));
2428 y2
= wxCint(ceil(src
.y
));
2432 y1
= y2
= wxCint (src
.y
);
2435 // get four points and the distances (square of the distance,
2436 // for efficiency reasons) for the interpolation formula
2438 // GRG: Do not calculate the points until they are
2439 // really needed -- this way we can calculate
2440 // just one, instead of four, if d1, d2, d3
2441 // or d4 are < gs_Epsilon
2443 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2444 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2445 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2446 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2448 // Now interpolate as a weighted average of the four surrounding
2449 // points, where the weights are the distances to each of those points
2451 // If the point is exactly at one point of the grid of the source
2452 // image, then don't interpolate -- just assign the pixel
2454 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2456 unsigned char *p
= data
[y1
] + (3 * x1
);
2462 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2464 else if (d2
< gs_Epsilon
)
2466 unsigned char *p
= data
[y1
] + (3 * x2
);
2472 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2474 else if (d3
< gs_Epsilon
)
2476 unsigned char *p
= data
[y2
] + (3 * x2
);
2482 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2484 else if (d4
< gs_Epsilon
)
2486 unsigned char *p
= data
[y2
] + (3 * x1
);
2492 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2496 // weights for the weighted average are proportional to the inverse of the distance
2497 unsigned char *v1
= data
[y1
] + (3 * x1
);
2498 unsigned char *v2
= data
[y1
] + (3 * x2
);
2499 unsigned char *v3
= data
[y2
] + (3 * x2
);
2500 unsigned char *v4
= data
[y2
] + (3 * x1
);
2502 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2506 *(dst
++) = (unsigned char)
2507 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2508 w3
* *(v3
++) + w4
* *(v4
++)) /
2509 (w1
+ w2
+ w3
+ w4
) );
2510 *(dst
++) = (unsigned char)
2511 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2512 w3
* *(v3
++) + w4
* *(v4
++)) /
2513 (w1
+ w2
+ w3
+ w4
) );
2514 *(dst
++) = (unsigned char)
2515 ( (w1
* *v1
+ w2
* *v2
+
2516 w3
* *v3
+ w4
* *v4
) /
2517 (w1
+ w2
+ w3
+ w4
) );
2521 v1
= alpha
[y1
] + (x1
);
2522 v2
= alpha
[y1
] + (x2
);
2523 v3
= alpha
[y2
] + (x2
);
2524 v4
= alpha
[y2
] + (x1
);
2526 *(alpha_dst
++) = (unsigned char)
2527 ( (w1
* *v1
+ w2
* *v2
+
2528 w3
* *v3
+ w4
* *v4
) /
2529 (w1
+ w2
+ w3
+ w4
) );
2545 else // not interpolating
2547 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2549 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2551 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2553 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2554 const int ys
= wxCint (src
.y
); // closest integer
2556 if (0 <= xs
&& xs
< GetWidth() &&
2557 0 <= ys
&& ys
< GetHeight())
2559 unsigned char *p
= data
[ys
] + (3 * xs
);
2565 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2574 *(alpha_dst
++) = 255;
2592 // A module to allow wxImage initialization/cleanup
2593 // without calling these functions from app.cpp or from
2594 // the user's application.
2596 class wxImageModule
: public wxModule
2598 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2601 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2602 void OnExit() { wxImage::CleanUpHandlers(); };
2605 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2608 #endif // wxUSE_IMAGE