Skip to content

Commit

Permalink
chore: Changed large if-else to a switch statement (#389)
Browse files Browse the repository at this point in the history
Changed large if-else statement to a switch statement.

-divbit2

Co-authored-by: divbit2 <[email protected]>
  • Loading branch information
swederik and divbit2 authored Jul 15, 2021
1 parent 1e92090 commit 111c4a3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 89 deletions.
96 changes: 48 additions & 48 deletions src/imageLoader/decodeImageFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,56 @@ function decodeImageFrame(
canvas,
options = {}
) {
// TODO: Turn this into a switch statement instead
if (transferSyntax === '1.2.840.10008.1.2') {
// Implicit VR Little Endian
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.1') {
// Explicit VR Little Endian
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.2') {
// Explicit VR Big Endian (retired)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.1.99') {
// Deflate transfer syntax (deflated by dicomParser)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.5') {
// RLE Lossless
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.50') {
// JPEG Baseline lossy process 1 (8 bit)
switch (transferSyntax) {
case '1.2.840.10008.1.2':
// Implicit VR Little Endian
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.1':
// Explicit VR Little Endian
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.2':
// Explicit VR Big Endian (retired)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.1.99':
// Deflate transfer syntax (deflated by dicomParser)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.5':
// RLE Lossless
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.50':
// JPEG Baseline lossy process 1 (8 bit)

// 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)
) {
return decodeJPEGBaseline8BitColor(imageFrame, pixelData, canvas);
}
// 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)
) {
return decodeJPEGBaseline8BitColor(imageFrame, pixelData, canvas);
}

return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.51') {
// JPEG Baseline lossy process 2 & 4 (12 bit)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.57') {
// JPEG Lossless, Nonhierarchical (Processes 14)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.70') {
// JPEG Lossless, Nonhierarchical (Processes 14 [Selection 1])
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.80') {
// JPEG-LS Lossless Image Compression
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.81') {
// JPEG-LS Lossy (Near-Lossless) Image Compression
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.90') {
// JPEG 2000 Lossless
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.91') {
// JPEG 2000 Lossy
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.51':
// JPEG Baseline lossy process 2 & 4 (12 bit)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.57':
// JPEG Lossless, Nonhierarchical (Processes 14)
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.70':
// JPEG Lossless, Nonhierarchical (Processes 14 [Selection 1])
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.80':
// JPEG-LS Lossless Image Compression
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.81':
// JPEG-LS Lossy (Near-Lossless) Image Compression
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.90':
// JPEG 2000 Lossless
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
case '1.2.840.10008.1.2.4.91':
// JPEG 2000 Lossy
return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
}

/* Don't know if these work...
Expand Down
96 changes: 55 additions & 41 deletions src/shared/decodeImageFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,61 @@ function decodeImageFrame(
) {
const start = new Date().getTime();

if (transferSyntax === '1.2.840.10008.1.2') {
// Implicit VR Little Endian
imageFrame = decodeLittleEndian(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.1') {
// Explicit VR Little Endian
imageFrame = decodeLittleEndian(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.2') {
// Explicit VR Big Endian (retired)
imageFrame = decodeBigEndian(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.1.99') {
// Deflate transfer syntax (deflated by dicomParser)
imageFrame = decodeLittleEndian(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.5') {
// RLE Lossless
imageFrame = decodeRLE(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.50') {
// JPEG Baseline lossy process 1 (8 bit)
imageFrame = decodeJPEGBaseline(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.51') {
// JPEG Baseline lossy process 2 & 4 (12 bit)
imageFrame = decodeJPEGBaseline(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.57') {
// JPEG Lossless, Nonhierarchical (Processes 14)
imageFrame = decodeJPEGLossless(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.70') {
// JPEG Lossless, Nonhierarchical (Processes 14 [Selection 1])
imageFrame = decodeJPEGLossless(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.80') {
// JPEG-LS Lossless Image Compression
imageFrame = decodeJPEGLS(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.81') {
// JPEG-LS Lossy (Near-Lossless) Image Compression
imageFrame = decodeJPEGLS(imageFrame, pixelData);
} else if (transferSyntax === '1.2.840.10008.1.2.4.90') {
// JPEG 2000 Lossless
imageFrame = decodeJPEG2000(imageFrame, pixelData, decodeConfig, options);
} else if (transferSyntax === '1.2.840.10008.1.2.4.91') {
// JPEG 2000 Lossy
imageFrame = decodeJPEG2000(imageFrame, pixelData, decodeConfig, options);
} else {
throw new Error(`no decoder for transfer syntax ${transferSyntax}`);
switch (transferSyntax) {
case '1.2.840.10008.1.2':
// Implicit VR Little Endian
imageFrame = decodeLittleEndian(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.1':
// Explicit VR Little Endian
imageFrame = decodeLittleEndian(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.2':
// Explicit VR Big Endian (retired)
imageFrame = decodeBigEndian(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.1.99':
// Deflate transfer syntax (deflated by dicomParser)
imageFrame = decodeLittleEndian(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.5':
// RLE Lossless
imageFrame = decodeRLE(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.50':
// JPEG Baseline lossy process 1 (8 bit)
imageFrame = decodeJPEGBaseline(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.51':
// JPEG Baseline lossy process 2 & 4 (12 bit)
imageFrame = decodeJPEGBaseline(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.57':
// JPEG Lossless, Nonhierarchical (Processes 14)
imageFrame = decodeJPEGLossless(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.70':
// JPEG Lossless, Nonhierarchical (Processes 14 [Selection 1])
imageFrame = decodeJPEGLossless(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.80':
// JPEG-LS Lossless Image Compression
imageFrame = decodeJPEGLS(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.81':
// JPEG-LS Lossy (Near-Lossless) Image Compression
imageFrame = decodeJPEGLS(imageFrame, pixelData);
break;
case '1.2.840.10008.1.2.4.90':
// JPEG 2000 Lossless
imageFrame = decodeJPEG2000(imageFrame, pixelData, decodeConfig, options);
break;
case '1.2.840.10008.1.2.4.91':
// JPEG 2000 Lossy
imageFrame = decodeJPEG2000(imageFrame, pixelData, decodeConfig, options);
break;
default:
throw new Error(`no decoder for transfer syntax ${transferSyntax}`);
}

/* Don't know if these work...
Expand Down

0 comments on commit 111c4a3

Please sign in to comment.