1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
36 #include "wx/xpmdecod.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxImageRefData
: public wxObjectRefData
50 virtual ~wxImageRefData();
54 unsigned char *m_data
;
57 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
59 // alpha channel data, may be NULL for the formats without alpha support
60 unsigned char *m_alpha
;
64 // if true, m_data is pointer to static data and shouldn't be freed
67 // same as m_static but for m_alpha
72 #endif // wxUSE_PALETTE
74 wxArrayString m_optionNames
;
75 wxArrayString m_optionValues
;
77 DECLARE_NO_COPY_CLASS(wxImageRefData
)
80 wxImageRefData::wxImageRefData()
85 m_alpha
= (unsigned char *) NULL
;
94 m_staticAlpha
= false;
97 wxImageRefData::~wxImageRefData()
101 if ( !m_staticAlpha
)
105 wxList
wxImage::sm_handlers
;
109 //-----------------------------------------------------------------------------
111 #define M_IMGDATA ((wxImageRefData *)m_refData)
113 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
115 wxImage::wxImage( int width
, int height
, bool clear
)
117 Create( width
, height
, clear
);
120 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
122 Create( width
, height
, data
, static_data
);
125 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
127 Create( width
, height
, data
, alpha
, static_data
);
130 wxImage::wxImage( const wxString
& name
, long type
, int index
)
132 LoadFile( name
, type
, index
);
135 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
137 LoadFile( name
, mimetype
, index
);
141 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
143 LoadFile( stream
, type
, index
);
146 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
148 LoadFile( stream
, mimetype
, index
);
150 #endif // wxUSE_STREAMS
152 wxImage::wxImage( const char** xpmData
)
157 wxImage::wxImage( char** xpmData
)
159 Create((const char**) xpmData
);
162 bool wxImage::Create( const char** xpmData
)
167 wxXPMDecoder decoder
;
168 (*this) = decoder
.ReadData(xpmData
);
175 bool wxImage::Create( int width
, int height
, bool clear
)
179 m_refData
= new wxImageRefData();
181 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
182 if (!M_IMGDATA
->m_data
)
189 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
191 M_IMGDATA
->m_width
= width
;
192 M_IMGDATA
->m_height
= height
;
193 M_IMGDATA
->m_ok
= true;
198 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
202 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
204 m_refData
= new wxImageRefData();
206 M_IMGDATA
->m_data
= data
;
207 M_IMGDATA
->m_width
= width
;
208 M_IMGDATA
->m_height
= height
;
209 M_IMGDATA
->m_ok
= true;
210 M_IMGDATA
->m_static
= static_data
;
215 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
219 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
221 m_refData
= new wxImageRefData();
223 M_IMGDATA
->m_data
= data
;
224 M_IMGDATA
->m_alpha
= alpha
;
225 M_IMGDATA
->m_width
= width
;
226 M_IMGDATA
->m_height
= height
;
227 M_IMGDATA
->m_ok
= true;
228 M_IMGDATA
->m_static
= static_data
;
233 void wxImage::Destroy()
238 wxImage
wxImage::Copy() const
242 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
244 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
246 unsigned char *data
= image
.GetData();
248 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
250 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
251 image
.SetMask( M_IMGDATA
->m_hasMask
);
253 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
255 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
257 // also copy the alpha channel
261 unsigned char* alpha
= image
.GetAlpha();
262 memcpy( alpha
, GetAlpha(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
265 // also copy the image options
266 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
267 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
272 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
274 if( xFactor
== 1 && yFactor
== 1 )
279 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
281 // can't scale to/from 0 size
282 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
283 wxT("invalid new image size") );
285 long old_height
= M_IMGDATA
->m_height
,
286 old_width
= M_IMGDATA
->m_width
;
288 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
289 wxT("invalid old image size") );
291 long width
= old_width
/ xFactor
;
292 long height
= old_height
/ yFactor
;
294 image
.Create( width
, height
, false );
296 char unsigned *data
= image
.GetData();
298 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
300 bool hasMask
= false ;
301 unsigned char maskRed
= 0;
302 unsigned char maskGreen
= 0;
303 unsigned char maskBlue
=0 ;
305 unsigned char *source_data
= M_IMGDATA
->m_data
;
306 unsigned char *target_data
= data
;
307 unsigned char *source_alpha
= 0 ;
308 unsigned char *target_alpha
= 0 ;
309 if (M_IMGDATA
->m_hasMask
)
312 maskRed
= M_IMGDATA
->m_maskRed
;
313 maskGreen
= M_IMGDATA
->m_maskGreen
;
314 maskBlue
=M_IMGDATA
->m_maskBlue
;
316 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
317 M_IMGDATA
->m_maskGreen
,
318 M_IMGDATA
->m_maskBlue
);
322 source_alpha
= M_IMGDATA
->m_alpha
;
326 target_alpha
= image
.GetAlpha() ;
330 for (long y
= 0; y
< height
; y
++)
332 for (long x
= 0; x
< width
; x
++)
334 unsigned long avgRed
= 0 ;
335 unsigned long avgGreen
= 0;
336 unsigned long avgBlue
= 0;
337 unsigned long avgAlpha
= 0 ;
338 unsigned long counter
= 0 ;
340 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
342 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
343 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
345 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
346 unsigned char red
= pixel
[0] ;
347 unsigned char green
= pixel
[1] ;
348 unsigned char blue
= pixel
[2] ;
349 unsigned char alpha
= 255 ;
351 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
352 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
367 *(target_data
++) = M_IMGDATA
->m_maskRed
;
368 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
369 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
374 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
375 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
376 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
377 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
382 // In case this is a cursor, make sure the hotspot is scaled accordingly:
383 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
384 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
385 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
386 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
387 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
388 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
393 wxImage
wxImage::Scale( int width
, int height
) const
397 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
399 // can't scale to/from 0 size
400 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
401 wxT("invalid new image size") );
403 long old_height
= M_IMGDATA
->m_height
,
404 old_width
= M_IMGDATA
->m_width
;
405 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
406 wxT("invalid old image size") );
408 if ( old_width
% width
== 0 && old_width
>= width
&&
409 old_height
% height
== 0 && old_height
>= height
)
411 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
413 image
.Create( width
, height
, false );
415 unsigned char *data
= image
.GetData();
417 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
419 unsigned char *source_data
= M_IMGDATA
->m_data
;
420 unsigned char *target_data
= data
;
421 unsigned char *source_alpha
= 0 ;
422 unsigned char *target_alpha
= 0 ;
424 if (M_IMGDATA
->m_hasMask
)
426 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
427 M_IMGDATA
->m_maskGreen
,
428 M_IMGDATA
->m_maskBlue
);
432 source_alpha
= M_IMGDATA
->m_alpha
;
436 target_alpha
= image
.GetAlpha() ;
440 long x_delta
= (old_width
<<16) / width
;
441 long y_delta
= (old_height
<<16) / height
;
443 unsigned char* dest_pixel
= target_data
;
446 for ( long j
= 0; j
< height
; j
++ )
448 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
449 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
452 for ( long i
= 0; i
< width
; i
++ )
454 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
455 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
456 dest_pixel
[0] = src_pixel
[0];
457 dest_pixel
[1] = src_pixel
[1];
458 dest_pixel
[2] = src_pixel
[2];
461 *(target_alpha
++) = *src_alpha_pixel
;
468 // In case this is a cursor, make sure the hotspot is scaled accordingly:
469 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
470 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
471 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
472 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
473 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
474 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
479 wxImage
wxImage::Rotate90( bool clockwise
) const
483 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
485 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
487 unsigned char *data
= image
.GetData();
489 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
491 unsigned char *source_data
= M_IMGDATA
->m_data
;
492 unsigned char *target_data
;
493 unsigned char *alpha_data
= 0 ;
494 unsigned char *source_alpha
= 0 ;
495 unsigned char *target_alpha
= 0 ;
497 if (M_IMGDATA
->m_hasMask
)
499 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
503 source_alpha
= M_IMGDATA
->m_alpha
;
507 alpha_data
= image
.GetAlpha() ;
511 long height
= M_IMGDATA
->m_height
;
512 long width
= M_IMGDATA
->m_width
;
514 for (long j
= 0; j
< height
; j
++)
516 for (long i
= 0; i
< width
; i
++)
520 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
522 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
526 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
528 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
530 memcpy( target_data
, source_data
, 3 );
535 memcpy( target_alpha
, source_alpha
, 1 );
544 wxImage
wxImage::Mirror( bool horizontally
) const
548 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
550 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
552 unsigned char *data
= image
.GetData();
553 unsigned char *alpha
= NULL
;
555 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
557 if (M_IMGDATA
->m_alpha
!= NULL
) {
559 alpha
= image
.GetAlpha();
560 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
563 if (M_IMGDATA
->m_hasMask
)
564 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
566 long height
= M_IMGDATA
->m_height
;
567 long width
= M_IMGDATA
->m_width
;
569 unsigned char *source_data
= M_IMGDATA
->m_data
;
570 unsigned char *target_data
;
574 for (long j
= 0; j
< height
; j
++)
577 target_data
= data
-3;
578 for (long i
= 0; i
< width
; i
++)
580 memcpy( target_data
, source_data
, 3 );
588 // src_alpha starts at the first pixel and increases by 1 after each step
589 // (a step here is the copy of the alpha value of one pixel)
590 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
591 // dest_alpha starts just beyond the first line, decreases before each step,
592 // and after each line is finished, increases by 2 widths (skipping the line
593 // just copied and the line that will be copied next)
594 unsigned char *dest_alpha
= alpha
+ width
;
596 for (long jj
= 0; jj
< height
; ++jj
)
598 for (long i
= 0; i
< width
; ++i
) {
599 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
601 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
607 for (long i
= 0; i
< height
; i
++)
609 target_data
= data
+ 3*width
*(height
-1-i
);
610 memcpy( target_data
, source_data
, (size_t)3*width
);
611 source_data
+= 3*width
;
616 // src_alpha starts at the first pixel and increases by 1 width after each step
617 // (a step here is the copy of the alpha channel of an entire line)
618 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
619 // dest_alpha starts just beyond the last line (beyond the whole image)
620 // and decreases by 1 width before each step
621 unsigned char *dest_alpha
= alpha
+ width
* height
;
623 for (long jj
= 0; jj
< height
; ++jj
)
626 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
635 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
639 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
641 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
642 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
643 image
, wxT("invalid subimage size") );
645 const int subwidth
= rect
.GetWidth();
646 const int subheight
= rect
.GetHeight();
648 image
.Create( subwidth
, subheight
, false );
650 const unsigned char *src_data
= GetData();
651 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
652 unsigned char *subdata
= image
.GetData();
653 unsigned char *subalpha
= NULL
;
655 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
657 if (src_alpha
!= NULL
) {
659 subalpha
= image
.GetAlpha();
660 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
663 if (M_IMGDATA
->m_hasMask
)
664 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
666 const int width
= GetWidth();
667 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
669 src_data
+= 3 * pixsoff
;
670 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
672 for (long j
= 0; j
< subheight
; ++j
)
674 memcpy( subdata
, src_data
, 3 * subwidth
);
675 subdata
+= 3 * subwidth
;
676 src_data
+= 3 * width
;
677 if (subalpha
!= NULL
) {
678 memcpy( subalpha
, src_alpha
, subwidth
);
679 subalpha
+= subwidth
;
687 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
688 int r_
, int g_
, int b_
) const
692 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
693 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
695 int width
= GetWidth(), height
= GetHeight();
696 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
698 unsigned char r
= (unsigned char)r_
;
699 unsigned char g
= (unsigned char)g_
;
700 unsigned char b
= (unsigned char)b_
;
701 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
703 GetOrFindMaskColour( &r
, &g
, &b
);
704 image
.SetMaskColour(r
, g
, b
);
707 image
.SetRGB(wxRect(), r
, g
, b
);
709 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
710 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
712 finalRect
.width
-= pos
.x
;
714 finalRect
.height
-= pos
.y
;
716 subRect
.Intersect(finalRect
);
718 if (!subRect
.IsEmpty())
720 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
721 image
.Paste(*this, pos
.x
, pos
.y
);
723 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
729 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
731 wxCHECK_RET( Ok(), wxT("invalid image") );
732 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
736 int width
= image
.GetWidth();
737 int height
= image
.GetHeight();
750 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
751 width
= M_IMGDATA
->m_width
- (x
+xx
);
752 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
753 height
= M_IMGDATA
->m_height
- (y
+yy
);
755 if (width
< 1) return;
756 if (height
< 1) return;
758 if ((!HasMask() && !image
.HasMask()) ||
759 (HasMask() && !image
.HasMask()) ||
760 ((HasMask() && image
.HasMask() &&
761 (GetMaskRed()==image
.GetMaskRed()) &&
762 (GetMaskGreen()==image
.GetMaskGreen()) &&
763 (GetMaskBlue()==image
.GetMaskBlue()))))
766 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
767 int source_step
= image
.GetWidth()*3;
769 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
770 int target_step
= M_IMGDATA
->m_width
*3;
771 for (int j
= 0; j
< height
; j
++)
773 memcpy( target_data
, source_data
, width
);
774 source_data
+= source_step
;
775 target_data
+= target_step
;
780 if (!HasMask() && image
.HasMask())
782 unsigned char r
= image
.GetMaskRed();
783 unsigned char g
= image
.GetMaskGreen();
784 unsigned char b
= image
.GetMaskBlue();
787 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
788 int source_step
= image
.GetWidth()*3;
790 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
791 int target_step
= M_IMGDATA
->m_width
*3;
793 for (int j
= 0; j
< height
; j
++)
795 for (int i
= 0; i
< width
; i
+=3)
797 if ((source_data
[i
] != r
) &&
798 (source_data
[i
+1] != g
) &&
799 (source_data
[i
+2] != b
))
801 memcpy( target_data
+i
, source_data
+i
, 3 );
804 source_data
+= source_step
;
805 target_data
+= target_step
;
810 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
811 unsigned char r2
, unsigned char g2
, unsigned char b2
)
813 wxCHECK_RET( Ok(), wxT("invalid image") );
815 unsigned char *data
= GetData();
817 const int w
= GetWidth();
818 const int h
= GetHeight();
820 for (int j
= 0; j
< h
; j
++)
821 for (int i
= 0; i
< w
; i
++)
823 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
833 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
837 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
839 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
841 unsigned char *dest
= image
.GetData();
843 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
845 unsigned char *src
= M_IMGDATA
->m_data
;
846 bool hasMask
= M_IMGDATA
->m_hasMask
;
847 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
848 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
849 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
852 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
854 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
855 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
857 // don't modify the mask
858 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
860 memcpy(dest
, src
, 3);
864 // calculate the luma
865 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
866 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
870 // copy the alpha channel, if any
873 const size_t alphaSize
= GetWidth() * GetHeight();
874 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
875 memcpy(alpha
, GetAlpha(), alphaSize
);
877 image
.SetAlpha(alpha
);
883 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
887 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
889 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
891 unsigned char *data
= image
.GetData();
893 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
895 if (M_IMGDATA
->m_hasMask
)
897 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
898 M_IMGDATA
->m_maskBlue
== b
)
899 image
.SetMaskColour( 255, 255, 255 );
901 image
.SetMaskColour( 0, 0, 0 );
904 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
906 unsigned char *srcd
= M_IMGDATA
->m_data
;
907 unsigned char *tard
= image
.GetData();
909 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
911 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
912 tard
[0] = tard
[1] = tard
[2] = 255;
914 tard
[0] = tard
[1] = tard
[2] = 0;
920 int wxImage::GetWidth() const
922 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
924 return M_IMGDATA
->m_width
;
927 int wxImage::GetHeight() const
929 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
931 return M_IMGDATA
->m_height
;
934 long wxImage::XYToIndex(int x
, int y
) const
938 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
940 return y
*M_IMGDATA
->m_width
+ x
;
946 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
948 long pos
= XYToIndex(x
, y
);
949 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
953 M_IMGDATA
->m_data
[ pos
] = r
;
954 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
955 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
958 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
960 wxCHECK_RET( Ok(), wxT("invalid image") );
963 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
964 if ( rect
== wxRect() )
970 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
971 imageRect
.Inside(rect
.GetBottomRight()),
972 wxT("invalid bounding rectangle") );
975 int x1
= rect
.GetLeft(),
977 x2
= rect
.GetRight() + 1,
978 y2
= rect
.GetBottom() + 1;
980 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
981 int x
, y
, width
= GetWidth();
982 for (y
= y1
; y
< y2
; y
++)
984 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
985 for (x
= x1
; x
< x2
; x
++)
994 unsigned char wxImage::GetRed( int x
, int y
) const
996 long pos
= XYToIndex(x
, y
);
997 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1001 return M_IMGDATA
->m_data
[pos
];
1004 unsigned char wxImage::GetGreen( int x
, int y
) const
1006 long pos
= XYToIndex(x
, y
);
1007 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1011 return M_IMGDATA
->m_data
[pos
+1];
1014 unsigned char wxImage::GetBlue( int x
, int y
) const
1016 long pos
= XYToIndex(x
, y
);
1017 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1021 return M_IMGDATA
->m_data
[pos
+2];
1024 bool wxImage::Ok() const
1026 // image of 0 width or height can't be considered ok - at least because it
1027 // causes crashes in ConvertToBitmap() if we don't catch it in time
1028 wxImageRefData
*data
= M_IMGDATA
;
1029 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1032 unsigned char *wxImage::GetData() const
1034 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1036 return M_IMGDATA
->m_data
;
1039 void wxImage::SetData( unsigned char *data
, bool static_data
)
1041 wxCHECK_RET( Ok(), wxT("invalid image") );
1043 wxImageRefData
*newRefData
= new wxImageRefData();
1045 newRefData
->m_width
= M_IMGDATA
->m_width
;
1046 newRefData
->m_height
= M_IMGDATA
->m_height
;
1047 newRefData
->m_data
= data
;
1048 newRefData
->m_ok
= true;
1049 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1050 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1051 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1052 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1053 newRefData
->m_static
= static_data
;
1057 m_refData
= newRefData
;
1060 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1062 wxImageRefData
*newRefData
= new wxImageRefData();
1066 newRefData
->m_width
= new_width
;
1067 newRefData
->m_height
= new_height
;
1068 newRefData
->m_data
= data
;
1069 newRefData
->m_ok
= true;
1070 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1071 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1072 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1073 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1077 newRefData
->m_width
= new_width
;
1078 newRefData
->m_height
= new_height
;
1079 newRefData
->m_data
= data
;
1080 newRefData
->m_ok
= true;
1082 newRefData
->m_static
= static_data
;
1086 m_refData
= newRefData
;
1089 // ----------------------------------------------------------------------------
1090 // alpha channel support
1091 // ----------------------------------------------------------------------------
1093 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1095 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1097 long pos
= XYToIndex(x
, y
);
1098 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1100 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1103 unsigned char wxImage::GetAlpha(int x
, int y
) const
1105 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1107 long pos
= XYToIndex(x
, y
);
1108 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1110 return M_IMGDATA
->m_alpha
[pos
];
1114 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1118 const int w
= M_IMGDATA
->m_width
;
1119 const int h
= M_IMGDATA
->m_height
;
1121 unsigned char *alpha
= GetAlpha();
1122 unsigned char *data
= GetData();
1124 for ( int y
= 0; y
< h
; y
++ )
1126 for ( int x
= 0; x
< w
; x
++ )
1138 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1140 wxCHECK_RET( Ok(), wxT("invalid image") );
1144 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1147 free(M_IMGDATA
->m_alpha
);
1148 M_IMGDATA
->m_alpha
= alpha
;
1149 M_IMGDATA
->m_staticAlpha
= static_data
;
1152 unsigned char *wxImage::GetAlpha() const
1154 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1156 return M_IMGDATA
->m_alpha
;
1159 void wxImage::InitAlpha()
1161 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1163 // initialize memory for alpha channel
1166 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1167 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1171 // use the mask to initialize the alpha channel.
1172 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1174 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1175 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1176 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1177 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1181 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1182 ? wxIMAGE_ALPHA_TRANSPARENT
1183 : wxIMAGE_ALPHA_OPAQUE
;
1186 M_IMGDATA
->m_hasMask
= false;
1190 // make the image fully opaque
1191 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1199 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1201 wxCHECK_RET( Ok(), wxT("invalid image") );
1203 M_IMGDATA
->m_maskRed
= r
;
1204 M_IMGDATA
->m_maskGreen
= g
;
1205 M_IMGDATA
->m_maskBlue
= b
;
1206 M_IMGDATA
->m_hasMask
= true;
1209 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1211 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1213 if (M_IMGDATA
->m_hasMask
)
1215 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1216 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1217 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1222 FindFirstUnusedColour(r
, g
, b
);
1227 unsigned char wxImage::GetMaskRed() const
1229 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1231 return M_IMGDATA
->m_maskRed
;
1234 unsigned char wxImage::GetMaskGreen() const
1236 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1238 return M_IMGDATA
->m_maskGreen
;
1241 unsigned char wxImage::GetMaskBlue() const
1243 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1245 return M_IMGDATA
->m_maskBlue
;
1248 void wxImage::SetMask( bool mask
)
1250 wxCHECK_RET( Ok(), wxT("invalid image") );
1252 M_IMGDATA
->m_hasMask
= mask
;
1255 bool wxImage::HasMask() const
1257 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1259 return M_IMGDATA
->m_hasMask
;
1262 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1264 long pos
= XYToIndex(x
, y
);
1265 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1268 if ( M_IMGDATA
->m_hasMask
)
1270 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1271 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1272 p
[1] == M_IMGDATA
->m_maskGreen
&&
1273 p
[2] == M_IMGDATA
->m_maskBlue
)
1280 if ( M_IMGDATA
->m_alpha
)
1282 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1284 // transparent enough
1293 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1294 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1296 // check that the images are the same size
1297 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1299 wxLogError( _("Image and mask have different sizes.") );
1303 // find unused colour
1304 unsigned char r
,g
,b
;
1305 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1307 wxLogError( _("No unused colour in image being masked.") );
1311 unsigned char *imgdata
= GetData();
1312 unsigned char *maskdata
= mask
.GetData();
1314 const int w
= GetWidth();
1315 const int h
= GetHeight();
1317 for (int j
= 0; j
< h
; j
++)
1319 for (int i
= 0; i
< w
; i
++)
1321 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1332 SetMaskColour(r
, g
, b
);
1338 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1343 unsigned char mr
, mg
, mb
;
1344 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1346 wxLogError( _("No unused colour in image being masked.") );
1351 SetMaskColour(mr
, mg
, mb
);
1353 unsigned char *imgdata
= GetData();
1354 unsigned char *alphadata
= GetAlpha();
1357 int h
= GetHeight();
1359 for (int y
= 0; y
< h
; y
++)
1361 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1363 if (*alphadata
< threshold
)
1372 free(M_IMGDATA
->m_alpha
);
1373 M_IMGDATA
->m_alpha
= NULL
;
1378 // ----------------------------------------------------------------------------
1379 // Palette functions
1380 // ----------------------------------------------------------------------------
1384 bool wxImage::HasPalette() const
1389 return M_IMGDATA
->m_palette
.Ok();
1392 const wxPalette
& wxImage::GetPalette() const
1394 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1396 return M_IMGDATA
->m_palette
;
1399 void wxImage::SetPalette(const wxPalette
& palette
)
1401 wxCHECK_RET( Ok(), wxT("invalid image") );
1403 M_IMGDATA
->m_palette
= palette
;
1406 #endif // wxUSE_PALETTE
1408 // ----------------------------------------------------------------------------
1409 // Option functions (arbitrary name/value mapping)
1410 // ----------------------------------------------------------------------------
1412 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1414 wxCHECK_RET( Ok(), wxT("invalid image") );
1416 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1417 if (idx
== wxNOT_FOUND
)
1419 M_IMGDATA
->m_optionNames
.Add(name
);
1420 M_IMGDATA
->m_optionValues
.Add(value
);
1424 M_IMGDATA
->m_optionNames
[idx
] = name
;
1425 M_IMGDATA
->m_optionValues
[idx
] = value
;
1429 void wxImage::SetOption(const wxString
& name
, int value
)
1432 valStr
.Printf(wxT("%d"), value
);
1433 SetOption(name
, valStr
);
1436 wxString
wxImage::GetOption(const wxString
& name
) const
1438 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1440 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1441 if (idx
== wxNOT_FOUND
)
1442 return wxEmptyString
;
1444 return M_IMGDATA
->m_optionValues
[idx
];
1447 int wxImage::GetOptionInt(const wxString
& name
) const
1449 return wxAtoi(GetOption(name
));
1452 bool wxImage::HasOption(const wxString
& name
) const
1454 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1456 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1459 // ----------------------------------------------------------------------------
1461 // ----------------------------------------------------------------------------
1463 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1464 long WXUNUSED_UNLESS_STREAMS(type
),
1465 int WXUNUSED_UNLESS_STREAMS(index
) )
1468 if (wxFileExists(filename
))
1470 wxFileInputStream
stream(filename
);
1471 wxBufferedInputStream
bstream( stream
);
1472 return LoadFile(bstream
, type
, index
);
1476 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1480 #else // !wxUSE_STREAMS
1482 #endif // wxUSE_STREAMS
1485 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1486 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
1487 int WXUNUSED_UNLESS_STREAMS(index
) )
1490 if (wxFileExists(filename
))
1492 wxFileInputStream
stream(filename
);
1493 wxBufferedInputStream
bstream( stream
);
1494 return LoadFile(bstream
, mimetype
, index
);
1498 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1502 #else // !wxUSE_STREAMS
1504 #endif // wxUSE_STREAMS
1509 bool wxImage::SaveFile( const wxString
& filename
) const
1511 wxString ext
= filename
.AfterLast('.').Lower();
1513 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1516 SaveFile(filename
, pHandler
->GetType());
1520 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1525 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1526 int WXUNUSED_UNLESS_STREAMS(type
) ) const
1529 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1531 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1533 wxFileOutputStream
stream(filename
);
1535 if ( stream
.IsOk() )
1537 wxBufferedOutputStream
bstream( stream
);
1538 return SaveFile(bstream
, type
);
1540 #endif // wxUSE_STREAMS
1545 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1546 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
1549 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1551 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1553 wxFileOutputStream
stream(filename
);
1555 if ( stream
.IsOk() )
1557 wxBufferedOutputStream
bstream( stream
);
1558 return SaveFile(bstream
, mimetype
);
1560 #endif // wxUSE_STREAMS
1565 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
1568 wxFileInputStream
stream(name
);
1569 return CanRead(stream
);
1575 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
1576 long WXUNUSED_UNLESS_STREAMS(type
) )
1579 wxFileInputStream
stream(name
);
1581 return GetImageCount(stream
, type
);
1589 bool wxImage::CanRead( wxInputStream
&stream
)
1591 const wxList
& list
= GetHandlers();
1593 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1595 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1596 if (handler
->CanRead( stream
))
1603 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1605 wxImageHandler
*handler
;
1607 if ( type
== wxBITMAP_TYPE_ANY
)
1609 wxList
&list
=GetHandlers();
1611 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1613 handler
=(wxImageHandler
*)node
->GetData();
1614 if ( handler
->CanRead(stream
) )
1615 return handler
->GetImageCount(stream
);
1619 wxLogWarning(_("No handler found for image type."));
1623 handler
= FindHandler(type
);
1627 wxLogWarning(_("No image handler for type %d defined."), type
);
1631 if ( handler
->CanRead(stream
) )
1633 return handler
->GetImageCount(stream
);
1637 wxLogError(_("Image file is not of type %d."), type
);
1642 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1646 m_refData
= new wxImageRefData
;
1648 wxImageHandler
*handler
;
1650 if ( type
== wxBITMAP_TYPE_ANY
)
1652 wxList
&list
=GetHandlers();
1654 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1656 handler
=(wxImageHandler
*)node
->GetData();
1657 if ( handler
->CanRead(stream
) )
1658 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1662 wxLogWarning( _("No handler found for image type.") );
1666 handler
= FindHandler(type
);
1670 wxLogWarning( _("No image handler for type %d defined."), type
);
1675 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1677 wxLogError(_("Image file is not of type %d."), type
);
1681 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1684 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1688 m_refData
= new wxImageRefData
;
1690 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1694 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1699 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1701 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1705 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1708 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1710 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1712 wxImageHandler
*handler
= FindHandler(type
);
1715 wxLogWarning( _("No image handler for type %d defined."), type
);
1720 return handler
->SaveFile( (wxImage
*)this, stream
);
1723 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1725 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1727 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1730 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1735 return handler
->SaveFile( (wxImage
*)this, stream
);
1737 #endif // wxUSE_STREAMS
1739 // ----------------------------------------------------------------------------
1740 // image I/O handlers
1741 // ----------------------------------------------------------------------------
1743 void wxImage::AddHandler( wxImageHandler
*handler
)
1745 // Check for an existing handler of the type being added.
1746 if (FindHandler( handler
->GetType() ) == 0)
1748 sm_handlers
.Append( handler
);
1752 // This is not documented behaviour, merely the simplest 'fix'
1753 // for preventing duplicate additions. If someone ever has
1754 // a good reason to add and remove duplicate handlers (and they
1755 // may) we should probably refcount the duplicates.
1756 // also an issue in InsertHandler below.
1758 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1759 handler
->GetName().c_str() );
1764 void wxImage::InsertHandler( wxImageHandler
*handler
)
1766 // Check for an existing handler of the type being added.
1767 if (FindHandler( handler
->GetType() ) == 0)
1769 sm_handlers
.Insert( handler
);
1773 // see AddHandler for additional comments.
1774 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1775 handler
->GetName().c_str() );
1780 bool wxImage::RemoveHandler( const wxString
& name
)
1782 wxImageHandler
*handler
= FindHandler(name
);
1785 sm_handlers
.DeleteObject(handler
);
1793 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1795 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1798 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1799 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1801 node
= node
->GetNext();
1806 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1808 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1811 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1812 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1813 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1815 node
= node
->GetNext();
1820 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1822 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1825 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1826 if (handler
->GetType() == bitmapType
) return handler
;
1827 node
= node
->GetNext();
1832 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1834 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1837 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1838 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1839 node
= node
->GetNext();
1844 void wxImage::InitStandardHandlers()
1847 AddHandler(new wxBMPHandler
);
1848 #endif // wxUSE_STREAMS
1851 void wxImage::CleanUpHandlers()
1853 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1856 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1857 wxList::compatibility_iterator next
= node
->GetNext();
1862 sm_handlers
.Clear();
1865 wxString
wxImage::GetImageExtWildcard()
1869 wxList
& Handlers
= wxImage::GetHandlers();
1870 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1873 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1874 fmts
+= wxT("*.") + Handler
->GetExtension();
1875 Node
= Node
->GetNext();
1876 if ( Node
) fmts
+= wxT(";");
1879 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1882 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1884 const double red
= rgb
.red
/ 255.0,
1885 green
= rgb
.green
/ 255.0,
1886 blue
= rgb
.blue
/ 255.0;
1888 // find the min and max intensity (and remember which one was it for the
1890 double minimumRGB
= red
;
1891 if ( green
< minimumRGB
)
1893 if ( blue
< minimumRGB
)
1896 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1897 double maximumRGB
= red
;
1898 if ( green
> maximumRGB
)
1903 if ( blue
> maximumRGB
)
1909 const double value
= maximumRGB
;
1911 double hue
= 0.0, saturation
;
1912 const double deltaRGB
= maximumRGB
- minimumRGB
;
1913 if ( wxIsNullDouble(deltaRGB
) )
1915 // Gray has no color
1924 hue
= (green
- blue
) / deltaRGB
;
1928 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1932 hue
= 4.0 + (red
- green
) / deltaRGB
;
1936 wxFAIL_MSG(wxT("hue not specified"));
1945 saturation
= deltaRGB
/ maximumRGB
;
1948 return HSVValue(hue
, saturation
, value
);
1951 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1953 double red
, green
, blue
;
1955 if ( wxIsNullDouble(hsv
.saturation
) )
1964 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1965 int i
= (int)floor(hue
);
1966 double f
= hue
- i
; // fractional part of h
1967 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1973 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1978 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1986 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1991 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1996 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2004 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2009 return RGBValue((unsigned char)(red
* 255.0),
2010 (unsigned char)(green
* 255.0),
2011 (unsigned char)(blue
* 255.0));
2015 * Rotates the hue of each pixel of the image. angle is a double in the range
2016 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2018 void wxImage::RotateHue(double angle
)
2020 unsigned char *srcBytePtr
;
2021 unsigned char *dstBytePtr
;
2022 unsigned long count
;
2023 wxImage::HSVValue hsv
;
2024 wxImage::RGBValue rgb
;
2026 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2027 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2028 if ( count
> 0 && !wxIsNullDouble(angle
) )
2030 srcBytePtr
= M_IMGDATA
->m_data
;
2031 dstBytePtr
= srcBytePtr
;
2034 rgb
.red
= *srcBytePtr
++;
2035 rgb
.green
= *srcBytePtr
++;
2036 rgb
.blue
= *srcBytePtr
++;
2037 hsv
= RGBtoHSV(rgb
);
2039 hsv
.hue
= hsv
.hue
+ angle
;
2041 hsv
.hue
= hsv
.hue
- 1.0;
2042 else if (hsv
.hue
< 0.0)
2043 hsv
.hue
= hsv
.hue
+ 1.0;
2045 rgb
= HSVtoRGB(hsv
);
2046 *dstBytePtr
++ = rgb
.red
;
2047 *dstBytePtr
++ = rgb
.green
;
2048 *dstBytePtr
++ = rgb
.blue
;
2049 } while (--count
!= 0);
2053 //-----------------------------------------------------------------------------
2055 //-----------------------------------------------------------------------------
2057 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2060 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2065 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2070 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2075 bool wxImageHandler::CanRead( const wxString
& name
)
2077 if (wxFileExists(name
))
2079 wxFileInputStream
stream(name
);
2080 return CanRead(stream
);
2083 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2088 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2090 wxFileOffset posOld
= stream
.TellI();
2091 if ( posOld
== wxInvalidOffset
)
2093 // can't test unseekable stream
2097 bool ok
= DoCanRead(stream
);
2099 // restore the old position to be able to test other formats and so on
2100 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2102 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2104 // reading would fail anyhow as we're not at the right position
2111 #endif // wxUSE_STREAMS
2113 // ----------------------------------------------------------------------------
2114 // image histogram stuff
2115 // ----------------------------------------------------------------------------
2118 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2123 unsigned char g2
) const
2125 unsigned long key
= MakeKey(r2
, g2
, b2
);
2127 while ( find(key
) != end() )
2129 // color already used
2141 wxLogError(_("No unused colour in image.") );
2147 key
= MakeKey(r2
, g2
, b2
);
2161 wxImage::FindFirstUnusedColour(unsigned char *r
,
2166 unsigned char g2
) const
2168 wxImageHistogram histogram
;
2170 ComputeHistogram(histogram
);
2172 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2178 // Counts and returns the number of different colours. Optionally stops
2179 // when it exceeds 'stopafter' different colours. This is useful, for
2180 // example, to see if the image can be saved as 8-bit (256 colour or
2181 // less, in this case it would be invoked as CountColours(256)). Default
2182 // value for stopafter is -1 (don't care).
2184 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2188 unsigned char r
, g
, b
;
2190 unsigned long size
, nentries
, key
;
2193 size
= GetWidth() * GetHeight();
2196 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2201 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2203 if (h
.Get(key
) == NULL
)
2214 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2216 unsigned char *p
= GetData();
2217 unsigned long nentries
= 0;
2221 const unsigned long size
= GetWidth() * GetHeight();
2223 unsigned char r
, g
, b
;
2224 for ( unsigned long n
= 0; n
< size
; n
++ )
2230 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2232 if ( entry
.value
++ == 0 )
2233 entry
.index
= nentries
++;
2240 * Rotation code by Carlos Moreno
2243 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2244 // does exactly the same thing. And I also got rid of wxRotationPixel
2245 // bacause of potential problems in architectures where alignment
2246 // is an issue, so I had to rewrite parts of the code.
2248 static const double gs_Epsilon
= 1e-10;
2250 static inline int wxCint (double x
)
2252 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2256 // Auxiliary function to rotate a point (x,y) with respect to point p0
2257 // make it inline and use a straight return to facilitate optimization
2258 // also, the function receives the sine and cosine of the angle to avoid
2259 // repeating the time-consuming calls to these functions -- sin/cos can
2260 // be computed and stored in the calling function.
2262 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2264 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2265 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2268 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2270 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2273 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2276 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2278 bool has_alpha
= HasAlpha();
2280 // Create pointer-based array to accelerate access to wxImage's data
2281 unsigned char ** data
= new unsigned char * [GetHeight()];
2282 data
[0] = GetData();
2283 for (i
= 1; i
< GetHeight(); i
++)
2284 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2286 // Same for alpha channel
2287 unsigned char ** alpha
= NULL
;
2290 alpha
= new unsigned char * [GetHeight()];
2291 alpha
[0] = GetAlpha();
2292 for (i
= 1; i
< GetHeight(); i
++)
2293 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2296 // precompute coefficients for rotation formula
2297 // (sine and cosine of the angle)
2298 const double cos_angle
= cos(angle
);
2299 const double sin_angle
= sin(angle
);
2301 // Create new Image to store the result
2302 // First, find rectangle that covers the rotated image; to do that,
2303 // rotate the four corners
2305 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2307 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2308 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2309 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2310 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2312 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2313 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2314 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2315 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2317 // Create rotated image
2318 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2319 // With alpha channel
2323 if (offset_after_rotation
!= NULL
)
2325 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2328 // GRG: The rotated (destination) image is always accessed
2329 // sequentially, so there is no need for a pointer-based
2330 // array here (and in fact it would be slower).
2332 unsigned char * dst
= rotated
.GetData();
2334 unsigned char * alpha_dst
= NULL
;
2336 alpha_dst
= rotated
.GetAlpha();
2338 // GRG: if the original image has a mask, use its RGB values
2339 // as the blank pixel, else, fall back to default (black).
2341 unsigned char blank_r
= 0;
2342 unsigned char blank_g
= 0;
2343 unsigned char blank_b
= 0;
2347 blank_r
= GetMaskRed();
2348 blank_g
= GetMaskGreen();
2349 blank_b
= GetMaskBlue();
2350 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2353 // Now, for each point of the rotated image, find where it came from, by
2354 // performing an inverse rotation (a rotation of -angle) and getting the
2355 // pixel at those coordinates
2357 // GRG: I've taken the (interpolating) test out of the loops, so that
2358 // it is done only once, instead of repeating it for each pixel.
2363 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2365 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2367 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2369 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2370 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2372 // interpolate using the 4 enclosing grid-points. Those
2373 // points can be obtained using floor and ceiling of the
2374 // exact coordinates of the point
2377 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2379 x1
= wxCint(floor(src
.x
));
2380 x2
= wxCint(ceil(src
.x
));
2382 else // else means that x is near one of the borders (0 or width-1)
2384 x1
= x2
= wxCint (src
.x
);
2387 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2389 y1
= wxCint(floor(src
.y
));
2390 y2
= wxCint(ceil(src
.y
));
2394 y1
= y2
= wxCint (src
.y
);
2397 // get four points and the distances (square of the distance,
2398 // for efficiency reasons) for the interpolation formula
2400 // GRG: Do not calculate the points until they are
2401 // really needed -- this way we can calculate
2402 // just one, instead of four, if d1, d2, d3
2403 // or d4 are < gs_Epsilon
2405 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2406 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2407 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2408 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2410 // Now interpolate as a weighted average of the four surrounding
2411 // points, where the weights are the distances to each of those points
2413 // If the point is exactly at one point of the grid of the source
2414 // image, then don't interpolate -- just assign the pixel
2416 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2418 unsigned char *p
= data
[y1
] + (3 * x1
);
2424 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2426 else if (d2
< gs_Epsilon
)
2428 unsigned char *p
= data
[y1
] + (3 * x2
);
2434 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2436 else if (d3
< gs_Epsilon
)
2438 unsigned char *p
= data
[y2
] + (3 * x2
);
2444 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2446 else if (d4
< gs_Epsilon
)
2448 unsigned char *p
= data
[y2
] + (3 * x1
);
2454 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2458 // weights for the weighted average are proportional to the inverse of the distance
2459 unsigned char *v1
= data
[y1
] + (3 * x1
);
2460 unsigned char *v2
= data
[y1
] + (3 * x2
);
2461 unsigned char *v3
= data
[y2
] + (3 * x2
);
2462 unsigned char *v4
= data
[y2
] + (3 * x1
);
2464 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2468 *(dst
++) = (unsigned char)
2469 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2470 w3
* *(v3
++) + w4
* *(v4
++)) /
2471 (w1
+ w2
+ w3
+ w4
) );
2472 *(dst
++) = (unsigned char)
2473 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2474 w3
* *(v3
++) + w4
* *(v4
++)) /
2475 (w1
+ w2
+ w3
+ w4
) );
2476 *(dst
++) = (unsigned char)
2477 ( (w1
* *v1
+ w2
* *v2
+
2478 w3
* *v3
+ w4
* *v4
) /
2479 (w1
+ w2
+ w3
+ w4
) );
2483 v1
= alpha
[y1
] + (x1
);
2484 v2
= alpha
[y1
] + (x2
);
2485 v3
= alpha
[y2
] + (x2
);
2486 v4
= alpha
[y2
] + (x1
);
2488 *(alpha_dst
++) = (unsigned char)
2489 ( (w1
* *v1
+ w2
* *v2
+
2490 w3
* *v3
+ w4
* *v4
) /
2491 (w1
+ w2
+ w3
+ w4
) );
2507 else // not interpolating
2509 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2511 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2513 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2515 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2516 const int ys
= wxCint (src
.y
); // closest integer
2518 if (0 <= xs
&& xs
< GetWidth() &&
2519 0 <= ys
&& ys
< GetHeight())
2521 unsigned char *p
= data
[ys
] + (3 * xs
);
2527 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2536 *(alpha_dst
++) = 255;
2554 // A module to allow wxImage initialization/cleanup
2555 // without calling these functions from app.cpp or from
2556 // the user's application.
2558 class wxImageModule
: public wxModule
2560 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2563 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2564 void OnExit() { wxImage::CleanUpHandlers(); };
2567 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2570 #endif // wxUSE_IMAGE