1010
1111import { isNullishDimension } from './core.js' ;
1212import { getFile } from './hub.js' ;
13- import { env } from '../env.js' ;
13+ import { env , apis } from '../env.js' ;
1414import { Tensor } from './tensor.js' ;
1515
1616// Will be empty (or not used) if running in browser or web-worker
1717import sharp from 'sharp' ;
1818
19- const BROWSER_ENV = typeof self !== 'undefined' ;
20- const WEBWORKER_ENV = BROWSER_ENV && self . constructor . name === 'DedicatedWorkerGlobalScope' ;
21-
2219let createCanvasFunction ;
2320let ImageDataClass ;
2421let loadImageFunction ;
25- if ( BROWSER_ENV ) {
22+ const IS_BROWSER_OR_WEBWORKER = apis . IS_BROWSER_ENV || apis . IS_WEBWORKER_ENV ;
23+ if ( IS_BROWSER_OR_WEBWORKER ) {
2624 // Running in browser or web-worker
2725 createCanvasFunction = ( /** @type {number } */ width , /** @type {number } */ height ) => {
2826 if ( ! self . OffscreenCanvas ) {
@@ -132,7 +130,7 @@ export class RawImage {
132130 * @returns {RawImage } The image object.
133131 */
134132 static fromCanvas ( canvas ) {
135- if ( ! BROWSER_ENV ) {
133+ if ( ! IS_BROWSER_OR_WEBWORKER ) {
136134 throw new Error ( 'fromCanvas() is only supported in browser environments.' )
137135 }
138136
@@ -161,7 +159,7 @@ export class RawImage {
161159 * @returns {Promise<RawImage> } The image object.
162160 */
163161 static async fromBlob ( blob ) {
164- if ( BROWSER_ENV ) {
162+ if ( IS_BROWSER_OR_WEBWORKER ) {
165163 // Running in environment with canvas
166164 const img = await loadImageFunction ( blob ) ;
167165
@@ -339,7 +337,7 @@ export class RawImage {
339337 height = ( width / this . width ) * this . height ;
340338 }
341339
342- if ( BROWSER_ENV ) {
340+ if ( IS_BROWSER_OR_WEBWORKER ) {
343341 // TODO use `resample` in browser environment
344342
345343 // Store number of channels before resizing
@@ -412,7 +410,7 @@ export class RawImage {
412410 return this ;
413411 }
414412
415- if ( BROWSER_ENV ) {
413+ if ( IS_BROWSER_OR_WEBWORKER ) {
416414 // Store number of channels before padding
417415 const numChannels = this . channels ;
418416
@@ -461,7 +459,7 @@ export class RawImage {
461459 const crop_width = x_max - x_min + 1 ;
462460 const crop_height = y_max - y_min + 1 ;
463461
464- if ( BROWSER_ENV ) {
462+ if ( IS_BROWSER_OR_WEBWORKER ) {
465463 // Store number of channels before resizing
466464 const numChannels = this . channels ;
467465
@@ -509,7 +507,7 @@ export class RawImage {
509507 const height_offset = ( this . height - crop_height ) / 2 ;
510508
511509
512- if ( BROWSER_ENV ) {
510+ if ( IS_BROWSER_OR_WEBWORKER ) {
513511 // Store number of channels before resizing
514512 const numChannels = this . channels ;
515513
@@ -614,7 +612,7 @@ export class RawImage {
614612 }
615613
616614 async toBlob ( type = 'image/png' , quality = 1 ) {
617- if ( ! BROWSER_ENV ) {
615+ if ( ! IS_BROWSER_OR_WEBWORKER ) {
618616 throw new Error ( 'toBlob() is only supported in browser environments.' )
619617 }
620618
@@ -640,7 +638,7 @@ export class RawImage {
640638 }
641639
642640 toCanvas ( ) {
643- if ( ! BROWSER_ENV ) {
641+ if ( ! IS_BROWSER_OR_WEBWORKER ) {
644642 throw new Error ( 'toCanvas() is only supported in browser environments.' )
645643 }
646644
@@ -744,8 +742,8 @@ export class RawImage {
744742 */
745743 async save ( path ) {
746744
747- if ( BROWSER_ENV ) {
748- if ( WEBWORKER_ENV ) {
745+ if ( IS_BROWSER_OR_WEBWORKER ) {
746+ if ( apis . IS_WEBWORKER_ENV ) {
749747 throw new Error ( 'Unable to save an image from a Web Worker.' )
750748 }
751749
@@ -781,7 +779,7 @@ export class RawImage {
781779 }
782780
783781 toSharp ( ) {
784- if ( BROWSER_ENV ) {
782+ if ( IS_BROWSER_OR_WEBWORKER ) {
785783 throw new Error ( 'toSharp() is only supported in server-side environments.' )
786784 }
787785
0 commit comments