imagescript 3.20.2 → 4.0.0-rc.1

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.js CHANGED
@@ -2,7 +2,7 @@ import { Image } from "./image.js";
2
2
  import * as jpeg from "./codecs/jpeg.js";
3
3
  import * as codec from "./codecs/gainmap.js";
4
4
  import { view } from "./util/mem.js";
5
- import { finite, isOptions, quality as checkQuality } from "./util/options.js";
5
+ import { finite, checkOptions, quality as checkQuality } from "./util/options.js";
6
6
  const perChannel = (value, name) => {
7
7
  const values = typeof value === 'number' ? [value, value, value] : Array.from(value);
8
8
  if (values.length === 1)
@@ -12,27 +12,58 @@ const perChannel = (value, name) => {
12
12
  return values.map(v => finite(v, name));
13
13
  };
14
14
  export class GainMap {
15
- gainMapMin;
16
- gainMapMax;
17
- gamma;
18
- offsetSDR;
19
- offsetHDR;
20
- hdrCapacityMin;
21
- hdrCapacityMax;
22
- useBaseColorSpace;
15
+ __min__;
16
+ __max__;
17
+ __gamma__;
18
+ __offsetSDR__;
19
+ __offsetHDR__;
20
+ __capacityMin__;
21
+ __capacityMax__;
22
+ __useBaseColorSpace__;
23
+ get min() { return [this.__min__[0], this.__min__[1], this.__min__[2]]; }
24
+ set min(value) { this.__min__ = perChannel(value, 'min'); }
25
+ get max() { return [this.__max__[0], this.__max__[1], this.__max__[2]]; }
26
+ set max(value) { this.__max__ = perChannel(value, 'max'); }
27
+ get gamma() { return [this.__gamma__[0], this.__gamma__[1], this.__gamma__[2]]; }
28
+ set gamma(value) {
29
+ const gamma = perChannel(value, 'gamma');
30
+ if (gamma.some(g => g <= 0))
31
+ throw new RangeError('The gain map\'s gamma must be above 0');
32
+ this.__gamma__ = gamma;
33
+ }
34
+ get offsetSDR() { return [this.__offsetSDR__[0], this.__offsetSDR__[1], this.__offsetSDR__[2]]; }
35
+ set offsetSDR(value) { this.__offsetSDR__ = perChannel(value, 'offsetSDR'); }
36
+ get offsetHDR() { return [this.__offsetHDR__[0], this.__offsetHDR__[1], this.__offsetHDR__[2]]; }
37
+ set offsetHDR(value) { this.__offsetHDR__ = perChannel(value, 'offsetHDR'); }
38
+ get hdrCapacityMin() { return this.__capacityMin__; }
39
+ set hdrCapacityMin(value) {
40
+ const capacity = finite(value, 'hdrCapacityMin');
41
+ if (capacity < 0)
42
+ throw new RangeError('The gain map\'s hdrCapacityMin can\'t be negative');
43
+ this.__capacityMin__ = capacity;
44
+ }
45
+ get hdrCapacityMax() { return this.__capacityMax__; }
46
+ set hdrCapacityMax(value) { this.__capacityMax__ = finite(value, 'hdrCapacityMax'); }
47
+ get useBaseColorSpace() { return this.__useBaseColorSpace__; }
48
+ set useBaseColorSpace(value) {
49
+ if (typeof value !== 'boolean')
50
+ throw new TypeError('useBaseColorSpace must be a boolean');
51
+ this.__useBaseColorSpace__ = value;
52
+ }
23
53
  __pixels__ = null;
24
54
  constructor(image, options = {}) {
55
+ checkOptions(options, 'new GainMap(image, { max: 2 })');
25
56
  if (!(image instanceof Image))
26
57
  throw new TypeError('The gain map has to be an Image');
27
- const { gainMapMin = 0, gainMapMax = 2, gamma = 1, offsetSDR = 1 / 64, offsetHDR = 1 / 64, hdrCapacityMin = 0, useBaseColorSpace = true } = options;
28
- this.gainMapMin = perChannel(gainMapMin, 'gainMapMin');
29
- this.gainMapMax = perChannel(gainMapMax, 'gainMapMax');
30
- this.gamma = perChannel(gamma, 'gamma');
31
- this.offsetSDR = perChannel(offsetSDR, 'offsetSDR');
32
- this.offsetHDR = perChannel(offsetHDR, 'offsetHDR');
33
- this.hdrCapacityMin = finite(hdrCapacityMin, 'hdrCapacityMin');
34
- this.hdrCapacityMax = finite(options.hdrCapacityMax ?? Math.max(...this.gainMapMax), 'hdrCapacityMax');
35
- this.useBaseColorSpace = Boolean(useBaseColorSpace);
58
+ const { min = 0, max = 2, gamma = 1, offsetSDR = 1 / 64, offsetHDR = 1 / 64, hdrCapacityMin = 0, useBaseColorSpace = true } = options;
59
+ this.min = min;
60
+ this.max = max;
61
+ this.gamma = gamma;
62
+ this.offsetSDR = offsetSDR;
63
+ this.offsetHDR = offsetHDR;
64
+ this.hdrCapacityMin = hdrCapacityMin;
65
+ this.hdrCapacityMax = options.hdrCapacityMax ?? Math.max(...this.__max__);
66
+ this.useBaseColorSpace = useBaseColorSpace;
36
67
  this.__pixels__ = image;
37
68
  this.__check__();
38
69
  }
@@ -52,9 +83,9 @@ export class GainMap {
52
83
  }
53
84
  return GainMap.__from_pixels__(image, params, false);
54
85
  }
55
- static __from_pixels__(image, params, grey = true) {
86
+ static __from_pixels__(image, params, gray = true) {
56
87
  const px = image.bitmap;
57
- if (grey) {
88
+ if (gray) {
58
89
  let spread = 0;
59
90
  for (let i = 0; i < px.length && spread <= 2; i += 4)
60
91
  spread = Math.max(spread, Math.abs(px[i] - px[i + 1]), Math.abs(px[i] - px[i + 2]));
@@ -68,36 +99,28 @@ export class GainMap {
68
99
  return gainMap;
69
100
  }
70
101
  __set_params__(p) {
71
- this.gainMapMin = p.min.slice();
72
- this.gainMapMax = p.max.slice();
73
- this.gamma = p.gamma.slice();
74
- this.offsetSDR = p.offsetSDR.slice();
75
- this.offsetHDR = p.offsetHDR.slice();
76
- this.hdrCapacityMin = p.capacityMin;
77
- this.hdrCapacityMax = p.capacityMax;
78
- this.useBaseColorSpace = p.useBaseColorSpace;
102
+ this.__min__ = p.min.slice();
103
+ this.__max__ = p.max.slice();
104
+ this.__gamma__ = p.gamma.slice();
105
+ this.__offsetSDR__ = p.offsetSDR.slice();
106
+ this.__offsetHDR__ = p.offsetHDR.slice();
107
+ this.__capacityMin__ = p.capacityMin;
108
+ this.__capacityMax__ = p.capacityMax;
109
+ this.__useBaseColorSpace__ = p.useBaseColorSpace;
79
110
  }
80
111
  __params__() {
81
112
  this.__check__();
82
113
  return {
83
- min: this.gainMapMin.slice(), max: this.gainMapMax.slice(), gamma: this.gamma.slice(), offsetSDR: this.offsetSDR.slice(),
84
- offsetHDR: this.offsetHDR.slice(), capacityMin: this.hdrCapacityMin, capacityMax: this.hdrCapacityMax, useBaseColorSpace: this.useBaseColorSpace
114
+ min: this.__min__.slice(), max: this.__max__.slice(), gamma: this.__gamma__.slice(), offsetSDR: this.__offsetSDR__.slice(),
115
+ offsetHDR: this.__offsetHDR__.slice(), capacityMin: this.__capacityMin__, capacityMax: this.__capacityMax__, useBaseColorSpace: this.__useBaseColorSpace__
85
116
  };
86
117
  }
87
118
  __check__() {
88
- const p = {
89
- min: perChannel(this.gainMapMin, 'gainMapMin'), max: perChannel(this.gainMapMax, 'gainMapMax'), gamma: perChannel(this.gamma, 'gamma'),
90
- offsetSDR: perChannel(this.offsetSDR, 'offsetSDR'), offsetHDR: perChannel(this.offsetHDR, 'offsetHDR'),
91
- capacityMin: finite(this.hdrCapacityMin, 'hdrCapacityMin'), capacityMax: finite(this.hdrCapacityMax, 'hdrCapacityMax'), useBaseColorSpace: this.useBaseColorSpace
92
- };
93
- for (let c = 0; c < 3; c++) {
94
- if (p.gamma[c] <= 0)
95
- throw new RangeError('The gain map\'s gamma must be above 0');
96
- if (p.max[c] < p.min[c])
97
- throw new RangeError('The gain map\'s gainMapMax must be at least its gainMapMin');
98
- }
99
- if (p.capacityMin < 0 || p.capacityMax <= p.capacityMin)
100
- throw new RangeError('The gain map\'s hdrCapacityMax must be above its hdrCapacityMin, which can\'t be negative');
119
+ for (let c = 0; c < 3; c++)
120
+ if (this.__max__[c] < this.__min__[c])
121
+ throw new RangeError('The gain map\'s max must be at least its min');
122
+ if (this.__capacityMax__ <= this.__capacityMin__)
123
+ throw new RangeError('The gain map\'s hdrCapacityMax must be above its hdrCapacityMin');
101
124
  }
102
125
  get image() {
103
126
  return this.__pixels__;
@@ -132,14 +155,14 @@ export class GainMap {
132
155
  image = image.clone().resize(width, height);
133
156
  const bitmap = image.bitmap;
134
157
  const pixels = new Uint8Array(bitmap.buffer, bitmap.byteOffset, bitmap.byteLength);
135
- const grey = this.channels === 1;
136
- return jpeg.encode(pixels, image.width, image.height, quality ?? 90, { grey, fullChroma: !grey });
158
+ const gray = this.channels === 1;
159
+ return jpeg.encode(pixels, image.width, image.height, quality ?? 90, { gray, fullChroma: !gray });
137
160
  }
138
161
  __neutral__() {
139
162
  return [0, 1, 2].map(c => {
140
- const min = this.gainMapMin[c], max = this.gainMapMax[c];
163
+ const min = this.__min__[c], max = this.__max__[c];
141
164
  const t = max > min ? Math.min(1, Math.max(0, -min / (max - min))) : 0;
142
- return Math.round(255 * t ** this.gamma[c]);
165
+ return Math.round(255 * t ** this.__gamma__[c]);
143
166
  });
144
167
  }
145
168
  __rescale__(oldWidth, oldHeight, width, height) {
@@ -177,8 +200,9 @@ export class GainMap {
177
200
  }
178
201
  this.image = out;
179
202
  }
180
- async encode(qualityOrOptions) {
181
- let quality = isOptions(qualityOrOptions) ? qualityOrOptions.quality : qualityOrOptions;
203
+ async encode(options) {
204
+ checkOptions(options, 'encode({ quality: 80 })');
205
+ let quality = options?.quality;
182
206
  if (quality !== undefined)
183
207
  quality = Math.max(1, checkQuality(quality));
184
208
  return codec.mapWithMetadata(this.__body__(quality), this.__params__());
@@ -51,10 +51,15 @@ export declare class ImageType {
51
51
  */
52
52
  static isBMP(data: BinaryData): boolean;
53
53
  /**
54
- * Whether the data is a Windows icon or cursor (ICO or CUR)
54
+ * Whether the data is a Windows icon (ICO)
55
55
  * @param data The file's data
56
56
  */
57
57
  static isICO(data: BinaryData): boolean;
58
+ /**
59
+ * Whether the data is a Windows cursor (CUR)
60
+ * @param data The file's data
61
+ */
62
+ static isCUR(data: BinaryData): boolean;
58
63
  /**
59
64
  * Whether the data starts like a QOI
60
65
  * @param data The file's data
@@ -91,7 +91,10 @@ export class ImageType {
91
91
  return isBMP(u8(dataView(data)));
92
92
  }
93
93
  static isICO(data) {
94
- return iconType(u8(dataView(data))) !== null;
94
+ return iconType(u8(dataView(data))) === 'ico';
95
+ }
96
+ static isCUR(data) {
97
+ return iconType(u8(dataView(data))) === 'cur';
95
98
  }
96
99
  static isQOI(data) {
97
100
  return isQOI(u8(dataView(data)));