Skip to content

Commit 7d843f1

Browse files
committed
Moved in mod and rayon the specific code
1 parent a60f1a6 commit 7d843f1

File tree

3 files changed

+78
-71
lines changed

3 files changed

+78
-71
lines changed

src/decoder.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::parser::{
88
};
99
use crate::read_u8;
1010
use crate::upsampler::Upsampler;
11-
use crate::worker::{with_worker, PreferWorkerKind, RowData, Worker};
11+
use crate::worker::{compute_image_parallel, with_worker, PreferWorkerKind, RowData, Worker};
1212
use alloc::borrow::ToOwned;
1313
use alloc::sync::Arc;
1414
use alloc::vec::Vec;
@@ -1180,71 +1180,7 @@ fn compute_image(
11801180
}
11811181
}
11821182

1183-
#[cfg(all(
1184-
not(any(target_arch = "asmjs", target_arch = "wasm32")),
1185-
feature = "rayon"
1186-
))]
1187-
fn compute_image_parallel(
1188-
components: &[Component],
1189-
data: Vec<Vec<u8>>,
1190-
output_size: Dimensions,
1191-
is_jfif: bool,
1192-
color_transform: Option<AdobeColorTransform>,
1193-
) -> Result<Vec<u8>> {
1194-
use rayon::prelude::*;
1195-
1196-
let color_convert_func = choose_color_convert_func(components.len(), is_jfif, color_transform)?;
1197-
let upsampler = Upsampler::new(components, output_size.width, output_size.height)?;
1198-
let line_size = output_size.width as usize * components.len();
1199-
let mut image = vec![0u8; line_size * output_size.height as usize];
1200-
1201-
image
1202-
.par_chunks_mut(line_size)
1203-
.with_max_len(1)
1204-
.enumerate()
1205-
.for_each(|(row, line)| {
1206-
upsampler.upsample_and_interleave_row(
1207-
&data,
1208-
row,
1209-
output_size.width as usize,
1210-
line,
1211-
color_convert_func,
1212-
);
1213-
});
1214-
1215-
Ok(image)
1216-
}
1217-
1218-
#[cfg(any(
1219-
any(target_arch = "asmjs", target_arch = "wasm32"),
1220-
not(feature = "rayon")
1221-
))]
1222-
fn compute_image_parallel(
1223-
components: &[Component],
1224-
data: Vec<Vec<u8>>,
1225-
output_size: Dimensions,
1226-
is_jfif: bool,
1227-
color_transform: Option<AdobeColorTransform>,
1228-
) -> Result<Vec<u8>> {
1229-
let color_convert_func = choose_color_convert_func(components.len(), is_jfif, color_transform)?;
1230-
let upsampler = Upsampler::new(components, output_size.width, output_size.height)?;
1231-
let line_size = output_size.width as usize * components.len();
1232-
let mut image = vec![0u8; line_size * output_size.height as usize];
1233-
1234-
for (row, line) in image.chunks_mut(line_size).enumerate() {
1235-
upsampler.upsample_and_interleave_row(
1236-
&data,
1237-
row,
1238-
output_size.width as usize,
1239-
line,
1240-
color_convert_func,
1241-
);
1242-
}
1243-
1244-
Ok(image)
1245-
}
1246-
1247-
fn choose_color_convert_func(
1183+
pub(crate) fn choose_color_convert_func(
12481184
component_count: usize,
12491185
_is_jfif: bool,
12501186
color_transform: Option<AdobeColorTransform>,

src/worker/mod.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ mod multithreaded;
66
))]
77
mod rayon;
88

9+
use crate::decoder::choose_color_convert_func;
910
use crate::error::Result;
10-
use crate::parser::Component;
11+
use crate::parser::{AdobeColorTransform, Component, Dimensions};
12+
use crate::upsampler::Upsampler;
1113
use alloc::sync::Arc;
1214
use alloc::vec::Vec;
1315

