Skip to content

Commit

Permalink
Merge pull request #164 from advancedtw/webp
Browse files Browse the repository at this point in the history
Webp image support
  • Loading branch information
silvia-odwyer committed Jan 31, 2024
2 parents d53cb48 + 3273bbb commit 55968ef
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ react_app_demo/package-lock.json
photon-docs/site
output*.jpg
output*.png
.vscode
39 changes: 25 additions & 14 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@ readme = "./README.md"
repository = "https://github.com/silvia-odwyer/photon"
version = "0.3.2"
edition = "2021"
exclude = [
"pkg/*",
"examples/input_images/*"
]
exclude = ["pkg/*", "examples/input_images/*"]
homepage = "https://silvia-odwyer.github.io/photon/"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
image = { version = "0.23.12", default-features = false, features = ["gif", "jpeg", "ico", "png", "pnm", "tga", "tiff", "webp", "bmp", "hdr", "dxt", "dds", "farbfeld"] }
palette="0.6.1"
rand="0.7.2"
imageproc = { version = "0.22.0", default-features = false }
rusttype="0.9.2"
base64="0.13.0"
time="0.3.21"
image = { version = "0.24.8", default-features = false, features = [
"gif",
"jpeg",
"ico",
"png",
"pnm",
"tga",
"tiff",
"webp",
"bmp",
"hdr",
"dxt",
"dds",
"farbfeld",
] }
palette = "0.6.1"
rand = "0.7.2"
imageproc = { version = "0.23.0", default-features = false }
rusttype = "0.9.2"
base64 = "0.13.0"
time = "0.3.21"
wasm-bindgen = { version = "=0.2.85", optional = true }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
Expand All @@ -44,7 +55,7 @@ console_error_panic_hook = { version = "0.1.7", optional = true }
wee_alloc = { version = "0.4.2", optional = true }

[dev-dependencies]
time="0.3.21"
time = "0.3.21"
criterion = "0.3"

[[bench]]
Expand All @@ -69,7 +80,7 @@ features = [
"console",
'CssStyleDeclaration',
'EventTarget',
"Blob"
"Blob",
]
optional = true

Expand All @@ -80,5 +91,5 @@ enable_wasm = [
"web-sys",
"js-sys",
"node-sys",
"console_error_panic_hook"
"console_error_panic_hook",
]
2 changes: 1 addition & 1 deletion crate/src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ pub fn selective_greyscale(mut photon_image: PhotonImage, ref_color: Rgb) {
img.put_pixel(x, y, px);
}

let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down
4 changes: 2 additions & 2 deletions crate/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn conv(photon_image: &mut PhotonImage, kernel: Kernel) {
}
}

photon_image.raw_pixels = filtered_img.to_bytes();
photon_image.raw_pixels = filtered_img.into_bytes();
}

/// Noise reduction.
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn box_blur(photon_image: &mut PhotonImage) {
pub fn gaussian_blur(photon_image: &mut PhotonImage, radius: i32) {
// construct pixel data
let img = helpers::dyn_image_from_raw(photon_image);
let mut src = img.to_bytes();
let mut src = img.into_bytes();

let width = photon_image.get_width();
let height = photon_image.get_height();
Expand Down
26 changes: 14 additions & 12 deletions crate/src/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn offset(photon_image: &mut PhotonImage, channel_index: usize, offset: u32)
}
}
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ pub fn multiple_offsets(

img.put_pixel(x, y, px);
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -311,7 +311,7 @@ pub fn halftone(photon_image: &mut PhotonImage) {
// img.put_pixel(x, y + 1, px2);
}
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -411,7 +411,7 @@ pub fn colorize(photon_image: &mut PhotonImage) {
px = image::Rgba([r as u8, g as u8, b as u8, 255]);
img.put_pixel(x, y, px);
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -520,10 +520,12 @@ pub fn solarize_retimg(photon_image: &PhotonImage) -> PhotonImage {
img.put_pixel(x, y, px);
}

let (width, height) = img.dimensions();

PhotonImage {
raw_pixels: img.to_bytes(),
width: img.width(),
height: img.height(),
raw_pixels: img.into_bytes(),
width,
height,
}
}

Expand Down Expand Up @@ -615,7 +617,7 @@ pub fn adjust_contrast(photon_image: &mut PhotonImage, contrast: f32) {
img.put_pixel(x, y, px);
}

photon_image.raw_pixels = img.to_bytes();
photon_image.raw_pixels = img.into_bytes();
}

