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 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
194 wxCHECK_RET( Ok(), _T("invalid image") );
196 int w
= M_IMGDATA
->m_width
;
197 int h
= M_IMGDATA
->m_height
;
199 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), _T("invalid image index") );
201 long pos
= (y
* w
+ x
) * 3;
203 M_IMGDATA
->m_data
[ pos
] = r
;
204 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
205 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
208 unsigned char wxImage::GetRed( int x
, int y
)
210 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
212 int w
= M_IMGDATA
->m_width
;
213 int h
= M_IMGDATA
->m_height
;
215 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
217 long pos
= (y
* w
+ x
) * 3;
219 return M_IMGDATA
->m_data
[pos
];
222 unsigned char wxImage::GetGreen( int x
, int y
)
224 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
226 int w
= M_IMGDATA
->m_width
;
227 int h
= M_IMGDATA
->m_height
;
229 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
231 long pos
= (y
* w
+ x
) * 3;
233 return M_IMGDATA
->m_data
[pos
+1];
236 unsigned char wxImage::GetBlue( int x
, int y
)
238 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
240 int w
= M_IMGDATA
->m_width
;
241 int h
= M_IMGDATA
->m_height
;
243 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
245 long pos
= (y
* w
+ x
) * 3;
247 return M_IMGDATA
->m_data
[pos
+2];
250 bool wxImage::Ok() const
252 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
255 char unsigned *wxImage::GetData() const
257 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, _T("invalid image") );
259 return M_IMGDATA
->m_data
;
262 void wxImage::SetData( char unsigned *data
)
264 wxCHECK_RET( Ok(), _T("invalid image") );
266 memcpy(M_IMGDATA
->m_data
, data
, M_IMGDATA
->m_width
* M_IMGDATA
->m_height
* 3);
269 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
271 wxCHECK_RET( Ok(), _T("invalid image") );
273 M_IMGDATA
->m_maskRed
= r
;
274 M_IMGDATA
->m_maskGreen
= g
;
275 M_IMGDATA
->m_maskBlue
= b
;
276 M_IMGDATA
->m_hasMask
= TRUE
;
279 unsigned char wxImage::GetMaskRed() const
281 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
283 return M_IMGDATA
->m_maskRed
;
286 unsigned char wxImage::GetMaskGreen() const
288 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
290 return M_IMGDATA
->m_maskGreen
;
293 unsigned char wxImage::GetMaskBlue() const
295 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
297 return M_IMGDATA
->m_maskBlue
;
300 void wxImage::SetMask( bool mask
)
302 wxCHECK_RET( Ok(), _T("invalid image") );
304 M_IMGDATA
->m_hasMask
= mask
;
307 bool wxImage::HasMask() const
309 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
311 return M_IMGDATA
->m_hasMask
;
314 int wxImage::GetWidth() const
316 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
318 return M_IMGDATA
->m_width
;
321 int wxImage::GetHeight() const
323 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
325 return M_IMGDATA
->m_height
;
328 bool wxImage::LoadFile( const wxString
& filename
, long type
)
331 if (wxFileExists(filename
))
333 wxFileInputStream
stream(filename
);
334 return LoadFile(stream
, type
);
338 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
342 #else // !wxUSE_STREAMS
344 #endif // wxUSE_STREAMS
347 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
350 if (wxFileExists(filename
))
352 wxFileInputStream
stream(filename
);
353 return LoadFile(stream
, mimetype
);
357 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
361 #else // !wxUSE_STREAMS
363 #endif // wxUSE_STREAMS
366 bool wxImage::SaveFile( const wxString
& filename
, int type
)
369 wxFileOutputStream
stream(filename
);
371 if ( stream
.LastError() == wxStream_NOERROR
)
372 return SaveFile(stream
, type
);
374 #endif // wxUSE_STREAMS
378 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
381 wxFileOutputStream
stream(filename
);
383 if ( stream
.LastError() == wxStream_NOERROR
)
384 return SaveFile(stream
, mimetype
);
386 #endif // wxUSE_STREAMS
391 //#include <stream.h>
393 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
397 m_refData
= new wxImageRefData
;
399 wxImageHandler
*handler
;
401 if (type
==wxBITMAP_TYPE_ANY
)
403 // here we can try to guess the handler according the extension,
404 // but we lose the stream name !?
405 // Probably we should write methods such as
406 // bool wxImageHandler::IsAppropriate(wxString&)
407 // bool wxImageHandler::IsAppropriate(sxInputStream&&)
408 // for png : see example.c
409 wxList
&list
=GetHandlers();
410 off_t pos
=stream
.TellI();
412 wxLogNull prevent_log
;
414 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
416 handler
=(wxImageHandler
*)node
->GetData();
417 //cout << handler->GetExtension() << endl;
418 if (handler
->LoadFile( this, stream
, FALSE
)) return TRUE
;
425 handler
= FindHandler(type
);
429 wxLogWarning( _T("No image handler for type %d defined."), type
);
434 return handler
->LoadFile( this, stream
);
437 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
441 m_refData
= new wxImageRefData
;
443 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
447 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
452 return handler
->LoadFile( this, stream
);
455 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
457 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
459 wxImageHandler
*handler
= FindHandler(type
);
463 wxLogWarning( _T("No image handler for type %d defined."), type
);
468 return handler
->SaveFile( this, stream
);
471 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
473 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
475 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
479 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
484 return handler
->SaveFile( this, stream
);
486 #endif // wxUSE_STREAMS
488 void wxImage::AddHandler( wxImageHandler
*handler
)
490 // make sure that the memory will be freed at the program end
491 sm_handlers
.DeleteContents(TRUE
);
493 sm_handlers
.Append( handler
);
496 void wxImage::InsertHandler( wxImageHandler
*handler
)
498 // make sure that the memory will be freed at the program end
499 sm_handlers
.DeleteContents(TRUE
);
501 sm_handlers
.Insert( handler
);
504 bool wxImage::RemoveHandler( const wxString
& name
)
506 wxImageHandler
*handler
= FindHandler(name
);
509 sm_handlers
.DeleteObject(handler
);
516 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
518 wxNode
*node
= sm_handlers
.First();
521 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
522 if (handler
->GetName().Cmp(name
) == 0) return handler
;
526 return (wxImageHandler
*)NULL
;
529 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
531 wxNode
*node
= sm_handlers
.First();
534 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
535 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
536 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
540 return (wxImageHandler
*)NULL
;
543 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
545 wxNode
*node
= sm_handlers
.First();
548 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
549 if (handler
->GetType() == bitmapType
) return handler
;
555 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
557 wxNode
*node
= sm_handlers
.First();
560 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
561 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
567 void wxImage::InitStandardHandlers()
569 AddHandler( new wxBMPHandler
);
572 void wxImage::CleanUpHandlers()
574 wxNode
*node
= sm_handlers
.First();
577 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
578 wxNode
*next
= node
->Next();
585 //-----------------------------------------------------------------------------
587 //-----------------------------------------------------------------------------
589 #if !USE_SHARED_LIBRARIES
590 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
594 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
599 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
603 #endif // wxUSE_STREAMS
605 //-----------------------------------------------------------------------------
606 // MSW conversion routines
607 //-----------------------------------------------------------------------------
611 wxBitmap
wxImage::ConvertToBitmap() const
616 // sizeLimit is the MS upper limit for the DIB size
617 int sizeLimit
= 1024*768*3;
619 // width and height of the device-dependent bitmap
620 int width
= GetWidth();
621 int bmpHeight
= GetHeight();
623 // calc the number of bytes per scanline and padding
624 int bytePerLine
= width
*3;
625 int sizeDWORD
= sizeof( DWORD
);
626 int lineBoundary
= bytePerLine
% sizeDWORD
;
628 if( lineBoundary
> 0 )
630 padding
= sizeDWORD
- lineBoundary
;
631 bytePerLine
+= padding
;
633 // calc the number of DIBs and heights of DIBs
636 int height
= sizeLimit
/bytePerLine
;
637 if( height
>= bmpHeight
)
641 numDIB
= bmpHeight
/ height
;
642 hRemain
= bmpHeight
% height
;
643 if( hRemain
>0 ) numDIB
++;
646 // set bitmap parameters
648 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
649 bitmap
.SetWidth( width
);
650 bitmap
.SetHeight( bmpHeight
);
651 bitmap
.SetDepth( wxDisplayDepth() );
653 // create a DIB header
654 int headersize
= sizeof(BITMAPINFOHEADER
);
655 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
656 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
657 // Fill in the DIB header
658 lpDIBh
->bmiHeader
.biSize
= headersize
;
659 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
660 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
661 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
662 // the general formula for biSizeImage:
663 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
664 lpDIBh
->bmiHeader
.biPlanes
= 1;
665 lpDIBh
->bmiHeader
.biBitCount
= 24;
666 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
667 lpDIBh
->bmiHeader
.biClrUsed
= 0;
668 // These seem not really needed for our purpose here.
669 lpDIBh
->bmiHeader
.biClrImportant
= 0;
670 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
671 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
672 // memory for DIB data
673 unsigned char *lpBits
;
674 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
677 wxFAIL_MSG( _T("could not allocate memory for DIB") );
682 // create and set the device-dependent bitmap
683 HDC hdc
= ::GetDC(NULL
);
684 HDC memdc
= ::CreateCompatibleDC( hdc
);
686 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
687 ::SelectObject( memdc
, hbitmap
);
689 // copy image data into DIB data and then into DDB (in a loop)
690 unsigned char *data
= GetData();
693 unsigned char *ptdata
= data
;
694 unsigned char *ptbits
;
696 for( n
=0; n
<numDIB
; n
++ )
698 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
700 // redefine height and size of the (possibly) last smaller DIB
701 // memory is not reallocated
703 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
704 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
708 for( j
=0; j
<height
; j
++ )
710 for( i
=0; i
<width
; i
++ )
712 *(ptbits
++) = *(ptdata
+2);
713 *(ptbits
++) = *(ptdata
+1);
714 *(ptbits
++) = *(ptdata
);
717 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
719 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
720 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
722 // if numDIB = 1, lines below can also be used
723 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
724 // The above line is equivalent to the following two lines.
725 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
726 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
727 // or the following lines
728 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
729 // HDC memdc = ::CreateCompatibleDC( hdc );
730 // ::SelectObject( memdc, hbitmap);
731 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
732 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
733 // ::SelectObject( memdc, 0 );
734 // ::DeleteDC( memdc );
736 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
738 // similarly, created an mono-bitmap for the possible mask
741 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
742 ::SelectObject( memdc
, hbitmap
);
743 if( numDIB
== 1 ) height
= bmpHeight
;
744 else height
= sizeLimit
/bytePerLine
;
745 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
746 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
748 unsigned char r
= GetMaskRed();
749 unsigned char g
= GetMaskGreen();
750 unsigned char b
= GetMaskBlue();
751 unsigned char zero
= 0, one
= 255;
753 for( n
=0; n
<numDIB
; n
++ )
755 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
757 // redefine height and size of the (possibly) last smaller DIB
758 // memory is not reallocated
760 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
761 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
764 for( int j
=0; j
<height
; j
++ )
766 for(i
=0; i
<width
; i
++ )
768 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
781 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
783 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
784 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
787 // create a wxMask object
788 wxMask
*mask
= new wxMask();
789 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
790 bitmap
.SetMask( mask
);
791 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
792 /* The following can also be used but is slow to run
793 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
794 wxMask *mask = new wxMask( bitmap, colour );
795 bitmap.SetMask( mask );
799 // free allocated resources
800 ::SelectObject( memdc
, 0 );
802 ::ReleaseDC(NULL
, hdc
);
806 // check the wxBitmap object
807 if( bitmap
.GetHBITMAP() )
808 bitmap
.SetOk( TRUE
);
810 bitmap
.SetOk( FALSE
);
815 wxImage::wxImage( const wxBitmap
&bitmap
)
820 wxFAIL_MSG( _T("invalid bitmap") );
824 // create an wxImage object
825 int width
= bitmap
.GetWidth();
826 int height
= bitmap
.GetHeight();
827 Create( width
, height
);
828 unsigned char *data
= GetData();
831 wxFAIL_MSG( _T("could not allocate data for image") );
835 // calc the number of bytes per scanline and padding in the DIB
836 int bytePerLine
= width
*3;
837 int sizeDWORD
= sizeof( DWORD
);
838 int lineBoundary
= bytePerLine
% sizeDWORD
;
840 if( lineBoundary
> 0 )
842 padding
= sizeDWORD
- lineBoundary
;
843 bytePerLine
+= padding
;
846 // create a DIB header
847 int headersize
= sizeof(BITMAPINFOHEADER
);
848 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
851 wxFAIL_MSG( _T("could not allocate data for DIB header") );
855 // Fill in the DIB header
856 lpDIBh
->bmiHeader
.biSize
= headersize
;
857 lpDIBh
->bmiHeader
.biWidth
= width
;
858 lpDIBh
->bmiHeader
.biHeight
= -height
;
859 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
860 lpDIBh
->bmiHeader
.biPlanes
= 1;
861 lpDIBh
->bmiHeader
.biBitCount
= 24;
862 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
863 lpDIBh
->bmiHeader
.biClrUsed
= 0;
864 // These seem not really needed for our purpose here.
865 lpDIBh
->bmiHeader
.biClrImportant
= 0;
866 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
867 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
868 // memory for DIB data
869 unsigned char *lpBits
;
870 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
873 wxFAIL_MSG( _T("could not allocate data for DIB") );
879 // copy data from the device-dependent bitmap to the DIB
880 HDC hdc
= ::GetDC(NULL
);
882 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
883 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
885 // copy DIB data into the wxImage object
887 unsigned char *ptdata
= data
;
888 unsigned char *ptbits
= lpBits
;
889 for( i
=0; i
<height
; i
++ )
891 for( j
=0; j
<width
; j
++ )
893 *(ptdata
++) = *(ptbits
+2);
894 *(ptdata
++) = *(ptbits
+1);
895 *(ptdata
++) = *(ptbits
);
901 // similarly, set data according to the possible mask bitmap
902 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
904 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
905 // memory DC created, color set, data copied, and memory DC deleted
906 HDC memdc
= ::CreateCompatibleDC( hdc
);
907 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
908 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
909 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
911 // background color set to RGB(16,16,16) in consistent with wxGTK
912 unsigned char r
=16, g
=16, b
=16;
915 for( i
=0; i
<height
; i
++ )
917 for( j
=0; j
<width
; j
++ )
931 SetMaskColour( r
, g
, b
);
938 // free allocated resources
939 ::ReleaseDC(NULL
, hdc
);
946 //-----------------------------------------------------------------------------
947 // GTK conversion routines
948 //-----------------------------------------------------------------------------
954 #include "gdk/gdkx.h"
956 #if (GTK_MINOR_VERSION > 0)
957 #include "gdk/gdkrgb.h"
960 wxBitmap
wxImage::ConvertToBitmap() const
964 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
966 int width
= GetWidth();
967 int height
= GetHeight();
969 bitmap
.SetHeight( height
);
970 bitmap
.SetWidth( width
);
972 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
976 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
977 if (visual
== NULL
) visual
= gdk_visual_get_system();
978 int bpp
= visual
->depth
;
980 bitmap
.SetDepth( bpp
);
982 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
983 if (bpp
< 8) bpp
= 8;
985 #if (GTK_MINOR_VERSION > 0)
987 if (!HasMask() && (bpp
> 8))
989 static bool s_hasInitialized
= FALSE
;
991 if (!s_hasInitialized
)
994 s_hasInitialized
= TRUE
;
997 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
999 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1003 GDK_RGB_DITHER_NONE
,
1014 // Create picture image
1016 GdkImage
*data_image
=
1017 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1019 // Create mask image
1021 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1025 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1027 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1029 wxMask
*mask
= new wxMask();
1030 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1032 bitmap
.SetMask( mask
);
1037 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1038 byte_order b_o
= RGB
;
1042 GdkVisual
*visual
= gdk_visual_get_system();
1043 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1044 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1045 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1046 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1047 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1048 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1051 int r_mask
= GetMaskRed();
1052 int g_mask
= GetMaskGreen();
1053 int b_mask
= GetMaskBlue();
1055 unsigned char* data
= GetData();
1058 for (int y
= 0; y
< height
; y
++)
1060 for (int x
= 0; x
< width
; x
++)
1062 int r
= data
[index
];
1064 int g
= data
[index
];
1066 int b
= data
[index
];
1071 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1072 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1074 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1079 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1080 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1082 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1090 if (wxTheApp
->m_colorCube
)
1092 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1096 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1097 GdkColor
*colors
= cmap
->colors
;
1098 int max
= 3 * (65536);
1100 for (int i
= 0; i
< cmap
->size
; i
++)
1102 int rdiff
= (r
<< 8) - colors
[i
].red
;
1103 int gdiff
= (g
<< 8) - colors
[i
].green
;
1104 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1105 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1106 if (sum
< max
) { pixel
= i
; max
= sum
; }
1110 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1116 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1117 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1122 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1123 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1132 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1133 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1134 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1135 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1136 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1137 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1139 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1148 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1150 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1152 gdk_image_destroy( data_image
);
1153 gdk_gc_unref( data_gc
);
1159 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1161 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1163 gdk_image_destroy( mask_image
);
1164 gdk_gc_unref( mask_gc
);
1170 wxImage::wxImage( const wxBitmap
&bitmap
)
1172 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1174 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1176 bitmap
.GetWidth(), bitmap
.GetHeight() );
1178 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1180 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1181 char unsigned *data
= GetData();
1185 gdk_image_destroy( gdk_image
);
1186 wxFAIL_MSG( _T("couldn't create image") );
1190 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1191 if (bitmap
.GetMask())
1193 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1195 bitmap
.GetWidth(), bitmap
.GetHeight() );
1197 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1200 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1201 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1202 int bpp
= visual
->depth
;
1203 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1205 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1208 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1210 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1212 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1213 pixel
= wxINT32_SWAP_ON_BE( pixel
);
1216 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1217 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1218 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1219 } else if (bpp
== 15)
1221 data
[pos
] = (pixel
>> 7) & 0xf8;
1222 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1223 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1224 } else if (bpp
== 16)
1226 data
[pos
] = (pixel
>> 8) & 0xf8;
1227 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1228 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1231 data
[pos
] = (pixel
>> 16) & 0xff;
1232 data
[pos
+1] = (pixel
>> 8) & 0xff;
1233 data
[pos
+2] = pixel
& 0xff;
1238 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1239 if (mask_pixel
== 0)
1251 gdk_image_destroy( gdk_image
);
1252 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1257 //-----------------------------------------------------------------------------
1258 // Motif conversion routines
1259 //-----------------------------------------------------------------------------
1264 #include "wx/utils.h"
1267 wxBitmap
wxImage::ConvertToBitmap() const
1271 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1273 int width
= GetWidth();
1274 int height
= GetHeight();
1276 bitmap
.SetHeight( height
);
1277 bitmap
.SetWidth( width
);
1279 Display
*dpy
= (Display
*) wxGetDisplay();
1280 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1281 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1285 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1286 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1288 bitmap
.Create( width
, height
, bpp
);
1293 GdkImage *mask_image = (GdkImage*) NULL;
1297 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1299 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1301 wxMask *mask = new wxMask();
1302 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1304 bitmap.SetMask( mask );
1308 // Retrieve depth info
1310 XVisualInfo vinfo_template
;
1313 vinfo_template
.visual
= vis
;
1314 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1315 vinfo_template
.depth
= bpp
;
1318 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1320 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1324 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1325 if (bpp
< 8) bpp
= 8;
1329 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1330 byte_order b_o
= RGB
;
1334 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1335 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1336 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1337 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1338 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1339 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1343 int r_mask = GetMaskRed();
1344 int g_mask = GetMaskGreen();
1345 int b_mask = GetMaskBlue();
1351 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1353 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1354 XQueryColors( dpy
, cmap
, colors
, 256 );
1357 unsigned char* data
= GetData();
1360 for (int y
= 0; y
< height
; y
++)
1362 for (int x
= 0; x
< width
; x
++)
1364 int r
= data
[index
];
1366 int g
= data
[index
];
1368 int b
= data
[index
];
1374 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1375 gdk_image_put_pixel( mask_image, x, y, 1 );
1377 gdk_image_put_pixel( mask_image, x, y, 0 );
1387 if (wxTheApp->m_colorCube)
1389 pixel = wxTheApp->m_colorCube
1390 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1395 int max
= 3 * (65536);
1396 for (int i
= 0; i
< 256; i
++)
1398 int rdiff
= (r
<< 8) - colors
[i
].red
;
1399 int gdiff
= (g
<< 8) - colors
[i
].green
;
1400 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1401 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1402 if (sum
< max
) { pixel
= i
; max
= sum
; }
1407 XPutPixel( data_image
, x
, y
, pixel
);
1412 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1413 XPutPixel( data_image
, x
, y
, pixel
);
1418 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1419 XPutPixel( data_image
, x
, y
, pixel
);
1428 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1429 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1430 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1431 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1432 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1433 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1435 XPutPixel( data_image
, x
, y
, pixel
);
1445 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1446 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1447 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1449 XDestroyImage( data_image
);
1457 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1459 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1461 gdk_image_destroy( mask_image );
1462 gdk_gc_unref( mask_gc );
1469 wxImage::wxImage( const wxBitmap
&bitmap
)
1471 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1473 Display
*dpy
= (Display
*) wxGetDisplay();
1474 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1475 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1477 XImage
*ximage
= XGetImage( dpy
,
1478 (Drawable
)bitmap
.GetPixmap(),
1480 bitmap
.GetWidth(), bitmap
.GetHeight(),
1481 AllPlanes
, ZPixmap
);
1483 wxCHECK_RET( ximage
, _T("couldn't create image") );
1485 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1486 char unsigned *data
= GetData();
1490 XDestroyImage( ximage
);
1491 wxFAIL_MSG( _T("couldn't create image") );
1496 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1497 if (bitmap.GetMask())
1499 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1501 bitmap.GetWidth(), bitmap.GetHeight() );
1503 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1507 // Retrieve depth info
1509 XVisualInfo vinfo_template
;
1512 vinfo_template
.visual
= vis
;
1513 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1514 vinfo_template
.depth
= bpp
;
1517 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1519 wxCHECK_RET( vi
, _T("no visual") );
1521 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1528 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1530 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1531 XQueryColors( dpy
, cmap
, colors
, 256 );
1535 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1537 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1539 int pixel
= XGetPixel( ximage
, i
, j
);
1542 data
[pos
] = colors
[pixel
].red
>> 8;
1543 data
[pos
+1] = colors
[pixel
].green
>> 8;
1544 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1545 } else if (bpp
== 15)
1547 data
[pos
] = (pixel
>> 7) & 0xf8;
1548 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1549 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1550 } else if (bpp
== 16)
1552 data
[pos
] = (pixel
>> 8) & 0xf8;
1553 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1554 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1557 data
[pos
] = (pixel
>> 16) & 0xff;
1558 data
[pos
+1] = (pixel
>> 8) & 0xff;
1559 data
[pos
+2] = pixel
& 0xff;
1565 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1566 if (mask_pixel == 0)
1579 XDestroyImage( ximage
);
1581 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1586 // A module to allow wxImage initialization/cleanup
1587 // without calling these functions from app.cpp or from
1588 // the user's application.
1590 class wxImageModule
: public wxModule
1592 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1595 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1596 void OnExit() { wxImage::CleanUpHandlers(); };
1599 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)