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 03:37, Mike Kluev wrote:

> Given that I do not know the direct way to get alpha channel
> information from image, ...

Then I didn't, now I know :) Thanks to Patrice Krysztofiak
from Quicktime list. It turns out to be as stupid simple as
grabbing the alpha component of offscreen. I was wrongly
assume it is always zero, because I was silly to always
use graphicsModeStraightAlpha mode in my tests. So, here
is the next version. Shorter, faster, more straightforward,
uses less resources. Hopefully the last, thank you for your
patience :)

===
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.

Error checking is omitted, do it yourself. Mainly this is due to
not obscure the logic (the first thing I do to understand some
external piece of code is removing error checking). Then,
different people have different ideas how to do 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
Special thanks to: Patrice Krysztofiak

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 offscreen;
OSErr err;
Rect r;
Handle dataH, maskH;
IconFamilyHandle iconFamily;
long i;
UInt32 *src;
UInt8 *dst;

iconFamily = (IconFamilyHandle)NewHandle(0);

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

dataH = NewHandleClear(128*128*4L);
HLock(dataH);
maskH = NewHandle(128*128);
err = NewGWorldFromPtr(&offscreen, 32, &r, NULL, NULL, 0,
*dataH, 128*4);
err = GraphicsImportSetGWorld(component, offscreen, NULL);
err = GraphicsImportDraw(component);

src = (UInt32 *)*dataH;
dst = (UInt8 *)*maskH;
for (i = 0; i < 128*128; i++) dst[i] = src[i] >> 24;

err = SetIconFamilyData(iconFamily, kThumbnail32BitData, dataH);
err = SetIconFamilyData(iconFamily, kThumbnail8BitMask, maskH);

DisposeHandle(dataH);
DisposeHandle(maskH);
DisposeGWorld(offscreen);

return iconFamily;
}

--- 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.