/// Tint an image by adding an offset to averaged RGB channel values.
Expand Down Expand Up @@ -672,7 +674,7 @@ pub fn tint(
img.put_pixel(x, y, px);
}

let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand All @@ -692,7 +694,7 @@ fn draw_horizontal_strips(photon_image: &mut PhotonImage, num_strips: u8, color:
y_pos = i as u32 * (height_strip * 2);
}

let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -766,7 +768,7 @@ fn draw_vertical_strips(photon_image: &mut PhotonImage, num_strips: u8, color: R
x_pos = i as u32 * (width_strip * 2);
}

let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -925,7 +927,7 @@ pub fn oil(photon_image: &mut PhotonImage, radius: i32, intensity: f64) {
)
}
}
let raw_pixels = target.to_bytes();
let raw_pixels = target.into_bytes();
photon_image.raw_pixels = raw_pixels;
}
/// Turn an image into an frosted glass see through
Expand Down
2 changes: 1 addition & 1 deletion crate/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn save_dyn_image(img: DynamicImage, filtered_img_path: &str) {
/// Get raw pixels (as a vec of u8s) from a DynamicImage
pub fn get_pixels(img: DynamicImage) -> Vec<u8> {
// get an image's raw pixels, and return as a vec of u8s
img.to_bytes()
img.into_bytes()
}

/// Convert a PhotonImage to a DynamicImage type (struct used by the `image` crate)
Expand Down
31 changes: 24 additions & 7 deletions crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@

use base64::{decode, encode};
use image::DynamicImage::ImageRgba8;
use image::{GenericImage, GenericImageView};
use image::GenericImage;
use serde::{Deserialize, Serialize};
use std::io::Cursor;

#[cfg(feature = "enable_wasm")]
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -179,7 +180,7 @@ impl PhotonImage {
img = ImageRgba8(img.to_rgba8());

let mut buffer = vec![];
img.write_to(&mut buffer, image::ImageOutputFormat::Png)
img.write_to(&mut Cursor::new(&mut buffer), image::ImageOutputFormat::Png)
.unwrap();
let base64 = encode(&buffer);

Expand All @@ -193,7 +194,7 @@ impl PhotonImage {
let mut img = helpers::dyn_image_from_raw(self);
img = ImageRgba8(img.to_rgba8());
let mut buffer = vec![];
img.write_to(&mut buffer, image::ImageOutputFormat::Png)
img.write_to(&mut Cursor::new(&mut buffer), image::ImageOutputFormat::Png)
.unwrap();
buffer
}
Expand All @@ -204,7 +205,19 @@ impl PhotonImage {
img = ImageRgba8(img.to_rgba8());
let mut buffer = vec![];
let out_format = image::ImageOutputFormat::Jpeg(quality);
img.write_to(&mut buffer, out_format).unwrap();
img.write_to(&mut Cursor::new(&mut buffer), out_format)
.unwrap();
buffer
}

/// Convert the PhotonImage to raw bytes. Returns a WEBP.
pub fn get_bytes_webp(&self) -> Vec<u8> {
let mut img = helpers::dyn_image_from_raw(self);
img = ImageRgba8(img.to_rgba8());
let mut buffer = vec![];
let out_format = image::ImageOutputFormat::WebP;
img.write_to(&mut Cursor::new(&mut buffer), out_format)
.unwrap();
buffer
}

Expand Down Expand Up @@ -472,12 +485,16 @@ pub fn base64_to_image(base64: &str) -> PhotonImage {

let mut img = image::load_from_memory(slice).unwrap();
img = ImageRgba8(img.to_rgba8());
let raw_pixels = img.to_bytes();

let width = img.width();
let height = img.height();

let raw_pixels = img.into_bytes();

PhotonImage {
raw_pixels,
width: img.width(),
height: img.height(),
width,
height,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crate/src/monochrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub fn grayscale_shades(photon_image: &mut PhotonImage, num_shades: u8) {

img.put_pixel(x, y, image::Rgba([gray, gray, gray, 255]));
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -409,7 +409,7 @@ pub fn single_channel_grayscale(photon_image: &mut PhotonImage, channel: usize)
image::Rgba([channel_data, channel_data, channel_data, 255]),
);
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down
12 changes: 6 additions & 6 deletions crate/src/multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ use wasm_bindgen::prelude::*;
///
/// let mut img = open_image("img.jpg").expect("File should open");
/// let water_mark = open_image("watermark.jpg").expect("File should open");
/// watermark(&mut img, &water_mark, 30_u32, 40_u32);
/// watermark(&mut img, &water_mark, 30_i64, 40_i64);
/// ```
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn watermark(img: &mut PhotonImage, watermark: &PhotonImage, x: u32, y: u32) {
pub fn watermark(img: &mut PhotonImage, watermark: &PhotonImage, x: i64, y: i64) {
let dyn_watermark: DynamicImage = crate::helpers::dyn_image_from_raw(watermark);
let mut dyn_img: DynamicImage = crate::helpers::dyn_image_from_raw(img);
image::imageops::overlay(&mut dyn_img, &dyn_watermark, x, y);
img.raw_pixels = dyn_img.to_bytes();
img.raw_pixels = dyn_img.into_bytes();
}

/// Blend two images together.
Expand Down Expand Up @@ -139,7 +139,7 @@ pub fn blend(
);
}
let dynimage = ImageRgba8(img);
photon_image.raw_pixels = dynimage.to_bytes();
photon_image.raw_pixels = dynimage.into_bytes();
}

// #[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn replace_background(
img.put_pixel(x, y, px);
}
}
let raw_pixels = img.to_bytes();
let raw_pixels = img.into_bytes();
photon_image.raw_pixels = raw_pixels;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn create_gradient(width: u32, height: u32) -> PhotonImage {
}
}
let rgba_img = ImageRgba8(image);
let raw_pixels = rgba_img.to_bytes();
let raw_pixels = rgba_img.into_bytes();
PhotonImage {
raw_pixels,
width,
Expand Down
2 changes: 1 addition & 1 deletion crate/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ pub fn image_to_bytes(img: PhotonImage) -> Vec<u8> {

let img_buffer = ImageBuffer::from_vec(width, height, raw_pixels).unwrap();
let dynimage = ImageRgba8(img_buffer);
dynimage.to_bytes()
dynimage.into_bytes()
}
4 changes: 2 additions & 2 deletions crate/src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn add_noise_rand(photon_image: &mut PhotonImage) {
);
img.put_pixel(x, y, px);
}
photon_image.raw_pixels = img.to_bytes();
photon_image.raw_pixels = img.into_bytes();
}

