Fixed handling of large files and fixed issue with line histogram colour fill

This commit is contained in:
mshwed 2019-04-28 14:38:03 -04:00
parent 802493fec4
commit 5225874498

View file

@ -73,9 +73,9 @@ class AdvancedEntropy extends Operation {
*/ */
calculateScanningEntropy(inputBytes, binWidth) { calculateScanningEntropy(inputBytes, binWidth) {
const entropyData = []; const entropyData = [];
binWidth = binWidth ?
Math.floor(inputBytes.length / binWidth) : if (inputBytes.length < 256) binWidth = 8;
Math.floor(inputBytes.length / 256); else binWidth = 256;
for (let bytePos = 0; bytePos < inputBytes.length; bytePos+=binWidth) { for (let bytePos = 0; bytePos < inputBytes.length; bytePos+=binWidth) {
const block = inputBytes.slice(bytePos, bytePos+binWidth); const block = inputBytes.slice(bytePos, bytePos+binWidth);
@ -145,14 +145,15 @@ class AdvancedEntropy extends Operation {
calculateByteFrequency(inputBytes) { calculateByteFrequency(inputBytes) {
const byteFrequency = []; const byteFrequency = [];
for (let i = 0; i < 256; i++) { for (let i = 0; i < 256; i++) {
let count = 0; if (inputBytes.length > 0) {
for (const byte of inputBytes) { let count = 0;
if (byte === i) { for (const byte of inputBytes) {
count++; if (byte === i) count++;
} }
byteFrequency.push(count / inputBytes.length);
} else {
byteFrequency.push(0);
} }
byteFrequency.push(count / (inputBytes.length + 1));
} }
return byteFrequency; return byteFrequency;
@ -179,7 +180,7 @@ class AdvancedEntropy extends Operation {
.attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`); .attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`);
const yScale = d3.scaleLinear() const yScale = d3.scaleLinear()
.domain(d3.extent(byteFrequency, d => d)) .domain([0, d3.max(byteFrequency, d => d)])
.range([svgHeight - margins.bottom, margins.top]); .range([svgHeight - margins.bottom, margins.top]);
const xScale = d3.scaleLinear() const xScale = d3.scaleLinear()
@ -193,8 +194,9 @@ class AdvancedEntropy extends Operation {
svg.append("path") svg.append("path")
.datum(byteFrequency) .datum(byteFrequency)
.attr("d", line) .attr("fill", "none")
.attr("fill", "steelblue"); .attr("stroke", "steelblue")
.attr("d", line);
this.createAxes(svg, xScale, yScale, svgHeight, svgWidth, margins, "", "Byte", "Byte Frequency"); this.createAxes(svg, xScale, yScale, svgHeight, svgWidth, margins, "", "Byte", "Byte Frequency");
@ -221,8 +223,9 @@ class AdvancedEntropy extends Operation {
.attr("height", "100%") .attr("height", "100%")
.attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`); .attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`);
const yExtent = d3.extent(byteFrequency, d => d);
const yScale = d3.scaleLinear() const yScale = d3.scaleLinear()
.domain(d3.extent(byteFrequency, d => d)) .domain(yExtent)
.range([svgHeight - margins.bottom, margins.top]); .range([svgHeight - margins.bottom, margins.top]);
const xScale = d3.scaleLinear() const xScale = d3.scaleLinear()
@ -235,7 +238,7 @@ class AdvancedEntropy extends Operation {
.attr("x", (_, i) => xScale(i) + binWidth) .attr("x", (_, i) => xScale(i) + binWidth)
.attr("y", dataPoint => yScale(dataPoint)) .attr("y", dataPoint => yScale(dataPoint))
.attr("width", binWidth) .attr("width", binWidth)
.attr("height", dataPoint => yScale(0) - yScale(dataPoint)) .attr("height", dataPoint => yScale(yExtent[0]) - yScale(dataPoint))
.attr("fill", "blue"); .attr("fill", "blue");
this.createAxes(svg, xScale, yScale, svgHeight, svgWidth, margins, "", "Byte", "Byte Frequency"); this.createAxes(svg, xScale, yScale, svgHeight, svgWidth, margins, "", "Byte", "Byte Frequency");
@ -350,7 +353,7 @@ class AdvancedEntropy extends Operation {
} else if (visualizationType === "Curve") { } else if (visualizationType === "Curve") {
return this.createEntropyCurve(this.calculateScanningEntropy(input).entropyData); return this.createEntropyCurve(this.calculateScanningEntropy(input).entropyData);
} else if (visualizationType === "Image") { } else if (visualizationType === "Image") {
return this.createEntropyImage(this.calculateScanningEntropy(input, 10000).entropyData); return this.createEntropyImage(this.calculateScanningEntropy(input).entropyData);
} }
} }
} }