Skip to content

Commit

Permalink
Add image format options to reduce code bloat, fixes iced-rs#376
Browse files Browse the repository at this point in the history
  • Loading branch information
valpackett committed Nov 25, 2020
1 parent fd9f1e2 commit 86260b4
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion glow/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl backend::Text for Backend {
}
}

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
impl backend::Image for Backend {
fn dimensions(&self, _handle: &iced_native::image::Handle) -> (u32, u32) {
(50, 50)
Expand Down
2 changes: 1 addition & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod platform {
)]
pub use crate::renderer::widget::qr_code;

#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
#[cfg_attr(docsrs, doc(cfg(feature = "image_rs")))]
pub mod image {
//! Display images in your user interface.
pub use crate::runtime::image::{Handle, Image};
Expand Down
16 changes: 15 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ repository = "https://github.com/hecrj/iced"

[features]
svg = ["resvg"]
image = ["png", "jpeg", "jpeg_rayon", "gif", "webp", "bmp"]
png = ["image_rs/png"]
jpeg = ["image_rs/jpeg"]
jpeg_rayon = ["image_rs/jpeg_rayon"]
gif = ["image_rs/gif"]
webp = ["image_rs/webp"]
pnm = ["image_rs/pnm"]
ico = ["image_rs/ico"]
bmp = ["image_rs/bmp"]
hdr = ["image_rs/hdr"]
dds = ["image_rs/dds"]
farbfeld = ["image_rs/farbfeld"]
canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"]
Expand Down Expand Up @@ -36,8 +48,10 @@ version = "0.1"
path = "../graphics"
features = ["font-fallback", "font-icons"]

[dependencies.image]
[dependencies.image_rs]
version = "0.23"
package = "image"
default-features = false
optional = true

[dependencies.resvg]
Expand Down
14 changes: 7 additions & 7 deletions wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iced_graphics::{Primitive, Viewport};
use iced_native::mouse;
use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
use crate::image;

/// A [`wgpu`] graphics backend for [`iced`].
Expand All @@ -22,7 +22,7 @@ pub struct Backend {
text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline,

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
image_pipeline: image::Pipeline,

default_text_size: u16,
Expand All @@ -42,15 +42,15 @@ impl Backend {
settings.antialiasing,
);

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
let image_pipeline = image::Pipeline::new(device, settings.format);

Self {
quad_pipeline,
text_pipeline,
triangle_pipeline,

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
image_pipeline,

default_text_size: settings.default_text_size,
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Backend {
);
}

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
self.image_pipeline.trim_cache();

*mouse_interaction
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Backend {
);
}

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
{
if !layer.images.is_empty() {
let scaled = transformation
Expand Down Expand Up @@ -272,7 +272,7 @@ impl backend::Text for Backend {
}
}

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
impl backend::Image for Backend {
fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) {
self.image_pipeline.dimensions(handle)
Expand Down
18 changes: 9 additions & 9 deletions wgpu/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod atlas;

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
mod raster;

#[cfg(feature = "svg")]
Expand All @@ -16,15 +16,15 @@ use std::mem;

use bytemuck::{Pod, Zeroable};

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
use iced_native::image;

#[cfg(feature = "svg")]
use iced_native::svg;

#[derive(Debug)]
pub struct Pipeline {
#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
raster_cache: RefCell<raster::Cache>,
#[cfg(feature = "svg")]
vector_cache: RefCell<vector::Cache>,
Expand Down Expand Up @@ -252,7 +252,7 @@ impl Pipeline {
});

Pipeline {
#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
raster_cache: RefCell::new(raster::Cache::new()),

#[cfg(feature = "svg")]
Expand All @@ -271,7 +271,7 @@ impl Pipeline {
}
}

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
pub fn dimensions(&self, handle: &image::Handle) -> (u32, u32) {
let mut cache = self.raster_cache.borrow_mut();
let memory = cache.load(&handle);
Expand Down Expand Up @@ -300,15 +300,15 @@ impl Pipeline {
) {
let instances: &mut Vec<Instance> = &mut Vec::new();

#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
let mut raster_cache = self.raster_cache.borrow_mut();

#[cfg(feature = "svg")]
let mut vector_cache = self.vector_cache.borrow_mut();

for image in images {
match &image {
#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
layer::Image::Raster { handle, bounds } => {
if let Some(atlas_entry) = raster_cache.upload(
handle,
Expand All @@ -324,7 +324,7 @@ impl Pipeline {
);
}
}
#[cfg(not(feature = "image"))]
#[cfg(not(feature = "image_rs"))]
layer::Image::Raster { .. } => {}

#[cfg(feature = "svg")]
Expand Down Expand Up @@ -453,7 +453,7 @@ impl Pipeline {
}

pub fn trim_cache(&mut self) {
#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
self.raster_cache.borrow_mut().trim(&mut self.texture_atlas);

#[cfg(feature = "svg")]
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/image/atlas/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Entry {
}

impl Entry {
#[cfg(feature = "image")]
#[cfg(feature = "image_rs")]
pub fn size(&self) -> (u32, u32) {
match self {
Entry::Contiguous(allocation) => allocation.size(),
Expand Down
8 changes: 4 additions & 4 deletions wgpu/src/image/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet};

#[derive(Debug)]
pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
Host(::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>),
Device(atlas::Entry),
NotFound,
Invalid,
Expand Down Expand Up @@ -42,14 +42,14 @@ impl Cache {

let memory = match handle.data() {
image::Data::Path(path) => {
if let Ok(image) = ::image::open(path) {
if let Ok(image) = ::image_rs::open(path) {
Memory::Host(image.to_bgra8())
} else {
Memory::NotFound
}
}
image::Data::Bytes(bytes) => {
if let Ok(image) = ::image::load_from_memory(&bytes) {
if let Ok(image) = ::image_rs::load_from_memory(&bytes) {
Memory::Host(image.to_bgra8())
} else {
Memory::Invalid
Expand All @@ -60,7 +60,7 @@ impl Cache {
height,
pixels,
} => {
if let Some(image) = ::image::ImageBuffer::from_vec(
if let Some(image) = ::image_rs::ImageBuffer::from_vec(
*width,
*height,
pixels.to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub use widget::*;

pub(crate) use iced_graphics::Transformation;

#[cfg(any(feature = "image", feature = "svg"))]
#[cfg(any(feature = "image_rs", feature = "svg"))]
mod image;

/// A [`wgpu`] graphics renderer for [`iced`].
Expand Down

0 comments on commit 86260b4

Please sign in to comment.