我有一个函数可以读取表面的像素并将最亮的像素的索引写入另一个数组。使用 gdb,我注意到像素数组中的所有值恰好在 3/4 处变为 0,并且在距该点正好 1020 个像素之后,程序段错误。这是代码:
void scanBrightPixels(int** write_to, int* pcount, SDL_Surface* image) {
SDL_PixelFormat* f = image->format;
SDL_LockSurface(image);
Uint32* pixels = (Uint32*) image->pixels;
SDL_UnlockSurface(image);
int lpcount = 0;
for (int i = 0; i < image->w * image->h / 4 * 3; i++) {
Uint8 r, g, b;
Uint8 brightness;
SDL_GetRGB(pixels[i], f, &r, &g, &b); // this is where the segfault happens
brightness = (Uint8) ((r * 0.33) + (g * 0.5) + (b * 0.16));
if (brightness >= 251) {
(*write_to)[lpcount] = i;
lpcount++;
}
}
*pcount = lpcount;
}
该函数也从 std::async 调用,如果这有所不同的话。有任何想法吗?