Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #343 - support web-workers for jpeg-8 color using built-in features 'createImageBitmap' and 'OffscreenCanvas' #344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/imageLoader/createImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function createImage(imageId, pixelData, transferSyntax, options) {
imageFrame.smallestPixelValue = minMax.min;
imageFrame.largestPixelValue = minMax.max;
}
} else {
imageFrame.pixelData = new Uint8Array(imageFrame.pixelData);
}

const image = {
Expand Down
8 changes: 5 additions & 3 deletions src/imageLoader/decodeImageFrame.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getOptions } from './internal/options.js';
import webWorkerManager from './webWorkerManager.js';
import decodeJPEGBaseline8BitColor from './decodeJPEGBaseline8BitColor.js';

import isNativeJpeg8ColorDecoderSupported from './isNativeJpeg8ColorDecoderSupported.js';
import isJPEGBaseline8BitColor from './isJPEGBaseline8BitColor.js';
// TODO: Find a way to allow useWebWorkers: false that doesn't make the main bundle huge
import { default as decodeImageFrameHandler } from '../shared/decodeImageFrame.js';
import calculateMinMax from '../shared/calculateMinMax.js';
Expand Down Expand Up @@ -88,8 +89,9 @@ function decodeImageFrame(
// Handle 8-bit JPEG Baseline color images using the browser's built-in
// JPEG decoding
if (
imageFrame.bitsAllocated === 8 &&
(imageFrame.samplesPerPixel === 3 || imageFrame.samplesPerPixel === 4)
isJPEGBaseline8BitColor(imageFrame, transferSyntax) &&
(options.useBrowserDecoderForJpeg8Color ||
!isNativeJpeg8ColorDecoderSupported())
) {
return decodeJPEGBaseline8BitColor(imageFrame, pixelData, canvas);
}
Expand Down
3 changes: 3 additions & 0 deletions src/imageLoader/isNativeJpeg8ColorDecoderSupported.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isNativeJpeg8ColorDecoderSupported() {
return 'createImageBitmap' in window && 'OffscreenCanvas' in window;
}
7 changes: 7 additions & 0 deletions src/shared/decodeImageFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import decodeJPEGBaseline from './decoders/decodeJPEGBaseline.js';
import decodeJPEGLossless from './decoders/decodeJPEGLossless.js';
import decodeJPEGLS from './decoders/decodeJPEGLS.js';
import decodeJPEG2000 from './decoders/decodeJPEG2000.js';
import decodeJPEGBaseline8BitColor from './decoders/decodeJPEGBaseline8BitColor.js';

function decodeImageFrame(
imageFrame,
Expand Down Expand Up @@ -32,6 +33,12 @@ function decodeImageFrame(
imageFrame = decodeRLE(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.50') {
// JPEG Baseline lossy process 1 (8 bit)
if (
imageFrame.bitsAllocated === 8 &&
(imageFrame.samplesPerPixel === 3 || imageFrame.samplesPerPixel === 4)
) {
return decodeJPEGBaseline8BitColor(imageFrame, pixelData);
}
imageFrame = decodeJPEGBaseline(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.51') {
// JPEG Baseline lossy process 2 & 4 (12 bit)
Expand Down
40 changes: 40 additions & 0 deletions src/shared/decoders/decodeJPEGBaseline8BitColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function decodeJPEGBaseline8BitColor(imageFrame, pixelData) {
const imgBlob = new Blob([pixelData], { type: 'image/jpeg' });

return new Promise((resolve, reject) => {
try {
const start = new Date().getTime();

createImageBitmap(imgBlob).then(imageBitmap => {
const offscreen = new OffscreenCanvas(
imageBitmap.width,
imageBitmap.height
);

imageFrame.rows = imageBitmap.height;
imageFrame.columns = imageBitmap.width;
const context = offscreen.getContext('2d');

context.drawImage(imageBitmap, 0, 0);
const imageData = context.getImageData(
0,
0,
imageBitmap.width,
imageBitmap.height
);
const end = new Date().getTime();

imageFrame.pixelData = imageData.data;
imageFrame.imageData = imageData;
imageFrame.decodeTimeInMS = end - start;

// calculate smallest and largest PixelValue
resolve(imageFrame);
});
} catch (error) {
reject(error);
}
});
}

export default decodeJPEGBaseline8BitColor;
33 changes: 18 additions & 15 deletions src/webWorker/decodeTask/decodeTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,38 @@ function handler(data, doneCallback) {

const strict =
decodeConfig && decodeConfig.decodeTask && decodeConfig.decodeTask.strict;
const imageFrame = data.data.imageFrame;
const imageFrameData = data.data.imageFrame;

// convert pixel data from ArrayBuffer to Uint8Array since web workers support passing ArrayBuffers but
// not typed arrays
const pixelData = new Uint8Array(data.data.pixelData);

decodeImageFrame(
imageFrame,
const result = decodeImageFrame(
imageFrameData,
data.data.transferSyntax,
pixelData,
decodeConfig.decodeTask,
data.data.options
);

if (!imageFrame.pixelData) {
throw new Error(
'decodeTask: imageFrame.pixelData is undefined after decoding'
);
}
Promise.resolve(result).then(imageFrame => {
if (!imageFrame.pixelData) {
throw new Error(
'decodeTask: imageFrame.pixelData is undefined after decoding'
);
}

calculateMinMax(imageFrame, strict);
// TODO: do we need this here? min/max is being calculated in imageLoader\createImage again!
calculateMinMax(imageFrame, strict);

// convert from TypedArray to ArrayBuffer since web workers support passing ArrayBuffers but not
// typed arrays
imageFrame.pixelData = imageFrame.pixelData.buffer;
// convert from TypedArray to ArrayBuffer since web workers support passing ArrayBuffers but not
// typed arrays
imageFrame.pixelData = imageFrame.pixelData.buffer;

// invoke the callback with our result and pass the pixelData in the transferList to move it to
// UI thread without making a copy
doneCallback(imageFrame, [imageFrame.pixelData]);
// invoke the callback with our result and pass the pixelData in the transferList to move it to
// UI thread without making a copy
doneCallback(imageFrame, [imageFrame.pixelData]);
});
}

export default {
Expand Down