Skip to content

Commit

Permalink
ImageMagickCommandFactory: add -flatten by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dunn committed Dec 11, 2017
1 parent 6acf20d commit d22911a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
26 changes: 23 additions & 3 deletions app/services/riiif/imagemagick_command_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,36 @@ class ImagemagickCommandFactory
# @param [Integer] compression (85) the compression level to use (set 0 for no compression)
# @param [String] sampling_factor ("4:2:0") the chroma sample factor (set 0 for no compression)
# @param [Boolean] strip_metadata (true) do we want to strip EXIF tags?
def initialize(path, info, transformation, compression: 85, sampling_factor: '4:2:0', strip_metadata: true)
# @param [Boolean] flatten (true) do we want to flatten the image/PDF layers?
def initialize(path, info, transformation, compression: 85,
sampling_factor: '4:2:0', strip_metadata: true, flatten: true)
@path = path
@info = info
@transformation = transformation
@compression = compression
@sampling_factor = sampling_factor
@strip_metadata = strip_metadata
@flatten = flatten
end

attr_reader :path, :info, :transformation, :compression, :sampling_factor, :strip_metadata
attr_reader :path, :info, :transformation, :compression,
:sampling_factor, :strip_metadata, :flatten

# @return [String] a command for running imagemagick to produce the requested output
def command
[external_command, crop, size, rotation, colorspace, quality, sampling, metadata, input, output].join
[
external_command,
crop,
size,
rotation,
colorspace,
quality,
sampling,
metadata,
flatten_layers,
input,
output
].join
end

def reduction_factor
Expand Down Expand Up @@ -88,5 +104,9 @@ def colorspace
' -colorspace Gray -type Bilevel'
end
end

def flatten_layers
' -flatten' if flatten
end
end
end
36 changes: 18 additions & 18 deletions spec/models/riiif/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

it 'renders' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -quality 85 -sampling-factor 4:2:0 -strip #{filename} jpg:-")
.with("convert -quality 85 -sampling-factor 4:2:0 -strip -flatten #{filename} jpg:-")
.and_return('imagedata')

expect(subject.render('size' => 'full', format: 'jpg')).to eq 'imagedata'
Expand Down Expand Up @@ -82,7 +82,7 @@

it 'returns the original' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -strip #{filename} png:-")
.with("convert -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -92,7 +92,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -crop 60x75+80+15 -strip #{filename} png:-")
.with("convert -crop 60x75+80+15 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -102,7 +102,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -crop 80.0%x70.0+18+13 -strip #{filename} png:-")
.with("convert -crop 80.0%x70.0+18+13 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -112,7 +112,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -crop 131x131+22+0 -strip #{filename} png:-")
.with("convert -crop 131x131+22+0 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -134,7 +134,7 @@

it 'returns the original' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -strip #{filename} png:-")
.with("convert -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -144,7 +144,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize 50.0% -strip #{filename} png:-")
.with("convert -resize 50.0% -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -154,7 +154,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize 12.5% -strip #{filename} png:-")
.with("convert -resize 12.5% -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -164,7 +164,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize 50 -strip #{filename} png:-")
.with("convert -resize 50 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -174,7 +174,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize x50 -strip #{filename} png:-")
.with("convert -resize x50 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -184,7 +184,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize 150x75! -strip #{filename} png:-")
.with("convert -resize 150x75! -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -193,7 +193,7 @@

it 'runs the correct imagemagick command' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -resize 150x75 -strip #{filename} png:-")
.with("convert -resize 150x75 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -215,7 +215,7 @@

it 'returns the original' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -strip #{filename} png:-")
.with("convert -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -225,7 +225,7 @@

it 'handles floats' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -virtual-pixel white +distort srt 22.5 -strip #{filename} png:-")
.with("convert -virtual-pixel white +distort srt 22.5 -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -247,7 +247,7 @@

it 'returns the original when specifing default' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -strip #{filename} png:-")
.with("convert -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -257,7 +257,7 @@

it 'returns the original when specifing color' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -strip #{filename} png:-")
.with("convert -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -267,7 +267,7 @@

it 'converts to grayscale' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -colorspace Gray -strip #{filename} png:-")
.with("convert -colorspace Gray -strip -flatten #{filename} png:-")
render
end
end
Expand All @@ -277,7 +277,7 @@

it 'converts to bitonal' do
expect(Riiif::CommandRunner).to receive(:execute)
.with("convert -colorspace Gray -type Bilevel -strip #{filename} png:-")
.with("convert -colorspace Gray -type Bilevel -strip -flatten #{filename} png:-")
render
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/transformers/riiif/kakadu_transformer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
.with('kdu_expand -quiet -i baseball.jp2 -num_threads 4 ' \
'-region "{0.1,0.2},{0.1,0.1}" -reduce 4 -o /tmp/foo.bmp')
expect(Riiif::CommandRunner).to receive(:execute)
.with('convert -resize 38x38! -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-')
.with('convert -resize 38x38! -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -54,7 +54,7 @@
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:-')
.with('convert -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -67,7 +67,7 @@
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 -resize 651 -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-')
.with('convert -resize 651 -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -80,7 +80,7 @@
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 -resize x581 -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-')
.with('convert -resize x581 -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -96,7 +96,7 @@
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:-')
.with('convert -resize 60.0% -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -109,7 +109,7 @@
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 408 -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-')
.with('convert -resize 408 -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -122,7 +122,7 @@
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 x481 -quality 85 -sampling-factor 4:2:0 -strip /tmp/foo.bmp jpg:-')
.with('convert -resize x481 -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand All @@ -135,7 +135,7 @@
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:-')
.with('convert -resize 80.0% -quality 85 -sampling-factor 4:2:0 -strip -flatten /tmp/foo.bmp jpg:-')
transform
end
end
Expand Down

0 comments on commit d22911a

Please sign in to comment.