2020-08-09 10:39:42 -07:00
|
|
|
#include "ppm.h"
|
2020-09-23 02:12:20 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(argc < 3)
|
|
|
|
|
{
|
|
|
|
|
printf("wrong number of arguments");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-14 09:29:32 -07:00
|
|
|
/* FILE * fin = fopen(argv[2], "r");
|
2020-08-09 10:39:42 -07:00
|
|
|
struct PPM * img = getPPM(fin);
|
2020-08-14 09:29:32 -07:00
|
|
|
showPPM(img);*/
|
|
|
|
|
if(argv[1][0] == 'e')encodeFile(argc,argv);
|
|
|
|
|
if(argv[1][0] == 'd')decodeFile(argc,argv);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void showPPM (struct PPM * img){
|
|
|
|
|
|
|
|
|
|
//print type
|
2020-08-14 09:29:32 -07:00
|
|
|
printf("%s", img->type);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
//print text
|
|
|
|
|
int j;
|
|
|
|
|
struct comment * t = img->comment;
|
2020-08-14 09:29:32 -07:00
|
|
|
printf("%s", img->comment->comment);
|
2020-08-09 10:39:42 -07:00
|
|
|
for(j = 1; j<img->cn; j++){
|
|
|
|
|
t = t->next;
|
2020-08-14 09:29:32 -07:00
|
|
|
printf("%s", t->comment);
|
2020-08-09 10:39:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//print general info
|
2020-08-14 09:29:32 -07:00
|
|
|
printf("%d %d\n", img->width, img->height);
|
2020-08-09 10:39:42 -07:00
|
|
|
printf("%d\n", img->max);
|
|
|
|
|
|
|
|
|
|
//print pixels
|
|
|
|
|
int k,l;
|
|
|
|
|
for(k=0; k<img->height; k++){
|
|
|
|
|
for(l=0; l<img->width; l++){
|
|
|
|
|
struct pixel p = img->p[k][l];
|
|
|
|
|
printf("%i %i %i\n", p.r, p.g, p.b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Utilizes the above readLine function to find the comments */
|
|
|
|
|
void getText(FILE * fin, struct PPM * img){
|
2020-08-14 09:29:32 -07:00
|
|
|
|
|
|
|
|
char ch = fgetc(fin);
|
2020-08-09 10:39:42 -07:00
|
|
|
while(ch == '#') //lines beginning with "#" character
|
|
|
|
|
{
|
2020-08-14 09:29:32 -07:00
|
|
|
|
|
|
|
|
ungetc(ch, fin);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
2020-08-14 09:29:32 -07:00
|
|
|
//get comment
|
|
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//add comment to linked list
|
|
|
|
|
if(img->cn == 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//no previous comments
|
2020-08-14 09:29:32 -07:00
|
|
|
|
|
|
|
|
img->comment = (struct comment * )malloc(sizeof(struct comment));
|
|
|
|
|
fgets(img->comment->comment,100,fin);
|
2020-08-09 10:39:42 -07:00
|
|
|
img->cn = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//previous comments
|
|
|
|
|
int j;
|
|
|
|
|
struct comment * current = img->comment;
|
2020-08-14 09:29:32 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
for(j=1;j<img->cn;j++)
|
|
|
|
|
{
|
|
|
|
|
current = current->next;
|
2020-08-14 09:29:32 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
}
|
2020-08-14 09:29:32 -07:00
|
|
|
current->next = (struct comment *)malloc(sizeof(struct comment));
|
|
|
|
|
fgets(current->next->comment,100,fin);
|
|
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
img->cn = img->cn + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//move to next line
|
|
|
|
|
ch = fgetc(fin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Restore to starting position
|
2020-08-14 09:29:32 -07:00
|
|
|
ungetc(ch, fin);
|
2020-08-09 10:39:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getPixels(FILE * fin, struct PPM * img){
|
|
|
|
|
|
|
|
|
|
//allocate rows
|
|
|
|
|
img->p = (struct pixel ** ) malloc(sizeof(struct pixel *) * img->height);
|
|
|
|
|
|
|
|
|
|
int j,k;
|
|
|
|
|
for(j=0;j<img->height;j++)
|
|
|
|
|
{
|
|
|
|
|
//allocate columns
|
|
|
|
|
img->p[j] = (struct pixel * ) malloc(sizeof(struct pixel) * img->width);
|
|
|
|
|
for (k=0;k<img->width;k++)
|
|
|
|
|
{
|
|
|
|
|
//get pixels for each
|
|
|
|
|
fscanf (fin, "%d", &(img->p[j][k].r)); //get memory address
|
|
|
|
|
fscanf (fin, "%d", &(img->p[j][k].g));
|
|
|
|
|
fscanf (fin, "%d", &(img->p[j][k].b));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Return PPM image file from fin */
|
|
|
|
|
struct PPM * getPPM(FILE * fin){
|
|
|
|
|
struct PPM * img = malloc(sizeof(struct PPM));
|
2020-08-14 09:29:32 -07:00
|
|
|
char ch;
|
2020-08-09 10:39:42 -07:00
|
|
|
//Type
|
2020-08-14 09:29:32 -07:00
|
|
|
ch = fgetc(fin);
|
|
|
|
|
img->type = malloc(4*sizeof(char));
|
|
|
|
|
if( ch == 'P')
|
|
|
|
|
{
|
|
|
|
|
ungetc(ch, fin);
|
|
|
|
|
fgets(img->type,4,fin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
img->cn = 0;
|
|
|
|
|
getText(fin, img);
|
|
|
|
|
|
|
|
|
|
//Size
|
|
|
|
|
fscanf (fin, "%d", &img->width);
|
|
|
|
|
fscanf (fin, "%d", &img->height);
|
|
|
|
|
fscanf (fin, "%d", &img->max);
|
|
|
|
|
|
|
|
|
|
//Pixels
|
2020-09-23 02:12:20 -07:00
|
|
|
//getPixels(fin, img);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
void writePPM(FILE * fin, struct PPM * img, const char *fileName){
|
|
|
|
|
FILE *pfile = NULL;
|
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
|
|
pfile = fopen(fileName, "w");
|
|
|
|
|
//fprintf(pfile, "%d\n %s\n %d %d\n %d\n", i->type, i->text, i->w, i->h, i->max);
|
2020-09-23 02:12:20 -07:00
|
|
|
fprintf(pfile, "P3\n%s%d %d\n255\n",img->comment->comment,img->width, img->height);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
for(x=0; x<img->height; x++) {
|
|
|
|
|
for(y=0; y<img->width;y++){
|
|
|
|
|
struct pixel p = img->p[x][y];
|
|
|
|
|
fprintf(pfile, "%i %i %i \n", p.r, p.g, p.b); }
|
|
|
|
|
}
|
|
|
|
|
fclose(pfile);
|
|
|
|
|
}
|
|
|
|
|
void encodeFile(int argc, char ** argv){
|
2020-09-23 02:12:20 -07:00
|
|
|
char txt[PPM_MAXMESS],secret[PPM_MAXMESS],c;
|
2020-08-09 10:39:42 -07:00
|
|
|
int n;
|
|
|
|
|
FILE * fin = fopen(argv[2], "r");
|
|
|
|
|
if(fin == NULL){
|
|
|
|
|
fprintf(stderr," steg: Error - Unable to open the file '%s' .\n", argv[2]);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct PPM * img = getPPM(fin);
|
|
|
|
|
|
|
|
|
|
//get txt
|
2020-09-23 02:12:20 -07:00
|
|
|
printf("Message to Encode -> ");
|
|
|
|
|
scanf( "%100[^\n]", txt);
|
|
|
|
|
printf("Enter no. of LSBs ( 3 to 21)] -> ");
|
2020-08-09 10:39:42 -07:00
|
|
|
scanf("%d",&n);
|
2020-09-23 02:12:20 -07:00
|
|
|
/* discard */
|
|
|
|
|
while((c= getchar()) != '\n' && c != EOF);
|
2020-08-14 09:29:32 -07:00
|
|
|
printf("Enter secret message -> ");
|
2020-09-23 02:12:20 -07:00
|
|
|
scanf( "%100[^\n]", secret);
|
|
|
|
|
printf("encoding in progress...\n");
|
|
|
|
|
|
|
|
|
|
int len = strlen(txt);
|
|
|
|
|
|
|
|
|
|
char msg_secret[100];
|
|
|
|
|
|
|
|
|
|
//showPPM(img);
|
2020-08-09 10:39:42 -07:00
|
|
|
|
2020-09-23 02:12:20 -07:00
|
|
|
encode ppm
|
|
|
|
|
img = encoder(img,txt,msg_secret,n);
|
2020-08-14 09:29:32 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
//output ppm
|
2020-09-23 02:12:20 -07:00
|
|
|
fputc(showPPM, outFin);
|
2020-08-09 10:39:42 -07:00
|
|
|
writePPM(fin, img, "modified.ppm");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void decodeFile(int argc, char ** argv){
|
|
|
|
|
|
|
|
|
|
FILE * i1 = fopen(argv[2], "r");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(i1 == NULL){
|
|
|
|
|
printf("steg: Error - File '%s' could not be opened. \n", argv[2]);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2020-09-23 02:12:20 -07:00
|
|
|
char[100] secret;
|
|
|
|
|
printf("enter the message");
|
|
|
|
|
scanf("%s");
|
2020-08-09 10:39:42 -07:00
|
|
|
|
|
|
|
|
//get files
|
|
|
|
|
struct PPM * img = getPPM(i1);
|
|
|
|
|
|
2020-09-23 02:12:20 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
//get encoded txt
|
|
|
|
|
char * str = decode(img,1234,3,9);
|
2020-09-23 02:12:20 -07:00
|
|
|
//char * decrypt_txt = decrypter(str);
|
2020-08-14 09:29:32 -07:00
|
|
|
for(int i = 0 ; i < 9;i++)printf("%c",str[i]);
|
|
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
//print txt
|
2020-09-23 02:12:20 -07:00
|
|
|
|
2020-08-09 10:39:42 -07:00
|
|
|
}
|