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
;
238 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
239 // are from 0 to size-1, hence all decrement the sizes
240 long old_old_width
= old_width
;
245 for ( long j
= 0; j
<= height
; j
++ )
247 // don't crash for images with height == 1
248 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
250 for ( long i
= 0; i
<= width
; i
++ )
252 long x_offset
= width
? (i
* old_width
) / width
: 0;
254 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
262 wxImage
wxImage::Rotate90( bool clockwise
) const
266 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
268 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
270 char unsigned *data
= image
.GetData();
272 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
274 if (M_IMGDATA
->m_hasMask
)
275 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
277 long height
= M_IMGDATA
->m_height
;
278 long width
= M_IMGDATA
->m_width
;
280 char unsigned *source_data
= M_IMGDATA
->m_data
;
281 char unsigned *target_data
;
283 for (long j
= 0; j
< height
; j
++)
285 for (long i
= 0; i
< width
; i
++)
288 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
290 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
291 memcpy( target_data
, source_data
, 3 );
299 wxImage
wxImage::Mirror( bool horizontally
) const
303 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
305 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
307 char unsigned *data
= image
.GetData();
309 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
311 if (M_IMGDATA
->m_hasMask
)
312 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
314 long height
= M_IMGDATA
->m_height
;
315 long width
= M_IMGDATA
->m_width
;
317 char unsigned *source_data
= M_IMGDATA
->m_data
;
318 char unsigned *target_data
;
322 for (long j
= 0; j
< height
; j
++)
325 target_data
= data
-3;
326 for (long i
= 0; i
< width
; i
++)
328 memcpy( target_data
, source_data
, 3 );
336 for (long i
= 0; i
< height
; i
++)
338 target_data
= data
+ 3*width
*(height
-1-i
);
339 memcpy( target_data
, source_data
, (size_t)3*width
);
340 source_data
+= 3*width
;
347 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
351 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
353 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
354 image
, wxT("invalid subimage size") );
356 int subwidth
=rect
.GetWidth();
357 const int subheight
=rect
.GetHeight();
359 image
.Create( subwidth
, subheight
);
361 char unsigned *subdata
= image
.GetData(), *data
=GetData();
363 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
365 if (M_IMGDATA
->m_hasMask
)
366 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
368 const int subleft
=3*rect
.GetLeft();
369 const int width
=3*GetWidth();
372 data
+=rect
.GetTop()*width
+subleft
;
374 for (long j
= 0; j
< subheight
; ++j
)
376 memcpy( subdata
, data
, subwidth
);
384 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
386 wxCHECK_RET( Ok(), wxT("invalid image") );
387 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
391 int width
= image
.GetWidth();
392 int height
= image
.GetHeight();
405 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
406 width
= M_IMGDATA
->m_width
- (x
+xx
);
407 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
408 height
= M_IMGDATA
->m_height
- (y
+yy
);
410 if (width
< 1) return;
411 if (height
< 1) return;
413 if ((!HasMask() && !image
.HasMask()) ||
414 ((HasMask() && image
.HasMask() &&
415 (GetMaskRed()==image
.GetMaskRed()) &&
416 (GetMaskGreen()==image
.GetMaskGreen()) &&
417 (GetMaskBlue()==image
.GetMaskBlue()))))
420 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
421 int source_step
= image
.GetWidth()*3;
423 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
424 int target_step
= M_IMGDATA
->m_width
*3;
425 for (int j
= 0; j
< height
; j
++)
427 memcpy( target_data
, source_data
, width
);
428 source_data
+= source_step
;
429 target_data
+= target_step
;
434 if (!HasMask() && image
.HasMask())
436 unsigned char r
= image
.GetMaskRed();
437 unsigned char g
= image
.GetMaskGreen();
438 unsigned char b
= image
.GetMaskBlue();
441 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
442 int source_step
= image
.GetWidth()*3;
444 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
445 int target_step
= M_IMGDATA
->m_width
*3;
447 for (int j
= 0; j
< height
; j
++)
449 for (int i
= 0; i
< width
; i
+=3)
451 if ((source_data
[i
] != r
) &&
452 (source_data
[i
+1] != g
) &&
453 (source_data
[i
+2] != b
))
455 memcpy( target_data
+i
, source_data
+i
, 3 );
458 source_data
+= source_step
;
459 target_data
+= target_step
;
464 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
465 unsigned char r2
, unsigned char g2
, unsigned char b2
)
467 wxCHECK_RET( Ok(), wxT("invalid image") );
469 char unsigned *data
= GetData();
471 const int w
= GetWidth();
472 const int h
= GetHeight();
474 for (int j
= 0; j
< h
; j
++)
475 for (int i
= 0; i
< w
; i
++)
477 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
487 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
491 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
493 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
495 char unsigned *data
= image
.GetData();
497 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
499 if (M_IMGDATA
->m_hasMask
)
501 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
502 M_IMGDATA
->m_maskBlue
== b
)
503 image
.SetMaskColour( 255, 255, 255 );
505 image
.SetMaskColour( 0, 0, 0 );
508 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
510 char unsigned *srcd
= M_IMGDATA
->m_data
;
511 char unsigned *tard
= image
.GetData();
513 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
515 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
516 tard
[0] = tard
[1] = tard
[2] = 255;
518 tard
[0] = tard
[1] = tard
[2] = 0;
524 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
526 wxCHECK_RET( Ok(), wxT("invalid image") );
528 int w
= M_IMGDATA
->m_width
;
529 int h
= M_IMGDATA
->m_height
;
531 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
533 long pos
= (y
* w
+ x
) * 3;
535 M_IMGDATA
->m_data
[ pos
] = r
;
536 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
537 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
540 unsigned char wxImage::GetRed( int x
, int y
) const
542 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
544 int w
= M_IMGDATA
->m_width
;
545 int h
= M_IMGDATA
->m_height
;
547 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
549 long pos
= (y
* w
+ x
) * 3;
551 return M_IMGDATA
->m_data
[pos
];
554 unsigned char wxImage::GetGreen( int x
, int y
) const
556 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
558 int w
= M_IMGDATA
->m_width
;
559 int h
= M_IMGDATA
->m_height
;
561 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
563 long pos
= (y
* w
+ x
) * 3;
565 return M_IMGDATA
->m_data
[pos
+1];
568 unsigned char wxImage::GetBlue( int x
, int y
) const
570 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
572 int w
= M_IMGDATA
->m_width
;
573 int h
= M_IMGDATA
->m_height
;
575 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
577 long pos
= (y
* w
+ x
) * 3;
579 return M_IMGDATA
->m_data
[pos
+2];
582 bool wxImage::Ok() const
584 // image of 0 width or height can't be considered ok - at least because it
585 // causes crashes in ConvertToBitmap() if we don't catch it in time
586 wxImageRefData
*data
= M_IMGDATA
;
587 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
590 char unsigned *wxImage::GetData() const
592 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
594 return M_IMGDATA
->m_data
;
597 void wxImage::SetData( char unsigned *data
)
599 wxCHECK_RET( Ok(), wxT("invalid image") );
601 wxImageRefData
*newRefData
= new wxImageRefData();
603 newRefData
->m_width
= M_IMGDATA
->m_width
;
604 newRefData
->m_height
= M_IMGDATA
->m_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 m_refData
= newRefData
;
617 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
619 wxImageRefData
*newRefData
= new wxImageRefData();
623 newRefData
->m_width
= new_width
;
624 newRefData
->m_height
= new_height
;
625 newRefData
->m_data
= data
;
626 newRefData
->m_ok
= TRUE
;
627 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
628 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
629 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
630 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
634 newRefData
->m_width
= new_width
;
635 newRefData
->m_height
= new_height
;
636 newRefData
->m_data
= data
;
637 newRefData
->m_ok
= TRUE
;
642 m_refData
= newRefData
;
645 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
647 wxCHECK_RET( Ok(), wxT("invalid image") );
649 M_IMGDATA
->m_maskRed
= r
;
650 M_IMGDATA
->m_maskGreen
= g
;
651 M_IMGDATA
->m_maskBlue
= b
;
652 M_IMGDATA
->m_hasMask
= TRUE
;
655 unsigned char wxImage::GetMaskRed() const
657 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
659 return M_IMGDATA
->m_maskRed
;
662 unsigned char wxImage::GetMaskGreen() const
664 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
666 return M_IMGDATA
->m_maskGreen
;
669 unsigned char wxImage::GetMaskBlue() const
671 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
673 return M_IMGDATA
->m_maskBlue
;
676 void wxImage::SetMask( bool mask
)
678 wxCHECK_RET( Ok(), wxT("invalid image") );
680 M_IMGDATA
->m_hasMask
= mask
;
683 bool wxImage::HasMask() const
685 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
687 return M_IMGDATA
->m_hasMask
;
690 int wxImage::GetWidth() const
692 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
694 return M_IMGDATA
->m_width
;
697 int wxImage::GetHeight() const
699 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
701 return M_IMGDATA
->m_height
;
706 bool wxImage::HasPalette() const
711 return M_IMGDATA
->m_palette
.Ok();
714 const wxPalette
& wxImage::GetPalette() const
716 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
718 return M_IMGDATA
->m_palette
;
721 void wxImage::SetPalette(const wxPalette
& palette
)
723 wxCHECK_RET( Ok(), wxT("invalid image") );
725 M_IMGDATA
->m_palette
= palette
;
728 // Option functions (arbitrary name/value mapping)
729 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
731 wxCHECK_RET( Ok(), wxT("invalid image") );
733 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
734 if (idx
== wxNOT_FOUND
)
736 M_IMGDATA
->m_optionNames
.Add(name
);
737 M_IMGDATA
->m_optionValues
.Add(value
);
741 M_IMGDATA
->m_optionNames
[idx
] = name
;
742 M_IMGDATA
->m_optionValues
[idx
] = value
;
746 void wxImage::SetOption(const wxString
& name
, int value
)
749 valStr
.Printf(wxT("%d"), value
);
750 SetOption(name
, valStr
);
753 wxString
wxImage::GetOption(const wxString
& name
) const
755 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
757 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
758 if (idx
== wxNOT_FOUND
)
759 return wxEmptyString
;
761 return M_IMGDATA
->m_optionValues
[idx
];
764 int wxImage::GetOptionInt(const wxString
& name
) const
766 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
768 return wxAtoi(GetOption(name
));
771 bool wxImage::HasOption(const wxString
& name
) const
773 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
775 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
778 bool wxImage::LoadFile( const wxString
& filename
, long type
)
781 if (wxFileExists(filename
))
783 wxFileInputStream
stream(filename
);
784 wxBufferedInputStream
bstream( stream
);
785 return LoadFile(bstream
, type
);
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::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
801 if (wxFileExists(filename
))
803 wxFileInputStream
stream(filename
);
804 wxBufferedInputStream
bstream( stream
);
805 return LoadFile(bstream
, mimetype
);
809 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
813 #else // !wxUSE_STREAMS
815 #endif // wxUSE_STREAMS
818 bool wxImage::SaveFile( const wxString
& filename
, int type
)
821 wxFileOutputStream
stream(filename
);
823 if ( stream
.LastError() == wxStream_NOERROR
)
825 wxBufferedOutputStream
bstream( stream
);
826 return SaveFile(bstream
, type
);
828 #endif // wxUSE_STREAMS
833 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
836 wxFileOutputStream
stream(filename
);
838 if ( stream
.LastError() == wxStream_NOERROR
)
840 wxBufferedOutputStream
bstream( stream
);
841 return SaveFile(bstream
, mimetype
);
843 #endif // wxUSE_STREAMS
848 bool wxImage::CanRead( const wxString
&name
)
851 wxFileInputStream
stream(name
);
852 return CanRead(stream
);
860 bool wxImage::CanRead( wxInputStream
&stream
)
862 wxList
&list
=GetHandlers();
864 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
866 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
867 if (handler
->CanRead( stream
))
874 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
878 m_refData
= new wxImageRefData
;
880 wxImageHandler
*handler
;
882 if (type
==wxBITMAP_TYPE_ANY
)
884 wxList
&list
=GetHandlers();
886 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
888 handler
=(wxImageHandler
*)node
->GetData();
889 if (handler
->CanRead( stream
))
890 return handler
->LoadFile( this, stream
);
894 wxLogWarning( _("No handler found for image type.") );
898 handler
= FindHandler(type
);
902 wxLogWarning( _("No image handler for type %d defined."), type
);
907 return handler
->LoadFile( this, stream
);
910 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
914 m_refData
= new wxImageRefData
;
916 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
920 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
925 return handler
->LoadFile( this, stream
);
928 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
930 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
932 wxImageHandler
*handler
= FindHandler(type
);
936 wxLogWarning( _("No image handler for type %d defined."), type
);
941 return handler
->SaveFile( this, stream
);
944 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
946 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
948 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
952 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
957 return handler
->SaveFile( this, stream
);
959 #endif // wxUSE_STREAMS
961 void wxImage::AddHandler( wxImageHandler
*handler
)
963 // make sure that the memory will be freed at the program end
964 sm_handlers
.DeleteContents(TRUE
);
966 sm_handlers
.Append( handler
);
969 void wxImage::InsertHandler( wxImageHandler
*handler
)
971 // make sure that the memory will be freed at the program end
972 sm_handlers
.DeleteContents(TRUE
);
974 sm_handlers
.Insert( handler
);
977 bool wxImage::RemoveHandler( const wxString
& name
)
979 wxImageHandler
*handler
= FindHandler(name
);
982 sm_handlers
.DeleteObject(handler
);
989 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
991 wxNode
*node
= sm_handlers
.First();
994 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
995 if (handler
->GetName().Cmp(name
) == 0) return handler
;
999 return (wxImageHandler
*)NULL
;
1002 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1004 wxNode
*node
= sm_handlers
.First();
1007 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1008 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1009 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1011 node
= node
->Next();
1013 return (wxImageHandler
*)NULL
;
1016 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1018 wxNode
*node
= sm_handlers
.First();
1021 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1022 if (handler
->GetType() == bitmapType
) return handler
;
1023 node
= node
->Next();
1028 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1030 wxNode
*node
= sm_handlers
.First();
1033 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1034 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1035 node
= node
->Next();
1040 void wxImage::InitStandardHandlers()
1042 AddHandler(new wxBMPHandler
);
1043 #if !defined(__WXGTK__) && !defined(__WXMOTIF__)
1044 AddHandler(new wxXPMHandler
);
1048 void wxImage::CleanUpHandlers()
1050 wxNode
*node
= sm_handlers
.First();
1053 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1054 wxNode
*next
= node
->Next();
1061 //-----------------------------------------------------------------------------
1063 //-----------------------------------------------------------------------------
1065 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1068 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1073 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1078 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1083 bool wxImageHandler::CanRead( const wxString
& name
)
1085 if (wxFileExists(name
))
1087 wxFileInputStream
stream(name
);
1088 return CanRead(stream
);
1092 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1099 #endif // wxUSE_STREAMS
1103 //-----------------------------------------------------------------------------
1104 // wxBitmap convertion routines
1105 //-----------------------------------------------------------------------------
1110 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1112 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1113 wxBitmap
bitmap( mono
, 1 );
1118 wxBitmap
wxImage::ConvertToBitmap() const
1120 wxBitmap
bitmap( *this );
1124 wxImage::wxImage( const wxBitmap
&bitmap
)
1126 *this = bitmap
.ConvertToImage();
1133 // A module to allow wxImage initialization/cleanup
1134 // without calling these functions from app.cpp or from
1135 // the user's application.
1137 class wxImageModule
: public wxModule
1139 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1142 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1143 void OnExit() { wxImage::CleanUpHandlers(); };
1146 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1149 //-----------------------------------------------------------------------------
1152 // Counts and returns the number of different colours. Optionally stops
1153 // when it exceeds 'stopafter' different colours. This is useful, for
1154 // example, to see if the image can be saved as 8-bit (256 colour or
1155 // less, in this case it would be invoked as CountColours(256)). Default
1156 // value for stopafter is -1 (don't care).
1158 unsigned long wxImage::CountColours( unsigned long stopafter
)
1162 unsigned char r
, g
, b
;
1164 unsigned long size
, nentries
, key
;
1167 size
= GetWidth() * GetHeight();
1170 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1175 key
= (r
<< 16) | (g
<< 8) | b
;
1177 if (h
.Get(key
) == NULL
)
1189 // Computes the histogram of the image and fills a hash table, indexed
1190 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1191 // wxHNode contains an 'index' (useful to build a palette with the image
1192 // colours) and a 'value', which is the number of pixels in the image with
1195 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1197 unsigned char r
, g
, b
;
1199 unsigned long size
, nentries
, key
;
1203 size
= GetWidth() * GetHeight();
1206 for (unsigned long j
= 0; j
< size
; j
++)
1211 key
= (r
<< 16) | (g
<< 8) | b
;
1213 hnode
= (wxHNode
*) h
.Get(key
);
1219 hnode
= new wxHNode();
1220 hnode
->index
= nentries
++;
1223 h
.Put(key
, (wxObject
*)hnode
);
1231 * Rotation code by Carlos Moreno
1234 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1235 // does exactly the same thing. And I also got rid of wxRotationPixel
1236 // bacause of potential problems in architectures where alignment
1237 // is an issue, so I had to rewrite parts of the code.
1239 static const double gs_Epsilon
= 1e-10;
1241 static inline int wxCint (double x
)
1243 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1247 // Auxiliary function to rotate a point (x,y) with respect to point p0
1248 // make it inline and use a straight return to facilitate optimization
1249 // also, the function receives the sine and cosine of the angle to avoid
1250 // repeating the time-consuming calls to these functions -- sin/cos can
1251 // be computed and stored in the calling function.
1253 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1255 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1256 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1259 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1261 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1264 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1267 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1269 // Create pointer-based array to accelerate access to wxImage's data
1270 unsigned char ** data
= new unsigned char * [GetHeight()];
1272 data
[0] = GetData();
1274 for (i
= 1; i
< GetHeight(); i
++)
1275 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1277 // precompute coefficients for rotation formula
1278 // (sine and cosine of the angle)
1279 const double cos_angle
= cos(angle
);
1280 const double sin_angle
= sin(angle
);
1282 // Create new Image to store the result
1283 // First, find rectangle that covers the rotated image; to do that,
1284 // rotate the four corners
1286 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1288 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1289 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1290 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1291 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1293 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1294 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1295 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1296 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1298 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1300 if (offset_after_rotation
!= NULL
)
1302 *offset_after_rotation
= wxPoint (x1
, y1
);
1305 // GRG: The rotated (destination) image is always accessed
1306 // sequentially, so there is no need for a pointer-based
1307 // array here (and in fact it would be slower).
1309 unsigned char * dst
= rotated
.GetData();
1311 // GRG: if the original image has a mask, use its RGB values
1312 // as the blank pixel, else, fall back to default (black).
1314 unsigned char blank_r
= 0;
1315 unsigned char blank_g
= 0;
1316 unsigned char blank_b
= 0;
1320 blank_r
= GetMaskRed();
1321 blank_g
= GetMaskGreen();
1322 blank_b
= GetMaskBlue();
1323 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1326 // Now, for each point of the rotated image, find where it came from, by
1327 // performing an inverse rotation (a rotation of -angle) and getting the
1328 // pixel at those coordinates
1330 // GRG: I've taken the (interpolating) test out of the loops, so that
1331 // it is done only once, instead of repeating it for each pixel.
1336 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1338 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1340 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1342 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1343 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1345 // interpolate using the 4 enclosing grid-points. Those
1346 // points can be obtained using floor and ceiling of the
1347 // exact coordinates of the point
1348 // C.M. 2000-02-17: when the point is near the border, special care is required.
1352 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1354 x1
= wxCint(floor(src
.x
));
1355 x2
= wxCint(ceil(src
.x
));
1357 else // else means that x is near one of the borders (0 or width-1)
1359 x1
= x2
= wxCint (src
.x
);
1362 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1364 y1
= wxCint(floor(src
.y
));
1365 y2
= wxCint(ceil(src
.y
));
1369 y1
= y2
= wxCint (src
.y
);
1372 // get four points and the distances (square of the distance,
1373 // for efficiency reasons) for the interpolation formula
1375 // GRG: Do not calculate the points until they are
1376 // really needed -- this way we can calculate
1377 // just one, instead of four, if d1, d2, d3
1378 // or d4 are < gs_Epsilon
1380 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1381 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1382 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1383 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1385 // Now interpolate as a weighted average of the four surrounding
1386 // points, where the weights are the distances to each of those points
1388 // If the point is exactly at one point of the grid of the source
1389 // image, then don't interpolate -- just assign the pixel
1391 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1393 unsigned char *p
= data
[y1
] + (3 * x1
);
1398 else if (d2
< gs_Epsilon
)
1400 unsigned char *p
= data
[y1
] + (3 * x2
);
1405 else if (d3
< gs_Epsilon
)
1407 unsigned char *p
= data
[y2
] + (3 * x2
);
1412 else if (d4
< gs_Epsilon
)
1414 unsigned char *p
= data
[y2
] + (3 * x1
);
1421 // weights for the weighted average are proportional to the inverse of the distance
1422 unsigned char *v1
= data
[y1
] + (3 * x1
);
1423 unsigned char *v2
= data
[y1
] + (3 * x2
);
1424 unsigned char *v3
= data
[y2
] + (3 * x2
);
1425 unsigned char *v4
= data
[y2
] + (3 * x1
);
1427 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1431 *(dst
++) = (unsigned char)
1432 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1433 w3
* *(v3
++) + w4
* *(v4
++)) /
1434 (w1
+ w2
+ w3
+ w4
) );
1435 *(dst
++) = (unsigned char)
1436 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1437 w3
* *(v3
++) + w4
* *(v4
++)) /
1438 (w1
+ w2
+ w3
+ w4
) );
1439 *(dst
++) = (unsigned char)
1440 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1441 w3
* *(v3
++) + w4
* *(v4
++)) /
1442 (w1
+ w2
+ w3
+ w4
) );
1454 else // not interpolating
1456 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1458 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1460 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1462 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1463 const int ys
= wxCint (src
.y
); // closest integer
1465 if (0 <= xs
&& xs
< GetWidth() &&
1466 0 <= ys
&& ys
< GetHeight())
1468 unsigned char *p
= data
[ys
] + (3 * xs
);
1488 #endif // wxUSE_IMAGE