generated from 8dcc/c-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.c
344 lines (283 loc) · 11.8 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
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
334
335
336
337
338
339
340
341
342
343
344
#include <assert.h>
#include <stdlib.h>
#include <png.h>
#include <ctype.h>
#include "include/args.h"
#include "include/read_file.h"
#include "include/image.h"
#include "include/util.h"
/* Bytes per pixel of the PNG image (R, G, B) */
#define PNG_BPP 3
/*----------------------------------------------------------------------------*/
void image_init(Image* image, size_t data_sz) {
/* The image conversion functions ignore zoom. It will be applied when
* generating the PNG. */
switch (g_mode) {
case MODE_GRAYSCALE:
case MODE_ASCII:
case MODE_ENTROPY: {
image->width = g_output_width;
image->height = data_sz / image->width;
if (data_sz % image->width != 0)
image->height++;
} break;
case MODE_HISTOGRAM: {
image->width = g_output_width;
image->height = 256;
} break;
case MODE_BIGRAMS: {
image->width = 256;
image->height = 256;
} break;
case MODE_DOTPLOT: {
image->width = data_sz;
image->height = data_sz;
} break;
}
/* Allocate the array that will contain the color information. We need to
* cast the first value to `size_t' to make sure the multiplication result
* doesn't overflow in an `uint32_t'. */
image->pixels = calloc((size_t)image->height * image->width, sizeof(Color));
if (image->pixels == NULL)
die("Failed to allocate pixel array");
}
void image_free(Image* image) {
free(image->pixels);
image->pixels = NULL;
}
/*----------------------------------------------------------------------------*/
void image_grayscale(Image* image, ByteArray* bytes) {
for (size_t y = 0; y < image->height; y++) {
for (size_t x = 0; x < image->width; x++) {
/* One-dimensional index for both the `bytes->data' and
* `image->pixels' arrays. */
const size_t raw_idx = image->width * y + x;
/* Pointer to the current color in the Image */
Color* color = &image->pixels[raw_idx];
/* The color brightness is determined by the byte value */
color->r = color->g = color->b = bytes->data[raw_idx];
}
}
}
void image_ascii(Image* image, ByteArray* bytes) {
for (size_t y = 0; y < image->height; y++) {
for (size_t x = 0; x < image->width; x++) {
const size_t raw_idx = (size_t)image->width * y + x;
Color* color = &image->pixels[raw_idx];
/* If we are not in-bounds, we are filling the last row; use a
* generic padding color. */
if (raw_idx >= bytes->size) {
color->r = color->g = color->b = 0;
continue;
}
/* Determine the RGB color of the pixel depending on the byte
* value. */
const uint8_t byte = bytes->data[raw_idx];
if (byte == 0x00 || byte == 0xFF) {
/* Common padding values, either black or white */
color->r = byte;
color->g = byte;
color->b = byte;
} else if (isgraph(byte) || isspace(byte)) {
/* Printable ASCII, blue */
color->r = 0x37;
color->g = 0x7E;
color->b = 0xB8;
} else {
/* Unknown, red */
color->r = 0xE4;
color->g = 0x1A;
color->b = 0x1C;
}
}
}
}
void image_entropy(Image* image, ByteArray* bytes) {
/* Iterate blocks of the input, each will share the same entropy color */
for (size_t i = 0; i < bytes->size; i += g_block_size) {
/* Make sure we are not reading past the end of `bytes->size' */
const size_t real_block_size =
(i + g_block_size < bytes->size) ? g_block_size : bytes->size - i;
/* Calculate the Shannon entropy for this block */
const double block_entropy = entropy(&bytes->data[i], real_block_size);
/* Calculate the [00..FF] color for this block based on the [0..8]
* entropy. */
uint8_t color_intensity = block_entropy * 255 / 8;
/* Render this block with the same color */
for (size_t j = 0; j < real_block_size; j++) {
Color* color = &image->pixels[i + j];
color->r = color->g = color->b = color_intensity;
}
}
}
void image_histogram(Image* image, ByteArray* bytes) {
assert(image->height == 256);
uint8_t most_frequent = 0;
uint32_t* occurrences = calloc(256, sizeof(uint32_t));
if (occurrences == NULL)
die("Failed to allocate occurrences array");
/* Store the occurrences including the most frequent byte */
for (size_t i = 0; i < bytes->size; i++) {
const uint8_t byte = bytes->data[i];
occurrences[byte]++;
if (occurrences[byte] > occurrences[most_frequent])
most_frequent = byte;
}
/* Draw each horizontal line based on occurrences relative to the most
* frequent byte. */
for (size_t y = 0; y < image->height; y++) {
const uint32_t line_width =
occurrences[y] * image->width / occurrences[most_frequent];
for (size_t x = 0; x < line_width; x++) {
Color* color = &image->pixels[image->width * y + x];
color->r = color->g = color->b = 0xFF;
}
}
/* We are done with the occurrences array */
free(occurrences);
}
void image_bigrams(Image* image, ByteArray* bytes) {
assert(image->width == 256 && image->height == 256);
/* Initialize the image to black */
for (size_t y = 0; y < image->height; y++) {
for (size_t x = 0; x < image->width; x++) {
Color* color = &image->pixels[image->width * y + x];
color->r = color->g = color->b = 0x00;
}
}
/* In this case we don't want to iterate the image, but the bytes-> We start
* from the second byte because we are plotting bigrams (pairs). */
for (size_t i = 1; i < bytes->size; i++) {
const uint8_t x = bytes->data[i - 1];
const uint8_t y = bytes->data[i];
/* The position is determined by the values of the current byte and the
* previous one. */
Color* color = &image->pixels[image->width * y + x];
/* This mode just plots whether a bigram is present or not in the input.
* We don't change the colors depending on the occurrences or anything
* like that. */
color->r = color->g = color->b = 0xFF;
}
}
void image_dotplot(Image* image, ByteArray* bytes) {
assert(image->width == bytes->size && image->height == bytes->size);
for (size_t y = 0; y < image->height; y++) {
for (size_t x = 0; x < image->width; x++) {
Color* color = &image->pixels[image->width * y + x];
/*
* The dotplot is used to meassure self-similarity. For each point
* (X,Y), set the point if the X-th sample matches the Y-th sample.
*
* For example:
*
* ABCABD
* +------
* A|* *
* B| * *
* C| *
* A|* *
* B| * *
* D| *
*/
color->r = color->g = color->b =
(bytes->data[x] == bytes->data[y]) ? 0xFF : 0x00;
}
}
}
/*----------------------------------------------------------------------------*/
void image_transform_squares(Image* image, uint32_t square_side) {
const int square_size = square_side * square_side;
const size_t total_pixels = image->width * image->height;
/* Increase the width and height if they are not divisible by the square
* side. */
if (image->width % square_side != 0)
image->width += square_side - image->width % square_side;
if (image->height % square_side != 0)
image->height += square_side - image->height % square_side;
/* Number of squares in each row. Division should be exact now. */
const size_t squares_per_row = image->width / square_side;
/* Allocate the array with the new image dimensions */
Color* new_pixels =
calloc((size_t)image->height * image->width, sizeof(Color));
/* Iterate the original pixels */
for (size_t i = 0; i < total_pixels; i++) {
/* Number and coordinates of the current square */
const size_t square_num = i / square_size;
const size_t square_y = square_num / squares_per_row;
const size_t square_x = square_num % squares_per_row;
/* Internal number and coordinates inside the current square */
const size_t internal_num = i % square_size;
const size_t internal_y = internal_num / square_side;
const size_t internal_x = internal_num % square_side;
/* Final coordinates in the image for the new point */
const size_t final_y = square_side * square_y + internal_y;
const size_t final_x = square_side * square_x + internal_x;
/* Copy the color in the old position to the new one */
new_pixels[image->width * final_y + final_x] = image->pixels[i];
}
/* Free the old pixel array and overwrite the pointer with the new one */
free(image->pixels);
image->pixels = new_pixels;
}
/*----------------------------------------------------------------------------*/
void image2png(Image* image, const char* filename) {
FILE* fd = fopen(filename, "wb");
if (!fd)
die("Can't open file: \"%s\"", filename);
png_structp png =
png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png)
die("Can't create png_structp");
png_infop info = png_create_info_struct(png);
if (!info)
die("Can't create png_infop");
/* The actual PNG image dimensions, remember that the Image is unscaled */
const int zoom = g_output_zoom;
uint32_t png_height = image->height * zoom;
uint32_t png_width = image->width * zoom;
/* Specify the PNG info */
png_init_io(png, fd);
png_set_IHDR(png, info, png_width, png_height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
/* Allocate the PNG rows. Since png_bytep is typedef'd to a pointer, this is
* a (void**). */
png_bytep* rows = calloc(png_height, sizeof(png_bytep));
if (rows == NULL)
die("Failed to allocate PNG rows");
for (uint32_t y = 0; y < png_height; y++) {
rows[y] = malloc(png_width * PNG_BPP);
if (rows[y] == NULL)
die("Failed to allocate PNG row %d", y);
}
/* Write the `bytes' array we received into the `rows' array we just
* allocated.
*
* The outer loops iterate the unscaled pixels, and are needed for accessing
* the `bytes->data' array. */
for (uint32_t y = 0; y < image->height; y++) {
for (uint32_t x = 0; x < image->width; x++) {
Color color = image->pixels[(size_t)image->width * y + x];
/* Draw a rectangle of side `g_output_zoom' */
for (int rect_y = 0; rect_y < zoom; rect_y++) {
for (int rect_x = 0; rect_x < zoom; rect_x++) {
const png_bytep row = rows[zoom * y + rect_y];
/* Note that we are using RGB, not RGBA */
row[PNG_BPP * (zoom * x + rect_x)] = color.r;
row[PNG_BPP * (zoom * x + rect_x) + 1] = color.g;
row[PNG_BPP * (zoom * x + rect_x) + 2] = color.b;
}
}
}
}
/* Write the rows into the PNG structure */
png_write_image(png, rows);
png_write_end(png, NULL);
/* Free each pointer of the `rows' array, and the array itself */
for (uint32_t y = 0; y < png_height; y++)
free(rows[y]);
free(rows);
fclose(fd);
png_destroy_write_struct(&png, &info);
}