Skip to content

Add basic image integration - #109

Draft
RunDevelopment wants to merge 7 commits into
image-rs:mainfrom
RunDevelopment:image-integration
Draft

RunDevelopment wants to merge 7 commits into
image-rs:mainfrom
RunDevelopment:image-integration

Conversation

@RunDevelopment

@RunDevelopment RunDevelopment commented Feb 18, 2026 •

Copy link
Copy Markdown
Member

I added basic integration with the image crate. This means:

  • I implemented ImageView::from(&ImageBuffer) and ImageView::try_from(&DynamicImage) (same for ImageViewMut).
  • I implemented TryFrom conversions for image::ColorType and dds::ColorFormat in both directions.
  • I added tests for the above.

Since all APIs that need pixel data in this crate use ImageView{,Mut}, this should make it relatively straightforward to use image types now. E.g. here's an example for encoding from the docs:

fn save_rgba_image(
    file: &mut std::fs::File,
    image: &image::RgbaImage,
    mipmaps: bool,
) -> Result<(), EncodingError> {
    let size = Size::new(image.width(), image.height());
    let mut encoder = Encoder::new_image(file, size, Format::BC7_UNORM, mipmaps)?;
    encoder.write_surface(ImageView::from(image))?;
    encoder.finish()?;
    Ok(())
}

Open questions/TODO:

  • What versions of image should this crate depend on? I'm currently using >=0.24, <1.0, but I'm not sure whether that's a good idea. Some guidance would be nice here.

    It uses 0.25 now after raising the MSRV of this crate.

  • All image to view conversion currently panic if the ImageBuffer has less data than expected. I.e. it's possible create ImageBuffers where the backing container has less than w*h*color.bytes_per_pixel() bytes. image seems to generally panic when such an image is used, but the docs don't directly say that such images are invalid.

    So do you think panicking inside <ImageView as From>::from is okay for such images, or should I move to using TryFrom to handle this case without panicking?

    The panics are okay.

  • <ImageView as TryFrom<&DynamicImage>>::Error is currently (). This error is returned when the DynamicImage has a ColorType that isn't supported by this crate. I'm not sure what error type to return here. Is () fine?

    They now use pub struct UnsupportedColorType;.

Could you please help me with this, @fintelia?


Related to image-rs/image-extras#16

@RunDevelopment
RunDevelopment marked this pull request as draft February 18, 2026 19:36
@fintelia

Copy link
Copy Markdown
Contributor

What versions of image should this crate depend on? I'm currently using >=0.24, <1.0, but I'm not sure whether that's a good idea. Some guidance would be nice here.

I'd generally recommend depending on only a single major version of a dependency. We generally aim to only release new major versions image every 1-2 years, so there shouldn't be too much churn.

So do you think panicking inside ::from is okay for such images, or should I move to using TryFrom to handle this case without panicking?

Panicking seems fine to me.

<ImageView as TryFrom<&DynamicImage>>::Error is currently (). This error is returned when the DynamicImage has a ColorType that isn't supported by this crate. I'm not sure what error type to return here. Is () fine?

I'd suggest making a struct with no fields (say struct UnsupportedColorType;) and using that as the return type.

Though one other comment on From / TryFrom: my own preference is generally not to use these traits for more complicated objects because they're somewhat inflexible and very easy to overlook in the docs.

@RunDevelopment

Copy link
Copy Markdown
Member Author

Thank you for your input!

What versions of image should this crate depend on? I'm currently using >=0.24, <1.0, but I'm not sure whether that's a good idea. Some guidance would be nice here.

I'd generally recommend depending on only a single major version of a dependency. We generally aim to only release new major versions image every 1-2 years, so there shouldn't be too much churn.

I initially wanted to depend on 0.25.x, but the MSRV of 0.25.0 is higher than the MSRV of this crate. So enabling the image@0.25.x dependency effectively raises the MSRV of this crate.

Honestly, I think this might be a good reason to raise the MSRV of this crate. It solves the dependency problem, and I wanted to use features from newer compiler versions anyway.

Though one other comment on From / TryFrom: my own preference is generally not to use these traits for more complicated objects because they're somewhat inflexible and very easy to overlook in the docs.

I agree with that, especially the discoverability. I'll just make them functions on the view types.

@fintelia

Copy link
Copy Markdown
Contributor

I'd suggest just raising the MSRV.

Though where did you see that the MSRV of image 0.25.0 was newer than this crate's? It had an MSRV of 1.67.1.

@RunDevelopment

Copy link
Copy Markdown
Member Author

Though where did you see that the MSRV of image 0.25.0 was newer than this crate's? It had an MSRV of 1.67.1.

It was until I raised the MSRV of this crate to 1.73.0 two days ago :). The MSRV was 1.63.0 before that.

I made raising the MSRV its own PR, because I knew that I would make lots of changes (mostly div_ceil).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants