@photivra/engine 2.8.0 → 2.9.0

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +92 -5
  2. package/CITATION.cff +1 -1
  3. package/README.md +8 -6
  4. package/SECURITY.md +1 -1
  5. package/dist/capture/export-jpeg.d.ts +19 -0
  6. package/dist/capture/export-jpeg.js +190 -95
  7. package/dist/capture/export-jpeg.js.map +1 -1
  8. package/dist/capture/native-environment-lean.js +19 -9
  9. package/dist/capture/native-environment-lean.js.map +1 -1
  10. package/dist/core/version.d.ts +1 -1
  11. package/dist/core/version.js +1 -1
  12. package/dist/core/version.js.map +1 -1
  13. package/dist/facade/diffraction-provider.js +3 -10
  14. package/dist/facade/diffraction-provider.js.map +1 -1
  15. package/dist/facade/expected-signal-setup.d.ts +1 -1
  16. package/dist/facade/expected-signal-setup.js +2 -6
  17. package/dist/facade/expected-signal-setup.js.map +1 -1
  18. package/dist/facade/expected-signal.d.ts +4 -1
  19. package/dist/facade/expected-signal.js +151 -31
  20. package/dist/facade/expected-signal.js.map +1 -1
  21. package/dist/facade/scene-radiance.d.ts +4 -1
  22. package/dist/facade/scene-radiance.js +4 -4
  23. package/dist/facade/scene-radiance.js.map +1 -1
  24. package/dist/facade/simulate-capture.d.ts +26 -1
  25. package/dist/facade/simulate-capture.js +151 -26
  26. package/dist/facade/simulate-capture.js.map +1 -1
  27. package/dist/facade/simulate-display-view.js +2 -1
  28. package/dist/facade/simulate-display-view.js.map +1 -1
  29. package/dist/index.d.ts +4 -3
  30. package/dist/index.js +2 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/optics/diffraction-psf-profile.d.ts +19 -17
  33. package/dist/optics/diffraction-psf-profile.js +49 -18
  34. package/dist/optics/diffraction-psf-profile.js.map +1 -1
  35. package/dist/optics/site-diffraction-kernel.d.ts +5 -0
  36. package/dist/optics/site-diffraction-kernel.js +32 -0
  37. package/dist/optics/site-diffraction-kernel.js.map +1 -0
  38. package/dist/output/print-jpeg.d.ts +28 -1
  39. package/dist/output/print-jpeg.js +148 -48
  40. package/dist/output/print-jpeg.js.map +1 -1
  41. package/dist/sensor/stochastic-seed.d.ts +12 -0
  42. package/dist/sensor/stochastic-seed.js +25 -0
  43. package/dist/sensor/stochastic-seed.js.map +1 -1
  44. package/docs/API_REFERENCE.md +8 -1
  45. package/docs/QUICK_START.md +59 -0
  46. package/docs/api/capture-export-jpeg.md +10 -0
  47. package/docs/api/core-version.md +1 -1
  48. package/docs/api/exports.json +37 -2
  49. package/docs/api/facade-scene-radiance.md +2 -1
  50. package/docs/api/facade-simulate-capture.md +28 -0
  51. package/docs/api/optics-diffraction-psf-profile.md +1 -1
  52. package/docs/api/output-print-jpeg.md +41 -0
  53. package/package.json +1 -1
@@ -3,7 +3,7 @@
3
3
  * Print JPEG export: downsamples an SDR image by exact area averaging, then writes a baseline JPEG
4
4
  * with an ICC colour profile. Bounded in size; the encoder is written for this project.
5
5
  */
6
- import { createExportJpegEncoder, EXPORT_JPEG_ENCODER_ID } from "../capture/export-jpeg.js";
6
+ import { createExportJpegEncoder, encodeJpegStrip, EXPORT_JPEG_ENCODER_ID } from "../capture/export-jpeg.js";
7
7
  import { packExportJpegExif, exportTiffNumbers, exportTiffAscii, exportTiffRationals } from "../capture/export-tiff.js";
8
8
  import { createSrgbIccProfile, SRGB_ICC_PROFILE_VERSION } from "../color/srgb-icc.js";
9
9
  import { decodeSrgbComponent, encodeSrgbComponent } from "../color/srgb-transfer.js";
@@ -68,6 +68,82 @@ export function parsePrintJpegInput(input) {
68
68
  return freezeOwnedData({ source, crop, outputWidth, outputHeight, quantizationStep, maximumEncodedBytes, maximumProviderReads, ...(ppi === undefined ? {} : { pixelsPerInch: ppi }),
69
69
  ...(r.maximumPixels === undefined ? {} : { maximumPixels }) });
70
70
  }
71
+ /** The JPEG writer of a Print plan: its size, quantization, budget, ICC profile, EXIF, XMP plan record and density. */
72
+ function printEncoder(plan) {
73
+ const exif = printExif(plan);
74
+ const escaped = JSON.stringify(plan).replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
75
+ const xmp = new TextEncoder().encode('<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:photivra="https://photivra.com/ns/simulation/1.0/" photivra:PrintExportPlan="' + escaped + '" photivra:SourceBytesVerified="False"/></rdf:RDF></x:xmpmeta>');
76
+ return createExportJpegEncoder({ width: plan.outputWidth, height: plan.outputHeight, quantizationStep: plan.quantizationStep, maximumOutputBytes: plan.maximumEncodedBytes, icc: createSrgbIccProfile(), exif, xmp,
77
+ ...(plan.pixelsPerInch === undefined ? {} : { densityDotsPerInch: Math.round(plan.pixelsPerInch) }) });
78
+ }
79
+ /** At 1:1, the output code of each source code (its decoded value re-encoded and rounded). */
80
+ function directCodes() {
81
+ return Uint8Array.from(srgbDecodeTable(), (value) => Math.min(255, Math.max(0, Math.round(encodeSrgbComponent(value) * 255))));
82
+ }
83
+ function requireIdentity(plan) {
84
+ if (plan.outputWidth !== plan.crop.width || plan.outputHeight !== plan.crop.height)
85
+ throw new InvalidConfigurationError("Print JPEG strips require a 1:1 plan (output size equal to the crop).");
86
+ }
87
+ /**
88
+ * Codes rows of a 1:1 Print JPEG (output size equal to the crop) apart from the rows before them, so a large file's strips
89
+ * can be written on separate workers. `strip.y` is a multiple of 8 and `strip.height` a multiple of 8 unless the strip ends at
90
+ * the image's bottom; `strip.samples` are those crop rows' 8-bit sRGB RGB, crop-wide. `composePrintJpegStrips` joins strips that
91
+ * cover the image, in order, into the bytes `createPrintJpegTask` writes for the same plan and source pixels.
92
+ */
93
+ export function createPrintJpegStrip(input, strip) {
94
+ const plan = parsePrintJpegInput(input);
95
+ requireIdentity(plan);
96
+ const r = requireAllowlistedRecord(strip, ["y", "height", "samples"], "Invalid Print JPEG strip.");
97
+ const y = integer(r.y, 0), height = integer(r.height), width = plan.outputWidth, samples = r.samples;
98
+ if (y % 8 !== 0 || y + height > plan.outputHeight || (height % 8 !== 0 && y + height !== plan.outputHeight))
99
+ throw new InvalidConfigurationError("Print JPEG strips start on a block row and cover whole block rows.");
100
+ if (!(samples instanceof Uint8Array) || samples.length !== width * height * 3)
101
+ throw new InvalidConfigurationError("Print JPEG strip requires the crop rows' RGB samples.");
102
+ const direct = directCodes(), blocksX = Math.ceil(width / 8), rowBytes = width * 3;
103
+ // The same block samples as createPrintJpegTask's 1:1 path: each block's edge pixels repeated into a partial block.
104
+ const bits = encodeJpegStrip(plan.quantizationStep, blocksX * Math.ceil(height / 8), (index, out) => {
105
+ const bx = (index % blocksX) * 8, by = Math.floor(index / blocksX) * 8, bw = Math.min(8, width - bx), bh = Math.min(8, plan.outputHeight - y - by);
106
+ for (let yy = 0, o = 0; yy < 8; yy++) {
107
+ const row = (by + Math.min(yy, bh - 1)) * rowBytes + bx * 3;
108
+ for (let x = 0; x < 8; x++, o += 3) {
109
+ const i = row + Math.min(x, bw - 1) * 3;
110
+ out[o] = direct[samples[i]];
111
+ out[o + 1] = direct[samples[i + 1]];
112
+ out[o + 2] = direct[samples[i + 2]];
113
+ }
114
+ }
115
+ });
116
+ return Object.freeze({ y, height, width, quantizationStep: plan.quantizationStep, bits });
117
+ }
118
+ /**
119
+ * Joins the strips of a 1:1 Print JPEG (`createPrintJpegStrip`), covering the image top to bottom in order, into its file:
120
+ * the same bytes `createPrintJpegTask` writes for the plan and the same source pixels. No source is read, so the output's
121
+ * `providerReads` is 0.
122
+ */
123
+ export function composePrintJpegStrips(input, strips) {
124
+ const plan = parsePrintJpegInput(input);
125
+ requireIdentity(plan);
126
+ if (!Array.isArray(strips) || strips.length < 1)
127
+ throw new InvalidConfigurationError("Print JPEG assembly requires strips.");
128
+ const encoder = printEncoder(plan);
129
+ try {
130
+ let next = 0;
131
+ for (const strip of strips) {
132
+ if (strip.y !== next || strip.width !== plan.outputWidth || strip.quantizationStep !== plan.quantizationStep)
133
+ throw new InvalidConfigurationError("Print JPEG strips must cover the plan's image in order.");
134
+ encoder.appendStrip(strip.bits);
135
+ next += strip.height;
136
+ }
137
+ if (next !== plan.outputHeight)
138
+ throw new InvalidConfigurationError("Print JPEG strips must cover the plan's image in order.");
139
+ const bytes = encoder.finish();
140
+ return { schemaVersion: PRINT_JPEG_SCHEMA_VERSION, input: plan, mediaType: "image/jpeg", bytes, iccProfileVersion: SRGB_ICC_PROFILE_VERSION, width: plan.outputWidth, height: plan.outputHeight, resampling: "exact-area-average-in-decoded-linear-light", sourceStage: "post-SDR-8-bit-quantization", digitalUpscalingApplied: false, sourceBytesVerified: false, providerReads: 0, encoderId: EXPORT_JPEG_ENCODER_ID };
141
+ }
142
+ catch (error) {
143
+ encoder.dispose();
144
+ throw error;
145
+ }
146
+ }
71
147
  /** Creates the export task. Filtering weights each source pixel by its exact area overlap, after decoding the declared 8-bit SDR source. */
