imagescript 4.0.0-rc.3 → 4.0.0-rc.5

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.
package/dist/gainmap.d.ts CHANGED
@@ -3,37 +3,61 @@ import type { DecodeOptions } from './image.js';
3
3
  import { BinaryData } from './util/mem.js';
4
4
  /** Options of {@link GainMap.encodeJPEG} */
5
5
  export interface GainMapEncodeOptions {
6
- /** The JPEG quality (1-100). Default: `90` */
6
+ /**
7
+ * The JPEG quality (1-100)
8
+ * @defaultValue `90`
9
+ */
7
10
  quality?: number;
8
11
  }
9
12
  /** A gain-map parameter shared across channels or specified as `[red, green, blue]` */
10
13
  export type GainMapChannelValues = number | readonly [red: number, green: number, blue: number];
11
14
  /** Options of the {@link GainMap} constructor */
12
15
  export interface GainMapOptions {
13
- /** Extra brightness at the map's darkest value (0); `-1` means half as bright. Default: `0` (no change) */
16
+ /**
17
+ * Extra brightness at the map's darkest value (0); `-1` means half as bright
18
+ * @defaultValue `0` (no change)
19
+ */
14
20
  min?: GainMapChannelValues;
15
- /** Extra brightness at the map's brightest value (255); `2` means four times as bright. Default: `2` */
21
+ /**
22
+ * Extra brightness at the map's brightest value (255); `2` means four times as bright
23
+ * @defaultValue `2`
24
+ */
16
25
  max?: GainMapChannelValues;
17
- /** How quickly the brightness changes between `min` and `max`; `1` changes it evenly. Default: `1` */
26
+ /**
27
+ * How quickly the brightness changes between `min` and `max`; `1` changes it evenly
28
+ * @defaultValue `1`
29
+ */
18
30
  gamma?: GainMapChannelValues;
19
31
  /**
20
32
  * Small brightness offset added before applying the boost, to keep black from being boosted indefinitely.
21
- * Default: `1/64`
33
+ * @defaultValue `1/64`
22
34
  */
23
35
  offsetSDR?: GainMapChannelValues;
24
- /** Small brightness offset subtracted from the result. Default: `1/64` */
36
+ /**
37
+ * Small brightness offset subtracted from the result
38
+ * @defaultValue `1/64`
39
+ */
25
40
  offsetHDR?: GainMapChannelValues;
26
- /** How bright a display must be before the map starts to apply; `0` is standard-screen brightness. Default: `0` */
41
+ /**
42
+ * How bright a display must be before the map starts to apply; `0` is standard-screen brightness
43
+ * @defaultValue `0`
44
+ */
27
45
  hdrCapacityMin?: number;
28
- /** How bright a display must be for the map to apply fully. Default: the largest value in `max` */
46
+ /**
47
+ * How bright a display must be for the map to apply fully
48
+ * @defaultValue The largest value in `max`
49
+ */
29
50
  hdrCapacityMax?: number;
30
- /** Whether the boost uses the original image's colors (`false`: the brighter HDR colors). Default: `true` */
51
+ /**
52
+ * Whether the boost uses the original image's colors (`false`: the brighter HDR colors)
53
+ * @defaultValue `true`
54
+ */
31
55
  useBaseColorSpace?: boolean;
32
56
  }
33
57
  /**
34
58
  * A gain map is a small companion image that tells a high-dynamic-range (HDR) display how much brighter to show each
35
- * part of a photo. Standard displays ignore it and show the regular-brightness image.
36
- *
59
+ * part of a photo. Standard displays ignore it and show the regular-brightness image
60
+ * @remarks
37
61
  * {@link Image.decode} reads gain maps from supported JPEG, AVIF and JPEG XL images into {@link Image.gainMap};
38
62
  * {@link Image.encodeJPEG}, {@link Image.encodeAVIF} and {@link Image.encodeJXL} can write them. The map covers the
39
63
  * whole image, at any resolution. Each pixel's value (0-255) controls how much extra brightness to add at that
@@ -83,14 +107,14 @@ export declare class GainMap {
83
107
  get hdrCapacityMin(): number;
84
108
  /**
85
109
  * Replaces the value (checked: an invalid one throws and the old one stays)
86
- * @param value A finite number from 0
110
+ * @param value A finite number from 0, below `hdrCapacityMax`
87
111
  */
88
112
  set hdrCapacityMin(value: number);
89
113
  /** log2 of the display headroom at which it applies fully (above `hdrCapacityMin`) */
90
114
  get hdrCapacityMax(): number;
91
115
  /**
92
116
  * Replaces the value (checked: an invalid one throws and the old one stays)
93
- * @param value A finite number
117
+ * @param value A finite number above `hdrCapacityMin`
94
118
  */
95
119
  set hdrCapacityMax(value: number);
96
120
  /** Whether the boost applies in the SDR image's color space (else in the HDR image's) */
