Compare commits

..
2 Commits
Author SHA1 Message Date
Matt Walsh 0982b90faa palletize 2022-05-13 12:09:02 -05:00
Matt Walsh 4a2cf1d993 change to @napi-rs/canvas 2022-05-13 11:55:11 -05:00
7 changed files with 315 additions and 832 deletions

No files matched your search

+1 -1
View File
@@ -46,7 +46,7 @@ if (single) {
const plots = plot(radarData, ['REF', 'VEL'], {
elevations,
size,
palettize: true,
palettize: false,
cropTo: size / 2,
});
+271 -798
View File
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "nexrad-level-2-plot",
"version": "2.4.0",
"version": "2.2.1",
"description": "Plotting library for nexrad-level-2-data",
"main": "src/index.js",
"scripts": {
@@ -24,7 +24,7 @@
},
"homepage": "https://github.com/netbymatt/nexrad-level-2-plot#readme",
"dependencies": {
"canvas": "^2.9.1",
"@napi-rs/canvas": "^0.1.21",
"nexrad-level-2-data": "^2.3.5"
},
"devDependencies": {
+4 -13
View File
@@ -1,4 +1,4 @@
const canvasObj = require('canvas');
const canvasObj = require('@napi-rs/canvas');
const { createCanvas } = canvasObj;
@@ -73,15 +73,6 @@ const draw = (data, _options) => {
if (options.size < 1) throw new Error('Provide options.size > 0');
const scale = DEFAULT_OPTIONS.size / options.size;
// wsr88d uses a gate size of 0.25km, tdwr uses a gate size of 0.15km, although it is reported as 0.3km for processing reasons
// this calculation scales the plot accordingly to the nominal 0.25km so all generated plots are at the same scale
const rawGateSize = data?.data?.[options.elevation]?.[0]?.record?.reflect?.gate_size ?? 0.25;
const realGateSize = rawGateSize !== 0.3 ? rawGateSize : rawGateSize / 2;
const gateSizeScaling = 0.25 / realGateSize;
const longRange = data?.vcp?.record?.elevations?.[options.elevation]?.super_res_control?.super_res?.['300km'];
const rangeDivider = longRange ? 2 : 1;
// calculate crop, adjust if necessary
const cropTo = Math.min(options.size, options.cropTo);
if (options.cropTo < 1) throw new Error('Provide options.cropTo > 0');
@@ -96,7 +87,7 @@ const draw = (data, _options) => {
// canvas settings
ctx.imageSmoothingEnabled = true;
ctx.lineWidth = options.lineWidth * rangeDivider / gateSizeScaling;
ctx.lineWidth = options.lineWidth;
ctx.translate(cropTo / 2, cropTo / 2);
ctx.rotate(-Math.PI / 2);
@@ -150,11 +141,11 @@ const draw = (data, _options) => {
if (bin.count) {
// rrle encoded
ctx.strokeStyle = palette.lookupRgba[bin.value];
ctx.arc(0, 0, (idx + deadZone) * rangeDivider / gateSizeScaling, startAngle, endAngle + resolution * (bin.count - 1));
ctx.arc(0, 0, (idx + deadZone), startAngle, endAngle + resolution * (bin.count - 1));
} else {
// plain data
ctx.strokeStyle = palette.lookupRgba[bin];
ctx.arc(0, 0, (idx + deadZone) * rangeDivider / gateSizeScaling, startAngle, endAngle);
ctx.arc(0, 0, (idx + deadZone), startAngle, endAngle);
}
ctx.stroke();
}
+24 -3
View File
@@ -11,9 +11,11 @@ class Palette {
this.palette = palette.palette;
this.limits = palette.limits;
this.transparentIndex = palette.transparentIndex;
this.transparentValue = this.indexToValue(this.transparentIndex);
this.lookupRgba = findDbzRbgaGenerator(this.limits, this.palette, palette.maxDbzIndex);
this.lookupIndex = findDbzIndexGenerator(this.limits, this.palette, palette.maxDbzIndex);
this.closest = {};
this.closestIndexCache = {};
this.closestValueCache = {};
// these may not exist and are overridden with default functions
if (palette.hasOwnProperty('downSampleReset')) {
@@ -60,7 +62,7 @@ class Palette {
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentIndex;
// short circuit previously calculated matches
const asHex = match[0].toString(16).padStart(2, '0') + match[1].toString(16).padStart(2, '0') + match[2].toString(16).padStart(2, '0');
if (this.closest[asHex]) return this.closest[asHex];
if (this.closestIndexCache[asHex]) return this.closestIndexCache[asHex];
// initial conditions
let closestIndex = 0;
let closest = Infinity;
@@ -75,10 +77,29 @@ class Palette {
}
}
// store closest match to speed up next iteration
this.closest[asHex] = closestIndex;
this.closestIndexCache[asHex] = closestIndex;
return closestIndex;
}
// match = [r,g,b[,a]]
closestValue(match) {
// short circuit for transparent (black)
if (match[0] <= 2 && match[1] <= 2 && match[2] <= 2) return this.transparentValue;
// short circuit previously calculated matches
const asHex = match[0].toString(16).padStart(2, '0') + match[1].toString(16).padStart(2, '0') + match[2].toString(16).padStart(2, '0');
if (this.closestValueCache[asHex]) return this.closestValueCache[asHex];
const closest = this.closestIndex(match);
const closestAsValue = this.indexToValue(closest);
this.closestValueCache[asHex] = closestAsValue;
return closestAsValue;
}
// index to value
indexToValue(index) {
return this.palette.slice(index * 4, index * 4 + 4);
}
// geometric distance
static geometricDistance(a, b) {
return a.reduce((acc, val, idx) => acc + (val - b[idx]) ** 2, 0);
+9 -5
View File
@@ -1,4 +1,4 @@
const { createCanvas } = require('canvas');
const { createCanvas } = require('@napi-rs/canvas');
const palettizeImage = (sourceCtx, palette) => {
// get the dimensions of the image from the ctx
@@ -10,7 +10,7 @@ const palettizeImage = (sourceCtx, palette) => {
// transform into indexed palette by finding the closest indexed color for each pixel
// create a destination image that is transparent and indexed
const indexedCanvas = createCanvas(dim.x, dim.y);
const indexedCtx = indexedCanvas.getContext('2d', { pixelFormat: 'A8' });
const indexedCtx = indexedCanvas.getContext('2d');
// get the source image data
const sourceImageData = sourceCtx.getImageData(0, 0, dim.x, dim.y);
@@ -18,9 +18,13 @@ const palettizeImage = (sourceCtx, palette) => {
const indexedImageData = indexedCtx.getImageData(0, 0, dim.x, dim.y);
// loop through each pixel
indexedImageData.data.forEach((val, idx) => {
indexedImageData.data[idx] = palette.closestIndex(sourceImageData.data.slice(idx * 4, idx * 4 + 3));
});
for (let i = 0; i < indexedImageData.data.length; i += 4) {
const [r, g, b, a] = palette.closestValue(sourceImageData.data.slice(i, i + 3));
indexedImageData.data[i] = r;
indexedImageData.data[i + 1] = g;
indexedImageData.data[i + 2] = b;
indexedImageData.data[i + 3] = a;
}
// write the new image data
indexedCtx.putImageData(indexedImageData, 0, 0);
+4 -10
View File
@@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs/promises');
// write a canvas to a Png file
/**
* Write a Canvas to the specified file
@@ -6,25 +6,19 @@ const fs = require('fs');
* @param {Canvas} data Canvas provided by the plot() function
* @returns Promise
*/
const writePngToFile = (fileName, data) => new Promise((resolve, reject) => {
const writePngToFile = async (fileName, data) => {
// the draw function will return false if no data was found, short circuit the output here
if (!data.canvas) {
resolve();
return;
}
// get a write stream
const writeStream = fs.createWriteStream(fileName);
// options
const options = {};
if (data.palette) options.palette = data.palette;
// write to the stream
data.canvas.createPNGStream(options).pipe(writeStream);
writeStream.on('finish', () => resolve(fileName));
writeStream.on('error', (e) => reject(e));
});
await fs.writeFile(fileName, await data.canvas.encode('webp', 100));
};
module.exports = {
writePngToFile,