Implement WebP support

This also updates the image crate to 0.24.2

Signed-off-by: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com>
This commit is contained in:
Ennui Langeweile 2022-05-10 18:40:19 -03:00
parent c23adde5a3
commit b2ea5f0160
No known key found for this signature in database
GPG key ID: 688A9E714BCD13B2
4 changed files with 22 additions and 12 deletions

18
Cargo.lock generated
View file

@ -884,7 +884,7 @@ dependencies = [
"euclid", "euclid",
"gfx_traits", "gfx_traits",
"gleam 0.12.1", "gleam 0.12.1",
"image 0.24.1", "image 0.24.2",
"ipc-channel", "ipc-channel",
"keyboard-types", "keyboard-types",
"libc", "libc",
@ -1689,9 +1689,9 @@ dependencies = [
[[package]] [[package]]
name = "exr" name = "exr"
version = "1.4.1" version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4badb9489a465cb2c555af1f00f0bfd8cecd6fc12ac11da9d5b40c5dd5f0200" checksum = "14cc0e06fb5f67e5d6beadf3a382fec9baca1aa751c6d5368fdeee7e5932c215"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"deflate 1.0.0", "deflate 1.0.0",
@ -2977,9 +2977,9 @@ dependencies = [
[[package]] [[package]]
name = "image" name = "image"
version = "0.24.1" version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db207d030ae38f1eb6f240d5a1c1c88ff422aa005d10f8c6c6fc5e75286ab30e" checksum = "28edd9d7bc256be2502e325ac0628bde30b7001b9b52e0abe31a1a9dc2701212"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
"byteorder", "byteorder",
@ -4180,7 +4180,7 @@ dependencies = [
"http", "http",
"hyper 0.14.5", "hyper 0.14.5",
"hyper_serde", "hyper_serde",
"image 0.24.1", "image 0.24.2",
"ipc-channel", "ipc-channel",
"lazy_static", "lazy_static",
"log", "log",
@ -5387,7 +5387,7 @@ dependencies = [
"html5ever", "html5ever",
"http", "http",
"hyper_serde", "hyper_serde",
"image 0.24.1", "image 0.24.2",
"indexmap", "indexmap",
"ipc-channel", "ipc-channel",
"itertools", "itertools",
@ -5665,7 +5665,7 @@ dependencies = [
"clipboard", "clipboard",
"euclid", "euclid",
"getopts", "getopts",
"image 0.24.1", "image 0.24.2",
"keyboard-types", "keyboard-types",
"lazy_static", "lazy_static",
"libc", "libc",
@ -7301,7 +7301,7 @@ dependencies = [
"euclid", "euclid",
"headers", "headers",
"http", "http",
"image 0.24.1", "image 0.24.2",
"ipc-channel", "ipc-channel",
"keyboard-types", "keyboard-types",
"log", "log",

View file

@ -20,6 +20,7 @@ headers = "0.3"
http = "0.2" http = "0.2"
hyper = "0.14" hyper = "0.14"
hyper_serde = "0.13" hyper_serde = "0.13"
image = "0.24"
ipc-channel = "0.14" ipc-channel = "0.14"
lazy_static = "1" lazy_static = "1"
log = "0.4" log = "0.4"
@ -29,7 +30,6 @@ mime = "0.3"
msg = { path = "../msg" } msg = { path = "../msg" }
num-traits = "0.2" num-traits = "0.2"
percent-encoding = "2.0" percent-encoding = "2.0"
piston_image = { package = "image", version = "0.24" }
pixels = { path = "../pixels" } pixels = { path = "../pixels" }
serde = "1.0" serde = "1.0"
servo_arc = { path = "../servo_arc" } servo_arc = { path = "../servo_arc" }

View file

@ -3,8 +3,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::image_cache::CorsStatus; use crate::image_cache::CorsStatus;
use image::ImageFormat;
use ipc_channel::ipc::IpcSharedMemory; use ipc_channel::ipc::IpcSharedMemory;
use piston_image::ImageFormat;
use pixels::PixelFormat; use pixels::PixelFormat;
use std::fmt; use std::fmt;
@ -50,7 +50,7 @@ pub fn load_from_memory(buffer: &[u8], cors_status: CorsStatus) -> Option<Image>
debug!("{}", msg); debug!("{}", msg);
None None
}, },
Ok(_) => match piston_image::load_from_memory(buffer) { Ok(_) => match image::load_from_memory(buffer) {
Ok(image) => { Ok(image) => {
let mut rgba = image.into_rgba8(); let mut rgba = image.into_rgba8();
pixels::rgba8_byte_swap_colors_inplace(&mut *rgba); pixels::rgba8_byte_swap_colors_inplace(&mut *rgba);
@ -79,6 +79,8 @@ pub fn detect_image_format(buffer: &[u8]) -> Result<ImageFormat, &str> {
Ok(ImageFormat::Jpeg) Ok(ImageFormat::Jpeg)
} else if is_png(buffer) { } else if is_png(buffer) {
Ok(ImageFormat::Png) Ok(ImageFormat::Png)
} else if is_webp(buffer) {
Ok(ImageFormat::WebP)
} else if is_bmp(buffer) { } else if is_bmp(buffer) {
Ok(ImageFormat::Bmp) Ok(ImageFormat::Bmp)
} else if is_ico(buffer) { } else if is_ico(buffer) {
@ -107,3 +109,7 @@ fn is_bmp(buffer: &[u8]) -> bool {
fn is_ico(buffer: &[u8]) -> bool { fn is_ico(buffer: &[u8]) -> bool {
buffer.starts_with(&[0x00, 0x00, 0x01, 0x00]) 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"
}

View file

@ -10,6 +10,9 @@ fn test_supported_images() {
let gif2 = [b'G', b'I', b'F', b'8', b'9', b'a']; let gif2 = [b'G', b'I', b'F', b'8', b'9', b'a'];
let jpeg = [0xff, 0xd8, 0xff]; let jpeg = [0xff, 0xd8, 0xff];
let png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; let png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
let webp = [
b'R', b'I', b'F', b'F', 0x01, 0x02, 0x03, 0x04, b'W', b'E', b'B', b'P', b'V', b'P',
];
let bmp = [0x42, 0x4D]; let bmp = [0x42, 0x4D];
let ico = [0x00, 0x00, 0x01, 0x00]; let ico = [0x00, 0x00, 0x01, 0x00];
let junk_format = [0x01, 0x02, 0x03, 0x04, 0x05]; let junk_format = [0x01, 0x02, 0x03, 0x04, 0x05];
@ -18,6 +21,7 @@ fn test_supported_images() {
assert!(detect_image_format(&gif2).is_ok()); assert!(detect_image_format(&gif2).is_ok());
assert!(detect_image_format(&jpeg).is_ok()); assert!(detect_image_format(&jpeg).is_ok());
assert!(detect_image_format(&png).is_ok()); assert!(detect_image_format(&png).is_ok());
assert!(detect_image_format(&webp).is_ok());
assert!(detect_image_format(&bmp).is_ok()); assert!(detect_image_format(&bmp).is_ok());
assert!(detect_image_format(&ico).is_ok()); assert!(detect_image_format(&ico).is_ok());
assert!(detect_image_format(&junk_format).is_err()); assert!(detect_image_format(&junk_format).is_err());