-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 additions
and
51 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Riiif | ||
# Represents a IIIF request | ||
class Transformation | ||
attr_reader :crop, :size, :quality, :rotation, :format | ||
def initialize(crop, size, quality, rotation, format) | ||
@crop = crop | ||
@size = size | ||
@quality = quality | ||
@rotation = rotation | ||
@format = format | ||
end | ||
|
||
# Create a clone of this Transformation, scaled by the factor | ||
# @param [Integer] factor the scale for the new transformation | ||
# @return [Transformation] a new transformation, scaled by factor | ||
def reduce(factor) | ||
Transformation.new(crop.dup, | ||
size.reduce(factor), | ||
quality, | ||
rotation, | ||
format) | ||
end | ||
|
||
# Create a clone of this Transformation, without the crop | ||
# @return [Transformation] a new transformation | ||
# TODO: it would be nice if we didn't need image_info | ||
def without_crop(image_info) | ||
Transformation.new(Region::Full.new(image_info), | ||
size.dup, | ||
quality, | ||
rotation, | ||
format) | ||
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
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
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Riiif::Transformation do | ||
subject(:transformation) do | ||
Riiif::Transformation.new(region, | ||
size, | ||
quality, | ||
rotation, | ||
fmt) | ||
end | ||
|
||
let(:region) { Riiif::Region::Full.new(image_info) } | ||
let(:size) { Riiif::Size::Percent.new(image_info, 20) } | ||
let(:quality) { nil } | ||
let(:rotation) { nil } | ||
let(:fmt) { nil } | ||
let(:image_info) { double('Image info', height: 4381, width: 6501) } | ||
|
||
describe 'reduce' do | ||
subject { transformation.reduce(factor) } | ||
context 'when reduced by 2' do | ||
let(:factor) { 2 } | ||
let(:size) { Riiif::Size::Percent.new(image_info, 20) } | ||
|
||
it 'downsamples the size' do | ||
expect(subject.size).to be_kind_of Riiif::Size::Percent | ||
expect(subject.size.percentage).to eq 80.0 | ||
end | ||
end | ||
end | ||
|
||
describe 'without_crop' do | ||
let(:region) { Riiif::Region::Absolute.new(image_info, 5, 6, 7, 8) } | ||
|
||
subject { transformation.without_crop(image_info) } | ||
it 'nullifies the crop' do | ||
expect(subject.crop).to be_kind_of Riiif::Region::Full | ||
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
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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Riiif::KakaduTransformer do | ||
subject(:instance) { described_class.new(path, image_info, transformation) } | ||
|
||
let(:image_info) { double('Image info', height: 4381, width: 6501) } | ||
let(:path) { 'baseball.jp2' } | ||
let(:region) { Riiif::Region::Full.new(image_info) } | ||
let(:size) { Riiif::Size::Full.new } | ||
let(:quality) { nil } | ||
let(:rotation) { nil } | ||
let(:fmt) { 'jpg' } | ||
|
||
let(:transformation) do | ||
Riiif::Transformation.new(region, | ||
size, | ||
quality, | ||
rotation, | ||
fmt) | ||
end | ||
|
||
describe '#transform' do | ||
let(:image_data) { double } | ||
|
||
subject(:transform) { instance.transform } | ||
|
||
before do | ||
allow(instance).to receive(:with_tempfile).and_yield('/tmp/foo.bmp') | ||
end | ||
|
||
context 'when reduction_factor is 0' do | ||
let(:reduction_factor) { 0 } | ||
it 'calls the Imagemagick transform' do | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('kdu_expand -quiet -i baseball.jp2 -num_threads 4 -o /tmp/foo.bmp') | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('convert -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-') | ||
transform | ||
end | ||
end | ||
|
||
context 'when reduction_factor is 1' do | ||
let(:size) { Riiif::Size::Percent.new(image_info, 30) } | ||
let(:reduction_factor) { 1 } | ||
|
||
it 'calls the Imagemagick transform' do | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('kdu_expand -quiet -i baseball.jp2 -num_threads 4 -reduce 1 -o /tmp/foo.bmp') | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('convert -resize 60.0% -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-') | ||
transform | ||
end | ||
end | ||
|
||
context 'when reduction_factor is 2' do | ||
let(:size) { Riiif::Size::Percent.new(image_info, 20) } | ||
let(:reduction_factor) { 2 } | ||
it 'calls the Imagemagick transform' do | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('kdu_expand -quiet -i baseball.jp2 -num_threads 4 -reduce 2 -o /tmp/foo.bmp') | ||
expect(Riiif::CommandRunner).to receive(:execute) | ||
.with('convert -resize 80.0% -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-') | ||
transform | ||
end | ||
end | ||
end | ||
end |