summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2025-04-13 22:02:32 +0200
committerache <ache@ache.one>2025-04-13 22:02:32 +0200
commit75f0119c7c6a07e2dadea1b15ebbf658f70a08f2 (patch)
treeda7bd969d9c15bf0a56398722fe569b852293cdc
parentInit commit (diff)
Implement sierra
-rw-r--r--src/main.rs88
1 files changed, 44 insertions, 44 deletions
diff --git a/src/main.rs b/src/main.rs
index 46e2063..2595f78 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,74 +7,74 @@ fn dither_atkinson(image: &mut RgbImage) {
let (width, height) = image.dimensions();
let mut error: Vec<f32> = vec![0.; (3 * width) as usize];
- for y in 0..height {
- for x in 0..width {
- let Rgb([r, g, b]) = image.get_pixel(x, y);
- let pixel = (*r as u32 + *g as u32 + *b as u32) as f32 / 3.;
- let err_pos: u32 = (y * width + x) % (3 * width);
+ for (x, y, mut pixel_) in &mut image.enumerate_pixels_mut() {
+ let Rgb([r, g, b]) = pixel_;
+ let pixel = (*r as u32 + *g as u32 + *b as u32) as f32 / 3.;
+ let err_pos: u32 = (y * width + x) % (3 * width);
- let (pixel, err) = if (pixel + error[err_pos as usize]) > 127. {
- (255, (pixel + error[err_pos as usize] - 255.) / 8.)
- } else {
- (0, (pixel + error[err_pos as usize]) / 8.)
- };
+ let (pixel, err) = if (pixel + error[err_pos as usize]) > 127. {
+ (255, (pixel + error[err_pos as usize] - 255.) / 8.)
+ } else {
+ (0, (pixel + error[err_pos as usize]) / 8.)
+ };
- error[err_pos as usize] = 0.;
- error[((err_pos + 1) % (3 * width)) as usize] += err;
- error[((err_pos + 2) % (3 * width)) as usize] += err;
- error[((err_pos + width + 1) % (3 * width)) as usize] += err;
- error[((err_pos + width - 1) % (3 * width)) as usize] += err;
- error[((err_pos + width) % (3 * width)) as usize] += err;
- error[((err_pos + 2 * width) % (3 * width)) as usize] += err;
+ error[err_pos as usize] = 0.;
+ error[((err_pos + 1) % (3 * width)) as usize] += err;
+ error[((err_pos + 2) % (3 * width)) as usize] += err;
+ error[((err_pos + width + 1) % (3 * width)) as usize] += err;
+ error[((err_pos + width - 1) % (3 * width)) as usize] += err;
+ error[((err_pos + width) % (3 * width)) as usize] += err;
+ error[((err_pos + 2 * width) % (3 * width)) as usize] += err;
- image.put_pixel(x, y, image::Rgb([pixel, pixel, pixel]));
- }
+ *pixel_ = image::Rgb([pixel, pixel, pixel]);
}
}
-fn dither_atkinson_1_level(image: &mut RgbImage) {
+fn dither_sierra(image: &mut RgbImage) {
let (width, height) = image.dimensions();
let mut error: Vec<f32> = vec![0.; (3 * width) as usize];
- for y in 0..height {
- for x in 0..width {
- let Rgb([r, g, b]) = image.get_pixel(x, y);
- let pixel = (*r as u32 + *g as u32 + *b as u32) as f32 / 3.;
- let err_pos: u32 = (y * width + x) % (3 * width);
+ for (x, y, mut pixel_) in &mut image.enumerate_pixels_mut() {
+ let Rgb([r, g, b]) = pixel_;
+ let pixel = (*r as u32 + *g as u32 + *b as u32) as f32 / 3.;
+ let err_pos: u32 = (y * width + x) % (3 * width);
- let (pixel, err) = if (pixel + error[err_pos as usize]) > 127. {
- (255, (pixel - 255.) / 8.)
- } else {
- (0, pixel / 8.)
- };
+ let (pixel, err) = if (pixel + error[err_pos as usize]) > 127. {
+ (255, (pixel + error[err_pos as usize] - 255.) / 32.)
+ } else {
+ (0, (pixel + error[err_pos as usize]) / 32.)
+ };
- error[err_pos as usize] = 0.;
- error[((err_pos + 1) % (3 * width)) as usize] += err;
- error[((err_pos + 2) % (3 * width)) as usize] += err;
- error[((err_pos + width + 1) % (3 * width)) as usize] += err;
- error[((err_pos + width - 1) % (3 * width)) as usize] += err;
- error[((err_pos + width) % (3 * width)) as usize] += err;
- error[((err_pos + 2 * width) % (3 * width)) as usize] += err;
+ error[err_pos as usize] = 0.;
+ error[((err_pos + 1) % (3 * width)) as usize] += 5. * err;
+ error[((err_pos + 2) % (3 * width)) as usize] += 3. * err;
+ error[((err_pos + width + 2) % (3 * width)) as usize] += 2. * err;
+ error[((err_pos + width + 1) % (3 * width)) as usize] += 4. * err;
+ error[((err_pos + width) % (3 * width)) as usize] += 5. * err;
+ error[((err_pos + width - 1) % (3 * width)) as usize] += 4. * err;
+ error[((err_pos + width - 2) % (3 * width)) as usize] += 2. * err;
+ error[((err_pos + 2 * width + 1) % (3 * width)) as usize] += 2. * err;
+ error[((err_pos + 2 * width) % (3 * width)) as usize] += 3. * err;
+ error[((err_pos + 2 * width - 1) % (3 * width)) as usize] += 2. * err;
- image.put_pixel(x, y, image::Rgb([pixel, pixel, pixel]));
- }
+ *pixel_ = image::Rgb([pixel, pixel, pixel]);
}
}
fn main() -> Result<(), image::ImageError> {
let args: Vec<String> = env::args().collect();
- let image_path = format!("{}.jpg", &args[1]);
- let image_out = format!("{}_dithered.jpg", &args[1]);
- let image_out_1l = format!("{}_dithered_1l.jpg", &args[1]);
+ let image_path = format!("{}.png", &args[1]);
+ let image_out = format!("{}_dithered_atkinso.jpg", &args[1]);
+ let image_out_sierra = format!("{}_dithered_sierra.jpg", &args[1]);
let mut img = ImageReader::open(&image_path)?.decode()?.into_rgb8();
dither_atkinson(&mut img);
img.save(image_out)?;
let mut img = ImageReader::open(&image_path)?.decode()?.into_rgb8();
- dither_atkinson_1_level(&mut img);
- img.save(image_out_1l)?;
+ dither_sierra(&mut img);
+ img.save(image_out_sierra)?;
Ok(())
}