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
585 // sizeLimit is the MS upper limit for the DIB size
586 int sizeLimit
= 1024*768*3;
588 // width and height of the device-dependent bitmap
589 int width
= GetWidth();
590 int bmpHeight
= GetHeight();
592 // calc the number of bytes per scanline and padding
593 int bytePerLine
= width
*3;
594 int sizeDWORD
= sizeof( DWORD
);
595 int lineBoundary
= bytePerLine
% sizeDWORD
;
597 if( lineBoundary
> 0 )
599 padding
= sizeDWORD
- lineBoundary
;
600 bytePerLine
+= padding
;
602 // calc the number of DIBs and heights of DIBs
605 int height
= sizeLimit
/bytePerLine
;
606 if( height
>= bmpHeight
)
610 numDIB
= bmpHeight
/ height
;
611 hRemain
= bmpHeight
% height
;
612 if( hRemain
>0 ) numDIB
++;
615 // set bitmap parameters
617 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
618 bitmap
.SetWidth( width
);
619 bitmap
.SetHeight( bmpHeight
);
620 bitmap
.SetDepth( wxDisplayDepth() );
622 // create a DIB header
623 int headersize
= sizeof(BITMAPINFOHEADER
);
624 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
625 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
626 // Fill in the DIB header
627 lpDIBh
->bmiHeader
.biSize
= headersize
;
628 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
629 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
630 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
631 // the general formula for biSizeImage:
632 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
633 lpDIBh
->bmiHeader
.biPlanes
= 1;
634 lpDIBh
->bmiHeader
.biBitCount
= 24;
635 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
636 lpDIBh
->bmiHeader
.biClrUsed
= 0;
637 // These seem not really needed for our purpose here.
638 lpDIBh
->bmiHeader
.biClrImportant
= 0;
639 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
640 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
641 // memory for DIB data
642 unsigned char *lpBits
;
643 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
646 wxFAIL_MSG( _T("could not allocate memory for DIB") );
651 // create and set the device-dependent bitmap
652 HDC hdc
= ::GetDC(NULL
);
653 HDC memdc
= ::CreateCompatibleDC( hdc
);
655 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
656 ::SelectObject( memdc
, hbitmap
);
658 // copy image data into DIB data and then into DDB (in a loop)
659 unsigned char *data
= GetData();
662 unsigned char *ptdata
= data
;
663 unsigned char *ptbits
;
665 for( n
=0; n
<numDIB
; n
++ )
667 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
669 // redefine height and size of the (possibly) last smaller DIB
670 // memory is not reallocated
672 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
673 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
677 for( j
=0; j
<height
; j
++ )
679 for( i
=0; i
<width
; i
++ )
681 *(ptbits
++) = *(ptdata
+2);
682 *(ptbits
++) = *(ptdata
+1);
683 *(ptbits
++) = *(ptdata
);
686 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
688 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
689 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
691 // if numDIB = 1, lines below can also be used
692 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
693 // The above line is equivalent to the following two lines.
694 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
695 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
696 // or the following lines
697 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
698 // HDC memdc = ::CreateCompatibleDC( hdc );
699 // ::SelectObject( memdc, hbitmap);
700 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
701 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
702 // ::SelectObject( memdc, 0 );
703 // ::DeleteDC( memdc );
705 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
707 // similarly, created an mono-bitmap for the possible mask
710 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
711 ::SelectObject( memdc
, hbitmap
);
712 if( numDIB
== 1 ) height
= bmpHeight
;
713 else height
= sizeLimit
/bytePerLine
;
714 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
715 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
717 unsigned char r
= GetMaskRed();
718 unsigned char g
= GetMaskGreen();
719 unsigned char b
= GetMaskBlue();
720 unsigned char zero
= 0, one
= 255;
722 for( n
=0; n
<numDIB
; n
++ )
724 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
726 // redefine height and size of the (possibly) last smaller DIB
727 // memory is not reallocated
729 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
730 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
733 for( int j
=0; j
<height
; j
++ )
735 for(i
=0; i
<width
; i
++ )
737 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
750 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
752 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
753 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
756 // create a wxMask object
757 wxMask
*mask
= new wxMask();
758 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
759 bitmap
.SetMask( mask
);
760 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
761 /* The following can also be used but is slow to run
762 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
763 wxMask *mask = new wxMask( bitmap, colour );
764 bitmap.SetMask( mask );
768 // free allocated resources
769 ::SelectObject( memdc
, 0 );
771 ::ReleaseDC(NULL
, hdc
);
775 // check the wxBitmap object
776 if( bitmap
.GetHBITMAP() )
777 bitmap
.SetOk( TRUE
);
779 bitmap
.SetOk( FALSE
);
784 wxImage::wxImage( const wxBitmap
&bitmap
)
789 wxFAIL_MSG( _T("invalid bitmap") );
793 // create an wxImage object
794 int width
= bitmap
.GetWidth();
795 int height
= bitmap
.GetHeight();
796 Create( width
, height
);
797 unsigned char *data
= GetData();
800 wxFAIL_MSG( _T("could not allocate data for image") );
804 // calc the number of bytes per scanline and padding in the DIB
805 int bytePerLine
= width
*3;
806 int sizeDWORD
= sizeof( DWORD
);
807 int lineBoundary
= bytePerLine
% sizeDWORD
;
809 if( lineBoundary
> 0 )
811 padding
= sizeDWORD
- lineBoundary
;
812 bytePerLine
+= padding
;
815 // create a DIB header
816 int headersize
= sizeof(BITMAPINFOHEADER
);
817 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
820 wxFAIL_MSG( _T("could not allocate data for DIB header") );
824 // Fill in the DIB header
825 lpDIBh
->bmiHeader
.biSize
= headersize
;
826 lpDIBh
->bmiHeader
.biWidth
= width
;
827 lpDIBh
->bmiHeader
.biHeight
= -height
;
828 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
829 lpDIBh
->bmiHeader
.biPlanes
= 1;
830 lpDIBh
->bmiHeader
.biBitCount
= 24;
831 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
832 lpDIBh
->bmiHeader
.biClrUsed
= 0;
833 // These seem not really needed for our purpose here.
834 lpDIBh
->bmiHeader
.biClrImportant
= 0;
835 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
836 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
837 // memory for DIB data
838 unsigned char *lpBits
;
839 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
842 wxFAIL_MSG( _T("could not allocate data for DIB") );
848 // copy data from the device-dependent bitmap to the DIB
849 HDC hdc
= ::GetDC(NULL
);
851 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
852 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
854 // copy DIB data into the wxImage object
856 unsigned char *ptdata
= data
;
857 unsigned char *ptbits
= lpBits
;
858 for( i
=0; i
<height
; i
++ )
860 for( j
=0; j
<width
; j
++ )
862 *(ptdata
++) = *(ptbits
+2);
863 *(ptdata
++) = *(ptbits
+1);
864 *(ptdata
++) = *(ptbits
);
870 // similarly, set data according to the possible mask bitmap
871 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
873 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
874 // memory DC created, color set, data copied, and memory DC deleted
875 HDC memdc
= ::CreateCompatibleDC( hdc
);
876 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
877 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
878 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
880 // background color set to RGB(16,16,16) in consistent with wxGTK
881 unsigned char r
=16, g
=16, b
=16;
884 for( i
=0; i
<height
; i
++ )
886 for( j
=0; j
<width
; j
++ )
900 SetMaskColour( r
, g
, b
);
907 // free allocated resources
908 ::ReleaseDC(NULL
, hdc
);
915 //-----------------------------------------------------------------------------
916 // GTK conversion routines
917 //-----------------------------------------------------------------------------
923 #include "gdk/gdkx.h"
925 #if (GTK_MINOR_VERSION > 0)
926 #include "gdk/gdkrgb.h"
929 wxBitmap
wxImage::ConvertToBitmap() const
933 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
935 int width
= GetWidth();
936 int height
= GetHeight();
938 bitmap
.SetHeight( height
);
939 bitmap
.SetWidth( width
);
941 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
945 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
946 if (visual
== NULL
) visual
= gdk_visual_get_system();
947 int bpp
= visual
->depth
;
949 bitmap
.SetDepth( bpp
);
951 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
952 if (bpp
< 8) bpp
= 8;
954 #if (GTK_MINOR_VERSION > 0)
956 if (!HasMask() && (bpp
> 8))
958 static bool s_hasInitialized
= FALSE
;
960 if (!s_hasInitialized
)
963 s_hasInitialized
= TRUE
;
966 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
968 gdk_draw_rgb_image( bitmap
.GetPixmap(),
983 // Create picture image
985 GdkImage
*data_image
=
986 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
990 GdkImage
*mask_image
= (GdkImage
*) NULL
;
994 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
996 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
998 wxMask
*mask
= new wxMask();
999 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1001 bitmap
.SetMask( mask
);
1006 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1007 byte_order b_o
= RGB
;
1011 GdkVisual
*visual
= gdk_visual_get_system();
1012 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1013 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1014 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1015 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1016 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1017 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1020 int r_mask
= GetMaskRed();
1021 int g_mask
= GetMaskGreen();
1022 int b_mask
= GetMaskBlue();
1024 unsigned char* data
= GetData();
1027 for (int y
= 0; y
< height
; y
++)
1029 for (int x
= 0; x
< width
; x
++)
1031 int r
= data
[index
];
1033 int g
= data
[index
];
1035 int b
= data
[index
];
1040 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1041 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1043 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1048 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1049 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1051 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1059 if (wxTheApp
->m_colorCube
)
1061 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1065 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1066 GdkColor
*colors
= cmap
->colors
;
1067 int max
= 3 * (65536);
1069 for (int i
= 0; i
< cmap
->size
; i
++)
1071 int rdiff
= (r
<< 8) - colors
[i
].red
;
1072 int gdiff
= (g
<< 8) - colors
[i
].green
;
1073 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1074 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1075 if (sum
< max
) { pixel
= i
; max
= sum
; }
1079 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1085 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1086 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1091 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1092 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1101 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1102 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1103 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1104 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1105 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1106 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1108 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1117 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1119 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1121 gdk_image_destroy( data_image
);
1122 gdk_gc_unref( data_gc
);
1128 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1130 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1132 gdk_image_destroy( mask_image
);
1133 gdk_gc_unref( mask_gc
);
1139 wxImage::wxImage( const wxBitmap
&bitmap
)
1141 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1143 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1145 bitmap
.GetWidth(), bitmap
.GetHeight() );
1147 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1149 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1150 char unsigned *data
= GetData();
1154 gdk_image_destroy( gdk_image
);
1155 wxFAIL_MSG( _T("couldn't create image") );
1159 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1160 if (bitmap
.GetMask())
1162 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1164 bitmap
.GetWidth(), bitmap
.GetHeight() );
1166 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1169 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1170 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1171 int bpp
= visual
->depth
;
1172 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1174 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1177 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1179 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1181 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1184 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1185 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1186 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1187 } else if (bpp
== 15)
1189 data
[pos
] = (pixel
>> 7) & 0xf8;
1190 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1191 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1192 } else if (bpp
== 16)
1194 data
[pos
] = (pixel
>> 8) & 0xf8;
1195 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1196 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1199 data
[pos
] = (pixel
>> 16) & 0xff;
1200 data
[pos
+1] = (pixel
>> 8) & 0xff;
1201 data
[pos
+2] = pixel
& 0xff;
1206 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1207 if (mask_pixel
== 0)
1219 gdk_image_destroy( gdk_image
);
1220 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1225 //-----------------------------------------------------------------------------
1226 // Motif conversion routines
1227 //-----------------------------------------------------------------------------
1232 #include "wx/utils.h"
1235 wxBitmap
wxImage::ConvertToBitmap() const
1239 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1241 int width
= GetWidth();
1242 int height
= GetHeight();
1244 bitmap
.SetHeight( height
);
1245 bitmap
.SetWidth( width
);
1247 Display
*dpy
= (Display
*) wxGetDisplay();
1248 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1249 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1253 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1254 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1256 bitmap
.Create( width
, height
, bpp
);
1261 GdkImage *mask_image = (GdkImage*) NULL;
1265 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1267 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1269 wxMask *mask = new wxMask();
1270 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1272 bitmap.SetMask( mask );
1276 // Retrieve depth info
1278 XVisualInfo vinfo_template
;
1281 vinfo_template
.visual
= vis
;
1282 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1283 vinfo_template
.depth
= bpp
;
1286 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1288 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1292 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1293 if (bpp
< 8) bpp
= 8;
1297 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1298 byte_order b_o
= RGB
;
1302 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1303 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1304 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1305 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1306 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1307 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1311 int r_mask = GetMaskRed();
1312 int g_mask = GetMaskGreen();
1313 int b_mask = GetMaskBlue();
1319 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1321 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1322 XQueryColors( dpy
, cmap
, colors
, 256 );
1325 unsigned char* data
= GetData();
1328 for (int y
= 0; y
< height
; y
++)
1330 for (int x
= 0; x
< width
; x
++)
1332 int r
= data
[index
];
1334 int g
= data
[index
];
1336 int b
= data
[index
];
1342 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1343 gdk_image_put_pixel( mask_image, x, y, 1 );
1345 gdk_image_put_pixel( mask_image, x, y, 0 );
1355 if (wxTheApp->m_colorCube)
1357 pixel = wxTheApp->m_colorCube
1358 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1363 int max
= 3 * (65536);
1364 for (int i
= 0; i
< 256; i
++)
1366 int rdiff
= (r
<< 8) - colors
[i
].red
;
1367 int gdiff
= (g
<< 8) - colors
[i
].green
;
1368 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1369 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1370 if (sum
< max
) { pixel
= i
; max
= sum
; }
1375 XPutPixel( data_image
, x
, y
, pixel
);
1380 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1381 XPutPixel( data_image
, x
, y
, pixel
);
1386 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1387 XPutPixel( data_image
, x
, y
, pixel
);
1396 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1397 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1398 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1399 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1400 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1401 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1403 XPutPixel( data_image
, x
, y
, pixel
);
1413 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1414 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1415 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1417 XDestroyImage( data_image
);
1425 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1427 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1429 gdk_image_destroy( mask_image );
1430 gdk_gc_unref( mask_gc );
1437 wxImage::wxImage( const wxBitmap
&bitmap
)
1439 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1441 Display
*dpy
= (Display
*) wxGetDisplay();
1442 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1443 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1445 XImage
*ximage
= XGetImage( dpy
,
1446 (Drawable
)bitmap
.GetPixmap(),
1448 bitmap
.GetWidth(), bitmap
.GetHeight(),
1449 AllPlanes
, ZPixmap
);
1451 wxCHECK_RET( ximage
, _T("couldn't create image") );
1453 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1454 char unsigned *data
= GetData();
1458 XDestroyImage( ximage
);
1459 wxFAIL_MSG( _T("couldn't create image") );
1464 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1465 if (bitmap.GetMask())
1467 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1469 bitmap.GetWidth(), bitmap.GetHeight() );
1471 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1475 // Retrieve depth info
1477 XVisualInfo vinfo_template
;
1480 vinfo_template
.visual
= vis
;
1481 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1482 vinfo_template
.depth
= bpp
;
1485 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1487 wxCHECK_RET( vi
, _T("no visual") );
1489 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1496 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1498 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1499 XQueryColors( dpy
, cmap
, colors
, 256 );
1503 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1505 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1507 int pixel
= XGetPixel( ximage
, i
, j
);
1510 data
[pos
] = colors
[pixel
].red
>> 8;
1511 data
[pos
+1] = colors
[pixel
].green
>> 8;
1512 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1513 } else if (bpp
== 15)
1515 data
[pos
] = (pixel
>> 7) & 0xf8;
1516 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1517 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1518 } else if (bpp
== 16)
1520 data
[pos
] = (pixel
>> 8) & 0xf8;
1521 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1522 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1525 data
[pos
] = (pixel
>> 16) & 0xff;
1526 data
[pos
+1] = (pixel
>> 8) & 0xff;
1527 data
[pos
+2] = pixel
& 0xff;
1533 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1534 if (mask_pixel == 0)
1547 XDestroyImage( ximage
);
1549 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1554 // A module to allow wxImage initialization/cleanup
1555 // without calling these functions from app.cpp or from
1556 // the user's application.
1558 class wxImageModule
: public wxModule
1560 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1563 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1564 void OnExit() { wxImage::CleanUpHandlers(); };
1567 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)