pub(super) fn unfilter_scanline(
    recon: &mut [u8],
    scanline: &[u8],
    precon: Option<&[u8]>,
    bytewidth: usize,
    filter_type: u8,
    length: usize,
) -> Result<(), Error> {
    match filter_type {
        0 => recon.clone_from_slice(scanline),
        1 => {
            recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
            for i in bytewidth..length {
                recon[i] = scanline[i].wrapping_add(recon[i - bytewidth]);
            }
        }
        2 => {
            if let Some(precon) = precon {
                for i in 0..length {
                    recon[i] = scanline[i].wrapping_add(precon[i]);
                }
            } else {
                recon.clone_from_slice(scanline);
            }
        }
        3 => {
            if let Some(precon) = precon {
                for i in 0..bytewidth {
                    recon[i] = scanline[i].wrapping_add(precon[i] >> 1);
                }
                for i in bytewidth..length {
                    let t = recon[i - bytewidth] as u16 + precon[i] as u16;
                    recon[i] = scanline[i].wrapping_add((t >> 1) as u8);
                }
            } else {
                recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
                for i in bytewidth..length {
                    recon[i] =
                        scanline[i].wrapping_add(recon[i - bytewidth] >> 1);
                }
            }
        }
        4 => {
            if let Some(precon) = precon {
                for i in 0..bytewidth {
                    recon[i] = scanline[i].wrapping_add(precon[i]);
                }
                for i in bytewidth..length {
                    recon[i] = scanline[i].wrapping_add(paeth_predictor(
                        recon[i - bytewidth] as i16,
                        precon[i] as i16,
                        precon[i - bytewidth] as i16,
                    ));
                }
            } else {
                recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
                for i in bytewidth..length {
                    recon[i] = scanline[i].wrapping_add(recon[i - bytewidth]);
                }
            }
        }
        _ => return Err(Error(36)),
    }
    Ok(())
}
/*
For PNG filter method 0
unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
the filter works byte per byte (bytewidth = 1)
precon is the previous unfiltered scanline, recon the result, scanline the current one
the incoming scanlines do NOT include the filter_type byte, that one is given in the parameter filter_type instead
recon and scanline MAY be the same memory address! precon must be disjoint.
*/
For PNG filter method 0
unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
the filter works byte per byte (bytewidth = 1)
precon is the previous unfiltered scanline, recon the result, scanline the current one
the incoming scanlines do NOT include the filter_type byte, that one is given in the parameter filter_type instead
recon and scanline MAY be the same memory address. precon must be disjoint.
For PNG filter method 0 unfilter a PNG image scanline by scanline . when the pixels are smaller than 1 byte the filter works byte per byte ( bytewidth = 1 ) precon is the previous unfiltered scanline recon the result scanline the current one the incoming scanlines do NOT include the filter_type byte that one is given in the parameter filter_type instead recon and scanline MAY be the same memory address . precon must be disjoint .
pub(super) fn unfilter_scanline(
    recon: &mut [u8],
    scanline: &[u8],
    precon: Option<&[u8]>,
    bytewidth: usize,
    filter_type: u8,
    length: usize,
) -> Result<(), Error> {
    match filter_type {
        0 => recon.clone_from_slice(scanline),
        1 => {
            recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
            for i in bytewidth..length {
                recon[i] = scanline[i].wrapping_add(recon[i - bytewidth]);
            }
        }
        2 => {
            if let Some(precon) = precon {
                for i in 0..length {
                    recon[i] = scanline[i].wrapping_add(precon[i]);
                }
            } else {
                recon.clone_from_slice(scanline);
            }
        }
        3 => {
            if let Some(precon) = precon {
                for i in 0..bytewidth {
                    recon[i] = scanline[i].wrapping_add(precon[i] >> 1);
                }
                for i in bytewidth..length {
                    let t = recon[i - bytewidth] as u16 + precon[i] as u16;
                    recon[i] = scanline[i].wrapping_add((t >> 1) as u8);
                }
            } else {
                recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
                for i in bytewidth..length {
                    recon[i] =
                        scanline[i].wrapping_add(recon[i - bytewidth] >> 1);
                }
            }
        }
        4 => {
            if let Some(precon) = precon {
                for i in 0..bytewidth {
                    recon[i] = scanline[i].wrapping_add(precon[i]);
                }
                for i in bytewidth..length {
                    recon[i] = scanline[i].wrapping_add(paeth_predictor(
                        recon[i - bytewidth] as i16,
                        precon[i] as i16,
                        precon[i - bytewidth] as i16,
                    ));
                }
            } else {
                recon[0..bytewidth].clone_from_slice(&scanline[0..bytewidth]);
                for i in bytewidth..length {
                    recon[i] = scanline[i].wrapping_add(recon[i - bytewidth]);
                }
            }
        }
        _ => return Err(Error(36)),
    }
    Ok(())
}
pub ( super ) fn unfilter_scanline ( recon : & mut [ u8 ] , scanline : & [ u8 ] , precon : Option < & [ u8 ] > , bytewidth : usize , filter_type : u8 , length : usize , ) -> Result < ( ) , Error > { match filter_type { 0 => recon . clone_from_slice ( scanline ) , 1 => { recon [ 0 .. bytewidth ] . clone_from_slice ( & scanline [ 0 .. bytewidth ] ) ; for i in bytewidth .. length { recon [ i ] = scanline [ i ] . wrapping_add ( recon [ i - bytewidth ] ) ; } } 2 => { if let Some ( precon ) = precon { for i in 0 .. length { recon [ i ] = scanline [ i ] . wrapping_add ( precon [ i ] ) ; } } else { recon . clone_from_slice ( scanline ) ; } } 3 => { if let Some ( precon ) = precon { for i in 0 .. bytewidth { recon [ i ] = scanline [ i ] . wrapping_add ( precon [ i ] >> 1 ) ; } for i in bytewidth .. length { let t = recon [ i - bytewidth ] as u16 + precon [ i ] as u16 ; recon [ i ] = scanline [ i ] . wrapping_add ( ( t >> 1 ) as u8 ) ; } } else { recon [ 0 .. bytewidth ] . clone_from_slice ( & scanline [ 0 .. bytewidth ] ) ; for i in bytewidth .. length { recon [ i ] = scanline [ i ] . wrapping_add ( recon [ i - bytewidth ] >> 1 ) ; } } } 4 => { if let Some ( precon ) = precon { for i in 0 .. bytewidth { recon [ i ] = scanline [ i ] . wrapping_add ( precon [ i ] ) ; } for i in bytewidth .. length { recon [ i ] = scanline [ i ] . wrapping_add ( paeth_predictor ( recon [ i - bytewidth ] as i16 , precon [ i ] as i16 , precon [ i - bytewidth ] as i16 , ) ) ; } } else { recon [ 0 .. bytewidth ] . clone_from_slice ( & scanline [ 0 .. bytewidth ] ) ; for i in bytewidth .. length { recon [ i ] = scanline [ i ] . wrapping_add ( recon [ i - bytewidth ] ) ; } } } _ => return Err ( Error ( 36 ) ) , } Ok ( ( ) ) }
For PNG filter method 0
unfilter a PNG image scanline by scanline.
For PNG filter method 0 unfilter a PNG image scanline by scanline .
recon
&mut [u8]
scanline
&[u8]
precon
Option<&[u8]>
bytewidth
usize
filter_type
u8
length
usize