TA的每日心情 | 开心 2024-11-20 21:23 |
---|
签到天数: 597 天 连续签到: 1 天 [LV.9]以坛为家II
|
本帖最后由 robe.zhang 于 2018-9-24 15:45 编辑
先看这个博文,写的很详细:
https://www.cnblogs.com/wainiwann/p/7086844.html
这个程序是读出文件头和图像信息头:
8bit 图像用到了调色板
16bit图像没有调色板
24bit图像也没有调色板,正好比8bit图像少256个调色索引项,每一个调色索引项4byte,正好是1024字节。16/24bit数据偏移比8bit数据偏移少1024。
这个是程序代码:
- #include<stdio.h>
- #include<string.h>
- #include<sys/types.h>
- #include <iostream>
- #pragma pack(2)
- using namespace std;
- #define u_int16_t unsigned short int
- #define u_int32_t unsigned int
- //下面两个结构是位图的结构
- typedef struct BITMAPFILEHEADER
- {
- u_int16_t bfType;
- u_int32_t bfSize;
- u_int16_t bfReserved1;
- u_int16_t bfReserved2;
- u_int32_t bfOffBits;
- }BITMAPFILEHEADER;
- typedef struct BITMAPINFOHEADER
- {
- u_int32_t biSize;
- u_int32_t biWidth;
- u_int32_t biHeight;
- u_int16_t biPlanes;
- u_int16_t biBitCount;
- u_int32_t biCompression;
- u_int32_t biSizeImage;
- u_int32_t biXPelsPerMeter;
- u_int32_t biYPelsPerMeter;
- u_int32_t biClrUsed;
- u_int32_t biClrImportant;
- }BITMAPINFODEADER;
- void showBmpHead(BITMAPFILEHEADER &pBmpHead){
- cout<<"====位图文件头信息====,大小:"<< sizeof(struct BITMAPFILEHEADER) <<endl;
- cout<<"类型 :"<<pBmpHead.bfType;
- char a,b;
- a=pBmpHead.bfType&0x00ff;
- b=(pBmpHead.bfType&0xff00)>>8;
- cout<<" / "<< a << b << ". ** 备注:是'BM',符合windows bitmap 图像标记" << endl;
- cout<<"大小 :"<<pBmpHead.bfSize<<endl;
- cout<<"保留1:"<<pBmpHead.bfReserved1<<endl;
- cout<<"保留2:"<<pBmpHead.bfReserved2<<endl;
- cout<<"偏移 :"<<pBmpHead.bfOffBits<<endl;
- }
- void showBmpInforHead(BITMAPINFODEADER &pBmpInforHead){
- cout<<"====位图信息头信息====,大小:"<< sizeof(BITMAPINFODEADER) << endl;
- cout<<"信息头长度 :"<<pBmpInforHead.biSize<<endl;
- cout<<"位图宽 :"<<pBmpInforHead.biWidth<<endl;
- cout<<"位图高 :"<<pBmpInforHead.biHeight<<endl;
- cout<<"biPlanes平面数 :"<<pBmpInforHead.biPlanes<<endl;
- cout<<"biBitCount颜色位数:"<<pBmpInforHead.biBitCount<<endl;
- cout<<"压缩 :"<<pBmpInforHead.biCompression<<endl;
- cout<<"biSizeImage大小:"<<pBmpInforHead.biSizeImage<<endl;
- cout<<"X方向分辨率 :"<<pBmpInforHead.biXPelsPerMeter<<endl;
- cout<<"Y方向分辨率 :"<<pBmpInforHead.biYPelsPerMeter<<endl;
- cout<<"使用的颜色数 :"<<pBmpInforHead.biClrUsed<<endl;
- cout<<"重要颜色数 :"<<pBmpInforHead.biClrImportant<<endl;
- }
- int main( int argc, char **argv )
- {
- if(argc!=2)
- {
- cout << "usage: ./main /path/to/file.bmp" << endl;
- return 0;
- }
- FILE *fp = fopen(argv[1], "rb");
- BITMAPFILEHEADER head;
- BITMAPINFODEADER info;
- fread(&head, 1, sizeof(BITMAPFILEHEADER), fp);
- fread(&info, 1, sizeof(BITMAPINFODEADER), fp);
- showBmpHead(head);
- showBmpInforHead(info);
- fclose(fp);
- return 0;
- }
复制代码 这是三张已经转好的bmp图像,linux gimp 处理图像功能还是很强大的,这个软件还免费
photo.bmp.tar
(320 KB, 下载次数: 0)
|
|