Skip to content

Commit

Permalink
Add Brighten Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahinkumar committed Jan 4, 2025
1 parent c5c5897 commit 2784343
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use axum::{Router, ServiceExt};

use resize::resizer;
use std::net::SocketAddr;
use proc::{blur, grayscale};
use proc::{blur, brighten, grayscale};

use serde::Deserialize;
//use std::time::Instant; // For timing functions
Expand Down Expand Up @@ -76,7 +76,8 @@ async fn handler(
if do_proc {
match process_params.filter.to_lowercase().as_str(){
"blur" => { bytes = blur(bytes, process_params.f_param)},
"bw" => {bytes = grayscale(bytes)}
"bw" => {bytes = grayscale(bytes)},
"brighten" => {bytes = brighten(bytes, process_params.f_param)},
_ => {}
}
//let elapsed = now.elapsed();
Expand Down
31 changes: 22 additions & 9 deletions src/proc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use std::io::Cursor;
use image::ImageReader;
use image::{DynamicImage, ImageReader};


pub fn blur(image_bytes: Vec<u8>,process_param: f32) -> Vec<u8>{
pub fn decoder(image_bytes: Vec<u8>)->DynamicImage{
let decoded = ImageReader::new(Cursor::new(image_bytes))
.with_guessed_format()
.expect("Unable to find format")
.decode()
.expect("Unable to decode");
return decoded;
}


pub fn blur(image_bytes: Vec<u8>,process_param: f32) -> Vec<u8>{
let decoded = decoder(image_bytes);
let blurred = decoded.fast_blur(process_param);

let mut bytes: Vec<u8> = Vec::new();
Expand All @@ -18,15 +24,22 @@ pub fn blur(image_bytes: Vec<u8>,process_param: f32) -> Vec<u8>{
}

pub fn grayscale(image_bytes: Vec<u8>) -> Vec<u8>{
let decoded = ImageReader::new(Cursor::new(image_bytes))
.with_guessed_format()
.expect("Unable to find format")
.decode()
.expect("Unable to decode");
let blurred = decoded.grayscale();
let decoded = decoder(image_bytes);
let grayscaled = decoded.grayscale();

let mut bytes: Vec<u8> = Vec::new();
blurred
grayscaled
.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Jpeg)
.expect("Unable to write");
bytes
}

pub fn brighten(image_bytes: Vec<u8>,process_param: f32) -> Vec<u8>{
let decoded = decoder(image_bytes);
let brightened = decoded.brighten(((process_param*51.0)) as i32);

let mut bytes: Vec<u8> = Vec::new();
brightened
.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Jpeg)
.expect("Unable to write");
bytes
Expand Down

0 comments on commit 2784343

Please sign in to comment.