Skip to content

Commit

Permalink
feat: add cropping via vips
Browse files Browse the repository at this point in the history
The difference in reference images:
- plasma-cropped-excessive-size - MAE (mean average error): 0.00950674
- plasma-cropped - MAE: 0.0107107
- plasma-cropped-negative-initial-coords - MAE: 0.00959682
- plasma-cropped-1x1 - no difference, duplicated in vips directory for completeness
- plasma-auto-cropped - MAE: 0.0181914 , also one pixel difference in height (pixbuf trims 243.75 down, vips rounds it up)
                        it also required adding tolerance due to differences between vips used on development and CI; (MAE: 0.00480813)

Libvips crop output appears a bit more noisy, but it seems to better resemble the original
  • Loading branch information
knarewski committed Nov 29, 2024
1 parent 5a33f46 commit c0925db
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Vips image processor (resizing)
- Vips colour filters
- Vips crop

### Removed
- [BREAKING] dropped support for a broken 'dominant' border colour
Expand Down
34 changes: 34 additions & 0 deletions lib/morandi/crop_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,39 @@ def apply_crop(pixbuf, x_coord, y_coord, width, height, fill_col = 0xffffffff)
end
pixbuf
end

def apply_crop_vips(img, x_coord, y_coord, width, height)
if x_coord.negative? ||
y_coord.negative? ||
((x_coord + width) > img.width) ||
((y_coord + height) > img.height)

extract_area_x = [0, x_coord].max
extract_area_y = [0, y_coord].max
area_to_copy = img.extract_area(extract_area_x, extract_area_y, img.width - extract_area_x,
img.height - extract_area_y)

fill_colour = [255, 255, 255]
pixel = (Vips::Image.black(1, 1).colourspace(:srgb) + fill_colour).cast(img.format)
canvas = pixel.embed 0, 0, width, height, extend: :copy

cropped = canvas.composite(area_to_copy, :over, x: [-x_coord, 0].max,
y: [-y_coord, 0].max,
compositing_space: area_to_copy.interpretation)

# Because image is drawn on an opaque white, alpha doesn't matter at this point anyway, so let's strip the
# alpha channel from the output. According to #composite docs, the resulting image always has alpha channel,
# but I added a guard to avoid regressions if that ever changes.
cropped = cropped.extract_band(0, n: cropped.bands - 1) if cropped.has_alpha?
cropped
else
x_coord = x_coord.clamp(0, img.width)
y_coord = y_coord.clamp(0, img.height)
width = width.clamp(1, img.width - x_coord)
height = height.clamp(1, img.height - y_coord)

img.crop(x_coord, y_coord, width, height)
end
end
end
end
19 changes: 19 additions & 0 deletions lib/morandi/vips_image_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def process!
@scale = 1.0
end

apply_crop!
apply_filters!

return unless @options['output.limit'] && @output_width && @output_height
Expand All @@ -82,6 +83,24 @@ def write_to_jpeg(write_to, quality = nil)

private

def apply_crop!
crop = @options['crop']

return if crop.nil? && @options['image.auto-crop'].eql?(false)

crop = crop.split(',').map(&:to_i) if crop.is_a?(String) && crop =~ /^\d+,\d+,\d+,\d+/

crop = nil unless crop.is_a?(Array) && crop.size.eql?(4) && crop.all? do |i|
i.is_a?(Numeric)
end
# can't crop, won't crop
return if @output_width.nil? && @output_height.nil? && crop.nil?

crop = crop.map { |s| (s.to_f * @scale).floor } if crop && not_equal_to_one(@scale)
crop ||= Morandi::CropUtils.autocrop_coords(@img.width, @img.height, @output_width, @output_height)
@img = Morandi::CropUtils.apply_crop_vips(@img, crop[0], crop[1], crop[2], crop[3])
end

def apply_filters!
filter_name = @options['fx']
return unless SUPPORTED_FILTERS.include?(filter_name)
Expand Down
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions spec/morandi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
end
end

describe 'with a big image and a bigger cropped area to fill', vips_wip: processor_name == 'vips' do
describe 'with a big image and a bigger cropped area to fill' do
let(:options) do
{
'crop' => '0,477,15839,18804',
Expand Down Expand Up @@ -148,7 +148,7 @@
end
end

context 'when give a "crop" option', vips_wip: processor_name == 'vips' do
context 'when give a "crop" option' do
let(:cropped_width) { 300 }
let(:cropped_height) { 300 }

Expand All @@ -162,7 +162,7 @@
expect(processed_image_width).to eq(cropped_width)
expect(processed_image_height).to eq(cropped_height)

expect(file_out).to match_reference_image('plasma-cropped')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-cropped')
end
end

Expand All @@ -176,7 +176,7 @@
expect(processed_image_width).to eq(cropped_width)
expect(processed_image_height).to eq(cropped_height)

expect(file_out).to match_reference_image('plasma-cropped')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-cropped')
end
end

Expand All @@ -190,7 +190,7 @@
expect(processed_image_width).to eq(cropped_width)
expect(processed_image_height).to eq(cropped_height)

expect(file_out).to match_reference_image('plasma-cropped-negative-initial-coords')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-cropped-negative-initial-coords')
end
end

Expand All @@ -204,7 +204,7 @@
expect(processed_image_width).to eq(original_image_width + 50)
expect(processed_image_height).to eq(original_image_height + 50)

expect(file_out).to match_reference_image('plasma-cropped-excessive-size')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-cropped-excessive-size')
end
end

Expand All @@ -218,7 +218,7 @@
expect(processed_image_width).to eq(1)
expect(processed_image_height).to eq(1)

expect(file_out).to match_reference_image('plasma-cropped-1x1')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-cropped-1x1')
end
end
end
Expand Down Expand Up @@ -332,7 +332,7 @@
end
end

describe 'when changing the dimensions and auto-cropping', vips_wip: processor_name == 'vips' do
describe 'when changing the dimensions and auto-cropping' do
let(:max_width) { 300 }
let(:max_height) { 200 }

Expand All @@ -353,7 +353,7 @@
expect(processed_image_width).to be <= max_width
expect(processed_image_height).to be <= max_height

expect(file_out).to match_reference_image('plasma-auto-cropped')
expect(file_out).to match_reference_image(reference_image_prefix, 'plasma-auto-cropped', tolerance: 0.0049)
end
end

Expand Down

0 comments on commit c0925db

Please sign in to comment.