imagescript 4.0.0-rc.3 → 4.0.0-rc.4

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.
@@ -22,7 +22,7 @@ function chunks(data, start, end) {
22
22
  const type = view.getUint32(offset, true);
23
23
  const size = view.getUint32(offset + 4, true);
24
24
  const dataStart = offset + 8;
25
- out.push({ type, data: data.subarray(dataStart, Math.min(end, dataStart + size)) });
25
+ out.push({ type, data: data.subarray(dataStart, Math.min(end, dataStart + size)), cut: dataStart + size > end });
26
26
  offset = dataStart + size + (size & 1);
27
27
  }
28
28
  return out;
@@ -148,19 +148,22 @@ export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PI
148
148
  const width = u24(header.data, 4) + 1, height = u24(header.data, 7) + 1;
149
149
  checkPixels(width, height, maxPixels, firstFrameOnly ? 1 : list.filter(c => c.type === ANMF).length);
150
150
  let loopCount = 0, background = 0;
151
- const frames = [];
152
- for (const chunk of list) {
153
- if (chunk.type === ANIM && chunk.data.length >= 6) {
154
- const d = chunk.data;
155
- background = (d[3] << 24 | d[2] << 16 | d[1] << 8 | d[0]) >>> 0;
156
- loopCount = d[4] | d[5] << 8;
157
- }
158
- else if (chunk.type === ANMF) {
159
- const d = chunk.data;
160
- if (d.length < 16)
161
- throw new Error('WebP: invalid ANMF chunk');
151
+ const anim = list.find(c => c.type === ANIM && c.data.length >= 6);
152
+ if (anim) {
153
+ const d = anim.data;
154
+ background = (d[3] << 24 | d[2] << 16 | d[1] << 8 | d[0]) >>> 0;
155
+ loopCount = d[4] | d[5] << 8;
156
+ }
157
+ const anmf = list.filter(c => c.type === ANMF && !c.cut);
158
+ if (!anmf.length && list.some(c => c.type === ANMF))
159
+ throw new Error('WebP: unexpected end of data');
160
+ for (const { data } of anmf)
161
+ if (data.length < 16)
162
+ throw new Error('WebP: invalid ANMF chunk');
163
+ function* frames() {
164
+ for (const { data: d } of anmf) {
162
165
  const frame = decodeBitstream(chunks(d, 16, d.length), maxPixels);
163
- frames.push({
166
+ yield {
164
167
  x: u24(d, 0) * 2,
165
168
  y: u24(d, 3) * 2,
166
169
  width: frame.width,
@@ -169,14 +172,12 @@ export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PI
169
172
  blend: (d[15] & 2) === 0,
170
173
  dispose: (d[15] & 1) === 1,
171
174
  pixels: frame.pixels
172
- });
175
+ };
173
176
  if (firstFrameOnly)
174
- break;
177
+ return;
175
178
  }
176
179
  }
177
- if (!frames.length)
178
- throw new Error('WebP: animation without frames');
179
- return { width, height, loopCount, background, frames, icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
180
+ return { width, height, loopCount, background, frames: frames(), icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
180
181
  }
181
182
  function orientationOf(list) {
182
183
  const exif = list.find(c => c.type === EXIF);
package/dist/colors.js CHANGED
@@ -1,4 +1,4 @@
1
- import { finite, color as checkColor } from "./util/options.js";
1
+ import { finite, checkNumber, color as checkColor } from "./util/options.js";
2
2
  export function channel(value) {
3
3
  return value > 255 ? 255 : value > 0 ? Math.round(value) : 0;
4
4
  }
@@ -48,20 +48,27 @@ export function rgbaToHSLA(r, g, b, a) {
48
48
  }
49
49
  return [h, s, l, a / 255];
50
50
  }
51
+ function checkPosition(position) {
52
+ checkNumber(position, 'gradient position');
53
+ if (Number.isNaN(position))
54
+ throw new RangeError(`Invalid gradient position: ${position}`);
55
+ }
51
56
  function gradientBetween(startColor, endColor) {
52
- const sr = startColor >>> 24;
53
- const sg = startColor >> 16 & 0xff;
54
- const sb = startColor >> 8 & 0xff;
55
- const sa = startColor & 0xff;
56
- const er = (endColor >>> 24) - sr;
57
- const eg = (endColor >> 16 & 0xff) - sg;
58
- const eb = (endColor >> 8 & 0xff) - sb;
59
- const ea = (endColor & 0xff) - sa;
57
+ const sr = startColor >>> 24, sg = startColor >> 16 & 0xff, sb = startColor >> 8 & 0xff, sa = startColor & 0xff;
58
+ const er = (endColor >>> 24) - sr, eg = (endColor >> 16 & 0xff) - sg, eb = (endColor >> 8 & 0xff) - sb, ea = (endColor & 0xff) - sa;
59
+ if (sa === 255 && ea === 0)
60
+ return position => {
61
+ const r = sr + position * er, g = sg + position * eg, b = sb + position * eb;
62
+ return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | 0xff) >>> 0;
63
+ };
64
+ const pr = sr * sa / 255, pg = sg * sa / 255, pb = sb * sa / 255;
65
+ const dr = (er + sr) * (ea + sa) / 255 - pr, dg = (eg + sg) * (ea + sa) / 255 - pg, db = (eb + sb) * (ea + sa) / 255 - pb;
60
66
  return position => {
61
- const r = sr + position * er;
62
- const g = sg + position * eg;
63
- const b = sb + position * eb;
64
67
  const a = sa + position * ea;
68
+ if (a <= 0)
69
+ return 0;
70
+ const k = 255 / a;
71
+ const r = Math.min(255, (pr + position * dr) * k + 1e-9), g = Math.min(255, (pg + position * dg) * k + 1e-9), b = Math.min(255, (pb + position * db) * k + 1e-9);
65
72
  return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff)) >>> 0;
66
73
  };
67
74
  }
