ort11
ort11

Reputation: 3349

How to tell if PNG file has an Alpha Channel / Transparency

Trying to find the best / easiest way using Objective C on MAC OSX if a PNG file has an Alpha Channel.

Upvotes: 0

Views: 1854

Answers (2)

Doug Lowder
Doug Lowder

Reputation: 26

Here's one way:

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithContentsOfFile:@"/path/to/image/test.png"];
BOOL alpha = [rep hasAlpha];

Upvotes: 1

Glenn Randers-Pehrson
Glenn Randers-Pehrson

Reputation: 12465

If the color-type byte, which is at a known location within the IHDR chunk, is 0, 2, or 3 AND there is no tRNS chunk present before the first IDAT chunk, there is no alpha channel and the image is opaque. If a tRNS chunk is present, or if the color-type is 4 or 6, the image has an alpha channel, which might have transparency in some pixels or it might be opaque.

Upvotes: 0

Related Questions