#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
#pragma pack(2)

/*

Definitions for Windows 3.0 and above DIB files

*/

typedef unsigned
char BYTE;
typedef unsigned
short WORD;
typedef unsigned
long DWORD;
typedef unsigned
short UINT;
typedef signed
long LONG;

typedef
struct tagRGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;

typedef
struct tagRGB
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
} RGB;


typedef
struct tagBITMAPINFOHEADER
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

/* constants for the biCompression field */
#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L

#define WIDTHBYTES(bits) (((bits)+31)/32*4)
typedef
struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[
1];
} BITMAPINFO;

typedef
struct tagBITMAPFILEHEADER
{
UINT bfType;
DWORD bfSize;
UINT bfReserved1;
UINT bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;

int main()
{
BITMAPFILEHEADER bmphead;
BITMAPINFOHEADER bmpinfohead;
RGB rgb;
WORD pixel;
char filename[] = "b.bmp";
FILE
*fp,*fp2;
int off;

fp
= fopen(filename,"rb");
fp2
= fopen("rs.c","w");

off
= fread((void*)&bmphead,sizeof(BITMAPFILEHEADER),1,fp);
printf(
"offset %d,size %d k\n",bmphead.bfOffBits,int(bmphead.bfSize/1024));

fread((
void*)&bmpinfohead,sizeof(BITMAPINFOHEADER),1,fp);
printf(
"bitcount = %d bisizeimage = %d,width =%d height = %d\n",bmpinfohead.biBitCount,bmpinfohead.biSizeImage ,
bmpinfohead.biWidth,bmpinfohead.biHeight);

int height = bmpinfohead.biHeight;
int width = bmpinfohead.biWidth;
int l_width =WIDTHBYTES(width* bmpinfohead.biBitCount);//计算位图的实际宽度并确保它为32的倍数


BYTE
*pClrData=(BYTE *)malloc(height*l_width);
memset(pClrData,
0,height*l_width);
long nData = height*l_width;

int nRead = fread(pClrData,1,nData,fp);
printf(
"%d\n",nRead);

int k;
int index = 0;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
k
= i*l_width + j*3;
rgb.rgbRed
= pClrData[k+2];
rgb.rgbGreen
= pClrData[k+1];
rgb.rgbBlue
= pClrData[k];
pixel
= ((rgb.rgbRed)>>3)<<11|(rgb.rgbGreen>>2)<<5|(rgb.rgbBlue>>3);
index
++;
fprintf(fp2,
"0x%x, ",pixel);
if(index%10==0)
fprintf(fp2,
"\n");
}
}
printf(
"%d\n",index);
fclose(fp2);
fclose(fp);
return 0;
}

 

posted on 2010-04-21 17:10  灰太狼大王  阅读(379)  评论(0编辑  收藏  举报