我有以下没有结构的模块:
img_util.rs:
pub mod img_util {
use image::{ImageBuffer, Pixel};
use std::{fs::File, io::BufWriter};
fn get_color_type<P>() -> Result<png::ColorType, std::io::Error>
where
P: Pixel<Subpixel = u8>,
{
match <P as Pixel>::CHANNEL_COUNT {
4 => Ok(png::ColorType::Rgba),
3 => Ok(png::ColorType::Rgb),
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Incorrect color channel count / format.",
)),
}
}
fn get_dpi_header_data(dpi: u32) -> Vec<u8> {
let dpm = 39.370079 * dpi as f32;
let rounded_dpm = dpm.round() as u32;
let mut data: Vec<u8> = Vec::new();
data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in X-direction.
data.extend_from_slice(&rounded_dpm.to_be_bytes()); // Pixels per unit in Y-direction.
data.push(1); // Indicate that meters are used as unit.
data
}
pub fn save_png_with_dpi<P>(
input_img: &ImageBuffer<P, Vec<u8>>,
output_img: File,
dpi: u32,
) -> Result<(), std::io::Error>
where
P: Pixel<Subpixel = u8>,
{
let w = &mut BufWriter::new(output_img);
let (width, height) = input_img.dimensions();
let mut encoder = png::Encoder::new(w, width, height);
encoder.set_color(get_color_type::<P>()?);
encoder.set_depth(png::BitDepth::Eight);
encoder.set_compression(png::Compression::Best);
let data = get_dpi_header_data(dpi);
let mut writer = encoder.write_header()?;
writer.write_chunk(png::chunk::pHYs, data.as_slice())?;
writer.write_image_data(input_img)?;
Ok(())
}
}
lib.rs:
pub mod img_util;
main.rs:
use visual_center::img_util::img_util;
img_util::save_png_with_dpi(
// arguments
).unwrap();
我可以使用该模块,但我对必须声明pub-mod-img_util两次(一次在
img_util.rs
另一个在
lib.rs
)。或者这可能是声明和使用没有结构的模块的唯一方法?