72
148
  export function createPrintJpegTask(input, provider) {
73
149
  const plan = parsePrintJpegInput(input);
@@ -84,11 +160,7 @@ export function createPrintJpegTask(input, provider) {
84
160
  throw new InvalidConfigurationError("Print export is single-use.");
85
161
  state = "running";
86
162
  try {
87
- const exif = printExif(plan);
88
- const escaped = JSON.stringify(plan).replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
89
- const xmp = new TextEncoder().encode('<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:photivra="https://photivra.com/ns/simulation/1.0/" photivra:PrintExportPlan="' + escaped + '" photivra:SourceBytesVerified="False"/></rdf:RDF></x:xmpmeta>');
90
- encoder = createExportJpegEncoder({ width: plan.outputWidth, height: plan.outputHeight, quantizationStep: plan.quantizationStep, maximumOutputBytes: plan.maximumEncodedBytes, icc: createSrgbIccProfile(), exif, xmp,
91
- ...(plan.pixelsPerInch === undefined ? {} : { densityDotsPerInch: Math.round(plan.pixelsPerInch) }) });
163
+ encoder = printEncoder(plan);
92
164
  const scaleX = plan.crop.width / plan.outputWidth, scaleY = plan.crop.height / plan.outputHeight, area = scaleX * scaleY;
93
165
  const cropRight = plan.crop.x + plan.crop.width, cropBottom = plan.crop.y + plan.crop.height, rowBytes = plan.crop.width * 3;
94
166
  // Every crop pixel is read exactly once, in tiles of at most 256 x 32 aligned to the crop origin.
@@ -137,56 +209,84 @@ export function createPrintJpegTask(input, provider) {
137
209
  }
138
210
  };
139
211
  const blocksX = Math.ceil(plan.outputWidth / 8), sums = new Float64Array(blocksX * 192);
140
- for (let by = 0; by < plan.outputHeight; by += 8) {
141
- check();
142
- sums.fill(0);
143
- const height = Math.min(8, plan.outputHeight - by), top = plan.crop.y + by * scaleY, bottom = by + height === plan.outputHeight ? cropBottom : plan.crop.y + (by + height) * scaleY;
144
- // Accumulation order per output pixel is unchanged from the per-block reader: 32-row bands from
145
- // floor(top), then 256-column chunks from floor(left), then rows, then columns. Identical IEEE sums.
146
- for (let sy = Math.floor(top); sy < Math.ceil(bottom); sy += 32) {
147
- const bandHeight = Math.min(32, Math.ceil(bottom) - sy);
148
- await ensureRows(sy, bandHeight);
212
+ // At 1:1 every output pixel is one source pixel with weight exactly 1, so its sum is exactly lut[code] and its
213
+ // sample is a function of the code alone: the same bytes from a 256-entry table, without the accumulation.
214
+ const identity = scaleX === 1 && scaleY === 1;
215
+ const direct = identity ? directCodes() : undefined;
216
+ if (direct)
217
+ for (let by = 0; by < plan.outputHeight; by += 8) {
149
218
  check();
150
- for (let bx = 0, block = 0; bx < plan.outputWidth; bx += 8, block++) {
151
- const width = Math.min(8, plan.outputWidth - bx), left = plan.crop.x + bx * scaleX, right = bx + width === plan.outputWidth ? cropRight : plan.crop.x + (bx + width) * scaleX, base = block * 192;
152
- for (let sx = Math.floor(left); sx < Math.ceil(right); sx += 256) {
153
- const chunkWidth = Math.min(256, Math.ceil(right) - sx);
154
- for (let iy = 0; iy < bandHeight; iy++)
155
- for (let ix = 0; ix < chunkWidth; ix++) {
156
- const px = sx + ix, py = sy + iy;
157
- const firstX = Math.max(0, Math.floor((px - plan.crop.x) / scaleX) - bx), lastX = Math.min(width - 1, Math.ceil((px + 1 - plan.crop.x) / scaleX) - bx - 1);
158
- const firstY = Math.max(0, Math.floor((py - plan.crop.y) / scaleY) - by), lastY = Math.min(height - 1, Math.ceil((py + 1 - plan.crop.y) / scaleY) - by - 1);
159
- const index = (py - cacheTop) * rowBytes + (px - plan.crop.x) * 3, r = lut[cache[index]], g = lut[cache[index + 1]], b = lut[cache[index + 2]];
160
- for (let oy = firstY; oy <= lastY; oy++)
161
- for (let ox = firstX; ox <= lastX; ox++) {
162
- const x0 = plan.crop.x + (bx + ox) * scaleX, y0 = plan.crop.y + (by + oy) * scaleY;
163
- const weight = Math.max(0, Math.min(px + 1, x0 + scaleX) - Math.max(px, x0)) * Math.max(0, Math.min(py + 1, y0 + scaleY) - Math.max(py, y0)) / area;
164
- const i = base + (oy * 8 + ox) * 3;
165
- sums[i] = sums[i] + r * weight;
166
- sums[i + 1] = sums[i + 1] + g * weight;
167
- sums[i + 2] = sums[i + 2] + b * weight;
168
- }
169
- }
219
+ const height = Math.min(8, plan.outputHeight - by);
220
+ await ensureRows(plan.crop.y + by, height);
221
+ check();
222
+ for (let bx = 0; bx < plan.outputWidth; bx += 8) {
223
+ const width = Math.min(8, plan.outputWidth - bx), samples = [];
224
+ for (let y = 0; y < 8; y++) {
225
+ const row = (plan.crop.y + by + Math.min(y, height - 1) - cacheTop) * rowBytes + bx * 3;
226
+ for (let x = 0; x < 8; x++) {
227
+ const index = row + Math.min(x, width - 1) * 3;
228
+ samples.push(direct[cache[index]], direct[cache[index + 1]], direct[cache[index + 2]]);
229
+ }
230
+ }
231
+ check();
232
+ encoder.writeRgbBlock(samples);
233
+ if (++blocks % 32 === 0) {
234
+ await yieldControl(abort.signal);
235
+ check();
170
236
  }
171
237
  }
172
238
  }
173
- for (let bx = 0, block = 0; bx < plan.outputWidth; bx += 8, block++) {
174
- const width = Math.min(8, plan.outputWidth - bx), base = block * 192, samples = [];
175
- for (let y = 0; y < 8; y++)
176
- for (let x = 0; x < 8; x++)
177
- for (let c = 0; c < 3; c++) {
178
- const value = sums[base + (Math.min(y, height - 1) * 8 + Math.min(x, width - 1)) * 3 + c];
179
- // Nearest code, ties up. Math.round is used because floor(x+0.5) wrongly rounds 0.49999999999999994 up.
180
- samples.push(Math.min(255, Math.max(0, Math.round(encodeSrgbComponent(value) * 255))));
181
- }
239
+ else
240
+ for (let by = 0; by < plan.outputHeight; by += 8) {
182
241
  check();
183
- encoder.writeRgbBlock(samples);
184
- if (++blocks % 32 === 0) {
185
- await yieldControl(abort.signal);
242
+ sums.fill(0);
243
+ const height = Math.min(8, plan.outputHeight - by), top = plan.crop.y + by * scaleY, bottom = by + height === plan.outputHeight ? cropBottom : plan.crop.y + (by + height) * scaleY;
244
+ // Accumulation order per output pixel is unchanged from the per-block reader: 32-row bands from
245
+ // floor(top), then 256-column chunks from floor(left), then rows, then columns. Identical IEEE sums.
246
+ for (let sy = Math.floor(top); sy < Math.ceil(bottom); sy += 32) {
247
+ const bandHeight = Math.min(32, Math.ceil(bottom) - sy);
248
+ await ensureRows(sy, bandHeight);
186
249
  check();
250
+ for (let bx = 0, block = 0; bx < plan.outputWidth; bx += 8, block++) {
251
+ const width = Math.min(8, plan.outputWidth - bx), left = plan.crop.x + bx * scaleX, right = bx + width === plan.outputWidth ? cropRight : plan.crop.x + (bx + width) * scaleX, base = block * 192;
252
+ for (let sx = Math.floor(left); sx < Math.ceil(right); sx += 256) {
253
+ const chunkWidth = Math.min(256, Math.ceil(right) - sx);
254
+ for (let iy = 0; iy < bandHeight; iy++)
255
+ for (let ix = 0; ix < chunkWidth; ix++) {
256
+ const px = sx + ix, py = sy + iy;
257
+ const firstX = Math.max(0, Math.floor((px - plan.crop.x) / scaleX) - bx), lastX = Math.min(width - 1, Math.ceil((px + 1 - plan.crop.x) / scaleX) - bx - 1);
258
+ const firstY = Math.max(0, Math.floor((py - plan.crop.y) / scaleY) - by), lastY = Math.min(height - 1, Math.ceil((py + 1 - plan.crop.y) / scaleY) - by - 1);
259
+ const index = (py - cacheTop) * rowBytes + (px - plan.crop.x) * 3, r = lut[cache[index]], g = lut[cache[index + 1]], b = lut[cache[index + 2]];
260
+ for (let oy = firstY; oy <= lastY; oy++)
261
+ for (let ox = firstX; ox <= lastX; ox++) {
262
+ const x0 = plan.crop.x + (bx + ox) * scaleX, y0 = plan.crop.y + (by + oy) * scaleY;
263
+ const weight = Math.max(0, Math.min(px + 1, x0 + scaleX) - Math.max(px, x0)) * Math.max(0, Math.min(py + 1, y0 + scaleY) - Math.max(py, y0)) / area;
264
+ const i = base + (oy * 8 + ox) * 3;
265
+ sums[i] = sums[i] + r * weight;
266
+ sums[i + 1] = sums[i + 1] + g * weight;
267
+ sums[i + 2] = sums[i + 2] + b * weight;
268
+ }
269
+ }
270
+ }
271
+ }
272
+ }
273
+ for (let bx = 0, block = 0; bx < plan.outputWidth; bx += 8, block++) {
274
+ const width = Math.min(8, plan.outputWidth - bx), base = block * 192, samples = [];
275
+ for (let y = 0; y < 8; y++)
276
+ for (let x = 0; x < 8; x++)
277
+ for (let c = 0; c < 3; c++) {
278
+ const value = sums[base + (Math.min(y, height - 1) * 8 + Math.min(x, width - 1)) * 3 + c];
279
+ // Nearest code, ties up. Math.round is used because floor(x+0.5) wrongly rounds 0.49999999999999994 up.
280
+ samples.push(Math.min(255, Math.max(0, Math.round(encodeSrgbComponent(value) * 255))));
281
+ }
282
+ check();
283
+ encoder.writeRgbBlock(samples);
284
+ if (++blocks % 32 === 0) {
285
+ await yieldControl(abort.signal);
286
+ check();
287
+ }
187
288
  }
188
289
  }
189
- }
190
290
  await yieldControl(abort.signal);
191
291
  check();
192
292
  const bytes = encoder.finish();
@@ -1 +1 @@
1
- {"version":3,"file":"print-jpeg.js","sourceRoot":"","sources":["../../src/output/print-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C,6BAA6B,EAAC,uBAAuB,EAAC,KAAK,EAAC,mBAAmB,EAAC,KAAK,EAAC,aAAa,EAAC,KAAK,EAAC,SAAS,EAAC,sBAAsB,EAAC,CAAC;gBAAA,KAAK,GAAC,WAAW,CAAC;YACvZ,CAAC;YAAA,OAAM,KAAK,EAAC,CAAC;gBAAA,OAAO,EAAE,CAAC;gBAAA,IAAG,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAC,CAAC;oBAAA,KAAK,GAAC,QAAQ,CAAC;oBAAA,KAAK,CAAC,KAAK,EAAE,CAAC;gBAAA,CAAC;gBAAA,MAAM,KAAK,CAAC;YAAA,CAAC;QAC/F,CAAC,EAAC,MAAM,KAAQ,IAAG,KAAK,KAAG,OAAO,IAAE,KAAK,KAAG,SAAS,IAAE,KAAK,KAAG,WAAW,EAAC,CAAC;YAAA,KAAK,GAAC,WAAW,CAAC;YAAA,KAAK,CAAC,KAAK,EAAE,CAAC;YAAA,OAAO,EAAE,CAAC;QAAA,CAAC,CAAA,CAAC;QACxH,OAAO,KAAQ,IAAG,KAAK,KAAG,aAAa,EAAC,CAAC;YAAA,KAAK,GAAC,UAAU,CAAC;YAAA,KAAK,CAAC,KAAK,EAAE,CAAC;YAAA,OAAO,EAAE,CAAC;QAAA,CAAC,CAAA,CAAC;QACpF,UAAU,KAAmB,IAAG,KAAK,KAAG,WAAW,IAAE,CAAC,MAAM;YAAC,MAAM,IAAI,yBAAyB,CAAC,yBAAyB,CAAC,CAAC,CAAA,MAAM,KAAK,GAAC,MAAM,CAAC,CAAA,MAAM,GAAC,IAAI,CAAC,CAAA,KAAK,GAAC,aAAa,CAAC,CAAA,OAAO,KAAK,CAAC,CAAA,CAAC;KAC9L,CAAC;AACJ,CAAC","sourcesContent":["// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Print JPEG export: downsamples an SDR image by exact area averaging, then writes a baseline JPEG\n * with an ICC colour profile. Bounded in size; the encoder is written for this project.\n */\nimport { createExportJpegEncoder, EXPORT_JPEG_ENCODER_ID } from \"../capture/export-jpeg.js\";\nimport { packExportJpegExif, exportTiffNumbers, exportTiffAscii, exportTiffRationals, type ExportTiffTag } from \"../capture/export-tiff.js\";\nimport { createSrgbIccProfile, SRGB_ICC_PROFILE_VERSION } from \"../color/srgb-icc.js\";\nimport { decodeSrgbComponent, encodeSrgbComponent } from \"../color/srgb-transfer.js\";\nimport { InvalidConfigurationError } from \"../core/configuration-error.js\";\nimport { requireAllowlistedRecord, requirePublicOpaqueId } from \"../core/record-validation.js\";\nimport { freezeOwnedData } from \"../core/owned-data.js\";\nimport { FULL_RESOLUTION_CEILING_PIXELS, resolveRaisedLimit, scaleBudgetCeiling } from \"../core/pixel-limits.js\";\nimport type { NativeRawTaskState } from \"../capture/native-raw.js\";\nimport type { RasterRect } from \"./capture-geometry.js\";\nexport const PRINT_JPEG_SCHEMA_VERSION=\"0.2.0\" as const;\n/**\n * Admission limits. `maximumPixels` is the default source limit; a request may raise it with its own\n * `maximumPixels`, up to `ceilingPixels`. A raised limit raises the `maximumEncodedBytes` ceiling in proportion\n * (256 000 000 bytes per 24 000 000 source pixels).\n */\nexport const PRINT_JPEG_LIMITS=Object.freeze({maximumPixels:24_000_000,ceilingPixels:FULL_RESOLUTION_CEILING_PIXELS,maximumDimension:16_384,\n maximumEncodedBytes:256_000_000,maximumProviderReads:1_000_000});\nlet srgbDecode:Float64Array|undefined;\n/** decodeSrgbComponent(v/255) for every 8-bit code v, computed once with the same function (identical values). */\nfunction srgbDecodeTable():Float64Array{return srgbDecode??=Float64Array.from({length:256},(_,v)=>decodeSrgbComponent(v/255));}\nexport interface PrintJpegSource {\n captureId:string;imageStateId:string;artifactId:string;sha256:string;\n width:number;height:number;\n encoding:\"encoded-srgb-8-rgb\";\n}\nexport interface PrintJpegInput {\n source:PrintJpegSource;\n /** Integer crop in the oriented output source image, not in sensor or CFA (colour filter array) coordinates. */\n crop:RasterRect;\n outputWidth:number;outputHeight:number;\n quantizationStep:number;\n maximumEncodedBytes:number;\n /**\n * Maximum provider `readTile` calls. The task reads each crop pixel exactly once, in tiles of at most\n * 256 x 32 pixels aligned to the crop origin, so it needs ceil(crop.height/32) x ceil(crop.width/256)\n * reads; a smaller budget fails before the first read.\n */\n maximumProviderReads:number;\n /**\n * Optional physical print density in pixels per inch (1..65535) from the caller's print plan. When present,\n * EXIF IFD0 records XResolution = YResolution = this value as a RATIONAL with ResolutionUnit = 2 (inches),\n * and JFIF APP0 records units = 1 with the nearest integer density (JFIF density fields are uint16).\n * When omitted no physical size is claimed: JFIF keeps units = 0 with a 1:1 aspect ratio. Exif 2.32 makes\n * the IFD0 resolution tags mandatory and says an unknown resolution \"shall\" be recorded as 72 dpi, so IFD0\n * carries 72/1 with ResolutionUnit = 2; that is the standard's unknown-resolution marker, not a print size.\n */\n pixelsPerInch?:number;\n /**\n * Optional opt-in source pixel limit for large images: an integer from 1 to `PRINT_JPEG_LIMITS.ceilingPixels`.\n * Default `PRINT_JPEG_LIMITS.maximumPixels` (24 000 000). When given it is recorded in the XMP export plan, like the\n * other admission budgets; the image data are unchanged.\n */\n maximumPixels?:number;\n}\nexport interface PrintJpegTileRequest {source:PrintJpegSource;x:number;y:number;width:number;height:number}\nexport interface PrintJpegTile extends PrintJpegTileRequest {samples:Uint8Array}\nexport interface PrintJpegProvider {\n readTile(request:Readonly<PrintJpegTileRequest>,signal:AbortSignal):Promise<PrintJpegTile>;\n yieldControl(signal:AbortSignal):Promise<void>;\n}\nexport interface PrintJpegOutput {\n schemaVersion:typeof PRINT_JPEG_SCHEMA_VERSION;\n input:PrintJpegInput;\n mediaType:\"image/jpeg\";\n bytes:Uint8Array;\n iccProfileVersion:typeof SRGB_ICC_PROFILE_VERSION;\n width:number;height:number;\n resampling:\"exact-area-average-in-decoded-linear-light\";\n sourceStage:\"post-SDR-8-bit-quantization\";\n digitalUpscalingApplied:false;\n sourceBytesVerified:false;\n /** Provider calls made: ceil(crop.height/32) x ceil(crop.width/256) tiles, each crop pixel read once. */\n providerReads:number;\n /** Byte behaviour of the baseline encoder that produced `bytes`. */\n encoderId:typeof EXPORT_JPEG_ENCODER_ID;\n}\nexport interface PrintJpegTask {\n readonly state:NativeRawTaskState;\n run():Promise<void>;cancel():void;dispose():void;takeOutput():PrintJpegOutput;\n}\n/** Exactly n/1 for a whole-number density; otherwise the shared bounded decimal-fraction approximation. */\nfunction resolutionRational(id:number,ppi:number):ExportTiffTag {\n if(!Number.isSafeInteger(ppi))return exportTiffRationals(id,false,[ppi]);\n const data=new Uint8Array(8),view=new DataView(data.buffer);view.setUint32(0,ppi,true);view.setUint32(4,1,true);\n return {id,type:5,count:1,data};\n}\n/**\n * Exif 2.32 APP1 for a compressed primary image with every mandatory tag (CIPA DC-008-2019 Tables 17-18):\n * IFD0 Orientation, X/YResolution + ResolutionUnit, Software, YCbCrPositioning; Exif IFD ExifVersion \"0232\",\n * ComponentsConfiguration, FlashpixVersion \"0100\", ColorSpace sRGB, PixelX/YDimension. Without a planned\n * density the resolution tags carry the standard's \"unknown resolution\" value of 72 dpi.\n */\nfunction printExif(plan:PrintJpegInput):Uint8Array {\n const ppi=plan.pixelsPerInch;\n const resolution=ppi===undefined ? [] : [resolutionRational(282,ppi),resolutionRational(283,ppi),exportTiffNumbers(296,3,[2])];\n return packExportJpegExif([exportTiffNumbers(274,3,[1]),...resolution,exportTiffAscii(305,\"Photivra Print JPEG\")],\n [exportTiffNumbers(40961,3,[1])],{pixelWidth:plan.outputWidth,pixelHeight:plan.outputHeight});\n}\nfunction integer(v:unknown,min=1):number {\n if(typeof v!==\"number\"||!Number.isSafeInteger(v)||v<min)throw new InvalidConfigurationError(\"Print raster/budgets require safe integers.\");return v;\n}\n/** Validates the input only. Source detail, perception and printer suitability are checked by separate APIs. */\nexport function parsePrintJpegInput(input:PrintJpegInput):PrintJpegInput {\n const r=requireAllowlistedRecord(input,[\"source\",\"crop\",\"outputWidth\",\"outputHeight\",\"quantizationStep\",\"maximumEncodedBytes\",\"maximumProviderReads\",\"pixelsPerInch\",\"maximumPixels\"],\"Invalid Print JPEG input.\");\n const s=requireAllowlistedRecord(r.source,[\"captureId\",\"imageStateId\",\"artifactId\",\"sha256\",\"width\",\"height\",\"encoding\"],\"Invalid Print source.\");\n if(typeof s.sha256!==\"string\"||!/^[a-f0-9]{64}$/.test(s.sha256)||s.encoding!==\"encoded-srgb-8-rgb\")throw new InvalidConfigurationError(\"Print requires identified encoded sRGB source bytes.\");\n const source:PrintJpegSource={captureId:requirePublicOpaqueId(s.captureId,\"Invalid capture ID.\"),imageStateId:requirePublicOpaqueId(s.imageStateId,\"Invalid image state ID.\"),artifactId:requirePublicOpaqueId(s.artifactId,\"Invalid artifact ID.\"),sha256:s.sha256,width:integer(s.width),height:integer(s.height),encoding:s.encoding};\n const c=requireAllowlistedRecord(r.crop,[\"x\",\"y\",\"width\",\"height\"],\"Invalid Print crop.\"),crop={x:integer(c.x,0),y:integer(c.y,0),width:integer(c.width),height:integer(c.height)};\n const outputWidth=integer(r.outputWidth),outputHeight=integer(r.outputHeight),quantizationStep=integer(r.quantizationStep),maximumEncodedBytes=integer(r.maximumEncodedBytes),maximumProviderReads=integer(r.maximumProviderReads);\n const L=PRINT_JPEG_LIMITS,maximumPixels=resolveRaisedLimit(r.maximumPixels,L.maximumPixels,L.ceilingPixels,\"Print maximumPixels must be an integer from 1 to \"+L.ceilingPixels+\".\");\n const encodedBytesCeiling=r.maximumPixels===undefined?L.maximumEncodedBytes:scaleBudgetCeiling(L.maximumEncodedBytes,maximumPixels);\n if(source.width>L.maximumDimension||source.height>L.maximumDimension||source.width*source.height>maximumPixels||crop.x+crop.width>source.width||crop.y+crop.height>source.height||\n outputWidth>crop.width||outputHeight>crop.height||quantizationStep>255||maximumEncodedBytes>encodedBytesCeiling||maximumProviderReads>L.maximumProviderReads||\n Math.abs(outputWidth*crop.height-outputHeight*crop.width)>.5*(crop.width+crop.height))throw new InvalidConfigurationError(\"Print requires bounded native/downsampled aspect-preserving crop output.\");\n const ppi=r.pixelsPerInch;\n if(ppi!==undefined&&(typeof ppi!==\"number\"||!Number.isFinite(ppi)||ppi<1||ppi>65535))throw new InvalidConfigurationError(\"Print density requires finite pixels per inch from 1 to 65535.\");\n return freezeOwnedData({source,crop,outputWidth,outputHeight,quantizationStep,maximumEncodedBytes,maximumProviderReads,...(ppi===undefined ? {} : {pixelsPerInch:ppi}),\n ...(r.maximumPixels===undefined ? {} : {maximumPixels})});\n}\n/** Creates the export task. Filtering weights each source pixel by its exact area overlap, after decoding the declared 8-bit SDR source. */\nexport function createPrintJpegTask(input:PrintJpegInput,provider:PrintJpegProvider):PrintJpegTask {\n const plan=parsePrintJpegInput(input);\n if(!provider||typeof provider.readTile!==\"function\"||typeof provider.yieldControl!==\"function\")throw new InvalidConfigurationError(\"Print export requires source reader and host yields.\");\n const read=provider.readTile.bind(provider),yieldControl=provider.yieldControl.bind(provider),abort=new AbortController();\n let state:NativeRawTaskState=\"ready\",output:PrintJpegOutput|null=null,encoder:ReturnType<typeof createExportJpegEncoder>|undefined;\n function check():void{if(abort.signal.aborted)throw new InvalidConfigurationError(\"Print export aborted.\");}\n function release():void{encoder?.dispose();encoder=undefined;output?.bytes.fill(0);output=null;}\n return {get state():NativeRawTaskState{return state;},\n async run():Promise<void>{\n if(state!==\"ready\")throw new InvalidConfigurationError(\"Print export is single-use.\");state=\"running\";\n try{\n const exif=printExif(plan);\n const escaped=JSON.stringify(plan).replaceAll(\"&\",\"&amp;\").replaceAll('\"',\"&quot;\").replaceAll(\"<\",\"&lt;\").replaceAll(\">\",\"&gt;\");\n const xmp=new TextEncoder().encode('<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description rdf:about=\"\" xmlns:photivra=\"https://photivra.com/ns/simulation/1.0/\" photivra:PrintExportPlan=\"'+escaped+'\" photivra:SourceBytesVerified=\"False\"/></rdf:RDF></x:xmpmeta>');\n encoder=createExportJpegEncoder({width:plan.outputWidth,height:plan.outputHeight,quantizationStep:plan.quantizationStep,maximumOutputBytes:plan.maximumEncodedBytes,icc:createSrgbIccProfile(),exif,xmp,\n ...(plan.pixelsPerInch===undefined ? {} : {densityDotsPerInch:Math.round(plan.pixelsPerInch)})});\n const scaleX=plan.crop.width/plan.outputWidth,scaleY=plan.crop.height/plan.outputHeight,area=scaleX*scaleY;\n const cropRight=plan.crop.x+plan.crop.width,cropBottom=plan.crop.y+plan.crop.height,rowBytes=plan.crop.width*3;\n // Every crop pixel is read exactly once, in tiles of at most 256 x 32 aligned to the crop origin.\n const tilesPerBand=Math.ceil(plan.crop.width/256),requiredReads=Math.ceil(plan.crop.height/32)*tilesPerBand;\n if(requiredReads>plan.maximumProviderReads)throw new InvalidConfigurationError(\"Print source exceeds provider-read budget.\");\n // Rolling cache of whole crop-width source rows [cacheTop, cacheBottom): at most 63 rows are retained.\n const cache=new Uint8Array(Math.min(64,plan.crop.height)*rowBytes),lut=srgbDecodeTable();\n let reads=0,blocks=0,cacheTop=plan.crop.y,cacheBottom=plan.crop.y;\n const ensureRows=async(firstRow:number,rowCount:number):Promise<void>=>{\n const keepFrom=Math.min(Math.max(cacheTop,firstRow),cacheBottom);\n if(keepFrom>cacheTop){cache.copyWithin(0,(keepFrom-cacheTop)*rowBytes,(cacheBottom-cacheTop)*rowBytes);cacheTop=keepFrom;}\n while(cacheBottom<firstRow+rowCount){\n const bandHeight=Math.min(32,cropBottom-cacheBottom);\n if((cacheBottom+bandHeight-cacheTop)*rowBytes>cache.length)throw new InvalidConfigurationError(\"Print source traversal is not monotonic.\");\n for(let sx=plan.crop.x;sx<cropRight;sx+=256){\n check();++reads;\n const request=Object.freeze({source:plan.source,x:sx,y:cacheBottom,width:Math.min(256,cropRight-sx),height:bandHeight});\n const supplied=await read(request,abort.signal);check();\n const tile=requireAllowlistedRecord(supplied,[\"source\",\"x\",\"y\",\"width\",\"height\",\"samples\"],\"Invalid Print source tile.\");\n const source=requireAllowlistedRecord(tile.source,[\"captureId\",\"imageStateId\",\"artifactId\",\"sha256\",\"width\",\"height\",\"encoding\"],\"Invalid Print tile source.\");\n for(const key of Object.keys(plan.source) as (keyof PrintJpegSource)[])if(source[key]!==plan.source[key])throw new InvalidConfigurationError(\"Print source identity mismatch.\");\n for(const key of [\"x\",\"y\",\"width\",\"height\"] as const)if(tile[key]!==request[key])throw new InvalidConfigurationError(\"Print source rectangle mismatch.\");\n const samples=tile.samples;if(!(samples instanceof Uint8Array)||!(samples.buffer instanceof ArrayBuffer)||samples.length!==request.width*request.height*3||samples.buffer.byteLength!==samples.byteLength)throw new InvalidConfigurationError(\"Print source requires exact owned bounded RGB tile storage.\");\n for(let iy=0;iy<bandHeight;iy++)cache.set(samples.subarray(iy*request.width*3,(iy+1)*request.width*3),(cacheBottom-cacheTop+iy)*rowBytes+(sx-plan.crop.x)*3);\n // One output block can cover a megapixel of source, so also yield to the host while reading the source.\n if(reads%32===0){await yieldControl(abort.signal);check();}\n }\n cacheBottom+=bandHeight;\n }\n };\n const blocksX=Math.ceil(plan.outputWidth/8),sums=new Float64Array(blocksX*192);\n for(let by=0;by<plan.outputHeight;by+=8){\n check();sums.fill(0);\n const height=Math.min(8,plan.outputHeight-by),top=plan.crop.y+by*scaleY,bottom=by+height===plan.outputHeight?cropBottom:plan.crop.y+(by+height)*scaleY;\n // Accumulation order per output pixel is unchanged from the per-block reader: 32-row bands from\n // floor(top), then 256-column chunks from floor(left), then rows, then columns. Identical IEEE sums.\n for(let sy=Math.floor(top);sy<Math.ceil(bottom);sy+=32){\n const bandHeight=Math.min(32,Math.ceil(bottom)-sy);await ensureRows(sy,bandHeight);check();\n for(let bx=0,block=0;bx<plan.outputWidth;bx+=8,block++){\n const width=Math.min(8,plan.outputWidth-bx),left=plan.crop.x+bx*scaleX,right=bx+width===plan.outputWidth?cropRight:plan.crop.x+(bx+width)*scaleX,base=block*192;\n for(let sx=Math.floor(left);sx<Math.ceil(right);sx+=256){\n const chunkWidth=Math.min(256,Math.ceil(right)-sx);\n for(let iy=0;iy<bandHeight;iy++)for(let ix=0;ix<chunkWidth;ix++){\n const px=sx+ix,py=sy+iy;\n const firstX=Math.max(0,Math.floor((px-plan.crop.x)/scaleX)-bx),lastX=Math.min(width-1,Math.ceil((px+1-plan.crop.x)/scaleX)-bx-1);\n const firstY=Math.max(0,Math.floor((py-plan.crop.y)/scaleY)-by),lastY=Math.min(height-1,Math.ceil((py+1-plan.crop.y)/scaleY)-by-1);\n const index=(py-cacheTop)*rowBytes+(px-plan.crop.x)*3,r=lut[cache[index]!]!,g=lut[cache[index+1]!]!,b=lut[cache[index+2]!]!;\n for(let oy=firstY;oy<=lastY;oy++)for(let ox=firstX;ox<=lastX;ox++){\n const x0=plan.crop.x+(bx+ox)*scaleX,y0=plan.crop.y+(by+oy)*scaleY;\n const weight=Math.max(0,Math.min(px+1,x0+scaleX)-Math.max(px,x0))*Math.max(0,Math.min(py+1,y0+scaleY)-Math.max(py,y0))/area;\n const i=base+(oy*8+ox)*3;sums[i]=sums[i]!+r*weight;sums[i+1]=sums[i+1]!+g*weight;sums[i+2]=sums[i+2]!+b*weight;\n }\n }\n }\n }\n }\n for(let bx=0,block=0;bx<plan.outputWidth;bx+=8,block++){\n const width=Math.min(8,plan.outputWidth-bx),base=block*192,samples:number[]=[];\n for(let y=0;y<8;y++)for(let x=0;x<8;x++)for(let c=0;c<3;c++){\n const value=sums[base+(Math.min(y,height-1)*8+Math.min(x,width-1))*3+c]!;\n // Nearest code, ties up. Math.round is used because floor(x+0.5) wrongly rounds 0.49999999999999994 up.\n samples.push(Math.min(255,Math.max(0,Math.round(encodeSrgbComponent(value)*255))));\n }\n check();encoder.writeRgbBlock(samples);if(++blocks%32===0){await yieldControl(abort.signal);check();}\n }\n }\n await yieldControl(abort.signal);check();const bytes=encoder.finish();encoder=undefined;\n output={schemaVersion:PRINT_JPEG_SCHEMA_VERSION,input:plan,mediaType:\"image/jpeg\",bytes,iccProfileVersion:SRGB_ICC_PROFILE_VERSION,width:plan.outputWidth,height:plan.outputHeight,resampling:\"exact-area-average-in-decoded-linear-light\",sourceStage:\"post-SDR-8-bit-quantization\",digitalUpscalingApplied:false,sourceBytesVerified:false,providerReads:reads,encoderId:EXPORT_JPEG_ENCODER_ID};state=\"completed\";\n }catch(error){release();if(!abort.signal.aborted){state=\"failed\";abort.abort();}throw error;}\n },cancel():void{if(state===\"ready\"||state===\"running\"||state===\"completed\"){state=\"cancelled\";abort.abort();release();}},\n dispose():void{if(state!==\"transferred\"){state=\"disposed\";abort.abort();release();}},\n takeOutput():PrintJpegOutput{if(state!==\"completed\"||!output)throw new InvalidConfigurationError(\"Print JPEG unavailable.\");const value=output;output=null;state=\"transferred\";return value;}\n };\n}\n"]}
1
+ {"version":3,"file":"print-jpeg.js","sourceRoot":"","sources":["../../src/output/print-jpeg.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC;;;GAGG;AACH,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,sBAAsB,EAAsB,MAAM,2BAA2B,CAAC;AACjI,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAsB,MAAM,2BAA2B,CAAC;AAC5I,OAAO,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AACtF,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACrF,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC/F,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,8BAA8B,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAGjH,MAAM,CAAC,MAAM,yBAAyB,GAAC,OAAgB,CAAC;AACxD;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAC,MAAM,CAAC,MAAM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AAW,EAAC,CAAC;YAAA,KAAK,GAAC,WAAW,CAAC;YAAA,KAAK,CAAC,KAAK,EAAE,CAAC;YAAA,OAAO,EAAE,CAAC;QAAA,CAAC,CAAA,CAAC;QACxH,OAAO,KAAQ,IAAG,KAAK,KAAG,aAAa,EAAC,CAAC;YAAA,KAAK,GAAC,UAAU,CAAC;YAAA,KAAK,CAAC,KAAK,EAAE,CAAC;YAAA,OAAO,EAAE,CAAC;QAAA,CAAC,CAAA,CAAC;QACpF,UAAU,KAAmB,IAAG,KAAK,KAAG,WAAW,IAAE,CAAC,MAAM;YAAC,MAAM,IAAI,yBAAyB,CAAC,yBAAyB,CAAC,CAAC,CAAA,MAAM,KAAK,GAAC,MAAM,CAAC,CAAA,MAAM,GAAC,IAAI,CAAC,CAAA,KAAK,GAAC,aAAa,CAAC,CAAA,OAAO,KAAK,CAAC,CAAA,CAAC;KAC9L,CAAC;AACJ,CAAC","sourcesContent":["// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Print JPEG export: downsamples an SDR image by exact area averaging, then writes a baseline JPEG\n * with an ICC colour profile. Bounded in size; the encoder is written for this project.\n */\nimport { createExportJpegEncoder, encodeJpegStrip, EXPORT_JPEG_ENCODER_ID, type JpegStripBits } from \"../capture/export-jpeg.js\";\nimport { packExportJpegExif, exportTiffNumbers, exportTiffAscii, exportTiffRationals, type ExportTiffTag } from \"../capture/export-tiff.js\";\nimport { createSrgbIccProfile, SRGB_ICC_PROFILE_VERSION } from \"../color/srgb-icc.js\";\nimport { decodeSrgbComponent, encodeSrgbComponent } from \"../color/srgb-transfer.js\";\nimport { InvalidConfigurationError } from \"../core/configuration-error.js\";\nimport { requireAllowlistedRecord, requirePublicOpaqueId } from \"../core/record-validation.js\";\nimport { freezeOwnedData } from \"../core/owned-data.js\";\nimport { FULL_RESOLUTION_CEILING_PIXELS, resolveRaisedLimit, scaleBudgetCeiling } from \"../core/pixel-limits.js\";\nimport type { NativeRawTaskState } from \"../capture/native-raw.js\";\nimport type { RasterRect } from \"./capture-geometry.js\";\nexport const PRINT_JPEG_SCHEMA_VERSION=\"0.2.0\" as const;\n/**\n * Admission limits. `maximumPixels` is the default source limit; a request may raise it with its own\n * `maximumPixels`, up to `ceilingPixels`. A raised limit raises the `maximumEncodedBytes` ceiling in proportion\n * (256 000 000 bytes per 24 000 000 source pixels).\n */\nexport const PRINT_JPEG_LIMITS=Object.freeze({maximumPixels:24_000_000,ceilingPixels:FULL_RESOLUTION_CEILING_PIXELS,maximumDimension:16_384,\n maximumEncodedBytes:256_000_000,maximumProviderReads:1_000_000});\nlet srgbDecode:Float64Array|undefined;\n/** decodeSrgbComponent(v/255) for every 8-bit code v, computed once with the same function (identical values). */\nfunction srgbDecodeTable():Float64Array{return srgbDecode??=Float64Array.from({length:256},(_,v)=>decodeSrgbComponent(v/255));}\nexport interface PrintJpegSource {\n captureId:string;imageStateId:string;artifactId:string;sha256:string;\n width:number;height:number;\n encoding:\"encoded-srgb-8-rgb\";\n}\nexport interface PrintJpegInput {\n source:PrintJpegSource;\n /** Integer crop in the oriented output source image, not in sensor or CFA (colour filter array) coordinates. */\n crop:RasterRect;\n outputWidth:number;outputHeight:number;\n quantizationStep:number;\n maximumEncodedBytes:number;\n /**\n * Maximum provider `readTile` calls. The task reads each crop pixel exactly once, in tiles of at most\n * 256 x 32 pixels aligned to the crop origin, so it needs ceil(crop.height/32) x ceil(crop.width/256)\n * reads; a smaller budget fails before the first read.\n */\n maximumProviderReads:number;\n /**\n * Optional physical print density in pixels per inch (1..65535) from the caller's print plan. When present,\n * EXIF IFD0 records XResolution = YResolution = this value as a RATIONAL with ResolutionUnit = 2 (inches),\n * and JFIF APP0 records units = 1 with the nearest integer density (JFIF density fields are uint16).\n * When omitted no physical size is claimed: JFIF keeps units = 0 with a 1:1 aspect ratio. Exif 2.32 makes\n * the IFD0 resolution tags mandatory and says an unknown resolution \"shall\" be recorded as 72 dpi, so IFD0\n * carries 72/1 with ResolutionUnit = 2; that is the standard's unknown-resolution marker, not a print size.\n */\n pixelsPerInch?:number;\n /**\n * Optional opt-in source pixel limit for large images: an integer from 1 to `PRINT_JPEG_LIMITS.ceilingPixels`.\n * Default `PRINT_JPEG_LIMITS.maximumPixels` (24 000 000). When given it is recorded in the XMP export plan, like the\n * other admission budgets; the image data are unchanged.\n */\n maximumPixels?:number;\n}\nexport interface PrintJpegTileRequest {source:PrintJpegSource;x:number;y:number;width:number;height:number}\nexport interface PrintJpegTile extends PrintJpegTileRequest {samples:Uint8Array}\nexport interface PrintJpegProvider {\n readTile(request:Readonly<PrintJpegTileRequest>,signal:AbortSignal):Promise<PrintJpegTile>;\n yieldControl(signal:AbortSignal):Promise<void>;\n}\nexport interface PrintJpegOutput {\n schemaVersion:typeof PRINT_JPEG_SCHEMA_VERSION;\n input:PrintJpegInput;\n mediaType:\"image/jpeg\";\n bytes:Uint8Array;\n iccProfileVersion:typeof SRGB_ICC_PROFILE_VERSION;\n width:number;height:number;\n resampling:\"exact-area-average-in-decoded-linear-light\";\n sourceStage:\"post-SDR-8-bit-quantization\";\n digitalUpscalingApplied:false;\n sourceBytesVerified:false;\n /** Provider calls made: ceil(crop.height/32) x ceil(crop.width/256) tiles, each crop pixel read once. */\n providerReads:number;\n /** Byte behaviour of the baseline encoder that produced `bytes`. */\n encoderId:typeof EXPORT_JPEG_ENCODER_ID;\n}\nexport interface PrintJpegTask {\n readonly state:NativeRawTaskState;\n run():Promise<void>;cancel():void;dispose():void;takeOutput():PrintJpegOutput;\n}\n/** Exactly n/1 for a whole-number density; otherwise the shared bounded decimal-fraction approximation. */\nfunction resolutionRational(id:number,ppi:number):ExportTiffTag {\n if(!Number.isSafeInteger(ppi))return exportTiffRationals(id,false,[ppi]);\n const data=new Uint8Array(8),view=new DataView(data.buffer);view.setUint32(0,ppi,true);view.setUint32(4,1,true);\n return {id,type:5,count:1,data};\n}\n/**\n * Exif 2.32 APP1 for a compressed primary image with every mandatory tag (CIPA DC-008-2019 Tables 17-18):\n * IFD0 Orientation, X/YResolution + ResolutionUnit, Software, YCbCrPositioning; Exif IFD ExifVersion \"0232\",\n * ComponentsConfiguration, FlashpixVersion \"0100\", ColorSpace sRGB, PixelX/YDimension. Without a planned\n * density the resolution tags carry the standard's \"unknown resolution\" value of 72 dpi.\n */\nfunction printExif(plan:PrintJpegInput):Uint8Array {\n const ppi=plan.pixelsPerInch;\n const resolution=ppi===undefined ? [] : [resolutionRational(282,ppi),resolutionRational(283,ppi),exportTiffNumbers(296,3,[2])];\n return packExportJpegExif([exportTiffNumbers(274,3,[1]),...resolution,exportTiffAscii(305,\"Photivra Print JPEG\")],\n [exportTiffNumbers(40961,3,[1])],{pixelWidth:plan.outputWidth,pixelHeight:plan.outputHeight});\n}\nfunction integer(v:unknown,min=1):number {\n if(typeof v!==\"number\"||!Number.isSafeInteger(v)||v<min)throw new InvalidConfigurationError(\"Print raster/budgets require safe integers.\");return v;\n}\n/** Validates the input only. Source detail, perception and printer suitability are checked by separate APIs. */\nexport function parsePrintJpegInput(input:PrintJpegInput):PrintJpegInput {\n const r=requireAllowlistedRecord(input,[\"source\",\"crop\",\"outputWidth\",\"outputHeight\",\"quantizationStep\",\"maximumEncodedBytes\",\"maximumProviderReads\",\"pixelsPerInch\",\"maximumPixels\"],\"Invalid Print JPEG input.\");\n const s=requireAllowlistedRecord(r.source,[\"captureId\",\"imageStateId\",\"artifactId\",\"sha256\",\"width\",\"height\",\"encoding\"],\"Invalid Print source.\");\n if(typeof s.sha256!==\"string\"||!/^[a-f0-9]{64}$/.test(s.sha256)||s.encoding!==\"encoded-srgb-8-rgb\")throw new InvalidConfigurationError(\"Print requires identified encoded sRGB source bytes.\");\n const source:PrintJpegSource={captureId:requirePublicOpaqueId(s.captureId,\"Invalid capture ID.\"),imageStateId:requirePublicOpaqueId(s.imageStateId,\"Invalid image state ID.\"),artifactId:requirePublicOpaqueId(s.artifactId,\"Invalid artifact ID.\"),sha256:s.sha256,width:integer(s.width),height:integer(s.height),encoding:s.encoding};\n const c=requireAllowlistedRecord(r.crop,[\"x\",\"y\",\"width\",\"height\"],\"Invalid Print crop.\"),crop={x:integer(c.x,0),y:integer(c.y,0),width:integer(c.width),height:integer(c.height)};\n const outputWidth=integer(r.outputWidth),outputHeight=integer(r.outputHeight),quantizationStep=integer(r.quantizationStep),maximumEncodedBytes=integer(r.maximumEncodedBytes),maximumProviderReads=integer(r.maximumProviderReads);\n const L=PRINT_JPEG_LIMITS,maximumPixels=resolveRaisedLimit(r.maximumPixels,L.maximumPixels,L.ceilingPixels,\"Print maximumPixels must be an integer from 1 to \"+L.ceilingPixels+\".\");\n const encodedBytesCeiling=r.maximumPixels===undefined?L.maximumEncodedBytes:scaleBudgetCeiling(L.maximumEncodedBytes,maximumPixels);\n if(source.width>L.maximumDimension||source.height>L.maximumDimension||source.width*source.height>maximumPixels||crop.x+crop.width>source.width||crop.y+crop.height>source.height||\n outputWidth>crop.width||outputHeight>crop.height||quantizationStep>255||maximumEncodedBytes>encodedBytesCeiling||maximumProviderReads>L.maximumProviderReads||\n Math.abs(outputWidth*crop.height-outputHeight*crop.width)>.5*(crop.width+crop.height))throw new InvalidConfigurationError(\"Print requires bounded native/downsampled aspect-preserving crop output.\");\n const ppi=r.pixelsPerInch;\n if(ppi!==undefined&&(typeof ppi!==\"number\"||!Number.isFinite(ppi)||ppi<1||ppi>65535))throw new InvalidConfigurationError(\"Print density requires finite pixels per inch from 1 to 65535.\");\n return freezeOwnedData({source,crop,outputWidth,outputHeight,quantizationStep,maximumEncodedBytes,maximumProviderReads,...(ppi===undefined ? {} : {pixelsPerInch:ppi}),\n ...(r.maximumPixels===undefined ? {} : {maximumPixels})});\n}\n/** The JPEG writer of a Print plan: its size, quantization, budget, ICC profile, EXIF, XMP plan record and density. */\nfunction printEncoder(plan:PrintJpegInput):ReturnType<typeof createExportJpegEncoder> {\n const exif=printExif(plan);\n const escaped=JSON.stringify(plan).replaceAll(\"&\",\"&amp;\").replaceAll('\"',\"&quot;\").replaceAll(\"<\",\"&lt;\").replaceAll(\">\",\"&gt;\");\n const xmp=new TextEncoder().encode('<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description rdf:about=\"\" xmlns:photivra=\"https://photivra.com/ns/simulation/1.0/\" photivra:PrintExportPlan=\"'+escaped+'\" photivra:SourceBytesVerified=\"False\"/></rdf:RDF></x:xmpmeta>');\n return createExportJpegEncoder({width:plan.outputWidth,height:plan.outputHeight,quantizationStep:plan.quantizationStep,maximumOutputBytes:plan.maximumEncodedBytes,icc:createSrgbIccProfile(),exif,xmp,\n ...(plan.pixelsPerInch===undefined ? {} : {densityDotsPerInch:Math.round(plan.pixelsPerInch)})});\n}\n/** At 1:1, the output code of each source code (its decoded value re-encoded and rounded). */\nfunction directCodes():Uint8Array {\n return Uint8Array.from(srgbDecodeTable(),(value)=>Math.min(255,Math.max(0,Math.round(encodeSrgbComponent(value)*255))));\n}\n/** Rows of a 1:1 Print JPEG for `createPrintJpegStrip`: crop rows `y` to `y + height` (relative to the crop), crop-wide RGB. */\nexport interface PrintJpegStripInput {y:number;height:number;samples:Uint8Array}\n/** A strip of a 1:1 Print JPEG's coded blocks (`createPrintJpegStrip`), for `composePrintJpegStrips`. */\nexport interface PrintJpegStrip {readonly y:number;readonly height:number;readonly width:number;readonly quantizationStep:number;readonly bits:JpegStripBits}\nfunction requireIdentity(plan:PrintJpegInput):void {\n if(plan.outputWidth!==plan.crop.width||plan.outputHeight!==plan.crop.height)throw new InvalidConfigurationError(\"Print JPEG strips require a 1:1 plan (output size equal to the crop).\");\n}\n/**\n * Codes rows of a 1:1 Print JPEG (output size equal to the crop) apart from the rows before them, so a large file's strips\n * can be written on separate workers. `strip.y` is a multiple of 8 and `strip.height` a multiple of 8 unless the strip ends at\n * the image's bottom; `strip.samples` are those crop rows' 8-bit sRGB RGB, crop-wide. `composePrintJpegStrips` joins strips that\n * cover the image, in order, into the bytes `createPrintJpegTask` writes for the same plan and source pixels.\n */\nexport function createPrintJpegStrip(input:PrintJpegInput,strip:PrintJpegStripInput):PrintJpegStrip {\n const plan=parsePrintJpegInput(input);requireIdentity(plan);\n const r=requireAllowlistedRecord(strip,[\"y\",\"height\",\"samples\"],\"Invalid Print JPEG strip.\");\n const y=integer(r.y,0),height=integer(r.height),width=plan.outputWidth,samples=r.samples;\n if(y%8!==0||y+height>plan.outputHeight||(height%8!==0&&y+height!==plan.outputHeight))throw new InvalidConfigurationError(\"Print JPEG strips start on a block row and cover whole block rows.\");\n if(!(samples instanceof Uint8Array)||samples.length!==width*height*3)throw new InvalidConfigurationError(\"Print JPEG strip requires the crop rows' RGB samples.\");\n const direct=directCodes(),blocksX=Math.ceil(width/8),rowBytes=width*3;\n // The same block samples as createPrintJpegTask's 1:1 path: each block's edge pixels repeated into a partial block.\n const bits=encodeJpegStrip(plan.quantizationStep,blocksX*Math.ceil(height/8),(index,out)=>{\n const bx=(index%blocksX)*8,by=Math.floor(index/blocksX)*8,bw=Math.min(8,width-bx),bh=Math.min(8,plan.outputHeight-y-by);\n for(let yy=0,o=0;yy<8;yy++){\n const row=(by+Math.min(yy,bh-1))*rowBytes+bx*3;\n for(let x=0;x<8;x++,o+=3){const i=row+Math.min(x,bw-1)*3;out[o]=direct[samples[i]!]!;out[o+1]=direct[samples[i+1]!]!;out[o+2]=direct[samples[i+2]!]!;}\n }\n });\n return Object.freeze({y,height,width,quantizationStep:plan.quantizationStep,bits});\n}\n/**\n * Joins the strips of a 1:1 Print JPEG (`createPrintJpegStrip`), covering the image top to bottom in order, into its file:\n * the same bytes `createPrintJpegTask` writes for the plan and the same source pixels. No source is read, so the output's\n * `providerReads` is 0.\n */\nexport function composePrintJpegStrips(input:PrintJpegInput,strips:readonly PrintJpegStrip[]):PrintJpegOutput {\n const plan=parsePrintJpegInput(input);requireIdentity(plan);\n if(!Array.isArray(strips)||strips.length<1)throw new InvalidConfigurationError(\"Print JPEG assembly requires strips.\");\n const encoder=printEncoder(plan);\n try{\n let next=0;\n for(const strip of strips){\n if(strip.y!==next||strip.width!==plan.outputWidth||strip.quantizationStep!==plan.quantizationStep)throw new InvalidConfigurationError(\"Print JPEG strips must cover the plan's image in order.\");\n encoder.appendStrip(strip.bits);next+=strip.height;\n }\n if(next!==plan.outputHeight)throw new InvalidConfigurationError(\"Print JPEG strips must cover the plan's image in order.\");\n const bytes=encoder.finish();\n return {schemaVersion:PRINT_JPEG_SCHEMA_VERSION,input:plan,mediaType:\"image/jpeg\",bytes,iccProfileVersion:SRGB_ICC_PROFILE_VERSION,width:plan.outputWidth,height:plan.outputHeight,resampling:\"exact-area-average-in-decoded-linear-light\",sourceStage:\"post-SDR-8-bit-quantization\",digitalUpscalingApplied:false,sourceBytesVerified:false,providerReads:0,encoderId:EXPORT_JPEG_ENCODER_ID};\n }catch(error){encoder.dispose();throw error;}\n}\n/** Creates the export task. Filtering weights each source pixel by its exact area overlap, after decoding the declared 8-bit SDR source. */\nexport function createPrintJpegTask(input:PrintJpegInput,provider:PrintJpegProvider):PrintJpegTask {\n const plan=parsePrintJpegInput(input);\n if(!provider||typeof provider.readTile!==\"function\"||typeof provider.yieldControl!==\"function\")throw new InvalidConfigurationError(\"Print export requires source reader and host yields.\");\n const read=provider.readTile.bind(provider),yieldControl=provider.yieldControl.bind(provider),abort=new AbortController();\n let state:NativeRawTaskState=\"ready\",output:PrintJpegOutput|null=null,encoder:ReturnType<typeof createExportJpegEncoder>|undefined;\n function check():void{if(abort.signal.aborted)throw new InvalidConfigurationError(\"Print export aborted.\");}\n function release():void{encoder?.dispose();encoder=undefined;output?.bytes.fill(0);output=null;}\n return {get state():NativeRawTaskState{return state;},\n async run():Promise<void>{\n if(state!==\"ready\")throw new InvalidConfigurationError(\"Print export is single-use.\");state=\"running\";\n try{\n encoder=printEncoder(plan);\n const scaleX=plan.crop.width/plan.outputWidth,scaleY=plan.crop.height/plan.outputHeight,area=scaleX*scaleY;\n const cropRight=plan.crop.x+plan.crop.width,cropBottom=plan.crop.y+plan.crop.height,rowBytes=plan.crop.width*3;\n // Every crop pixel is read exactly once, in tiles of at most 256 x 32 aligned to the crop origin.\n const tilesPerBand=Math.ceil(plan.crop.width/256),requiredReads=Math.ceil(plan.crop.height/32)*tilesPerBand;\n if(requiredReads>plan.maximumProviderReads)throw new InvalidConfigurationError(\"Print source exceeds provider-read budget.\");\n // Rolling cache of whole crop-width source rows [cacheTop, cacheBottom): at most 63 rows are retained.\n const cache=new Uint8Array(Math.min(64,plan.crop.height)*rowBytes),lut=srgbDecodeTable();\n let reads=0,blocks=0,cacheTop=plan.crop.y,cacheBottom=plan.crop.y;\n const ensureRows=async(firstRow:number,rowCount:number):Promise<void>=>{\n const keepFrom=Math.min(Math.max(cacheTop,firstRow),cacheBottom);\n if(keepFrom>cacheTop){cache.copyWithin(0,(keepFrom-cacheTop)*rowBytes,(cacheBottom-cacheTop)*rowBytes);cacheTop=keepFrom;}\n while(cacheBottom<firstRow+rowCount){\n const bandHeight=Math.min(32,cropBottom-cacheBottom);\n if((cacheBottom+bandHeight-cacheTop)*rowBytes>cache.length)throw new InvalidConfigurationError(\"Print source traversal is not monotonic.\");\n for(let sx=plan.crop.x;sx<cropRight;sx+=256){\n check();++reads;\n const request=Object.freeze({source:plan.source,x:sx,y:cacheBottom,width:Math.min(256,cropRight-sx),height:bandHeight});\n const supplied=await read(request,abort.signal);check();\n const tile=requireAllowlistedRecord(supplied,[\"source\",\"x\",\"y\",\"width\",\"height\",\"samples\"],\"Invalid Print source tile.\");\n const source=requireAllowlistedRecord(tile.source,[\"captureId\",\"imageStateId\",\"artifactId\",\"sha256\",\"width\",\"height\",\"encoding\"],\"Invalid Print tile source.\");\n for(const key of Object.keys(plan.source) as (keyof PrintJpegSource)[])if(source[key]!==plan.source[key])throw new InvalidConfigurationError(\"Print source identity mismatch.\");\n for(const key of [\"x\",\"y\",\"width\",\"height\"] as const)if(tile[key]!==request[key])throw new InvalidConfigurationError(\"Print source rectangle mismatch.\");\n const samples=tile.samples;if(!(samples instanceof Uint8Array)||!(samples.buffer instanceof ArrayBuffer)||samples.length!==request.width*request.height*3||samples.buffer.byteLength!==samples.byteLength)throw new InvalidConfigurationError(\"Print source requires exact owned bounded RGB tile storage.\");\n for(let iy=0;iy<bandHeight;iy++)cache.set(samples.subarray(iy*request.width*3,(iy+1)*request.width*3),(cacheBottom-cacheTop+iy)*rowBytes+(sx-plan.crop.x)*3);\n // One output block can cover a megapixel of source, so also yield to the host while reading the source.\n if(reads%32===0){await yieldControl(abort.signal);check();}\n }\n cacheBottom+=bandHeight;\n }\n };\n const blocksX=Math.ceil(plan.outputWidth/8),sums=new Float64Array(blocksX*192);\n // At 1:1 every output pixel is one source pixel with weight exactly 1, so its sum is exactly lut[code] and its\n // sample is a function of the code alone: the same bytes from a 256-entry table, without the accumulation.\n const identity=scaleX===1&&scaleY===1;\n const direct=identity?directCodes():undefined;\n if(direct)for(let by=0;by<plan.outputHeight;by+=8){\n check();\n const height=Math.min(8,plan.outputHeight-by);await ensureRows(plan.crop.y+by,height);check();\n for(let bx=0;bx<plan.outputWidth;bx+=8){\n const width=Math.min(8,plan.outputWidth-bx),samples:number[]=[];\n for(let y=0;y<8;y++){\n const row=(plan.crop.y+by+Math.min(y,height-1)-cacheTop)*rowBytes+bx*3;\n for(let x=0;x<8;x++){const index=row+Math.min(x,width-1)*3;samples.push(direct[cache[index]!]!,direct[cache[index+1]!]!,direct[cache[index+2]!]!);}\n }\n check();encoder.writeRgbBlock(samples);if(++blocks%32===0){await yieldControl(abort.signal);check();}\n }\n }\n else for(let by=0;by<plan.outputHeight;by+=8){\n check();sums.fill(0);\n const height=Math.min(8,plan.outputHeight-by),top=plan.crop.y+by*scaleY,bottom=by+height===plan.outputHeight?cropBottom:plan.crop.y+(by+height)*scaleY;\n // Accumulation order per output pixel is unchanged from the per-block reader: 32-row bands from\n // floor(top), then 256-column chunks from floor(left), then rows, then columns. Identical IEEE sums.\n for(let sy=Math.floor(top);sy<Math.ceil(bottom);sy+=32){\n const bandHeight=Math.min(32,Math.ceil(bottom)-sy);await ensureRows(sy,bandHeight);check();\n for(let bx=0,block=0;bx<plan.outputWidth;bx+=8,block++){\n const width=Math.min(8,plan.outputWidth-bx),left=plan.crop.x+bx*scaleX,right=bx+width===plan.outputWidth?cropRight:plan.crop.x+(bx+width)*scaleX,base=block*192;\n for(let sx=Math.floor(left);sx<Math.ceil(right);sx+=256){\n const chunkWidth=Math.min(256,Math.ceil(right)-sx);\n for(let iy=0;iy<bandHeight;iy++)for(let ix=0;ix<chunkWidth;ix++){\n const px=sx+ix,py=sy+iy;\n const firstX=Math.max(0,Math.floor((px-plan.crop.x)/scaleX)-bx),lastX=Math.min(width-1,Math.ceil((px+1-plan.crop.x)/scaleX)-bx-1);\n const firstY=Math.max(0,Math.floor((py-plan.crop.y)/scaleY)-by),lastY=Math.min(height-1,Math.ceil((py+1-plan.crop.y)/scaleY)-by-1);\n const index=(py-cacheTop)*rowBytes+(px-plan.crop.x)*3,r=lut[cache[index]!]!,g=lut[cache[index+1]!]!,b=lut[cache[index+2]!]!;\n for(let oy=firstY;oy<=lastY;oy++)for(let ox=firstX;ox<=lastX;ox++){\n const x0=plan.crop.x+(bx+ox)*scaleX,y0=plan.crop.y+(by+oy)*scaleY;\n const weight=Math.max(0,Math.min(px+1,x0+scaleX)-Math.max(px,x0))*Math.max(0,Math.min(py+1,y0+scaleY)-Math.max(py,y0))/area;\n const i=base+(oy*8+ox)*3;sums[i]=sums[i]!+r*weight;sums[i+1]=sums[i+1]!+g*weight;sums[i+2]=sums[i+2]!+b*weight;\n }\n }\n }\n }\n }\n for(let bx=0,block=0;bx<plan.outputWidth;bx+=8,block++){\n const width=Math.min(8,plan.outputWidth-bx),base=block*192,samples:number[]=[];\n for(let y=0;y<8;y++)for(let x=0;x<8;x++)for(let c=0;c<3;c++){\n const value=sums[base+(Math.min(y,height-1)*8+Math.min(x,width-1))*3+c]!;\n // Nearest code, ties up. Math.round is used because floor(x+0.5) wrongly rounds 0.49999999999999994 up.\n samples.push(Math.min(255,Math.max(0,Math.round(encodeSrgbComponent(value)*255))));\n }\n check();encoder.writeRgbBlock(samples);if(++blocks%32===0){await yieldControl(abort.signal);check();}\n }\n }\n await yieldControl(abort.signal);check();const bytes=encoder.finish();encoder=undefined;\n output={schemaVersion:PRINT_JPEG_SCHEMA_VERSION,input:plan,mediaType:\"image/jpeg\",bytes,iccProfileVersion:SRGB_ICC_PROFILE_VERSION,width:plan.outputWidth,height:plan.outputHeight,resampling:\"exact-area-average-in-decoded-linear-light\",sourceStage:\"post-SDR-8-bit-quantization\",digitalUpscalingApplied:false,sourceBytesVerified:false,providerReads:reads,encoderId:EXPORT_JPEG_ENCODER_ID};state=\"completed\";\n }catch(error){release();if(!abort.signal.aborted){state=\"failed\";abort.abort();}throw error;}\n },cancel():void{if(state===\"ready\"||state===\"running\"||state===\"completed\"){state=\"cancelled\";abort.abort();release();}},\n dispose():void{if(state!==\"transferred\"){state=\"disposed\";abort.abort();release();}},\n takeOutput():PrintJpegOutput{if(state!==\"completed\"||!output)throw new InvalidConfigurationError(\"Print JPEG unavailable.\");const value=output;output=null;state=\"transferred\";return value;}\n };\n}\n"]}
@@ -25,6 +25,18 @@ export interface DeterministicUniformStream {
25
25
  * Changing any of this changes simulated RAW output and needs a model and replay review.
26
26
  */
27
27
  export declare function createDeterministicUniformStream(seedUint32: number): DeterministicUniformStream;
28
+ /** A uniform stream reseeded in place (internal, #418). */
29
+ export interface ResettableUniformStream extends DeterministicUniformStream {
30
+ /** Sets the state createDeterministicUniformStream(seedUint32) starts from, so the draws that follow are that stream's. */
31
+ reset(seedUint32: number): void;
32
+ }
33
+ /**
34
+ * The stream of createDeterministicUniformStream, reseeded in place instead of created anew (internal, #418). The seeding and
35
+ * the xorshift32 step are the same statements, so after `reset(seed)` every draw equals that of
36
+ * `createDeterministicUniformStream(seed)`. It saves a typed array and a closure per stream where a hot loop needs two
37
+ * fresh streams per site.
38
+ */
39
+ export declare function createResettableUniformStream(): ResettableUniformStream;
28
40
  /**
29
41
  * Box–Muller transform of two uniform values in (0,1) (Box & Muller 1958). Each call uses two values
30
42
  * and returns one standard normal sample. The second possible sample is not cached, so the order of
@@ -88,6 +88,31 @@ export function createDeterministicUniformStream(seedUint32) {
88
88
  }
89
89
  };
90
90
  }
91
+ /**
92
+ * The stream of createDeterministicUniformStream, reseeded in place instead of created anew (internal, #418). The seeding and
93
+ * the xorshift32 step are the same statements, so after `reset(seed)` every draw equals that of
94
+ * `createDeterministicUniformStream(seed)`. It saves a typed array and a closure per stream where a hot loop needs two
95
+ * fresh streams per site.
96
+ */
97
+ export function createResettableUniformStream() {
98
+ const state = new Uint32Array(1);
99
+ return {
100
+ reset(seedUint32) {
101
+ state[0] = fmix32((seedUint32 ^ 0x9e3779b9) >>> 0);
102
+ if (state[0] === 0) {
103
+ state[0] = 0x6d2b79f5;
104
+ }
105
+ },
106
+ next() {
107
+ let x = state[0];
108
+ x ^= x << 13;
109
+ x ^= x >>> 17;
110
+ x ^= x << 5;
111
+ state[0] = x;
112
+ return (state[0] + 0.5) / 0x1_0000_0000;
113
+ }
114
+ };
115
+ }
91
116
  /**
92
117
  * Box–Muller transform of two uniform values in (0,1) (Box & Muller 1958). Each call uses two values
93
118
  * and returns one standard normal sample. The second possible sample is not cached, so the order of
@@ -1 +1 @@
1
- {"version":3,"file":"stochastic-seed.js","sourceRoot":"","sources":["../../src/sensor/stochastic-seed.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE3F,kEAAkE;AAClE,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAU,CAAC;AACvD,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAU,CAAC;AAE3D,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,MAAM,UAAU,GAAG,UAAU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AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,EAAW,CAAC;AAC/D,MAAM,CAAC,MAAM,mCAAmC,GAAG,EAAW,CAAC;AAC/D,MAAM,CAAC,MAAM,wCAAwC,GAAG,EAAW,CAAC;AACpE,MAAM,CAAC,MAAM,kCAAkC,GAAG,EAAW,CAAC;AAC9D,MAAM,CAAC,MAAM,qCAAqC,GAAG,EAAW,CAAC;AAEjE;;;;;;GAMG;AACH,MAAM,UAAU,kCAAkC,CAChD,wBAAgC,EAChC,KAAa,EACb,KAAa,EACb,QAAgB;IAEhB,IAAI,CAAC,QAAQ,CAAC,wBAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;QACvG,MAAM,IAAI,2BAA2B,CAAC,oDAAoD,CAAC,CAAC;IAC9F,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,wBAAwB,CAAC,CAAC;IAC1C,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACvB,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACvB,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC1B,OAAO,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;AACxB,CAAC","sourcesContent":["// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Seeds for the random-number streams behind simulated sensor noise. The same inputs always give\n * the same noise.\n *\n * Each per-site stream seed is a strong 32-bit hash of the capture seed, the absolute native\n * row-major site index and a fixed stream ID. The hash is MurmurHash3_x86_32 (Austin Appleby,\n * placed in the public domain) of the 12-byte little-endian message `[captureSeed, siteIndex,\n * streamId]` with hash seed 0. Its avalanche finalizer (`fmix32`) makes a one-bit input change flip\n * each output bit with probability close to 1/2. So neighbouring sites, the two streams of one site\n * and captures with nearby seeds get unrelated seeds. A linear schedule such as `seed + 2*index` fed\n * into a linear generator (xorshift) lacks this property, and produced correlated noise with too\n * little variance (issue #270).\n *\n * This is deterministic and not cryptographic; it does not claim unpredictability. The moment and\n * correlation tests, not this hash alone, show that the resulting noise is statistically acceptable.\n */\n\nimport { InvalidScientificInputError } from \"../core/validation.js\";\nimport { portableCosTwoPi, portableLog, portableSinTwoPi } from \"../core/portable-math.js\";\n\n/** Stream IDs. Changing them changes every simulated RAW code. */\nexport const NATIVE_SITE_CHARGE_STREAM_ID = 0 as const;\nexport const NATIVE_SITE_READ_NOISE_STREAM_ID = 1 as const;\n\nconst MURMUR3_C1 = 0xcc9e2d51;\nconst MURMUR3_C2 = 0x1b873593;\n\n/** MurmurHash3 32-bit avalanche finalizer; a bijection on uint32 with fmix32(0) = 0. */\nexport function fmix32(value: number): number {\n let h = value >>> 0;\n h ^= h >>> 16;\n h = Math.imul(h, 0x85ebca6b);\n h ^= h >>> 13;\n h = Math.imul(h, 0xc2b2ae35);\n h ^= h >>> 16;\n return h >>> 0;\n}\n\nfunction mixBlock(hash: number, block: number): number {\n let k = Math.imul(block >>> 0, MURMUR3_C1);\n k = (k << 15) | (k >>> 17);\n k = Math.imul(k, MURMUR3_C2);\n let h = (hash ^ k) >>> 0;\n h = (h << 13) | (h >>> 19);\n return (Math.imul(h, 5) + 0xe6546b64) >>> 0;\n}\n\nfunction isUint32(value: number): boolean {\n return Number.isInteger(value) && value >= 0 && value <= 0xffffffff;\n}\n\n/**\n * Returns the uint32 seed of one random stream for one absolute native site.\n * All inputs must be uint32 integers. The site index is the full-frame native row-major\n * index, never relative to a tile or chunk, so how the work is split cannot change it.\n */\nexport function deriveNativeSiteStreamSeedUint32(\n captureSeedUint32: number,\n nativeSiteIndex: number,\n streamId: number\n): number {\n if (!isUint32(captureSeedUint32) || !isUint32(nativeSiteIndex) || !isUint32(streamId)) {\n throw new InvalidScientificInputError(\"Stochastic stream seed inputs must be uint32 integers.\");\n }\n let h = 0;\n h = mixBlock(h, captureSeedUint32);\n h = mixBlock(h, nativeSiteIndex);\n h = mixBlock(h, streamId);\n return fmix32(h ^ 12);\n}\n\n/** One deterministic stream of uniforms strictly inside (0,1). */\nexport interface DeterministicUniformStream {\n next(): number;\n}\n\n/**\n * Creates the deterministic, non-cryptographic xorshift32 random stream for one charge, read-noise\n * or fixed-pattern identity.\n *\n * The seed is first scrambled by the MurmurHash3 `fmix32` avalanche finalizer. xorshift32 is linear\n * over GF(2), so an unscrambled seed would give nearby seeds almost identical first draws, and noise\n * that is correlated across the sensor with too little variance (issue #270). The nonzero fallback\n * avoids the zero state, which xorshift can never leave. Adding 0.5 before dividing gives values\n * strictly inside (0,1), so the logarithms in the Poisson and normal transforms never see 0 or 1.\n * Changing any of this changes simulated RAW output and needs a model and replay review.\n */\nexport function createDeterministicUniformStream(seedUint32: number): DeterministicUniformStream {\n // The state lives in a Uint32Array: storing the int32 result there is the same `>>> 0` reduction, and a uint32 above\n // 2^30 kept in a closure variable would be boxed on every step (half the speed). The draws are unchanged.\n const state = new Uint32Array(1);\n state[0] = fmix32((seedUint32 ^ 0x9e3779b9) >>> 0);\n if (state[0] === 0) {\n state[0] = 0x6d2b79f5;\n }\n return {\n next(): number {\n let x = state[0]!;\n x ^= x << 13;\n x ^= x >>> 17;\n x ^= x << 5;\n state[0] = x;\n return (state[0] + 0.5) / 0x1_0000_0000;\n }\n };\n}\n\n/**\n * Box–Muller transform of two uniform values in (0,1) (Box & Muller 1958). Each call uses two values\n * and returns one standard normal sample. The second possible sample is not cached, so the order of\n * draws from the stream never shifts.\n */\nexport function sampleDeterministicStandardNormal(random: DeterministicUniformStream): number {\n const u1 = random.next();\n const u2 = random.next();\n return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n}\n\n/**\n * The same Box–Muller draw (same two uniforms, same order) with the engine's portable logarithm and cosine\n * (`portableLog`, `portableCosTwoPi`), so the sample has the same bits in every JavaScript engine. Used by the\n * display view; the native noise keeps `sampleDeterministicStandardNormal`.\n */\nexport function samplePortableStandardNormal(random: DeterministicUniformStream): number {\n const u1 = random.next();\n const u2 = random.next();\n return Math.sqrt(-2 * portableLog(u1)) * portableCosTwoPi(u2);\n}\n\n/**\n * Standard normal samples from one stream, two per pair of uniforms: both Box–Muller outputs, r·cos(2πu₂) and then\n * r·sin(2πu₂) with r = √(−2 ln u₁) (Box & Muller 1958; the two are independent standard normals), with the portable\n * functions. Each call returns the next sample; the stream advances by two uniforms on every other call. Used by the\n * display view's AHD correction states, which draw many normals from one stream.\n */\nexport function createPortableStandardNormalPairs(random: DeterministicUniformStream): () => number {\n let spare = 0, hasSpare = false;\n return () => {\n if (hasSpare) { hasSpare = false; return spare; }\n const u1 = random.next(), u2 = random.next(), r = Math.sqrt(-2 * portableLog(u1));\n spare = r * portableSinTwoPi(u2); hasSpare = true;\n return r * portableCosTwoPi(u2);\n };\n}\n\n/**\n * Fixed-pattern stream IDs. They seed spatial patterns that belong to one physical sensor, never\n * per-capture noise, and are hashed with the sensor-instance seed instead of the capture seed.\n */\nexport const SENSOR_FIXED_PATTERN_PRNU_STREAM_ID = 16 as const;\nexport const SENSOR_FIXED_PATTERN_DSNU_STREAM_ID = 17 as const;\nexport const SENSOR_FIXED_PATTERN_HOT_PIXEL_STREAM_ID = 18 as const;\nexport const SENSOR_FIXED_PATTERN_ROW_STREAM_ID = 19 as const;\nexport const SENSOR_FIXED_PATTERN_COLUMN_STREAM_ID = 20 as const;\n\n/**\n * Returns the uint32 seed of one fixed-pattern stream for one physical colour-sampling site: the\n * MurmurHash3_x86_32 hash (hash seed 0) of the 16-byte little-endian message\n * `[sensorInstanceSeed, siteX, siteY, streamId]`. Site coordinates are absolute indices in the\n * colour-sampling grid, so the pattern belongs to the physical sensor and does not change with crop,\n * tiling or the per-capture seed. Deterministic and not cryptographic.\n */\nexport function deriveSensorFixedPatternSeedUint32(\n sensorInstanceSeedUint32: number,\n siteX: number,\n siteY: number,\n streamId: number\n): number {\n if (!isUint32(sensorInstanceSeedUint32) || !isUint32(siteX) || !isUint32(siteY) || !isUint32(streamId)) {\n throw new InvalidScientificInputError(\"Fixed-pattern seed inputs must be uint32 integers.\");\n }\n let h = 0;\n h = mixBlock(h, sensorInstanceSeedUint32);\n h = mixBlock(h, siteX);\n h = mixBlock(h, siteY);\n h = mixBlock(h, streamId);\n return fmix32(h ^ 16);\n}\n"]}
1
+ {"version":3,"file":"stochastic-seed.js","sourceRoot":"","sources":["../../src/sensor/stochastic-seed.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE3F,kEAAkE;AAClE,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAU,CAAC;AACvD,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAU,CAAC;AAE3D,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,MAAM,UAAU,GAAG,UAAU,CAAC;AAE9B,wFAAwF;AACxF,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,IAAI,CAAC,GAAG,KAAK,KAAK,CAAC,CAAC;IACpB,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IAC7B,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IAC7B,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY,EAAE,KAAa;IAC3C,IAAI,C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apache-2.0\n\n/**\n * Seeds for the random-number streams behind simulated sensor noise. The same inputs always give\n * the same noise.\n *\n * Each per-site stream seed is a strong 32-bit hash of the capture seed, the absolute native\n * row-major site index and a fixed stream ID. The hash is MurmurHash3_x86_32 (Austin Appleby,\n * placed in the public domain) of the 12-byte little-endian message `[captureSeed, siteIndex,\n * streamId]` with hash seed 0. Its avalanche finalizer (`fmix32`) makes a one-bit input change flip\n * each output bit with probability close to 1/2. So neighbouring sites, the two streams of one site\n * and captures with nearby seeds get unrelated seeds. A linear schedule such as `seed + 2*index` fed\n * into a linear generator (xorshift) lacks this property, and produced correlated noise with too\n * little variance (issue #270).\n *\n * This is deterministic and not cryptographic; it does not claim unpredictability. The moment and\n * correlation tests, not this hash alone, show that the resulting noise is statistically acceptable.\n */\n\nimport { InvalidScientificInputError } from \"../core/validation.js\";\nimport { portableCosTwoPi, portableLog, portableSinTwoPi } from \"../core/portable-math.js\";\n\n/** Stream IDs. Changing them changes every simulated RAW code. */\nexport const NATIVE_SITE_CHARGE_STREAM_ID = 0 as const;\nexport const NATIVE_SITE_READ_NOISE_STREAM_ID = 1 as const;\n\nconst MURMUR3_C1 = 0xcc9e2d51;\nconst MURMUR3_C2 = 0x1b873593;\n\n/** MurmurHash3 32-bit avalanche finalizer; a bijection on uint32 with fmix32(0) = 0. */\nexport function fmix32(value: number): number {\n let h = value >>> 0;\n h ^= h >>> 16;\n h = Math.imul(h, 0x85ebca6b);\n h ^= h >>> 13;\n h = Math.imul(h, 0xc2b2ae35);\n h ^= h >>> 16;\n return h >>> 0;\n}\n\nfunction mixBlock(hash: number, block: number): number {\n let k = Math.imul(block >>> 0, MURMUR3_C1);\n k = (k << 15) | (k >>> 17);\n k = Math.imul(k, MURMUR3_C2);\n let h = (hash ^ k) >>> 0;\n h = (h << 13) | (h >>> 19);\n return (Math.imul(h, 5) + 0xe6546b64) >>> 0;\n}\n\nfunction isUint32(value: number): boolean {\n return Number.isInteger(value) && value >= 0 && value <= 0xffffffff;\n}\n\n/**\n * Returns the uint32 seed of one random stream for one absolute native site.\n * All inputs must be uint32 integers. The site index is the full-frame native row-major\n * index, never relative to a tile or chunk, so how the work is split cannot change it.\n */\nexport function deriveNativeSiteStreamSeedUint32(\n captureSeedUint32: number,\n nativeSiteIndex: number,\n streamId: number\n): number {\n if (!isUint32(captureSeedUint32) || !isUint32(nativeSiteIndex) || !isUint32(streamId)) {\n throw new InvalidScientificInputError(\"Stochastic stream seed inputs must be uint32 integers.\");\n }\n let h = 0;\n h = mixBlock(h, captureSeedUint32);\n h = mixBlock(h, nativeSiteIndex);\n h = mixBlock(h, streamId);\n return fmix32(h ^ 12);\n}\n\n/** One deterministic stream of uniforms strictly inside (0,1). */\nexport interface DeterministicUniformStream {\n next(): number;\n}\n\n/**\n * Creates the deterministic, non-cryptographic xorshift32 random stream for one charge, read-noise\n * or fixed-pattern identity.\n *\n * The seed is first scrambled by the MurmurHash3 `fmix32` avalanche finalizer. xorshift32 is linear\n * over GF(2), so an unscrambled seed would give nearby seeds almost identical first draws, and noise\n * that is correlated across the sensor with too little variance (issue #270). The nonzero fallback\n * avoids the zero state, which xorshift can never leave. Adding 0.5 before dividing gives values\n * strictly inside (0,1), so the logarithms in the Poisson and normal transforms never see 0 or 1.\n * Changing any of this changes simulated RAW output and needs a model and replay review.\n */\nexport function createDeterministicUniformStream(seedUint32: number): DeterministicUniformStream {\n // The state lives in a Uint32Array: storing the int32 result there is the same `>>> 0` reduction, and a uint32 above\n // 2^30 kept in a closure variable would be boxed on every step (half the speed). The draws are unchanged.\n const state = new Uint32Array(1);\n state[0] = fmix32((seedUint32 ^ 0x9e3779b9) >>> 0);\n if (state[0] === 0) {\n state[0] = 0x6d2b79f5;\n }\n return {\n next(): number {\n let x = state[0]!;\n x ^= x << 13;\n x ^= x >>> 17;\n x ^= x << 5;\n state[0] = x;\n return (state[0] + 0.5) / 0x1_0000_0000;\n }\n };\n}\n\n/** A uniform stream reseeded in place (internal, #418). */\nexport interface ResettableUniformStream extends DeterministicUniformStream {\n /** Sets the state createDeterministicUniformStream(seedUint32) starts from, so the draws that follow are that stream's. */\n reset(seedUint32: number): void;\n}\n\n/**\n * The stream of createDeterministicUniformStream, reseeded in place instead of created anew (internal, #418). The seeding and\n * the xorshift32 step are the same statements, so after `reset(seed)` every draw equals that of\n * `createDeterministicUniformStream(seed)`. It saves a typed array and a closure per stream where a hot loop needs two\n * fresh streams per site.\n */\nexport function createResettableUniformStream(): ResettableUniformStream {\n const state = new Uint32Array(1);\n return {\n reset(seedUint32: number): void {\n state[0] = fmix32((seedUint32 ^ 0x9e3779b9) >>> 0);\n if (state[0] === 0) {\n state[0] = 0x6d2b79f5;\n }\n },\n next(): number {\n let x = state[0]!;\n x ^= x << 13;\n x ^= x >>> 17;\n x ^= x << 5;\n state[0] = x;\n return (state[0] + 0.5) / 0x1_0000_0000;\n }\n };\n}\n\n/**\n * Box–Muller transform of two uniform values in (0,1) (Box & Muller 1958). Each call uses two values\n * and returns one standard normal sample. The second possible sample is not cached, so the order of\n * draws from the stream never shifts.\n */\nexport function sampleDeterministicStandardNormal(random: DeterministicUniformStream): number {\n const u1 = random.next();\n const u2 = random.next();\n return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n}\n\n/**\n * The same Box–Muller draw (same two uniforms, same order) with the engine's portable logarithm and cosine\n * (`portableLog`, `portableCosTwoPi`), so the sample has the same bits in every JavaScript engine. Used by the\n * display view; the native noise keeps `sampleDeterministicStandardNormal`.\n */\nexport function samplePortableStandardNormal(random: DeterministicUniformStream): number {\n const u1 = random.next();\n const u2 = random.next();\n return Math.sqrt(-2 * portableLog(u1)) * portableCosTwoPi(u2);\n}\n\n/**\n * Standard normal samples from one stream, two per pair of uniforms: both Box–Muller outputs, r·cos(2πu₂) and then\n * r·sin(2πu₂) with r = √(−2 ln u₁) (Box & Muller 1958; the two are independent standard normals), with the portable\n * functions. Each call returns the next sample; the stream advances by two uniforms on every other call. Used by the\n * display view's AHD correction states, which draw many normals from one stream.\n */\nexport function createPortableStandardNormalPairs(random: DeterministicUniformStream): () => number {\n let spare = 0, hasSpare = false;\n return () => {\n if (hasSpare) { hasSpare = false; return spare; }\n const u1 = random.next(), u2 = random.next(), r = Math.sqrt(-2 * portableLog(u1));\n spare = r * portableSinTwoPi(u2); hasSpare = true;\n return r * portableCosTwoPi(u2);\n };\n}\n\n/**\n * Fixed-pattern stream IDs. They seed spatial patterns that belong to one physical sensor, never\n * per-capture noise, and are hashed with the sensor-instance seed instead of the capture seed.\n */\nexport const SENSOR_FIXED_PATTERN_PRNU_STREAM_ID = 16 as const;\nexport const SENSOR_FIXED_PATTERN_DSNU_STREAM_ID = 17 as const;\nexport const SENSOR_FIXED_PATTERN_HOT_PIXEL_STREAM_ID = 18 as const;\nexport const SENSOR_FIXED_PATTERN_ROW_STREAM_ID = 19 as const;\nexport const SENSOR_FIXED_PATTERN_COLUMN_STREAM_ID = 20 as const;\n\n/**\n * Returns the uint32 seed of one fixed-pattern stream for one physical colour-sampling site: the\n * MurmurHash3_x86_32 hash (hash seed 0) of the 16-byte little-endian message\n * `[sensorInstanceSeed, siteX, siteY, streamId]`. Site coordinates are absolute indices in the\n * colour-sampling grid, so the pattern belongs to the physical sensor and does not change with crop,\n * tiling or the per-capture seed. Deterministic and not cryptographic.\n */\nexport function deriveSensorFixedPatternSeedUint32(\n sensorInstanceSeedUint32: number,\n siteX: number,\n siteY: number,\n streamId: number\n): number {\n if (!isUint32(sensorInstanceSeedUint32) || !isUint32(siteX) || !isUint32(siteY) || !isUint32(streamId)) {\n throw new InvalidScientificInputError(\"Fixed-pattern seed inputs must be uint32 integers.\");\n }\n let h = 0;\n h = mixBlock(h, sensorInstanceSeedUint32);\n h = mixBlock(h, siteX);\n h = mixBlock(h, siteY);\n h = mixBlock(h, streamId);\n return fmix32(h ^ 16);\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  # Public API reference
2
2
 
3
- Generated from the exact root exports for **@photivra/engine 2.8.0 / root contract 2.8.0** by `node scripts/generate-api-reference.mjs`. No module deep import is supported.
3
+ Generated from the exact root exports for **@photivra/engine 2.9.0 / root contract 2.9.0** by `node scripts/generate-api-reference.mjs`. No module deep import is supported.
4
4
 
5
5
  Use [the developer guide](https://github.com/Photivra/photivra/blob/main/docs/DEVELOPERS.md) for executable paths and scientific boundaries. Types are contracts, not runtime validation: parse untrusted data, preserve units/reference frames, read assessment blockers, and retain evidence/limitations. Optional properties do not imply a universal default. Model/schema IDs remain independent of the distribution.
6
6
 
@@ -126,6 +126,7 @@ Start here to find a function by task. [Using Photivra from an agent](AGENT_GUID
126
126
  | Export | Kind |
127
127
  | --- | --- |
128
128
  | [`EXPORT_JPEG_ENCODER_ID`](api/capture-export-jpeg.md#export_jpeg_encoder_id) | Runtime |
129
+ | [`JpegStripBits`](api/capture-export-jpeg.md#jpegstripbits) | Type |
129
130
 
130
131
  ## capture/focus-control.ts
131
132
 
@@ -1185,6 +1186,7 @@ Start here to find a function by task. [Using Photivra from an agent](AGENT_GUID
1185
1186
  | --- | --- |
1186
1187
  | [`DisplayViewResult`](api/facade-simulate-capture.md#displayviewresult) | Type |
1187
1188
  | [`IdealReferenceResult`](api/facade-simulate-capture.md#idealreferenceresult) | Type |
1189
+ | [`serializeSimulatedCaptureDngFromParts`](api/facade-simulate-capture.md#serializesimulatedcapturedngfromparts) | Runtime |
1188
1190
  | [`SIMULATE_CAPTURE_BOX_APERTURE_DEFAULT_SAMPLES`](api/facade-simulate-capture.md#simulate_capture_box_aperture_default_samples) | Runtime |
1189
1191
  | [`SIMULATE_CAPTURE_COST_OPTIONS`](api/facade-simulate-capture.md#simulate_capture_cost_options) | Runtime |
1190
1192
  | [`SIMULATE_CAPTURE_DEFAULT_ILLUMINANCE_LUX`](api/facade-simulate-capture.md#simulate_capture_default_illuminance_lux) | Runtime |
@@ -1196,6 +1198,7 @@ Start here to find a function by task. [Using Photivra from an agent](AGENT_GUID
1196
1198
  | [`SimulateCaptureColorEncoding`](api/facade-simulate-capture.md#simulatecapturecolorencoding) | Type |
1197
1199
  | [`SimulateCaptureCostOption`](api/facade-simulate-capture.md#simulatecapturecostoption) | Type |
1198
1200
  | [`SimulateCaptureDemosaic`](api/facade-simulate-capture.md#simulatecapturedemosaic) | Type |
1201
+ | [`SimulateCaptureDngPart`](api/facade-simulate-capture.md#simulatecapturedngpart) | Type |
1199
1202
  | [`SimulateCaptureExposureMode`](api/facade-simulate-capture.md#simulatecaptureexposuremode) | Type |
1200
1203
  | [`SimulateCaptureLensDistortion`](api/facade-simulate-capture.md#simulatecapturelensdistortion) | Type |
1201
1204
  | [`SimulateCaptureMotion`](api/facade-simulate-capture.md#simulatecapturemotion) | Type |
@@ -2020,6 +2023,8 @@ Start here to find a function by task. [Using Photivra from an agent](AGENT_GUID
2020
2023
 
2021
2024
  | Export | Kind |
2022
2025
  | --- | --- |
2026
+ | [`composePrintJpegStrips`](api/output-print-jpeg.md#composeprintjpegstrips) | Runtime |
2027
+ | [`createPrintJpegStrip`](api/output-print-jpeg.md#createprintjpegstrip) | Runtime |
2023
2028
  | [`createPrintJpegTask`](api/output-print-jpeg.md#createprintjpegtask) | Runtime |
2024
2029
  | [`parsePrintJpegInput`](api/output-print-jpeg.md#parseprintjpeginput) | Runtime |
2025
2030
  | [`PRINT_JPEG_LIMITS`](api/output-print-jpeg.md#print_jpeg_limits) | Runtime |
@@ -2028,6 +2033,8 @@ Start here to find a function by task. [Using Photivra from an agent](AGENT_GUID
2028
2033
  | [`PrintJpegOutput`](api/output-print-jpeg.md#printjpegoutput) | Type |
2029
2034
  | [`PrintJpegProvider`](api/output-print-jpeg.md#printjpegprovider) | Type |
2030
2035
  | [`PrintJpegSource`](api/output-print-jpeg.md#printjpegsource) | Type |
2036
+ | [`PrintJpegStrip`](api/output-print-jpeg.md#printjpegstrip) | Type |
2037
+ | [`PrintJpegStripInput`](api/output-print-jpeg.md#printjpegstripinput) | Type |
2031
2038
  | [`PrintJpegTask`](api/output-print-jpeg.md#printjpegtask) | Type |
2032
2039
  | [`PrintJpegTile`](api/output-print-jpeg.md#printjpegtile) | Type |
2033
2040
  | [`PrintJpegTileRequest`](api/output-print-jpeg.md#printjpegtilerequest) | Type |