-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pixel): add imagePyramid() iterator
- Loading branch information
1 parent
f6c0525
commit 7f77e07
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |