Skip to content

Commit

Permalink
Improve documentation on filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Sep 26, 2024
1 parent d96defd commit e1f429e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,8 +1346,11 @@ impl<'a, W: Write> StreamWriter<'a, W> {
/// Set the used filter type for the next frame.
///
/// The default filter is [`FilterType::Sub`] which provides a basic prediction algorithm for
/// sample values based on the previous. For a potentially better compression ratio, at the
/// cost of more complex processing, try out [`FilterType::Paeth`].
/// sample values based on the previous.
///
/// For optimal compression ratio you should enable adaptive filtering
/// instead of setting a single filter for the entire image, see
/// [set_adaptive_filter](Self::set_adaptive_filter).
pub fn set_filter(&mut self, filter: FilterType) {
self.filter = filter;
}
Expand All @@ -1356,8 +1359,9 @@ impl<'a, W: Write> StreamWriter<'a, W> {
///
/// Adaptive filtering attempts to select the best filter for each line
/// based on heuristics which minimize the file size for compression rather
/// than use a single filter for the entire image. The default method is
/// [`AdaptiveFilterType::NonAdaptive`].
/// than use a single filter for the entire image.
///
/// The default method is [`AdaptiveFilterType::NonAdaptive`].
pub fn set_adaptive_filter(&mut self, adaptive_filter: AdaptiveFilterType) {
self.adaptive_filter = adaptive_filter;
}
Expand Down
12 changes: 8 additions & 4 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ mod simd {
/// Compression in general benefits from repetitive data. The filter is a content-aware method of
/// compressing the range of occurring byte values to help the compression algorithm. Note that
/// this does not operate on pixels but on raw bytes of a scanline.
///
/// Details on how each filter works can be found in the [PNG Book](http://www.libpng.org/pub/png/book/chapter09.html).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FilterType {
Expand Down Expand Up @@ -297,12 +299,14 @@ impl FilterType {
}
}

/// The filtering method for preprocessing scanline data before compression.
/// Adaptive filtering tries every possible filter for each row and keeps the best one.
/// This improves compression ratio, but makes encoding slower.
///
/// Adaptive filtering performs additional computation in an attempt to maximize
/// the compression of the data. [`NonAdaptive`] filtering is the default.
/// It is recommended to use `Adaptive` whenever you care about compression ratio.
/// Filtering is quite cheap compared to other parts of encoding, but can contribute
/// to the compression ratio significantly.
///
/// [`NonAdaptive`]: AdaptiveFilterType::NonAdaptive
/// `NonAdaptive` filtering is the default.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AdaptiveFilterType {
Expand Down

0 comments on commit e1f429e

Please sign in to comment.