Skip to content

Commit 0957794

Browse files
committed
options for trim, colorToAlpha and GainMap.encode, deprecate positional arguments
1 parent e6cf594 commit 0957794

6 files changed

Lines changed: 117 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
and the encoders'). Animations that loop forever still have `-1` as their `loopCount` (also the decoded ones).
2828
- `Animation.webpEncoder()`, `gifEncoder()` and `apngEncoder()` take the options of `encodeWebP()`, `encodeGIF()` and
2929
`encodeAPNG()`, plus `loopCount`: the options are documented once.
30+
- `trim({ color, tolerance })` and `colorToAlpha({ color, transparencyThreshold, opacityThreshold })`: the color goes into the options, like the other options. `GainMap.encode({ quality })` likewise.
3031

3132
### Deprecated
3233

34+
- Positional optional arguments, the options take the same values: `trim(color, options)`, `colorToAlpha(color, options)`, `GainMap.encode(quality)`, `resize`/`scale`/`contain`/`fit`/`cover` with a `mode` argument (use `ResizeOptions`), `Frame` constructor and `Frame.from()` with duration and offsets (use `FrameOptions`), `Animation` constructor with `loopCount` (use `AnimationOptions`), `Animation.decode(input, onlyExtractFirstFrame)`, `Animation.resize(width, height, mode)`, `Animation.encodeGIF(quality)` and `encodeAPNG(compression)`, `encodePNG(compression, metadata)` and `encodePNG(metadata)`, `encodeJPEG(quality)`, `renderSVG(svg, size, mode)`, `composite(source, x, y, options)`, `rotate(angle, resize)`, `sharpen(amount, radius)`, `roundCorners(radius)`, `fisheye(strength)`, `dominantColor(ignoreBlack, ignoreWhite, threshold)`. All of them still work until 4.0.
35+
- The numeric frame disposal ids (`0`-`3`): use the names (`'any'`, `'keep'`, `'previous'`, `'background'`). Removed in 4.0.
3336
- `ResizeModes.AUTO` (and `Image.RESIZE_AUTO`, `Frame.RESIZE_AUTO`): pass only the width or the height instead.
3437
Removed in 4.0.
3538
- Iterating over an image (`for (const [x, y] of image)`) and `iterateWithColors()`: use `coordinates()` and

