PNG IDAT chunk data calculation - c

I'm trying for learning propose to write my own png file in c language.
I have already read the PNG file format specification and i am now able to write a basic PNG(signature, IHDR CHUNK, 1px-IDAT CHUNK AND IEND CHUNK).
But now i want to write a array of pixels into the PNG, all chunks are going okk but I don't understand how to generate the IDAT data CHUNK.
In libPNG documentation they explain to deflate() the scanline, preced by the filer byte method.
My understanding of this problem:
A scanline is an array of horizontal pixels preceded by the filter method(0) then of size: (3*screenWidth + 1)
for each line (screenHeight value), I compress() the scanline with zlib and store it in a file.
But After generating the png, it is always black and the value is always 0 in hex editor.
what am i doing wrong? or is the the good way to generate IDAT data values?

You did not put any code in your question, so we have no way of knowing what you're actually doing. "I compress() the scanline with zlib and store it in a file." sounds wrong. You need to assemble all of the scan lines as described, and then compress the entire thing with zlib, once.

Related

Zlib decompress bytes with unknown compressed length in C

I am trying to write my own png reader without any external libraries. I need to use Zlib to decompress the png's IDAT chunk. I have managed to do it in python using zlib.decompress(), and I am trying to replicate it in C. I was reading over zlib's docs and found uncompress(), however it requires a destination length which I would not know.
I could set a destination to be much larger than possible for the png, but this seems like a cop-out and would break my program If I had a really big picture. However, i have found a function inflate() which can be used multiple times. If I could do this, i could realloc() memory if needed with each call. Yet I don't understand the docs for it very well and have not found much examples for this type of thing. Could anyone provide some code or help point me in the right direction?
You do know the destination length. Exactly. The PNG header information tells you how many rows, how many columns, and how many bytes per pixel. Multiply it all out, add a byte per row for the filtering, and you have your answer.
Allocate that amount of memory, and decompress into that.
Note that there can be multiple IDAT chunks, but combined they contain a single zlib stream.

How to discover an image that was converted from one format to anothter?

Is it possible to trace back an image say a png image to a jpg? For example an image x.jpg that was converted to x.png. Is there a way of telling that x.png is essentially x.jpg, with the difference being formats?
If l convert img.jpg to img.png is it posssible for me to get back img.jpg?
I intend to check this in C.
As to your first question, the meta-information stored in PNG can tell what the original format or file was. But there is no requirement to store this meta-information in the file.
As to your second question: PNG is a lossless format. So if you decompress a Jpeg image into a bitmap and then encode that bitmap as PNG, you can at least get back from th PNG to the bitmap of the jpeg.
Getting back to the jpeg essentially means re-encoding (compressing) the bitmap, but to arrive at the bitwise identical Jpeg file means using the same compressor settings that were used to create the original Jpeg. As you probably don't know those settings (and it may depend on the compressor code too), I would say "No, you can't get back to the original Jpeg."

How can I read image files?

I need to get the RGB value reading image. How can i do it in C?
The image format can be png,jpg,bmp or other usual format.
It has to be saved in a text file.
A very easy-to-use image library that can cover the reading and writing of all these formats would be FreeImage. It is primarily a C library, but there are also wrappers for C++, etc.
When you say "saved in a text file", that is pretty atypical for images due to the fact that binary formats are much more compact that storing raw string values for the pixel intensities. Additionally, many formats use compression, which would mean there isn't really a given "value" per-pixel ... instead the data must be decompressed before you can individually assign a value to every pixel. There are some image formats such as PPM that can be stored as ASCII data, but again, that's not necessarily the most efficient way to store a large image.
So for your workflow, you would use a library like FreeImage to read the values out of the image file, and then write back the uncompressed pixel values to a PPM file, or a custom-formatted text file.

How to convert an image to WORD (uint16) array?

I have some images (.bmp, .png, .jpg) in my directory. I want to convert this image to WORD array in order to display this image in LCD in 565 formats (16 bit pixel). How to convert an image to WORD array? Please help. Is is there any utility to just convert the image to binary? or Please provide some code in Windows C/CPP to convert it to binary?
This will depend a whole lot on the exact format of the input image.
Just converting "to words" isn't really expressing what you want to do, which is probably more like "convert bitmap images to an array of RGB565 pixels in row-major format".
You should look at image-processing libraries that allow you to load bitmap images, and read out the value of each pixel.
You can probably just convert directly to RGB565, shouldn't be too hard from any other bitmap format.
Note that there are both indexed and "true color" bitmap formats, and you sound like you need to handle both. If you'er lucky, the library for each format will abstract this away and have e.g. auint32 read_pixel_as_rgb888() function.
Also note that many bitmap image formats focus a lot on compression, which is why just reading in the bits of the file is not nearly enough, you need to de-compress the data according to the format. This is quite complex, which is why pre-written libraries are the only sane choice.
For PNG, look at libpng, for JPEG look for libjpeg. On second thought, these libraries might be a bit too low-level, and maybe you should look at something like SDL_Image instead.
You could try to use CImg to open image files - http://cimg.sourceforge.net/

how to take a JPEG out of IplImage* and not saving it to hard drive put saved dat into char*?

So what I need is simple: I have IplImage* I want to encode it into JPEG and wrap it with some additional JPEG data if needed (JPEG files contain noty only encoded pixels) and put tha file (not saved onto hard drive) into char* buffer. How to do such thing?
JPEG is a complex format. You can use the IJG jpeg library as a base to work with. However, be warned, it is a mess in itself and has a slight learning curve. It is open source, and you'll typically need to configure its build according to your compiler using a provided makefile (which may not be found with the library code itself)

Resources