tinyimg - v1.0.0
    Preparing search index...

    Class Image

    A chainable transformation over one source.

    Nothing runs until you ask for the output. Every method here appends to a plan, and the plan runs once, when bytes or one of its siblings is called. That is not an implementation detail: it is what lets a 100x100 thumbnail of a 16 megapixel photograph decode a 500x500 region at a quarter scale instead of 16 megapixels, and the decision cannot be made after the first operation has already run.

    The handle lives in the module's memory, so an image has to be released. Prefer using:

    using image = await tinyimg.open(request.body);
    const webp = await image.fit(800, 600, { gravity: 'face' }).sharpen(1).bytes('webp', { quality: 80 });

    Without using, call dispose in a finally. bytes does not dispose, because a caller may want two encodings of one plan.

    Index
    • get operations(): number

      Operations appended so far.

      The count before any rewrite, so it says what was asked for rather than what will run; see decide for the latter.

      Returns number

    • get sourceFrames(): number

      Frames the source holds, which is 1 for a still image.

      Only the first is decoded, so a value above 1 means an encode of this plan is a still taken from an animation. The count comes from the header read open already performed.

      Returns number

    • Sets what a pad or an arbitrary rotation fills with.

      Not an operation: it is a property of the plan, so calling it twice replaces rather than stacking, and calling it after fit still applies to that fit.

      Parameters

      Returns this

    • Blurs.

      A true gaussian, as three box passes. The planner moves a blur that feeds a reduction of two or more to after the reduction, at a scaled sigma, which is exact for a gaussian.

      Parameters

      • sigma: number

        Radius in pixels. Zero is a no-op and is eliminated.

      Returns this

    • Scales every channel. 1 is unchanged.

      Parameters

      • factor: number

      Returns this

    • Runs the plan and encodes the result.

      The plan is not consumed, so this can be called twice for two formats of one transformation and the planner's work is done once.

      Parameters

      • Optionalformat: ImageFormat

        Container to encode as. Defaults to the source's own.

      • options: EncodeOptions = {}

        Encoder settings.

      Returns Promise<Uint8Array<ArrayBuffer>>

      The encoded bytes.

    • Scales every channel about mid gray. 1 is unchanged.

      Parameters

      • factor: number

      Returns this

    • Takes a rectangle, in the coordinates the previous operation produced.

      Parameters

      • x: number
      • y: number
      • width: number
      • height: number

      Returns this

    • The result as a data: URL.

      For an inline <img src> or a placeholder. Base64 costs a third more bytes than the image, so this is for small results.

      Parameters

      Returns Promise<string>

    • Reports what the planner decided, without producing the image.

      The saving is visible here: region and scale are what the decoder will be asked for, and eliminated and collapsed are what the rewrites removed. Useful in a log line, and it is how the library's own tests assert a plan rather than inferring it from the pixels.

      Returns PlanDecision

    • Releases the plan and the source bytes.

      Safe to call twice. Every later call on the image throws instead of reading freed memory.

      Returns void

    • Chooses how much work the decode may spend.

      fancy, the default, decodes to the bitstream's definition. fast lets a lossy decoder drop its smoothing pass, which is 1.53x on a lossy WebP for 46.8 dB and 1.11x to 1.25x on a subsampled JPEG for 43.6 to 59.5 dB.

      A lossless format has nothing to drop, because it defines its pixels exactly, so PNG, GIF, TIFF and lossless WebP decode identically either way. So does a 4:4:4 JPEG, which has no chroma to upsample.

      This is the decode side only. The encoder's effort is an argument to bytes.

      Parameters

      • effort: "fancy" | "fast"

        How hard to work.

      Returns this

    • Resamples and then crops or pads to an exact extent.

      Parameters

      • width: number

        Target width.

      • height: number

        Target height.

      • options: FitOptions & { fit?: Fit } = {}

        fit defaults to cover; gravity to center; filter to auto.

      Returns this

    • Turns operation fusion off, so every operation runs as its own pass.

      Leave this on. It exists to measure what the planner is worth: with it off the same chain costs one traversal and one buffer per operation, which is what the library would be without the planner. bench/ is the only caller that should ever pass false.

      Parameters

      • enabled: boolean

        False to run one operation per pass.

      Returns this

    • Raises every channel to a power. 1 is unchanged.

      Parameters

      • value: number

      Returns this

    • Replaces every channel with the pixel's luminance, and drops the color channels.

      Returns this

    • Rotates the hue, in degrees.

      Parameters

      • degrees: number

      Returns this

    • Subtracts every channel from full scale, leaving alpha alone.

      Returns this

    • Runs the plan and hands back the raw samples.

      Below the encoders, for a caller who wants the pixels themselves: a histogram, a hand-written kernel, or a measurement that should not include an encoder's time. Rows are tightly packed and there is no stride separate from width * channels.

      Returns Promise<RawImage>

      The extent, the channel count and width * height * channels bytes.

    • Resamples to an extent.

      Parameters

      • width: number

        Target width. Zero, or omitted with a height given, keeps the aspect ratio.

      • height: number = 0

        Target height. Same rule.

      • filter: ResampleFilter = 'auto'

        Weights to sample through. auto is an area average down and a cubic up, which is the right answer for almost every request.

      Returns this

    • The result as a Response, which is what a Worker returns.

      Parameters

      • Optionalformat: ImageFormat

        Container to encode as.

      • options: EncodeOptions & { headers?: HeadersInit } = {}

        Encoder settings, plus headers merged into the response's own.

      Returns Promise<Response>

    • Turns by a multiple of 90 degrees clockwise.

      Folded into the output addressing rather than run as a pass, so any number of turns and flips cost one pass between them. rotateFree is the arbitrary-angle form.

      Parameters

      • degrees: 0 | 90 | 180 | 270

      Returns this

    • Turns by an arbitrary angle, filling the corners with the background.

      Runs after the plan rather than inside it. An arbitrary rotation changes the extent by a non-integer factor, so it cannot fold into the sample map the way a quarter turn does; it is applied to the materialized image. A quarter turn passed here is still handed to rotate, which is free.

      Parameters

      • degrees: number

      Returns this

    • Moves every channel toward or away from its luminance. 1 is unchanged.

      Parameters

      • factor: number

      Returns this

    • Sharpens with an unsharp mask.

      Parameters

      • amount: number

        How much of the high-frequency difference to add back. 1 is a typical value.

      • sigma: number = 1

        Radius of the blur it is measured against.

      Returns this

    • Trims a uniform border.

      Runs after the plan rather than inside it. How much to trim is a function of the pixels, and the planner decides the decode region and scale before a pixel is read, so a trim cannot be a plan operation. Chain it before a resize and the resize still happens first.

      Parameters

      • tolerance: number = 8

        How far a pixel may differ from the border color and still be trimmed.

      Returns this

    • Opens a source without decoding it.

      The header is read so the planner knows the extent it is working from; no pixel is touched until the plan runs.

      Parameters

      Returns Promise<Image>

      A transformation with no operations in it yet.