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

Add Rgb(a)32F DynamicImage Variant #1516

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5918804
add f32 dynamic image variant | deduplicate dynamic image macro | ded…
johannesvollmer Jul 19, 2021
558b91c
advance f32 support small steps
johannesvollmer Jul 26, 2021
935ce56
Merge branch 'next' of https://github.com/image-rs/image into f32-dyn…
johannesvollmer Jul 26, 2021
d12c226
allow deprecated, fix dynamic image errors
johannesvollmer Aug 1, 2021
16553d6
Merge branch 'next' of https://github.com/image-rs/image into f32-dyn…
johannesvollmer Aug 1, 2021
3bf27ed
undo rest of reverted changes
johannesvollmer Aug 2, 2021
47f98c3
implement f32 for pixel ... horribly. assumes f32 max = 1.0
johannesvollmer Aug 2, 2021
edb44b5
add next version to all deprecation messages
johannesvollmer Aug 2, 2021
c136d19
use the appropriate max value (for generic code that might have to ca…
johannesvollmer Aug 2, 2021
009ee98
clamp f32 just in case some inaccuracies occur
johannesvollmer Aug 2, 2021
896162e
reformat that ugly code
johannesvollmer Aug 2, 2021
37dd616
cleanup imports
johannesvollmer Aug 3, 2021
6282577
impl FromSample only for samples, and make sample impl less hacky
johannesvollmer Aug 3, 2021
4ca0be3
impl `fn grayscale` for `Rgb(a)32FImage`
johannesvollmer Aug 3, 2021
a050a53
maybe... add some luma<f32> functions...?
johannesvollmer Aug 3, 2021
f5ec196
update bytemuck dependency in public-private-dependencies test
johannesvollmer Aug 3, 2021
2a81cc6
rename `sample` to `pixel component`
johannesvollmer Oct 3, 2021
061a082
make `trait pixel component` sealed (and fix rename leftovers)
johannesvollmer Oct 3, 2021
c04a1f8
make `trait pixel component` sealed (and fix rename leftovers)
johannesvollmer Oct 8, 2021
d90ea73
seprate the `pixel` trait into `withcolortype` and `pixel`
johannesvollmer Oct 9, 2021
36afa88
Merge branch 'next' of https://github.com/image-rs/image into f32-dyn…
johannesvollmer Oct 18, 2021
2f2c0a0
remove overly restrictive trait bound for a color hint
johannesvollmer Oct 18, 2021
1413592
fix documentation
johannesvollmer Oct 18, 2021
8adafe4
re-added the GIMP color model to the pixels with color type
johannesvollmer Oct 18, 2021
fd45a0a
move GIMP color model back into the pixel trait to minimize change
johannesvollmer Oct 18, 2021
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ name = "image"
path = "./src/lib.rs"

[dependencies]
bytemuck = "1"
bytemuck = { version = "1.7.0", features = ["extern_crate_alloc"] } # includes cast_vec
byteorder = "1.3.2"
num-iter = "0.1.32"
num-rational = { version = "0.4", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml.public-private-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ path = "./src/lib.rs"

[dependencies]
# Not yet public.
bytemuck = "1"
bytemuck = { version = "1.7.0", features = ["extern_crate_alloc"] } # includes cast_vec
byteorder = "1.3.2"
num-iter = "0.1.32"
num-rational = "0.3"
Expand Down
25 changes: 14 additions & 11 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::dynimage::{save_buffer, save_buffer_with_format, write_buffer_with_fo
use crate::error::ImageResult;
use crate::image::{GenericImage, GenericImageView, ImageFormat, ImageOutputFormat};
use crate::math::Rect;
use crate::traits::{EncodableLayout, Pixel};
use crate::traits::{EncodableLayout, Pixel, PixelWithColorType};
use crate::utils::expand_packed;

/// Iterate over pixel refs.
Expand Down Expand Up @@ -585,7 +585,7 @@ where
/// ```no_run
/// use image::{GenericImage, GenericImageView, ImageBuffer, open};
///
/// let on_top = open("path/to/some.png").unwrap().into_rgb();
/// let on_top = open("path/to/some.png").unwrap().into_rgb8();
/// let mut img = ImageBuffer::from_fn(512, 512, |x, y| {
/// if (x + y) % 2 == 0 {
/// image::Rgb([0, 0, 0])
Expand All @@ -602,8 +602,8 @@ where
/// ```no_run
/// use image::{open, DynamicImage};
///
/// let rgba = open("path/to/some.png").unwrap().into_rgba();
/// let gray = DynamicImage::ImageRgba8(rgba).into_luma();
/// let rgba = open("path/to/some.png").unwrap().into_rgba8();
/// let gray = DynamicImage::ImageRgba8(rgba).into_luma8();
/// ```
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ImageBuffer<P: Pixel, Container> {
Expand Down Expand Up @@ -795,7 +795,7 @@ where
FlatSamples {
samples: self.data,
layout,
color_hint: Some(P::COLOR_TYPE),
color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType
}
}

Expand All @@ -809,7 +809,7 @@ where
FlatSamples {
samples: self.data.as_ref(),
layout,
color_hint: Some(P::COLOR_TYPE),
color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType
}
}

Expand All @@ -823,7 +823,7 @@ where
FlatSamples {
samples: self.data.as_mut(),
layout,
color_hint: Some(P::COLOR_TYPE),
color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType
}
}
}
Expand Down Expand Up @@ -934,14 +934,15 @@ where
pub fn save<Q>(&self, path: Q) -> ImageResult<()>
where
Q: AsRef<Path>,
P: PixelWithColorType,
{
// This is valid as the subpixel is u8.
save_buffer(
path,
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
<P as PixelWithColorType>::COLOR_TYPE,
)
}
}
Expand All @@ -960,14 +961,15 @@ where
pub fn save_with_format<Q>(&self, path: Q, format: ImageFormat) -> ImageResult<()>
where
Q: AsRef<Path>,
P: PixelWithColorType,
{
// This is valid as the subpixel is u8.
save_buffer_with_format(
path,
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
<P as PixelWithColorType>::COLOR_TYPE,
format,
)
}
Expand All @@ -990,14 +992,15 @@ where
where
W: std::io::Write + std::io::Seek,
F: Into<ImageOutputFormat>,
P: PixelWithColorType,
{
// This is valid as the subpixel is u8.
write_buffer_with_format(
writer,
self.as_bytes(),
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
<P as PixelWithColorType>::COLOR_TYPE,
format,
)
}
Expand Down Expand Up @@ -1320,7 +1323,7 @@ where
/// let image_path = "examples/fractal.png";
/// let image = image::open(&image_path)
/// .expect("Open file failed")
/// .to_rgba();
/// .to_rgba8();
///
/// let gray_image: GrayImage = image.convert();
/// ```
Expand Down
13 changes: 8 additions & 5 deletions src/codecs/jpeg/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::utils::clamp;

