Skip to content

Commit

Permalink
fix: scaling calc
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Feb 2, 2024
1 parent a6735a7 commit b90d3ec
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions platform/image_processor/src/processor/job/scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ impl ScalingOptions {
(scales.len() - 1, input_scale_factor)
});

dbg!(&scales);


if self.input_image_scaling {
let scaled_width = padded_size.width / scales[best_idx].1 / input_scale_factor;
let scaled_height = padded_size.height / scales[best_idx].1 / input_scale_factor;
Expand All @@ -240,8 +239,6 @@ impl ScalingOptions {
if self.upscale.preserve_source() {
let padded_size = padded_size / input_scale_factor;

dbg!(&padded_size);

let size = scales[best_idx].0;

if size.width > padded_size.width || size.height > padded_size.height {
Expand Down Expand Up @@ -280,7 +277,7 @@ impl ScalingOptions {

scales
.into_iter()
.map(|(mut size, _)| {
.map(|(mut size, scale)| {
let input_aspect_ratio = self.input_aspect_ratio();

if self.preserve_aspect_height && self.aspect_ratio <= Ratio::ONE {
Expand All @@ -293,6 +290,23 @@ impl ScalingOptions {
size.width = width;
}

if self.preserve_aspect_height || self.preserve_aspect_width {
// need to make sure the new sizes are not larger than the max for this scale
let (width, height) = if self.aspect_ratio > Ratio::ONE {
(scale * self.aspect_ratio, scale)
} else {
(scale, scale / self.aspect_ratio)
};

if size.width > width {
size.height *= width / size.width;
size.width = width;
} else if size.height > height {
size.width *= height / size.height;
size.height = height;
}
}

Size {
width: size.width.as_f64().round() as usize,
height: size.height.as_f64().round() as usize,
Expand Down Expand Up @@ -333,8 +347,11 @@ impl ScalingOptions {

let (width, height) = if self.aspect_ratio < Ratio::ONE {
(width, width / self.aspect_ratio)
} else {
} else if self.aspect_ratio > Ratio::ONE {
(height * self.aspect_ratio, height)
} else {
let max = width.max(height);
(max, max)
};

Size { width, height }
Expand Down

0 comments on commit b90d3ec

Please sign in to comment.