G
G
Grigory Dikiy2016-10-24 11:03:53
C++ / C#
Grigory Dikiy, 2016-10-24 11:03:53

Get RGB pixel information?

Good afternoon! I am studying OpenGL and it became necessary to get information about a pixel from an image, namely the value R * G * B.
The image is loaded using stbi:

int width, height, numComponents;
    unsigned char * data = stbi_load("./resources/models/terrain/heightmap.png", &width, &height, &numComponents, STBI_rgb);

    if(data == NULL)
        std::cerr << "Unable to load texture: hightmap.png" << std::endl;

 After loading into data, the information is stored in the format:
f45eec83b8b147b9832dbce0707cf23b.png
I assumed that the pixels in data are stored as follows: r1g1b1r2g2b2 .. and wrote a simple function:
int Terrain::GetRGB(int x, int y, unsigned char * image, int width, int height)
{
    int rgb;

    rgb = image[3 * x * width + 3 * y * height]     +
          image[3 * x * width + 3 * y * height + 1] +
          image[3 * x * width + 3 * y * height + 2];

    return rgb;
}

But the function crashes when the value is:
5a31c230d9a649c8b1ff0b9a1375dd02.png
Maybe someone has worked with this format and knows how to use it in order to get a pixel? I will be grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-10-24
@frilix

Rather
R - (y * width + x) * 3
G - (y * width + x) * 3 + 1
B - (y * width + x) * 3 + 2
x and y with 0, not with 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question