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

Sep 17, 2018 at 3:45pm
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 Sep 17, 2018 at 4:17pm
Sep 17, 2018 at 4:02pm
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 
Sep 17, 2018 at 4:10pm
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 Sep 17, 2018 at 4:10pm
Sep 17, 2018 at 4:29pm
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.
Sep 17, 2018 at 4:40pm
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 Sep 17, 2018 at 4:46pm
Sep 17, 2018 at 4:47pm
closed account (E0p9LyTq)
Do you understand the concept of pointer math?

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