0

I am trying to load the pixel rgb/ga information of a png image into a matrix, using the library png++, to do some computations with them.

My Code (which does not work at the moment):

#include <iostream>
#include <png++/image.hpp>
#include <png++/rgb_pixel.hpp>

int main(int argc, const char * argv[]) {

const std::string path="img_03.png";
png::image< png::basic_rgb_pixel <unsigned char> > pic(path);
pixel=pic.get_pixel(0, 0);
pixelp = &pixel;


    std::cout << "value=" << pic[10][10].red << std::endl; //output: '?'
    std::cout << "value=" << pixel.red << std::endl; //nothing
    std::cout << "pointer=" << pixelp << std::endl; //delivers adress

pic.read(path);

    std::cout << "value=" << pic[10][10].red << std::endl; //nothing
    pic.write("picOutput.png");  //same picture
return 0;
}

However, none of those methods work to get the rgb values of each pixel. If there is another way to get rgb/ga information of each pixel, please mention it.

The line pic.write("picOutput.png"); delivers the same png i loaded in the line pic.read(path). This is a personal exercise for me to get more used to C++, criticise my code as much as you can.

Thanks!

Tuni
  • 129
  • 1
  • 9
  • 1
    Before people start hopping up and down and shouting "Read the manual" please note that there isn't much of a manual. – user4581301 Nov 20 '17 at 19:41
  • which is kind of my problem. I only have Stackexchange for a source – Tuni Nov 20 '17 at 19:42
  • 1
    Fortunately the writer of png++ is a semi-regular on SO. I don't know if this will work, but lets see if we can flag him: @Alex – user4581301 Nov 20 '17 at 19:44
  • Turns out the link to the documentation isn't working at all right now. Here's something a bit deeper that seems to get around the dead link: http://www.nongnu.org/pngpp/doc/0.2.1/annotated.html and one directly to the `image` class: http://www.nongnu.org/pngpp/doc/0.2.1/classpng_1_1image.html – user4581301 Nov 20 '17 at 19:47
  • 1
    You can use **ImageMagick** to convert a `PNG` to a `NetPBM` `PPM` file in either ASCII or binary and that is dead simple to read. `convert input.png result.ppm` or `convert input.png -compress none result.ppm` (textual ASCII). Likewise `pngtopam` from the **NetPBM** suite. https://en.m.wikipedia.org/wiki/Netpbm_format - see **PPM Example**. – Mark Setchell Nov 20 '17 at 20:00

1 Answers1

0

Here comes the solution:

change line:

std::cout << "value=" << pic[10][10].red << std::endl; //nothing

with:

std::cout << "value=" << (int) pic[10][10].red << std::endl; //nothing

because std::cout can't output types of unsigned char. Thanks to Alex!

For in-depth explanation, look here: cout not printing unsigned char

Tuni
  • 129
  • 1
  • 9