Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resize): Add initial support for magic kernel #70

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/resize/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## @jsquash/[email protected]

### Adds

- Adds initial Magic Kernel resizing method support ([Using the Rust library](https://github.com/SevInf/magic-kernel-rust))
- `magicKernel` - The original Magic Kernel algorithm
- `magicKernelSharp2013` - A sharpened version of the Magic Kernel algorithm
- `magicKernelSharp2021` - A further sharpened version of the Magic Kernel algorithm

## @jsquash/[email protected]

### Breaking Changes
Expand Down
10 changes: 8 additions & 2 deletions packages/resize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Note: You will need to either manually include the wasm files from the codec dir

### resize(data: ImageData, options: ResizeOptions): Promise<ImageData>

Resizes an ImageData object to the
Resizes an ImageData object to the specified dimensions.

#### data
Type: `ImageData`
Expand All @@ -37,7 +37,7 @@ Type: `Partial<ResizeOptions> & { width: number, height: number }`
The resize options for the output image. [See default values](./meta.ts).
- `width` (number) the width to resize the image to
- `height` (number) the height to resize the image to
- `method?` (`'triangle'` | `'catrom'` | `'mitchell'` | `'lanczos3'` | `'hqx'`) the algorithm used to resize the image. Defaults to `lanczos3`.
- `method?` (`'triangle'` | `'catrom'` | `'mitchell'` | `'lanczos3'` | `'hqx'` | `'magicKernel'` | `'magicKernelSharp2013'` | `'magicKernelSharp2021'`) the algorithm used to resize the image. Defaults to `lanczos3`.
- `fitMethod?` (`'stretch'` | `'contain'`) whether the image is stretched to fit the dimensions or cropped. Defaults to `stretch`.
- `premultiply?` (boolean) Defaults to `true`
- `linearRGB?` (boolean) Defaults to `true`
Expand Down Expand Up @@ -68,4 +68,10 @@ import resize, { initResize } from '@jsquash/resize';
initResize(WASM_MODULE); // The `WASM_MODULE` variable will need to be sourced by yourself and passed as an ArrayBuffer.

resize(image, options);

// Optionally if you know you are using the hqx method or magicKernel method you can also initialise those modules
import { initHqx, initMagicKernel } from '@jsquash/resize';

initHqx(HQX_WASM_MODULE);
initMagicKernel(MAGIC_KERNEL_WASM_MODULE);
```
50 changes: 50 additions & 0 deletions packages/resize/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import type { WorkerResizeOptions } from './meta.js';
import type { InitInput as InitResizeInput } from './lib/resize/pkg/squoosh_resize.js';
import type { InitInput as InitHqxInput } from './lib/hqx/pkg/squooshhqx.js';
import type { InitInput as InitMagicKernelInput } from './lib/magic-kernel/pkg/jsquash_magic_kernel.js';
import { getContainOffsets } from './util.js';
import initResizeWasm, {
resize as wasmResize,
} from './lib/resize/pkg/squoosh_resize.js';
import initHqxWasm, { resize as wasmHqx } from './lib/hqx/pkg/squooshhqx.js';
import initMagicKernelWasm, {
resize as wasmMagicKernel,
} from './lib/magic-kernel/pkg/jsquash_magic_kernel.js';
import { defaultOptions } from './meta.js';

const MAGIC_KERNEL_METHODS = [
'magicKernel',
'magicKernelSharp2013',
'magicKernelSharp2021',
];

let resizeWasmReady: Promise<unknown>;
let hqxWasmReady: Promise<unknown>;
let magicKernelWasmReady: Promise<unknown>;

export function initResize(moduleOrPath?: InitResizeInput) {
if (!resizeWasmReady) {
Expand All @@ -25,14 +36,31 @@ export function initHqx(moduleOrPath?: InitHqxInput) {
return hqxWasmReady;
}

export function initMagicKernel(moduleOrPath?: InitMagicKernelInput) {
if (!magicKernelWasmReady) {
magicKernelWasmReady = initMagicKernelWasm(moduleOrPath);
}
return magicKernelWasmReady;
}

interface HqxResizeOptions extends WorkerResizeOptions {
method: 'hqx';
}

interface MagicKernelResizeOptions extends WorkerResizeOptions {
method: 'magicKernel' | 'magicKernelSharp2013' | 'magicKernelSharp2021';
}

function optsIsHqxOpts(opts: WorkerResizeOptions): opts is HqxResizeOptions {
return opts.method === 'hqx';
}

function optsIsMagicKernelOpts(
opts: WorkerResizeOptions,
): opts is MagicKernelResizeOptions {
return MAGIC_KERNEL_METHODS.includes(opts.method);
}

function crop(
data: ImageData,
sx: number,
Expand Down Expand Up @@ -102,6 +130,24 @@ async function hqx(
);
}

async function magicKernel(
input: ImageData,
opts: MagicKernelResizeOptions,
): Promise<ImageData> {
await initMagicKernel();

const result = wasmMagicKernel(
new Uint8Array(input.data.buffer),
input.width,
input.height,
opts.width,
opts.height,
opts.method,
);

return result;
}

export default async function resize(
data: ImageData,
overrideOptions: Partial<WorkerResizeOptions> & {
Expand Down Expand Up @@ -141,6 +187,10 @@ export default async function resize(
);
}

if (optsIsMagicKernelOpts(options)) {
return magicKernel(input, options);
}

const result = wasmResize(
new Uint8Array(input.data.buffer),
input.width,
Expand Down
152 changes: 152 additions & 0 deletions packages/resize/lib/magic-kernel/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions packages/resize/lib/magic-kernel/LICENSE.codec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Serhii Tatarintsev

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.
5 changes: 5 additions & 0 deletions packages/resize/lib/magic-kernel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Magic Kernel

- Source: https://github.com/SevInf/magic-kernel-rust
- Version: v0.1.0
- License: MIT
22 changes: 22 additions & 0 deletions packages/resize/lib/magic-kernel/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "jsquash-magic-kernel"
version = "0.1.0"
authors = ["Jamie <[email protected]>"]
edition = "2021"
publish = false

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O", "--no-validation"]

[lib]
crate-type = ["cdylib"]

[dependencies]
magic-kernel = "0.1.0"
wasm-bindgen = "0.2.95"
js-sys = "0.3.72"
web-sys = { version = "0.3.72", features = ["ImageData"] }

[profile.release]
lto = true
opt-level = "s"
8 changes: 8 additions & 0 deletions packages/resize/lib/magic-kernel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "jsquash-magic-kernel",
"scripts": {
"build": "../../../tools/build-rust.sh && npm run patch-pre-script",
"patch-pre-script": "cat pre.js >> pkg/jsquash_magic_kernel.js.js"
},
"type": "module"
}
5 changes: 5 additions & 0 deletions packages/resize/lib/magic-kernel/pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Magic Kernel

- Source: https://github.com/SevInf/magic-kernel-rust
- Version: v0.1.0
- License: MIT
Loading
Loading