1
不正确的读数我做了3×3的图像与黑色所有的广场(0,0,0),除非角落... 在那里我有一个红色,绿色,蓝色和白色像素,如下图所示:充分利用图像
R, 0, G
0, 0, 0
B, 0, W
如果我正确理解,应该将其放置在像素数据数组中的R, 0, G, 0, 0, 0, B, 0, W
。 我遇到的问题是,什么是打印出来的是:
[255, 0, 0] [0, 0, 0] [0, 0, 0]
[0, 0, 0] [0, 0, 0] [0, 0, 0]
[0, 0, 255] [0, 0, 255] [255, 0, 0]
这里是我的代码:
Uint32 GetPixel(SDL_Surface *img, int x, int y) {
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32*)img->pixels;
//Get the requested pixel
Uint32 offsetY = y * img->w;
Uint32 offsetPixel = offsetY + x;
Uint32 pixel = pixels[offsetPixel];
return pixel;
}
int main(int argc, char *argv[]) {
printf("Hello world!\n");
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *img = IMG_Load("Images/Colors.png");
vector <Uint32> pixels;
SDL_LockSurface(img);
for (int y = 0; y < img->h; y++) {
Uint8 r, g, b;
Uint32 pixel;
for (int x = 0; x < img->w; x++) {
pixel = GetPixel(img, x, y);
SDL_GetRGB(pixel, img->format, &r, &g, &b);
printf("[%u, %u, %u]\t", r, g, b);
pixels.push_back(pixel);
}
printf("\n");
}
SDL_UnlockSurface(img);
system("pause");
return 0;
}
编辑:我期待什么:
[255, 0, 0] [0, 0, 0] [0, 255, 0]
[0, 0, 0] [0, 0, 0] [0, 0, 0]
[0, 0, 255] [0, 0, 0] [255, 255, 255]
它的工作,但你能指定什么是错?我是否访问错误的内存? – Ledii
更完整的/正确的版本:http://sdl.beuc.net/sdl.wiki/Pixel_Access。基本上,问题是,你假设两件事情可能不是真正的neccessarily:1)行间距== img-> W * BPP - 看到这一点:http://www.gamedev.net/topic/518067-sdl -what-is-pitch /; 2)你有像素的32位编码(在我的测试中,我正在处理一个3bpp png文件) – dragosht