pub(super) fn decode_generic(
    state: &mut State,
    inp: &[u8],
) -> Result<(Vec<u8>, u32, u32), Error> {
    let mut found_iend = false; /*the data from idat chunks*/
    /*for unknown chunk order*/
    let mut unknown = false;
    let mut critical_pos = ChunkPosition::IHDR;
    /*provide some proper output values if error will happen*/
    let (info, w, h) = lodepng_inspect(&state.decoder, inp)?;
    state.info_png = info;

    /*reads header and resets other parameters in state->info_png*/
    let numpixels = match w.checked_mul(h) {
        Some(n) => n,
        None => {
            return Err(Error(92));
        }
    };
    /*multiplication overflow possible further below. Allows up to 2^31-1 pixel
    bytes with 16-bit RGBA, the rest is room for filter bytes.*/
    if numpixels > 268435455 {
        return Err(Error(92)); /*first byte of the first chunk after the header*/
    }
    let mut idat = Vec::new();
    let mut chunk = &inp[33..];
    /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
    IDAT data is put at the start of the in buffer*/
    while !found_iend {
        if chunk.len() < 12 {
            return Err(Error(30));
        }
        /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/
        let data = lodepng_chunk_data(chunk)?;
        match lodepng_chunk_type(chunk) {
            b"IDAT" => {
                idat.extend_from_slice(data);
                critical_pos = ChunkPosition::IDAT;
            }
            b"IEND" => {
                found_iend = true;
            }
            b"PLTE" => {
                read_chunk_plte(&mut state.info_png.color, data)?;
                critical_pos = ChunkPosition::PLTE;
            }
            b"tRNS" => {
                read_chunk_trns(&mut state.info_png.color, data)?;
            }
            b"bKGD" => {
                read_chunk_bkgd(&mut state.info_png, data)?;
            }
            b"tEXt" => {
                // Uncompressed TEXT chunk.
                read_chunk_text(&mut state.info_png, data)?;
            }
            b"zTXt" => {
                // Compressed TEXT chunk.
                read_chunk_ztxt(
                    &mut state.info_png,
                    &state.decoder.zlibsettings,
                    data,
                )?;
            }
            b"iTXt" => {
                // International TEXT chunk.
                read_chunk_itxt(
                    &mut state.info_png,
                    &state.decoder.zlibsettings,
                    data,
                )?;
            }
            b"tIME" => {
                read_chunk_time(&mut state.info_png, data)?;
            }
            b"pHYs" => {
                read_chunk_phys(&mut state.info_png, data)?;
            }
            _ => {
                // An unknown chunk.
                if !lodepng_chunk_ancillary(chunk) {
                    return Err(Error(69));
                }
                unknown = true;
                state.info_png.push_unknown_chunk(critical_pos, chunk)?;
            }
        };
        if state.decoder.check_crc
            && !unknown
            && !lodepng_chunk_check_crc(chunk)
        {
            return Err(Error(57));
        }
        if !found_iend {
            chunk = lodepng_chunk_next(chunk);
        }
    }
    /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
    If the decompressed size does not match the prediction, the image must be corrupt.*/
    let predict = if state.info_png.interlace_method == 0 {
        /*The extra *h is added because this are the filter bytes every scanline starts with*/
        state.info_png.color.raw_size_idat(w, h) + h
    } else {
        /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/
        let color = &state.info_png.color;
        let mut predict =
            color.raw_size_idat((w + 7) >> 3, (h + 7) >> 3) + ((h + 7) >> 3);
        if w > 4 {
            predict += color.raw_size_idat((w + 3) >> 3, (h + 7) >> 3)
                + ((h + 7) >> 3);
        }
        predict +=
            color.raw_size_idat((w + 3) >> 2, (h + 3) >> 3) + ((h + 3) >> 3);
        if w > 2 {
            predict += color.raw_size_idat((w + 1) >> 2, (h + 3) >> 2)
                + ((h + 3) >> 2);
        }
        predict +=
            color.raw_size_idat((w + 1) >> 1, (h + 1) >> 2) + ((h + 1) >> 2);
        if w > 1 {
            predict += color.raw_size_idat((w + 0) >> 1, (h + 1) >> 1)
                + ((h + 1) >> 1);
        }
        predict += color.raw_size_idat(w + 0, (h + 0) >> 1) + ((h + 0) >> 1);
        predict
    };
    let mut scanlines = zlib_decompress(&idat, &state.decoder.zlibsettings)?;
    if scanlines.len() != predict as usize {
        /*decompressed size doesn't match prediction*/
        return Err(Error(91));
    }
    let mut out = Vec::new();
    out.resize(state.info_png.color.raw_size(w, h), 0);
    postprocess_scanlines(&mut out, &mut scanlines, w, h, &state.info_png)?;
    Ok((out, w, h))
}
/*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/
read a PNG, the result will be in the same color type as the PNG (hence "generic")
read a PNG the result will be in the same color type as the PNG ( hence " generic " )
pub(super) fn decode_generic(
    state: &mut State,
    inp: &[u8],
) -> Result<(Vec<u8>, u32, u32), Error> {
    let mut found_iend = false; /*the data from idat chunks*/
    /*for unknown chunk order*/
    let mut unknown = false;
    let mut critical_pos = ChunkPosition::IHDR;
    /*provide some proper output values if error will happen*/
    let (info, w, h) = lodepng_inspect(&state.decoder, inp)?;
    state.info_png = info;
    /*reads header and resets other parameters in state->info_png*/
    let numpixels = match w.checked_mul(h) {
        Some(n) => n,
        None => {
            return Err(Error(92));
        }
    };
    /*multiplication overflow possible further below. Allows up to 2^31-1 pixel
    bytes with 16-bit RGBA, the rest is room for filter bytes.*/
    if numpixels > 268435455 {
        return Err(Error(92)); /*first byte of the first chunk after the header*/
    }
    let mut idat = Vec::new();
    let mut chunk = &inp[33..];
    /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
    IDAT data is put at the start of the in buffer*/
    while !found_iend {
        if chunk.len() < 12 {
            return Err(Error(30));
        }
        /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/
        let data = lodepng_chunk_data(chunk)?;
        match lodepng_chunk_type(chunk) {
            b"IDAT" => {
                idat.extend_from_slice(data);
                critical_pos = ChunkPosition::IDAT;
            }
            b"IEND" => {
                found_iend = true;
            }
            b"PLTE" => {
                read_chunk_plte(&mut state.info_png.color, data)?;
                critical_pos = ChunkPosition::PLTE;
            }
            b"tRNS" => {
                read_chunk_trns(&mut state.info_png.color, data)?;
            }
            b"bKGD" => {
                read_chunk_bkgd(&mut state.info_png, data)?;
            }
            b"tEXt" => {
                // Uncompressed TEXT chunk.
                read_chunk_text(&mut state.info_png, data)?;
            }
            b"zTXt" => {
                // Compressed TEXT chunk.
                read_chunk_ztxt(
                    &mut state.info_png,
                    &state.decoder.zlibsettings,
                    data,
                )?;
            }
            b"iTXt" => {
                // International TEXT chunk.
                read_chunk_itxt(
                    &mut state.info_png,
                    &state.decoder.zlibsettings,
                    data,
                )?;
            }
            b"tIME" => {
                read_chunk_time(&mut state.info_png, data)?;
            }
            b"pHYs" => {
                read_chunk_phys(&mut state.info_png, data)?;
            }
            _ => {
                // An unknown chunk.
                if !lodepng_chunk_ancillary(chunk) {
                    return Err(Error(69));
                }
                unknown = true;
                state.info_png.push_unknown_chunk(critical_pos, chunk)?;
            }
        };
        if state.decoder.check_crc
            && !unknown
            && !lodepng_chunk_check_crc(chunk)
        {
            return Err(Error(57));
        }
        if !found_iend {
            chunk = lodepng_chunk_next(chunk);
        }
    }
    /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
    If the decompressed size does not match the prediction, the image must be corrupt.*/
    let predict = if state.info_png.interlace_method == 0 {
        /*The extra *h is added because this are the filter bytes every scanline starts with*/
        state.info_png.color.raw_size_idat(w, h) + h
    } else {
        /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/
        let color = &state.info_png.color;
        let mut predict =
            color.raw_size_idat((w + 7) >> 3, (h + 7) >> 3) + ((h + 7) >> 3);
        if w > 4 {
            predict += color.raw_size_idat((w + 3) >> 3, (h + 7) >> 3)
                + ((h + 7) >> 3);
        }
        predict +=
            color.raw_size_idat((w + 3) >> 2, (h + 3) >> 3) + ((h + 3) >> 3);
        if w > 2 {
            predict += color.raw_size_idat((w + 1) >> 2, (h + 3) >> 2)
                + ((h + 3) >> 2);
        }
        predict +=
            color.raw_size_idat((w + 1) >> 1, (h + 1) >> 2) + ((h + 1) >> 2);
        if w > 1 {
            predict += color.raw_size_idat((w + 0) >> 1, (h + 1) >> 1)
                + ((h + 1) >> 1);
        }
        predict += color.raw_size_idat(w + 0, (h + 0) >> 1) + ((h + 0) >> 1);
        predict
    };
    let mut scanlines = zlib_decompress(&idat, &state.decoder.zlibsettings)?;
    if scanlines.len() != predict as usize {
        /*decompressed size doesn't match prediction*/
        return Err(Error(91));
    }
    let mut out = Vec::new();
    out.resize(state.info_png.color.raw_size(w, h), 0);
    postprocess_scanlines(&mut out, &mut scanlines, w, h, &state.info_png)?;
    Ok((out, w, h))
}
pub ( super ) fn decode_generic ( state : & mut State , inp : & [ u8 ] , ) -> Result < ( Vec < u8 > , u32 , u32 ) , Error > { let mut found_iend = false ; /*the data from idat chunks*/ /*for unknown chunk order*/ let mut unknown = false ; let mut critical_pos = ChunkPosition :: IHDR ; /*provide some proper output values if error will happen*/ let ( info , w , h ) = lodepng_inspect ( & state . decoder , inp ) ? ; state . info_png = info ; /*reads header and resets other parameters in state->info_png*/ let numpixels = match w . checked_mul ( h ) { Some ( n ) => n , None => { return Err ( Error ( 92 ) ) ; } } ; /*multiplication overflow possible further below. Allows up to 2^31-1 pixel
    bytes with 16-bit RGBA, the rest is room for filter bytes.*/ if numpixels > 268435455 { return Err ( Error ( 92 ) ) ; /*first byte of the first chunk after the header*/ } let mut idat = Vec :: new ( ) ; let mut chunk = & inp [ 33 .. ] ; /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
    IDAT data is put at the start of the in buffer*/ while ! found_iend { if chunk . len ( ) < 12 { return Err ( Error ( 30 ) ) ; } /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/ let data = lodepng_chunk_data ( chunk ) ? ; match lodepng_chunk_type ( chunk ) { b" " => { idat . extend_from_slice ( data ) ; critical_pos = ChunkPosition :: IDAT ; } b" " => { found_iend = true ; } b" " => { read_chunk_plte ( & mut state . info_png . color , data ) ? ; critical_pos = ChunkPosition :: PLTE ; } b" " => { read_chunk_trns ( & mut state . info_png . color , data ) ? ; } b" " => { read_chunk_bkgd ( & mut state . info_png , data ) ? ; } b" " => { // Uncompressed TEXT chunk. read_chunk_text ( & mut state . info_png , data ) ? ; } b" " => { // Compressed TEXT chunk. read_chunk_ztxt ( & mut state . info_png , & state . decoder . zlibsettings , data , ) ? ; } b" " => { // International TEXT chunk. read_chunk_itxt ( & mut state . info_png , & state . decoder . zlibsettings , data , ) ? ; } b" " => { read_chunk_time ( & mut state . info_png , data ) ? ; } b" " => { read_chunk_phys ( & mut state . info_png , data ) ? ; } _ => { // An unknown chunk. if ! lodepng_chunk_ancillary ( chunk ) { return Err ( Error ( 69 ) ) ; } unknown = true ; state . info_png . push_unknown_chunk ( critical_pos , chunk ) ? ; } } ; if state . decoder . check_crc && ! unknown && ! lodepng_chunk_check_crc ( chunk ) { return Err ( Error ( 57 ) ) ; } if ! found_iend { chunk = lodepng_chunk_next ( chunk ) ; } } /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
    If the decompressed size does not match the prediction, the image must be corrupt.*/ let predict = if state . info_png . interlace_method == 0 { /*The extra *h is added because this are the filter bytes every scanline starts with*/ state . info_png . color . raw_size_idat ( w , h ) + h } else { /*Adam-7 interlaced: predicted size is the sum of the 7 sub-images sizes*/ let color = & state . info_png . color ; let mut predict = color . raw_size_idat ( ( w + 7 ) >> 3 , ( h + 7 ) >> 3 ) + ( ( h + 7 ) >> 3 ) ; if w > 4 { predict += color . raw_size_idat ( ( w + 3 ) >> 3 , ( h + 7 ) >> 3 ) + ( ( h + 7 ) >> 3 ) ; } predict += color . raw_size_idat ( ( w + 3 ) >> 2 , ( h + 3 ) >> 3 ) + ( ( h + 3 ) >> 3 ) ; if w > 2 { predict += color . raw_size_idat ( ( w + 1 ) >> 2 , ( h + 3 ) >> 2 ) + ( ( h + 3 ) >> 2 ) ; } predict += color . raw_size_idat ( ( w + 1 ) >> 1 , ( h + 1 ) >> 2 ) + ( ( h + 1 ) >> 2 ) ; if w > 1 { predict += color . raw_size_idat ( ( w + 0 ) >> 1 , ( h + 1 ) >> 1 ) + ( ( h + 1 ) >> 1 ) ; } predict += color . raw_size_idat ( w + 0 , ( h + 0 ) >> 1 ) + ( ( h + 0 ) >> 1 ) ; predict } ; let mut scanlines = zlib_decompress ( & idat , & state . decoder . zlibsettings ) ? ; if scanlines . len ( ) != predict as usize { /*decompressed size doesn't match prediction*/ return Err ( Error ( 91 ) ) ; } let mut out = Vec :: new ( ) ; out . resize ( state . info_png . color . raw_size ( w , h ) , 0 ) ; postprocess_scanlines ( & mut out , & mut scanlines , w , h , & state . info_png ) ? ; Ok ( ( out , w , h ) ) }
read a PNG, the result will be in the same color type as the PNG (hence "generic")
read a PNG the result will be in the same color type as the PNG ( hence " generic " )
state
&mut State
inp
&[u8]