8000 Chore/update deps by Roms1383 · Pull Request #27 · kosinix/raster · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Chore/update deps #27

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ homepage = "https://github.com/kosinix/raster"
repository = "https://github.com/kosinix/raster.git"

[dependencies.image]
version = "0.19"
version = "0.24"
default-features = false
features = ["jpeg", "jpeg_rayon"]

[dependencies.gif]
version = "0.10"
version = "0.12"

[dependencies.png]
version = "0.12"
version = "0.17"
26 changes: 15 additions & 11 deletions src/endec.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A module for encoding/decoding.

// from rust
use std::path::Path;
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;

// from external crate
use gif;
Expand All @@ -16,13 +16,13 @@ use ImageFormat;

// Decode GIF
pub fn decode_gif(image_file: &File) -> RasterResult<Image> {
let mut decoder = gif::Decoder::new(image_file);
let mut decoder = gif::DecodeOptions::new();

// Configure the decoder such that it will expand the image to RGBA.
gif::SetParameter::set(&mut decoder, gif::ColorOutput::RGBA);
decoder.set_color_output(gif::ColorOutput::RGBA);

// Read the file header
let mut reader = decoder.read_info()?;
let mut reader = decoder.read_info(image_file)?;

// Read frame 1.
// TODO: Work on all frames
Expand Down Expand Up @@ -52,20 +52,24 @@ pub fn encode_gif(image: &Image, path: &Path) -> RasterResult<()> {
image.height as u16,
&mut image.bytes.clone(),
); // TODO: Perf issue?
let mut encoder = gif::Encoder::new(writer, frame.width, frame.height, &[])?;
encoder.write_frame(&frame).map_err(RasterError::Io)?;
let mut encoder = gif::Encoder::new(writer, frame.width, frame.height, &[])
.map_err(|e| RasterError::Encode(ImageFormat::Gif, e.to_string()))?;
encoder
.write_frame(&frame)
.map_err(|e| RasterError::Encode(ImageFormat::Gif, e.to_string()))?;
Ok(())
}

