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/module.h"
27 #include "wx/palette.h"
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
35 #include "wx/xpmdecod.h"
41 // make the code compile with either wxFile*Stream or wxFFile*Stream:
42 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
46 typedef wxFileInputStream wxImageFileInputStream
;
47 typedef wxFileOutputStream wxImageFileOutputStream
;
49 typedef wxFFileInputStream wxImageFileInputStream
;
50 typedef wxFFileOutputStream wxImageFileOutputStream
;
51 #endif // wxUSE_FILE/wxUSE_FFILE
52 #endif // HAS_FILE_STREAMS
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 class wxImageRefData
: public wxObjectRefData
62 virtual ~wxImageRefData();
66 unsigned char *m_data
;
69 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
71 // alpha channel data, may be NULL for the formats without alpha support
72 unsigned char *m_alpha
;
76 // if true, m_data is pointer to static data and shouldn't be freed
79 // same as m_static but for m_alpha
84 #endif // wxUSE_PALETTE
86 wxArrayString m_optionNames
;
87 wxArrayString m_optionValues
;
89 DECLARE_NO_COPY_CLASS(wxImageRefData
)
92 wxImageRefData::wxImageRefData()
97 m_alpha
= (unsigned char *) NULL
;
106 m_staticAlpha
= false;
109 wxImageRefData::~wxImageRefData()
113 if ( !m_staticAlpha
)
117 wxList
wxImage::sm_handlers
;
121 //-----------------------------------------------------------------------------
123 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
125 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
127 wxImage::wxImage( int width
, int height
, bool clear
)
129 Create( width
, height
, clear
);
132 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
134 Create( width
, height
, data
, static_data
);
137 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
139 Create( width
, height
, data
, alpha
, static_data
);
142 wxImage::wxImage( const wxString
& name
, long type
, int index
)
144 LoadFile( name
, type
, index
);
147 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
149 LoadFile( name
, mimetype
, index
);
153 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
155 LoadFile( stream
, type
, index
);
158 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
160 LoadFile( stream
, mimetype
, index
);
162 #endif // wxUSE_STREAMS
164 wxImage::wxImage( const char** xpmData
)
169 wxImage::wxImage( char** xpmData
)
171 Create((const char**) xpmData
);
174 bool wxImage::Create( const char** xpmData
)
179 wxXPMDecoder decoder
;
180 (*this) = decoder
.ReadData(xpmData
);
187 bool wxImage::Create( int width
, int height
, bool clear
)
191 m_refData
= new wxImageRefData();
193 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
194 if (!M_IMGDATA
->m_data
)
201 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
203 M_IMGDATA
->m_width
= width
;
204 M_IMGDATA
->m_height
= height
;
205 M_IMGDATA
->m_ok
= true;
210 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
214 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
216 m_refData
= new wxImageRefData();
218 M_IMGDATA
->m_data
= data
;
219 M_IMGDATA
->m_width
= width
;
220 M_IMGDATA
->m_height
= height
;
221 M_IMGDATA
->m_ok
= true;
222 M_IMGDATA
->m_static
= static_data
;
227 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
231 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
233 m_refData
= new wxImageRefData();
235 M_IMGDATA
->m_data
= data
;
236 M_IMGDATA
->m_alpha
= alpha
;
237 M_IMGDATA
->m_width
= width
;
238 M_IMGDATA
->m_height
= height
;
239 M_IMGDATA
->m_ok
= true;
240 M_IMGDATA
->m_static
= static_data
;
245 void wxImage::Destroy()
250 wxObjectRefData
* wxImage::CreateRefData() const
252 return new wxImageRefData
;
255 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
257 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
258 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
260 wxImageRefData
* refData_new
= new wxImageRefData
;
261 refData_new
->m_width
= refData
->m_width
;
262 refData_new
->m_height
= refData
->m_height
;
263 refData_new
->m_maskRed
= refData
->m_maskRed
;
264 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
265 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
266 refData_new
->m_hasMask
= refData
->m_hasMask
;
267 refData_new
->m_ok
= true;
268 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
269 if (refData
->m_alpha
!= NULL
)
271 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
272 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
275 refData_new
->m_data
= (unsigned char*)malloc(size
);
276 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
278 refData_new
->m_palette
= refData
->m_palette
;
280 refData_new
->m_optionNames
= refData
->m_optionNames
;
281 refData_new
->m_optionValues
= refData
->m_optionValues
;
285 wxImage
wxImage::Copy() const
289 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
291 image
.m_refData
= CloneRefData(m_refData
);
296 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
298 if( xFactor
== 1 && yFactor
== 1 )
303 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
305 // can't scale to/from 0 size
306 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
307 wxT("invalid new image size") );
309 long old_height
= M_IMGDATA
->m_height
,
310 old_width
= M_IMGDATA
->m_width
;
312 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
313 wxT("invalid old image size") );
315 long width
= old_width
/ xFactor
;
316 long height
= old_height
/ yFactor
;
318 image
.Create( width
, height
, false );
320 char unsigned *data
= image
.GetData();
322 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
324 bool hasMask
= false ;
325 unsigned char maskRed
= 0;
326 unsigned char maskGreen
= 0;
327 unsigned char maskBlue
=0 ;
329 unsigned char *source_data
= M_IMGDATA
->m_data
;
330 unsigned char *target_data
= data
;
331 unsigned char *source_alpha
= 0 ;
332 unsigned char *target_alpha
= 0 ;
333 if (M_IMGDATA
->m_hasMask
)
336 maskRed
= M_IMGDATA
->m_maskRed
;
337 maskGreen
= M_IMGDATA
->m_maskGreen
;
338 maskBlue
=M_IMGDATA
->m_maskBlue
;
340 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
341 M_IMGDATA
->m_maskGreen
,
342 M_IMGDATA
->m_maskBlue
);
346 source_alpha
= M_IMGDATA
->m_alpha
;
350 target_alpha
= image
.GetAlpha() ;
354 for (long y
= 0; y
< height
; y
++)
356 for (long x
= 0; x
< width
; x
++)
358 unsigned long avgRed
= 0 ;
359 unsigned long avgGreen
= 0;
360 unsigned long avgBlue
= 0;
361 unsigned long avgAlpha
= 0 ;
362 unsigned long counter
= 0 ;
364 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
366 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
367 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
369 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
370 unsigned char red
= pixel
[0] ;
371 unsigned char green
= pixel
[1] ;
372 unsigned char blue
= pixel
[2] ;
373 unsigned char alpha
= 255 ;
375 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
376 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
391 *(target_data
++) = M_IMGDATA
->m_maskRed
;
392 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
393 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
398 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
399 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
400 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
401 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
406 // In case this is a cursor, make sure the hotspot is scaled accordingly:
407 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
408 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
409 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
410 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
411 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
412 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
417 wxImage
wxImage::Scale( int width
, int height
) const
421 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
423 // can't scale to/from 0 size
424 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
425 wxT("invalid new image size") );
427 long old_height
= M_IMGDATA
->m_height
,
428 old_width
= M_IMGDATA
->m_width
;
429 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
430 wxT("invalid old image size") );
432 if ( old_width
% width
== 0 && old_width
>= width
&&
433 old_height
% height
== 0 && old_height
>= height
)
435 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
437 image
.Create( width
, height
, false );
439 unsigned char *data
= image
.GetData();
441 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
443 unsigned char *source_data
= M_IMGDATA
->m_data
;
444 unsigned char *target_data
= data
;
445 unsigned char *source_alpha
= 0 ;
446 unsigned char *target_alpha
= 0 ;
448 if (M_IMGDATA
->m_hasMask
)
450 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
451 M_IMGDATA
->m_maskGreen
,
452 M_IMGDATA
->m_maskBlue
);
456 source_alpha
= M_IMGDATA
->m_alpha
;
460 target_alpha
= image
.GetAlpha() ;
464 long x_delta
= (old_width
<<16) / width
;
465 long y_delta
= (old_height
<<16) / height
;
467 unsigned char* dest_pixel
= target_data
;
470 for ( long j
= 0; j
< height
; j
++ )
472 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
473 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
476 for ( long i
= 0; i
< width
; i
++ )
478 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
479 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
480 dest_pixel
[0] = src_pixel
[0];
481 dest_pixel
[1] = src_pixel
[1];
482 dest_pixel
[2] = src_pixel
[2];
485 *(target_alpha
++) = *src_alpha_pixel
;
492 // In case this is a cursor, make sure the hotspot is scaled accordingly:
493 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
494 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
495 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
496 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
497 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
498 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
503 wxImage
wxImage::Rotate90( bool clockwise
) const
507 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
509 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
511 unsigned char *data
= image
.GetData();
513 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
515 unsigned char *source_data
= M_IMGDATA
->m_data
;
516 unsigned char *target_data
;
517 unsigned char *alpha_data
= 0 ;
518 unsigned char *source_alpha
= 0 ;
519 unsigned char *target_alpha
= 0 ;
521 if (M_IMGDATA
->m_hasMask
)
523 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
527 source_alpha
= M_IMGDATA
->m_alpha
;
531 alpha_data
= image
.GetAlpha() ;
535 long height
= M_IMGDATA
->m_height
;
536 long width
= M_IMGDATA
->m_width
;
538 for (long j
= 0; j
< height
; j
++)
540 for (long i
= 0; i
< width
; i
++)
544 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
546 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
550 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
552 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
554 memcpy( target_data
, source_data
, 3 );
559 memcpy( target_alpha
, source_alpha
, 1 );
568 wxImage
wxImage::Mirror( bool horizontally
) const
572 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
574 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
576 unsigned char *data
= image
.GetData();
577 unsigned char *alpha
= NULL
;
579 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
581 if (M_IMGDATA
->m_alpha
!= NULL
) {
583 alpha
= image
.GetAlpha();
584 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
587 if (M_IMGDATA
->m_hasMask
)
588 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
590 long height
= M_IMGDATA
->m_height
;
591 long width
= M_IMGDATA
->m_width
;
593 unsigned char *source_data
= M_IMGDATA
->m_data
;
594 unsigned char *target_data
;
598 for (long j
= 0; j
< height
; j
++)
601 target_data
= data
-3;
602 for (long i
= 0; i
< width
; i
++)
604 memcpy( target_data
, source_data
, 3 );
612 // src_alpha starts at the first pixel and increases by 1 after each step
613 // (a step here is the copy of the alpha value of one pixel)
614 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
615 // dest_alpha starts just beyond the first line, decreases before each step,
616 // and after each line is finished, increases by 2 widths (skipping the line
617 // just copied and the line that will be copied next)
618 unsigned char *dest_alpha
= alpha
+ width
;
620 for (long jj
= 0; jj
< height
; ++jj
)
622 for (long i
= 0; i
< width
; ++i
) {
623 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
625 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
631 for (long i
= 0; i
< height
; i
++)
633 target_data
= data
+ 3*width
*(height
-1-i
);
634 memcpy( target_data
, source_data
, (size_t)3*width
);
635 source_data
+= 3*width
;
640 // src_alpha starts at the first pixel and increases by 1 width after each step
641 // (a step here is the copy of the alpha channel of an entire line)
642 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
643 // dest_alpha starts just beyond the last line (beyond the whole image)
644 // and decreases by 1 width before each step
645 unsigned char *dest_alpha
= alpha
+ width
* height
;
647 for (long jj
= 0; jj
< height
; ++jj
)
650 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
659 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
663 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
665 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
666 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
667 image
, wxT("invalid subimage size") );
669 const int subwidth
= rect
.GetWidth();
670 const int subheight
= rect
.GetHeight();
672 image
.Create( subwidth
, subheight
, false );
674 const unsigned char *src_data
= GetData();
675 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
676 unsigned char *subdata
= image
.GetData();
677 unsigned char *subalpha
= NULL
;
679 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
681 if (src_alpha
!= NULL
) {
683 subalpha
= image
.GetAlpha();
684 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
687 if (M_IMGDATA
->m_hasMask
)
688 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
690 const int width
= GetWidth();
691 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
693 src_data
+= 3 * pixsoff
;
694 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
696 for (long j
= 0; j
< subheight
; ++j
)
698 memcpy( subdata
, src_data
, 3 * subwidth
);
699 subdata
+= 3 * subwidth
;
700 src_data
+= 3 * width
;
701 if (subalpha
!= NULL
) {
702 memcpy( subalpha
, src_alpha
, subwidth
);
703 subalpha
+= subwidth
;
711 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
712 int r_
, int g_
, int b_
) const
716 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
717 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
719 int width
= GetWidth(), height
= GetHeight();
720 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
722 unsigned char r
= (unsigned char)r_
;
723 unsigned char g
= (unsigned char)g_
;
724 unsigned char b
= (unsigned char)b_
;
725 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
727 GetOrFindMaskColour( &r
, &g
, &b
);
728 image
.SetMaskColour(r
, g
, b
);
731 image
.SetRGB(wxRect(), r
, g
, b
);
733 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
734 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
736 finalRect
.width
-= pos
.x
;
738 finalRect
.height
-= pos
.y
;
740 subRect
.Intersect(finalRect
);
742 if (!subRect
.IsEmpty())
744 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
745 image
.Paste(*this, pos
.x
, pos
.y
);
747 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
753 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
755 wxCHECK_RET( Ok(), wxT("invalid image") );
756 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
762 int width
= image
.GetWidth();
763 int height
= image
.GetHeight();
776 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
777 width
= M_IMGDATA
->m_width
- (x
+xx
);
778 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
779 height
= M_IMGDATA
->m_height
- (y
+yy
);
781 if (width
< 1) return;
782 if (height
< 1) return;
784 if ((!HasMask() && !image
.HasMask()) ||
785 (HasMask() && !image
.HasMask()) ||
786 ((HasMask() && image
.HasMask() &&
787 (GetMaskRed()==image
.GetMaskRed()) &&
788 (GetMaskGreen()==image
.GetMaskGreen()) &&
789 (GetMaskBlue()==image
.GetMaskBlue()))))
792 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
793 int source_step
= image
.GetWidth()*3;
795 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
796 int target_step
= M_IMGDATA
->m_width
*3;
797 for (int j
= 0; j
< height
; j
++)
799 memcpy( target_data
, source_data
, width
);
800 source_data
+= source_step
;
801 target_data
+= target_step
;
806 if (!HasMask() && image
.HasMask())
808 unsigned char r
= image
.GetMaskRed();
809 unsigned char g
= image
.GetMaskGreen();
810 unsigned char b
= image
.GetMaskBlue();
813 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
814 int source_step
= image
.GetWidth()*3;
816 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
817 int target_step
= M_IMGDATA
->m_width
*3;
819 for (int j
= 0; j
< height
; j
++)
821 for (int i
= 0; i
< width
; i
+=3)
823 if ((source_data
[i
] != r
) &&
824 (source_data
[i
+1] != g
) &&
825 (source_data
[i
+2] != b
))
827 memcpy( target_data
+i
, source_data
+i
, 3 );
830 source_data
+= source_step
;
831 target_data
+= target_step
;
836 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
837 unsigned char r2
, unsigned char g2
, unsigned char b2
)
839 wxCHECK_RET( Ok(), wxT("invalid image") );
843 unsigned char *data
= GetData();
845 const int w
= GetWidth();
846 const int h
= GetHeight();
848 for (int j
= 0; j
< h
; j
++)
849 for (int i
= 0; i
< w
; i
++)
851 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
861 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
865 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
867 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
869 unsigned char *dest
= image
.GetData();
871 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
873 unsigned char *src
= M_IMGDATA
->m_data
;
874 bool hasMask
= M_IMGDATA
->m_hasMask
;
875 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
876 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
877 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
880 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
882 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
883 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
885 // don't modify the mask
886 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
888 memcpy(dest
, src
, 3);
892 // calculate the luma
893 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
894 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
898 // copy the alpha channel, if any
901 const size_t alphaSize
= GetWidth() * GetHeight();
902 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
903 memcpy(alpha
, GetAlpha(), alphaSize
);
905 image
.SetAlpha(alpha
);
911 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
915 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
917 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
919 unsigned char *data
= image
.GetData();
921 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
923 if (M_IMGDATA
->m_hasMask
)
925 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
926 M_IMGDATA
->m_maskBlue
== b
)
927 image
.SetMaskColour( 255, 255, 255 );
929 image
.SetMaskColour( 0, 0, 0 );
932 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
934 unsigned char *srcd
= M_IMGDATA
->m_data
;
935 unsigned char *tard
= image
.GetData();
937 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
939 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
940 tard
[0] = tard
[1] = tard
[2] = 255;
942 tard
[0] = tard
[1] = tard
[2] = 0;
948 int wxImage::GetWidth() const
950 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
952 return M_IMGDATA
->m_width
;
955 int wxImage::GetHeight() const
957 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
959 return M_IMGDATA
->m_height
;
962 long wxImage::XYToIndex(int x
, int y
) const
966 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
968 return y
*M_IMGDATA
->m_width
+ x
;
974 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
976 long pos
= XYToIndex(x
, y
);
977 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
983 M_IMGDATA
->m_data
[ pos
] = r
;
984 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
985 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
988 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
990 wxCHECK_RET( Ok(), wxT("invalid image") );
995 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
996 if ( rect
== wxRect() )
1002 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
1003 imageRect
.Inside(rect
.GetBottomRight()),
1004 wxT("invalid bounding rectangle") );
1007 int x1
= rect
.GetLeft(),
1009 x2
= rect
.GetRight() + 1,
1010 y2
= rect
.GetBottom() + 1;
1012 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1013 int x
, y
, width
= GetWidth();
1014 for (y
= y1
; y
< y2
; y
++)
1016 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1017 for (x
= x1
; x
< x2
; x
++)
1026 unsigned char wxImage::GetRed( int x
, int y
) const
1028 long pos
= XYToIndex(x
, y
);
1029 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1033 return M_IMGDATA
->m_data
[pos
];
1036 unsigned char wxImage::GetGreen( int x
, int y
) const
1038 long pos
= XYToIndex(x
, y
);
1039 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1043 return M_IMGDATA
->m_data
[pos
+1];
1046 unsigned char wxImage::GetBlue( int x
, int y
) const
1048 long pos
= XYToIndex(x
, y
);
1049 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1053 return M_IMGDATA
->m_data
[pos
+2];
1056 bool wxImage::Ok() const
1058 // image of 0 width or height can't be considered ok - at least because it
1059 // causes crashes in ConvertToBitmap() if we don't catch it in time
1060 wxImageRefData
*data
= M_IMGDATA
;
1061 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1064 unsigned char *wxImage::GetData() const
1066 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1068 return M_IMGDATA
->m_data
;
1071 void wxImage::SetData( unsigned char *data
, bool static_data
)
1073 wxCHECK_RET( Ok(), wxT("invalid image") );
1075 wxImageRefData
*newRefData
= new wxImageRefData();
1077 newRefData
->m_width
= M_IMGDATA
->m_width
;
1078 newRefData
->m_height
= M_IMGDATA
->m_height
;
1079 newRefData
->m_data
= data
;
1080 newRefData
->m_ok
= true;
1081 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1082 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1083 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1084 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1085 newRefData
->m_static
= static_data
;
1089 m_refData
= newRefData
;
1092 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1094 wxImageRefData
*newRefData
= new wxImageRefData();
1098 newRefData
->m_width
= new_width
;
1099 newRefData
->m_height
= new_height
;
1100 newRefData
->m_data
= data
;
1101 newRefData
->m_ok
= true;
1102 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1103 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1104 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1105 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1109 newRefData
->m_width
= new_width
;
1110 newRefData
->m_height
= new_height
;
1111 newRefData
->m_data
= data
;
1112 newRefData
->m_ok
= true;
1114 newRefData
->m_static
= static_data
;
1118 m_refData
= newRefData
;
1121 // ----------------------------------------------------------------------------
1122 // alpha channel support
1123 // ----------------------------------------------------------------------------
1125 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1127 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1129 long pos
= XYToIndex(x
, y
);
1130 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1134 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1137 unsigned char wxImage::GetAlpha(int x
, int y
) const
1139 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1141 long pos
= XYToIndex(x
, y
);
1142 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1144 return M_IMGDATA
->m_alpha
[pos
];
1148 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1152 const int w
= M_IMGDATA
->m_width
;
1153 const int h
= M_IMGDATA
->m_height
;
1155 unsigned char *alpha
= GetAlpha();
1156 unsigned char *data
= GetData();
1158 for ( int y
= 0; y
< h
; y
++ )
1160 for ( int x
= 0; x
< w
; x
++ )
1172 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1174 wxCHECK_RET( Ok(), wxT("invalid image") );
1180 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1183 free(M_IMGDATA
->m_alpha
);
1184 M_IMGDATA
->m_alpha
= alpha
;
1185 M_IMGDATA
->m_staticAlpha
= static_data
;
1188 unsigned char *wxImage::GetAlpha() const
1190 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1192 return M_IMGDATA
->m_alpha
;
1195 void wxImage::InitAlpha()
1197 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1199 // initialize memory for alpha channel
1202 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1203 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1207 // use the mask to initialize the alpha channel.
1208 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1210 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1211 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1212 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1213 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1217 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1218 ? wxIMAGE_ALPHA_TRANSPARENT
1219 : wxIMAGE_ALPHA_OPAQUE
;
1222 M_IMGDATA
->m_hasMask
= false;
1226 // make the image fully opaque
1227 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1231 // ----------------------------------------------------------------------------
1233 // ----------------------------------------------------------------------------
1235 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1237 wxCHECK_RET( Ok(), wxT("invalid image") );
1241 M_IMGDATA
->m_maskRed
= r
;
1242 M_IMGDATA
->m_maskGreen
= g
;
1243 M_IMGDATA
->m_maskBlue
= b
;
1244 M_IMGDATA
->m_hasMask
= true;
1247 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1249 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1251 if (M_IMGDATA
->m_hasMask
)
1253 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1254 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1255 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1260 FindFirstUnusedColour(r
, g
, b
);
1265 unsigned char wxImage::GetMaskRed() const
1267 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1269 return M_IMGDATA
->m_maskRed
;
1272 unsigned char wxImage::GetMaskGreen() const
1274 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1276 return M_IMGDATA
->m_maskGreen
;
1279 unsigned char wxImage::GetMaskBlue() const
1281 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1283 return M_IMGDATA
->m_maskBlue
;
1286 void wxImage::SetMask( bool mask
)
1288 wxCHECK_RET( Ok(), wxT("invalid image") );
1292 M_IMGDATA
->m_hasMask
= mask
;
1295 bool wxImage::HasMask() const
1297 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1299 return M_IMGDATA
->m_hasMask
;
1302 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1304 long pos
= XYToIndex(x
, y
);
1305 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1308 if ( M_IMGDATA
->m_hasMask
)
1310 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1311 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1312 p
[1] == M_IMGDATA
->m_maskGreen
&&
1313 p
[2] == M_IMGDATA
->m_maskBlue
)
1320 if ( M_IMGDATA
->m_alpha
)
1322 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1324 // transparent enough
1333 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1334 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1336 // check that the images are the same size
1337 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1339 wxLogError( _("Image and mask have different sizes.") );
1343 // find unused colour
1344 unsigned char r
,g
,b
;
1345 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1347 wxLogError( _("No unused colour in image being masked.") );
1353 unsigned char *imgdata
= GetData();
1354 unsigned char *maskdata
= mask
.GetData();
1356 const int w
= GetWidth();
1357 const int h
= GetHeight();
1359 for (int j
= 0; j
< h
; j
++)
1361 for (int i
= 0; i
< w
; i
++)
1363 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1374 SetMaskColour(r
, g
, b
);
1380 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1385 unsigned char mr
, mg
, mb
;
1386 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1388 wxLogError( _("No unused colour in image being masked.") );
1395 SetMaskColour(mr
, mg
, mb
);
1397 unsigned char *imgdata
= GetData();
1398 unsigned char *alphadata
= GetAlpha();
1401 int h
= GetHeight();
1403 for (int y
= 0; y
< h
; y
++)
1405 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1407 if (*alphadata
< threshold
)
1416 free(M_IMGDATA
->m_alpha
);
1417 M_IMGDATA
->m_alpha
= NULL
;
1422 // ----------------------------------------------------------------------------
1423 // Palette functions
1424 // ----------------------------------------------------------------------------
1428 bool wxImage::HasPalette() const
1433 return M_IMGDATA
->m_palette
.Ok();
1436 const wxPalette
& wxImage::GetPalette() const
1438 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1440 return M_IMGDATA
->m_palette
;
1443 void wxImage::SetPalette(const wxPalette
& palette
)
1445 wxCHECK_RET( Ok(), wxT("invalid image") );
1449 M_IMGDATA
->m_palette
= palette
;
1452 #endif // wxUSE_PALETTE
1454 // ----------------------------------------------------------------------------
1455 // Option functions (arbitrary name/value mapping)
1456 // ----------------------------------------------------------------------------
1458 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1460 wxCHECK_RET( Ok(), wxT("invalid image") );
1464 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1465 if (idx
== wxNOT_FOUND
)
1467 M_IMGDATA
->m_optionNames
.Add(name
);
1468 M_IMGDATA
->m_optionValues
.Add(value
);
1472 M_IMGDATA
->m_optionNames
[idx
] = name
;
1473 M_IMGDATA
->m_optionValues
[idx
] = value
;
1477 void wxImage::SetOption(const wxString
& name
, int value
)
1480 valStr
.Printf(wxT("%d"), value
);
1481 SetOption(name
, valStr
);
1484 wxString
wxImage::GetOption(const wxString
& name
) const
1486 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1488 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1489 if (idx
== wxNOT_FOUND
)
1490 return wxEmptyString
;
1492 return M_IMGDATA
->m_optionValues
[idx
];
1495 int wxImage::GetOptionInt(const wxString
& name
) const
1497 return wxAtoi(GetOption(name
));
1500 bool wxImage::HasOption(const wxString
& name
) const
1502 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1504 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1507 // ----------------------------------------------------------------------------
1509 // ----------------------------------------------------------------------------
1511 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1512 long WXUNUSED_UNLESS_STREAMS(type
),
1513 int WXUNUSED_UNLESS_STREAMS(index
) )
1515 #if HAS_FILE_STREAMS
1516 if (wxFileExists(filename
))
1518 wxImageFileInputStream
stream(filename
);
1519 wxBufferedInputStream
bstream( stream
);
1520 return LoadFile(bstream
, type
, index
);
1524 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1528 #else // !HAS_FILE_STREAMS
1530 #endif // HAS_FILE_STREAMS
1533 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1534 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
1535 int WXUNUSED_UNLESS_STREAMS(index
) )
1537 #if HAS_FILE_STREAMS
1538 if (wxFileExists(filename
))
1540 wxImageFileInputStream
stream(filename
);
1541 wxBufferedInputStream
bstream( stream
);
1542 return LoadFile(bstream
, mimetype
, index
);
1546 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1550 #else // !HAS_FILE_STREAMS
1552 #endif // HAS_FILE_STREAMS
1557 bool wxImage::SaveFile( const wxString
& filename
) const
1559 wxString ext
= filename
.AfterLast('.').Lower();
1561 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1564 SaveFile(filename
, pHandler
->GetType());
1568 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1573 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1574 int WXUNUSED_UNLESS_STREAMS(type
) ) const
1576 #if HAS_FILE_STREAMS
1577 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1579 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1581 wxImageFileOutputStream
stream(filename
);
1583 if ( stream
.IsOk() )
1585 wxBufferedOutputStream
bstream( stream
);
1586 return SaveFile(bstream
, type
);
1588 #endif // HAS_FILE_STREAMS
1593 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1594 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
1596 #if HAS_FILE_STREAMS
1597 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1599 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1601 wxImageFileOutputStream
stream(filename
);
1603 if ( stream
.IsOk() )
1605 wxBufferedOutputStream
bstream( stream
);
1606 return SaveFile(bstream
, mimetype
);
1608 #endif // HAS_FILE_STREAMS
1613 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
1615 #if HAS_FILE_STREAMS
1616 wxImageFileInputStream
stream(name
);
1617 return CanRead(stream
);
1623 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
1624 long WXUNUSED_UNLESS_STREAMS(type
) )
1626 #if HAS_FILE_STREAMS
1627 wxImageFileInputStream
stream(name
);
1629 return GetImageCount(stream
, type
);
1637 bool wxImage::CanRead( wxInputStream
&stream
)
1639 const wxList
& list
= GetHandlers();
1641 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1643 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1644 if (handler
->CanRead( stream
))
1651 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1653 wxImageHandler
*handler
;
1655 if ( type
== wxBITMAP_TYPE_ANY
)
1657 wxList
&list
=GetHandlers();
1659 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1661 handler
=(wxImageHandler
*)node
->GetData();
1662 if ( handler
->CanRead(stream
) )
1663 return handler
->GetImageCount(stream
);
1667 wxLogWarning(_("No handler found for image type."));
1671 handler
= FindHandler(type
);
1675 wxLogWarning(_("No image handler for type %ld defined."), type
);
1679 if ( handler
->CanRead(stream
) )
1681 return handler
->GetImageCount(stream
);
1685 wxLogError(_("Image file is not of type %ld."), type
);
1690 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1694 m_refData
= new wxImageRefData
;
1696 wxImageHandler
*handler
;
1698 if ( type
== wxBITMAP_TYPE_ANY
)
1700 wxList
&list
=GetHandlers();
1702 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1704 handler
=(wxImageHandler
*)node
->GetData();
1705 if ( handler
->CanRead(stream
) )
1706 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1710 wxLogWarning( _("No handler found for image type.") );
1714 handler
= FindHandler(type
);
1718 wxLogWarning( _("No image handler for type %ld defined."), type
);
1723 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1725 wxLogError(_("Image file is not of type %ld."), type
);
1729 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1732 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1736 m_refData
= new wxImageRefData
;
1738 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1742 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1747 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1749 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1753 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1756 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1758 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1760 wxImageHandler
*handler
= FindHandler(type
);
1763 wxLogWarning( _("No image handler for type %d defined."), type
);
1768 return handler
->SaveFile( (wxImage
*)this, stream
);
1771 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1773 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1775 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1778 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1783 return handler
->SaveFile( (wxImage
*)this, stream
);
1785 #endif // wxUSE_STREAMS
1787 // ----------------------------------------------------------------------------
1788 // image I/O handlers
1789 // ----------------------------------------------------------------------------
1791 void wxImage::AddHandler( wxImageHandler
*handler
)
1793 // Check for an existing handler of the type being added.
1794 if (FindHandler( handler
->GetType() ) == 0)
1796 sm_handlers
.Append( handler
);
1800 // This is not documented behaviour, merely the simplest 'fix'
1801 // for preventing duplicate additions. If someone ever has
1802 // a good reason to add and remove duplicate handlers (and they
1803 // may) we should probably refcount the duplicates.
1804 // also an issue in InsertHandler below.
1806 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1807 handler
->GetName().c_str() );
1812 void wxImage::InsertHandler( wxImageHandler
*handler
)
1814 // Check for an existing handler of the type being added.
1815 if (FindHandler( handler
->GetType() ) == 0)
1817 sm_handlers
.Insert( handler
);
1821 // see AddHandler for additional comments.
1822 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1823 handler
->GetName().c_str() );
1828 bool wxImage::RemoveHandler( const wxString
& name
)
1830 wxImageHandler
*handler
= FindHandler(name
);
1833 sm_handlers
.DeleteObject(handler
);
1841 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1843 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1846 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1847 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1849 node
= node
->GetNext();
1854 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1856 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1859 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1860 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1861 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1863 node
= node
->GetNext();
1868 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1870 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1873 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1874 if (handler
->GetType() == bitmapType
) return handler
;
1875 node
= node
->GetNext();
1880 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1882 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1885 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1886 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1887 node
= node
->GetNext();
1892 void wxImage::InitStandardHandlers()
1895 AddHandler(new wxBMPHandler
);
1896 #endif // wxUSE_STREAMS
1899 void wxImage::CleanUpHandlers()
1901 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1904 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1905 wxList::compatibility_iterator next
= node
->GetNext();
1910 sm_handlers
.Clear();
1913 wxString
wxImage::GetImageExtWildcard()
1917 wxList
& Handlers
= wxImage::GetHandlers();
1918 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1921 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1922 fmts
+= wxT("*.") + Handler
->GetExtension();
1923 Node
= Node
->GetNext();
1924 if ( Node
) fmts
+= wxT(";");
1927 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1930 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1932 const double red
= rgb
.red
/ 255.0,
1933 green
= rgb
.green
/ 255.0,
1934 blue
= rgb
.blue
/ 255.0;
1936 // find the min and max intensity (and remember which one was it for the
1938 double minimumRGB
= red
;
1939 if ( green
< minimumRGB
)
1941 if ( blue
< minimumRGB
)
1944 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1945 double maximumRGB
= red
;
1946 if ( green
> maximumRGB
)
1951 if ( blue
> maximumRGB
)
1957 const double value
= maximumRGB
;
1959 double hue
= 0.0, saturation
;
1960 const double deltaRGB
= maximumRGB
- minimumRGB
;
1961 if ( wxIsNullDouble(deltaRGB
) )
1963 // Gray has no color
1972 hue
= (green
- blue
) / deltaRGB
;
1976 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1980 hue
= 4.0 + (red
- green
) / deltaRGB
;
1984 wxFAIL_MSG(wxT("hue not specified"));
1993 saturation
= deltaRGB
/ maximumRGB
;
1996 return HSVValue(hue
, saturation
, value
);
1999 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2001 double red
, green
, blue
;
2003 if ( wxIsNullDouble(hsv
.saturation
) )
2012 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2013 int i
= (int)floor(hue
);
2014 double f
= hue
- i
; // fractional part of h
2015 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2021 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2026 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2034 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2039 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2044 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2052 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2057 return RGBValue((unsigned char)(red
* 255.0),
2058 (unsigned char)(green
* 255.0),
2059 (unsigned char)(blue
* 255.0));
2063 * Rotates the hue of each pixel of the image. angle is a double in the range
2064 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2066 void wxImage::RotateHue(double angle
)
2070 unsigned char *srcBytePtr
;
2071 unsigned char *dstBytePtr
;
2072 unsigned long count
;
2073 wxImage::HSVValue hsv
;
2074 wxImage::RGBValue rgb
;
2076 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2077 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2078 if ( count
> 0 && !wxIsNullDouble(angle
) )
2080 srcBytePtr
= M_IMGDATA
->m_data
;
2081 dstBytePtr
= srcBytePtr
;
2084 rgb
.red
= *srcBytePtr
++;
2085 rgb
.green
= *srcBytePtr
++;
2086 rgb
.blue
= *srcBytePtr
++;
2087 hsv
= RGBtoHSV(rgb
);
2089 hsv
.hue
= hsv
.hue
+ angle
;
2091 hsv
.hue
= hsv
.hue
- 1.0;
2092 else if (hsv
.hue
< 0.0)
2093 hsv
.hue
= hsv
.hue
+ 1.0;
2095 rgb
= HSVtoRGB(hsv
);
2096 *dstBytePtr
++ = rgb
.red
;
2097 *dstBytePtr
++ = rgb
.green
;
2098 *dstBytePtr
++ = rgb
.blue
;
2099 } while (--count
!= 0);
2103 //-----------------------------------------------------------------------------
2105 //-----------------------------------------------------------------------------
2107 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2110 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2115 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2120 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2125 bool wxImageHandler::CanRead( const wxString
& name
)
2127 if (wxFileExists(name
))
2129 wxImageFileInputStream
stream(name
);
2130 return CanRead(stream
);
2133 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2138 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2140 wxFileOffset posOld
= stream
.TellI();
2141 if ( posOld
== wxInvalidOffset
)
2143 // can't test unseekable stream
2147 bool ok
= DoCanRead(stream
);
2149 // restore the old position to be able to test other formats and so on
2150 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2152 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2154 // reading would fail anyhow as we're not at the right position
2161 #endif // wxUSE_STREAMS
2163 // ----------------------------------------------------------------------------
2164 // image histogram stuff
2165 // ----------------------------------------------------------------------------
2168 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2173 unsigned char g2
) const
2175 unsigned long key
= MakeKey(r2
, g2
, b2
);
2177 while ( find(key
) != end() )
2179 // color already used
2191 wxLogError(_("No unused colour in image.") );
2197 key
= MakeKey(r2
, g2
, b2
);
2211 wxImage::FindFirstUnusedColour(unsigned char *r
,
2216 unsigned char g2
) const
2218 wxImageHistogram histogram
;
2220 ComputeHistogram(histogram
);
2222 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2228 // Counts and returns the number of different colours. Optionally stops
2229 // when it exceeds 'stopafter' different colours. This is useful, for
2230 // example, to see if the image can be saved as 8-bit (256 colour or
2231 // less, in this case it would be invoked as CountColours(256)). Default
2232 // value for stopafter is -1 (don't care).
2234 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2238 unsigned char r
, g
, b
;
2240 unsigned long size
, nentries
, key
;
2243 size
= GetWidth() * GetHeight();
2246 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2251 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2253 if (h
.Get(key
) == NULL
)
2264 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2266 unsigned char *p
= GetData();
2267 unsigned long nentries
= 0;
2271 const unsigned long size
= GetWidth() * GetHeight();
2273 unsigned char r
, g
, b
;
2274 for ( unsigned long n
= 0; n
< size
; n
++ )
2280 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2282 if ( entry
.value
++ == 0 )
2283 entry
.index
= nentries
++;
2290 * Rotation code by Carlos Moreno
2293 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2294 // does exactly the same thing. And I also got rid of wxRotationPixel
2295 // bacause of potential problems in architectures where alignment
2296 // is an issue, so I had to rewrite parts of the code.
2298 static const double gs_Epsilon
= 1e-10;
2300 static inline int wxCint (double x
)
2302 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2306 // Auxiliary function to rotate a point (x,y) with respect to point p0
2307 // make it inline and use a straight return to facilitate optimization
2308 // also, the function receives the sine and cosine of the angle to avoid
2309 // repeating the time-consuming calls to these functions -- sin/cos can
2310 // be computed and stored in the calling function.
2312 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2314 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2315 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2318 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2320 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2323 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2326 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2328 bool has_alpha
= HasAlpha();
2330 // Create pointer-based array to accelerate access to wxImage's data
2331 unsigned char ** data
= new unsigned char * [GetHeight()];
2332 data
[0] = GetData();
2333 for (i
= 1; i
< GetHeight(); i
++)
2334 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2336 // Same for alpha channel
2337 unsigned char ** alpha
= NULL
;
2340 alpha
= new unsigned char * [GetHeight()];
2341 alpha
[0] = GetAlpha();
2342 for (i
= 1; i
< GetHeight(); i
++)
2343 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2346 // precompute coefficients for rotation formula
2347 // (sine and cosine of the angle)
2348 const double cos_angle
= cos(angle
);
2349 const double sin_angle
= sin(angle
);
2351 // Create new Image to store the result
2352 // First, find rectangle that covers the rotated image; to do that,
2353 // rotate the four corners
2355 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2357 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2358 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2359 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2360 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2362 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2363 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2364 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2365 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2367 // Create rotated image
2368 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2369 // With alpha channel
2373 if (offset_after_rotation
!= NULL
)
2375 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2378 // GRG: The rotated (destination) image is always accessed
2379 // sequentially, so there is no need for a pointer-based
2380 // array here (and in fact it would be slower).
2382 unsigned char * dst
= rotated
.GetData();
2384 unsigned char * alpha_dst
= NULL
;
2386 alpha_dst
= rotated
.GetAlpha();
2388 // GRG: if the original image has a mask, use its RGB values
2389 // as the blank pixel, else, fall back to default (black).
2391 unsigned char blank_r
= 0;
2392 unsigned char blank_g
= 0;
2393 unsigned char blank_b
= 0;
2397 blank_r
= GetMaskRed();
2398 blank_g
= GetMaskGreen();
2399 blank_b
= GetMaskBlue();
2400 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2403 // Now, for each point of the rotated image, find where it came from, by
2404 // performing an inverse rotation (a rotation of -angle) and getting the
2405 // pixel at those coordinates
2407 // GRG: I've taken the (interpolating) test out of the loops, so that
2408 // it is done only once, instead of repeating it for each pixel.
2413 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2415 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2417 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2419 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2420 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2422 // interpolate using the 4 enclosing grid-points. Those
2423 // points can be obtained using floor and ceiling of the
2424 // exact coordinates of the point
2427 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2429 x1
= wxCint(floor(src
.x
));
2430 x2
= wxCint(ceil(src
.x
));
2432 else // else means that x is near one of the borders (0 or width-1)
2434 x1
= x2
= wxCint (src
.x
);
2437 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2439 y1
= wxCint(floor(src
.y
));
2440 y2
= wxCint(ceil(src
.y
));
2444 y1
= y2
= wxCint (src
.y
);
2447 // get four points and the distances (square of the distance,
2448 // for efficiency reasons) for the interpolation formula
2450 // GRG: Do not calculate the points until they are
2451 // really needed -- this way we can calculate
2452 // just one, instead of four, if d1, d2, d3
2453 // or d4 are < gs_Epsilon
2455 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2456 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2457 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2458 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2460 // Now interpolate as a weighted average of the four surrounding
2461 // points, where the weights are the distances to each of those points
2463 // If the point is exactly at one point of the grid of the source
2464 // image, then don't interpolate -- just assign the pixel
2466 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2468 unsigned char *p
= data
[y1
] + (3 * x1
);
2474 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2476 else if (d2
< gs_Epsilon
)
2478 unsigned char *p
= data
[y1
] + (3 * x2
);
2484 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2486 else if (d3
< gs_Epsilon
)
2488 unsigned char *p
= data
[y2
] + (3 * x2
);
2494 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2496 else if (d4
< gs_Epsilon
)
2498 unsigned char *p
= data
[y2
] + (3 * x1
);
2504 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2508 // weights for the weighted average are proportional to the inverse of the distance
2509 unsigned char *v1
= data
[y1
] + (3 * x1
);
2510 unsigned char *v2
= data
[y1
] + (3 * x2
);
2511 unsigned char *v3
= data
[y2
] + (3 * x2
);
2512 unsigned char *v4
= data
[y2
] + (3 * x1
);
2514 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2518 *(dst
++) = (unsigned char)
2519 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2520 w3
* *(v3
++) + w4
* *(v4
++)) /
2521 (w1
+ w2
+ w3
+ w4
) );
2522 *(dst
++) = (unsigned char)
2523 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2524 w3
* *(v3
++) + w4
* *(v4
++)) /
2525 (w1
+ w2
+ w3
+ w4
) );
2526 *(dst
++) = (unsigned char)
2527 ( (w1
* *v1
+ w2
* *v2
+
2528 w3
* *v3
+ w4
* *v4
) /
2529 (w1
+ w2
+ w3
+ w4
) );
2533 v1
= alpha
[y1
] + (x1
);
2534 v2
= alpha
[y1
] + (x2
);
2535 v3
= alpha
[y2
] + (x2
);
2536 v4
= alpha
[y2
] + (x1
);
2538 *(alpha_dst
++) = (unsigned char)
2539 ( (w1
* *v1
+ w2
* *v2
+
2540 w3
* *v3
+ w4
* *v4
) /
2541 (w1
+ w2
+ w3
+ w4
) );
2557 else // not interpolating
2559 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2561 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2563 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2565 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2566 const int ys
= wxCint (src
.y
); // closest integer
2568 if (0 <= xs
&& xs
< GetWidth() &&
2569 0 <= ys
&& ys
< GetHeight())
2571 unsigned char *p
= data
[ys
] + (3 * xs
);
2577 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2586 *(alpha_dst
++) = 255;
2604 // A module to allow wxImage initialization/cleanup
2605 // without calling these functions from app.cpp or from
2606 // the user's application.
2608 class wxImageModule
: public wxModule
2610 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2613 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2614 void OnExit() { wxImage::CleanUpHandlers(); };
2617 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2620 #endif // wxUSE_IMAGE