Skip to content

Commit 13404ea

Browse files
committed
Make color conversions faster by rewriting them as iterators. This avoids bounds checks in hot loops
1 parent 073e21e commit 13404ea

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/decoder.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -897,34 +897,33 @@ fn choose_color_convert_func(component_count: usize,
897897
fn color_convert_line_null(_data: &mut [u8], _width: usize) {
898898
}
899899

900-
fn color_convert_line_ycbcr(data: &mut [u8], width: usize) {
901-
for i in 0 .. width {
902-
let (r, g, b) = ycbcr_to_rgb(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]);
903-
904-
data[i * 3] = r;
905-
data[i * 3 + 1] = g;
906-
data[i * 3 + 2] = b;
900+
fn color_convert_line_ycbcr(data: &mut [u8], _width: usize) {
901+
for chunk in data.chunks_exact_mut(3) {
902+
let (r, g, b) = ycbcr_to_rgb(chunk[0], chunk[1], chunk[2]);
903+
chunk[0] = r;
904+
chunk[1] = g;
905+
chunk[2] = b;
907906
}
908907
}
909908

910-
fn color_convert_line_ycck(data: &mut [u8], width: usize) {
911-
for i in 0 .. width {
912-
let (r, g, b) = ycbcr_to_rgb(data[i * 4], data[i * 4 + 1], data[i * 4 + 2]);
913-
let k = data[i * 4 + 3];
909+
fn color_convert_line_ycck(data: &mut [u8], _width: usize) {
910+
for chunk in data.chunks_exact_mut(4) {
911+
let (r, g, b) = ycbcr_to_rgb(chunk[0], chunk[1], chunk[2]);
912+
let k = chunk[3];
913+
chunk[0] = r;
914+
chunk[1] = g;
915+
chunk[2] = b;
916+
chunk[3] = 255 - k;
914917

915-
data[i * 4] = r;
916-
data[i * 4 + 1] = g;
917-
data[i * 4 + 2] = b;
918-
data[i * 4 + 3] = 255 - k;
919918
}
920919
}
921920

922-
fn color_convert_line_cmyk(data: &mut [u8], width: usize) {
923-
for i in 0 .. width {
924-
data[i * 4] = 255 - data[i * 4];
925-
data[i * 4 + 1] = 255 - data[i * 4 + 1];
926-
data[i * 4 + 2] = 255 - data[i * 4 + 2];
927-
data[i * 4 + 3] = 255 - data[i * 4 + 3];
921+
fn color_convert_line_cmyk(data: &mut [u8], _width: usize) {
922+
for chunk in data.chunks_exact_mut(4) {
923+
chunk[0] = 255 - chunk[0];
924+
chunk[1] = 255 - chunk[1];
925+
chunk[2] = 255 - chunk[2];
926+
chunk[3] = 255 - chunk[3];
928927
}
929928
}
930929

0 commit comments

Comments
 (0)