@@ -292,7 +292,8 @@ class PaletteColorLookupTable {
292292 const startpoint = lut [ offset - 1 ]
293293 const step = ( endpoint - startpoint ) / ( length - 1 )
294294 for ( let j = 0 ; j < length ; j ++ ) {
295- lut [ offset + j ] = startpoint + Math . round ( j * step )
295+ const value = startpoint + Math . round ( j * step )
296+ lut [ offset + j ] = value
296297 }
297298 offset += length
298299 } else if ( opcode === 2 ) {
@@ -368,17 +369,23 @@ class PaletteColorLookupTable {
368369 )
369370 }
370371
371- if ( this [ _attrs ] . bitsPerEntry === 16 ) {
372+ const maxValues = [
373+ Math . max ( ...redLUT ) ,
374+ Math . max ( ...greenLUT ) ,
375+ Math . max ( ...blueLUT )
376+ ]
377+ const maxInput = Math . max ( ...maxValues )
378+ const maxOutput = 255
379+ if ( this [ _attrs ] . bitsPerEntry === 16 && maxInput > 255 ) {
372380 /*
373381 * Only palettes with 256 entries and 8 bit per entry are supported for
374382 * display. Therefore, data need to rescaled and resampled.
375383 */
376- const maxInput = Math . pow ( 2 , 16 ) - 1
377- const maxOutput = Math . pow ( 2 , 8 ) - 1
378- const steps = Math . pow ( 2 , 16 ) / Math . pow ( 2 , 8 )
379- this [ _attrs ] . data = new Array ( steps )
380- for ( let i = 0 ; i < steps ; i ++ ) {
381- const j = i * steps
384+ const n = 256
385+ const step = this [ _attrs ] . numberOfEntries / n
386+ this [ _attrs ] . data = new Array ( n )
387+ for ( let i = 0 ; i < n ; i ++ ) {
388+ const j = i * step
382389 this [ _attrs ] . data [ i ] = [
383390 Math . round ( rescale ( redLUT [ j ] , 0 , maxInput , 0 , maxOutput ) ) ,
384391 Math . round ( rescale ( greenLUT [ j ] , 0 , maxInput , 0 , maxOutput ) ) ,
@@ -388,11 +395,7 @@ class PaletteColorLookupTable {
388395 } else {
389396 this [ _attrs ] . data = new Array ( this [ _attrs ] . numberOfEntries )
390397 for ( let i = 0 ; i < this [ _attrs ] . numberOfEntries ; i ++ ) {
391- this [ _attrs ] . data [ i ] = [
392- redLUT [ i ] ,
393- greenLUT [ i ] ,
394- blueLUT [ i ]
395- ]
398+ this [ _attrs ] . data [ i ] = [ redLUT [ i ] , greenLUT [ i ] , blueLUT [ i ] ]
396399 }
397400 }
398401 }
0 commit comments