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

into_png: Consume a Pixmap to write it to PNG without copying #79

Open
Pr0methean opened this issue Apr 26, 2023 · 2 comments
Open

into_png: Consume a Pixmap to write it to PNG without copying #79

Pr0methean opened this issue Apr 26, 2023 · 2 comments

Comments

@Pr0methean
Copy link

The TODO note in encode_png to eliminate the clone of the Pixmap can be addressed by adding a variant function that consumes self. Here's an example I've implemented from the outside:

pub fn into_png(mut image: MaybeFromPool<Pixmap>) -> Result<Vec<u8>, png::EncodingError> {
    for pixel in image.pixels_mut() {
        unsafe {
            // Treat this PremultipliedColorU8 slice as a ColorU8 slice
            *pixel = mem::transmute(pixel.demultiply());
        }
    }

    let mut data = Vec::with_capacity(1024 * 1024);
    {
        let mut encoder = png::Encoder::new(&mut data, image.width(), image.height());
        encoder.set_color(png::ColorType::Rgba);
        encoder.set_depth(png::BitDepth::Eight);
        let mut writer = encoder.write_header()?;
        writer.write_image_data(image.data())?;
    }

    Ok(data)
}
@RazrFalcon
Copy link
Collaborator

Hm... yes, we can add such function. But then Pixmap cannot be used anymore.

We could also add mutating encode_png variant, which would demultiply and multiply back. But I'm not sure if this could be done in a lossless way.

@Pr0methean
Copy link
Author

Pr0methean commented Apr 26, 2023

But then Pixmap cannot be used anymore.

That's more or less the point: it can be obtained with a method such as Arc::unwrap_or_clone or Cow::into_owned so that the question of whether to keep a copy for future use is separate from tiny-skia's responsibilities.

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

No branches or pull requests

2 participants