从SDL_Texture访问像素颜色
Access pixel color from SDL_Texture
我的代码抛出了一个访问错误。这是代码:
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;
}
这一行:
Uint8 *colors = (Uint8*)getPixel;
据我从您的代码中可以看出,纹理中位置 (x,y) 处的 32 位 RGBA 像素颜色值getPixel
。然后,出于某种原因将此值强制转换为Uint8*
。
所以现在colors
包含一个无意义的地址,等于像素颜色值,而不是您可能想要的getPixel
地址。因此,当您执行colors[0]
并尝试通过此"地址"读取内存时,会出现访问冲突。
您的意思是在转换之前先获取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
即可。
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 无法获取窗口 C++ 的像素颜色
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 从SDL_Texture访问像素颜色
- 结构像素颜色值
- 将OpenCV中的像素颜色映射到预定的颜色列表中
- magick 获得像素颜色.PixelPacket似乎不再存在了
- 在数组中设置像素颜色
- 在 openCV 中更改单个像素颜色
- 获取图像的像素颜色
- 使用 BMP 作为高度图.如何访问像素颜色以设置高度
- 以最快的方式获取像素颜色
- glsl-获取像素颜色[像素着色器]
- 如何访问位图中的像素颜色
- 快板定义像素颜色
- 使用像素颜色的人工神经网络
- 使用magick++获取像素颜色
- FreeImage:获取像素颜色
- 从指定坐标[x, y]处的位图数据获取像素颜色