-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Differences to pixbuf: - plasma-straighten-positive-5 - MAE (Mean Average Error): 0.0117613 - plasma-straighten-on-vertical-image - MAE: 0.0159686 - plasma-straighten-negative-20 - MAE: 0.01176 Visually all the images appear to be shifted by 1px. Since I didn't spot any issues with maths, I'm inclined to assume that it's due to rounding/trimming during computations.
- Loading branch information
Showing
7 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'vips' | ||
|
||
module Morandi | ||
module Operation | ||
# Straighten operation | ||
# Does a small (ie. not 90,180,270 deg) rotation and zooms to avoid cropping | ||
# @!visibility private | ||
class VipsStraighten < ImageOperation | ||
# Colour for filling background post-rotation. It can bleed into the edge pixels during resize. | ||
# Setting it to gray minimises the average impact | ||
ROTATION_BACKGROUND_FILL_COLOUR = 127 | ||
ROTATION_BACKGROUND_FILL_ALPHA = 255 | ||
|
||
def self.rotation_background_fill_colour(channels_count:, alpha:) | ||
return [ROTATION_BACKGROUND_FILL_COLOUR] * channels_count unless alpha # Eg [127, 127, 127] for RGB | ||
|
||
# Eg [127, 127, 127, 255] for RGBA | ||
([ROTATION_BACKGROUND_FILL_COLOUR] * (channels_count - 1)) + [ROTATION_BACKGROUND_FILL_ALPHA] | ||
end | ||
|
||
attr_accessor :angle | ||
|
||
def call(img) | ||
return img if angle.zero? | ||
|
||
original_width = img.width | ||
original_height = img.height | ||
|
||
# It is possible to first rotate, then fetch width/height of resulting image to calculate scale, | ||
# but that would make us lose precision which degrades cropping accuracy | ||
rotation_value_rad = angle * (Math::PI / 180) | ||
post_rotation_bounding_box_width = (img.height.to_f * Math.sin(rotation_value_rad).abs) + | ||
(img.width.to_f * Math.cos(rotation_value_rad).abs) | ||
post_rotation_bounding_box_height = (img.width.to_f * Math.sin(rotation_value_rad).abs) + | ||
(img.height.to_f * Math.cos(rotation_value_rad).abs) | ||
|
||
# Calculate scaling required to fit the original width/height within rotated image without including background | ||
scale = [post_rotation_bounding_box_width / original_width, | ||
post_rotation_bounding_box_height / original_height].max | ||
|
||
background_fill_colour = self.class.rotation_background_fill_colour(channels_count: img.bands, | ||
alpha: img.has_alpha?) | ||
img = img.similarity(angle: angle, scale: scale, background: background_fill_colour) | ||
|
||
# Better precision than img.width/img.height due to fractions preservation | ||
post_scale_bounding_box_width = post_rotation_bounding_box_width * scale | ||
post_scale_bounding_box_height = post_rotation_bounding_box_height * scale | ||
|
||
width_diff = post_scale_bounding_box_width - original_width | ||
height_diff = post_scale_bounding_box_height - original_height | ||
|
||
# Round to nearest integer to reduce risk of ROTATION_BACKGROUND_FILL_COLOUR being visible in the corner | ||
crop_x = (width_diff / 2).round | ||
crop_y = (height_diff / 2).round | ||
|
||
img.crop(crop_x, crop_y, original_width, original_height) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.6 KB
spec/fixtures/reference_images/vips/plasma-straighten-on-vertical-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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