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:03

Read SIPF

Name: Anonymous 2011-09-10 14:05

>>2
I'm sorry, I don't come here much, what is SIPF? Do you mean SICP?

Name: Anonymous 2011-09-10 14:17

a list (x,y) of pixels in a png file that are not white.
You'll get no help from us on this blatant apartheid policy.
Take your racism elsewhere.

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));
        }
    }
}

Name: Anonymous 2011-09-10 14:45

>>4
I am so sorry. I'd like to loop through all pixels and treat them equally.

Name: Anonymous 2011-09-10 14:45

>>5
Many thanks sir

Name: Anonymous 2011-09-10 14:55

>>5
I have zero experience with libpng, I'd just use readpng_get_image() to get the image?

Name: Anonymous 2011-09-10 14:57

>>8
libpng offers a few different ways. Just read the documentation, there's a tutorial in there somewhere.

Name: Anonymous 2011-09-10 15:10

>>9
Okay thanks

Name: Anonymous 2011-09-10 16:16

>>4
hey nigger, check my doubles

Name: Anonymous 2011-09-10 17:55

Use libpng++ (the c++ bindings)

Even a monkey can manipulate images with that.

Hell, even I could figure out how it worked! Much better than the crappy C API!

Name: Anonymous 2011-09-10 18:54

>>12
The C++ bindings are shitty because they use global operator new/delete, which makes them useless for serious software development. libpng lets you provide your own malloc/free functions via hooks.

Name: Anonymous 2011-09-10 21:39

>>13
Why would you use malloc and free in C++?

Name: Anonymous 2011-09-10 21:46

>>13
That wasn't my point. What does malloc and free have anything to do with being able to override the underlying allocation mechanism?

libpng lets you override. libpng++ does not for things like allocation of image buffers. it's hardcoded to use operator new/delete.

Name: Anonymous 2011-09-10 21:54

|

Name: Anonymous 2011-09-10 21:58

Name: Anonymous 2011-09-10 22:17

Fuck C++.

Name: tdavis 2011-09-10 22:28

"cout" with "endl" is the silliest shit.
operator overloading is stupid except for linear algebra.
exceptions needin a class seems silly.

Bone-headed shit.

I guess "new" is useful.  In LoseThos is haven't implemented it and just use malloc in an initialization routine.  That's pretty-much just as good.

God says...
Line: 8562

36:2 And Moses called Bezaleel and Aholiab, and every wise hearted
man, in whose heart the LORD had put wisdom, even every one whose
heart stirred him up to come unto the work to do it: 36:3 And they
received of Moses all the offering, which the children of Israel had
brought for the work of the service of the sanctuary, to make it
withal. And they brought yet unto him free offerings every morning.

36:4 And all the wise men, that wrought all the work of the sanctuary,
came every man from his work which they made; 36:5 And they spake unto
Moses, saying, The people bring much more than enough for the service
of the work, which the LORD commanded to make.

Name: Anonymous 2011-09-11 2:12

>>17
That doesn't explain why there would ever be a reason to use malloc in C++. Whether or not new is implemented using malloc is not a reason.

Name: Anonymous 2011-09-11 2:54

not immediately implementing a superior language when forced to write C/C++
2011

seriously, guys?

Name: Anonymous 2011-09-11 3:37

>>21
Oh my, another monkey escaped from the zoo.

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2011-09-11 4:21

>>5
for (i = 0; i < num_px; i++)
 if(buffer[i]!=16777215)
  printf("%d,%d\n",i%width,i/height);

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2011-09-11 4:23

>>23
s/i\/height/i\/width

Name: Glenn Randers-Pehrson 2011-09-11 20:40

pngtest.c, which is part of the libpng distribution, has example call-back functions, one of which counts black pixels -- you could easily modify it to count pixels of another color.

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