1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
18 #include "wx/palette.h"
19 #include "wx/bitmap.h"
28 #if !USE_SHARED_LIBRARIES
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
30 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
33 #include <PictUtils.h>
35 CTabHandle
wxMacCreateColorTable( int numColors
)
37 CTabHandle newColors
; /* Handle to the new color table */
38 short index
; /* Index into the table of colors */
39 /* Allocate memory for the color table */
40 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
41 sizeof (ColorSpec
) * (numColors
- 1) );
44 /* Initialize the fields */
45 (**newColors
).ctSeed
= GetCTSeed();
46 (**newColors
).ctFlags
= 0;
47 (**newColors
).ctSize
= numColors
- 1;
48 /* Initialize the table of colors */
53 void wxMacDestroyColorTable( CTabHandle colors
)
55 DisposeHandle( (Handle
) colors
) ;
58 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
60 (**newColors
).ctTable
[index
].value
= index
;
61 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
62 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
63 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
66 GWorldPtr
wxMacCreateGWorld( int height
, int width
, int depth
)
70 Rect rect
= { 0 , 0 , width
, height
} ;
74 // get max pixel depth
76 GetCWMgrPort( &port
) ;
79 maxDevice
= GetMaxDevice( &port
->portRect
) ;
81 depth
= (**((**maxDevice
).gdPMap
)).pixelSize
;
86 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
94 void wxMacDestroyGWorld( GWorldPtr gw
)
100 wxBitmapRefData::wxBitmapRefData()
111 m_bitmapType
= kMacBitmapTypeUnknownType
;
114 wxBitmapRefData::~wxBitmapRefData()
116 switch (m_bitmapType
)
118 case kMacBitmapTypePict
:
122 KillPicture( m_hPict
) ;
127 case kMacBitmapTypeGrafWorld
:
131 wxMacDestroyGWorld( m_hBitmap
) ;
148 wxList
wxBitmap::sm_handlers
;
154 if ( wxTheBitmapList
)
155 wxTheBitmapList
->AddBitmap(this);
158 wxBitmap::~wxBitmap()
161 wxTheBitmapList
->DeleteObject(this);
164 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
166 m_refData
= new wxBitmapRefData
;
168 M_BITMAPDATA
->m_width
= the_width
;
169 M_BITMAPDATA
->m_height
= the_height
;
170 M_BITMAPDATA
->m_depth
= no_bits
;
171 M_BITMAPDATA
->m_numColors
= 0;
174 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
175 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
176 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
179 GDHandle origDevice
;
181 GetGWorld( &origPort
, &origDevice
) ;
182 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
184 // bits is a word aligned array
186 unsigned char* linestart
= (unsigned char*) bits
;
187 int linesize
= ( the_width
/ 16 ) * 2 ;
188 if ( the_width
% 16 )
193 RGBColor colors
[2] = {
194 { 0xFFFF , 0xFFFF , 0xFFFF } ,
198 for( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
200 for( int x
= 0 ; x
< the_width
; ++x
)
204 int mask
= 1 << bit
;
205 if ( linestart
[index
] & mask
)
207 SetCPixel( x
, y
, &colors
[1] ) ;
211 SetCPixel( x
, y
, &colors
[0] ) ;
217 SetGWorld( origPort
, origDevice
) ;
221 //multicolor BITMAPs not yet implemented
224 if ( wxTheBitmapList
)
225 wxTheBitmapList
->AddBitmap(this);
228 wxBitmap::wxBitmap(int w
, int h
, int d
)
230 (void)Create(w
, h
, d
);
232 if ( wxTheBitmapList
)
233 wxTheBitmapList
->AddBitmap(this);
236 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
238 (void) Create(data
, type
, width
, height
, depth
);
240 if ( wxTheBitmapList
)
241 wxTheBitmapList
->AddBitmap(this);
244 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
246 LoadFile(filename
, (int)type
);
248 if ( wxTheBitmapList
)
249 wxTheBitmapList
->AddBitmap(this);
252 wxBitmap::wxBitmap(const char **data
)
254 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
257 bool wxBitmap::Create(int w
, int h
, int d
)
261 m_refData
= new wxBitmapRefData
;
263 M_BITMAPDATA
->m_width
= w
;
264 M_BITMAPDATA
->m_height
= h
;
265 M_BITMAPDATA
->m_depth
= d
;
267 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
268 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
269 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
270 return M_BITMAPDATA
->m_ok
;
273 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
275 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
276 M_BITMAPDATA
->m_hBitmap
= bmp
;
277 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
280 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
284 m_refData
= new wxBitmapRefData
;
286 wxBitmapHandler
*handler
= FindHandler(type
);
288 if ( handler
== NULL
) {
289 wxLogWarning("no bitmap handler for type %d defined.", type
);
294 return handler
->LoadFile(this, filename
, type
, -1, -1);
297 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
301 m_refData
= new wxBitmapRefData
;
303 wxBitmapHandler
*handler
= FindHandler(type
);
305 if ( handler
== NULL
) {
306 wxLogWarning("no bitmap handler for type %d defined.", type
);
311 return handler
->Create(this, data
, type
, width
, height
, depth
);
314 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
316 wxBitmapHandler
*handler
= FindHandler(type
);
318 if ( handler
== NULL
) {
319 wxLogWarning("no bitmap handler for type %d defined.", type
);
324 return handler
->SaveFile(this, filename
, type
, palette
);
327 void wxBitmap::SetWidth(int w
)
330 m_refData
= new wxBitmapRefData
;
332 M_BITMAPDATA
->m_width
= w
;
335 void wxBitmap::SetHeight(int h
)
338 m_refData
= new wxBitmapRefData
;
340 M_BITMAPDATA
->m_height
= h
;
343 void wxBitmap::SetDepth(int d
)
346 m_refData
= new wxBitmapRefData
;
348 M_BITMAPDATA
->m_depth
= d
;
351 void wxBitmap::SetQuality(int q
)
354 m_refData
= new wxBitmapRefData
;
356 M_BITMAPDATA
->m_quality
= q
;
359 void wxBitmap::SetOk(bool isOk
)
362 m_refData
= new wxBitmapRefData
;
364 M_BITMAPDATA
->m_ok
= isOk
;
367 void wxBitmap::SetPalette(const wxPalette
& palette
)
370 m_refData
= new wxBitmapRefData
;
372 M_BITMAPDATA
->m_bitmapPalette
= palette
;
375 void wxBitmap::SetMask(wxMask
*mask
)
378 m_refData
= new wxBitmapRefData
;
380 M_BITMAPDATA
->m_bitmapMask
= mask
;
383 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
385 sm_handlers
.Append(handler
);
388 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
390 sm_handlers
.Insert(handler
);
393 bool wxBitmap::RemoveHandler(const wxString
& name
)
395 wxBitmapHandler
*handler
= FindHandler(name
);
398 sm_handlers
.DeleteObject(handler
);
405 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
407 wxNode
*node
= sm_handlers
.First();
410 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
411 if ( handler
->GetName() == name
)
418 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
420 wxNode
*node
= sm_handlers
.First();
423 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
424 if ( handler
->GetExtension() == extension
&&
425 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
432 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
434 wxNode
*node
= sm_handlers
.First();
437 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
438 if (handler
->GetType() == bitmapType
)
456 // Construct a mask from a bitmap and a colour indicating
457 // the transparent area
458 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
463 Create(bitmap
, colour
);
466 // Construct a mask from a bitmap and a palette index indicating
467 // the transparent area
468 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
474 Create(bitmap
, paletteIndex
);
477 // Construct a mask from a mono bitmap (copies the bitmap).
478 wxMask::wxMask(const wxBitmap
& bitmap
)
489 // TODO: delete mask bitmap
492 // Create a mask from a mono bitmap (copies the bitmap).
493 bool wxMask::Create(const wxBitmap
& bitmap
)
499 // Create a mask from a bitmap and a palette index indicating
500 // the transparent area
501 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
507 // Create a mask from a bitmap and a colour indicating
508 // the transparent area
509 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
519 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
521 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
526 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
527 int desiredWidth
, int desiredHeight
)
532 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
541 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
543 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
545 inline wxPICTResourceHandler()
547 m_name
= "Macintosh Pict resource";
549 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
552 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
553 int desiredWidth
, int desiredHeight
);
555 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
557 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
558 int desiredWidth
, int desiredHeight
)
562 strcpy( (char*) theName
, name
) ;
563 c2pstr( (char*) theName
) ;
565 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
570 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
571 DetachResource( (Handle
) thePict
) ;
572 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
573 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
574 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
575 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
577 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
578 M_BITMAPHANDLERDATA
->m_ok
= true ;
579 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
580 // M_BITMAPHANDLERDATA->m_bitmapPalette;
581 // M_BITMAPHANDLERDATA->m_quality;
587 /* TODO: bitmap handlers, a bit like this:
588 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
590 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
592 inline wxBMPResourceHandler()
594 m_name = "Windows bitmap resource";
596 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
599 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
600 int desiredWidth, int desiredHeight);
602 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
605 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
607 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
609 inline wxXPMFileHandler(void)
611 m_name
= "XPM bitmap file";
613 m_type
= wxBITMAP_TYPE_XPM
;
616 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
617 int desiredWidth
= -1, int desiredHeight
= -1);
618 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
620 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
622 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
623 int desiredWidth
, int desiredHeight
)
627 XpmAttributes xpmAttr
;
630 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
631 dc
= CreateCompatibleDC(NULL
);
634 xpmAttr
.valuemask
= XpmReturnPixels
;
635 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
637 if (errorStatus
== XpmSuccess
)
639 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
642 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
644 M_BITMAPHANDLERDATA
->m_width
= (bm
.bmWidth
);
645 M_BITMAPHANDLERDATA
->m_height
= (bm
.bmHeight
);
646 M_BITMAPHANDLERDATA
->m_depth
= (bm
.bmPlanes
* bm
.bmBitsPixel
);
647 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
648 XpmFreeAttributes(&xpmAttr
);
651 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
656 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
665 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
670 Visual
*visual
= NULL
;
673 dc
= CreateCompatibleDC(NULL
);
676 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
677 { /* for following SetPixel */
678 /* fill the XImage struct 'by hand' */
679 ximage
.width
= M_BITMAPHANDLERDATA
->m_width
;
680 ximage
.height
= M_BITMAPHANDLERDATA
->m_height
;
681 ximage
.depth
= M_BITMAPHANDLERDATA
->m_depth
;
682 ximage
.bitmap
= (void *)M_BITMAPHANDLERDATA
->m_hBitmap
;
683 int errorStatus
= XpmWriteFileFromImage(&dc
, WXSTRINGCAST name
,
684 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
689 if (errorStatus
== XpmSuccess
)
690 return TRUE
; /* no error */
701 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
703 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
705 inline wxXPMDataHandler(void)
707 m_name
= "XPM bitmap data";
709 m_type
= wxBITMAP_TYPE_XPM_DATA
;
712 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
714 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
716 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
720 XpmAttributes xpmAttr
;
722 xpmAttr
.valuemask
= XpmReturnInfos
; // get infos back
723 ErrorStatus
= XpmCreateImageFromData( GetMainDevice() , (char **)data
,
724 &ximage
, (XImage
**) NULL
, &xpmAttr
);
726 if (ErrorStatus
== XpmSuccess
)
728 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
729 M_BITMAPHANDLERDATA
->m_numColors
= 0;
730 M_BITMAPHANDLERDATA
->m_hBitmap
= ximage
->gworldptr
;
732 M_BITMAPHANDLERDATA
->m_width
= ximage
->width
;
733 M_BITMAPHANDLERDATA
->m_height
= ximage
->height
;
734 M_BITMAPHANDLERDATA
->m_depth
= ximage
->depth
;
735 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
736 XpmFreeAttributes(&xpmAttr
);
737 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
738 ximage
->gworldptr
= NULL
;
739 XImageFree(ximage
); // releases the malloc, but does not detroy
741 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
747 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
753 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
755 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
757 inline wxBMPResourceHandler()
759 m_name
= "Windows bitmap resource";
761 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
764 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
765 int desiredWidth
, int desiredHeight
);
768 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
770 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
771 int desiredWidth
, int desiredHeight
)
773 // TODO: load colourmap.
775 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ::LoadBitmap(wxGetInstance(), name);
776 if (M_BITMAPHANDLERDATA->m_hBitmap)
778 M_BITMAPHANDLERDATA->m_ok = TRUE;
780 GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(BITMAP), (LPSTR) &bm);
781 M_BITMAPHANDLERDATA->m_width = bm.bmWidth;
782 M_BITMAPHANDLERDATA->m_height = bm.bmHeight;
783 M_BITMAPHANDLERDATA->m_depth = bm.bmBitsPixel;
787 // it's probably not found
788 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name
.c_str());
793 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
795 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
797 inline wxBMPFileHandler(void)
799 m_name
= "Windows bitmap file";
801 m_type
= wxBITMAP_TYPE_BMP
;
804 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
805 int desiredWidth
, int desiredHeight
);
806 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
809 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
811 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
812 int desiredWidth
, int desiredHeight
)
814 #if USE_IMAGE_LOADING_IN_MSW
815 wxPalette
*palette
= NULL
;
816 bool success
= FALSE
;
818 if (type & wxBITMAP_DISCARD_COLOURMAP)
819 success = wxLoadIntoBitmap(WXSTRINGCAST name, bitmap);
822 success
= (wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0);
823 if (!success
&& palette
)
829 M_BITMAPHANDLERDATA
->m_bitmapPalette
= *palette
;
836 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
838 #if USE_IMAGE_LOADING_IN_MSW
839 wxPalette
*actualPalette
= (wxPalette
*)pal
;
840 if (!actualPalette
&& (!M_BITMAPHANDLERDATA
->m_bitmapPalette
.IsNull()))
841 actualPalette
= & (M_BITMAPHANDLERDATA
->m_bitmapPalette
);
842 return (wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0);
850 void wxBitmap::CleanUpHandlers()
852 wxNode
*node
= sm_handlers
.First();
855 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
856 wxNode
*next
= node
->Next();
863 void wxBitmap::InitStandardHandlers()
865 AddHandler( new wxPICTResourceHandler
) ;
866 AddHandler( new wxICONResourceHandler
) ;
867 AddHandler(new wxXPMFileHandler
);
868 AddHandler(new wxXPMDataHandler
);
869 AddHandler(new wxBMPResourceHandler
);
870 AddHandler(new wxBMPFileHandler
);