@@ -130,14 +154,16 @@ export declare class GainMap {
130
154
  * Encodes the gain map alone: a JPEG with its parameters (hdrgm XMP and ISO 21496-1), like the second image in an
131
155
  * Ultra HDR JPEG
132
156
  * @param options See {@link GainMapEncodeOptions}
157
+ * @see {@link Image.encodeJPEG} for the image with its gain map embedded
133
158
  */
134
159
  encodeJPEG(options?: GainMapEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
135
160
  /**
136
161
  * Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR
137
162
  * JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters)
138
163
  * @param data The JPEG
139
- * @param options The `maxPixels` limit, like {@link DecodeOptions.maxPixels} (a map larger than that throws a
140
- * {@link LimitError}); a damaged JPEG throws an error saying so, like {@link Image.decode}
164
+ * @param options The `maxPixels` limit, like {@link DecodeOptions.maxPixels}
165
+ * @throws {@link LimitError} if the map has more than `options.maxPixels` pixels
166
+ * @throws `Error` if the data has no gain map (or no parameters for it), or is damaged
141
167
  */
142
168
  static decode(data: BinaryData, options?: Pick<DecodeOptions, 'maxPixels'>): Promise<GainMap>;
143
169
  }
package/dist/gainmap.js CHANGED
@@ -5,11 +5,12 @@ import * as codec from "./codecs/gainmap.js";
5
5
  import { view } from "./util/mem.js";
6
6
  import { finite, checkOptions, pixelLimit, quality as checkQuality } from "./util/options.js";
7
7
  const perChannel = (value, name) => {
8
+ const given = value;
9
+ if (typeof given !== 'number' && !Array.isArray(given))
10
+ throw new TypeError(`${name} must be a number or an array of 3 numbers, not ${given === null ? 'null' : typeof given}`);
8
11
  const values = typeof value === 'number' ? [value, value, value] : Array.from(value);
9
- if (values.length === 1)
10
- values.push(values[0], values[0]);
11
12
  if (values.length !== 3)
12
- throw new RangeError(`${name} must be a number or an array of 3 numbers`);
13
+ throw new RangeError(`${name} must be a number or an array of 3 numbers, not ${values.length}`);
13
14
  return values.map(v => finite(v, name));
14
15
  };
15
16
  export class GainMap {
@@ -41,10 +42,17 @@ export class GainMap {
41
42
  const capacity = finite(value, 'hdrCapacityMin');
42
43
  if (capacity < 0)
43
44
  throw new RangeError('The gain map\'s hdrCapacityMin can\'t be negative');
45
+ if (this.__capacityMax__ !== undefined && !(capacity < this.__capacityMax__))
46
+ throw new RangeError(`The gain map's hdrCapacityMin must be below its hdrCapacityMax (${capacity} >= ${this.__capacityMax__})`);
44
47
  this.__capacityMin__ = capacity;
45
48
  }
46
49
  get hdrCapacityMax() { return this.__capacityMax__; }
47
- set hdrCapacityMax(value) { this.__capacityMax__ = finite(value, 'hdrCapacityMax'); }
50
+ set hdrCapacityMax(value) {
51
+ const capacity = finite(value, 'hdrCapacityMax');
52
+ if (this.__capacityMin__ !== undefined && !(capacity > this.__capacityMin__))
53
+ throw new RangeError(`The gain map's hdrCapacityMax (by default the largest max) must be above its hdrCapacityMin (${capacity} <= ${this.__capacityMin__})`);
54
+ this.__capacityMax__ = capacity;
55
+ }
48
56
  get useBaseColorSpace() { return this.__useBaseColorSpace__; }
49
57
  set useBaseColorSpace(value) {
50
58
  if (typeof value !== 'boolean')
@@ -63,7 +71,7 @@ export class GainMap {
63
71
  this.offsetSDR = offsetSDR;
64
72
  this.offsetHDR = offsetHDR;
65
73
  this.hdrCapacityMin = hdrCapacityMin;
66
- this.hdrCapacityMax = options.hdrCapacityMax ?? Math.max(...this.__max__);
74
+ this.hdrCapacityMax = options.hdrCapacityMax === undefined ? Math.max(...this.__max__) : options.hdrCapacityMax;
67
75
  this.useBaseColorSpace = useBaseColorSpace;
68
76
  this.__pixels__ = image;
69
77
  this.__check__();
@@ -120,8 +128,6 @@ export class GainMap {
120
128
  for (let c = 0; c < 3; c++)
121
129
  if (this.__max__[c] < this.__min__[c])
122
130
  throw new RangeError(`The gain map's max must be at least its min (${this.__max__[c]} < ${this.__min__[c]}; the values are checked together when the map is cloned or encoded)`);
123
- if (this.__capacityMax__ <= this.__capacityMin__)
124
- throw new RangeError(`The gain map's hdrCapacityMax (by default the largest max) must be above its hdrCapacityMin (${this.__capacityMax__} <= ${this.__capacityMin__})`);
125
131
  }
126
132
  get image() {
127
133
  return this.__pixels__;
@@ -203,9 +209,12 @@ export class GainMap {
203
209
  }
204
210
  async encodeJPEG(options) {
205
211
  checkOptions(options, 'gainMap.encodeJPEG({ quality: 80 })');
206
- let quality = options?.quality;
207
- if (quality !== undefined)
208
- quality = Math.max(1, checkQuality(quality));
212
+ const quality = options?.quality;
213
+ if (quality !== undefined) {
214
+ checkQuality(quality);
215
+ if (quality < 1)
216
+ throw new RangeError(`quality has to be between 1 and 100, not ${quality}`);
217
+ }
209
218
  return codec.mapWithMetadata(this.__body__(quality), this.__params__());
210
219
  }
211
220
  static async decode(data, options = {}) {