imagescript 3.20.3 → 4.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +16 -12
  2. package/README.md +21 -12
  3. package/THIRD_PARTY_NOTICES.md +1 -1
  4. package/dist/animation-encoder.d.ts +1 -34
  5. package/dist/animation-encoder.js +35 -34
  6. package/dist/animation.d.ts +108 -122
  7. package/dist/animation.js +285 -121
  8. package/dist/codecs/av1/decode.js +1 -1
  9. package/dist/codecs/av1/encode.js +1 -1
  10. package/dist/codecs/gif.js +11 -9
  11. package/dist/codecs/hdr.d.ts +1 -1
  12. package/dist/codecs/jpeg.js +13 -13
  13. package/dist/codecs/jxl/headers.js +3 -3
  14. package/dist/codecs/jxl/patches-enc.js +1 -1
  15. package/dist/codecs/jxl.js +5 -5
  16. package/dist/codecs/netpbm.js +7 -7
  17. package/dist/codecs/png.js +15 -15
  18. package/dist/codecs/quantize.js +34 -8
  19. package/dist/codecs/tiff.js +1 -1
  20. package/dist/codecs/webp.js +3 -3
  21. package/dist/codecs/zlib.js +1 -1
  22. package/dist/colors.d.ts +8 -7
  23. package/dist/colors.js +23 -3
  24. package/dist/frame.d.ts +94 -100
  25. package/dist/frame.js +89 -65
  26. package/dist/gainmap.d.ts +88 -46
  27. package/dist/gainmap.js +75 -51
  28. package/dist/image-type.d.ts +6 -1
  29. package/dist/image-type.js +4 -1
  30. package/dist/image.d.ts +317 -738
  31. package/dist/image.js +308 -438
  32. package/dist/index.d.ts +19 -25
  33. package/dist/index.js +15 -14
  34. package/dist/metadata.d.ts +4 -3
  35. package/dist/metadata.js +9 -7
  36. package/dist/render/colr.js +1 -1
  37. package/dist/render/font.js +1 -1
  38. package/dist/render/layout.js +1 -1
  39. package/dist/render/segment.js +1 -1
  40. package/dist/render/shapers/hangul.js +1 -1
  41. package/dist/render/svg.js +68 -20
  42. package/dist/render/text.js +7 -9
  43. package/dist/text-layout.d.ts +5 -35
  44. package/dist/text-layout.js +11 -12
  45. package/dist/util/gain.js +12 -12
  46. package/dist/util/mem.d.ts +1 -1
  47. package/dist/util/optimize.js +3 -1
  48. package/dist/util/options.js +46 -7
  49. package/package.json +15 -10
  50. package/LICENSE.AGPL-3.0 +0 -619
  51. package/LICENSE.MIT +0 -19
  52. package/dist/gif.d.ts +0 -36
  53. package/dist/gif.js +0 -22
  54. package/dist/util/rotate.js +0 -43
  55. package/dist/version.js +0 -1
package/dist/frame.d.ts CHANGED
@@ -1,25 +1,13 @@
1
- import { Image, ResizeMode, ResizeOptions, ResizeToOptions, Color, TrimOptions } from './image.js';
2
- /** Names of the frame disposal modes, see {@link Frame.disposalMode} */
3
- export type FrameDisposalModeName = 'any' | 'keep' | 'previous' | 'background';
1
+ import { Image, ResizeOptions, ResizeToOptions, TrimOptions, RotateOptions, TransformOptions, AffineTransform, ShadowOptions, OutlineOptions } from './image.js';
4
2
  /**
5
- * Ids of the frame disposal modes, as GIFs store them (0 any, 1 keep, 2 previous, 3 background)
6
- * @deprecated Use the names ({@link FrameDisposalModeName}: `'any'`, `'keep'`, `'previous'`, `'background'`), the ids will
7
- * be removed in 4.0
3
+ * What happens to a frame's area before the next frame is drawn, see {@link Frame.disposalMode}:
4
+ * - `'keep'` (the default): the frame stays, the next one is drawn over it
5
+ * - `'background'`: the frame's area is cleared to transparent
6
+ * - `'previous'`: the frame's area goes back to what it was before the frame
7
+ * - `'any'`: the decoder decides (in practice like `'keep'`)
8
8
  */
