mirror of
https://github.com/psychopy/psychojs.git
synced 2025-05-10 10:40:54 +00:00
removed unused code from gif parser
This commit is contained in:
parent
58063a2645
commit
8535396eee
@ -165,151 +165,6 @@ export const lzw = (minCodeSize, data, pixelCount, memoryBuffer, bufferOffset) =
|
||||
return dstPixels
|
||||
}
|
||||
|
||||
export const lzw_contiguous = (minCodeSize, data, pixelCount) => {
|
||||
console.log("pixelCount", pixelCount);
|
||||
const MAX_STACK_SIZE = 4096
|
||||
const nullCode = -1
|
||||
const npix = pixelCount
|
||||
var available,
|
||||
clear,
|
||||
code_mask,
|
||||
code_size,
|
||||
end_of_information,
|
||||
in_code,
|
||||
old_code,
|
||||
bits,
|
||||
code,
|
||||
i,
|
||||
datum,
|
||||
data_size,
|
||||
first,
|
||||
top,
|
||||
bi,
|
||||
pi
|
||||
|
||||
// const dstPixels = new Array(pixelCount)
|
||||
// const prefix = new Array(MAX_STACK_SIZE)
|
||||
// const suffix = new Array(MAX_STACK_SIZE)
|
||||
// const pixelStack = new Array(MAX_STACK_SIZE + 1)
|
||||
|
||||
const dstPixels = new Uint8Array(pixelCount)
|
||||
const prefix = new Uint16Array(MAX_STACK_SIZE)
|
||||
const suffix = new Uint16Array(MAX_STACK_SIZE)
|
||||
const pixelStack = new Uint8Array(MAX_STACK_SIZE + 1)
|
||||
|
||||
// Initialize GIF data stream decoder.
|
||||
data_size = minCodeSize
|
||||
clear = 1 << data_size
|
||||
end_of_information = clear + 1
|
||||
available = clear + 2
|
||||
old_code = nullCode
|
||||
code_size = data_size + 1
|
||||
code_mask = (1 << code_size) - 1
|
||||
for (code = 0; code < clear; code++) {
|
||||
// prefix[code] = 0
|
||||
suffix[code] = code
|
||||
}
|
||||
|
||||
// Decode GIF pixel stream.
|
||||
var datum, bits, count, first, top, pi, bi
|
||||
datum = bits = count = first = top = pi = bi = 0
|
||||
for (i = 0; i < npix && bi < data.length; ) {
|
||||
if (top === 0) {
|
||||
if (bits < code_size) {
|
||||
// get the next byte
|
||||
datum += data[bi] << bits
|
||||
|
||||
bits += 8
|
||||
bi++
|
||||
continue
|
||||
}
|
||||
// if (bi === 49418) {
|
||||
// console.log("bi", bi, "pi", pi, "top", top, "bits", bits, "code_size", code_size, "code_mask", code_mask);
|
||||
// console.log("code", code, "next code", datum & code_mask, "old_code", old_code, "datum_shifted", datum >> code_size);
|
||||
// }
|
||||
// Get the next code.
|
||||
code = datum & code_mask
|
||||
datum >>= code_size
|
||||
bits -= code_size
|
||||
// Interpret the code
|
||||
if (code > available || code == end_of_information) {
|
||||
//end of info bi 692436 pi 2513585 top 0 bits 2 code_size 9 code_mask 511
|
||||
// console.log("end of info", "bi", bi, "pi", pi, "top", top, "bits", bits, "code_size", code_size, "code_mask", code_mask);
|
||||
// console.log("datum", datum, "code", code, "next code", datum & code_mask, "available", available);
|
||||
datum = bits = count = first = top = 0;
|
||||
code_size = data_size + 1
|
||||
code_mask = (1 << code_size) - 1
|
||||
available = clear + 2
|
||||
old_code = nullCode
|
||||
// prefix.fill(0);
|
||||
// suffix.fill(0);
|
||||
// pixelStack.fill(0);
|
||||
// for (code = 0; code < clear; code++) {
|
||||
// // prefix[code] = 0
|
||||
// suffix[code] = code
|
||||
// }
|
||||
continue;
|
||||
break
|
||||
}
|
||||
if (code === clear) {
|
||||
// Reset decoder.
|
||||
code_size = data_size + 1
|
||||
code_mask = (1 << code_size) - 1
|
||||
available = clear + 2
|
||||
old_code = nullCode
|
||||
// console.log("code is clear", "bi", bi, "pi", pi, "top", top, "bits", bits, "code_size", code_size, "code_mask", code_mask);
|
||||
// console.log("next code", datum & code_mask);
|
||||
continue
|
||||
}
|
||||
if (old_code === nullCode) {
|
||||
pixelStack[top++] = suffix[code]
|
||||
old_code = code
|
||||
first = code
|
||||
continue
|
||||
}
|
||||
in_code = code
|
||||
if (code === available) {
|
||||
pixelStack[top++] = first
|
||||
code = old_code
|
||||
}
|
||||
while (code > clear && top <= MAX_STACK_SIZE) {
|
||||
pixelStack[top++] = suffix[code]
|
||||
code = prefix[code]
|
||||
}
|
||||
|
||||
first = suffix[code] & 0xff
|
||||
pixelStack[top++] = first
|
||||
|
||||
// add a new string to the table, but only if space is available
|
||||
// if not, just continue with current table until a clear code is found
|
||||
// (deferred clear code implementation as per GIF spec)
|
||||
if (available < MAX_STACK_SIZE) {
|
||||
// if (available === 258 && old_code === 258) {
|
||||
// console.log("258");
|
||||
// }
|
||||
prefix[available] = old_code
|
||||
suffix[available] = first
|
||||
available++
|
||||
if ((available & code_mask) === 0 && available < MAX_STACK_SIZE) {
|
||||
code_size++
|
||||
code_mask += available
|
||||
}
|
||||
}
|
||||
old_code = in_code
|
||||
}
|
||||
// Pop a pixel off the pixel stack.
|
||||
top--
|
||||
dstPixels[pi++] = pixelStack[top]
|
||||
i++
|
||||
}
|
||||
|
||||
// for (i = pi; i < npix; i++) {
|
||||
// dstPixels[i] = 0 // clear missing pixels
|
||||
// }
|
||||
|
||||
return dstPixels
|
||||
}
|
||||
|
||||
export const parseGIF = arrayBuffer => {
|
||||
const byteData = new Uint8Array(arrayBuffer)
|
||||
return parse(buildStream(byteData), GIF)
|
||||
@ -421,73 +276,3 @@ export const decompressFrames = (parsedGif, buildImagePatches) => {
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export const decompressFramesContiguous = (parsedGif, buildImagePatches) => {
|
||||
// return parsedGif.frames
|
||||
// .filter(f => f.image)
|
||||
// .map(f => decompressFrame(f, parsedGif.gct, buildImagePatches))
|
||||
let totalPixels = 0;
|
||||
let totalBlocks = 0;
|
||||
let out = [];
|
||||
let frameData = [];
|
||||
let i, j = 0;
|
||||
let frameStartIdx = 0;
|
||||
let pixelsPerFrame = 0;
|
||||
|
||||
for (i = 0, j = 0; i < parsedGif.frames.length; i++) {
|
||||
if (parsedGif.frames[i].image) {
|
||||
pixelsPerFrame = parsedGif.frames[i].image.descriptor.width * parsedGif.frames[i].image.descriptor.height;
|
||||
totalPixels += pixelsPerFrame;
|
||||
totalBlocks += parsedGif.frames[i].image.data.blocks.length;
|
||||
// frameData[j] = {
|
||||
// frameStart: frameStartIdx,
|
||||
// frameEnd: frameStartIdx + pixelsPerFrame - 1,
|
||||
// top: parsedGif.frames[i].image.descriptor.top,
|
||||
// left: parsedGif.frames[i].image.descriptor.left,
|
||||
// width: parsedGif.frames[i].image.descriptor.width,
|
||||
// height: parsedGif.frames[i].image.descriptor.height
|
||||
// };
|
||||
// frameStartIdx = frameStartIdx + pixelsPerFrame;
|
||||
j++
|
||||
}
|
||||
}
|
||||
|
||||
const allBlocks = new Uint8Array(totalBlocks);
|
||||
// const allPixels = new Uint8Array(totalPixels);
|
||||
|
||||
let k;
|
||||
for (i = 0, j = 0; i < parsedGif.frames.length; i++) {
|
||||
if (parsedGif.frames[i].image) {
|
||||
allBlocks.set(parsedGif.frames[i].image.data.blocks, j);
|
||||
j += parsedGif.frames[i].image.data.blocks.length;
|
||||
// if (j === 0) {
|
||||
// allBlocks.set(parsedGif.frames[i].image.data.blocks, j);
|
||||
// j += parsedGif.frames[i].image.data.blocks.length;
|
||||
// } else {
|
||||
// for (k = 1; k < parsedGif.frames[i].image.data.blocks.length; k++) {
|
||||
// allBlocks[j + k - 1] = parsedGif.frames[i].image.data.blocks[k];
|
||||
// }
|
||||
// j += parsedGif.frames[i].image.data.blocks.length - 1;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
console.log(allBlocks);
|
||||
window.allBlocks = allBlocks;
|
||||
|
||||
const decompressedPixels = lzw_contiguous(parsedGif.frames[1].image.data.minCodeSize, allBlocks, totalPixels);
|
||||
|
||||
// const dstPixels = new Uint16Array(totalPixels);
|
||||
// let frameStart = 0;
|
||||
// let frameEnd = 0;
|
||||
|
||||
// for (i = 0; i < parsedGif.frames.length; i++) {
|
||||
// if (parsedGif.frames[i].image) {
|
||||
// out[j] = decompressFrame(parsedGif.frames[i], parsedGif.gct, buildImagePatches);
|
||||
// // out[j] = decompressFrame(parsedGif.frames[i], parsedGif.gct, buildImagePatches, prefix, suffix, pixelStack, dstPixels, frameStart, frameEnd);
|
||||
// j++;
|
||||
// }
|
||||
// }
|
||||
|
||||
return decompressedPixels;
|
||||
}
|
||||
|
@ -98,7 +98,6 @@ class AnimatedGIF extends PIXI.Sprite
|
||||
let colorData;
|
||||
|
||||
if (decompressedFrameData.pixels.length === pixelBuffer.length / 4)
|
||||
// if (false)
|
||||
{
|
||||
// Not all GIF files are perfectly optimized
|
||||
// and instead of having tiny patch of pixels that actually changed from previous frame
|
||||
|
@ -16,7 +16,7 @@ import { VisualStim } from "./VisualStim.js";
|
||||
import {Camera} from "../hardware";
|
||||
// import { parseGIF, decompressFrames } from "gifuct-js";
|
||||
import { AnimatedGIF } from "./AnimatedGIF.js";
|
||||
import { parseGIF, decompressFrames, decompressFramesContiguous } from "../util/GifParser.js";
|
||||
import { parseGIF, decompressFrames } from "../util/GifParser.js";
|
||||
|
||||
/**
|
||||
* Gif Stimulus.
|
||||
@ -257,10 +257,6 @@ export class GifStim extends util.mix(VisualStim).with(ColorMixin)
|
||||
}
|
||||
this._resource = { parsedGif, decompressedFrames, fullFrames };
|
||||
this.psychoJS.serverManager.cacheResourceData(image, this._resource);
|
||||
// let t2c = performance.now();
|
||||
// let pixels2 = decompressFramesContiguous(gif, false);
|
||||
// window.pixels2 = pixels2;
|
||||
// let dect2 = performance.now() - t2c;
|
||||
console.log(`animated gif "${this._name}",`, "parse=", pt, "decompress=", dect);
|
||||
}
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user