Moved color type to different function

This commit is contained in:
famfo 2022-01-25 12:37:15 +01:00
parent 814aeef994
commit 3cbb97e5b3
2 changed files with 18 additions and 7 deletions

View file

@ -145,6 +145,17 @@ fn unfuck_cif_number_fast(number: &str) -> Result<u8> {
Ok(number)
}
fn unfuck_color_type(color_type: &str) -> Result<ColorType> {
assert_trailing_leading(color_type)?;
let mut buf = ArrayString::<18>::new();
let split = color_type.split_ascii_whitespace().take(8).collect::<ArrayVec<_, 64>>();
join_to_arraystring(&split, &mut buf)?;
let color_type = *(nummap::COLOR_TYPE_NUMMAP.get(&buf).ok_or_else(|| InvalidSizeError(color_type.to_string()))?);
Ok(color_type)
}
fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
let infile = File::open(inputfile)?;
let outfile = File::create(outputfile)?;
@ -189,18 +200,13 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
.ok_or_else(|| InvalidSizeError(size_string.to_string()))?
.trim_start(),
)?;
let format = match unfuck_cif_number_fast(
let format = unfuck_color_type(
size_split[2]
.trim_start()
.strip_prefix("bitów_na_piksel:")
.ok_or_else(|| InvalidSizeError(size_string.to_string()))?
.trim_start(),
)? {
24 => ColorType::Rgb,
32 => ColorType::Rgba,
_ => return Err(InvalidSizeError(size_string.to_string())),
};
)?;
if width == 0 || height == 0 {
return Err(NoDataError)

View file

@ -1261,3 +1261,8 @@ pub static U8_NUMMAP: Map<&'static str, u8> = phf_map! {
"dwieście pięćdziesiąt cztery" => 254,
"dwieście pięćdziesiąt pięć" => 255,
};
pub static COLOR_TYPE_NUMMAP: Map<&'static str, png::ColorType> = phf_map! {
"dwadzieścia cztery" => png::ColorType::Rgb,
"trzydzieści dwa" => png::ColorType::Rgba,
};