forked from faroscore/leaflet-geotiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-geotiff-rgb.js
33 lines (28 loc) · 1.31 KB
/
leaflet-geotiff-rgb.js
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
// Depends on:
// https://github.com/santilland/plotty
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
var L = require('@myeasyfarm/leaflet-geotiff');
}
L.LeafletGeotiff.RGB = L.LeafletGeotiffRenderer.extend({
initialize: function() {
this.name = 'Canvas Renderer';
},
render: function(raster, canvas, ctx, args) {
var rasterImageData = ctx.createImageData(raster.width, raster.height);
var isGrayscale = raster.data.length === 1;
for (let i = 0, j = 0; i < rasterImageData.data.length; i += 4, j += 1) {
rasterImageData.data[i] = raster.data[0][j]; // R value
rasterImageData.data[i + 1] = raster.data[isGrayscale ? 0 : 1][j]; // G value
rasterImageData.data[i + 2] = raster.data[isGrayscale ? 0 : 2][j]; // B value
rasterImageData.data[i + 3] = isGrayscale || !raster.data[3] ? 255 : raster.data[3][j]; // A value
}
var imageData = this.parent.transform(rasterImageData, args);
ctx.putImageData(imageData, args.xStart, args.yStart);
},
});
L.LeafletGeotiff.rgb = function(options) {
return new L.LeafletGeotiff.RGB(options);
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = L.LeafletGeotiff;
}