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 23/08/2003 02:22, Matt Gough wrote:

> Please post it. It would be nice for future searchers to to get the
> 'definitive' version of this.

Dear future searcher,

Here it is, the sample that converts graphics file (supported
by QuickTime, e.g. TIFF) to 128*128 thumbnail IconRef respecting
alpha channel. Thanks to Larry it doesn't leak GWorld anymore,
nor does it suffer from the double blending and banding problem
(banding was caused by using 8-bit offscreens which in turn was
caused by premature optimization on my part - I wished to use
one less offscreen). It works faster than previous version
because it no longer uses intermediate picture handles.

Given that I do not know the direct way to get alpha channel
information from image, internally it "reverse constructing" the
alpha mask of image by drawing two versions of image, one over
white background and another over black background and subtracting
those to get the mask. It should be 100% correct, but it is not
the "direct" and probably not the fastest way possible. If you
know better way to get alpha channel information please let me know.

Error checking is omitted, do it yourself. Mainly this is due to
not obscure the logic. Then, different people have different ideas
how to do error checking (the first thing I do to understand some
external piece of code is removing error checking).

Let me know if it works (and especially if it doesn't work) for you.
If it doesn't work for you on some image e-mail me that image.

Author: Mike Kluev
Contributor and initial testing: Laurence Harris

Keywords: IconRef, TIFF, alpha channel, translucency, transparency,
transparent, translucent, graphicsModeStraightAlpha.

--- Cut Here ---

IconFamilyHandle CreateIconFromComponent(GraphicsImportComponent component);
IconRef FileToIconRef(FSRef *fsRef);

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

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

err = GetGraphicsImporterForFile(&fsSpec, &component);
iconFamily = CreateIconFromComponent(component);
CloseComponent(component);

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

#warning "Check for errors!!!"
IconFamilyHandle CreateIconFromComponent(GraphicsImportComponent component)
{
GWorldPtr blackOffscreen, whiteOffscreen, grayOffscreen;
CGrafPtr savedPort;
GDHandle savedDevice;
CTabHandle cTab;
OSErr err;
Rect r;
Handle h;
IconFamilyHandle iconFamily;

iconFamily = (IconFamilyHandle)NewHandle(0);

SetRect(&r, 0, 0, 128, 128);
err = GraphicsImportSetBoundsRect(component, &r);

GetGWorld(&savedPort, &savedDevice);

h = NewHandleClear(128*128*4);
HLock(h);
err = NewGWorldFromPtr(&blackOffscreen, 32, &r, NULL, NULL, 0,
*h, 128*4);
HLock((Handle)GetGWorldPixMap(blackOffscreen));
SetGWorld(blackOffscreen, NULL);
err = GraphicsImportSetGWorld(component, blackOffscreen, NULL);
EraseRect(&r);
err = GraphicsImportDraw(component);
err = SetIconFamilyData(iconFamily, kThumbnail32BitData, h);
PaintRect(&r);
err = GraphicsImportSetGraphicsMode(component,
graphicsModeStraightAlpha, NULL);
err = GraphicsImportDraw(component);

err = NewGWorld(&whiteOffscreen, 32, &r, NULL, NULL, 0);
HLock((Handle)GetGWorldPixMap(whiteOffscreen));
SetGWorld(whiteOffscreen, NULL);
err = GraphicsImportSetGWorld(component, whiteOffscreen, NULL);
EraseRect(&r);
err = GraphicsImportDraw(component);

CopyBits(GetPortBitMapForCopyBits(blackOffscreen),
GetPortBitMapForCopyBits(whiteOffscreen), &r, &r, subOver, NULL);

DisposeHandle(h);
h = NewHandle(128*128);
HLock(h);
cTab = GetCTable(8 + 32);
err = NewGWorldFromPtr(&grayOffscreen, 8, &r, cTab, NULL, 0,
*h, 128);
DisposeCTable(cTab);
HLock((Handle)GetGWorldPixMap(grayOffscreen));
SetGWorld(grayOffscreen, NULL);
CopyBits(GetPortBitMapForCopyBits(whiteOffscreen),
GetPortBitMapForCopyBits(grayOffscreen), &r, &r, srcCopy, NULL);

err = SetIconFamilyData(iconFamily, kThumbnail8BitMask, h);

SetGWorld(savedPort, savedDevice);

DisposeHandle(h);
DisposeGWorld(blackOffscreen);
DisposeGWorld(whiteOffscreen);
DisposeGWorld(grayOffscreen);

return iconFamily;
}

--- Cut Here ---
_______________________________________________
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: Matt Gough <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.