Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to grab IconRef from a .tiff file.



On 21/08/2003 15:36, Mike Kluev wrote:

> On 21/08/2003 05:35, Laurence Harris wrote:
>
>> On 8/20/03 7:38 PM, Mike Kluev didst favor us with:
>>
>>> err = GraphicsImportSetGraphicsMode(component,
>>> graphicsModeStraightAlpha, NULL);
>>
>> What does this line do? In my tests, this has no effect on the resulting
>> icon. For me, SetIconFamilyData always creates a solid black mask no matter
>> what I do. Am I missing something?
>
> It is supposed to preserve alpha channel information. In fact it
> does, so the picture created with GraphicsImportGetAsPicture still
> contain alpha. Unfortunately it is of no use here, because when
> picture is converted to icon with SetIconFamilyData the icon is
> created without proper mask. As of now I do not know how to create
> an icon with proper mask from translucent picture.

I didn't know as of *then*, I do know as of *now* :)
Below is source fragment that converts translucent image file to
translucent iconRef. Now I add the mask information to iconFamily
as well as picture. The tricky part was to get this alpha mask
from image. The way I do it in GetThumbnailMaskHandle could be
called "reverse constructing". If anybody knows better way to do
it please let me know.

The full source is below. I omit error checking for simplicity.

--- Cut Here ---

Handle GetThumbnailMaskHandle(PicHandle pict);
IconRef FileToIconRef(FSRef *fsRef);

#warning "Check for errors!!!"
IconRef FileToIconRef(FSRef *fsRef)
{
GraphicsImportComponent component;
OSStatus err;
FSSpec fsSpec;
PicHandle pict;
IconFamilyHandle iconFamily;
IconRef iconRef;
const OSType kFakeCreator = '#%$^', kFakeType = '#$&$';
Handle h;

err = FSGetCatalogInfo(fsRef, kFSCatInfoNone, NULL,
NULL, &fsSpec, NULL);

err = GetGraphicsImporterForFile(&fsSpec, &component);
err = GraphicsImportSetGraphicsMode(component,
graphicsModeStraightAlpha, NULL);
err = GraphicsImportGetAsPicture(component, &pict);
CloseComponent(component);

iconFamily = (IconFamilyHandle)NewHandle(0);
err = SetIconFamilyData(iconFamily, 'PICT', (Handle)pict);

h = GetThumbnailMaskHandle(pict);
err = SetIconFamilyData(iconFamily, kThumbnail8BitMask, h);
DisposeHandle(h);

err = RegisterIconRefFromIconFamily(kFakeCreator, kFakeType,
iconFamily, &iconRef);
DisposeHandle((Handle)iconFamily);
err = AcquireIconRef(iconRef);
err = UnregisterIconRef(kFakeCreator, kFakeType);
return iconRef;
}

#warning "Check for errors!!!"
Handle GetThumbnailMaskHandle(PicHandle pict)
{
GWorldPtr offscreen, offscreen2;
CGrafPtr savedPort;
GDHandle savedDevice;
Handle h;
CTabHandle cTab;
OSErr err;
Rect r;

SetRect(&r, 0, 0, 128, 128);
cTab = GetCTable(8 + 32);

GetGWorld(&savedPort, &savedDevice);

err = NewGWorld(&offscreen, 8, &r, cTab, NULL, 0);
HLock((Handle)GetGWorldPixMap(offscreen));
SetGWorld(offscreen, NULL);
PaintRect(&r);
DrawPicture(pict, &r);

h = NewHandle(128*128);
HLock(h);
err = NewGWorldFromPtr(&offscreen2, 8, &r, cTab, NULL, 0,
*h, 128);
HLock((Handle)GetGWorldPixMap(offscreen2));
SetGWorld(offscreen2, NULL);
EraseRect(&r);
DrawPicture(pict, &r);

CopyBits(GetPortBitMapForCopyBits(offscreen),
GetPortBitMapForCopyBits(offscreen2), &r, &r, subOver, NULL);

SetGWorld(savedPort, savedDevice);

DisposeGWorld(offscreen);
DisposeGWorld(offscreen2);
DisposeCTable(cTab);

return h;
}

--- Cut Here ---

Mike
_______________________________________________
carbon-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/carbon-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: How to grab IconRef from a .tiff file. (From: Mike Kluev <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.