Add image filters

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 63ea60b10a
commit b9cf095cfa
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -270,7 +270,7 @@ Hide a section (including subsections) in the TOC
#[toc-hidden] Section
Set the size of an image
!(url)[width = 42% height=auto]
!(url)[width = 42%, height=auto, grayscale, brightness=10, contrast=1.2]
Set the source of a quote
[author=Me date=[[date]] display="{{author}} - {{date}}"]> It's me

@ -710,6 +710,8 @@ impl Placeholder {
pub trait Metadata {
fn get_bool(&self, key: &str) -> bool;
fn get_string(&self, key: &str) -> Option<String>;
fn get_float(&self, key: &str) -> Option<f64>;
fn get_integer(&self, key: &str) -> Option<i64>;
fn get_string_map(&self) -> HashMap<String, String>;
}
@ -730,6 +732,24 @@ impl Metadata for InlineMetadata {
}
}
fn get_float(&self, key: &str) -> Option<f64> {
if let Some(MetadataValue::Float(f)) = self.data.get(key) {
Some(*f)
} else if let Some(MetadataValue::Integer(i)) = self.data.get(key) {
Some(*i as f64)
} else {
None
}
}
fn get_integer(&self, key: &str) -> Option<i64> {
if let Some(MetadataValue::Integer(i)) = self.data.get(key) {
Some(*i)
} else {
None
}
}
fn get_string_map(&self) -> HashMap<String, String> {
let mut string_map = HashMap::new();
for (k, v) in &self.data {

@ -140,21 +140,24 @@ impl ParseInline for Parser {
self.ctm.seek_one()?;
if let Ok(url) = self.parse_url(true) {
let metadata = if let Ok(meta) = self.parse_inline_metadata() {
Some(meta)
} else {
None
};
let metadata = self.parse_inline_metadata().ok();
let path = url.url.clone();
let pending_image = self
.options
.document
.images
.lock()
.add_image(PathBuf::from(path));
if let Some(meta) = &metadata {
pending_image.lock().assign_from_meta(meta)
}
Ok(Image {
url,
metadata,
image_data: self
.options
.document
.images
.lock()
.add_image(PathBuf::from(path)),
image_data: pending_image,
})
} else {
Err(self.ctm.rewind_with_error(start_index))
@ -502,6 +505,7 @@ impl ParseInline for Parser {
self.ctm.seek_any(&INLINE_WHITESPACE)?;
let mut value = MetadataValue::Bool(true);
if self.ctm.check_char(&EQ) {
self.ctm.seek_one()?;
self.ctm.seek_any(&INLINE_WHITESPACE)?;

@ -1,3 +1,4 @@
use crate::elements::Metadata;
use crate::utils::caching::CacheStorage;
use crate::utils::downloads::download_path;
use image::imageops::FilterType;
@ -60,6 +61,9 @@ pub struct PendingImage {
pub data: Option<Vec<u8>>,
cache: CacheStorage,
pub mime: Mime,
brightness: Option<i32>,
contrast: Option<f32>,
grayscale: bool,
}
impl PendingImage {
@ -71,9 +75,22 @@ impl PendingImage {
data: None,
cache: CacheStorage::new(),
mime,
brightness: None,
contrast: None,
grayscale: false,
}
}
pub fn assign_from_meta<M: Metadata>(&mut self, meta: &M) {
if let Some(brightness) = meta.get_integer("brightness") {
self.brightness = Some(brightness as i32);
}
if let Some(contrast) = meta.get_float("contrast") {
self.contrast = Some(contrast as f32);
}
self.grayscale = meta.get_bool("grayscale");
}
/// Converts the image to the specified target format (specified by target_extension)
pub fn convert(
&mut self,
@ -87,6 +104,7 @@ impl PendingImage {
.and_then(|extension| ImageFormat::from_extension(extension))
})
.unwrap_or(ImageFormat::Png);
let output_path = self.get_output_path(format, target_size);
self.mime = get_mime(&output_path);
@ -118,6 +136,18 @@ impl PendingImage {
image = image.resize(width, height, FilterType::Lanczos3);
}
}
if let Some(brightness) = self.brightness {
image = image.brighten(brightness);
}
if let Some(contrast) = self.contrast {
image = image.adjust_contrast(contrast);
}
if self.grayscale {
image = image.grayscale();
}
let data = Vec::new();
let mut writer = Cursor::new(data);
@ -155,7 +185,14 @@ impl PendingImage {
if let Some(target_size) = target_size {
file_name += &*format!("-{}-{}", target_size.0, target_size.1);
}
file_name += format!("-{}-converted", type_name).as_str();
if let Some(b) = self.brightness {
file_name += &*format!("-{}", b);
}
if let Some(c) = self.contrast {
file_name += &*format!("-{}", c);
}
file_name += format!("-{}", type_name).as_str();
path.set_file_name(file_name);
path.set_extension(extension);

Loading…
Cancel
Save