@paperpic/core 0.1.0 → 0.3.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.
- package/dist/docs/examples/attach-and-scan.md +196 -0
- package/dist/docs/examples/custom-toolbar.md +183 -0
- package/dist/docs/guide/accessibility.md +1 -1
- package/dist/docs/guide/editing.md +6 -15
- package/dist/docs/guide/getting-started.md +83 -10
- package/dist/docs/guide/loading.md +128 -0
- package/dist/docs/guide/performance.md +17 -3
- package/dist/docs/guide/saving.md +6 -0
- package/dist/docs/guide/styling.md +2 -0
- package/dist/docs/guide/tools/adjust.md +2 -0
- package/dist/docs/guide/tools/crop.md +2 -0
- package/dist/docs/guide/tools/rotate-and-flip.md +3 -1
- package/dist/docs/guide/troubleshooting.md +45 -0
- package/dist/docs/guide/viewer.md +1 -1
- package/dist/docs/reference/events.md +11 -7
- package/dist/docs/reference/methods.md +8 -8
- package/dist/docs/reference/options.md +7 -7
- package/dist/docs/reference/react.md +11 -11
- package/dist/docs/reference/state.md +4 -4
- package/dist/docs/reference/types.md +301 -0
- package/dist/docs/reference/vue.md +10 -10
- package/dist/index.d.ts +125 -49
- package/dist/index.js +1469 -1350
- package/dist/{pipeline-MP1RPlXT.js → steps-DuGiMvk0.js} +236 -75
- package/dist/types-C3ZHKId1.d.ts +31 -0
- package/dist/worker.d.ts +13 -61
- package/dist/worker.js +3 -10
- package/package.json +2 -2
|
@@ -188,6 +188,211 @@ function subtract(a, b) {
|
|
|
188
188
|
a[2] - b[2]
|
|
189
189
|
];
|
|
190
190
|
}
|
|
191
|
+
// src/state/view.ts
|
|
192
|
+
/**
|
|
193
|
+
* Rounds an angle in degrees to the nearest multiple of 90 and wraps it into [0, 360).
|
|
194
|
+
*
|
|
195
|
+
* @throws {RangeError} If `degrees` is not a finite number.
|
|
196
|
+
*/
|
|
197
|
+
function normalizeRotation(degrees) {
|
|
198
|
+
if (!Number.isFinite(degrees)) throw new RangeError(`Rotation must be a finite number, got ${degrees}`);
|
|
199
|
+
return (Math.round(degrees / 90) * 90 % 360 + 360) % 360;
|
|
200
|
+
}
|
|
201
|
+
const MAX_ZOOM = 8;
|
|
202
|
+
/** A view of the whole image that nothing has measured yet. */
|
|
203
|
+
function createView() {
|
|
204
|
+
return {
|
|
205
|
+
zoom: 1,
|
|
206
|
+
panX: 0,
|
|
207
|
+
panY: 0,
|
|
208
|
+
rotation: 0,
|
|
209
|
+
fit: "image"
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/** The size of the image as the user sees it, turned. */
|
|
213
|
+
function displayedSize(image, rotation) {
|
|
214
|
+
return rotation === 90 || rotation === 270 ? {
|
|
215
|
+
width: image.height,
|
|
216
|
+
height: image.width
|
|
217
|
+
} : image;
|
|
218
|
+
}
|
|
219
|
+
/** Converts a position in the image as the user sees it, turned, to the viewport. */
|
|
220
|
+
function frameToViewport(view, image, viewport, { x, y }) {
|
|
221
|
+
const { width, height } = displayedSize(image, view.rotation);
|
|
222
|
+
return {
|
|
223
|
+
x: viewport.width / 2 + view.panX + (x - width / 2) * view.zoom,
|
|
224
|
+
y: viewport.height / 2 + view.panY + (y - height / 2) * view.zoom
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/** Converts a position in the viewport to the image as the user sees it, turned. */
|
|
228
|
+
function viewportToFrame(view, image, viewport, { x, y }) {
|
|
229
|
+
const { width, height } = displayedSize(image, view.rotation);
|
|
230
|
+
return {
|
|
231
|
+
x: (x - viewport.width / 2 - view.panX) / view.zoom + width / 2,
|
|
232
|
+
y: (y - viewport.height / 2 - view.panY) / view.zoom + height / 2
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function fitZoom(fit, image, viewport, rotation) {
|
|
236
|
+
const { width, height } = displayedSize(image, rotation);
|
|
237
|
+
const byWidth = viewport.width / width;
|
|
238
|
+
const byHeight = viewport.height / height;
|
|
239
|
+
switch (fit) {
|
|
240
|
+
case "width": return byWidth;
|
|
241
|
+
case "height": return byHeight;
|
|
242
|
+
case "image": return Math.min(byWidth, byHeight);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/** The zoom range for an image. Fitting a very small image can go over the usual maximum. */
|
|
246
|
+
function zoomLimits(image, viewport, rotation) {
|
|
247
|
+
return {
|
|
248
|
+
min: Math.min(fitZoom("image", image, viewport, rotation), 1),
|
|
249
|
+
max: Math.max(MAX_ZOOM, fitZoom("width", image, viewport, rotation), fitZoom("height", image, viewport, rotation))
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Keeps the image from leaving the viewport, and centers it along any side where it fits.
|
|
254
|
+
* `sideMargin` is how far an image that overflows can go past its sides, so a toolbar over it never hides a part for good.
|
|
255
|
+
*/
|
|
256
|
+
function clampPan(view, image, viewport, sideMargin = 0) {
|
|
257
|
+
const { width, height } = displayedSize(image, view.rotation);
|
|
258
|
+
const margin = overflows(view, image, viewport) ? sideMargin : 0;
|
|
259
|
+
const limitX = Math.max(0, (width * view.zoom - viewport.width) / 2) + margin;
|
|
260
|
+
const limitY = Math.max(0, (height * view.zoom - viewport.height) / 2);
|
|
261
|
+
return {
|
|
262
|
+
...view,
|
|
263
|
+
panX: Math.min(limitX, Math.max(-limitX, view.panX)),
|
|
264
|
+
panY: Math.min(limitY, Math.max(-limitY, view.panY))
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** Applies a fit. When the image overflows, it starts from its top left corner, where reading starts. */
|
|
268
|
+
function fitView(view, fit, image, viewport) {
|
|
269
|
+
const zoom = fitZoom(fit, image, viewport, view.rotation);
|
|
270
|
+
return clampPan({
|
|
271
|
+
...view,
|
|
272
|
+
zoom,
|
|
273
|
+
fit,
|
|
274
|
+
panX: Number.POSITIVE_INFINITY,
|
|
275
|
+
panY: Number.POSITIVE_INFINITY
|
|
276
|
+
}, image, viewport);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Sets the zoom and keeps the image point under `point` in place.
|
|
280
|
+
* A value out of range is clamped to the zoom limits.
|
|
281
|
+
*/
|
|
282
|
+
function zoomTo(view, zoom, point, image, viewport, sideMargin = 0) {
|
|
283
|
+
const { min, max } = zoomLimits(image, viewport, view.rotation);
|
|
284
|
+
const next = Math.min(max, Math.max(min, zoom));
|
|
285
|
+
const scale = next / view.zoom;
|
|
286
|
+
const fromCenterX = point.x - viewport.width / 2;
|
|
287
|
+
const fromCenterY = point.y - viewport.height / 2;
|
|
288
|
+
return clampPan({
|
|
289
|
+
...view,
|
|
290
|
+
zoom: next,
|
|
291
|
+
fit: null,
|
|
292
|
+
panX: fromCenterX - (fromCenterX - view.panX) * scale,
|
|
293
|
+
panY: fromCenterY - (fromCenterY - view.panY) * scale
|
|
294
|
+
}, image, viewport, sideMargin);
|
|
295
|
+
}
|
|
296
|
+
/** Moves the image by a distance in CSS pixels. */
|
|
297
|
+
function panBy(view, deltaX, deltaY, image, viewport, sideMargin = 0) {
|
|
298
|
+
return clampPan({
|
|
299
|
+
...view,
|
|
300
|
+
panX: view.panX + deltaX,
|
|
301
|
+
panY: view.panY + deltaY
|
|
302
|
+
}, image, viewport, sideMargin);
|
|
303
|
+
}
|
|
304
|
+
/** Sets the rotation, keeping the fit in effect if there is one. */
|
|
305
|
+
function setRotation(view, rotation, image, viewport) {
|
|
306
|
+
if (rotation === view.rotation) return view;
|
|
307
|
+
const rotated = {
|
|
308
|
+
...view,
|
|
309
|
+
rotation,
|
|
310
|
+
panX: 0,
|
|
311
|
+
panY: 0
|
|
312
|
+
};
|
|
313
|
+
return view.fit ? fitView(rotated, view.fit, image, viewport) : clampPan(rotated, image, viewport);
|
|
314
|
+
}
|
|
315
|
+
/** Turns the view by a quarter turn. */
|
|
316
|
+
function rotateView(view, direction, image, viewport) {
|
|
317
|
+
return setRotation(view, normalizeRotation(view.rotation + 90 * direction), image, viewport);
|
|
318
|
+
}
|
|
319
|
+
/** Adapts the view to a new viewport size. */
|
|
320
|
+
function resizeView(view, image, viewport) {
|
|
321
|
+
return view.fit ? fitView(view, view.fit, image, viewport) : clampPan(view, image, viewport);
|
|
322
|
+
}
|
|
323
|
+
/** Whether the image is larger than the viewport, so there is something to pan. */
|
|
324
|
+
function overflows(view, image, viewport) {
|
|
325
|
+
const { width, height } = displayedSize(image, view.rotation);
|
|
326
|
+
const tolerance = .5;
|
|
327
|
+
return width * view.zoom > viewport.width + tolerance || height * view.zoom > viewport.height + tolerance;
|
|
328
|
+
}
|
|
329
|
+
// src/processing/adjust.ts
|
|
330
|
+
/** The shift of the tones at full brightness, as a fraction of the whole range. */
|
|
331
|
+
const MAX_SHIFT = .5;
|
|
332
|
+
/** The contrast factor is `2 ** (contrast * CONTRAST_EXPONENT)`, from a quarter to four times. */
|
|
333
|
+
const CONTRAST_EXPONENT = 2;
|
|
334
|
+
const LEVELS = 256;
|
|
335
|
+
function assertInRange$1(name, value) {
|
|
336
|
+
if (!(value >= -1 && value <= 1)) throw new RangeError(`${name} must be between -1 and 1, got ${value}`);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Changes the brightness and contrast of an image. Each channel goes through the same table of 256 values,
|
|
340
|
+
* so it is fast enough to run on every change of a ruler. Transparency does not change.
|
|
341
|
+
*
|
|
342
|
+
* @throws {RangeError} If an option is out of range.
|
|
343
|
+
*/
|
|
344
|
+
function adjustPixels({ data, width, height }, { brightness = 0, contrast = 0 } = {}) {
|
|
345
|
+
assertInRange$1("brightness", brightness);
|
|
346
|
+
assertInRange$1("contrast", contrast);
|
|
347
|
+
const factor = 2 ** (contrast * CONTRAST_EXPONENT);
|
|
348
|
+
const shift = brightness * MAX_SHIFT;
|
|
349
|
+
const levels = new Uint8ClampedArray(LEVELS);
|
|
350
|
+
for (let level = 0; level < LEVELS; level++) levels[level] = ((level / 255 - .5) * factor + .5 + shift) * 255;
|
|
351
|
+
const output = new Uint8ClampedArray(data.length);
|
|
352
|
+
for (let offset = 0; offset < data.length; offset += 4) {
|
|
353
|
+
output[offset] = levels[data[offset]];
|
|
354
|
+
output[offset + 1] = levels[data[offset + 1]];
|
|
355
|
+
output[offset + 2] = levels[data[offset + 2]];
|
|
356
|
+
output[offset + 3] = data[offset + 3];
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
data: output,
|
|
360
|
+
width,
|
|
361
|
+
height
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
// src/processing/flip.ts
|
|
365
|
+
/** Mirrors an image left to right, moving whole pixels, so it loses no quality. */
|
|
366
|
+
function flipPixels({ data, width, height }) {
|
|
367
|
+
const source = new Uint32Array(data.buffer, data.byteOffset, width * height);
|
|
368
|
+
const output = new Uint32Array(width * height);
|
|
369
|
+
for (let row = 0; row < width * height; row += width) {
|
|
370
|
+
const last = row + width - 1;
|
|
371
|
+
for (let x = 0; x < width; x++) output[row + x] = source[last - x];
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
data: new Uint8ClampedArray(output.buffer),
|
|
375
|
+
width,
|
|
376
|
+
height
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
// src/processing/rotate.ts
|
|
380
|
+
/** Turns an image clockwise, moving whole pixels, so it loses no quality. */
|
|
381
|
+
function rotatePixels({ data, width, height }, rotation) {
|
|
382
|
+
const source = new Uint32Array(data.buffer, data.byteOffset, width * height);
|
|
383
|
+
const output = new Uint32Array(width * height);
|
|
384
|
+
const sideways = rotation !== 180;
|
|
385
|
+
const outputWidth = sideways ? height : width;
|
|
386
|
+
for (let y = 0, index = 0; y < height; y++) for (let x = 0; x < width; x++, index++) {
|
|
387
|
+
const target = rotation === 90 ? x * outputWidth + (height - 1 - y) : rotation === 180 ? (height - 1 - y) * outputWidth + (width - 1 - x) : (width - 1 - x) * outputWidth + y;
|
|
388
|
+
output[target] = source[index];
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
data: new Uint8ClampedArray(output.buffer),
|
|
392
|
+
width: outputWidth,
|
|
393
|
+
height: sideways ? width : height
|
|
394
|
+
};
|
|
395
|
+
}
|
|
191
396
|
// src/processing/grayscale.ts
|
|
192
397
|
/**
|
|
193
398
|
* Converts an image to luminance, with one byte per pixel.
|
|
@@ -213,7 +418,7 @@ const MAX_WHITE_POINT = .98;
|
|
|
213
418
|
/** Stores ratios up to about 1.33, since anything brighter than the mean is white at every setting. */
|
|
214
419
|
const RATIO_SCALE = 192;
|
|
215
420
|
const RATIO_CODES = 256;
|
|
216
|
-
function assertInRange
|
|
421
|
+
function assertInRange(name, value, condition) {
|
|
217
422
|
if (!condition) throw new RangeError(`${name} is out of range, got ${value}`);
|
|
218
423
|
}
|
|
219
424
|
/**
|
|
@@ -222,7 +427,7 @@ function assertInRange$1(name, value, condition) {
|
|
|
222
427
|
* @throws {RangeError} If an option is out of range.
|
|
223
428
|
*/
|
|
224
429
|
function prepareThreshold(source, { windowRatio = DEFAULT_WINDOW_RATIO } = {}) {
|
|
225
|
-
assertInRange
|
|
430
|
+
assertInRange("windowRatio", windowRatio, windowRatio > 0);
|
|
226
431
|
const { width, height } = source;
|
|
227
432
|
const gray = toGrayscale(source);
|
|
228
433
|
const ratios = new Uint8ClampedArray(width * height);
|
|
@@ -253,15 +458,15 @@ function prepareThreshold(source, { windowRatio = DEFAULT_WINDOW_RATIO } = {}) {
|
|
|
253
458
|
};
|
|
254
459
|
}
|
|
255
460
|
/**
|
|
256
|
-
* Produces the final image from a prepared threshold. It is fast enough to run on every change of a
|
|
461
|
+
* Produces the final image from a prepared threshold. It is fast enough to run on every change of a ruler.
|
|
257
462
|
* Pixels well below the local mean turn black, pixels close to it turn white, and the ones in between stay gray according to `softness`.
|
|
258
463
|
*
|
|
259
464
|
* @throws {RangeError} If an option is out of range.
|
|
260
465
|
*/
|
|
261
466
|
function applyThreshold({ ratios, width, height }, options = {}) {
|
|
262
467
|
const { sensitivity = DEFAULT_SENSITIVITY, softness = DEFAULT_SOFTNESS } = options;
|
|
263
|
-
assertInRange
|
|
264
|
-
assertInRange
|
|
468
|
+
assertInRange("sensitivity", sensitivity, sensitivity >= 0 && sensitivity <= 1);
|
|
469
|
+
assertInRange("softness", softness, softness >= 0 && softness <= 1);
|
|
265
470
|
const limit = 1 - MAX_DROP * (1 - sensitivity);
|
|
266
471
|
const blackPoint = limit - softness * SHARE_BELOW_LIMIT;
|
|
267
472
|
const whitePoint = Math.min(limit + softness * SHARE_ABOVE_LIMIT, Math.max(MAX_WHITE_POINT, limit));
|
|
@@ -280,80 +485,13 @@ function applyThreshold({ ratios, width, height }, options = {}) {
|
|
|
280
485
|
};
|
|
281
486
|
}
|
|
282
487
|
/**
|
|
283
|
-
* Cleans a document image in one step. To react to a
|
|
488
|
+
* Cleans a document image in one step. To react to a ruler, call `prepareThreshold` once and `applyThreshold` on each change.
|
|
284
489
|
*
|
|
285
490
|
* @throws {RangeError} If an option is out of range.
|
|
286
491
|
*/
|
|
287
492
|
function adaptiveThreshold(source, options = {}) {
|
|
288
493
|
return applyThreshold(prepareThreshold(source, options), options);
|
|
289
494
|
}
|
|
290
|
-
// src/processing/adjust.ts
|
|
291
|
-
/** The shift of the tones at full brightness, as a fraction of the whole range. */
|
|
292
|
-
const MAX_SHIFT = .5;
|
|
293
|
-
/** The contrast factor is `2 ** (contrast * CONTRAST_EXPONENT)`, from a quarter to four times. */
|
|
294
|
-
const CONTRAST_EXPONENT = 2;
|
|
295
|
-
const LEVELS = 256;
|
|
296
|
-
function assertInRange(name, value) {
|
|
297
|
-
if (!(value >= -1 && value <= 1)) throw new RangeError(`${name} must be between -1 and 1, got ${value}`);
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Changes the brightness and contrast of an image. Each channel goes through the same table of 256 values,
|
|
301
|
-
* so it is fast enough to run on every change of a slider. Transparency does not change.
|
|
302
|
-
*
|
|
303
|
-
* @throws {RangeError} If an option is out of range.
|
|
304
|
-
*/
|
|
305
|
-
function adjustPixels({ data, width, height }, { brightness = 0, contrast = 0 } = {}) {
|
|
306
|
-
assertInRange("brightness", brightness);
|
|
307
|
-
assertInRange("contrast", contrast);
|
|
308
|
-
const factor = 2 ** (contrast * CONTRAST_EXPONENT);
|
|
309
|
-
const shift = brightness * MAX_SHIFT;
|
|
310
|
-
const levels = new Uint8ClampedArray(LEVELS);
|
|
311
|
-
for (let level = 0; level < LEVELS; level++) levels[level] = ((level / 255 - .5) * factor + .5 + shift) * 255;
|
|
312
|
-
const output = new Uint8ClampedArray(data.length);
|
|
313
|
-
for (let offset = 0; offset < data.length; offset += 4) {
|
|
314
|
-
output[offset] = levels[data[offset]];
|
|
315
|
-
output[offset + 1] = levels[data[offset + 1]];
|
|
316
|
-
output[offset + 2] = levels[data[offset + 2]];
|
|
317
|
-
output[offset + 3] = data[offset + 3];
|
|
318
|
-
}
|
|
319
|
-
return {
|
|
320
|
-
data: output,
|
|
321
|
-
width,
|
|
322
|
-
height
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
// src/processing/flip.ts
|
|
326
|
-
/** Mirrors an image left to right, moving whole pixels, so it loses no quality. */
|
|
327
|
-
function flipPixels({ data, width, height }) {
|
|
328
|
-
const source = new Uint32Array(data.buffer, data.byteOffset, width * height);
|
|
329
|
-
const output = new Uint32Array(width * height);
|
|
330
|
-
for (let row = 0; row < width * height; row += width) {
|
|
331
|
-
const last = row + width - 1;
|
|
332
|
-
for (let x = 0; x < width; x++) output[row + x] = source[last - x];
|
|
333
|
-
}
|
|
334
|
-
return {
|
|
335
|
-
data: new Uint8ClampedArray(output.buffer),
|
|
336
|
-
width,
|
|
337
|
-
height
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
// src/processing/rotate.ts
|
|
341
|
-
/** Turns an image clockwise, moving whole pixels, so it loses no quality. */
|
|
342
|
-
function rotatePixels({ data, width, height }, rotation) {
|
|
343
|
-
const source = new Uint32Array(data.buffer, data.byteOffset, width * height);
|
|
344
|
-
const output = new Uint32Array(width * height);
|
|
345
|
-
const sideways = rotation !== 180;
|
|
346
|
-
const outputWidth = sideways ? height : width;
|
|
347
|
-
for (let y = 0, index = 0; y < height; y++) for (let x = 0; x < width; x++, index++) {
|
|
348
|
-
const target = rotation === 90 ? x * outputWidth + (height - 1 - y) : rotation === 180 ? (height - 1 - y) * outputWidth + (width - 1 - x) : (width - 1 - x) * outputWidth + y;
|
|
349
|
-
output[target] = source[index];
|
|
350
|
-
}
|
|
351
|
-
return {
|
|
352
|
-
data: new Uint8ClampedArray(output.buffer),
|
|
353
|
-
width: outputWidth,
|
|
354
|
-
height: sideways ? width : height
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
495
|
// src/processing/warp.ts
|
|
358
496
|
/**
|
|
359
497
|
* Straightens the area inside `quad` into a rectangle of the given size, with bilinear interpolation.
|
|
@@ -419,7 +557,30 @@ function warpPerspective(source, quad, width, height) {
|
|
|
419
557
|
height
|
|
420
558
|
};
|
|
421
559
|
}
|
|
422
|
-
// src/pipeline/
|
|
560
|
+
// src/pipeline/steps.ts
|
|
561
|
+
/** Whether a step changes only the tones. */
|
|
562
|
+
function isToneStep(step) {
|
|
563
|
+
return step.type === "threshold" || step.type === "adjust";
|
|
564
|
+
}
|
|
565
|
+
const UPRIGHT = {
|
|
566
|
+
degrees: 0,
|
|
567
|
+
mirrored: false
|
|
568
|
+
};
|
|
569
|
+
/** Mirrors an orientation left to right. A flip after a turn is the same as a flip before the opposite turn. */
|
|
570
|
+
function mirrorOrientation({ degrees, mirrored }) {
|
|
571
|
+
return {
|
|
572
|
+
degrees: -degrees,
|
|
573
|
+
mirrored: !mirrored
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
/** The steps of an orientation, in the order it means: the flip, then the turn. */
|
|
577
|
+
function orientationSteps({ degrees, mirrored }) {
|
|
578
|
+
const rotation = normalizeRotation(degrees);
|
|
579
|
+
return [...mirrored ? [{ type: "flip" }] : [], ...rotation === 0 ? [] : [{
|
|
580
|
+
type: "rotate",
|
|
581
|
+
rotation
|
|
582
|
+
}]];
|
|
583
|
+
}
|
|
423
584
|
function applyStep(pixels, step) {
|
|
424
585
|
switch (step.type) {
|
|
425
586
|
case "rotate": return rotatePixels(pixels, step.rotation);
|
|
@@ -444,4 +605,4 @@ function applyStep(pixels, step) {
|
|
|
444
605
|
function applySteps(source, steps) {
|
|
445
606
|
return steps.reduce(applyStep, source);
|
|
446
607
|
}
|
|
447
|
-
export {
|
|
608
|
+
export { zoomTo as C, cropBox as D, scaleQuad as E, zoomLimits as S, estimateOutputSize as T, panBy as _, orientationSteps as a, setRotation as b, applyThreshold as c, createView as d, displayedSize as f, overflows as g, normalizeRotation as h, mirrorOrientation as i, prepareThreshold as l, frameToViewport as m, applySteps as n, DEFAULT_SENSITIVITY as o, fitView as p, isToneStep as r, DEFAULT_SOFTNESS as s, UPRIGHT as t, adjustPixels as u, resizeView as v, estimateAspectRatio as w, viewportToFrame as x, rotateView as y };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** An RGBA image in memory, with the same layout as `ImageData`. */
|
|
2
|
+
interface PixelBuffer {
|
|
3
|
+
/** Four bytes per pixel, row by row, starting at the top left corner. */
|
|
4
|
+
data: Uint8ClampedArray;
|
|
5
|
+
height: number;
|
|
6
|
+
width: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A width and a height in pixels.
|
|
10
|
+
*
|
|
11
|
+
* @see https://paperpic.dev/reference/types#size
|
|
12
|
+
*/
|
|
13
|
+
interface Size {
|
|
14
|
+
height: number;
|
|
15
|
+
width: number;
|
|
16
|
+
}
|
|
17
|
+
/** A position in image pixels. */
|
|
18
|
+
interface Point {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
}
|
|
22
|
+
/** A rectangle as fractions of the image, from 0 to 1, so it means the same at any resolution. */
|
|
23
|
+
interface NormalizedRect {
|
|
24
|
+
height: number;
|
|
25
|
+
width: number;
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
}
|
|
29
|
+
/** Four corners, in order: top left, top right, bottom right, bottom left. */
|
|
30
|
+
type Quad = readonly [Point, Point, Point, Point];
|
|
31
|
+
export { Size as i, PixelBuffer as n, Quad as r, NormalizedRect as t };
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,34 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
interface PixelBuffer {
|
|
3
|
-
/** Four bytes per pixel, row by row, starting at the top left corner. */
|
|
4
|
-
data: Uint8ClampedArray;
|
|
5
|
-
height: number;
|
|
6
|
-
width: number;
|
|
7
|
-
}
|
|
8
|
-
/** A position in image pixels. */
|
|
9
|
-
interface Point {
|
|
10
|
-
x: number;
|
|
11
|
-
y: number;
|
|
12
|
-
}
|
|
13
|
-
/** A rectangle as fractions of the image, from 0 to 1, so it means the same at any resolution. */
|
|
14
|
-
interface NormalizedRect {
|
|
15
|
-
height: number;
|
|
16
|
-
width: number;
|
|
17
|
-
x: number;
|
|
18
|
-
y: number;
|
|
19
|
-
}
|
|
20
|
-
/** Four corners, in order: top left, top right, bottom right, bottom left. */
|
|
21
|
-
type Quad = readonly [Point, Point, Point, Point];
|
|
1
|
+
import { n as PixelBuffer, r as Quad, t as NormalizedRect } from "./types-C3ZHKId1.js";
|
|
22
2
|
/** A clockwise rotation that changes the image. */
|
|
23
3
|
type QuarterTurn = 90 | 180 | 270;
|
|
24
4
|
/** Changes the brightness and contrast, each from -1 to 1, where 0 leaves the image as it is. */
|
|
25
|
-
interface
|
|
5
|
+
interface AdjustStep {
|
|
26
6
|
brightness: number;
|
|
27
7
|
contrast: number;
|
|
28
8
|
type: 'adjust';
|
|
29
9
|
}
|
|
30
10
|
/** Straightens the area inside `quad`, whose corners are fractions of the image, into these proportions. */
|
|
31
|
-
interface
|
|
11
|
+
interface PerspectiveStep {
|
|
32
12
|
/** The proportions of the result, as width divided by height. */
|
|
33
13
|
aspectRatio: number;
|
|
34
14
|
quad: Quad;
|
|
@@ -41,7 +21,7 @@ interface ThresholdStep {
|
|
|
41
21
|
type: 'threshold';
|
|
42
22
|
}
|
|
43
23
|
/** A step that changes the tones and not the shape. It always runs after the others. */
|
|
44
|
-
type ToneStep =
|
|
24
|
+
type ToneStep = AdjustStep | ThresholdStep;
|
|
45
25
|
/** One thing the pipeline does to the pixels. */
|
|
46
26
|
type PipelineStep = {
|
|
47
27
|
rotation: QuarterTurn;
|
|
@@ -53,52 +33,24 @@ type PipelineStep = {
|
|
|
53
33
|
} | {
|
|
54
34
|
rect: NormalizedRect;
|
|
55
35
|
type: 'crop';
|
|
56
|
-
} |
|
|
57
|
-
|
|
58
|
-
/** Side of the local window, as a fraction of the shorter image side. Larger than the image is fine. */
|
|
59
|
-
windowRatio?: number;
|
|
60
|
-
}
|
|
61
|
-
interface ApplyThresholdOptions {
|
|
62
|
-
/** From 0 to 1. Higher values darken more pixels, which recovers faint text. */
|
|
63
|
-
sensitivity?: number;
|
|
64
|
-
/**
|
|
65
|
-
* From 0 to 1. How much gray the output keeps around the limit, which makes letters easier to read.
|
|
66
|
-
* Use 0 for strict black and white, which gives smaller files but breaks thin strokes.
|
|
67
|
-
*/
|
|
68
|
-
softness?: number;
|
|
69
|
-
}
|
|
70
|
-
type ThresholdOptions = PrepareThresholdOptions & ApplyThresholdOptions;
|
|
71
|
-
/** The expensive part of a threshold, which sensitivity and softness do not change, so a slider can reuse it. */
|
|
36
|
+
} | PerspectiveStep | ToneStep;
|
|
37
|
+
/** The expensive part of a threshold, which sensitivity and softness do not change, so a ruler can reuse it. */
|
|
72
38
|
interface PreparedThreshold {
|
|
73
39
|
height: number;
|
|
74
40
|
/** One byte per pixel: its brightness relative to the local mean, multiplied by `RATIO_SCALE`. */
|
|
75
41
|
ratios: Uint8ClampedArray;
|
|
76
42
|
width: number;
|
|
77
43
|
}
|
|
78
|
-
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
steps: readonly PipelineStep[];
|
|
84
|
-
} | {
|
|
44
|
+
/**
|
|
45
|
+
* A job for the processing worker: run the steps, or run them and prepare a threshold on the result. The pixel buffer
|
|
46
|
+
* is transferred, so the sender loses access to it.
|
|
47
|
+
*/
|
|
48
|
+
export interface WorkerRequest {
|
|
85
49
|
id: number;
|
|
86
|
-
operation: 'prepare';
|
|
50
|
+
operation: 'pipeline' | 'prepare';
|
|
87
51
|
source: PixelBuffer;
|
|
88
52
|
steps: readonly PipelineStep[];
|
|
89
|
-
}
|
|
90
|
-
id: number;
|
|
91
|
-
operation: 'threshold';
|
|
92
|
-
options?: ThresholdOptions;
|
|
93
|
-
source: PixelBuffer;
|
|
94
|
-
} | {
|
|
95
|
-
height: number;
|
|
96
|
-
id: number;
|
|
97
|
-
operation: 'warp';
|
|
98
|
-
quad: Quad;
|
|
99
|
-
source: PixelBuffer;
|
|
100
|
-
width: number;
|
|
101
|
-
};
|
|
53
|
+
}
|
|
102
54
|
/**
|
|
103
55
|
* What the worker sends: that it loaded and can take jobs, or the answer to a job, with the time the operation took.
|
|
104
56
|
*/
|
package/dist/worker.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
// src/
|
|
3
|
-
function run(request) {
|
|
4
|
-
switch (request.operation) {
|
|
5
|
-
case "pipeline": return applySteps(request.source, request.steps);
|
|
6
|
-
case "threshold": return adaptiveThreshold(request.source, request.options);
|
|
7
|
-
case "warp": return warpPerspective(request.source, request.quad, request.width, request.height);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
1
|
+
import { l as prepareThreshold, n as applySteps } from "./steps-DuGiMvk0.js";
|
|
2
|
+
// src/pipeline/worker.ts
|
|
10
3
|
const scope = globalThis;
|
|
11
4
|
scope.onmessage = ({ data: request }) => {
|
|
12
5
|
try {
|
|
@@ -20,7 +13,7 @@ scope.onmessage = ({ data: request }) => {
|
|
|
20
13
|
}, [prepared.ratios.buffer]);
|
|
21
14
|
return;
|
|
22
15
|
}
|
|
23
|
-
const result =
|
|
16
|
+
const result = applySteps(request.source, request.steps);
|
|
24
17
|
const milliseconds = performance.now() - start;
|
|
25
18
|
scope.postMessage({
|
|
26
19
|
id: request.id,
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paperpic/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"description": "Document image viewer and editor for the browser: straighten, crop, rotate and clean up phone photos, scans and any image with text. Use it with plain JavaScript or through a PaperPic wrapper.",
|
|
6
6
|
"author": "Marco Caparrós <hello@paperpic.dev>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://paperpic.dev",
|