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.
@@ -13,11 +13,15 @@ export function oneOf(value, allowed, name) {
13
13
  }
14
14
  return value;
15
15
  }
16
+ export function checkNumber(value, name) {
17
+ if (typeof value !== 'number')
18
+ throw new TypeError(`${name} must be a number, not ${value === null ? 'null' : typeof value}`);
19
+ }
16
20
  export function finite(value, name) {
17
- const number = Number(value);
18
- if (!Number.isFinite(number))
21
+ checkNumber(value, name);
22
+ if (!Number.isFinite(value))
19
23
  throw new RangeError(`Invalid ${name}: ${value}`);
20
- return number;
24
+ return value;
21
25
  }
22
26
  export function loopCount(value, name = 'loop count') {
23
27
  if (typeof value !== 'number')
@@ -43,6 +47,24 @@ export function fitDuration(duration, format) {
43
47
  throw new RangeError(`refuse frame durations that don't fit the format: ${format} frames last up to ${max} ms, not ${duration}`);
44
48
  return duration;
45
49
  }
50
+ export function nonNegative(value, name) {
51
+ finite(value, name);
52
+ if (value < 0)
53
+ throw new RangeError(`${name} must be 0 or more, not ${value}`);
54
+ return value;
55
+ }
56
+ export function unitInterval(value, name) {
57
+ finite(value, name);
58
+ if (value < 0 || value > 1)
59
+ throw new RangeError(`${name} must be 0-1, not ${value}`);
60
+ return value;
61
+ }
62
+ export function positive(value, name) {
63
+ finite(value, name);
64
+ if (!(value > 0))
65
+ throw new RangeError(`${name} must be above 0, not ${value}`);
66
+ return value;
67
+ }
46
68
  export function quality(value, name = 'quality') {
47
69
  const q = finite(value, name);
48
70
  if (q < 0 || q > 100)
@@ -50,9 +72,10 @@ export function quality(value, name = 'quality') {
50
72
  return q;
51
73
  }
52
74
  export function pixelLimit(value, name = 'maxPixels') {
53
- if (!(Number(value) > 0))
75
+ checkNumber(value, name);
76
+ if (!(value > 0))
54
77
  throw new RangeError(`${name} must be a positive number, not ${value}`);
55
- return Number(value);
78
+ return value;
56
79
  }
57
80
  export function gainMapScale(value) {
58
81
  const scale = finite(value, 'gain map scale');
@@ -61,15 +84,18 @@ export function gainMapScale(value) {
61
84
  return scale;
62
85
  }
63
86
  export function checkThreads(threads) {
87
+ checkNumber(threads, 'threads');
64
88
  if (!Number.isInteger(threads) || threads < 1)
65
89
  throw new RangeError(`threads must be a positive integer, not ${threads}`);
66
90
  }
67
91
  export function effort(value, max) {
92
+ checkNumber(value, 'effort');
68
93
  if (!Number.isInteger(value) || value < 0 || value > max)
69
94
  throw new RangeError(`effort must be an integer 0-${max}, not ${value}`);
70
95
  return value;
71
96
  }
72
97
  export function compressionLevel(value) {
98
+ checkNumber(value, 'compression level');
73
99
  if (!Number.isInteger(value) || value < 0 || value > 9)
74
100
  throw new RangeError(`compression level must be an integer 0-9, not ${value}`);
75
101
  return value;
@@ -0,0 +1,43 @@
1
+ import { littleEndian, isOpaque, premultiply8, unpremultiply8 } from "./alpha.js";
2
+ function span(start, step, limit, from, to) {
3
+ if (step === 0)
4
+ return start >= 0 && start < limit ? [from, to] : [from, from];
5
+ const a = -start / step, b = (limit - start) / step;
6
+ const lo = Math.floor(Math.min(a, b)) - 1, hi = Math.ceil(Math.max(a, b)) + 1;
7
+ return [Math.max(from, lo), Math.max(Math.max(from, lo), Math.min(to, hi))];
8
+ }
9
+ export function rotateBilinear(src, width, height, out, outWidth, outHeight, sin, cos) {
10
+ if (!littleEndian)
11
+ return false;
12
+ let bitmap = src;
13
+ if (!isOpaque(src))
14
+ premultiply8(src, bitmap = new Uint8ClampedArray(src.length));
15
+ const pixels = new Int32Array(bitmap.buffer, bitmap.byteOffset, width * height);
16
+ const stride = width + 2;
17
+ const padded = new Int32Array(stride * (height + 2));
18
+ for (let y = 0; y < height; y++)
19
+ padded.set(pixels.subarray(y * width, (y + 1) * width), (y + 1) * stride + 1);
20
+ const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
21
+ const x0 = 0.5 - outWidth / 2, cx = width / 2 + 0.5, cy = height / 2 + 0.5;
22
+ for (let oy = 0; oy < outHeight; oy++) {
23
+ const dy = oy + 0.5 - outHeight / 2;
24
+ const sx0 = x0 * cos - dy * sin + cx, sy0 = x0 * sin + dy * cos + cy;
25
+ const [xFrom, xTo] = span(sx0, cos, width + 1, 0, outWidth);
26
+ const [from, to] = span(sy0, sin, height + 1, xFrom, xTo);
27
+ let o = oy * outWidth + from, sx = sx0 + from * cos, sy = sy0 + from * sin;
28
+ for (let ox = from; ox < to; ox++, o++, sx += cos, sy += sin) {
29
+ if (!(sx >= 0 && sy >= 0 && sx < width + 1 && sy < height + 1))
30
+ continue;
31
+ const ix = sx | 0, iy = sy | 0;
32
+ const fx = (sx - ix) * 256 + 0.5 | 0, fy = (sy - iy) * 256 + 0.5 | 0;
33
+ const w11 = fx * fy + 128 >> 8, w10 = fx - w11, w01 = fy - w11, w00 = 256 - fx - fy + w11;
34
+ const i = iy * stride + ix;
35
+ const p00 = padded[i], p10 = padded[i + 1], p01 = padded[i + stride], p11 = padded[i + stride + 1];
36
+ const rb = Math.imul(p00 & 0xff00ff, w00) + Math.imul(p10 & 0xff00ff, w10) + Math.imul(p01 & 0xff00ff, w01) + Math.imul(p11 & 0xff00ff, w11) + 0x800080;
37
+ const ga = Math.imul(p00 >>> 8 & 0xff00ff, w00) + Math.imul(p10 >>> 8 & 0xff00ff, w10) + Math.imul(p01 >>> 8 & 0xff00ff, w01) + Math.imul(p11 >>> 8 & 0xff00ff, w11) + 0x800080;
38
+ result[o] = (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
39
+ }
40
+ }
41
+ unpremultiply8(out);
42
+ return true;
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "4.0.0-rc.3",
3
+ "version": "4.0.0-rc.4",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -77,7 +77,6 @@
77
77
  "pbm",
78
78
  "pam",
79
79
  "pfm",
80
- "hdr",
81
80
  "radiance",
82
81
  "animation",
83
82
  "text rendering",