Merge pull request #392 from unrelentingtech/image-debloat

Add image format options to reduce code bloat
This commit is contained in:
Héctor Ramón 2021-02-13 16:18:40 +01:00 committed by GitHub
commit 4de164dcc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 23 deletions

View File

@ -9,6 +9,18 @@ repository = "https://github.com/hecrj/iced"
[features] [features]
svg = ["resvg", "usvg"] svg = ["resvg", "usvg"]
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"] canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"] qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"] default_system_font = ["iced_graphics/font-source"]
@ -35,8 +47,10 @@ version = "0.1"
path = "../graphics" path = "../graphics"
features = ["font-fallback", "font-icons"] features = ["font-fallback", "font-icons"]
[dependencies.image] [dependencies.image_rs]
version = "0.23" version = "0.23"
package = "image"
default-features = false
optional = true optional = true
[dependencies.resvg] [dependencies.resvg]

View File

@ -9,7 +9,7 @@ use iced_graphics::{Primitive, Viewport};
use iced_native::mouse; use iced_native::mouse;
use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment}; use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
use crate::image; use crate::image;
/// A [`wgpu`] graphics backend for [`iced`]. /// A [`wgpu`] graphics backend for [`iced`].
@ -22,7 +22,7 @@ pub struct Backend {
text_pipeline: text::Pipeline, text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline, triangle_pipeline: triangle::Pipeline,
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
image_pipeline: image::Pipeline, image_pipeline: image::Pipeline,
default_text_size: u16, default_text_size: u16,
@ -40,7 +40,7 @@ impl Backend {
settings.antialiasing, settings.antialiasing,
); );
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
let image_pipeline = image::Pipeline::new(device, settings.format); let image_pipeline = image::Pipeline::new(device, settings.format);
Self { Self {
@ -48,7 +48,7 @@ impl 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: settings.default_text_size, default_text_size: settings.default_text_size,
@ -92,7 +92,7 @@ impl Backend {
); );
} }
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
self.image_pipeline.trim_cache(); self.image_pipeline.trim_cache();
*mouse_interaction *mouse_interaction
@ -142,7 +142,7 @@ impl Backend {
); );
} }
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
{ {
if !layer.images.is_empty() { if !layer.images.is_empty() {
let scaled = transformation let scaled = transformation
@ -270,7 +270,7 @@ impl backend::Text for Backend {
} }
} }
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
impl backend::Image for Backend { impl backend::Image for Backend {
fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) { fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) {
self.image_pipeline.dimensions(handle) self.image_pipeline.dimensions(handle)

View File

@ -1,6 +1,6 @@
mod atlas; mod atlas;
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
mod raster; mod raster;
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -16,7 +16,7 @@ use std::mem;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
use iced_native::image; use iced_native::image;
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -24,7 +24,7 @@ use iced_native::svg;
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
raster_cache: RefCell<raster::Cache>, raster_cache: RefCell<raster::Cache>,
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
vector_cache: RefCell<vector::Cache>, vector_cache: RefCell<vector::Cache>,
@ -259,7 +259,7 @@ impl Pipeline {
}); });
Pipeline { Pipeline {
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
raster_cache: RefCell::new(raster::Cache::new()), raster_cache: RefCell::new(raster::Cache::new()),
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -278,7 +278,7 @@ impl Pipeline {
} }
} }
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
pub fn dimensions(&self, handle: &image::Handle) -> (u32, u32) { pub fn dimensions(&self, handle: &image::Handle) -> (u32, u32) {
let mut cache = self.raster_cache.borrow_mut(); let mut cache = self.raster_cache.borrow_mut();
let memory = cache.load(&handle); let memory = cache.load(&handle);
@ -307,7 +307,7 @@ impl Pipeline {
) { ) {
let instances: &mut Vec<Instance> = &mut Vec::new(); let instances: &mut Vec<Instance> = &mut Vec::new();
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
let mut raster_cache = self.raster_cache.borrow_mut(); let mut raster_cache = self.raster_cache.borrow_mut();
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -315,7 +315,7 @@ impl Pipeline {
for image in images { for image in images {
match &image { match &image {
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
layer::Image::Raster { handle, bounds } => { layer::Image::Raster { handle, bounds } => {
if let Some(atlas_entry) = raster_cache.upload( if let Some(atlas_entry) = raster_cache.upload(
handle, handle,
@ -331,7 +331,7 @@ impl Pipeline {
); );
} }
} }
#[cfg(not(feature = "image"))] #[cfg(not(feature = "image_rs"))]
layer::Image::Raster { .. } => {} layer::Image::Raster { .. } => {}
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -464,7 +464,7 @@ impl Pipeline {
} }
pub fn trim_cache(&mut self) { pub fn trim_cache(&mut self) {
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
self.raster_cache.borrow_mut().trim(&mut self.texture_atlas); self.raster_cache.borrow_mut().trim(&mut self.texture_atlas);
#[cfg(feature = "svg")] #[cfg(feature = "svg")]

View File

@ -10,7 +10,7 @@ pub enum Entry {
} }
impl Entry { impl Entry {
#[cfg(feature = "image")] #[cfg(feature = "image_rs")]
pub fn size(&self) -> (u32, u32) { pub fn size(&self) -> (u32, u32) {
match self { match self {
Entry::Contiguous(allocation) => allocation.size(), Entry::Contiguous(allocation) => allocation.size(),

View File

@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet};
#[derive(Debug)] #[derive(Debug)]
pub enum Memory { pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>), Host(::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>),
Device(atlas::Entry), Device(atlas::Entry),
NotFound, NotFound,
Invalid, Invalid,
@ -42,14 +42,14 @@ impl Cache {
let memory = match handle.data() { let memory = match handle.data() {
image::Data::Path(path) => { image::Data::Path(path) => {
if let Ok(image) = ::image::open(path) { if let Ok(image) = ::image_rs::open(path) {
Memory::Host(image.to_bgra8()) Memory::Host(image.to_bgra8())
} else { } else {
Memory::NotFound Memory::NotFound
} }
} }
image::Data::Bytes(bytes) => { 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()) Memory::Host(image.to_bgra8())
} else { } else {
Memory::Invalid Memory::Invalid
@ -60,7 +60,7 @@ impl Cache {
height, height,
pixels, pixels,
} => { } => {
if let Some(image) = ::image::ImageBuffer::from_vec( if let Some(image) = ::image_rs::ImageBuffer::from_vec(
*width, *width,
*height, *height,
pixels.to_vec(), pixels.to_vec(),

View File

@ -49,7 +49,7 @@ pub use widget::*;
pub(crate) use iced_graphics::Transformation; pub(crate) use iced_graphics::Transformation;
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
mod image; mod image;
/// A [`wgpu`] graphics renderer for [`iced`]. /// A [`wgpu`] graphics renderer for [`iced`].