user3367378
user3367378

Reputation:

Extract pixel value with libpng

I have a square (1024x1024) 8-bit grayscale image that I'm trying to use as a "landscape" (it's a torrential height model) and all I'm trying to do is just get something like

byte = pngimg.at(x, y)

I've been scouring libpng's pages and googling but nothing is showing up. In Java it's as simple as a buffered image. Where is the C++ equivalent for a png?

Upvotes: 1

Views: 2626

Answers (1)

Tadeusz Puźniakowski
Tadeusz Puźniakowski

Reputation: 56

C++ does not support image types in STL, so you have to depend on some external library to read pixels. I used to do it using OpenCV library, and it's function imread. Recently I created a very simple library in C++11 for loading images in PNG, and it depends only on libPNG (and STL). The code that loads pixel data from PNG is at github repo. It takes binary data from vector and creates buffer with vector of pixels.

One note - if you are using libPNG, you have to remember about releasing libPNG resources with png_destroy_read_struct . I had some problems with memory leaks when I forgot about it.

Upvotes: 1

Related Questions