Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/test-suites.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export const suites = Object.freeze({
],
"appearance": [
"accent-color.test.mjs",
"narrow-viewer-layout.test.mjs"
"narrow-viewer-layout.test.mjs",
"scanned-page-images.test.mjs"
],
"chatgpt": [
"chatgpt-pdf-relay.test.mjs",
Expand Down
61 changes: 61 additions & 0 deletions src/viewer/theme/scanned-page-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// PDF.js imageCoordinates contain normalized top-left, bottom-left, and top-right
// coordinates for each embedded image. Scanned pages may be built from tiles.
function imageAreaFraction(imageCoordinates, offset) {
const topLeftX = imageCoordinates[offset];
const topLeftY = imageCoordinates[offset + 1];
const bottomLeftX = imageCoordinates[offset + 2];
const bottomLeftY = imageCoordinates[offset + 3];
const topRightX = imageCoordinates[offset + 4];
const topRightY = imageCoordinates[offset + 5];
const leftX = bottomLeftX - topLeftX;
const leftY = bottomLeftY - topLeftY;
const topX = topRightX - topLeftX;
const topY = topRightY - topLeftY;

return Math.abs(leftX * topY - leftY * topX);
}

export function hasPageSizedImage(imageCoordinates) {
const SCANNED_PAGE_IMAGE_AREA_THRESHOLD = 0.8;
const PAGE_COVERAGE_SAMPLE_GRID = 20;
const cells = new Uint8Array(PAGE_COVERAGE_SAMPLE_GRID ** 2);
let coveredCount = 0;

for (let offset = 0; offset + 5 < (imageCoordinates?.length || 0); offset += 6) {
const area = imageAreaFraction(imageCoordinates, offset);
if (area >= SCANNED_PAGE_IMAGE_AREA_THRESHOLD) return true;
if (area <= 0) continue;

const x = imageCoordinates[offset];
const y = imageCoordinates[offset + 1];
const vx = imageCoordinates[offset + 2] - x;
const vy = imageCoordinates[offset + 3] - y;
const ux = imageCoordinates[offset + 4] - x;
const uy = imageCoordinates[offset + 5] - y;
const determinant = ux * vy - uy * vx;
if (Math.abs(determinant) < Number.EPSILON) continue;

const minX = Math.max(0, Math.floor(Math.min(x, x + ux, x + vx, x + ux + vx) * PAGE_COVERAGE_SAMPLE_GRID));
const maxX = Math.min(PAGE_COVERAGE_SAMPLE_GRID, Math.ceil(Math.max(x, x + ux, x + vx, x + ux + vx) * PAGE_COVERAGE_SAMPLE_GRID));
const minY = Math.max(0, Math.floor(Math.min(y, y + uy, y + vy, y + uy + vy) * PAGE_COVERAGE_SAMPLE_GRID));
const maxY = Math.min(PAGE_COVERAGE_SAMPLE_GRID, Math.ceil(Math.max(y, y + uy, y + vy, y + uy + vy) * PAGE_COVERAGE_SAMPLE_GRID));

for (let row = minY; row < maxY; row += 1) {
for (let col = minX; col < maxX; col += 1) {
const index = row * PAGE_COVERAGE_SAMPLE_GRID + col;
if (cells[index]) continue;
const dx = (col + 0.5) / PAGE_COVERAGE_SAMPLE_GRID - x;
const dy = (row + 0.5) / PAGE_COVERAGE_SAMPLE_GRID - y;
const u = (dx * vy - dy * vx) / determinant;
const v = (ux * dy - uy * dx) / determinant;
if (u >= 0 && u <= 1 && v >= 0 && v <= 1) {
cells[index] = 1;
coveredCount += 1;
if (coveredCount / cells.length >= SCANNED_PAGE_IMAGE_AREA_THRESHOLD) return true;
}
}
}
}

return false;
}
27 changes: 1 addition & 26 deletions src/viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { installMarkdownCopy, markPdfTextStyles, markPdfTextStructure } from "./
import { highlightTextLayer, registerSearchText } from "./search/search-highlight.js";
import { normalizeSearchText } from "./search/search-text.js";
import { PagePreviews } from "./page-previews.js";
import { hasPageSizedImage } from "./theme/scanned-page-images.js";
import { preparePrintDocument } from "./print-renderer.js";
import { highlightFootnote, renderFootnoteHighlight } from "./navigation/footnote-highlight.js";
import { findPrintedContentsEntries, resolvePrintedContentsPage, contentsEntryHasEmbeddedLink } from "./navigation/printed-contents.js";
Expand Down Expand Up @@ -77,7 +78,6 @@ const LIGHT_MODE_SHARE_ICON = extensionAssetUrl(
"src/assets/copy-page-icon-light.png",
"assets/copy-page-icon-light.png",
);
const SCANNED_PAGE_IMAGE_AREA_THRESHOLD = 0.8;
const RENDER_WINDOW_RADIUS = 3;
const PDF_METADATA_FIELDS = [
["Title", "Title", "dc:title"],
Expand Down Expand Up @@ -144,31 +144,6 @@ const renderedPages = new Set();
const staleRenderedPages = new Set();
const pageTextCache = new Map();

function imageAreaFraction(imageCoordinates, offset) {
const topLeftX = imageCoordinates[offset];
const topLeftY = imageCoordinates[offset + 1];
const bottomLeftX = imageCoordinates[offset + 2];
const bottomLeftY = imageCoordinates[offset + 3];
const topRightX = imageCoordinates[offset + 4];
const topRightY = imageCoordinates[offset + 5];
const leftX = bottomLeftX - topLeftX;
const leftY = bottomLeftY - topLeftY;
const topX = topRightX - topLeftX;
const topY = topRightY - topLeftY;

return Math.abs(leftX * topY - leftY * topX);
}

function hasPageSizedImage(imageCoordinates) {
for (let offset = 0; offset + 5 < imageCoordinates.length; offset += 6) {
if (imageAreaFraction(imageCoordinates, offset) >= SCANNED_PAGE_IMAGE_AREA_THRESHOLD) {
return true;
}
}

return false;
}

function clampUnit(value) {
return Math.min(1, Math.max(0, value));
}
Expand Down
39 changes: 39 additions & 0 deletions tests/scanned-page-images.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import assert from "node:assert/strict";
import test from "node:test";
import { hasPageSizedImage } from "../src/viewer/theme/scanned-page-images.js";

function image(x, y, width, height) {
return [x, y, x, y + height, x + width, y];
}

test("dark mode recognizes a scanned page rendered as one large image", () => {
assert.equal(hasPageSizedImage(image(0, 0, 0.8, 1)), true);
});

test("dark mode recognizes scanned pages split into rectangular image tiles", () => {
// GAO report GGD-92-109 reproducer: white scan tiles must not be restored over inversion.
const tiles = [
...image(0, 0, 0.5, 0.5),
...image(0.5, 0, 0.5, 0.5),
...image(0, 0.5, 0.5, 0.5),
...image(0.5, 0.5, 0.5, 0.5),
];
assert.equal(hasPageSizedImage(tiles), true);
assert.equal(hasPageSizedImage([
...image(0, 0, 0.9, 0.5),
...image(0, 0.5, 0.9, 0.5),
]), true);
});

test("overlapping images do not artificially count as a whole-page scan", () => {
const repeated = Array.from({ length: 4 }, () => image(0, 0, 0.5, 0.5)).flat();
assert.equal(hasPageSizedImage(repeated), false);
});

test("separate illustrations still preserve their original colors", () => {
assert.equal(hasPageSizedImage([
...image(0.05, 0.05, 0.45, 0.5),
...image(0.55, 0.05, 0.4, 0.5),
]), false);
assert.equal(hasPageSizedImage([]), false);
});
Loading