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"
27 #include "wx/bitmap.h"
28 #include "wx/filefn.h"
29 #include "wx/wfstream.h"
31 #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 finalRect
.width
-= pos
.x
;
713 finalRect
.height
-= pos
.y
;
715 subRect
.Intersect(finalRect
);
717 if (!subRect
.IsEmpty())
719 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
720 image
.Paste(*this, pos
.x
, pos
.y
);
722 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
728 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
730 wxCHECK_RET( Ok(), wxT("invalid image") );
731 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
735 int width
= image
.GetWidth();
736 int height
= image
.GetHeight();
749 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
750 width
= M_IMGDATA
->m_width
- (x
+xx
);
751 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
752 height
= M_IMGDATA
->m_height
- (y
+yy
);
754 if (width
< 1) return;
755 if (height
< 1) return;
757 if ((!HasMask() && !image
.HasMask()) ||
758 (HasMask() && !image
.HasMask()) ||
759 ((HasMask() && image
.HasMask() &&
760 (GetMaskRed()==image
.GetMaskRed()) &&
761 (GetMaskGreen()==image
.GetMaskGreen()) &&
762 (GetMaskBlue()==image
.GetMaskBlue()))))
765 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
766 int source_step
= image
.GetWidth()*3;
768 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
769 int target_step
= M_IMGDATA
->m_width
*3;
770 for (int j
= 0; j
< height
; j
++)
772 memcpy( target_data
, source_data
, width
);
773 source_data
+= source_step
;
774 target_data
+= target_step
;
779 if (!HasMask() && image
.HasMask())
781 unsigned char r
= image
.GetMaskRed();
782 unsigned char g
= image
.GetMaskGreen();
783 unsigned char b
= image
.GetMaskBlue();
786 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
787 int source_step
= image
.GetWidth()*3;
789 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
790 int target_step
= M_IMGDATA
->m_width
*3;
792 for (int j
= 0; j
< height
; j
++)
794 for (int i
= 0; i
< width
; i
+=3)
796 if ((source_data
[i
] != r
) &&
797 (source_data
[i
+1] != g
) &&
798 (source_data
[i
+2] != b
))
800 memcpy( target_data
+i
, source_data
+i
, 3 );
803 source_data
+= source_step
;
804 target_data
+= target_step
;
809 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
810 unsigned char r2
, unsigned char g2
, unsigned char b2
)
812 wxCHECK_RET( Ok(), wxT("invalid image") );
814 unsigned char *data
= GetData();
816 const int w
= GetWidth();
817 const int h
= GetHeight();
819 for (int j
= 0; j
< h
; j
++)
820 for (int i
= 0; i
< w
; i
++)
822 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
832 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
836 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
838 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
840 unsigned char *dest
= image
.GetData();
842 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
844 unsigned char *src
= M_IMGDATA
->m_data
;
845 bool hasMask
= M_IMGDATA
->m_hasMask
;
846 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
847 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
848 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
851 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
853 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
854 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
856 // don't modify the mask
857 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
859 memcpy(dest
, src
, 3);
863 // calculate the luma
864 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
865 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
872 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
876 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
878 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
880 unsigned char *data
= image
.GetData();
882 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
884 if (M_IMGDATA
->m_hasMask
)
886 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
887 M_IMGDATA
->m_maskBlue
== b
)
888 image
.SetMaskColour( 255, 255, 255 );
890 image
.SetMaskColour( 0, 0, 0 );
893 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
895 unsigned char *srcd
= M_IMGDATA
->m_data
;
896 unsigned char *tard
= image
.GetData();
898 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
900 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
901 tard
[0] = tard
[1] = tard
[2] = 255;
903 tard
[0] = tard
[1] = tard
[2] = 0;
909 int wxImage::GetWidth() const
911 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
913 return M_IMGDATA
->m_width
;
916 int wxImage::GetHeight() const
918 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
920 return M_IMGDATA
->m_height
;
923 long wxImage::XYToIndex(int x
, int y
) const
927 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
929 return y
*M_IMGDATA
->m_width
+ x
;
935 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
937 long pos
= XYToIndex(x
, y
);
938 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
942 M_IMGDATA
->m_data
[ pos
] = r
;
943 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
944 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
947 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
949 wxCHECK_RET( Ok(), wxT("invalid image") );
952 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
953 if ( rect
== wxRect() )
959 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
960 imageRect
.Inside(rect
.GetBottomRight()),
961 wxT("invalid bounding rectangle") );
964 int x1
= rect
.GetLeft(),
966 x2
= rect
.GetRight() + 1,
967 y2
= rect
.GetBottom() + 1;
969 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
970 int x
, y
, width
= GetWidth();
971 for (y
= y1
; y
< y2
; y
++)
973 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
974 for (x
= x1
; x
< x2
; x
++)
983 unsigned char wxImage::GetRed( int x
, int y
) const
985 long pos
= XYToIndex(x
, y
);
986 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
990 return M_IMGDATA
->m_data
[pos
];
993 unsigned char wxImage::GetGreen( int x
, int y
) const
995 long pos
= XYToIndex(x
, y
);
996 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1000 return M_IMGDATA
->m_data
[pos
+1];
1003 unsigned char wxImage::GetBlue( int x
, int y
) const
1005 long pos
= XYToIndex(x
, y
);
1006 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1010 return M_IMGDATA
->m_data
[pos
+2];
1013 bool wxImage::Ok() const
1015 // image of 0 width or height can't be considered ok - at least because it
1016 // causes crashes in ConvertToBitmap() if we don't catch it in time
1017 wxImageRefData
*data
= M_IMGDATA
;
1018 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1021 unsigned char *wxImage::GetData() const
1023 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1025 return M_IMGDATA
->m_data
;
1028 void wxImage::SetData( unsigned char *data
, bool static_data
)
1030 wxCHECK_RET( Ok(), wxT("invalid image") );
1032 wxImageRefData
*newRefData
= new wxImageRefData();
1034 newRefData
->m_width
= M_IMGDATA
->m_width
;
1035 newRefData
->m_height
= M_IMGDATA
->m_height
;
1036 newRefData
->m_data
= data
;
1037 newRefData
->m_ok
= true;
1038 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1039 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1040 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1041 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1042 newRefData
->m_static
= static_data
;
1046 m_refData
= newRefData
;
1049 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1051 wxImageRefData
*newRefData
= new wxImageRefData();
1055 newRefData
->m_width
= new_width
;
1056 newRefData
->m_height
= new_height
;
1057 newRefData
->m_data
= data
;
1058 newRefData
->m_ok
= true;
1059 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1060 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1061 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1062 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1066 newRefData
->m_width
= new_width
;
1067 newRefData
->m_height
= new_height
;
1068 newRefData
->m_data
= data
;
1069 newRefData
->m_ok
= true;
1071 newRefData
->m_static
= static_data
;
1075 m_refData
= newRefData
;
1078 // ----------------------------------------------------------------------------
1079 // alpha channel support
1080 // ----------------------------------------------------------------------------
1082 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1084 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1086 long pos
= XYToIndex(x
, y
);
1087 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1089 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1092 unsigned char wxImage::GetAlpha(int x
, int y
) const
1094 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1096 long pos
= XYToIndex(x
, y
);
1097 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1099 return M_IMGDATA
->m_alpha
[pos
];
1103 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1107 const int w
= M_IMGDATA
->m_width
;
1108 const int h
= M_IMGDATA
->m_height
;
1110 unsigned char *alpha
= GetAlpha();
1111 unsigned char *data
= GetData();
1113 for ( int y
= 0; y
< h
; y
++ )
1115 for ( int x
= 0; x
< w
; x
++ )
1127 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1129 wxCHECK_RET( Ok(), wxT("invalid image") );
1133 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1136 free(M_IMGDATA
->m_alpha
);
1137 M_IMGDATA
->m_alpha
= alpha
;
1138 M_IMGDATA
->m_staticAlpha
= static_data
;
1141 unsigned char *wxImage::GetAlpha() const
1143 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1145 return M_IMGDATA
->m_alpha
;
1148 void wxImage::InitAlpha()
1150 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1152 // initialize memory for alpha channel
1155 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1156 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1160 // use the mask to initialize the alpha channel.
1161 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1163 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1164 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1165 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1166 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1170 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1171 ? wxIMAGE_ALPHA_TRANSPARENT
1172 : wxIMAGE_ALPHA_OPAQUE
;
1175 M_IMGDATA
->m_hasMask
= false;
1179 // make the image fully opaque
1180 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1184 // ----------------------------------------------------------------------------
1186 // ----------------------------------------------------------------------------
1188 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1190 wxCHECK_RET( Ok(), wxT("invalid image") );
1192 M_IMGDATA
->m_maskRed
= r
;
1193 M_IMGDATA
->m_maskGreen
= g
;
1194 M_IMGDATA
->m_maskBlue
= b
;
1195 M_IMGDATA
->m_hasMask
= true;
1198 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1200 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1202 if (M_IMGDATA
->m_hasMask
)
1204 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1205 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1206 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1211 FindFirstUnusedColour(r
, g
, b
);
1216 unsigned char wxImage::GetMaskRed() const
1218 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1220 return M_IMGDATA
->m_maskRed
;
1223 unsigned char wxImage::GetMaskGreen() const
1225 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1227 return M_IMGDATA
->m_maskGreen
;
1230 unsigned char wxImage::GetMaskBlue() const
1232 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1234 return M_IMGDATA
->m_maskBlue
;
1237 void wxImage::SetMask( bool mask
)
1239 wxCHECK_RET( Ok(), wxT("invalid image") );
1241 M_IMGDATA
->m_hasMask
= mask
;
1244 bool wxImage::HasMask() const
1246 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1248 return M_IMGDATA
->m_hasMask
;
1251 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1253 long pos
= XYToIndex(x
, y
);
1254 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1257 if ( M_IMGDATA
->m_hasMask
)
1259 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1260 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1261 p
[1] == M_IMGDATA
->m_maskGreen
&&
1262 p
[2] == M_IMGDATA
->m_maskBlue
)
1269 if ( M_IMGDATA
->m_alpha
)
1271 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1273 // transparent enough
1282 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1283 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1285 // check that the images are the same size
1286 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1288 wxLogError( _("Image and mask have different sizes.") );
1292 // find unused colour
1293 unsigned char r
,g
,b
;
1294 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1296 wxLogError( _("No unused colour in image being masked.") );
1300 unsigned char *imgdata
= GetData();
1301 unsigned char *maskdata
= mask
.GetData();
1303 const int w
= GetWidth();
1304 const int h
= GetHeight();
1306 for (int j
= 0; j
< h
; j
++)
1308 for (int i
= 0; i
< w
; i
++)
1310 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1321 SetMaskColour(r
, g
, b
);
1327 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1332 unsigned char mr
, mg
, mb
;
1333 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1335 wxLogError( _("No unused colour in image being masked.") );
1340 SetMaskColour(mr
, mg
, mb
);
1342 unsigned char *imgdata
= GetData();
1343 unsigned char *alphadata
= GetAlpha();
1346 int h
= GetHeight();
1348 for (int y
= 0; y
< h
; y
++)
1350 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1352 if (*alphadata
< threshold
)
1361 free(M_IMGDATA
->m_alpha
);
1362 M_IMGDATA
->m_alpha
= NULL
;
1367 // ----------------------------------------------------------------------------
1368 // Palette functions
1369 // ----------------------------------------------------------------------------
1373 bool wxImage::HasPalette() const
1378 return M_IMGDATA
->m_palette
.Ok();
1381 const wxPalette
& wxImage::GetPalette() const
1383 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1385 return M_IMGDATA
->m_palette
;
1388 void wxImage::SetPalette(const wxPalette
& palette
)
1390 wxCHECK_RET( Ok(), wxT("invalid image") );
1392 M_IMGDATA
->m_palette
= palette
;
1395 #endif // wxUSE_PALETTE
1397 // ----------------------------------------------------------------------------
1398 // Option functions (arbitrary name/value mapping)
1399 // ----------------------------------------------------------------------------
1401 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1403 wxCHECK_RET( Ok(), wxT("invalid image") );
1405 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1406 if (idx
== wxNOT_FOUND
)
1408 M_IMGDATA
->m_optionNames
.Add(name
);
1409 M_IMGDATA
->m_optionValues
.Add(value
);
1413 M_IMGDATA
->m_optionNames
[idx
] = name
;
1414 M_IMGDATA
->m_optionValues
[idx
] = value
;
1418 void wxImage::SetOption(const wxString
& name
, int value
)
1421 valStr
.Printf(wxT("%d"), value
);
1422 SetOption(name
, valStr
);
1425 wxString
wxImage::GetOption(const wxString
& name
) const
1427 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1429 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1430 if (idx
== wxNOT_FOUND
)
1431 return wxEmptyString
;
1433 return M_IMGDATA
->m_optionValues
[idx
];
1436 int wxImage::GetOptionInt(const wxString
& name
) const
1438 return wxAtoi(GetOption(name
));
1441 bool wxImage::HasOption(const wxString
& name
) const
1443 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1445 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1448 // ----------------------------------------------------------------------------
1450 // ----------------------------------------------------------------------------
1452 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1455 if (wxFileExists(filename
))
1457 wxFileInputStream
stream(filename
);
1458 wxBufferedInputStream
bstream( stream
);
1459 return LoadFile(bstream
, type
, index
);
1463 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1467 #else // !wxUSE_STREAMS
1469 #endif // wxUSE_STREAMS
1472 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1475 if (wxFileExists(filename
))
1477 wxFileInputStream
stream(filename
);
1478 wxBufferedInputStream
bstream( stream
);
1479 return LoadFile(bstream
, mimetype
, index
);
1483 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1487 #else // !wxUSE_STREAMS
1489 #endif // wxUSE_STREAMS
1494 bool wxImage::SaveFile( const wxString
& filename
) const
1496 wxString ext
= filename
.AfterLast('.').Lower();
1498 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1501 SaveFile(filename
, pHandler
->GetType());
1505 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1510 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1513 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1515 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1517 wxFileOutputStream
stream(filename
);
1519 if ( stream
.IsOk() )
1521 wxBufferedOutputStream
bstream( stream
);
1522 return SaveFile(bstream
, type
);
1524 #endif // wxUSE_STREAMS
1529 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1532 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1534 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1536 wxFileOutputStream
stream(filename
);
1538 if ( stream
.IsOk() )
1540 wxBufferedOutputStream
bstream( stream
);
1541 return SaveFile(bstream
, mimetype
);
1543 #endif // wxUSE_STREAMS
1548 bool wxImage::CanRead( const wxString
&name
)
1551 wxFileInputStream
stream(name
);
1552 return CanRead(stream
);
1558 int wxImage::GetImageCount( const wxString
&name
, long type
)
1561 wxFileInputStream
stream(name
);
1563 return GetImageCount(stream
, type
);
1571 bool wxImage::CanRead( wxInputStream
&stream
)
1573 const wxList
& list
= GetHandlers();
1575 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1577 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1578 if (handler
->CanRead( stream
))
1585 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1587 wxImageHandler
*handler
;
1589 if ( type
== wxBITMAP_TYPE_ANY
)
1591 wxList
&list
=GetHandlers();
1593 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1595 handler
=(wxImageHandler
*)node
->GetData();
1596 if ( handler
->CanRead(stream
) )
1597 return handler
->GetImageCount(stream
);
1601 wxLogWarning(_("No handler found for image type."));
1605 handler
= FindHandler(type
);
1609 wxLogWarning(_("No image handler for type %d defined."), type
);
1613 if ( handler
->CanRead(stream
) )
1615 return handler
->GetImageCount(stream
);
1619 wxLogError(_("Image file is not of type %d."), type
);
1624 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1628 m_refData
= new wxImageRefData
;
1630 wxImageHandler
*handler
;
1632 if ( type
== wxBITMAP_TYPE_ANY
)
1634 wxList
&list
=GetHandlers();
1636 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1638 handler
=(wxImageHandler
*)node
->GetData();
1639 if ( handler
->CanRead(stream
) )
1640 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1644 wxLogWarning( _("No handler found for image type.") );
1648 handler
= FindHandler(type
);
1652 wxLogWarning( _("No image handler for type %d defined."), type
);
1657 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1659 wxLogError(_("Image file is not of type %d."), type
);
1663 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1666 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1670 m_refData
= new wxImageRefData
;
1672 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1676 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1681 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1683 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1687 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1690 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1692 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1694 wxImageHandler
*handler
= FindHandler(type
);
1697 wxLogWarning( _("No image handler for type %d defined."), type
);
1702 return handler
->SaveFile( (wxImage
*)this, stream
);
1705 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1707 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1709 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1712 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1717 return handler
->SaveFile( (wxImage
*)this, stream
);
1719 #endif // wxUSE_STREAMS
1721 // ----------------------------------------------------------------------------
1722 // image I/O handlers
1723 // ----------------------------------------------------------------------------
1725 void wxImage::AddHandler( wxImageHandler
*handler
)
1727 // Check for an existing handler of the type being added.
1728 if (FindHandler( handler
->GetType() ) == 0)
1730 sm_handlers
.Append( handler
);
1734 // This is not documented behaviour, merely the simplest 'fix'
1735 // for preventing duplicate additions. If someone ever has
1736 // a good reason to add and remove duplicate handlers (and they
1737 // may) we should probably refcount the duplicates.
1738 // also an issue in InsertHandler below.
1740 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1741 handler
->GetName().c_str() );
1746 void wxImage::InsertHandler( wxImageHandler
*handler
)
1748 // Check for an existing handler of the type being added.
1749 if (FindHandler( handler
->GetType() ) == 0)
1751 sm_handlers
.Insert( handler
);
1755 // see AddHandler for additional comments.
1756 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1757 handler
->GetName().c_str() );
1762 bool wxImage::RemoveHandler( const wxString
& name
)
1764 wxImageHandler
*handler
= FindHandler(name
);
1767 sm_handlers
.DeleteObject(handler
);
1775 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1777 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1780 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1781 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1783 node
= node
->GetNext();
1788 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1790 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1793 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1794 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1795 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1797 node
= node
->GetNext();
1802 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1804 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1807 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1808 if (handler
->GetType() == bitmapType
) return handler
;
1809 node
= node
->GetNext();
1814 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1816 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1819 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1820 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1821 node
= node
->GetNext();
1826 void wxImage::InitStandardHandlers()
1829 AddHandler(new wxBMPHandler
);
1830 #endif // wxUSE_STREAMS
1833 void wxImage::CleanUpHandlers()
1835 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1838 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1839 wxList::compatibility_iterator next
= node
->GetNext();
1844 sm_handlers
.Clear();
1847 wxString
wxImage::GetImageExtWildcard()
1851 wxList
& Handlers
= wxImage::GetHandlers();
1852 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1855 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1856 fmts
+= wxT("*.") + Handler
->GetExtension();
1857 Node
= Node
->GetNext();
1858 if ( Node
) fmts
+= wxT(";");
1861 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1864 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1866 const double red
= rgb
.red
/ 255.0,
1867 green
= rgb
.green
/ 255.0,
1868 blue
= rgb
.blue
/ 255.0;
1870 // find the min and max intensity (and remember which one was it for the
1872 double minimumRGB
= red
;
1873 if ( green
< minimumRGB
)
1875 if ( blue
< minimumRGB
)
1878 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1879 double maximumRGB
= red
;
1880 if ( green
> maximumRGB
)
1885 if ( blue
> maximumRGB
)
1891 const double value
= maximumRGB
;
1893 double hue
= 0.0, saturation
;
1894 const double deltaRGB
= maximumRGB
- minimumRGB
;
1895 if ( wxIsNullDouble(deltaRGB
) )
1897 // Gray has no color
1906 hue
= (green
- blue
) / deltaRGB
;
1910 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1914 hue
= 4.0 + (red
- green
) / deltaRGB
;
1918 wxFAIL_MSG(wxT("hue not specified"));
1927 saturation
= deltaRGB
/ maximumRGB
;
1930 return HSVValue(hue
, saturation
, value
);
1933 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1935 double red
, green
, blue
;
1937 if ( wxIsNullDouble(hsv
.saturation
) )
1946 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1947 int i
= (int)floor(hue
);
1948 double f
= hue
- i
; // fractional part of h
1949 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1955 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1960 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1968 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1973 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1978 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1986 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1991 return RGBValue((unsigned char)(red
* 255.0),
1992 (unsigned char)(green
* 255.0),
1993 (unsigned char)(blue
* 255.0));
1997 * Rotates the hue of each pixel of the image. angle is a double in the range
1998 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2000 void wxImage::RotateHue(double angle
)
2002 unsigned char *srcBytePtr
;
2003 unsigned char *dstBytePtr
;
2004 unsigned long count
;
2005 wxImage::HSVValue hsv
;
2006 wxImage::RGBValue rgb
;
2008 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2009 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2010 if ( count
> 0 && !wxIsNullDouble(angle
) )
2012 srcBytePtr
= M_IMGDATA
->m_data
;
2013 dstBytePtr
= srcBytePtr
;
2016 rgb
.red
= *srcBytePtr
++;
2017 rgb
.green
= *srcBytePtr
++;
2018 rgb
.blue
= *srcBytePtr
++;
2019 hsv
= RGBtoHSV(rgb
);
2021 hsv
.hue
= hsv
.hue
+ angle
;
2023 hsv
.hue
= hsv
.hue
- 1.0;
2024 else if (hsv
.hue
< 0.0)
2025 hsv
.hue
= hsv
.hue
+ 1.0;
2027 rgb
= HSVtoRGB(hsv
);
2028 *dstBytePtr
++ = rgb
.red
;
2029 *dstBytePtr
++ = rgb
.green
;
2030 *dstBytePtr
++ = rgb
.blue
;
2031 } while (--count
!= 0);
2035 //-----------------------------------------------------------------------------
2037 //-----------------------------------------------------------------------------
2039 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2042 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2047 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2052 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2057 bool wxImageHandler::CanRead( const wxString
& name
)
2059 if (wxFileExists(name
))
2061 wxFileInputStream
stream(name
);
2062 return CanRead(stream
);
2065 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2070 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2072 wxFileOffset posOld
= stream
.TellI();
2073 if ( posOld
== wxInvalidOffset
)
2075 // can't test unseekable stream
2079 bool ok
= DoCanRead(stream
);
2081 // restore the old position to be able to test other formats and so on
2082 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2084 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2086 // reading would fail anyhow as we're not at the right position
2093 #endif // wxUSE_STREAMS
2095 // ----------------------------------------------------------------------------
2096 // image histogram stuff
2097 // ----------------------------------------------------------------------------
2100 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2105 unsigned char g2
) const
2107 unsigned long key
= MakeKey(r2
, g2
, b2
);
2109 while ( find(key
) != end() )
2111 // color already used
2123 wxLogError(_("No unused colour in image.") );
2129 key
= MakeKey(r2
, g2
, b2
);
2143 wxImage::FindFirstUnusedColour(unsigned char *r
,
2148 unsigned char g2
) const
2150 wxImageHistogram histogram
;
2152 ComputeHistogram(histogram
);
2154 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2160 // Counts and returns the number of different colours. Optionally stops
2161 // when it exceeds 'stopafter' different colours. This is useful, for
2162 // example, to see if the image can be saved as 8-bit (256 colour or
2163 // less, in this case it would be invoked as CountColours(256)). Default
2164 // value for stopafter is -1 (don't care).
2166 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2170 unsigned char r
, g
, b
;
2172 unsigned long size
, nentries
, key
;
2175 size
= GetWidth() * GetHeight();
2178 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2183 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2185 if (h
.Get(key
) == NULL
)
2196 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2198 unsigned char *p
= GetData();
2199 unsigned long nentries
= 0;
2203 const unsigned long size
= GetWidth() * GetHeight();
2205 unsigned char r
, g
, b
;
2206 for ( unsigned long n
= 0; n
< size
; n
++ )
2212 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2214 if ( entry
.value
++ == 0 )
2215 entry
.index
= nentries
++;
2222 * Rotation code by Carlos Moreno
2225 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2226 // does exactly the same thing. And I also got rid of wxRotationPixel
2227 // bacause of potential problems in architectures where alignment
2228 // is an issue, so I had to rewrite parts of the code.
2230 static const double gs_Epsilon
= 1e-10;
2232 static inline int wxCint (double x
)
2234 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2238 // Auxiliary function to rotate a point (x,y) with respect to point p0
2239 // make it inline and use a straight return to facilitate optimization
2240 // also, the function receives the sine and cosine of the angle to avoid
2241 // repeating the time-consuming calls to these functions -- sin/cos can
2242 // be computed and stored in the calling function.
2244 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2246 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2247 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2250 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2252 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2255 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2258 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2260 bool has_alpha
= HasAlpha();
2262 // Create pointer-based array to accelerate access to wxImage's data
2263 unsigned char ** data
= new unsigned char * [GetHeight()];
2264 data
[0] = GetData();
2265 for (i
= 1; i
< GetHeight(); i
++)
2266 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2268 // Same for alpha channel
2269 unsigned char ** alpha
= NULL
;
2272 alpha
= new unsigned char * [GetHeight()];
2273 alpha
[0] = GetAlpha();
2274 for (i
= 1; i
< GetHeight(); i
++)
2275 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2278 // precompute coefficients for rotation formula
2279 // (sine and cosine of the angle)
2280 const double cos_angle
= cos(angle
);
2281 const double sin_angle
= sin(angle
);
2283 // Create new Image to store the result
2284 // First, find rectangle that covers the rotated image; to do that,
2285 // rotate the four corners
2287 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2289 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2290 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2291 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2292 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2294 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2295 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2296 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2297 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2299 // Create rotated image
2300 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2301 // With alpha channel
2305 if (offset_after_rotation
!= NULL
)
2307 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2310 // GRG: The rotated (destination) image is always accessed
2311 // sequentially, so there is no need for a pointer-based
2312 // array here (and in fact it would be slower).
2314 unsigned char * dst
= rotated
.GetData();
2316 unsigned char * alpha_dst
= NULL
;
2318 alpha_dst
= rotated
.GetAlpha();
2320 // GRG: if the original image has a mask, use its RGB values
2321 // as the blank pixel, else, fall back to default (black).
2323 unsigned char blank_r
= 0;
2324 unsigned char blank_g
= 0;
2325 unsigned char blank_b
= 0;
2329 blank_r
= GetMaskRed();
2330 blank_g
= GetMaskGreen();
2331 blank_b
= GetMaskBlue();
2332 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2335 // Now, for each point of the rotated image, find where it came from, by
2336 // performing an inverse rotation (a rotation of -angle) and getting the
2337 // pixel at those coordinates
2339 // GRG: I've taken the (interpolating) test out of the loops, so that
2340 // it is done only once, instead of repeating it for each pixel.
2345 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2347 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2349 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2351 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2352 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2354 // interpolate using the 4 enclosing grid-points. Those
2355 // points can be obtained using floor and ceiling of the
2356 // exact coordinates of the point
2359 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2361 x1
= wxCint(floor(src
.x
));
2362 x2
= wxCint(ceil(src
.x
));
2364 else // else means that x is near one of the borders (0 or width-1)
2366 x1
= x2
= wxCint (src
.x
);
2369 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2371 y1
= wxCint(floor(src
.y
));
2372 y2
= wxCint(ceil(src
.y
));
2376 y1
= y2
= wxCint (src
.y
);
2379 // get four points and the distances (square of the distance,
2380 // for efficiency reasons) for the interpolation formula
2382 // GRG: Do not calculate the points until they are
2383 // really needed -- this way we can calculate
2384 // just one, instead of four, if d1, d2, d3
2385 // or d4 are < gs_Epsilon
2387 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2388 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2389 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2390 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2392 // Now interpolate as a weighted average of the four surrounding
2393 // points, where the weights are the distances to each of those points
2395 // If the point is exactly at one point of the grid of the source
2396 // image, then don't interpolate -- just assign the pixel
2398 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2400 unsigned char *p
= data
[y1
] + (3 * x1
);
2406 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2408 else if (d2
< gs_Epsilon
)
2410 unsigned char *p
= data
[y1
] + (3 * x2
);
2416 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2418 else if (d3
< gs_Epsilon
)
2420 unsigned char *p
= data
[y2
] + (3 * x2
);
2426 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2428 else if (d4
< gs_Epsilon
)
2430 unsigned char *p
= data
[y2
] + (3 * x1
);
2436 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2440 // weights for the weighted average are proportional to the inverse of the distance
2441 unsigned char *v1
= data
[y1
] + (3 * x1
);
2442 unsigned char *v2
= data
[y1
] + (3 * x2
);
2443 unsigned char *v3
= data
[y2
] + (3 * x2
);
2444 unsigned char *v4
= data
[y2
] + (3 * x1
);
2446 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
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
) );
2458 *(dst
++) = (unsigned char)
2459 ( (w1
* *v1
+ w2
* *v2
+
2460 w3
* *v3
+ w4
* *v4
) /
2461 (w1
+ w2
+ w3
+ w4
) );
2465 v1
= alpha
[y1
] + (x1
);
2466 v2
= alpha
[y1
] + (x2
);
2467 v3
= alpha
[y2
] + (x2
);
2468 v4
= alpha
[y2
] + (x1
);
2470 *(alpha_dst
++) = (unsigned char)
2471 ( (w1
* *v1
+ w2
* *v2
+
2472 w3
* *v3
+ w4
* *v4
) /
2473 (w1
+ w2
+ w3
+ w4
) );
2489 else // not interpolating
2491 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2493 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2495 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2497 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2498 const int ys
= wxCint (src
.y
); // closest integer
2500 if (0 <= xs
&& xs
< GetWidth() &&
2501 0 <= ys
&& ys
< GetHeight())
2503 unsigned char *p
= data
[ys
] + (3 * xs
);
2509 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2518 *(alpha_dst
++) = 255;
2536 // A module to allow wxImage initialization/cleanup
2537 // without calling these functions from app.cpp or from
2538 // the user's application.
2540 class wxImageModule
: public wxModule
2542 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2545 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2546 void OnExit() { wxImage::CleanUpHandlers(); };
2549 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2552 #endif // wxUSE_IMAGE