- 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
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
import { BaseImageResource } from './BaseImageResource';
import { settings } from '@pixi/settings';
import { ALPHA_MODES } from '@pixi/constants';
import type { BaseTexture } from '../BaseTexture';
import type { Renderer } from '../../Renderer';
import type { GLTexture } from '../GLTexture';
export interface IImageResourceOptions
{
/** Start loading process */
autoLoad?: boolean;
/** Whether its required to create a bitmap before upload. */
createBitmap?: boolean;
/** Load image using cross origin. */
crossorigin?: boolean | string;
/** Premultiply image alpha in bitmap. */
alphaMode?: ALPHA_MODES;
}
/**
* Resource type for HTMLImageElement.
* @memberof PIXI
*/
export class ImageResource extends BaseImageResource
{
/** URL of the image source */
url: string;
/**
* If the image should be disposed after upload
* @default false
*/
preserveBitmap: boolean;
/**
* If capable, convert the image using createImageBitmap API.
* @default PIXI.settings.CREATE_IMAGE_BITMAP
*/
createBitmap: boolean;
/**
* Controls texture alphaMode field
* Copies from options
* Default is `null`, copies option from baseTexture
* @readonly
*/
alphaMode: ALPHA_MODES;
/**
* The ImageBitmap element created for a {@code HTMLImageElement}.
* @default null
*/
bitmap: ImageBitmap;
/**
* Promise when loading.
* @default null
*/
private _load: Promise<ImageResource>;
/** When process is completed */
private _process: Promise<ImageResource>;
/**
* @param source - image source or URL
* @param options
* @param {boolean} [options.autoLoad=true] - start loading process
* @param {boolean} [options.createBitmap=PIXI.settings.CREATE_IMAGE_BITMAP] - whether its required to create
* a bitmap before upload
* @param {boolean} [options.crossorigin=true] - Load image using cross origin
* @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.UNPACK] - Premultiply image alpha in bitmap
*/
constructor(source: HTMLImageElement | string, options?: IImageResourceOptions)
{
options = options || {};
if (!(source instanceof HTMLImageElement))
{
const imageElement = new Image();
BaseImageResource.crossOrigin(imageElement, source, options.crossorigin);
imageElement.src = source;
source = imageElement;
}
super(source);
// FireFox 68, and possibly other versions, seems like setting the HTMLImageElement#width and #height
// to non-zero values before its loading completes if images are in a cache.
// Because of this, need to set the `_width` and the `_height` to zero to avoid uploading incomplete images.
// Please refer to the issue #5968 (https://github.com/pixijs/pixi.js/issues/5968).
if (!source.complete && !!this._width && !!this._height)
{
this._width = 0;
this._height = 0;
}
this.url = source.src;
this._process = null;
this.preserveBitmap = false;
this.createBitmap = (options.createBitmap !== undefined
? options.createBitmap : settings.CREATE_IMAGE_BITMAP) && !!globalThis.createImageBitmap;
this.alphaMode = typeof options.alphaMode === 'number' ? options.alphaMode : null;
this.bitmap = null;
this._load = null;
if (options.autoLoad !== false)
{
this.load();
}
}
/**
* Returns a promise when image will be loaded and processed.
* @param createBitmap - whether process image into bitmap
*/
load(createBitmap?: boolean): Promise<ImageResource>
{
if (this._load)
{
return this._load;
}
if (createBitmap !== undefined)
{
this.createBitmap = createBitmap;
}
this._load = new Promise((resolve, reject): void =>
{
const source = this.source as HTMLImageElement;
this.url = source.src;
const completed = (): void =>
{
if (this.destroyed)
{
return;
}
source.onload = null;
source.onerror = null;
this.resize(source.width, source.height);
this._load = null;
if (this.createBitmap)
{
resolve(this.process());
}
else
{
resolve(this);
}
};
if (source.complete && source.src)
{
completed();
}
else
{
source.onload = completed;
source.onerror = (event): void =>
{
// Avoids Promise freezing when resource broken
reject(event);
this.onError.emit(event);
};
}
});
return this._load;
}
/**
* Called when we need to convert image into BitmapImage.
* Can be called multiple times, real promise is cached inside.
* @returns - Cached promise to fill that bitmap
*/
process(): Promise<ImageResource>
{
const source = this.source as HTMLImageElement;
if (this._process !== null)
{
return this._process;
}
if (this.bitmap !== null || !globalThis.createImageBitmap)
{
return Promise.resolve(this);
}
const createImageBitmap = globalThis.createImageBitmap as any;
const cors = !source.crossOrigin || source.crossOrigin === 'anonymous';
this._process = fetch(source.src,
{
mode: cors ? 'cors' : 'no-cors'
})
.then((r) => r.blob())
.then((blob) => createImageBitmap(blob,
0, 0, source.width, source.height,
{
premultiplyAlpha: this.alphaMode === ALPHA_MODES.UNPACK ? 'premultiply' : 'none',
}))
.then((bitmap: ImageBitmap) =>
{
if (this.destroyed)
{
return Promise.reject();
}
this.bitmap = bitmap;
this.update();
this._process = null;
return Promise.resolve(this);
});
return this._process;
}
/**
* Upload the image resource to GPU.
* @param renderer - Renderer to upload to
* @param baseTexture - BaseTexture for this resource
* @param glTexture - GLTexture to use
* @returns {boolean} true is success
*/
upload(renderer: Renderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean
{
if (typeof this.alphaMode === 'number')
{
// bitmap stores unpack premultiply flag, we dont have to notify texImage2D about it
baseTexture.alphaMode = this.alphaMode;
}
if (!this.createBitmap)
{
return super.upload(renderer, baseTexture, glTexture);
}
if (!this.bitmap)
{
// yeah, ignore the output
this.process();
if (!this.bitmap)
{
return false;
}
}
super.upload(renderer, baseTexture, glTexture, this.bitmap);
if (!this.preserveBitmap)
{
// checks if there are other renderers that possibly need this bitmap
let flag = true;
const glTextures = baseTexture._glTextures;
for (const key in glTextures)
{
const otherTex = glTextures[key];
if (otherTex !== glTexture && otherTex.dirtyId !== baseTexture.dirtyId)
{
flag = false;
break;
}
}
if (flag)
{
if (this.bitmap.close)
{
this.bitmap.close();
}
this.bitmap = null;
}
}
return true;
}
/** Destroys this resource. */
dispose(): void
{
(this.source as HTMLImageElement).onload = null;
(this.source as HTMLImageElement).onerror = null;
super.dispose();
if (this.bitmap)
{
this.bitmap.close();
this.bitmap = null;
}
this._process = null;
this._load = null;
}
/**
* Used to auto-detect the type of resource.
* @param {*} source - The source object
* @returns {boolean} `true` if source is string or HTMLImageElement
*/
static test(source: unknown): source is string | HTMLImageElement
{
return typeof source === 'string' || source instanceof HTMLImageElement;
}
}