Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

bring gifstream into lib folder #3647

Ben1da039fed40b
diff --git a/.eslintignore b/.eslintignore
index 8625f73a7..64e7a1501 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -11,4 +11,5 @@ web/dist/*
web/build/*
test/*
web/js/edsc/*
+web/js/lib/*
diff --git a/package-lock.json b/package-lock.json
index 1ee1d648d..b50fe6e0f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -3358,22 +3358,6 @@
"resolved": "https://registry.npmjs.org/@emotion/utils/-/utils-0.8.2.tgz",
"integrity": "sha512-rLu3wcBWH4P5q1CGoSSH/i9hrXs7SlbRLkoq9IGuoPYNGQvDJ3pt/wmOM+XgYjIDRMVIdkUWt0RsfzF50JfnCw=="
},
- "@entryline/gifstream": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/@entryline/gifstream/-/gifstream-1.2.1.tgz",
- "integrity": "sha512-RzVzK6yKQDIpo2ui0cWKa0/HA3BlnkWvuu5YIwcC9xz7uoantCUoSrgw1M0zqoSQSj6URB1MhJcWDs9SWLA+MQ==",
- "requires": {
- "bluebird": "^3.5.1",
- "web-streams-polyfill": "^2.0.4"
- },
- "dependencies": {
- "web-streams-polyfill": {
- "version": "2.0.5",
- "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-2.0.5.tgz",
- "integrity": "sha512-jECu/7ilpv3Q3bRP9yVtC+/DkEKM4imnIJMT7DD5Dx0TB9ylWpPtanpnQSxf4x2q1iyMr6kYEYD9hGipbRhh5w=="
- }
- }
- },
"@fortawesome/fontawesome-common-types": {
"version": "0.2.27",
"resolved": "https://registry.npmjs.org/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.2.27.tgz",
diff --git a/package.json b/package.json
index 4fac6a3b0..a5c73e5ea 100644
--- a/package.json
+++ b/package.json
@@ -168,7 +168,6 @@
"dependencies": {
"@elastic/react-search-ui": "^1.5.1",
"@elastic/react-search-ui-views": "^1.5.1",
- "@entryline/gifstream": "^1.2.0",
"@fortawesome/fontawesome-svg-core": "^1.2.27",
"@fortawesome/free-brands-svg-icons": "^5.12.1",
"@fortawesome/free-regular-svg-icons": "^5.12.1",
diff --git a/web/js/containers/gif.js b/web/js/containers/gif.js
index 0b2632031..35e037748 100644
--- a/web/js/containers/gif.js
+++ b/web/js/containers/gif.js
@@ -2,12 +2,12 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Spinner from 'react-loader';
import { connect } from 'react-redux';
-import GifStream from '@entryline/gifstream';
import * as olProj from 'ol/proj';
import { debounce as lodashDebounce, round as lodashRound } from 'lodash';
import {
Progress, Modal, ModalBody, ModalHeader,
} from 'reactstrap';
+import GifStream from '../lib/gifstream';
import GifPanel from '../components/animation-widget/gif-panel';
import util from '../util/util';
diff --git a/web/js/lib/gifstream.js b/web/js/lib/gifstream.js
new file mode 100644
index 000000000..74066423f
--- /dev/null
+++ b/web/js/lib/gifstream.js
@@ -0,0 +1,293 @@
+import Promise from 'bluebird';
+import GifWriter from './gifwriter';
+import NeuQuant from './neuquant';
+
+Promise.config({ cancellation: true });
+export default class GifStream {
+ constructor() {
+ this.canvas = null;
+ this.ctx = null;
+ }
+
+ /**
+ * Add frame contents to canvas
+ *
+ * https://github.com/yahoo/gifshot/blob/master/src/modules/core/AnimatedGIF.js
+ *
+ * @param {Object} ctx 2d Canvas Context
+ * @param {Object} img Image Element
+ * @param {String} frameText Text to be applied to this string
+ *
+ * @return {Object} 2d Canvas Context
+ */
+ addFrameDetails(ctx, img) {
+ const { options } = this;
+ const frameText = img.text;
+ const {
+ fontColor,
+ fontSize,
+ fontFamily,
+ fontWeight,
+ gifHeight,
+ gifWidth,
+ text,
+ textAlign,
+ textBaseline,
+ waterMark,
+ waterMarkHeight,
+ waterMarkWidth,
+ waterMarkXCoordinate,
+ waterMarkYCoordinate,
+ } = options;
+ const textXCoordinate = options.textXCoordinate
+ ? options.textXCoordinate
+ : textAlign === 'left'
+ ? 1
+ : textAlign === 'right'
+ ? gifWidth
+ : gifWidth / 2;
+ const textYCoordinate = options.textYCoordinate
+ ? options.textYCoordinate
+ : textBaseline === 'top'
+ ? 1
+ : textBaseline === 'center'
+ ? gifHeight / 2
+ : gifHeight;
+ const font = `${fontWeight} ${fontSize} ${fontFamily}`;
+ const textToUse = frameText && options.showFrameText ? frameText : text;
+
+ try {
+ ctx.drawImage(img, 0, 0, gifWidth, gifHeight);
+ if (textToUse) {
+ ctx.font = font;
+ ctx.fillStyle = fontColor;
+ ctx.textAlign = textAlign;
+ ctx.textBaseline = textBaseline;
+ if (options.stroke) {
+ const { stroke } = options;
+ ctx.strokeStyle = stroke.color;
+ ctx.lineWidth = stroke.pixels * 2;
+ ctx.strokeText(frameText, textXCoordinate, textYCoordinate);
+ }
+ ctx.fillText(textToUse, textXCoordinate, textYCoordinate);
+ }
+ if (waterMark) {
+ ctx.drawImage(
+ waterMark,
+ waterMarkXCoordinate,
+ waterMarkYCoordinate,
+ waterMarkWidth,
+ waterMarkHeight,
+ );
+ }
+ return ctx;
+ } catch (e) {
+ return `${e}`;
+ }
+ }
+
+ cancel() {
+ this.cancelled = true;
+ if (this.promise) {
+ this.promise.cancel();
+ } else {
+ this.cancelled = true;
+ }
+ }
+
+ /**
+ * Create GIF from options
+ *
+ * @param {Object} options GIF options
+ * @param {Function} callback Function to call once GIF is created
+ * @return {void}
+ */
+ createGIF(options, callback) {
+ if (this.canvas) { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); }
+ this.canvas = document.createElement('canvas');
+ this.cancelled = false;
+ this.ctx = this.canvas.getContext('2d');
+ this.options = options;
+ this.canvas.width = options.gifWidth;
+ this.canvas.height = options.gifHeight;
+
+ const imagePromiseArray = [];
+ if (options.images.length < 1) {
+ throw new Error('No images found');
+ }
+ options.images.forEach((imageObj) => {
+ imagePromiseArray.push(this.getImagePromise(imageObj).catch(returnError));
+ });
+ function returnError(e) {
+ const callbackObj = {
+ blob: null,
+ error: e,
+ };
+ callback(callbackObj);
+ }
+ function returnCancel() {
+ console.warn('GIF creation has been cancelled');
+ const callbackObj = {
+ cancelled: true,
+ };
+ callback(callbackObj);
+ }
+
+ this.promise = Promise.all(imagePromiseArray).then((images) => {
+ const gifStream = this.getStream(images, this.ctx);
+ const reader = gifStream.getReader();
+ const chunks = [];
+ let processedImages = 1;
+ const finished = (chunks) => {
+ const callbackObj = {
+ blob: new Blob(chunks, { type: 'image/gif' }),
+ error: '',
+ };
+ callback(callbackObj);
+ };
+
+ const pull = () => {
+ const imageLength = options.images.length;
+ return reader.read().then((result) => {
+ const chunk = result.value;
+ if (result.done) {
+ finished(chunks);
+ } else if (this.cancelled) {
+ returnCancel();
+ } else {
+ chunks.push(new Uint8Array(chunk));
+ options.progressCallback(
+ Math.round((processedImages / imageLength) * 100),
+ );
+ processedImages++;
+ setTimeout(() =>
+ // This was needed in order for callback to be applied
+ pull(),
+ 10);
+ }
+ });
+ };
+ options.progressCallback(0);
+ pull();
+ });
+ }
+
+ getImagePromise(frame) {
+ return new Promise((resolve, reject, onCancel) => {
+ const img = new Image();
+ img.width = this.options.gifWidth;
+ img.height = this.options.gifHeight;
+ img.text = frame.text;
+ img.delay = frame.delay;
+ img.crossOrigin = 'Anonymous';
+ img.onload = () => {
+ URL.revokeObjectURL(img.src); // Free up some memory
+ resolve(img);
+ delete img.onload;
+ delete img.onerror;
+ };
+ img.onerror = (e) => {
+ reject(e);
+ delete img.onload;
+ delete img.onerror;
+ };
+ img.src = frame.src;
+ onCancel(() => {
+ img.src = ''; // https://stackoverflow.com/a/5278475
+ });
+ });
+ }
+
+ /**
+ * Generate GIF-creating Stream
+ * @param {Array} frames Array of GIF image objects
+ * @param {Object} ctx 2d Canvas Cntext
+ * @return {Object} Returns GifWriter Stream
+ */
+ getStream(frames, ctx) {
+ const { options } = this;
+ const width = options.gifWidth;
+ const height = options.gifHeight;
+ const totalImages = frames.length;
+ let processedImages = 0;
+ const self = this;
+ const rs = new ReadableStream({
+ pull: function pull(controller) {
+ const frame = frames.shift();
+ if (!frame) controller.close();
+ let imgData;
+ let rgbComponents;
+ let paletteRGB;
+ let nq;
+ let paletteArray;
+ let numberPixels;
+ let pixels;
+ let r;
+ let g;
+ let b;
+ let k = 0;
+ let delay = frame.delay ? frame.delay / 10 : 100;
+ processedImages++;
+ delay = processedImages === totalImages && options.extraLastFrameDelay
+ ? delay + options.extraLastFrameDelay / 10
+ : delay; // Add an extra
+ ctx = self.addFrameDetails(ctx, frame);
+ imgData = ctx.getImageData(0, 0, width, height);
+ rgbComponents = dataToRGB(imgData.data, imgData.width, imgData.height);
+ nq = new NeuQuant(rgbComponents, rgbComponents.length, 15);
+ paletteRGB = nq.process();
+ paletteArray = new Uint32Array(componentizedPaletteToArray(paletteRGB));
+ numberPixels = imgData.height * imgData.width;
+ pixels = new Uint8Array(imgData.height * imgData.width);
+ for (let i = 0; i < numberPixels; i++) {
+ r = rgbComponents[k++];
+ g = rgbComponents[k++];
+ b = rgbComponents[k++];
+ pixels[i] = nq.map(r, g, b);
+ }
+ controller.enqueue([
+ 0,
+ 0,
+ width,
+ height,
+ pixels,
+ {
+ palette: paletteArray,
+ delay,
+ },
+ ]);
+ },
+ });
+ return new GifWriter(rs, width, height, {
+ loop: options.loop || 0, // From GIF: 0 = loop forever, null = not looping, n > 0 = loop n times and stop
+ });
+ }
+}
+
+// part of neuquant conversion
+function componentizedPaletteToArray(paletteRGB) {
+ const paletteArray = [];
+ let r;
+ let g;
+ let b;
+ for (let i = 0; i < paletteRGB.length; i += 3) {
+ r = paletteRGB[i];
+ g = paletteRGB[i + 1];
+ b = paletteRGB[i + 2];
+ paletteArray.push((r << 16) | (g << 8) | b);
+ }
+ return paletteArray;
+}
+// part of neuquant conversion
+function dataToRGB(data, width, height) {
+ let i = 0;
+ const length = width * height * 4;
+ const rgb = [];
+ while (i < length) {
+ rgb.push(data[i++]);
+ rgb.push(data[i++]);
+ rgb.push(data[i++]);
+ i++;
+ }
+ return rgb;
+}
diff --git a/web/js/lib/gifwriter.js b/web/js/lib/gifwriter.js
new file mode 100644
index 000000000..a81079a40
--- /dev/null
+++ b/web/js/lib/gifwriter.js
@@ -0,0 +1,379 @@
+/*
+ gifWriter.js
+ ============
+*/
+
+// (c) Dean McNamee <[email protected]>, 2013.
+//
+// https://github.com/deanm/omggif
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+//
+// omggif is a JavaScript implementation of a GIF 89a encoder and decoder,
+// including animation and compression. It does not rely on any specific
+// underlying system, so should run in the browser, Node, or Plask.
+
+let loop_count;
+let global_palette;
+let buf;
+let p;
+
+export default class gifWriter {
+ constructor(rs, width, height, gopts) {
+ const buf = [];
+ let p = 0;
+ gopts = gopts === undefined ? {} : gopts;
+ loop_count = gopts.loop === undefined ? null : gopts.loop;
+ global_palette = gopts.palette === undefined ? null : gopts.palette;
+
+ if (width <= 0 || height <= 0 || width > 65535 || height > 65535) { throw 'Width/Height invalid.'; }
+
+ // - Header.
+ buf[p++] = 0x47;
+ buf[p++] = 0x49;
+ buf[p++] = 0x46; // GIF
+ buf[p++] = 0x38;
+ buf[p++] = 0x39;
+ buf[p++] = 0x61; // 89a
+
+ // Handling of Global Color Table (palette) and background index.
+ const gp_num_colors_pow2 = 0;
+ const background = 0;
+
+ // - Logical Screen Descriptor.
+ // NOTE(deanm): w/h apparently ignored by implementations, but set anyway.
+ buf[p++] = width & 0xff;
+ buf[p++] = (width >> 8) & 0xff;
+ buf[p++] = height & 0xff;
+ buf[p++] = (height >> 8) & 0xff;
+ // NOTE: Indicates 0-bpp original color resolution (unused?).
+ buf[p++] = (global_palette !== null ? 0x80 : 0) | gp_num_colors_pow2; // Global Color Table Flag. // NOTE: No sort flag (unused?).
+ buf[p++] = background; // Background Color Index.
+ buf[p++] = 0; // Pixel aspect ratio (unused?).
+
+ if (loop_count !== null) {
+ // Netscape block for looping.
+ if (loop_count < 0 || loop_count > 65535) throw 'Loop count invalid.';
+
+ // Extension code, label, and length.
+ buf[p++] = 0x21;
+ buf[p++] = 0xff;
+ buf[p++] = 0x0b;
+ // NETSCAPE2.0
+ buf[p++] = 0x4e;
+ buf[p++] = 0x45;
+ buf[p++] = 0x54;
+ buf[p++] = 0x53;
+ buf[p++] = 0x43;
+ buf[p++] = 0x41;
+ buf[p++] = 0x50;
+ buf[p++] = 0x45;
+ buf[p++] = 0x32;
+ buf[p++] = 0x2e;
+ buf[p++] = 0x30;
+ // Sub-block
+ buf[p++] = 0x03;
+ buf[p++] = 0x01;
+ buf[p++] = loop_count & 0xff;
+ buf[p++] = (loop_count >> 8) & 0xff;
+ buf[p++] = 0x00; // Terminator.
+ }
+ const self = this;
+ const reader = rs.getReader();
+
+ return new ReadableStream({
+ start: function start(controller) {
+ controller.enqueue(new Uint8Array(buf));
+ },
+ pull: function pull(controller) {
+ return reader.read().then((_ref2) => {
+ const { done } = _ref2;
+ const { value } = _ref2;
+
+ if (done) {
+ controller.enqueue(new Uint8Array([0x3b]));
+ controller.close();
+ return;
+ }
+
+ self.addFrame.apply(self, [controller].concat(value));
+ });
+ },
+ });
+ }
+
+ addFrame(controller, x, y, w, h, indexed_pixels, opts) {
+ buf = [];
+ p = 0;
+ opts = opts === undefined ? {} : opts;
+ // TODO(deanm): Bounds check x, y. Do they need to be within the virtual
+ // canvas width/height, I imagine?
+ if (x < 0 || y < 0 || x > 65535 || y > 65535) throw 'x/y invalid.';
+
+ if (w <= 0 || h <= 0 || w > 65535 || h > 65535) { throw 'Width/Height invalid.'; }
+
+ if (indexed_pixels.length < w * h) { throw 'Not enough pixels for the frame size.'; }
+
+ let using_local_palette = true;
+ let { palette } = opts;
+ if (palette === undefined || palette === null) {
+ using_local_palette = false;
+ palette = global_palette;
+ }
+
+ if (palette === undefined || palette === null) { throw 'Must supply either a local or global palette.'; }
+
+ let num_colors = check_palette_and_num_colors(palette);
+
+ // Compute the min_code_size (power of 2), destroying num_colors.
+ let min_code_size = 0;
+ while (num_colors >>= 1) ++min_code_size;
+ num_colors = 1 << min_code_size; // Now we can easily get it back.
+
+ const delay = opts.delay === undefined ? 0 : opts.delay;
+
+ // From the spec:
+ // 0 - No disposal specified. The decoder is
+ // not required to take any action.
+ // 1 - Do not dispose. The graphic is to be left
+ // in place.
+ // 2 - Restore to background color. The area used by the
+ // graphic must be restored to the background color.
+ // 3 - Restore to previous. The decoder is required to
+ // restore the area overwritten by the graphic with
+ // what was there prior to rendering the graphic.
+ // 4-7 - To be defined.
+ // NOTE(deanm): Dispose background doesn't really work, apparently most
+ // browsers ignore the background palette index and clear to transparency.
+ const disposal = opts.disposal === undefined ? 0 : opts.disposal;
+ if (disposal < 0 || disposal > 3)
+ // 4-7 is reserved.
+ { throw 'Disposal out of range.'; }
+
+ let use_transparency = false;
+ let transparent_index = 0;
+ if (opts.transparent !== undefined && opts.transparent !== null) {
+ use_transparency = true;
+ transparent_index = opts.transparent;
+ if (transparent_index < 0 || transparent_index >= num_colors) { throw 'Transparent color index.'; }
+ }
+
+ if (disposal !== 0 || use_transparency || delay !== 0) {
+ // - Graphics Control Extension
+ buf[p++] = 0x21;
+ buf[p++] = 0xf9; // Extension / Label.
+ buf[p++] = 4; // Byte size.
+
+ buf[p++] = (disposal << 2) | (use_transparency === true ? 1 : 0);
+ buf[p++] = delay & 0xff;
+ buf[p++] = (delay >> 8) & 0xff;
+ buf[p++] = transparent_index; // Transparent color index.
+ buf[p++] = 0; // Block Terminator.
+ }
+
+ // - Image Descriptor
+ buf[p++] = 0x2c; // Image Seperator.
+ buf[p++] = x & 0xff;
+ buf[p++] = (x >> 8) & 0xff; // Left.
+ buf[p++] = y & 0xff;
+ buf[p++] = (y >> 8) & 0xff; // Top.
+ buf[p++] = w & 0xff;
+ buf[p++] = (w >> 8) & 0xff;
+ buf[p++] = h & 0xff;
+ buf[p++] = (h >> 8) & 0xff;
+ // NOTE: No sort flag (unused?).
+ // TODO(deanm): Support interlace.
+ buf[p++] = using_local_palette === true ? 0x80 | (min_code_size - 1) : 0;
+
+ // - Local Color Table
+ if (using_local_palette === true) {
+ for (let i = 0, il = palette.length; i < il; ++i) {
+ const rgb = palette[i];
+ buf[p++] = (rgb >> 16) & 0xff;
+ buf[p++] = (rgb >> 8) & 0xff;
+ buf[p++] = rgb & 0xff;
+ }
+ }
+
+ p = GifWriterOutputLZWCodeStream(
+ buf,
+ p,
+ min_code_size < 2 ? 2 : min_code_size,
+ indexed_pixels,
+ );
+ controller.enqueue(new Uint8Array(buf));
+ }
+}
+// Main compression routine, palette indexes -> LZW code stream.
+// |index_stream| must have at least one entry.
+function GifWriterOutputLZWCodeStream(buf, p, min_code_size, index_stream) {
+ buf[p++] = min_code_size;
+ let cur_subblock = p++; // Pointing at the length field.
+
+ const clear_code = 1 << min_code_size;
+ const code_mask = clear_code - 1;
+ const eoi_code = clear_code + 1;
+ let next_code = eoi_code + 1;
+
+ let cur_code_size = min_code_size + 1; // Number of bits per code.
+ let cur_shift = 0;
+ // We have at most 12-bit codes, so we should have to hold a max of 19
+ // bits here (and then we would write out).
+ let cur = 0;
+
+ function emit_bytes_to_buffer(bit_block_size) {
+ while (cur_shift >= bit_block_size) {
+ buf[p++] = cur & 0xff;
+ cur >>= 8;
+ cur_shift -= 8;
+ if (p === cur_subblock + 256) {
+ // Finished a subblock.
+ buf[cur_subblock] = 255;
+ cur_subblock = p++;
+ }
+ }
+ }
+
+ function emit_code(c) {
+ cur |= c << cur_shift;
+ cur_shift += cur_code_size;
+ emit_bytes_to_buffer(8);
+ }
+
+ // I am not an expert on the topic, and I don't want to write a thesis.
+ // However, it is good to outline here the basic algorithm and the few data
+ // structures and optimizations here that make this implementation fast.
+ // The basic idea behind LZW is to build a table of previously seen runs
+ // addressed by a short id (herein called output code). All data is
+ // referenced by a code, which represents one or more values from the
+ // original input stream. All input bytes can be referenced as the same
+ // value as an output code. So if you didn't want any compression, you
+ // could more or less just output the original bytes as codes (there are
+ // some details to this, but it is the idea). In order to achieve
+ // compression, values greater then the input range (codes can be up to
+ // 12-bit while input only 8-bit) represent a sequence of previously seen
+ // inputs. The decompressor is able to build the same mapping while
+ // decoding, so there is always a shared common knowledge between the
+ // encoding and decoder, which is also important for "timing" aspects like
+ // how to handle variable bit width code encoding.
+ //
+ // One obvious but very important consequence of the table system is there
+ // is always a unique id (at most 12-bits) to map the runs. 'A' might be
+ // 4, then 'AA' might be 10, 'AAA' 11, 'AAAA' 12, etc. This relationship
+ // can be used for an effecient lookup strategy for the code mapping. We
+ // need to know if a run has been seen before, and be able to map that run
+ // to the output code. Since we start with known unique ids (input bytes),
+ // and then from those build more unique ids (table entries), we can
+ // continue this chain (almost like a linked list) to always have small
+ // integer values that represent the current byte chains in the encoder.
+ // This means instead of tracking the input bytes (AAAABCD) to know our
+ // current state, we can track the table entry for AAAABC (it is guaranteed
+ // to exist by the nature of the algorithm) and the next character D.
+ // Therefor the tuple of (table_entry, byte) is guaranteed to also be
+ // unique. This allows us to create a simple lookup key for mapping input
+ // sequences to codes (table indices) without having to store or search
+ // any of the code sequences. So if 'AAAA' has a table entry of 12, the
+ // tuple of ('AAAA', K) for any input byte K will be unique, and can be our
+ // key. This leads to a integer value at most 20-bits, which can always
+ // fit in an SMI value and be used as a fast sparse array / object key.
+
+ // Output code for the current contents of the index buffer.
+ let ib_code = index_stream[0] & code_mask; // Load first input index.
+ let code_table = {}; // Key'd on our 20-bit "tuple".
+
+ emit_code(clear_code); // Spec says first code should be a clear code.
+
+ // First index already loaded, process the rest of the stream.
+ for (let i = 1, il = index_stream.length; i < il; ++i) {
+ const k = index_stream[i] & code_mask;
+ const cur_key = (ib_code << 8) | k; // (prev, k) unique tuple.
+ const cur_code = code_table[cur_key]; // buffer + k.
+
+ // Check if we have to create a new code table entry.
+ if (cur_code === undefined) {
+ // We don't have buffer + k.
+ // Emit index buffer (without k).
+ // This is an inline version of emit_code, because this is the core
+ // writing routine of the compressor (and V8 cannot inline emit_code
+ // because it is a closure here in a different context). Additionally
+ // we can call emit_byte_to_buffer less often, because we can have
+ // 30-bits (from our 31-bit signed SMI), and we know our codes will only
+ // be 12-bits, so can safely have 18-bits there without overflow.
+ // emit_code(ib_code);
+ cur |= ib_code << cur_shift;
+ cur_shift += cur_code_size;
+ while (cur_shift >= 8) {
+ buf[p++] = cur & 0xff;
+ cur >>= 8;
+ cur_shift -= 8;
+ if (p === cur_subblock + 256) {
+ // Finished a subblock.
+ buf[cur_subblock] = 255;
+ cur_subblock = p++;
+ }
+ }
+
+ if (next_code === 4096) {
+ // Table full, need a clear.
+ emit_code(clear_code);
+ next_code = eoi_code + 1;
+ cur_code_size = min_code_size + 1;
+ code_table = {};
+ } else {
+ // Table not full, insert a new entry.
+ // Increase our variable bit code sizes if necessary. This is a bit
+ // tricky as it is based on "timing" between the encoding and
+ // decoder. From the encoders perspective this should happen after
+ // we've already emitted the index buffer and are about to create the
+ // first table entry that would overflow our current code bit size.
+ if (next_code >= 1 << cur_code_size) ++cur_code_size;
+ code_table[cur_key] = next_code++; // Insert into code table.
+ }
+
+ ib_code = k; // Index buffer to single input k.
+ } else {
+ ib_code = cur_code; // Index buffer to sequence in code table.
+ }
+ }
+
+ emit_code(ib_code); // There will still be something in the index buffer.
+ emit_code(eoi_code); // End Of Information.
+
+ // Flush / finalize the sub-blocks stream to the buffer.
+ emit_bytes_to_buffer(1);
+
+ // Finish the sub-blocks, writing out any unfinished lengths and
+ // terminating with a sub-block of length 0. If we have already started
+ // but not yet used a sub-block it can just become the terminator.
+ if (cur_subblock + 1 === p) {
+ // Started but unused.
+ buf[cur_subblock] = 0;
+ } else {
+ // Started and used, write length and additional terminator block.
+ buf[cur_subblock] = p - cur_subblock - 1;
+ buf[p++] = 0;
+ }
+ return p;
+}
+function check_palette_and_num_colors(palette) {
+ const num_colors = palette.length;
+
+ if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors - 1)) { throw 'Invalid code/color length, must be power of 2 and 2 .. 256.'; }
+ return num_colors;
+}
diff --git a/web/js/lib/neuquant.js b/web/js/lib/neuquant.js
new file mode 100644
index 000000000..aa7518ed8
--- /dev/null
+++ b/web/js/lib/neuquant.js
@@ -0,0 +1,550 @@
+/*
+ NeuQuant.js
+ ===========
+*/
+
+/*
+ * NeuQuant Neural-Net Quantization Algorithm
+ * ------------------------------------------
+ *
+ * Copyright (c) 1994 Anthony Dekker
+ *
+ * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
+ * "Kohonen neural networks for optimal colour quantization" in "Network:
+ * Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
+ * the algorithm.
+ *
+ * Any party obtaining a copy of these files from the author, directly or
+ * indirectly, is granted, free of charge, a full and unrestricted irrevocable,
+ * world-wide, paid up, royalty-free, nonexclusive right and license to deal in
+ * this software and documentation files (the "Software"), including without
+ * limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons who
+ * receive copies from any such party to do so, with the only requirement being
+ * that this copyright notice remain intact.
+ */
+
+/*
+ * This class handles Neural-Net quantization algorithm
+ * @author Kevin Weiner (original Java version - [email protected])
+ * @author Thibault Imbert (AS3 version - bytearray.org)
+ * @version 0.1 AS3 implementation
+ * @version 0.2 JS->AS3 "translation" by antimatter15
+ * @version 0.3 JS clean up + using modern JS idioms by sole - http://soledadpenades.com
+ * Also implement fix in color conversion described at http://stackoverflow.com/questions/16371712/neuquant-js-javascript-color-quantization-hidden-bug-in-js-conversion
+ */
+
+export default function NeuQuant() {
+ const netsize = 256; // number of colours used
+
+ // four primes near 500 - assume no image has a length so large
+ // that it is divisible by all four primes
+ const prime1 = 499;
+ const prime2 = 491;
+ const prime3 = 487;
+ const prime4 = 503;
+
+ // minimum size for input image
+ const minpicturebytes = 3 * prime4;
+
+ // Network Definitions
+
+ const maxnetpos = netsize - 1;
+ const netbiasshift = 4; // bias for colour values
+ const ncycles = 100; // no. of learning cycles
+
+ // defs for freq and bias
+ const intbiasshift = 16; // bias for fractions
+ const intbias = 1 << intbiasshift;
+ const gammashift = 10; // gamma = 1024
+ const gamma = 1 << gammashift;
+ const betashift = 10;
+ const beta = intbias >> betashift; // beta = 1/1024
+ const betagamma = intbias << (gammashift - betashift);
+
+ // defs for decreasing radius factor
+ // For 256 colors, radius starts at 32.0 biased by 6 bits
+ // and decreases by a factor of 1/30 each cycle
+ const initrad = netsize >> 3;
+ const radiusbiasshift = 6;
+ const radiusbias = 1 << radiusbiasshift;
+ const initradius = initrad * radiusbias;
+ const radiusdec = 30;
+
+ // defs for decreasing alpha factor
+ // Alpha starts at 1.0 biased by 10 bits
+ const alphabiasshift = 10;
+ const initalpha = 1 << alphabiasshift;
+ let alphadec;
+
+ // radbias and alpharadbias used for radpower calculation
+ const radbiasshift = 8;
+ const radbias = 1 << radbiasshift;
+ const alpharadbshift = alphabiasshift + radbiasshift;
+ const alpharadbias = 1 << alpharadbshift;
+
+ // Input image
+ let thepicture;
+ // Height * Width * 3
+ let lengthcount;
+ // Sampling factor 1..30
+ let samplefac;
+
+ // The network itself
+ let network;
+ const netindex = [];
+
+ // for network lookup - really 256
+ const bias = [];
+
+ // bias and freq arrays for learning
+ const freq = [];
+ const radpower = [];
+
+ function NeuQuantConstructor(thepic, len, sample) {
+ let i;
+ let p;
+
+ thepicture = thepic;
+ lengthcount = len;
+ samplefac = sample;
+
+ network = new Array(netsize);
+
+ for (i = 0; i < netsize; i++) {
+ network[i] = new Array(4);
+ p = network[i];
+ p[0] = p[1] = p[2] = ((i << (netbiasshift + 8)) / netsize) | 0;
+ freq[i] = (intbias / netsize) | 0; // 1 / netsize
+ bias[i] = 0;
+ }
+ }
+
+ function colorMap() {
+ const map = [];
+ const index = new Array(netsize);
+ for (let i = 0; i < netsize; i++) index[network[i][3]] = i;
+ let k = 0;
+ for (let l = 0; l < netsize; l++) {
+ const j = index[l];
+ map[k++] = network[j][0];
+ map[k++] = network[j][1];
+ map[k++] = network[j][2];
+ }
+ return map;
+ }
+
+ // Insertion sort of network and building of netindex[0..255]
+ // (to do after unbias)
+ function inxbuild() {
+ let i;
+ let j;
+ let smallpos;
+ let smallval;
+ let p;
+ let q;
+ let previouscol;
+ let startpos;
+
+ previouscol = 0;
+ startpos = 0;
+
+ for (i = 0; i < netsize; i++) {
+ p = network[i];
+ smallpos = i;
+ smallval = p[1]; // index on g
+ // find smallest in i..netsize-1
+ for (j = i + 1; j < netsize; j++) {
+ q = network[j];
+
+ if (q[1] < smallval) {
+ // index on g
+ smallpos = j;
+ smallval = q[1]; // index on g
+ }
+ }
+
+ q = network[smallpos];
+
+ // swap p (i) and q (smallpos) entries
+ if (i != smallpos) {
+ j = q[0];
+ q[0] = p[0];
+ p[0] = j;
+ j = q[1];
+ q[1] = p[1];
+ p[1] = j;
+ j = q[2];
+ q[2] = p[2];
+ p[2] = j;
+ j = q[3];
+ q[3] = p[3];
+ p[3] = j;
+ }
+
+ // smallval entry is now in position i
+ if (smallval != previouscol) {
+ netindex[previouscol] = (startpos + i) >> 1;
+
+ for (j = previouscol + 1; j < smallval; j++) {
+ netindex[j] = i;
+ }
+
+ previouscol = smallval;
+ startpos = i;
+ }
+ }
+
+ netindex[previouscol] = (startpos + maxnetpos) >> 1;
+ for (j = previouscol + 1; j < 256; j++) {
+ netindex[j] = maxnetpos; // really 256
+ }
+ }
+
+ // Main Learning Loop
+
+ function learn() {
+ let i;
+ let j;
+ let b;
+ let g;
+ let r;
+ let radius;
+ let rad;
+ let alpha;
+ let step;
+ let delta;
+ let samplepixels;
+ let p;
+ let pix;
+ let lim;
+
+ if (lengthcount < minpicturebytes) {
+ samplefac = 1;
+ }
+
+ alphadec = 30 + (samplefac - 1) / 3;
+ p = thepicture;
+ pix = 0;
+ lim = lengthcount;
+ samplepixels = lengthcount / (3 * samplefac);
+ delta = (samplepixels / ncycles) | 0;
+ alpha = initalpha;
+ radius = initradius;
+
+ rad = radius >> radiusbiasshift;
+ if (rad <= 1) {
+ rad = 0;
+ }
+
+ for (i = 0; i < rad; i++) {
+ radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad));
+ }
+
+ if (lengthcount < minpicturebytes) {
+ step = 3;
+ } else if (lengthcount % prime1 !== 0) {
+ step = 3 * prime1;
+ } else if (lengthcount % prime2 !== 0) {
+ step = 3 * prime2;
+ } else if (lengthcount % prime3 !== 0) {
+ step = 3 * prime3;
+ } else {
+ step = 3 * prime4;
+ }
+
+ i = 0;
+
+ while (i < samplepixels) {
+ b = (p[pix + 0] & 0xff) << netbiasshift;
+ g = (p[pix + 1] & 0xff) << netbiasshift;
+ r = (p[pix + 2] & 0xff) << netbiasshift;
+ j = contest(b, g, r);
+
+ altersingle(alpha, j, b, g, r);
+
+ if (rad !== 0) {
+ // Alter neighbours
+ alterneigh(rad, j, b, g, r);
+ }
+
+ pix += step;
+
+ if (pix >= lim) {
+ pix -= lengthcount;
+ }
+
+ i++;
+
+ if (delta === 0) {
+ delta = 1;
+ }
+
+ if (i % delta === 0) {
+ alpha -= alpha / alphadec;
+ radius -= radius / radiusdec;
+ rad = radius >> radiusbiasshift;
+
+ if (rad <= 1) {
+ rad = 0;
+ }
+
+ for (j = 0; j < rad; j++) {
+ radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad));
+ }
+ }
+ }
+ }
+
+ // Search for BGR values 0..255 (after net is unbiased) and return colour index
+ function map(b, g, r) {
+ let i;
+ let j;
+ let dist;
+ let a;
+ let bestd;
+ let p;
+ let best;
+
+ // Biggest possible distance is 256 * 3
+ bestd = 1000;
+ best = -1;
+ i = netindex[g]; // index on g
+ j = i - 1; // start at netindex[g] and work outwards
+
+ while (i < netsize || j >= 0) {
+ if (i < netsize) {
+ p = network[i];
+
+ dist = p[1] - g; // inx key
+
+ if (dist >= bestd) {
+ i = netsize; // stop iter
+ } else {
+ i++;
+
+ if (dist < 0) {
+ dist = -dist;
+ }
+
+ a = p[0] - b;
+
+ if (a < 0) {
+ a = -a;
+ }
+
+ dist += a;
+
+ if (dist < bestd) {
+ a = p[2] - r;
+
+ if (a < 0) {
+ a = -a;
+ }
+
+ dist += a;
+
+ if (dist < bestd) {
+ bestd = dist;
+ best = p[3];
+ }
+ }
+ }
+ }
+
+ if (j >= 0) {
+ p = network[j];
+
+ dist = g - p[1]; // inx key - reverse dif
+
+ if (dist >= bestd) {
+ j = -1; // stop iter
+ } else {
+ j--;
+ if (dist < 0) {
+ dist = -dist;
+ }
+ a = p[0] - b;
+ if (a < 0) {
+ a = -a;
+ }
+ dist += a;
+
+ if (dist < bestd) {
+ a = p[2] - r;
+ if (a < 0) {
+ a = -a;
+ }
+ dist += a;
+ if (dist < bestd) {
+ bestd = dist;
+ best = p[3];
+ }
+ }
+ }
+ }
+ }
+
+ return best;
+ }
+
+ function process() {
+ learn();
+ unbiasnet();
+ inxbuild();
+ return colorMap();
+ }
+
+ // Unbias network to give byte values 0..255 and record position i
+ // to prepare for sort
+ function unbiasnet() {
+ let i;
+ let j;
+
+ for (i = 0; i < netsize; i++) {
+ network[i][0] >>= netbiasshift;
+ network[i][1] >>= netbiasshift;
+ network[i][2] >>= netbiasshift;
+ network[i][3] = i; // record colour no
+ }
+ }
+
+ // Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2))
+ // in radpower[|i-j|]
+ function alterneigh(rad, i, b, g, r) {
+ let j;
+ let k;
+ let lo;
+ let hi;
+ let a;
+ let m;
+
+ let p;
+
+ lo = i - rad;
+ if (lo < -1) {
+ lo = -1;
+ }
+
+ hi = i + rad;
+
+ if (hi > netsize) {
+ hi = netsize;
+ }
+
+ j = i + 1;
+ k = i - 1;
+ m = 1;
+
+ while (j < hi || k > lo) {
+ a = radpower[m++];
+
+ if (j < hi) {
+ p = network[j++];
+
+ try {
+ p[0] -= ((a * (p[0] - b)) / alpharadbias) | 0;
+ p[1] -= ((a * (p[1] - g)) / alpharadbias) | 0;
+ p[2] -= ((a * (p[2] - r)) / alpharadbias) | 0;
+ } catch (e) {}
+ }
+
+ if (k > lo) {
+ p = network[k--];
+
+ try {
+ p[0] -= ((a * (p[0] - b)) / alpharadbias) | 0;
+ p[1] -= ((a * (p[1] - g)) / alpharadbias) | 0;
+ p[2] -= ((a * (p[2] - r)) / alpharadbias) | 0;
+ } catch (e) {}
+ }
+ }
+ }
+
+ // Move neuron i towards biased (b,g,r) by factor alpha
+ function altersingle(alpha, i, b, g, r) {
+ // alter hit neuron
+ const n = network[i];
+ const alphaMult = alpha / initalpha;
+ n[0] -= (alphaMult * (n[0] - b)) | 0;
+ n[1] -= (alphaMult * (n[1] - g)) | 0;
+ n[2] -= (alphaMult * (n[2] - r)) | 0;
+ }
+
+ // Search for biased BGR values
+ function contest(b, g, r) {
+ // finds closest neuron (min dist) and updates freq
+ // finds best neuron (min dist-bias) and returns position
+ // for frequently chosen neurons, freq[i] is high and bias[i] is negative
+ // bias[i] = gamma*((1/netsize)-freq[i])
+
+ let i;
+ let dist;
+ let a;
+ let biasdist;
+ let betafreq;
+ let bestpos;
+ let bestbiaspos;
+ let bestd;
+ let bestbiasd;
+ let n;
+
+ bestd = ~(1 << 31);
+ bestbiasd = bestd;
+ bestpos = -1;
+ bestbiaspos = bestpos;
+
+ for (i = 0; i < netsize; i++) {
+ n = network[i];
+ dist = n[0] - b;
+
+ if (dist < 0) {
+ dist = -dist;
+ }
+
+ a = n[1] - g;
+
+ if (a < 0) {
+ a = -a;
+ }
+
+ dist += a;
+
+ a = n[2] - r;
+
+ if (a < 0) {
+ a = -a;
+ }
+
+ dist += a;
+
+ if (dist < bestd) {
+ bestd = dist;
+ bestpos = i;
+ }
+
+ biasdist = dist - (bias[i] >> (intbiasshift - netbiasshift));
+
+ if (biasdist < bestbiasd) {
+ bestbiasd = biasdist;
+ bestbiaspos = i;
+ }
+
+ betafreq = freq[i] >> betashift;
+ freq[i] -= betafreq;
+ bias[i] += betafreq << gammashift;
+ }
+
+ freq[bestpos] += beta;
+ bias[bestpos] -= betagamma;
+ return bestbiaspos;
+ }
+
+ NeuQuantConstructor.apply(this, arguments);
+
+ const exports = {};
+ exports.map = map;
+ exports.process = process;
+
+ return exports;
+}