imagescript 3.20.0 → 3.20.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/image.js CHANGED
@@ -692,22 +692,42 @@ export class Image {
692
692
  const sourceOS = sourceMap?.offsetSDR ?? [0, 0, 0], sourceOH = sourceMap?.offsetHDR ?? [0, 0, 0];
693
693
  const oS = map.offsetSDR, oH = map.offsetHDR;
694
694
  const src = source.bitmap, Gs = [0, 0, 0];
695
+ let lastGain = NaN, lastPower = 1;
695
696
  gain.redraw(this, map, left, top, right, bottom, (px, py, S, boost, out) => {
696
697
  const s = 4 * ((py - y) * source.width + px - x), b = 4 * ((py - top) * beforeWidth + px - left);
697
698
  const as = src[s + 3] / 255 * opacity, ab = before[b + 3] / 255;
698
699
  const ao = as + ab * (1 - as);
699
- if (sample)
700
- sample(px - x, py - y, Gs);
700
+ if (blend && ab === 1) {
701
+ for (let c = 0; c < 3; c++)
702
+ out[c] = (S[c] + oS[c]) * boost[c] - oH[c];
703
+ return;
704
+ }
705
+ if (ao <= 0) {
706
+ out[0] = out[1] = out[2] = 0;
707
+ return;
708
+ }
709
+ let sampled = false;
701
710
  for (let c = 0; c < 3; c++) {
702
711
  const Sd = gain.LINEAR[before[b + c]], Ss = gain.LINEAR[src[s + c]];
703
712
  const Hd = (Sd + oS[c]) * boost[c] - oH[c];
704
- const Hs = sample ? (Ss + sourceOS[c]) * 2 ** Gs[c] - sourceOH[c] : Ss;
705
- if (blend && ab === 1) {
706
- out[c] = (S[c] + oS[c]) * boost[c] - oH[c];
713
+ const share = Math.abs(Ss - Sd) > 1e-4 ? Math.min(1, Math.max(0, (S[c] - Sd) / (Ss - Sd))) : as / ao;
714
+ if (share === 0) {
715
+ out[c] = Hd;
707
716
  continue;
708
717
  }
709
- const share = Math.abs(Ss - Sd) > 1e-4 ? Math.min(1, Math.max(0, (S[c] - Sd) / (Ss - Sd))) : ao > 0 ? as / ao : 0;
710
- out[c] = ao > 0 ? share * Hs + (1 - share) * Hd : 0;
718
+ let Hs = Ss;
719
+ if (sample) {
720
+ if (!sampled) {
721
+ sample(px - x, py - y, Gs);
722
+ sampled = true;
723
+ }
724
+ if (Gs[c] !== lastGain) {
725
+ lastGain = Gs[c];
726
+ lastPower = 2 ** lastGain;
727
+ }
728
+ Hs = (Ss + sourceOS[c]) * lastPower - sourceOH[c];
729
+ }
730
+ out[c] = share * Hs + (1 - share) * Hd;
711
731
  }
712
732
  }, map.channels === 1 && (!sourceMap || sourceMap.channels === 1), Math.max(0, ...(sourceMap?.gainMapMax ?? [0])));
713
733
  return this;
package/dist/util/gain.js CHANGED
@@ -2,13 +2,14 @@ import { Image } from "../image.js";
2
2
  export const LINEAR = new Float32Array(256).map((_, v) => (v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
3
3
  export const encodeSRGB = (v) => 255 * (v <= 0.0031308 ? 12.92 * Math.max(0, v) : 1.055 * Math.min(1, v) ** (1 / 2.4) - 0.055);
4
4
  const LUMA = [0.2126, 0.7152, 0.0722];
5
+ const gainTables = (map) => [0, 1, 2].map(c => {
6
+ const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
7
+ return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
8
+ });
5
9
  export function decodeGains(map) {
6
10
  const { bitmap } = map.image;
7
11
  const gains = new Float32Array(bitmap.length / 4 * 3);
8
- const tables = [0, 1, 2].map(c => {
9
- const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
10
- return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
11
- });
12
+ const tables = gainTables(map);
12
13
  for (let i = 0, g = 0; i < bitmap.length; i += 4, g += 3) {
13
14
  gains[g] = tables[0][bitmap[i]];
14
15
  gains[g + 1] = tables[1][bitmap[i + 1]];
@@ -78,18 +79,22 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
78
79
  return;
79
80
  const mapWidth = map.width, mapHeight = map.height;
80
81
  const sx = mapWidth / width, sy = mapHeight / height;
81
- const gains = decodeGains(map);
82
82
  const oS = map.offsetSDR, oH = map.offsetHDR;
83
83
  const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(0, ...map.gainMapMax, extraMax);
84
84
  const S = [0, 0, 0], H = [0, 0, 0], sumS = [0, 0, 0], sumH = [0, 0, 0];
85
85
  const i0 = Math.max(0, Math.floor(left * sx)), i1 = Math.min(mapWidth, Math.ceil(right * sx));
86
86
  const j0 = Math.max(0, Math.floor(top * sy)), j1 = Math.min(mapHeight, Math.ceil(bottom * sy));
87
+ if (i1 <= i0 || j1 <= j0)
88
+ return;
89
+ const tables = gainTables(map), stored = map.image.bitmap;
90
+ const regionWidth = i1 - i0;
91
+ const next = new Float32Array(regionWidth * (j1 - j0) * 3);
87
92
  for (let j = j0; j < j1; j++) {
88
93
  const y0 = Math.max(0, Math.floor(j / sy)), y1 = Math.min(height, Math.max(y0 + 1, Math.floor((j + 1) / sy)));
89
94
  for (let i = i0; i < i1; i++) {
90
95
  const x0 = Math.max(0, Math.floor(i / sx)), x1 = Math.min(width, Math.max(x0 + 1, Math.floor((i + 1) / sx)));
91
- const g = 3 * (j * mapWidth + i);
92
- const boost = [2 ** gains[g], 2 ** gains[g + 1], 2 ** gains[g + 2]];
96
+ const m = 4 * (j * mapWidth + i), g = 3 * ((j - j0) * regionWidth + (i - i0));
97
+ const boost = [2 ** tables[0][stored[m]], 2 ** tables[1][stored[m + 1]], 2 ** tables[2][stored[m + 2]]];
93
98
  sumS.fill(0);
94
99
  sumH.fill(0);
95
100
  for (let y = y0; y < y1; y++)
@@ -113,13 +118,35 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
113
118
  s += LUMA[c] * (sumS[c] / n + oS[c]);
114
119
  h += LUMA[c] * (sumH[c] / n + oH[c]);
115
120
  }
116
- gains[g] = gains[g + 1] = gains[g + 2] = ratio(h, s, lo, hi);
121
+ next[g] = next[g + 1] = next[g + 2] = ratio(h, s, lo, hi);
117
122
  }
118
123
  else
119
124
  for (let c = 0; c < 3; c++)
120
- gains[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
125
+ next[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
121
126
  }
122
127
  }
128
+ const { gainMapMin: min, gainMapMax: max, gamma } = map;
129
+ let fits = !single || (min[0] === min[1] && min[1] === min[2] && max[0] === max[1] && max[1] === max[2] && gamma[0] === gamma[1] && gamma[1] === gamma[2]);
130
+ for (let g = 0; fits && g < next.length; g += 3)
131
+ for (let c = 0; c < 3; c++)
132
+ if (!(next[g + c] >= min[c] && next[g + c] <= max[c]))
133
+ fits = false;
134
+ if (fits) {
135
+ for (let j = j0; j < j1; j++)
136
+ for (let i = i0, g = 3 * (j - j0) * regionWidth, m = 4 * (j * mapWidth + i0); i < i1; i++, g += 3, m += 4)
137
+ for (let c = 0; c < 3; c++) {
138
+ const t = max[c] > min[c] ? (next[g + c] - min[c]) / (max[c] - min[c]) : 0;
139
+ stored[m + c] = 255 * Math.min(1, Math.max(0, t)) ** gamma[c];
140
+ }
141
+ return;
142
+ }
143
+ const gains = decodeGains(map);
144
+ for (let j = j0; j < j1; j++)
145
+ for (let i = i0, g = 3 * (j - j0) * regionWidth, m = 3 * (j * mapWidth + i0); i < i1; i++, g += 3, m += 3) {
146
+ gains[m] = next[g];
147
+ gains[m + 1] = next[g + 1];
148
+ gains[m + 2] = next[g + 2];
149
+ }
123
150
  encodeGains(map, gains, mapWidth, mapHeight, single);
124
151
  }
125
152
  const ratio = (h, s, lo, hi) => {
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.20.0';
1
+ export const VERSION = '3.20.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.20.0",
3
+ "version": "3.20.1",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",