-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_visualize.c
279 lines (239 loc) · 5.93 KB
/
fs_visualize.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
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <png.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__i386__) || defined(__x86_64__)
#include <cpuid.h>
#include <mm_malloc.h>
#else
#define _mm_malloc(s, a) malloc(s)
#define _mm_free free
#endif
extern unsigned char avg_sse2(unsigned char *buf, int len);
#define MAX_PIXELS 1024*1024
#define MAX_FILE_NAME 256
#define MAX_CACHING 8*1024*1024
unsigned long calc_pix_size(off_t fsize)
{
unsigned long psize = sysconf(_SC_PAGESIZE);
while ((fsize + psize - 1) / psize > MAX_PIXELS)
psize <<= 1;
return psize;
}
unsigned char avg_generic(unsigned char *buf, int len)
{
unsigned int sum = 0;
int i = len;
while (i--)
sum += *buf++;
return sum / len;
}
/*
* This function is originally from
* http://www.labbookpages.co.uk/software/imgProc/libPNG.html
*/
int writeImage(char* filename, int width, int height, int *buffer, bool color)
{
int code = 0;
FILE *fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep row = NULL;
// Open file for writing (binary mode)
fp = fopen(filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Could not open file %s for writing\n", filename);
code = 1;
goto finalise;
}
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate write struct\n");
code = 1;
goto finalise;
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate info struct\n");
code = 1;
goto finalise;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n");
code = 1;
goto finalise;
}
png_init_io(png_ptr, fp);
// Write header (8 bit gray)
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
color ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// Allocate memory for one row
row = (png_bytep) malloc((color ? 3 : 1) * width * sizeof(png_byte));
if (row == NULL) {
fprintf(stderr, "Error allocating memory\n");
code = 1;
goto finalise;
}
// Write image data
int x, y, pixel;
for (y=0 ; y<height ; y++) {
for (x=0 ; x<width ; x++) {
pixel = buffer[y*width + x];
if (color == false) {
row[x] = pixel;
} else if (pixel >= 0) {
row[3 * x] = pixel;
row[3 * x + 1] = pixel;
row[3 * x + 2] = pixel;
} else {
row[3 * x] = -pixel;
row[3 * x + 1] = 0;
row[3 * x + 2] = 0;
}
}
png_write_row(png_ptr, row);
}
// End write
png_write_end(png_ptr, NULL);
finalise:
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
free(row);
return code;
}
int main(int argc, char **argv)
{
int fp;
off_t len, progress_thr = 0, processed = 0;
unsigned long pix_size;
unsigned char *buf;
bool color = false;
int i, pixels, w, pos = 0, cached = 0, *img;
time_t t;
char outf_name[MAX_FILE_NAME];
unsigned char (*avg)(unsigned char *buf, int len) = avg_generic;
if (argc < 2 || argc > 3) {
printf("Usage: %s <source_file> [<target_file>]\n", argv[0]);
return 0;
}
if (argc > 2) {
outf_name[MAX_FILE_NAME - 1] = '\0';
strncpy(outf_name, argv[2], MAX_FILE_NAME - 1);
}
fp = open(argv[1], O_RDONLY);
if (fp < 0) {
perror(argv[1]);
goto err1;
}
len = lseek(fp, 0L, SEEK_END);
if (len < 0) {
perror(argv[1]);
goto err2;
}
lseek(fp, 0L, SEEK_SET);
pix_size = calc_pix_size(len);
buf = _mm_malloc(pix_size, 16);
if (buf == NULL) {
perror("");
goto err2;
}
#if defined(__i386__) || defined(__x86_64__)
__builtin_cpu_init();
if (__builtin_cpu_supports("sse2")) {
printf("Using SSE2 optimizations\n");
avg = avg_sse2;
}
#endif
pixels = (len + pix_size - 1) / pix_size;
w = ceil(sqrt(pixels));
fprintf(stderr, "len %lld, pix_size %lu, pixels %d, w %d\n",
(long long int)len, pix_size, pixels, w);
img = malloc(sizeof(*img) * w * w);
if (img == NULL) {
perror("");
goto err3;
}
while (len - processed > pix_size) {
i = read(fp, buf, pix_size);
/*
* calculate byte average using optimized algorithm found
* above or in case of error indicate it with negative value
*/
processed += pix_size;
if (i == pix_size) {
img[pos++] = (*avg)(buf, i);
} else {
fprintf(stderr, "\nRead error at %lld. Error %d. %s\n",
(long long int)processed - pix_size,
errno, strerror(errno));
img[pos++] = -255;
color = true;
/*
* File offset update is undefined after read error.
* Seek exactly at the next chunk.
*/
lseek(fp, processed, SEEK_SET);
}
cached += i;
if (cached >= MAX_CACHING) {
posix_fadvise(fp, processed - cached, cached,
POSIX_FADV_DONTNEED);
cached = 0;
}
if (processed > progress_thr) {
progress_thr += len / 100;
printf("\r%d %%", (int)(processed / (len / 100)));
fflush(stdout);
}
}
/*
* calculate remaining bytes. Have to use generic byte average
* algorithm since optimized algorithms are not expected to be able
* to deal with data that is not multiple of PAGE_SIZE
*/
if (len - processed > 0) {
i = read(fp, buf, len - processed);
if (i == (len - processed)) {
img[pos] = avg_generic(buf, i);
} else {
fprintf(stderr, "\nRead error at %lld. Error %d. %s\n",
(long long int)processed,
errno, strerror(errno));
img[pos] = -255;
color = true;
}
}
puts("\r100 %");
if (argc < 3) {
t = time(NULL);
strftime(outf_name, sizeof(outf_name), "%Y%m%d-%H%M%S.png", localtime(&t));
}
writeImage(outf_name, w, w, img, color);
free(img);
_mm_free(buf);
close(fp);
return 0;
err3:
_mm_free(buf);
err2:
if (fp)
close(fp);
err1:
return 1;
}