Image Processing with c++ and OpenGL, trying to understand a code

I'm learning image processing using C++ and OpenGL.
I encountered this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char *argv[])
{
  unsigned char* image;
    image = SOIL_load_image("Images/image.png", &width, &height, 0, SOIL_LOAD_RGBA);
    if(image == NULL) exit(0);
  }

static void createImages(void)
{
    unsigned char *p;

    p = image;
    for(int i = 0; i < height*width; i++) {     
        if(*p == 0 && *(p+1) == 0) { //(p+1)==0-->Green pixel =0, what is *p==0 ?
     {...

}


what does *p == 0 in condition mean ? checking pointer to the image as 0 ?
Does that mean checking if the image is loaded ?
Last edited on
It looks wrong to me. I can't find the documentation for SOIL_load_image(), but I would guess that the correct way to check for an invalid return value would be
1
2
if (!image)
    //handle error 
or
1
2
if (image == nullptr)
    //handle error 
or (in C)
1
2
if (image == NULL)
    //handle error 
My bad, it looks wrong because I put only small chunk of the code that I don't undertsand, please see my updated question. The program is working fine. I just don't understand the *p==0 part
Last edited on
closed account (E0p9LyTq)
createImages() is using pointer math to step through and check if two adjacent elements in your image char array are green pixels.

You are likely to step out of bounds when making the ending pixel if check *(p + 1) == 0 when *p is the end pixel in your array.
Sorry, I don't quite understand. I think checking *(p + 1) == 0 is just checking the green pixel value equals 0 or not (from range 0-255).

So, condition if(*p == 0 && *(p+1) == 0) is to check if green pixel value is 0 and *p==0, what is this *p==0 ?

Later in this code, there's also this condition if(*p == 255 && *(p+1) == 0), what's the *p in this condition ? is it also range from 0-255 ? But this is pointing to the image, not the RGB channel
Last edited on
closed account (E0p9LyTq)
Do you understand the concept of pointer math?

https://www.tutorialspoint.com/cplusplus/cpp_pointer_arithmatic.htm
I do
hm. I got it now basically it's p[0]. Thanks all!
closed account (E0p9LyTq)
If p should be incremented/changed in your loop it won't be p[0].
Topic archived. No new replies allowed.