سؤال

I wrote this function that I want to use in a program, but for some reason, it fails despite nothing going wrong:

std::deque <std::deque <bool> > load_image(std::string & image_name){
    SDL_Surface * image = open_image(image_name);
    if (!image)
        exit(3);
    Uint32 * pixels = (Uint32 *) image -> pixels;
    std::deque <std::deque <bool> > grid(HEIGHT, std::deque <bool>(WIDTH, false));
    for(int y = 0; y < std::min(image -> h, HEIGHT); y++)
        for(int x = 0; x < std::min(image -> w, WIDTH); x++)
            grid[y][x] = (pixels[(image -> w * y) + x] == 0);
    SDL_FreeSurface(image);
    return grid;
}

I'm simply trying to copy whether or not the pixel is black into the grid. When I run grid[y][x] and (pixels[(image -> w * y) + x] == 0) separately, the program runs fine. When I do grid[y][x] = (pixels[(image -> w * y) + x] == 0);, the program crashes somewhere in the middle of the image.

I'm pretty sure (image -> w * y) + x gets the correct pixel, no matter what x and y are limited to, so what am I not seeing??

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top