From d1926fb1791150640d818cb09819f4fe6a60c205 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 10 Jun 2026 18:27:50 +0200 Subject: [PATCH] Use the `convertBlackAndWhiteToRGBA` helper with grayscale images in `putBinaryImageData` This removes a little bit of code duplication, which only exist since the `src/display/canvas.js` code pre-dates the helper function by many years. *Note:* Given that `OffscreenCanvas` is enabled by default there's currently not a lot of test coverage for this code-path, hence the added browser-test. --- src/display/canvas.js | 46 +++++++---------------------------------- test/test_manifest.json | 8 +++++++ 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/display/canvas.js b/src/display/canvas.js index 9e7ddd334..8561a4939 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -349,48 +349,16 @@ function putBinaryImageData(ctx, imgData) { // imgData.kind tells us which one this is. if (imgData.kind === ImageKind.GRAYSCALE_1BPP) { // Grayscale, 1 bit per pixel (i.e. black-and-white). - const srcLength = src.byteLength; - const dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2); - const dest32DataLength = dest32.length; - const fullSrcDiff = (width + 7) >> 3; - const white = 0xffffffff; - const black = FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff; - for (i = 0; i < totalChunks; i++) { thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight; - destPos = 0; - for (j = 0; j < thisChunkHeight; j++) { - const srcDiff = srcLength - srcPos; - let k = 0; - const kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7; - const kEndUnrolled = kEnd & ~7; - let mask = 0; - let srcByte = 0; - for (; k < kEndUnrolled; k += 8) { - srcByte = src[srcPos++]; - dest32[destPos++] = srcByte & 128 ? white : black; - dest32[destPos++] = srcByte & 64 ? white : black; - dest32[destPos++] = srcByte & 32 ? white : black; - dest32[destPos++] = srcByte & 16 ? white : black; - dest32[destPos++] = srcByte & 8 ? white : black; - dest32[destPos++] = srcByte & 4 ? white : black; - dest32[destPos++] = srcByte & 2 ? white : black; - dest32[destPos++] = srcByte & 1 ? white : black; - } - for (; k < kEnd; k++) { - if (mask === 0) { - srcByte = src[srcPos++]; - mask = 128; - } - dest32[destPos++] = srcByte & mask ? white : black; - mask >>= 1; - } - } - // We ran out of input. Make all remaining pixels transparent. - while (destPos < dest32DataLength) { - dest32[destPos++] = 0; - } + ({ srcPos } = convertBlackAndWhiteToRGBA({ + src, + srcPos, + dest, + width, + height: thisChunkHeight, + })); ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT); } diff --git a/test/test_manifest.json b/test/test_manifest.json index f44db6344..aef2038d3 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -5389,6 +5389,14 @@ "rounds": 1, "type": "eq" }, + { + "id": "images_1bit_grayscale-disable-isOffscreenCanvasSupported", + "file": "pdfs/images_1bit_grayscale.pdf", + "md5": "e1c36a19563944891bd30cfc0199d07f", + "rounds": 1, + "type": "eq", + "isOffscreenCanvasSupported": false + }, { "id": "html5checker", "file": "pdfs/html5checker.pdf",