-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.d.ts
517 lines (486 loc) · 15 KB
/
api.d.ts
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* @license
* Copyright (c) 2022 Daniel Imms <http://www.growingwiththeweb.com>
* Released under MIT license. See LICENSE in the project root for details.
*/
/**
* Decodes a TGA file.
*
* @param data The complete TGA file data to decode.
* @param options Options to configure how decoding happens.
*
* @throws A {@link DecodeError} when an error is encountered or a {@link DecodeWarning} when a
* warning is encountered in strict mode. In Typescript, `instanceof` can be used to narrow the type
* safely.
*/
export function decodeTga(data: Readonly<Uint8Array>, options?: IDecodeTgaOptions): Promise<IDecodedTga>;
/**
* Encodes a tga file.
*
* @param data The image data in rgba format.
* @param options Options to configure how encoding happens.
*/
export function encodeTga(data: Readonly<IImage32>, options?: IEncodeTgaOptions): Promise<IEncodedTga>;
/**
* A TGA that has been successfully decoded.
*/
export interface IDecodedTga {
/**
* The image dimensions and data.
*/
image: IImage32;
/**
* The details of the TGA including internals on how it was encoded as well as other metadata.
*/
details: ITgaDetails;
/**
* Any warnings that were encountered during decoding. Warnings are generally safe to ignore, here
* are some examples:
*
* - The image was determined to have ambigous alpha when
* {@link IDecodeTgaOptions.detectAmbiguousAlphaChannel} was set.
* - Unexpected field values encountered that have fallbacks like an unrecognized color map type
* which gets treated as a regular color map.
* - Invalid offset values
*
* Strict mode can be enabled via {@link IDecodeTgaOptions.strictMode} which will throw an error when
* any warning is encountered.
*/
warnings: DecodeWarning[];
}
/**
* A set of options to configure how decoding happens.
*/
export interface IDecodeTgaOptions {
/**
* Since different TGA decoders behave slightly differently, sometimes images come out as 100%
* transparent. When this feature is enabled, any image with ambiguous alpha (when alpha is
* processed but attribute bits per pixel is 0) will be checked to see if the decoded image is
* 100% transparent and there is data in the rgb channels and if so will make the image fully
* opaque and will add a warning to {@link IDecodedTga.warnings}.
*
* This setting is ignored when in {@link IDecodeTgaOptions.strictMode}.
*/
detectAmbiguousAlphaChannel?: boolean;
/**
* Enables strict mode which will throw an error when the first warning is encountered. Strict
* mode should be used when it's important that the TGA is completely valid, when strict mode is
* not enabled the decoder will be as error tolerant as possible and report any warnings that
* would has failed in strict mode in {@link IDecodedTga.warnings}.
*/
strictMode?: boolean;
}
/**
* A tga that has been successfully decoded.
*/
export interface IEncodedTga {
data: Uint8Array;
/**
* Any warnings that were encountered during encoding. Warnings typically fall into the following
* categories:
*
* - An explicitly provided color type must be changed in order to encode the image (eg.
* specifying Truecolor but having more than one transparent pixel).
*
* Strict mode can be enabled via {@link IEncodePngOptions.strictMode} which will throw an error
* when any warning is encountered.
*/
warnings: EncodeWarning[];
/**
* Any informational messages when encoding. These are things of note but not important enough to
* be a warning.
*/
info: string[];
}
/**
* A set of options to configure how encoding happens.
*/
export interface IEncodeTgaOptions {
/**
* The bit depth to encode with. When unspecified, the library will scan the image and determine
* the best value based on the content, it's best to pass this in if know to avoid the scan
* iterating over every pixel in the image.
*
* {@link imageType} must be specified when the bit depth is.
*/
bitDepth?: BitDepth;
/**
* Defines how to encode the image. This can be an explicit {@link ImageType} or a hint
* {@link ImageTypeCompressionHint} which will detect with the given compression.
*
* {@link bitDepth} must be specified when the image type is.
*/
imageType?: ImageType | ImageTypeCompressionHint;
/**
* Enabled strict encoding which will throw when warnings are encountered.
*/
strictMode?: boolean;
/**
* Optional identifying information about the image.
*
* Field, 0-255 bytes
*/
imageId?: string;
/**
* These bytes specify the absolute coordinate for the lower left corner of the image as it is
* positioned on a display device having an origin at the lower left of the screen. This is
* typically ignored in modern software.
*/
origin?: {
/**
* These bytes specify the absolute horizontal coordinate for the lower left corner of the image
* as it is positioned on a display device having an origin at the lower left of the screen. This
* is typically ignored in modern software.
*
* Field 5.1, 16 bit unsigned
*/
x?: number;
/**
* These bytes specify the absolute vertical coordinate for the lower left corner of the image as
* it is positioned on a display device having an origin at the lower left of the screen. This is
* typically ignored in modern software.
*
* Field 5.2, 16 bit unsigned
*/
y?: number;
};
/**
* The screen origin defines in what order the image data is encoded.
*/
screenOrigin?: ScreenOrigin;
}
/**
* The details of the TGA including internals on how it was encoded as well as other metadata.
*/
export interface ITgaDetails {
header: ITgaHeader;
/**
* Optional identifying information about the image.
*
* Field, 0-255 bytes
*/
imageId: string;
footer?: ITgaFooter;
/**
* The {@link IExtensionArea} of the TGA file if it exists.
*/
extensionArea?: IExtensionArea;
/**
* Developer directory entries in the TGA file.
*/
developerDirectory?: IDeveloperDirectoryEntry[];
}
export interface ITgaHeader {
/**
* The length in bytes of the image ID (field 6).
*
* Field 1, 8 bit unsigned
*/
idLength: number;
/**
* The optional color map of the TGA.
*/
colorMap: ITgaColorMap | undefined,
/**
* The type of image.
*
* Field 3, 8 bit unsigned
*/
imageType: ImageType;
/**
* These bytes specify the absolute coordinate for the lower left corner of the image as it is
* positioned on a display device having an origin at the lower left of the screen. This is
* typically ignored in modern software.
*/
origin: {
/**
* These bytes specify the absolute horizontal coordinate for the lower left corner of the image
* as it is positioned on a display device having an origin at the lower left of the screen. This
* is typically ignored in modern software.
*
* Field 5.1, 16 bit unsigned
*/
x: number;
/**
* These bytes specify the absolute vertical coordinate for the lower left corner of the image as
* it is positioned on a display device having an origin at the lower left of the screen. This is
* typically ignored in modern software.
*
* Field 5.2, 16 bit unsigned
*/
y: number;
};
/**
* The width of the image.
*
* Field 5.3, 16 bit unsigned
*/
width: number;
/**
* The height of the image.
*
* Field 5.4, 16 bit unsigned
*/
height: number;
/**
* The bit depth of the image.
*
* Field 5.5, 8 bit unsigned
*/
bitDepth: BitDepth;
/**
* Specifies the number of attribute bits (aka alpha bits) per pixel
*
* | Bits | Meaning
* |------|-------------------
* | 0-3 | Alpha channel bits
* | 4-5 | Image origin
* | 6-7 | Unused (some documents say this is interleaving but it's not in the v2.0 spec)
*
* Field 5.6, 8 bit unsigned
*/
imageDescriptor: number;
/**
* The number of attribute bits (aka alpha bits) extracted from {@link imageDescriptor}.
*/
attributeBitsPerPixel: number;
/**
* The screen origin extracted from {@link imageDescriptor}.
*/
screenOrigin: ScreenOrigin;
}
export interface ITgaFooter {
/**
* The byte offset of the {@link IDecodedTga.extensionArea}.
*/
extensionAreaOffset?: number;
/**
* The byte offset of the {@link IDecodedTga.developerDirectory}.
*/
developerDirectoryOffset?: number;
}
/**
* An optional extension area in the TGA file, containing various metadata as well as important
* decoding information like how to use the alpha channel. If an extension area is specified its
* {@link extensionSize} must be 495 in order to be considered a valid version 2.0 TGA file.
*/
export interface IExtensionArea {
/**
* The size of the extension area. This must be 495 for a version 2.0 TGA file.
*/
extensionSize: number;
/**
* The name of the person who created the image.
*/
authorName: string | undefined;
/**
* Comments of the author.
*/
authorComments: string | undefined;
/**
* The date and time when the image was saved.
*/
dateTimestamp: Date | undefined;
/**
* The name of the job, allowing the author to tie images with jobs.
*/
jobName: string | undefined;
/**
* A running total of the amount of time invested in a particular image.
*/
jobTime: { hours: number, minutes: number, seconds: number } | undefined;
/**
* The program the image was created within.
*/
softwareId: string | undefined;
/**
* The version number of the program used to create the image as a number followed by a letter.
* Possible range is <0 to 2.55><letter> (eg. 1.17b).
*/
softwareVersion: string | undefined;
/**
* The key color as an array in [r, g, b, a] format. The key color can be thought of as the
* 'background color' or 'transparent color'. This could be used in image viewers for example to
* select an idea background color. This library will decode this field but it's left up to the
* embedding application to use it if applicable.
*
* When this is undefined it means all values were 0 which should be treated the same as black
* according to the specification.
*/
keyColor: Uint8Array | undefined;
/**
* The pixel aspect ratio when it is important to preserve the proper aspect ratio of the saved
* image. If this is undefined it means the aspect ratio was not specified and is composed of
* square pixels.
*/
aspectRatio: number | undefined;
/**
* The gamma value which can be used to correct the image. If this is 1.0 gamma was _specified_
* but there is no gamma value. If this is undefined no gamma value was specified and should be
* treated the same as no gamma value.
*/
gamma: number | undefined;
/**
* The byte offset of the color correction table, this is currently not decoded.
*/
colorCorrectionOffset: number;
/**
* The byte offset of the postage stamp image (ie. the thumbnail), this is currently not decoded.
*/
postageStampOffset: number;
/**
* The byte offset of the scan line table, this is currently not decoded.
*/
scanLineOffset: number;
/**
* The attribute type (aka alpha type) of the image, this field is taken into account when
* decoding the image data. The meaning of each value is show below:
*
* | Value | Meaning
* |---------|---------
* | 0 | No alpha data is included
* | 1 | Undefined data in the alpha field, can be ignored
* | 2 | Undefined data in the alpha field, but should be retained
* | 3 | Useful alpha channel data is present
* | 4 | Pre-multiplied alpha (not currently supported)
* | 5-127 | Reserved
* | 128-255 | Un-assigned
*/
attributesType: number;
}
/**
* An entry in the developer area which contains arbitrary data as defined by the developer,
* typically identified by the {@link tag}. This library doesn't understand any developer area entry
* since they can be anything. To look at data for each entry read from {@link offset} to
* {@link offset}+{@link length}.
*/
export interface IDeveloperDirectoryEntry {
/**
* An identifying tag for the entry.
*/
tag: number;
/**
* The offset the entry starts at.
*/
offset: number;
/**
* The length of the entry.
*/
length: number;
}
/**
* A 32-bit image, 8 bits per channel in rgba format.
*/
export interface IImage32 {
data: Uint8Array;
width: number;
height: number;
}
/**
* The bit depth defines how many bits are used per pixel. Whether a bit depth is supported depends
* on the image type that it's used with as shown in the below table:
*
* | Image type | Bit depths | Channel bits | Alpha bits\*
* |--------------|------------|--------------|-------------
* | Color mapped | 8 | 8 | 0
* | True color | 15 | 5 | 0
* | True color | 16 | 5 | 1
* | True color | 24 | 8 | 0
* | True color | 32 | 8 | 8
* | Grayscale | 8 | 8 | 0
* | Grayscale | 16 | 8 | 8
*
* \* Alpha bits may get ignored
*/
export type BitDepth = 8 | 15 | 16 | 24 | 32;
export interface ITgaColorMap {
/**
* The type of color map.
*
* Field 2, 8 bit unsigned
*/
type: ColorMapType;
/**
* The index of the starting entry when loading the color map. This allows ignoring the beginning
* of the color map.
*
* Field 4.1, 16 bit unsigned
*/
origin: number;
/**
* The total number of entries in the color map.
*
* Field 4.2, 16 bit unsigned
*/
length: number;
/**
* The bits per entry of the color map. This is typically 15, 16, 24 or 32.
*
* Field 4.3, 8 bit unsigned
*/
depth: number;
}
export const enum ColorMapType {
NoColorMap = 0,
ColorMap = 1,
Unrecognized = 2,
}
export const enum ImageType {
NoImageData = 0,
UncompressedColorMapped = 1,
UncompressedTrueColor = 2,
UncompressedGrayscale = 3,
RunLengthEncodedColorMapped = 9,
RunLengthEncodedTrueColor = 10,
RunLengthEncodedGrayscale = 11
}
export const enum ImageTypeCompressionHint {
Uncompressed = -1,
RunLengthEncoded = -2
}
export const enum ScreenOrigin {
BottomLeft = 0,
BottomRight = 1,
TopLeft = 2,
TopRight = 3
}
/**
* A critical error occurred during decoding.
*/
export class DecodeError extends Error {
/**
* The byte offset of the error in the datastream.
*/
offset: number;
/**
* The partially decoded image which gives access to decode warnings, dimensions, etc.
*/
partiallyDecodedImage: Partial<Exclude<IDecodedTga, 'details'> & { details: Partial<ITgaDetails> }>;
}
/**
* A warning occurred during decoding.
*/
export class DecodeWarning extends Error {
/**
* The byte offset of the warning in the datastream.
*/
offset: number;
}
/**
* A critical error occurred during encoding.
*/
export class EncodeError extends Error {
/**
* The byte offset of the error in the datastream.
*/
offset: number;
}
/**
* A warning occurred during encoding.
*/
export class EncodeWarning extends Error {
/**
* The byte offset of the warning in the datastream.
*/
offset: number;
}