-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbedding.c
333 lines (275 loc) · 7.46 KB
/
Embedding.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Name : Manas Pratim Biswas
Team 1
1. Nidhi Shah 002011001053(A1)
2. Dibyabrata Panja 002011001028(A1)
3. Manas Pratim Biswas 002011001025(A1)
4. Md Bosirullah Mondal 002011001030(A1)
In this assignment, we have embeded secret information using LSB
substitution. We have performed 1-LSB substitution and 2-LSB
substitution with OPAP. Then we have calculated the MSE and PSNR
for the above processes and ultimately written resultant the 2-D
array in a PGM file
*/
// Header files
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
// Global Variable Declaration
const int N = 256;
int originalArray[256][256]; // stores the original 2-D Matrix
int oneLSB_SecretArray[256][256]; // stores the 1-LSB Secret Array
int twoLSB_SecretArray[256][256][2]; // stores the 2-LSB Secret Array
int oneLSB_EmbeddedArray[256][256]; // stores the 1-LSB Embedded Array
int twoLSB_EmbeddedArray[256][256]; // stores the 2-LSB Embedded Array
int twoLSB_OPAPArray[256][256]; // stores the 2-LSB OPAP Array
int choiceOfEmbedding; // stores the choice of Embedding
int i,j; // used to run loops
double oneLSB_MSE; // stores the 1-LSB MSE
double oneLSB_PSNR; // stores the 1-LSB PSNR
double twoLSB_MSE; // stores the 2-LSB MSE
double twoLSB_PSNR; // stores the 2-LSB PSNR
double twoLSB_OPAP_MSE; // stores the 2-LSB OPAP MSE
double twoLSB_OPAP_PSNR; // stores the 2-:SB OPAP PSNR
int oneLSBSubstitution(int n, int bit)
{
if(n&1)
{
if(bit==1)
return n;
else
return n-1;
}
else
{
if(bit==1)
return n+1;
else
return n;
}
}
int twoLSBSubstitution(int n, int twoLSB)
{
int lastTwoBits = n%4;
n-=lastTwoBits;
n+=twoLSB;
return n;
}
int findTwoLSB(int secondBit, int firstBit)
{
if(secondBit==0&&firstBit==0)
return 0;
else if(secondBit==0&&firstBit==1)
return 1;
else if(secondBit==1&&firstBit==0)
return 2;
else
return 3;
}
int calculateOPAP(int vDash, int v)
{
int delta = vDash - v;
int vDoubleDash;
if(delta > 2 && delta < 4)
{
if(vDash >= 4)
vDoubleDash = vDash-4;
else
vDoubleDash = vDash;
}
else if(delta >= -4 && delta <= -2)
{
if(vDash < 256-4)
vDoubleDash = vDash + 4;
else
vDoubleDash = vDash;
}
else
{
vDoubleDash = vDash;
}
return vDoubleDash;
}
void readOriginalArray()
{
FILE *ptr;
ptr = NULL;
ptr = fopen("256x256_cover1.txt","r");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
fscanf(ptr,"%d",&originalArray[i][j]);
}
fclose(ptr);
}
void readOneLSBSecretInfo()
{
FILE *ptr;
ptr = NULL;
ptr = fopen("Secret_info1.txt","r");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
fscanf(ptr,"%d",&oneLSB_SecretArray[i][j]);
}
fclose(ptr);
}
void readTwoLSBSecretInfo()
{
FILE *ptr;
ptr = NULL;
ptr = fopen("Secret_info8.txt","r");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
fscanf(ptr,"%d",&twoLSB_SecretArray[i][j][1]);
fscanf(ptr,"%d",&twoLSB_SecretArray[i][j][0]);
}
}
fclose(ptr);
}
void embedOneLSB()
{
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
oneLSB_EmbeddedArray[i][j] = oneLSBSubstitution(originalArray[i][j],oneLSB_SecretArray[i][j]);
}
}
}
void embedTwoLSB()
{
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
int firstBit, secondBit;
firstBit = twoLSB_SecretArray[i][j][0];
secondBit = twoLSB_SecretArray[i][j][1];
int twoLSB = findTwoLSB(secondBit,firstBit);
twoLSB_EmbeddedArray[i][j] = twoLSBSubstitution(originalArray[i][j],twoLSB);
}
}
}
void embedTwoLSBwithOPAP()
{
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
int vDash = twoLSB_EmbeddedArray[i][j];
int v = originalArray[i][j];
twoLSB_OPAPArray[i][j] = calculateOPAP(vDash,v);
}
}
}
void oneLSB_Calculations()
{
int squaredError = 0;
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
int difference = originalArray[i][j]-oneLSB_EmbeddedArray[i][j];
squaredError += difference*difference;
}
}
oneLSB_MSE = (1.0*squaredError)/(N*N);
oneLSB_PSNR = 10.0*log10(1.0*(N-1)*(N-1)/oneLSB_MSE);
printf("The MSE for 1-LSB Substition is : %.2f\n",oneLSB_MSE);
printf("The PSNR for 1-LSB Substition is : %.2f\n",oneLSB_PSNR);
}
void twoLSB_Calculations()
{
int squaredError = 0;
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
int difference = originalArray[i][j]-twoLSB_EmbeddedArray[i][j];
squaredError += difference*difference;
}
}
twoLSB_MSE = (1.0*squaredError)/(N*N);
twoLSB_PSNR = 10.0*log10(1.0*(N-1)*(N-1)/twoLSB_MSE);
squaredError = 0;
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
int difference = originalArray[i][j]-twoLSB_OPAPArray[i][j];
squaredError += difference*difference;
}
}
twoLSB_OPAP_MSE = (1.0*squaredError)/(N*N);
twoLSB_OPAP_PSNR = 10.0*log10(1.0*(N-1)*(N-1)/twoLSB_OPAP_MSE);
printf("The MSE for 2-LSB Substition is : %.2f\n",twoLSB_MSE);
printf("The PSNR for 2-LSB Substition is : %.2f\n",twoLSB_PSNR);
printf("The MSE for 2-LSB Substition with OPAP is : %.2f\n",twoLSB_OPAP_MSE);
printf("The PSNR for 2-LSB Substition with OPAP is : %.2f\n",twoLSB_OPAP_PSNR);
}
void oneLSB_EmbeddedPGMFile()
{
FILE *pgm;
pgm = NULL;
int w = 256, h = 256;
pgm = fopen("pgmImg1.pgm","wb");
fprintf(pgm,"P2\n");
fprintf(pgm,"%d %d\n",w,h);
fprintf(pgm,"255\n");
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
fprintf(pgm,"%d ",oneLSB_EmbeddedArray[i][j]);
}
fprintf(pgm,"\n");
}
fclose(pgm);
}
void twoLSB_EmbeddedPGMFile()
{
FILE *pgm;
pgm = NULL;
int w = 256, h = 256;
pgm = fopen("pgmImg2.pgm","wb");
fprintf(pgm,"P2\n");
fprintf(pgm,"%d %d\n",w,h);
fprintf(pgm,"255\n");
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
fprintf(pgm,"%d ",twoLSB_OPAPArray[i][j]);
}
fprintf(pgm,"\n");
}
fclose(pgm);
}
int main()
{
readOriginalArray();
printf("Enter 1 for 1-LSB Replacement Embedding \n");
printf("Enter 2 for 2-LSB Replacement Embedding \n");
scanf("%d",&choiceOfEmbedding);
switch(choiceOfEmbedding)
{
case 1 :
readOneLSBSecretInfo();
embedOneLSB();
oneLSB_Calculations();
oneLSB_EmbeddedPGMFile();
break;
case 2 :
readTwoLSBSecretInfo();
embedTwoLSB();
embedTwoLSBwithOPAP();
twoLSB_Calculations();
twoLSB_EmbeddedPGMFile();
break;
default:
printf("Wrong choice of Embedding!");
}
}