@@ -130,13 +137,15 @@ export class Colors {
130
137
  if (positions.length === 0)
131
138
  throw new RangeError('Invalid gradient point count');
132
139
  else if (positions.length === 1) {
133
- return () => values[0];
140
+ return position => {
141
+ checkPosition(position);
142
+ return values[0];
143
+ };
134
144
  }
135
145
  else if (positions.length === 2) {
136
146
  const gradient = gradientBetween(values[0], values[1]);
137
147
  return position => {
138
- if (Number.isNaN(position))
139
- throw new RangeError(`Invalid gradient position: ${position}`);
148
+ checkPosition(position);
140
149
  if (position <= positions[0])
141
150
  return values[0];
142
151
  if (position >= positions[1])
@@ -163,8 +172,7 @@ export class Colors {
163
172
  }
164
173
  if (!gradients.length)
165
174
  return position => {
166
- if (Number.isNaN(position))
167
- throw new RangeError(`Invalid gradient position: ${position}`);
175
+ checkPosition(position);
168
176
  return position <= minDef ? values[0] : values[values.length - 1];
169
177
  };
170
178
  return position => {
package/dist/frame.d.ts CHANGED
@@ -9,17 +9,30 @@ import { Image, ResizeOptions, ResizeToOptions, TrimOptions, RotateOptions, Tran
9
9
  export type FrameDisposalMode = 'any' | 'keep' | 'previous' | 'background';
10
10
  /** Options of {@link Frame} */
11
11
  export interface FrameOptions {
12
- /** The frame's duration in milliseconds. Default: `100` */
12
+ /**
13
+ * The frame's duration in milliseconds
14
+ * @defaultValue `100`
15
+ */
13
16
  duration?: number;
14
- /** The frame's offset on the x-axis. Default: `0` */
17
+ /**
18
+ * The frame's offset on the x-axis
19
+ * @defaultValue `0`
20
+ */
15
21
  xOffset?: number;
16
- /** The frame's offset on the y-axis. Default: `0` */
22
+ /**
23
+ * The frame's offset on the y-axis
24
+ * @defaultValue `0`
25
+ */
17
26
  yOffset?: number;
18
- /** The frame's disposal mode, see {@link Frame.disposalMode}. Default: `'keep'` */
27
+ /**
28
+ * The frame's disposal mode, see {@link Frame.disposalMode}
29
+ * @defaultValue `'keep'`
30
+ */
19
31
  disposalMode?: FrameDisposalMode;
20
32
  }
21
33
  /**
22
- * A frame of an {@link Animation}: an {@link Image} ({@link Frame.image}) with a duration, offsets and a disposal mode.
34
+ * A frame of an {@link Animation}: an {@link Image} ({@link Frame.image}) with a duration, offsets and a disposal mode
35
+ * @remarks
23
36
  * The frame has the methods that change the image's size (`resize()` including its `'contain'` and `'cover'` fit
24
37
  * modes, `scale()`, `crop()`, `trim()`, `rotate()`, `transform()`, `dropShadow()` and `outline()`), which move the
25
38
  * offsets along so the frame stays in place in the animation; every other change goes to the image
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
@@ -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,6 +5,9 @@ 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
12
  if (values.length === 1)
10
13
  values.push(values[0], values[0]);
@@ -63,7 +66,7 @@ export class GainMap {
63
66
  this.offsetSDR = offsetSDR;
64
67
  this.offsetHDR = offsetHDR;
65
68
  this.hdrCapacityMin = hdrCapacityMin;
66
- this.hdrCapacityMax = options.hdrCapacityMax ?? Math.max(...this.__max__);
69
+ this.hdrCapacityMax = options.hdrCapacityMax === undefined ? Math.max(...this.__max__) : options.hdrCapacityMax;
67
70
  this.useBaseColorSpace = useBaseColorSpace;
68
71
  this.__pixels__ = image;
69
72
  this.__check__();