@@ -25,66 +25,12 @@ import { addCircularRefsDataTables, LayoutDataTablePage, LayoutPage } from '../o
2525import { addCircularRefsOcr } from '../objects/ocrObjects.js' ;
2626import { PageMetrics } from '../objects/pageMetricsObjects.js' ;
2727import { checkCharWarn , convertOCR } from '../recognizeConvert.js' ;
28+ import { importImageFileToBase64 } from '../utils/imageUtils.js' ;
2829import {
2930 readOcrFile , clearObjectProperties , objectAssignDefined , readTextFile ,
3031} from '../utils/miscUtils.js' ;
3132import { 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 ( / j p e ? 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 ) ;
0 commit comments