From 067ac3272a84e482475c4e64df70711d13ae338b Mon Sep 17 00:00:00 2001 From: Priyatham Sai Chand Date: Fri, 14 Aug 2020 21:59:32 +0530 Subject: [PATCH] Stable working of encoding --- encode.c | 75 +++++++++++++++++++++++++++---------------------------- steg.c | 76 +++++++++++++++++++++++++++++++------------------------- 2 files changed, 78 insertions(+), 73 deletions(-) diff --git a/encode.c b/encode.c index bb2c399..5048638 100644 --- a/encode.c +++ b/encode.c @@ -87,26 +87,23 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n) for(int j = n -1; j >= 0;j--) { - if(k > 8*mSize)break; - else - { red->bin[7-j] = msg_bin[k]; + k++; - } + } for(int j = n -1; j >= 0;j--) { - if(k > 8*mSize)break; - else - { + green->bin[7-j] = msg_bin[k]; + k++; - } + } @@ -116,6 +113,7 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n) else { blue->bin[7-j] = msg_bin[k]; + k++; } @@ -124,25 +122,14 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n) p->r = bind2dec(red); p->g = bind2dec(green); p->b = bind2dec(blue); - //img ->p[row][column] = &p; + } - for(int s = 0; s < 8*mSize; s++) - { - printf("%d",msg_bin[s]); - - - - - } - + return img; - - - - + } @@ -152,7 +139,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) srand(secret); int width,rando,sum = 0,row,column,i = 0,k = 0,p = 0; int no_of_pixels = (int)ceil(mSize*8/(3*n)); - int buffer[8*mSize]; + char * buffer = malloc(sizeof(char) * 8 * mSize); while(i <= no_of_pixels) { @@ -162,7 +149,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) row = sum / (width); column = sum - (row * width); - printf("%d %d\n",row,column); + //get pixel struct pixel * p = &(img->p[row][column]); struct conv * red = dec2bind(p->r); @@ -173,6 +160,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) { buffer[k] =red->bin[7-j]; + k++; @@ -181,6 +169,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) { buffer[k] =green->bin[7-j]; + k++; @@ -189,6 +178,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) { buffer[k] =blue->bin[7-j]; + k++; @@ -196,32 +186,39 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize) } - for(int s = 0; s < 8*mSize; s++) - { - - printf("%d",buffer[s]); - - } - - struct conv * msg_bin[mSize]; int msg_no[mSize]; - char * str = (char *)malloc(sizeof(char) * mSize); + char * str = (char *)malloc(sizeof(char) * 7); struct conv * test; - int temp[8],temp1; - /*for(int r = 0; r < 8; r++) + int temp[8],t = 0; + int temp1[mSize]; + for(int r = 0; r < mSize; r++) { - temp[r] = buffer[r]; + for(int s = 0; s < 8; s++) + { + temp[s] = buffer[t]; + t++; + + + } + temp1[r] = bind2dectest(temp); } - temp1 = bind2dec1(temp); - printf("%d",temp1);*/ + for(int u = 0;u < 9; u++) + { + + str[u] = temp1[u]; + + + } + + - return "asdf"; + return str; } diff --git a/steg.c b/steg.c index b49fb99..cc98bb4 100644 --- a/steg.c +++ b/steg.c @@ -10,11 +10,11 @@ int main(int argc, char ** argv) else { - FILE * fin = fopen(argv[2], "r"); +/* FILE * fin = fopen(argv[2], "r"); struct PPM * img = getPPM(fin); - showPPM(img); - //if(argv[1][0] == 'e')encodeFile(argc,argv); - //if(argv[1][0] == 'd')decodeFile(argc,argv); + showPPM(img);*/ + if(argv[1][0] == 'e')encodeFile(argc,argv); + if(argv[1][0] == 'd')decodeFile(argc,argv); } @@ -25,20 +25,19 @@ int main(int argc, char ** argv) void showPPM (struct PPM * img){ //print type - printf("%s\n", img->type); + printf("%s", img->type); //print text int j; struct comment * t = img->comment; - printf("%s\n", img->comment->comment); + printf("%s", img->comment->comment); for(j = 1; jcn; j++){ t = t->next; - printf("%s\n", t->comment); + printf("%s", t->comment); } //print general info - printf("%d\n", img->width); - printf("%d\n", img->height); + printf("%d %d\n", img->width, img->height); printf("%d\n", img->max); //print pixels @@ -54,14 +53,15 @@ void showPPM (struct PPM * img){ /* Utilizes the above readLine function to find the comments */ void getText(FILE * fin, struct PPM * img){ - char ch = fgetc(fin); + + char ch = fgetc(fin); while(ch == '#') //lines beginning with "#" character { - ch = fgetc(fin); - //ungetc(ch, fin); + + ungetc(ch, fin); -/* //get comment - //char * line; + //get comment + //add comment to linked list @@ -69,9 +69,9 @@ void getText(FILE * fin, struct PPM * img){ { //no previous comments - struct comment * current = img->comment; - current = (struct comment * )malloc(sizeof(struct comment)); - fgets(current->comment,100,fin); + + img->comment = (struct comment * )malloc(sizeof(struct comment)); + fgets(img->comment->comment,100,fin); img->cn = 1; } else @@ -79,15 +79,15 @@ void getText(FILE * fin, struct PPM * img){ //previous comments int j; struct comment * current = img->comment; - + for(j=1;jcn;j++) { current = current->next; - current->next = (struct comment *)malloc(sizeof(struct comment)); - fgets(current->next->comment,100,fin); + } - - + current->next = (struct comment *)malloc(sizeof(struct comment)); + fgets(current->next->comment,100,fin); + img->cn = img->cn + 1; } @@ -96,7 +96,7 @@ void getText(FILE * fin, struct PPM * img){ } //Restore to starting position - ungetc(ch, fin);*/ + ungetc(ch, fin); } void getPixels(FILE * fin, struct PPM * img){ @@ -123,10 +123,17 @@ void getPixels(FILE * fin, struct PPM * img){ /* Return PPM image file from fin */ struct PPM * getPPM(FILE * fin){ struct PPM * img = malloc(sizeof(struct PPM)); - + char ch; //Type - img->type = malloc(2*sizeof(char)); - img->type = "P3"; + ch = fgetc(fin); + img->type = malloc(4*sizeof(char)); + if( ch == 'P') + { + ungetc(ch, fin); + fgets(img->type,4,fin); + + + } img->cn = 0; getText(fin, img); @@ -157,7 +164,7 @@ void writePPM(FILE * fin, struct PPM * img, const char *fileName){ fclose(pfile); } void encodeFile(int argc, char ** argv){ - char txt[PPM_MAXMESS]; + char txt[PPM_MAXMESS],secret[PPM_MAXMESS]; int n; FILE * fin = fopen(argv[2], "r"); if(fin == NULL){ @@ -169,16 +176,16 @@ void encodeFile(int argc, char ** argv){ //get txt printf("Message to Encode-> "); - fgets(txt, PPM_MAXMESS, stdin); - printf("Enter no. of LSBs ( 3 to 21)"); + gets(txt); + printf("Enter no. of LSBs ( 3 to 21)]-> "); scanf("%d",&n); - + printf("Enter secret message -> "); + gets(secret); printf("encoding in progress...\n"); //encode ppm img = encoder(img, txt,1234,n); - printf("test"); - showPPM(img); + //output ppm //fputc(showPPM, outFin); writePPM(fin, img, "modified.ppm"); @@ -201,8 +208,9 @@ void decodeFile(int argc, char ** argv){ //get encoded txt char * str = decode(img,1234,3,9); - + for(int i = 0 ; i < 9;i++)printf("%c",str[i]); + //print txt - printf("%s\n", str); + //printf("%s", str); }