1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxImageRefData
: public wxObjectRefData
53 virtual ~wxImageRefData();
57 unsigned char *m_data
;
60 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 // alpha channel data, may be NULL for the formats without alpha support
63 unsigned char *m_alpha
;
70 #endif // wxUSE_PALETTE
72 wxArrayString m_optionNames
;
73 wxArrayString m_optionValues
;
75 DECLARE_NO_COPY_CLASS(wxImageRefData
)
78 wxImageRefData::wxImageRefData()
83 m_alpha
= (unsigned char *) NULL
;
94 wxImageRefData::~wxImageRefData()
102 wxList
wxImage::sm_handlers
;
106 //-----------------------------------------------------------------------------
108 #define M_IMGDATA ((wxImageRefData *)m_refData)
110 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
116 wxImage::wxImage( int width
, int height
, bool clear
)
118 Create( width
, height
, clear
);
121 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
123 Create( width
, height
, data
, static_data
);
126 wxImage::wxImage( const wxString
& name
, long type
, int index
)
128 LoadFile( name
, type
, index
);
131 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
133 LoadFile( name
, mimetype
, index
);
137 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
139 LoadFile( stream
, type
, index
);
142 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
144 LoadFile( stream
, mimetype
, index
);
146 #endif // wxUSE_STREAMS
148 wxImage::wxImage( const wxImage
& image
)
154 wxImage::wxImage( const wxImage
* image
)
156 if (image
) Ref(*image
);
159 bool wxImage::Create( int width
, int height
, bool clear
)
163 m_refData
= new wxImageRefData();
165 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
166 if (!M_IMGDATA
->m_data
)
173 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
175 M_IMGDATA
->m_width
= width
;
176 M_IMGDATA
->m_height
= height
;
177 M_IMGDATA
->m_ok
= true;
182 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
186 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
188 m_refData
= new wxImageRefData();
190 M_IMGDATA
->m_data
= data
;
191 M_IMGDATA
->m_width
= width
;
192 M_IMGDATA
->m_height
= height
;
193 M_IMGDATA
->m_ok
= true;
194 M_IMGDATA
->m_static
= static_data
;
199 void wxImage::Destroy()
204 wxImage
wxImage::Copy() const
208 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
210 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
212 unsigned char *data
= image
.GetData();
214 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
216 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
217 image
.SetMask( M_IMGDATA
->m_hasMask
);
219 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
224 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
226 if( xFactor
== 1 && yFactor
== 1 )
231 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
233 // can't scale to/from 0 size
234 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
235 wxT("invalid new image size") );
237 long old_height
= M_IMGDATA
->m_height
,
238 old_width
= M_IMGDATA
->m_width
;
240 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
241 wxT("invalid old image size") );
243 long width
= old_width
/ xFactor
;
244 long height
= old_height
/ yFactor
;
246 image
.Create( width
, height
, false );
248 char unsigned *data
= image
.GetData();
250 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
252 bool hasMask
= false ;
253 unsigned char maskRed
= 0;
254 unsigned char maskGreen
= 0;
255 unsigned char maskBlue
=0 ;
256 if (M_IMGDATA
->m_hasMask
)
259 maskRed
= M_IMGDATA
->m_maskRed
;
260 maskGreen
= M_IMGDATA
->m_maskGreen
;
261 maskBlue
=M_IMGDATA
->m_maskBlue
;
263 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
264 M_IMGDATA
->m_maskGreen
,
265 M_IMGDATA
->m_maskBlue
);
267 char unsigned *source_data
= M_IMGDATA
->m_data
;
268 char unsigned *target_data
= data
;
270 for (long y
= 0; y
< height
; y
++)
272 for (long x
= 0; x
< width
; x
++)
274 unsigned long avgRed
= 0 ;
275 unsigned long avgGreen
= 0;
276 unsigned long avgBlue
= 0;
277 unsigned long counter
= 0 ;
279 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
281 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
282 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
284 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
285 unsigned char red
= pixel
[0] ;
286 unsigned char green
= pixel
[1] ;
287 unsigned char blue
= pixel
[2] ;
288 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
299 *(target_data
++) = M_IMGDATA
->m_maskRed
;
300 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
301 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
305 *(target_data
++) = avgRed
/ counter
;
306 *(target_data
++) = avgGreen
/ counter
;
307 *(target_data
++) = avgBlue
/ counter
;
312 // In case this is a cursor, make sure the hotspot is scalled accordingly:
313 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
314 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
315 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
316 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
317 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
318 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
323 wxImage
wxImage::Scale( int width
, int height
) const
327 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
329 // can't scale to/from 0 size
330 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
331 wxT("invalid new image size") );
333 long old_height
= M_IMGDATA
->m_height
,
334 old_width
= M_IMGDATA
->m_width
;
335 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
336 wxT("invalid old image size") );
338 if ( old_width
% width
== 0 && old_width
>= width
&&
339 old_height
% height
== 0 && old_height
>= height
)
341 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
343 image
.Create( width
, height
, false );
345 unsigned char *data
= image
.GetData();
347 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
349 if (M_IMGDATA
->m_hasMask
)
351 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
352 M_IMGDATA
->m_maskGreen
,
353 M_IMGDATA
->m_maskBlue
);
356 unsigned char *source_data
= M_IMGDATA
->m_data
;
357 unsigned char *target_data
= data
;
359 long x_delta
= (old_width
<<16) / width
;
360 long y_delta
= (old_height
<<16) / height
;
362 unsigned char* dest_pixel
= target_data
;
365 for ( long j
= 0; j
< height
; j
++ )
367 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
370 for ( long i
= 0; i
< width
; i
++ )
372 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
373 dest_pixel
[0] = src_pixel
[0];
374 dest_pixel
[1] = src_pixel
[1];
375 dest_pixel
[2] = src_pixel
[2];
383 // In case this is a cursor, make sure the hotspot is scalled accordingly:
384 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
385 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
386 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
387 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
388 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
389 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
394 wxImage
wxImage::Rotate90( bool clockwise
) const
398 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
400 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
402 unsigned char *data
= image
.GetData();
404 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
406 if (M_IMGDATA
->m_hasMask
)
407 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
409 long height
= M_IMGDATA
->m_height
;
410 long width
= M_IMGDATA
->m_width
;
412 unsigned char *source_data
= M_IMGDATA
->m_data
;
413 unsigned char *target_data
;
415 for (long j
= 0; j
< height
; j
++)
417 for (long i
= 0; i
< width
; i
++)
420 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
422 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
423 memcpy( target_data
, source_data
, 3 );
431 wxImage
wxImage::Mirror( bool horizontally
) const
435 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
437 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
439 unsigned char *data
= image
.GetData();
441 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
443 if (M_IMGDATA
->m_hasMask
)
444 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
446 long height
= M_IMGDATA
->m_height
;
447 long width
= M_IMGDATA
->m_width
;
449 unsigned char *source_data
= M_IMGDATA
->m_data
;
450 unsigned char *target_data
;
454 for (long j
= 0; j
< height
; j
++)
457 target_data
= data
-3;
458 for (long i
= 0; i
< width
; i
++)
460 memcpy( target_data
, source_data
, 3 );
468 for (long i
= 0; i
< height
; i
++)
470 target_data
= data
+ 3*width
*(height
-1-i
);
471 memcpy( target_data
, source_data
, (size_t)3*width
);
472 source_data
+= 3*width
;
479 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
483 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
485 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
486 image
, wxT("invalid subimage size") );
488 int subwidth
=rect
.GetWidth();
489 const int subheight
=rect
.GetHeight();
491 image
.Create( subwidth
, subheight
, false );
493 unsigned char *subdata
= image
.GetData(), *data
=GetData();
495 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
497 if (M_IMGDATA
->m_hasMask
)
498 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
500 const int subleft
=3*rect
.GetLeft();
501 const int width
=3*GetWidth();
504 data
+=rect
.GetTop()*width
+subleft
;
506 for (long j
= 0; j
< subheight
; ++j
)
508 memcpy( subdata
, data
, subwidth
);
516 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
518 wxCHECK_RET( Ok(), wxT("invalid image") );
519 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
523 int width
= image
.GetWidth();
524 int height
= image
.GetHeight();
537 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
538 width
= M_IMGDATA
->m_width
- (x
+xx
);
539 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
540 height
= M_IMGDATA
->m_height
- (y
+yy
);
542 if (width
< 1) return;
543 if (height
< 1) return;
545 if ((!HasMask() && !image
.HasMask()) ||
546 ((HasMask() && image
.HasMask() &&
547 (GetMaskRed()==image
.GetMaskRed()) &&
548 (GetMaskGreen()==image
.GetMaskGreen()) &&
549 (GetMaskBlue()==image
.GetMaskBlue()))))
552 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
553 int source_step
= image
.GetWidth()*3;
555 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
556 int target_step
= M_IMGDATA
->m_width
*3;
557 for (int j
= 0; j
< height
; j
++)
559 memcpy( target_data
, source_data
, width
);
560 source_data
+= source_step
;
561 target_data
+= target_step
;
566 if (!HasMask() && image
.HasMask())
568 unsigned char r
= image
.GetMaskRed();
569 unsigned char g
= image
.GetMaskGreen();
570 unsigned char b
= image
.GetMaskBlue();
573 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
574 int source_step
= image
.GetWidth()*3;
576 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
577 int target_step
= M_IMGDATA
->m_width
*3;
579 for (int j
= 0; j
< height
; j
++)
581 for (int i
= 0; i
< width
; i
+=3)
583 if ((source_data
[i
] != r
) &&
584 (source_data
[i
+1] != g
) &&
585 (source_data
[i
+2] != b
))
587 memcpy( target_data
+i
, source_data
+i
, 3 );
590 source_data
+= source_step
;
591 target_data
+= target_step
;
596 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
597 unsigned char r2
, unsigned char g2
, unsigned char b2
)
599 wxCHECK_RET( Ok(), wxT("invalid image") );
601 unsigned char *data
= GetData();
603 const int w
= GetWidth();
604 const int h
= GetHeight();
606 for (int j
= 0; j
< h
; j
++)
607 for (int i
= 0; i
< w
; i
++)
609 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
619 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
623 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
625 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
627 unsigned char *data
= image
.GetData();
629 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
631 if (M_IMGDATA
->m_hasMask
)
633 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
634 M_IMGDATA
->m_maskBlue
== b
)
635 image
.SetMaskColour( 255, 255, 255 );
637 image
.SetMaskColour( 0, 0, 0 );
640 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
642 unsigned char *srcd
= M_IMGDATA
->m_data
;
643 unsigned char *tard
= image
.GetData();
645 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
647 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
648 tard
[0] = tard
[1] = tard
[2] = 255;
650 tard
[0] = tard
[1] = tard
[2] = 0;
656 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
658 wxCHECK_RET( Ok(), wxT("invalid image") );
660 int w
= M_IMGDATA
->m_width
;
661 int h
= M_IMGDATA
->m_height
;
663 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
665 long pos
= (y
* w
+ x
) * 3;
667 M_IMGDATA
->m_data
[ pos
] = r
;
668 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
669 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
672 unsigned char wxImage::GetRed( int x
, int y
) const
674 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
676 int w
= M_IMGDATA
->m_width
;
677 int h
= M_IMGDATA
->m_height
;
679 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
681 long pos
= (y
* w
+ x
) * 3;
683 return M_IMGDATA
->m_data
[pos
];
686 unsigned char wxImage::GetGreen( int x
, int y
) const
688 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
690 int w
= M_IMGDATA
->m_width
;
691 int h
= M_IMGDATA
->m_height
;
693 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
695 long pos
= (y
* w
+ x
) * 3;
697 return M_IMGDATA
->m_data
[pos
+1];
700 unsigned char wxImage::GetBlue( int x
, int y
) const
702 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
704 int w
= M_IMGDATA
->m_width
;
705 int h
= M_IMGDATA
->m_height
;
707 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
709 long pos
= (y
* w
+ x
) * 3;
711 return M_IMGDATA
->m_data
[pos
+2];
714 bool wxImage::Ok() const
716 // image of 0 width or height can't be considered ok - at least because it
717 // causes crashes in ConvertToBitmap() if we don't catch it in time
718 wxImageRefData
*data
= M_IMGDATA
;
719 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
722 unsigned char *wxImage::GetData() const
724 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
726 return M_IMGDATA
->m_data
;
729 void wxImage::SetData( unsigned char *data
)
731 wxCHECK_RET( Ok(), wxT("invalid image") );
733 wxImageRefData
*newRefData
= new wxImageRefData();
735 newRefData
->m_width
= M_IMGDATA
->m_width
;
736 newRefData
->m_height
= M_IMGDATA
->m_height
;
737 newRefData
->m_data
= data
;
738 newRefData
->m_ok
= true;
739 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
740 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
741 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
742 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
746 m_refData
= newRefData
;
749 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
751 wxImageRefData
*newRefData
= new wxImageRefData();
755 newRefData
->m_width
= new_width
;
756 newRefData
->m_height
= new_height
;
757 newRefData
->m_data
= data
;
758 newRefData
->m_ok
= true;
759 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
760 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
761 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
762 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
766 newRefData
->m_width
= new_width
;
767 newRefData
->m_height
= new_height
;
768 newRefData
->m_data
= data
;
769 newRefData
->m_ok
= true;
774 m_refData
= newRefData
;
777 // ----------------------------------------------------------------------------
778 // alpha channel support
779 // ----------------------------------------------------------------------------
781 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
783 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
785 int w
= M_IMGDATA
->m_width
,
786 h
= M_IMGDATA
->m_height
;
788 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
790 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
793 unsigned char wxImage::GetAlpha(int x
, int y
)
795 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
797 int w
= M_IMGDATA
->m_width
,
798 h
= M_IMGDATA
->m_height
;
800 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
802 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
805 void wxImage::SetAlpha( unsigned char *alpha
)
807 wxCHECK_RET( Ok(), wxT("invalid image") );
811 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
814 delete [] M_IMGDATA
->m_alpha
;
815 M_IMGDATA
->m_alpha
= alpha
;
818 unsigned char *wxImage::GetAlpha() const
820 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
822 return M_IMGDATA
->m_alpha
;
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
831 wxCHECK_RET( Ok(), wxT("invalid image") );
833 M_IMGDATA
->m_maskRed
= r
;
834 M_IMGDATA
->m_maskGreen
= g
;
835 M_IMGDATA
->m_maskBlue
= b
;
836 M_IMGDATA
->m_hasMask
= true;
839 unsigned char wxImage::GetMaskRed() const
841 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
843 return M_IMGDATA
->m_maskRed
;
846 unsigned char wxImage::GetMaskGreen() const
848 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
850 return M_IMGDATA
->m_maskGreen
;
853 unsigned char wxImage::GetMaskBlue() const
855 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
857 return M_IMGDATA
->m_maskBlue
;
860 void wxImage::SetMask( bool mask
)
862 wxCHECK_RET( Ok(), wxT("invalid image") );
864 M_IMGDATA
->m_hasMask
= mask
;
867 bool wxImage::HasMask() const
869 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
871 return M_IMGDATA
->m_hasMask
;
874 int wxImage::GetWidth() const
876 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
878 return M_IMGDATA
->m_width
;
881 int wxImage::GetHeight() const
883 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
885 return M_IMGDATA
->m_height
;
888 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
889 unsigned char mr
, unsigned char mg
, unsigned char mb
)
891 // check that the images are the same size
892 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
894 wxLogError( _("Image and Mask have different sizes") );
898 // find unused colour
899 unsigned char r
,g
,b
;
900 if (!FindFirstUnusedColour(&r
, &g
, &b
))
902 wxLogError( _("No Unused Color in image being masked") );
906 unsigned char *imgdata
= GetData();
907 unsigned char *maskdata
= mask
.GetData();
909 const int w
= GetWidth();
910 const int h
= GetHeight();
912 for (int j
= 0; j
< h
; j
++)
914 for (int i
= 0; i
< w
; i
++)
916 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
927 SetMaskColour(r
, g
, b
);
937 bool wxImage::HasPalette() const
942 return M_IMGDATA
->m_palette
.Ok();
945 const wxPalette
& wxImage::GetPalette() const
947 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
949 return M_IMGDATA
->m_palette
;
952 void wxImage::SetPalette(const wxPalette
& palette
)
954 wxCHECK_RET( Ok(), wxT("invalid image") );
956 M_IMGDATA
->m_palette
= palette
;
959 #endif // wxUSE_PALETTE
961 // Option functions (arbitrary name/value mapping)
962 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
964 wxCHECK_RET( Ok(), wxT("invalid image") );
966 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
967 if (idx
== wxNOT_FOUND
)
969 M_IMGDATA
->m_optionNames
.Add(name
);
970 M_IMGDATA
->m_optionValues
.Add(value
);
974 M_IMGDATA
->m_optionNames
[idx
] = name
;
975 M_IMGDATA
->m_optionValues
[idx
] = value
;
979 void wxImage::SetOption(const wxString
& name
, int value
)
982 valStr
.Printf(wxT("%d"), value
);
983 SetOption(name
, valStr
);
986 wxString
wxImage::GetOption(const wxString
& name
) const
988 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
990 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
991 if (idx
== wxNOT_FOUND
)
992 return wxEmptyString
;
994 return M_IMGDATA
->m_optionValues
[idx
];
997 int wxImage::GetOptionInt(const wxString
& name
) const
999 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1001 return wxAtoi(GetOption(name
));
1004 bool wxImage::HasOption(const wxString
& name
) const
1006 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1008 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1011 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1014 if (wxFileExists(filename
))
1016 wxFileInputStream
stream(filename
);
1017 wxBufferedInputStream
bstream( stream
);
1018 return LoadFile(bstream
, type
, index
);
1022 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1026 #else // !wxUSE_STREAMS
1028 #endif // wxUSE_STREAMS
1031 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1034 if (wxFileExists(filename
))
1036 wxFileInputStream
stream(filename
);
1037 wxBufferedInputStream
bstream( stream
);
1038 return LoadFile(bstream
, mimetype
, index
);
1042 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1046 #else // !wxUSE_STREAMS
1048 #endif // wxUSE_STREAMS
1053 bool wxImage::SaveFile( const wxString
& filename
) const
1055 wxString ext
= filename
.AfterLast('.').Lower();
1057 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1060 SaveFile(filename
, pHandler
->GetType());
1064 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1069 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1072 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1074 wxFileOutputStream
stream(filename
);
1076 if ( stream
.IsOk() )
1078 wxBufferedOutputStream
bstream( stream
);
1079 return SaveFile(bstream
, type
);
1081 #endif // wxUSE_STREAMS
1086 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1089 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1091 wxFileOutputStream
stream(filename
);
1093 if ( stream
.IsOk() )
1095 wxBufferedOutputStream
bstream( stream
);
1096 return SaveFile(bstream
, mimetype
);
1098 #endif // wxUSE_STREAMS
1103 bool wxImage::CanRead( const wxString
&name
)
1106 wxFileInputStream
stream(name
);
1107 return CanRead(stream
);
1113 int wxImage::GetImageCount( const wxString
&name
, long type
)
1116 wxFileInputStream
stream(name
);
1118 return GetImageCount(stream
, type
);
1126 bool wxImage::CanRead( wxInputStream
&stream
)
1128 const wxList
& list
= GetHandlers();
1130 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1132 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1133 if (handler
->CanRead( stream
))
1140 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1142 wxImageHandler
*handler
;
1144 if ( type
== wxBITMAP_TYPE_ANY
)
1146 wxList
&list
=GetHandlers();
1148 for (wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext())
1150 handler
=(wxImageHandler
*)node
->GetData();
1151 if ( handler
->CanRead(stream
) )
1152 return handler
->GetImageCount(stream
);
1156 wxLogWarning(_("No handler found for image type."));
1160 handler
= FindHandler(type
);
1164 wxLogWarning(_("No image handler for type %d defined."), type
);
1168 if ( handler
->CanRead(stream
) )
1170 return handler
->GetImageCount(stream
);
1174 wxLogError(_("Image file is not of type %d."), type
);
1179 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1183 m_refData
= new wxImageRefData
;
1185 wxImageHandler
*handler
;
1187 if ( type
== wxBITMAP_TYPE_ANY
)
1189 wxList
&list
=GetHandlers();
1191 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1193 handler
=(wxImageHandler
*)node
->GetData();
1194 if ( handler
->CanRead(stream
) )
1195 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1199 wxLogWarning( _("No handler found for image type.") );
1203 handler
= FindHandler(type
);
1207 wxLogWarning( _("No image handler for type %d defined."), type
);
1212 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1215 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1219 m_refData
= new wxImageRefData
;
1221 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1225 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1230 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1233 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1235 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1237 wxImageHandler
*handler
= FindHandler(type
);
1241 wxLogWarning( _("No image handler for type %d defined."), type
);
1246 return handler
->SaveFile( (wxImage
*)this, stream
);
1249 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1251 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1253 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1257 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1262 return handler
->SaveFile( (wxImage
*)this, stream
);
1264 #endif // wxUSE_STREAMS
1266 void wxImage::AddHandler( wxImageHandler
*handler
)
1268 // make sure that the memory will be freed at the program end
1269 sm_handlers
.DeleteContents(true);
1271 // Check for an existing handler of the type being added.
1272 if (FindHandler( handler
->GetType() ) == 0)
1274 sm_handlers
.Append( handler
);
1278 // This is not documented behaviour, merely the simplest 'fix'
1279 // for preventing duplicate additions. If someone ever has
1280 // a good reason to add and remove duplicate handlers (and they
1281 // may) we should probably refcount the duplicates.
1282 // also an issue in InsertHandler below.
1284 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1285 handler
->GetName().c_str() );
1290 void wxImage::InsertHandler( wxImageHandler
*handler
)
1292 // make sure that the memory will be freed at the program end
1293 sm_handlers
.DeleteContents(true);
1295 // Check for an existing handler of the type being added.
1296 if (FindHandler( handler
->GetType() ) == 0)
1298 sm_handlers
.Insert( handler
);
1302 // see AddHandler for additional comments.
1303 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1304 handler
->GetName().c_str() );
1309 bool wxImage::RemoveHandler( const wxString
& name
)
1311 wxImageHandler
*handler
= FindHandler(name
);
1314 sm_handlers
.DeleteObject(handler
);
1321 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1323 wxNode
*node
= sm_handlers
.GetFirst();
1326 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1327 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1329 node
= node
->GetNext();
1334 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1336 wxNode
*node
= sm_handlers
.GetFirst();
1339 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1340 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1341 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1343 node
= node
->GetNext();
1348 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1350 wxNode
*node
= sm_handlers
.GetFirst();
1353 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1354 if (handler
->GetType() == bitmapType
) return handler
;
1355 node
= node
->GetNext();
1360 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1362 wxNode
*node
= sm_handlers
.GetFirst();
1365 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1366 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1367 node
= node
->GetNext();
1372 void wxImage::InitStandardHandlers()
1375 AddHandler(new wxBMPHandler
);
1376 #endif // wxUSE_STREAMS
1379 void wxImage::CleanUpHandlers()
1381 wxNode
*node
= sm_handlers
.GetFirst();
1384 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1385 wxNode
*next
= node
->GetNext();
1393 //-----------------------------------------------------------------------------
1395 //-----------------------------------------------------------------------------
1397 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1400 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1405 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1410 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1415 bool wxImageHandler::CanRead( const wxString
& name
)
1417 if (wxFileExists(name
))
1419 wxFileInputStream
stream(name
);
1420 return CanRead(stream
);
1423 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1428 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1430 off_t posOld
= stream
.TellI();
1431 if ( posOld
== wxInvalidOffset
)
1433 // can't test unseekable stream
1437 bool ok
= DoCanRead(stream
);
1439 // restore the old position to be able to test other formats and so on
1440 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1442 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1444 // reading would fail anyhow as we're not at the right position
1451 #endif // wxUSE_STREAMS
1455 //-----------------------------------------------------------------------------
1456 // Deprecated wxBitmap conversion routines
1457 //-----------------------------------------------------------------------------
1459 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1462 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1464 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1465 wxBitmap
bitmap( mono
, 1 );
1470 wxBitmap
wxImage::ConvertToBitmap() const
1472 wxBitmap
bitmap( *this );
1476 wxImage::wxImage( const wxBitmap
&bitmap
)
1478 *this = bitmap
.ConvertToImage();
1481 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1484 // ----------------------------------------------------------------------------
1485 // image histogram stuff
1486 // ----------------------------------------------------------------------------
1489 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1494 unsigned char g2
) const
1496 unsigned long key
= MakeKey(r2
, g2
, b2
);
1498 while ( find(key
) != end() )
1500 // color already used
1512 wxLogError(_("GetUnusedColour:: No Unused Color in image ") );
1518 key
= MakeKey(r2
, g2
, b2
);
1532 wxImage::FindFirstUnusedColour(unsigned char *r
,
1537 unsigned char g2
) const
1539 wxImageHistogram histogram
;
1541 ComputeHistogram(histogram
);
1543 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1549 // Counts and returns the number of different colours. Optionally stops
1550 // when it exceeds 'stopafter' different colours. This is useful, for
1551 // example, to see if the image can be saved as 8-bit (256 colour or
1552 // less, in this case it would be invoked as CountColours(256)). Default
1553 // value for stopafter is -1 (don't care).
1555 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1559 unsigned char r
, g
, b
;
1561 unsigned long size
, nentries
, key
;
1564 size
= GetWidth() * GetHeight();
1567 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1572 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1574 if (h
.Get(key
) == NULL
)
1585 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1587 unsigned char *p
= GetData();
1588 unsigned long nentries
= 0;
1592 const unsigned long size
= GetWidth() * GetHeight();
1594 unsigned char r
, g
, b
;
1595 for ( unsigned long n
= 0; n
< size
; n
++ )
1601 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1603 if ( entry
.value
++ == 0 )
1604 entry
.index
= nentries
++;
1611 * Rotation code by Carlos Moreno
1614 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1615 // does exactly the same thing. And I also got rid of wxRotationPixel
1616 // bacause of potential problems in architectures where alignment
1617 // is an issue, so I had to rewrite parts of the code.
1619 static const double gs_Epsilon
= 1e-10;
1621 static inline int wxCint (double x
)
1623 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1627 // Auxiliary function to rotate a point (x,y) with respect to point p0
1628 // make it inline and use a straight return to facilitate optimization
1629 // also, the function receives the sine and cosine of the angle to avoid
1630 // repeating the time-consuming calls to these functions -- sin/cos can
1631 // be computed and stored in the calling function.
1633 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1635 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1636 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1639 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1641 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1644 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1647 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1649 // Create pointer-based array to accelerate access to wxImage's data
1650 unsigned char ** data
= new unsigned char * [GetHeight()];
1652 data
[0] = GetData();
1654 for (i
= 1; i
< GetHeight(); i
++)
1655 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1657 // precompute coefficients for rotation formula
1658 // (sine and cosine of the angle)
1659 const double cos_angle
= cos(angle
);
1660 const double sin_angle
= sin(angle
);
1662 // Create new Image to store the result
1663 // First, find rectangle that covers the rotated image; to do that,
1664 // rotate the four corners
1666 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1668 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1669 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1670 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1671 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1673 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1674 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1675 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1676 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1678 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1680 if (offset_after_rotation
!= NULL
)
1682 *offset_after_rotation
= wxPoint (x1
, y1
);
1685 // GRG: The rotated (destination) image is always accessed
1686 // sequentially, so there is no need for a pointer-based
1687 // array here (and in fact it would be slower).
1689 unsigned char * dst
= rotated
.GetData();
1691 // GRG: if the original image has a mask, use its RGB values
1692 // as the blank pixel, else, fall back to default (black).
1694 unsigned char blank_r
= 0;
1695 unsigned char blank_g
= 0;
1696 unsigned char blank_b
= 0;
1700 blank_r
= GetMaskRed();
1701 blank_g
= GetMaskGreen();
1702 blank_b
= GetMaskBlue();
1703 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1706 // Now, for each point of the rotated image, find where it came from, by
1707 // performing an inverse rotation (a rotation of -angle) and getting the
1708 // pixel at those coordinates
1710 // GRG: I've taken the (interpolating) test out of the loops, so that
1711 // it is done only once, instead of repeating it for each pixel.
1716 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1718 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1720 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1722 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1723 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1725 // interpolate using the 4 enclosing grid-points. Those
1726 // points can be obtained using floor and ceiling of the
1727 // exact coordinates of the point
1728 // C.M. 2000-02-17: when the point is near the border, special care is required.
1732 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1734 x1
= wxCint(floor(src
.x
));
1735 x2
= wxCint(ceil(src
.x
));
1737 else // else means that x is near one of the borders (0 or width-1)
1739 x1
= x2
= wxCint (src
.x
);
1742 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1744 y1
= wxCint(floor(src
.y
));
1745 y2
= wxCint(ceil(src
.y
));
1749 y1
= y2
= wxCint (src
.y
);
1752 // get four points and the distances (square of the distance,
1753 // for efficiency reasons) for the interpolation formula
1755 // GRG: Do not calculate the points until they are
1756 // really needed -- this way we can calculate
1757 // just one, instead of four, if d1, d2, d3
1758 // or d4 are < gs_Epsilon
1760 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1761 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1762 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1763 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1765 // Now interpolate as a weighted average of the four surrounding
1766 // points, where the weights are the distances to each of those points
1768 // If the point is exactly at one point of the grid of the source
1769 // image, then don't interpolate -- just assign the pixel
1771 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1773 unsigned char *p
= data
[y1
] + (3 * x1
);
1778 else if (d2
< gs_Epsilon
)
1780 unsigned char *p
= data
[y1
] + (3 * x2
);
1785 else if (d3
< gs_Epsilon
)
1787 unsigned char *p
= data
[y2
] + (3 * x2
);
1792 else if (d4
< gs_Epsilon
)
1794 unsigned char *p
= data
[y2
] + (3 * x1
);
1801 // weights for the weighted average are proportional to the inverse of the distance
1802 unsigned char *v1
= data
[y1
] + (3 * x1
);
1803 unsigned char *v2
= data
[y1
] + (3 * x2
);
1804 unsigned char *v3
= data
[y2
] + (3 * x2
);
1805 unsigned char *v4
= data
[y2
] + (3 * x1
);
1807 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1811 *(dst
++) = (unsigned char)
1812 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1813 w3
* *(v3
++) + w4
* *(v4
++)) /
1814 (w1
+ w2
+ w3
+ w4
) );
1815 *(dst
++) = (unsigned char)
1816 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1817 w3
* *(v3
++) + w4
* *(v4
++)) /
1818 (w1
+ w2
+ w3
+ w4
) );
1819 *(dst
++) = (unsigned char)
1820 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1821 w3
* *(v3
++) + w4
* *(v4
++)) /
1822 (w1
+ w2
+ w3
+ w4
) );
1834 else // not interpolating
1836 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1838 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1840 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1842 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1843 const int ys
= wxCint (src
.y
); // closest integer
1845 if (0 <= xs
&& xs
< GetWidth() &&
1846 0 <= ys
&& ys
< GetHeight())
1848 unsigned char *p
= data
[ys
] + (3 * xs
);
1872 // A module to allow wxImage initialization/cleanup
1873 // without calling these functions from app.cpp or from
1874 // the user's application.
1876 class wxImageModule
: public wxModule
1878 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1881 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
1882 void OnExit() { wxImage::CleanUpHandlers(); };
1885 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1888 #endif // wxUSE_IMAGE