四月 27, 2011

Dev C++轉換及顯示BMP 24位元全彩圖檔


#include "stdio.h"
#include "stdlib.h"
#include "graphics.h"

int create(char *filename);
int show(char *filename);
unsigned char header[24];

int main(void){

char filename[128],mcs;
void *temp;
int x,y;
unsigned long size;
FILE *fp;

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);

//--------------------------------------------讀取檔頭開始
x=800;
y=600;
size=imagesize(0,0,x,y);
x--;
y--;

setfillstyle(7,LIGHTBLUE);
bar(0,0,x,y);

temp = malloc(size);
getimage(0, 0, x, y, temp);

fp = fopen("temp.tmp", "wb");
fwrite(temp, sizeof(unsigned char), size, fp);
fclose(fp);
free(temp);

fp = fopen("temp.tmp", "rb");
fread(header, sizeof(unsigned char), 24, fp);
fclose(fp);
//----------------------------------------------讀取檔頭結尾

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,s,t ,dn=0,mfst=0,x=0,y=0;
unsigned long size,tsize;
unsigned char *target;
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]==24){
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--){
mfst=256*mfst+sheader[10+i];
x=256*x+sheader[18+i];
y=256*y+sheader[22+i];
dn=256*dn+sheader[34+i];
}
dn=3*x;
x--;
y--;
tsize=imagesize(0,0,x,y);
printf("x=%d y=%d size=%d -->> %dn ",x+1,y+1,tsize,dn);

target=new unsigned char [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);

target[22]=header[22]; //修正檔頭
target[23]=header[23]; //修正檔頭

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

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


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

引用URL

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