All files / src ImageResult.ts

89.47% Statements 17/19
60% Branches 3/5
100% Functions 3/3
89.47% Lines 17/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                            1x 3x 3x   3x 2x 2x 2x 2x 2x 2x 2x   2x   2x   3x 3x 3x 3x                                                                                
import type { ImageKind } from 'pdfjs-dist/legacy/build/pdf.mjs';
 
/**
 * ImageKindKey
 * - Represents the keys of the ImageKind enum (e.g. "GRAYSCALE_1BPP", "RGB_24BPP", "RGBA_32BPP").
 */
export type ImageKindKey = keyof typeof ImageKind;
 
/**
 * ImageKindValue
 * - Represents the numeric values of the ImageKind enum (e.g. 1, 2, 3).
 */
export type ImageKindValue = (typeof ImageKind)[ImageKindKey];
 
export class ImageResult {
	pages: Array<PageImages> = [];
	total: number = 0;
 
	public getPageImage(num: number, name: string): EmbeddedImage | null {
		for (const pageData of this.pages) {
			if (pageData.pageNumber === num) {
				for (const img of pageData.images) {
					if (img.fileName === name) {
						return img;
					}
				}
			}
		}
		return null;
	}
 
	constructor(total: number) {
		this.total = total;
	}
}
 
/**
 * PageImages
 * - Represents all embedded images found on a single PDF page.
 * - pageNumber: 1-based page index.
 * - images: Array of EmbeddedImage objects for this page.
 */
export interface PageImages {
	pageNumber: number;
	images: EmbeddedImage[];
}
 
/**
 * EmbeddedImage
 * - Normalized representation of an embedded image extracted from the PDF.
 * - `data`: Raw image bytes (e.g. PNG/JPEG) as Uint8Array. Use this for file writing or binary processing.
 * - `dataUrl`: Optional data URL (e.g. "data:image/png;base64,...") for directly embedding in <img> src.
 *   Storing both lets consumers choose the most convenient form; consider omitting one to save memory.
 * - `fileName`: Suggested filename or resource name for the image (used when saving to disk).
 * - `width` / `height`: Dimensions in pixels.
 * - `kind`: ImageKindValue from pdfjs-dist indicating the pixel format (e.g. GRAYSCALE_1BPP / RGB_24BPP / RGBA_32BPP).
 */
export interface EmbeddedImage {
	// Raw binary image data (PNG/JPEG) normalized to Uint8Array.
	data: Uint8Array;
 
	// Optional base64 data URL for easy embedding in HTML.
	dataUrl: string;
 
	// Suggested filename or resource identifier for the image.
	fileName: string;
 
	// Image dimensions in pixels.
	width: number;
	height: number;
 
	// Color format as defined by pdfjs ImageKind numeric values.
	kind: ImageKindValue;
}