Skip to content
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

16 bit per channel image buffers and dynamic images #1085

Merged
merged 13 commits into from Dec 12, 2019
5 changes: 3 additions & 2 deletions Cargo.toml
Expand Up @@ -20,13 +20,14 @@ name = "image"
path = "./src/lib.rs"

[dependencies]
byteorder = "1.2.1"
bytemuck = "1"
byteorder = "1.3.2"
num-iter = "0.1.32"
num-rational = { version = "0.2.1", default-features = false }
num-traits = "0.2.0"
gif = { version = "0.10.0", optional = true }
jpeg = { package = "jpeg-decoder", version = "0.1", default-features = false, optional = true }
png = { version = "0.15", optional = true }
png = { version = "0.15.2", optional = true }
scoped_threadpool = { version = "0.1", optional = true }
tiff = { version = "0.4.0", optional = true }

Expand Down
14 changes: 13 additions & 1 deletion src/bmp/encoder.rs
Expand Up @@ -2,7 +2,7 @@ use byteorder::{LittleEndian, WriteBytesExt};
use std::io::{self, Write};

use color;
use image::{ImageError, ImageResult};
use image::{ImageEncoder, ImageError, ImageResult};

const BITMAPFILEHEADER_SIZE: u32 = 14;
const BITMAPINFOHEADER_SIZE: u32 = 40;
Expand Down Expand Up @@ -213,6 +213,18 @@ impl<'a, W: Write + 'a> BMPEncoder<'a, W> {
}
}

impl<'a, W: Write> ImageEncoder for BMPEncoder<'a, W> {
fn write_image(
mut self,
buf: &[u8],
width: u32,
height: u32,
color_type: color::ColorType,
) -> ImageResult<()> {
self.encode(buf, width, height, color_type)
}
}

fn get_unsupported_error_message(c: color::ColorType) -> String {
format!(
"Unsupported color type {:?}. Supported types: RGB(8), RGBA(8), Gray(8), GrayA(8).",
Expand Down
24 changes: 17 additions & 7 deletions src/buffer.rs
Expand Up @@ -8,7 +8,7 @@ use color::{ColorType, FromColor, Luma, LumaA, Rgb, Rgba, Bgr, Bgra};
use flat::{FlatSamples, SampleLayout};
use dynimage::{save_buffer, save_buffer_with_format};
use image::{GenericImage, GenericImageView, ImageFormat, ImageResult};
use traits::Primitive;
use traits::{EncodableLayout, Primitive};
use utils::expand_packed;

/// A generalized pixel.
Expand Down Expand Up @@ -745,8 +745,9 @@ where

impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel<Subpixel = u8> + 'static,
Container: Deref<Target = [u8]>,
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the path specified.
///
Expand All @@ -759,7 +760,7 @@ where
// This is valid as the subpixel is u8.
save_buffer(
path,
self,
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
Expand All @@ -769,8 +770,9 @@ where

impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel<Subpixel = u8> + 'static,
Container: Deref<Target = [u8]>,
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the specified path in
/// the specified format.
Expand All @@ -784,7 +786,7 @@ where
// This is valid as the subpixel is u8.
save_buffer_with_format(
path,
self,
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
Expand Down Expand Up @@ -1076,6 +1078,14 @@ pub type GrayAlphaImage = ImageBuffer<LumaA<u8>, Vec<u8>>;
pub(crate) type BgrImage = ImageBuffer<Bgr<u8>, Vec<u8>>;
/// Sendable Bgr + alpha channel image buffer
pub(crate) type BgraImage = ImageBuffer<Bgra<u8>, Vec<u8>>;
/// Sendable 16-bit Rgb image buffer
pub(crate) type Rgb16Image = ImageBuffer<Rgb<u16>, Vec<u16>>;
/// Sendable 16-bit Rgb + alpha channel image buffer
pub(crate) type Rgba16Image = ImageBuffer<Rgba<u16>, Vec<u16>>;
/// Sendable 16-bit grayscale image buffer
pub(crate) type Gray16Image = ImageBuffer<Luma<u16>, Vec<u16>>;
/// Sendable 16-bit grayscale + alpha channel image buffer
pub(crate) type GrayAlpha16Image = ImageBuffer<LumaA<u16>, Vec<u16>>;

#[cfg(test)]
mod test {
Expand Down