diff --git a/app/services/riiif/imagemagick_command_factory.rb b/app/services/riiif/imagemagick_command_factory.rb index 55d8d3d..1e03787 100644 --- a/app/services/riiif/imagemagick_command_factory.rb +++ b/app/services/riiif/imagemagick_command_factory.rb @@ -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 @@ -88,5 +104,9 @@ def colorspace ' -colorspace Gray -type Bilevel' end end + + def flatten_layers + ' -flatten' if flatten + end end end diff --git a/spec/models/riiif/image_spec.rb b/spec/models/riiif/image_spec.rb index cffcbe4..ebaf7f6 100644 --- a/spec/models/riiif/image_spec.rb +++ b/spec/models/riiif/image_spec.rb @@ -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' @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/transformers/riiif/kakadu_transformer_spec.rb b/spec/transformers/riiif/kakadu_transformer_spec.rb index d2be599..e830ba2 100644 --- a/spec/transformers/riiif/kakadu_transformer_spec.rb +++ b/spec/transformers/riiif/kakadu_transformer_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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