Skip to content

Move most uses of the std crate to core and alloc #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
rust: ["1.36.0", stable, beta, nightly]
rust: ["1.48.0", stable, beta, nightly]
features: ["", "rayon"]
command: [test, benchmark]

Expand All @@ -29,11 +29,11 @@ jobs:
- name: test
run: >
cargo test --tests --benches --no-default-features --features "$FEATURES"
if: ${{ matrix.command == 'test' && matrix.rust != '1.36.0' }}
if: ${{ matrix.command == 'test' && matrix.rust != '1.48.0' }}
env:
FEATURES: ${{ matrix.features }}
- name: benchmark
run: cargo bench --bench decoding_benchmark --no-default-features --features "$FEATURES" -- --warm-up-time 1 --measurement-time 1 --sample-size 25
if: ${{ matrix.command == 'benchmark' && matrix.rust != '1.36.0' }}
if: ${{ matrix.command == 'benchmark' && matrix.rust != '1.48.0' }}
env:
FEATURES: ${{ matrix.features }}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.36
1.48.0
15 changes: 9 additions & 6 deletions src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use alloc::borrow::ToOwned;
use alloc::{format, vec};
use alloc::vec::Vec;
use alloc::sync::Arc;
use core::cmp;
use core::mem;
use core::ops::Range;
use crate::read_u8;
use error::{Error, Result, UnsupportedFeature};
use huffman::{fill_default_mjpeg_tables, HuffmanDecoder, HuffmanTable};
Expand All @@ -6,11 +13,7 @@ use parser::{AdobeColorTransform, AppData, CodingProcess, Component, Dimensions,
parse_app, parse_com, parse_dht, parse_dqt, parse_dri, parse_sof, parse_sos, IccChunk,
ScanInfo};
use upsampler::Upsampler;
use std::cmp;
use std::io::Read;
use std::mem;
use std::ops::Range;
use std::sync::Arc;
use worker::{RowData, PlatformWorker, Worker};

pub const MAX_COMPONENTS: usize = 4;
Expand Down Expand Up @@ -1122,6 +1125,6 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
}

fn clamp_to_u8(value: i32) -> i32 {
let value = std::cmp::max(value, 0);
std::cmp::min(value, 255)
let value = cmp::max(value, 0);
cmp::min(value, 255)
}
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use alloc::boxed::Box;
use alloc::string::String;
use alloc::fmt;
use core::result;
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;

pub type Result<T> = ::std::result::Result<T, Error>;
pub type Result<T> = result::Result<T, Error>;

/// An enumeration over JPEG features (currently) unsupported by this library.
///
Expand Down
6 changes: 5 additions & 1 deletion src/huffman.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloc::borrow::ToOwned;
use alloc::vec;
use alloc::vec::Vec;
use core::iter;
use crate::read_u8;
use error::{Error, Result};
use marker::Marker;
Expand Down Expand Up @@ -253,7 +257,7 @@ fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
let huffsize = bits.iter()
.enumerate()
.fold(Vec::new(), |mut acc, (i, &value)| {
acc.extend(std::iter::repeat((i + 1) as u8).take(value as usize));
acc.extend(iter::repeat((i + 1) as u8).take(value as usize));
acc
});

Expand Down
6 changes: 3 additions & 3 deletions src/idct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// One example is tests/crashtest/images/imagetestsuite/b0b8914cc5f7a6eff409f16d8cc236c5.jpg
// That's why wrapping operators are needed.
use crate::parser::Dimensions;
use std::{convert::TryFrom, num::Wrapping};
use core::{convert::TryFrom, num::Wrapping};

