Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / February 2006



Tip: Looking for answers? Try searching our database.

PNG Resource with Alpha Channel?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
steve@walkereffects.com - 12 Oct 2005 18:56 GMT
How can I load an image resource with an alpha? I've been looking at
CGImageCreateWithPNGDataProvider and CGImageCreateWithJPEGDataProvider,
however neither appear to support alpha channels. I tried saving PNG
with transparency from Photoshop, but it crashed my app upon trying to
load it.

In the past I used QuickDraw and it would load PICT resources with
alpha just fine. I'm open to using other formats such as TIFF or PICT,
but I can't find any reference for it.

Thanks,

Steven Walker
Walker Effects
www.walkereffects.com
toby - 12 Oct 2005 19:17 GMT
> How can I load an image resource with an alpha? I've been looking at
> CGImageCreateWithPNGDataProvider and CGImageCreateWithJPEGDataProvider,
> however neither appear to support alpha channels. I tried saving PNG
> with transparency from Photoshop, but it crashed my app upon trying to
> load it.

Have you tried libpng? http://www.libpng.org/pub/png/libpng.html

> In the past I used QuickDraw and it would load PICT resources with
> alpha just fine. I'm open to using other formats such as TIFF or PICT,
[quoted text clipped - 5 lines]
> Walker Effects
> www.walkereffects.com
steve@walkereffects.com - 12 Oct 2005 22:31 GMT
I have not tried libpng... but I'd really like to stay within the
Carbon/Quartz API. Certainly other programmers must be using alpha
channels with image resources.
steve@walkereffects.com - 12 Oct 2005 22:41 GMT
Ok, I figured it out! The PNG format -does- support alpha channels,
it's just that I was relying on Photoshop to save it correctly. I just
download the free SuperPNG format plugin from fnordware and it works
perfectly!

If anyone else runs into this issue, they can download SuperPNG from:
http://fnordware.com/superpng

Walker
Alex Curylo - 15 Oct 2005 22:00 GMT
in article 1129153316.094950.225310@g49g2000cwa.googlegroups.com,
steve@walkereffects.com at steve@walkereffects.com wrote on 10/12/05 2:41
PM:

> Ok, I figured it out! The PNG format -does- support alpha channels,
> it's just that I was relying on Photoshop to save it correctly. I just
[quoted text clipped - 3 lines]
> If anyone else runs into this issue, they can download SuperPNG from:
> http://fnordware.com/superpng

And for those who don't see the point in paying insane amounts of money for
Photoshop, they can just use the free-in-your-Applications-folder
GraphicConverter, which does a marvelous and convenient job of all your
alpha channel handling needs. I actually IB'd up some hotkeys for Add Alpha
and Show Alpha commands since I use it so much to convert the .bmp image and
mask files the Windows guys give me.
jacobd - 24 Feb 2006 14:39 GMT
typedef struct {
size_t width;
size_t height;
size_t bitsPerComponent;
size_t bitsPerPixel;
size_t bytesPerRow;
size_t size;
CGImageAlphaInfo ai;
CGColorSpaceRef cs;
unsigned char *data;
CMProfileRef prof;
} BitmapInfo;

bool readBitmapInfo(GraphicsImportComponent gi, BitmapInfo *bi)
{
ComponentResult result;
ImageDescriptionHandle imageDescH = NULL;
ImageDescription *desc;
Handle profile = NULL;

result = GraphicsImportGetImageDescription(gi, &imageDescH);
if( noErr != result || imageDescH == NULL ) {
return false;
}

desc = *imageDescH;

bi->width = desc->width;
bi->height = desc->height;
bi->bitsPerComponent = 8;
bi->bitsPerPixel = 32;
bi->bytesPerRow = (bi->bitsPerPixel * bi->width + 7)/8;
bi->ai = (desc->depth == 32) ? kCGImageAlphaFirst :
kCGImageAlphaNoneSkipFirst;
bi->size = bi->bytesPerRow * bi->height;
bi->data = (unsigned char*) malloc(bi->size);

bi->cs = NULL;
bi->prof = NULL;

if( imageDescH != NULL)
DisposeHandle((Handle)imageDescH);
return true;
}

bool getBitmapData(GraphicsImportComponent gi, BitmapInfo *bi)
{

GWorldPtr gWorld;
QDErr err = noErr;
Rect boundsRect = { 0, 0, bi->height, bi->width };
ComponentResult result;

if( bi->data == NULL ) {
return false;
}

err = NewGWorldFromPtr( (GWorldPtr*)&gWorld, (unsigned
long)k32ARGBPixelFormat, (const Rect*)&boundsRect,
(CTabHandle)NULL, (GDHandle)NULL, 0,
(Ptr)(bi->data), bi->bytesPerRow );

if (noErr != err) {
return false;
}

if( (result = GraphicsImportSetGWorld(gi, gWorld, NULL)) != noErr ) {
DisposeGWorld(gWorld);
return false;
}

if( (result = GraphicsImportDraw(gi)) != noErr ) {
DisposeGWorld(gWorld);
return false;
}

DisposeGWorld(gWorld);
return true;
}

//caller should call free( outBitMapInfo->data ) after use
outBitMapInfo
bool GetBitmapFromPNGFile(FSSpec* lpszFilename,BitmapInfo
*outBitMapInfo)
{
OSStatus theErr = noErr;
bool bRet = false;
if ( !lpszFilename || !outBitMapInfo )
return false;
FInfo theFinfo;
if ( ::FSpGetFInfo(lpszFilename,&theFinfo) != noErr )
{
//file not exist
return false;
}
GraphicsImportComponent gi;
theErr = GetGraphicsImporterForFile(lpszFilename,&gi);
if ( theErr != noErr )
return false;

if (!readBitmapInfo(gi,outBitMapInfo))
{
CloseComponent(gi);
//get bitmaoInfo err
return false;
}
bRet = getBitmapData(gi,outBitMapInfo);
CloseComponent(gi);
return bRet;
}

NOTES:outBitMapInfo->data is a pixmap, bitsPerPixel is 32, first
byte is alpha, if it is 0 means this
> pix is transparence.(value from 0 to 255)
> you

* posted via http://www.mymac.ws
* please report abuse to http://xinbox.com/mymac
jacobd - 24 Feb 2006 14:39 GMT
other image format can get pixmap too, such as tiff,jepg etc.
//caller should call free( outBitMapInfo->data ) after use
outBitMapInfo
bool GetBitmapFromTIFFFile(FSSpec* lpszFilename,BitmapInfo
*outBitMapInfo)
{
OSStatus theErr = noErr;
bool bRet = false;
if ( !lpszFilename || !outBitMapInfo )
return false;
FInfo theFinfo;
if ( ::FSpGetFInfo(lpszFilename,&theFinfo) != noErr )
{
//file not exist
return false;
}
GraphicsImportComponent gi;
theErr = GetGraphicsImporterForFile(lpszFilename,&gi);
if ( theErr != noErr )
return false;

if (!readBitmapInfo(gi,outBitMapInfo))
{
CloseComponent(gi);
//get bitmaoInfo err
return false;
}
bRet = getBitmapData(gi,outBitMapInfo);
CloseComponent(gi);
return bRet;
> } jacobd,  about png

* posted via http://www.mymac.ws
* please report abuse to http://xinbox.com/mymac
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.