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
;
63 #endif // wxUSE_PALETTE
64 wxArrayString m_optionNames
;
65 wxArrayString m_optionValues
;
68 wxImageRefData::wxImageRefData()
72 m_data
= (unsigned char*) NULL
;
81 wxImageRefData::~wxImageRefData()
83 if (m_data
&& !m_static
)
87 wxList
wxImage::sm_handlers
;
91 //-----------------------------------------------------------------------------
93 #define M_IMGDATA ((wxImageRefData *)m_refData)
95 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
101 wxImage::wxImage( int width
, int height
)
103 Create( width
, height
);
106 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
108 Create( width
, height
, data
, static_data
);
111 wxImage::wxImage( const wxString
& name
, long type
)
113 LoadFile( name
, type
);
116 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
118 LoadFile( name
, mimetype
);
122 wxImage::wxImage( wxInputStream
& stream
, long type
)
124 LoadFile( stream
, type
);
127 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
129 LoadFile( stream
, mimetype
);
131 #endif // wxUSE_STREAMS
133 wxImage::wxImage( const wxImage
& image
)
138 wxImage::wxImage( const wxImage
* image
)
140 if (image
) Ref(*image
);
143 void wxImage::Create( int width
, int height
)
147 m_refData
= new wxImageRefData();
149 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
150 if (M_IMGDATA
->m_data
)
152 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
154 M_IMGDATA
->m_width
= width
;
155 M_IMGDATA
->m_height
= height
;
156 M_IMGDATA
->m_ok
= TRUE
;
164 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
168 m_refData
= new wxImageRefData();
170 M_IMGDATA
->m_data
= data
;
171 if (M_IMGDATA
->m_data
)
173 M_IMGDATA
->m_width
= width
;
174 M_IMGDATA
->m_height
= height
;
175 M_IMGDATA
->m_ok
= TRUE
;
176 M_IMGDATA
->m_static
= static_data
;
184 void wxImage::Destroy()
189 wxImage
wxImage::Copy() const
193 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
195 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
197 char unsigned *data
= image
.GetData();
199 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
201 if (M_IMGDATA
->m_hasMask
)
202 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
204 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
209 wxImage
wxImage::Scale( int width
, int height
) const
213 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
215 // can't scale to/from 0 size
216 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
217 wxT("invalid new image size") );
219 long old_height
= M_IMGDATA
->m_height
,
220 old_width
= M_IMGDATA
->m_width
;
221 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
222 wxT("invalid old image size") );
224 image
.Create( width
, height
);
226 char unsigned *data
= image
.GetData();
228 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
230 if (M_IMGDATA
->m_hasMask
)
232 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
233 M_IMGDATA
->m_maskGreen
,
234 M_IMGDATA
->m_maskBlue
);
237 char unsigned *source_data
= M_IMGDATA
->m_data
;
238 char unsigned *target_data
= data
;
241 // This is nonsense, RR.
243 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
244 // are from 0 to size-1, hence all decrement the sizes
245 long old_old_width
= old_width
;
250 for ( long j
= 0; j
<= height
; j
++ )
252 // don't crash for images with height == 1
253 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
255 for ( long i
= 0; i
<= width
; i
++ )
257 long x_offset
= width
? (i
* old_width
) / width
: 0;
259 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
264 for (long j
= 0; j
< height
; j
++)
266 long y_offset
= (j
* old_height
/ height
) * old_width
;
268 for (long i
= 0; i
< width
; i
++)
271 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
281 wxImage
wxImage::Rotate90( bool clockwise
) const
285 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
287 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
289 char unsigned *data
= image
.GetData();
291 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
293 if (M_IMGDATA
->m_hasMask
)
294 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
296 long height
= M_IMGDATA
->m_height
;
297 long width
= M_IMGDATA
->m_width
;
299 char unsigned *source_data
= M_IMGDATA
->m_data
;
300 char unsigned *target_data
;
302 for (long j
= 0; j
< height
; j
++)
304 for (long i
= 0; i
< width
; i
++)
307 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
309 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
310 memcpy( target_data
, source_data
, 3 );
318 wxImage
wxImage::Mirror( bool horizontally
) const
322 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
324 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
326 char unsigned *data
= image
.GetData();
328 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
330 if (M_IMGDATA
->m_hasMask
)
331 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
333 long height
= M_IMGDATA
->m_height
;
334 long width
= M_IMGDATA
->m_width
;
336 char unsigned *source_data
= M_IMGDATA
->m_data
;
337 char unsigned *target_data
;
341 for (long j
= 0; j
< height
; j
++)
344 target_data
= data
-3;
345 for (long i
= 0; i
< width
; i
++)
347 memcpy( target_data
, source_data
, 3 );
355 for (long i
= 0; i
< height
; i
++)
357 target_data
= data
+ 3*width
*(height
-1-i
);
358 memcpy( target_data
, source_data
, (size_t)3*width
);
359 source_data
+= 3*width
;
366 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
370 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
372 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
373 image
, wxT("invalid subimage size") );
375 int subwidth
=rect
.GetWidth();
376 const int subheight
=rect
.GetHeight();
378 image
.Create( subwidth
, subheight
);
380 char unsigned *subdata
= image
.GetData(), *data
=GetData();
382 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
384 if (M_IMGDATA
->m_hasMask
)
385 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
387 const int subleft
=3*rect
.GetLeft();
388 const int width
=3*GetWidth();
391 data
+=rect
.GetTop()*width
+subleft
;
393 for (long j
= 0; j
< subheight
; ++j
)
395 memcpy( subdata
, data
, subwidth
);
403 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
405 wxCHECK_RET( Ok(), wxT("invalid image") );
406 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
410 int width
= image
.GetWidth();
411 int height
= image
.GetHeight();
424 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
425 width
= M_IMGDATA
->m_width
- (x
+xx
);
426 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
427 height
= M_IMGDATA
->m_height
- (y
+yy
);
429 if (width
< 1) return;
430 if (height
< 1) return;
432 if ((!HasMask() && !image
.HasMask()) ||
433 ((HasMask() && image
.HasMask() &&
434 (GetMaskRed()==image
.GetMaskRed()) &&
435 (GetMaskGreen()==image
.GetMaskGreen()) &&
436 (GetMaskBlue()==image
.GetMaskBlue()))))
439 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
440 int source_step
= image
.GetWidth()*3;
442 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
443 int target_step
= M_IMGDATA
->m_width
*3;
444 for (int j
= 0; j
< height
; j
++)
446 memcpy( target_data
, source_data
, width
);
447 source_data
+= source_step
;
448 target_data
+= target_step
;
453 if (!HasMask() && image
.HasMask())
455 unsigned char r
= image
.GetMaskRed();
456 unsigned char g
= image
.GetMaskGreen();
457 unsigned char b
= image
.GetMaskBlue();
460 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
461 int source_step
= image
.GetWidth()*3;
463 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
464 int target_step
= M_IMGDATA
->m_width
*3;
466 for (int j
= 0; j
< height
; j
++)
468 for (int i
= 0; i
< width
; i
+=3)
470 if ((source_data
[i
] != r
) &&
471 (source_data
[i
+1] != g
) &&
472 (source_data
[i
+2] != b
))
474 memcpy( target_data
+i
, source_data
+i
, 3 );
477 source_data
+= source_step
;
478 target_data
+= target_step
;
483 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
484 unsigned char r2
, unsigned char g2
, unsigned char b2
)
486 wxCHECK_RET( Ok(), wxT("invalid image") );
488 char unsigned *data
= GetData();
490 const int w
= GetWidth();
491 const int h
= GetHeight();
493 for (int j
= 0; j
< h
; j
++)
494 for (int i
= 0; i
< w
; i
++)
496 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
506 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
510 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
512 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
514 char unsigned *data
= image
.GetData();
516 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
518 if (M_IMGDATA
->m_hasMask
)
520 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
521 M_IMGDATA
->m_maskBlue
== b
)
522 image
.SetMaskColour( 255, 255, 255 );
524 image
.SetMaskColour( 0, 0, 0 );
527 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
529 char unsigned *srcd
= M_IMGDATA
->m_data
;
530 char unsigned *tard
= image
.GetData();
532 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
534 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
535 tard
[0] = tard
[1] = tard
[2] = 255;
537 tard
[0] = tard
[1] = tard
[2] = 0;
543 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
545 wxCHECK_RET( Ok(), wxT("invalid image") );
547 int w
= M_IMGDATA
->m_width
;
548 int h
= M_IMGDATA
->m_height
;
550 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
552 long pos
= (y
* w
+ x
) * 3;
554 M_IMGDATA
->m_data
[ pos
] = r
;
555 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
556 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
559 unsigned char wxImage::GetRed( int x
, int y
) const
561 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
563 int w
= M_IMGDATA
->m_width
;
564 int h
= M_IMGDATA
->m_height
;
566 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
568 long pos
= (y
* w
+ x
) * 3;
570 return M_IMGDATA
->m_data
[pos
];
573 unsigned char wxImage::GetGreen( int x
, int y
) const
575 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
577 int w
= M_IMGDATA
->m_width
;
578 int h
= M_IMGDATA
->m_height
;
580 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
582 long pos
= (y
* w
+ x
) * 3;
584 return M_IMGDATA
->m_data
[pos
+1];
587 unsigned char wxImage::GetBlue( int x
, int y
) const
589 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
591 int w
= M_IMGDATA
->m_width
;
592 int h
= M_IMGDATA
->m_height
;
594 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
596 long pos
= (y
* w
+ x
) * 3;
598 return M_IMGDATA
->m_data
[pos
+2];
601 bool wxImage::Ok() const
603 // image of 0 width or height can't be considered ok - at least because it
604 // causes crashes in ConvertToBitmap() if we don't catch it in time
605 wxImageRefData
*data
= M_IMGDATA
;
606 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
609 char unsigned *wxImage::GetData() const
611 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
613 return M_IMGDATA
->m_data
;
616 void wxImage::SetData( char unsigned *data
)
618 wxCHECK_RET( Ok(), wxT("invalid image") );
620 wxImageRefData
*newRefData
= new wxImageRefData();
622 newRefData
->m_width
= M_IMGDATA
->m_width
;
623 newRefData
->m_height
= M_IMGDATA
->m_height
;
624 newRefData
->m_data
= data
;
625 newRefData
->m_ok
= TRUE
;
626 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
627 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
628 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
629 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
633 m_refData
= newRefData
;
636 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
638 wxImageRefData
*newRefData
= new wxImageRefData();
642 newRefData
->m_width
= new_width
;
643 newRefData
->m_height
= new_height
;
644 newRefData
->m_data
= data
;
645 newRefData
->m_ok
= TRUE
;
646 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
647 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
648 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
649 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
653 newRefData
->m_width
= new_width
;
654 newRefData
->m_height
= new_height
;
655 newRefData
->m_data
= data
;
656 newRefData
->m_ok
= TRUE
;
661 m_refData
= newRefData
;
664 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
666 wxCHECK_RET( Ok(), wxT("invalid image") );
668 M_IMGDATA
->m_maskRed
= r
;
669 M_IMGDATA
->m_maskGreen
= g
;
670 M_IMGDATA
->m_maskBlue
= b
;
671 M_IMGDATA
->m_hasMask
= TRUE
;
674 unsigned char wxImage::GetMaskRed() const
676 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
678 return M_IMGDATA
->m_maskRed
;
681 unsigned char wxImage::GetMaskGreen() const
683 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
685 return M_IMGDATA
->m_maskGreen
;
688 unsigned char wxImage::GetMaskBlue() const
690 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
692 return M_IMGDATA
->m_maskBlue
;
695 void wxImage::SetMask( bool mask
)
697 wxCHECK_RET( Ok(), wxT("invalid image") );
699 M_IMGDATA
->m_hasMask
= mask
;
702 bool wxImage::HasMask() const
704 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
706 return M_IMGDATA
->m_hasMask
;
709 int wxImage::GetWidth() const
711 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
713 return M_IMGDATA
->m_width
;
716 int wxImage::GetHeight() const
718 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
720 return M_IMGDATA
->m_height
;
724 bool wxImage::GetUnusedColour ( unsigned char * r
, unsigned char * g
, unsigned char * b
)
729 ComputeHistogram( hTable
);
731 // start with blackest color and work to lightest
732 // 0,0,0 is quite likely to be a used color
733 unsigned char r2
= 1;
734 unsigned char g2
= 0;
735 unsigned char b2
= 0;
737 key
= (r2
<< 16) | (g2
<< 8) | b2
;
739 while ( (wxHNode
*) hTable
.Get(key
) )
741 // color already used
753 wxLogError( _("GetUnusedColour:: No Unused Color in image ") );
759 key
= (r2
<< 16) | (g2
<< 8) | b2
;
766 bool wxImage::ApplyMask ( const wxImage
& mask
)
768 // what to do if we already have a mask ??
769 if (M_IMGDATA
->m_hasMask
|| mask
.HasMask() )
771 wxLogError( _("Image already masked") );
775 // check that the images are the same size
776 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
778 wxLogError( _("Image and Mask have different sizes") );
782 // find unused colour
783 unsigned char r
,g
,b
;
784 if (!GetUnusedColour (&r
, &g
, &b
))
786 wxLogError( _("No Unused Color in image being masked") );
790 char unsigned *imgdata
= GetData();
791 char unsigned *maskdata
= mask
.GetData();
793 const int w
= GetWidth();
794 const int h
= GetHeight();
796 for (int j
= 0; j
< h
; j
++)
797 for (int i
= 0; i
< w
; i
++)
799 if ((maskdata
[0] > 128) && (maskdata
[1] > 128) && (maskdata
[2] > 128 ))
809 M_IMGDATA
->m_maskRed
= r
;
810 M_IMGDATA
->m_maskGreen
= g
;
811 M_IMGDATA
->m_maskBlue
= b
;
812 M_IMGDATA
->m_hasMask
= TRUE
;
821 bool wxImage::HasPalette() const
826 return M_IMGDATA
->m_palette
.Ok();
829 const wxPalette
& wxImage::GetPalette() const
831 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
833 return M_IMGDATA
->m_palette
;
836 void wxImage::SetPalette(const wxPalette
& palette
)
838 wxCHECK_RET( Ok(), wxT("invalid image") );
840 M_IMGDATA
->m_palette
= palette
;
843 #endif // wxUSE_PALETTE
845 // Option functions (arbitrary name/value mapping)
846 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
848 wxCHECK_RET( Ok(), wxT("invalid image") );
850 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
851 if (idx
== wxNOT_FOUND
)
853 M_IMGDATA
->m_optionNames
.Add(name
);
854 M_IMGDATA
->m_optionValues
.Add(value
);
858 M_IMGDATA
->m_optionNames
[idx
] = name
;
859 M_IMGDATA
->m_optionValues
[idx
] = value
;
863 void wxImage::SetOption(const wxString
& name
, int value
)
866 valStr
.Printf(wxT("%d"), value
);
867 SetOption(name
, valStr
);
870 wxString
wxImage::GetOption(const wxString
& name
) const
872 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
874 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
875 if (idx
== wxNOT_FOUND
)
876 return wxEmptyString
;
878 return M_IMGDATA
->m_optionValues
[idx
];
881 int wxImage::GetOptionInt(const wxString
& name
) const
883 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
885 return wxAtoi(GetOption(name
));
888 bool wxImage::HasOption(const wxString
& name
) const
890 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
892 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
895 bool wxImage::LoadFile( const wxString
& filename
, long type
)
898 if (wxFileExists(filename
))
900 wxFileInputStream
stream(filename
);
901 wxBufferedInputStream
bstream( stream
);
902 return LoadFile(bstream
, type
);
906 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
910 #else // !wxUSE_STREAMS
912 #endif // wxUSE_STREAMS
915 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
918 if (wxFileExists(filename
))
920 wxFileInputStream
stream(filename
);
921 wxBufferedInputStream
bstream( stream
);
922 return LoadFile(bstream
, mimetype
);
926 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
930 #else // !wxUSE_STREAMS
932 #endif // wxUSE_STREAMS
935 bool wxImage::SaveFile( const wxString
& filename
, int type
)
938 wxFileOutputStream
stream(filename
);
940 if ( stream
.LastError() == wxStream_NOERROR
)
942 wxBufferedOutputStream
bstream( stream
);
943 return SaveFile(bstream
, type
);
945 #endif // wxUSE_STREAMS
950 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
953 wxFileOutputStream
stream(filename
);
955 if ( stream
.LastError() == wxStream_NOERROR
)
957 wxBufferedOutputStream
bstream( stream
);
958 return SaveFile(bstream
, mimetype
);
960 #endif // wxUSE_STREAMS
965 bool wxImage::CanRead( const wxString
&name
)
968 wxFileInputStream
stream(name
);
969 return CanRead(stream
);
977 bool wxImage::CanRead( wxInputStream
&stream
)
979 wxList
&list
=GetHandlers();
981 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
983 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
984 if (handler
->CanRead( stream
))
991 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
995 m_refData
= new wxImageRefData
;
997 wxImageHandler
*handler
;
999 if (type
==wxBITMAP_TYPE_ANY
)
1001 wxList
&list
=GetHandlers();
1003 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1005 handler
=(wxImageHandler
*)node
->GetData();
1006 if (handler
->CanRead( stream
))
1007 return handler
->LoadFile( this, stream
);
1011 wxLogWarning( _("No handler found for image type.") );
1015 handler
= FindHandler(type
);
1017 if (handler
== NULL
)
1019 wxLogWarning( _("No image handler for type %d defined."), type
);
1024 return handler
->LoadFile( this, stream
);
1027 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
1031 m_refData
= new wxImageRefData
;
1033 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1035 if (handler
== NULL
)
1037 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1042 return handler
->LoadFile( this, stream
);
1045 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
1047 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1049 wxImageHandler
*handler
= FindHandler(type
);
1051 if (handler
== NULL
)
1053 wxLogWarning( _("No image handler for type %d defined."), type
);
1058 return handler
->SaveFile( this, stream
);
1061 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
1063 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1065 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1067 if (handler
== NULL
)
1069 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1074 return handler
->SaveFile( this, stream
);
1076 #endif // wxUSE_STREAMS
1078 void wxImage::AddHandler( wxImageHandler
*handler
)
1080 // make sure that the memory will be freed at the program end
1081 sm_handlers
.DeleteContents(TRUE
);
1083 sm_handlers
.Append( handler
);
1086 void wxImage::InsertHandler( wxImageHandler
*handler
)
1088 // make sure that the memory will be freed at the program end
1089 sm_handlers
.DeleteContents(TRUE
);
1091 sm_handlers
.Insert( handler
);
1094 bool wxImage::RemoveHandler( const wxString
& name
)
1096 wxImageHandler
*handler
= FindHandler(name
);
1099 sm_handlers
.DeleteObject(handler
);
1106 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1108 wxNode
*node
= sm_handlers
.First();
1111 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1112 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1114 node
= node
->Next();
1116 return (wxImageHandler
*)NULL
;
1119 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1121 wxNode
*node
= sm_handlers
.First();
1124 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1125 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1126 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1128 node
= node
->Next();
1130 return (wxImageHandler
*)NULL
;
1133 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1135 wxNode
*node
= sm_handlers
.First();
1138 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1139 if (handler
->GetType() == bitmapType
) return handler
;
1140 node
= node
->Next();
1145 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1147 wxNode
*node
= sm_handlers
.First();
1150 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1151 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1152 node
= node
->Next();
1157 void wxImage::InitStandardHandlers()
1159 AddHandler(new wxBMPHandler
);
1160 #if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__)
1161 AddHandler(new wxXPMHandler
);
1165 void wxImage::CleanUpHandlers()
1167 wxNode
*node
= sm_handlers
.First();
1170 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1171 wxNode
*next
= node
->Next();
1178 //-----------------------------------------------------------------------------
1180 //-----------------------------------------------------------------------------
1182 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1185 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1190 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1195 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1200 bool wxImageHandler::CanRead( const wxString
& name
)
1202 if (wxFileExists(name
))
1204 wxFileInputStream
stream(name
);
1205 return CanRead(stream
);
1209 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1216 #endif // wxUSE_STREAMS
1220 //-----------------------------------------------------------------------------
1221 // wxBitmap convertion routines
1222 //-----------------------------------------------------------------------------
1227 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1229 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1230 wxBitmap
bitmap( mono
, 1 );
1235 wxBitmap
wxImage::ConvertToBitmap() const
1237 wxBitmap
bitmap( *this );
1241 wxImage::wxImage( const wxBitmap
&bitmap
)
1243 *this = bitmap
.ConvertToImage();
1250 // A module to allow wxImage initialization/cleanup
1251 // without calling these functions from app.cpp or from
1252 // the user's application.
1254 class wxImageModule
: public wxModule
1256 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1259 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1260 void OnExit() { wxImage::CleanUpHandlers(); };
1263 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1266 //-----------------------------------------------------------------------------
1269 // Counts and returns the number of different colours. Optionally stops
1270 // when it exceeds 'stopafter' different colours. This is useful, for
1271 // example, to see if the image can be saved as 8-bit (256 colour or
1272 // less, in this case it would be invoked as CountColours(256)). Default
1273 // value for stopafter is -1 (don't care).
1275 unsigned long wxImage::CountColours( unsigned long stopafter
)
1279 unsigned char r
, g
, b
;
1281 unsigned long size
, nentries
, key
;
1284 size
= GetWidth() * GetHeight();
1287 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1292 key
= (r
<< 16) | (g
<< 8) | b
;
1294 if (h
.Get(key
) == NULL
)
1306 // Computes the histogram of the image and fills a hash table, indexed
1307 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1308 // wxHNode contains an 'index' (useful to build a palette with the image
1309 // colours) and a 'value', which is the number of pixels in the image with
1312 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1314 unsigned char r
, g
, b
;
1316 unsigned long size
, nentries
, key
;
1320 size
= GetWidth() * GetHeight();
1323 for (unsigned long j
= 0; j
< size
; j
++)
1328 key
= (r
<< 16) | (g
<< 8) | b
;
1330 hnode
= (wxHNode
*) h
.Get(key
);
1336 hnode
= new wxHNode();
1337 hnode
->index
= nentries
++;
1340 h
.Put(key
, (wxObject
*)hnode
);
1348 * Rotation code by Carlos Moreno
1351 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1352 // does exactly the same thing. And I also got rid of wxRotationPixel
1353 // bacause of potential problems in architectures where alignment
1354 // is an issue, so I had to rewrite parts of the code.
1356 static const double gs_Epsilon
= 1e-10;
1358 static inline int wxCint (double x
)
1360 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1364 // Auxiliary function to rotate a point (x,y) with respect to point p0
1365 // make it inline and use a straight return to facilitate optimization
1366 // also, the function receives the sine and cosine of the angle to avoid
1367 // repeating the time-consuming calls to these functions -- sin/cos can
1368 // be computed and stored in the calling function.
1370 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1372 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1373 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1376 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1378 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1381 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1384 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1386 // Create pointer-based array to accelerate access to wxImage's data
1387 unsigned char ** data
= new unsigned char * [GetHeight()];
1389 data
[0] = GetData();
1391 for (i
= 1; i
< GetHeight(); i
++)
1392 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1394 // precompute coefficients for rotation formula
1395 // (sine and cosine of the angle)
1396 const double cos_angle
= cos(angle
);
1397 const double sin_angle
= sin(angle
);
1399 // Create new Image to store the result
1400 // First, find rectangle that covers the rotated image; to do that,
1401 // rotate the four corners
1403 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1405 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1406 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1407 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1408 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1410 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1411 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1412 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1413 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1415 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1417 if (offset_after_rotation
!= NULL
)
1419 *offset_after_rotation
= wxPoint (x1
, y1
);
1422 // GRG: The rotated (destination) image is always accessed
1423 // sequentially, so there is no need for a pointer-based
1424 // array here (and in fact it would be slower).
1426 unsigned char * dst
= rotated
.GetData();
1428 // GRG: if the original image has a mask, use its RGB values
1429 // as the blank pixel, else, fall back to default (black).
1431 unsigned char blank_r
= 0;
1432 unsigned char blank_g
= 0;
1433 unsigned char blank_b
= 0;
1437 blank_r
= GetMaskRed();
1438 blank_g
= GetMaskGreen();
1439 blank_b
= GetMaskBlue();
1440 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1443 // Now, for each point of the rotated image, find where it came from, by
1444 // performing an inverse rotation (a rotation of -angle) and getting the
1445 // pixel at those coordinates
1447 // GRG: I've taken the (interpolating) test out of the loops, so that
1448 // it is done only once, instead of repeating it for each pixel.
1453 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1455 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1457 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1459 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1460 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1462 // interpolate using the 4 enclosing grid-points. Those
1463 // points can be obtained using floor and ceiling of the
1464 // exact coordinates of the point
1465 // C.M. 2000-02-17: when the point is near the border, special care is required.
1469 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1471 x1
= wxCint(floor(src
.x
));
1472 x2
= wxCint(ceil(src
.x
));
1474 else // else means that x is near one of the borders (0 or width-1)
1476 x1
= x2
= wxCint (src
.x
);
1479 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1481 y1
= wxCint(floor(src
.y
));
1482 y2
= wxCint(ceil(src
.y
));
1486 y1
= y2
= wxCint (src
.y
);
1489 // get four points and the distances (square of the distance,
1490 // for efficiency reasons) for the interpolation formula
1492 // GRG: Do not calculate the points until they are
1493 // really needed -- this way we can calculate
1494 // just one, instead of four, if d1, d2, d3
1495 // or d4 are < gs_Epsilon
1497 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1498 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1499 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1500 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1502 // Now interpolate as a weighted average of the four surrounding
1503 // points, where the weights are the distances to each of those points
1505 // If the point is exactly at one point of the grid of the source
1506 // image, then don't interpolate -- just assign the pixel
1508 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1510 unsigned char *p
= data
[y1
] + (3 * x1
);
1515 else if (d2
< gs_Epsilon
)
1517 unsigned char *p
= data
[y1
] + (3 * x2
);
1522 else if (d3
< gs_Epsilon
)
1524 unsigned char *p
= data
[y2
] + (3 * x2
);
1529 else if (d4
< gs_Epsilon
)
1531 unsigned char *p
= data
[y2
] + (3 * x1
);
1538 // weights for the weighted average are proportional to the inverse of the distance
1539 unsigned char *v1
= data
[y1
] + (3 * x1
);
1540 unsigned char *v2
= data
[y1
] + (3 * x2
);
1541 unsigned char *v3
= data
[y2
] + (3 * x2
);
1542 unsigned char *v4
= data
[y2
] + (3 * x1
);
1544 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1548 *(dst
++) = (unsigned char)
1549 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1550 w3
* *(v3
++) + w4
* *(v4
++)) /
1551 (w1
+ w2
+ w3
+ w4
) );
1552 *(dst
++) = (unsigned char)
1553 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1554 w3
* *(v3
++) + w4
* *(v4
++)) /
1555 (w1
+ w2
+ w3
+ w4
) );
1556 *(dst
++) = (unsigned char)
1557 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1558 w3
* *(v3
++) + w4
* *(v4
++)) /
1559 (w1
+ w2
+ w3
+ w4
) );
1571 else // not interpolating
1573 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1575 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1577 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1579 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1580 const int ys
= wxCint (src
.y
); // closest integer
1582 if (0 <= xs
&& xs
< GetWidth() &&
1583 0 <= ys
&& ys
< GetHeight())
1585 unsigned char *p
= data
[ys
] + (3 * xs
);
1605 #endif // wxUSE_IMAGE