-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7336197
commit 20b2c4a
Showing
3 changed files
with
45 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npx lint-staged |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
import {decompress} from '../../vendor/bz2.js'; | ||
|
||
export const sleep = async (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms)); | ||
export const downloadUrl = async (url: string): Promise<Uint8Array> => new Uint8Array(await (await fetch(url)).arrayBuffer()); | ||
export const downloadUrl = async (url: string): Promise<Uint8Array> => | ||
new Uint8Array(await (await fetch(url)).arrayBuffer()); | ||
export const downloadText = async (url: string): Promise<string> => (await fetch(url)).text(); | ||
|
||
export const decompressBz2 = (data: Uint8Array, addMagic: boolean = true, prepend: boolean = true): Uint8Array => { | ||
if (addMagic) { | ||
const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); | ||
if (addMagic) { | ||
const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); | ||
|
||
if (prepend) { | ||
const temp = data; | ||
data = new Uint8Array(magic.length + data.length); | ||
data.set(temp, magic.length); | ||
} | ||
|
||
data.set(magic, 0); | ||
if (prepend) { | ||
const temp = data; | ||
data = new Uint8Array(magic.length + data.length); | ||
data.set(temp, magic.length); | ||
} | ||
|
||
return decompress(data); | ||
data.set(magic, 0); | ||
} | ||
|
||
return decompress(data); | ||
}; | ||
|
||
export const decodeJpeg = async (data: Uint8Array | null): Promise<ImageData> => { | ||
if (!data) { | ||
throw new Error("Input jpeg data was null!"); | ||
} | ||
if (data[0] !== 0xFF) { | ||
// fix invalid JPEG header | ||
data[0] = 0xFF; | ||
} | ||
|
||
// create img element | ||
const img = document.createElement('img'); | ||
img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); | ||
|
||
// wait for img to load | ||
await new Promise(resolve => img.onload = resolve); | ||
|
||
// get imagedata from img element | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = img.naturalWidth; | ||
canvas.height = img.naturalHeight; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
throw new Error('Canvas 2d not found!!!!!!!!'); | ||
} | ||
ctx.drawImage(img, 0, 0); | ||
return ctx.getImageData(0, 0, canvas.width, canvas.height); | ||
if (!data) { | ||
throw new Error('Input jpeg data was null!'); | ||
} | ||
if (data[0] !== 0xff) { | ||
// fix invalid JPEG header | ||
data[0] = 0xff; | ||
} | ||
|
||
// create img element | ||
const img = document.createElement('img'); | ||
img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); | ||
|
||
// wait for img to load | ||
await new Promise(resolve => (img.onload = resolve)); | ||
|
||
// get imagedata from img element | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = img.naturalWidth; | ||
canvas.height = img.naturalHeight; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
throw new Error('Canvas 2d not found!!!!!!!!'); | ||
} | ||
ctx.drawImage(img, 0, 0); | ||
return ctx.getImageData(0, 0, canvas.width, canvas.height); | ||
}; | ||
|
||
export const arraycopy = (src: Uint32Array, srcPos: number, dst: Uint32Array, dstPos: number, length: number): void => { | ||
while (length--) dst[dstPos++] = src[srcPos++]; | ||
} | ||
while (length--) dst[dstPos++] = src[srcPos++]; | ||
}; |