-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2png.c
292 lines (219 loc) · 7.53 KB
/
png2png.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <png.h>
#include <setjmp.h>
#define MIN(x,y) ((x) < (y) ? (x) : (y))
unsigned short _rotr_short(unsigned short value, int shift) {
if (shift == 16)
return value;
return (value >> shift) | (value << (16 - shift));
}
unsigned short _rotl_short(unsigned short value, int shift) {
if (shift == 16)
return value;
return (value << shift) | (value >> (16 - shift));
}
unsigned char _rotl_byte(unsigned char value, int shift) {
if (shift == 8)
return value;
return (value << shift) | (value >> (8 - shift));
}
unsigned char _rotr_byte(unsigned char value, int shift) {
if (shift == 8)
return value;
return (value >> shift) | (value << (8 - shift));
}
void print_binary(char *msg, unsigned int number)
{
printf("%s ", msg);
unsigned int mask = 1 << 31;
for (int i=0; i < 32; i++) {
if (i % 8 == 0) putc(' ', stdout);
putc((number & mask) ? '1' : '0', stdout);
mask = mask >> 1;
}
putc('\n', stdout);
}
int main(int argc, char **argv) {
if (argc != 4) {
fprintf(stderr, "Usage: %s < scramble | descramble > <input png file path> <output png file path>\n", argv[0]);
exit(1);
}
char scramble;
if (!strcmp(argv[1], "scramble")) {
scramble = 1;
} else if (!strcmp(argv[1], "descramble")) {
scramble = 0;
} else {
fprintf(stderr, "Invalid first argument (should be 'scramble' or 'descramble')\n");
exit(1);
}
char *infile_name = argv[2];
FILE *infile;
if ((infile = fopen(infile_name, "rb")) == NULL) {
fprintf(stderr, "Could not open file %s for reading", infile_name);
exit(1);
}
char *outfile_name = argv[3];
FILE *outfile;
if ((outfile = fopen(outfile_name, "wb")) == NULL) {
fprintf(stderr, "Could not open file %s for writing", outfile_name);
exit(1);
}
unsigned char sig[8];
if (fread(sig, 1, sizeof(sig), infile) < 8) {
fclose(infile);
fprintf(stderr,"Invalid PNG file\n");
exit(1);
}
if (png_sig_cmp(sig, 0, 8)) {
fclose(infile);
fprintf(stderr,"Invalid PNG file\n");
exit(1);
}
// Read PNG file
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL) {
fclose(infile);
fprintf(stderr,"Cannot allocate read struct\n");
exit(1);
}
png_infop info = png_create_info_struct(png);
if (info == NULL) {
png_destroy_read_struct(&png, NULL, NULL);
fclose(infile);
fprintf(stderr,"Cannot allocate info struct\n");
exit(1);
}
// pass open file to png struct
png_init_io(png, infile);
// skip signature bytes (we already read those)
png_set_sig_bytes(png, sizeof(sig));
// get image information
png_read_info(png, info);
png_uint_32 w = png_get_image_width(png, info);
png_uint_32 h = png_get_image_height(png, info);
// set least one byte per channel
if (png_get_bit_depth(png, info) < 8) {
png_set_packing(png);
}
// if transparency, convert it to alpha
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png);
}
switch(png_get_color_type(png, info)) {
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGBA:
break;
case PNG_COLOR_TYPE_GRAY:
case PNG_COLOR_TYPE_GRAY_ALPHA:
case PNG_COLOR_TYPE_PALETTE:
default:
png_destroy_read_struct(&png, &info, NULL);
fclose(infile);
fprintf(stderr,"Cannot determine color type\n");
exit(1);
}
png_uint_32 bpp = png_get_rowbytes(png, info) / w;
png_set_interlace_handling(png);
png_read_update_info(png, info);
// allocate pixel buffer
unsigned char *pixels = (unsigned char*)calloc(w*h*bpp, sizeof(unsigned char));
// setup array with row pointers into pixel buffer
png_bytep rows[h];
unsigned char *p = pixels;
for(int i = 0; i < h; i++) {
rows[i] = p;
p += w * bpp;
}
// read all rows (data goes into 'pixels' buffer)
png_read_image(png, rows);
png_read_end(png, NULL);
png_destroy_read_struct(&png, &info, NULL);
// Apply the scrambling/unscrambling
unsigned char **row_buffers = (unsigned char **)calloc(h, sizeof(unsigned char *));
for (int i=0; i < h; i++) {
row_buffers[i] = (unsigned char *)calloc(3*w, sizeof(unsigned char));
}
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
unsigned char red = rows[row][col * bpp + 0];
unsigned char green = rows[row][col * bpp + 1];
unsigned char blue = rows[row][col * bpp + 2];
// printf("------------------------------------------\n");
// printf("original r=%d g=%d b=%d\n",red, green, blue);
// print_binary("original ", (red << 16) + (green << 8) + blue);
unsigned char target_red;
unsigned char target_green;
unsigned char target_blue;
if (scramble) {
unsigned short us = (red << 8) + (blue);
// print_binary("Original us: ", (unsigned int)us);
us = (us << 5 ) | (us >> 11);
// print_binary("Rotated us : ", (unsigned int)us);
unsigned int threebytes = (us << 8) + green;
// print_binary("threebytes: ", threebytes);
threebytes = threebytes ^ (101231 * col * col + 41231 * row * row );
// print_binary("scrambled threebytes: ", threebytes);
target_red = (threebytes >> 16) & 255;
target_green = (threebytes >> 8) & 255;
target_blue = threebytes & 255;
} else {
unsigned int threebytes = (((unsigned short)red) << 16) +
(((unsigned short)green) << 8) + blue;
threebytes = threebytes ^ (101231 * col * col + 41231 * row * row);
// print_binary("descrambled threebytes: ", threebytes);
target_green = (threebytes) & 255;
// print_binary("target green: ", (unsigned int)target_green);
unsigned short us = (threebytes >> 8);
// print_binary("I am getting: ", (unsigned int)us);
us = (us >> 5) | (us << 11);
// print_binary("recovered us:", (unsigned int) us);
target_blue = us & 255;
target_red = (us >> 8) & 255;
}
unsigned int threebytes = ((unsigned int)(target_red) << 16) + ((unsigned int)(target_green) << 8) + (unsigned int)(target_blue);
// print_binary("final: ",threebytes);
// printf("final: r=%d g=%d b=%d\n",target_red, target_green, target_blue);
row_buffers[row][col * 3 + 0] = target_red;
row_buffers[row][col * 3 + 1] = target_green;
row_buffers[row][col * 3 + 2] = target_blue;
}
}
// Save file
{
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep row = NULL;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(outfile);
fprintf(stderr, "Cannot allocate write struct\n");
exit(1);
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(outfile);
fprintf(stderr, "Cannot allocated info struct\n");
exit(1);
}
// Pass open file to png struct
png_init_io(png_ptr, outfile);
// Write header
png_set_IHDR(png_ptr, info_ptr, w, h,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// Write image data
for (int row=0; row < h; row++) {
png_write_row(png_ptr, row_buffers[row]);
}
// End write
png_write_end(png_ptr, NULL);
fclose(outfile);
}
exit(0);
}