-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembed1LSB.c
More file actions
183 lines (163 loc) · 4.98 KB
/
embed1LSB.c
File metadata and controls
183 lines (163 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Objectives : (a) Information embedding using 1-LSB substitution
* (b) Calculation of MSE and PSNR
* (c) Writing resultant 2D array in PGM file
*/
/*
* Compilation command : gcc -o embed1LSB embed1LSB.c
* Execution sequence : ./embed1LSB
*/
// importing library files
#include<stdio.h>
// defining bit size
#define bit_size 8
// declaring global variables
float MSE,PSNR;
// declaring functions
void embedInfo(FILE* matrixFile,FILE* secretFile,FILE* newMatrixFile);
char* oneLSB(char* binary,int secretInfo);
char* decToBin(int decimal);
int binToDec(char binary[]);
int extractNum(char c);
float power(float base,float expo);
float logten(float x);
void reverse(char* str);
// main function
int main()
{
// declaring file pointers for embedding secret information
FILE *secretFile,*matrixFile,*newMatrixFile;
// opening files
secretFile = fopen("SecretInfo128.txt","rb"); // contains secret information
matrixFile = fopen("CoverInfo1.txt","rb"); // contains the original matrix
newMatrixFile = fopen("NewImage.pgm","wb"); // contains the modified matrix
// checking if all files exist or not
if(secretFile==NULL || matrixFile==NULL)
printf("File(s) not found.");
else
{
// embedding information and calculating MSE and PSNR
MSE=0.0f;
PSNR=0.0f;
embedInfo(matrixFile,secretFile,newMatrixFile);
// closing files
fclose(newMatrixFile);
fclose(secretFile);
fclose(matrixFile);
//printing MSE and PSNR
printf("MSE for 1-LSB substitution = %.4f\n",MSE);
printf("PSNR for 1-LSB substitution = %.4f\n",PSNR);
}
return 0;
}
// function for embedding information, writing in new file and calculating MSE and PSNR
void embedInfo(FILE* matrixFile,FILE* secretFile, FILE* newMatrixFile)
{
fprintf(newMatrixFile,"P2\n"); // writing magic number to identify pgm file
fprintf(newMatrixFile,"%d %d\n",256,256); // writing matrix width and height to the file
fprintf(newMatrixFile,"255\n"); // writing the maximum gray value to the file
float squaredError=0.0f;
int i;
for(i=0; i<256; i++)
{
int j;
for(j=0; j<256; j++)
{
int secretInfo, matrixElement, newElement;
fscanf(secretFile,"%d",&secretInfo);
fscanf(matrixFile,"%d",&matrixElement);
newElement = binToDec(oneLSB(decToBin(matrixElement),secretInfo));
fprintf(newMatrixFile,"%d ",newElement);
squaredError+=power((matrixElement-newElement),2);
}
}
// calculating MSE and PSNR
int n=256*256;
MSE=squaredError/n;
int max=power(2,bit_size)-1;
//Simplifying the expression PSNR=10*logten(power(max,2)/MSE)
PSNR=20*logten(max)-10*logten(MSE);
}
// function for implementing 1-LSB substitution
char* oneLSB(char *binary, int secretInfo)
{
binary[bit_size-1] = (char)(secretInfo+48);
return binary;
}
// function for converting decimal number to binary form
char *decToBin(int decimal)
{
int i=0;
float temp;
//char* result = (char*)malloc((bit_size + 1) * sizeof(char));
static char result[bit_size+1];
while(decimal != 0 )
{
result[i] = (char)((decimal % 2)+48);
decimal /= 2;
i++;
}
result[bit_size] ='\0';
if(i<bit_size)
{
int k;
for(k=i; k<bit_size;k++)
result[k] = '0';
}
reverse(result);
return result;
}
// function for converting binary number to decimal form
int binToDec(char binary[])
{
int decimal=0;
int i;
for(i=0;i<bit_size;i++)
decimal += (extractNum(binary[i]) * power(2,bit_size-i-1));
return(decimal);
}
// function for extracting integer value from a character
int extractNum(char c)
{
int num;
if(c < 58 && c > 47)
num = c - '0';
else
num = c - 'A' + 10;
return(num);
}
// function to calculate power
float power(float base,float expo)
{
int i;
float result=1.0f;
for(i=0;i<expo;i++)
result*=base;
return result;
}
// function to calculate logarithm to the base 10 of x
float logten(float x)
{
float count=1.0f, curr=0.0f;
int i;
for(i=0;i<1000;i++)
{
curr+=power((x-1)/(x+1),count)/count;
count+=2.0f;
}
curr*=2.0f;
curr/=2.302585f;
return curr;
}
// function to reverse a character array
void reverse(char *str)
{
int j;
char ch='0';
for(j=0;j<bit_size/2;j++)
{
ch=str[j];
str[j]=str[bit_size-1-j];
str[bit_size-1-j]=ch;
}
}