-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
190 lines (139 loc) · 3.92 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <embed.h>
#include <fractal.h>
/* Global Variables */
char *input;
bitmap_t output;
FILE *fptr, *fp;
int dimensions = 800;
bitmap_t initialize() {
/* Initialisation */
input = (char*)calloc(FILE_SIZE, sizeof(char*));
// Create an image
output.width = dimensions;
output.height = dimensions;
return output;
}
int generate_fractal_img() {
/* Initialisation */
output = initialize();
restart:
output.pixels = calloc (output.width * output.height, sizeof (pixel_t));
if (! output.pixels) {
return -1;
}
/* Fractal Algorithm */
// Set random seed
srand(time(NULL));
// Generate fractal bitmap
output = fractal(output, (int)rand());
/* Anti-aliasing Algorithm */
// Blur bitmap twice
for (int i=0; i < 2; i++) output = antialias(output);
/* Verify Fractal */
if (!verify(output)) {
free (output.pixels);
// Restart fractal generation if entire bitmap is black
goto restart;
}
/* Save embedded image */
if (save_png_to_file (& output, "output.png")) {
fprintf (stderr, "Error writing file.\n");
return -1;
} else {
puts("Original image outputted to ./output.png");
}
return 0;
}
void read_input() {
char ch;
int cnt = 0;
while ((ch = fgetc(fp)) != EOF) {
input[cnt] = ch;
cnt++;
}
fclose(fp);
}
void embed_string() {
// Initialisation
int handle_ret, choice, rref, gref, bref, pixelcnt = 0, inputlen = strlen(input);
int *coord = (int *)calloc(inputlen * 2, sizeof(int *));
int divdimension = floor(((float)dimensions / 3));
int buffer[divdimension];
// Create indexed array
#pragma omp parallel for
for (int i=0; i < divdimension; i++) buffer[i] = i;
// Mix indexed array
#pragma omp parallel for
for (int i=0; i < inputlen * 2; i++) {
regen:
choice = rand() % divdimension;
if (buffer[choice] == choice) {
coord[i] = choice;
if (i%2 != 0) buffer[choice] = -1;
} else {
goto regen;
}
}
// Divide pixels to [3 x 3] matrix
#pragma omp parallel
while (pixelcnt <= inputlen) {
// Obtain pixel values of ref. pixel
pixel_t * pixelref = pixel_at (& output, coord[pixelcnt]*3+1, coord[pixelcnt+1]*3+1);
rref = pixelref->red;
gref = pixelref->green;
bref = pixelref->blue;
// printf("%d %d %d\n", rref, gref, bref);
// Embed bits with input string
#pragma omp critical
{
handle_ret = handle(coord[pixelcnt], coord[pixelcnt+1], output, rref, gref, bref, fptr, input);
if (handle_ret == 0)
return;
pixelcnt += 2;
}
}
}
int main(int argc, char *argv[]) {
/* Check for cmdline argument */
if (argc != 2) {
puts("Invalid arguments!");
return -1;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error reading '%s' file.\n", argv[1]);
return -1;
}
/* Generate placeholder image */
if (generate_fractal_img() == -1) {
puts("Error generating image!");
exit(1);
}
/* Steganography initialization */
// Read input string
read_input();
// Convert first char to binary string
char binval = input[0];
if (!binval) {
puts("Error writing char!");
exit(1);
}
// Set bits global variable in embed.h
bits = chartobin(binval);
// More Initialisation
fptr = fopen("embed.log","w");
int lix = floor((float)output.height/3);
int liy = floor((float)output.width/3);
// Print total Embedding capacity
printf("Total Embd. Capacity: %d\n", calcCapacity(lix, liy, output));
/* Steganography embedding */
// Embed string into output image
embed_string();
// Free pointers
fclose(fptr);
free (output.pixels);
free (input);
return 0;
}