@@ -116,6 +116,23 @@ function copyRgbaImage(src, dest, alpha01) {
116116 }
117117}
118118
119+ function isDefaultDecodeHelper ( decode , expectedLen ) {
120+ if ( ! Array . isArray ( decode ) ) {
121+ return true ;
122+ }
123+ const decodeLen = decode . length ;
124+
125+ if ( decodeLen < expectedLen ) {
126+ warn ( "Decode map length is too short." ) ;
127+ return true ;
128+ }
129+ if ( decodeLen > expectedLen ) {
130+ info ( "Truncating too long decode map." ) ;
131+ decode . length = expectedLen ;
132+ }
133+ return false ;
134+ }
135+
119136class ColorSpace {
120137 static #rgbBuf = new Uint8ClampedArray ( 3 ) ;
121138
@@ -185,8 +202,8 @@ class ColorSpace {
185202 /**
186203 * Refer to the static `ColorSpace.isDefaultDecode` method below.
187204 */
188- isDefaultDecode ( decodeMap , bpc ) {
189- return ColorSpace . isDefaultDecode ( decodeMap , this . numComps ) ;
205+ isDefaultDecode ( decode , bpc ) {
206+ return ColorSpace . isDefaultDecode ( decode , this . numComps ) ;
190207 }
191208
192209 /**
@@ -322,11 +339,7 @@ class ColorSpace {
322339 * @param {number } numComps - Number of components the color space has.
323340 */
324341 static isDefaultDecode ( decode , numComps ) {
325- if ( ! Array . isArray ( decode ) ) {
326- return true ;
327- }
328- if ( numComps * 2 !== decode . length ) {
329- warn ( "The decode map is not the correct length" ) ;
342+ if ( isDefaultDecodeHelper ( decode , numComps * 2 ) ) {
330343 return true ;
331344 }
332345 for ( let i = 0 , ii = decode . length ; i < ii ; i += 2 ) {
@@ -424,7 +437,7 @@ class PatternCS extends ColorSpace {
424437 this . base = baseCS ;
425438 }
426439
427- isDefaultDecode ( decodeMap , bpc ) {
440+ isDefaultDecode ( decode , bpc ) {
428441 unreachable ( "Should not call PatternCS.isDefaultDecode" ) ;
429442 }
430443}
@@ -489,19 +502,15 @@ class IndexedCS extends ColorSpace {
489502 return this . base . getOutputLength ( inputLength * this . base . numComps , alpha01 ) ;
490503 }
491504
492- isDefaultDecode ( decodeMap , bpc ) {
493- if ( ! Array . isArray ( decodeMap ) ) {
494- return true ;
495- }
496- if ( decodeMap . length !== 2 ) {
497- warn ( "Decode map length is not correct" ) ;
505+ isDefaultDecode ( decode , bpc ) {
506+ if ( isDefaultDecodeHelper ( decode , 2 ) ) {
498507 return true ;
499508 }
500509 if ( ! Number . isInteger ( bpc ) || bpc < 1 ) {
501510 warn ( "Bits per component is not correct" ) ;
502511 return true ;
503512 }
504- return decodeMap [ 0 ] === 0 && decodeMap [ 1 ] === ( 1 << bpc ) - 1 ;
513+ return decode [ 0 ] === 0 && decode [ 1 ] === ( 1 << bpc ) - 1 ;
505514 }
506515}
507516
@@ -1282,7 +1291,7 @@ class LabCS extends ColorSpace {
12821291 return ( ( inputLength * ( 3 + alpha01 ) ) / 3 ) | 0 ;
12831292 }
12841293
1285- isDefaultDecode ( decodeMap , bpc ) {
1294+ isDefaultDecode ( decode , bpc ) {
12861295 // XXX: Decoding is handled with the lab conversion because of the strange
12871296 // ranges that are used.
12881297 return true ;
0 commit comments