Skip to content

Commit

Permalink
re-organize image utils and lanczos
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr Martian committed Nov 3, 2024
1 parent 379271e commit 4859237
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 101 deletions.
38 changes: 38 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,41 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

---

Some code has been repurposed from <https://github.com/rgba-image/lanczos>

MIT License

Copyright (c) 2018 Nik Coughlin

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.

Adapted to typescript from pica: <https://github.com/nodeca/pica>

(The MIT License)

Copyright (C) 2014-2017 by Vitaly Puzrin

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.
1 change: 1 addition & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './delaunator';
export * from './lanczos';
export * from './orthodrome';
export * from './polylabel';
12 changes: 5 additions & 7 deletions src/tools/lanczos/index.ts → src/tools/lanczos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { copy, createImage, resize } from './util';

export * from './util';
import { copyImage, createImage, resizeImage } from '../util';

/**
* Lanczos filter function for downscaling
Expand Down Expand Up @@ -50,13 +48,13 @@ export function lanczos(
dw === dest.width &&
dh === dest.height
) {
resize(source, dest, use2);
resizeImage(source, dest, use2);
return;
}

const croppedSource = createImage(sw, sh);
const croppedDest = createImage(dw, dh);
copy(source, croppedSource, sx, sy);
resize(croppedSource, croppedDest, use2);
copy(croppedDest, dest, 0, 0, croppedDest.width, croppedDest.height, dx, dy);
copyImage(source, croppedSource, sx, sy);
resizeImage(croppedSource, croppedDest, use2);
copyImage(croppedDest, dest, 0, 0, croppedDest.width, croppedDest.height, dx, dy);
}
160 changes: 67 additions & 93 deletions src/tools/lanczos/util.ts → src/util/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function toFixedPoint(value: number): number {
* @param use2 - use 2nd lanczos filter instead of 3rd
* @returns - filter
*/
export function filters(
function filters(
srcSize: number,
destSize: number,
scale: number,
Expand Down Expand Up @@ -121,74 +121,6 @@ export function filters(
return packedFilter;
}

/**
* Convolve an image with a filter
* @param source - the source image
* @param dest - the destination image
* @param sw - source width
* @param sh - source height
* @param dw - destination width
* @param filters - image filter
*/
export function convolve(
source: Uint8ClampedArray,
dest: Uint8ClampedArray,
sw: number,
sh: number,
dw: number,
filters: Int16Array,
): void {
let srcOffset = 0;
let destOffset = 0;

// For each row
for (let sourceY = 0; sourceY < sh; sourceY++) {
let filterPtr = 0;

// Apply precomputed filters to each destination row point
for (let destX = 0; destX < dw; destX++) {
// Get the filter that determines the current output pixel.
const filterShift = filters[filterPtr++];

let srcPtr = (srcOffset + filterShift * 4) | 0;

let r = 0;
let g = 0;
let b = 0;
let a = 0;

// Apply the filter to the row to get the destination pixel r, g, b, a
for (let filterSize = filters[filterPtr++]; filterSize > 0; filterSize--) {
const filterValue = filters[filterPtr++];

r = (r + filterValue * source[srcPtr]) | 0;
g = (g + filterValue * source[srcPtr + 1]) | 0;
b = (b + filterValue * source[srcPtr + 2]) | 0;
a = (a + filterValue * source[srcPtr + 3]) | 0;

srcPtr = (srcPtr + 4) | 0;
}

// Bring this value back in range. All of the filter scaling factors
// are in fixed point with FIXED_FRAC_BITS bits of fractional part.
//
// (!) Add 1/2 of value before clamping to get proper rounding. In other
// case brightness loss will be noticeable if you resize image with white
// border and place it on white background.
//
dest[destOffset] = (r + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 1] = (g + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 2] = (b + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 3] = (a + (1 << 13)) >> FIXED_FRAC_BITS;

destOffset = (destOffset + sh * 4) | 0;
}

destOffset = ((sourceY + 1) * 4) | 0;
srcOffset = ((sourceY + 1) * sw * 4) | 0;
}
}

/**
* Copy the contents of the source image to the destination image
* @param source - the source image
Expand All @@ -200,7 +132,7 @@ export function convolve(
* @param dx - destination starting x point [Default: 0]
* @param dy - destination starting y point [Default: 0]
*/
export function copy(
export function copyImage(
source: ImageData,
dest: ImageData,
sx = 0,
Expand Down Expand Up @@ -295,7 +227,7 @@ export function createImage(
* @param dest - the destination image
* @param use2 - use 2nd lanczos filter instead of 3rd
*/
export function resize(source: ImageData, dest: ImageData, use2 = false): void {
export function resizeImage(source: ImageData, dest: ImageData, use2 = false): void {
const xRatio = dest.width / source.width;
const yRatio = dest.height / source.height;

Expand All @@ -304,32 +236,74 @@ export function resize(source: ImageData, dest: ImageData, use2 = false): void {

const tmp = new Uint8ClampedArray(dest.width * source.height * 4);

convolve(source.data, tmp, source.width, source.height, dest.width, filtersX);
convolve(tmp, dest.data, source.height, dest.width, dest.height, filtersY);
convolveImage(source.data, tmp, source.width, source.height, dest.width, filtersX);
convolveImage(tmp, dest.data, source.height, dest.width, dest.height, filtersY);
}

/*
Adapted to typescript from pica: https://github.com/nodeca/pica
/**
* Convolve an image with a filter
* @param source - the source image
* @param dest - the destination image
* @param sw - source width
* @param sh - source height
* @param dw - destination width
* @param filters - image filter
*/
export function convolveImage(
source: Uint8ClampedArray,
dest: Uint8ClampedArray,
sw: number,
sh: number,
dw: number,
filters: Int16Array,
): void {
let srcOffset = 0;
let destOffset = 0;

// For each row
for (let sourceY = 0; sourceY < sh; sourceY++) {
let filterPtr = 0;

// Apply precomputed filters to each destination row point
for (let destX = 0; destX < dw; destX++) {
// Get the filter that determines the current output pixel.
const filterShift = filters[filterPtr++];

let srcPtr = (srcOffset + filterShift * 4) | 0;

let r = 0;
let g = 0;
let b = 0;
let a = 0;

// Apply the filter to the row to get the destination pixel r, g, b, a
for (let filterSize = filters[filterPtr++]; filterSize > 0; filterSize--) {
const filterValue = filters[filterPtr++];

(The MIT License)
r = (r + filterValue * source[srcPtr]) | 0;
g = (g + filterValue * source[srcPtr + 1]) | 0;
b = (b + filterValue * source[srcPtr + 2]) | 0;
a = (a + filterValue * source[srcPtr + 3]) | 0;

Copyright (C) 2014-2017 by Vitaly Puzrin
srcPtr = (srcPtr + 4) | 0;
}

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:
// Bring this value back in range. All of the filter scaling factors
// are in fixed point with FIXED_FRAC_BITS bits of fractional part.
//
// (!) Add 1/2 of value before clamping to get proper rounding. In other
// case brightness loss will be noticeable if you resize image with white
// border and place it on white background.
//
dest[destOffset] = (r + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 1] = (g + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 2] = (b + (1 << 13)) >> FIXED_FRAC_BITS;
dest[destOffset + 3] = (a + (1 << 13)) >> FIXED_FRAC_BITS;

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
destOffset = (destOffset + sh * 4) | 0;
}

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.
*/
destOffset = ((sourceY + 1) * 4) | 0;
srcOffset = ((sourceY + 1) * sw * 4) | 0;
}
}
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './gzip';
export * from './image';
export * from './lzw';

/** The formats available to DecompressionStream */
Expand Down
2 changes: 1 addition & 1 deletion tests/tools/lanczos/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createImage, lanczos } from '../../../src/tools/lanczos';
import { createImage, lanczos } from '../../../src';
import { expect, test } from 'bun:test';

import sharp from 'sharp';
Expand Down

0 comments on commit 4859237

Please sign in to comment.