#include #include #include #include #include #include void CloseF(); void CodingText(); void HeaderInf(); int LogicScreen(); void Coding(); void Decoding(); void Copyring(); FILE *InF, *OutF, *TextF; int numbyteIn=0,numbyteOut=0,numbyteText=0,power=3,size; char what,type[3]; char InName[250],OutName[250],TextName[250]; ///////////////////////*Openning files*////////////////////// int main (void) { int count=-1,j; char ch; puts("Coding / Decoding"); while(count==-1){ what = getchar(); what = toupper(what); switch (what) { case 'C': what=1; count=1; break; case 'D': what=0; count=1; break; default: continue;} } puts("Write name of start picture file"); gets(InName); gets(InName); if(what) { puts("Write name of end picture file"); gets(OutName); puts("Write name of text file"); gets(TextName);} if((InF = fopen(InName, "rb")) == NULL){ printf("File '%s' not found\n", InName); getchar(); CloseF(InF,OutF,TextF);} if(what){ if((OutF = fopen(OutName, "wb+")) == NULL){ printf("Oshibka sozdania fila '%s'\n", OutName); CloseF(InF,OutF,TextF); return 0;} if((TextF = fopen(TextName, "rb")) == NULL){ printf("File '%s' not found\n", TextName); CloseF(InF,OutF,TextF);; return 0;} printf("Insert type of file for coding: "); gets(type); what = 1; } else{ puts("Write name of end file without type"); j=0; gets(TextName); /*while(TextName[j]!='.'){ ch = TextName[j++] = getch(); puts(ch);}*/} printf("Insert pow of coding 1 bit on byte or 2 bits on byte: "); count=-1; while(count!=1){ count = getchar(); switch (count) { case '1': power = 1; count = 1; break; case '2': power = 2; count = 1; break; default: count = -1; continue;} } HeaderInf(InF,OutF,TextF); count = LogicScreen(InF); if (what) Coding(InF,OutF,TextF,count); else Decoding(OutF,TextF,count); CloseF(InF,OutF,TextF); return 0; } ///////////////////////*Closing Files*/////////////////////// void CloseF(FILE *InF, FILE *OutF, FILE *TextF) { fclose(InF); if(what) fclose(OutF); fclose(TextF); puts("Operacia vipolnena"); getch(); exit (0); } /////////////////*Scanning for Signature*//////////////////// void HeaderInf(FILE *InF, FILE *OutF, FILE *TextF) { int gifbyte; char Signature[3]="GIF"; char Version1[3]="87a"; char Version2[3]="89a"; fseek(InF,numbyteIn,SEEK_SET); //Closing programm if file not GIF 87a or 89a for(numbyteIn=0; numbyteIn<3; numbyteIn++) if((gifbyte = fgetc(InF))!= Signature[numbyteIn]) { puts("This file hasn't GIF format"); CloseF(InF,OutF,TextF); } for(numbyteIn=0; numbyteIn<3; numbyteIn++){ gifbyte = fgetc(InF); if(gifbyte == Version1[numbyteIn] || Version2[numbyteIn]) continue; else{ puts("This file hasn't GIF format"); CloseF(InF,OutF,TextF);} } numbyteIn=ftell(InF); return; } /////////////////////*Logigic Screen*//////////////////////// int LogicScreen(FILE *InF) { int i,count=0; unsigned int mask[8]={128,64,32,16,8,4,2,1}; int wight=0, height=0, PackedFields, Background; double PixelAspectRatio; fseek(InF,numbyteIn,SEEK_SET); wight = fgetc(InF); //printf("%i ", wight); if(!wight){ wight = fgetc(InF);// printf("%i ", wight); } numbyteIn+=2; fseek(InF,numbyteIn,SEEK_SET); height = fgetc(InF); //printf("%i ", height); if(!wight){ height = fgetc(InF); //printf("%i ", height); } numbyteIn+=2; fseek(InF,numbyteIn,SEEK_SET); PackedFields = fgetc(InF); //printf("%i", PackedFields); Background = fgetc(InF); //printf("%i", Background ); PixelAspectRatio = fgetc(InF); //printf("%f", PixelAspectRatio ); //printf("Size of your picture is %i x %i\n", wight,height); numbyteIn = ftell(InF); if((PackedFields & mask[0]) == mask[0]) puts("Global Color Table is using"); else puts("Global Color Table isn't using"); for(i=1; i<4;i++) if((PackedFields & mask[i]) == mask[i]) count += mask[i]; //printf("Number of bits on colors dpi %i\n", count-1); count=0; if((PackedFields & mask[4]) == mask[4]) puts("Global Color Table is sorting"); else puts("Global Color Table isn't sorting"); for(i=5; i<8; i++) if((PackedFields & mask[i]) == mask[i]) count += mask[i]; count = (int)pow(2,count+1); printf("Number colors on byte %i\n", count); if(!PixelAspectRatio) PixelAspectRatio = 1; PixelAspectRatio += 15; PixelAspectRatio /= 64; printf("Attitutde widht pixel to his height is %.2f\n", PixelAspectRatio); return 3*count; } /////////////////*Coding text into picture*////////////////// void Coding(FILE *InF, FILE *OutF, FILE *TextF, int count) { char bufer[8],massiv[8]; int ch,i,j=0,byte,k,m; unsigned char mask[4]={254,253,1,2}; if(what){ fseek(TextF,numbyteText,SEEK_SET); ch = fgetc(TextF); while(ch != EOF){ ch = fgetc(TextF); size++;} printf("Size of text file - %i\n", size); ch=32; if(size/power > 92){ puts("This file couldn't coding in this picture! Operation has been breaked."); getch(); CloseF(InF,OutF,TextF);} numbyteText = 0; } fseek(TextF,numbyteText,SEEK_SET); if(numbyteOut < numbyteIn){ fseek(InF,0,SEEK_SET); fseek(OutF,numbyteOut,SEEK_SET); while(numbyteOut < numbyteIn){ byte = fgetc(InF); fputc(byte,OutF); numbyteOut++;}} while(j<4){ if(j == 3) ch = (int)size; else ch = type[j]; for(i=7; i>=0; i--){ bufer[i] = ch % 2; ch /= 2;} m = 0; for(i=0; i<8/power; i++){ ch = byte = fgetc(InF); for(k=power; k>0; k--){ ch = bufer[m]; bufer[m++] = 0; if(ch == 0){ if(k == 1) byte = byte & mask[0]; else byte = byte & mask[1];} else{ if(k == 1) byte = byte | mask[2]; else byte = byte | mask[3];} } fputc(byte,OutF); } count--; j++;} while(count>0){ if((ch = fgetc(TextF)) == EOF) break; for(i=7; i>=0; i--){ bufer[i] = ch % 2; ch /= 2;} m = 0; for(i=0; i<8/power; i++){ ch = byte = fgetc(InF); for(k=power; k>0; k--){ ch = bufer[m]; bufer[m++] = 0; if(ch == 0){ if(k == 1) byte = byte & mask[0]; else byte = byte & mask[1];} else{ if(k == 1) byte = byte | mask[2]; else byte = byte | mask[3];} } fputc(byte,OutF); } count--;} numbyteIn = ftell(InF); numbyteOut = ftell(OutF); numbyteText = ftell(TextF); Copyring(InF,OutF,TextF); return; } /////////////////*Decoding from picture*//////////////////////// void Decoding(FILE *OutF, FILE *TextF, int count) { int bufer[8]; int i,j,byte,g=0; unsigned char mask[2]={2,1}; size=256; fseek(InF,numbyteIn,SEEK_SET); while(count>0){ if(power == 2){ for(i=0; i<8; ){ byte = getc(InF); bufer[i++] = (byte & mask[0])/2; bufer[i++] = byte & mask[1];} byte = 0; j=7;count--; for(i=0; i<8; i++){ byte += bufer[i] * (int)pow(2,j); j--;} } if(power == 1){ for(i=0; i<8; i++){ byte = getc(InF); bufer[i] = byte & mask[1];} byte = 0; j=7;count--; for(i=0; i<8; i++){ byte += bufer[i] * (int)pow(2,j); j--;} } if(g == 3 || g == 5) g++; if(g < 3){ type[g] = byte; g++;} if(g == 4){ g = 0; size = byte; //count = 0; printf("type of file - %s, size of file - %i\n", type,size); /*g = strlen(TextName); for(i=0; i<3; i++) TextName[g++] = type[i];*/ if((TextF = fopen(TextName, "wb+")) == NULL && what){ printf("Oshibka sozdania fila '%s'\n", OutName); getchar(); CloseF(InF,OutF,TextF);} fseek(TextF,numbyteText,SEEK_SET); g = 5;} if(g>5){ fputc(byte,TextF); // count++; } } numbyteText = ftell(TextF); numbyteIn = ftell(InF); CloseF(InF,OutF,TextF); return; } //////////////*copering*///////////////////////////// void Copyring(FILE *InF, FILE *OutF, FILE *TextF) { int byte; fseek(InF,numbyteIn,SEEK_SET); while((byte=fgetc(InF)) != EOF){ fputc(byte,OutF);} CloseF(InF,OutF,TextF); return; }