16色真采转换编译问题
#include<stdio.h>
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
#define noerror 0
#define errorfileopen1
#define errorfiletype 2
#define errorimagecolor 3
typedef struct tagbitmapfileheader {
unsigned int bftype;
unsigned longbfsize;
unsigned intbfreserved1; unsigned intbfreserved2;
unsigned longbfoffbits;
} bitmapfileheader;
typedef struct tagbitmapinfoheader {
unsigned longbisize;
unsigned long biwidth;
unsigned longbiheight;
unsigned intbiplanes;
unsigned intbibitcount;
unsigned long bicompression;
unsigned long bisizeimage;
unsigned long bixpelspermeter;
unsigned long biypelspermeter;
unsigned long biclrused;
unsigned long biclrimportant;
} bitmapinfoheader;
typedef struct tagrgbquad {
unsigned char rgbblue;
unsigned char rgbgreen;
unsigned charrgbred;
unsigned char rgbreserved;
} rgbquad;
void main(int argc,char *argv[]);int showbmp(char *filename);
int getcolor(unsigned char r,unsigned char g, unsigned char b,int x,int y)
;
void setvideomode(unsigned char mode);
void setpalreg(unsigned char *palreg);
void setdacreg(unsigned char *dacreg, int color, int count);
void putpixel(int x, int y,unsigned char color);
unsigned char palreg[17]= { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
unsigned char standardpal[48]= {
0, 0, 0, 32, 0, 0, 0,32, 0, 32,32, 0, 0, 0,32, 32, 0,32, 0,32,32, 32,32,
32, 48,48,48, 63, 0, 0, 0,63, 0, 63,63, 0, 0, 0,63, 63, 0,63, 0,63,63, 63,63,63,};
unsigned char lightnessmatrix [16][16]= {
{ 0,235,59,219,15,231,55,215,2,232,56,217,12,229,52,213},
{128,64,187,123,143,79,183,119,130,66,184,120,140,76,180,116},
{33,192,16,251,47,207,31,247,34,194,18,248,44,204,28,244},
{161,97,144,80,175,111,159,95,162,98,146,82,172,108,156,92},
{8,225,48,208,5,239,63,223,10,226,50,210,6,236,60,220},
{136,72,176,112,133,69,191,127,138,74,178,114,134,70,188,124},
{41,200,24,240,36,197,20,255,42,202,26,242,38,198,22,252},
{169,105,152,88,164,100,148,84,170,106,154,90,166,102,150,86},
{3,233,57,216,13,228,53,212,1,234,58,218,14,230,54,214},
{131,67,185,121,141,77,181,117,129,65,186,122,142,78,182,118},
{35,195,19,249,45,205,29,245,32,193,17,250,46,206,30,246},
{163,99,147,83,173,109,157,93,160,96,145,81,174,110,158,94},
{11,227,51,211,7,237,61,221,9,224,49,209,4,238,62,222},
{139,75,179,115,135,71,189,125,137,73,177,113,132,68,190,126},
{43,203,27,243,39,199,23,253,40,201,25,241,37,196,21,254},
{171,107,155,91,167,103,151,87,168,104,153,89,165,101,149,85},
};
unsigned char colortable[2][2][2]= {
{{0,12},{10,14}},{{9,13},{11,15}}};
unsigned char colormap[256][3];
void main (int argc, char *argv[])
{
if(argc!=2) {
printf("usage:\n\tshow back.bmp\n");
exit(1);
}
showbmp(argv[1]);
}
int showbmp(char *filename)
{
file *fp;
bitmapfileheader filehead;
bitmapinfoheader infohead;
rgbquad rgb;
int n, w,y,x,c,color;
unsigned char buffer[4096];
if (!(fp=fopen(filename,"rb")))
return(errorfileopen);
fread(&filehead,siazeof(bitmapfileheader),1,fp);
if(filehead.bftype!='bm')
return(errorfiletype);
fread(&infohead,sizeof(bitmapfileheader),1,fp);
if(infohead.bibitcount!=8 && infohead.bibitcount!=24) {
fclose(fp);
return(errorimagecolor);
}
setvideomode(0x12);
setpalreg(palreg);
setdacreg(standardpa1,0,16);
if(infohead.bibitcount==8) {
for (n=0;n<256;n++) {
fread(&rgb, sizeof(rgbquad),1,fp);
colormap[n][0]=rgb.rgbred;
colormap[n][1]=rgb.rgbgreen;
colormap[n][2]=rgb.rgbblue;
}
w=(infohead.biwidth+3)/4*4;
for(y=infohead.biheight-1;y>=480;y--)
fread(buffer,sizeof(unsigned char),w,fp);
for(;y>0;y--) {
fread(buffer, sizeof(unsigned char),w,fp);
for (x=0;x<infohead.biwidth && x<640;x++) {
c=buffer[x];
color=getcolor(color
map[c][0],colormap[c][1],c
olormap[c][2],x,y);
putpixel (x,y,color);
}
}
}
else {
w=(infohead.biwidth*3+3)/4*4;
for(y=infohead.biheight-1;y>639;y--)
fread(buffer,sizeof(unsigned char),w,fp);
for(;y>=0;y--) {
fread(buffer,sizeof(unsigned char),w,fp);
for (x=0;x<infohead.biwidth && x<640;x++) {
c=x*3;
color=getcolor(buffer[c+2],buffer[c+1],buffer[
c],x,y);
putpixel(x,y,color);
}
}
}
getch();
fclose(fp);
setvideomode(0x03);
return(noerror);
}
int getcolor(unsigned char r, unsigned char g,unsigned char b, int x,
int y){
unsigned int l=lightnessmatrix[y & 0x0f][x & 0x0f];
return(colortable[(
unsigned int)r*256/255>l][(
unsigned int)g*256/255>l][(
unsigned int)b*256/255>l]);}
void setvideomode(unsigned char mode){
-h=0x00;
-al=mode;
geninterrupt(0x10);
}
voidsetpalreg(unsigned char *palreg){
-es=fp-seg((unsigned char far*)palreg);
-dx=fp-off((unsigned char far*)palreg;
-ax=0x1002;
geninterrupt(0x10);
}
void setdacreg(unsigned char *dacreg,int color,int count){
-es=fp-seg((unsigned char far*)dacreg);
-dx=fp-off((unsigned char far*)dacreg);
-ax=0x1012;
-bx=color;
-cx=count;
geninterrupt(0x10);
}
void putpixel(int x, int y, unsigned charcolor){
-ah=0x0c;
-al=color;
-cx=x;
-dx=y;
geninterrupt(0x10);
}
}
