Skip to content

Commit 78afce2

Browse files
authored
fix: rendering of palette color for no useRGB flag (#459)
1 parent 5583aa5 commit 78afce2

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

src/imageLoader/createImage.js

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -198,39 +198,59 @@ function createImage(imageId, pixelData, transferSyntax, options = {}) {
198198
const isColorImage = isColorImageFn(imageFrame.photometricInterpretation);
199199

200200
if (isColorImage) {
201-
// JPEGBaseline (8 bits) is already returning the pixel data in the right format (rgba)
202-
// because it's using a canvas to load and decode images.
203-
if (!isJPEGBaseline8BitColor(imageFrame, transferSyntax) && useRGBA) {
204-
canvas.height = imageFrame.rows;
205-
canvas.width = imageFrame.columns;
201+
if (useRGBA) {
202+
// JPEGBaseline (8 bits) is already returning the pixel data in the right format (rgba)
203+
// because it's using a canvas to load and decode images.
204+
if (!isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
205+
canvas.height = imageFrame.rows;
206+
canvas.width = imageFrame.columns;
207+
208+
const context = canvas.getContext('2d');
209+
210+
const imageData = context.createImageData(
211+
imageFrame.columns,
212+
imageFrame.rows
213+
);
206214

207-
const context = canvas.getContext('2d');
215+
convertColorSpace(imageFrame, imageData.data, useRGBA);
208216

209-
const imageData = context.createImageData(
210-
imageFrame.columns,
211-
imageFrame.rows
212-
);
217+
imageFrame.imageData = imageData;
218+
imageFrame.pixelData = imageData.data;
219+
}
220+
} else {
221+
if (isJPEGBaseline8BitColor(imageFrame, transferSyntax)) {
222+
// If we don't need the RGBA but the decoding is done with RGBA (the case
223+
// for JPEG Baseline 8 bit color), AND the option specifies to use RGB (no RGBA)
224+
// we need to remove the A channel from pixel data
225+
const colorBuffer = new Uint8ClampedArray(
226+
(imageFrame.pixelData.length / 4) * 3
227+
);
213228

214-
convertColorSpace(imageFrame, imageData.data, useRGBA);
215-
216-
imageFrame.imageData = imageData;
217-
imageFrame.pixelData = imageData.data;
218-
} else if (
219-
isJPEGBaseline8BitColor(imageFrame, transferSyntax) &&
220-
!useRGBA
221-
) {
222-
// If we don't need the RGBA but the decoding is done with RGBA (the case
223-
// for JPEG Baseline 8 bit color), AND the option specifies to use RGB (no RGBA)
224-
// we need to remove the A channel from pixel data
225-
const newPixelData = new imageFrame.pixelData.constructor(
226-
(imageFrame.pixelData.length / 4) * 3
227-
);
229+
// remove the A from the RGBA of the imageFrame
230+
imageFrame.pixelData = removeAFromRGBA(
231+
imageFrame.pixelData,
232+
colorBuffer
233+
);
234+
} else if (imageFrame.photometricInterpretation === 'PALETTE COLOR') {
235+
canvas.height = imageFrame.rows;
236+
canvas.width = imageFrame.columns;
228237

229-
// remove the A from the RGBA of the imageFrame
230-
imageFrame.pixelData = removeAFromRGBA(
231-
imageFrame.pixelData,
232-
newPixelData
233-
);
238+
const context = canvas.getContext('2d');
239+
240+
const imageData = context.createImageData(
241+
imageFrame.columns,
242+
imageFrame.rows
243+
);
244+
245+
convertColorSpace(imageFrame, imageData.data, true);
246+
247+
const colorBuffer = new imageData.data.constructor(
248+
(imageData.data.length / 4) * 3
249+
);
250+
251+
// remove the A from the RGBA of the imageFrame
252+
imageFrame.pixelData = removeAFromRGBA(imageData.data, colorBuffer);
253+
}
234254
}
235255

236256
// calculate smallest and largest PixelValue of the converted pixelData

0 commit comments

Comments
 (0)