Skip to content

Commit 5583aa5

Browse files
authored
perf: add option to use RGB instead of RGBA (#457)
* feat: add transferSyntaxUID return via metadata * perf: add option to use RGB instead of RGBA * fix: build * fix: properly fixes the useRGBA option
1 parent deb1780 commit 5583aa5

File tree

11 files changed

+233
-76
lines changed

11 files changed

+233
-76
lines changed

src/imageLoader/colorSpaceConverters/convertPALETTECOLOR.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function convertLUTto8Bit(lut, shift) {
1515
* Convert pixel data with PALETTE COLOR Photometric Interpretation to RGBA
1616
*
1717
* @param {ImageFrame} imageFrame
18-
* @param {Uint8ClampedArray} rgbaBuffer
18+
* @param {Uint8ClampedArray} colorBuffer
1919
* @returns {void}
2020
*/
21-
export default function (imageFrame, rgbaBuffer) {
21+
export default function (imageFrame, colorBuffer, useRGBA) {
2222
const numPixels = imageFrame.columns * imageFrame.rows;
2323
const pixelData = imageFrame.pixelData;
2424
const rData = imageFrame.redPaletteColorLookupTableData;
@@ -28,7 +28,7 @@ export default function (imageFrame, rgbaBuffer) {
2828

2929
let palIndex = 0;
3030

31-
let rgbaIndex = 0;
31+
let bufferIndex = 0;
3232

3333
const start = imageFrame.redPaletteColorLookupTableDescriptor[1];
3434
const shift =
@@ -38,6 +38,27 @@ export default function (imageFrame, rgbaBuffer) {
3838
const gDataCleaned = convertLUTto8Bit(gData, shift);
3939
const bDataCleaned = convertLUTto8Bit(bData, shift);
4040

41+
if (useRGBA) {
42+
for (let i = 0; i < numPixels; ++i) {
43+
let value = pixelData[palIndex++];
44+
45+
if (value < start) {
46+
value = 0;
47+
} else if (value > start + len - 1) {
48+
value = len - 1;
49+
} else {
50+
value -= start;
51+
}
52+
53+
colorBuffer[bufferIndex++] = rDataCleaned[value];
54+
colorBuffer[bufferIndex++] = gDataCleaned[value];
55+
colorBuffer[bufferIndex++] = bDataCleaned[value];
56+
colorBuffer[bufferIndex++] = 255;
57+
}
58+
59+
return;
60+
}
61+
4162
for (let i = 0; i < numPixels; ++i) {
4263
let value = pixelData[palIndex++];
4364

@@ -49,9 +70,8 @@ export default function (imageFrame, rgbaBuffer) {
4970
value -= start;
5071
}
5172

52-
rgbaBuffer[rgbaIndex++] = rDataCleaned[value];
53-
rgbaBuffer[rgbaIndex++] = gDataCleaned[value];
54-
rgbaBuffer[rgbaIndex++] = bDataCleaned[value];
55-
rgbaBuffer[rgbaIndex++] = 255;
73+
colorBuffer[bufferIndex++] = rDataCleaned[value];
74+
colorBuffer[bufferIndex++] = gDataCleaned[value];
75+
colorBuffer[bufferIndex++] = bDataCleaned[value];
5676
}
5777
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (imageFrame, rgbaBuffer) {
1+
export default function (imageFrame, colorBuffer, useRGBA) {
22
if (imageFrame === undefined) {
33
throw new Error('decodeRGB: rgbBuffer must not be undefined');
44
}
@@ -10,12 +10,19 @@ export default function (imageFrame, rgbaBuffer) {
1010

1111
let rgbIndex = 0;
1212

13-
let rgbaIndex = 0;
13+
let bufferIndex = 0;
1414

15-
for (let i = 0; i < numPixels; i++) {
16-
rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // red
17-
rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // green
18-
rgbaBuffer[rgbaIndex++] = imageFrame[rgbIndex++]; // blue
19-
rgbaBuffer[rgbaIndex++] = 255; // alpha
15+
if (useRGBA) {
16+
for (let i = 0; i < numPixels; i++) {
17+
colorBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // red
18+
colorBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // green
19+
colorBuffer[bufferIndex++] = imageFrame[rgbIndex++]; // blue
20+
colorBuffer[bufferIndex++] = 255; // alpha
21+
}
22+
23+
return;
2024
}
25+
26+
// if RGB buffer
27+
colorBuffer.set(imageFrame);
2128
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (imageFrame, rgbaBuffer) {
1+
export default function (imageFrame, colorBuffer, useRGBA) {
22
if (imageFrame === undefined) {
33
throw new Error('decodeRGB: rgbBuffer must not be undefined');
44
}
@@ -8,18 +8,25 @@ export default function (imageFrame, rgbaBuffer) {
88

99
const numPixels = imageFrame.length / 3;
1010

11-
let rgbaIndex = 0;
11+
let bufferIndex = 0;
1212

1313
let rIndex = 0;
1414

1515
let gIndex = numPixels;
1616

1717
let bIndex = numPixels * 2;
1818

19-
for (let i = 0; i < numPixels; i++) {
20-
rgbaBuffer[rgbaIndex++] = imageFrame[rIndex++]; // red
21-
rgbaBuffer[rgbaIndex++] = imageFrame[gIndex++]; // green
22-
rgbaBuffer[rgbaIndex++] = imageFrame[bIndex++]; // blue
23-
rgbaBuffer[rgbaIndex++] = 255; // alpha
19+
if (useRGBA) {
20+
for (let i = 0; i < numPixels; i++) {
21+
colorBuffer[bufferIndex++] = imageFrame[rIndex++]; // red
22+
colorBuffer[bufferIndex++] = imageFrame[gIndex++]; // green
23+
colorBuffer[bufferIndex++] = imageFrame[bIndex++]; // blue
24+
colorBuffer[bufferIndex++] = 255; // alpha
25+
}
26+
27+
return;
2428
}
29+
30+
// if RGB buffer
31+
colorBuffer.set(imageFrame);
2532
}
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (imageFrame, rgbaBuffer) {
1+
export default function (imageFrame, colorBuffer, useRGBA) {
22
if (imageFrame === undefined) {
33
throw new Error('decodeRGB: ybrBuffer must not be undefined');
44
}
@@ -10,22 +10,45 @@ export default function (imageFrame, rgbaBuffer) {
1010

1111
let ybrIndex = 0;
1212

13-
let rgbaIndex = 0;
13+
let bufferIndex = 0;
14+
15+
if (useRGBA) {
16+
for (let i = 0; i < numPixels; i += 2) {
17+
const y1 = imageFrame[ybrIndex++];
18+
const y2 = imageFrame[ybrIndex++];
19+
const cb = imageFrame[ybrIndex++];
20+
const cr = imageFrame[ybrIndex++];
21+
22+
colorBuffer[bufferIndex++] = y1 + 1.402 * (cr - 128); // red
23+
colorBuffer[bufferIndex++] =
24+
y1 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
25+
colorBuffer[bufferIndex++] = y1 + 1.772 * (cb - 128); // blue
26+
colorBuffer[bufferIndex++] = 255; // alpha
27+
28+
colorBuffer[bufferIndex++] = y2 + 1.402 * (cr - 128); // red
29+
colorBuffer[bufferIndex++] =
30+
y2 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
31+
colorBuffer[bufferIndex++] = y2 + 1.772 * (cb - 128); // blue
32+
colorBuffer[bufferIndex++] = 255; // alpha
33+
}
34+
35+
return;
36+
}
1437

1538
for (let i = 0; i < numPixels; i += 2) {
1639
const y1 = imageFrame[ybrIndex++];
1740
const y2 = imageFrame[ybrIndex++];
1841
const cb = imageFrame[ybrIndex++];
1942
const cr = imageFrame[ybrIndex++];
2043

21-
rgbaBuffer[rgbaIndex++] = y1 + 1.402 * (cr - 128); // red
22-
rgbaBuffer[rgbaIndex++] = y1 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
23-
rgbaBuffer[rgbaIndex++] = y1 + 1.772 * (cb - 128); // blue
24-
rgbaBuffer[rgbaIndex++] = 255; // alpha
44+
colorBuffer[bufferIndex++] = y1 + 1.402 * (cr - 128); // red
45+
colorBuffer[bufferIndex++] =
46+
y1 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
47+
colorBuffer[bufferIndex++] = y1 + 1.772 * (cb - 128); // blue
2548

26-
rgbaBuffer[rgbaIndex++] = y2 + 1.402 * (cr - 128); // red
27-
rgbaBuffer[rgbaIndex++] = y2 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
28-
rgbaBuffer[rgbaIndex++] = y2 + 1.772 * (cb - 128); // blue
29-
rgbaBuffer[rgbaIndex++] = 255; // alpha
49+
colorBuffer[bufferIndex++] = y2 + 1.402 * (cr - 128); // red
50+
colorBuffer[bufferIndex++] =
51+
y2 - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
52+
colorBuffer[bufferIndex++] = y2 + 1.772 * (cb - 128); // blue
3053
}
3154
}
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (imageFrame, rgbaBuffer) {
1+
export default function (imageFrame, colorBuffer, useRGBA) {
22
if (imageFrame === undefined) {
33
throw new Error('decodeRGB: ybrBuffer must not be undefined');
44
}
@@ -10,16 +10,32 @@ export default function (imageFrame, rgbaBuffer) {
1010

1111
let ybrIndex = 0;
1212

13-
let rgbaIndex = 0;
13+
let bufferIndex = 0;
14+
15+
if (useRGBA) {
16+
for (let i = 0; i < numPixels; i++) {
17+
const y = imageFrame[ybrIndex++];
18+
const cb = imageFrame[ybrIndex++];
19+
const cr = imageFrame[ybrIndex++];
20+
21+
colorBuffer[bufferIndex++] = y + 1.402 * (cr - 128); // red
22+
colorBuffer[bufferIndex++] =
23+
y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
24+
colorBuffer[bufferIndex++] = y + 1.772 * (cb - 128); // blue
25+
colorBuffer[bufferIndex++] = 255; // alpha
26+
}
27+
28+
return;
29+
}
1430

1531
for (let i = 0; i < numPixels; i++) {
1632
const y = imageFrame[ybrIndex++];
1733
const cb = imageFrame[ybrIndex++];
1834
const cr = imageFrame[ybrIndex++];
1935

20-
rgbaBuffer[rgbaIndex++] = y + 1.402 * (cr - 128); // red
21-
rgbaBuffer[rgbaIndex++] = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
22-
rgbaBuffer[rgbaIndex++] = y + 1.772 * (cb - 128); // blue
23-
rgbaBuffer[rgbaIndex++] = 255; // alpha
36+
colorBuffer[bufferIndex++] = y + 1.402 * (cr - 128); // red
37+
colorBuffer[bufferIndex++] =
38+
y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
39+
colorBuffer[bufferIndex++] = y + 1.772 * (cb - 128); // blue
2440
}
2541
}
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (imageFrame, rgbaBuffer) {
1+
export default function (imageFrame, colorBuffer, useRGBA) {
22
if (imageFrame === undefined) {
33
throw new Error('decodeRGB: ybrBuffer must not be undefined');
44
}
@@ -8,22 +8,38 @@ export default function (imageFrame, rgbaBuffer) {
88

99
const numPixels = imageFrame.length / 3;
1010

11-
let rgbaIndex = 0;
11+
let bufferIndex = 0;
1212

1313
let yIndex = 0;
1414

1515
let cbIndex = numPixels;
1616

1717
let crIndex = numPixels * 2;
1818

19+
if (useRGBA) {
20+
for (let i = 0; i < numPixels; i++) {
21+
const y = imageFrame[yIndex++];
22+
const cb = imageFrame[cbIndex++];
23+
const cr = imageFrame[crIndex++];
24+
25+
colorBuffer[bufferIndex++] = y + 1.402 * (cr - 128); // red
26+
colorBuffer[bufferIndex++] =
27+
y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
28+
colorBuffer[bufferIndex++] = y + 1.772 * (cb - 128); // blue
29+
colorBuffer[bufferIndex++] = 255; // alpha
30+
}
31+
32+
return;
33+
}
34+
1935
for (let i = 0; i < numPixels; i++) {
2036
const y = imageFrame[yIndex++];
2137
const cb = imageFrame[cbIndex++];
2238
const cr = imageFrame[crIndex++];
2339

24-
rgbaBuffer[rgbaIndex++] = y + 1.402 * (cr - 128); // red
25-
rgbaBuffer[rgbaIndex++] = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
26-
rgbaBuffer[rgbaIndex++] = y + 1.772 * (cb - 128); // blue
27-
rgbaBuffer[rgbaIndex++] = 255; // alpha
40+
colorBuffer[bufferIndex++] = y + 1.402 * (cr - 128); // red
41+
colorBuffer[bufferIndex++] =
42+
y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128); // green
43+
colorBuffer[bufferIndex++] = y + 1.772 * (cb - 128); // blue
2844
}
2945
}

src/imageLoader/convertColorSpace.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,36 @@ import {
77
convertPALETTECOLOR,
88
} from './colorSpaceConverters/index.js';
99

10-
function convertRGB(imageFrame, rgbaBuffer) {
10+
function convertRGB(imageFrame, colorBuffer, useRGBA) {
1111
if (imageFrame.planarConfiguration === 0) {
12-
convertRGBColorByPixel(imageFrame.pixelData, rgbaBuffer);
12+
convertRGBColorByPixel(imageFrame.pixelData, colorBuffer, useRGBA);
1313
} else {
14-
convertRGBColorByPlane(imageFrame.pixelData, rgbaBuffer);
14+
convertRGBColorByPlane(imageFrame.pixelData, colorBuffer, useRGBA);
1515
}
1616
}
1717

18-
function convertYBRFull(imageFrame, rgbaBuffer) {
18+
function convertYBRFull(imageFrame, colorBuffer, useRGBA) {
1919
if (imageFrame.planarConfiguration === 0) {
20-
convertYBRFullByPixel(imageFrame.pixelData, rgbaBuffer);
20+
convertYBRFullByPixel(imageFrame.pixelData, colorBuffer, useRGBA);
2121
} else {
22-
convertYBRFullByPlane(imageFrame.pixelData, rgbaBuffer);
22+
convertYBRFullByPlane(imageFrame.pixelData, colorBuffer, useRGBA);
2323
}
2424
}
2525

26-
export default function convertColorSpace(imageFrame, imageData) {
27-
const rgbaBuffer = imageData.data;
26+
export default function convertColorSpace(imageFrame, colorBuffer, useRGBA) {
2827
// convert based on the photometric interpretation
29-
3028
if (imageFrame.photometricInterpretation === 'RGB') {
31-
convertRGB(imageFrame, rgbaBuffer);
29+
convertRGB(imageFrame, colorBuffer, useRGBA);
3230
} else if (imageFrame.photometricInterpretation === 'YBR_RCT') {
33-
convertRGB(imageFrame, rgbaBuffer);
31+
convertRGB(imageFrame, colorBuffer, useRGBA);
3432
} else if (imageFrame.photometricInterpretation === 'YBR_ICT') {
35-
convertRGB(imageFrame, rgbaBuffer);
33+
convertRGB(imageFrame, colorBuffer, useRGBA);
3634
} else if (imageFrame.photometricInterpretation === 'PALETTE COLOR') {
37-
convertPALETTECOLOR(imageFrame, rgbaBuffer);
35+
convertPALETTECOLOR(imageFrame, colorBuffer, useRGBA);
3836
} else if (imageFrame.photometricInterpretation === 'YBR_FULL_422') {
39-
convertYBRFull422ByPixel(imageFrame.pixelData, rgbaBuffer);
37+
convertYBRFull422ByPixel(imageFrame.pixelData, colorBuffer, useRGBA);
4038
} else if (imageFrame.photometricInterpretation === 'YBR_FULL') {
41-
convertYBRFull(imageFrame, rgbaBuffer);
39+
convertYBRFull(imageFrame, colorBuffer, useRGBA);
4240
} else {
4341
throw new Error(
4442
`No color space conversion for photometric interpretation ${imageFrame.photometricInterpretation}`

0 commit comments

Comments
 (0)