all files / src/imageLoader/colorSpaceConverters/ convertPALETTECOLOR.js

0% Statements 0/29
0% Branches 0/6
0% Functions 0/2
0% Lines 0/29
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                                                                                                             
/* eslint no-bitwise: 0 */
 
function convertLUTto8Bit (lut, shift) {
  const numEntries = lut.length;
  const cleanedLUT = new Uint8ClampedArray(numEntries);
 
  for (let i = 0; i < numEntries; ++i) {
    cleanedLUT[i] = lut[i] >> shift;
  }
 
  return cleanedLUT;
}
 
/**
 * Convert pixel data with PALETTE COLOR Photometric Interpretation to RGBA
 *
 * @param {ImageFrame} imageFrame
 * @param {Uint8ClampedArray} rgbaBuffer
 * @returns {void}
 */
export default function (imageFrame, rgbaBuffer) {
  const numPixels = imageFrame.columns * imageFrame.rows;
  const pixelData = imageFrame.pixelData;
  const rData = imageFrame.redPaletteColorLookupTableData;
  const gData = imageFrame.greenPaletteColorLookupTableData;
  const bData = imageFrame.bluePaletteColorLookupTableData;
  const len = imageFrame.redPaletteColorLookupTableData.length;
  let palIndex = 0;
  let rgbaIndex = 0;
 
  const start = imageFrame.redPaletteColorLookupTableDescriptor[1];
  const shift = imageFrame.redPaletteColorLookupTableDescriptor[2] === 8 ? 0 : 8;
 
  const rDataCleaned = convertLUTto8Bit(rData, shift);
  const gDataCleaned = convertLUTto8Bit(gData, shift);
  const bDataCleaned = convertLUTto8Bit(bData, shift);
 
  for (let i = 0; i < numPixels; ++i) {
    let value = pixelData[palIndex++];
 
    if (value < start) {
      value = 0;
    } else if (value > start + len - 1) {
      value = len - 1;
    } else {
      value -= start;
    }
 
    rgbaBuffer[rgbaIndex++] = rDataCleaned[value];
    rgbaBuffer[rgbaIndex++] = gDataCleaned[value];
    rgbaBuffer[rgbaIndex++] = bDataCleaned[value];
    rgbaBuffer[rgbaIndex++] = 255;
  }
}