Skip to content

Commit 0fc036e

Browse files
committed
Moved utility function to utils file
1 parent 1e5318a commit 0fc036e

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

js/import/import.js

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,12 @@ import { addCircularRefsDataTables, LayoutDataTablePage, LayoutPage } from '../o
2525
import { addCircularRefsOcr } from '../objects/ocrObjects.js';
2626
import { PageMetrics } from '../objects/pageMetricsObjects.js';
2727
import { checkCharWarn, convertOCR } from '../recognizeConvert.js';
28+
import { importImageFileToBase64 } from '../utils/imageUtils.js';
2829
import {
2930
readOcrFile, clearObjectProperties, objectAssignDefined, readTextFile,
3031
} from '../utils/miscUtils.js';
3132
import { importOCRFiles } from './importOCR.js';
3233

33-
/**
34-
* Automatically detects the image type (jpeg or png).
35-
* @param {Uint8Array} image
36-
* @returns {('jpeg'|'png')}
37-
*/
38-
const detectImageFormat = (image) => {
39-
if (image[0] === 0xFF && image[1] === 0xD8) {
40-
return 'jpeg';
41-
} if (image[0] === 0x89 && image[1] === 0x50) {
42-
return 'png';
43-
}
44-
throw new Error('Unsupported image type');
45-
};
46-
47-
/**
48-
*
49-
* @param {File|FileNode|ArrayBuffer} file
50-
* @returns {Promise<string>}
51-
*/
52-
const importImageFile = async (file) => new Promise((resolve, reject) => {
53-
if (file instanceof ArrayBuffer) {
54-
const imageUint8 = new Uint8Array(file);
55-
const format = detectImageFormat(imageUint8);
56-
const binary = String.fromCharCode(...imageUint8);
57-
resolve(`data:image/${format};base64,${btoa(binary)}`);
58-
return;
59-
}
60-
61-
// The `typeof process` condition is necessary to avoid error in Node.js versions <20, where `File` is not defined.
62-
if (typeof process === 'undefined' && file instanceof File) {
63-
const reader = new FileReader();
64-
65-
reader.onloadend = async () => {
66-
resolve(/** @type {string} */(reader.result));
67-
};
68-
69-
reader.onerror = (error) => {
70-
reject(error);
71-
};
72-
73-
reader.readAsDataURL(file);
74-
return;
75-
}
76-
77-
if (typeof process !== 'undefined') {
78-
if (!file?.name) reject(new Error('Invalid input. Must be a FileNode or ArrayBuffer.'));
79-
const format = file.name.match(/jpe?g$/i) ? 'jpeg' : 'png';
80-
// @ts-ignore
81-
resolve(`data:image/${format};base64,${file.fileData.toString('base64')}`);
82-
return;
83-
}
84-
85-
reject(new Error('Invalid input. Must be a File or ArrayBuffer.'));
86-
});
87-
8834
/**
8935
* Standardize file-like inputs between platforms.
9036
* If run in the browser, URLs are fetched and converted to `File` objects.
@@ -481,7 +427,7 @@ export async function importFiles(files) {
481427
if (inputData.imageMode) {
482428
ImageCache.pageCount = inputData.pageCount;
483429
for (let i = 0; i < inputData.pageCount; i++) {
484-
ImageCache.nativeSrc[i] = await importImageFile(imageFiles[i]).then(async (imgStr) => {
430+
ImageCache.nativeSrc[i] = await importImageFileToBase64(imageFiles[i]).then(async (imgStr) => {
485431
const imgWrapper = new ImageWrapper(i, imgStr, 'native', false, false);
486432
const imageDims = await imageUtils.getDims(imgWrapper);
487433
pageMetricsArr[i] = new PageMetrics(imageDims);

js/utils/imageUtils.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,61 @@ export function imageStrToBlob(imgStr) {
3535
return imgBlob;
3636
}
3737

38+
/**
39+
* Automatically detects the image type (jpeg or png).
40+
* @param {Uint8Array} image
41+
* @returns {('jpeg'|'png')}
42+
*/
43+
const detectImageFormat = (image) => {
44+
if (image[0] === 0xFF && image[1] === 0xD8) {
45+
return 'jpeg';
46+
} if (image[0] === 0x89 && image[1] === 0x50) {
47+
return 'png';
48+
}
49+
throw new Error('Unsupported image type');
50+
};
51+
52+
/**
53+
*
54+
* @param {File|FileNode|ArrayBuffer} file
55+
* @returns {Promise<string>}
56+
*/
57+
export const importImageFileToBase64 = async (file) => new Promise((resolve, reject) => {
58+
if (file instanceof ArrayBuffer) {
59+
const imageUint8 = new Uint8Array(file);
60+
const format = detectImageFormat(imageUint8);
61+
const binary = String.fromCharCode(...imageUint8);
62+
resolve(`data:image/${format};base64,${btoa(binary)}`);
63+
return;
64+
}
65+
66+
// The `typeof process` condition is necessary to avoid error in Node.js versions <20, where `File` is not defined.
67+
if (typeof process === 'undefined' && file instanceof File) {
68+
const reader = new FileReader();
69+
70+
reader.onloadend = async () => {
71+
resolve(/** @type {string} */(reader.result));
72+
};
73+
74+
reader.onerror = (error) => {
75+
reject(error);
76+
};
77+
78+
reader.readAsDataURL(file);
79+
return;
80+
}
81+
82+
if (typeof process !== 'undefined') {
83+
if (!file?.name) reject(new Error('Invalid input. Must be a FileNode or ArrayBuffer.'));
84+
const format = file.name.match(/jpe?g$/i) ? 'jpeg' : 'png';
85+
// @ts-ignore
86+
resolve(`data:image/${format};base64,${file.fileData.toString('base64')}`);
87+
return;
88+
}
89+
90+
reject(new Error('Invalid input. Must be a File or ArrayBuffer.'));
91+
});
92+
3893
/**
3994
* Converts a base64 encoded string to an array of bytes.
4095
*

0 commit comments

Comments
 (0)