Stable working of encoding

This commit is contained in:
Priyatham Sai Chand 2020-08-14 21:59:32 +05:30
parent 4d73945ce9
commit 067ac3272a
2 changed files with 78 additions and 73 deletions

View file

@ -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--) for(int j = n -1; j >= 0;j--)
{ {
if(k > 8*mSize)break;
else
{
red->bin[7-j] = msg_bin[k]; red->bin[7-j] = msg_bin[k];
k++; k++;
}
} }
for(int j = n -1; j >= 0;j--) for(int j = n -1; j >= 0;j--)
{ {
if(k > 8*mSize)break;
else
{
green->bin[7-j] = msg_bin[k]; green->bin[7-j] = msg_bin[k];
k++; k++;
}
} }
@ -116,6 +113,7 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n)
else else
{ {
blue->bin[7-j] = msg_bin[k]; blue->bin[7-j] = msg_bin[k];
k++; k++;
} }
@ -124,14 +122,6 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n)
p->r = bind2dec(red); p->r = bind2dec(red);
p->g = bind2dec(green); p->g = bind2dec(green);
p->b = bind2dec(blue); p->b = bind2dec(blue);
//img ->p[row][column] = &p;
}
for(int s = 0; s < 8*mSize; s++)
{
printf("%d",msg_bin[s]);
@ -140,9 +130,6 @@ struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n)
return img; return img;
} }
@ -152,7 +139,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize)
srand(secret); srand(secret);
int width,rando,sum = 0,row,column,i = 0,k = 0,p = 0; int width,rando,sum = 0,row,column,i = 0,k = 0,p = 0;
int no_of_pixels = (int)ceil(mSize*8/(3*n)); 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) while(i <= no_of_pixels)
{ {
@ -162,7 +149,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize)
row = sum / (width); row = sum / (width);
column = sum - (row * width); column = sum - (row * width);
printf("%d %d\n",row,column);
//get pixel //get pixel
struct pixel * p = &(img->p[row][column]); struct pixel * p = &(img->p[row][column]);
struct conv * red = dec2bind(p->r); 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]; buffer[k] =red->bin[7-j];
k++; k++;
@ -181,6 +169,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize)
{ {
buffer[k] =green->bin[7-j]; buffer[k] =green->bin[7-j];
k++; k++;
@ -189,6 +178,7 @@ char * decode(struct PPM * img, unsigned int secret,int n,int mSize)
{ {
buffer[k] =blue->bin[7-j]; buffer[k] =blue->bin[7-j];
k++; 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]; struct conv * msg_bin[mSize];
int msg_no[mSize]; int msg_no[mSize];
char * str = (char *)malloc(sizeof(char) * mSize); char * str = (char *)malloc(sizeof(char) * 7);
struct conv * test; struct conv * test;
int temp[8],temp1; int temp[8],t = 0;
/*for(int r = 0; r < 8; r++) 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); for(int u = 0;u < 9; u++)
printf("%d",temp1);*/ {
return "asdf"; str[u] = temp1[u];
}
return str;
} }

66
steg.c
View file

@ -10,11 +10,11 @@ int main(int argc, char ** argv)
else else
{ {
FILE * fin = fopen(argv[2], "r"); /* FILE * fin = fopen(argv[2], "r");
struct PPM * img = getPPM(fin); struct PPM * img = getPPM(fin);
showPPM(img); showPPM(img);*/
//if(argv[1][0] == 'e')encodeFile(argc,argv); if(argv[1][0] == 'e')encodeFile(argc,argv);
//if(argv[1][0] == 'd')decodeFile(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){ void showPPM (struct PPM * img){
//print type //print type
printf("%s\n", img->type); printf("%s", img->type);
//print text //print text
int j; int j;
struct comment * t = img->comment; struct comment * t = img->comment;
printf("%s\n", img->comment->comment); printf("%s", img->comment->comment);
for(j = 1; j<img->cn; j++){ for(j = 1; j<img->cn; j++){
t = t->next; t = t->next;
printf("%s\n", t->comment); printf("%s", t->comment);
} }
//print general info //print general info
printf("%d\n", img->width); printf("%d %d\n", img->width, img->height);
printf("%d\n", img->height);
printf("%d\n", img->max); printf("%d\n", img->max);
//print pixels //print pixels
@ -54,14 +53,15 @@ void showPPM (struct PPM * img){
/* Utilizes the above readLine function to find the comments */ /* Utilizes the above readLine function to find the comments */
void getText(FILE * fin, struct PPM * img){ void getText(FILE * fin, struct PPM * img){
char ch = fgetc(fin); char ch = fgetc(fin);
while(ch == '#') //lines beginning with "#" character while(ch == '#') //lines beginning with "#" character
{ {
ch = fgetc(fin);
//ungetc(ch, fin);
/* //get comment ungetc(ch, fin);
//char * line;
//get comment
//add comment to linked list //add comment to linked list
@ -69,9 +69,9 @@ void getText(FILE * fin, struct PPM * img){
{ {
//no previous comments //no previous comments
struct comment * current = img->comment;
current = (struct comment * )malloc(sizeof(struct comment)); img->comment = (struct comment * )malloc(sizeof(struct comment));
fgets(current->comment,100,fin); fgets(img->comment->comment,100,fin);
img->cn = 1; img->cn = 1;
} }
else else
@ -83,10 +83,10 @@ void getText(FILE * fin, struct PPM * img){
for(j=1;j<img->cn;j++) for(j=1;j<img->cn;j++)
{ {
current = current->next; current = current->next;
}
current->next = (struct comment *)malloc(sizeof(struct comment)); current->next = (struct comment *)malloc(sizeof(struct comment));
fgets(current->next->comment,100,fin); fgets(current->next->comment,100,fin);
}
img->cn = img->cn + 1; img->cn = img->cn + 1;
} }
@ -96,7 +96,7 @@ void getText(FILE * fin, struct PPM * img){
} }
//Restore to starting position //Restore to starting position
ungetc(ch, fin);*/ ungetc(ch, fin);
} }
void getPixels(FILE * fin, struct PPM * img){ void getPixels(FILE * fin, struct PPM * img){
@ -123,10 +123,17 @@ void getPixels(FILE * fin, struct PPM * img){
/* Return PPM image file from fin */ /* Return PPM image file from fin */
struct PPM * getPPM(FILE * fin){ struct PPM * getPPM(FILE * fin){
struct PPM * img = malloc(sizeof(struct PPM)); struct PPM * img = malloc(sizeof(struct PPM));
char ch;
//Type //Type
img->type = malloc(2*sizeof(char)); ch = fgetc(fin);
img->type = "P3"; img->type = malloc(4*sizeof(char));
if( ch == 'P')
{
ungetc(ch, fin);
fgets(img->type,4,fin);
}
img->cn = 0; img->cn = 0;
getText(fin, img); getText(fin, img);
@ -157,7 +164,7 @@ void writePPM(FILE * fin, struct PPM * img, const char *fileName){
fclose(pfile); fclose(pfile);
} }
void encodeFile(int argc, char ** argv){ void encodeFile(int argc, char ** argv){
char txt[PPM_MAXMESS]; char txt[PPM_MAXMESS],secret[PPM_MAXMESS];
int n; int n;
FILE * fin = fopen(argv[2], "r"); FILE * fin = fopen(argv[2], "r");
if(fin == NULL){ if(fin == NULL){
@ -169,16 +176,16 @@ void encodeFile(int argc, char ** argv){
//get txt //get txt
printf("Message to Encode-> "); printf("Message to Encode-> ");
fgets(txt, PPM_MAXMESS, stdin); gets(txt);
printf("Enter no. of LSBs ( 3 to 21)"); printf("Enter no. of LSBs ( 3 to 21)]-> ");
scanf("%d",&n); scanf("%d",&n);
printf("Enter secret message -> ");
gets(secret);
printf("encoding in progress...\n"); printf("encoding in progress...\n");
//encode ppm //encode ppm
img = encoder(img, txt,1234,n); img = encoder(img, txt,1234,n);
printf("test");
showPPM(img);
//output ppm //output ppm
//fputc(showPPM, outFin); //fputc(showPPM, outFin);
writePPM(fin, img, "modified.ppm"); writePPM(fin, img, "modified.ppm");
@ -201,8 +208,9 @@ void decodeFile(int argc, char ** argv){
//get encoded txt //get encoded txt
char * str = decode(img,1234,3,9); char * str = decode(img,1234,3,9);
for(int i = 0 ; i < 9;i++)printf("%c",str[i]);
//print txt //print txt
printf("%s\n", str); //printf("%s", str);
} }