-
Notifications
You must be signed in to change notification settings - Fork 11
/
image.c
162 lines (149 loc) · 4.43 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "hkpriv.h"
#include "ksort.h"
KSORT_INIT_GENERIC(float)
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
struct cnt_aux {
double cnt[4];
uint32_t tot;
};
static void draw_tads(uint8_t *buf, const int64_t *off, int w, double s, int is_upper, int n_tads, const struct hk_pair *tads)
{
int i, j;
for (i = 0; i < n_tads; ++i) {
const struct hk_pair *p = &tads[i];
int64_t x[2];
int32_t y[2];
x[0] = off[p->chr>>32] + hk_ppos1(p);
x[1] = off[(int32_t)p->chr] + hk_ppos2(p);
y[0] = (int32_t)(x[0] * s);
y[1] = (int32_t)(x[1] * s);
assert(y[0] < w && y[1] < w);
for (j = y[0]; j < y[1]; ++j) {
if (is_upper) {
uint8_t *p = &buf[(y[0] * w + j) * 3];
*p++ = 255, *p++ = 0, *p++ = 0;
p = &buf[(j * w + y[1]) * 3];
*p++ = 255, *p++ = 0, *p++ = 0;
} else {
uint8_t *p = &buf[(y[1] * w + j) * 3];
*p++ = 255, *p++ = 0, *p++ = 0;
p = &buf[(j * w + y[0]) * 3];
*p++ = 255, *p++ = 0, *p++ = 0;
}
}
}
}
void hk_pair_image(const struct hk_sdict *d, int32_t n_pairs, const struct hk_pair *pairs, int w, float phase_thres, int no_grad,
int n_tads, const struct hk_pair *tads, int n_tads_prev, const struct hk_pair *tads_prev, const char *fn)
{
int64_t tot_len, *off;
int32_t i, j, ww = w * w, pixel_bp, m_tmp, n_tmp;
uint8_t *buf;
float *tmp, c_max;
double s, t;
struct cnt_aux *cnt;
for (tot_len = 0, i = 0; i < d->n; ++i)
tot_len += d->len[i];
s = (double)(w - d->n + 1) / tot_len;
pixel_bp = tot_len / (w - d->n + 1) + 1;
off = CALLOC(int64_t, d->n);
for (tot_len = 0, i = 0; i < d->n; ++i) {
off[i] = tot_len;
tot_len += d->len[i] + pixel_bp;
}
cnt = CALLOC(struct cnt_aux, w * w);
for (i = 0; i < n_pairs; ++i) {
const struct hk_pair *p = &pairs[i];
int64_t x[2], z;
int32_t y[2];
struct cnt_aux *c;
x[0] = off[p->chr>>32] + hk_ppos1(p);
x[1] = off[(int32_t)p->chr] + hk_ppos2(p);
y[0] = (int32_t)(x[0] * s);
y[1] = (int32_t)(x[1] * s);
assert(y[0] < w && y[1] < w);
z = y[0] * w + y[1];
c = &cnt[z];
++c->tot;
for (j = 0; j < 4; ++j)
c->cnt[j] += p->_.p4[j];
}
// figure out the max depth
m_tmp = n_tmp = 0, tmp = 0;
for (i = 0; i < w; ++i) {
for (j = i; j < w; ++j) {
int32_t z = i * w + j;
struct cnt_aux *c = &cnt[z];
if (c->tot == 0) continue;
if (phase_thres > 0.0) {
int32_t k;
double max = -1.0;
for (k = 0; k < 4; ++k)
max = max > c->cnt[k]? max : c->cnt[k];
if (max / c->tot >= phase_thres) {
if (n_tmp == m_tmp)
EXPAND(tmp, m_tmp);
tmp[n_tmp++] = max;
}
} else {
if (n_tmp == m_tmp)
EXPAND(tmp, m_tmp);
tmp[n_tmp++] = c->tot;
}
}
}
c_max = ks_ksmall_float(n_tmp, tmp, (int)(n_tmp * .99 + .499));
free(tmp);
t = 255.0 / c_max;
buf = CALLOC(uint8_t, ww * 3);
for (i = 0; i < w; ++i) {
for (j = i; j < w; ++j) {
int32_t z = i * w + j;
uint8_t *q = &buf[(j * w + i) * 3];
uint8_t *p = &buf[(i * w + j) * 3];
struct cnt_aux *c = &cnt[z];
if (c->tot == 0) {
*p++ = 0, *p++ = 0, *p++ = 0, *q++ = 0, *q++ = 0, *q++ = 0;
} else {
if (phase_thres > 0.0) {
int x, k, max_k = -1;
double max = -1.0;
for (k = 0; k < 4; ++k)
if (c->cnt[k] > max)
max_k = k, max = c->cnt[k];
x = max >= c->tot * phase_thres? (int)(max * t) : (int)(c->tot * t);
if (no_grad || x > 255) x = 255;
if (max < c->tot * phase_thres) *p++ = x/2, *p++ = x/2, *p++ = x/2, *q++ = x/2, *q++ = x/2, *q++ = x/2;
else if (max_k == 0) *p++ = x, *p++ = 0, *p++ = 0, *q++ = x, *q++ = 0, *q++ = 0;
else if (max_k == 1) *p++ = x, *p++ = 0, *p++ = x, *q++ = 0, *q++ = x, *q++ = x;
else if (max_k == 2) *p++ = 0, *p++ = x, *p++ = x, *q++ = x, *q++ = 0, *q++ = x;
else if (max_k == 3) *p++ = 0, *p++ = x, *p++ = 0, *q++ = 0, *q++ = x, *q++ = 0;
} else {
int x = (int)(c->tot * t);
if (no_grad || x > 255) x = 255;
*p++ = x, *p++ = x, *p++ = x, *q++ = x, *q++ = x, *q++ = x;
}
}
}
}
// write lines
for (i = 0; i < d->n - 1; ++i) {
int x = (int)(off[i+1] * s + .499);
uint8_t *p;
for (j = 0, p = &buf[x * w * 3]; j < w; ++j, p += 3)
p[0] = 32, p[1] = 32, p[2] = 32;
for (j = 0, p = &buf[x * 3]; j < w; ++j, p += w * 3)
p[0] = 32, p[1] = 32, p[2] = 32;
}
// draw TAD lines
draw_tads(buf, off, w, s, 1, n_tads, tads);
draw_tads(buf, off, w, s, 0, n_tads_prev, tads_prev);
stbi_write_png(fn, w, w, 3, buf, w * 3);
free(buf);
free(cnt);
free(off);
}