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 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
395 m_refData
= new wxImageRefData
;
397 wxImageHandler
*handler
= FindHandler(type
);
401 wxLogWarning( _T("No image handler for type %d defined."), type
);
406 return handler
->LoadFile( this, stream
);
409 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
413 m_refData
= new wxImageRefData
;
415 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
419 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
424 return handler
->LoadFile( this, stream
);
427 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
429 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
431 wxImageHandler
*handler
= FindHandler(type
);
435 wxLogWarning( _T("No image handler for type %d defined."), type
);
440 return handler
->SaveFile( this, stream
);
443 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
445 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
447 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
451 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
456 return handler
->SaveFile( this, stream
);
458 #endif // wxUSE_STREAMS
460 void wxImage::AddHandler( wxImageHandler
*handler
)
462 // make sure that the memory will be freed at the program end
463 sm_handlers
.DeleteContents(TRUE
);
465 sm_handlers
.Append( handler
);
468 void wxImage::InsertHandler( wxImageHandler
*handler
)
470 // make sure that the memory will be freed at the program end
471 sm_handlers
.DeleteContents(TRUE
);
473 sm_handlers
.Insert( handler
);
476 bool wxImage::RemoveHandler( const wxString
& name
)
478 wxImageHandler
*handler
= FindHandler(name
);
481 sm_handlers
.DeleteObject(handler
);
488 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
490 wxNode
*node
= sm_handlers
.First();
493 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
494 if (handler
->GetName().Cmp(name
) == 0) return handler
;
498 return (wxImageHandler
*)NULL
;
501 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
503 wxNode
*node
= sm_handlers
.First();
506 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
507 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
508 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
512 return (wxImageHandler
*)NULL
;
515 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
517 wxNode
*node
= sm_handlers
.First();
520 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
521 if (handler
->GetType() == bitmapType
) return handler
;
527 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
529 wxNode
*node
= sm_handlers
.First();
532 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
533 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
539 void wxImage::InitStandardHandlers()
541 AddHandler( new wxBMPHandler
);
544 void wxImage::CleanUpHandlers()
546 wxNode
*node
= sm_handlers
.First();
549 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
550 wxNode
*next
= node
->Next();
557 //-----------------------------------------------------------------------------
559 //-----------------------------------------------------------------------------
561 #if !USE_SHARED_LIBRARIES
562 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
566 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
571 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
575 #endif // wxUSE_STREAMS
577 //-----------------------------------------------------------------------------
578 // MSW conversion routines
579 //-----------------------------------------------------------------------------
583 wxBitmap
wxImage::ConvertToBitmap() const
588 // sizeLimit is the MS upper limit for the DIB size
589 int sizeLimit
= 1024*768*3;
591 // width and height of the device-dependent bitmap
592 int width
= GetWidth();
593 int bmpHeight
= GetHeight();
595 // calc the number of bytes per scanline and padding
596 int bytePerLine
= width
*3;
597 int sizeDWORD
= sizeof( DWORD
);
598 int lineBoundary
= bytePerLine
% sizeDWORD
;
600 if( lineBoundary
> 0 )
602 padding
= sizeDWORD
- lineBoundary
;
603 bytePerLine
+= padding
;
605 // calc the number of DIBs and heights of DIBs
608 int height
= sizeLimit
/bytePerLine
;
609 if( height
>= bmpHeight
)
613 numDIB
= bmpHeight
/ height
;
614 hRemain
= bmpHeight
% height
;
615 if( hRemain
>0 ) numDIB
++;
618 // set bitmap parameters
620 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
621 bitmap
.SetWidth( width
);
622 bitmap
.SetHeight( bmpHeight
);
623 bitmap
.SetDepth( wxDisplayDepth() );
625 // create a DIB header
626 int headersize
= sizeof(BITMAPINFOHEADER
);
627 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
628 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
629 // Fill in the DIB header
630 lpDIBh
->bmiHeader
.biSize
= headersize
;
631 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
632 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
633 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
634 // the general formula for biSizeImage:
635 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
636 lpDIBh
->bmiHeader
.biPlanes
= 1;
637 lpDIBh
->bmiHeader
.biBitCount
= 24;
638 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
639 lpDIBh
->bmiHeader
.biClrUsed
= 0;
640 // These seem not really needed for our purpose here.
641 lpDIBh
->bmiHeader
.biClrImportant
= 0;
642 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
643 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
644 // memory for DIB data
645 unsigned char *lpBits
;
646 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
649 wxFAIL_MSG( _T("could not allocate memory for DIB") );
654 // create and set the device-dependent bitmap
655 HDC hdc
= ::GetDC(NULL
);
656 HDC memdc
= ::CreateCompatibleDC( hdc
);
658 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
659 ::SelectObject( memdc
, hbitmap
);
661 // copy image data into DIB data and then into DDB (in a loop)
662 unsigned char *data
= GetData();
665 unsigned char *ptdata
= data
;
666 unsigned char *ptbits
;
668 for( n
=0; n
<numDIB
; n
++ )
670 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
672 // redefine height and size of the (possibly) last smaller DIB
673 // memory is not reallocated
675 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
676 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
680 for( j
=0; j
<height
; j
++ )
682 for( i
=0; i
<width
; i
++ )
684 *(ptbits
++) = *(ptdata
+2);
685 *(ptbits
++) = *(ptdata
+1);
686 *(ptbits
++) = *(ptdata
);
689 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
691 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
692 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
694 // if numDIB = 1, lines below can also be used
695 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
696 // The above line is equivalent to the following two lines.
697 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
698 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
699 // or the following lines
700 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
701 // HDC memdc = ::CreateCompatibleDC( hdc );
702 // ::SelectObject( memdc, hbitmap);
703 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
704 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
705 // ::SelectObject( memdc, 0 );
706 // ::DeleteDC( memdc );
708 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
710 // similarly, created an mono-bitmap for the possible mask
713 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
714 ::SelectObject( memdc
, hbitmap
);
715 if( numDIB
== 1 ) height
= bmpHeight
;
716 else height
= sizeLimit
/bytePerLine
;
717 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
718 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
720 unsigned char r
= GetMaskRed();
721 unsigned char g
= GetMaskGreen();
722 unsigned char b
= GetMaskBlue();
723 unsigned char zero
= 0, one
= 255;
725 for( n
=0; n
<numDIB
; n
++ )
727 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
729 // redefine height and size of the (possibly) last smaller DIB
730 // memory is not reallocated
732 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
733 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
736 for( int j
=0; j
<height
; j
++ )
738 for(i
=0; i
<width
; i
++ )
740 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
753 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
755 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
756 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
759 // create a wxMask object
760 wxMask
*mask
= new wxMask();
761 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
762 bitmap
.SetMask( mask
);
763 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
764 /* The following can also be used but is slow to run
765 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
766 wxMask *mask = new wxMask( bitmap, colour );
767 bitmap.SetMask( mask );
771 // free allocated resources
772 ::SelectObject( memdc
, 0 );
774 ::ReleaseDC(NULL
, hdc
);
778 // check the wxBitmap object
779 if( bitmap
.GetHBITMAP() )
780 bitmap
.SetOk( TRUE
);
782 bitmap
.SetOk( FALSE
);
787 wxImage::wxImage( const wxBitmap
&bitmap
)
792 wxFAIL_MSG( _T("invalid bitmap") );
796 // create an wxImage object
797 int width
= bitmap
.GetWidth();
798 int height
= bitmap
.GetHeight();
799 Create( width
, height
);
800 unsigned char *data
= GetData();
803 wxFAIL_MSG( _T("could not allocate data for image") );
807 // calc the number of bytes per scanline and padding in the DIB
808 int bytePerLine
= width
*3;
809 int sizeDWORD
= sizeof( DWORD
);
810 int lineBoundary
= bytePerLine
% sizeDWORD
;
812 if( lineBoundary
> 0 )
814 padding
= sizeDWORD
- lineBoundary
;
815 bytePerLine
+= padding
;
818 // create a DIB header
819 int headersize
= sizeof(BITMAPINFOHEADER
);
820 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
823 wxFAIL_MSG( _T("could not allocate data for DIB header") );
827 // Fill in the DIB header
828 lpDIBh
->bmiHeader
.biSize
= headersize
;
829 lpDIBh
->bmiHeader
.biWidth
= width
;
830 lpDIBh
->bmiHeader
.biHeight
= -height
;
831 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
832 lpDIBh
->bmiHeader
.biPlanes
= 1;
833 lpDIBh
->bmiHeader
.biBitCount
= 24;
834 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
835 lpDIBh
->bmiHeader
.biClrUsed
= 0;
836 // These seem not really needed for our purpose here.
837 lpDIBh
->bmiHeader
.biClrImportant
= 0;
838 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
839 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
840 // memory for DIB data
841 unsigned char *lpBits
;
842 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
845 wxFAIL_MSG( _T("could not allocate data for DIB") );
851 // copy data from the device-dependent bitmap to the DIB
852 HDC hdc
= ::GetDC(NULL
);
854 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
855 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
857 // copy DIB data into the wxImage object
859 unsigned char *ptdata
= data
;
860 unsigned char *ptbits
= lpBits
;
861 for( i
=0; i
<height
; i
++ )
863 for( j
=0; j
<width
; j
++ )
865 *(ptdata
++) = *(ptbits
+2);
866 *(ptdata
++) = *(ptbits
+1);
867 *(ptdata
++) = *(ptbits
);
873 // similarly, set data according to the possible mask bitmap
874 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
876 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
877 // memory DC created, color set, data copied, and memory DC deleted
878 HDC memdc
= ::CreateCompatibleDC( hdc
);
879 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
880 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
881 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
883 // background color set to RGB(16,16,16) in consistent with wxGTK
884 unsigned char r
=16, g
=16, b
=16;
887 for( i
=0; i
<height
; i
++ )
889 for( j
=0; j
<width
; j
++ )
903 SetMaskColour( r
, g
, b
);
910 // free allocated resources
911 ::ReleaseDC(NULL
, hdc
);
918 //-----------------------------------------------------------------------------
919 // GTK conversion routines
920 //-----------------------------------------------------------------------------
926 #include "gdk/gdkx.h"
928 #if (GTK_MINOR_VERSION > 0)
929 #include "gdk/gdkrgb.h"
932 wxBitmap
wxImage::ConvertToBitmap() const
936 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
938 int width
= GetWidth();
939 int height
= GetHeight();
941 bitmap
.SetHeight( height
);
942 bitmap
.SetWidth( width
);
944 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
948 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
949 if (visual
== NULL
) visual
= gdk_visual_get_system();
950 int bpp
= visual
->depth
;
952 bitmap
.SetDepth( bpp
);
954 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
955 if (bpp
< 8) bpp
= 8;
957 #if (GTK_MINOR_VERSION > 0)
959 if (!HasMask() && (bpp
> 8))
961 static bool s_hasInitialized
= FALSE
;
963 if (!s_hasInitialized
)
966 s_hasInitialized
= TRUE
;
969 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
971 gdk_draw_rgb_image( bitmap
.GetPixmap(),
986 // Create picture image
988 GdkImage
*data_image
=
989 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
993 GdkImage
*mask_image
= (GdkImage
*) NULL
;
997 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
999 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1001 wxMask
*mask
= new wxMask();
1002 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1004 bitmap
.SetMask( mask
);
1009 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1010 byte_order b_o
= RGB
;
1014 GdkVisual
*visual
= gdk_visual_get_system();
1015 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1016 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1017 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1018 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1019 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1020 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1023 int r_mask
= GetMaskRed();
1024 int g_mask
= GetMaskGreen();
1025 int b_mask
= GetMaskBlue();
1027 unsigned char* data
= GetData();
1030 for (int y
= 0; y
< height
; y
++)
1032 for (int x
= 0; x
< width
; x
++)
1034 int r
= data
[index
];
1036 int g
= data
[index
];
1038 int b
= data
[index
];
1043 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1044 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1046 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1051 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1052 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1054 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1062 if (wxTheApp
->m_colorCube
)
1064 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1068 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1069 GdkColor
*colors
= cmap
->colors
;
1070 int max
= 3 * (65536);
1072 for (int i
= 0; i
< cmap
->size
; i
++)
1074 int rdiff
= (r
<< 8) - colors
[i
].red
;
1075 int gdiff
= (g
<< 8) - colors
[i
].green
;
1076 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1077 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1078 if (sum
< max
) { pixel
= i
; max
= sum
; }
1082 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1088 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1089 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1094 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1095 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1104 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1105 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1106 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1107 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1108 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1109 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1111 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1120 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1122 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1124 gdk_image_destroy( data_image
);
1125 gdk_gc_unref( data_gc
);
1131 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1133 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1135 gdk_image_destroy( mask_image
);
1136 gdk_gc_unref( mask_gc
);
1142 wxImage::wxImage( const wxBitmap
&bitmap
)
1144 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1146 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1148 bitmap
.GetWidth(), bitmap
.GetHeight() );
1150 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1152 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1153 char unsigned *data
= GetData();
1157 gdk_image_destroy( gdk_image
);
1158 wxFAIL_MSG( _T("couldn't create image") );
1162 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1163 if (bitmap
.GetMask())
1165 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1167 bitmap
.GetWidth(), bitmap
.GetHeight() );
1169 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1172 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1173 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1174 int bpp
= visual
->depth
;
1175 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1177 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1180 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1182 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1184 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1187 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1188 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1189 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1190 } else if (bpp
== 15)
1192 data
[pos
] = (pixel
>> 7) & 0xf8;
1193 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1194 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1195 } else if (bpp
== 16)
1197 data
[pos
] = (pixel
>> 8) & 0xf8;
1198 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1199 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1202 data
[pos
] = (pixel
>> 16) & 0xff;
1203 data
[pos
+1] = (pixel
>> 8) & 0xff;
1204 data
[pos
+2] = pixel
& 0xff;
1209 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1210 if (mask_pixel
== 0)
1222 gdk_image_destroy( gdk_image
);
1223 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1228 //-----------------------------------------------------------------------------
1229 // Motif conversion routines
1230 //-----------------------------------------------------------------------------
1235 #include "wx/utils.h"
1238 wxBitmap
wxImage::ConvertToBitmap() const
1242 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1244 int width
= GetWidth();
1245 int height
= GetHeight();
1247 bitmap
.SetHeight( height
);
1248 bitmap
.SetWidth( width
);
1250 Display
*dpy
= (Display
*) wxGetDisplay();
1251 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1252 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1256 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1257 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1259 bitmap
.Create( width
, height
, bpp
);
1264 GdkImage *mask_image = (GdkImage*) NULL;
1268 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1270 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1272 wxMask *mask = new wxMask();
1273 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1275 bitmap.SetMask( mask );
1279 // Retrieve depth info
1281 XVisualInfo vinfo_template
;
1284 vinfo_template
.visual
= vis
;
1285 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1286 vinfo_template
.depth
= bpp
;
1289 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1291 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1295 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1296 if (bpp
< 8) bpp
= 8;
1300 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1301 byte_order b_o
= RGB
;
1305 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1306 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1307 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1308 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1309 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1310 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1314 int r_mask = GetMaskRed();
1315 int g_mask = GetMaskGreen();
1316 int b_mask = GetMaskBlue();
1322 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1324 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1325 XQueryColors( dpy
, cmap
, colors
, 256 );
1328 unsigned char* data
= GetData();
1331 for (int y
= 0; y
< height
; y
++)
1333 for (int x
= 0; x
< width
; x
++)
1335 int r
= data
[index
];
1337 int g
= data
[index
];
1339 int b
= data
[index
];
1345 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1346 gdk_image_put_pixel( mask_image, x, y, 1 );
1348 gdk_image_put_pixel( mask_image, x, y, 0 );
1358 if (wxTheApp->m_colorCube)
1360 pixel = wxTheApp->m_colorCube
1361 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1366 int max
= 3 * (65536);
1367 for (int i
= 0; i
< 256; i
++)
1369 int rdiff
= (r
<< 8) - colors
[i
].red
;
1370 int gdiff
= (g
<< 8) - colors
[i
].green
;
1371 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1372 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1373 if (sum
< max
) { pixel
= i
; max
= sum
; }
1378 XPutPixel( data_image
, x
, y
, pixel
);
1383 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1384 XPutPixel( data_image
, x
, y
, pixel
);
1389 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1390 XPutPixel( data_image
, x
, y
, pixel
);
1399 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1400 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1401 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1402 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1403 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1404 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1406 XPutPixel( data_image
, x
, y
, pixel
);
1416 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1417 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1418 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1420 XDestroyImage( data_image
);
1428 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1430 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1432 gdk_image_destroy( mask_image );
1433 gdk_gc_unref( mask_gc );
1440 wxImage::wxImage( const wxBitmap
&bitmap
)
1442 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1444 Display
*dpy
= (Display
*) wxGetDisplay();
1445 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1446 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1448 XImage
*ximage
= XGetImage( dpy
,
1449 (Drawable
)bitmap
.GetPixmap(),
1451 bitmap
.GetWidth(), bitmap
.GetHeight(),
1452 AllPlanes
, ZPixmap
);
1454 wxCHECK_RET( ximage
, _T("couldn't create image") );
1456 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1457 char unsigned *data
= GetData();
1461 XDestroyImage( ximage
);
1462 wxFAIL_MSG( _T("couldn't create image") );
1467 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1468 if (bitmap.GetMask())
1470 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1472 bitmap.GetWidth(), bitmap.GetHeight() );
1474 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1478 // Retrieve depth info
1480 XVisualInfo vinfo_template
;
1483 vinfo_template
.visual
= vis
;
1484 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1485 vinfo_template
.depth
= bpp
;
1488 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1490 wxCHECK_RET( vi
, _T("no visual") );
1492 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1499 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1501 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1502 XQueryColors( dpy
, cmap
, colors
, 256 );
1506 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1508 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1510 int pixel
= XGetPixel( ximage
, i
, j
);
1513 data
[pos
] = colors
[pixel
].red
>> 8;
1514 data
[pos
+1] = colors
[pixel
].green
>> 8;
1515 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1516 } else if (bpp
== 15)
1518 data
[pos
] = (pixel
>> 7) & 0xf8;
1519 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1520 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1521 } else if (bpp
== 16)
1523 data
[pos
] = (pixel
>> 8) & 0xf8;
1524 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1525 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1528 data
[pos
] = (pixel
>> 16) & 0xff;
1529 data
[pos
+1] = (pixel
>> 8) & 0xff;
1530 data
[pos
+2] = pixel
& 0xff;
1536 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1537 if (mask_pixel == 0)
1550 XDestroyImage( ximage
);
1552 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1557 // A module to allow wxImage initialization/cleanup
1558 // without calling these functions from app.cpp or from
1559 // the user's application.
1561 class wxImageModule
: public wxModule
1563 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1566 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1567 void OnExit() { wxImage::CleanUpHandlers(); };
1570 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)