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

Export Image and ImageData by default #1011

Merged
merged 9 commits into from
Jun 2, 2020
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ You can find its changes [documented below](#060---2020-06-01).

### Changed

- `Image` and `ImageData` exported by default. ([#1011] by [@covercash2])
covercash2 marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

### Removed
Expand Down Expand Up @@ -315,6 +317,7 @@ Last release without a changelog :(
[#1003]: https://github.com/xi-editor/druid/pull/1003
[#1007]: https://github.com/xi-editor/druid/pull/1007
[#1008]: https://github.com/xi-editor/druid/pull/1008
[#1011]: https://github.com/xi-editor/druid/pull/1011
[#1013]: https://github.com/xi-editor/druid/pull/1013

[Unreleased]: https://github.com/xi-editor/druid/compare/v0.6.0...master
Expand Down
19 changes: 19 additions & 0 deletions druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@
//! }
//! ```
//!
//! # Optional Features
//!
//! * `im` - Efficient immutable data structures using the [`im` crate],
//! which is made available via the [`im` module].
//! * `svg` - Scalable Vector Graphics for icons and other scalable images using the [`usvg` crate].
//! * `image` - Bitmap image support using the [`image` crate].
//! * `x11` - Work-in-progress X11 Linux backend instead of GTK.
//!
//! Features can be added with `cargo`. For example, in your `Cargo.toml`:
//! ```no_compile
//! [dependencies.druid]
//! version = "0.6.0"
//! features = ["im", "svg", "image"]
//! ```
//!
//! [`Widget`]: trait.Widget.html
//! [`Data`]: trait.Data.html
//! [`Lens`]: trait.Lens.html
Expand All @@ -100,6 +115,10 @@
//! [`piet`]: https://docs.rs/piet
//! [`druid/examples`]: https://github.com/xi-editor/druid/tree/v0.6.0/druid/examples
//! [druid book]: https://xi-editor.io/druid/intro.html
//! [`im` crate]: https://crates.io/crates/im
//! [`im` module]: im/index.html
//! [`usvg` crate]: https://crates.io/crates/usvg
//! [`image` crate]: https://crates.io/crates/image

#![deny(intra_doc_link_resolution_failure, unsafe_code)]
#![allow(clippy::new_ret_no_self, clippy::needless_doctest_main)]
Expand Down
69 changes: 35 additions & 34 deletions druid/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
//! An Image widget.
//! Please consider using SVG and the SVG wideget as it scales much better.

use std::convert::AsRef;
use std::error::Error;
use std::path::Path;
#[cfg(feature = "image")]
use std::{convert::AsRef, error::Error, path::Path};

use crate::{
piet::{ImageFormat, InterpolationMode},
Expand Down Expand Up @@ -127,9 +126,41 @@ impl ImageData {
}
}

/// Get the size in pixels of the contained image.
fn get_size(&self) -> Size {
Size::new(self.x_pixels as f64, self.y_pixels as f64)
}

/// Convert ImageData into Piet draw instructions.
fn to_piet(&self, offset_matrix: Affine, ctx: &mut PaintCtx, interpolation: InterpolationMode) {
ctx.with_save(|ctx| {
ctx.transform(offset_matrix);
let size = self.get_size();
let im = ctx
.make_image(
size.width as usize,
size.height as usize,
&self.pixels,
self.format,
)
.unwrap();
ctx.draw_image(&im, size.to_rect(), interpolation);
})
}
}

#[cfg(feature = "image")]
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
impl ImageData {
/// Load an image from a DynamicImage from the image crate
pub fn from_dynamic_image(image_data: image::DynamicImage) -> ImageData {
if has_alpha_channel(&image_data) {
use image::ColorType::*;
let has_alpha_channel = match image_data.color() {
La8 | Rgba8 | La16 | Rgba16 | Bgra8 => true,
_ => false,
};

if has_alpha_channel {
Self::from_dynamic_image_with_alpha(image_data)
} else {
Self::from_dynamic_image_without_alpha(image_data)
Expand Down Expand Up @@ -173,36 +204,6 @@ impl ImageData {
let image_data = image::open(path).map_err(|e| e)?;
Ok(ImageData::from_dynamic_image(image_data))
}

/// Get the size in pixels of the contained image.
fn get_size(&self) -> Size {
Size::new(self.x_pixels as f64, self.y_pixels as f64)
}

/// Convert ImageData into Piet draw instructions.
fn to_piet(&self, offset_matrix: Affine, ctx: &mut PaintCtx, interpolation: InterpolationMode) {
ctx.with_save(|ctx| {
ctx.transform(offset_matrix);
let size = self.get_size();
let im = ctx
.make_image(
size.width as usize,
size.height as usize,
&self.pixels,
self.format,
)
.unwrap();
ctx.draw_image(&im, size.to_rect(), interpolation);
})
}
}

fn has_alpha_channel(image: &image::DynamicImage) -> bool {
use image::ColorType::*;
match image.color() {
La8 | Rgba8 | La16 | Rgba16 | Bgra8 => true,
_ => false,
}
}

impl Default for ImageData {
Expand Down
4 changes: 0 additions & 4 deletions druid/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ mod either;
mod env_scope;
mod flex;
mod identity_wrapper;
#[cfg(feature = "image")]
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
mod image;
mod invalidation;
mod label;
Expand All @@ -52,8 +50,6 @@ mod view_switcher;
mod widget;
mod widget_ext;

#[cfg(feature = "image")]
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
pub use self::image::{Image, ImageData};
covercash2 marked this conversation as resolved.
Show resolved Hide resolved
pub use align::Align;
pub use button::Button;
Expand Down