refactor: recode_to_size(): Rename strict_limits to is_avatar

This commit is contained in:
iequidoo
2025-03-07 19:49:55 -03:00
committed by l
parent d2e1e57890
commit 58b7efe006

View File

@@ -266,14 +266,14 @@ impl<'a> BlobObject<'a> {
}; };
let maybe_sticker = &mut false; let maybe_sticker = &mut false;
let strict_limits = true; let is_avatar = true;
self.recode_to_size( self.recode_to_size(
context, context,
None, // The name of an avatar doesn't matter None, // The name of an avatar doesn't matter
maybe_sticker, maybe_sticker,
img_wh, img_wh,
max_bytes, max_bytes,
strict_limits, is_avatar,
)?; )?;
Ok(()) Ok(())
@@ -302,21 +302,17 @@ impl<'a> BlobObject<'a> {
), ),
MediaQuality::Worse => (constants::WORSE_IMAGE_SIZE, constants::WORSE_IMAGE_BYTES), MediaQuality::Worse => (constants::WORSE_IMAGE_SIZE, constants::WORSE_IMAGE_BYTES),
}; };
let strict_limits = false; let is_avatar = false;
let new_name = self.recode_to_size( let new_name =
context, self.recode_to_size(context, name, maybe_sticker, img_wh, max_bytes, is_avatar)?;
name,
maybe_sticker,
img_wh,
max_bytes,
strict_limits,
)?;
Ok(new_name) Ok(new_name)
} }
/// If `!strict_limits`, then if `max_bytes` is exceeded, reduce the image to `img_wh` and just /// Recodes the image so that it fits into limits on width/height and byte size.
/// proceed with the result. ///
/// If `!is_avatar`, then if `max_bytes` is exceeded, reduces the image to `img_wh` and proceeds
/// with the result without rechecking.
/// ///
/// This modifies the blob object in-place. /// This modifies the blob object in-place.
/// ///
@@ -331,10 +327,10 @@ impl<'a> BlobObject<'a> {
maybe_sticker: &mut bool, maybe_sticker: &mut bool,
mut img_wh: u32, mut img_wh: u32,
max_bytes: usize, max_bytes: usize,
strict_limits: bool, is_avatar: bool,
) -> Result<String> { ) -> Result<String> {
// Add white background only to avatars to spare the CPU. // Add white background only to avatars to spare the CPU.
let mut add_white_bg = img_wh <= constants::BALANCED_AVATAR_SIZE; let mut add_white_bg = is_avatar;
let mut no_exif = false; let mut no_exif = false;
let no_exif_ref = &mut no_exif; let no_exif_ref = &mut no_exif;
let mut name = name.unwrap_or_else(|| self.name.clone()); let mut name = name.unwrap_or_else(|| self.name.clone());
@@ -405,7 +401,7 @@ impl<'a> BlobObject<'a> {
// also `Viewtype::Gif` (maybe renamed to `Animation`) should be used for animated // also `Viewtype::Gif` (maybe renamed to `Animation`) should be used for animated
// images. // images.
let do_scale = exceeds_max_bytes let do_scale = exceeds_max_bytes
|| strict_limits || is_avatar
&& (exceeds_wh && (exceeds_wh
|| exif.is_some() && { || exif.is_some() && {
if mem::take(&mut add_white_bg) { if mem::take(&mut add_white_bg) {
@@ -442,7 +438,7 @@ impl<'a> BlobObject<'a> {
ofmt.clone(), ofmt.clone(),
max_bytes, max_bytes,
&mut encoded, &mut encoded,
)? && strict_limits )? && is_avatar
{ {
if img_wh < 20 { if img_wh < 20 {
return Err(format_err!( return Err(format_err!(
@@ -492,7 +488,7 @@ impl<'a> BlobObject<'a> {
match res { match res {
Ok(_) => res, Ok(_) => res,
Err(err) => { Err(err) => {
if !strict_limits && no_exif { if !is_avatar && no_exif {
warn!( warn!(
context, context,
"Cannot recode image, using original data: {err:#}.", "Cannot recode image, using original data: {err:#}.",