1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
36 #include "wx/xpmdecod.h"
42 // make the code compile with either wxFile*Stream or wxFFile*Stream:
43 #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE))
47 typedef wxFileInputStream wxImageFileInputStream
;
48 typedef wxFileOutputStream wxImageFileOutputStream
;
50 typedef wxFFileInputStream wxImageFileInputStream
;
51 typedef wxFFileOutputStream wxImageFileOutputStream
;
52 #endif // wxUSE_FILE/wxUSE_FFILE
53 #endif // HAS_FILE_STREAMS
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 class wxImageRefData
: public wxObjectRefData
63 virtual ~wxImageRefData();
67 unsigned char *m_data
;
70 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
72 // alpha channel data, may be NULL for the formats without alpha support
73 unsigned char *m_alpha
;
77 // if true, m_data is pointer to static data and shouldn't be freed
80 // same as m_static but for m_alpha
85 #endif // wxUSE_PALETTE
87 wxArrayString m_optionNames
;
88 wxArrayString m_optionValues
;
90 DECLARE_NO_COPY_CLASS(wxImageRefData
)
93 wxImageRefData::wxImageRefData()
98 m_alpha
= (unsigned char *) NULL
;
107 m_staticAlpha
= false;
110 wxImageRefData::~wxImageRefData()
114 if ( !m_staticAlpha
)
118 wxList
wxImage::sm_handlers
;
122 //-----------------------------------------------------------------------------
124 #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
126 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
128 wxImage::wxImage( int width
, int height
, bool clear
)
130 Create( width
, height
, clear
);
133 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
135 Create( width
, height
, data
, static_data
);
138 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
140 Create( width
, height
, data
, alpha
, static_data
);
143 wxImage::wxImage( const wxString
& name
, long type
, int index
)
145 LoadFile( name
, type
, index
);
148 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
150 LoadFile( name
, mimetype
, index
);
154 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
156 LoadFile( stream
, type
, index
);
159 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
161 LoadFile( stream
, mimetype
, index
);
163 #endif // wxUSE_STREAMS
165 wxImage::wxImage( const char** xpmData
)
170 wxImage::wxImage( char** xpmData
)
172 Create((const char**) xpmData
);
175 bool wxImage::Create( const char** xpmData
)
180 wxXPMDecoder decoder
;
181 (*this) = decoder
.ReadData(xpmData
);
188 bool wxImage::Create( int width
, int height
, bool clear
)
192 m_refData
= new wxImageRefData();
194 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
195 if (!M_IMGDATA
->m_data
)
202 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
204 M_IMGDATA
->m_width
= width
;
205 M_IMGDATA
->m_height
= height
;
206 M_IMGDATA
->m_ok
= true;
211 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
215 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
217 m_refData
= new wxImageRefData();
219 M_IMGDATA
->m_data
= data
;
220 M_IMGDATA
->m_width
= width
;
221 M_IMGDATA
->m_height
= height
;
222 M_IMGDATA
->m_ok
= true;
223 M_IMGDATA
->m_static
= static_data
;
228 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
232 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
234 m_refData
= new wxImageRefData();
236 M_IMGDATA
->m_data
= data
;
237 M_IMGDATA
->m_alpha
= alpha
;
238 M_IMGDATA
->m_width
= width
;
239 M_IMGDATA
->m_height
= height
;
240 M_IMGDATA
->m_ok
= true;
241 M_IMGDATA
->m_static
= static_data
;
246 void wxImage::Destroy()
251 wxObjectRefData
* wxImage::CreateRefData() const
253 return new wxImageRefData
;
256 wxObjectRefData
* wxImage::CloneRefData(const wxObjectRefData
* that
) const
258 const wxImageRefData
* refData
= wx_static_cast(const wxImageRefData
*, that
);
259 wxCHECK_MSG(refData
->m_ok
, NULL
, wxT("invalid image") );
261 wxImageRefData
* refData_new
= new wxImageRefData
;
262 refData_new
->m_width
= refData
->m_width
;
263 refData_new
->m_height
= refData
->m_height
;
264 refData_new
->m_maskRed
= refData
->m_maskRed
;
265 refData_new
->m_maskGreen
= refData
->m_maskGreen
;
266 refData_new
->m_maskBlue
= refData
->m_maskBlue
;
267 refData_new
->m_hasMask
= refData
->m_hasMask
;
268 refData_new
->m_ok
= true;
269 unsigned size
= unsigned(refData
->m_width
) * unsigned(refData
->m_height
);
270 if (refData
->m_alpha
!= NULL
)
272 refData_new
->m_alpha
= (unsigned char*)malloc(size
);
273 memcpy(refData_new
->m_alpha
, refData
->m_alpha
, size
);
276 refData_new
->m_data
= (unsigned char*)malloc(size
);
277 memcpy(refData_new
->m_data
, refData
->m_data
, size
);
279 refData_new
->m_palette
= refData
->m_palette
;
281 refData_new
->m_optionNames
= refData
->m_optionNames
;
282 refData_new
->m_optionValues
= refData
->m_optionValues
;
286 wxImage
wxImage::Copy() const
290 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
292 image
.m_refData
= CloneRefData(m_refData
);
297 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
299 if( xFactor
== 1 && yFactor
== 1 )
304 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
306 // can't scale to/from 0 size
307 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
308 wxT("invalid new image size") );
310 long old_height
= M_IMGDATA
->m_height
,
311 old_width
= M_IMGDATA
->m_width
;
313 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
314 wxT("invalid old image size") );
316 long width
= old_width
/ xFactor
;
317 long height
= old_height
/ yFactor
;
319 image
.Create( width
, height
, false );
321 char unsigned *data
= image
.GetData();
323 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
325 bool hasMask
= false ;
326 unsigned char maskRed
= 0;
327 unsigned char maskGreen
= 0;
328 unsigned char maskBlue
=0 ;
330 unsigned char *source_data
= M_IMGDATA
->m_data
;
331 unsigned char *target_data
= data
;
332 unsigned char *source_alpha
= 0 ;
333 unsigned char *target_alpha
= 0 ;
334 if (M_IMGDATA
->m_hasMask
)
337 maskRed
= M_IMGDATA
->m_maskRed
;
338 maskGreen
= M_IMGDATA
->m_maskGreen
;
339 maskBlue
=M_IMGDATA
->m_maskBlue
;
341 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
342 M_IMGDATA
->m_maskGreen
,
343 M_IMGDATA
->m_maskBlue
);
347 source_alpha
= M_IMGDATA
->m_alpha
;
351 target_alpha
= image
.GetAlpha() ;
355 for (long y
= 0; y
< height
; y
++)
357 for (long x
= 0; x
< width
; x
++)
359 unsigned long avgRed
= 0 ;
360 unsigned long avgGreen
= 0;
361 unsigned long avgBlue
= 0;
362 unsigned long avgAlpha
= 0 ;
363 unsigned long counter
= 0 ;
365 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
367 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
368 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
370 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
371 unsigned char red
= pixel
[0] ;
372 unsigned char green
= pixel
[1] ;
373 unsigned char blue
= pixel
[2] ;
374 unsigned char alpha
= 255 ;
376 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
377 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
392 *(target_data
++) = M_IMGDATA
->m_maskRed
;
393 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
394 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
399 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
400 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
401 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
402 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
407 // In case this is a cursor, make sure the hotspot is scaled accordingly:
408 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
409 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
410 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
411 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
412 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
413 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
418 wxImage
wxImage::Scale( int width
, int height
) const
422 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
424 // can't scale to/from 0 size
425 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
426 wxT("invalid new image size") );
428 long old_height
= M_IMGDATA
->m_height
,
429 old_width
= M_IMGDATA
->m_width
;
430 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
431 wxT("invalid old image size") );
433 if ( old_width
% width
== 0 && old_width
>= width
&&
434 old_height
% height
== 0 && old_height
>= height
)
436 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
438 image
.Create( width
, height
, false );
440 unsigned char *data
= image
.GetData();
442 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
444 unsigned char *source_data
= M_IMGDATA
->m_data
;
445 unsigned char *target_data
= data
;
446 unsigned char *source_alpha
= 0 ;
447 unsigned char *target_alpha
= 0 ;
449 if (M_IMGDATA
->m_hasMask
)
451 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
452 M_IMGDATA
->m_maskGreen
,
453 M_IMGDATA
->m_maskBlue
);
457 source_alpha
= M_IMGDATA
->m_alpha
;
461 target_alpha
= image
.GetAlpha() ;
465 long x_delta
= (old_width
<<16) / width
;
466 long y_delta
= (old_height
<<16) / height
;
468 unsigned char* dest_pixel
= target_data
;
471 for ( long j
= 0; j
< height
; j
++ )
473 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
474 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
477 for ( long i
= 0; i
< width
; i
++ )
479 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
480 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
481 dest_pixel
[0] = src_pixel
[0];
482 dest_pixel
[1] = src_pixel
[1];
483 dest_pixel
[2] = src_pixel
[2];
486 *(target_alpha
++) = *src_alpha_pixel
;
493 // In case this is a cursor, make sure the hotspot is scaled accordingly:
494 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
495 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
496 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
497 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
498 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
499 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
504 wxImage
wxImage::Rotate90( bool clockwise
) const
508 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
510 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
512 unsigned char *data
= image
.GetData();
514 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
516 unsigned char *source_data
= M_IMGDATA
->m_data
;
517 unsigned char *target_data
;
518 unsigned char *alpha_data
= 0 ;
519 unsigned char *source_alpha
= 0 ;
520 unsigned char *target_alpha
= 0 ;
522 if (M_IMGDATA
->m_hasMask
)
524 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
528 source_alpha
= M_IMGDATA
->m_alpha
;
532 alpha_data
= image
.GetAlpha() ;
536 long height
= M_IMGDATA
->m_height
;
537 long width
= M_IMGDATA
->m_width
;
539 for (long j
= 0; j
< height
; j
++)
541 for (long i
= 0; i
< width
; i
++)
545 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
547 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
551 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
553 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
555 memcpy( target_data
, source_data
, 3 );
560 memcpy( target_alpha
, source_alpha
, 1 );
569 wxImage
wxImage::Mirror( bool horizontally
) const
573 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
575 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
577 unsigned char *data
= image
.GetData();
578 unsigned char *alpha
= NULL
;
580 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
582 if (M_IMGDATA
->m_alpha
!= NULL
) {
584 alpha
= image
.GetAlpha();
585 wxCHECK_MSG( alpha
, image
, wxT("unable to create alpha channel") );
588 if (M_IMGDATA
->m_hasMask
)
589 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
591 long height
= M_IMGDATA
->m_height
;
592 long width
= M_IMGDATA
->m_width
;
594 unsigned char *source_data
= M_IMGDATA
->m_data
;
595 unsigned char *target_data
;
599 for (long j
= 0; j
< height
; j
++)
602 target_data
= data
-3;
603 for (long i
= 0; i
< width
; i
++)
605 memcpy( target_data
, source_data
, 3 );
613 // src_alpha starts at the first pixel and increases by 1 after each step
614 // (a step here is the copy of the alpha value of one pixel)
615 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
616 // dest_alpha starts just beyond the first line, decreases before each step,
617 // and after each line is finished, increases by 2 widths (skipping the line
618 // just copied and the line that will be copied next)
619 unsigned char *dest_alpha
= alpha
+ width
;
621 for (long jj
= 0; jj
< height
; ++jj
)
623 for (long i
= 0; i
< width
; ++i
) {
624 *(--dest_alpha
) = *(src_alpha
++); // copy one pixel
626 dest_alpha
+= 2 * width
; // advance beyond the end of the next line
632 for (long i
= 0; i
< height
; i
++)
634 target_data
= data
+ 3*width
*(height
-1-i
);
635 memcpy( target_data
, source_data
, (size_t)3*width
);
636 source_data
+= 3*width
;
641 // src_alpha starts at the first pixel and increases by 1 width after each step
642 // (a step here is the copy of the alpha channel of an entire line)
643 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
644 // dest_alpha starts just beyond the last line (beyond the whole image)
645 // and decreases by 1 width before each step
646 unsigned char *dest_alpha
= alpha
+ width
* height
;
648 for (long jj
= 0; jj
< height
; ++jj
)
651 memcpy( dest_alpha
, src_alpha
, (size_t)width
);
660 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
664 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
666 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) &&
667 (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
668 image
, wxT("invalid subimage size") );
670 const int subwidth
= rect
.GetWidth();
671 const int subheight
= rect
.GetHeight();
673 image
.Create( subwidth
, subheight
, false );
675 const unsigned char *src_data
= GetData();
676 const unsigned char *src_alpha
= M_IMGDATA
->m_alpha
;
677 unsigned char *subdata
= image
.GetData();
678 unsigned char *subalpha
= NULL
;
680 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
682 if (src_alpha
!= NULL
) {
684 subalpha
= image
.GetAlpha();
685 wxCHECK_MSG( subalpha
, image
, wxT("unable to create alpha channel"));
688 if (M_IMGDATA
->m_hasMask
)
689 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
691 const int width
= GetWidth();
692 const int pixsoff
= rect
.GetLeft() + width
* rect
.GetTop();
694 src_data
+= 3 * pixsoff
;
695 src_alpha
+= pixsoff
; // won't be used if was NULL, so this is ok
697 for (long j
= 0; j
< subheight
; ++j
)
699 memcpy( subdata
, src_data
, 3 * subwidth
);
700 subdata
+= 3 * subwidth
;
701 src_data
+= 3 * width
;
702 if (subalpha
!= NULL
) {
703 memcpy( subalpha
, src_alpha
, subwidth
);
704 subalpha
+= subwidth
;
712 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
713 int r_
, int g_
, int b_
) const
717 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
718 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
720 int width
= GetWidth(), height
= GetHeight();
721 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
723 unsigned char r
= (unsigned char)r_
;
724 unsigned char g
= (unsigned char)g_
;
725 unsigned char b
= (unsigned char)b_
;
726 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
728 GetOrFindMaskColour( &r
, &g
, &b
);
729 image
.SetMaskColour(r
, g
, b
);
732 image
.SetRGB(wxRect(), r
, g
, b
);
734 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
735 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
737 finalRect
.width
-= pos
.x
;
739 finalRect
.height
-= pos
.y
;
741 subRect
.Intersect(finalRect
);
743 if (!subRect
.IsEmpty())
745 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
746 image
.Paste(*this, pos
.x
, pos
.y
);
748 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
754 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
756 wxCHECK_RET( Ok(), wxT("invalid image") );
757 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
763 int width
= image
.GetWidth();
764 int height
= image
.GetHeight();
777 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
778 width
= M_IMGDATA
->m_width
- (x
+xx
);
779 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
780 height
= M_IMGDATA
->m_height
- (y
+yy
);
782 if (width
< 1) return;
783 if (height
< 1) return;
785 if ((!HasMask() && !image
.HasMask()) ||
786 (HasMask() && !image
.HasMask()) ||
787 ((HasMask() && image
.HasMask() &&
788 (GetMaskRed()==image
.GetMaskRed()) &&
789 (GetMaskGreen()==image
.GetMaskGreen()) &&
790 (GetMaskBlue()==image
.GetMaskBlue()))))
793 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
794 int source_step
= image
.GetWidth()*3;
796 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
797 int target_step
= M_IMGDATA
->m_width
*3;
798 for (int j
= 0; j
< height
; j
++)
800 memcpy( target_data
, source_data
, width
);
801 source_data
+= source_step
;
802 target_data
+= target_step
;
807 if (!HasMask() && image
.HasMask())
809 unsigned char r
= image
.GetMaskRed();
810 unsigned char g
= image
.GetMaskGreen();
811 unsigned char b
= image
.GetMaskBlue();
814 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
815 int source_step
= image
.GetWidth()*3;
817 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
818 int target_step
= M_IMGDATA
->m_width
*3;
820 for (int j
= 0; j
< height
; j
++)
822 for (int i
= 0; i
< width
; i
+=3)
824 if ((source_data
[i
] != r
) &&
825 (source_data
[i
+1] != g
) &&
826 (source_data
[i
+2] != b
))
828 memcpy( target_data
+i
, source_data
+i
, 3 );
831 source_data
+= source_step
;
832 target_data
+= target_step
;
837 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
838 unsigned char r2
, unsigned char g2
, unsigned char b2
)
840 wxCHECK_RET( Ok(), wxT("invalid image") );
844 unsigned char *data
= GetData();
846 const int w
= GetWidth();
847 const int h
= GetHeight();
849 for (int j
= 0; j
< h
; j
++)
850 for (int i
= 0; i
< w
; i
++)
852 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
862 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
866 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
868 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
870 unsigned char *dest
= image
.GetData();
872 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
874 unsigned char *src
= M_IMGDATA
->m_data
;
875 bool hasMask
= M_IMGDATA
->m_hasMask
;
876 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
877 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
878 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
881 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
883 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
884 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
886 // don't modify the mask
887 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
889 memcpy(dest
, src
, 3);
893 // calculate the luma
894 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
895 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
899 // copy the alpha channel, if any
902 const size_t alphaSize
= GetWidth() * GetHeight();
903 unsigned char *alpha
= (unsigned char*)malloc(alphaSize
);
904 memcpy(alpha
, GetAlpha(), alphaSize
);
906 image
.SetAlpha(alpha
);
912 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
916 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
918 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
920 unsigned char *data
= image
.GetData();
922 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
924 if (M_IMGDATA
->m_hasMask
)
926 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
927 M_IMGDATA
->m_maskBlue
== b
)
928 image
.SetMaskColour( 255, 255, 255 );
930 image
.SetMaskColour( 0, 0, 0 );
933 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
935 unsigned char *srcd
= M_IMGDATA
->m_data
;
936 unsigned char *tard
= image
.GetData();
938 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
940 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
941 tard
[0] = tard
[1] = tard
[2] = 255;
943 tard
[0] = tard
[1] = tard
[2] = 0;
949 int wxImage::GetWidth() const
951 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
953 return M_IMGDATA
->m_width
;
956 int wxImage::GetHeight() const
958 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
960 return M_IMGDATA
->m_height
;
963 long wxImage::XYToIndex(int x
, int y
) const
967 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
969 return y
*M_IMGDATA
->m_width
+ x
;
975 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
977 long pos
= XYToIndex(x
, y
);
978 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
984 M_IMGDATA
->m_data
[ pos
] = r
;
985 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
986 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
989 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
991 wxCHECK_RET( Ok(), wxT("invalid image") );
996 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
997 if ( rect
== wxRect() )
1003 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
1004 imageRect
.Inside(rect
.GetBottomRight()),
1005 wxT("invalid bounding rectangle") );
1008 int x1
= rect
.GetLeft(),
1010 x2
= rect
.GetRight() + 1,
1011 y2
= rect
.GetBottom() + 1;
1013 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
1014 int x
, y
, width
= GetWidth();
1015 for (y
= y1
; y
< y2
; y
++)
1017 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
1018 for (x
= x1
; x
< x2
; x
++)
1027 unsigned char wxImage::GetRed( int x
, int y
) const
1029 long pos
= XYToIndex(x
, y
);
1030 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1034 return M_IMGDATA
->m_data
[pos
];
1037 unsigned char wxImage::GetGreen( int x
, int y
) const
1039 long pos
= XYToIndex(x
, y
);
1040 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1044 return M_IMGDATA
->m_data
[pos
+1];
1047 unsigned char wxImage::GetBlue( int x
, int y
) const
1049 long pos
= XYToIndex(x
, y
);
1050 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1054 return M_IMGDATA
->m_data
[pos
+2];
1057 bool wxImage::Ok() const
1059 // image of 0 width or height can't be considered ok - at least because it
1060 // causes crashes in ConvertToBitmap() if we don't catch it in time
1061 wxImageRefData
*data
= M_IMGDATA
;
1062 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
1065 unsigned char *wxImage::GetData() const
1067 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1069 return M_IMGDATA
->m_data
;
1072 void wxImage::SetData( unsigned char *data
, bool static_data
)
1074 wxCHECK_RET( Ok(), wxT("invalid image") );
1076 wxImageRefData
*newRefData
= new wxImageRefData();
1078 newRefData
->m_width
= M_IMGDATA
->m_width
;
1079 newRefData
->m_height
= M_IMGDATA
->m_height
;
1080 newRefData
->m_data
= data
;
1081 newRefData
->m_ok
= true;
1082 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1083 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1084 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1085 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1086 newRefData
->m_static
= static_data
;
1090 m_refData
= newRefData
;
1093 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
1095 wxImageRefData
*newRefData
= new wxImageRefData();
1099 newRefData
->m_width
= new_width
;
1100 newRefData
->m_height
= new_height
;
1101 newRefData
->m_data
= data
;
1102 newRefData
->m_ok
= true;
1103 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
1104 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
1105 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1106 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1110 newRefData
->m_width
= new_width
;
1111 newRefData
->m_height
= new_height
;
1112 newRefData
->m_data
= data
;
1113 newRefData
->m_ok
= true;
1115 newRefData
->m_static
= static_data
;
1119 m_refData
= newRefData
;
1122 // ----------------------------------------------------------------------------
1123 // alpha channel support
1124 // ----------------------------------------------------------------------------
1126 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1128 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1130 long pos
= XYToIndex(x
, y
);
1131 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1135 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1138 unsigned char wxImage::GetAlpha(int x
, int y
) const
1140 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1142 long pos
= XYToIndex(x
, y
);
1143 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1145 return M_IMGDATA
->m_alpha
[pos
];
1149 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1153 const int w
= M_IMGDATA
->m_width
;
1154 const int h
= M_IMGDATA
->m_height
;
1156 unsigned char *alpha
= GetAlpha();
1157 unsigned char *data
= GetData();
1159 for ( int y
= 0; y
< h
; y
++ )
1161 for ( int x
= 0; x
< w
; x
++ )
1173 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1175 wxCHECK_RET( Ok(), wxT("invalid image") );
1181 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1184 free(M_IMGDATA
->m_alpha
);
1185 M_IMGDATA
->m_alpha
= alpha
;
1186 M_IMGDATA
->m_staticAlpha
= static_data
;
1189 unsigned char *wxImage::GetAlpha() const
1191 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1193 return M_IMGDATA
->m_alpha
;
1196 void wxImage::InitAlpha()
1198 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1200 // initialize memory for alpha channel
1203 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1204 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1208 // use the mask to initialize the alpha channel.
1209 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1211 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1212 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1213 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1214 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1218 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1219 ? wxIMAGE_ALPHA_TRANSPARENT
1220 : wxIMAGE_ALPHA_OPAQUE
;
1223 M_IMGDATA
->m_hasMask
= false;
1227 // make the image fully opaque
1228 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1232 // ----------------------------------------------------------------------------
1234 // ----------------------------------------------------------------------------
1236 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1238 wxCHECK_RET( Ok(), wxT("invalid image") );
1242 M_IMGDATA
->m_maskRed
= r
;
1243 M_IMGDATA
->m_maskGreen
= g
;
1244 M_IMGDATA
->m_maskBlue
= b
;
1245 M_IMGDATA
->m_hasMask
= true;
1248 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1250 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1252 if (M_IMGDATA
->m_hasMask
)
1254 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1255 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1256 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1261 FindFirstUnusedColour(r
, g
, b
);
1266 unsigned char wxImage::GetMaskRed() const
1268 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1270 return M_IMGDATA
->m_maskRed
;
1273 unsigned char wxImage::GetMaskGreen() const
1275 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1277 return M_IMGDATA
->m_maskGreen
;
1280 unsigned char wxImage::GetMaskBlue() const
1282 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1284 return M_IMGDATA
->m_maskBlue
;
1287 void wxImage::SetMask( bool mask
)
1289 wxCHECK_RET( Ok(), wxT("invalid image") );
1293 M_IMGDATA
->m_hasMask
= mask
;
1296 bool wxImage::HasMask() const
1298 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1300 return M_IMGDATA
->m_hasMask
;
1303 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1305 long pos
= XYToIndex(x
, y
);
1306 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1309 if ( M_IMGDATA
->m_hasMask
)
1311 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1312 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1313 p
[1] == M_IMGDATA
->m_maskGreen
&&
1314 p
[2] == M_IMGDATA
->m_maskBlue
)
1321 if ( M_IMGDATA
->m_alpha
)
1323 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1325 // transparent enough
1334 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1335 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1337 // check that the images are the same size
1338 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1340 wxLogError( _("Image and mask have different sizes.") );
1344 // find unused colour
1345 unsigned char r
,g
,b
;
1346 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1348 wxLogError( _("No unused colour in image being masked.") );
1354 unsigned char *imgdata
= GetData();
1355 unsigned char *maskdata
= mask
.GetData();
1357 const int w
= GetWidth();
1358 const int h
= GetHeight();
1360 for (int j
= 0; j
< h
; j
++)
1362 for (int i
= 0; i
< w
; i
++)
1364 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1375 SetMaskColour(r
, g
, b
);
1381 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1386 unsigned char mr
, mg
, mb
;
1387 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1389 wxLogError( _("No unused colour in image being masked.") );
1396 SetMaskColour(mr
, mg
, mb
);
1398 unsigned char *imgdata
= GetData();
1399 unsigned char *alphadata
= GetAlpha();
1402 int h
= GetHeight();
1404 for (int y
= 0; y
< h
; y
++)
1406 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1408 if (*alphadata
< threshold
)
1417 free(M_IMGDATA
->m_alpha
);
1418 M_IMGDATA
->m_alpha
= NULL
;
1423 // ----------------------------------------------------------------------------
1424 // Palette functions
1425 // ----------------------------------------------------------------------------
1429 bool wxImage::HasPalette() const
1434 return M_IMGDATA
->m_palette
.Ok();
1437 const wxPalette
& wxImage::GetPalette() const
1439 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1441 return M_IMGDATA
->m_palette
;
1444 void wxImage::SetPalette(const wxPalette
& palette
)
1446 wxCHECK_RET( Ok(), wxT("invalid image") );
1450 M_IMGDATA
->m_palette
= palette
;
1453 #endif // wxUSE_PALETTE
1455 // ----------------------------------------------------------------------------
1456 // Option functions (arbitrary name/value mapping)
1457 // ----------------------------------------------------------------------------
1459 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1461 wxCHECK_RET( Ok(), wxT("invalid image") );
1465 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1466 if (idx
== wxNOT_FOUND
)
1468 M_IMGDATA
->m_optionNames
.Add(name
);
1469 M_IMGDATA
->m_optionValues
.Add(value
);
1473 M_IMGDATA
->m_optionNames
[idx
] = name
;
1474 M_IMGDATA
->m_optionValues
[idx
] = value
;
1478 void wxImage::SetOption(const wxString
& name
, int value
)
1481 valStr
.Printf(wxT("%d"), value
);
1482 SetOption(name
, valStr
);
1485 wxString
wxImage::GetOption(const wxString
& name
) const
1487 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1489 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1490 if (idx
== wxNOT_FOUND
)
1491 return wxEmptyString
;
1493 return M_IMGDATA
->m_optionValues
[idx
];
1496 int wxImage::GetOptionInt(const wxString
& name
) const
1498 return wxAtoi(GetOption(name
));
1501 bool wxImage::HasOption(const wxString
& name
) const
1503 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1505 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1508 // ----------------------------------------------------------------------------
1510 // ----------------------------------------------------------------------------
1512 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1513 long WXUNUSED_UNLESS_STREAMS(type
),
1514 int WXUNUSED_UNLESS_STREAMS(index
) )
1516 #if HAS_FILE_STREAMS
1517 if (wxFileExists(filename
))
1519 wxImageFileInputStream
stream(filename
);
1520 wxBufferedInputStream
bstream( stream
);
1521 return LoadFile(bstream
, type
, index
);
1525 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1529 #else // !HAS_FILE_STREAMS
1531 #endif // HAS_FILE_STREAMS
1534 bool wxImage::LoadFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1535 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
),
1536 int WXUNUSED_UNLESS_STREAMS(index
) )
1538 #if HAS_FILE_STREAMS
1539 if (wxFileExists(filename
))
1541 wxImageFileInputStream
stream(filename
);
1542 wxBufferedInputStream
bstream( stream
);
1543 return LoadFile(bstream
, mimetype
, index
);
1547 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1551 #else // !HAS_FILE_STREAMS
1553 #endif // HAS_FILE_STREAMS
1558 bool wxImage::SaveFile( const wxString
& filename
) const
1560 wxString ext
= filename
.AfterLast('.').Lower();
1562 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1565 SaveFile(filename
, pHandler
->GetType());
1569 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1574 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1575 int WXUNUSED_UNLESS_STREAMS(type
) ) const
1577 #if HAS_FILE_STREAMS
1578 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1580 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1582 wxImageFileOutputStream
stream(filename
);
1584 if ( stream
.IsOk() )
1586 wxBufferedOutputStream
bstream( stream
);
1587 return SaveFile(bstream
, type
);
1589 #endif // HAS_FILE_STREAMS
1594 bool wxImage::SaveFile( const wxString
& WXUNUSED_UNLESS_STREAMS(filename
),
1595 const wxString
& WXUNUSED_UNLESS_STREAMS(mimetype
) ) const
1597 #if HAS_FILE_STREAMS
1598 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1600 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1602 wxImageFileOutputStream
stream(filename
);
1604 if ( stream
.IsOk() )
1606 wxBufferedOutputStream
bstream( stream
);
1607 return SaveFile(bstream
, mimetype
);
1609 #endif // HAS_FILE_STREAMS
1614 bool wxImage::CanRead( const wxString
& WXUNUSED_UNLESS_STREAMS(name
) )
1616 #if HAS_FILE_STREAMS
1617 wxImageFileInputStream
stream(name
);
1618 return CanRead(stream
);
1624 int wxImage::GetImageCount( const wxString
& WXUNUSED_UNLESS_STREAMS(name
),
1625 long WXUNUSED_UNLESS_STREAMS(type
) )
1627 #if HAS_FILE_STREAMS
1628 wxImageFileInputStream
stream(name
);
1630 return GetImageCount(stream
, type
);
1638 bool wxImage::CanRead( wxInputStream
&stream
)
1640 const wxList
& list
= GetHandlers();
1642 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1644 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1645 if (handler
->CanRead( stream
))
1652 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1654 wxImageHandler
*handler
;
1656 if ( type
== wxBITMAP_TYPE_ANY
)
1658 wxList
&list
=GetHandlers();
1660 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1662 handler
=(wxImageHandler
*)node
->GetData();
1663 if ( handler
->CanRead(stream
) )
1664 return handler
->GetImageCount(stream
);
1668 wxLogWarning(_("No handler found for image type."));
1672 handler
= FindHandler(type
);
1676 wxLogWarning(_("No image handler for type %ld defined."), type
);
1680 if ( handler
->CanRead(stream
) )
1682 return handler
->GetImageCount(stream
);
1686 wxLogError(_("Image file is not of type %ld."), type
);
1691 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1695 m_refData
= new wxImageRefData
;
1697 wxImageHandler
*handler
;
1699 if ( type
== wxBITMAP_TYPE_ANY
)
1701 wxList
&list
=GetHandlers();
1703 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1705 handler
=(wxImageHandler
*)node
->GetData();
1706 if ( handler
->CanRead(stream
) )
1707 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1711 wxLogWarning( _("No handler found for image type.") );
1715 handler
= FindHandler(type
);
1719 wxLogWarning( _("No image handler for type %ld defined."), type
);
1724 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1726 wxLogError(_("Image file is not of type %ld."), type
);
1730 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1733 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1737 m_refData
= new wxImageRefData
;
1739 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1743 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1748 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1750 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1754 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1757 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1759 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1761 wxImageHandler
*handler
= FindHandler(type
);
1764 wxLogWarning( _("No image handler for type %d defined."), type
);
1769 return handler
->SaveFile( (wxImage
*)this, stream
);
1772 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1774 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1776 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1779 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1784 return handler
->SaveFile( (wxImage
*)this, stream
);
1786 #endif // wxUSE_STREAMS
1788 // ----------------------------------------------------------------------------
1789 // image I/O handlers
1790 // ----------------------------------------------------------------------------
1792 void wxImage::AddHandler( wxImageHandler
*handler
)
1794 // Check for an existing handler of the type being added.
1795 if (FindHandler( handler
->GetType() ) == 0)
1797 sm_handlers
.Append( handler
);
1801 // This is not documented behaviour, merely the simplest 'fix'
1802 // for preventing duplicate additions. If someone ever has
1803 // a good reason to add and remove duplicate handlers (and they
1804 // may) we should probably refcount the duplicates.
1805 // also an issue in InsertHandler below.
1807 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1808 handler
->GetName().c_str() );
1813 void wxImage::InsertHandler( wxImageHandler
*handler
)
1815 // Check for an existing handler of the type being added.
1816 if (FindHandler( handler
->GetType() ) == 0)
1818 sm_handlers
.Insert( handler
);
1822 // see AddHandler for additional comments.
1823 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1824 handler
->GetName().c_str() );
1829 bool wxImage::RemoveHandler( const wxString
& name
)
1831 wxImageHandler
*handler
= FindHandler(name
);
1834 sm_handlers
.DeleteObject(handler
);
1842 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1844 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1847 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1848 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1850 node
= node
->GetNext();
1855 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1857 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1860 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1861 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1862 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1864 node
= node
->GetNext();
1869 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1871 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1874 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1875 if (handler
->GetType() == bitmapType
) return handler
;
1876 node
= node
->GetNext();
1881 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1883 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1886 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1887 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1888 node
= node
->GetNext();
1893 void wxImage::InitStandardHandlers()
1896 AddHandler(new wxBMPHandler
);
1897 #endif // wxUSE_STREAMS
1900 void wxImage::CleanUpHandlers()
1902 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1905 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1906 wxList::compatibility_iterator next
= node
->GetNext();
1911 sm_handlers
.Clear();
1914 wxString
wxImage::GetImageExtWildcard()
1918 wxList
& Handlers
= wxImage::GetHandlers();
1919 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1922 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1923 fmts
+= wxT("*.") + Handler
->GetExtension();
1924 Node
= Node
->GetNext();
1925 if ( Node
) fmts
+= wxT(";");
1928 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1931 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1933 const double red
= rgb
.red
/ 255.0,
1934 green
= rgb
.green
/ 255.0,
1935 blue
= rgb
.blue
/ 255.0;
1937 // find the min and max intensity (and remember which one was it for the
1939 double minimumRGB
= red
;
1940 if ( green
< minimumRGB
)
1942 if ( blue
< minimumRGB
)
1945 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1946 double maximumRGB
= red
;
1947 if ( green
> maximumRGB
)
1952 if ( blue
> maximumRGB
)
1958 const double value
= maximumRGB
;
1960 double hue
= 0.0, saturation
;
1961 const double deltaRGB
= maximumRGB
- minimumRGB
;
1962 if ( wxIsNullDouble(deltaRGB
) )
1964 // Gray has no color
1973 hue
= (green
- blue
) / deltaRGB
;
1977 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1981 hue
= 4.0 + (red
- green
) / deltaRGB
;
1985 wxFAIL_MSG(wxT("hue not specified"));
1994 saturation
= deltaRGB
/ maximumRGB
;
1997 return HSVValue(hue
, saturation
, value
);
2000 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
2002 double red
, green
, blue
;
2004 if ( wxIsNullDouble(hsv
.saturation
) )
2013 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
2014 int i
= (int)floor(hue
);
2015 double f
= hue
- i
; // fractional part of h
2016 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
2022 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2027 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2035 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2040 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2045 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
2053 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
2058 return RGBValue((unsigned char)(red
* 255.0),
2059 (unsigned char)(green
* 255.0),
2060 (unsigned char)(blue
* 255.0));
2064 * Rotates the hue of each pixel of the image. angle is a double in the range
2065 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2067 void wxImage::RotateHue(double angle
)
2071 unsigned char *srcBytePtr
;
2072 unsigned char *dstBytePtr
;
2073 unsigned long count
;
2074 wxImage::HSVValue hsv
;
2075 wxImage::RGBValue rgb
;
2077 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
2078 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
2079 if ( count
> 0 && !wxIsNullDouble(angle
) )
2081 srcBytePtr
= M_IMGDATA
->m_data
;
2082 dstBytePtr
= srcBytePtr
;
2085 rgb
.red
= *srcBytePtr
++;
2086 rgb
.green
= *srcBytePtr
++;
2087 rgb
.blue
= *srcBytePtr
++;
2088 hsv
= RGBtoHSV(rgb
);
2090 hsv
.hue
= hsv
.hue
+ angle
;
2092 hsv
.hue
= hsv
.hue
- 1.0;
2093 else if (hsv
.hue
< 0.0)
2094 hsv
.hue
= hsv
.hue
+ 1.0;
2096 rgb
= HSVtoRGB(hsv
);
2097 *dstBytePtr
++ = rgb
.red
;
2098 *dstBytePtr
++ = rgb
.green
;
2099 *dstBytePtr
++ = rgb
.blue
;
2100 } while (--count
!= 0);
2104 //-----------------------------------------------------------------------------
2106 //-----------------------------------------------------------------------------
2108 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
2111 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
2116 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
2121 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
2126 bool wxImageHandler::CanRead( const wxString
& name
)
2128 if (wxFileExists(name
))
2130 wxImageFileInputStream
stream(name
);
2131 return CanRead(stream
);
2134 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2139 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2141 wxFileOffset posOld
= stream
.TellI();
2142 if ( posOld
== wxInvalidOffset
)
2144 // can't test unseekable stream
2148 bool ok
= DoCanRead(stream
);
2150 // restore the old position to be able to test other formats and so on
2151 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2153 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2155 // reading would fail anyhow as we're not at the right position
2162 #endif // wxUSE_STREAMS
2164 // ----------------------------------------------------------------------------
2165 // image histogram stuff
2166 // ----------------------------------------------------------------------------
2169 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2174 unsigned char g2
) const
2176 unsigned long key
= MakeKey(r2
, g2
, b2
);
2178 while ( find(key
) != end() )
2180 // color already used
2192 wxLogError(_("No unused colour in image.") );
2198 key
= MakeKey(r2
, g2
, b2
);
2212 wxImage::FindFirstUnusedColour(unsigned char *r
,
2217 unsigned char g2
) const
2219 wxImageHistogram histogram
;
2221 ComputeHistogram(histogram
);
2223 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2229 // Counts and returns the number of different colours. Optionally stops
2230 // when it exceeds 'stopafter' different colours. This is useful, for
2231 // example, to see if the image can be saved as 8-bit (256 colour or
2232 // less, in this case it would be invoked as CountColours(256)). Default
2233 // value for stopafter is -1 (don't care).
2235 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2239 unsigned char r
, g
, b
;
2241 unsigned long size
, nentries
, key
;
2244 size
= GetWidth() * GetHeight();
2247 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2252 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2254 if (h
.Get(key
) == NULL
)
2265 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2267 unsigned char *p
= GetData();
2268 unsigned long nentries
= 0;
2272 const unsigned long size
= GetWidth() * GetHeight();
2274 unsigned char r
, g
, b
;
2275 for ( unsigned long n
= 0; n
< size
; n
++ )
2281 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2283 if ( entry
.value
++ == 0 )
2284 entry
.index
= nentries
++;
2291 * Rotation code by Carlos Moreno
2294 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2295 // does exactly the same thing. And I also got rid of wxRotationPixel
2296 // bacause of potential problems in architectures where alignment
2297 // is an issue, so I had to rewrite parts of the code.
2299 static const double gs_Epsilon
= 1e-10;
2301 static inline int wxCint (double x
)
2303 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2307 // Auxiliary function to rotate a point (x,y) with respect to point p0
2308 // make it inline and use a straight return to facilitate optimization
2309 // also, the function receives the sine and cosine of the angle to avoid
2310 // repeating the time-consuming calls to these functions -- sin/cos can
2311 // be computed and stored in the calling function.
2313 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2315 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2316 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2319 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2321 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2324 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2327 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2329 bool has_alpha
= HasAlpha();
2331 // Create pointer-based array to accelerate access to wxImage's data
2332 unsigned char ** data
= new unsigned char * [GetHeight()];
2333 data
[0] = GetData();
2334 for (i
= 1; i
< GetHeight(); i
++)
2335 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2337 // Same for alpha channel
2338 unsigned char ** alpha
= NULL
;
2341 alpha
= new unsigned char * [GetHeight()];
2342 alpha
[0] = GetAlpha();
2343 for (i
= 1; i
< GetHeight(); i
++)
2344 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2347 // precompute coefficients for rotation formula
2348 // (sine and cosine of the angle)
2349 const double cos_angle
= cos(angle
);
2350 const double sin_angle
= sin(angle
);
2352 // Create new Image to store the result
2353 // First, find rectangle that covers the rotated image; to do that,
2354 // rotate the four corners
2356 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2358 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2359 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2360 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2361 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2363 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2364 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2365 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2366 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2368 // Create rotated image
2369 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2370 // With alpha channel
2374 if (offset_after_rotation
!= NULL
)
2376 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2379 // GRG: The rotated (destination) image is always accessed
2380 // sequentially, so there is no need for a pointer-based
2381 // array here (and in fact it would be slower).
2383 unsigned char * dst
= rotated
.GetData();
2385 unsigned char * alpha_dst
= NULL
;
2387 alpha_dst
= rotated
.GetAlpha();
2389 // GRG: if the original image has a mask, use its RGB values
2390 // as the blank pixel, else, fall back to default (black).
2392 unsigned char blank_r
= 0;
2393 unsigned char blank_g
= 0;
2394 unsigned char blank_b
= 0;
2398 blank_r
= GetMaskRed();
2399 blank_g
= GetMaskGreen();
2400 blank_b
= GetMaskBlue();
2401 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2404 // Now, for each point of the rotated image, find where it came from, by
2405 // performing an inverse rotation (a rotation of -angle) and getting the
2406 // pixel at those coordinates
2408 // GRG: I've taken the (interpolating) test out of the loops, so that
2409 // it is done only once, instead of repeating it for each pixel.
2414 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2416 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2418 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2420 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2421 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2423 // interpolate using the 4 enclosing grid-points. Those
2424 // points can be obtained using floor and ceiling of the
2425 // exact coordinates of the point
2428 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2430 x1
= wxCint(floor(src
.x
));
2431 x2
= wxCint(ceil(src
.x
));
2433 else // else means that x is near one of the borders (0 or width-1)
2435 x1
= x2
= wxCint (src
.x
);
2438 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2440 y1
= wxCint(floor(src
.y
));
2441 y2
= wxCint(ceil(src
.y
));
2445 y1
= y2
= wxCint (src
.y
);
2448 // get four points and the distances (square of the distance,
2449 // for efficiency reasons) for the interpolation formula
2451 // GRG: Do not calculate the points until they are
2452 // really needed -- this way we can calculate
2453 // just one, instead of four, if d1, d2, d3
2454 // or d4 are < gs_Epsilon
2456 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2457 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2458 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2459 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2461 // Now interpolate as a weighted average of the four surrounding
2462 // points, where the weights are the distances to each of those points
2464 // If the point is exactly at one point of the grid of the source
2465 // image, then don't interpolate -- just assign the pixel
2467 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2469 unsigned char *p
= data
[y1
] + (3 * x1
);
2475 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2477 else if (d2
< gs_Epsilon
)
2479 unsigned char *p
= data
[y1
] + (3 * x2
);
2485 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2487 else if (d3
< gs_Epsilon
)
2489 unsigned char *p
= data
[y2
] + (3 * x2
);
2495 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2497 else if (d4
< gs_Epsilon
)
2499 unsigned char *p
= data
[y2
] + (3 * x1
);
2505 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2509 // weights for the weighted average are proportional to the inverse of the distance
2510 unsigned char *v1
= data
[y1
] + (3 * x1
);
2511 unsigned char *v2
= data
[y1
] + (3 * x2
);
2512 unsigned char *v3
= data
[y2
] + (3 * x2
);
2513 unsigned char *v4
= data
[y2
] + (3 * x1
);
2515 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2519 *(dst
++) = (unsigned char)
2520 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2521 w3
* *(v3
++) + w4
* *(v4
++)) /
2522 (w1
+ w2
+ w3
+ w4
) );
2523 *(dst
++) = (unsigned char)
2524 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2525 w3
* *(v3
++) + w4
* *(v4
++)) /
2526 (w1
+ w2
+ w3
+ w4
) );
2527 *(dst
++) = (unsigned char)
2528 ( (w1
* *v1
+ w2
* *v2
+
2529 w3
* *v3
+ w4
* *v4
) /
2530 (w1
+ w2
+ w3
+ w4
) );
2534 v1
= alpha
[y1
] + (x1
);
2535 v2
= alpha
[y1
] + (x2
);
2536 v3
= alpha
[y2
] + (x2
);
2537 v4
= alpha
[y2
] + (x1
);
2539 *(alpha_dst
++) = (unsigned char)
2540 ( (w1
* *v1
+ w2
* *v2
+
2541 w3
* *v3
+ w4
* *v4
) /
2542 (w1
+ w2
+ w3
+ w4
) );
2558 else // not interpolating
2560 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2562 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2564 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2566 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2567 const int ys
= wxCint (src
.y
); // closest integer
2569 if (0 <= xs
&& xs
< GetWidth() &&
2570 0 <= ys
&& ys
< GetHeight())
2572 unsigned char *p
= data
[ys
] + (3 * xs
);
2578 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2587 *(alpha_dst
++) = 255;
2605 // A module to allow wxImage initialization/cleanup
2606 // without calling these functions from app.cpp or from
2607 // the user's application.
2609 class wxImageModule
: public wxModule
2611 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2614 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2615 void OnExit() { wxImage::CleanUpHandlers(); };
2618 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2621 #endif // wxUSE_IMAGE