// Decode PNG
pub fn decode_png(image_file: &File) -> RasterResult<Image> {
let decoder = png::Decoder::new(image_file);
let (info, mut reader) = decoder.read_info()?;
let mut bytes = vec![0; info.buffer_size()];
let mut reader = decoder.read_info()?;
let mut bytes = vec![0; reader.output_buffer_size()];

reader.next_frame(&mut bytes)?;

if info.color_type == png::ColorType::RGB {
let info = reader.info();
if info.color_type == png::ColorType::Rgb {
// Applies only to RGB

let mut insert_count = 0;
Expand All @@ -91,8 +95,8 @@ pub fn encode_png(image: &Image, path: &Path) -> RasterResult<()> {
let ref mut w = BufWriter::new(file);

let mut encoder = png::Encoder::new(w, image.width as u32, image.height as u32);
png::HasParameters::set(&mut encoder, png::ColorType::RGBA);
png::HasParameters::set(&mut encoder, png::BitDepth::Eight);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
Ok(writer.write_image_data(&image.bytes)?)
}
81 changes: 49 additions & 32 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A module for error types.

use std::convert::TryInto;
// from rust
use std::io::Error as IoError;
use std::num::ParseIntError;
Expand Down Expand Up @@ -54,9 +55,6 @@ impl From<gif::DecodingError> for RasterError {
gif::DecodingError::Format(msg) => {
RasterError::Decode(ImageFormat::Gif, msg.to_string())
}
gif::DecodingError::Internal(msg) => {
RasterError::Decode(ImageFormat::Gif, msg.to_string())
}
gif::DecodingError::Io(io_err) => RasterError::Io(io_err),
}
}
Expand All @@ -69,26 +67,53 @@ impl From<gif::DecodingError> for RasterError {
// raster::open
impl From<piston_image::ImageError> for RasterError {
fn from(err: piston_image::ImageError) -> RasterError {
match err {
piston_image::ImageError::FormatError(msg) => {
RasterError::Decode(ImageFormat::Jpeg, msg)
}
piston_image::ImageError::DimensionError => {
RasterError::Decode(ImageFormat::Jpeg, "DimensionError".to_string())
}
piston_image::ImageError::Unsup 8000 portedError(msg) => {
RasterError::Decode(ImageFormat::Jpeg, msg)
}
piston_image::ImageError::UnsupportedColor(_) => {
RasterError::Decode(ImageFormat::Jpeg, "UnsupportedColor".to_string())
if let piston_image::ImageError::Parameter(_) = err {
return RasterError::Unexpected;
}
if let piston_image::ImageError::IoError(io_err) = err {
return RasterError::Io(io_err);
}
if let piston_image::ImageError::Limits(limit_err) = err {
match limit_err.kind() {
piston_image::error::LimitErrorKind::DimensionError => {
return RasterError::Unexpected; // TODO: where to get dimensions from ?
}
piston_image::error::LimitErrorKind::InsufficientMemory => {
return RasterError::Io(std::io::Error::new(
std::io::ErrorKind::OutOfMemory,
"insufficient memory",
))
}
piston_image::error::LimitErrorKind::Unsupported { limits, .. } => {
match (limits.max_image_width, limits.max_image_height) {
(Some(w), Some(h)) => {
return RasterError::PixelOutOfBounds(w as i32, h as i32)
}
_ => return RasterError::Unexpected, // TODO: where to get dimensions from ?
}
}
_ => return RasterError::Unexpected,
}
piston_image::ImageError::NotEnoughData => {
RasterError::Decode(ImageFormat::Jpeg, "NotEnoughData".to_string())
}
let hint: piston_image::error::ImageFormatHint = match &err {
piston_image::ImageError::Encoding(encoding_err) => encoding_err.format_hint(),
piston_image::ImageError::Decoding(decoding_err) => decoding_err.format_hint(),
_ => unreachable!(), // processed above
};
let format: Result<ImageFormat, RasterError> = match &err {
piston_image::ImageError::Decoding(_) => hint.try_into(),
piston_image::ImageError::Encoding(_) => hint.try_into(),
_ => unreachable!(), // processed above
};
match (&err, format) {
(_, Err(raster_err)) => raster_err,
(piston_image::ImageError::Encoding(_), Ok(format)) => {
return RasterError::Encode(format, err.to_string())
}
piston_image::ImageError::IoError(io_err) => RasterError::Io(io_err),
piston_image::ImageError::ImageEnd => {
RasterError::Decode(ImageFormat::Jpeg, "ImageEnd".to_string())
(piston_image::ImageError::Decoding(_), Ok(format)) => {
return RasterError::Decode(format, err.to_string())
}
_ => unreachable!(), // processed above
}
}
}
Expand All @@ -102,18 +127,8 @@ impl From<png::DecodingError> for RasterError {
png::DecodingError::Format(_) => {
RasterError::Decode(ImageFormat::Png, "Format".to_string())
}
png::DecodingError::InvalidSignature => {
RasterError::Decode(ImageFormat::Png, "InvalidSignature".to_string())
}
png::DecodingError::CrcMismatch { .. } => {
RasterError::Decode(ImageFormat::Png, "CrcMismatch".to_string())
}
png::DecodingError::Other(_) => {
RasterError::Decode(ImageFormat::Png, "Other".to_string())
}
png::DecodingError::CorruptFlateStream => {
RasterError::Decode(ImageFormat::Png, "CorruptFlateStream".to_string())
}
png::DecodingError::Parameter(_) => RasterError::Unexpected,
png::DecodingError::LimitsExceeded => RasterError::Unexpected, // TODO: where to get dimensions from ?
}
}
}
Expand All @@ -126,6 +141,8 @@ impl From<png::EncodingError> for RasterError {
png::EncodingError::Format(_) => {
RasterError::Encode(ImageFormat::Png, "Format".to_string())
}
png::EncodingError::Parameter(_) => RasterError::Unexpected,
png::EncodingError::LimitsExceeded => RasterError::Unexpected, // TODO: where to get dimensions from ?
}
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::collections::HashMap;
// from external crate

// from local crate
use error::{RasterError, RasterResult};
use color::Color;
use error::{RasterError, RasterResult};

/// A struct for easily representing a raster image.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -264,3 +264,47 @@ pub enum ImageFormat {
Jpeg,
Png,
}

impl std::convert::TryFrom<piston_image::ImageFormat> for ImageFormat {
type Error = RasterError;

fn try_from(value: piston_image::ImageFormat) -> Result<Self, Self::Error> {
match value {
piston_image::ImageFormat::Png => Ok(Self::Png),
piston_image::ImageFormat::Jpeg => Ok(Self::Jpeg),
piston_image::ImageFormat::Gif => Ok(Self::Gif),
format => Err(Self::Error::UnsupportedFormat(
format.extensions_str().join(", "),
)),
}
}
}

impl std::convert::TryFrom 9E19 <piston_image::error::ImageFormatHint> for ImageFormat {
type Error = RasterError;

fn try_from(value: piston_image::error::ImageFormatHint) -> Result<Self, Self::Error> {
let from_str = |x: &str| match x.to_lowercase().as_str() {
name if name.ends_with("gif") => Ok(Self::Gif),
name if name.ends_with("png") => Ok(Self::Png),
name if name.ends_with("jpg") || name.ends_with("jpeg") => Ok(Self::Jpeg),
name => Err(Self::Error::UnsupportedFormat(name.to_string())),
};
match value {
piston_image::error::ImageFormatHint::Exact(format) => match format {
image::ImageFormat::Gif => Ok(Self::Gif),
image::ImageFormat::Png => Ok(Self::Png),
image::ImageFormat::Jpeg => Ok(Self::Jpeg),
format => Err(Self::Error::UnsupportedFormat(
format.extensions_str().join(", "),
)),
},
piston_image::error::ImageFormatHint::Name(name) => from_str(name.as_str()),
piston_image::error::ImageFormatHint::PathExtension(path) => {
from_str(path.to_string_lossy().as_ref())
}
piston_image::error::ImageFormatHint::Unknown => Err(Self::Error::Unexpected),
_ => Err(Self::Error::Unexpected),
}
}
}
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,29 @@
//!

// modules
mod blend;
mod color;
pub mod compare;
pub mod editor;
mod endec;
pub mod error;
pub mod filter;
pub mod interpolate;
pub mod transform;
mod blend;
mod color;
mod endec;
mod image;
pub mod interpolate;
mod position;
pub mod transform;

// crates
extern crate gif;
extern crate image as piston_image;
extern crate png;

// from rust
use std::ascii::AsciiExt;
use std::fs::File;
use std::path::Path;

// from external crate
use piston_image::GenericImage;
use piston_image::GenericImageView;

// from local crate
use error::{RasterError, RasterResult};
Expand Down Expand Up @@ -143,7 +142,8 @@ pub use transform::TransformMode;
/// ```
pub fn open(image_file: &str) -> RasterResult<Image> {
let path = Path::new(image_file);
let ext = path.extension()
let ext = path
.extension()
.and_then(|s| s.to_str())
.map_or("".to_string(), |s| s.to_ascii_lowercase());

Expand All @@ -159,7 +159,7 @@ pub fn open(image_file: &str) -> RasterResult<Image> {
for y in 0..h {
for x in 0..w {
let p = src.get_pixel(x, y);
bytes.extend_from_slice(&p.data[0..4]);
bytes.extend_from_slice(&p.0[0..4]);
}
}
Ok(Image {
Expand Down Expand Up @@ -191,21 +191,21 @@ pub fn open(image_file: &str) -> RasterResult<Image> {
/// ```
pub fn save(image: &Image, out: &str) -> RasterResult<()> {
let path = Path::new(out);
let ext = path.extension()
let ext = path
.extension()
.and_then(|s| s.to_str())
.map_or("".to_string(), |s| s.to_ascii_lowercase());

match &ext[..] {
"gif" => Ok(endec::encode_gif(&image, &path)?),
"jpg" | "jpeg" => {
piston_image::save_buffer(
&path,
&image.bytes,
image.width as u32,
image.height as u32,
piston_image::RGBA(8),
).map_err(|_| RasterError::Encode(ImageFormat::Jpeg, "Format".to_string()))
}
"jpg" | "jpeg" => piston_image::save_buffer(
&path,
&image.bytes,
image.width as u32,
image.height as u32,
piston_image::ColorType::Rgb8,
)
.map_err(|_| RasterError::Encode(ImageFormat::Jpeg, "Format".to_string())),
"png" => Ok(endec::encode_png(&image, &path)?),
_ => Err(RasterError::UnsupportedFormat(ext)),
}
Expand Down
0