-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathinfragram.coffee
177 lines (157 loc) · 5.86 KB
/
infragram.coffee
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
image = null
mode = "raw"
r_exp = ""
g_exp = ""
b_exp = ""
m_exp = "" #monochrome
class Image
constructor: (@data, @width, @height, @channels) ->
copyToImageData: (imgData) ->
imgData.data.set(@data)
extrema: ->
n = @width * @height
mins = (@data[i] for i in [0...@channels])
maxs = (@data[i] for i in [0...@channels])
j = 0
for i in [0...n]
for c in [0...@channels]
if @data[j] > maxs[c]
maxs[c] = @data[j]
if @data[j] < mins[c]
mins[c] = @data[j]
j++
return [mins, maxs]
histogram = (array, [min,max], nbins) ->
bins = (0 for i in [0...nbins])
d = (max - min) / nbins
for a in array
i = Math.floor((a - min) / d)
if 0 <= i < nbins
bins[i]++
return bins
segmented_colormap = (segments) -> (x) ->
[y0, y1] = [0, 0]
[x0, x1] = [segments[0][0], 1]
if x < x0
return y0
for [xstart, y0, y1],i in segments
x0 = xstart
if i == segments.length - 1
x1 = 1
break
x1 = segments[i+1][0]
if xstart <= x < x1
break
result = []
for i in [0...y0.length]
result[i] = (x-x0) / (x1 - x0) * (y1[i] - y0[i]) + y0[i]
return result
get_channels = (img) ->
n = img.width * img.height;
r = new Float32Array(n);
g = new Float32Array(n);
b = new Float32Array(n);
for i in [0...n]
r[i] = img.data[4*i + 0];
g[i] = img.data[4*i + 1];
b[i] = img.data[4*i + 2];
mkImage = (d) -> new Image(d, img.width, img.height, 1);
return [mkImage(r), mkImage(g), mkImage(b)];
ndvi = (nir, vis) ->
n = nir.width * nir.height;
d = new Float64Array(n);
for i in [0...n]
d[i] = (nir.data[i] - vis.data[i]) / (nir.data[i] + vis.data[i]);
return new Image(d, nir.width, nir.height, 1);
colorify = (img, colormap) ->
n = img.width * img.height;
data = new Uint8ClampedArray(4*n);
j = 0;
for i in [0...n]
[r,g,b] = colormap(img.data[i]);
data[j++] = r;
data[j++] = g;
data[j++] = b;
data[j++] = 255;
cimg = new Image();
cimg.width = img.width;
cimg.height = img.height;
cimg.data = data;
return new Image(data, img.width, img.height, 4);
infragrammar = (img) ->
n = img.width * img.height;
r = new Float32Array(n);
g = new Float32Array(n);
b = new Float32Array(n);
#o = new Uint8ClampedArray(4*n);
o = new Float64Array(4*n);
for i in [0...n]
r[i] = img.data[4*i + 0]/255;
g[i] = img.data[4*i + 1]/255;
b[i] = img.data[4*i + 2]/255;
o[4*i + 0] = 255*r_exp(r[i],g[i],b[i]);
o[4*i + 1] = 255*g_exp(r[i],g[i],b[i]);
o[4*i + 2] = 255*b_exp(r[i],g[i],b[i]);
o[4*i + 3] = 255
return new Image(o, img.width, img.height, 4);
render = (img) ->
e = document.getElementById("image");
e.width = img.width
e.height = img.height
ctx = e.getContext("2d");
d = ctx.getImageData(0, 0, img.width, img.height);
img.copyToImageData(d);
ctx.putImageData(d, 0, 0);
segments = [ [0, [0,0,0], [255,255,255]],
[1, [255,255,255], [255,255,255]] ]
update = (img) ->
if mode == "ndvi"
[r,g,b] = get_channels(img)
ndvi_img = ndvi(r,b)
[[min],[max]] = ndvi_img.extrema()
d = max - min
colormap = segmented_colormap(segments)
result = colorify(ndvi_img, colormap)
else if mode == "raw"
result = img
else if mode == "nir"
[r,g,b] = get_channels(img)
result = colorify(r, (x) -> [x, x, x])
else if mode == "infragrammar"
result = infragrammar(img)
$('#download').show()
render(result)
file_reader = new FileReader();
file_reader.onload = (oFREvent) ->
file = document.forms["file-form"]["file-sel"].files[0];
data = new Uint8Array(file_reader.result);
if file.type == "image/png"
png = new PNG(data);
data = png.decode()
img = new Image(data, png.width, png.height);
else if file.type == "image/jpeg"
jpeg = new JpegImage();
jpeg.parse(data);
d = new Uint8ClampedArray(4 * jpeg.width * jpeg.height);
img = new Image(d, jpeg.width, jpeg.height, 4);
jpeg.copyToImageData(img);
else
document.getElementById("error").html = "Unrecognized file format (supports PNG and JPEG)";
return
image = img
update(img,'raw')
on_file_sel = () ->
file = document.forms["file-form"]["file-sel"].files[0];
if file
file_reader.readAsArrayBuffer(file);
download = () ->
e = document.getElementById("image");
ctx = e.getContext("2d");
window.open(ctx.canvas.toDataURL(),'_newtab').focus()
save_expressions = (r,g,b) ->
r = r.replace(/S/g,$('#slider').val()/100)
g = g.replace(/S/g,$('#slider').val()/100)
b = b.replace(/S/g,$('#slider').val()/100)
eval("r_exp = function(R,G,B){return "+r+";}")
eval("g_exp = function(R,G,B){return "+g+";}")
eval("b_exp = function(R,G,B){return "+b+";}")