This is another point where no matter what I do, I feel as dumb as dirt...
Graphics on the Mac is certainly not my strong suit, and the client's app
is OS8.5.1, using MacApp. As always, any pointers greatly appreciated.
The challenge:
Convert a color PICT in a res file to a monochrome image, and add it back to
the res file as a new resouce.
The problem: CopyBits() is filing the destination with nulls.
The code:
PicHandle TPixel::MakeImageMono()
{
PicHandle thePict = nil;
long lDataLen = 0, lGestaltReturn = 0;
short nUniqueID = 0;
OSErr err = noErr;
PictInfo colorPictInfo, monoPictInfo;
CRect destRect;
CTabHandle theColorTable;
GDHandle hOldGD, hColorGD, hMonoGD;
CGrafPtr oldPort, colorPort, monoPort;
ResNumber nColorPhoto = 128, nMonoPhoto = 129;
GWorldPtr gwColor = nil;
GWorldPtr gwMono = nil;
PixMapHandle hColorMap;
PixMapHandle hMonoMap;
// MacApp File Handling
TFile *testResFile;
FSSpec spec;
CStr255 theName;
short myVRefNum = gApplication->fAppVRefNum;
long myAppDir = gApplication->fAppDir;
short pictRes = 0;
theName = ":Sales:testpict";
err = FSMakeFSSpec(myVRefNum,myAppDir,theName,&spec);
if (err == noErr)
{
testResFile = new TFile;
testResFile->IFile('rsrc','RSED',false,needResourceFork,false,true);
testResFile->Specify(spec);
testResFile->SetPermissions(fsRdWrPerm,fsRdWrPerm);
err = testResFile->OpenFile();
pictRes = testResFile->UseResource();
UseResFile(pictRes);
}
thePict = GetPicture(nColorPhoto);
GetPictInfo(thePict,&colorPictInfo,0,0,0,0);
destRect = (*thePict)->picFrame;
// Open a color port and fill it
if ( (err = NewGWorld(&gwColor,32,destRect,0,nil,0)) == noErr)
{
// Save the current port
GetGWorld(&oldPort, &hOldGD);
SetGWorld(gwColor, 0);
GetGWorld(&colorPort, &hColorGD);
EraseRect( destRect);
hColorMap = GetGWorldPixMap(gwColor);
LockPixels(hColorMap);
// Load the pict into the color grafport
DrawPicture(thePict,destRect);
}
// Load the grayscale colortable and create the mono grafport
theColorTable = GetCTable(40);
if ( (err = NewGWorld(&gwMono,8,destRect,theColorTable,nil,0)) == noErr)
{
hMonoMap = GetGWorldPixMap(gwMono);
GetGWorld(&monoPort, &hMonoGD);
EraseRect( destRect);
SetIfColor(gRGBBlack);
SetIfBkColor(gRGBWhite);
LockPixels(hMonoMap);
CopyBits(&(((GrafPtr)gwColor)->portBits),&(((GrafPtr)gwMono)->portBits),dest
Rect,
destRect,srcCopy,nil);
}
UnlockPixels(hColorMap);
//ClosePicture();
// Make sure our defaults are set
CStr255 theResName = "MonoPictName";
AddResource( (Handle) hMonoMap, 'PICT', nMonoPhoto, theResName);
WriteResource((Handle) hMonoMap);
UnlockPixels(hMonoMap);
err = testResFile->CloseFile();
err = testResFile->FlushVolume();
FreeIfObject(testResFile);
UseResFile(gApplication->fAppRes);
}