Skip to content

Commit

Permalink
Added Transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahinkumar committed Jan 4, 2025
1 parent 6272254 commit cdf9186
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
13 changes: 2 additions & 11 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use std::io::Cursor;
use image::{DynamicImage, ImageReader};


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;
}
use std::io::Cursor;
use crate::utils::decoder;


pub fn blur(image_bytes: Vec<u8>,process_param: f32) -> Vec<u8>{
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod transform;
pub mod filter;
pub mod utils;

use axum::extract::{Path, Query, Request};
use axum::http::header;
Expand All @@ -23,7 +24,7 @@ struct ProcessParameters {
filter: String,
f_param: f32,
transform: String,
t_param: i8,
t_param: i32,
}

fn default_param() -> ProcessParameters {
Expand Down
69 changes: 62 additions & 7 deletions src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::io::Cursor;
use image::ImageReader;
use image::DynamicImage;

use crate::utils::decoder;

pub fn resizer(image_bytes: Vec<u8>,x: u32 ,y: u32 ,filter: &str) -> Vec<u8>{
let decoded = ImageReader::new(Cursor::new(image_bytes))
.with_guessed_format()
.expect("Unable to find format")
.decode()
.expect("Unable to decode");
let decoded = decoder(image_bytes);
let final_filter = choose_resize_filter(filter);
//println!("{:?}",filter);

let resized = decoded.resize(x, y, final_filter);

let mut bytes: Vec<u8> = Vec::new();
Expand All @@ -19,6 +17,63 @@ pub fn resizer(image_bytes: Vec<u8>,x: u32 ,y: u32 ,filter: &str) -> Vec<u8>{
bytes
}


pub fn flip_horizontal(image_bytes: Vec<u8>) -> Vec<u8>{
let decoded = decoder(image_bytes);
let flipped_h = decoded.fliph();

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

pub fn flip_vertical(image_bytes: Vec<u8>) -> Vec<u8>{
let decoded = decoder(image_bytes);
let flipped_v = decoded.flipv();

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

pub fn rotate(image_bytes: Vec<u8>,transform_param: i32) -> Vec<u8>{
let decoded = decoder(image_bytes);
let rotated: DynamicImage;
if transform_param == 1 {
rotated = decoded.rotate90();
}else if transform_param == 2 {
rotated = decoded.rotate180();
}else if transform_param == 3 {
rotated = decoded.rotate270();
}else{
rotated = decoded
}

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

pub fn hue_rotate(image_bytes: Vec<u8>,transform_param: i32) -> Vec<u8>{
let decoded = decoder(image_bytes);
let hue_rotated = decoded.huerotate(transform_param);

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




fn choose_resize_filter(filter: &str) -> image::imageops::FilterType {
//For now we choose the Nearest resize filter implicitly.
match filter {
Expand Down
11 changes: 11 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::io::Cursor;
use image::{DynamicImage, ImageReader};

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;
}

0 comments on commit cdf9186

Please sign in to comment.