Stirling-PDF/public/functions/splitOn.js

115 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-10-27 02:56:13 +02:00
import { detectEmptyPages } from "./shared/detectEmptyPages.js";
import { getImagesOnPage } from "./shared/getImagesOnPage.js";
2023-10-28 19:30:12 +02:00
import { createSubDocument } from "./shared/createSubDocument.js";
2023-10-27 02:56:13 +02:00
/**
* @typedef {"BAR_CODE"|"QR_CODE"|"BLANK_PAGE"} SplitType
*/
/**
*
* @param {Uint16Array} snapshot
* @param {SplitType} type
* @param {} PDFJS
2023-10-27 02:56:13 +02:00
* @param {import('opencv-wasm')} OpenCV
* @param {} PDFLib
* @returns
*/
2023-10-27 03:14:22 +02:00
export async function splitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PDFLib, jsQR) {
let splitAtPages = [];
switch (type) {
case "BAR_CODE":
// TODO: Implement
2023-10-27 02:56:13 +02:00
throw new Error("This split-type has not been implemented yet");
break;
case "QR_CODE":
2023-10-27 02:56:13 +02:00
splitAtPages = await getPagesWithQRCode(snapshot);
break;
case "BLANK_PAGE":
splitAtPages = await detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV);
break;
default:
throw new Error("An invalid split-type was provided.")
break;
}
console.log("Split At Pages: ", splitAtPages);
2023-10-28 19:30:12 +02:00
// Remove detected Pages & Split
const pdfDoc = await PDFLib.PDFDocument.load(snapshot);
2023-10-28 19:30:12 +02:00
const numberOfPages = pdfDoc.getPages().length;
2023-10-28 19:30:12 +02:00
let pagesArray = [];
let splitAfter = splitAtPages.shift();
const subDocuments = [];
for (let i = 0; i < numberOfPages; i++) {
console.log(i);
if(i == splitAfter) {
if(pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
pagesArray = [];
}
splitAfter = splitAtPages.shift();
}
else { // Skip splitAtPage
console.log("PagesArray")
pagesArray.push(i);
}
}
if(pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
}
pagesArray = [];
return subDocuments;
2023-10-27 02:56:13 +02:00
async function getPagesWithQRCode(snapshot) {
const pdfDoc = await PDFJS.getDocument(snapshot).promise;
const pagesWithQR = [];
2023-10-28 19:30:12 +02:00
for (let i = 0; i < pdfDoc.numPages; i++) {
const page = await pdfDoc.getPage(i + 1);
2023-10-27 02:56:13 +02:00
const images = await getImagesOnPage(page, PDFJS);
for (const image of images) {
const data = await checkForQROnImage(image);
if(data == "https://github.com/Frooodle/Stirling-PDF") {
pagesWithQR.push(i);
}
}
}
return pagesWithQR;
}
async function checkForQROnImage(image) {
2023-10-27 03:14:22 +02:00
// TODO: There is an issue with the jsQR package (The package expects rgba but sometimes we have rgb), and the package seems to be stale, we could create a fork and fix the issue. In the meanwhile we just force rgba:
// Check for rgb and convert to rgba
2023-10-27 02:56:13 +02:00
if(image.data.length == image.width * image.height * 3) {
const tmpArray = new Uint8ClampedArray(image.width * image.height * 4);
// Iterate through the original array and add an alpha channel
for (let i = 0, j = 0; i < image.data.length; i += 3, j += 4) {
tmpArray[j] = image.data[i]; // Red channel
tmpArray[j + 1] = image.data[i + 1]; // Green channel
tmpArray[j + 2] = image.data[i + 2]; // Blue channel
tmpArray[j + 3] = 255; // Alpha channel (fully opaque)
}
image.data = tmpArray;
}
const code = jsQR(image.data, image.width, image.height);
if(code)
return code.data;
else
return null;
}
};