1
0
Fork 0

08: C: Add input visualization (Netpbm P6)

This commit is contained in:
Mia Herkt 2022-12-09 01:56:28 +01:00
parent 922cce3c8d
commit 6d847ea310
Signed by: mia
GPG Key ID: 72E154B8622EC191
1 changed files with 43 additions and 2 deletions

View File

@ -1,10 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
int vis(const char *outpath, const char *in, size_t w, size_t h, size_t stride) {
int fd = open(outpath, O_CREAT | O_RDWR | O_NONBLOCK, 0644);
if (fd == -1) return 1;
char head[128];
size_t ps = snprintf(head, 128, "P6\n%lu %lu\n255\n", w, h);
if (ps >= 128) return 1;
size_t mapsiz = ps + w * h * 3;
ftruncate(fd, mapsiz);
unsigned char *o = mmap(NULL, mapsiz, PROT_WRITE, MAP_SHARED, fd, 0);
if (o == MAP_FAILED) return 1;
unsigned char *out = o;
strncpy((char*)out, head, ps);
out += ps;
for(size_t y = 0; y < h; y++) {
for (size_t x = 0; x < w; x++) {
int val = in[stride * y + x] - '0';
float v = val * 0.75 / 10.0;
unsigned char *b = out + w * y * 3 + x * 3;
*b++ = 180 * v / 3;
*b++ = 220 * v;
*b++ = 150 * v / 1.8;
}
}
munmap(o, mapsiz);
return close(fd);
}
int main(int argc, char **argv) {
if (argc < 2) return 1;
@ -22,10 +53,20 @@ int main(int argc, char **argv) {
size_t visible = w * 2 + (h - 2) * 2;
size_t maxscore = 0;
if (argc > 2) {
fprintf(stderr, "Writing %s... ", argv[2]);
fflush(stderr);
if (vis(argv[2], in, w, h, stride)) {
perror("");
return 1;
}
fprintf(stderr, "Done!\n");
}
#pragma omp parallel for reduction(+:visible) reduction(max:maxscore) schedule(runtime)
for (size_t y = 1; y < h - 1; y++) {
for (size_t x = 1; x < w - 1; x++) {
size_t i, score = 0;
size_t i, score;
char val = in[stride * y + x];
int vis = 0;
@ -59,5 +100,5 @@ int main(int argc, char **argv) {
}
}
printf("Silver: %lu\nGold: %lu\n", visible, maxscore);
fprintf(stderr, "Silver: %lu\nGold: %lu\n", visible, maxscore);
}