pub(crate) fn choose_idct_size(full_size: Dimensions, requested_size: Dimensions) -> usize {
fn scaled(len: u16, scale: usize) -> u16 { ((len as u32 * scale as u32 - 1) / 8 + 1) as u16 }
Expand Down Expand Up @@ -417,8 +417,8 @@ fn test_dequantize_and_idct_block_8x8_all_zero() {
fn test_dequantize_and_idct_block_8x8_saturated() {
let mut output = [0u8; 8 * 8];
dequantize_and_idct_block_8x8(
&[std::i16::MAX; 8*8],
&[std::u16::MAX; 8*8],
&[i16::MAX; 8*8],
&[u16::MAX; 8*8],
8,
&mut output);
let expected = [
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
#![deny(missing_docs)]
#![forbid(unsafe_code)]

extern crate core;
extern crate alloc;

#[cfg(feature="rayon")]
extern crate rayon;

pub use decoder::{Decoder, ImageInfo, PixelFormat};
pub use error::{Error, UnsupportedFeature};
pub use parser::CodingProcess;

use std::io;

mod decoder;
mod error;
mod huffman;
Expand All @@ -45,13 +50,13 @@ mod parser;
mod upsampler;
mod worker;

fn read_u8<R: std::io::Read>(reader: &mut R) -> std::io::Result<u8> {
fn read_u8<R: io::Read>(reader: &mut R) -> io::Result<u8> {
let mut buf = [0];
reader.read_exact(&mut buf)?;
Ok(buf[0])
}

fn read_u16_from_be<R: std::io::Read>(reader: &mut R) -> std::io::Result<u16> {
fn read_u16_from_be<R: io::Read>(reader: &mut R) -> io::Result<u16> {
let mut buf = [0, 0];
reader.read_exact(&mut buf)?;
Ok(u16::from_be_bytes(buf))
Expand Down
9 changes: 6 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use alloc::borrow::ToOwned;
use alloc::{format, vec};
use alloc::vec::Vec;
use core::ops::{self, Range};
use crate::{read_u16_from_be, read_u8};
use error::{Error, Result, UnsupportedFeature};
use huffman::{HuffmanTable, HuffmanTableClass};
use marker::Marker;
use marker::Marker::*;
use std::io::{self, Read};
use std::ops::Range;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Dimensions {
Expand Down Expand Up @@ -379,7 +382,7 @@ pub fn parse_sos<R: Read>(reader: &mut R, frame: &FrameInfo) -> Result<ScanInfo>

let blocks_per_mcu = component_indices.iter().map(|&i| {
frame.components[i].horizontal_sampling_factor as u32 * frame.components[i].vertical_sampling_factor as u32
}).fold(0, ::std::ops::Add::add);
}).fold(0, ops::Add::add);

if component_count > 1 && blocks_per_mcu > 10 {
return Err(Error::Format("scan with more than one component and more than 10 blocks per MCU".to_owned()));
Expand Down Expand Up @@ -537,7 +540,7 @@ pub fn parse_dht<R: Read>(reader: &mut R, is_baseline: Option<bool>) -> Result<(
let mut counts = [0u8; 16];
reader.read_exact(&mut counts)?;

let size = counts.iter().map(|&val| val as usize).fold(0, ::std::ops::Add::add);
let size = counts.iter().map(|&val| val as usize).fold(0, ops::Add::add);

if size == 0 {
return Err(Error::Format("encountered table with zero length in DHT".to_owned()));
Expand Down
3 changes: 3 additions & 0 deletions src/upsampler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use error::{Error, Result, UnsupportedFeature};
use parser::Component;

Expand Down
6 changes: 4 additions & 2 deletions src/worker/immediate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use alloc::vec;
use alloc::vec::Vec;
use core::mem;
use decoder::MAX_COMPONENTS;
use error::Result;
use idct::dequantize_and_idct_block;
use std::mem;
use std::sync::Arc;
use alloc::sync::Arc;
use parser::Component;
use super::{RowData, Worker};

Expand Down
8 changes: 5 additions & 3 deletions src/worker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
mod immediate;
#[cfg(feature = "std")]
mod multithreaded;

#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))]
#[cfg(all(feature = "std", not(any(target_arch = "asmjs", target_arch = "wasm32"))))]
pub use self::multithreaded::MultiThreadedWorker as PlatformWorker;
#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))]
#[cfg(any(not(feature = "std"), target_arch = "asmjs", target_arch = "wasm32"))]
pub use self::immediate::ImmediateWorker as PlatformWorker;

use alloc::sync::Arc;
use alloc::vec::Vec;
use error::Result;
use parser::Component;
use std::sync::Arc;

pub struct RowData {
pub index: usize,
Expand Down