Skip to content

Commit 7b3efc2

Browse files
committed
(#336) Limit switching of channels only to BGR images
1 parent 2a16762 commit 7b3efc2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Diff for: lib/provider/io/imageToJimp.function.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import Jimp from "jimp";
22
import {Image} from "../../image.class";
3+
import {ColorMode} from "../../colormode.enum";
34

45
export function imageToJimp(image: Image): Jimp {
56
const jimpImage = new Jimp({
67
data: image.data,
78
width: image.width,
89
height: image.height
910
});
10-
// Images treat data in BGR format, so we have to switch red and blue color channels
11-
jimpImage.scan(0, 0, jimpImage.bitmap.width, jimpImage.bitmap.height, function (_, __, idx) {
12-
const red = this.bitmap.data[idx];
13-
this.bitmap.data[idx] = this.bitmap.data[idx + 2];
14-
this.bitmap.data[idx + 2] = red;
15-
});
11+
if (image.colorMode === ColorMode.BGR) {
12+
// Image treats data in BGR format, so we have to switch red and blue color channels
13+
jimpImage.scan(0, 0, jimpImage.bitmap.width, jimpImage.bitmap.height, function (_, __, idx) {
14+
const red = this.bitmap.data[idx];
15+
this.bitmap.data[idx] = this.bitmap.data[idx + 2];
16+
this.bitmap.data[idx + 2] = red;
17+
});
18+
}
1619
return jimpImage;
1720
}

0 commit comments

Comments
 (0)