1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
35 #include "wx/xpmdecod.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 class wxImageRefData
: public wxObjectRefData
49 virtual ~wxImageRefData();
53 unsigned char *m_data
;
56 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
58 // alpha channel data, may be NULL for the formats without alpha support
59 unsigned char *m_alpha
;
63 // if true, m_data is pointer to static data and shouldn't be freed
66 // same as m_static but for m_alpha
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
93 m_staticAlpha
= false;
96 wxImageRefData::~wxImageRefData()
100 if ( !m_staticAlpha
)
104 wxList
wxImage::sm_handlers
;
108 //-----------------------------------------------------------------------------
110 #define M_IMGDATA ((wxImageRefData *)m_refData)
112 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
114 wxImage::wxImage( int width
, int height
, bool clear
)
116 Create( width
, height
, clear
);
119 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
121 Create( width
, height
, data
, static_data
);
124 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
126 Create( width
, height
, data
, alpha
, static_data
);
129 wxImage::wxImage( const wxString
& name
, long type
, int index
)
131 LoadFile( name
, type
, index
);
134 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
136 LoadFile( name
, mimetype
, index
);
140 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
142 LoadFile( stream
, type
, index
);
145 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
147 LoadFile( stream
, mimetype
, index
);
149 #endif // wxUSE_STREAMS
151 wxImage::wxImage( const char** xpmData
)
156 wxImage::wxImage( char** xpmData
)
158 Create((const char**) xpmData
);
161 bool wxImage::Create( const char** xpmData
)
166 wxXPMDecoder decoder
;
167 (*this) = decoder
.ReadData(xpmData
);
174 bool wxImage::Create( int width
, int height
, bool clear
)
178 m_refData
= new wxImageRefData();
180 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
181 if (!M_IMGDATA
->m_data
)
188 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
190 M_IMGDATA
->m_width
= width
;
191 M_IMGDATA
->m_height
= height
;
192 M_IMGDATA
->m_ok
= true;
197 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
201 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
203 m_refData
= new wxImageRefData();
205 M_IMGDATA
->m_data
= data
;
206 M_IMGDATA
->m_width
= width
;
207 M_IMGDATA
->m_height
= height
;
208 M_IMGDATA
->m_ok
= true;
209 M_IMGDATA
->m_static
= static_data
;
214 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
218 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
220 m_refData
= new wxImageRefData();
222 M_IMGDATA
->m_data
= data
;
223 M_IMGDATA
->m_alpha
= alpha
;
224 M_IMGDATA
->m_width
= width
;
225 M_IMGDATA
->m_height
= height
;
226 M_IMGDATA
->m_ok
= true;
227 M_IMGDATA
->m_static
= static_data
;
232 void wxImage::Destroy()
237 wxImage
wxImage::Copy() const
241 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
243 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
245 unsigned char *data
= image
.GetData();
247 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
249 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
250 image
.SetMask( M_IMGDATA
->m_hasMask
);
252 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
254 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
256 // also copy the alpha channel
260 unsigned char* alpha
= image
.GetAlpha();
261 memcpy( alpha
, GetAlpha(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
264 // also copy the image options
265 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
266 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
271 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
273 if( xFactor
== 1 && yFactor
== 1 )
278 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
280 // can't scale to/from 0 size
281 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
282 wxT("invalid new image size") );
284 long old_height
= M_IMGDATA
->m_height
,
285 old_width
= M_IMGDATA
->m_width
;
287 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
288 wxT("invalid old image size") );
290 long width
= old_width
/ xFactor
;
291 long height
= old_height
/ yFactor
;
293 image
.Create( width
, height
, false );
295 char unsigned *data
= image
.GetData();
297 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
299 bool hasMask
= false ;
300 unsigned char maskRed
= 0;
301 unsigned char maskGreen
= 0;
302 unsigned char maskBlue
=0 ;
304 unsigned char *source_data
= M_IMGDATA
->m_data
;
305 unsigned char *target_data
= data
;
306 unsigned char *source_alpha
= 0 ;
307 unsigned char *target_alpha
= 0 ;
308 if (M_IMGDATA
->m_hasMask
)
311 maskRed
= M_IMGDATA
->m_maskRed
;
312 maskGreen
= M_IMGDATA
->m_maskGreen
;
313 maskBlue
=M_IMGDATA
->m_maskBlue
;
315 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
316 M_IMGDATA
->m_maskGreen
,
317 M_IMGDATA
->m_maskBlue
);
321 source_alpha
= M_IMGDATA
->m_alpha
;
325 target_alpha
= image
.GetAlpha() ;
329 for (long y
= 0; y
< height
; y
++)
331 for (long x
= 0; x
< width
; x
++)
333 unsigned long avgRed
= 0 ;
334 unsigned long avgGreen
= 0;
335 unsigned long avgBlue
= 0;
336 unsigned long avgAlpha
= 0 ;
337 unsigned long counter
= 0 ;
339 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
341 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
342 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
344 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
345 unsigned char red
= pixel
[0] ;
346 unsigned char green
= pixel
[1] ;
347 unsigned char blue
= pixel
[2] ;
348 unsigned char alpha
= 255 ;
350 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
351 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
366 *(target_data
++) = M_IMGDATA
->m_maskRed
;
367 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
368 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
373 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
374 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
375 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
376 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
381 // In case this is a cursor, make sure the hotspot is scaled accordingly:
382 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
383 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
384 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
385 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
386 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
387 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
392 wxImage
wxImage::Scale( int width
, int height
) const
396 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
398 // can't scale to/from 0 size
399 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
400 wxT("invalid new image size") );
402 long old_height
= M_IMGDATA
->m_height
,
403 old_width
= M_IMGDATA
->m_width
;
404 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
405 wxT("invalid old image size") );
407 if ( old_width
% width
== 0 && old_width
>= width
&&
408 old_height
% height
== 0 && old_height
>= height
)
410 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
412 image
.Create( width
, height
, false );
414 unsigned char *data
= image
.GetData();
416 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
418 unsigned char *source_data
= M_IMGDATA
->m_data
;
419 unsigned char *target_data
= data
;
420 unsigned char *source_alpha
= 0 ;
421 unsigned char *target_alpha
= 0 ;
423 if (M_IMGDATA
->m_hasMask
)
425 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
426 M_IMGDATA
->m_maskGreen
,
427 M_IMGDATA
->m_maskBlue
);
431 source_alpha
= M_IMGDATA
->m_alpha
;
435 target_alpha
= image
.GetAlpha() ;
439 long x_delta
= (old_width
<<16) / width
;
440 long y_delta
= (old_height
<<16) / height
;
442 unsigned char* dest_pixel
= target_data
;
445 for ( long j
= 0; j
< height
; j
++ )
447 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
448 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
451 for ( long i
= 0; i
< width
; i
++ )
453 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
454 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
455 dest_pixel
[0] = src_pixel
[0];
456 dest_pixel
[1] = src_pixel
[1];
457 dest_pixel
[2] = src_pixel
[2];
460 *(target_alpha
++) = *src_alpha_pixel
;
467 // In case this is a cursor, make sure the hotspot is scaled accordingly:
468 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
469 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
470 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
471 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
472 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
473 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
478 wxImage
wxImage::Rotate90( bool clockwise
) const
482 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
484 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
486 unsigned char *data
= image
.GetData();
488 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
490 unsigned char *source_data
= M_IMGDATA
->m_data
;
491 unsigned char *target_data
;
492 unsigned char *alpha_data
= 0 ;
493 unsigned char *source_alpha
= 0 ;
494 unsigned char *target_alpha
= 0 ;
496 if (M_IMGDATA
->m_hasMask
)
498 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
502 source_alpha
= M_IMGDATA
->m_alpha
;
506 alpha_data
= image
.GetAlpha() ;
510 long height
= M_IMGDATA
->m_height
;
511 long width
= M_IMGDATA
->m_width
;
513 for (long j
= 0; j
< height
; j
++)
515 for (long i
= 0; i
< width
; i
++)
519 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
521 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
525 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
527 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
529 memcpy( target_data
, source_data
, 3 );
534 memcpy( target_alpha
, source_alpha
, 1 );
543 wxImage
wxImage::Mirror( bool horizontally
) const
547 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
549 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
551 unsigned char *data
= image
.GetData();
552 unsigned char *alpha
= NULL
;
554 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
556 if (M_IMGDATA
->m_alpha
!= NULL
) {
558 alpha
= image
.GetAlpha();
559 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
562 if (M_IMGDATA
->m_hasMask
)
563 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
565 long height
= M_IMGDATA
->m_height
;
566 long width
= M_IMGDATA
->m_width
;
568 unsigned char *source_data
= M_IMGDATA
->m_data
;
569 unsigned char *target_data
;
573 for (long j
= 0; j
< height
; j
++)
576 target_data
= data
-3;
577 for (long i
= 0; i
< width
; i
++)
579 memcpy( target_data
, source_data
, 3 );
587 // src_alpha starts at the first pixel and increases by 1 after each step
588 // (a step here is the copy of the alpha value of one pixel)
589 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
590 // dest_alpha starts just beyond the first line, decreases before each step,
591 // and after each line is finished, increases by 2 widths (skipping the line
592 // just copied and the line that will be copied next)
593 unsigned char *dest_alpha
= alpha
+ width
;
595 for (long jj
= 0; jj
< height
; ++jj
)
597 for (long i
= 0; i
< width
; ++i
) {
598 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
600 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
606 for (long i
= 0; i
< height
; i
++)
608 target_data
= data
+ 3*width
*(height
-1-i
);
609 memcpy( target_data
, source_data
, (size_t)3*width
);
610 source_data
+= 3*width
;
615 // src_alpha starts at the first pixel and increases by 1 width after each step
616 // (a step here is the copy of the alpha channel of an entire line)
617 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
618 // dest_alpha starts just beyond the last line (beyond the whole image)
619 // and decreases by 1 width before each step
620 unsigned char *dest_alpha
= alpha
+ width
* height
;
622 for (long jj
= 0; jj
< height
; ++jj
)
625 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
634 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
638 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
640 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
641 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
642 image
, wxT("invalid subimage size") );
644 const int subwidth
= rect
.GetWidth();
645 const int subheight
= rect
.GetHeight();
647 image
.Create( subwidth
, subheight
, false );
649 const unsigned char *src_data
= GetData();
650 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
651 unsigned char *subdata
= image
.GetData();
652 unsigned char *subalpha
= NULL
;
654 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
656 if (src_alpha
!= NULL
) {
658 subalpha
= image
.GetAlpha();
659 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
662 if (M_IMGDATA
->m_hasMask
)
663 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
665 const int width
= GetWidth();
666 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
668 src_data
+= 3 * pixsoff
;
669 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
671 for (long j
= 0; j
< subheight
; ++j
)
673 memcpy( subdata
, src_data
, 3 * subwidth
);
674 subdata
+= 3 * subwidth
;
675 src_data
+= 3 * width
;
676 if (subalpha
!= NULL
) {
677 memcpy( subalpha
, src_alpha
, subwidth
);
678 subalpha
+= subwidth
;
686 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
687 int r_
, int g_
, int b_
) const
691 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
692 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
694 int width
= GetWidth(), height
= GetHeight();
695 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
697 unsigned char r
= (unsigned char)r_
;
698 unsigned char g
= (unsigned char)g_
;
699 unsigned char b
= (unsigned char)b_
;
700 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
702 GetOrFindMaskColour( &r
, &g
, &b
);
703 image
.SetMaskColour(r
, g
, b
);
706 image
.SetRGB(wxRect(), r
, g
, b
);
708 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
709 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
711 subRect
.Intersect(finalRect
);
713 if (!subRect
.IsEmpty())
715 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
716 image
.Paste(*this, pos
.x
, pos
.y
);
718 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
724 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
726 wxCHECK_RET( Ok(), wxT("invalid image") );
727 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
731 int width
= image
.GetWidth();
732 int height
= image
.GetHeight();
745 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
746 width
= M_IMGDATA
->m_width
- (x
+xx
);
747 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
748 height
= M_IMGDATA
->m_height
- (y
+yy
);
750 if (width
< 1) return;
751 if (height
< 1) return;
753 if ((!HasMask() && !image
.HasMask()) ||
754 (HasMask() && !image
.HasMask()) ||
755 ((HasMask() && image
.HasMask() &&
756 (GetMaskRed()==image
.GetMaskRed()) &&
757 (GetMaskGreen()==image
.GetMaskGreen()) &&
758 (GetMaskBlue()==image
.GetMaskBlue()))))
761 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
762 int source_step
= image
.GetWidth()*3;
764 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
765 int target_step
= M_IMGDATA
->m_width
*3;
766 for (int j
= 0; j
< height
; j
++)
768 memcpy( target_data
, source_data
, width
);
769 source_data
+= source_step
;
770 target_data
+= target_step
;
775 if (!HasMask() && image
.HasMask())
777 unsigned char r
= image
.GetMaskRed();
778 unsigned char g
= image
.GetMaskGreen();
779 unsigned char b
= image
.GetMaskBlue();
782 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
783 int source_step
= image
.GetWidth()*3;
785 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
786 int target_step
= M_IMGDATA
->m_width
*3;
788 for (int j
= 0; j
< height
; j
++)
790 for (int i
= 0; i
< width
; i
+=3)
792 if ((source_data
[i
] != r
) &&
793 (source_data
[i
+1] != g
) &&
794 (source_data
[i
+2] != b
))
796 memcpy( target_data
+i
, source_data
+i
, 3 );
799 source_data
+= source_step
;
800 target_data
+= target_step
;
805 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
806 unsigned char r2
, unsigned char g2
, unsigned char b2
)
808 wxCHECK_RET( Ok(), wxT("invalid image") );
810 unsigned char *data
= GetData();
812 const int w
= GetWidth();
813 const int h
= GetHeight();
815 for (int j
= 0; j
< h
; j
++)
816 for (int i
= 0; i
< w
; i
++)
818 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
828 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
832 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
834 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
836 unsigned char *dest
= image
.GetData();
838 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
840 unsigned char *src
= M_IMGDATA
->m_data
;
841 bool hasMask
= M_IMGDATA
->m_hasMask
;
842 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
843 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
844 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
847 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
849 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
850 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
852 // don't modify the mask
853 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
855 memcpy(dest
, src
, 3);
859 // calculate the luma
860 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
861 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
868 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
872 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
874 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
876 unsigned char *data
= image
.GetData();
878 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
880 if (M_IMGDATA
->m_hasMask
)
882 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
883 M_IMGDATA
->m_maskBlue
== b
)
884 image
.SetMaskColour( 255, 255, 255 );
886 image
.SetMaskColour( 0, 0, 0 );
889 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
891 unsigned char *srcd
= M_IMGDATA
->m_data
;
892 unsigned char *tard
= image
.GetData();
894 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
896 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
897 tard
[0] = tard
[1] = tard
[2] = 255;
899 tard
[0] = tard
[1] = tard
[2] = 0;
905 int wxImage::GetWidth() const
907 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
909 return M_IMGDATA
->m_width
;
912 int wxImage::GetHeight() const
914 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
916 return M_IMGDATA
->m_height
;
919 long wxImage::XYToIndex(int x
, int y
) const
923 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
925 return y
*M_IMGDATA
->m_width
+ x
;
931 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
933 long pos
= XYToIndex(x
, y
);
934 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
938 M_IMGDATA
->m_data
[ pos
] = r
;
939 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
940 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
943 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
945 wxCHECK_RET( Ok(), wxT("invalid image") );
948 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
949 if ( rect
== wxRect() )
955 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
956 imageRect
.Inside(rect
.GetBottomRight()),
957 wxT("invalid bounding rectangle") );
960 int x1
= rect
.GetLeft(),
962 x2
= rect
.GetRight() + 1,
963 y2
= rect
.GetBottom() + 1;
965 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
966 int x
, y
, width
= GetWidth();
967 for (y
= y1
; y
< y2
; y
++)
969 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
970 for (x
= x1
; x
< x2
; x
++)
979 unsigned char wxImage::GetRed( int x
, int y
) const
981 long pos
= XYToIndex(x
, y
);
982 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
986 return M_IMGDATA
->m_data
[pos
];
989 unsigned char wxImage::GetGreen( int x
, int y
) const
991 long pos
= XYToIndex(x
, y
);
992 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
996 return M_IMGDATA
->m_data
[pos
+1];
999 unsigned char wxImage::GetBlue( int x
, int y
) const
1001 long pos
= XYToIndex(x
, y
);
1002 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1006 return M_IMGDATA
->m_data
[pos
+2];
1009 bool wxImage::Ok() const
1011 // image of 0 width or height can't be considered ok - at least because it
1012 // causes crashes in ConvertToBitmap() if we don't catch it in time
1013 wxImageRefData
*data
= M_IMGDATA
;
1014 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1017 unsigned char *wxImage::GetData() const
1019 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1021 return M_IMGDATA
->m_data
;
1024 void wxImage::SetData( unsigned char *data
, bool static_data
)
1026 wxCHECK_RET( Ok(), wxT("invalid image") );
1028 wxImageRefData
*newRefData
= new wxImageRefData();
1030 newRefData
->m_width
= M_IMGDATA
->m_width
;
1031 newRefData
->m_height
= M_IMGDATA
->m_height
;
1032 newRefData
->m_data
= data
;
1033 newRefData
->m_ok
= true;
1034 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1035 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1036 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1037 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1038 newRefData
->m_static
= static_data
;
1042 m_refData
= newRefData
;
1045 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1047 wxImageRefData
*newRefData
= new wxImageRefData();
1051 newRefData
->m_width
= new_width
;
1052 newRefData
->m_height
= new_height
;
1053 newRefData
->m_data
= data
;
1054 newRefData
->m_ok
= true;
1055 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1056 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1057 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1058 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1062 newRefData
->m_width
= new_width
;
1063 newRefData
->m_height
= new_height
;
1064 newRefData
->m_data
= data
;
1065 newRefData
->m_ok
= true;
1067 newRefData
->m_static
= static_data
;
1071 m_refData
= newRefData
;
1074 // ----------------------------------------------------------------------------
1075 // alpha channel support
1076 // ----------------------------------------------------------------------------
1078 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1080 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1082 long pos
= XYToIndex(x
, y
);
1083 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1085 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1088 unsigned char wxImage::GetAlpha(int x
, int y
) const
1090 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1092 long pos
= XYToIndex(x
, y
);
1093 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1095 return M_IMGDATA
->m_alpha
[pos
];
1099 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1103 const int w
= M_IMGDATA
->m_width
;
1104 const int h
= M_IMGDATA
->m_height
;
1106 unsigned char *alpha
= GetAlpha();
1107 unsigned char *data
= GetData();
1109 for ( int y
= 0; y
< h
; y
++ )
1111 for ( int x
= 0; x
< w
; x
++ )
1123 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1125 wxCHECK_RET( Ok(), wxT("invalid image") );
1129 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1132 free(M_IMGDATA
->m_alpha
);
1133 M_IMGDATA
->m_alpha
= alpha
;
1134 M_IMGDATA
->m_staticAlpha
= static_data
;
1137 unsigned char *wxImage::GetAlpha() const
1139 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1141 return M_IMGDATA
->m_alpha
;
1144 void wxImage::InitAlpha()
1146 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1148 // initialize memory for alpha channel
1151 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1152 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1156 // use the mask to initialize the alpha channel.
1157 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1159 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1160 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1161 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1162 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1166 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1167 ? wxIMAGE_ALPHA_TRANSPARENT
1168 : wxIMAGE_ALPHA_OPAQUE
;
1171 M_IMGDATA
->m_hasMask
= false;
1175 // make the image fully opaque
1176 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1180 // ----------------------------------------------------------------------------
1182 // ----------------------------------------------------------------------------
1184 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1186 wxCHECK_RET( Ok(), wxT("invalid image") );
1188 M_IMGDATA
->m_maskRed
= r
;
1189 M_IMGDATA
->m_maskGreen
= g
;
1190 M_IMGDATA
->m_maskBlue
= b
;
1191 M_IMGDATA
->m_hasMask
= true;
1194 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1196 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1198 if (M_IMGDATA
->m_hasMask
)
1200 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1201 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1202 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1207 FindFirstUnusedColour(r
, g
, b
);
1212 unsigned char wxImage::GetMaskRed() const
1214 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1216 return M_IMGDATA
->m_maskRed
;
1219 unsigned char wxImage::GetMaskGreen() const
1221 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1223 return M_IMGDATA
->m_maskGreen
;
1226 unsigned char wxImage::GetMaskBlue() const
1228 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1230 return M_IMGDATA
->m_maskBlue
;
1233 void wxImage::SetMask( bool mask
)
1235 wxCHECK_RET( Ok(), wxT("invalid image") );
1237 M_IMGDATA
->m_hasMask
= mask
;
1240 bool wxImage::HasMask() const
1242 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1244 return M_IMGDATA
->m_hasMask
;
1247 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1249 long pos
= XYToIndex(x
, y
);
1250 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1253 if ( M_IMGDATA
->m_hasMask
)
1255 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1256 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1257 p
[1] == M_IMGDATA
->m_maskGreen
&&
1258 p
[2] == M_IMGDATA
->m_maskBlue
)
1265 if ( M_IMGDATA
->m_alpha
)
1267 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1269 // transparent enough
1278 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1279 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1281 // check that the images are the same size
1282 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1284 wxLogError( _("Image and mask have different sizes.") );
1288 // find unused colour
1289 unsigned char r
,g
,b
;
1290 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1292 wxLogError( _("No unused colour in image being masked.") );
1296 unsigned char *imgdata
= GetData();
1297 unsigned char *maskdata
= mask
.GetData();
1299 const int w
= GetWidth();
1300 const int h
= GetHeight();
1302 for (int j
= 0; j
< h
; j
++)
1304 for (int i
= 0; i
< w
; i
++)
1306 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1317 SetMaskColour(r
, g
, b
);
1323 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1328 unsigned char mr
, mg
, mb
;
1329 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1331 wxLogError( _("No unused colour in image being masked.") );
1336 SetMaskColour(mr
, mg
, mb
);
1338 unsigned char *imgdata
= GetData();
1339 unsigned char *alphadata
= GetAlpha();
1342 int h
= GetHeight();
1344 for (int y
= 0; y
< h
; y
++)
1346 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1348 if (*alphadata
< threshold
)
1357 free(M_IMGDATA
->m_alpha
);
1358 M_IMGDATA
->m_alpha
= NULL
;
1363 // ----------------------------------------------------------------------------
1364 // Palette functions
1365 // ----------------------------------------------------------------------------
1369 bool wxImage::HasPalette() const
1374 return M_IMGDATA
->m_palette
.Ok();
1377 const wxPalette
& wxImage::GetPalette() const
1379 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1381 return M_IMGDATA
->m_palette
;
1384 void wxImage::SetPalette(const wxPalette
& palette
)
1386 wxCHECK_RET( Ok(), wxT("invalid image") );
1388 M_IMGDATA
->m_palette
= palette
;
1391 #endif // wxUSE_PALETTE
1393 // ----------------------------------------------------------------------------
1394 // Option functions (arbitrary name/value mapping)
1395 // ----------------------------------------------------------------------------
1397 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1399 wxCHECK_RET( Ok(), wxT("invalid image") );
1401 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1402 if (idx
== wxNOT_FOUND
)
1404 M_IMGDATA
->m_optionNames
.Add(name
);
1405 M_IMGDATA
->m_optionValues
.Add(value
);
1409 M_IMGDATA
->m_optionNames
[idx
] = name
;
1410 M_IMGDATA
->m_optionValues
[idx
] = value
;
1414 void wxImage::SetOption(const wxString
& name
, int value
)
1417 valStr
.Printf(wxT("%d"), value
);
1418 SetOption(name
, valStr
);
1421 wxString
wxImage::GetOption(const wxString
& name
) const
1423 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1425 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1426 if (idx
== wxNOT_FOUND
)
1427 return wxEmptyString
;
1429 return M_IMGDATA
->m_optionValues
[idx
];
1432 int wxImage::GetOptionInt(const wxString
& name
) const
1434 return wxAtoi(GetOption(name
));
1437 bool wxImage::HasOption(const wxString
& name
) const
1439 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1441 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1444 // ----------------------------------------------------------------------------
1446 // ----------------------------------------------------------------------------
1448 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1451 if (wxFileExists(filename
))
1453 wxFileInputStream
stream(filename
);
1454 wxBufferedInputStream
bstream( stream
);
1455 return LoadFile(bstream
, type
, index
);
1459 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1463 #else // !wxUSE_STREAMS
1465 #endif // wxUSE_STREAMS
1468 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1471 if (wxFileExists(filename
))
1473 wxFileInputStream
stream(filename
);
1474 wxBufferedInputStream
bstream( stream
);
1475 return LoadFile(bstream
, mimetype
, index
);
1479 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1483 #else // !wxUSE_STREAMS
1485 #endif // wxUSE_STREAMS
1490 bool wxImage::SaveFile( const wxString
& filename
) const
1492 wxString ext
= filename
.AfterLast('.').Lower();
1494 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1497 SaveFile(filename
, pHandler
->GetType());
1501 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1506 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1509 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1511 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1513 wxFileOutputStream
stream(filename
);
1515 if ( stream
.IsOk() )
1517 wxBufferedOutputStream
bstream( stream
);
1518 return SaveFile(bstream
, type
);
1520 #endif // wxUSE_STREAMS
1525 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1528 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1530 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1532 wxFileOutputStream
stream(filename
);
1534 if ( stream
.IsOk() )
1536 wxBufferedOutputStream
bstream( stream
);
1537 return SaveFile(bstream
, mimetype
);
1539 #endif // wxUSE_STREAMS
1544 bool wxImage::CanRead( const wxString
&name
)
1547 wxFileInputStream
stream(name
);
1548 return CanRead(stream
);
1554 int wxImage::GetImageCount( const wxString
&name
, long type
)
1557 wxFileInputStream
stream(name
);
1559 return GetImageCount(stream
, type
);
1567 bool wxImage::CanRead( wxInputStream
&stream
)
1569 const wxList
& list
= GetHandlers();
1571 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1573 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1574 if (handler
->CanRead( stream
))
1581 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1583 wxImageHandler
*handler
;
1585 if ( type
== wxBITMAP_TYPE_ANY
)
1587 wxList
&list
=GetHandlers();
1589 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1591 handler
=(wxImageHandler
*)node
->GetData();
1592 if ( handler
->CanRead(stream
) )
1593 return handler
->GetImageCount(stream
);
1597 wxLogWarning(_("No handler found for image type."));
1601 handler
= FindHandler(type
);
1605 wxLogWarning(_("No image handler for type %d defined."), type
);
1609 if ( handler
->CanRead(stream
) )
1611 return handler
->GetImageCount(stream
);
1615 wxLogError(_("Image file is not of type %d."), type
);
1620 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1624 m_refData
= new wxImageRefData
;
1626 wxImageHandler
*handler
;
1628 if ( type
== wxBITMAP_TYPE_ANY
)
1630 wxList
&list
=GetHandlers();
1632 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1634 handler
=(wxImageHandler
*)node
->GetData();
1635 if ( handler
->CanRead(stream
) )
1636 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1640 wxLogWarning( _("No handler found for image type.") );
1644 handler
= FindHandler(type
);
1648 wxLogWarning( _("No image handler for type %d defined."), type
);
1653 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1655 wxLogError(_("Image file is not of type %d."), type
);
1659 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1662 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1666 m_refData
= new wxImageRefData
;
1668 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1672 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1677 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1679 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1683 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1686 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1688 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1690 wxImageHandler
*handler
= FindHandler(type
);
1693 wxLogWarning( _("No image handler for type %d defined."), type
);
1698 return handler
->SaveFile( (wxImage
*)this, stream
);
1701 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1703 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1705 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1708 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1713 return handler
->SaveFile( (wxImage
*)this, stream
);
1715 #endif // wxUSE_STREAMS
1717 // ----------------------------------------------------------------------------
1718 // image I/O handlers
1719 // ----------------------------------------------------------------------------
1721 void wxImage::AddHandler( wxImageHandler
*handler
)
1723 // Check for an existing handler of the type being added.
1724 if (FindHandler( handler
->GetType() ) == 0)
1726 sm_handlers
.Append( handler
);
1730 // This is not documented behaviour, merely the simplest 'fix'
1731 // for preventing duplicate additions. If someone ever has
1732 // a good reason to add and remove duplicate handlers (and they
1733 // may) we should probably refcount the duplicates.
1734 // also an issue in InsertHandler below.
1736 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1737 handler
->GetName().c_str() );
1742 void wxImage::InsertHandler( wxImageHandler
*handler
)
1744 // Check for an existing handler of the type being added.
1745 if (FindHandler( handler
->GetType() ) == 0)
1747 sm_handlers
.Insert( handler
);
1751 // see AddHandler for additional comments.
1752 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1753 handler
->GetName().c_str() );
1758 bool wxImage::RemoveHandler( const wxString
& name
)
1760 wxImageHandler
*handler
= FindHandler(name
);
1763 sm_handlers
.DeleteObject(handler
);
1771 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1773 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1776 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1777 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1779 node
= node
->GetNext();
1784 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1786 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1789 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1790 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1791 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1793 node
= node
->GetNext();
1798 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1800 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1803 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1804 if (handler
->GetType() == bitmapType
) return handler
;
1805 node
= node
->GetNext();
1810 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1812 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1815 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1816 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1817 node
= node
->GetNext();
1822 void wxImage::InitStandardHandlers()
1825 AddHandler(new wxBMPHandler
);
1826 #endif // wxUSE_STREAMS
1829 void wxImage::CleanUpHandlers()
1831 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1834 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1835 wxList::compatibility_iterator next
= node
->GetNext();
1840 sm_handlers
.Clear();
1843 wxString
wxImage::GetImageExtWildcard()
1847 wxList
& Handlers
= wxImage::GetHandlers();
1848 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1851 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1852 fmts
+= wxT("*.") + Handler
->GetExtension();
1853 Node
= Node
->GetNext();
1854 if ( Node
) fmts
+= wxT(";");
1857 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1860 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1862 const double red
= rgb
.red
/ 255.0,
1863 green
= rgb
.green
/ 255.0,
1864 blue
= rgb
.blue
/ 255.0;
1866 // find the min and max intensity (and remember which one was it for the
1868 double minimumRGB
= red
;
1869 if ( green
< minimumRGB
)
1871 if ( blue
< minimumRGB
)
1874 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1875 double maximumRGB
= red
;
1876 if ( green
> maximumRGB
)
1881 if ( blue
> maximumRGB
)
1887 const double value
= maximumRGB
;
1889 double hue
= 0.0, saturation
;
1890 const double deltaRGB
= maximumRGB
- minimumRGB
;
1891 if ( wxIsNullDouble(deltaRGB
) )
1893 // Gray has no color
1902 hue
= (green
- blue
) / deltaRGB
;
1906 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1910 hue
= 4.0 + (red
- green
) / deltaRGB
;
1914 wxFAIL_MSG(wxT("hue not specified"));
1923 saturation
= deltaRGB
/ maximumRGB
;
1926 return HSVValue(hue
, saturation
, value
);
1929 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1931 double red
, green
, blue
;
1933 if ( wxIsNullDouble(hsv
.saturation
) )
1942 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1943 int i
= (int)floor(hue
);
1944 double f
= hue
- i
; // fractional part of h
1945 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1951 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1956 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1964 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1969 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1974 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1982 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1987 return RGBValue((unsigned char)(red
* 255.0),
1988 (unsigned char)(green
* 255.0),
1989 (unsigned char)(blue
* 255.0));
1993 * Rotates the hue of each pixel of the image. angle is a double in the range
1994 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1996 void wxImage::RotateHue(double angle
)
1998 unsigned char *srcBytePtr
;
1999 unsigned char *dstBytePtr
;
2000 unsigned long count
;
2001 wxImage::HSVValue hsv
;
2002 wxImage::RGBValue rgb
;
2004 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2005 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2006 if ( count
> 0 && !wxIsNullDouble(angle
) )
2008 srcBytePtr
= M_IMGDATA
->m_data
;
2009 dstBytePtr
= srcBytePtr
;
2012 rgb
.red
= *srcBytePtr
++;
2013 rgb
.green
= *srcBytePtr
++;
2014 rgb
.blue
= *srcBytePtr
++;
2015 hsv
= RGBtoHSV(rgb
);
2017 hsv
.hue
= hsv
.hue
+ angle
;
2019 hsv
.hue
= hsv
.hue
- 1.0;
2020 else if (hsv
.hue
< 0.0)
2021 hsv
.hue
= hsv
.hue
+ 1.0;
2023 rgb
= HSVtoRGB(hsv
);
2024 *dstBytePtr
++ = rgb
.red
;
2025 *dstBytePtr
++ = rgb
.green
;
2026 *dstBytePtr
++ = rgb
.blue
;
2027 } while (--count
!= 0);
2031 //-----------------------------------------------------------------------------
2033 //-----------------------------------------------------------------------------
2035 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2038 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2043 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2048 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2053 bool wxImageHandler::CanRead( const wxString
& name
)
2055 if (wxFileExists(name
))
2057 wxFileInputStream
stream(name
);
2058 return CanRead(stream
);
2061 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2066 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2068 wxFileOffset posOld
= stream
.TellI();
2069 if ( posOld
== wxInvalidOffset
)
2071 // can't test unseekable stream
2075 bool ok
= DoCanRead(stream
);
2077 // restore the old position to be able to test other formats and so on
2078 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2080 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2082 // reading would fail anyhow as we're not at the right position
2089 #endif // wxUSE_STREAMS
2091 // ----------------------------------------------------------------------------
2092 // image histogram stuff
2093 // ----------------------------------------------------------------------------
2096 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2101 unsigned char g2
) const
2103 unsigned long key
= MakeKey(r2
, g2
, b2
);
2105 while ( find(key
) != end() )
2107 // color already used
2119 wxLogError(_("No unused colour in image.") );
2125 key
= MakeKey(r2
, g2
, b2
);
2139 wxImage::FindFirstUnusedColour(unsigned char *r
,
2144 unsigned char g2
) const
2146 wxImageHistogram histogram
;
2148 ComputeHistogram(histogram
);
2150 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2156 // Counts and returns the number of different colours. Optionally stops
2157 // when it exceeds 'stopafter' different colours. This is useful, for
2158 // example, to see if the image can be saved as 8-bit (256 colour or
2159 // less, in this case it would be invoked as CountColours(256)). Default
2160 // value for stopafter is -1 (don't care).
2162 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2166 unsigned char r
, g
, b
;
2168 unsigned long size
, nentries
, key
;
2171 size
= GetWidth() * GetHeight();
2174 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2179 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2181 if (h
.Get(key
) == NULL
)
2192 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2194 unsigned char *p
= GetData();
2195 unsigned long nentries
= 0;
2199 const unsigned long size
= GetWidth() * GetHeight();
2201 unsigned char r
, g
, b
;
2202 for ( unsigned long n
= 0; n
< size
; n
++ )
2208 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2210 if ( entry
.value
++ == 0 )
2211 entry
.index
= nentries
++;
2218 * Rotation code by Carlos Moreno
2221 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2222 // does exactly the same thing. And I also got rid of wxRotationPixel
2223 // bacause of potential problems in architectures where alignment
2224 // is an issue, so I had to rewrite parts of the code.
2226 static const double gs_Epsilon
= 1e-10;
2228 static inline int wxCint (double x
)
2230 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2234 // Auxiliary function to rotate a point (x,y) with respect to point p0
2235 // make it inline and use a straight return to facilitate optimization
2236 // also, the function receives the sine and cosine of the angle to avoid
2237 // repeating the time-consuming calls to these functions -- sin/cos can
2238 // be computed and stored in the calling function.
2240 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2242 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2243 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2246 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2248 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2251 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2254 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2256 bool has_alpha
= HasAlpha();
2258 // Create pointer-based array to accelerate access to wxImage's data
2259 unsigned char ** data
= new unsigned char * [GetHeight()];
2260 data
[0] = GetData();
2261 for (i
= 1; i
< GetHeight(); i
++)
2262 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2264 // Same for alpha channel
2265 unsigned char ** alpha
= NULL
;
2268 alpha
= new unsigned char * [GetHeight()];
2269 alpha
[0] = GetAlpha();
2270 for (i
= 1; i
< GetHeight(); i
++)
2271 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2274 // precompute coefficients for rotation formula
2275 // (sine and cosine of the angle)
2276 const double cos_angle
= cos(angle
);
2277 const double sin_angle
= sin(angle
);
2279 // Create new Image to store the result
2280 // First, find rectangle that covers the rotated image; to do that,
2281 // rotate the four corners
2283 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2285 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2286 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2287 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2288 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2290 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2291 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2292 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2293 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2295 // Create rotated image
2296 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2297 // With alpha channel
2301 if (offset_after_rotation
!= NULL
)
2303 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2306 // GRG: The rotated (destination) image is always accessed
2307 // sequentially, so there is no need for a pointer-based
2308 // array here (and in fact it would be slower).
2310 unsigned char * dst
= rotated
.GetData();
2312 unsigned char * alpha_dst
= NULL
;
2314 alpha_dst
= rotated
.GetAlpha();
2316 // GRG: if the original image has a mask, use its RGB values
2317 // as the blank pixel, else, fall back to default (black).
2319 unsigned char blank_r
= 0;
2320 unsigned char blank_g
= 0;
2321 unsigned char blank_b
= 0;
2325 blank_r
= GetMaskRed();
2326 blank_g
= GetMaskGreen();
2327 blank_b
= GetMaskBlue();
2328 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2331 // Now, for each point of the rotated image, find where it came from, by
2332 // performing an inverse rotation (a rotation of -angle) and getting the
2333 // pixel at those coordinates
2335 // GRG: I've taken the (interpolating) test out of the loops, so that
2336 // it is done only once, instead of repeating it for each pixel.
2341 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2343 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2345 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2347 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2348 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2350 // interpolate using the 4 enclosing grid-points. Those
2351 // points can be obtained using floor and ceiling of the
2352 // exact coordinates of the point
2355 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2357 x1
= wxCint(floor(src
.x
));
2358 x2
= wxCint(ceil(src
.x
));
2360 else // else means that x is near one of the borders (0 or width-1)
2362 x1
= x2
= wxCint (src
.x
);
2365 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2367 y1
= wxCint(floor(src
.y
));
2368 y2
= wxCint(ceil(src
.y
));
2372 y1
= y2
= wxCint (src
.y
);
2375 // get four points and the distances (square of the distance,
2376 // for efficiency reasons) for the interpolation formula
2378 // GRG: Do not calculate the points until they are
2379 // really needed -- this way we can calculate
2380 // just one, instead of four, if d1, d2, d3
2381 // or d4 are < gs_Epsilon
2383 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2384 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2385 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2386 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2388 // Now interpolate as a weighted average of the four surrounding
2389 // points, where the weights are the distances to each of those points
2391 // If the point is exactly at one point of the grid of the source
2392 // image, then don't interpolate -- just assign the pixel
2394 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2396 unsigned char *p
= data
[y1
] + (3 * x1
);
2402 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2404 else if (d2
< gs_Epsilon
)
2406 unsigned char *p
= data
[y1
] + (3 * x2
);
2412 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2414 else if (d3
< gs_Epsilon
)
2416 unsigned char *p
= data
[y2
] + (3 * x2
);
2422 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2424 else if (d4
< gs_Epsilon
)
2426 unsigned char *p
= data
[y2
] + (3 * x1
);
2432 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2436 // weights for the weighted average are proportional to the inverse of the distance
2437 unsigned char *v1
= data
[y1
] + (3 * x1
);
2438 unsigned char *v2
= data
[y1
] + (3 * x2
);
2439 unsigned char *v3
= data
[y2
] + (3 * x2
);
2440 unsigned char *v4
= data
[y2
] + (3 * x1
);
2442 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2446 *(dst
++) = (unsigned char)
2447 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2448 w3
* *(v3
++) + w4
* *(v4
++)) /
2449 (w1
+ w2
+ w3
+ w4
) );
2450 *(dst
++) = (unsigned char)
2451 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2452 w3
* *(v3
++) + w4
* *(v4
++)) /
2453 (w1
+ w2
+ w3
+ w4
) );
2454 *(dst
++) = (unsigned char)
2455 ( (w1
* *v1
+ w2
* *v2
+
2456 w3
* *v3
+ w4
* *v4
) /
2457 (w1
+ w2
+ w3
+ w4
) );
2461 v1
= alpha
[y1
] + (x1
);
2462 v2
= alpha
[y1
] + (x2
);
2463 v3
= alpha
[y2
] + (x2
);
2464 v4
= alpha
[y2
] + (x1
);
2466 *(alpha_dst
++) = (unsigned char)
2467 ( (w1
* *v1
+ w2
* *v2
+
2468 w3
* *v3
+ w4
* *v4
) /
2469 (w1
+ w2
+ w3
+ w4
) );
2485 else // not interpolating
2487 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2489 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2491 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2493 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2494 const int ys
= wxCint (src
.y
); // closest integer
2496 if (0 <= xs
&& xs
< GetWidth() &&
2497 0 <= ys
&& ys
< GetHeight())
2499 unsigned char *p
= data
[ys
] + (3 * xs
);
2505 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2514 *(alpha_dst
++) = 255;
2532 // A module to allow wxImage initialization/cleanup
2533 // without calling these functions from app.cpp or from
2534 // the user's application.
2536 class wxImageModule
: public wxModule
2538 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2541 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2542 void OnExit() { wxImage::CleanUpHandlers(); };
2545 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2548 #endif // wxUSE_IMAGE