1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
39 #include "wx/msw/private.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxImageRefData
: public wxObjectRefData
55 unsigned char *m_data
;
57 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
61 wxImageRefData::wxImageRefData()
65 m_data
= (unsigned char*) NULL
;
73 wxImageRefData::~wxImageRefData()
75 if (m_data
) free( m_data
);
78 wxList
wxImage::sm_handlers
;
80 //-----------------------------------------------------------------------------
82 #define M_IMGDATA ((wxImageRefData *)m_refData)
84 #if !USE_SHARED_LIBRARIES
85 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
92 wxImage::wxImage( int width
, int height
)
94 Create( width
, height
);
97 wxImage::wxImage( const wxString
& name
, long type
)
99 LoadFile( name
, type
);
102 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
104 LoadFile( name
, mimetype
);
108 wxImage::wxImage( wxInputStream
& stream
, long type
)
110 LoadFile( stream
, type
);
113 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
115 LoadFile( stream
, mimetype
);
117 #endif // wxUSE_STREAMS
119 wxImage::wxImage( const wxImage
& image
)
124 wxImage::wxImage( const wxImage
* image
)
126 if (image
) Ref(*image
);
129 void wxImage::Create( int width
, int height
)
131 m_refData
= new wxImageRefData();
133 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
134 if (M_IMGDATA
->m_data
)
136 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
138 M_IMGDATA
->m_width
= width
;
139 M_IMGDATA
->m_height
= height
;
140 M_IMGDATA
->m_ok
= TRUE
;
148 void wxImage::Destroy()
153 wxImage
wxImage::Scale( int width
, int height
) const
157 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
159 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, _T("invalid image size") );
161 image
.Create( width
, height
);
163 char unsigned *data
= image
.GetData();
165 wxCHECK_MSG( data
, image
, _T("unable to create image") );
167 if (M_IMGDATA
->m_hasMask
)
168 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
170 long old_height
= M_IMGDATA
->m_height
;
171 long old_width
= M_IMGDATA
->m_width
;
173 char unsigned *source_data
= M_IMGDATA
->m_data
;
174 char unsigned *target_data
= data
;
176 for (long j
= 0; j
< height
; j
++)
178 long y_offset
= (j
* old_height
/ height
) * old_width
;
180 for (long i
= 0; i
< width
; i
++)
183 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
192 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
196 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
198 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight())
199 , image
, _T("invalid subimage size") );
201 int subwidth
=rect
.GetWidth();
202 const int subheight
=rect
.GetHeight();
204 image
.Create( subwidth
, subheight
);
206 char unsigned *subdata
= image
.GetData(), *data
=GetData();
208 wxCHECK_MSG( subdata
, image
, _T("unable to create image") );
210 if (M_IMGDATA
->m_hasMask
)
211 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
213 const int subleft
=3*rect
.GetLeft();
214 const int width
=3*GetWidth();
217 data
+=rect
.GetTop()*width
+subleft
;
219 for (long j
= 0; j
< subheight
; ++j
)
221 memcpy( subdata
, data
, subwidth
);
229 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
231 wxCHECK_RET( Ok(), _T("invalid image") );
233 int w
= M_IMGDATA
->m_width
;
234 int h
= M_IMGDATA
->m_height
;
236 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), _T("invalid image index") );
238 long pos
= (y
* w
+ x
) * 3;
240 M_IMGDATA
->m_data
[ pos
] = r
;
241 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
242 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
245 unsigned char wxImage::GetRed( int x
, int y
)
247 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
249 int w
= M_IMGDATA
->m_width
;
250 int h
= M_IMGDATA
->m_height
;
252 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
254 long pos
= (y
* w
+ x
) * 3;
256 return M_IMGDATA
->m_data
[pos
];
259 unsigned char wxImage::GetGreen( int x
, int y
)
261 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
263 int w
= M_IMGDATA
->m_width
;
264 int h
= M_IMGDATA
->m_height
;
266 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
268 long pos
= (y
* w
+ x
) * 3;
270 return M_IMGDATA
->m_data
[pos
+1];
273 unsigned char wxImage::GetBlue( int x
, int y
)
275 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
277 int w
= M_IMGDATA
->m_width
;
278 int h
= M_IMGDATA
->m_height
;
280 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
282 long pos
= (y
* w
+ x
) * 3;
284 return M_IMGDATA
->m_data
[pos
+2];
287 bool wxImage::Ok() const
289 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
292 char unsigned *wxImage::GetData() const
294 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, _T("invalid image") );
296 return M_IMGDATA
->m_data
;
299 void wxImage::SetData( char unsigned *data
)
301 wxCHECK_RET( Ok(), _T("invalid image") );
303 memcpy(M_IMGDATA
->m_data
, data
, M_IMGDATA
->m_width
* M_IMGDATA
->m_height
* 3);
306 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
308 wxCHECK_RET( Ok(), _T("invalid image") );
310 M_IMGDATA
->m_maskRed
= r
;
311 M_IMGDATA
->m_maskGreen
= g
;
312 M_IMGDATA
->m_maskBlue
= b
;
313 M_IMGDATA
->m_hasMask
= TRUE
;
316 unsigned char wxImage::GetMaskRed() const
318 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
320 return M_IMGDATA
->m_maskRed
;
323 unsigned char wxImage::GetMaskGreen() const
325 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
327 return M_IMGDATA
->m_maskGreen
;
330 unsigned char wxImage::GetMaskBlue() const
332 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
334 return M_IMGDATA
->m_maskBlue
;
337 void wxImage::SetMask( bool mask
)
339 wxCHECK_RET( Ok(), _T("invalid image") );
341 M_IMGDATA
->m_hasMask
= mask
;
344 bool wxImage::HasMask() const
346 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
348 return M_IMGDATA
->m_hasMask
;
351 int wxImage::GetWidth() const
353 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
355 return M_IMGDATA
->m_width
;
358 int wxImage::GetHeight() const
360 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
362 return M_IMGDATA
->m_height
;
365 bool wxImage::LoadFile( const wxString
& filename
, long type
)
368 if (wxFileExists(filename
))
370 wxFileInputStream
stream(filename
);
371 return LoadFile(stream
, type
);
375 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
379 #else // !wxUSE_STREAMS
381 #endif // wxUSE_STREAMS
384 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
387 if (wxFileExists(filename
))
389 wxFileInputStream
stream(filename
);
390 return LoadFile(stream
, mimetype
);
394 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
398 #else // !wxUSE_STREAMS
400 #endif // wxUSE_STREAMS
403 bool wxImage::SaveFile( const wxString
& filename
, int type
)
406 wxFileOutputStream
stream(filename
);
408 if ( stream
.LastError() == wxStream_NOERROR
)
409 return SaveFile(stream
, type
);
411 #endif // wxUSE_STREAMS
415 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
418 wxFileOutputStream
stream(filename
);
420 if ( stream
.LastError() == wxStream_NOERROR
)
421 return SaveFile(stream
, mimetype
);
423 #endif // wxUSE_STREAMS
429 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
433 m_refData
= new wxImageRefData
;
435 wxImageHandler
*handler
;
437 if (type
==wxBITMAP_TYPE_ANY
)
439 wxList
&list
=GetHandlers();
441 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
443 handler
=(wxImageHandler
*)node
->GetData();
444 if (handler
->CanRead( stream
))
445 return handler
->LoadFile( this, stream
);
449 wxLogWarning( _T("No handler found for this image.") );
453 handler
= FindHandler(type
);
457 wxLogWarning( _T("No image handler for type %d defined."), type
);
462 return handler
->LoadFile( this, stream
);
465 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
469 m_refData
= new wxImageRefData
;
471 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
475 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
480 return handler
->LoadFile( this, stream
);
483 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
485 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
487 wxImageHandler
*handler
= FindHandler(type
);
491 wxLogWarning( _T("No image handler for type %d defined."), type
);
496 return handler
->SaveFile( this, stream
);
499 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
501 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
503 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
507 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
512 return handler
->SaveFile( this, stream
);
514 #endif // wxUSE_STREAMS
516 void wxImage::AddHandler( wxImageHandler
*handler
)
518 // make sure that the memory will be freed at the program end
519 sm_handlers
.DeleteContents(TRUE
);
521 sm_handlers
.Append( handler
);
524 void wxImage::InsertHandler( wxImageHandler
*handler
)
526 // make sure that the memory will be freed at the program end
527 sm_handlers
.DeleteContents(TRUE
);
529 sm_handlers
.Insert( handler
);
532 bool wxImage::RemoveHandler( const wxString
& name
)
534 wxImageHandler
*handler
= FindHandler(name
);
537 sm_handlers
.DeleteObject(handler
);
544 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
546 wxNode
*node
= sm_handlers
.First();
549 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
550 if (handler
->GetName().Cmp(name
) == 0) return handler
;
554 return (wxImageHandler
*)NULL
;
557 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
559 wxNode
*node
= sm_handlers
.First();
562 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
563 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
564 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
568 return (wxImageHandler
*)NULL
;
571 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
573 wxNode
*node
= sm_handlers
.First();
576 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
577 if (handler
->GetType() == bitmapType
) return handler
;
583 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
585 wxNode
*node
= sm_handlers
.First();
588 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
589 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
595 void wxImage::InitStandardHandlers()
597 AddHandler( new wxBMPHandler
);
600 void wxImage::CleanUpHandlers()
602 wxNode
*node
= sm_handlers
.First();
605 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
606 wxNode
*next
= node
->Next();
613 //-----------------------------------------------------------------------------
615 //-----------------------------------------------------------------------------
617 #if !USE_SHARED_LIBRARIES
618 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
622 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
627 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
632 bool wxImageHandler::CanRead( wxInputStream
& stream
)
637 bool wxImageHandler::CanRead( const wxString
& name
)
640 if (wxFileExists(name
))
642 wxFileInputStream
stream(name
);
643 return CanRead(stream
);
647 wxLogError( _T("Can't check image format of file '%s': file does not exist."), name
.c_str() );
651 #else // !wxUSE_STREAMS
653 #endif // wxUSE_STREAMS
658 #endif // wxUSE_STREAMS
660 //-----------------------------------------------------------------------------
661 // MSW conversion routines
662 //-----------------------------------------------------------------------------
666 wxBitmap
wxImage::ConvertToBitmap() const
671 // sizeLimit is the MS upper limit for the DIB size
672 int sizeLimit
= 1024*768*3;
674 // width and height of the device-dependent bitmap
675 int width
= GetWidth();
676 int bmpHeight
= GetHeight();
678 // calc the number of bytes per scanline and padding
679 int bytePerLine
= width
*3;
680 int sizeDWORD
= sizeof( DWORD
);
681 int lineBoundary
= bytePerLine
% sizeDWORD
;
683 if( lineBoundary
> 0 )
685 padding
= sizeDWORD
- lineBoundary
;
686 bytePerLine
+= padding
;
688 // calc the number of DIBs and heights of DIBs
691 int height
= sizeLimit
/bytePerLine
;
692 if( height
>= bmpHeight
)
696 numDIB
= bmpHeight
/ height
;
697 hRemain
= bmpHeight
% height
;
698 if( hRemain
>0 ) numDIB
++;
701 // set bitmap parameters
703 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
704 bitmap
.SetWidth( width
);
705 bitmap
.SetHeight( bmpHeight
);
706 bitmap
.SetDepth( wxDisplayDepth() );
708 // create a DIB header
709 int headersize
= sizeof(BITMAPINFOHEADER
);
710 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
711 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
712 // Fill in the DIB header
713 lpDIBh
->bmiHeader
.biSize
= headersize
;
714 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
715 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
716 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
717 // the general formula for biSizeImage:
718 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
719 lpDIBh
->bmiHeader
.biPlanes
= 1;
720 lpDIBh
->bmiHeader
.biBitCount
= 24;
721 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
722 lpDIBh
->bmiHeader
.biClrUsed
= 0;
723 // These seem not really needed for our purpose here.
724 lpDIBh
->bmiHeader
.biClrImportant
= 0;
725 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
726 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
727 // memory for DIB data
728 unsigned char *lpBits
;
729 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
732 wxFAIL_MSG( _T("could not allocate memory for DIB") );
737 // create and set the device-dependent bitmap
738 HDC hdc
= ::GetDC(NULL
);
739 HDC memdc
= ::CreateCompatibleDC( hdc
);
741 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
742 ::SelectObject( memdc
, hbitmap
);
744 // copy image data into DIB data and then into DDB (in a loop)
745 unsigned char *data
= GetData();
748 unsigned char *ptdata
= data
;
749 unsigned char *ptbits
;
751 for( n
=0; n
<numDIB
; n
++ )
753 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
755 // redefine height and size of the (possibly) last smaller DIB
756 // memory is not reallocated
758 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
759 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
763 for( j
=0; j
<height
; j
++ )
765 for( i
=0; i
<width
; i
++ )
767 *(ptbits
++) = *(ptdata
+2);
768 *(ptbits
++) = *(ptdata
+1);
769 *(ptbits
++) = *(ptdata
);
772 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
774 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
775 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
777 // if numDIB = 1, lines below can also be used
778 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
779 // The above line is equivalent to the following two lines.
780 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
781 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
782 // or the following lines
783 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
784 // HDC memdc = ::CreateCompatibleDC( hdc );
785 // ::SelectObject( memdc, hbitmap);
786 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
787 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
788 // ::SelectObject( memdc, 0 );
789 // ::DeleteDC( memdc );
791 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
793 // similarly, created an mono-bitmap for the possible mask
796 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
797 ::SelectObject( memdc
, hbitmap
);
798 if( numDIB
== 1 ) height
= bmpHeight
;
799 else height
= sizeLimit
/bytePerLine
;
800 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
801 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
803 unsigned char r
= GetMaskRed();
804 unsigned char g
= GetMaskGreen();
805 unsigned char b
= GetMaskBlue();
806 unsigned char zero
= 0, one
= 255;
808 for( n
=0; n
<numDIB
; n
++ )
810 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
812 // redefine height and size of the (possibly) last smaller DIB
813 // memory is not reallocated
815 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
816 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
819 for( int j
=0; j
<height
; j
++ )
821 for(i
=0; i
<width
; i
++ )
823 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
836 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
838 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
839 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
842 // create a wxMask object
843 wxMask
*mask
= new wxMask();
844 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
845 bitmap
.SetMask( mask
);
846 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
847 /* The following can also be used but is slow to run
848 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
849 wxMask *mask = new wxMask( bitmap, colour );
850 bitmap.SetMask( mask );
854 // free allocated resources
855 ::SelectObject( memdc
, 0 );
857 ::ReleaseDC(NULL
, hdc
);
861 // check the wxBitmap object
862 if( bitmap
.GetHBITMAP() )
863 bitmap
.SetOk( TRUE
);
865 bitmap
.SetOk( FALSE
);
870 wxImage::wxImage( const wxBitmap
&bitmap
)
875 wxFAIL_MSG( _T("invalid bitmap") );
879 // create an wxImage object
880 int width
= bitmap
.GetWidth();
881 int height
= bitmap
.GetHeight();
882 Create( width
, height
);
883 unsigned char *data
= GetData();
886 wxFAIL_MSG( _T("could not allocate data for image") );
890 // calc the number of bytes per scanline and padding in the DIB
891 int bytePerLine
= width
*3;
892 int sizeDWORD
= sizeof( DWORD
);
893 int lineBoundary
= bytePerLine
% sizeDWORD
;
895 if( lineBoundary
> 0 )
897 padding
= sizeDWORD
- lineBoundary
;
898 bytePerLine
+= padding
;
901 // create a DIB header
902 int headersize
= sizeof(BITMAPINFOHEADER
);
903 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
906 wxFAIL_MSG( _T("could not allocate data for DIB header") );
910 // Fill in the DIB header
911 lpDIBh
->bmiHeader
.biSize
= headersize
;
912 lpDIBh
->bmiHeader
.biWidth
= width
;
913 lpDIBh
->bmiHeader
.biHeight
= -height
;
914 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
915 lpDIBh
->bmiHeader
.biPlanes
= 1;
916 lpDIBh
->bmiHeader
.biBitCount
= 24;
917 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
918 lpDIBh
->bmiHeader
.biClrUsed
= 0;
919 // These seem not really needed for our purpose here.
920 lpDIBh
->bmiHeader
.biClrImportant
= 0;
921 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
922 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
923 // memory for DIB data
924 unsigned char *lpBits
;
925 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
928 wxFAIL_MSG( _T("could not allocate data for DIB") );
934 // copy data from the device-dependent bitmap to the DIB
935 HDC hdc
= ::GetDC(NULL
);
937 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
938 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
940 // copy DIB data into the wxImage object
942 unsigned char *ptdata
= data
;
943 unsigned char *ptbits
= lpBits
;
944 for( i
=0; i
<height
; i
++ )
946 for( j
=0; j
<width
; j
++ )
948 *(ptdata
++) = *(ptbits
+2);
949 *(ptdata
++) = *(ptbits
+1);
950 *(ptdata
++) = *(ptbits
);
956 // similarly, set data according to the possible mask bitmap
957 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
959 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
960 // memory DC created, color set, data copied, and memory DC deleted
961 HDC memdc
= ::CreateCompatibleDC( hdc
);
962 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
963 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
964 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
966 // background color set to RGB(16,16,16) in consistent with wxGTK
967 unsigned char r
=16, g
=16, b
=16;
970 for( i
=0; i
<height
; i
++ )
972 for( j
=0; j
<width
; j
++ )
986 SetMaskColour( r
, g
, b
);
993 // free allocated resources
994 ::ReleaseDC(NULL
, hdc
);
1001 //-----------------------------------------------------------------------------
1002 // GTK conversion routines
1003 //-----------------------------------------------------------------------------
1007 #include "gtk/gtk.h"
1008 #include "gdk/gdk.h"
1009 #include "gdk/gdkx.h"
1011 #if (GTK_MINOR_VERSION > 0)
1012 #include "gdk/gdkrgb.h"
1015 wxBitmap
wxImage::ConvertToBitmap() const
1019 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1021 int width
= GetWidth();
1022 int height
= GetHeight();
1024 bitmap
.SetHeight( height
);
1025 bitmap
.SetWidth( width
);
1027 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1031 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1032 if (visual
== NULL
) visual
= gdk_visual_get_system();
1033 int bpp
= visual
->depth
;
1035 bitmap
.SetDepth( bpp
);
1037 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1038 if (bpp
< 8) bpp
= 8;
1040 #if (GTK_MINOR_VERSION > 0)
1042 if (!HasMask() && (bpp
> 8))
1044 static bool s_hasInitialized
= FALSE
;
1046 if (!s_hasInitialized
)
1049 s_hasInitialized
= TRUE
;
1052 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1054 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1058 GDK_RGB_DITHER_NONE
,
1069 // Create picture image
1071 GdkImage
*data_image
=
1072 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1074 // Create mask image
1076 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1080 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1082 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1084 wxMask
*mask
= new wxMask();
1085 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1087 bitmap
.SetMask( mask
);
1092 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1093 byte_order b_o
= RGB
;
1097 GdkVisual
*visual
= gdk_visual_get_system();
1098 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1099 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1100 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1101 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1102 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1103 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1106 int r_mask
= GetMaskRed();
1107 int g_mask
= GetMaskGreen();
1108 int b_mask
= GetMaskBlue();
1110 unsigned char* data
= GetData();
1113 for (int y
= 0; y
< height
; y
++)
1115 for (int x
= 0; x
< width
; x
++)
1117 int r
= data
[index
];
1119 int g
= data
[index
];
1121 int b
= data
[index
];
1126 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1127 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1129 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1134 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1135 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1137 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1145 if (wxTheApp
->m_colorCube
)
1147 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1151 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1152 GdkColor
*colors
= cmap
->colors
;
1153 int max
= 3 * (65536);
1155 for (int i
= 0; i
< cmap
->size
; i
++)
1157 int rdiff
= (r
<< 8) - colors
[i
].red
;
1158 int gdiff
= (g
<< 8) - colors
[i
].green
;
1159 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1160 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1161 if (sum
< max
) { pixel
= i
; max
= sum
; }
1165 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1171 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1172 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1177 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1178 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1187 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1188 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1189 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1190 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1191 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1192 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1194 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1203 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1205 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1207 gdk_image_destroy( data_image
);
1208 gdk_gc_unref( data_gc
);
1214 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1216 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1218 gdk_image_destroy( mask_image
);
1219 gdk_gc_unref( mask_gc
);
1225 wxImage::wxImage( const wxBitmap
&bitmap
)
1227 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1229 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1231 bitmap
.GetWidth(), bitmap
.GetHeight() );
1233 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1235 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1236 char unsigned *data
= GetData();
1240 gdk_image_destroy( gdk_image
);
1241 wxFAIL_MSG( _T("couldn't create image") );
1245 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1246 if (bitmap
.GetMask())
1248 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1250 bitmap
.GetWidth(), bitmap
.GetHeight() );
1252 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1255 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1256 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1257 int bpp
= visual
->depth
;
1258 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1260 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1263 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1265 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1267 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1268 pixel
= wxINT32_SWAP_ON_BE( pixel
);
1271 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1272 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1273 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1274 } else if (bpp
== 15)
1276 data
[pos
] = (pixel
>> 7) & 0xf8;
1277 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1278 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1279 } else if (bpp
== 16)
1281 data
[pos
] = (pixel
>> 8) & 0xf8;
1282 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1283 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1286 data
[pos
] = (pixel
>> 16) & 0xff;
1287 data
[pos
+1] = (pixel
>> 8) & 0xff;
1288 data
[pos
+2] = pixel
& 0xff;
1293 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1294 if (mask_pixel
== 0)
1306 gdk_image_destroy( gdk_image
);
1307 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1312 //-----------------------------------------------------------------------------
1313 // Motif conversion routines
1314 //-----------------------------------------------------------------------------
1319 #include "wx/utils.h"
1322 wxBitmap
wxImage::ConvertToBitmap() const
1326 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1328 int width
= GetWidth();
1329 int height
= GetHeight();
1331 bitmap
.SetHeight( height
);
1332 bitmap
.SetWidth( width
);
1334 Display
*dpy
= (Display
*) wxGetDisplay();
1335 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1336 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1340 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1341 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1343 bitmap
.Create( width
, height
, bpp
);
1348 GdkImage *mask_image = (GdkImage*) NULL;
1352 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1354 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1356 wxMask *mask = new wxMask();
1357 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1359 bitmap.SetMask( mask );
1363 // Retrieve depth info
1365 XVisualInfo vinfo_template
;
1368 vinfo_template
.visual
= vis
;
1369 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1370 vinfo_template
.depth
= bpp
;
1373 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1375 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1379 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1380 if (bpp
< 8) bpp
= 8;
1384 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1385 byte_order b_o
= RGB
;
1389 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1390 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1391 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1392 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1393 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1394 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1398 int r_mask = GetMaskRed();
1399 int g_mask = GetMaskGreen();
1400 int b_mask = GetMaskBlue();
1406 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1408 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1409 XQueryColors( dpy
, cmap
, colors
, 256 );
1412 unsigned char* data
= GetData();
1415 for (int y
= 0; y
< height
; y
++)
1417 for (int x
= 0; x
< width
; x
++)
1419 int r
= data
[index
];
1421 int g
= data
[index
];
1423 int b
= data
[index
];
1429 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1430 gdk_image_put_pixel( mask_image, x, y, 1 );
1432 gdk_image_put_pixel( mask_image, x, y, 0 );
1442 if (wxTheApp->m_colorCube)
1444 pixel = wxTheApp->m_colorCube
1445 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1450 int max
= 3 * (65536);
1451 for (int i
= 0; i
< 256; i
++)
1453 int rdiff
= (r
<< 8) - colors
[i
].red
;
1454 int gdiff
= (g
<< 8) - colors
[i
].green
;
1455 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1456 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1457 if (sum
< max
) { pixel
= i
; max
= sum
; }
1462 XPutPixel( data_image
, x
, y
, pixel
);
1467 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1468 XPutPixel( data_image
, x
, y
, pixel
);
1473 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1474 XPutPixel( data_image
, x
, y
, pixel
);
1483 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1484 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1485 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1486 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1487 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1488 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1490 XPutPixel( data_image
, x
, y
, pixel
);
1500 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1501 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1502 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1504 XDestroyImage( data_image
);
1512 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1514 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1516 gdk_image_destroy( mask_image );
1517 gdk_gc_unref( mask_gc );
1524 wxImage::wxImage( const wxBitmap
&bitmap
)
1526 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1528 Display
*dpy
= (Display
*) wxGetDisplay();
1529 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1530 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1532 XImage
*ximage
= XGetImage( dpy
,
1533 (Drawable
)bitmap
.GetPixmap(),
1535 bitmap
.GetWidth(), bitmap
.GetHeight(),
1536 AllPlanes
, ZPixmap
);
1538 wxCHECK_RET( ximage
, _T("couldn't create image") );
1540 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1541 char unsigned *data
= GetData();
1545 XDestroyImage( ximage
);
1546 wxFAIL_MSG( _T("couldn't create image") );
1551 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1552 if (bitmap.GetMask())
1554 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1556 bitmap.GetWidth(), bitmap.GetHeight() );
1558 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1562 // Retrieve depth info
1564 XVisualInfo vinfo_template
;
1567 vinfo_template
.visual
= vis
;
1568 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1569 vinfo_template
.depth
= bpp
;
1572 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1574 wxCHECK_RET( vi
, _T("no visual") );
1576 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1583 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1585 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1586 XQueryColors( dpy
, cmap
, colors
, 256 );
1590 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1592 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1594 int pixel
= XGetPixel( ximage
, i
, j
);
1597 data
[pos
] = colors
[pixel
].red
>> 8;
1598 data
[pos
+1] = colors
[pixel
].green
>> 8;
1599 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1600 } else if (bpp
== 15)
1602 data
[pos
] = (pixel
>> 7) & 0xf8;
1603 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1604 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1605 } else if (bpp
== 16)
1607 data
[pos
] = (pixel
>> 8) & 0xf8;
1608 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1609 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1612 data
[pos
] = (pixel
>> 16) & 0xff;
1613 data
[pos
+1] = (pixel
>> 8) & 0xff;
1614 data
[pos
+2] = pixel
& 0xff;
1620 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1621 if (mask_pixel == 0)
1634 XDestroyImage( ximage
);
1636 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1641 // A module to allow wxImage initialization/cleanup
1642 // without calling these functions from app.cpp or from
1643 // the user's application.
1645 class wxImageModule
: public wxModule
1647 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1650 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1651 void OnExit() { wxImage::CleanUpHandlers(); };
1654 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)