-
Notifications
You must be signed in to change notification settings - Fork 15
Change default upscaling to bilinear interpolation #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
60498ae
Add fancy upscaling as the default for lossy webp images
tristanphease d977022
Cleanup cargo fmt and cargo clippy
tristanphease 2b37274
Merge branch 'main' into yuv-fancy-upscaling
tristanphease 1fb49c8
Update yuv code with the buffer width change
tristanphease 8bb2d32
Fix bug with final row when upscaling
tristanphease 9878139
Update test reference images to not use -nofancy from libwebp
tristanphease 4e9f48b
Cleanup cargo clippy and fmt
tristanphease c9cf0a4
Add decoder options for choosing the upsampling option
tristanphease 2a4de23
Change to use pub fields instead of setter methods for decoder options
tristanphease 244255b
Move upsamplingmethod enum into decoder and export directly in lib.rs
tristanphease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ use std::num::NonZeroU16; | |
use std::ops::Range; | ||
|
||
use crate::extended::{self, get_alpha_predictor, read_alpha_chunk, WebPExtendedInfo}; | ||
use crate::vp8::UpsamplingMethod; | ||
|
||
use super::lossless::LosslessDecoder; | ||
use super::vp8::Vp8Decoder; | ||
|
@@ -271,6 +272,29 @@ pub enum LoopCount { | |
Times(NonZeroU16), | ||
} | ||
|
||
/// WebP decoder configuration options | ||
#[derive(Clone)] | ||
pub struct WebPDecodeOptions { | ||
lossy_upsampling: UpsamplingMethod, | ||
} | ||
|
||
impl WebPDecodeOptions { | ||
/// Sets the upsampling method used in conversion from lossy yuv to rgb | ||
/// | ||
/// Defaults to `Bilinear`. | ||
pub fn set_lossy_upsampling(&mut self, lossy_upsampling: UpsamplingMethod) { | ||
|
||
self.lossy_upsampling = lossy_upsampling; | ||
} | ||
} | ||
|
||
impl Default for WebPDecodeOptions { | ||
fn default() -> Self { | ||
Self { | ||
lossy_upsampling: UpsamplingMethod::Bilinear, | ||
} | ||
} | ||
} | ||
|
||
/// WebP image format decoder. | ||
pub struct WebPDecoder<R> { | ||
r: R, | ||
|
@@ -289,12 +313,23 @@ pub struct WebPDecoder<R> { | |
loop_duration: u64, | ||
|
||
chunks: HashMap<WebPRiffChunk, Range<u64>>, | ||
|
||
webp_decode_options: WebPDecodeOptions, | ||
} | ||
|
||
impl<R: BufRead + Seek> WebPDecoder<R> { | ||
/// Create a new `WebPDecoder` from the reader `r`. The decoder performs many small reads, so the | ||
/// reader should be buffered. | ||
pub fn new(r: R) -> Result<Self, DecodingError> { | ||
Self::new_with_options(r, WebPDecodeOptions::default()) | ||
} | ||
|
||
/// Create a new `WebPDecoder` from the reader `r` with the options `WebPDecodeOptions`. The decoder | ||
/// performs many small reads, so the reader should be buffered. | ||
pub fn new_with_options( | ||
r: R, | ||
webp_decode_options: WebPDecodeOptions, | ||
) -> Result<Self, DecodingError> { | ||
let mut decoder = Self { | ||
r, | ||
width: 0, | ||
|
@@ -308,6 +343,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
has_alpha: false, | ||
loop_count: LoopCount::Times(NonZeroU16::new(1).unwrap()), | ||
loop_duration: 0, | ||
webp_decode_options, | ||
}; | ||
decoder.read_data()?; | ||
Ok(decoder) | ||
|
@@ -653,7 +689,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
} | ||
|
||
if self.has_alpha() { | ||
frame.fill_rgba(buf); | ||
frame.fill_rgba(buf, self.webp_decode_options.lossy_upsampling); | ||
|
||
let range = self | ||
.chunks | ||
|
@@ -684,7 +720,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
} | ||
} | ||
} else { | ||
frame.fill_rgb(buf); | ||
frame.fill_rgb(buf, self.webp_decode_options.lossy_upsampling); | ||
} | ||
} | ||
|
||
|
@@ -758,7 +794,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
return Err(DecodingError::InconsistentImageSizes); | ||
} | ||
let mut rgb_frame = vec![0; frame_width as usize * frame_height as usize * 3]; | ||
raw_frame.fill_rgb(&mut rgb_frame); | ||
raw_frame.fill_rgb(&mut rgb_frame, self.webp_decode_options.lossy_upsampling); | ||
(rgb_frame, false) | ||
} | ||
WebPRiffChunk::VP8L => { | ||
|
@@ -789,7 +825,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
let frame = Vp8Decoder::decode_frame((&mut self.r).take(next_chunk_size))?; | ||
|
||
let mut rgba_frame = vec![0; frame_width as usize * frame_height as usize * 4]; | ||
frame.fill_rgba(&mut rgba_frame); | ||
frame.fill_rgba(&mut rgba_frame, self.webp_decode_options.lossy_upsampling); | ||
|
||
for y in 0..frame.height { | ||
for x in 0..frame.width { | ||
|
@@ -880,6 +916,11 @@ impl<R: BufRead + Seek> WebPDecoder<R> { | |
self.animation.next_frame_start = self.chunks.get(&WebPRiffChunk::ANMF).unwrap().start - 8; | ||
self.animation.dispose_next_frame = true; | ||
} | ||
|
||
/// Sets the upsampling method that is used in lossy decoding | ||
pub fn set_lossy_upsampling(&mut self, upsampling_method: UpsamplingMethod) { | ||
self.webp_decode_options.lossy_upsampling = upsampling_method; | ||
} | ||
} | ||
|
||
pub(crate) fn range_reader<R: BufRead + Seek>( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.