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
use super::text::*; use image; pub static LETTERS_FOLDER : &str = "./src/assets/"; pub static LETTERS_EXT : &str = ".png"; struct ImgCropOverlayInfos { pub x: u32, pub y: u32, pub width: u32, pub height: u32, } fn cols_gray_avg(img : &image::ImageBuffer<image::Luma<u8>, std::vec::Vec<u8>>) -> Vec<f32> { let mut avgs : Vec<f32> = Vec::new(); for x in 0..img.width() { let mut avg : f32 = 0.0; for y in 0..img.height() { let px = img.get_pixel(x, y).0[0]; avg += px as f32; } avg /= img.height() as f32; avgs.push(avg); } avgs } fn rows_gray_avg(img : &image::ImageBuffer<image::Luma<u8>, std::vec::Vec<u8>>) -> Vec<f32> { let mut avgs : Vec<f32> = Vec::new(); for y in 0..img.height() { let mut avg : f32 = 0.0; for x in 0..img.width() { let px = img.get_pixel(x, y).0[0]; avg += px as f32; } avg /= img.width() as f32; avgs.push(avg); } avgs } pub fn update_images(dic_name : Option<&str>) -> Result<(), String> { let dic_path = paths::dic_path(&dic_name); let abc_path = dic_path.join("00 abc.png"); let img = match image::open(abc_path) { Ok(i) => i, Err(e) => panic!(e), }; let mut img_colorful = img.clone().to_rgba(); let mut img_gray = img.to_luma(); let c_avg = cols_gray_avg(&img_gray); let mut crops = Vec::new(); let mut x : u32 = 0; let mut width : u32; let mut max_height = 0; for (i, col_avg) in c_avg.iter().enumerate() { if *col_avg < 254.0 { if x == 0 { x = i as u32; } } else if x != 0 { width = i as u32 - x; if width > 5 { let img_h = img_gray.height(); let letter = image::imageops::crop(&mut img_gray, x, 0, width, img_h).to_image(); let mut r_avg = rows_gray_avg(&letter); let mut y : u32 = 0; let mut height : u32 = 0; for (i2, row_avg) in r_avg.iter().enumerate() { if *row_avg < 254.0 && y == 0 { y = i2 as u32; break; } } r_avg.reverse(); for (i2, row_avg) in r_avg.iter().enumerate() { if *row_avg < 254.0 { height = r_avg.len() as u32 - i2 as u32 - y; break; } } let crop = ImgCropOverlayInfos { x, y, width, height }; if height > max_height { max_height = height; } crops.push(crop); } x = 0; } } let over_line_height = max_height as f32; let under_line_height = over_line_height / 5.0; let line_height = over_line_height + under_line_height; let qnt = crops.len(); println!("{}", qnt); if qnt < 58 { return Err(format!("Expected 58 characters, found {}, please sanitize the input file.", qnt)); } for (i, c) in CHARS.iter().enumerate() { let crop = &crops[i]; let letter = image::imageops::crop(&mut img_colorful, crop.x, crop.y, crop.width, crop.height).to_image(); let l_width = letter.width() as f32; let width = (l_width + l_width * 0.2) as u32; let width = if width == letter.width() { width + 1 } else { width }; let mut bkg = image::RgbaImage::from_pixel(width, line_height as u32, image::Rgba([255, 255, 255, 1])); let x = 0; let y = if CHARS_UNDER.contains(c) { line_height as u32 - letter.height() } else { over_line_height as u32 - letter.height() }; image::imageops::overlay(&mut bkg, &letter, x, y); let mut l_path = dic_path.join(Letter::char_name(*c)); l_path.set_extension("png"); bkg.save(l_path).unwrap(); } Ok(()) }