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"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 class wxImageRefData
: public wxObjectRefData
52 unsigned char *m_data
;
54 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
58 wxArrayString m_optionNames
;
59 wxArrayString m_optionValues
;
62 wxImageRefData::wxImageRefData()
66 m_data
= (unsigned char*) NULL
;
75 wxImageRefData::~wxImageRefData()
77 if (m_data
&& !m_static
)
81 wxList
wxImage::sm_handlers
;
85 //-----------------------------------------------------------------------------
87 #define M_IMGDATA ((wxImageRefData *)m_refData)
89 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
95 wxImage::wxImage( int width
, int height
)
97 Create( width
, height
);
100 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
102 Create( width
, height
, data
, static_data
);
105 wxImage::wxImage( const wxString
& name
, long type
)
107 LoadFile( name
, type
);
110 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
112 LoadFile( name
, mimetype
);
116 wxImage::wxImage( wxInputStream
& stream
, long type
)
118 LoadFile( stream
, type
);
121 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
123 LoadFile( stream
, mimetype
);
125 #endif // wxUSE_STREAMS
127 wxImage::wxImage( const wxImage
& image
)
132 wxImage::wxImage( const wxImage
* image
)
134 if (image
) Ref(*image
);
137 void wxImage::Create( int width
, int height
)
141 m_refData
= new wxImageRefData();
143 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
144 if (M_IMGDATA
->m_data
)
146 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
148 M_IMGDATA
->m_width
= width
;
149 M_IMGDATA
->m_height
= height
;
150 M_IMGDATA
->m_ok
= TRUE
;
158 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
162 m_refData
= new wxImageRefData();
164 M_IMGDATA
->m_data
= data
;
165 if (M_IMGDATA
->m_data
)
167 M_IMGDATA
->m_width
= width
;
168 M_IMGDATA
->m_height
= height
;
169 M_IMGDATA
->m_ok
= TRUE
;
170 M_IMGDATA
->m_static
= static_data
;
178 void wxImage::Destroy()
183 wxImage
wxImage::Copy() const
187 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
189 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
191 char unsigned *data
= image
.GetData();
193 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
195 if (M_IMGDATA
->m_hasMask
)
196 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
198 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
203 wxImage
wxImage::Scale( int width
, int height
) const
207 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
209 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, wxT("invalid image size") );
211 image
.Create( width
, height
);
213 char unsigned *data
= image
.GetData();
215 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
217 if (M_IMGDATA
->m_hasMask
)
218 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
220 long old_height
= M_IMGDATA
->m_height
;
221 long old_width
= M_IMGDATA
->m_width
;
223 char unsigned *source_data
= M_IMGDATA
->m_data
;
224 char unsigned *target_data
= data
;
226 for (long j
= 0; j
< height
; j
++)
228 long y_offset
= (j
* old_height
/ height
) * old_width
;
230 for (long i
= 0; i
< width
; i
++)
233 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
242 wxImage
wxImage::Rotate90( bool clockwise
) const
246 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
248 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
250 char unsigned *data
= image
.GetData();
252 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
254 if (M_IMGDATA
->m_hasMask
)
255 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
257 long height
= M_IMGDATA
->m_height
;
258 long width
= M_IMGDATA
->m_width
;
260 char unsigned *source_data
= M_IMGDATA
->m_data
;
261 char unsigned *target_data
;
263 for (long j
= 0; j
< height
; j
++)
265 for (long i
= 0; i
< width
; i
++)
268 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
270 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
271 memcpy( target_data
, source_data
, 3 );
279 wxImage
wxImage::Mirror( bool horizontally
) const
283 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
285 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
287 char unsigned *data
= image
.GetData();
289 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
291 if (M_IMGDATA
->m_hasMask
)
292 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
294 long height
= M_IMGDATA
->m_height
;
295 long width
= M_IMGDATA
->m_width
;
297 char unsigned *source_data
= M_IMGDATA
->m_data
;
298 char unsigned *target_data
;
302 for (long j
= 0; j
< height
; j
++)
305 target_data
= data
-3;
306 for (long i
= 0; i
< width
; i
++)
308 memcpy( target_data
, source_data
, 3 );
316 for (long i
= 0; i
< height
; i
++)
318 target_data
= data
+ 3*width
*(height
-1-i
);
319 memcpy( target_data
, source_data
, (size_t)3*width
);
320 source_data
+= 3*width
;
327 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
331 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
333 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
334 image
, wxT("invalid subimage size") );
336 int subwidth
=rect
.GetWidth();
337 const int subheight
=rect
.GetHeight();
339 image
.Create( subwidth
, subheight
);
341 char unsigned *subdata
= image
.GetData(), *data
=GetData();
343 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
345 if (M_IMGDATA
->m_hasMask
)
346 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
348 const int subleft
=3*rect
.GetLeft();
349 const int width
=3*GetWidth();
352 data
+=rect
.GetTop()*width
+subleft
;
354 for (long j
= 0; j
< subheight
; ++j
)
356 memcpy( subdata
, data
, subwidth
);
364 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
366 wxCHECK_RET( Ok(), wxT("invalid image") );
367 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
371 int width
= image
.GetWidth();
372 int height
= image
.GetHeight();
385 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
386 width
= M_IMGDATA
->m_width
- (x
+xx
);
387 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
388 height
= M_IMGDATA
->m_height
- (y
+yy
);
390 if (width
< 1) return;
391 if (height
< 1) return;
393 if ((!HasMask() && !image
.HasMask()) ||
394 ((HasMask() && image
.HasMask() &&
395 (GetMaskRed()==image
.GetMaskRed()) &&
396 (GetMaskGreen()==image
.GetMaskGreen()) &&
397 (GetMaskBlue()==image
.GetMaskBlue()))))
400 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
401 int source_step
= image
.GetWidth()*3;
403 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
404 int target_step
= M_IMGDATA
->m_width
*3;
405 for (int j
= 0; j
< height
; j
++)
407 memcpy( target_data
, source_data
, width
);
408 source_data
+= source_step
;
409 target_data
+= target_step
;
414 if (!HasMask() && image
.HasMask())
416 unsigned char r
= image
.GetMaskRed();
417 unsigned char g
= image
.GetMaskGreen();
418 unsigned char b
= image
.GetMaskBlue();
421 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
422 int source_step
= image
.GetWidth()*3;
424 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
425 int target_step
= M_IMGDATA
->m_width
*3;
427 for (int j
= 0; j
< height
; j
++)
429 for (int i
= 0; i
< width
; i
+=3)
431 if ((source_data
[i
] != r
) &&
432 (source_data
[i
+1] != g
) &&
433 (source_data
[i
+2] != b
))
435 memcpy( target_data
+i
, source_data
+i
, 3 );
438 source_data
+= source_step
;
439 target_data
+= target_step
;
444 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
445 unsigned char r2
, unsigned char g2
, unsigned char b2
)
447 wxCHECK_RET( Ok(), wxT("invalid image") );
449 char unsigned *data
= GetData();
451 const int w
= GetWidth();
452 const int h
= GetHeight();
454 for (int j
= 0; j
< h
; j
++)
455 for (int i
= 0; i
< w
; i
++)
457 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
467 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
471 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
473 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
475 char unsigned *data
= image
.GetData();
477 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
479 if (M_IMGDATA
->m_hasMask
)
481 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
482 M_IMGDATA
->m_maskBlue
== b
)
483 image
.SetMaskColour( 255, 255, 255 );
485 image
.SetMaskColour( 0, 0, 0 );
488 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
490 char unsigned *srcd
= M_IMGDATA
->m_data
;
491 char unsigned *tard
= image
.GetData();
493 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
495 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
496 tard
[0] = tard
[1] = tard
[2] = 255;
498 tard
[0] = tard
[1] = tard
[2] = 0;
504 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
506 wxCHECK_RET( Ok(), wxT("invalid image") );
508 int w
= M_IMGDATA
->m_width
;
509 int h
= M_IMGDATA
->m_height
;
511 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
513 long pos
= (y
* w
+ x
) * 3;
515 M_IMGDATA
->m_data
[ pos
] = r
;
516 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
517 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
520 unsigned char wxImage::GetRed( int x
, int y
) const
522 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
524 int w
= M_IMGDATA
->m_width
;
525 int h
= M_IMGDATA
->m_height
;
527 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
529 long pos
= (y
* w
+ x
) * 3;
531 return M_IMGDATA
->m_data
[pos
];
534 unsigned char wxImage::GetGreen( int x
, int y
) const
536 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
538 int w
= M_IMGDATA
->m_width
;
539 int h
= M_IMGDATA
->m_height
;
541 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
543 long pos
= (y
* w
+ x
) * 3;
545 return M_IMGDATA
->m_data
[pos
+1];
548 unsigned char wxImage::GetBlue( int x
, int y
) const
550 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
552 int w
= M_IMGDATA
->m_width
;
553 int h
= M_IMGDATA
->m_height
;
555 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
557 long pos
= (y
* w
+ x
) * 3;
559 return M_IMGDATA
->m_data
[pos
+2];
562 bool wxImage::Ok() const
564 // image of 0 width or height can't be considered ok - at least because it
565 // causes crashes in ConvertToBitmap() if we don't catch it in time
566 wxImageRefData
*data
= M_IMGDATA
;
567 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
570 char unsigned *wxImage::GetData() const
572 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
574 return M_IMGDATA
->m_data
;
577 void wxImage::SetData( char unsigned *data
)
579 wxCHECK_RET( Ok(), wxT("invalid image") );
581 wxImageRefData
*newRefData
= new wxImageRefData();
583 newRefData
->m_width
= M_IMGDATA
->m_width
;
584 newRefData
->m_height
= M_IMGDATA
->m_height
;
585 newRefData
->m_data
= data
;
586 newRefData
->m_ok
= TRUE
;
587 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
588 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
589 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
590 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
594 m_refData
= newRefData
;
597 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
599 wxImageRefData
*newRefData
= new wxImageRefData();
603 newRefData
->m_width
= new_width
;
604 newRefData
->m_height
= new_height
;
605 newRefData
->m_data
= data
;
606 newRefData
->m_ok
= TRUE
;
607 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
608 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
609 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
610 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
614 newRefData
->m_width
= new_width
;
615 newRefData
->m_height
= new_height
;
616 newRefData
->m_data
= data
;
617 newRefData
->m_ok
= TRUE
;
622 m_refData
= newRefData
;
625 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
627 wxCHECK_RET( Ok(), wxT("invalid image") );
629 M_IMGDATA
->m_maskRed
= r
;
630 M_IMGDATA
->m_maskGreen
= g
;
631 M_IMGDATA
->m_maskBlue
= b
;
632 M_IMGDATA
->m_hasMask
= TRUE
;
635 unsigned char wxImage::GetMaskRed() const
637 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
639 return M_IMGDATA
->m_maskRed
;
642 unsigned char wxImage::GetMaskGreen() const
644 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
646 return M_IMGDATA
->m_maskGreen
;
649 unsigned char wxImage::GetMaskBlue() const
651 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
653 return M_IMGDATA
->m_maskBlue
;
656 void wxImage::SetMask( bool mask
)
658 wxCHECK_RET( Ok(), wxT("invalid image") );
660 M_IMGDATA
->m_hasMask
= mask
;
663 bool wxImage::HasMask() const
665 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
667 return M_IMGDATA
->m_hasMask
;
670 int wxImage::GetWidth() const
672 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
674 return M_IMGDATA
->m_width
;
677 int wxImage::GetHeight() const
679 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
681 return M_IMGDATA
->m_height
;
686 bool wxImage::HasPalette() const
691 return M_IMGDATA
->m_palette
.Ok();
694 const wxPalette
& wxImage::GetPalette() const
696 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
698 return M_IMGDATA
->m_palette
;
701 void wxImage::SetPalette(const wxPalette
& palette
)
703 wxCHECK_RET( Ok(), wxT("invalid image") );
705 M_IMGDATA
->m_palette
= palette
;
708 // Option functions (arbitrary name/value mapping)
709 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
711 wxCHECK_RET( Ok(), wxT("invalid image") );
713 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
714 if (idx
== wxNOT_FOUND
)
716 M_IMGDATA
->m_optionNames
.Add(name
);
717 M_IMGDATA
->m_optionValues
.Add(value
);
721 M_IMGDATA
->m_optionNames
[idx
] = name
;
722 M_IMGDATA
->m_optionValues
[idx
] = value
;
726 void wxImage::SetOption(const wxString
& name
, int value
)
729 valStr
.Printf(wxT("%d"), value
);
730 SetOption(name
, valStr
);
733 wxString
wxImage::GetOption(const wxString
& name
) const
735 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
737 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
738 if (idx
== wxNOT_FOUND
)
739 return wxEmptyString
;
741 return M_IMGDATA
->m_optionValues
[idx
];
744 int wxImage::GetOptionInt(const wxString
& name
) const
746 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
748 return wxAtoi(GetOption(name
));
751 bool wxImage::HasOption(const wxString
& name
) const
753 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
755 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
758 bool wxImage::LoadFile( const wxString
& filename
, long type
)
761 if (wxFileExists(filename
))
763 wxFileInputStream
stream(filename
);
764 wxBufferedInputStream
bstream( stream
);
765 return LoadFile(bstream
, type
);
769 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
773 #else // !wxUSE_STREAMS
775 #endif // wxUSE_STREAMS
778 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
781 if (wxFileExists(filename
))
783 wxFileInputStream
stream(filename
);
784 wxBufferedInputStream
bstream( stream
);
785 return LoadFile(bstream
, mimetype
);
789 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
793 #else // !wxUSE_STREAMS
795 #endif // wxUSE_STREAMS
798 bool wxImage::SaveFile( const wxString
& filename
, int type
)
801 wxFileOutputStream
stream(filename
);
803 if ( stream
.LastError() == wxStream_NOERROR
)
805 wxBufferedOutputStream
bstream( stream
);
806 return SaveFile(bstream
, type
);
808 #endif // wxUSE_STREAMS
813 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
816 wxFileOutputStream
stream(filename
);
818 if ( stream
.LastError() == wxStream_NOERROR
)
820 wxBufferedOutputStream
bstream( stream
);
821 return SaveFile(bstream
, mimetype
);
823 #endif // wxUSE_STREAMS
828 bool wxImage::CanRead( const wxString
&name
)
831 wxFileInputStream
stream(name
);
832 return CanRead(stream
);
840 bool wxImage::CanRead( wxInputStream
&stream
)
842 wxList
&list
=GetHandlers();
844 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
846 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
847 if (handler
->CanRead( stream
))
854 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
858 m_refData
= new wxImageRefData
;
860 wxImageHandler
*handler
;
862 if (type
==wxBITMAP_TYPE_ANY
)
864 wxList
&list
=GetHandlers();
866 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
868 handler
=(wxImageHandler
*)node
->GetData();
869 if (handler
->CanRead( stream
))
870 return handler
->LoadFile( this, stream
);
874 wxLogWarning( _("No handler found for image type.") );
878 handler
= FindHandler(type
);
882 wxLogWarning( _("No image handler for type %d defined."), type
);
887 return handler
->LoadFile( this, stream
);
890 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
894 m_refData
= new wxImageRefData
;
896 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
900 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
905 return handler
->LoadFile( this, stream
);
908 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
910 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
912 wxImageHandler
*handler
= FindHandler(type
);
916 wxLogWarning( _("No image handler for type %d defined."), type
);
921 return handler
->SaveFile( this, stream
);
924 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
926 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
928 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
932 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
937 return handler
->SaveFile( this, stream
);
939 #endif // wxUSE_STREAMS
941 void wxImage::AddHandler( wxImageHandler
*handler
)
943 // make sure that the memory will be freed at the program end
944 sm_handlers
.DeleteContents(TRUE
);
946 sm_handlers
.Append( handler
);
949 void wxImage::InsertHandler( wxImageHandler
*handler
)
951 // make sure that the memory will be freed at the program end
952 sm_handlers
.DeleteContents(TRUE
);
954 sm_handlers
.Insert( handler
);
957 bool wxImage::RemoveHandler( const wxString
& name
)
959 wxImageHandler
*handler
= FindHandler(name
);
962 sm_handlers
.DeleteObject(handler
);
969 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
971 wxNode
*node
= sm_handlers
.First();
974 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
975 if (handler
->GetName().Cmp(name
) == 0) return handler
;
979 return (wxImageHandler
*)NULL
;
982 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
984 wxNode
*node
= sm_handlers
.First();
987 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
988 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
989 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
993 return (wxImageHandler
*)NULL
;
996 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
998 wxNode
*node
= sm_handlers
.First();
1001 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1002 if (handler
->GetType() == bitmapType
) return handler
;
1003 node
= node
->Next();
1008 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1010 wxNode
*node
= sm_handlers
.First();
1013 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1014 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1015 node
= node
->Next();
1020 void wxImage::InitStandardHandlers()
1022 AddHandler( new wxBMPHandler
);
1025 void wxImage::CleanUpHandlers()
1027 wxNode
*node
= sm_handlers
.First();
1030 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1031 wxNode
*next
= node
->Next();
1038 //-----------------------------------------------------------------------------
1040 //-----------------------------------------------------------------------------
1042 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1045 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1050 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1055 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1060 bool wxImageHandler::CanRead( const wxString
& name
)
1062 if (wxFileExists(name
))
1064 wxFileInputStream
stream(name
);
1065 return CanRead(stream
);
1069 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1076 #endif // wxUSE_STREAMS
1080 //-----------------------------------------------------------------------------
1081 // wxBitmap convertion routines
1082 //-----------------------------------------------------------------------------
1087 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1089 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1090 wxBitmap
bitmap( mono
, 1 );
1095 wxBitmap
wxImage::ConvertToBitmap() const
1097 wxBitmap
bitmap( *this );
1101 wxImage::wxImage( const wxBitmap
&bitmap
)
1103 *this = bitmap
.ConvertToImage();
1110 // A module to allow wxImage initialization/cleanup
1111 // without calling these functions from app.cpp or from
1112 // the user's application.
1114 class wxImageModule
: public wxModule
1116 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1119 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1120 void OnExit() { wxImage::CleanUpHandlers(); };
1123 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1126 //-----------------------------------------------------------------------------
1129 // Counts and returns the number of different colours. Optionally stops
1130 // when it exceeds 'stopafter' different colours. This is useful, for
1131 // example, to see if the image can be saved as 8-bit (256 colour or
1132 // less, in this case it would be invoked as CountColours(256)). Default
1133 // value for stopafter is -1 (don't care).
1135 unsigned long wxImage::CountColours( unsigned long stopafter
)
1139 unsigned char r
, g
, b
, *p
;
1140 unsigned long size
, nentries
, key
;
1143 size
= GetWidth() * GetHeight();
1146 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1151 key
= (r
<< 16) | (g
<< 8) | b
;
1153 if (h
.Get(key
) == NULL
)
1165 // Computes the histogram of the image and fills a hash table, indexed
1166 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1167 // wxHNode contains an 'index' (useful to build a palette with the image
1168 // colours) and a 'value', which is the number of pixels in the image with
1171 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1173 unsigned char r
, g
, b
, *p
;
1174 unsigned long size
, nentries
, key
;
1178 size
= GetWidth() * GetHeight();
1181 for (unsigned long j
= 0; j
< size
; j
++)
1186 key
= (r
<< 16) | (g
<< 8) | b
;
1188 hnode
= (wxHNode
*) h
.Get(key
);
1194 hnode
= new wxHNode();
1195 hnode
->index
= nentries
++;
1198 h
.Put(key
, (wxObject
*)hnode
);
1206 * Rotation code by Carlos Moreno
1209 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1210 // does exactly the same thing. And I also got rid of wxRotationPixel
1211 // bacause of potential problems in architectures where alignment
1212 // is an issue, so I had to rewrite parts of the code.
1214 static const double gs_Epsilon
= 1e-10;
1216 static inline int wxCint (double x
)
1218 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1222 // Auxiliary function to rotate a point (x,y) with respect to point p0
1223 // make it inline and use a straight return to facilitate optimization
1224 // also, the function receives the sine and cosine of the angle to avoid
1225 // repeating the time-consuming calls to these functions -- sin/cos can
1226 // be computed and stored in the calling function.
1228 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1230 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1231 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1234 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1236 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1239 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1242 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1244 // Create pointer-based array to accelerate access to wxImage's data
1245 unsigned char ** data
= new unsigned char * [GetHeight()];
1247 data
[0] = GetData();
1249 for (i
= 1; i
< GetHeight(); i
++)
1250 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1252 // precompute coefficients for rotation formula
1253 // (sine and cosine of the angle)
1254 const double cos_angle
= cos(angle
);
1255 const double sin_angle
= sin(angle
);
1257 // Create new Image to store the result
1258 // First, find rectangle that covers the rotated image; to do that,
1259 // rotate the four corners
1261 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1263 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1264 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1265 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1266 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1268 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1269 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1270 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1271 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1273 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1275 if (offset_after_rotation
!= NULL
)
1277 *offset_after_rotation
= wxPoint (x1
, y1
);
1280 // GRG: The rotated (destination) image is always accessed
1281 // sequentially, so there is no need for a pointer-based
1282 // array here (and in fact it would be slower).
1284 unsigned char * dst
= rotated
.GetData();
1286 // GRG: if the original image has a mask, use its RGB values
1287 // as the blank pixel, else, fall back to default (black).
1289 unsigned char blank_r
= 0;
1290 unsigned char blank_g
= 0;
1291 unsigned char blank_b
= 0;
1295 blank_r
= GetMaskRed();
1296 blank_g
= GetMaskGreen();
1297 blank_b
= GetMaskBlue();
1298 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1301 // Now, for each point of the rotated image, find where it came from, by
1302 // performing an inverse rotation (a rotation of -angle) and getting the
1303 // pixel at those coordinates
1305 // GRG: I've taken the (interpolating) test out of the loops, so that
1306 // it is done only once, instead of repeating it for each pixel.
1311 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1313 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1315 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1317 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1318 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1320 // interpolate using the 4 enclosing grid-points. Those
1321 // points can be obtained using floor and ceiling of the
1322 // exact coordinates of the point
1323 // C.M. 2000-02-17: when the point is near the border, special care is required.
1327 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1329 x1
= wxCint(floor(src
.x
));
1330 x2
= wxCint(ceil(src
.x
));
1332 else // else means that x is near one of the borders (0 or width-1)
1334 x1
= x2
= wxCint (src
.x
);
1337 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1339 y1
= wxCint(floor(src
.y
));
1340 y2
= wxCint(ceil(src
.y
));
1344 y1
= y2
= wxCint (src
.y
);
1347 // get four points and the distances (square of the distance,
1348 // for efficiency reasons) for the interpolation formula
1350 // GRG: Do not calculate the points until they are
1351 // really needed -- this way we can calculate
1352 // just one, instead of four, if d1, d2, d3
1353 // or d4 are < gs_Epsilon
1355 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1356 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1357 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1358 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1360 // Now interpolate as a weighted average of the four surrounding
1361 // points, where the weights are the distances to each of those points
1363 // If the point is exactly at one point of the grid of the source
1364 // image, then don't interpolate -- just assign the pixel
1366 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1368 unsigned char *p
= data
[y1
] + (3 * x1
);
1373 else if (d2
< gs_Epsilon
)
1375 unsigned char *p
= data
[y1
] + (3 * x2
);
1380 else if (d3
< gs_Epsilon
)
1382 unsigned char *p
= data
[y2
] + (3 * x2
);
1387 else if (d4
< gs_Epsilon
)
1389 unsigned char *p
= data
[y2
] + (3 * x1
);
1396 // weights for the weighted average are proportional to the inverse of the distance
1397 unsigned char *v1
= data
[y1
] + (3 * x1
);
1398 unsigned char *v2
= data
[y1
] + (3 * x2
);
1399 unsigned char *v3
= data
[y2
] + (3 * x2
);
1400 unsigned char *v4
= data
[y2
] + (3 * x1
);
1402 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1406 *(dst
++) = (unsigned char)
1407 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1408 w3
* *(v3
++) + w4
* *(v4
++)) /
1409 (w1
+ w2
+ w3
+ w4
) );
1410 *(dst
++) = (unsigned char)
1411 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1412 w3
* *(v3
++) + w4
* *(v4
++)) /
1413 (w1
+ w2
+ w3
+ w4
) );
1414 *(dst
++) = (unsigned char)
1415 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1416 w3
* *(v3
++) + w4
* *(v4
++)) /
1417 (w1
+ w2
+ w3
+ w4
) );
1429 else // not interpolating
1431 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1433 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1435 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1437 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1438 const int ys
= wxCint (src
.y
); // closest integer
1440 if (0 <= xs
&& xs
< GetWidth() &&
1441 0 <= ys
&& ys
< GetHeight())
1443 unsigned char *p
= data
[ys
] + (3 * xs
);