Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Loop through pixels with libpng

Name: Anonymous 2011-09-10 14:02

Hi /prog/,

I need to write a little tool that gives a list (x,y) of pixels in a png file that are not white. I did this in java, but because of the high footprint, I'd like to redo it in C/C++. I have a little experience with this languages, but none with libpng (I assumed this was the way to go).

So what I basically want to do is loop through each pixel in a PNG file and check the color. What would be the best way to do that with libpng?

Thanks in advance

Name: Anonymous 2011-09-10 14:38

>>1
Use libpng to decompress the PNG image into a 32-bit RGBA buffer with a given stride, and then iterate through every pixel such that you compute the x and y coordinate at every iteration:


struct coord2
{
    int x;
    int y;
    coord2() { }
    coord2(int a, int b) : x(a), y(b) { }
};

std::vector<coord2> results;
uint32_t* buffer = ...; // initialize with data from libpng

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
        uint32_t pixel = buffer[(y * stride) + x];
        if (is_match(pixel)) {
            results.push_back(coord2(x, y));
        }
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List