imagescript 3.13.0 → 3.13.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.
package/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  <h1 align="center">
2
2
  <picture>
3
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/matmen/ImageScript/master/media/wordmark-tagline-dark.svg">
4
- <img alt="ImageScript: Pure TypeScript image processing that runs everywhere" src="https://raw.githubusercontent.com/matmen/ImageScript/master/media/wordmark-tagline.svg" width="640">
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/imagescript/ImageScript/master/media/wordmark-tagline-dark.svg">
4
+ <img alt="ImageScript: Pure TypeScript image processing that runs everywhere" src="https://raw.githubusercontent.com/imagescript/ImageScript/master/media/wordmark-tagline.svg" width="640">
5
5
  </picture>
6
6
  </h1>
7
7
 
8
- [![Discord Server](https://img.shields.io/discord/691713541262147687.svg?label=Discord&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2&style=for-the-badge)](https://discord.gg/8hPrwAH)
9
8
  [![Documentation](https://img.shields.io/badge/Documentation-informational?style=for-the-badge&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIiB3aWR0aD0iNTEyIiBoZWlnaHQ9IjUxMiIgcm9sZT0iaW1nIiBhcmlhLWxhYmVsPSJJbWFnZVNjcmlwdCI+PHRpdGxlPkltYWdlU2NyaXB0PC90aXRsZT48cGF0aCBkPSJNMTMyIDk2aC0xNGEzNCAzNCAwIDAgMC0zNCAzNHY3NGEzMCAzMCAwIDAgMS0zMCAzMGgtNnY0NGg2YTMwIDMwIDAgMCAxIDMwIDMwdjc0YTM0IDM0IDAgMCAwIDM0IDM0aDE0IE0zODAgOTZoMTRhMzQgMzQgMCAwIDEgMzQgMzR2NzRhMzAgMzAgMCAwID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github](https://img.shields.io/badge/Github-Repository-181717?logo=github&style=for-the-badge)](https://github.com/matmen/ImageScript)
11
- [![NPM](https://nodei.co/npm/imagescript.png)](https://www.npmjs.com/package/imagescript)
9
+ [![Playground](https://img.shields.io/badge/Playground-Try%20it%20live-ff2d75?style=for-the-badge&labelColor=ff7a18&logoColor=white&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHZpZXdCb3g9JzAgMCAyNCAyNCc%2BPHBhdGggZmlsbD0nd2hpdGUnIGQ9J003IDQuNXYxNWwxMi41LTcuNXonLz48L3N2Zz4%3D)](https://try.imagescript.dev/)
10
+
11
+ [![Github](https://img.shields.io/badge/Github-Repository-181717?logo=github&style=for-the-badge)](https://github.com/imagescript/ImageScript)
12
+ [![Discord Server](https://img.shields.io/discord/691713541262147687.svg?label=Discord&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2&style=for-the-badge)](https://discord.gg/8hPrwAH)
12
13
 
13
14
  ---
14
15
 
@@ -35,10 +36,13 @@ image.resize(256, Image.RESIZE_AUTO).roundCorners(16);
35
36
  await writeFile('output.png', await image.encode());
36
37
  ```
37
38
 
38
- Coordinates start at `(0, 0)`, the top left pixel. For a bigger example, see the
39
- [script that generates this image](https://github.com/matmen/ImageScript/blob/master/tests/readme.js):
39
+ <p align="center">
40
+ <a href="https://try.imagescript.dev/">
41
+ <img src="https://github.com/imagescript/ImageScript/raw/master/media/playground-cta.svg" alt="Try ImageScript in the Playground" width="440">
42
+ </a>
43
+ </p>
40
44
 
41
- [![Example](https://github.com/matmen/ImageScript/raw/master/tests/targets/readme.png)](https://github.com/matmen/ImageScript/blob/master/tests/readme.js)
45
+ [![Example](https://github.com/imagescript/ImageScript/raw/master/tests/targets/readme.png)](https://github.com/imagescript/ImageScript/blob/master/tests/readme.js)
42
46
 
43
47
  ---
44
48
 
@@ -76,7 +80,7 @@ P3, Adobe RGB, ...) are converted to sRGB when decoding, so images look the way
76
80
  [hueShift](https://imagescript.dev/classes/Image.html#hueshift), ...
77
81
 
78
82
  The [documentation](https://imagescript.dev/) covers the full API. When upgrading from an older major version,
79
- see the [changelog](https://github.com/matmen/ImageScript/blob/master/CHANGELOG.md). [bench/](bench/README.md)
83
+ see the [changelog](https://github.com/imagescript/ImageScript/blob/master/CHANGELOG.md). [bench/](bench/README.md)
80
84
  compares ImageScript's speed and file sizes with Jimp, sharp and other libraries.
81
85
 
82
86
  ---
package/dist/animation.js CHANGED
@@ -1,4 +1,4 @@
1
- import { profileTransform } from "./util/icc.js";
1
+ import { colorSpaceTransform, profileTransform } from "./util/icc.js";
2
2
  import * as gifCodec from "./codecs/gif.js";
3
3
  import * as png from "./codecs/png.js";
4
4
  import * as webp from "./codecs/webp.js";
@@ -191,8 +191,8 @@ export class Animation extends Array {
191
191
  const { onlyFirstFrame: onlyExtractFirstFrame = false, maxPixels = MAX_PIXELS, applyColorProfile = true, threads = Math.max(1, Math.min(availableThreads(), 8)) } = isOptions(onlyFirstFrameOrOptions) ? onlyFirstFrameOrOptions : { onlyFirstFrame: onlyFirstFrameOrOptions };
192
192
  if (!Number.isInteger(threads) || threads < 1)
193
193
  throw new RangeError(`threads must be a positive integer, not ${threads}`);
194
- const profile = (frames, icc) => {
195
- const transform = applyColorProfile && icc ? profileTransform(icc) : null;
194
+ const profile = (frames, icc, space) => {
195
+ const transform = !applyColorProfile ? null : icc ? profileTransform(icc) : space ? colorSpaceTransform(space.xy, space.curve) : null;
196
196
  if (transform)
197
197
  for (const frame of frames)
198
198
  transform(frame.bitmap);
@@ -218,17 +218,18 @@ export class Animation extends Array {
218
218
  }
219
219
  if (type === 'jxl') {
220
220
  const frames = [];
221
- let loopCount = -1, icc;
221
+ let loopCount = -1, icc, space;
222
222
  for await (const decoded of jxl.frames(data, maxPixels, 'reinhard', threads)) {
223
223
  const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
224
224
  frame.bitmap = decoded.pixels;
225
225
  frames.push(frame.__orient__(decoded.orientation));
226
226
  loopCount = fromPlays(decoded.loopCount);
227
227
  icc = decoded.icc;
228
+ space = decoded.colorSpace;
228
229
  if (onlyExtractFirstFrame)
229
230
  break;
230
231
  }
231
- return new Animation(profile(frames, icc), frames.length > 1 ? loopCount : -1);
232
+ return new Animation(profile(frames, icc, space), frames.length > 1 ? loopCount : -1);
232
233
  }
233
234
  if (type)
234
235
  return new Animation([Frame.from(await Image.decode(data, { maxPixels, applyColorProfile }), 0)]);
@@ -1,13 +1,14 @@
1
1
  import { startThreads } from "../util/threads.js";
2
2
  import { MAX_PIXELS, checkPixels } from "../util/limits.js";
3
3
  import { BitReader } from "./jxl/bits.js";
4
- import { decodeICC, imageHeader, readICC } from "./jxl/headers.js";
4
+ import { CS_GREY, CS_RGB, decodeICC, imageHeader, PRI_BT2100, PRI_CUSTOM, PRI_P3, readICC, TF_DCI, TF_HLG, TF_LINEAR, TF_PQ, WP_CUSTOM, WP_DCI, WP_E } from "./jxl/headers.js";
5
5
  import { frameHeader, toc } from "./jxl/frame-header.js";
6
6
  import { decodeFrame } from "./jxl/frame.js";
7
7
  import { ColorOutput } from "./jxl/color.js";
8
8
  import { encodeRGBA } from "./jxl/fast-lossless.js";
9
9
  import { encodeVarDCT } from "./jxl/vardct-enc.js";
10
10
  import { concat } from "../util/mem.js";
11
+ import { srgbDecode } from "../util/icc.js";
11
12
  const CONTAINER = [0, 0, 0, 12, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a];
12
13
  export function isJXL(d) {
13
14
  if (d.length >= 2 && d[0] === 0xff && d[1] === 0x0a)
@@ -59,6 +60,28 @@ function codestream(d) {
59
60
  }
60
61
  return out;
61
62
  }
63
+ const WHITE_D65 = [0.3127, 0.3290];
64
+ const PRIMARIES = {
65
+ [PRI_BT2100]: [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]],
66
+ [PRI_P3]: [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]],
67
+ };
68
+ export function colorSpace(image) {
69
+ const c = image.colour;
70
+ if (image.xybEncoded || c.wantICC || (c.colourSpace !== CS_RGB && c.colourSpace !== CS_GREY))
71
+ return;
72
+ if (c.transfer === TF_PQ || c.transfer === TF_HLG)
73
+ return;
74
+ const curve = c.transfer === -1 ? (c.gamma > 0 ? v => v ** (1 / c.gamma) : null)
75
+ : c.transfer === TF_LINEAR ? v => v : c.transfer === TF_DCI ? v => v ** 2.6 : null;
76
+ const white = c.whitePoint === WP_CUSTOM ? c.white : c.whitePoint === WP_E ? [1 / 3, 1 / 3] : c.whitePoint === WP_DCI ? [0.314, 0.351] : null;
77
+ const primaries = c.colourSpace === CS_GREY ? null : c.primaries === PRI_CUSTOM
78
+ ? [0, 2, 4].map(i => c.primariesXY.slice(i, i + 2)) : PRIMARIES[c.primaries] ?? null;
79
+ if (c.colourSpace === CS_GREY)
80
+ return curve ? { xy: null, curve } : undefined;
81
+ if (!curve && !white && !primaries)
82
+ return;
83
+ return { xy: [...primaries ?? [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06]], white ?? WHITE_D65], curve: curve ?? srgbDecode };
84
+ }
62
85
  export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = 1) {
63
86
  const d = codestream(data);
64
87
  const r = new BitReader(d);
@@ -66,6 +89,7 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
66
89
  checkPixels(image.width, image.height, maxPixels);
67
90
  const stream = image.colour.wantICC ? readICC(r) : null;
68
91
  const icc = stream && !image.xybEncoded ? decodeICC(stream) ?? undefined : undefined;
92
+ const space = icc ? undefined : colorSpace(image);
69
93
  r.zeroPadToByte();
70
94
  let at = r.bytePosition;
71
95
  if (image.preview) {
@@ -94,7 +118,7 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
94
118
  shown++;
95
119
  yield {
96
120
  width: image.width, height: image.height, pixels: state.output.toRGBA(frame.canvas), duration: frame.header.duration * tick,
97
- orientation: image.orientation, loopCount: a ? a.numLoops : 1, icc
121
+ orientation: image.orientation, loopCount: a ? a.numLoops : 1, icc, colorSpace: space
98
122
  };
99
123
  }
100
124
  if (frame.header.isLast)
package/dist/image.js CHANGED
@@ -22,7 +22,7 @@ import * as quantize from "./codecs/quantize.js";
22
22
  import * as textRenderer from "./render/text.js";
23
23
  import * as svgRenderer from "./render/svg.js";
24
24
  import { view } from "./util/mem.js";
25
- import { cicpTransform, profileTransform } from "./util/icc.js";
25
+ import { cicpTransform, colorSpaceTransform, profileTransform } from "./util/icc.js";
26
26
  import { ImageType } from "./image-type.js";
27
27
  import { TextLayout } from "./text-layout.js";
28
28
  import { resolveMetadata, pngText, exifData } from "./metadata.js";
@@ -1114,8 +1114,10 @@ export class Image {
1114
1114
  return tga.decode(data, maxPixels);
1115
1115
  throw new Error('Unsupported image type');
1116
1116
  });
1117
- if (applyColorProfile)
1118
- (decoded.icc ? profileTransform(decoded.icc) : decoded.cicp ? cicpTransform(...decoded.cicp) : null)?.(decoded.pixels);
1117
+ if (applyColorProfile) {
1118
+ const { icc, cicp, colorSpace } = decoded;
1119
+ (icc ? profileTransform(icc) : cicp ? cicpTransform(...cicp) : colorSpace ? colorSpaceTransform(colorSpace.xy, colorSpace.curve) : null)?.(decoded.pixels);
1120
+ }
1119
1121
  const image = new Image(decoded.width, decoded.height);
1120
1122
  image.bitmap = decoded.pixels;
1121
1123
  return applyOrientation && decoded.orientation ? image.__orient__(decoded.orientation) : image;
@@ -22,7 +22,7 @@ export interface Metadata {
22
22
  copyright?: string;
23
23
  /** The image's creation time. Default: now */
24
24
  creationTime?: string | number | Date;
25
- /** The software used to create the image. Default: `'github.com/matmen/ImageScript vX.X.X'`, `''` leaves it out */
25
+ /** The software used to create the image. Default: `'ImageScript vX.X.X'`, `''` leaves it out */
26
26
  software?: string;
27
27
  /** A disclaimer for the image */
28
28
  disclaimer?: string;
package/dist/metadata.js CHANGED
@@ -9,7 +9,7 @@ export function resolveMetadata(metadata = {}) {
9
9
  throw new RangeError('Invalid creation time');
10
10
  const resolved = { creationTime: time };
11
11
  for (const key of TEXT_FIELDS) {
12
- const value = key === 'software' && software === undefined ? `github.com/matmen/ImageScript v${VERSION}` : metadata?.[key];
12
+ const value = key === 'software' && software === undefined ? `ImageScript v${VERSION}` : metadata?.[key];
13
13
  if (value)
14
14
  resolved[key] = String(value);
15
15
  }
package/dist/util/icc.js CHANGED
@@ -129,6 +129,15 @@ export function cicpTransform(primaries, transfer) {
129
129
  const xy = CICP_PRIMARIES[primaries];
130
130
  if (!xy || transfer === 16 || transfer === 18)
131
131
  return null;
132
+ const curve = transfer === 4 ? v => v ** 2.2 : transfer === 5 ? v => v ** 2.8 : transfer === 8 ? v => v : srgbDecode;
133
+ return colorSpaceTransform(xy, curve);
134
+ }
135
+ export const srgbDecode = v => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
136
+ export function colorSpaceTransform(xy, curve) {
137
+ if (!xy)
138
+ return transform({ gray: true, matrix: SRGB_D50, curves: [curve, curve, curve] });
139
+ if (xy.some(([x, y]) => !(y > 0) || !Number.isFinite(x)))
140
+ return null;
132
141
  const xyz = ([x, y]) => [x / y, 1, (1 - x - y) / y];
133
142
  const p = [0, 1, 2].map(i => xyz(xy[i])), white = xyz(xy[3]);
134
143
  const columns = [p[0][0], p[1][0], p[2][0], p[0][1], p[1][1], p[2][1], p[0][2], p[1][2], p[2][2]];
@@ -140,8 +149,6 @@ export function cicpTransform(primaries, transfer) {
140
149
  const cone = (v) => [0, 1, 2].map(r => BRADFORD[r * 3] * v[0] + BRADFORD[r * 3 + 1] * v[1] + BRADFORD[r * 3 + 2] * v[2]);
141
150
  const from = cone(white), to = cone(D50);
142
151
  const adapt = multiply3(invert3(BRADFORD), multiply3([to[0] / from[0], 0, 0, 0, to[1] / from[1], 0, 0, 0, to[2] / from[2]], BRADFORD));
143
- const curve = transfer === 4 ? v => v ** 2.2 : transfer === 5 ? v => v ** 2.8 : transfer === 8 ? v => v
144
- : v => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
145
152
  return transform({ gray: false, matrix: multiply3(adapt, matrix), curves: [curve, curve, curve] });
146
153
  }
147
154
  function transform(p) {
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.13.0';
1
+ export const VERSION = '3.13.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.13.0",
3
+ "version": "3.13.2",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "repository": {
35
35
  "type": "git",
36
- "url": "git+https://github.com/matmen/ImageScript.git"
36
+ "url": "git+https://github.com/imagescript/ImageScript.git"
37
37
  },
38
38
  "keywords": [
39
39
  "image",
@@ -85,7 +85,7 @@
85
85
  "author": "Mathis Mensing <mathis@matmen.dev>",
86
86
  "license": "(AGPL-3.0-or-later OR MIT)",
87
87
  "bugs": {
88
- "url": "https://github.com/matmen/ImageScript/issues"
88
+ "url": "https://github.com/imagescript/ImageScript/issues"
89
89
  },
90
90
  "homepage": "https://imagescript.dev/",
91
91
  "engines": {