pub(crate) fn auto_choose_color(
    image: &[u8],
    w: usize,
    h: usize,
    mode_in: &ColorMode,
) -> Result<ColorMode, Error> {
    let mut mode_out = ColorMode::new();
    let mut prof = get_color_profile(image, w as u32, h as u32, mode_in)?;

    mode_out.clear_key();
    if prof.key.is_some() && w * h <= 16 {
        prof.alpha = true;
        prof.key = None;
        /*PNG has no alphachannel modes with less than 8-bit per channel*/
        if prof.bits < 8 {
            prof.bits = 8;
        };
    }
    let n = prof.numcolors;
    let palettebits = if n <= 2 {
        1
    } else if n <= 4 {
        2
    } else if n <= 16 {
        4
    } else {
        8
    };
    let palette_ok = (n <= 256 && prof.bits <= 8)
        && (w * h >= (n * 2) as usize)
        && (prof.colored != 0 || prof.bits > palettebits);
    if palette_ok {
        let pal = &prof.palette[0..prof.numcolors as usize];
        /*remove potential earlier palette*/
        mode_out.palette_clear();
        for p in pal {
            mode_out.palette_add(*p)?;
        }
        mode_out.colortype = ColorType::Palette;
        mode_out.set_bitdepth(palettebits);
        if mode_in.colortype == ColorType::Palette
            && mode_in.palette().len() >= mode_out.palette().len()
            && mode_in.bitdepth() == mode_out.bitdepth()
        {
            /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/
            mode_out = mode_in.clone();
        };
    } else {
        mode_out.set_bitdepth(prof.bits);
        mode_out.colortype = if prof.alpha {
            if prof.colored != 0 {
                ColorType::Rgba
            } else {
                ColorType::GreyAlpha
            }
        } else if prof.colored != 0 {
            ColorType::Rgb
        } else {
            ColorType::Grey
        };
        if let Some((key_r, key_g, key_b)) = prof.key {
            let mask = ((1 << mode_out.bitdepth()) - 1) as u16;
            /*profile always uses 16-bit, mask converts it*/
            mode_out.set_key(key_r & mask, key_g & mask, key_b & mask);
        };
    }
    Ok(mode_out)
}
/*Automatically chooses color type that gives smallest amount of bits in the
output image, e.g. grey if there are only greyscale pixels, palette if there
are less than 256 colors, ...
Updates values of mode with a potentially smaller color model. mode_out should
contain the user chosen color model, but will be overwritten with the new chosen one.*/
Automatically chooses color type that gives smallest amount of bits in the
output image, e.g. grey if there are only greyscale pixels, palette if there
are less than 256 colors,
Updates values of mode with a potentially smaller color model. mode_out should
contain the user chosen color model, but will be overwritten with the new chosen one.
Automatically chooses color type that gives smallest amount of bits in the output image e . g . grey if there are only greyscale pixels palette if there are less than 256 colors Updates values of mode with a potentially smaller color model . mode_out should contain the user chosen color model but will be overwritten with the new chosen one .
pub(crate) fn auto_choose_color(
    image: &[u8],
    w: usize,
    h: usize,
    mode_in: &ColorMode,
) -> Result<ColorMode, Error> {
    let mut mode_out = ColorMode::new();
    let mut prof = get_color_profile(image, w as u32, h as u32, mode_in)?;
    mode_out.clear_key();
    if prof.key.is_some() && w * h <= 16 {
        prof.alpha = true;
        prof.key = None;
        /*PNG has no alphachannel modes with less than 8-bit per channel*/
        if prof.bits < 8 {
            prof.bits = 8;
        };
    }
    let n = prof.numcolors;
    let palettebits = if n <= 2 {
        1
    } else if n <= 4 {
        2
    } else if n <= 16 {
        4
    } else {
        8
    };
    let palette_ok = (n <= 256 && prof.bits <= 8)
        && (w * h >= (n * 2) as usize)
        && (prof.colored != 0 || prof.bits > palettebits);
    if palette_ok {
        let pal = &prof.palette[0..prof.numcolors as usize];
        /*remove potential earlier palette*/
        mode_out.palette_clear();
        for p in pal {
            mode_out.palette_add(*p)?;
        }
        mode_out.colortype = ColorType::Palette;
        mode_out.set_bitdepth(palettebits);
        if mode_in.colortype == ColorType::Palette
            && mode_in.palette().len() >= mode_out.palette().len()
            && mode_in.bitdepth() == mode_out.bitdepth()
        {
            /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/
            mode_out = mode_in.clone();
        };
    } else {
        mode_out.set_bitdepth(prof.bits);
        mode_out.colortype = if prof.alpha {
            if prof.colored != 0 {
                ColorType::Rgba
            } else {
                ColorType::GreyAlpha
            }
        } else if prof.colored != 0 {
            ColorType::Rgb
        } else {
            ColorType::Grey
        };
        if let Some((key_r, key_g, key_b)) = prof.key {
            let mask = ((1 << mode_out.bitdepth()) - 1) as u16;
            /*profile always uses 16-bit, mask converts it*/
            mode_out.set_key(key_r & mask, key_g & mask, key_b & mask);
        };
    }
    Ok(mode_out)
}
pub ( crate ) fn auto_choose_color ( image : & [ u8 ] , w : usize , h : usize , mode_in : & ColorMode , ) -> Result < ColorMode , Error > { let mut mode_out = ColorMode :: new ( ) ; let mut prof = get_color_profile ( image , w as u32 , h as u32 , mode_in ) ? ; mode_out . clear_key ( ) ; if prof . key . is_some ( ) && w * h <= 16 { prof . alpha = true ; prof . key = None ; /*PNG has no alphachannel modes with less than 8-bit per channel*/ if prof . bits < 8 { prof . bits = 8 ; } ; } let n = prof . numcolors ; let palettebits = if n <= 2 { 1 } else if n <= 4 { 2 } else if n <= 16 { 4 } else { 8 } ; let palette_ok = ( n <= 256 && prof . bits <= 8 ) && ( w * h >= ( n * 2 ) as usize ) && ( prof . colored != 0 || prof . bits > palettebits ) ; if palette_ok { let pal = & prof . palette [ 0 .. prof . numcolors as usize ] ; /*remove potential earlier palette*/ mode_out . palette_clear ( ) ; for p in pal { mode_out . palette_add ( * p ) ? ; } mode_out . colortype = ColorType :: Palette ; mode_out . set_bitdepth ( palettebits ) ; if mode_in . colortype == ColorType :: Palette && mode_in . palette ( ) . len ( ) >= mode_out . palette ( ) . len ( ) && mode_in . bitdepth ( ) == mode_out . bitdepth ( ) { /*If input should have same palette colors, keep original to preserve its order and prevent conversion*/ mode_out = mode_in . clone ( ) ; } ; } else { mode_out . set_bitdepth ( prof . bits ) ; mode_out . colortype = if prof . alpha { if prof . colored != 0 { ColorType :: Rgba } else { ColorType :: GreyAlpha } } else if prof . colored != 0 { ColorType :: Rgb } else { ColorType :: Grey } ; if let Some ( ( key_r , key_g , key_b ) ) = prof . key { let mask = ( ( 1 << mode_out . bitdepth ( ) ) - 1 ) as u16 ; /*profile always uses 16-bit, mask converts it*/ mode_out . set_key ( key_r & mask , key_g & mask , key_b & mask ) ; } ; } Ok ( mode_out ) }
Automatically chooses color type that gives smallest amount of bits in the
output image, e.g.
Automatically chooses color type that gives smallest amount of bits in the output image e . g .
image
&[u8]
w
usize
h
usize
mode_in
&ColorMode