@u1f992/pdfdiff 0.0.1 → 0.2.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/.github/workflows/gh-pages.yml +60 -0
- package/.github/workflows/publish.yml +34 -0
- package/.vscode/extensions.json +3 -0
- package/.vscode/settings.json +20 -0
- package/LICENSE +674 -0
- package/README.md +24 -45
- package/dist/browser.d.ts +2 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +3690 -0
- package/dist/browser.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +39856 -0
- package/dist/cli.js.map +1 -0
- package/dist/coi-serviceworker.min.js +2 -0
- package/dist/diff.d.ts +15 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/image.d.ts +13 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.html +72 -0
- package/dist/index.js +3554 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/iterable.d.ts +2 -0
- package/dist/iterable.d.ts.map +1 -0
- package/dist/iterable.test.d.ts +2 -0
- package/dist/iterable.test.d.ts.map +1 -0
- package/dist/jimp.d.ts +6 -0
- package/dist/jimp.d.ts.map +1 -0
- package/dist/mupdf-wasm.wasm +0 -0
- package/dist/pdf.d.ts +5 -0
- package/dist/pdf.d.ts.map +1 -0
- package/dist/rgba-color.d.ts +4 -0
- package/dist/rgba-color.d.ts.map +1 -0
- package/dist/rgba-color.test.d.ts +2 -0
- package/dist/rgba-color.test.d.ts.map +1 -0
- package/dist/style.css +19 -0
- package/dist/worker.d.ts +42 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +3515 -0
- package/dist/worker.js.map +1 -0
- package/package.json +45 -7
- package/prettier.config.js +3 -0
- package/prototyping/README.md +1 -0
- package/prototyping/flat-map-concurrency.js +218 -0
- package/prototyping/worker.js +10 -0
- package/rollup.config.js +108 -0
- package/src/browser.ts +196 -0
- package/src/cli.ts +195 -0
- package/src/diff.ts +85 -0
- package/src/image.ts +150 -0
- package/src/index.html +72 -0
- package/src/index.test.ts +97 -0
- package/src/index.ts +275 -0
- package/src/iterable.test.ts +40 -0
- package/src/iterable.ts +24 -0
- package/src/jimp.ts +15 -0
- package/src/pdf.ts +74 -0
- package/src/rgba-color.test.ts +43 -0
- package/src/rgba-color.ts +63 -0
- package/src/style.css +19 -0
- package/src/worker.ts +159 -0
- package/test/a.pdf +0 -0
- package/test/b.pdf +0 -0
- package/test/base.xcf +0 -0
- package/test/expected.png +0 -0
- package/test/mask.pdf +0 -0
- package/tsconfig.json +50 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import * as jimp from "jimp";
|
|
19
|
+
import * as mupdf from "mupdf";
|
|
20
|
+
import Worker from "web-worker";
|
|
21
|
+
|
|
22
|
+
import { type Pallet } from "./diff.ts";
|
|
23
|
+
import { isValidAlignStrategy, type AlignStrategy } from "./image.ts";
|
|
24
|
+
import { withIndex } from "./iterable.ts";
|
|
25
|
+
import { parseHex, formatHex } from "./rgba-color.ts";
|
|
26
|
+
import type { JimpInstance } from "./jimp.ts";
|
|
27
|
+
import type {
|
|
28
|
+
InitMessage,
|
|
29
|
+
PageMessage,
|
|
30
|
+
PageResultMessage,
|
|
31
|
+
ReadyMessage,
|
|
32
|
+
} from "./worker.ts";
|
|
33
|
+
|
|
34
|
+
export { withIndex, isValidAlignStrategy, parseHex, formatHex };
|
|
35
|
+
|
|
36
|
+
type Options = {
|
|
37
|
+
dpi: number;
|
|
38
|
+
alpha: boolean;
|
|
39
|
+
mask: Uint8Array | undefined;
|
|
40
|
+
align: AlignStrategy;
|
|
41
|
+
pallet: Pallet;
|
|
42
|
+
workers: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type Result = {
|
|
46
|
+
a: JimpInstance;
|
|
47
|
+
b: JimpInstance;
|
|
48
|
+
diff: JimpInstance;
|
|
49
|
+
addition: [number, number][];
|
|
50
|
+
deletion: [number, number][];
|
|
51
|
+
modification: [number, number][];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const defaultOptions: Options = {
|
|
55
|
+
dpi: 150,
|
|
56
|
+
alpha: true,
|
|
57
|
+
mask: undefined,
|
|
58
|
+
align: "resize",
|
|
59
|
+
pallet: {
|
|
60
|
+
addition: [0x4c, 0xae, 0x4f, 0xff],
|
|
61
|
+
deletion: [0xff, 0x57, 0x24, 0xff],
|
|
62
|
+
modification: [0xff, 0xc1, 0x05, 0xff],
|
|
63
|
+
},
|
|
64
|
+
workers: 1,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function asSharedBytes(bytes: Uint8Array): Uint8Array {
|
|
68
|
+
const isNode =
|
|
69
|
+
typeof globalThis.process !== "undefined" &&
|
|
70
|
+
!!globalThis.process.versions?.node;
|
|
71
|
+
const coiOk =
|
|
72
|
+
(globalThis as { crossOriginIsolated?: boolean }).crossOriginIsolated ===
|
|
73
|
+
true;
|
|
74
|
+
if (typeof SharedArrayBuffer !== "undefined" && (isNode || coiOk)) {
|
|
75
|
+
const sab = new SharedArrayBuffer(bytes.byteLength);
|
|
76
|
+
const view = new Uint8Array(sab);
|
|
77
|
+
view.set(bytes);
|
|
78
|
+
return view;
|
|
79
|
+
}
|
|
80
|
+
return new Uint8Array(bytes);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class WorkerHandle {
|
|
84
|
+
worker: InstanceType<typeof Worker>;
|
|
85
|
+
private pendingResolve:
|
|
86
|
+
| ((data: ReadyMessage | PageResultMessage) => void)
|
|
87
|
+
| null = null;
|
|
88
|
+
private pendingReject: ((reason: unknown) => void) | null = null;
|
|
89
|
+
|
|
90
|
+
constructor(url: URL) {
|
|
91
|
+
this.worker = new Worker(url, { type: "module" });
|
|
92
|
+
this.worker.addEventListener(
|
|
93
|
+
"message",
|
|
94
|
+
(e: MessageEvent<ReadyMessage | PageResultMessage>) => {
|
|
95
|
+
const resolve = this.pendingResolve;
|
|
96
|
+
this.pendingResolve = null;
|
|
97
|
+
this.pendingReject = null;
|
|
98
|
+
resolve?.(e.data);
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
this.worker.addEventListener("error", (e: ErrorEvent) => {
|
|
102
|
+
const reject = this.pendingReject;
|
|
103
|
+
this.pendingResolve = null;
|
|
104
|
+
this.pendingReject = null;
|
|
105
|
+
reject?.(e.error ?? new Error(e.message));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
init(msg: InitMessage): Promise<ReadyMessage> {
|
|
110
|
+
return new Promise<ReadyMessage>((resolve, reject) => {
|
|
111
|
+
this.pendingResolve = resolve as (
|
|
112
|
+
data: ReadyMessage | PageResultMessage,
|
|
113
|
+
) => void;
|
|
114
|
+
this.pendingReject = reject;
|
|
115
|
+
this.worker.postMessage(msg);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
processPage(index: number): Promise<PageResultMessage> {
|
|
120
|
+
return new Promise<PageResultMessage>((resolve, reject) => {
|
|
121
|
+
this.pendingResolve = resolve as (
|
|
122
|
+
data: ReadyMessage | PageResultMessage,
|
|
123
|
+
) => void;
|
|
124
|
+
this.pendingReject = reject;
|
|
125
|
+
const msg: PageMessage = { type: "page", index };
|
|
126
|
+
this.worker.postMessage(msg);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
terminate() {
|
|
131
|
+
this.worker.terminate();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function workerUrl(): URL {
|
|
136
|
+
return new URL(
|
|
137
|
+
import.meta.url.endsWith(".ts") ? "./worker.ts" : "./worker.js",
|
|
138
|
+
import.meta.url,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function pageResultToResult(msg: PageResultMessage): Result {
|
|
143
|
+
return {
|
|
144
|
+
a: jimp.Jimp.fromBitmap({
|
|
145
|
+
width: msg.a.width,
|
|
146
|
+
height: msg.a.height,
|
|
147
|
+
data: new Uint8Array(msg.a.data),
|
|
148
|
+
}) as JimpInstance,
|
|
149
|
+
b: jimp.Jimp.fromBitmap({
|
|
150
|
+
width: msg.b.width,
|
|
151
|
+
height: msg.b.height,
|
|
152
|
+
data: new Uint8Array(msg.b.data),
|
|
153
|
+
}) as JimpInstance,
|
|
154
|
+
diff: jimp.Jimp.fromBitmap({
|
|
155
|
+
width: msg.diff.width,
|
|
156
|
+
height: msg.diff.height,
|
|
157
|
+
data: new Uint8Array(msg.diff.data),
|
|
158
|
+
}) as JimpInstance,
|
|
159
|
+
addition: msg.addition,
|
|
160
|
+
deletion: msg.deletion,
|
|
161
|
+
modification: msg.modification,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export async function* visualizeDifferences(
|
|
166
|
+
a: Uint8Array,
|
|
167
|
+
b: Uint8Array,
|
|
168
|
+
options: Partial<Omit<Options, "pallet"> & { pallet: Partial<Pallet> }>,
|
|
169
|
+
) {
|
|
170
|
+
const merged = {
|
|
171
|
+
dpi: options?.dpi ?? defaultOptions.dpi,
|
|
172
|
+
alpha: options?.alpha ?? defaultOptions.alpha,
|
|
173
|
+
mask: options?.mask ?? defaultOptions.mask,
|
|
174
|
+
align: options?.align ?? defaultOptions.align,
|
|
175
|
+
pallet: {
|
|
176
|
+
addition: options?.pallet?.addition ?? defaultOptions.pallet.addition,
|
|
177
|
+
deletion: options?.pallet?.deletion ?? defaultOptions.pallet.deletion,
|
|
178
|
+
modification:
|
|
179
|
+
options?.pallet?.modification ?? defaultOptions.pallet.modification,
|
|
180
|
+
},
|
|
181
|
+
workers: options?.workers ?? defaultOptions.workers,
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const probe = mupdf.PDFDocument.openDocument(a, "application/pdf");
|
|
185
|
+
const probeB = mupdf.PDFDocument.openDocument(b, "application/pdf");
|
|
186
|
+
const probeMask =
|
|
187
|
+
typeof merged.mask !== "undefined"
|
|
188
|
+
? mupdf.PDFDocument.openDocument(merged.mask, "application/pdf")
|
|
189
|
+
: new mupdf.PDFDocument();
|
|
190
|
+
const maxPages = Math.max(
|
|
191
|
+
probe.countPages(),
|
|
192
|
+
probeB.countPages(),
|
|
193
|
+
probeMask.countPages(),
|
|
194
|
+
);
|
|
195
|
+
probe.destroy();
|
|
196
|
+
probeB.destroy();
|
|
197
|
+
probeMask.destroy();
|
|
198
|
+
|
|
199
|
+
if (maxPages === 0) return;
|
|
200
|
+
|
|
201
|
+
const aBytes = asSharedBytes(a);
|
|
202
|
+
const bBytes = asSharedBytes(b);
|
|
203
|
+
const maskBytes =
|
|
204
|
+
typeof merged.mask !== "undefined" ? asSharedBytes(merged.mask) : null;
|
|
205
|
+
|
|
206
|
+
const initMsg: InitMessage = {
|
|
207
|
+
type: "init",
|
|
208
|
+
aBytes,
|
|
209
|
+
bBytes,
|
|
210
|
+
maskBytes,
|
|
211
|
+
dpi: merged.dpi,
|
|
212
|
+
alpha: merged.alpha,
|
|
213
|
+
pallet: merged.pallet,
|
|
214
|
+
align: merged.align,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const N = Math.max(1, Math.min(merged.workers, maxPages));
|
|
218
|
+
const url = workerUrl();
|
|
219
|
+
const worker0 = new WorkerHandle(url);
|
|
220
|
+
await worker0.init(initMsg);
|
|
221
|
+
|
|
222
|
+
const buffered = new Map<number, Result>();
|
|
223
|
+
let nextToAssign = 0;
|
|
224
|
+
|
|
225
|
+
const workers: WorkerHandle[] = [worker0];
|
|
226
|
+
for (let i = 1; i < N; i++) {
|
|
227
|
+
const w = new WorkerHandle(url);
|
|
228
|
+
await w.init(initMsg);
|
|
229
|
+
workers.push(w);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const resolvers = new Map<number, (r: Result) => void>();
|
|
233
|
+
let workerError: unknown = null;
|
|
234
|
+
|
|
235
|
+
const loops = workers.map(async (w) => {
|
|
236
|
+
while (nextToAssign < maxPages && workerError === null) {
|
|
237
|
+
const idx = nextToAssign++;
|
|
238
|
+
try {
|
|
239
|
+
const msg = await w.processPage(idx);
|
|
240
|
+
const result = pageResultToResult(msg);
|
|
241
|
+
const resolve = resolvers.get(idx);
|
|
242
|
+
if (resolve) {
|
|
243
|
+
resolvers.delete(idx);
|
|
244
|
+
resolve(result);
|
|
245
|
+
} else {
|
|
246
|
+
buffered.set(idx, result);
|
|
247
|
+
}
|
|
248
|
+
} catch (e) {
|
|
249
|
+
workerError = e;
|
|
250
|
+
for (const [, resolve] of resolvers) resolve(null as never);
|
|
251
|
+
resolvers.clear();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
for (let i = 0; i < maxPages; i++) {
|
|
259
|
+
if (workerError !== null) throw workerError;
|
|
260
|
+
let r: Result;
|
|
261
|
+
const buf = buffered.get(i);
|
|
262
|
+
if (buf !== undefined) {
|
|
263
|
+
buffered.delete(i);
|
|
264
|
+
r = buf;
|
|
265
|
+
} else {
|
|
266
|
+
r = await new Promise<Result>((resolve) => resolvers.set(i, resolve));
|
|
267
|
+
if (workerError !== null) throw workerError;
|
|
268
|
+
}
|
|
269
|
+
yield r;
|
|
270
|
+
}
|
|
271
|
+
await Promise.all(loops);
|
|
272
|
+
} finally {
|
|
273
|
+
for (const w of workers) w.terminate();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import assert from "assert";
|
|
19
|
+
import test from "node:test";
|
|
20
|
+
|
|
21
|
+
import { withIndex } from "./iterable.ts";
|
|
22
|
+
|
|
23
|
+
test("withIndex", async () => {
|
|
24
|
+
assert.deepStrictEqual(
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
await Array.fromAsync(
|
|
27
|
+
withIndex(
|
|
28
|
+
(async function* () {
|
|
29
|
+
yield "a";
|
|
30
|
+
yield "b";
|
|
31
|
+
})(),
|
|
32
|
+
0,
|
|
33
|
+
),
|
|
34
|
+
),
|
|
35
|
+
[
|
|
36
|
+
[0, "a"],
|
|
37
|
+
[1, "b"],
|
|
38
|
+
],
|
|
39
|
+
);
|
|
40
|
+
});
|
package/src/iterable.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export async function* withIndex<T>(iter: AsyncIterable<T>, start = 0) {
|
|
19
|
+
let index = start;
|
|
20
|
+
for await (const item of iter) {
|
|
21
|
+
yield [index, item] as [number, T];
|
|
22
|
+
index++;
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/jimp.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as jimp from "jimp";
|
|
2
|
+
|
|
3
|
+
export type JimpInstance = Pick<
|
|
4
|
+
jimp.JimpInstance,
|
|
5
|
+
| "width"
|
|
6
|
+
| "height"
|
|
7
|
+
| "bitmap"
|
|
8
|
+
| "getPixelColor"
|
|
9
|
+
| "setPixelColor"
|
|
10
|
+
| "resize"
|
|
11
|
+
| "composite"
|
|
12
|
+
> & {
|
|
13
|
+
getBuffer: (mime: "image/png") => ReturnType<jimp.JimpInstance["getBuffer"]>;
|
|
14
|
+
getBase64: (mime: "image/png") => ReturnType<jimp.JimpInstance["getBase64"]>;
|
|
15
|
+
};
|
package/src/pdf.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import * as jimp from "jimp";
|
|
19
|
+
import * as mupdf from "mupdf";
|
|
20
|
+
|
|
21
|
+
import type { JimpInstance } from "./jimp.ts";
|
|
22
|
+
|
|
23
|
+
export function* loadPages(pdf: mupdf.Document) {
|
|
24
|
+
for (let i = 0; i < pdf.countPages(); i++) {
|
|
25
|
+
yield pdf.loadPage(i);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pixmapToRGBA(pixmap: mupdf.Pixmap): Uint8Array {
|
|
30
|
+
const width = pixmap.getWidth();
|
|
31
|
+
const height = pixmap.getHeight();
|
|
32
|
+
const stride = pixmap.getStride();
|
|
33
|
+
const hasAlpha = pixmap.getAlpha() !== 0;
|
|
34
|
+
const samples = pixmap.getPixels();
|
|
35
|
+
|
|
36
|
+
if (hasAlpha && stride === width * 4) {
|
|
37
|
+
return new Uint8Array(samples);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const out = new Uint8Array(width * height * 4);
|
|
41
|
+
const srcBpp = pixmap.getNumberOfComponents() + (hasAlpha ? 1 : 0);
|
|
42
|
+
for (let y = 0; y < height; y++) {
|
|
43
|
+
const srcRow = y * stride;
|
|
44
|
+
const dstRow = y * width * 4;
|
|
45
|
+
for (let x = 0; x < width; x++) {
|
|
46
|
+
const s = srcRow + x * srcBpp;
|
|
47
|
+
const d = dstRow + x * 4;
|
|
48
|
+
out[d] = samples[s]!;
|
|
49
|
+
out[d + 1] = samples[s + 1]!;
|
|
50
|
+
out[d + 2] = samples[s + 2]!;
|
|
51
|
+
out[d + 3] = hasAlpha ? samples[s + 3]! : 255;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function pageToImage(
|
|
58
|
+
page: mupdf.Page,
|
|
59
|
+
dpi: number,
|
|
60
|
+
alpha: boolean,
|
|
61
|
+
) {
|
|
62
|
+
const zoom = dpi / 72;
|
|
63
|
+
const pixmap = page.toPixmap(
|
|
64
|
+
[zoom, 0, 0, zoom, 0, 0],
|
|
65
|
+
mupdf.ColorSpace.DeviceRGB,
|
|
66
|
+
alpha,
|
|
67
|
+
);
|
|
68
|
+
const width = pixmap.getWidth();
|
|
69
|
+
const height = pixmap.getHeight();
|
|
70
|
+
const data = pixmapToRGBA(pixmap);
|
|
71
|
+
pixmap.destroy();
|
|
72
|
+
page.destroy();
|
|
73
|
+
return jimp.Jimp.fromBitmap({ width, height, data }) as JimpInstance;
|
|
74
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import assert from "assert";
|
|
19
|
+
import test from "node:test";
|
|
20
|
+
|
|
21
|
+
import { parseHex, formatHex } from "./rgba-color.ts";
|
|
22
|
+
|
|
23
|
+
test("parseHex", async (ctx) => {
|
|
24
|
+
await ctx.test("#rgb", () => {
|
|
25
|
+
assert.deepStrictEqual(parseHex("#fed"), [0xff, 0xee, 0xdd, 0xff]);
|
|
26
|
+
});
|
|
27
|
+
await ctx.test("#rrggbb", () => {
|
|
28
|
+
assert.deepStrictEqual(parseHex("#fffefd"), [0xff, 0xfe, 0xfd, 0xff]);
|
|
29
|
+
});
|
|
30
|
+
await ctx.test("#rgba", () => {
|
|
31
|
+
assert.deepStrictEqual(parseHex("#fedc"), [0xff, 0xee, 0xdd, 0xcc]);
|
|
32
|
+
});
|
|
33
|
+
await ctx.test("#rrggbbaa", () => {
|
|
34
|
+
assert.deepStrictEqual(parseHex("#fffefdfc"), [0xff, 0xfe, 0xfd, 0xfc]);
|
|
35
|
+
});
|
|
36
|
+
await ctx.test("invalid", () => {
|
|
37
|
+
assert.deepStrictEqual(parseHex("foobar"), null);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("formatHex", () => {
|
|
42
|
+
assert.deepStrictEqual(formatHex([0xff, 0xfe, 0xfd, 0xfc]), "#fffefdfc");
|
|
43
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 Koutaro Mukai
|
|
3
|
+
*
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
* (at your option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type RGBAColor = [number, number, number, number];
|
|
19
|
+
|
|
20
|
+
export const parseHex = (hex: string) => {
|
|
21
|
+
if (/^#([0-9a-fA-F]{3})$/.test(hex)) {
|
|
22
|
+
return [
|
|
23
|
+
parseInt(hex[1]! + hex[1]!, 16),
|
|
24
|
+
parseInt(hex[2]! + hex[2]!, 16),
|
|
25
|
+
parseInt(hex[3]! + hex[3]!, 16),
|
|
26
|
+
255,
|
|
27
|
+
] as RGBAColor;
|
|
28
|
+
}
|
|
29
|
+
if (/^#([0-9a-fA-F]{4})$/.test(hex)) {
|
|
30
|
+
return [
|
|
31
|
+
parseInt(hex[1]! + hex[1]!, 16),
|
|
32
|
+
parseInt(hex[2]! + hex[2]!, 16),
|
|
33
|
+
parseInt(hex[3]! + hex[3]!, 16),
|
|
34
|
+
parseInt(hex[4]! + hex[4]!, 16),
|
|
35
|
+
] as RGBAColor;
|
|
36
|
+
}
|
|
37
|
+
if (/^#([0-9a-fA-F]{6})$/.test(hex)) {
|
|
38
|
+
return [
|
|
39
|
+
parseInt(hex.slice(1, 3), 16),
|
|
40
|
+
parseInt(hex.slice(3, 5), 16),
|
|
41
|
+
parseInt(hex.slice(5, 7), 16),
|
|
42
|
+
255,
|
|
43
|
+
] as RGBAColor;
|
|
44
|
+
}
|
|
45
|
+
if (/^#([0-9a-fA-F]{8})$/.test(hex)) {
|
|
46
|
+
return [
|
|
47
|
+
parseInt(hex.slice(1, 3), 16),
|
|
48
|
+
parseInt(hex.slice(3, 5), 16),
|
|
49
|
+
parseInt(hex.slice(5, 7), 16),
|
|
50
|
+
parseInt(hex.slice(7, 9), 16),
|
|
51
|
+
] as RGBAColor;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const formatHex = ([r, g, b, a]: RGBAColor) =>
|
|
57
|
+
"#" +
|
|
58
|
+
[r, g, b, a]
|
|
59
|
+
.map((v) => {
|
|
60
|
+
const hex = v.toString(16);
|
|
61
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
62
|
+
})
|
|
63
|
+
.join("");
|
package/src/style.css
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.summary-content {
|
|
2
|
+
display: inline-flex;
|
|
3
|
+
align-items: baseline;
|
|
4
|
+
gap: 20px;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.checkerboard-bg {
|
|
8
|
+
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAAAAABWESUoAAAAHUlEQVQ4y2P4gQYeoAGGUQUjSgG6ALqGUQUjSgEAdjWwLh+tpFgAAAAASUVORK5CYII=");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.diff-table {
|
|
12
|
+
border: 1px solid black;
|
|
13
|
+
border-collapse: collapse;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.diff-table th,
|
|
17
|
+
.diff-table td {
|
|
18
|
+
border: 1px solid black;
|
|
19
|
+
}
|