Skip to content

Commit

Permalink
fix: rendering of palette color for no useRGB flag (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi authored Jul 25, 2022
1 parent 5583aa5 commit 78afce2
Showing 1 changed file with 49 additions and 29 deletions.
78 changes: 49 additions & 29 deletions src/imageLoader/createImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,59 @@ function createImage(imageId, pixelData, transferSyntax, options = {}) {
const isColorImage = isColorImageFn(imageFrame.photometricInterpretation);

if (isColorImage) {
// JPEGBaseline (8 bits) is already returning the pixel data in the right format (rgba)
// because it's using a canvas to load and decode images.
if (!isJPEGBaseline8BitColor(imageFrame, transferSyntax) && useRGBA) {
canvas.height = imageFrame.rows;
canvas.width = imageFrame.columns;
if (useRGBA) {
// JPEGBaseline (8 bits) is already returning the pixel data in the right format (rgba)
// because it's using a canvas to load and decode images.
if (!isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
canvas.height = imageFrame.rows;
canvas.width = imageFrame.columns;

const context = canvas.getContext('2d');

const imageData = context.createImageData(
imageFrame.columns,
imageFrame.rows
);

const context = canvas.getContext('2d');
convertColorSpace(imageFrame, imageData.data, useRGBA);

const imageData = context.createImageData(
imageFrame.columns,
imageFrame.rows
);
imageFrame.imageData = imageData;
imageFrame.pixelData = imageData.data;
}
} else {
if (isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
// If we don't need the RGBA but the decoding is done with RGBA (the case
// for JPEG Baseline 8 bit color), AND the option specifies to use RGB (no RGBA)
// we need to remove the A channel from pixel data
const colorBuffer = new Uint8ClampedArray(
(imageFrame.pixelData.length / 4) * 3
);

convertColorSpace(imageFrame, imageData.data, useRGBA);

imageFrame.imageData = imageData;
imageFrame.pixelData = imageData.data;
} else if (
isJPEGBaseline8BitColor(imageFrame, transferSyntax) &&
!useRGBA
) {
// If we don't need the RGBA but the decoding is done with RGBA (the case
// for JPEG Baseline 8 bit color), AND the option specifies to use RGB (no RGBA)
// we need to remove the A channel from pixel data
const newPixelData = new imageFrame.pixelData.constructor(
(imageFrame.pixelData.length / 4) * 3
);
// remove the A from the RGBA of the imageFrame
imageFrame.pixelData = removeAFromRGBA(
imageFrame.pixelData,
colorBuffer
);
} else if (imageFrame.photometricInterpretation === 'PALETTE COLOR') {
canvas.height = imageFrame.rows;
canvas.width = imageFrame.columns;

// remove the A from the RGBA of the imageFrame
imageFrame.pixelData = removeAFromRGBA(
imageFrame.pixelData,
newPixelData
);
const context = canvas.getContext('2d');

const imageData = context.createImageData(
imageFrame.columns,
imageFrame.rows
);

convertColorSpace(imageFrame, imageData.data, true);

const colorBuffer = new imageData.data.constructor(
(imageData.data.length / 4) * 3
);

// remove the A from the RGBA of the imageFrame
imageFrame.pixelData = removeAFromRGBA(imageData.data, colorBuffer);
}
}

// calculate smallest and largest PixelValue of the converted pixelData
Expand Down

0 comments on commit 78afce2

Please sign in to comment.