|
@@ -10,8 +10,8 @@
|
|
|
|
|
|
import fs from 'fs';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import path from 'path';
|
|
-import { type BufferRet as JpegBufferRet } from 'jpeg-js'; // Only import type here, the actual import is done by LazyImports
|
|
|
|
-import { type PNG } from 'pngjs'; // Only import type here, the actual import is done by LazyImports
|
|
|
|
|
|
+import { type BufferRet as JpegBufferRet } from 'jpeg-js'; // Only import type here, the actual import must be provided by the caller
|
|
|
|
+import { type PNG } from 'pngjs'; // Only import type here, the actual import must be provided by the caller
|
|
|
|
|
|
import { Canvas3D, Canvas3DContext, Canvas3DProps, DefaultCanvas3DParams } from '../../mol-canvas3d/canvas3d';
|
|
import { Canvas3D, Canvas3DContext, Canvas3DProps, DefaultCanvas3DParams } from '../../mol-canvas3d/canvas3d';
|
|
import { ImagePass, ImageProps } from '../../mol-canvas3d/passes/image';
|
|
import { ImagePass, ImageProps } from '../../mol-canvas3d/passes/image';
|
|
@@ -22,16 +22,14 @@ import { AssetManager } from '../../mol-util/assets';
|
|
import { ColorNames } from '../../mol-util/color/names';
|
|
import { ColorNames } from '../../mol-util/color/names';
|
|
import { PixelData } from '../../mol-util/image';
|
|
import { PixelData } from '../../mol-util/image';
|
|
import { InputObserver } from '../../mol-util/input/input-observer';
|
|
import { InputObserver } from '../../mol-util/input/input-observer';
|
|
-import { LazyImports } from '../../mol-util/lazy-imports';
|
|
|
|
import { ParamDefinition } from '../../mol-util/param-definition';
|
|
import { ParamDefinition } from '../../mol-util/param-definition';
|
|
|
|
|
|
|
|
|
|
-const lazyImports = LazyImports.create('gl', 'jpeg-js', 'pngjs') as {
|
|
|
|
|
|
+export interface ExternalModules {
|
|
'gl': typeof import('gl'),
|
|
'gl': typeof import('gl'),
|
|
- 'jpeg-js': typeof import('jpeg-js'),
|
|
|
|
- 'pngjs': typeof import('pngjs'),
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
|
|
+ 'jpeg-js'?: typeof import('jpeg-js'),
|
|
|
|
+ 'pngjs'?: typeof import('pngjs'),
|
|
|
|
+}
|
|
|
|
|
|
export type HeadlessScreenshotHelperOptions = {
|
|
export type HeadlessScreenshotHelperOptions = {
|
|
webgl?: WebGLContextAttributes,
|
|
webgl?: WebGLContextAttributes,
|
|
@@ -51,11 +49,11 @@ export class HeadlessScreenshotHelper {
|
|
readonly canvas3d: Canvas3D;
|
|
readonly canvas3d: Canvas3D;
|
|
readonly imagePass: ImagePass;
|
|
readonly imagePass: ImagePass;
|
|
|
|
|
|
- constructor(readonly canvasSize: { width: number, height: number }, canvas3d?: Canvas3D, options?: HeadlessScreenshotHelperOptions) {
|
|
|
|
|
|
+ constructor(readonly externalModules: ExternalModules, readonly canvasSize: { width: number, height: number }, canvas3d?: Canvas3D, options?: HeadlessScreenshotHelperOptions) {
|
|
if (canvas3d) {
|
|
if (canvas3d) {
|
|
this.canvas3d = canvas3d;
|
|
this.canvas3d = canvas3d;
|
|
} else {
|
|
} else {
|
|
- const glContext = lazyImports.gl(this.canvasSize.width, this.canvasSize.height, options?.webgl ?? defaultWebGLAttributes());
|
|
|
|
|
|
+ const glContext = this.externalModules.gl(this.canvasSize.width, this.canvasSize.height, options?.webgl ?? defaultWebGLAttributes());
|
|
const webgl = createContext(glContext);
|
|
const webgl = createContext(glContext);
|
|
const input = InputObserver.create();
|
|
const input = InputObserver.create();
|
|
const attribs = { ...Canvas3DContext.DefaultAttribs };
|
|
const attribs = { ...Canvas3DContext.DefaultAttribs };
|
|
@@ -93,14 +91,20 @@ export class HeadlessScreenshotHelper {
|
|
|
|
|
|
async getImagePng(imageSize?: { width: number, height: number }, postprocessing?: Partial<PostprocessingProps>): Promise<PNG> {
|
|
async getImagePng(imageSize?: { width: number, height: number }, postprocessing?: Partial<PostprocessingProps>): Promise<PNG> {
|
|
const imageData = await this.getImageRaw(imageSize, postprocessing);
|
|
const imageData = await this.getImageRaw(imageSize, postprocessing);
|
|
- const generatedPng = new lazyImports.pngjs.PNG({ width: imageData.width, height: imageData.height });
|
|
|
|
|
|
+ if (!this.externalModules.pngjs) {
|
|
|
|
+ throw new Error("External module 'pngjs' was not provided. If you want to use getImagePng, you must import 'pngjs' and provide it to the HeadlessPluginContext/HeadlessScreenshotHelper constructor.");
|
|
|
|
+ }
|
|
|
|
+ const generatedPng = new this.externalModules.pngjs.PNG({ width: imageData.width, height: imageData.height });
|
|
generatedPng.data = Buffer.from(imageData.data.buffer);
|
|
generatedPng.data = Buffer.from(imageData.data.buffer);
|
|
return generatedPng;
|
|
return generatedPng;
|
|
}
|
|
}
|
|
|
|
|
|
async getImageJpeg(imageSize?: { width: number, height: number }, postprocessing?: Partial<PostprocessingProps>, jpegQuality: number = 90): Promise<JpegBufferRet> {
|
|
async getImageJpeg(imageSize?: { width: number, height: number }, postprocessing?: Partial<PostprocessingProps>, jpegQuality: number = 90): Promise<JpegBufferRet> {
|
|
const imageData = await this.getImageRaw(imageSize, postprocessing);
|
|
const imageData = await this.getImageRaw(imageSize, postprocessing);
|
|
- const generatedJpeg = lazyImports['jpeg-js'].encode(imageData, jpegQuality);
|
|
|
|
|
|
+ if (!this.externalModules['jpeg-js']) {
|
|
|
|
+ throw new Error("External module 'jpeg-js' was not provided. If you want to use getImageJpeg, you must import 'jpeg-js' and provide it to the HeadlessPluginContext/HeadlessScreenshotHelper constructor.");
|
|
|
|
+ }
|
|
|
|
+ const generatedJpeg = this.externalModules['jpeg-js'].encode(imageData, jpegQuality);
|
|
return generatedJpeg;
|
|
return generatedJpeg;
|
|
}
|
|
}
|
|
|
|
|