diff options
author | Martin Robinson <mrobinson@igalia.com> | 2023-10-05 19:47:39 +0200 |
---|---|---|
committer | Martin Robinson <mrobinson@igalia.com> | 2023-11-03 15:38:18 +0000 |
commit | f4d3af296c05260dfbb3deea4f8fa400cb6887d3 (patch) | |
tree | 169db2cc68e01a755b30500dd525f1a2ec2da861 /components/shared/net/image | |
parent | 863529d9622c68f0a9535225237eb5e5c5b8c757 (diff) | |
download | servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.tar.gz servo-f4d3af296c05260dfbb3deea4f8fa400cb6887d3.zip |
Move `*_traits` and other shared types to `shared`
This is the start of the organization of types that are in their own
crates in order to break dependency cycles between other crates. The
idea here is that putting these packages into their own directory is the
first step toward cleaning them up. They have grown organically and it
is difficult to explain to new folks where to put new shared types. Many
of these crates contain more than traits or don't contain traits at all.
Notably, `script_traits` isn't touched because it is vendored from
Gecko. Eventually this will move to `third_party`.
Diffstat (limited to 'components/shared/net/image')
-rw-r--r-- | components/shared/net/image/base.rs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/components/shared/net/image/base.rs b/components/shared/net/image/base.rs new file mode 100644 index 00000000000..74e2d8823dc --- /dev/null +++ b/components/shared/net/image/base.rs @@ -0,0 +1,121 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use std::fmt; + +use image::ImageFormat; +use ipc_channel::ipc::IpcSharedMemory; +use log::debug; +use malloc_size_of_derive::MallocSizeOf; +use pixels::PixelFormat; +use serde::{Deserialize, Serialize}; +use webrender_api::ImageKey; + +use crate::image_cache::CorsStatus; + +#[derive(Clone, Deserialize, MallocSizeOf, Serialize)] +pub struct Image { + pub width: u32, + pub height: u32, + pub format: PixelFormat, + #[ignore_malloc_size_of = "Defined in ipc-channel"] + pub bytes: IpcSharedMemory, + #[ignore_malloc_size_of = "Defined in webrender_api"] + pub id: Option<ImageKey>, + pub cors_status: CorsStatus, +} + +impl fmt::Debug for Image { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "Image {{ width: {}, height: {}, format: {:?}, ..., id: {:?} }}", + self.width, self.height, self.format, self.id + ) + } +} + +#[derive(Clone, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] +pub struct ImageMetadata { + pub width: u32, + pub height: u32, +} + +// FIXME: Images must not be copied every frame. Instead we should atomically +// reference count them. + +pub fn load_from_memory(buffer: &[u8], cors_status: CorsStatus) -> Option<Image> { + if buffer.is_empty() { + return None; + } + + let image_fmt_result = detect_image_format(buffer); + match image_fmt_result { + Err(msg) => { + debug!("{}", msg); + None + }, + Ok(_) => match image::load_from_memory(buffer) { + Ok(image) => { + let mut rgba = image.into_rgba8(); + pixels::rgba8_byte_swap_colors_inplace(&mut *rgba); + Some(Image { + width: rgba.width(), + height: rgba.height(), + format: PixelFormat::BGRA8, + bytes: IpcSharedMemory::from_bytes(&*rgba), + id: None, + cors_status, + }) + }, + Err(e) => { + debug!("Image decoding error: {:?}", e); + None + }, + }, + } +} + +// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img +pub fn detect_image_format(buffer: &[u8]) -> Result<ImageFormat, &str> { + if is_gif(buffer) { + Ok(ImageFormat::Gif) + } else if is_jpeg(buffer) { + Ok(ImageFormat::Jpeg) + } else if is_png(buffer) { + Ok(ImageFormat::Png) + } else if is_webp(buffer) { + Ok(ImageFormat::WebP) + } else if is_bmp(buffer) { + Ok(ImageFormat::Bmp) + } else if is_ico(buffer) { + Ok(ImageFormat::Ico) + } else { + Err("Image Format Not Supported") + } +} + +fn is_gif(buffer: &[u8]) -> bool { + buffer.starts_with(b"GIF87a") || buffer.starts_with(b"GIF89a") +} + +fn is_jpeg(buffer: &[u8]) -> bool { + buffer.starts_with(&[0xff, 0xd8, 0xff]) +} + +fn is_png(buffer: &[u8]) -> bool { + buffer.starts_with(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]) +} + +fn is_bmp(buffer: &[u8]) -> bool { + buffer.starts_with(&[0x42, 0x4D]) +} + +fn is_ico(buffer: &[u8]) -> bool { + buffer.starts_with(&[0x00, 0x00, 0x01, 0x00]) +} + +fn is_webp(buffer: &[u8]) -> bool { + buffer.starts_with(b"RIFF") && buffer.len() >= 14 && &buffer[8..14] == b"WEBPVP" +} |