-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhelpers.c
More file actions
81 lines (67 loc) · 1.92 KB
/
helpers.c
File metadata and controls
81 lines (67 loc) · 1.92 KB
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
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
void print_ppm_header(const char* fmt, int32_t w, int32_t h) {
printf("%s\n%"PRIi32" %"PRIi32"\n", fmt, w, h);
}
void print(const uint8_t* s) {
printf("%s", s);
}
void println(const uint8_t* s) {
printf("%s\n", s);
}
void print_i32(int32_t i) {
printf("%"PRIi32"\n", i);
}
void print_u8(uint8_t c) {
printf("%c\n", c);
}
void print_f64(double d) {
printf("%.9f\n", d);
}
void print_piece_mask(const uint64_t* a) {
for (int i = 0; i < 12; ++i)
printf("%"PRIu64" ", a[i]);
printf("\n");
}
void print_piece_def(const uint8_t* a) {
for (int i = 0; i < 40; ++i)
printf("%d ", a[i]);
printf("\n");
}
void print_meteor_scnt(int32_t cnt) {
printf("%"PRIi32" solutions found\n\n", cnt);
}
void print_meteor_lines(const uint8_t* a) {
printf("%c %c %c %c %c \n %c %c %c %c %c \n",
a[0] + '0', a[1] + '0', a[2] + '0', a[3] + '0', a[4] + '0',
a[5] + '0', a[6] + '0', a[7] + '0', a[8] + '0', a[9] + '0');
}
double* alloc_img(int32_t w, int32_t h) {
return calloc(sizeof(double), w * h * 3);
}
static inline uint8_t convert_col(double col) {
col *= 255.0;
if (col < 0) col = 0;
if (col > 255) col = 255;
return col;
}
void save_img(int32_t w, int32_t h, const double* img) {
print_ppm_header("P6", w, h);
printf("255\n");
uint8_t* out_row = malloc(sizeof(uint8_t) * 3 * w);
for (int32_t y = 0; y < h; y++) {
const double* in_row = &img[y * (w * 3)];
for (int32_t x = 0; x < w; x++) {
out_row[x * 3 + 0] = convert_col(in_row[x * 3 + 0]);
out_row[x * 3 + 1] = convert_col(in_row[x * 3 + 1]);
out_row[x * 3 + 2] = convert_col(in_row[x * 3 + 2]);
}
fwrite(out_row, w * 3, sizeof(uint8_t), stdout);
}
free(out_row);
}
void* anydsl_alloc(int32_t dont_care, int64_t size) {
return malloc(size);
}