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"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 class wxImageRefData
: public wxObjectRefData
56 unsigned char *m_data
;
58 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 wxArrayString m_optionNames
;
63 wxArrayString m_optionValues
;
66 wxImageRefData::wxImageRefData()
70 m_data
= (unsigned char*) NULL
;
79 wxImageRefData::~wxImageRefData()
81 if (m_data
&& !m_static
)
85 wxList
wxImage::sm_handlers
;
89 //-----------------------------------------------------------------------------
91 #define M_IMGDATA ((wxImageRefData *)m_refData)
93 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
99 wxImage::wxImage( int width
, int height
)
101 Create( width
, height
);
104 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
106 Create( width
, height
, data
, static_data
);
109 wxImage::wxImage( const wxString
& name
, long type
)
111 LoadFile( name
, type
);
114 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
116 LoadFile( name
, mimetype
);
120 wxImage::wxImage( wxInputStream
& stream
, long type
)
122 LoadFile( stream
, type
);
125 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
127 LoadFile( stream
, mimetype
);
129 #endif // wxUSE_STREAMS
131 wxImage::wxImage( const wxImage
& image
)
136 wxImage::wxImage( const wxImage
* image
)
138 if (image
) Ref(*image
);
141 void wxImage::Create( int width
, int height
)
145 m_refData
= new wxImageRefData();
147 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
148 if (M_IMGDATA
->m_data
)
150 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
152 M_IMGDATA
->m_width
= width
;
153 M_IMGDATA
->m_height
= height
;
154 M_IMGDATA
->m_ok
= TRUE
;
162 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
166 m_refData
= new wxImageRefData();
168 M_IMGDATA
->m_data
= data
;
169 if (M_IMGDATA
->m_data
)
171 M_IMGDATA
->m_width
= width
;
172 M_IMGDATA
->m_height
= height
;
173 M_IMGDATA
->m_ok
= TRUE
;
174 M_IMGDATA
->m_static
= static_data
;
182 void wxImage::Destroy()
187 wxImage
wxImage::Copy() const
191 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
193 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
195 char unsigned *data
= image
.GetData();
197 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
199 if (M_IMGDATA
->m_hasMask
)
200 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
202 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
207 wxImage
wxImage::Scale( int width
, int height
) const
211 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
213 // can't scale to/from 0 size
214 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
215 wxT("invalid new image size") );
217 long old_height
= M_IMGDATA
->m_height
,
218 old_width
= M_IMGDATA
->m_width
;
219 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
220 wxT("invalid old image size") );
222 image
.Create( width
, height
);
224 char unsigned *data
= image
.GetData();
226 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
228 if (M_IMGDATA
->m_hasMask
)
230 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
231 M_IMGDATA
->m_maskGreen
,
232 M_IMGDATA
->m_maskBlue
);
235 char unsigned *source_data
= M_IMGDATA
->m_data
;
236 char unsigned *target_data
= data
;
239 // This is nonsense, RR.
241 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
242 // are from 0 to size-1, hence all decrement the sizes
243 long old_old_width
= old_width
;
248 for ( long j
= 0; j
<= height
; j
++ )
250 // don't crash for images with height == 1
251 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
253 for ( long i
= 0; i
<= width
; i
++ )
255 long x_offset
= width
? (i
* old_width
) / width
: 0;
257 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
262 for (long j
= 0; j
< height
; j
++)
264 long y_offset
= (j
* old_height
/ height
) * old_width
;
266 for (long i
= 0; i
< width
; i
++)
269 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
279 wxImage
wxImage::Rotate90( bool clockwise
) const
283 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
285 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
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
;
300 for (long j
= 0; j
< height
; j
++)
302 for (long i
= 0; i
< width
; i
++)
305 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
307 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
308 memcpy( target_data
, source_data
, 3 );
316 wxImage
wxImage::Mirror( bool horizontally
) const
320 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
322 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
324 char unsigned *data
= image
.GetData();
326 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
328 if (M_IMGDATA
->m_hasMask
)
329 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
331 long height
= M_IMGDATA
->m_height
;
332 long width
= M_IMGDATA
->m_width
;
334 char unsigned *source_data
= M_IMGDATA
->m_data
;
335 char unsigned *target_data
;
339 for (long j
= 0; j
< height
; j
++)
342 target_data
= data
-3;
343 for (long i
= 0; i
< width
; i
++)
345 memcpy( target_data
, source_data
, 3 );
353 for (long i
= 0; i
< height
; i
++)
355 target_data
= data
+ 3*width
*(height
-1-i
);
356 memcpy( target_data
, source_data
, (size_t)3*width
);
357 source_data
+= 3*width
;
364 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
368 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
370 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
371 image
, wxT("invalid subimage size") );
373 int subwidth
=rect
.GetWidth();
374 const int subheight
=rect
.GetHeight();
376 image
.Create( subwidth
, subheight
);
378 char unsigned *subdata
= image
.GetData(), *data
=GetData();
380 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
382 if (M_IMGDATA
->m_hasMask
)
383 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
385 const int subleft
=3*rect
.GetLeft();
386 const int width
=3*GetWidth();
389 data
+=rect
.GetTop()*width
+subleft
;
391 for (long j
= 0; j
< subheight
; ++j
)
393 memcpy( subdata
, data
, subwidth
);
401 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
403 wxCHECK_RET( Ok(), wxT("invalid image") );
404 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
408 int width
= image
.GetWidth();
409 int height
= image
.GetHeight();
422 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
423 width
= M_IMGDATA
->m_width
- (x
+xx
);
424 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
425 height
= M_IMGDATA
->m_height
- (y
+yy
);
427 if (width
< 1) return;
428 if (height
< 1) return;
430 if ((!HasMask() && !image
.HasMask()) ||
431 ((HasMask() && image
.HasMask() &&
432 (GetMaskRed()==image
.GetMaskRed()) &&
433 (GetMaskGreen()==image
.GetMaskGreen()) &&
434 (GetMaskBlue()==image
.GetMaskBlue()))))
437 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
438 int source_step
= image
.GetWidth()*3;
440 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
441 int target_step
= M_IMGDATA
->m_width
*3;
442 for (int j
= 0; j
< height
; j
++)
444 memcpy( target_data
, source_data
, width
);
445 source_data
+= source_step
;
446 target_data
+= target_step
;
451 if (!HasMask() && image
.HasMask())
453 unsigned char r
= image
.GetMaskRed();
454 unsigned char g
= image
.GetMaskGreen();
455 unsigned char b
= image
.GetMaskBlue();
458 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
459 int source_step
= image
.GetWidth()*3;
461 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
462 int target_step
= M_IMGDATA
->m_width
*3;
464 for (int j
= 0; j
< height
; j
++)
466 for (int i
= 0; i
< width
; i
+=3)
468 if ((source_data
[i
] != r
) &&
469 (source_data
[i
+1] != g
) &&
470 (source_data
[i
+2] != b
))
472 memcpy( target_data
+i
, source_data
+i
, 3 );
475 source_data
+= source_step
;
476 target_data
+= target_step
;
481 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
482 unsigned char r2
, unsigned char g2
, unsigned char b2
)
484 wxCHECK_RET( Ok(), wxT("invalid image") );
486 char unsigned *data
= GetData();
488 const int w
= GetWidth();
489 const int h
= GetHeight();
491 for (int j
= 0; j
< h
; j
++)
492 for (int i
= 0; i
< w
; i
++)
494 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
504 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
508 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
510 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
512 char unsigned *data
= image
.GetData();
514 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
516 if (M_IMGDATA
->m_hasMask
)
518 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
519 M_IMGDATA
->m_maskBlue
== b
)
520 image
.SetMaskColour( 255, 255, 255 );
522 image
.SetMaskColour( 0, 0, 0 );
525 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
527 char unsigned *srcd
= M_IMGDATA
->m_data
;
528 char unsigned *tard
= image
.GetData();
530 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
532 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
533 tard
[0] = tard
[1] = tard
[2] = 255;
535 tard
[0] = tard
[1] = tard
[2] = 0;
541 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
543 wxCHECK_RET( Ok(), wxT("invalid image") );
545 int w
= M_IMGDATA
->m_width
;
546 int h
= M_IMGDATA
->m_height
;
548 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
550 long pos
= (y
* w
+ x
) * 3;
552 M_IMGDATA
->m_data
[ pos
] = r
;
553 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
554 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
557 unsigned char wxImage::GetRed( int x
, int y
) const
559 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
561 int w
= M_IMGDATA
->m_width
;
562 int h
= M_IMGDATA
->m_height
;
564 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
566 long pos
= (y
* w
+ x
) * 3;
568 return M_IMGDATA
->m_data
[pos
];
571 unsigned char wxImage::GetGreen( int x
, int y
) const
573 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
575 int w
= M_IMGDATA
->m_width
;
576 int h
= M_IMGDATA
->m_height
;
578 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
580 long pos
= (y
* w
+ x
) * 3;
582 return M_IMGDATA
->m_data
[pos
+1];
585 unsigned char wxImage::GetBlue( int x
, int y
) const
587 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
589 int w
= M_IMGDATA
->m_width
;
590 int h
= M_IMGDATA
->m_height
;
592 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
594 long pos
= (y
* w
+ x
) * 3;
596 return M_IMGDATA
->m_data
[pos
+2];
599 bool wxImage::Ok() const
601 // image of 0 width or height can't be considered ok - at least because it
602 // causes crashes in ConvertToBitmap() if we don't catch it in time
603 wxImageRefData
*data
= M_IMGDATA
;
604 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
607 char unsigned *wxImage::GetData() const
609 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
611 return M_IMGDATA
->m_data
;
614 void wxImage::SetData( char unsigned *data
)
616 wxCHECK_RET( Ok(), wxT("invalid image") );
618 wxImageRefData
*newRefData
= new wxImageRefData();
620 newRefData
->m_width
= M_IMGDATA
->m_width
;
621 newRefData
->m_height
= M_IMGDATA
->m_height
;
622 newRefData
->m_data
= data
;
623 newRefData
->m_ok
= TRUE
;
624 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
625 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
626 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
627 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
631 m_refData
= newRefData
;
634 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
636 wxImageRefData
*newRefData
= new wxImageRefData();
640 newRefData
->m_width
= new_width
;
641 newRefData
->m_height
= new_height
;
642 newRefData
->m_data
= data
;
643 newRefData
->m_ok
= TRUE
;
644 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
645 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
646 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
647 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
651 newRefData
->m_width
= new_width
;
652 newRefData
->m_height
= new_height
;
653 newRefData
->m_data
= data
;
654 newRefData
->m_ok
= TRUE
;
659 m_refData
= newRefData
;
662 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
664 wxCHECK_RET( Ok(), wxT("invalid image") );
666 M_IMGDATA
->m_maskRed
= r
;
667 M_IMGDATA
->m_maskGreen
= g
;
668 M_IMGDATA
->m_maskBlue
= b
;
669 M_IMGDATA
->m_hasMask
= TRUE
;
672 unsigned char wxImage::GetMaskRed() const
674 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
676 return M_IMGDATA
->m_maskRed
;
679 unsigned char wxImage::GetMaskGreen() const
681 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
683 return M_IMGDATA
->m_maskGreen
;
686 unsigned char wxImage::GetMaskBlue() const
688 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
690 return M_IMGDATA
->m_maskBlue
;
693 void wxImage::SetMask( bool mask
)
695 wxCHECK_RET( Ok(), wxT("invalid image") );
697 M_IMGDATA
->m_hasMask
= mask
;
700 bool wxImage::HasMask() const
702 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
704 return M_IMGDATA
->m_hasMask
;
707 int wxImage::GetWidth() const
709 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
711 return M_IMGDATA
->m_width
;
714 int wxImage::GetHeight() const
716 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
718 return M_IMGDATA
->m_height
;
723 bool wxImage::HasPalette() const
728 return M_IMGDATA
->m_palette
.Ok();
731 const wxPalette
& wxImage::GetPalette() const
733 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
735 return M_IMGDATA
->m_palette
;
738 void wxImage::SetPalette(const wxPalette
& palette
)
740 wxCHECK_RET( Ok(), wxT("invalid image") );
742 M_IMGDATA
->m_palette
= palette
;
745 // Option functions (arbitrary name/value mapping)
746 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
748 wxCHECK_RET( Ok(), wxT("invalid image") );
750 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
751 if (idx
== wxNOT_FOUND
)
753 M_IMGDATA
->m_optionNames
.Add(name
);
754 M_IMGDATA
->m_optionValues
.Add(value
);
758 M_IMGDATA
->m_optionNames
[idx
] = name
;
759 M_IMGDATA
->m_optionValues
[idx
] = value
;
763 void wxImage::SetOption(const wxString
& name
, int value
)
766 valStr
.Printf(wxT("%d"), value
);
767 SetOption(name
, valStr
);
770 wxString
wxImage::GetOption(const wxString
& name
) const
772 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
774 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
775 if (idx
== wxNOT_FOUND
)
776 return wxEmptyString
;
778 return M_IMGDATA
->m_optionValues
[idx
];
781 int wxImage::GetOptionInt(const wxString
& name
) const
783 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
785 return wxAtoi(GetOption(name
));
788 bool wxImage::HasOption(const wxString
& name
) const
790 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
792 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
795 bool wxImage::LoadFile( const wxString
& filename
, long type
)
798 if (wxFileExists(filename
))
800 wxFileInputStream
stream(filename
);
801 wxBufferedInputStream
bstream( stream
);
802 return LoadFile(bstream
, type
);
806 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
810 #else // !wxUSE_STREAMS
812 #endif // wxUSE_STREAMS
815 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
818 if (wxFileExists(filename
))
820 wxFileInputStream
stream(filename
);
821 wxBufferedInputStream
bstream( stream
);
822 return LoadFile(bstream
, mimetype
);
826 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
830 #else // !wxUSE_STREAMS
832 #endif // wxUSE_STREAMS
835 bool wxImage::SaveFile( const wxString
& filename
, int type
)
838 wxFileOutputStream
stream(filename
);
840 if ( stream
.LastError() == wxStream_NOERROR
)
842 wxBufferedOutputStream
bstream( stream
);
843 return SaveFile(bstream
, type
);
845 #endif // wxUSE_STREAMS
850 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
853 wxFileOutputStream
stream(filename
);
855 if ( stream
.LastError() == wxStream_NOERROR
)
857 wxBufferedOutputStream
bstream( stream
);
858 return SaveFile(bstream
, mimetype
);
860 #endif // wxUSE_STREAMS
865 bool wxImage::CanRead( const wxString
&name
)
868 wxFileInputStream
stream(name
);
869 return CanRead(stream
);
877 bool wxImage::CanRead( wxInputStream
&stream
)
879 wxList
&list
=GetHandlers();
881 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
883 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
884 if (handler
->CanRead( stream
))
891 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
895 m_refData
= new wxImageRefData
;
897 wxImageHandler
*handler
;
899 if (type
==wxBITMAP_TYPE_ANY
)
901 wxList
&list
=GetHandlers();
903 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
905 handler
=(wxImageHandler
*)node
->GetData();
906 if (handler
->CanRead( stream
))
907 return handler
->LoadFile( this, stream
);
911 wxLogWarning( _("No handler found for image type.") );
915 handler
= FindHandler(type
);
919 wxLogWarning( _("No image handler for type %d defined."), type
);
924 return handler
->LoadFile( this, stream
);
927 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
931 m_refData
= new wxImageRefData
;
933 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
937 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
942 return handler
->LoadFile( this, stream
);
945 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
947 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
949 wxImageHandler
*handler
= FindHandler(type
);
953 wxLogWarning( _("No image handler for type %d defined."), type
);
958 return handler
->SaveFile( this, stream
);
961 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
963 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
965 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
969 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
974 return handler
->SaveFile( this, stream
);
976 #endif // wxUSE_STREAMS
978 void wxImage::AddHandler( wxImageHandler
*handler
)
980 // make sure that the memory will be freed at the program end
981 sm_handlers
.DeleteContents(TRUE
);
983 sm_handlers
.Append( handler
);
986 void wxImage::InsertHandler( wxImageHandler
*handler
)
988 // make sure that the memory will be freed at the program end
989 sm_handlers
.DeleteContents(TRUE
);
991 sm_handlers
.Insert( handler
);
994 bool wxImage::RemoveHandler( const wxString
& name
)
996 wxImageHandler
*handler
= FindHandler(name
);
999 sm_handlers
.DeleteObject(handler
);
1006 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1008 wxNode
*node
= sm_handlers
.First();
1011 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1012 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1014 node
= node
->Next();
1016 return (wxImageHandler
*)NULL
;
1019 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1021 wxNode
*node
= sm_handlers
.First();
1024 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1025 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1026 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1028 node
= node
->Next();
1030 return (wxImageHandler
*)NULL
;
1033 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1035 wxNode
*node
= sm_handlers
.First();
1038 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1039 if (handler
->GetType() == bitmapType
) return handler
;
1040 node
= node
->Next();
1045 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1047 wxNode
*node
= sm_handlers
.First();
1050 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1051 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1052 node
= node
->Next();
1057 void wxImage::InitStandardHandlers()
1059 AddHandler(new wxBMPHandler
);
1060 #if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__)
1061 AddHandler(new wxXPMHandler
);
1065 void wxImage::CleanUpHandlers()
1067 wxNode
*node
= sm_handlers
.First();
1070 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1071 wxNode
*next
= node
->Next();
1078 //-----------------------------------------------------------------------------
1080 //-----------------------------------------------------------------------------
1082 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1085 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1090 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1095 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1100 bool wxImageHandler::CanRead( const wxString
& name
)
1102 if (wxFileExists(name
))
1104 wxFileInputStream
stream(name
);
1105 return CanRead(stream
);
1109 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1116 #endif // wxUSE_STREAMS
1120 //-----------------------------------------------------------------------------
1121 // wxBitmap convertion routines
1122 //-----------------------------------------------------------------------------
1127 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1129 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1130 wxBitmap
bitmap( mono
, 1 );
1135 wxBitmap
wxImage::ConvertToBitmap() const
1137 wxBitmap
bitmap( *this );
1141 wxImage::wxImage( const wxBitmap
&bitmap
)
1143 *this = bitmap
.ConvertToImage();
1150 // A module to allow wxImage initialization/cleanup
1151 // without calling these functions from app.cpp or from
1152 // the user's application.
1154 class wxImageModule
: public wxModule
1156 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1159 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1160 void OnExit() { wxImage::CleanUpHandlers(); };
1163 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1166 //-----------------------------------------------------------------------------
1169 // Counts and returns the number of different colours. Optionally stops
1170 // when it exceeds 'stopafter' different colours. This is useful, for
1171 // example, to see if the image can be saved as 8-bit (256 colour or
1172 // less, in this case it would be invoked as CountColours(256)). Default
1173 // value for stopafter is -1 (don't care).
1175 unsigned long wxImage::CountColours( unsigned long stopafter
)
1179 unsigned char r
, g
, b
;
1181 unsigned long size
, nentries
, key
;
1184 size
= GetWidth() * GetHeight();
1187 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1192 key
= (r
<< 16) | (g
<< 8) | b
;
1194 if (h
.Get(key
) == NULL
)
1206 // Computes the histogram of the image and fills a hash table, indexed
1207 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1208 // wxHNode contains an 'index' (useful to build a palette with the image
1209 // colours) and a 'value', which is the number of pixels in the image with
1212 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1214 unsigned char r
, g
, b
;
1216 unsigned long size
, nentries
, key
;
1220 size
= GetWidth() * GetHeight();
1223 for (unsigned long j
= 0; j
< size
; j
++)
1228 key
= (r
<< 16) | (g
<< 8) | b
;
1230 hnode
= (wxHNode
*) h
.Get(key
);
1236 hnode
= new wxHNode();
1237 hnode
->index
= nentries
++;
1240 h
.Put(key
, (wxObject
*)hnode
);
1248 * Rotation code by Carlos Moreno
1251 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1252 // does exactly the same thing. And I also got rid of wxRotationPixel
1253 // bacause of potential problems in architectures where alignment
1254 // is an issue, so I had to rewrite parts of the code.
1256 static const double gs_Epsilon
= 1e-10;
1258 static inline int wxCint (double x
)
1260 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1264 // Auxiliary function to rotate a point (x,y) with respect to point p0
1265 // make it inline and use a straight return to facilitate optimization
1266 // also, the function receives the sine and cosine of the angle to avoid
1267 // repeating the time-consuming calls to these functions -- sin/cos can
1268 // be computed and stored in the calling function.
1270 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1272 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1273 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1276 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1278 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1281 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1284 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1286 // Create pointer-based array to accelerate access to wxImage's data
1287 unsigned char ** data
= new unsigned char * [GetHeight()];
1289 data
[0] = GetData();
1291 for (i
= 1; i
< GetHeight(); i
++)
1292 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1294 // precompute coefficients for rotation formula
1295 // (sine and cosine of the angle)
1296 const double cos_angle
= cos(angle
);
1297 const double sin_angle
= sin(angle
);
1299 // Create new Image to store the result
1300 // First, find rectangle that covers the rotated image; to do that,
1301 // rotate the four corners
1303 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1305 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1306 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1307 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1308 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1310 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1311 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1312 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1313 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1315 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1317 if (offset_after_rotation
!= NULL
)
1319 *offset_after_rotation
= wxPoint (x1
, y1
);
1322 // GRG: The rotated (destination) image is always accessed
1323 // sequentially, so there is no need for a pointer-based
1324 // array here (and in fact it would be slower).
1326 unsigned char * dst
= rotated
.GetData();
1328 // GRG: if the original image has a mask, use its RGB values
1329 // as the blank pixel, else, fall back to default (black).
1331 unsigned char blank_r
= 0;
1332 unsigned char blank_g
= 0;
1333 unsigned char blank_b
= 0;
1337 blank_r
= GetMaskRed();
1338 blank_g
= GetMaskGreen();
1339 blank_b
= GetMaskBlue();
1340 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1343 // Now, for each point of the rotated image, find where it came from, by
1344 // performing an inverse rotation (a rotation of -angle) and getting the
1345 // pixel at those coordinates
1347 // GRG: I've taken the (interpolating) test out of the loops, so that
1348 // it is done only once, instead of repeating it for each pixel.
1353 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1355 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1357 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1359 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1360 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1362 // interpolate using the 4 enclosing grid-points. Those
1363 // points can be obtained using floor and ceiling of the
1364 // exact coordinates of the point
1365 // C.M. 2000-02-17: when the point is near the border, special care is required.
1369 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1371 x1
= wxCint(floor(src
.x
));
1372 x2
= wxCint(ceil(src
.x
));
1374 else // else means that x is near one of the borders (0 or width-1)
1376 x1
= x2
= wxCint (src
.x
);
1379 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1381 y1
= wxCint(floor(src
.y
));
1382 y2
= wxCint(ceil(src
.y
));
1386 y1
= y2
= wxCint (src
.y
);
1389 // get four points and the distances (square of the distance,
1390 // for efficiency reasons) for the interpolation formula
1392 // GRG: Do not calculate the points until they are
1393 // really needed -- this way we can calculate
1394 // just one, instead of four, if d1, d2, d3
1395 // or d4 are < gs_Epsilon
1397 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1398 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1399 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1400 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1402 // Now interpolate as a weighted average of the four surrounding
1403 // points, where the weights are the distances to each of those points
1405 // If the point is exactly at one point of the grid of the source
1406 // image, then don't interpolate -- just assign the pixel
1408 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1410 unsigned char *p
= data
[y1
] + (3 * x1
);
1415 else if (d2
< gs_Epsilon
)
1417 unsigned char *p
= data
[y1
] + (3 * x2
);
1422 else if (d3
< gs_Epsilon
)
1424 unsigned char *p
= data
[y2
] + (3 * x2
);
1429 else if (d4
< gs_Epsilon
)
1431 unsigned char *p
= data
[y2
] + (3 * x1
);
1438 // weights for the weighted average are proportional to the inverse of the distance
1439 unsigned char *v1
= data
[y1
] + (3 * x1
);
1440 unsigned char *v2
= data
[y1
] + (3 * x2
);
1441 unsigned char *v3
= data
[y2
] + (3 * x2
);
1442 unsigned char *v4
= data
[y2
] + (3 * x1
);
1444 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1448 *(dst
++) = (unsigned char)
1449 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1450 w3
* *(v3
++) + w4
* *(v4
++)) /
1451 (w1
+ w2
+ w3
+ w4
) );
1452 *(dst
++) = (unsigned char)
1453 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1454 w3
* *(v3
++) + w4
* *(v4
++)) /
1455 (w1
+ w2
+ w3
+ w4
) );
1456 *(dst
++) = (unsigned char)
1457 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1458 w3
* *(v3
++) + w4
* *(v4
++)) /
1459 (w1
+ w2
+ w3
+ w4
) );
1471 else // not interpolating
1473 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1475 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1477 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1479 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1480 const int ys
= wxCint (src
.y
); // closest integer
1482 if (0 <= xs
&& xs
< GetWidth() &&
1483 0 <= ys
&& ys
< GetHeight())
1485 unsigned char *p
= data
[ys
] + (3 * xs
);
1505 #endif // wxUSE_IMAGE