/// Add pink-tinted noise to an image.
Expand Down Expand Up @@ -111,5 +111,5 @@ pub fn pink_noise(photon_image: &mut PhotonImage) {
px = image::Rgba([new_r_val, new_g_val, new_b_val, 255]);
img.put_pixel(x, y, px);
}
photon_image.raw_pixels = img.to_bytes();
photon_image.raw_pixels = img.into_bytes();
}
8 changes: 4 additions & 4 deletions crate/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ use wasm_bindgen::prelude::*;
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// draw_text_with_border(&mut img, "Welcome to Photon!", 10_u32, 10_u32);
/// draw_text_with_border(&mut img, "Welcome to Photon!", 10_i32, 10_i32);
/// ```
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn draw_text_with_border(photon_img: &mut PhotonImage, text: &str, x: u32, y: u32) {
pub fn draw_text_with_border(photon_img: &mut PhotonImage, text: &str, x: i32, y: i32) {
let mut image = helpers::dyn_image_from_raw(photon_img).to_rgba8();

let mut image2: DynamicImage =
Expand Down Expand Up @@ -102,10 +102,10 @@ pub fn draw_text_with_border(photon_img: &mut PhotonImage, text: &str, x: u32, y
///
/// // Open the image. A PhotonImage is returned.
/// let mut img = open_image("img.jpg").expect("File should open");
/// draw_text(&mut img, "Welcome to Photon!", 10_u32, 10_u32);
/// draw_text(&mut img, "Welcome to Photon!", 10_i32, 10_i32);
/// ```
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn draw_text(photon_img: &mut PhotonImage, text: &str, x: u32, y: u32) {
pub fn draw_text(photon_img: &mut PhotonImage, text: &str, x: i32, y: i32) {
let mut image = helpers::dyn_image_from_raw(photon_img).to_rgba8();

let mut image2: DynamicImage =
Expand Down

0 comments on commit 55968ef

Please sign in to comment.