Adding files

This commit is contained in:
Priyatham-sai-chand 2020-08-09 23:09:42 +05:30 committed by GitHub
parent e4c1f0c890
commit 4d73945ce9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 760452 additions and 0 deletions

243
encode.c Normal file
View file

@ -0,0 +1,243 @@
#include "ppm.h"
struct conv * dec2bind(int num)
{
int i = 7;
struct conv * obj = (struct conv *)malloc(sizeof(struct conv));
for(int j = 0;j < 8; j++)
obj->bin[j] = 0;
while(num > 0)
{
if(num % 2 == 0)
obj->bin[i] = 0;
else
obj->bin[i] = 1;
i--;
num = num / 2;
}
return obj;
}
int bind2dec(struct conv * obj)
{
int n = 0;
int i;
for (i = 0; i < 8; ++i) {
n <<= 1;
n += obj->bin[i] - 0;
}
return n;
}
int bind2dectest(int arr[])
{
int n = 0;
int i;
for (i = 0; i < 8; ++i) {
n <<= 1;
n += arr[i] - 0;
}
return n;
}
struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n)
{
int mSize = strlen(message);
srand(secret);
int width,rando,sum = 0,row,column,o = 0,i = 0,k = 0,t = 0;
int no_of_pixels = (int)ceil(mSize*8/(3*n));
int msg_bin[8*mSize];
struct conv * msg_char[mSize];
for(int k = 0; k < mSize; k++)
{
msg_char[k] = dec2bind(message[k]);
for(int m = 0; m < 8; m++)
{
msg_bin[o] = msg_char[k]->bin[m];
o++;
}
}
while( i <= no_of_pixels)
{
rando = rand()%100;
sum += rando;
row = sum / (width);
column = sum - (row * width);
//get pixel
struct pixel * p = &(img->p[row][column]);
struct conv * red = dec2bind(p->r);
struct conv * green = dec2bind(p->g);
struct conv * blue = dec2bind(p->b);
i++;
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++;
}
}
for(int j = n -1; j >= 0;j--)
{
if(k > 8*mSize)break;
else
{
blue->bin[7-j] = msg_bin[k];
k++;
}
}
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;
}
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];
while(i <= no_of_pixels)
{
rando = rand()%100;
sum += rando;
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);
struct conv * green = dec2bind(p->g);
struct conv * blue = dec2bind(p->b);
i++;
for(int j = n -1; j >= 0;j--)
{
buffer[k] =red->bin[7-j];
k++;
}
for(int j = n -1; j >= 0;j--)
{
buffer[k] =green->bin[7-j];
k++;
}
for(int j = n -1; j >= 0;j--)
{
buffer[k] =blue->bin[7-j];
k++;
}
}
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);
struct conv * test;
int temp[8],temp1;
/*for(int r = 0; r < 8; r++)
{
temp[r] = buffer[r];
}
temp1 = bind2dec1(temp);
printf("%d",temp1);*/
return "asdf";
}

536069
home-cat.ppm Normal file

File diff suppressed because it is too large Load diff

BIN
obj.exe Normal file

Binary file not shown.

67
ppm.h Normal file
View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include<math.h>
#define PPM1 'P'
#define PPM2 '3'
#define PPM_FORMAT (PPM1 * 255 + PPM2)
#define PPM_TYPE PPM_FORMAT
#define PPM_MAXVAL 255
#define PPM_MAXMESS 100 //defines max for encoding and decoding
#define BMAX 100 //defines max for buffer
/* Typedef struct holds integer rgb values within type identifier pixel */
typedef struct pixel{
int r, g, b;
} pixel;
/* text struct holds strings of comments using LLDS */
typedef struct comment {
char comment[BMAX];
struct comment * next;
}comment;
struct conv
{
int bin[8];
};
/* PART A - Struct created to hold PPM Images(Uses a 2D array for rows and cols) */
struct PPM {
char * type;
struct comment * comment;
int cn;
int width, height, max; //Width, height and max size
struct pixel ** p;
};
/* Declaration of functions. Implementations made in ppmfun.c */
char * readLine(FILE * fin);
void showPPM(struct PPM * i);
void getText(FILE * fin, struct PPM * i);
void getPixels(FILE * fin, struct PPM * i);
struct PPM * getPPM(FILE * fin);
struct PPM * encoder(struct PPM * img, char * message,unsigned int secret,int n);
struct conv * dec2bind(int num);
int bind2dec(struct conv * obj);
void writePPM(FILE * fin ,struct PPM * i, const char *fileName);
char * decode(struct PPM * img, unsigned int secret,int n,int mSize);
void encodeFile(int argc, char ** argv);
void decodeFile(int argc, char ** argv);

1
star.ppm Normal file
View file

@ -0,0 +1 @@
File not found!

223864
star_ascii.ppm Normal file

File diff suppressed because it is too large Load diff

BIN
steg Normal file

Binary file not shown.

208
steg.c Normal file
View file

@ -0,0 +1,208 @@
#include "ppm.h"
int main(int argc, char ** argv)
{
if(argc < 3)
{
printf("wrong number of arguments");
}
else
{
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);
}
}
void showPPM (struct PPM * img){
//print type
printf("%s\n", img->type);
//print text
int j;
struct comment * t = img->comment;
printf("%s\n", img->comment->comment);
for(j = 1; j<img->cn; j++){
t = t->next;
printf("%s\n", t->comment);
}
//print general info
printf("%d\n", img->width);
printf("%d\n", img->height);
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){
char ch = fgetc(fin);
while(ch == '#') //lines beginning with "#" character
{
ch = fgetc(fin);
//ungetc(ch, fin);
/* //get comment
//char * line;
//add comment to linked list
if(img->cn == 0)
{
//no previous comments
struct comment * current = img->comment;
current = (struct comment * )malloc(sizeof(struct comment));
fgets(current->comment,100,fin);
img->cn = 1;
}
else
{
//previous comments
int j;
struct comment * current = img->comment;
for(j=1;j<img->cn;j++)
{
current = current->next;
current->next = (struct comment *)malloc(sizeof(struct comment));
fgets(current->next->comment,100,fin);
}
img->cn = img->cn + 1;
}
//move to next line
ch = fgetc(fin);
}
//Restore to starting position
ungetc(ch, fin);*/
}
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));
//Type
img->type = malloc(2*sizeof(char));
img->type = "P3";
img->cn = 0;
getText(fin, img);
//Size
fscanf (fin, "%d", &img->width);
fscanf (fin, "%d", &img->height);
fscanf (fin, "%d", &img->max);
//Pixels
getPixels(fin, img);
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);
fprintf(pfile, "P3\n%s%d %d\n255\n",img->comment,img->width, img->height);
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){
char txt[PPM_MAXMESS];
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
printf("Message to Encode-> ");
fgets(txt, PPM_MAXMESS, stdin);
printf("Enter no. of LSBs ( 3 to 21)");
scanf("%d",&n);
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");
}
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);
}
//get files
struct PPM * img = getPPM(i1);
//get encoded txt
char * str = decode(img,1234,3,9);
//print txt
printf("%s\n", str);
}