@@ -50,3 +52,38 @@ pub fn with_worker<T>(prefer: PreferWorkerKind, f: impl FnOnce(&mut dyn Worker)
5052
_ => self::immediate::with_immediate(f),
5153
}
5254
}
55+
56+
pub fn compute_image_parallel(
57+
components: &[Component],
58+
data: Vec<Vec<u8>>,
59+
output_size: Dimensions,
60+
is_jfif: bool,
61+
color_transform: Option<AdobeColorTransform>,
62+
) -> Result<Vec<u8>> {
63+
#[cfg(all(
64+
not(any(target_arch = "asmjs", target_arch = "wasm32")),
65+
feature = "rayon"
66+
))]
67+
return rayon::compute_image_parallel(components, data, output_size, is_jfif, color_transform);
68+
69+
#[allow(unreachable_code)]
70+
{
71+
let color_convert_func =
72+
choose_color_convert_func(components.len(), is_jfif, color_transform)?;
73+
let upsampler = Upsampler::new(components, output_size.width, output_size.height)?;
74+
let line_size = output_size.width as usize * components.len();
75+
let mut image = vec![0u8; line_size * output_size.height as usize];
76+
77+
for (row, line) in image.chunks_mut(line_size).enumerate() {
78+
upsampler.upsample_and_interleave_row(
79+
&data,
80+
row,
81+
output_size.width as usize,
82+
line,
83+
color_convert_func,
84+
);
85+
}
86+
87+
Ok(image)
88+
}
89+
}

src/worker/rayon.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use core::convert::TryInto;
22

3-
use crate::decoder::MAX_COMPONENTS;
3+
use rayon::iter::{IndexedParallelIterator, ParallelIterator};
4+
use rayon::slice::ParallelSliceMut;
5+
6+
use crate::decoder::choose_color_convert_func;
47
use crate::error::Result;
58
use crate::idct::dequantize_and_idct_block;
6-
use crate::parser::Component;
9+
use crate::parser::{AdobeColorTransform, Component};
10+
use crate::upsampler::Upsampler;
11+
use crate::{decoder::MAX_COMPONENTS, parser::Dimensions};
712

813
use std::sync::Arc;
914

@@ -166,8 +171,8 @@ impl super::Worker for Scoped {
166171
let quantization_table = inner.quantization_tables[index].as_ref().unwrap().clone();
167172

168173
inner.offsets[index] += metadata.bytes_used();
169-
let (result_block, tail) = core::mem::take(&mut result_blocks[index])
170-
.split_at_mut(metadata.bytes_used());
174+
let (result_block, tail) =
175+
core::mem::take(&mut result_blocks[index]).split_at_mut(metadata.bytes_used());
171176
result_blocks[index] = tail;
172177

173178
scope.spawn(move |_| {
@@ -190,3 +195,32 @@ impl ComponentMetadata {
190195
self.block_count * self.dct_scale * self.dct_scale
191196
}
192197
}
198+
199+
pub fn compute_image_parallel(
200+
components: &[Component],
201+
data: Vec<Vec<u8>>,
202+
output_size: Dimensions,
203+
is_jfif: bool,
204+
color_transform: Option<AdobeColorTransform>,
205+
) -> Result<Vec<u8>> {
206+
let color_convert_func = choose_color_convert_func(components.len(), is_jfif, color_transform)?;
207+
let upsampler = Upsampler::new(components, output_size.width, output_size.height)?;
208+
let line_size = output_size.width as usize * components.len();
209+
let mut image = vec![0u8; line_size * output_size.height as usize];
210+
211+
image
212+
.par_chunks_mut(line_size)
213+
.with_max_len(1)
214+
.enumerate()
215+
.for_each(|(row, line)| {
216+
upsampler.upsample_and_interleave_row(
217+
&data,
218+
row,
219+
output_size.width as usize,
220+
line,
221+
color_convert_func,
222+
);
223+
});
224+
225+
Ok(image)
226+
}

0 commit comments

Comments
 (0)