typedef struct tagBITMAPFILEHEADER {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
}__attribute__((packed)) BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}__attribute__((packed)) BITMAPINFOHEADER;
typedef struct tagRGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}__attribute__((packed)) RGBQUAD;
unsigned int read_pixel(FILE* fb_fp, int x, int y)
{
unsigned int pixel = 0;
unsigned int location = 0;
unsigned int width = 0;
unsigned int bpp = 0;
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
fseek(fb_fp, 0L, SEEK_SET);
fread(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, fb_fp);
if (bmpFileHeader.bfType != 0x4d42) {
printf("It's not a BMP file\n");
return 0;
}
fread(&bmpInfoHeader, sizeof(BITMAPINFOHEADER), 1, fb_fp);
width = bmpInfoHeader.biWidth;
bpp = bmpInfoHeader.biBitCount >> 3;
location = bmpFileHeader.bfOffBits + (width * (bmpInfoHeader.biHeight - y - 1) + x) * bpp;
fseek(fb_fp, location, SEEK_SET);
fread(&pixel, bpp, 1, fb_fp);
if (bpp == 3) {
pixel = ((pixel & 0xff) << 16) | (pixel & 0xff00) | ((pixel & 0xff0000) >> 16);
}
return pixel;
}
int main(void)
{
FILE* fb_fp;
int x, y;
unsigned int pixel;
fb_fp = fopen(FB_DEVICE_NAME, "rb");
if (!fb_fp) {
printf("open %s failed\n", FB_DEVICE_NAME);
return -1;
}
for (y = 0; y < 480; y++) {
for (x = 0; x < 800; x++) {
pixel = read_pixel(fb_fp, x, y);
printf("(%03u,%03u,%03u)", (pixel >> 16) & 0xff, (pixel >> 8) & 0xff, pixel & 0xff);
}
printf("\n");
}
fclose(fb_fp);
return 0;
}
</stdio.h>
暂无评论