📄 bitmap.cpp
字号:
/////////////////////////////////////////////////////////////////////////////// Name: src/mac/classic/bitmap.cpp// Purpose: wxBitmap// Author: Stefan Csomor// Modified by:// Created: 1998-01-01// RCS-ID: $Id: bitmap.cpp,v 1.12 2006/10/30 19:26:38 VZ Exp $// Copyright: (c) Stefan Csomor// Licence: wxWindows licence/////////////////////////////////////////////////////////////////////////////#include "wx/wxprec.h"#ifdef __BORLANDC__ #pragma hdrstop#endif#include "wx/bitmap.h"#ifndef WX_PRECOMP #include "wx/log.h" #include "wx/icon.h" #include "wx/image.h"#endif#include "wx/xpmdecod.h"#include "wx/rawbmp.h"IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )#ifdef __DARWIN__ #include <ApplicationServices/ApplicationServices.h>#else #include <PictUtils.h>#endif#include "wx/mac/uma.h"CTabHandle wxMacCreateColorTable( int numColors ){ CTabHandle newColors; /* Handle to the new color table */ /* Allocate memory for the color table */ newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) + sizeof (ColorSpec) * (numColors - 1) ); if (newColors != nil) { /* Initialize the fields */ (**newColors).ctSeed = GetCTSeed(); (**newColors).ctFlags = 0; (**newColors).ctSize = numColors - 1; /* Initialize the table of colors */ } return newColors ;}void wxMacDestroyColorTable( CTabHandle colors ){ DisposeHandle( (Handle) colors ) ;}void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue ){ (**newColors).ctTable[index].value = index; (**newColors).ctTable[index].rgb.red = red ; // someRedValue; (**newColors).ctTable[index].rgb.green = green ; // someGreenValue; (**newColors).ctTable[index].rgb.blue = blue ; // someBlueValue;}GWorldPtr wxMacCreateGWorld( int width , int height , int depth ){ OSErr err = noErr ; GWorldPtr port ; Rect rect = { 0 , 0 , height , width } ; if ( depth < 0 ) { depth = wxDisplayDepth() ; } err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ; if ( err == noErr ) { return port ; } return NULL ;}void wxMacDestroyGWorld( GWorldPtr gw ){ if ( gw ) DisposeGWorld( gw ) ;}#define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */OSErr SetupCIconHandlePixMap( CIconHandle icon , short depth , Rect *bounds , CTabHandle colors ){ CTabHandle newColors; /* Color table used for the off-screen PixMap */ Ptr offBaseAddr; /* Pointer to the off-screen pixel image */ OSErr error; /* Returns error code */ short bytesPerRow; /* Number of bytes per row in the PixMap */ error = noErr; newColors = nil; offBaseAddr = nil; bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4; /* Clone the clut if indexed color; allocate a dummy clut if direct color*/ if (depth <= 8) { newColors = colors; error = HandToHand((Handle *) &newColors); } else { newColors = (CTabHandle) NewHandle(sizeof(ColorTable) - sizeof(CSpecArray)); error = MemError(); } if (error == noErr) { /* Allocate pixel image; long integer multiplication avoids overflow */ (**icon).iconData = NewHandle((unsigned long) bytesPerRow * (bounds->bottom - bounds->top)); if ((**icon).iconData != nil) { /* Initialize fields common to indexed and direct PixMaps */ (**icon).iconPMap.baseAddr = 0; /* Point to image */ (**icon).iconPMap.rowBytes = bytesPerRow | /* MSB set for PixMap */ 0x8000; (**icon).iconPMap.bounds = *bounds; /* Use given bounds */ (**icon).iconPMap.pmVersion = 0; /* No special stuff */ (**icon).iconPMap.packType = 0; /* Default PICT pack */ (**icon).iconPMap.packSize = 0; /* Always zero in mem */ (**icon).iconPMap.hRes = kDefaultRes; /* 72 DPI default res */ (**icon).iconPMap.vRes = kDefaultRes; /* 72 DPI default res */ (**icon).iconPMap.pixelSize = depth; /* Set # bits/pixel */ /* Initialize fields specific to indexed and direct PixMaps */ if (depth <= 8) { /* PixMap is indexed */ (**icon).iconPMap.pixelType = 0; /* Indicates indexed */ (**icon).iconPMap.cmpCount = 1; /* Have 1 component */ (**icon).iconPMap.cmpSize = depth; /* Component size=depth */ (**icon).iconPMap.pmTable = newColors; /* Handle to CLUT */ } else { /* PixMap is direct */ (**icon).iconPMap.pixelType = RGBDirect; /* Indicates direct */ (**icon).iconPMap.cmpCount = 3; /* Have 3 components */ if (depth == 16) (**icon).iconPMap.cmpSize = 5; /* 5 bits/component */ else (**icon).iconPMap.cmpSize = 8; /* 8 bits/component */ (**newColors).ctSeed = 3 * (**icon).iconPMap.cmpSize; (**newColors).ctFlags = 0; (**newColors).ctSize = 0; (**icon).iconPMap.pmTable = newColors; } } else error = MemError(); } else newColors = nil; /* If no errors occurred, return a handle to the new off-screen PixMap */ if (error != noErr) { if (newColors != nil) DisposeCTable(newColors); } /* Return the error code */ return error;}CIconHandle wxMacCreateCIcon(GWorldPtr image , GWorldPtr mask , short dstDepth , short iconSize ){ GWorldPtr saveWorld; GDHandle saveHandle; GetGWorld(&saveWorld,&saveHandle); // save Graphics env state SetGWorld(image,nil); Rect frame = { 0 , 0 , iconSize , iconSize } ; Rect imageBounds = frame ; GetPortBounds( image , &imageBounds ) ; int bwSize = iconSize / 8 * iconSize ; CIconHandle icon = (CIconHandle) NewHandleClear( sizeof ( CIcon ) + 2 * bwSize) ; HLock((Handle)icon) ; SetupCIconHandlePixMap( icon , dstDepth , &frame,GetCTable(dstDepth)) ; HLock( (**icon).iconData ) ; (**icon).iconPMap.baseAddr = *(**icon).iconData ; LockPixels(GetGWorldPixMap(image)); CopyBits(GetPortBitMapForCopyBits(image), (BitMapPtr)&((**icon).iconPMap), &imageBounds, &imageBounds, srcCopy | ditherCopy, nil); UnlockPixels(GetGWorldPixMap(image)); HUnlock( (**icon).iconData ) ; (**icon).iconMask.rowBytes = iconSize / 8 ; (**icon).iconMask.bounds = frame ; (**icon).iconBMap.rowBytes = iconSize / 8 ; (**icon).iconBMap.bounds = frame ; (**icon).iconMask.baseAddr = (char*) &(**icon).iconMaskData ; (**icon).iconBMap.baseAddr = (char*) &(**icon).iconMaskData + bwSize ; if ( mask ) { Rect r ; GetPortBounds( image , &r ) ; LockPixels(GetGWorldPixMap(mask) ) ; CopyBits(GetPortBitMapForCopyBits(mask) , &(**icon).iconBMap , &r , &r, srcCopy , nil ) ; CopyBits(GetPortBitMapForCopyBits(mask) , &(**icon).iconMask , &r , &r, srcCopy , nil ) ; UnlockPixels(GetGWorldPixMap( mask ) ) ; } else { Rect r ; GetPortBounds( image , &r ) ; LockPixels(GetGWorldPixMap(image)); CopyBits(GetPortBitMapForCopyBits(image) , &(**icon).iconBMap , &r , &r, srcCopy , nil ) ; CopyBits(GetPortBitMapForCopyBits(image) , &(**icon).iconMask , &r , &r, srcCopy , nil ) ; UnlockPixels(GetGWorldPixMap(image)); } (**icon).iconMask.baseAddr = NULL ; (**icon).iconBMap.baseAddr = NULL ; (**icon).iconPMap.baseAddr = NULL ; HUnlock((Handle)icon) ; SetGWorld(saveWorld,saveHandle); return icon;}PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask){ CGrafPtr origPort ; GDHandle origDev ; PicHandle pict; RGBColor white = { 0xffff ,0xffff , 0xffff } ; RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; GetGWorld( &origPort , &origDev ) ; RgnHandle clipRgn = NULL ; if ( mask ) { clipRgn = NewRgn() ; LockPixels( GetGWorldPixMap( mask ) ) ; BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ; UnlockPixels( GetGWorldPixMap( mask ) ) ; } SetGWorld( wp , NULL ) ; Rect portRect ; if ( clipRgn ) GetRegionBounds( clipRgn , &portRect ) ; else GetPortBounds( wp , &portRect ) ; pict = OpenPicture(&portRect); if(pict) { RGBForeColor( &black ) ; RGBBackColor( &white ) ; if ( clipRgn ) SetClip( clipRgn ) ; LockPixels( GetGWorldPixMap( wp ) ) ; CopyBits(GetPortBitMapForCopyBits(wp), GetPortBitMapForCopyBits(wp), &portRect, &portRect, srcCopy,clipRgn); UnlockPixels( GetGWorldPixMap( wp ) ) ; ClosePicture(); } SetGWorld( origPort , origDev ) ; if ( clipRgn ) DisposeRgn( clipRgn ) ; return pict;}void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ){ memset( info , 0 , sizeof(ControlButtonContentInfo) ) ; if ( bitmap.Ok() ) { wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ; if ( bmap == NULL ) return ; if ( bmap->m_bitmapType == kMacBitmapTypePict ) { info->contentType = kControlContentPictHandle ; info->u.picture = MAC_WXHMETAFILE(bmap->m_hPict) ; } else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld ) { if ( (forceType == kControlContentCIconHandle || ( bmap->m_width == bmap->m_height && forceType != kControlContentPictHandle ) ) && ((bmap->m_width & 0x3) == 0) ) { info->contentType = kControlContentCIconHandle ; if ( bitmap.GetMask() ) { info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap()) , 8 , bmap->m_width ) ; } else { info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL , 8 , bmap->m_width ) ; } } else { info->contentType = kControlContentPictHandle ; if ( bitmap.GetMask() ) { info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap() ) ) ; } else { info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ) ; } } } else if ( bmap->m_bitmapType == kMacBitmapTypeIcon ) { info->contentType = kControlContentCIconHandle ; info->u.cIconHandle = MAC_WXHICON(bmap->m_hIcon) ; } }}wxBitmapRefData::wxBitmapRefData() : m_width(0) , m_height(0) , m_depth(0) , m_ok(false) , m_numColors(0) , m_quality(0){ m_bitmapMask = NULL; m_hBitmap = NULL ; m_hPict = NULL ; m_hIcon = NULL ; m_bitmapType = kMacBitmapTypeUnknownType ; m_hasAlpha = false;}// TODO move this to a public function of Bitmap Refstatic void DisposeBitmapRefData(wxBitmapRefData *data){ if ( !data ) return ; switch (data->m_bitmapType) { case kMacBitmapTypePict : { if ( data->m_hPict ) { KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ; data->m_hPict = NULL ; } } break ; case kMacBitmapTypeGrafWorld : { if ( data->m_hBitmap ) { wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ; data->m_hBitmap = NULL ; } } break ; case kMacBitmapTypeIcon : if ( data->m_hIcon ) { DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ; data->m_hIcon = NULL ; } default : // unkown type ? break ; } if (data->m_bitmapMask) { delete data->m_bitmapMask; data->m_bitmapMask = NULL; }}wxBitmapRefData::~wxBitmapRefData(){ DisposeBitmapRefData( this ) ;}bool wxBitmap::CopyFromIcon(const wxIcon& icon){ Ref(icon) ; return true;}wxBitmap::wxBitmap(){ m_refData = NULL;}wxBitmap::~wxBitmap(){}wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits){ m_refData = new wxBitmapRefData; M_BITMAPDATA->m_width = the_width ; M_BITMAPDATA->m_height = the_height ; M_BITMAPDATA->m_depth = no_bits ; M_BITMAPDATA->m_numColors = 0; if ( no_bits == 1 ) { M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( the_width , the_height , no_bits ) ; M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ; CGrafPtr origPort ; GDHandle origDevice ; GetGWorld( &origPort , &origDevice ) ; SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ; // bits is a char array unsigned char* linestart = (unsigned char*) bits ; int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; if ( the_width % (sizeof(unsigned char) * 8) ) { linesize += sizeof(unsigned char); } RGBColor colors[2] = { { 0xFFFF , 0xFFFF , 0xFFFF } , { 0, 0 , 0 } } ; for ( int y = 0 ; y < the_height ; ++y , linestart += linesize ) { for ( int x = 0 ; x < the_width ; ++x ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -