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"
29 #include "wx/filefn.h"
30 #include "wx/wfstream.h"
32 #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
);
873 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
877 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
879 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
881 unsigned char *data
= image
.GetData();
883 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
885 if (M_IMGDATA
->m_hasMask
)
887 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
888 M_IMGDATA
->m_maskBlue
== b
)
889 image
.SetMaskColour( 255, 255, 255 );
891 image
.SetMaskColour( 0, 0, 0 );
894 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
896 unsigned char *srcd
= M_IMGDATA
->m_data
;
897 unsigned char *tard
= image
.GetData();
899 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
901 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
902 tard
[0] = tard
[1] = tard
[2] = 255;
904 tard
[0] = tard
[1] = tard
[2] = 0;
910 int wxImage::GetWidth() const
912 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
914 return M_IMGDATA
->m_width
;
917 int wxImage::GetHeight() const
919 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
921 return M_IMGDATA
->m_height
;
924 long wxImage::XYToIndex(int x
, int y
) const
928 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
930 return y
*M_IMGDATA
->m_width
+ x
;
936 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
938 long pos
= XYToIndex(x
, y
);
939 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
943 M_IMGDATA
->m_data
[ pos
] = r
;
944 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
945 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
948 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
950 wxCHECK_RET( Ok(), wxT("invalid image") );
953 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
954 if ( rect
== wxRect() )
960 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
961 imageRect
.Inside(rect
.GetBottomRight()),
962 wxT("invalid bounding rectangle") );
965 int x1
= rect
.GetLeft(),
967 x2
= rect
.GetRight() + 1,
968 y2
= rect
.GetBottom() + 1;
970 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
971 int x
, y
, width
= GetWidth();
972 for (y
= y1
; y
< y2
; y
++)
974 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
975 for (x
= x1
; x
< x2
; x
++)
984 unsigned char wxImage::GetRed( int x
, int y
) const
986 long pos
= XYToIndex(x
, y
);
987 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
991 return M_IMGDATA
->m_data
[pos
];
994 unsigned char wxImage::GetGreen( 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
+1];
1004 unsigned char wxImage::GetBlue( 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
+2];
1014 bool wxImage::Ok() const
1016 // image of 0 width or height can't be considered ok - at least because it
1017 // causes crashes in ConvertToBitmap() if we don't catch it in time
1018 wxImageRefData
*data
= M_IMGDATA
;
1019 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1022 unsigned char *wxImage::GetData() const
1024 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1026 return M_IMGDATA
->m_data
;
1029 void wxImage::SetData( unsigned char *data
, bool static_data
)
1031 wxCHECK_RET( Ok(), wxT("invalid image") );
1033 wxImageRefData
*newRefData
= new wxImageRefData();
1035 newRefData
->m_width
= M_IMGDATA
->m_width
;
1036 newRefData
->m_height
= M_IMGDATA
->m_height
;
1037 newRefData
->m_data
= data
;
1038 newRefData
->m_ok
= true;
1039 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1040 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1041 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1042 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1043 newRefData
->m_static
= static_data
;
1047 m_refData
= newRefData
;
1050 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1052 wxImageRefData
*newRefData
= new wxImageRefData();
1056 newRefData
->m_width
= new_width
;
1057 newRefData
->m_height
= new_height
;
1058 newRefData
->m_data
= data
;
1059 newRefData
->m_ok
= true;
1060 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1061 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1062 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1063 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1067 newRefData
->m_width
= new_width
;
1068 newRefData
->m_height
= new_height
;
1069 newRefData
->m_data
= data
;
1070 newRefData
->m_ok
= true;
1072 newRefData
->m_static
= static_data
;
1076 m_refData
= newRefData
;
1079 // ----------------------------------------------------------------------------
1080 // alpha channel support
1081 // ----------------------------------------------------------------------------
1083 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1085 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1087 long pos
= XYToIndex(x
, y
);
1088 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1090 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1093 unsigned char wxImage::GetAlpha(int x
, int y
) const
1095 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1097 long pos
= XYToIndex(x
, y
);
1098 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1100 return M_IMGDATA
->m_alpha
[pos
];
1104 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1108 const int w
= M_IMGDATA
->m_width
;
1109 const int h
= M_IMGDATA
->m_height
;
1111 unsigned char *alpha
= GetAlpha();
1112 unsigned char *data
= GetData();
1114 for ( int y
= 0; y
< h
; y
++ )
1116 for ( int x
= 0; x
< w
; x
++ )
1128 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1130 wxCHECK_RET( Ok(), wxT("invalid image") );
1134 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1137 free(M_IMGDATA
->m_alpha
);
1138 M_IMGDATA
->m_alpha
= alpha
;
1139 M_IMGDATA
->m_staticAlpha
= static_data
;
1142 unsigned char *wxImage::GetAlpha() const
1144 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1146 return M_IMGDATA
->m_alpha
;
1149 void wxImage::InitAlpha()
1151 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1153 // initialize memory for alpha channel
1156 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1157 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1161 // use the mask to initialize the alpha channel.
1162 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1164 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1165 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1166 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1167 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1171 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1172 ? wxIMAGE_ALPHA_TRANSPARENT
1173 : wxIMAGE_ALPHA_OPAQUE
;
1176 M_IMGDATA
->m_hasMask
= false;
1180 // make the image fully opaque
1181 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1185 // ----------------------------------------------------------------------------
1187 // ----------------------------------------------------------------------------
1189 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1191 wxCHECK_RET( Ok(), wxT("invalid image") );
1193 M_IMGDATA
->m_maskRed
= r
;
1194 M_IMGDATA
->m_maskGreen
= g
;
1195 M_IMGDATA
->m_maskBlue
= b
;
1196 M_IMGDATA
->m_hasMask
= true;
1199 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1201 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1203 if (M_IMGDATA
->m_hasMask
)
1205 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1206 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1207 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1212 FindFirstUnusedColour(r
, g
, b
);
1217 unsigned char wxImage::GetMaskRed() const
1219 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1221 return M_IMGDATA
->m_maskRed
;
1224 unsigned char wxImage::GetMaskGreen() const
1226 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1228 return M_IMGDATA
->m_maskGreen
;
1231 unsigned char wxImage::GetMaskBlue() const
1233 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1235 return M_IMGDATA
->m_maskBlue
;
1238 void wxImage::SetMask( bool mask
)
1240 wxCHECK_RET( Ok(), wxT("invalid image") );
1242 M_IMGDATA
->m_hasMask
= mask
;
1245 bool wxImage::HasMask() const
1247 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1249 return M_IMGDATA
->m_hasMask
;
1252 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1254 long pos
= XYToIndex(x
, y
);
1255 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1258 if ( M_IMGDATA
->m_hasMask
)
1260 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1261 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1262 p
[1] == M_IMGDATA
->m_maskGreen
&&
1263 p
[2] == M_IMGDATA
->m_maskBlue
)
1270 if ( M_IMGDATA
->m_alpha
)
1272 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1274 // transparent enough
1283 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1284 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1286 // check that the images are the same size
1287 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1289 wxLogError( _("Image and mask have different sizes.") );
1293 // find unused colour
1294 unsigned char r
,g
,b
;
1295 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1297 wxLogError( _("No unused colour in image being masked.") );
1301 unsigned char *imgdata
= GetData();
1302 unsigned char *maskdata
= mask
.GetData();
1304 const int w
= GetWidth();
1305 const int h
= GetHeight();
1307 for (int j
= 0; j
< h
; j
++)
1309 for (int i
= 0; i
< w
; i
++)
1311 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1322 SetMaskColour(r
, g
, b
);
1328 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1333 unsigned char mr
, mg
, mb
;
1334 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1336 wxLogError( _("No unused colour in image being masked.") );
1341 SetMaskColour(mr
, mg
, mb
);
1343 unsigned char *imgdata
= GetData();
1344 unsigned char *alphadata
= GetAlpha();
1347 int h
= GetHeight();
1349 for (int y
= 0; y
< h
; y
++)
1351 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1353 if (*alphadata
< threshold
)
1362 free(M_IMGDATA
->m_alpha
);
1363 M_IMGDATA
->m_alpha
= NULL
;
1368 // ----------------------------------------------------------------------------
1369 // Palette functions
1370 // ----------------------------------------------------------------------------
1374 bool wxImage::HasPalette() const
1379 return M_IMGDATA
->m_palette
.Ok();
1382 const wxPalette
& wxImage::GetPalette() const
1384 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1386 return M_IMGDATA
->m_palette
;
1389 void wxImage::SetPalette(const wxPalette
& palette
)
1391 wxCHECK_RET( Ok(), wxT("invalid image") );
1393 M_IMGDATA
->m_palette
= palette
;
1396 #endif // wxUSE_PALETTE
1398 // ----------------------------------------------------------------------------
1399 // Option functions (arbitrary name/value mapping)
1400 // ----------------------------------------------------------------------------
1402 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1404 wxCHECK_RET( Ok(), wxT("invalid image") );
1406 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1407 if (idx
== wxNOT_FOUND
)
1409 M_IMGDATA
->m_optionNames
.Add(name
);
1410 M_IMGDATA
->m_optionValues
.Add(value
);
1414 M_IMGDATA
->m_optionNames
[idx
] = name
;
1415 M_IMGDATA
->m_optionValues
[idx
] = value
;
1419 void wxImage::SetOption(const wxString
& name
, int value
)
1422 valStr
.Printf(wxT("%d"), value
);
1423 SetOption(name
, valStr
);
1426 wxString
wxImage::GetOption(const wxString
& name
) const
1428 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1430 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1431 if (idx
== wxNOT_FOUND
)
1432 return wxEmptyString
;
1434 return M_IMGDATA
->m_optionValues
[idx
];
1437 int wxImage::GetOptionInt(const wxString
& name
) const
1439 return wxAtoi(GetOption(name
));
1442 bool wxImage::HasOption(const wxString
& name
) const
1444 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1446 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1449 // ----------------------------------------------------------------------------
1451 // ----------------------------------------------------------------------------
1453 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1456 if (wxFileExists(filename
))
1458 wxFileInputStream
stream(filename
);
1459 wxBufferedInputStream
bstream( stream
);
1460 return LoadFile(bstream
, type
, index
);
1464 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1468 #else // !wxUSE_STREAMS
1470 #endif // wxUSE_STREAMS
1473 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1476 if (wxFileExists(filename
))
1478 wxFileInputStream
stream(filename
);
1479 wxBufferedInputStream
bstream( stream
);
1480 return LoadFile(bstream
, mimetype
, index
);
1484 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1488 #else // !wxUSE_STREAMS
1490 #endif // wxUSE_STREAMS
1495 bool wxImage::SaveFile( const wxString
& filename
) const
1497 wxString ext
= filename
.AfterLast('.').Lower();
1499 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1502 SaveFile(filename
, pHandler
->GetType());
1506 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1511 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1514 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1516 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1518 wxFileOutputStream
stream(filename
);
1520 if ( stream
.IsOk() )
1522 wxBufferedOutputStream
bstream( stream
);
1523 return SaveFile(bstream
, type
);
1525 #endif // wxUSE_STREAMS
1530 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1533 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1535 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1537 wxFileOutputStream
stream(filename
);
1539 if ( stream
.IsOk() )
1541 wxBufferedOutputStream
bstream( stream
);
1542 return SaveFile(bstream
, mimetype
);
1544 #endif // wxUSE_STREAMS
1549 bool wxImage::CanRead( const wxString
&name
)
1552 wxFileInputStream
stream(name
);
1553 return CanRead(stream
);
1559 int wxImage::GetImageCount( const wxString
&name
, long type
)
1562 wxFileInputStream
stream(name
);
1564 return GetImageCount(stream
, type
);
1572 bool wxImage::CanRead( wxInputStream
&stream
)
1574 const wxList
& list
= GetHandlers();
1576 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1578 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1579 if (handler
->CanRead( stream
))
1586 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1588 wxImageHandler
*handler
;
1590 if ( type
== wxBITMAP_TYPE_ANY
)
1592 wxList
&list
=GetHandlers();
1594 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1596 handler
=(wxImageHandler
*)node
->GetData();
1597 if ( handler
->CanRead(stream
) )
1598 return handler
->GetImageCount(stream
);
1602 wxLogWarning(_("No handler found for image type."));
1606 handler
= FindHandler(type
);
1610 wxLogWarning(_("No image handler for type %d defined."), type
);
1614 if ( handler
->CanRead(stream
) )
1616 return handler
->GetImageCount(stream
);
1620 wxLogError(_("Image file is not of type %d."), type
);
1625 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1629 m_refData
= new wxImageRefData
;
1631 wxImageHandler
*handler
;
1633 if ( type
== wxBITMAP_TYPE_ANY
)
1635 wxList
&list
=GetHandlers();
1637 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1639 handler
=(wxImageHandler
*)node
->GetData();
1640 if ( handler
->CanRead(stream
) )
1641 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1645 wxLogWarning( _("No handler found for image type.") );
1649 handler
= FindHandler(type
);
1653 wxLogWarning( _("No image handler for type %d defined."), type
);
1658 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1660 wxLogError(_("Image file is not of type %d."), type
);
1664 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1667 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1671 m_refData
= new wxImageRefData
;
1673 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1677 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1682 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1684 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1688 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1691 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1693 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1695 wxImageHandler
*handler
= FindHandler(type
);
1698 wxLogWarning( _("No image handler for type %d defined."), type
);
1703 return handler
->SaveFile( (wxImage
*)this, stream
);
1706 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1708 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1710 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1713 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1718 return handler
->SaveFile( (wxImage
*)this, stream
);
1720 #endif // wxUSE_STREAMS
1722 // ----------------------------------------------------------------------------
1723 // image I/O handlers
1724 // ----------------------------------------------------------------------------
1726 void wxImage::AddHandler( wxImageHandler
*handler
)
1728 // Check for an existing handler of the type being added.
1729 if (FindHandler( handler
->GetType() ) == 0)
1731 sm_handlers
.Append( handler
);
1735 // This is not documented behaviour, merely the simplest 'fix'
1736 // for preventing duplicate additions. If someone ever has
1737 // a good reason to add and remove duplicate handlers (and they
1738 // may) we should probably refcount the duplicates.
1739 // also an issue in InsertHandler below.
1741 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1742 handler
->GetName().c_str() );
1747 void wxImage::InsertHandler( wxImageHandler
*handler
)
1749 // Check for an existing handler of the type being added.
1750 if (FindHandler( handler
->GetType() ) == 0)
1752 sm_handlers
.Insert( handler
);
1756 // see AddHandler for additional comments.
1757 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1758 handler
->GetName().c_str() );
1763 bool wxImage::RemoveHandler( const wxString
& name
)
1765 wxImageHandler
*handler
= FindHandler(name
);
1768 sm_handlers
.DeleteObject(handler
);
1776 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1778 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1781 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1782 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1784 node
= node
->GetNext();
1789 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1791 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1794 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1795 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1796 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1798 node
= node
->GetNext();
1803 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1805 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1808 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1809 if (handler
->GetType() == bitmapType
) return handler
;
1810 node
= node
->GetNext();
1815 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1817 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1820 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1821 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1822 node
= node
->GetNext();
1827 void wxImage::InitStandardHandlers()
1830 AddHandler(new wxBMPHandler
);
1831 #endif // wxUSE_STREAMS
1834 void wxImage::CleanUpHandlers()
1836 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1839 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1840 wxList::compatibility_iterator next
= node
->GetNext();
1845 sm_handlers
.Clear();
1848 wxString
wxImage::GetImageExtWildcard()
1852 wxList
& Handlers
= wxImage::GetHandlers();
1853 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1856 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1857 fmts
+= wxT("*.") + Handler
->GetExtension();
1858 Node
= Node
->GetNext();
1859 if ( Node
) fmts
+= wxT(";");
1862 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1865 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1867 const double red
= rgb
.red
/ 255.0,
1868 green
= rgb
.green
/ 255.0,
1869 blue
= rgb
.blue
/ 255.0;
1871 // find the min and max intensity (and remember which one was it for the
1873 double minimumRGB
= red
;
1874 if ( green
< minimumRGB
)
1876 if ( blue
< minimumRGB
)
1879 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1880 double maximumRGB
= red
;
1881 if ( green
> maximumRGB
)
1886 if ( blue
> maximumRGB
)
1892 const double value
= maximumRGB
;
1894 double hue
= 0.0, saturation
;
1895 const double deltaRGB
= maximumRGB
- minimumRGB
;
1896 if ( wxIsNullDouble(deltaRGB
) )
1898 // Gray has no color
1907 hue
= (green
- blue
) / deltaRGB
;
1911 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1915 hue
= 4.0 + (red
- green
) / deltaRGB
;
1919 wxFAIL_MSG(wxT("hue not specified"));
1928 saturation
= deltaRGB
/ maximumRGB
;
1931 return HSVValue(hue
, saturation
, value
);
1934 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1936 double red
, green
, blue
;
1938 if ( wxIsNullDouble(hsv
.saturation
) )
1947 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1948 int i
= (int)floor(hue
);
1949 double f
= hue
- i
; // fractional part of h
1950 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1956 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1961 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1969 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1974 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1979 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1987 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1992 return RGBValue((unsigned char)(red
* 255.0),
1993 (unsigned char)(green
* 255.0),
1994 (unsigned char)(blue
* 255.0));
1998 * Rotates the hue of each pixel of the image. angle is a double in the range
1999 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2001 void wxImage::RotateHue(double angle
)
2003 unsigned char *srcBytePtr
;
2004 unsigned char *dstBytePtr
;
2005 unsigned long count
;
2006 wxImage::HSVValue hsv
;
2007 wxImage::RGBValue rgb
;
2009 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2010 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2011 if ( count
> 0 && !wxIsNullDouble(angle
) )
2013 srcBytePtr
= M_IMGDATA
->m_data
;
2014 dstBytePtr
= srcBytePtr
;
2017 rgb
.red
= *srcBytePtr
++;
2018 rgb
.green
= *srcBytePtr
++;
2019 rgb
.blue
= *srcBytePtr
++;
2020 hsv
= RGBtoHSV(rgb
);
2022 hsv
.hue
= hsv
.hue
+ angle
;
2024 hsv
.hue
= hsv
.hue
- 1.0;
2025 else if (hsv
.hue
< 0.0)
2026 hsv
.hue
= hsv
.hue
+ 1.0;
2028 rgb
= HSVtoRGB(hsv
);
2029 *dstBytePtr
++ = rgb
.red
;
2030 *dstBytePtr
++ = rgb
.green
;
2031 *dstBytePtr
++ = rgb
.blue
;
2032 } while (--count
!= 0);
2036 //-----------------------------------------------------------------------------
2038 //-----------------------------------------------------------------------------
2040 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2043 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2048 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2053 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2058 bool wxImageHandler::CanRead( const wxString
& name
)
2060 if (wxFileExists(name
))
2062 wxFileInputStream
stream(name
);
2063 return CanRead(stream
);
2066 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2071 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2073 wxFileOffset posOld
= stream
.TellI();
2074 if ( posOld
== wxInvalidOffset
)
2076 // can't test unseekable stream
2080 bool ok
= DoCanRead(stream
);
2082 // restore the old position to be able to test other formats and so on
2083 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2085 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2087 // reading would fail anyhow as we're not at the right position
2094 #endif // wxUSE_STREAMS
2096 // ----------------------------------------------------------------------------
2097 // image histogram stuff
2098 // ----------------------------------------------------------------------------
2101 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2106 unsigned char g2
) const
2108 unsigned long key
= MakeKey(r2
, g2
, b2
);
2110 while ( find(key
) != end() )
2112 // color already used
2124 wxLogError(_("No unused colour in image.") );
2130 key
= MakeKey(r2
, g2
, b2
);
2144 wxImage::FindFirstUnusedColour(unsigned char *r
,
2149 unsigned char g2
) const
2151 wxImageHistogram histogram
;
2153 ComputeHistogram(histogram
);
2155 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2161 // Counts and returns the number of different colours. Optionally stops
2162 // when it exceeds 'stopafter' different colours. This is useful, for
2163 // example, to see if the image can be saved as 8-bit (256 colour or
2164 // less, in this case it would be invoked as CountColours(256)). Default
2165 // value for stopafter is -1 (don't care).
2167 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2171 unsigned char r
, g
, b
;
2173 unsigned long size
, nentries
, key
;
2176 size
= GetWidth() * GetHeight();
2179 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2184 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2186 if (h
.Get(key
) == NULL
)
2197 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2199 unsigned char *p
= GetData();
2200 unsigned long nentries
= 0;
2204 const unsigned long size
= GetWidth() * GetHeight();
2206 unsigned char r
, g
, b
;
2207 for ( unsigned long n
= 0; n
< size
; n
++ )
2213 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2215 if ( entry
.value
++ == 0 )
2216 entry
.index
= nentries
++;
2223 * Rotation code by Carlos Moreno
2226 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2227 // does exactly the same thing. And I also got rid of wxRotationPixel
2228 // bacause of potential problems in architectures where alignment
2229 // is an issue, so I had to rewrite parts of the code.
2231 static const double gs_Epsilon
= 1e-10;
2233 static inline int wxCint (double x
)
2235 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2239 // Auxiliary function to rotate a point (x,y) with respect to point p0
2240 // make it inline and use a straight return to facilitate optimization
2241 // also, the function receives the sine and cosine of the angle to avoid
2242 // repeating the time-consuming calls to these functions -- sin/cos can
2243 // be computed and stored in the calling function.
2245 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2247 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2248 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2251 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2253 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2256 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2259 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2261 bool has_alpha
= HasAlpha();
2263 // Create pointer-based array to accelerate access to wxImage's data
2264 unsigned char ** data
= new unsigned char * [GetHeight()];
2265 data
[0] = GetData();
2266 for (i
= 1; i
< GetHeight(); i
++)
2267 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2269 // Same for alpha channel
2270 unsigned char ** alpha
= NULL
;
2273 alpha
= new unsigned char * [GetHeight()];
2274 alpha
[0] = GetAlpha();
2275 for (i
= 1; i
< GetHeight(); i
++)
2276 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2279 // precompute coefficients for rotation formula
2280 // (sine and cosine of the angle)
2281 const double cos_angle
= cos(angle
);
2282 const double sin_angle
= sin(angle
);
2284 // Create new Image to store the result
2285 // First, find rectangle that covers the rotated image; to do that,
2286 // rotate the four corners
2288 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2290 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2291 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2292 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2293 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2295 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2296 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2297 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2298 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2300 // Create rotated image
2301 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2302 // With alpha channel
2306 if (offset_after_rotation
!= NULL
)
2308 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2311 // GRG: The rotated (destination) image is always accessed
2312 // sequentially, so there is no need for a pointer-based
2313 // array here (and in fact it would be slower).
2315 unsigned char * dst
= rotated
.GetData();
2317 unsigned char * alpha_dst
= NULL
;
2319 alpha_dst
= rotated
.GetAlpha();
2321 // GRG: if the original image has a mask, use its RGB values
2322 // as the blank pixel, else, fall back to default (black).
2324 unsigned char blank_r
= 0;
2325 unsigned char blank_g
= 0;
2326 unsigned char blank_b
= 0;
2330 blank_r
= GetMaskRed();
2331 blank_g
= GetMaskGreen();
2332 blank_b
= GetMaskBlue();
2333 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2336 // Now, for each point of the rotated image, find where it came from, by
2337 // performing an inverse rotation (a rotation of -angle) and getting the
2338 // pixel at those coordinates
2340 // GRG: I've taken the (interpolating) test out of the loops, so that
2341 // it is done only once, instead of repeating it for each pixel.
2346 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2348 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2350 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2352 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2353 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2355 // interpolate using the 4 enclosing grid-points. Those
2356 // points can be obtained using floor and ceiling of the
2357 // exact coordinates of the point
2360 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2362 x1
= wxCint(floor(src
.x
));
2363 x2
= wxCint(ceil(src
.x
));
2365 else // else means that x is near one of the borders (0 or width-1)
2367 x1
= x2
= wxCint (src
.x
);
2370 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2372 y1
= wxCint(floor(src
.y
));
2373 y2
= wxCint(ceil(src
.y
));
2377 y1
= y2
= wxCint (src
.y
);
2380 // get four points and the distances (square of the distance,
2381 // for efficiency reasons) for the interpolation formula
2383 // GRG: Do not calculate the points until they are
2384 // really needed -- this way we can calculate
2385 // just one, instead of four, if d1, d2, d3
2386 // or d4 are < gs_Epsilon
2388 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2389 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2390 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2391 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2393 // Now interpolate as a weighted average of the four surrounding
2394 // points, where the weights are the distances to each of those points
2396 // If the point is exactly at one point of the grid of the source
2397 // image, then don't interpolate -- just assign the pixel
2399 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2401 unsigned char *p
= data
[y1
] + (3 * x1
);
2407 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2409 else if (d2
< gs_Epsilon
)
2411 unsigned char *p
= data
[y1
] + (3 * x2
);
2417 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2419 else if (d3
< gs_Epsilon
)
2421 unsigned char *p
= data
[y2
] + (3 * x2
);
2427 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2429 else if (d4
< gs_Epsilon
)
2431 unsigned char *p
= data
[y2
] + (3 * x1
);
2437 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2441 // weights for the weighted average are proportional to the inverse of the distance
2442 unsigned char *v1
= data
[y1
] + (3 * x1
);
2443 unsigned char *v2
= data
[y1
] + (3 * x2
);
2444 unsigned char *v3
= data
[y2
] + (3 * x2
);
2445 unsigned char *v4
= data
[y2
] + (3 * x1
);
2447 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2451 *(dst
++) = (unsigned char)
2452 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2453 w3
* *(v3
++) + w4
* *(v4
++)) /
2454 (w1
+ w2
+ w3
+ w4
) );
2455 *(dst
++) = (unsigned char)
2456 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2457 w3
* *(v3
++) + w4
* *(v4
++)) /
2458 (w1
+ w2
+ w3
+ w4
) );
2459 *(dst
++) = (unsigned char)
2460 ( (w1
* *v1
+ w2
* *v2
+
2461 w3
* *v3
+ w4
* *v4
) /
2462 (w1
+ w2
+ w3
+ w4
) );
2466 v1
= alpha
[y1
] + (x1
);
2467 v2
= alpha
[y1
] + (x2
);
2468 v3
= alpha
[y2
] + (x2
);
2469 v4
= alpha
[y2
] + (x1
);
2471 *(alpha_dst
++) = (unsigned char)
2472 ( (w1
* *v1
+ w2
* *v2
+
2473 w3
* *v3
+ w4
* *v4
) /
2474 (w1
+ w2
+ w3
+ w4
) );
2490 else // not interpolating
2492 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2494 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2496 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2498 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2499 const int ys
= wxCint (src
.y
); // closest integer
2501 if (0 <= xs
&& xs
< GetWidth() &&
2502 0 <= ys
&& ys
< GetHeight())
2504 unsigned char *p
= data
[ys
] + (3 * xs
);
2510 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2519 *(alpha_dst
++) = 255;
2537 // A module to allow wxImage initialization/cleanup
2538 // without calling these functions from app.cpp or from
2539 // the user's application.
2541 class wxImageModule
: public wxModule
2543 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2546 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2547 void OnExit() { wxImage::CleanUpHandlers(); };
2550 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2553 #endif // wxUSE_IMAGE