refactor: use blurhash-as for encoding and decoding

This commit is contained in:
ThatOneCalculator 2023-07-13 19:00:26 -07:00
parent 0920422cf0
commit 242f564115
5 changed files with 37 additions and 14 deletions

View file

@ -47,7 +47,7 @@
"aws-sdk": "2.1413.0", "aws-sdk": "2.1413.0",
"axios": "^1.4.0", "axios": "^1.4.0",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"blurhash": "2.0.5", "blurhash-as": "^0.10.0",
"bull": "4.10.4", "bull": "4.10.4",
"cacheable-lookup": "7.0.0", "cacheable-lookup": "7.0.0",
"calckey-js": "workspace:*", "calckey-js": "workspace:*",

View file

@ -10,11 +10,12 @@ import FFmpeg from "fluent-ffmpeg";
import isSvg from "is-svg"; import isSvg from "is-svg";
import { type predictionType } from "nsfwjs"; import { type predictionType } from "nsfwjs";
import sharp from "sharp"; import sharp from "sharp";
import { encode } from "blurhash"; import * as blurhash from "blurhash-as";
import { detectSensitive } from "@/services/detect-sensitive.js"; import { detectSensitive } from "@/services/detect-sensitive.js";
import { createTempDir } from "./create-temp.js"; import { createTempDir } from "./create-temp.js";
const pipeline = util.promisify(stream.pipeline); const pipeline = util.promisify(stream.pipeline);
blurhash.init();
export type FileInfo = { export type FileInfo = {
size: number; size: number;
@ -432,7 +433,13 @@ function getBlurhash(path: string): Promise<string> {
let hash; let hash;
try { try {
hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7); hash = blurhash.encode(
new Uint8ClampedArray(buffer),
width,
height,
7,
7,
);
} catch (e) { } catch (e) {
return reject(e); return reject(e);
} }

View file

@ -30,6 +30,7 @@
"autobind-decorator": "2.4.0", "autobind-decorator": "2.4.0",
"autosize": "5.0.2", "autosize": "5.0.2",
"blurhash": "2.0.5", "blurhash": "2.0.5",
"blurhash-as": "^0.10.0",
"broadcast-channel": "5.1.0", "broadcast-channel": "5.1.0",
"browser-image-resizer": "github:misskey-dev/browser-image-resizer", "browser-image-resizer": "github:misskey-dev/browser-image-resizer",
"calckey-js": "workspace:*", "calckey-js": "workspace:*",

View file

@ -4,12 +4,12 @@
ref="canvas" ref="canvas"
:width="size" :width="size"
:height="size" :height="size"
:title="title" :title="title ?? ''"
/> />
<img <img
v-if="src" v-if="src"
:src="src" :src="src"
:title="title" :title="title ?? ''"
:type="type" :type="type"
:alt="alt" :alt="alt"
:class="{ cover }" :class="{ cover }"
@ -21,7 +21,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import { onMounted } from "vue"; import { onMounted } from "vue";
import { decodeBlurHash } from "fast-blurhash"; import * as blurhash from "blurhash-as/browser";
import wasmURL from "blurhash-as/build/optimized.wasm?url";
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
@ -46,9 +47,10 @@ const props = withDefaults(
const canvas = $ref<HTMLCanvasElement>(); const canvas = $ref<HTMLCanvasElement>();
let loaded = $ref(false); let loaded = $ref(false);
function draw() { async function draw() {
if (props.hash == null || canvas == null) return; if (props.hash == null || canvas == null) return;
const pixels = decodeBlurHash(props.hash, props.size, props.size); blurhash.setURL(wasmURL);
const pixels = await blurhash.decode(props.hash, props.size, props.size);
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
const imageData = ctx!.createImageData(props.size, props.size); const imageData = ctx!.createImageData(props.size, props.size);
imageData.data.set(pixels); imageData.data.set(pixels);

View file

@ -147,9 +147,9 @@ importers:
bcryptjs: bcryptjs:
specifier: 2.4.3 specifier: 2.4.3
version: 2.4.3 version: 2.4.3
blurhash: blurhash-as:
specifier: 2.0.5 specifier: ^0.10.0
version: 2.0.5 version: 0.10.0
bull: bull:
specifier: 4.10.4 specifier: 4.10.4
version: 4.10.4 version: 4.10.4
@ -736,6 +736,9 @@ importers:
blurhash: blurhash:
specifier: 2.0.5 specifier: 2.0.5
version: 2.0.5 version: 2.0.5
blurhash-as:
specifier: ^0.10.0
version: 0.10.0
broadcast-channel: broadcast-channel:
specifier: 5.1.0 specifier: 5.1.0
version: 5.1.0 version: 5.1.0
@ -780,7 +783,7 @@ importers:
version: 2.30.0 version: 2.30.0
emojilib: emojilib:
specifier: github:thatonecalculator/emojilib specifier: github:thatonecalculator/emojilib
version: github.com/thatonecalculator/emojilib/1d6adc1af8105b4937d2f0e7479acf5bd565bd9b version: github.com/thatonecalculator/emojilib/d3c8c6a77d4362b3b3180099f1d2eac344ce245c
escape-regexp: escape-regexp:
specifier: 0.0.1 specifier: 0.0.1
version: 0.0.1 version: 0.0.1
@ -1078,6 +1081,9 @@ packages:
'@jridgewell/gen-mapping': 0.3.3 '@jridgewell/gen-mapping': 0.3.3
'@jridgewell/trace-mapping': 0.3.18 '@jridgewell/trace-mapping': 0.3.18
/@assemblyscript/loader@0.27.5:
resolution: {integrity: sha512-DGEJQIKh/pMseal4i26haGutxNfs3nGzwY0MNseoFKcE3DKIXQ25deWpNOqIJQPvjS4FKH+oUpzsTv0oa17+xg==}
/@babel/code-frame@7.22.5: /@babel/code-frame@7.22.5:
resolution: {integrity: sha512-Xmwn266vad+6DAqEB2A6V/CcZVp62BbwVmcOJc2RPuwih1kw02TjQvWVWlcKGbBPd+8/0V5DEkOcizRGYsspYQ==} resolution: {integrity: sha512-Xmwn266vad+6DAqEB2A6V/CcZVp62BbwVmcOJc2RPuwih1kw02TjQvWVWlcKGbBPd+8/0V5DEkOcizRGYsspYQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -5411,8 +5417,15 @@ packages:
resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==} resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==}
dev: true dev: true
/blurhash-as@0.10.0:
resolution: {integrity: sha512-0xkq5uhvdQZfbytKAPtuy3vkkGdsZJxxqL3l6lWIgTIoEdH85jU/0HRENFDH5AW3MwChc6CnfXB4RUvvoRbJeA==}
engines: {node: '>=10'}
dependencies:
'@assemblyscript/loader': 0.27.5
/blurhash@2.0.5: /blurhash@2.0.5:
resolution: {integrity: sha512-cRygWd7kGBQO3VEhPiTgq4Wc43ctsM+o46urrmPOiuAe+07fzlSB9OJVdpgDL0jPqXUVQ9ht7aq7kxOeJHRK+w==} resolution: {integrity: sha512-cRygWd7kGBQO3VEhPiTgq4Wc43ctsM+o46urrmPOiuAe+07fzlSB9OJVdpgDL0jPqXUVQ9ht7aq7kxOeJHRK+w==}
dev: true
/bmp-js@0.1.0: /bmp-js@0.1.0:
resolution: {integrity: sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==} resolution: {integrity: sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==}
@ -17486,8 +17499,8 @@ packages:
url-polyfill: 1.1.12 url-polyfill: 1.1.12
dev: true dev: true
github.com/thatonecalculator/emojilib/1d6adc1af8105b4937d2f0e7479acf5bd565bd9b: github.com/thatonecalculator/emojilib/d3c8c6a77d4362b3b3180099f1d2eac344ce245c:
resolution: {tarball: https://codeload.github.com/thatonecalculator/emojilib/tar.gz/1d6adc1af8105b4937d2f0e7479acf5bd565bd9b} resolution: {tarball: https://codeload.github.com/thatonecalculator/emojilib/tar.gz/d3c8c6a77d4362b3b3180099f1d2eac344ce245c}
name: emojilib name: emojilib
version: 3.0.10 version: 3.0.10
dev: true dev: true