1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
39 #include "wx/xpmdecod.h"
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 class wxImageRefData
: public wxObjectRefData
58 virtual ~wxImageRefData();
62 unsigned char *m_data
;
65 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
67 // alpha channel data, may be NULL for the formats without alpha support
68 unsigned char *m_alpha
;
72 // if true, m_data is pointer to static data and shouldn't be freed
75 // same as m_static but for m_alpha
80 #endif // wxUSE_PALETTE
82 wxArrayString m_optionNames
;
83 wxArrayString m_optionValues
;
85 DECLARE_NO_COPY_CLASS(wxImageRefData
)
88 wxImageRefData::wxImageRefData()
93 m_alpha
= (unsigned char *) NULL
;
102 m_staticAlpha
= false;
105 wxImageRefData::~wxImageRefData()
109 if ( !m_staticAlpha
)
113 wxList
wxImage::sm_handlers
;
117 //-----------------------------------------------------------------------------
119 #define M_IMGDATA ((wxImageRefData *)m_refData)
121 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
123 wxImage::wxImage( int width
, int height
, bool clear
)
125 Create( width
, height
, clear
);
128 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
130 Create( width
, height
, data
, static_data
);
133 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
135 Create( width
, height
, data
, alpha
, static_data
);
138 wxImage::wxImage( const wxString
& name
, long type
, int index
)
140 LoadFile( name
, type
, index
);
143 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
145 LoadFile( name
, mimetype
, index
);
149 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
151 LoadFile( stream
, type
, index
);
154 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
156 LoadFile( stream
, mimetype
, index
);
158 #endif // wxUSE_STREAMS
160 wxImage::wxImage( const wxImage
& image
)
166 wxImage::wxImage( const wxImage
* image
)
168 if (image
) Ref(*image
);
171 wxImage::wxImage( const char** xpmData
)
176 wxImage::wxImage( char** xpmData
)
178 Create((const char**) xpmData
);
181 bool wxImage::Create( const char** xpmData
)
186 wxXPMDecoder decoder
;
187 (*this) = decoder
.ReadData(xpmData
);
194 bool wxImage::Create( int width
, int height
, bool clear
)
198 m_refData
= new wxImageRefData();
200 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
201 if (!M_IMGDATA
->m_data
)
208 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
210 M_IMGDATA
->m_width
= width
;
211 M_IMGDATA
->m_height
= height
;
212 M_IMGDATA
->m_ok
= true;
217 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
221 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
223 m_refData
= new wxImageRefData();
225 M_IMGDATA
->m_data
= data
;
226 M_IMGDATA
->m_width
= width
;
227 M_IMGDATA
->m_height
= height
;
228 M_IMGDATA
->m_ok
= true;
229 M_IMGDATA
->m_static
= static_data
;
234 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
238 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
240 m_refData
= new wxImageRefData();
242 M_IMGDATA
->m_data
= data
;
243 M_IMGDATA
->m_alpha
= alpha
;
244 M_IMGDATA
->m_width
= width
;
245 M_IMGDATA
->m_height
= height
;
246 M_IMGDATA
->m_ok
= true;
247 M_IMGDATA
->m_static
= static_data
;
252 void wxImage::Destroy()
257 wxImage
wxImage::Copy() const
261 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
263 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
265 unsigned char *data
= image
.GetData();
267 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
269 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
270 image
.SetMask( M_IMGDATA
->m_hasMask
);
272 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
274 // also copy the image options
275 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
276 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
277 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
282 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
284 if( xFactor
== 1 && yFactor
== 1 )
289 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
291 // can't scale to/from 0 size
292 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
293 wxT("invalid new image size") );
295 long old_height
= M_IMGDATA
->m_height
,
296 old_width
= M_IMGDATA
->m_width
;
298 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
299 wxT("invalid old image size") );
301 long width
= old_width
/ xFactor
;
302 long height
= old_height
/ yFactor
;
304 image
.Create( width
, height
, false );
306 char unsigned *data
= image
.GetData();
308 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
310 bool hasMask
= false ;
311 unsigned char maskRed
= 0;
312 unsigned char maskGreen
= 0;
313 unsigned char maskBlue
=0 ;
315 unsigned char *source_data
= M_IMGDATA
->m_data
;
316 unsigned char *target_data
= data
;
317 unsigned char *source_alpha
= 0 ;
318 unsigned char *target_alpha
= 0 ;
319 if (M_IMGDATA
->m_hasMask
)
322 maskRed
= M_IMGDATA
->m_maskRed
;
323 maskGreen
= M_IMGDATA
->m_maskGreen
;
324 maskBlue
=M_IMGDATA
->m_maskBlue
;
326 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
327 M_IMGDATA
->m_maskGreen
,
328 M_IMGDATA
->m_maskBlue
);
332 source_alpha
= M_IMGDATA
->m_alpha
;
336 target_alpha
= image
.GetAlpha() ;
340 for (long y
= 0; y
< height
; y
++)
342 for (long x
= 0; x
< width
; x
++)
344 unsigned long avgRed
= 0 ;
345 unsigned long avgGreen
= 0;
346 unsigned long avgBlue
= 0;
347 unsigned long avgAlpha
= 0 ;
348 unsigned long counter
= 0 ;
350 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
352 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
353 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
355 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
356 unsigned char red
= pixel
[0] ;
357 unsigned char green
= pixel
[1] ;
358 unsigned char blue
= pixel
[2] ;
359 unsigned char alpha
= 255 ;
361 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
362 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
377 *(target_data
++) = M_IMGDATA
->m_maskRed
;
378 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
379 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
384 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
385 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
386 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
387 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
392 // In case this is a cursor, make sure the hotspot is scalled accordingly:
393 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
394 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
395 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
396 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
397 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
398 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
403 wxImage
wxImage::Scale( int width
, int height
) const
407 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
409 // can't scale to/from 0 size
410 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
411 wxT("invalid new image size") );
413 long old_height
= M_IMGDATA
->m_height
,
414 old_width
= M_IMGDATA
->m_width
;
415 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
416 wxT("invalid old image size") );
418 if ( old_width
% width
== 0 && old_width
>= width
&&
419 old_height
% height
== 0 && old_height
>= height
)
421 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
423 image
.Create( width
, height
, false );
425 unsigned char *data
= image
.GetData();
427 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
429 unsigned char *source_data
= M_IMGDATA
->m_data
;
430 unsigned char *target_data
= data
;
431 unsigned char *source_alpha
= 0 ;
432 unsigned char *target_alpha
= 0 ;
434 if (M_IMGDATA
->m_hasMask
)
436 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
437 M_IMGDATA
->m_maskGreen
,
438 M_IMGDATA
->m_maskBlue
);
442 source_alpha
= M_IMGDATA
->m_alpha
;
446 target_alpha
= image
.GetAlpha() ;
450 long x_delta
= (old_width
<<16) / width
;
451 long y_delta
= (old_height
<<16) / height
;
453 unsigned char* dest_pixel
= target_data
;
456 for ( long j
= 0; j
< height
; j
++ )
458 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
459 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
462 for ( long i
= 0; i
< width
; i
++ )
464 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
465 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
466 dest_pixel
[0] = src_pixel
[0];
467 dest_pixel
[1] = src_pixel
[1];
468 dest_pixel
[2] = src_pixel
[2];
471 *(target_alpha
++) = *src_alpha_pixel
;
478 // In case this is a cursor, make sure the hotspot is scalled accordingly:
479 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
480 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
481 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
482 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
483 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
484 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
489 wxImage
wxImage::Rotate90( bool clockwise
) const
493 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
495 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
497 unsigned char *data
= image
.GetData();
499 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
501 unsigned char *source_data
= M_IMGDATA
->m_data
;
502 unsigned char *target_data
;
503 unsigned char *alpha_data
= 0 ;
504 unsigned char *source_alpha
= 0 ;
505 unsigned char *target_alpha
= 0 ;
507 if (M_IMGDATA
->m_hasMask
)
509 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
513 source_alpha
= M_IMGDATA
->m_alpha
;
517 alpha_data
= image
.GetAlpha() ;
521 long height
= M_IMGDATA
->m_height
;
522 long width
= M_IMGDATA
->m_width
;
524 for (long j
= 0; j
< height
; j
++)
526 for (long i
= 0; i
< width
; i
++)
530 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
532 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
536 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
538 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
540 memcpy( target_data
, source_data
, 3 );
545 memcpy( target_alpha
, source_alpha
, 1 );
554 wxImage
wxImage::Mirror( bool horizontally
) const
558 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
560 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
562 unsigned char *data
= image
.GetData();
564 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
566 if (M_IMGDATA
->m_hasMask
)
567 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
569 long height
= M_IMGDATA
->m_height
;
570 long width
= M_IMGDATA
->m_width
;
572 unsigned char *source_data
= M_IMGDATA
->m_data
;
573 unsigned char *target_data
;
577 for (long j
= 0; j
< height
; j
++)
580 target_data
= data
-3;
581 for (long i
= 0; i
< width
; i
++)
583 memcpy( target_data
, source_data
, 3 );
591 for (long i
= 0; i
< height
; i
++)
593 target_data
= data
+ 3*width
*(height
-1-i
);
594 memcpy( target_data
, source_data
, (size_t)3*width
);
595 source_data
+= 3*width
;
602 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
606 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
608 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
609 image
, wxT("invalid subimage size") );
611 int subwidth
=rect
.GetWidth();
612 const int subheight
=rect
.GetHeight();
614 image
.Create( subwidth
, subheight
, false );
616 unsigned char *subdata
= image
.GetData(), *data
=GetData();
618 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
620 if (M_IMGDATA
->m_hasMask
)
621 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
623 const int subleft
=3*rect
.GetLeft();
624 const int width
=3*GetWidth();
627 data
+=rect
.GetTop()*width
+subleft
;
629 for (long j
= 0; j
< subheight
; ++j
)
631 memcpy( subdata
, data
, subwidth
);
639 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
640 int r_
, int g_
, int b_
) const
644 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
645 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
647 int width
= GetWidth(), height
= GetHeight();
648 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
650 unsigned char r
= (unsigned char)r_
;
651 unsigned char g
= (unsigned char)g_
;
652 unsigned char b
= (unsigned char)b_
;
653 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
655 GetOrFindMaskColour( &r
, &g
, &b
);
656 image
.SetMaskColour(r
, g
, b
);
659 image
.SetRGB(wxRect(), r
, g
, b
);
661 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
662 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
664 subRect
.Intersect(finalRect
);
666 if (!subRect
.IsEmpty())
668 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
669 image
.Paste(*this, pos
.x
, pos
.y
);
671 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
677 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
679 wxCHECK_RET( Ok(), wxT("invalid image") );
680 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
684 int width
= image
.GetWidth();
685 int height
= image
.GetHeight();
698 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
699 width
= M_IMGDATA
->m_width
- (x
+xx
);
700 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
701 height
= M_IMGDATA
->m_height
- (y
+yy
);
703 if (width
< 1) return;
704 if (height
< 1) return;
706 if ((!HasMask() && !image
.HasMask()) ||
707 (HasMask() && !image
.HasMask()) ||
708 ((HasMask() && image
.HasMask() &&
709 (GetMaskRed()==image
.GetMaskRed()) &&
710 (GetMaskGreen()==image
.GetMaskGreen()) &&
711 (GetMaskBlue()==image
.GetMaskBlue()))))
714 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
715 int source_step
= image
.GetWidth()*3;
717 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
718 int target_step
= M_IMGDATA
->m_width
*3;
719 for (int j
= 0; j
< height
; j
++)
721 memcpy( target_data
, source_data
, width
);
722 source_data
+= source_step
;
723 target_data
+= target_step
;
728 if (!HasMask() && image
.HasMask())
730 unsigned char r
= image
.GetMaskRed();
731 unsigned char g
= image
.GetMaskGreen();
732 unsigned char b
= image
.GetMaskBlue();
735 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
736 int source_step
= image
.GetWidth()*3;
738 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
739 int target_step
= M_IMGDATA
->m_width
*3;
741 for (int j
= 0; j
< height
; j
++)
743 for (int i
= 0; i
< width
; i
+=3)
745 if ((source_data
[i
] != r
) &&
746 (source_data
[i
+1] != g
) &&
747 (source_data
[i
+2] != b
))
749 memcpy( target_data
+i
, source_data
+i
, 3 );
752 source_data
+= source_step
;
753 target_data
+= target_step
;
758 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
759 unsigned char r2
, unsigned char g2
, unsigned char b2
)
761 wxCHECK_RET( Ok(), wxT("invalid image") );
763 unsigned char *data
= GetData();
765 const int w
= GetWidth();
766 const int h
= GetHeight();
768 for (int j
= 0; j
< h
; j
++)
769 for (int i
= 0; i
< w
; i
++)
771 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
781 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
785 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
787 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
789 unsigned char *data
= image
.GetData();
791 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
793 if (M_IMGDATA
->m_hasMask
)
795 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
796 M_IMGDATA
->m_maskBlue
== b
)
797 image
.SetMaskColour( 255, 255, 255 );
799 image
.SetMaskColour( 0, 0, 0 );
802 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
804 unsigned char *srcd
= M_IMGDATA
->m_data
;
805 unsigned char *tard
= image
.GetData();
807 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
809 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
810 tard
[0] = tard
[1] = tard
[2] = 255;
812 tard
[0] = tard
[1] = tard
[2] = 0;
818 int wxImage::GetWidth() const
820 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
822 return M_IMGDATA
->m_width
;
825 int wxImage::GetHeight() const
827 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
829 return M_IMGDATA
->m_height
;
832 long wxImage::XYToIndex(int x
, int y
) const
836 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
838 return y
*M_IMGDATA
->m_width
+ x
;
844 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
846 long pos
= XYToIndex(x
, y
);
847 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
851 M_IMGDATA
->m_data
[ pos
] = r
;
852 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
853 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
856 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
858 wxCHECK_RET( Ok(), wxT("invalid image") );
861 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
862 if ( rect
== wxRect() )
868 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
869 imageRect
.Inside(rect
.GetBottomRight()),
870 wxT("invalid bounding rectangle") );
873 int x1
= rect
.GetLeft(),
875 x2
= rect
.GetRight() + 1,
876 y2
= rect
.GetBottom() + 1;
878 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
879 int x
, y
, width
= GetWidth();
880 for (y
= y1
; y
< y2
; y
++)
882 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
883 for (x
= x1
; x
< x2
; x
++)
892 unsigned char wxImage::GetRed( int x
, int y
) const
894 long pos
= XYToIndex(x
, y
);
895 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
899 return M_IMGDATA
->m_data
[pos
];
902 unsigned char wxImage::GetGreen( int x
, int y
) const
904 long pos
= XYToIndex(x
, y
);
905 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
909 return M_IMGDATA
->m_data
[pos
+1];
912 unsigned char wxImage::GetBlue( int x
, int y
) const
914 long pos
= XYToIndex(x
, y
);
915 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
919 return M_IMGDATA
->m_data
[pos
+2];
922 bool wxImage::Ok() const
924 // image of 0 width or height can't be considered ok - at least because it
925 // causes crashes in ConvertToBitmap() if we don't catch it in time
926 wxImageRefData
*data
= M_IMGDATA
;
927 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
930 unsigned char *wxImage::GetData() const
932 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
934 return M_IMGDATA
->m_data
;
937 void wxImage::SetData( unsigned char *data
, bool static_data
)
939 wxCHECK_RET( Ok(), wxT("invalid image") );
941 wxImageRefData
*newRefData
= new wxImageRefData();
943 newRefData
->m_width
= M_IMGDATA
->m_width
;
944 newRefData
->m_height
= M_IMGDATA
->m_height
;
945 newRefData
->m_data
= data
;
946 newRefData
->m_ok
= true;
947 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
948 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
949 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
950 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
951 newRefData
->m_static
= static_data
;
955 m_refData
= newRefData
;
958 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
960 wxImageRefData
*newRefData
= new wxImageRefData();
964 newRefData
->m_width
= new_width
;
965 newRefData
->m_height
= new_height
;
966 newRefData
->m_data
= data
;
967 newRefData
->m_ok
= true;
968 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
969 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
970 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
971 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
975 newRefData
->m_width
= new_width
;
976 newRefData
->m_height
= new_height
;
977 newRefData
->m_data
= data
;
978 newRefData
->m_ok
= true;
980 newRefData
->m_static
= static_data
;
984 m_refData
= newRefData
;
987 // ----------------------------------------------------------------------------
988 // alpha channel support
989 // ----------------------------------------------------------------------------
991 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
993 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
995 long pos
= XYToIndex(x
, y
);
996 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
998 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1001 unsigned char wxImage::GetAlpha(int x
, int y
) const
1003 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1005 long pos
= XYToIndex(x
, y
);
1006 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1008 return M_IMGDATA
->m_alpha
[pos
];
1012 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1016 const int w
= M_IMGDATA
->m_width
;
1017 const int h
= M_IMGDATA
->m_height
;
1019 unsigned char *alpha
= GetAlpha();
1020 unsigned char *data
= GetData();
1022 for ( int y
= 0; y
< h
; y
++ )
1024 for ( int x
= 0; x
< w
; x
++ )
1036 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1038 wxCHECK_RET( Ok(), wxT("invalid image") );
1042 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1045 free(M_IMGDATA
->m_alpha
);
1046 M_IMGDATA
->m_alpha
= alpha
;
1047 M_IMGDATA
->m_staticAlpha
= static_data
;
1050 unsigned char *wxImage::GetAlpha() const
1052 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1054 return M_IMGDATA
->m_alpha
;
1057 void wxImage::InitAlpha()
1059 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1061 // initialize memory for alpha channel
1064 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1065 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1069 // use the mask to initialize the alpha channel.
1070 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1072 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1073 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1074 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1075 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1079 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1080 ? wxIMAGE_ALPHA_TRANSPARENT
1081 : wxIMAGE_ALPHA_OPAQUE
;
1084 M_IMGDATA
->m_hasMask
= false;
1088 // make the image fully opaque
1089 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1093 // ----------------------------------------------------------------------------
1095 // ----------------------------------------------------------------------------
1097 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1099 wxCHECK_RET( Ok(), wxT("invalid image") );
1101 M_IMGDATA
->m_maskRed
= r
;
1102 M_IMGDATA
->m_maskGreen
= g
;
1103 M_IMGDATA
->m_maskBlue
= b
;
1104 M_IMGDATA
->m_hasMask
= true;
1107 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1109 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1111 if (M_IMGDATA
->m_hasMask
)
1113 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1114 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1115 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1120 FindFirstUnusedColour(r
, g
, b
);
1125 unsigned char wxImage::GetMaskRed() const
1127 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1129 return M_IMGDATA
->m_maskRed
;
1132 unsigned char wxImage::GetMaskGreen() const
1134 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1136 return M_IMGDATA
->m_maskGreen
;
1139 unsigned char wxImage::GetMaskBlue() const
1141 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1143 return M_IMGDATA
->m_maskBlue
;
1146 void wxImage::SetMask( bool mask
)
1148 wxCHECK_RET( Ok(), wxT("invalid image") );
1150 M_IMGDATA
->m_hasMask
= mask
;
1153 bool wxImage::HasMask() const
1155 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1157 return M_IMGDATA
->m_hasMask
;
1160 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1162 long pos
= XYToIndex(x
, y
);
1163 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1166 if ( M_IMGDATA
->m_hasMask
)
1168 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1169 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1170 p
[1] == M_IMGDATA
->m_maskGreen
&&
1171 p
[2] == M_IMGDATA
->m_maskBlue
)
1178 if ( M_IMGDATA
->m_alpha
)
1180 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1182 // transparent enough
1191 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1192 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1194 // check that the images are the same size
1195 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1197 wxLogError( _("Image and mask have different sizes.") );
1201 // find unused colour
1202 unsigned char r
,g
,b
;
1203 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1205 wxLogError( _("No unused colour in image being masked.") );
1209 unsigned char *imgdata
= GetData();
1210 unsigned char *maskdata
= mask
.GetData();
1212 const int w
= GetWidth();
1213 const int h
= GetHeight();
1215 for (int j
= 0; j
< h
; j
++)
1217 for (int i
= 0; i
< w
; i
++)
1219 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1230 SetMaskColour(r
, g
, b
);
1236 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1241 unsigned char mr
, mg
, mb
;
1242 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1244 wxLogError( _("No unused colour in image being masked.") );
1249 SetMaskColour(mr
, mg
, mb
);
1251 unsigned char *imgdata
= GetData();
1252 unsigned char *alphadata
= GetAlpha();
1255 int h
= GetHeight();
1257 for (int y
= 0; y
< h
; y
++)
1259 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1261 if (*alphadata
< threshold
)
1270 free(M_IMGDATA
->m_alpha
);
1271 M_IMGDATA
->m_alpha
= NULL
;
1276 // ----------------------------------------------------------------------------
1277 // Palette functions
1278 // ----------------------------------------------------------------------------
1282 bool wxImage::HasPalette() const
1287 return M_IMGDATA
->m_palette
.Ok();
1290 const wxPalette
& wxImage::GetPalette() const
1292 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1294 return M_IMGDATA
->m_palette
;
1297 void wxImage::SetPalette(const wxPalette
& palette
)
1299 wxCHECK_RET( Ok(), wxT("invalid image") );
1301 M_IMGDATA
->m_palette
= palette
;
1304 #endif // wxUSE_PALETTE
1306 // ----------------------------------------------------------------------------
1307 // Option functions (arbitrary name/value mapping)
1308 // ----------------------------------------------------------------------------
1310 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1312 wxCHECK_RET( Ok(), wxT("invalid image") );
1314 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1315 if (idx
== wxNOT_FOUND
)
1317 M_IMGDATA
->m_optionNames
.Add(name
);
1318 M_IMGDATA
->m_optionValues
.Add(value
);
1322 M_IMGDATA
->m_optionNames
[idx
] = name
;
1323 M_IMGDATA
->m_optionValues
[idx
] = value
;
1327 void wxImage::SetOption(const wxString
& name
, int value
)
1330 valStr
.Printf(wxT("%d"), value
);
1331 SetOption(name
, valStr
);
1334 wxString
wxImage::GetOption(const wxString
& name
) const
1336 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1338 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1339 if (idx
== wxNOT_FOUND
)
1340 return wxEmptyString
;
1342 return M_IMGDATA
->m_optionValues
[idx
];
1345 int wxImage::GetOptionInt(const wxString
& name
) const
1347 return wxAtoi(GetOption(name
));
1350 bool wxImage::HasOption(const wxString
& name
) const
1352 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1354 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1357 // ----------------------------------------------------------------------------
1359 // ----------------------------------------------------------------------------
1361 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1364 if (wxFileExists(filename
))
1366 wxFileInputStream
stream(filename
);
1367 wxBufferedInputStream
bstream( stream
);
1368 return LoadFile(bstream
, type
, index
);
1372 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1376 #else // !wxUSE_STREAMS
1378 #endif // wxUSE_STREAMS
1381 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1384 if (wxFileExists(filename
))
1386 wxFileInputStream
stream(filename
);
1387 wxBufferedInputStream
bstream( stream
);
1388 return LoadFile(bstream
, mimetype
, index
);
1392 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1396 #else // !wxUSE_STREAMS
1398 #endif // wxUSE_STREAMS
1403 bool wxImage::SaveFile( const wxString
& filename
) const
1405 wxString ext
= filename
.AfterLast('.').Lower();
1407 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1410 SaveFile(filename
, pHandler
->GetType());
1414 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1419 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1422 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1424 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1426 wxFileOutputStream
stream(filename
);
1428 if ( stream
.IsOk() )
1430 wxBufferedOutputStream
bstream( stream
);
1431 return SaveFile(bstream
, type
);
1433 #endif // wxUSE_STREAMS
1438 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1441 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1443 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1445 wxFileOutputStream
stream(filename
);
1447 if ( stream
.IsOk() )
1449 wxBufferedOutputStream
bstream( stream
);
1450 return SaveFile(bstream
, mimetype
);
1452 #endif // wxUSE_STREAMS
1457 bool wxImage::CanRead( const wxString
&name
)
1460 wxFileInputStream
stream(name
);
1461 return CanRead(stream
);
1467 int wxImage::GetImageCount( const wxString
&name
, long type
)
1470 wxFileInputStream
stream(name
);
1472 return GetImageCount(stream
, type
);
1480 bool wxImage::CanRead( wxInputStream
&stream
)
1482 const wxList
& list
= GetHandlers();
1484 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1486 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1487 if (handler
->CanRead( stream
))
1494 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1496 wxImageHandler
*handler
;
1498 if ( type
== wxBITMAP_TYPE_ANY
)
1500 wxList
&list
=GetHandlers();
1502 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1504 handler
=(wxImageHandler
*)node
->GetData();
1505 if ( handler
->CanRead(stream
) )
1506 return handler
->GetImageCount(stream
);
1510 wxLogWarning(_("No handler found for image type."));
1514 handler
= FindHandler(type
);
1518 wxLogWarning(_("No image handler for type %d defined."), type
);
1522 if ( handler
->CanRead(stream
) )
1524 return handler
->GetImageCount(stream
);
1528 wxLogError(_("Image file is not of type %d."), type
);
1533 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1537 m_refData
= new wxImageRefData
;
1539 wxImageHandler
*handler
;
1541 if ( type
== wxBITMAP_TYPE_ANY
)
1543 wxList
&list
=GetHandlers();
1545 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1547 handler
=(wxImageHandler
*)node
->GetData();
1548 if ( handler
->CanRead(stream
) )
1549 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1553 wxLogWarning( _("No handler found for image type.") );
1557 handler
= FindHandler(type
);
1561 wxLogWarning( _("No image handler for type %d defined."), type
);
1566 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1569 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1573 m_refData
= new wxImageRefData
;
1575 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1579 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1584 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1587 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1589 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1591 wxImageHandler
*handler
= FindHandler(type
);
1594 wxLogWarning( _("No image handler for type %d defined."), type
);
1599 return handler
->SaveFile( (wxImage
*)this, stream
);
1602 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1604 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1606 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1609 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1614 return handler
->SaveFile( (wxImage
*)this, stream
);
1616 #endif // wxUSE_STREAMS
1618 // ----------------------------------------------------------------------------
1619 // image I/O handlers
1620 // ----------------------------------------------------------------------------
1622 void wxImage::AddHandler( wxImageHandler
*handler
)
1624 // Check for an existing handler of the type being added.
1625 if (FindHandler( handler
->GetType() ) == 0)
1627 sm_handlers
.Append( handler
);
1631 // This is not documented behaviour, merely the simplest 'fix'
1632 // for preventing duplicate additions. If someone ever has
1633 // a good reason to add and remove duplicate handlers (and they
1634 // may) we should probably refcount the duplicates.
1635 // also an issue in InsertHandler below.
1637 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1638 handler
->GetName().c_str() );
1643 void wxImage::InsertHandler( wxImageHandler
*handler
)
1645 // Check for an existing handler of the type being added.
1646 if (FindHandler( handler
->GetType() ) == 0)
1648 sm_handlers
.Insert( handler
);
1652 // see AddHandler for additional comments.
1653 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1654 handler
->GetName().c_str() );
1659 bool wxImage::RemoveHandler( const wxString
& name
)
1661 wxImageHandler
*handler
= FindHandler(name
);
1664 sm_handlers
.DeleteObject(handler
);
1672 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1674 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1677 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1678 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1680 node
= node
->GetNext();
1685 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1687 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1690 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1691 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1692 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1694 node
= node
->GetNext();
1699 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1701 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1704 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1705 if (handler
->GetType() == bitmapType
) return handler
;
1706 node
= node
->GetNext();
1711 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1713 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1716 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1717 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1718 node
= node
->GetNext();
1723 void wxImage::InitStandardHandlers()
1726 AddHandler(new wxBMPHandler
);
1727 #endif // wxUSE_STREAMS
1730 void wxImage::CleanUpHandlers()
1732 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1735 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1736 wxList::compatibility_iterator next
= node
->GetNext();
1741 sm_handlers
.Clear();
1744 wxString
wxImage::GetImageExtWildcard()
1748 wxList
& Handlers
= wxImage::GetHandlers();
1749 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1752 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1753 fmts
+= wxT("*.") + Handler
->GetExtension();
1754 Node
= Node
->GetNext();
1755 if ( Node
) fmts
+= wxT(";");
1758 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1761 //-----------------------------------------------------------------------------
1763 //-----------------------------------------------------------------------------
1765 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1768 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1773 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1778 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1783 bool wxImageHandler::CanRead( const wxString
& name
)
1785 if (wxFileExists(name
))
1787 wxFileInputStream
stream(name
);
1788 return CanRead(stream
);
1791 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1796 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1798 wxFileOffset posOld
= stream
.TellI();
1799 if ( posOld
== wxInvalidOffset
)
1801 // can't test unseekable stream
1805 bool ok
= DoCanRead(stream
);
1807 // restore the old position to be able to test other formats and so on
1808 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1810 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1812 // reading would fail anyhow as we're not at the right position
1819 #endif // wxUSE_STREAMS
1821 // ----------------------------------------------------------------------------
1822 // image histogram stuff
1823 // ----------------------------------------------------------------------------
1826 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1831 unsigned char g2
) const
1833 unsigned long key
= MakeKey(r2
, g2
, b2
);
1835 while ( find(key
) != end() )
1837 // color already used
1849 wxLogError(_("No unused colour in image.") );
1855 key
= MakeKey(r2
, g2
, b2
);
1869 wxImage::FindFirstUnusedColour(unsigned char *r
,
1874 unsigned char g2
) const
1876 wxImageHistogram histogram
;
1878 ComputeHistogram(histogram
);
1880 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1886 // Counts and returns the number of different colours. Optionally stops
1887 // when it exceeds 'stopafter' different colours. This is useful, for
1888 // example, to see if the image can be saved as 8-bit (256 colour or
1889 // less, in this case it would be invoked as CountColours(256)). Default
1890 // value for stopafter is -1 (don't care).
1892 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1896 unsigned char r
, g
, b
;
1898 unsigned long size
, nentries
, key
;
1901 size
= GetWidth() * GetHeight();
1904 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1909 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1911 if (h
.Get(key
) == NULL
)
1922 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1924 unsigned char *p
= GetData();
1925 unsigned long nentries
= 0;
1929 const unsigned long size
= GetWidth() * GetHeight();
1931 unsigned char r
, g
, b
;
1932 for ( unsigned long n
= 0; n
< size
; n
++ )
1938 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1940 if ( entry
.value
++ == 0 )
1941 entry
.index
= nentries
++;
1948 * Rotation code by Carlos Moreno
1951 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1952 // does exactly the same thing. And I also got rid of wxRotationPixel
1953 // bacause of potential problems in architectures where alignment
1954 // is an issue, so I had to rewrite parts of the code.
1956 static const double gs_Epsilon
= 1e-10;
1958 static inline int wxCint (double x
)
1960 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1964 // Auxiliary function to rotate a point (x,y) with respect to point p0
1965 // make it inline and use a straight return to facilitate optimization
1966 // also, the function receives the sine and cosine of the angle to avoid
1967 // repeating the time-consuming calls to these functions -- sin/cos can
1968 // be computed and stored in the calling function.
1970 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1972 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1973 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1976 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1978 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1981 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1984 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1986 bool has_alpha
= HasAlpha();
1988 // Create pointer-based array to accelerate access to wxImage's data
1989 unsigned char ** data
= new unsigned char * [GetHeight()];
1990 data
[0] = GetData();
1991 for (i
= 1; i
< GetHeight(); i
++)
1992 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1994 // Same for alpha channel
1995 unsigned char ** alpha
= NULL
;
1998 alpha
= new unsigned char * [GetHeight()];
1999 alpha
[0] = GetAlpha();
2000 for (i
= 1; i
< GetHeight(); i
++)
2001 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2004 // precompute coefficients for rotation formula
2005 // (sine and cosine of the angle)
2006 const double cos_angle
= cos(angle
);
2007 const double sin_angle
= sin(angle
);
2009 // Create new Image to store the result
2010 // First, find rectangle that covers the rotated image; to do that,
2011 // rotate the four corners
2013 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2015 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2016 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2017 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2018 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2020 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2021 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2022 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2023 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2025 // Create rotated image
2026 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
2027 // With alpha channel
2031 if (offset_after_rotation
!= NULL
)
2033 *offset_after_rotation
= wxPoint (x1
, y1
);
2036 // GRG: The rotated (destination) image is always accessed
2037 // sequentially, so there is no need for a pointer-based
2038 // array here (and in fact it would be slower).
2040 unsigned char * dst
= rotated
.GetData();
2042 unsigned char * alpha_dst
= NULL
;
2044 alpha_dst
= rotated
.GetAlpha();
2046 // GRG: if the original image has a mask, use its RGB values
2047 // as the blank pixel, else, fall back to default (black).
2049 unsigned char blank_r
= 0;
2050 unsigned char blank_g
= 0;
2051 unsigned char blank_b
= 0;
2055 blank_r
= GetMaskRed();
2056 blank_g
= GetMaskGreen();
2057 blank_b
= GetMaskBlue();
2058 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2061 // Now, for each point of the rotated image, find where it came from, by
2062 // performing an inverse rotation (a rotation of -angle) and getting the
2063 // pixel at those coordinates
2065 // GRG: I've taken the (interpolating) test out of the loops, so that
2066 // it is done only once, instead of repeating it for each pixel.
2071 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2073 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2075 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2077 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2078 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2080 // interpolate using the 4 enclosing grid-points. Those
2081 // points can be obtained using floor and ceiling of the
2082 // exact coordinates of the point
2083 // C.M. 2000-02-17: when the point is near the border, special care is required.
2087 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2089 x1
= wxCint(floor(src
.x
));
2090 x2
= wxCint(ceil(src
.x
));
2092 else // else means that x is near one of the borders (0 or width-1)
2094 x1
= x2
= wxCint (src
.x
);
2097 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2099 y1
= wxCint(floor(src
.y
));
2100 y2
= wxCint(ceil(src
.y
));
2104 y1
= y2
= wxCint (src
.y
);
2107 // get four points and the distances (square of the distance,
2108 // for efficiency reasons) for the interpolation formula
2110 // GRG: Do not calculate the points until they are
2111 // really needed -- this way we can calculate
2112 // just one, instead of four, if d1, d2, d3
2113 // or d4 are < gs_Epsilon
2115 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2116 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2117 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2118 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2120 // Now interpolate as a weighted average of the four surrounding
2121 // points, where the weights are the distances to each of those points
2123 // If the point is exactly at one point of the grid of the source
2124 // image, then don't interpolate -- just assign the pixel
2126 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2128 unsigned char *p
= data
[y1
] + (3 * x1
);
2135 unsigned char *p
= alpha
[y1
] + x1
;
2136 *(alpha_dst
++) = *p
;
2139 else if (d2
< gs_Epsilon
)
2141 unsigned char *p
= data
[y1
] + (3 * x2
);
2148 unsigned char *p
= alpha
[y1
] + x2
;
2149 *(alpha_dst
++) = *p
;
2152 else if (d3
< gs_Epsilon
)
2154 unsigned char *p
= data
[y2
] + (3 * x2
);
2161 unsigned char *p
= alpha
[y2
] + x2
;
2162 *(alpha_dst
++) = *p
;
2165 else if (d4
< gs_Epsilon
)
2167 unsigned char *p
= data
[y2
] + (3 * x1
);
2174 unsigned char *p
= alpha
[y2
] + x1
;
2175 *(alpha_dst
++) = *p
;
2180 // weights for the weighted average are proportional to the inverse of the distance
2181 unsigned char *v1
= data
[y1
] + (3 * x1
);
2182 unsigned char *v2
= data
[y1
] + (3 * x2
);
2183 unsigned char *v3
= data
[y2
] + (3 * x2
);
2184 unsigned char *v4
= data
[y2
] + (3 * x1
);
2186 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2190 *(dst
++) = (unsigned char)
2191 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2192 w3
* *(v3
++) + w4
* *(v4
++)) /
2193 (w1
+ w2
+ w3
+ w4
) );
2194 *(dst
++) = (unsigned char)
2195 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2196 w3
* *(v3
++) + w4
* *(v4
++)) /
2197 (w1
+ w2
+ w3
+ w4
) );
2198 *(dst
++) = (unsigned char)
2199 ( (w1
* *v1
+ w2
* *v2
+
2200 w3
* *v3
+ w4
* *v4
) /
2201 (w1
+ w2
+ w3
+ w4
) );
2205 unsigned char *v1
= alpha
[y1
] + (x1
);
2206 unsigned char *v2
= alpha
[y1
] + (x2
);
2207 unsigned char *v3
= alpha
[y2
] + (x2
);
2208 unsigned char *v4
= alpha
[y2
] + (x1
);
2210 *(alpha_dst
++) = (unsigned char)
2211 ( (w1
* *v1
+ w2
* *v2
+
2212 w3
* *v3
+ w4
* *v4
) /
2213 (w1
+ w2
+ w3
+ w4
) );
2229 else // not interpolating
2231 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2233 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2235 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2237 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2238 const int ys
= wxCint (src
.y
); // closest integer
2240 if (0 <= xs
&& xs
< GetWidth() &&
2241 0 <= ys
&& ys
< GetHeight())
2243 unsigned char *p
= data
[ys
] + (3 * xs
);
2250 unsigned char *p
= alpha
[ys
] + (xs
);
2251 *(alpha_dst
++) = *p
;
2261 *(alpha_dst
++) = 255;
2279 // A module to allow wxImage initialization/cleanup
2280 // without calling these functions from app.cpp or from
2281 // the user's application.
2283 class wxImageModule
: public wxModule
2285 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2288 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2289 void OnExit() { wxImage::CleanUpHandlers(); };
2292 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2295 #endif // wxUSE_IMAGE