Spaces:
Sleeping
Sleeping
File size: 6,141 Bytes
343eed9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | use crate::error::Result;
use image::DynamicImage;
#[derive(Debug, Clone, Copy)]
pub enum ColorGrade {
CinematicDark,
CoolTones,
WarmTones,
Desaturated,
HighContrast,
None,
}
pub struct PostProcessor {
pub color_grade: ColorGrade,
pub contrast: f32,
pub brightness: f32,
pub saturation: f32,
}
impl Default for PostProcessor {
fn default() -> Self {
Self {
color_grade: ColorGrade::CinematicDark,
contrast: 1.1,
brightness: 0.95,
saturation: 1.1,
}
}
}
impl PostProcessor {
pub fn new(color_grade: ColorGrade) -> Self {
Self {
color_grade,
..Default::default()
}
}
/// Appliquer tous les traitements de post-processing
pub fn process(&self, img: DynamicImage) -> Result<DynamicImage> {
let mut result = img;
// 1. Appliquer le grade de couleur
result = self.apply_color_grade(result)?;
// 2. Ajuster contraste/luminosité
result = self.adjust_contrast_brightness(result)?;
// 3. Ajuster saturation
result = self.adjust_saturation(result)?;
Ok(result)
}
fn apply_color_grade(&self, img: DynamicImage) -> Result<DynamicImage> {
match self.color_grade {
ColorGrade::CinematicDark => self.apply_cinematic_dark(img),
ColorGrade::CoolTones => self.apply_cool_tones(img),
ColorGrade::WarmTones => self.apply_warm_tones(img),
ColorGrade::Desaturated => self.apply_desaturated(img),
ColorGrade::HighContrast => self.apply_high_contrast(img),
ColorGrade::None => Ok(img),
}
}
fn apply_cinematic_dark(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
// Ajouter une teinte bleuée et assombrir
pixel.0 = [
(r as f32 * 0.95) as u8,
(g as f32 * 0.98) as u8,
(b as f32 * 1.05).min(255.0) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn apply_cool_tones(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
pixel.0 = [
(r as f32 * 0.9) as u8,
(g as f32 * 0.98) as u8,
(b as f32 * 1.1).min(255.0) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn apply_warm_tones(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
pixel.0 = [
(r as f32 * 1.1).min(255.0) as u8,
(g as f32 * 1.05).min(255.0) as u8,
(b as f32 * 0.9) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn apply_desaturated(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
let gray = ((r as f32 + g as f32 + b as f32) / 3.0) as u8;
// 70% saturé = 30% gris
let sat = 0.7;
pixel.0 = [
((r as f32 * sat + gray as f32 * (1.0 - sat)) as u8),
((g as f32 * sat + gray as f32 * (1.0 - sat)) as u8),
((b as f32 * sat + gray as f32 * (1.0 - sat)) as u8),
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn apply_high_contrast(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
// Formule: (val - 128) * 1.5 + 128
let contrast = 1.5;
let offset = 128.0;
pixel.0 = [
((r as f32 - offset) * contrast + offset).clamp(0.0, 255.0) as u8,
((g as f32 - offset) * contrast + offset).clamp(0.0, 255.0) as u8,
((b as f32 - offset) * contrast + offset).clamp(0.0, 255.0) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn adjust_contrast_brightness(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
pixel.0 = [
((r as f32 - 128.0) * self.contrast + 128.0 + self.brightness * 255.0)
.clamp(0.0, 255.0) as u8,
((g as f32 - 128.0) * self.contrast + 128.0 + self.brightness * 255.0)
.clamp(0.0, 255.0) as u8,
((b as f32 - 128.0) * self.contrast + 128.0 + self.brightness * 255.0)
.clamp(0.0, 255.0) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
fn adjust_saturation(&self, img: DynamicImage) -> Result<DynamicImage> {
let rgba = img.to_rgba8();
let mut output = rgba.clone();
for pixel in output.pixels_mut() {
let [r, g, b, a] = pixel.0;
let gray = (r as f32 + g as f32 + b as f32) / 3.0;
pixel.0 = [
(r as f32 + (gray - r as f32) * (1.0 - self.saturation)).clamp(0.0, 255.0) as u8,
(g as f32 + (gray - g as f32) * (1.0 - self.saturation)).clamp(0.0, 255.0) as u8,
(b as f32 + (gray - b as f32) * (1.0 - self.saturation)).clamp(0.0, 255.0) as u8,
a,
];
}
Ok(DynamicImage::ImageRgba8(output))
}
}
|