Skip to content

Commit

Permalink
feat(pixel): add imagePyramid() iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 2, 2021
1 parent f6c0525 commit 7f77e07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/pixel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./dither";
export * from "./float";
export * from "./normal-map";
export * from "./packed";
export * from "./pyramid";
export * from "./sample";
export * from "./utils";

Expand Down
31 changes: 31 additions & 0 deletions packages/pixel/src/pyramid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assert } from "@thi.ng/api";
import type { KernelSpec } from "./api";
import { convolveImage, LANCZOS } from "./convolve";
import type { FloatBuffer } from "./float";

/**
* Yields an iterator of progressively downsampled versions of `src` (using
* `kernel` for filtering, default: {@link LANCZOS}(2)). Each image will be half
* size of the previous result, stopping only once either width or height
* becomes less than `minSize` (default: 1). If `includeOrig` is enabled
* (default), the first emitted image will be the original `src`.
*
* @param src
* @param kernel
* @param minSize
* @param includeOrig
*/
export function* imagePyramid(
src: FloatBuffer,
kernel: KernelSpec = LANCZOS(2),
minSize = 1,
includeOrig = true
) {
assert(minSize > 0, `invalid min size`);
minSize <<= 1;
if (includeOrig) yield src;
while (src.width >= minSize && src.height >= minSize) {
src = convolveImage(src, { kernel, stride: 2 });
yield src;
}
}

0 comments on commit 7f77e07

Please sign in to comment.