‎src/animation.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class Animation extends Array<Frame> {
136136
* Creates a new animation
137137
* @param frames The frames to create the animation from
138138
* @param loopCount How many times to repeat the animation (`Infinity` = forever, 0 = play once, 1 = play twice, ...)
139+
* @deprecated Pass {@link AnimationOptions} instead, the positional arguments will be removed in 4.0
139140
*/
140141
constructor(frames: Frame[], loopCount?: number);
141142
constructor(frames: Frame[], loopCountOrOptions: number | AnimationOptions = -1) {
@@ -237,6 +238,7 @@ export class Animation extends Array<Frame> {
237238
* @param width The new width
238239
* @param height The new height
239240
* @param mode The resizing mode to use. Default: {@link ResizeModes.BILINEAR}
241+
* @deprecated Pass {@link ResizeOptions} with `mode` instead, the positional mode will be removed in 4.0
240242
*/
241243
resize(width: number, height: number, mode?: ResizeMode): this;
242244
/**
@@ -290,6 +292,7 @@ export class Animation extends Array<Frame> {
290292
* animation shows, but not necessarily to the same frames. Semi-transparent pixels become opaque or transparent
291293
* after the frames are composited
292294
* @param quality 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above)
295+
* @deprecated Pass {@link GIFEncodeOptions} instead, the positional arguments will be removed in 4.0
293296
*/
294297
encodeGIF(quality?: number): Promise<Uint8Array>;
295298
async encodeGIF(qualityOrOptions: number | GIFEncodeOptions = 95): Promise<Uint8Array> {
@@ -326,6 +329,7 @@ export class Animation extends Array<Frame> {
326329
* merged (unless the `optimize` option is false): the file shows exactly the same animation, but doesn't
327330
* necessarily decode to the same frames
328331
* @param compression The zlib compression level (0-9), higher is smaller but slower. Default: `4`
332+
* @deprecated Pass {@link APNGEncodeOptions} instead, the positional arguments will be removed in 4.0
329333
*/
330334
encodeAPNG(compression?: number): Promise<Uint8Array>;
331335
async encodeAPNG(compressionOrOptions: number | APNGEncodeOptions = 4): Promise<Uint8Array> {
@@ -455,6 +459,7 @@ export class Animation extends Array<Frame> {
455459
* Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
456460
* @param input The binary data to decode
457461
* @param onlyExtractFirstFrame Whether to stop decoding after the first frame
462+
* @deprecated Pass {@link AnimationDecodeOptions} instead, the positional arguments will be removed in 4.0
458463
*/
459464
static decode(input: BinaryData, onlyExtractFirstFrame?: boolean): Promise<Animation>;
460465
static async decode(input: BinaryData, onlyFirstFrameOrOptions: boolean | AnimationDecodeOptions = false): Promise<Animation> {

‎src/frame.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { isOptions, finite } from './util/options.ts';
33

44
/** Names of the frame disposal modes, see {@link Frame.disposalMode} */
55
export type FrameDisposalModeName = 'any' | 'keep' | 'previous' | 'background';
6-
/** Ids of the frame disposal modes, as GIFs store them (0 any, 1 keep, 2 previous, 3 background) */
6+
/**
7+
* Ids of the frame disposal modes, as GIFs store them (0 any, 1 keep, 2 previous, 3 background). Use the names
8+
* ({@link FrameDisposalModeName}) instead: the ids will be removed in 4.0
9+
*/
710
export type FrameDisposalModeId = 0 | 1 | 2 | 3;
811

912
const DISPOSAL_MODES: readonly FrameDisposalModeName[] = ['any', 'keep', 'previous', 'background'];
@@ -108,6 +111,7 @@ export class Frame extends Image {
108111
* @param xOffset The frame's offset on the x-axis
109112
* @param yOffset The frame's offset on the y-axis
110113
* @param disposalMode The frame's disposal mode
114+
* @deprecated Pass {@link FrameOptions} instead, the positional arguments will be removed in 4.0
111115
*/
112116
constructor(width: number, height: number, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId);
113117
constructor(width: number, height: number, durationOrOptions?: number | FrameOptions, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId) {
@@ -165,6 +169,7 @@ export class Frame extends Image {
165169
* @param xOffset The frame's offset on the x-axis
166170
* @param yOffset The frame's offset on the y-axis
167171
* @param disposalMode The frame's disposal mode
172+
* @deprecated Pass {@link FrameOptions} instead, the positional arguments will be removed in 4.0
168173
*/
169174
static from(image: Image, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId): Frame;
170175
static from(image: Image, durationOrOptions?: number | FrameOptions, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId): Frame {
@@ -185,12 +190,20 @@ export class Frame extends Image {
185190
return Frame.from(this, { duration: this.duration, xOffset: this.xOffset, yOffset: this.yOffset, disposalMode: this.disposalMode });
186191
}
187192

193+
/**
194+
* Removes borders of one color, moving the frame's offsets so the remaining pixels stay in place
195+
* @param options See {@link TrimOptions}
196+
*/
197+
override trim(options?: TrimOptions): this;
188198
/**
189199
* Removes borders of one color, moving the frame's offsets so the remaining pixels stay in place
190200
* @param color The border color, by default the color of the top left pixel
191201
* @param options See {@link TrimOptions}
202+
* @deprecated Pass {@link TrimOptions} instead, the positional arguments will be removed in 4.0
192203
*/
193-
override trim(color?: Color, options: TrimOptions = {}): this {
204+
override trim(color?: Color, options?: TrimOptions): this;
205+
override trim(colorOrOptions?: Color | TrimOptions, optionsArgument: TrimOptions = {}): this {
206+
const { color, ...options } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
194207
const bounds = this.__trim_bounds__(color, options);
195208
if (!bounds) return this;
196209
this.xOffset += bounds[0];
@@ -203,9 +216,17 @@ export class Frame extends Image {
203216
* Pass {@link ResizeToOptions} with only a width or a height to keep the aspect ratio.
204217
* @param width The new width
205218
* @param height The new height
206-
* @param modeOrOptions The resizing mode (default: {@link ResizeModes.BILINEAR}) or {@link ResizeOptions}
219+
* @param options See {@link ResizeOptions}
220+
*/
221+
override resize(width: number, height: number, options?: ResizeOptions): this;
222+
/**
223+
* Resizes the frame, scaling its offsets along with it.
224+
* @param width The new width
225+
* @param height The new height
226+
* @param mode The resizing mode (default: {@link ResizeModes.BILINEAR})
227+
* @deprecated Pass {@link ResizeOptions} with `mode` instead, the positional mode will be removed in 4.0
207228
*/
208-
override resize(width: number, height: number, modeOrOptions?: ResizeMode | ResizeOptions): this;
229+
override resize(width: number, height: number, mode?: ResizeMode): this;
209230
/**
210231
* Resizes the frame, scaling its offsets along with it: to the given width or height (the other side keeps the
211232
* aspect ratio), or to both

‎src/gainmap.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { Image } from './image.ts';
22
import * as jpeg from './codecs/jpeg.ts';
33
import * as codec from './codecs/gainmap.ts';
44
import { BinaryData, view } from './util/mem.ts';
5-
import { finite, quality as checkQuality } from './util/options.ts';
5+
import { finite, isOptions, quality as checkQuality } from './util/options.ts';
6+
7+
/** Options of {@link GainMap.encode} */
8+
export interface GainMapEncodeOptions {
9+
/** The JPEG quality (1-100). Default: `90` */
10+
quality?: number;
11+
}
612

713
/** Options of the {@link GainMap} constructor: per channel values can be one number for all three or [r, g, b] */
814
export interface GainMapOptions {
@@ -258,12 +264,21 @@ export class GainMap {
258264
this.image = out;
259265
}
260266

267+
/**
268+
* Encodes the gain map alone: a JPEG with its parameters (hdrgm XMP and ISO 21496-1), like the second image in an
269+
* Ultra HDR JPEG
270+
* @param options See {@link GainMapEncodeOptions}
271+
*/
272+
encode(options?: GainMapEncodeOptions): Promise<Uint8Array>;
261273
/**
262274
* Encodes the gain map alone: a JPEG with its parameters (hdrgm XMP and ISO 21496-1), like the second image in an
263275
* Ultra HDR JPEG
264276
* @param quality The JPEG quality (1-100). Default: `90`
277+
* @deprecated Pass {@link GainMapEncodeOptions} instead, the positional arguments will be removed in 4.0
265278
*/
266-
async encode(quality?: number): Promise<Uint8Array> {
279+
encode(quality?: number): Promise<Uint8Array>;
280+
async encode(qualityOrOptions?: number | GainMapEncodeOptions): Promise<Uint8Array> {
281+
let quality = isOptions(qualityOrOptions) ? qualityOrOptions.quality : qualityOrOptions;
267282
if (quality !== undefined) quality = Math.max(1, checkQuality(quality));
268283
return codec.mapWithMetadata(this.__body__(quality), this.__params__());
269284
}

0 commit comments

Comments
 (0)