@@ -14,12 +14,14 @@ import { ByteStream } from '../../io/bytestream.js';
14
14
// https://en.wikipedia.org/wiki/PNG#File_format
15
15
// https://www.w3.org/TR/2003/REC-PNG-20031110
16
16
17
+ // let DEBUG = true;
17
18
let DEBUG = false ;
18
19
const SIG = new Uint8Array ( [ 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ] ) ;
19
20
20
21
/** @enum {string} */
21
22
export const PngParseEventType = {
22
23
IHDR : 'image_header' ,
24
+ gAMA : 'image_gamma' ,
23
25
PLTE : 'palette' ,
24
26
IDAT : 'image_data' ,
25
27
} ;
@@ -59,6 +61,15 @@ export class PngImageHeaderEvent extends Event {
59
61
}
60
62
}
61
63
64
+ export class PngImageGammaEvent extends Event {
65
+ /** @param {number } */
66
+ constructor ( gamma ) {
67
+ super ( PngParseEventType . gAMA ) ;
68
+ /** @type {number } */
69
+ this . gamma = gamma ;
70
+ }
71
+ }
72
+
62
73
/**
63
74
* @typedef PngColor
64
75
* @property {number } red
@@ -132,6 +143,16 @@ export class PngParser extends EventTarget {
132
143
return this ;
133
144
}
134
145
146
+ /**
147
+ * Type-safe way to bind a listener for a PngImageGammaEvent.
148
+ * @param {function(PngImageGammaEvent): void } listener
149
+ * @returns {PngParser } for chaining
150
+ */
151
+ onGamma ( listener ) {
152
+ super . addEventListener ( PngParseEventType . gAMA , listener ) ;
153
+ return this ;
154
+ }
155
+
135
156
/**
136
157
* Type-safe way to bind a listener for a PngPaletteEvent.
137
158
* @param {function(PngPaletteEvent): void } listener
@@ -204,6 +225,12 @@ export class PngParser extends EventTarget {
204
225
this . dispatchEvent ( new PngImageHeaderEvent ( header ) ) ;
205
226
break ;
206
227
228
+ // https://www.w3.org/TR/2003/REC-PNG-20031110/#11gAMA
229
+ case 'gAMA' :
230
+ if ( length !== 4 ) throw `Bad length for gAMA: ${ length } ` ;
231
+ this . dispatchEvent ( new PngImageGammaEvent ( chStream . readNumber ( 4 ) ) ) ;
232
+ break ;
233
+
207
234
// https://www.w3.org/TR/2003/REC-PNG-20031110/#11PLTE
208
235
case 'PLTE' :
209
236
if ( this . colorType === undefined ) throw `PLTE before IHDR` ;
@@ -282,8 +309,11 @@ async function main() {
282
309
parser . onImageHeader ( evt => {
283
310
// console.dir(evt.imageHeader);
284
311
} ) ;
312
+ parser . onGamma ( evt => {
313
+ // console.dir(evt.imageGamma);
314
+ } ) ;
285
315
parser . onPalette ( evt => {
286
- console . dir ( evt . palette ) ;
316
+ // console.dir(evt.palette);
287
317
} ) ;
288
318
parser . onImageData ( evt => {
289
319
// console.dir(evt);
0 commit comments