Reading IDAT chunk from png file - libpng

So I want to extract data from png file in which those params are always the same:
Bit depth: 8
Color type: 6
Compression method: 0
Filter method: 0
Interlace method: 0
What I want is array of all pixels as rgba. I already have IDAT chunk extracted but I really don't know what I should do next.
According to libpng I have to reverse creating image data process.
As I understand, I have to decompress chunk content, reverse the filtering process and I should get truecolor pixels representation but I really don't know how to decompress it and reverse the filtering process.

Related

Does compressing a PNG file affect idat header only?

I want to parse PNG file into chunks.
I know that if the file compressed the IDAT chunk will be affected, will it affect other chunks (Ancillary Chunks) ?
Read about the compression here and seems that other chunks will not be affected.

How can I load a jpeg image from a buffer?

I want to store a jpeg image in a RC_DATA resource, but not a single image in one single RC_DATA. There are many things in that RC_DATA, all muxed together. At runtime I load that RC_DATA in a bufer and extract all the object, including this Jpeg. Now I have this image in a memory buffer and I need to load it in a TJpegImage or TBitmap. How can I do that ? I saw that those classes doesn't have some methods to achieve this...
Copy the JPEG bytes from your buffer into a TMemoryStream (or, use a TCustomMemoryStream to point directly at the JPEG bytes to avoid making a copy). And then you can pass that stream to TJPEGImage.LoadFromStream().

Read the PNG image as NSData, and some extra bytes appear

Above is the data to read the picture using iOS SDK,
00000004 43674249 50002006 2cb87766
are extra bytes.
Does anyone know what these bytes are?
Your image is not a standard PNG according to the official specifications. The bytes you point out form a valid PNG chunk:
00000004 Data length
43674249 Block ID "CgBI"
50002006 4 bytes of proprietary information
2CB87766 CRC checksum of the data block
but the location and the indicated type of the chunk are invalid for a conforming PNG image.
This indicates the image is processed with Apple's modified version of pngcrush. See http://iphonedevwiki.net/index.php/CgBI_file_format#Differences_from_PNG for non-official information on the block contents and other deviations from the official specification.

Is it possible to generate a favicon.ico file under 5,430 bytes?

Considering Google can't even do it, I'm assuming the answer is "No"?
I just went through the basic suggestions from audreyr's "Favicon Cheat Sheet" and created a favicon.ico file consisting of two optimized png files using ImageMagick like so:
$convert favicon-16.png favicon-32.png favicon.ico
My favicon-16.png file was 137 bytes after optimizing with optipng and my favicon-32.png file was 144 bytes after optimization.
So you can understand my surprise when the combined favicon.ico file created by ImageMagick ended up being 5,430 bytes. Coincidentally, that's the exact same size as Google's official favicon.ico file.
Is 5,430 bytes the absolute minimum size for any true image/x-icon file?
That seems a little excessive when realistically every single browser accessing my favicon.ico file will be extracting the 144 byte 32x32 png version.
If the source favicon-16.png and favicon-32.png are truecolor (RGB888 or RGBA8888), ImageMagick will write an uncompressed 5430-byte ICO file. However, if they are indexed-color (i.e., in PNG8 format) or grayscale the ICO may be smaller (I observe 3638-byte ICO files in these cases).
The images are stored within the ICO in BMP format, not PNG (only 256x256 images get stored in PNG format inside the ICO).

Find out if PNG is 8 or 24 bits and alpha

Given a UIImage/NSImage or NSData instance, how do you find out programatically if a PNG is 8 bits or 24 bits? And if it has an alpha channel?
Is there any Cocoa/Cocoa Touch API that helps with this?
To avoid duplication, here is the non-programatic answer to the question, and here's a way to find out if an image is a PNG.
As a person who programmed for a long time on a J2ME platform, I know the PNG format very well. If you have the raw data as a NSData instance, looking up the information is very easy since the PNG format is very straightforward.
See PNG Specification
The PNG file starts with a signature and then it contains a sequence of chunks. You are interested only in the first chunk, IHDR.
So, the code should be along the lines of:
Skip the first 8 bytes (signature)
Skip 4 bytes (IHDR chunk length)
Skip 4 bytes (IHDR chunk type = "IHDR")
Read IHDR
Width: 4 bytes
Height: 4 bytes
Bit depth: 1 byte
Color type: 1 byte
Compression method: 1 byte
Filter method: 1 byte
Interlace method: 1 byte
For alpha, you should also check if there is a tRNS (transparency) chunk in the file. To find a chunk, you can go with the following algorithm:
Read chunk length (4 bytes)
Read chunk type (4 bytes)
Check chunk type whether it is the type we are looking for
Skip chunk length bytes
Skip 4 bytes of CRC
Repeat
EDIT:
To find info about a UIImage instance, get its CGImage and use one of the CGImageGet... functions.
It should be noted that All integer values in a PNG file are read in Big-endian format.

Resources