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