从SDL_Texture访问像素颜色

时间:2017-11-04 01:00:26

标签: c++ sdl pixel sdl-2

我的代码抛出了访问错误。这是代码:

SDL_Color *getPixelColor(SDL_Surface *loadingSurface, int x, int y) {

    SDL_Color getColor = {0,0,0};

    SDL_Texture *readingTexture = nullptr;
    readingTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, loadingSurface->w, loadingSurface->h);
    SDL_SetTextureBlendMode(readingTexture, SDL_BLENDMODE_BLEND);

    void * vPixels;

    SDL_LockTexture(readingTexture, &loadingSurface->clip_rect, &vPixels, &loadingSurface->pitch);

    memcpy(vPixels, loadingSurface->pixels, loadingSurface->w * loadingSurface->h);

    Uint32 *aPixels = (Uint32*)vPixels;

    SDL_UnlockTexture(readingTexture);

    //Get pixel at location.
    Uint32 getPixel = aPixels[y * loadingSurface->w + x];
    Uint8 *colors = (Uint8*)getPixel;

    //cout << colors[0] << " " << colors[1] << " " << colors[2] << endl;

    getColor.r = colors[0];
    getColor.g = colors[1];
    getColor.b = colors[2];

    SDL_Color *color = &getColor;
    return color;

}

抛出异常:读取访问冲突。 颜色是0xFF00。

我随时尝试访问颜色。 SDL文档中描述的方法都返回相同的错误,并且除了LazyFoo之外没有互联网上的信息(此方法基于此方法,并且不起作用。)

感谢您的帮助。

修改

我通过重新编写代码并忽略了一些用于理解SDL某些部分的不良来源来修复我的代码。这是工作代码:

Uint32 getPixel(SDL_Surface *loadingSurface, int x, int y) {

    Uint32 *pixels = (Uint32*)loadingSurface->pixels;
    return pixels[(y * loadingSurface->pitch / 4) + x]; // I noticed that each y selection was off by 4 pixels in the y so I divided by 4. Why this is the case remains a mystery.

}

为什么我需要除以4是个谜,需要一段时间来弄明白。有什么想法吗?

SDL_Color getPixelColor(SDL_Surface *loadingSurface, int x, int y) {

    SDL_Color getColor = {0,0,0};
    SDL_PixelFormat *format;
    Uint32 pixel, index;
    Uint8 red, green, blue, alpha;

    format = loadingSurface->format;
    SDL_LockSurface(loadingSurface);
    pixel = getPixel(loadingSurface, x, y);
    SDL_UnlockSurface(loadingSurface);

    index = pixel & format->Rmask;  
    index = index >> format->Rshift; 
    index = index << format->Rloss;  
    red = (Uint8)index;

    index = pixel & format->Gmask;   
    index = index >> format->Gshift; 
    index = index << format->Gloss;  
    green = (Uint8)index;

    index = pixel & format->Bmask;   
    index = index >> format->Bshift; 
    index = index << format->Bloss;  
    blue = (Uint8)index;

    index = pixel & format->Amask;
    index = index >> format->Ashift;
    index = index << format->Aloss;
    alpha = (Uint8)index;

    getColor.r = red;
    getColor.g = green;
    getColor.b = blue;
    getColor.a = alpha;

    return getColor;

}

1 个答案:

答案 0 :(得分:1)

这一行:

Uint8 *colors = (Uint8*)getPixel;

就我的代码而言,getPixel是纹理中位置(x,y)处的32位RGBA像素颜色值。然后,由于某种原因,您将此值转换为Uint8*

所以现在colors包含一个无意义的地址,等于像素颜色值,而不是getPixel的地址,正如您可能想要的那样。因此,当您执行colors[0]并尝试通过此&#34;地址&#34;来读取内存时,您会收到访问冲突。

您是否要在转换之前首先获取getPixel的地址,以便访问各个颜色通道值?

Uint8 *colors = (Uint8*)&getPixel; // Note the &

与您的确切问题无关,但也与您的代码有关:您正在返回指向本地变量的指针:

SDL_Color getColor = {0,0,0};
// etc.
SDL_Color *color = &getColor;
return color;
当函数返回时,

getColor将超出范围,函数的调用者将获得的是一个悬空指针。

您应该重写您的函数以按值返回SDL_Color:只需return getColor;并将返回类型更改为SDL_Color