Skip to content

Commit 0f69cca

Browse files
authored
Merge pull request image-rs#56 from redox-os/master
Make rayon optional
2 parents 2199f92 + 7baf38d commit 0f69cca

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ matrix:
1212
- rust: nightly
1313
env:
1414
- FEATURES=""
15+
- FEATURES="rayon"
1516
script:
1617
- cargo build --verbose --no-default-features --features "$FEATURES" &&
1718
cargo test --verbose --no-default-features --features "$FEATURES"

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ exclude = ["tests/*"]
1212

1313
[dependencies]
1414
byteorder = "0.5"
15-
rayon = "0.5"
15+
16+
[dependencies.rayon]
17+
version = "0.5"
18+
optional = true
19+
20+
[features]
21+
default = ["rayon"]
1622

1723
[dev-dependencies]
1824
walkdir = "0.1"

src/decoder.rs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use huffman::{HuffmanDecoder, HuffmanTable};
44
use marker::Marker;
55
use parser::{AdobeColorTransform, AppData, CodingProcess, Component, Dimensions, EntropyCoding, FrameInfo,
66
parse_app, parse_com, parse_dht, parse_dqt, parse_dri, parse_sof, parse_sos, ScanInfo};
7-
use rayon::par_iter::*;
87
use upsampler::Upsampler;
98
use std::cmp;
109
use std::io::Read;
@@ -755,25 +754,58 @@ fn compute_image(components: &[Component],
755754
Ok(buffer)
756755
}
757756
else {
758-
let color_convert_func = try!(choose_color_convert_func(components.len(), is_jfif, color_transform));
759-
let upsampler = try!(Upsampler::new(components, output_size.width, output_size.height));
760-
let line_size = output_size.width as usize * components.len();
761-
let mut image = vec![0u8; line_size * output_size.height as usize];
762-
763-
image.chunks_mut(line_size)
764-
.collect::<Vec<&mut [u8]>>()
765-
.par_iter_mut()
766-
.weight_max()
767-
.enumerate()
768-
.for_each(|(row, line)| {
769-
upsampler.upsample_and_interleave_row(data, row, output_size.width as usize, *line);
770-
color_convert_func(*line, output_size.width as usize);
771-
});
772-
773-
Ok(image)
757+
compute_image_parallel(components, data, output_size, is_jfif, color_transform)
774758
}
775759
}
776760

761+
#[cfg(feature="rayon")]
762+
fn compute_image_parallel(components: &[Component],
763+
data: &[Vec<u8>],
764+
output_size: Dimensions,
765+
is_jfif: bool,
766+
color_transform: Option<AdobeColorTransform>) -> Result<Vec<u8>> {
767+
use rayon::par_iter::*;
768+
769+
let color_convert_func = try!(choose_color_convert_func(components.len(), is_jfif, color_transform));
770+
let upsampler = try!(Upsampler::new(components, output_size.width, output_size.height));
771+
let line_size = output_size.width as usize * components.len();
772+
let mut image = vec![0u8; line_size * output_size.height as usize];
773+
774+
image.chunks_mut(line_size)
775+
.collect::<Vec<&mut [u8]>>()
776+
.par_iter_mut()
777+
.weight_max()
778+
.enumerate()
779+
.for_each(|(row, line)| {
780+
upsampler.upsample_and_interleave_row(data, row, output_size.width as usize, *line);
781+
color_convert_func(*line, output_size.width as usize);
782+
});
783+
784+
Ok(image)
785+
}
786+
787+
#[cfg(not(feature="rayon"))]
788+
fn compute_image_parallel(components: &[Component],
789+
data: &[Vec<u8>],
790+
output_size: Dimensions,
791+
is_jfif: bool,
792+
color_transform: Option<AdobeColorTransform>) -> Result<Vec<u8>> {
793+
let color_convert_func = try!(choose_color_convert_func(components.len(), is_jfif, color_transform));
794+
let upsampler = try!(Upsampler::new(components, output_size.width, output_size.height));
795+
let line_size = output_size.width as usize * components.len();
796+
let mut image = vec![0u8; line_size * output_size.height as usize];
797+
798+
for (row, line) in image.chunks_mut(line_size)
799+
.collect::<Vec<&mut [u8]>>()
800+
.iter_mut()
801+
.enumerate() {
802+
upsampler.upsample_and_interleave_row(data, row, output_size.width as usize, *line);
803+
color_convert_func(*line, output_size.width as usize);
804+
}
805+
806+
Ok(image)
807+
}
808+
777809
fn choose_color_convert_func(component_count: usize,
778810
_is_jfif: bool,
779811
color_transform: Option<AdobeColorTransform>)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate byteorder;
2+
#[cfg(feature="rayon")]
23
extern crate rayon;
34

45
pub use decoder::{Decoder, ImageInfo, PixelFormat};

0 commit comments

Comments
 (0)