Added main files

This commit is contained in:
Toastie 2024-10-18 21:52:45 +13:00
parent fa6d3107e6
commit 5eb7e75106
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
12 changed files with 1162 additions and 6 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
dist
node_modules
.cache

23
LICENSE
View file

@ -1,9 +1,20 @@
MIT License Copyright (c) 2017-2021 Eric Nograles and others
Copyright (c) 2024 Valkyriecoms 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:
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 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
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. 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.

133
README.md
View file

@ -1,2 +1,135 @@
# browser-image-resizer # browser-image-resizer
## Introduction
This library allows for cross-browser image downscaling utilizing `OffscreenCanvas`.
## Note
- This is browser-only utility and will not work in Node.js.
- Safari 16.4 or later is required due to the use of `OffscreenCanvas`.
https://caniuse.com/offscreencanvas
## Installation
### NPM/Yarn/pnpm
- `npm install @valkyriecoms/browser-image-resizer`
- `yarn add @valkyriecoms/browser-image-resizer`
- `pnpm add @valkyriecoms/browser-image-resizer`
## Usage
### In the main thread
```typescript
import { readAndCompressImage } from 'browser-image-resizer';
const config = {
quality: 0.7,
width: 800,
height: 600
};
// Note: A single file comes from event.target.files on <input>
async function uploadImage(file) {
try {
let resizedImage = await readAndCompressImage(file, config);
const url = `http://localhost:3001/upload`;
const formData = new FormData();
formData.append('images', resizedImage);
const options = {
method: 'POST',
body: formData
};
let result = await fetch(url, options);
// TODO: Handle the result
console.log(result);
return result;
} catch (error) {
console.error(error);
throw(error);
}
}
```
### In worker
Even large images can be processed in a separate thread using a worker.
#### worker.js
```typescript
import { readAndCompressImage } from "browser-image-resizer";
onmessage = async (e) => {
const converted = await readAndCompressImage(e.data, { maxWidth: 300 });
postMessage(converted, [converted]);
}
```
#### Main Thread
```typescript
const worker = new Worker('worker.js');
const img = document.getElementById('viewer_img');
worker.onmessage = (e) => {
img.src = URL.createObjectURL(e.data);
};
async function convert(file: File) {
const bmp = await createImageBitmap(file);
worker.postMessage(bmp, [bmp]);
}
```
## API
### `readAndCompressImage(file, config) => Promise<Blob | OffscreenCanvas>`
#### Inputs
* `file`: An image source that createImageBitmap can read.
See https://developer.mozilla.org/en-US/docs/Web/API/createImageBitmap
* `config`: See below
| Property Name | Purpose | Default Value |
| ------------- |-------------| -----:|
| `argorithm` | Algorithm used for downscaling (see below) | 'null' |
| `processByHalf` | Whether to process downscaling by `drawImage(source, 0, 0, source.width / 2, source.height / 2)` until the size is smaller than twice the target size. | true |
| `quality` | The quality of jpeg (or webp) | 0.5 |
| `maxWidth` | The maximum width for the downscaled image | 800 |
| `maxHeight` | The maximum height for the downscaled image | 600 |
| `debug` | console.log image update operations | false |
| `mimeType` | specify image output type other than jpeg / If set `null`, function returns OffscreenCanvas | 'image/jpeg' |
##### `argorithm`
* `null`: Just resize with `drawImage()`. The best quality and fastest.
* `bilinear`: Better quality, slower. Comes from upstream (ericnogralesbrowser-image-resizer).
* `hermite`: Worse quality, faster. Comes from [viliusle/Hermite-resize](https://github.com/viliusle/Hermite-resize). Will dispatch workers for better performance.
* `hermite_single`: Worse quality, faster. Single-threaded.
#### Outputs
A Promise that yields an Image Blob or OffscreenCanvas
### `calculateSize(src, config)`
`calculateSize(src: { width: number; height: number; }, config) => { width: number; height: number; }`
With this function you can get the pre-calculated width and height of the resulting image.
### Output Image Specification
The output image is derived from `OffscreenCanvas.convertToBlob`.
https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/convertToBlob
- EXIF and other metadata will be erased.
- Rotation will be automatically corrected.
* It is based on the specifications of recent versions of modern browsers and may not work with older browsers.
* See https://github.com/w3c/csswg-drafts/issues/4666#issuecomment-610962845
* Firefox support seems to be available from version 78.
- Color profile is srgb. Firefox 97 does not attach the ICC profile, but Chrome does.
- You can specify image/webp as the mimeType but [Safari does not support.](https://developer.apple.com/documentation/webkitjs/htmlcanvaselement/1630000-todataurl).

29
build.js Normal file
View file

@ -0,0 +1,29 @@
const esbuild = require('esbuild');
const watch = process.argv[2]?.includes('watch');
/** @type {esbuild.BuildOptions} */
const buildOptions = {
entryPoints: [ `${__dirname}/src/index.ts` ],
bundle: true,
format: 'esm',
treeShaking: true,
minify: false,
absWorkingDir: __dirname,
outbase: `${__dirname}/src`,
outdir: `${__dirname}/dist`,
loader: {
'.ts': 'ts'
},
tsconfig: `${__dirname}/tsconfig.json`,
};
(async () => {
if (!watch) {
await esbuild.build(buildOptions);
console.log('done');
} else {
const context = await esbuild.context(buildOptions);
await context.watch();
console.log('watching...');
}
})();

23
package.json Normal file
View file

@ -0,0 +1,23 @@
{
"name": "@valkyriecoms/browser-image-resizer",
"version": "2024.1.0",
"description": "A browser-based utility to downscale and resize images using OffscreenCanvas",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Eric Nograles <grales@gmail.com>",
"license": "MIT",
"packageManager": "pnpm@8.6.12",
"files": [
"dist"
],
"devDependencies": {
"esbuild": "^0.18.20",
"typescript": "^5.3.3"
},
"scripts": {
"build": "node build.js && tsc --project tsconfig.build.json",
"dev": "node build.js watch",
"test": "cd tests/vite-project && pnpm run dev"
},
"repository": "https://toastielab.dev/Valkyriecoms/browser-image-resizer.git"
}

249
pnpm-lock.yaml Normal file
View file

@ -0,0 +1,249 @@
lockfileVersion: '6.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
devDependencies:
esbuild:
specifier: ^0.18.20
version: 0.18.20
typescript:
specifier: ^5.3.3
version: 5.3.3
packages:
/@esbuild/android-arm64@0.18.20:
resolution: {integrity: sha512-Nz4rJcchGDtENV0eMKUNa6L12zz2zBDXuhj/Vjh18zGqB44Bi7MBMSXjgunJgjRhCmKOjnPuZp4Mb6OKqtMHLQ==}
engines: {node: '>=12'}
cpu: [arm64]
os: [android]
requiresBuild: true
dev: true
optional: true
/@esbuild/android-arm@0.18.20:
resolution: {integrity: sha512-fyi7TDI/ijKKNZTUJAQqiG5T7YjJXgnzkURqmGj13C6dCqckZBLdl4h7bkhHt/t0WP+zO9/zwroDvANaOqO5Sw==}
engines: {node: '>=12'}
cpu: [arm]
os: [android]
requiresBuild: true
dev: true
optional: true
/@esbuild/android-x64@0.18.20:
resolution: {integrity: sha512-8GDdlePJA8D6zlZYJV/jnrRAi6rOiNaCC/JclcXpB+KIuvfBN4owLtgzY2bsxnx666XjJx2kDPUmnTtR8qKQUg==}
engines: {node: '>=12'}
cpu: [x64]
os: [android]
requiresBuild: true
dev: true
optional: true
/@esbuild/darwin-arm64@0.18.20:
resolution: {integrity: sha512-bxRHW5kHU38zS2lPTPOyuyTm+S+eobPUnTNkdJEfAddYgEcll4xkT8DB9d2008DtTbl7uJag2HuE5NZAZgnNEA==}
engines: {node: '>=12'}
cpu: [arm64]
os: [darwin]
requiresBuild: true
dev: true
optional: true
/@esbuild/darwin-x64@0.18.20:
resolution: {integrity: sha512-pc5gxlMDxzm513qPGbCbDukOdsGtKhfxD1zJKXjCCcU7ju50O7MeAZ8c4krSJcOIJGFR+qx21yMMVYwiQvyTyQ==}
engines: {node: '>=12'}
cpu: [x64]
os: [darwin]
requiresBuild: true
dev: true
optional: true
/@esbuild/freebsd-arm64@0.18.20:
resolution: {integrity: sha512-yqDQHy4QHevpMAaxhhIwYPMv1NECwOvIpGCZkECn8w2WFHXjEwrBn3CeNIYsibZ/iZEUemj++M26W3cNR5h+Tw==}
engines: {node: '>=12'}
cpu: [arm64]
os: [freebsd]
requiresBuild: true
dev: true
optional: true
/@esbuild/freebsd-x64@0.18.20:
resolution: {integrity: sha512-tgWRPPuQsd3RmBZwarGVHZQvtzfEBOreNuxEMKFcd5DaDn2PbBxfwLcj4+aenoh7ctXcbXmOQIn8HI6mCSw5MQ==}
engines: {node: '>=12'}
cpu: [x64]
os: [freebsd]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-arm64@0.18.20:
resolution: {integrity: sha512-2YbscF+UL7SQAVIpnWvYwM+3LskyDmPhe31pE7/aoTMFKKzIc9lLbyGUpmmb8a8AixOL61sQ/mFh3jEjHYFvdA==}
engines: {node: '>=12'}
cpu: [arm64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-arm@0.18.20:
resolution: {integrity: sha512-/5bHkMWnq1EgKr1V+Ybz3s1hWXok7mDFUMQ4cG10AfW3wL02PSZi5kFpYKrptDsgb2WAJIvRcDm+qIvXf/apvg==}
engines: {node: '>=12'}
cpu: [arm]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-ia32@0.18.20:
resolution: {integrity: sha512-P4etWwq6IsReT0E1KHU40bOnzMHoH73aXp96Fs8TIT6z9Hu8G6+0SHSw9i2isWrD2nbx2qo5yUqACgdfVGx7TA==}
engines: {node: '>=12'}
cpu: [ia32]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-loong64@0.18.20:
resolution: {integrity: sha512-nXW8nqBTrOpDLPgPY9uV+/1DjxoQ7DoB2N8eocyq8I9XuqJ7BiAMDMf9n1xZM9TgW0J8zrquIb/A7s3BJv7rjg==}
engines: {node: '>=12'}
cpu: [loong64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-mips64el@0.18.20:
resolution: {integrity: sha512-d5NeaXZcHp8PzYy5VnXV3VSd2D328Zb+9dEq5HE6bw6+N86JVPExrA6O68OPwobntbNJ0pzCpUFZTo3w0GyetQ==}
engines: {node: '>=12'}
cpu: [mips64el]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-ppc64@0.18.20:
resolution: {integrity: sha512-WHPyeScRNcmANnLQkq6AfyXRFr5D6N2sKgkFo2FqguP44Nw2eyDlbTdZwd9GYk98DZG9QItIiTlFLHJHjxP3FA==}
engines: {node: '>=12'}
cpu: [ppc64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-riscv64@0.18.20:
resolution: {integrity: sha512-WSxo6h5ecI5XH34KC7w5veNnKkju3zBRLEQNY7mv5mtBmrP/MjNBCAlsM2u5hDBlS3NGcTQpoBvRzqBcRtpq1A==}
engines: {node: '>=12'}
cpu: [riscv64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-s390x@0.18.20:
resolution: {integrity: sha512-+8231GMs3mAEth6Ja1iK0a1sQ3ohfcpzpRLH8uuc5/KVDFneH6jtAJLFGafpzpMRO6DzJ6AvXKze9LfFMrIHVQ==}
engines: {node: '>=12'}
cpu: [s390x]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/linux-x64@0.18.20:
resolution: {integrity: sha512-UYqiqemphJcNsFEskc73jQ7B9jgwjWrSayxawS6UVFZGWrAAtkzjxSqnoclCXxWtfwLdzU+vTpcNYhpn43uP1w==}
engines: {node: '>=12'}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: true
optional: true
/@esbuild/netbsd-x64@0.18.20:
resolution: {integrity: sha512-iO1c++VP6xUBUmltHZoMtCUdPlnPGdBom6IrO4gyKPFFVBKioIImVooR5I83nTew5UOYrk3gIJhbZh8X44y06A==}
engines: {node: '>=12'}
cpu: [x64]
os: [netbsd]
requiresBuild: true
dev: true
optional: true
/@esbuild/openbsd-x64@0.18.20:
resolution: {integrity: sha512-e5e4YSsuQfX4cxcygw/UCPIEP6wbIL+se3sxPdCiMbFLBWu0eiZOJ7WoD+ptCLrmjZBK1Wk7I6D/I3NglUGOxg==}
engines: {node: '>=12'}
cpu: [x64]
os: [openbsd]
requiresBuild: true
dev: true
optional: true
/@esbuild/sunos-x64@0.18.20:
resolution: {integrity: sha512-kDbFRFp0YpTQVVrqUd5FTYmWo45zGaXe0X8E1G/LKFC0v8x0vWrhOWSLITcCn63lmZIxfOMXtCfti/RxN/0wnQ==}
engines: {node: '>=12'}
cpu: [x64]
os: [sunos]
requiresBuild: true
dev: true
optional: true
/@esbuild/win32-arm64@0.18.20:
resolution: {integrity: sha512-ddYFR6ItYgoaq4v4JmQQaAI5s7npztfV4Ag6NrhiaW0RrnOXqBkgwZLofVTlq1daVTQNhtI5oieTvkRPfZrePg==}
engines: {node: '>=12'}
cpu: [arm64]
os: [win32]
requiresBuild: true
dev: true
optional: true
/@esbuild/win32-ia32@0.18.20:
resolution: {integrity: sha512-Wv7QBi3ID/rROT08SABTS7eV4hX26sVduqDOTe1MvGMjNd3EjOz4b7zeexIR62GTIEKrfJXKL9LFxTYgkyeu7g==}
engines: {node: '>=12'}
cpu: [ia32]
os: [win32]
requiresBuild: true
dev: true
optional: true
/@esbuild/win32-x64@0.18.20:
resolution: {integrity: sha512-kTdfRcSiDfQca/y9QIkng02avJ+NCaQvrMejlsB3RRv5sE9rRoeBPISaZpKxHELzRxZyLvNts1P27W3wV+8geQ==}
engines: {node: '>=12'}
cpu: [x64]
os: [win32]
requiresBuild: true
dev: true
optional: true
/esbuild@0.18.20:
resolution: {integrity: sha512-ceqxoedUrcayh7Y7ZX6NdbbDzGROiyVBgC4PriJThBKSVPWnnFHZAkfI1lJT8QFkOwH4qOS2SJkS4wvpGl8BpA==}
engines: {node: '>=12'}
hasBin: true
requiresBuild: true
optionalDependencies:
'@esbuild/android-arm': 0.18.20
'@esbuild/android-arm64': 0.18.20
'@esbuild/android-x64': 0.18.20
'@esbuild/darwin-arm64': 0.18.20
'@esbuild/darwin-x64': 0.18.20
'@esbuild/freebsd-arm64': 0.18.20
'@esbuild/freebsd-x64': 0.18.20
'@esbuild/linux-arm': 0.18.20
'@esbuild/linux-arm64': 0.18.20
'@esbuild/linux-ia32': 0.18.20
'@esbuild/linux-loong64': 0.18.20
'@esbuild/linux-mips64el': 0.18.20
'@esbuild/linux-ppc64': 0.18.20
'@esbuild/linux-riscv64': 0.18.20
'@esbuild/linux-s390x': 0.18.20
'@esbuild/linux-x64': 0.18.20
'@esbuild/netbsd-x64': 0.18.20
'@esbuild/openbsd-x64': 0.18.20
'@esbuild/sunos-x64': 0.18.20
'@esbuild/win32-arm64': 0.18.20
'@esbuild/win32-ia32': 0.18.20
'@esbuild/win32-x64': 0.18.20
dev: true
/typescript@5.3.3:
resolution: {integrity: sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==}
engines: {node: '>=14.17'}
hasBin: true
dev: true

79
src/bilinear.ts Normal file
View file

@ -0,0 +1,79 @@
export function bilinear(srcCanvasData: ImageData, destCanvasData: ImageData, scale: number) {
function inner(f00: number, f10: number, f01: number, f11: number, x: number, y: number) {
let un_x = 1.0 - x;
let un_y = 1.0 - y;
return f00 * un_x * un_y + f10 * x * un_y + f01 * un_x * y + f11 * x * y;
}
let i: number, j: number;
let iyv: number, iy0: number, iy1: number, ixv: number, ix0: number, ix1: number;
let idxD: number, idxS00: number, idxS10: number, idxS01: number, idxS11: number;
let dx: number, dy: number;
let r: number, g: number, b: number, a: number;
for (i = 0; i < destCanvasData.height; ++i) {
iyv = i / scale;
iy0 = Math.floor(iyv);
// Math.ceil can go over bounds
iy1 =
Math.ceil(iyv) > srcCanvasData.height - 1
? srcCanvasData.height - 1
: Math.ceil(iyv);
for (j = 0; j < destCanvasData.width; ++j) {
ixv = j / scale;
ix0 = Math.floor(ixv);
// Math.ceil can go over bounds
ix1 =
Math.ceil(ixv) > srcCanvasData.width - 1
? srcCanvasData.width - 1
: Math.ceil(ixv);
idxD = (j + destCanvasData.width * i) * 4;
// matrix to vector indices
idxS00 = (ix0 + srcCanvasData.width * iy0) * 4;
idxS10 = (ix1 + srcCanvasData.width * iy0) * 4;
idxS01 = (ix0 + srcCanvasData.width * iy1) * 4;
idxS11 = (ix1 + srcCanvasData.width * iy1) * 4;
// overall coordinates to unit square
dx = ixv - ix0;
dy = iyv - iy0;
// I let the r, g, b, a on purpose for debugging
r = inner(
srcCanvasData.data[idxS00],
srcCanvasData.data[idxS10],
srcCanvasData.data[idxS01],
srcCanvasData.data[idxS11],
dx,
dy
);
destCanvasData.data[idxD] = r;
g = inner(
srcCanvasData.data[idxS00 + 1],
srcCanvasData.data[idxS10 + 1],
srcCanvasData.data[idxS01 + 1],
srcCanvasData.data[idxS11 + 1],
dx,
dy
);
destCanvasData.data[idxD + 1] = g;
b = inner(
srcCanvasData.data[idxS00 + 2],
srcCanvasData.data[idxS10 + 2],
srcCanvasData.data[idxS01 + 2],
srcCanvasData.data[idxS11 + 2],
dx,
dy
);
destCanvasData.data[idxD + 2] = b;
a = inner(
srcCanvasData.data[idxS00 + 3],
srcCanvasData.data[idxS10 + 3],
srcCanvasData.data[idxS01 + 3],
srcCanvasData.data[idxS11 + 3],
dx,
dy
);
destCanvasData.data[idxD + 3] = a;
}
}
}

317
src/hermite.ts Normal file
View file

@ -0,0 +1,317 @@
/*
* Hermite resize - fast image resize/resample using Hermite filter.
* https://github.com/viliusle/Hermite-resize/blob/fae53290d2b03520a6fc81d734c3028902a599c0/src/hermite.js
* Author: ViliusL
* License: MIT https://github.com/viliusle/Hermite-resize/blob/fae53290d2b03520a6fc81d734c3028902a599c0/MIT-LICENSE.txt
*/
import { getImageData } from "./scaling_operations";
type WorkerSouceData = {
source: ImageData;
startY: number;
height: number;
}
type WorkerSouceMessage = {
srcWidth: number;
srcHeight: number;
destWidth: number;
destHeight: number;
core: number;
source: ArrayBufferLike;
debug?: boolean;
}
type WorkerResultMessage = {
core: number;
target: Uint8ClampedArray;
}
export class Hermit {
private cores: number;
private workersArchive: Worker[] = [];
private workerBlobURL: string;
/**
* contructor
*/
constructor() {
this.cores = Math.min(navigator.hardwareConcurrency || 4, 4);
this.workerBlobURL = globalThis.URL.createObjectURL(new Blob(['(',
function () {
//begin worker
onmessage = function (event: MessageEvent<WorkerSouceMessage>) {
if (event.data.debug) {
console.log('browser-image-resizer: hermite worker: start', event.data.core, event.data);
console.time('work')
}
const core = event.data.core;
const srcWidth = event.data.srcWidth;
const srcHeight = event.data.srcHeight;
const destWidth = event.data.destWidth;
const destHeight = event.data.destHeight;
const ratio_w = srcWidth / destWidth;
const ratio_h = srcHeight / destHeight;
const ratio_w_half = Math.ceil(ratio_w / 2);
const ratio_h_half = Math.ceil(ratio_h / 2);
//let source_h = source.length / width_source / 4;
const source = new Uint8ClampedArray(event.data.source);
const target_size = destWidth * destHeight * 4;
const target_memory = new ArrayBuffer(target_size);
const target = new Uint8ClampedArray(target_memory, 0, target_size);
//calculate
for (let j = 0; j < destHeight; j++) {
for (let i = 0; i < destWidth; i++) {
const x2 = (i + j * destWidth) * 4;
let weight = 0;
let weights = 0;
let weights_alpha = 0;
let gx_r = 0;
let gx_g = 0;
let gx_b = 0;
let gx_a = 0;
const center_y = j * ratio_h;
const xx_start = Math.floor(i * ratio_w);
const xx_stop = Math.min(Math.ceil((i + 1) * ratio_w), srcWidth);
const yy_start = Math.floor(j * ratio_h);
const yy_stop = Math.min(Math.ceil((j + 1) * ratio_h), srcHeight);
for (let yy = yy_start; yy < yy_stop; yy++) {
let dy = Math.abs(center_y - yy) / ratio_h_half;
let center_x = i * ratio_w;
let w0 = dy * dy; //pre-calc part of w
for (let xx = xx_start; xx < xx_stop; xx++) {
let dx = Math.abs(center_x - xx) / ratio_w_half;
let w = Math.sqrt(w0 + dx * dx);
if (w >= 1) {
//pixel too far
continue;
}
//hermite filter
weight = 2 * w * w * w - 3 * w * w + 1;
//calc source pixel location
let pos_x = 4 * (xx + yy * srcWidth);
//alpha
gx_a += weight * source[pos_x + 3];
weights_alpha += weight;
//colors
if (source[pos_x + 3] < 255)
weight = weight * source[pos_x + 3] / 250;
gx_r += weight * source[pos_x];
gx_g += weight * source[pos_x + 1];
gx_b += weight * source[pos_x + 2];
weights += weight;
}
}
target[x2] = gx_r / weights;
target[x2 + 1] = gx_g / weights;
target[x2 + 2] = gx_b / weights;
target[x2 + 3] = gx_a / weights_alpha;
}
}
//return
const objData: WorkerResultMessage = {
core,
target,
};
(globalThis.postMessage as any)(objData, [target.buffer]);
if (event.data.debug) {
console.timeEnd('work');
console.log('browser-image-resizer: Worker: end', event.data.core);
}
};
//end worker
}.toString(),
')()'], { type: 'application/javascript' }));
};
/**
* Hermite resize. Detect cpu count and use best option for user.
*/
public resampleAuto(srcCanvas: OffscreenCanvas, destCanvas: OffscreenCanvas, config: { debug?: boolean, argorithm?: string }) {
if (!!globalThis.Worker && this.cores > 1 && config?.argorithm !== 'hermite_single') {
//workers supported and we have at least 2 cpu cores - using multithreading
return this.resample(srcCanvas, destCanvas, config);
} else {
//1 cpu version
const { srcImgData, destImgData } = getImageData(srcCanvas, destCanvas);
this.resampleSingle(srcImgData, destImgData, config);
destCanvas.getContext('2d')!.putImageData(destImgData, 0, 0);
return;
}
};
/**
* Hermite resize, multicore version - fast image resize/resample using Hermite filter.
*/
async resample(srcCanvas: OffscreenCanvas, destCanvas: OffscreenCanvas, config: { debug?: boolean }) {
return new Promise<void>((resolve, reject) => {
if (config.debug) console.time('hermite_multi');
const ratio_h = srcCanvas.height / destCanvas.height;
//stop old workers
if (this.workersArchive.length > 0) {
for (let c = 0; c < this.cores; c++) {
if (this.workersArchive[c] != undefined) {
this.workersArchive[c].terminate();
delete this.workersArchive[c];
}
}
}
this.workersArchive = new Array(this.cores);
//prepare source and target data for workers
const ctx = srcCanvas.getContext('2d');
if (!ctx) return reject('Canvas is empty (resample)');
if (config.debug) {
console.log('browser-image-resizer: hermite_multi: ', this.cores, 'cores');
console.log('browser-image-resizer: source size: ', srcCanvas.width, srcCanvas.height, 'ratio_h: ', ratio_h);
console.log('browser-image-resizer: target size: ', destCanvas.width, destCanvas.height);
}
const data_part: WorkerSouceData[] = [];
const block_height = Math.ceil(srcCanvas.height / this.cores / 2) * 2;
let end_y = -1;
for (let c = 0; c < this.cores; c++) {
//source
const offset_y = end_y + 1;
if (offset_y >= srcCanvas.height) {
//size too small, nothing left for this core
continue;
}
end_y = Math.min(offset_y + block_height - 1, srcCanvas.height - 1);
const current_block_height = Math.min(block_height, srcCanvas.height - offset_y);
if (config.debug) {
console.log('browser-image-resizer: source split: ', '#' + c, offset_y, end_y, 'height: ' + current_block_height);
}
data_part.push({
source: ctx.getImageData(0, offset_y, srcCanvas.width, block_height),
startY: Math.ceil(offset_y / ratio_h),
height: current_block_height
});
}
//start
const destCtx = destCanvas.getContext('2d');
if (!destCtx) return reject('Canvas is empty (resample dest)');
let workers_in_use = data_part.length;
for (let c = 0; c < data_part.length; c++) {
const my_worker = new Worker(this.workerBlobURL);
this.workersArchive[c] = my_worker;
my_worker.onmessage = (event: MessageEvent<WorkerResultMessage>) => {
workers_in_use--;
const core = event.data.core;
//draw
const height_part = Math.ceil(data_part[core].height / ratio_h);
const target = destCtx.createImageData(destCanvas.width, height_part);
target.data.set(event.data.target);
destCtx.putImageData(target, 0, data_part[core].startY);
if (workers_in_use <= 0) {
//all workers done
resolve();
if (config.debug) console.timeEnd('hermite_multi');
}
this.workersArchive[core].terminate();
delete this.workersArchive[core];
};
my_worker.onerror = err => reject(err);
const objData: WorkerSouceMessage = {
srcWidth: srcCanvas.width,
srcHeight: data_part[c].height,
destWidth: destCanvas.width,
destHeight: Math.ceil(data_part[c].height / ratio_h),
core: c,
source: data_part[c].source.data.buffer,
debug: config.debug,
};
my_worker.postMessage(objData, [objData.source]);
}
});
};
/**
* Hermite resize - fast image resize/resample using Hermite filter. 1 cpu version!
*/
resampleSingle(srcCanvasData: ImageData, destCanvasData: ImageData, config: { debug?: boolean }) {
const ratio_w = srcCanvasData.width / destCanvasData.width;
const ratio_h = srcCanvasData.height / destCanvasData.height;
const ratio_w_half = Math.ceil(ratio_w / 2);
const ratio_h_half = Math.ceil(ratio_h / 2);
const data = srcCanvasData.data;
const data2 = destCanvasData.data;
if (config.debug) {
console.log('browser-image-resizer: source size: ', srcCanvasData.width, srcCanvasData.height, 'ratio_h: ', ratio_h);
console.log('browser-image-resizer: target size: ', destCanvasData.width, destCanvasData.height);
console.time('hermite_single');
}
for (let j = 0; j < destCanvasData.height; j++) {
for (let i = 0; i < destCanvasData.width; i++) {
const x2 = (i + j * destCanvasData.width) * 4;
let weight = 0;
let weights = 0;
let weights_alpha = 0;
let gx_r = 0;
let gx_g = 0;
let gx_b = 0;
let gx_a = 0;
const center_y = j * ratio_h;
const xx_start = Math.floor(i * ratio_w);
const xx_stop = Math.min(Math.ceil((i + 1) * ratio_w), srcCanvasData.width);
const yy_start = Math.floor(j * ratio_h);
const yy_stop = Math.min(Math.ceil((j + 1) * ratio_h), srcCanvasData.height);
for (let yy = yy_start; yy < yy_stop; yy++) {
let dy = Math.abs(center_y - yy) / ratio_h_half;
let center_x = i * ratio_w;
let w0 = dy * dy; //pre-calc part of w
for (let xx = xx_start; xx < xx_stop; xx++) {
let dx = Math.abs(center_x - xx) / ratio_w_half;
let w = Math.sqrt(w0 + dx * dx);
if (w >= 1) {
//pixel too far
continue;
}
//hermite filter
weight = 2 * w * w * w - 3 * w * w + 1;
let pos_x = 4 * (xx + yy * srcCanvasData.width);
//alpha
gx_a += weight * data[pos_x + 3];
weights_alpha += weight;
//colors
if (data[pos_x + 3] < 255)
weight = weight * data[pos_x + 3] / 250;
gx_r += weight * data[pos_x];
gx_g += weight * data[pos_x + 1];
gx_b += weight * data[pos_x + 2];
weights += weight;
}
}
data2[x2] = gx_r / weights;
data2[x2 + 1] = gx_g / weights;
data2[x2 + 2] = gx_b / weights;
data2[x2 + 3] = gx_a / weights_alpha;
}
}
if (config.debug) {
console.timeEnd('hermite_single');
}
};
}

82
src/index.ts Normal file
View file

@ -0,0 +1,82 @@
import { Hermit as _Hermite } from './hermite';
import { bilinear as _bilinear } from './bilinear';
import { findMaxWidth, getTargetHeight, scaleImage } from './scaling_operations';
export const Hermit = _Hermite;
export const bilinear = _bilinear;
type BrowserImageResizerConfigBase = {
/**
* Algorithm used for downscaling
*
* * `null`: Just resize with `drawImage()`. The best quality and fastest.
* * `bilinear`: Better quality, slower. Comes from upstream (ericnogralesbrowser-image-resizer).
* * `hermite`: Worse quality, faster. Comes from [viliusle/Hermite-resize](https://github.com/viliusle/Hermite-resize). Will dispatch workers for better performance.
* * `hermite_single`: Worse quality, faster. Single-threaded.
*
* default: null
*/
argorithm: 'bilinear' | 'hermite' | 'hermite_single' | 'null' | null;
/**
* Whether to process downscaling by `drawImage(source, 0, 0, source.width / 2, source.height / 2)`
* until the size is smaller than twice the target size.
*
* There seems to be no situation where it is necessary to change to false.
*
* default: true
*/
processByHalf: boolean;
maxWidth: number;
maxHeight: number;
maxSize?: number; // ???
/**
* Scale ratio. Strictly limited to maxWidth.
*/
scaleRatio?: number;
/**
* Output logs to console
*/
debug: boolean;
}
export type BrowserImageResizerConfigWithConvertedOutput = BrowserImageResizerConfigBase & {
quality: number;
mimeType: string;
};
export type BrowserImageResizerConfigWithOffscreenCanvasOutput = BrowserImageResizerConfigBase & {
mimeType: null;
}
export type BrowserImageResizerConfig = BrowserImageResizerConfigWithConvertedOutput | BrowserImageResizerConfigWithOffscreenCanvasOutput;
const DEFAULT_CONFIG = {
argorithm: 'null',
processByHalf: true,
quality: 0.5,
maxWidth: 800,
maxHeight: 600,
debug: false,
mimeType: 'image/jpeg',
} as const;
export async function readAndCompressImage(img: ImageBitmapSource | OffscreenCanvas, userConfig: Partial<BrowserImageResizerConfigWithConvertedOutput>): Promise<Blob>
export async function readAndCompressImage(img: ImageBitmapSource | OffscreenCanvas, userConfig: Partial<Omit<BrowserImageResizerConfigWithOffscreenCanvasOutput, 'quality'>>): Promise<OffscreenCanvas>
export async function readAndCompressImage(
img: ImageBitmapSource | OffscreenCanvas,
userConfig: Partial<BrowserImageResizerConfig>
) {
const config = Object.assign({}, DEFAULT_CONFIG, userConfig);
return scaleImage({ img, config });
}
export function calculateSize(src: { width: number; height: number }, userConfig: Partial<BrowserImageResizerConfigBase>) {
const config = Object.assign({}, DEFAULT_CONFIG, userConfig);
const width = findMaxWidth(config, src);
const height = getTargetHeight(src.height, width / src.width, config);
return { width: Math.floor(width), height };
}

179
src/scaling_operations.ts Normal file
View file

@ -0,0 +1,179 @@
import { BrowserImageResizerConfig } from '.';
import { bilinear } from './bilinear';
import { Hermit } from './hermite';
let hermite: Hermit;
function isIos() {
if (typeof navigator === 'undefined') return false;
if (!navigator.userAgent) return false;
return /iPad|iPhone|iPod/.test(navigator.userAgent);
}
export function getTargetHeight(srcHeight: number, scale: number, config: BrowserImageResizerConfig) {
return Math.min(Math.floor(srcHeight * scale), config.maxHeight);
}
export function findMaxWidth(config: BrowserImageResizerConfig, canvas: { width: number; height: number }) {
//Let's find the max available width for scaled image
const ratio = canvas.width / canvas.height;
let mWidth = Math.min(
canvas.width,
config.maxWidth,
ratio * config.maxHeight
);
if (
config.maxSize &&
config.maxSize > 0 &&
config.maxSize < (canvas.width * canvas.height) / 1000
)
mWidth = Math.min(
mWidth,
Math.floor((config.maxSize * 1000) / canvas.height)
);
if (!!config.scaleRatio)
mWidth = Math.min(mWidth, Math.floor(config.scaleRatio * canvas.width));
const rHeight = getTargetHeight(canvas.height, mWidth / canvas.width, config);
if (config.debug) {
console.log(
'browser-image-resizer: original image size = ' +
canvas.width +
' px (width) X ' +
canvas.height +
' px (height)'
);
console.log(
'browser-image-resizer: scaled image size = ' +
mWidth +
' px (width) X ' +
rHeight +
' px (height)'
);
}
if (mWidth <= 0) {
mWidth = 1;
console.warn("browser-image-resizer: image size is too small");
}
if (isIos() && mWidth * rHeight > 167777216) {
console.error("browser-image-resizer: image size is too large for iOS WebKit.", mWidth, rHeight);
throw new Error("browser-image-resizer: image size is too large for iOS WebKit.");
}
return mWidth;
}
export function getImageData(canvas: OffscreenCanvas, scaled: OffscreenCanvas) {
const srcImgData = canvas
?.getContext('2d')
?.getImageData(0, 0, canvas.width, canvas.height);
const destImgData = scaled
?.getContext('2d')
?.createImageData(scaled.width, scaled.height);
if (!srcImgData || !destImgData) throw Error('Canvas is empty (scaleCanvasWithAlgorithm). You should run this script after the document is ready.');
return { srcImgData, destImgData };
}
function prepareHermit() {
if (!hermite) hermite = new Hermit();
}
async function scaleCanvasWithAlgorithm(canvas: OffscreenCanvas, config: BrowserImageResizerConfig & { outputWidth: number }) {
const scale = config.outputWidth / canvas.width;
const scaled = new OffscreenCanvas(Math.floor(config.outputWidth), getTargetHeight(canvas.height, scale, config));
switch (config.argorithm) {
case 'hermite': {
prepareHermit();
await hermite.resampleAuto(canvas, scaled, config as BrowserImageResizerConfig & { argorithm: 'hermite' | 'hermite_single' });
break;
} case 'hermite_single': {
const { srcImgData, destImgData } = getImageData(canvas, scaled);
prepareHermit();
hermite.resampleSingle(srcImgData, destImgData, config);
scaled?.getContext('2d')?.putImageData(destImgData, 0, 0);
break;
} case 'bilinear': {
const { srcImgData, destImgData } = getImageData(canvas, scaled);
bilinear(srcImgData, destImgData, scale);
scaled?.getContext('2d')?.putImageData(destImgData, 0, 0);
break;
} default: {
scaled.getContext('2d')?.drawImage(canvas, 0, 0, scaled.width, scaled.height);
break;
}
}
return scaled;
}
function getHalfScaleCanvas(src: OffscreenCanvas | HTMLCanvasElement) {
const half = new OffscreenCanvas(src.width / 2, src.height / 2);
half
?.getContext('2d')
?.drawImage(src, 0, 0, half.width, half.height);
return half;
}
export async function scaleImage({ img, config }: {
img: ImageBitmapSource | OffscreenCanvas;
config: BrowserImageResizerConfig;
}) {
if (config.debug) {
console.log('browser-image-resizer: Scale: Started', img);
}
let converting: OffscreenCanvas;
if (img instanceof OffscreenCanvas) {
converting = img;
} else {
const bmp = await createImageBitmap(img);
/**
* iOS WebKit has particularly strict maximum OffscreenCanvas size, so
* Force shrinking
*/
if (isIos() && bmp.width * bmp.height > 16777216) {
const scale = Math.sqrt(16777216 / (bmp.width * bmp.height));
if (config.debug) console.log(`browser-image-resizer: scale: Image is too large in iOS WebKit`);
converting = new OffscreenCanvas(Math.floor(bmp.width * scale), Math.floor(bmp.height * scale));
converting.getContext('2d')?.drawImage(bmp, 0, 0, converting.width, converting.height);
} else {
converting = new OffscreenCanvas(bmp.width, bmp.height);
converting.getContext('2d')?.drawImage(bmp, 0, 0);
}
}
if (!converting?.getContext('2d')) throw Error('browser-image-resizer: Canvas Context is empty.');
const maxWidth = findMaxWidth(config, converting);
if (!maxWidth) throw Error(`browser-image-resizer: maxWidth is ${maxWidth}!!`);
if (config.debug) console.log(`browser-image-resizer: scale: maxWidth is ${maxWidth}`);
while (config.processByHalf && converting.width >= 2 * maxWidth) {
if (config.debug) console.log(`browser-image-resizer: scale: Scaling canvas by half from ${converting.width}`);
converting = getHalfScaleCanvas(converting);
}
if (converting.width > maxWidth) {
if (config.debug) console.log(`browser-image-resizer: scale: Scaling canvas by ${config.argorithm} from ${converting.width} to ${maxWidth}`);
converting = await scaleCanvasWithAlgorithm(
converting,
Object.assign(config, { outputWidth: maxWidth }),
);
}
if (config.mimeType === null) {
return converting;
}
const imageData = await converting.convertToBlob({ type: config.mimeType, quality: config.quality });
return imageData;
}

11
tsconfig.build.json Normal file
View file

@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"include": [
"./src/index.ts"
],
"compilerOptions": {
"emitDeclarationOnly": true,
"isolatedModules": false,
"declaration": true,
},
}

40
tsconfig.json Normal file
View file

@ -0,0 +1,40 @@
{
"compilerOptions": {
"allowJs": true,
"noEmitOnError": false,
"noImplicitAny": false,
"noImplicitReturns": true,
"noUnusedParameters": false,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"declaration": false,
"sourceMap": false,
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"removeComments": false,
"noLib": false,
"strict": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"isolatedModules": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
},
"typeRoots": [
"node_modules/@types",
"@types",
],
"lib": [
"esnext",
"dom"
],
"outDir": "./dist"
},
"compileOnSave": false,
"include": [
"./**/*.ts"
],
}