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 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
31 #include <PictUtils.h>
33 CTabHandle
wxMacCreateColorTable( int numColors
)
35 CTabHandle newColors
; /* Handle to the new color table */
36 short index
; /* Index into the table of colors */
37 /* Allocate memory for the color table */
38 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
39 sizeof (ColorSpec
) * (numColors
- 1) );
42 /* Initialize the fields */
43 (**newColors
).ctSeed
= GetCTSeed();
44 (**newColors
).ctFlags
= 0;
45 (**newColors
).ctSize
= numColors
- 1;
46 /* Initialize the table of colors */
51 void wxMacDestroyColorTable( CTabHandle colors
)
53 DisposeHandle( (Handle
) colors
) ;
56 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
58 (**newColors
).ctTable
[index
].value
= index
;
59 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
60 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
61 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
64 GWorldPtr
wxMacCreateGWorld( int height
, int width
, int depth
)
68 Rect rect
= { 0 , 0 , width
, height
} ;
72 // get max pixel depth
74 GetCWMgrPort( &port
) ;
77 maxDevice
= GetMaxDevice( &port
->portRect
) ;
79 depth
= (**((**maxDevice
).gdPMap
)).pixelSize
;
84 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
92 void wxMacDestroyGWorld( GWorldPtr gw
)
98 wxBitmapRefData::wxBitmapRefData()
109 m_bitmapType
= kMacBitmapTypeUnknownType
;
112 wxBitmapRefData::~wxBitmapRefData()
114 switch (m_bitmapType
)
116 case kMacBitmapTypePict
:
120 KillPicture( m_hPict
) ;
125 case kMacBitmapTypeGrafWorld
:
129 wxMacDestroyGWorld( m_hBitmap
) ;
146 wxList
wxBitmap::sm_handlers
;
152 if ( wxTheBitmapList
)
153 wxTheBitmapList
->AddBitmap(this);
156 wxBitmap::~wxBitmap()
159 wxTheBitmapList
->DeleteObject(this);
162 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
164 m_refData
= new wxBitmapRefData
;
166 M_BITMAPDATA
->m_width
= the_width
;
167 M_BITMAPDATA
->m_height
= the_height
;
168 M_BITMAPDATA
->m_depth
= no_bits
;
169 M_BITMAPDATA
->m_numColors
= 0;
172 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
173 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
174 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
177 GDHandle origDevice
;
179 GetGWorld( &origPort
, &origDevice
) ;
180 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
182 // bits is a word aligned array
184 unsigned char* linestart
= (unsigned char*) bits
;
185 int linesize
= ( the_width
/ 16 ) * 2 ;
186 if ( the_width
% 16 )
191 RGBColor colors
[2] = {
192 { 0xFFFF , 0xFFFF , 0xFFFF } ,
196 for( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
198 for( int x
= 0 ; x
< the_width
; ++x
)
202 int mask
= 1 << bit
;
203 if ( linestart
[index
] & mask
)
205 SetCPixel( x
, y
, &colors
[1] ) ;
209 SetCPixel( x
, y
, &colors
[0] ) ;
215 SetGWorld( origPort
, origDevice
) ;
219 //multicolor BITMAPs not yet implemented
222 if ( wxTheBitmapList
)
223 wxTheBitmapList
->AddBitmap(this);
226 wxBitmap::wxBitmap(int w
, int h
, int d
)
228 (void)Create(w
, h
, d
);
230 if ( wxTheBitmapList
)
231 wxTheBitmapList
->AddBitmap(this);
234 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
236 (void) Create(data
, type
, width
, height
, depth
);
238 if ( wxTheBitmapList
)
239 wxTheBitmapList
->AddBitmap(this);
242 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
244 LoadFile(filename
, (int)type
);
246 if ( wxTheBitmapList
)
247 wxTheBitmapList
->AddBitmap(this);
250 wxBitmap::wxBitmap(const char **data
)
252 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
255 bool wxBitmap::Create(int w
, int h
, int d
)
259 m_refData
= new wxBitmapRefData
;
261 M_BITMAPDATA
->m_width
= w
;
262 M_BITMAPDATA
->m_height
= h
;
263 M_BITMAPDATA
->m_depth
= d
;
265 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
266 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
267 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
268 return M_BITMAPDATA
->m_ok
;
271 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
273 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
274 M_BITMAPDATA
->m_hBitmap
= bmp
;
275 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
278 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
282 m_refData
= new wxBitmapRefData
;
284 wxBitmapHandler
*handler
= FindHandler(type
);
286 if ( handler
== NULL
) {
287 wxLogWarning("no bitmap handler for type %d defined.", type
);
292 return handler
->LoadFile(this, filename
, type
, -1, -1);
295 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
299 m_refData
= new wxBitmapRefData
;
301 wxBitmapHandler
*handler
= FindHandler(type
);
303 if ( handler
== NULL
) {
304 wxLogWarning("no bitmap handler for type %d defined.", type
);
309 return handler
->Create(this, data
, type
, width
, height
, depth
);
312 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
314 wxBitmapHandler
*handler
= FindHandler(type
);
316 if ( handler
== NULL
) {
317 wxLogWarning("no bitmap handler for type %d defined.", type
);
322 return handler
->SaveFile(this, filename
, type
, palette
);
325 void wxBitmap::SetWidth(int w
)
328 m_refData
= new wxBitmapRefData
;
330 M_BITMAPDATA
->m_width
= w
;
333 void wxBitmap::SetHeight(int h
)
336 m_refData
= new wxBitmapRefData
;
338 M_BITMAPDATA
->m_height
= h
;
341 void wxBitmap::SetDepth(int d
)
344 m_refData
= new wxBitmapRefData
;
346 M_BITMAPDATA
->m_depth
= d
;
349 void wxBitmap::SetQuality(int q
)
352 m_refData
= new wxBitmapRefData
;
354 M_BITMAPDATA
->m_quality
= q
;
357 void wxBitmap::SetOk(bool isOk
)
360 m_refData
= new wxBitmapRefData
;
362 M_BITMAPDATA
->m_ok
= isOk
;
365 void wxBitmap::SetPalette(const wxPalette
& palette
)
368 m_refData
= new wxBitmapRefData
;
370 M_BITMAPDATA
->m_bitmapPalette
= palette
;
373 void wxBitmap::SetMask(wxMask
*mask
)
376 m_refData
= new wxBitmapRefData
;
378 M_BITMAPDATA
->m_bitmapMask
= mask
;
381 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
383 sm_handlers
.Append(handler
);
386 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
388 sm_handlers
.Insert(handler
);
391 bool wxBitmap::RemoveHandler(const wxString
& name
)
393 wxBitmapHandler
*handler
= FindHandler(name
);
396 sm_handlers
.DeleteObject(handler
);
403 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
405 wxNode
*node
= sm_handlers
.First();
408 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
409 if ( handler
->GetName() == name
)
416 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
418 wxNode
*node
= sm_handlers
.First();
421 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
422 if ( handler
->GetExtension() == extension
&&
423 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
430 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
432 wxNode
*node
= sm_handlers
.First();
435 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
436 if (handler
->GetType() == bitmapType
)
454 // Construct a mask from a bitmap and a colour indicating
455 // the transparent area
456 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
461 Create(bitmap
, colour
);
464 // Construct a mask from a bitmap and a palette index indicating
465 // the transparent area
466 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
472 Create(bitmap
, paletteIndex
);
475 // Construct a mask from a mono bitmap (copies the bitmap).
476 wxMask::wxMask(const wxBitmap
& bitmap
)
487 // TODO: delete mask bitmap
490 // Create a mask from a mono bitmap (copies the bitmap).
491 bool wxMask::Create(const wxBitmap
& bitmap
)
497 // Create a mask from a bitmap and a palette index indicating
498 // the transparent area
499 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
505 // Create a mask from a bitmap and a colour indicating
506 // the transparent area
507 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
517 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
519 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
524 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
525 int desiredWidth
, int desiredHeight
)
530 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
539 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
541 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
543 inline wxPICTResourceHandler()
545 m_name
= "Macintosh Pict resource";
547 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
550 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
551 int desiredWidth
, int desiredHeight
);
553 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
555 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
556 int desiredWidth
, int desiredHeight
)
560 strcpy( (char*) theName
, name
) ;
561 c2pstr( (char*) theName
) ;
563 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
568 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
569 DetachResource( (Handle
) thePict
) ;
570 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
571 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
572 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
573 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
575 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
576 M_BITMAPHANDLERDATA
->m_ok
= true ;
577 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
578 // M_BITMAPHANDLERDATA->m_bitmapPalette;
579 // M_BITMAPHANDLERDATA->m_quality;
585 /* TODO: bitmap handlers, a bit like this:
586 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
588 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
590 inline wxBMPResourceHandler()
592 m_name = "Windows bitmap resource";
594 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
597 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
598 int desiredWidth, int desiredHeight);
600 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
603 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
605 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
607 inline wxXPMFileHandler(void)
609 m_name
= "XPM bitmap file";
611 m_type
= wxBITMAP_TYPE_XPM
;
614 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
615 int desiredWidth
= -1, int desiredHeight
= -1);
616 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
618 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
620 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
621 int desiredWidth
, int desiredHeight
)
625 XpmAttributes xpmAttr
;
628 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
629 dc
= CreateCompatibleDC(NULL
);
632 xpmAttr
.valuemask
= XpmReturnPixels
;
633 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
635 if (errorStatus
== XpmSuccess
)
637 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
640 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
642 M_BITMAPHANDLERDATA
->m_width
= (bm
.bmWidth
);
643 M_BITMAPHANDLERDATA
->m_height
= (bm
.bmHeight
);
644 M_BITMAPHANDLERDATA
->m_depth
= (bm
.bmPlanes
* bm
.bmBitsPixel
);
645 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
646 XpmFreeAttributes(&xpmAttr
);
649 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
654 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
663 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
668 Visual
*visual
= NULL
;
671 dc
= CreateCompatibleDC(NULL
);
674 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
675 { /* for following SetPixel */
676 /* fill the XImage struct 'by hand' */
677 ximage
.width
= M_BITMAPHANDLERDATA
->m_width
;
678 ximage
.height
= M_BITMAPHANDLERDATA
->m_height
;
679 ximage
.depth
= M_BITMAPHANDLERDATA
->m_depth
;
680 ximage
.bitmap
= (void *)M_BITMAPHANDLERDATA
->m_hBitmap
;
681 int errorStatus
= XpmWriteFileFromImage(&dc
, WXSTRINGCAST name
,
682 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
687 if (errorStatus
== XpmSuccess
)
688 return TRUE
; /* no error */
699 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
701 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
703 inline wxXPMDataHandler(void)
705 m_name
= "XPM bitmap data";
707 m_type
= wxBITMAP_TYPE_XPM_DATA
;
710 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
712 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
714 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
718 XpmAttributes xpmAttr
;
720 xpmAttr
.valuemask
= XpmReturnInfos
; // get infos back
721 ErrorStatus
= XpmCreateImageFromData( GetMainDevice() , (char **)data
,
722 &ximage
, (XImage
**) NULL
, &xpmAttr
);
724 if (ErrorStatus
== XpmSuccess
)
726 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
727 M_BITMAPHANDLERDATA
->m_numColors
= 0;
728 M_BITMAPHANDLERDATA
->m_hBitmap
= ximage
->gworldptr
;
730 M_BITMAPHANDLERDATA
->m_width
= ximage
->width
;
731 M_BITMAPHANDLERDATA
->m_height
= ximage
->height
;
732 M_BITMAPHANDLERDATA
->m_depth
= ximage
->depth
;
733 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
734 XpmFreeAttributes(&xpmAttr
);
735 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
736 ximage
->gworldptr
= NULL
;
737 XImageFree(ximage
); // releases the malloc, but does not detroy
739 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
745 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
751 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
753 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
755 inline wxBMPResourceHandler()
757 m_name
= "Windows bitmap resource";
759 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
762 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
763 int desiredWidth
, int desiredHeight
);
766 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
768 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
769 int desiredWidth
, int desiredHeight
)
771 // TODO: load colourmap.
773 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ::LoadBitmap(wxGetInstance(), name);
774 if (M_BITMAPHANDLERDATA->m_hBitmap)
776 M_BITMAPHANDLERDATA->m_ok = TRUE;
778 GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(BITMAP), (LPSTR) &bm);
779 M_BITMAPHANDLERDATA->m_width = bm.bmWidth;
780 M_BITMAPHANDLERDATA->m_height = bm.bmHeight;
781 M_BITMAPHANDLERDATA->m_depth = bm.bmBitsPixel;
785 // it's probably not found
786 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name
.c_str());
791 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
793 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
795 inline wxBMPFileHandler(void)
797 m_name
= "Windows bitmap file";
799 m_type
= wxBITMAP_TYPE_BMP
;
802 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
803 int desiredWidth
, int desiredHeight
);
804 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
807 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
809 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
810 int desiredWidth
, int desiredHeight
)
812 #if USE_IMAGE_LOADING_IN_MSW
813 wxPalette
*palette
= NULL
;
814 bool success
= FALSE
;
816 if (type & wxBITMAP_DISCARD_COLOURMAP)
817 success = wxLoadIntoBitmap(WXSTRINGCAST name, bitmap);
820 success
= (wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0);
821 if (!success
&& palette
)
827 M_BITMAPHANDLERDATA
->m_bitmapPalette
= *palette
;
834 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
836 #if USE_IMAGE_LOADING_IN_MSW
837 wxPalette
*actualPalette
= (wxPalette
*)pal
;
838 if (!actualPalette
&& (!M_BITMAPHANDLERDATA
->m_bitmapPalette
.IsNull()))
839 actualPalette
= & (M_BITMAPHANDLERDATA
->m_bitmapPalette
);
840 return (wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0);
848 void wxBitmap::CleanUpHandlers()
850 wxNode
*node
= sm_handlers
.First();
853 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
854 wxNode
*next
= node
->Next();
861 void wxBitmap::InitStandardHandlers()
863 AddHandler( new wxPICTResourceHandler
) ;
864 AddHandler( new wxICONResourceHandler
) ;
865 AddHandler(new wxXPMFileHandler
);
866 AddHandler(new wxXPMDataHandler
);
867 AddHandler(new wxBMPResourceHandler
);
868 AddHandler(new wxBMPFileHandler
);