Read RGB values from PNG in C++

13,247

You can't easily open a PNG file without libraries.

Here is sample of opening a PNG WITH a library... and it's already a lot of code http://zarb.org/~gc/html/libpng.html

The thing is that PNG is compressed format. There's a lot of "mathemagics" happening to compress the RGB values into the special format.

A .BMP (Windows bitmap for example) is a very easy way for you to start parsing manually, if you are doing this for training, but other formats you need a library.

Of couse, you CAN do everything from scratch... but will take a while and by the question you did, you are a little far from it.

Share:
13,247
hiquetj
Author by

hiquetj

College student just trying to learn the ropes.. Computer Engineering major but have done a lot of web development

Updated on June 04, 2022

Comments

  • hiquetj
    hiquetj about 2 years

    Basically, what I am trying to is learn how to open an image manually WITHOUT LIBRARIES and see the RGB contents.

    I want to do this so I can calculate the Hue and Saturation of the RGB to write back a 2D array image.

    So far I have got this:

    #include <iostream>
    #include <stdio.h>      /* printf */
    #include <math.h>       /* acos */
    #include <fstream>      /* for file I/O */
    
    using namespace std;
    
    int main() {
            FILE* fp = NULL;
            fp = fopen("soccerball.png", "rb");
            cout << "done" << endl;
            return 0;
    }
    

    I know it is not much, as I'm used to Python more. But I honestly don't know where to start. And there are literally no examples of this it seems on the internet.

    So I read an image. Now I want something like:

    img[Height][width]

    so I can get

    img_r[h][w], img_g[h][w], img_b[h][w]

    So I can use those values to calculate a Hue 2d array in same format.

    img_hue[h][w]

    Thanks