五月 02, 2011

Dev C++轉換及顯示BMP 16色 4位元圖檔(未修正檔頭)

若無法顯示圖檔,需使用檔頭轉換程式轉換檔頭

#include
#include
#include

int create(char *filename);
int show(char *filename);

int main(void){

char filename[128];
char mcs;

printf("(C)reate (S)how = ");
scanf("%s",&mcs);
if(mcs=='C' || mcs=='c' || mcs=='S' || mcs=='s'){
printf("Filename=");
scanf("%s",filename);

initwindow(800,600);

if(mcs=='C' || mcs=='c'){
if(!create(filename))
printf("<告知> %s.bmp 轉檔完成 !n",filename);
}
else
show(filename);

system("PAUSE");
closegraph();
}
return 0;
}

int show(char *filename){
char fname_bmp[128];
unsigned long size;
void *temp;
FILE *fp;

sprintf(fname_bmp, "%s.xxx", filename);

if (!(fp = fopen(fname_bmp, "rb"))){
printf("<告知> %s.xxx 不存在 !n",filename);
return -1;
}
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0, SEEK_SET);

temp = malloc(size);

fread(temp,sizeof(unsigned char),size,fp);
fclose(fp);

putimage(0,0,temp,COPY_PUT);

free(temp);

return 0;
}

int create(char *filename){
unsigned char sheader[118];
long i,j,k,m,p,s,t;
long size,tsize,dn=0,dd,x=0,y=0;
char fname_bmp[128];
FILE *fp;
unsigned char color[16][4];
void *temp;

sprintf(fname_bmp, "%s.bmp", filename);

if (!(fp = fopen(fname_bmp, "rb"))){
printf("<錯誤> %s.bmp 不存在 !n",filename);
return -1;
}

fread(sheader, sizeof(unsigned char), 118, fp);

if(sheader[28]==4){
fseek(fp,0,SEEK_END);
size=ftell(fp);
fseek(fp,0,SEEK_SET);

unsigned char image[size];
fread(image, sizeof(unsigned char), size, fp);
fclose(fp);
for(i=0;i<16;i++)
for(j=0;j<4;j++)
color[i][j]=sheader[54+4*i+j];


for(i=3;i>=0;i--){
x=256*x+sheader[18+i];
y=256*y+sheader[22+i];
dn=256*dn+sheader[34+i];
}
dn=dn/y;
dd=dn-x/2;
x--;
y--;
tsize=imagesize(0,0,x,y);
printf("x=%d y=%d size=%d -->> %d %dn ",x+1,y+1,tsize,dn,dd);

unsigned char target[tsize];

temp = malloc(tsize);
setcolor(RED);
setfillstyle(1,LIGHTRED);
bar(0,0,x,y);
getimage(0, 0, x, y, temp);

sprintf(fname_bmp, "%s.xxx", filename);
fp = fopen(fname_bmp, "wb");
fwrite(temp, sizeof(unsigned char), tsize, fp);
fclose(fp);
free(temp);

fp = fopen(fname_bmp, "rb");
fread(target, sizeof(unsigned char), tsize, fp);
fclose(fp);

k=24;
for(j=y;j>=0;j--){
s=dn*j+118; //n*j+118
t=s+dn-dd-1; //n*j+118+n-1
printf("%0x - %0xt",s,t);
for(i=s;i<=t;i++){
p=image[i]/16;
for(m=0;m<3;m++)
target[k++]=color[p][m];
k++;
p=image[i]%16;
for(m=0;m<3;m++)
target[k++]=color[p][m];
k++;
}
}
printf("n");
fp = fopen(fname_bmp, "wb");
fwrite(target, sizeof(unsigned char), tsize, fp);
free(target);
fclose(fp);

return 0;
}
else{
printf("<錯誤> %s 非16色格式bmp檔n",fname_bmp);
fclose(fp);
return -1;
}
}


Posted by superman at 03:03  迴響 (0)引用 (0)C語言教學

引用URL

http://blog2.cyhs.tp.edu.tw/teacher/trackback.php?id=2764
回應文章