Hi all,
I implemented RLE using Turbo C.
However there are a few limitations with the code.
It compresses 256-bit color images and monochrome bitmap images well.
However fails with most other bmp files.
Is this normal?
Also please give suggestions about the code.
Thanks in advance.Code:#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
FILE *fp,*fq,*fr;
unsigned long int t1,t2,i=1,t;
int repeat;
char fname[30],fname1[30],str[10]="",choice;
while(1){
clrscr();
fflush(stdin);
printf("Do you want to Compress or Decompress[C/D]\n");
scanf("%c",&choice);
switch(choice){
case 'C':
printf("Enter the name of the file to be compressed\n");
scanf("%s",fname);
fp=fopen(fname,"rb");
printf("Enter the name of the output file\n");
scanf("%s",fname1);
fq=fopen(fname1,"wb");
t1=fgetc(fp);
t2=t1;
while((t2=fgetc(fp))!=EOF){
if(t1==t2){
i++;
}
else{
fputc((char)t1,fq);
fprintf(fq,"%d",i);
fputc('#',fq);
i=1;
t1=t2;
}
}
fputc((char)t1,fq);
fprintf(fq,"%d",i);
fputc('#',fq);
fseek(fp,0,SEEK_END);
fseek(fq,0,SEEK_END);
printf("\nFile Compression Succesful\n");
printf("\nSize of original file(%s) is %ld bytes\n",fname,ftell(fp));
printf("\nSize of Compressed file(%s) is %ld bytes\n",fname1,ftell(fq));
fclose(fp);
fclose(fq);
getch();
break;
case 'D':
printf("Enter the name of the compressed file\n");
scanf("%s",fname);
printf("Enter the name of the output file\n");
scanf("%s",fname1);
fp=fopen(fname,"rb");
fr=fopen(fname1,"wb");
while((t2=fgetc(fp))!=EOF){
i=0;
while((t1=getc(fp))!='#'){
str[i]=t1;
i++;
}
str[i]='\0';
repeat=atoi(str);
for(i=1;i<=repeat;i++){
fprintf(fr,"%c",t2);
}
}
fseek(fp,0,SEEK_END);
fseek(fr,0,SEEK_END);
printf("\nFile Decompression Succesful\n");
printf("\nSize of Compressed file(%s) is %ld bytes\n",fname,ftell(fp));
printf("\nSize of Decompressed file(%s) is %ld bytes\n",fname1,ftell(fr));
fclose(fp);
fclose(fr);
getch();
break;
default:
exit(0);
}
}
getch();
return 0;
}