📄 project.c
字号:
#include<stdio.h>
#include<process.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef unsigned int UINT;
typedef signed long LONG;
typedef struct tagBITMAPFILEHEADER
{
UINT bfType;
DWORD bfSize;
UINT bfReserved1;
UINT bfReserved2;
DWORD bfoffBIts;
}BITMAPFILEHEADER;
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;
BITMAPINFOHEADER bitfile;
BITMAPINFOHEADER bitinfo;
FILE * fpi, *fpo;
long offset,sum,count,width,height;
int i,repcount;
char data,datatp;
void compress(char filei[12],char fileo[12]){
if((fpi=fopen(filei,"rb"))==NULL)
{
printf("can not open DIB file");
exit(1);
}
if((fpo=fopen(fileo,"wb"))==NULL)
{
printf("can not create a file");
exit(1);
}
fread(&bitfile,sizeof(BITMAPFILEHEADER),1,fpi);
fread(&bitinfo,sizeof(BITMAPINFOHEADER),1,fpi);
offset=bitinfo.biWidth;
height=bitinfo.biHeight;
rewind(fpi);
for(i=0;i<offset;i++)fprintf(fpo,"%c",getc(fpi));
if(bitinfo.biBitCount==4) sum=(width*height)/2;
if(bitinfo.biBitCount==8) sum=width*height;
count=01;
data=getc(fpi);
do{
datatp=getc(fpi);
count++;
repcount=1;
while((data==datatp)&&(repcount<63)&&(count<sum))
{
repcount ++;
datatp=getc(fpi);
count++;
}
if ((repcount >1)||(unsigned int)data>0xbf)
{
repcount +=0xc0;
fprintf(fpo,"%c",repcount);
}
fprintf(fpo,"%c",data);
data=datatp;
}while(count<sum);
fclose(fpi);
fclose(fpo);
return;
}
void decompress(char filei[12],char fileo[12]){
if((fpi=fopen(filei,"rb"))==NULL){
printf("can not open a compressed file");
exit(1);
}
if((fpo=fopen(fileo,"wb"))==NULL)
{
printf("can not create a file");
exit(1);
}
fread(&bitfile,sizeof(BITMAPFILEHEADER),1,fpi);
fread(&bitinfo,sizeof(BITMAPINFOHEADER),1,fpi);
offset=bitinfo.biWidth;
height=bitinfo.biHeight;
rewind(fpi);
for(i=0;i<offset;i++) fprintf(fpo,"%c",getc(fpi));
if(bitinfo.biBitCount==4) sum=(width*height)/2;
if(bitinfo.biBitCount==8) sum=width*height;
count=01;
do{
data=getc(fpi);
if(((data&0xc0)>>6)==3)
{
repcount=data&0x3f;
data=getc(fpi);
for(i=0;i<repcount;i++)
{
fprintf(fpo,"%c",data);
count++;
}
}else{
fprintf(fpo,"%c",data);
count++;
}
}while(count<sum);
fclose(fpi);
fclose(fpo);
return;
}
void main(int argc,char *argv[])
{
decompress("a.bmp","b.bmp");
decompress("b.bmp","c.bmp");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -