How can I create PNG Files from an array of bytes or read a PNG file to create an array of bytes?

I looked up the format but it looks nothing like a bitmap and I read the GDI doesn't support PNG's. So how does LibPNG do it then? I don't want to use LibPNG or anything. I want to do it myself. I tried changing BI_RGB to BI_PNG but that just corrupts it all. There isn't a single tutorial out there on reading and using the PNG format with C++. Any ideas or links on how to read it? I read the entire format I just don't know where to start.

For bitmaps I use:

    Pixels.clear();
    memset(&Info, 0, sizeof(BITMAPINFO));
    Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    Info.bmiHeader.biPlanes = 1;
    Info.bmiHeader.biBitCount = BitsPerPixel;
    Info.bmiHeader.biCompression = BI_RGB;
    Info.bmiHeader.biWidth = width;
    Info.bmiHeader.biHeight = height;
    Info.bmiHeader.biSizeImage = size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
    bFileHeader.bfType = 0x4D42;
    bFileHeader.bfSize = bFileHeader.bfOffBits + size;
    bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(Info.bmiHeader);
    Pixels.resize(width * height);
    //CreateFile... Reads pixels and flip rows. :)

Recommended Answers

All 3 Replies

I looked up the format but it looks nothing like a bitmap...

Why should it look like a bitmap? It's a PNG format.

... and I read the GDI doesn't support PNG's. So how does LibPNG do it then?

By not using GDI I suppose. Since it's called LibPNG, it was probably designed to deal with PNG files specifically.

I don't want to use LibPNG or anything. I want to do it myself.

Then you need to completely understand the PNG format. More study is necessary. Aren't there books that discuss the format? Since it's a free format, there are sites that discuss the format.

I tried changing BI_RGB to BI_PNG but that just corrupts it all.

Does that surprise you?

There isn't a single tutorial out there on reading and using the PNG format with C++.

You need to understand the format itself, irregardless of the language you want to use. The format is everything. You don't want to learn how to read with C++. C++ is just a tool. Once you understand the format, you can use any tool you want, including C++.

Reading images is not a simple task. They are quite complex in design. You'd be better off with a tool designed to deal with the format rather than designing your own.

If this is a one of exercise, then you can always use GDI+ which of course supports the .PNG image format.

Milton

The specification is hosted by libPNG here: http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html

Read the specification and figure out how to write a decoder.
If you've never worked with compression algorithms before like LZW and LZMA, it's probably a good idea to start now.

Actually, actually...
forget all that.
Start with a GIF decoder. Learn the ins and outs of image compression. THEN try tackling something as nuts as PNG.
http://www.w3.org/Graphics/GIF/spec-gif89a.txt

Edit: I've never actually written one, so if you're up for the challenge, I'll be happy to work with you on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.