use super::entropy::build_huff_lut;
use super::transform;
use crate::traits::PixelWithColorType;

// Markers
// Baseline DCT
Expand Down Expand Up @@ -472,8 +473,9 @@ impl<'a, W: Write> JpegEncoder<'a, W> {
pub fn encode_image<I: GenericImageView>(
&mut self,
image: &I,
) -> ImageResult<()> {
) -> ImageResult<()> where I::Pixel: PixelWithColorType {
let n = I::Pixel::CHANNEL_COUNT;
let color_type = I::Pixel::COLOR_TYPE;
let num_components = if n == 1 || n == 2 { 1 } else { 3 };

self.writer.write_marker(SOI)?;
Expand Down Expand Up @@ -551,8 +553,7 @@ impl<'a, W: Write> JpegEncoder<'a, W> {
build_scan_header(&mut buf, &self.components[..num_components]);
self.writer.write_segment(SOS, &buf)?;


if I::Pixel::COLOR_TYPE.has_color() {
if color_type.has_color() {
self.encode_rgb(image)
} else {
self.encode_gray(image)
Expand Down Expand Up @@ -774,9 +775,11 @@ fn encode_coefficient(coefficient: i32) -> (u8, u16) {

#[inline]
fn rgb_to_ycbcr<P: Pixel>(pixel: P) -> (u8, u8, u8) {
use num_traits::{cast::ToPrimitive, bounds::Bounded};
use num_traits::{cast::ToPrimitive};
use crate::traits::Primitive;

let [r, g, b] = pixel.to_rgb().0;
let max: f32 = P::Subpixel::max_value().to_f32().unwrap();
let max: f32 = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap();
let r: f32 = r.to_f32().unwrap();
let g: f32 = g.to_f32().unwrap();
let b: f32 = b.to_f32().unwrap();
Expand Down
Loading