From a2d03d8b2e00bd8d2686e84e6fd2b1009f7b86e2 Mon Sep 17 00:00:00 2001 From: John Wooten <60260750+JFWooten4@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:44:49 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20Detect=20tiled=20scanned=20pages?= =?UTF-8?q?=20in=20PDF=20dark=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Count the combined on-page coverage of image tiles before restoring their original colors. This allows black-and-white scanned documents split into smaller images to darken as intended, while preserving ordinary illustrations. Add targeted regression coverage. Co-authored-by: ChatGPT 6 --- scripts/test-suites.mjs | 3 +- src/viewer/theme/scanned-page-images.js | 61 +++++++++++++++++++++++++ src/viewer/viewer.js | 27 +---------- tests/scanned-page-images.test.mjs | 39 ++++++++++++++++ 4 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 src/viewer/theme/scanned-page-images.js create mode 100644 tests/scanned-page-images.test.mjs diff --git a/scripts/test-suites.mjs b/scripts/test-suites.mjs index 7cfeba1..6218a72 100644 --- a/scripts/test-suites.mjs +++ b/scripts/test-suites.mjs @@ -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", diff --git a/src/viewer/theme/scanned-page-images.js b/src/viewer/theme/scanned-page-images.js new file mode 100644 index 0000000..45caeb9 --- /dev/null +++ b/src/viewer/theme/scanned-page-images.js @@ -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; +} diff --git a/src/viewer/viewer.js b/src/viewer/viewer.js index 663883e..77565c4 100644 --- a/src/viewer/viewer.js +++ b/src/viewer/viewer.js @@ -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"; @@ -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"], @@ -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)); } diff --git a/tests/scanned-page-images.test.mjs b/tests/scanned-page-images.test.mjs new file mode 100644 index 0000000..3c53669 --- /dev/null +++ b/tests/scanned-page-images.test.mjs @@ -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); +});