9
- export type FrameDisposalModeId = 0 | 1 | 2 | 3;
10
- /** What happens to a frame's area before the next frame is drawn, see {@link Frame.disposalMode} */
11
- export declare class FrameDisposalModes {
12
- /** The decoder decides (in practice like {@link FrameDisposalModes.KEEP}) */
13
- static readonly ANY = "any";
14
- /** The frame stays, the next one is drawn over it (the default) */
15
- static readonly KEEP = "keep";
16
- /** The frame's area goes back to what it was before the frame */
17
- static readonly PREVIOUS = "previous";
18
- /** The frame's area is cleared to transparent */
19
- static readonly BACKGROUND = "background";
20
- private constructor();
21
- }
22
- /** Options of {@link Frame} and {@link Frame.from} */
9
+ export type FrameDisposalMode = 'any' | 'keep' | 'previous' | 'background';
10
+ /** Options of {@link Frame} */
23
11
  export interface FrameOptions {
24
12
  /** The frame's duration in milliseconds. Default: `100` */
25
13
  duration?: number;
@@ -28,124 +16,130 @@ export interface FrameOptions {
28
16
  /** The frame's offset on the y-axis. Default: `0` */
29
17
  yOffset?: number;
30
18
  /** The frame's disposal mode, see {@link Frame.disposalMode}. Default: `'keep'` */
31
- disposalMode?: FrameDisposalModeName | FrameDisposalModeId;
19
+ disposalMode?: FrameDisposalMode;
32
20
  }
33
21
  /**
34
- * Represents a frame of an {@link Animation}: an {@link Image} with a duration, offsets and a disposal mode, so every
35
- * method of {@link Image} works on it too
22
+ * A frame of an {@link Animation}: an {@link Image} ({@link Frame.image}) with a duration, offsets and a disposal mode.
23
+ * The frame has the methods that change the image's size (`resize()` including its `'contain'` and `'cover'` fit
24
+ * modes, `scale()`, `crop()`, `trim()`, `rotate()`, `transform()`, `dropShadow()` and `outline()`), which move the
25
+ * offsets along so the frame stays in place in the animation; every other change goes to the image
26
+ * (`frame.image.blur(2)`). The image's own size-changing methods leave the offsets as they are. Offsets can become
27
+ * negative: the parts of a frame outside the animation's canvas aren't encoded
36
28
  */
37
- export declare class Frame extends Image {
38
- /**
39
- * Milliseconds. GIFs store hundredths of a second, so this is rounded to 10 ms when encoding a GIF;
40
- * note that browsers play frames shorter than 20 ms with 100 ms
41
- */
42
- duration: number;
43
- /** The frame's horizontal offset in the animation */
44
- xOffset: number;
45
- /** The frame's vertical offset in the animation */
46
- yOffset: number;
29
+ export declare class Frame {
47
30
  /**
48
- * @deprecated Use {@link FrameDisposalModes.ANY}, `Frame.DISPOSAL_*` will be removed in 4.0
49
- * @group Deprecated
31
+ * Creates a frame of an image. The frame uses the image as it is (it isn't copied): changes to it are the frame's
32
+ * @param image The frame's pixels
33
+ * @param options See {@link FrameOptions}
50
34
  */
51
- static readonly DISPOSAL_ANY: typeof FrameDisposalModes.ANY;
35
+ constructor(image: Image, options?: FrameOptions);
36
+ /** The frame's pixels. Changing the image changes the frame; the offsets stay as they are */
37
+ get image(): Image;
52
38
  /**
53
- * @deprecated Use {@link FrameDisposalModes.KEEP}, `Frame.DISPOSAL_*` will be removed in 4.0
54
- * @group Deprecated
39
+ * Replaces the frame's pixels
40
+ * @param image The new image (used as it is, not copied)
55
41
  */
56
- static readonly DISPOSAL_KEEP: typeof FrameDisposalModes.KEEP;
42
+ set image(image: Image);
43
+ /** The frame's width (its image's) */
44
+ get width(): number;
45
+ /** The frame's height (its image's) */
46
+ get height(): number;
57
47
  /**
58
- * @deprecated Use {@link FrameDisposalModes.PREVIOUS}, `Frame.DISPOSAL_*` will be removed in 4.0
59
- * @group Deprecated
48
+ * The frame's duration in milliseconds (a finite number from 0, else it throws a RangeError). GIFs store
49
+ * hundredths of a second, so this is rounded to 10 ms when encoding a GIF; note that browsers play frames shorter
50
+ * than 20 ms with 100 ms
60
51
  */
61
- static readonly DISPOSAL_PREVIOUS: typeof FrameDisposalModes.PREVIOUS;
52
+ get duration(): number;
62
53
  /**
63
- * @deprecated Use {@link FrameDisposalModes.BACKGROUND}, `Frame.DISPOSAL_*` will be removed in 4.0
64
- * @group Deprecated
54
+ * Sets the duration
55
+ * @param duration Milliseconds, a finite number from 0
65
56
  */
66
- static readonly DISPOSAL_BACKGROUND: typeof FrameDisposalModes.BACKGROUND;
57
+ set duration(duration: number);
67
58
  /**
68
- * Creates a new, blank frame
69
- * @param width The width in pixels
70
- * @param height The height in pixels
71
- * @param options See {@link FrameOptions}
59
+ * The frame's horizontal offset in the animation: an integer, values with a fraction are truncated like image sizes
60
+ * (a value that isn't a finite number throws a RangeError)
72
61
  */
73
- constructor(width: number, height: number, options?: FrameOptions);
62
+ get xOffset(): number;
74
63
  /**
75
- * Creates a new, blank frame
76
- * @param width The width in pixels
77
- * @param height The height in pixels
78
- * @param duration The frame's duration (in ms)
79
- * @param xOffset The frame's offset on the x-axis
80
- * @param yOffset The frame's offset on the y-axis
81
- * @param disposalMode The frame's disposal mode
82
- * @deprecated Pass {@link FrameOptions} instead, the positional arguments will be removed in 4.0
64
+ * Sets the horizontal offset
65
+ * @param x The offset in pixels
83
66
  */
84
- constructor(width: number, height: number, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId);
67
+ set xOffset(x: number);
68
+ /** The frame's vertical offset in the animation: an integer, like {@link Frame.xOffset} */
69
+ get yOffset(): number;
85
70
  /**
86
- * The frame's disposal mode (`'any'`, `'keep'`, `'previous'` or `'background'`). Setting it by id (0-3) is deprecated,
87
- * the ids will be removed in 4.0
71
+ * Sets the vertical offset
72
+ * @param y The offset in pixels
88
73
  */
89
- get disposalMode(): FrameDisposalModeName;
74
+ set yOffset(y: number);
75
+ /** The frame's disposal mode, see {@link FrameDisposalMode} */
76
+ get disposalMode(): FrameDisposalMode;
90
77
  /**
91
78
  * Sets the disposal mode
92
- * @param disposalMode The mode's name (`'any'`, `'keep'`, `'previous'`, `'background'`) or, deprecated, its id (0-3)
79
+ * @param disposalMode The mode's name (`'any'`, `'keep'`, `'previous'`, `'background'`)
93
80
  */
94
- set disposalMode(disposalMode: FrameDisposalModeName | FrameDisposalModeId);
81
+ set disposalMode(disposalMode: FrameDisposalMode);
95
82
  /** A short description for debugging, e.g. `Frame<64x64x100ms>` */
96
83
  toString(): string;
84
+ /** Clones the frame: its image (with its pixels and gain map), duration, offsets and disposal mode */
85
+ clone(): Frame;
97
86
  /**
98
- * Converts an Image instance to a Frame, cloning it in the process
99
- * @param image The image to create the frame from
100
- * @param options See {@link FrameOptions}
87
+ * Resizes the frame, scaling its offsets along with it (as if the whole animation was resized by the same factor)
88
+ * @param width The new width
89
+ * @param height The new height
90
+ * @param options See {@link ResizeOptions}
91
+ */
92
+ resize(width: number, height: number, options?: ResizeOptions): this;
93
+ /**
94
+ * Resizes the frame, scaling its offsets along with it: to the given width or height (the other side keeps the
95
+ * aspect ratio), or to both
96
+ * @param options The size and how to resize, see {@link ResizeToOptions}
101
97
  */
102
- static from(image: Image, options?: FrameOptions): Frame;
98
+ resize(options: ResizeToOptions): this;
103
99
  /**
104
- * Converts an Image instance to a Frame, cloning it in the process
105
- * @param image The image to create the frame from
106
- * @param duration The frame's duration (in ms)
107
- * @param xOffset The frame's offset on the x-axis
108
- * @param yOffset The frame's offset on the y-axis
109
- * @param disposalMode The frame's disposal mode
110
- * @deprecated Pass {@link FrameOptions} instead, the positional arguments will be removed in 4.0
100
+ * Scales the frame by a factor, and its offsets along with it
101
+ * @param factor The factor
102
+ * @param options See {@link ResizeOptions}
111
103
  */
112
- static from(image: Image, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId): Frame;
104
+ scale(factor: number, options?: ResizeOptions): this;
113
105
  /**
114
- * Clones the frame, including its duration, offsets and disposal mode
106
+ * Crops the frame to a region of it, moving the offsets so the remaining pixels stay where they are in the
107
+ * animation. The region is clipped to the frame
108
+ * @param x The x offset in the frame (0-based)
109
+ * @param y The y offset in the frame (0-based)
110
+ * @param width The new width
111
+ * @param height The new height
115
112
  */
116
- clone(): Frame;
113
+ crop(x: number, y: number, width: number, height: number): this;
117
114
  /**
118
- * Removes borders of one color, moving the frame's offsets so the remaining pixels stay in place
115
+ * Removes borders of one color, moving the offsets so the remaining pixels stay where they are in the animation
119
116
  * @param options See {@link TrimOptions}
120
117
  */
121
118
  trim(options?: TrimOptions): this;
122
119
  /**
123
- * Removes borders of one color, moving the frame's offsets so the remaining pixels stay in place
124
- * @param color The border color, by default the color of the top left pixel
125
- * @param options See {@link TrimOptions}
126
- * @deprecated Pass {@link TrimOptions} instead, the positional arguments will be removed in 4.0
120
+ * Rotates the frame clockwise around its middle, like {@link Image.rotate}: when it grows (or shrinks), the
121
+ * offsets move so its middle stays where it is in the animation
122
+ * @param angle The angle in degrees, clockwise
123
+ * @param options See {@link RotateOptions}
127
124
  */
128
- trim(color?: Color, options?: TrimOptions): this;
125
+ rotate(angle: number, options?: RotateOptions): this;
129
126
  /**
130
- * Resizes the frame, scaling its offsets along with it.
131
- * Pass {@link ResizeToOptions} with only a width or a height to keep the aspect ratio.
132
- * @param width The new width
133
- * @param height The new height
134
- * @param options See {@link ResizeOptions}
127
+ * Transforms the frame with an affine transform, like {@link Image.transform}: when it grows, the offsets move
128
+ * so the transform's origin stays where it is in the animation
129
+ * @param matrix The six values [a, b, c, d, e, f]
130
+ * @param options See {@link TransformOptions}
135
131
  */
136
- resize(width: number, height: number, options?: ResizeOptions): this;
132
+ transform(matrix: AffineTransform, options?: TransformOptions): this;
137
133
  /**
138
- * Resizes the frame, scaling its offsets along with it.
139
- * @param width The new width
140
- * @param height The new height
141
- * @param mode The resizing mode (default: {@link ResizeModes.BILINEAR})
142
- * @deprecated Pass {@link ResizeOptions} with `mode` instead, the positional mode will be removed in 4.0
134
+ * Adds a shadow behind the frame's image, like {@link Image.dropShadow}: when it grows up or left, the offsets
135
+ * move so the image stays where it is in the animation
136
+ * @param options See {@link ShadowOptions}
143
137
  */
144
- resize(width: number, height: number, mode?: ResizeMode): this;
138
+ dropShadow(options?: ShadowOptions): this;
145
139
  /**
146
- * Resizes the frame, scaling its offsets along with it: to the given width or height (the other side keeps the
147
- * aspect ratio), or to both
148
- * @param options The size and how to resize, see {@link ResizeToOptions}
140
+ * Draws an outline around the frame's shape, like {@link Image.outline}: when it grows up or left, the offsets
141
+ * move so the image stays where it is in the animation
142
+ * @param options See {@link OutlineOptions}
149
143
  */
150
- resize(options: ResizeToOptions): this;
144
+ outline(options?: OutlineOptions): this;
151
145
  }
package/dist/frame.js CHANGED
@@ -1,40 +1,61 @@
1
- import { Image, resizeArguments } from "./image.js";
2
- import { isOptions, finite } from "./util/options.js";
1
+ import { Image } from "./image.js";
2
+ import { checkOptions, finite, oneOf } from "./util/options.js";
3
3
  const DISPOSAL_MODES = ['any', 'keep', 'previous', 'background'];
4
- export class FrameDisposalModes {
5
- static ANY = 'any';
6
- static KEEP = 'keep';
7
- static PREVIOUS = 'previous';
8
- static BACKGROUND = 'background';
9
- constructor() { }
10
- }
11
- export class Frame extends Image {
12
- duration;
13
- xOffset;
14
- yOffset;
4
+ export class Frame {
5
+ __image__;
6
+ __duration__ = 100;
7
+ __x__ = 0;
8
+ __y__ = 0;
15
9
  __disposalMode__ = 1;
16
- static DISPOSAL_ANY = FrameDisposalModes.ANY;
17
- static DISPOSAL_KEEP = FrameDisposalModes.KEEP;
18
- static DISPOSAL_PREVIOUS = FrameDisposalModes.PREVIOUS;
19
- static DISPOSAL_BACKGROUND = FrameDisposalModes.BACKGROUND;
20
10
  static __convert_disposal_mode__(mode) {
21
- const id = typeof mode === 'string' ? DISPOSAL_MODES.indexOf(mode) : mode;
22
- if (!(id >= 0 && id <= 3))
23
- throw new RangeError('Invalid disposal mode');
24
- return id;
25
- }
26
- constructor(width, height, durationOrOptions, xOffset, yOffset, disposalMode) {
27
- const { duration = 100, xOffset: x = 0, yOffset: y = 0, disposalMode: disposal = FrameDisposalModes.KEEP } = isOptions(durationOrOptions) ? durationOrOptions : { duration: durationOrOptions, xOffset, yOffset, disposalMode };
28
- if (finite(duration, 'frame duration') < 0)
29
- throw new RangeError('Invalid frame duration');
30
- finite(x, 'x offset');
31
- finite(y, 'y offset');
32
- super(width, height);
11
+ return DISPOSAL_MODES.indexOf(oneOf(mode, DISPOSAL_MODES, 'disposal mode'));
12
+ }
13
+ constructor(image, options = {}) {
14
+ if (!(image instanceof Image))
15
+ throw new TypeError('A frame takes an Image, like new Frame(image, { duration: 100 })');
16
+ checkOptions(options, 'Frame(image, { duration: 100 })');
17
+ const { duration = 100, xOffset: x = 0, yOffset: y = 0, disposalMode: disposal = 'keep' } = options;
18
+ this.__image__ = image;
33
19
  this.duration = duration;
34
20
  this.xOffset = x;
35
21
  this.yOffset = y;
36
22
  this.disposalMode = disposal;
37
23
  }
24
+ get image() {
25
+ return this.__image__;
26
+ }
27
+ set image(image) {
28
+ if (!(image instanceof Image))
29
+ throw new TypeError('A frame\'s image has to be an Image');
30
+ this.__image__ = image;
31
+ }
32
+ get width() {
33
+ return this.__image__.width;
34
+ }
35
+ get height() {
36
+ return this.__image__.height;
37
+ }
38
+ get duration() {
39
+ return this.__duration__;
40
+ }
41
+ set duration(duration) {
42
+ const value = finite(duration, 'frame duration');
43
+ if (value < 0)
44
+ throw new RangeError('Invalid frame duration');
45
+ this.__duration__ = value;
46
+ }
47
+ get xOffset() {
48
+ return this.__x__;
49
+ }
50
+ set xOffset(x) {
51
+ this.__x__ = Math.trunc(finite(x, 'x offset')) || 0;
52
+ }
53
+ get yOffset() {
54
+ return this.__y__;
55
+ }
56
+ set yOffset(y) {
57
+ this.__y__ = Math.trunc(finite(y, 'y offset')) || 0;
58
+ }
38
59
  get disposalMode() {
39
60
  return DISPOSAL_MODES[this.__disposalMode__];
40
61
  }
@@ -47,44 +68,47 @@ export class Frame extends Image {
47
68
  toString() {
48
69
  return `Frame<${this.width}x${this.height}x${this.duration}ms>`;
49
70
  }
50
- static from(image, durationOrOptions, xOffset, yOffset, disposalMode) {
51
- if (!(image instanceof Image))
52
- throw new TypeError('Invalid image passed');
53
- const options = isOptions(durationOrOptions) ? durationOrOptions : { duration: durationOrOptions, xOffset, yOffset, disposalMode };
54
- const frame = new Frame(image.width, image.height, options);
55
- frame.bitmap.set(image.bitmap);
56
- return frame;
57
- }
58
71
  clone() {
59
- return Frame.from(this, { duration: this.duration, xOffset: this.xOffset, yOffset: this.yOffset, disposalMode: this.disposalMode });
60
- }
61
- __room__() {
62
- return { left: this.xOffset, top: this.yOffset };
63
- }
64
- __grown__(left, top) {
65
- this.xOffset -= left;
66
- this.yOffset -= top;
67
- }
68
- trim(colorOrOptions, optionsArgument = {}) {
69
- const { color, ...options } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
70
- const bounds = this.__trim_bounds__(color, options);
71
- if (!bounds)
72
- return this;
73
- this.xOffset += bounds[0];
74
- this.yOffset += bounds[1];
75
- return this.__apply__(this.__crop__(...bounds));
76
- }
77
- resize(widthOrOptions, heightArgument, modeArgument) {
78
- const [width, height, modeOrOptions] = resizeArguments(widthOrOptions, heightArgument, modeArgument);
79
- const originalWidth = this.width;
80
- const originalHeight = this.height;
81
- super.resize(width, height, modeOrOptions);
82
- this.xOffset = Math.round(this.xOffset * this.width / originalWidth);
83
- this.yOffset = Math.round(this.yOffset * this.height / originalHeight);
72
+ return new Frame(this.__image__.clone(), { duration: this.duration, xOffset: this.xOffset, yOffset: this.yOffset, disposalMode: this.disposalMode });
73
+ }
74
+ __tracked__(change) {
75
+ const image = this.__image__;
76
+ image.__moved__ = (scaleX, scaleY, x, y) => {
77
+ this.xOffset = Math.round(this.xOffset * scaleX + x);
78
+ this.yOffset = Math.round(this.yOffset * scaleY + y);
79
+ };
80
+ try {
81
+ change(image);
82
+ }
83
+ finally {
84
+ delete image.__moved__;
85
+ }
84
86
  return this;
85
87
  }
86
- __rescaled__(width, height) {
87
- this.xOffset = Math.round(this.xOffset * width / this.width);
88
- this.yOffset = Math.round(this.yOffset * height / this.height);
88
+ resize(widthOrOptions, heightArgument, optionsArgument) {
89
+ return this.__tracked__(image => typeof widthOrOptions === 'number'
90
+ ? image.resize(widthOrOptions, heightArgument, optionsArgument)
91
+ : image.resize(widthOrOptions));
92
+ }
93
+ scale(factor, options) {
94
+ return this.__tracked__(image => image.scale(factor, options));
95
+ }
96
+ crop(x, y, width, height) {
97
+ return this.__tracked__(image => image.crop(x, y, width, height));
98
+ }
99
+ trim(options) {
100
+ return this.__tracked__(image => image.trim(options));
101
+ }
102
+ rotate(angle, options) {
103
+ return this.__tracked__(image => image.rotate(angle, options));
104
+ }
105
+ transform(matrix, options) {
106
+ return this.__tracked__(image => image.transform(matrix, options));
107
+ }
108
+ dropShadow(options) {
109
+ return this.__tracked__(image => image.dropShadow(options));
110
+ }
111
+ outline(options) {
112
+ return this.__tracked__(image => image.outline(options));
89
113
  }
90
114
  }
package/dist/gainmap.d.ts CHANGED
@@ -5,56 +5,105 @@ export interface GainMapEncodeOptions {
5
5
  /** The JPEG quality (1-100). Default: `90` */
6
6
  quality?: number;
7
7
  }
8
- /** Options of the {@link GainMap} constructor: per channel values can be one number for all three or [r, g, b] */
8
+ /** A gain-map parameter shared across channels or specified as `[red, green, blue]` */
9
+ export type GainMapChannelValues = number | readonly [red: number, green: number, blue: number];
10
+ /** Options of the {@link GainMap} constructor */
9
11
  export interface GainMapOptions {
10
- /** log2 of the boost of the map's value 0 (`-1`: half as bright). Default: `0` */
11
- gainMapMin?: number | number[];
12
- /** log2 of the boost of the map's value 255 (`2`: 4 times as bright). Default: `2` */
13
- gainMapMax?: number | number[];
14
- /** the gamma the map's values are stored with. Default: `1` */
15
- gamma?: number | number[];
16
- /** added to the SDR image's linear values before the boost (keeps black from being boosted forever). Default: `1/64` */
17
- offsetSDR?: number | number[];
18
- /** subtracted from the HDR result. Default: `1/64` */
19
- offsetHDR?: number | number[];
20
- /** log2 of the display headroom from which on the map starts to apply. Default: `0` */
12
+ /** Extra brightness at the map's darkest value (0); `-1` means half as bright. Default: `0` (no change) */
13
+ min?: GainMapChannelValues;
14
+ /** Extra brightness at the map's brightest value (255); `2` means four times as bright. Default: `2` */
15
+ max?: GainMapChannelValues;
16
+ /** How quickly the brightness changes between `min` and `max`; `1` changes it evenly. Default: `1` */
17
+ gamma?: GainMapChannelValues;
18
+ /**
19
+ * Small brightness offset added before applying the boost, to keep black from being boosted indefinitely.
20
+ * Default: `1/64`
21
+ */
22
+ offsetSDR?: GainMapChannelValues;
23
+ /** Small brightness offset subtracted from the result. Default: `1/64` */
24
+ offsetHDR?: GainMapChannelValues;
25
+ /** How bright a display must be before the map starts to apply; `0` is standard-screen brightness. Default: `0` */
21
26
  hdrCapacityMin?: number;
22
- /** log2 of the display headroom at which it applies fully. Default: `gainMapMax`'s largest value */
27
+ /** How bright a display must be for the map to apply fully. Default: the largest value in `max` */
23
28
  hdrCapacityMax?: number;
24
- /** whether the boost applies in the SDR image's color space (`false`: in the HDR image's). Default: `true` */
29
+ /** Whether the boost uses the original image's colors (`false`: the brighter HDR colors). Default: `true` */
25
30
  useBaseColorSpace?: boolean;
26
31
  }
27
32
  /**
28
- * A gain map: a small image saying how much brighter each part of an image gets on an HDR display, as Ultra HDR
29
- * (JPEG-R) photos from phones carry it. HDR displays show the image boosted by it, the others the image as it is.
33
+ * A gain map is a small companion image that tells a high-dynamic-range (HDR) display how much brighter to show each
34
+ * part of a photo. Standard displays ignore it and show the regular-brightness image.
35
+ *
36
+ * {@link Image.decode} reads gain maps from supported JPEG, AVIF and JPEG XL images into {@link Image.gainMap};
37
+ * {@link Image.encodeJPEG}, {@link Image.encodeAVIF} and {@link Image.encodeJXL} can write them. The map covers the
38
+ * whole image, at any resolution. Each pixel's value (0-255) controls how much extra brightness to add at that
39
+ * location. For developers who need the exact calculation, the boost is
40
+ * `2^(min + (max - min) * (v / 255)^(1 / gamma))`, per color channel.
30
41
  *
31
- * {@link Image.decode} reads the gain maps of JPEGs, AVIFs and JPEG XLs into {@link Image.gainMap},
32
- * {@link Image.encodeJPEG}, {@link Image.encodeAVIF} and {@link Image.encodeJXL} write them. The map covers the whole image, at any resolution (phones
33
- * write a quarter of the width and height). Its value `v` (0-255, per channel or one for all) boosts the linear
34
- * light of the image by `2^(gainMapMin + (gainMapMax - gainMapMin) * (v / 255)^(1 / gamma))`
42
+ * The parameters are checked when they are set: a value that isn't finite (or an array of other than 1 or 3 of them)
43
+ * throws, and the map keeps its value
35
44
  */
36
45
  export declare class GainMap {
37
- /** log2 of the boost of the map's value 0, per channel (red, green, blue) */
38
- gainMapMin: number[];
39
- /** log2 of the boost of the map's value 255, per channel */
40
- gainMapMax: number[];
41
- /** the gamma the map's values are stored with, per channel */
42
- gamma: number[];
43
- /** added to the SDR image's linear values before the boost, per channel */
44
- offsetSDR: number[];
45
- /** subtracted from the HDR result, per channel */
46
- offsetHDR: number[];
47
- /** log2 of the display headroom from which on the map starts to apply */
48
- hdrCapacityMin: number;
49
- /** log2 of the display headroom at which it applies fully */
50
- hdrCapacityMax: number;
51
- /** whether the boost applies in the SDR image's color space (else in the HDR image's) */
52
- useBaseColorSpace: boolean;
46
+ /** log2 of the boost of the map's value 0, per channel (red, green, blue; a copy: assign to change it) */
47
+ get min(): readonly [red: number, green: number, blue: number];
48
+ /**
49
+ * Replaces the value (checked: an invalid one throws and the old one stays)
50
+ * @param value One number for all channels or [r, g, b]
51
+ */
52
+ set min(value: GainMapChannelValues);
53
+ /** log2 of the boost of the map's value 255, per channel (a copy: assign to change it) */
54
+ get max(): readonly [red: number, green: number, blue: number];
55
+ /**
56
+ * Replaces the value (checked: an invalid one throws and the old one stays)
57
+ * @param value One number for all channels or [r, g, b]
58
+ */
59
+ set max(value: GainMapChannelValues);
60
+ /** The gamma the map's values are stored with, per channel (a copy: assign to change it) */
61
+ get gamma(): readonly [red: number, green: number, blue: number];
62
+ /**
63
+ * Replaces the value (checked: an invalid one throws and the old one stays)
64
+ * @param value One number above 0 for all channels or [r, g, b]
65
+ */
66
+ set gamma(value: GainMapChannelValues);
67
+ /** Added to the SDR image's linear values before the boost, per channel (a copy: assign to change it) */
68
+ get offsetSDR(): readonly [red: number, green: number, blue: number];
69
+ /**
70
+ * Replaces the value (checked: an invalid one throws and the old one stays)
71
+ * @param value One number for all channels or [r, g, b]
72
+ */
73
+ set offsetSDR(value: GainMapChannelValues);
74
+ /** Subtracted from the HDR result, per channel (a copy: assign to change it) */
75
+ get offsetHDR(): readonly [red: number, green: number, blue: number];
76
+ /**
77
+ * Replaces the value (checked: an invalid one throws and the old one stays)
78
+ * @param value One number for all channels or [r, g, b]
79
+ */
80
+ set offsetHDR(value: GainMapChannelValues);
81
+ /** log2 of the display headroom from which on the map starts to apply (from 0) */
82
+ get hdrCapacityMin(): number;
83
+ /**
84
+ * Replaces the value (checked: an invalid one throws and the old one stays)
85
+ * @param value A finite number from 0
86
+ */
87
+ set hdrCapacityMin(value: number);
88
+ /** log2 of the display headroom at which it applies fully (above `hdrCapacityMin`) */
89
+ get hdrCapacityMax(): number;
90
+ /**
91
+ * Replaces the value (checked: an invalid one throws and the old one stays)
92
+ * @param value A finite number
93
+ */
94
+ set hdrCapacityMax(value: number);
95
+ /** Whether the boost applies in the SDR image's color space (else in the HDR image's) */
96
+ get useBaseColorSpace(): boolean;
97
+ /**
98
+ * Replaces the value (checked: an invalid one throws and the old one stays)
99
+ * @param value Whether the boost applies in the SDR image's color space
100
+ */
101
+ set useBaseColorSpace(value: boolean);
53
102
  /** its pixels */
54
103
  private __pixels__;
55
104
  /**
56
105
  * Creates a gain map
57
- * @param image The map's pixels: grey (red, green and blue the same) for one map for all channels. Its alpha is
106
+ * @param image The map's pixels: gray (red, green and blue the same) for one map for all channels. Its alpha is
58
107
  * ignored
59
108
  * @param options See {@link GainMapOptions}
60
109
  */
@@ -72,7 +121,7 @@ export declare class GainMap {
72
121
  get width(): number;
73
122
  /** The map's height */
74
123
  get height(): number;
75
- /** `1` for a grey map (one boost for red, green and blue), `3` for one per channel */
124
+ /** `1` for a gray map (one boost for red, green and blue), `3` for one per channel */
76
125
  get channels(): 1 | 3;
77
126
  /** Clones the gain map (its pixels too) */
78
127
  clone(): GainMap;
@@ -81,14 +130,7 @@ export declare class GainMap {
81
130
  * Ultra HDR JPEG
82
131
  * @param options See {@link GainMapEncodeOptions}
83
132
  */
84
- encode(options?: GainMapEncodeOptions): Promise<Uint8Array>;
85
- /**
86
- * Encodes the gain map alone: a JPEG with its parameters (hdrgm XMP and ISO 21496-1), like the second image in an
87
- * Ultra HDR JPEG
88
- * @param quality The JPEG quality (1-100). Default: `90`
89
- * @deprecated Pass {@link GainMapEncodeOptions} instead, the positional arguments will be removed in 4.0
90
- */
91
- encode(quality?: number): Promise<Uint8Array>;
133
+ encode(options?: GainMapEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
92
134
  /**
93
135
  * Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR
94
136
  * JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters)