-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
200 lines (153 loc) · 5.15 KB
/
image.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include "image.h"
void skip_comment_lines(FILE *);
ImagePGM *readPGM(char *filename) {
ImagePGM *data = (ImagePGM *) malloc(sizeof(ImagePGM));
FILE *pgmFile = fopen(filename, "r");
if (pgmFile == NULL) {
printf("\n Cannot open file to read.");
return NULL;
}
fscanf(pgmFile, "%s", data->magic);
if (strcmp(data->magic, "P2")) {
printf("\n Wrong file type!\n");
exit(1);
}
skip_comment_lines(pgmFile);
fscanf(pgmFile, "%d %d %d", &data->width, &data->height, &data->max_value);
//fgetc(pgmFile);
printf("\n PGM File reading...\n Format: %s\n Height: %d\n Width: %d\n ", data->magic, data->height, data->width);
data->pixels = (int **) malloc(data->height * sizeof(int *));
for (int i = 0; i < data->height; ++i)
data->pixels[i] = (int *) malloc(data->width * sizeof(int));
//printf("\n Malloc'ed.\n");
printf("\n data->height = %d data->width = %d\n", data->height, data->width);
//printf("\n Gonna enter for-loop zoneZ\n");
for (int i = 0; i < data->height; ++i) {
//printf("\n In da hood..");
for (int j = 0; j < data->width; ++j)
fscanf(pgmFile, "%d", &data->pixels[i][j]);
}
fclose(pgmFile);
printf("\n File read and closed.\n");
//DEBUG
printf("\n DEBUG OUTPUT\n");
printf("\n Reading from file %s\n\n", filename);
printf("%s\n", data->magic);
printf("%d %d\n", data->width, data->height);
printf("%d\n", data->max_value);
/*
for (int i = 0; i < data->width; ++i)
for (int j = 0; j < data->height; ++j)
{
printf("%d ", data->pixels[i][j]);
if (j % 23 == 0 && j != 0)
printf("\n");
}
*/
//DEBUG
printf("\n PGM File read successfully.\n");
return data;
}
int writePGM(ImagePGM *data, char *filename) {
FILE *pgmFile = fopen(filename, "w+");
if (pgmFile == NULL) {
printf("\n cannot open file to write.");
return 0;
}
//DEBUG
printf("\n DEBUG OUTPUT\n");
printf("\n writing to file %s\n\n", filename);
printf("%s\n", data->magic);
printf("%d %d\n", data->width, data->height);
printf("%d\n", data->max_value);
fprintf(pgmFile, "%s\n", data->magic);
fprintf(pgmFile, "%d %d %d\n", data->width, data->height, data->max_value);
for (int i = 0; i < data->height; ++i)
for (int j = 0; j < data->width; ++j)
fprintf(pgmFile, "%d ", data->pixels[i][j]);
fclose(pgmFile);
printf("\n File written successfully.\n");
return 1;
// Deallocate now
}
int encode(ImagePGM *data, char msg_text[]) {
if (data->width + 1 < 10 || data->height < 10)
return 0;
int msg_len = strlen(msg_text);
printf("Encoding '%s' into PGM File...\n", msg_text);
int width = data->width - 1;
int height = data->height - 1;
for (int k = 0; k <= msg_len; ++k) {
char toEncode = msg_text[k];
//printf("\n Char to encode: %c\n",toEncode);
for (int i = 0; i < 8; ++i) {
int bitToEncode = toEncode & (int) (pow(2, 7 - i));
if (bitToEncode)
bitToEncode = 1;
//printf("%d (%c:%d ANDed with %d)",bitToEncode,toEncode,toEncode,(int) (pow(2, i)) );
if (bitToEncode == 0)
data->pixels[height][width] &= 11111110;
else
data->pixels[height][width] |= 00000001;
//printf(" -> Written at data->pixels[%d][%d]\n",height,width);
width--;
if (width < 0) {
height--;
width = data->width - 1;
}
}
//printf("\n\n ------------- '%c' written successfully. -----------\n",toEncode);
}
return 1;
}
char *decode(ImagePGM *data) {
char *msg_text = (char *) malloc(100 * sizeof(char));
int msgIndex = 0;
printf("\nDecoding PGM File...\n");
bool decrypted = false;
int hidden_pixels[8];
int width = data->width - 1;
int height = data->height - 1;
while (!decrypted) {
int hidden = 0;
//printf("\n Decrypting bits: ");
for (int k = 0; k < 8; ++k) {
hidden_pixels[k] = data->pixels[height][width] & 1;
//printf("%d ",hidden_pixels[k]);
hidden += (int) (pow(2, 7 - k) * hidden_pixels[k]);
width--;
if (width < 0) {
height--;
width = data->width - 1;
}
}
//printf("\n Decoded char = %c",(char)hidden);
msg_text[msgIndex++] = (char) hidden;
if (hidden == 0)
decrypted = true;
}
//printf("\n Decrypted PGM to: %s\n",msg_text);
printf("\n\n");
return msg_text;
}
void freePGM(ImagePGM *data) {
printf("\n Freeing up memory...\n");
free(data->pixels);
}
void skip_comment_lines(FILE *fp) {
int ch;
char line[100];
while ((ch = fgetc(fp)) != EOF && isspace(ch)) {}
if (ch == '#') {
fgets(line, sizeof(line), fp);
skip_comment_lines(fp);
} else {
fseek(fp, -1, SEEK_CUR);
}
}