我正在尝试在TCPP的图形窗口中导入 Anand.BMP 文件,
为此它的源代码如下
(注意:我没有在源代码中提到头文件):
struct A
{
char type[2];
unsigned long size;
unsigned short int reserved1,reserved2;
unsigned long offset;
unsigned long width,height;
unsigned short int planes;
unsigned short int bits;
unsigned long compression;
unsigned long imagesize;
unsigned long xresolution,yresolution;
unsigned long ncolors;
unsigned long importantcolors;
}HEADER;
huge DetectSvga()
{
return 2;
}
void show()
{
fstream File;
File.open("C:\\TURBOC3\\BIN\\Anand.BMP",ios::in|ios::binary);
char ch;
File.read((char*)&HEADER,sizeof(HEADER));
unsigned int i;
char ColorBytes[4];
char *PaletteData;
PaletteData=new char[256*3];
if(PaletteData)
{
for(i=0;i<256;i++)
{
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
outp(0x03c8,0);
for(i=0;i<256*3;i++)
outp(0x03c9,PaletteData[i]);
delete[]PaletteData;
}
for(i=0;i<HEADER.height;i++)
{
for(int j=0;j<HEADER.width;)
{
File.read(&ch,1);
putpixel(0+(j++),0+HEADER.height-i-1,ch);
}
}
File.close();
}
void main()
{
clrscr();
int gd=DETECT,gm,a;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
installuserdriver("svga256",&DetectSvga);
show();
getch();
closegraph();
}
现在,我没有在图形窗口中获取 BMP 文件,
即
图形窗口没有正确显示 Anand.bmp;输出是这样显示的,
那么如何解决呢?
为了方便起见,我在这里附上我的 Anand.BMP 文件。
我认为调色板没有通过PaletteData指针正确显示,
即错误出现在这段代码中:
for(i=0;i<256;i++)
{
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
根据建议,我将上述代码修改如下:
[编辑]:
typedef unsigned long DWORD;
typedef unsigned int WORD;
typedef unsigned short BYTE;
//---------------------------------------------------------------------------
class BMP
{
public:
BYTE *data;
DWORD size;
#pragma pack(push,1)
struct _hdr
{
char ID[2];
DWORD size;
WORD reserved1[2]; // ?
DWORD offset;
DWORD reserved2; // ?
DWORD width,height;
WORD planes;
WORD bits;
DWORD compression;
DWORD imagesize;
DWORD xresolution,yresolution;
DWORD ncolors;
DWORD importantcolors;
};
#pragma pack(pop)
BMP(){ data=NULL; free(); }
~BMP(){ free(); }
void free(){ if (data) delete[] data; data=NULL; size=0; }
void load(char* filename)
{
FILE *hnd;
free();
if ((hnd=fopen(filename, "rb")) == NULL) return; // open file for read binary (not sure with the "b" check in build help)
size=fseek(hnd,0,2);
fseek(hnd,0,0);
BYTE data[256];
if (data==NULL) // not enough memory or empty file
{
size=0;
fclose(hnd);
return;
}
fread(data,256,1,hnd); // read 256 of 1 BYTES into data array
fclose(hnd); // close file
}
void draw(int x0,int y0)
{
_hdr *hdr=(_hdr*)data;
int x,y,xs,ys,skip;
DWORD pal[256],c; // palete to convert 8bpp -> 32bit VCL color
BYTE *p;
if (size<2) return;
if (hdr->ID[0]!='B') return; // check magic number
if (hdr->ID[1]!='M') return;
if (hdr->planes!=1) return; // check format
if (hdr->bits!=8) return;
if (hdr->compression!=0) return;
// palette
p=data+hdr->offset-(3*256);
p=data+sizeof(_hdr);
for (x=0;x<256;x++)
{
c =(*p) ; p++; // B
c|=(*p)<< 8; p++; // G
c|=(*p)<<16; p++; // R
p++; // A
pal[x]=c;
}
// image
xs=hdr->width;
ys=hdr->height;
p=data+hdr->offset;
skip=(((hdr->bits*hdr->width)+31)>>5)<<2; // compute scanline align
skip-=hdr->width;
for (y=0;y<ys;y++)
{
for (x=0;x<xs;x++,p++)
{
putpixel(x0+x,y0+ys-y-1,*p);
}
p+=skip; // handle align
}
y++;
}
};
//---------------------------------------------------------------------------
huge DetectSvga()
{
return 2;
}
void main()
{
clrscr();
int gd=DETECT,gm,a;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
installuserdriver("svga256",&DetectSvga);
BMP bmp;
bmp.load("C:\\TURBOC3\\BIN\\Anand.BMP");
bmp.draw(0,0);
getch();
closegraph();
}
现在,上面的代码没有给出错误但有 2 个警告!!
警告:
1::for(x=0;x<256;x++)
“包含for
的函数未内联扩展”
2:,}
即在void load()
函数末尾:“包含某些if
语句的函数未内联扩展”
结果图像未显示在输出窗口中
输出显示如下
我认为y++;
应该在for (y=0;y<ys;y++){...}
循环内
所以,请分析编辑后的代码......