From 1f9489f5bfd3023329e0c296304720ec5dff7983 Mon Sep 17 00:00:00 2001 From: Jon Bracy Date: Tue, 14 Nov 2023 11:16:06 -0600 Subject: [PATCH] Update Quality defaults for support image types --- lib/bob_ross/backends/imagemagick.rb | 28 +++++++++++++++++++++++++++- lib/bob_ross/backends/libvips.rb | 26 ++++++++++++++++---------- test/bob_ross/image_test.rb | 16 ++++++++-------- test/bob_ross/server_test.rb | 16 ++++++++-------- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/lib/bob_ross/backends/imagemagick.rb b/lib/bob_ross/backends/imagemagick.rb index 3358fca..9fb1b0a 100644 --- a/lib/bob_ross/backends/imagemagick.rb +++ b/lib/bob_ross/backends/imagemagick.rb @@ -58,12 +58,38 @@ def transform(image, transformations, options) end end + saver_options = case options[:format] + when 'image/avif' + {Q: 45} + when 'image/heic' + {Q: 40} + when 'image/webp' + {Q: 45, min_size: true, effort: 6} + when 'image/jp2' + {Q: 40} + when 'image/jpeg' + {Q: 43, optimize_coding: true, trellis_quant: true, overshoot_deringing: true} + when 'image/png' + {compression: 9} + else + {} + end + + case options[:format] + when 'image/avif' + params << "-quality 45" unless options[:quality] + when 'image/heic' + params << "-quality 40" unless options[:quality] + when 'image/webp' + params << "-quality 45" unless options[:quality] + when 'image/jp2' + params << "-quality 40" unless options[:quality] when 'image/jpeg' + params << "-quality 43" unless options[:quality] params << "-define jpeg:optimize-coding=on" when 'image/png' params << "-define png:compression-level=9" - # else 'image/webp', 'image/jp2', 'image/heif', , 'image/avif' end options.each do |key, value| diff --git a/lib/bob_ross/backends/libvips.rb b/lib/bob_ross/backends/libvips.rb index 738753b..6a15a10 100644 --- a/lib/bob_ross/backends/libvips.rb +++ b/lib/bob_ross/backends/libvips.rb @@ -306,23 +306,31 @@ def transform(image, transformations, options) output = Tempfile.new(['blob', ".#{MiniMime.lookup_by_content_type(options[:format]).extension}"], binmode: true) saver_options = case options[:format] - when 'image/jpeg' - {optimize_coding: true, trellis_quant: true, overshoot_deringing: true} + when 'image/avif' + {Q: 45} + when 'image/heic' + {Q: 40} when 'image/webp' - {min_size: true, effort: 6} + {Q: 45, min_size: true, effort: 6} + when 'image/jp2' + {Q: 40} + when 'image/jpeg' + {Q: 43, optimize_coding: true, trellis_quant: true, overshoot_deringing: true} when 'image/png' {compression: 9} - else # 'image/jp2', 'image/heif', , 'image/avif' + else {} end options.each do |key, value| case key when :quality - saver_options[:Q] = if %w(image/jpeg image/jp2 image/heif image/avif).include?(options[:format]) - [[1, value.to_i].max, 100].min - else - value.to_i + if !value.nil? + saver_options[:Q] = if %w(image/jpeg image/jp2 image/heif image/avif).include?(options[:format]) + [[1, value.to_i].max, 100].min + else + value.to_i + end end when :strip saver_options[:strip] = true @@ -333,9 +341,7 @@ def transform(image, transformations, options) saver_options[:lossless] = true end end - # transformations.delete(:lossless) - vips.write_to_file(output.path, **select_valid_saver_options(output.path, saver_options)) output end diff --git a/test/bob_ross/image_test.rb b/test/bob_ross/image_test.rb index 007f3d0..7d45220 100644 --- a/test/bob_ross/image_test.rb +++ b/test/bob_ross/image_test.rb @@ -263,7 +263,7 @@ class BobRossImageTest < Minitest::Test geometry: '100x67', signature: [ 'b8428b80e19b60f18037e90f35de035b93f83c6f3bfd09ef4e088e2e6b7b8da1', - 'c39e3f0612cdf56c3354d632e9dbbb7bcc8bd8408b19d7cd226126e744115aaa' + '5c86d9f437c1cbc9827b36d1c08d5bd4d8b31828786f194cd2a7f2fd125868c8' ] }) @@ -271,7 +271,7 @@ class BobRossImageTest < Minitest::Test geometry: '150x100', signature: [ '6a119768c3135cfc8cd340fe273f765518512e99097da0961b7b80eb885a4e24', - 'ba2a72f86332cf5f0bc2146040d1925b340a22ab831a352ec5e713379ee1c108' + '6d1bd3539daa6de5e6cb8b97f1e90cc6e0c5694d0bc2abe0aa5fe01c27717ba7' ] }) @@ -279,7 +279,7 @@ class BobRossImageTest < Minitest::Test geometry: '100x67', signature: [ 'b8428b80e19b60f18037e90f35de035b93f83c6f3bfd09ef4e088e2e6b7b8da1', - 'c39e3f0612cdf56c3354d632e9dbbb7bcc8bd8408b19d7cd226126e744115aaa' + '5c86d9f437c1cbc9827b36d1c08d5bd4d8b31828786f194cd2a7f2fd125868c8' ] }) @@ -287,7 +287,7 @@ class BobRossImageTest < Minitest::Test geometry: '100x100', signature: [ 'b6a9cbf02cf05ba5ea6531daa6084b181ab30f5a0a078b224241297fa19619af', - 'bb90253f26775ec23e90c4465306da6a4ac1e1b18f4c14f1b1286b263373c885' + 'fcd4f1025705227b31d1310fd8c4160c0669c4f31fd840d9b4107165ef55f07a' ] }) @@ -303,7 +303,7 @@ class BobRossImageTest < Minitest::Test geometry: '200x133', signature: [ '04e2b2b059aba5fa0a094fc9e96019e19502396dce8c411c5f82a29de70db96a', - '53446dc48de4b954252e9ca03a3022fb05cf1dab24b0cd2f5e89e2f6f5084b58' + '47c34660701ddc02c5f18ba41392c3fe6188b1339c92d71ed324d3184d86173e' ] }) @@ -327,7 +327,7 @@ class BobRossImageTest < Minitest::Test geometry: '200x200', signature: [ 'd0900a56baa9dc3ca3f3499263565eca2f3595023b999a1308cabc01f0266a74', - '6cbf8367ca117379cbe93f4d7e1da51d9def5910f42fe6f863b6145ab692f081' + '10927e09b94dffc29842d3e760d9a02c7e3ce7eef613aba45dcba1abe4297a56' ] }) @@ -335,7 +335,7 @@ class BobRossImageTest < Minitest::Test geometry: '200x200', signature: [ 'f3209ac875fef39f31c0746cda7b80a65a05432534754cc7f6616d34128dc342', - '9603566efc107c75c7e48bba736ff5c2e73639c6bb9d7c541ccc0b6d2f5e8904' + 'e2d39515596ec280e3f92b2f767ae90aec51c26b593e82214d023c8e45686aa4' ] }) @@ -343,7 +343,7 @@ class BobRossImageTest < Minitest::Test geometry: '200x200', signature: [ 'f887e8e7c293970bd73f3880395e2d03b9adfcd222b24331c95fa5bc679c5127', - '58ddee7ed63418c00da0502bb9e7917c164e1275d155bdff8b2feac0e16a280c' + '1033fa0394f2d141f2fcf8ad40c6dfe9eb7f189a793101c8fef0ae75b5a87d78' ] }) end diff --git a/test/bob_ross/server_test.rb b/test/bob_ross/server_test.rb index c02e531..724c34f 100644 --- a/test/bob_ross/server_test.rb +++ b/test/bob_ross/server_test.rb @@ -217,19 +217,19 @@ def create_server(configs={}) response = server.get("/flyer") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['0bfb297897f6f5cb3395105745fd35a0', 'e35901e7be8d4ce03a0b7b94da8e8778'], Digest::MD5.hexdigest(response.body) + assert_includes ['7d5daa0941b10cb47277e954a77413b2', '2f4645b128d93f5d9304b79baabb9fdd'], Digest::MD5.hexdigest(response.body) response = server.get("/S100/floorplan") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['0f3ce776621680da230e625554aa5372', '05b1a34160cbcce7d19cbd2293e5b3bf'], Digest::MD5.hexdigest(response.body) + assert_includes ['592a7aed4be6c66f3deac77762153823', '2d0c2c8966dc484c540141f57ae73cae'], Digest::MD5.hexdigest(response.body) response = server.get("/S50x50/floorplan") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['f50d18da6bd6a86952cf4bad797553ec', '9200b154ad0488b05dc8ea775bbda46d'], Digest::MD5.hexdigest(response.body) + assert_includes ['4bd78a3a3f7be88b2272b2697e10183b', '9863144037ac5cccca31b0d22304bf7b'], Digest::MD5.hexdigest(response.body) response = server.get("/Sx50/flyer") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['8db654d754f9c7e584669f4cb75f57ad', '2dee9aa27e44be62796f0ca6d086eda7'], Digest::MD5.hexdigest(response.body) + assert_includes ['c7bb8896007fdc48fcb683a5533e5712', 'ff8552e8b6990d9b293c2c2c88bfa116'], Digest::MD5.hexdigest(response.body) end test 'a Video' do @@ -237,18 +237,18 @@ def create_server(configs={}) response = server.get("/video") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['53a4df2f7588fe954f2dcc57d6b8bab8', 'd3d0ffa9b1dd870594b6af2dabb782b8'], Digest::MD5.hexdigest(response.body) + assert_includes ['dd0af2d65277b93f7c3de4007be21081', '76311e89661fc5d21828084271c08455'], Digest::MD5.hexdigest(response.body) response = server.get("/S100/video") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['2303a1cb94aed6e5889ed082071c52d5', '9f5cf9cef5377d7222aaf071eeaeb60a'], Digest::MD5.hexdigest(response.body) + assert_includes ['549a9a3fff71f7ae5c135142a2885166', '351a240af0ec0db5758d5120abc73984'], Digest::MD5.hexdigest(response.body) response = server.get("/S50x50/video") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['ed50d0301a8f5c6029154a084a1f4eb7', '768ff98e2a6936d61574c9511aae3546'], Digest::MD5.hexdigest(response.body) + assert_includes ['f103fb67f500511bd29ec10c5205b40f', '87c98e63d160c2d15ff5aeec1ed866b7'], Digest::MD5.hexdigest(response.body) response = server.get("/Sx50/video") assert_equal 'image/jpeg', response.headers['Content-Type'] - assert_includes ['a6055e4f8356c833dde52d4daa9f5570', '5364e3a417d111b38fa05e503400c553'], Digest::MD5.hexdigest(response.body) + assert_includes ['d462eac44ca95715b288c4e501dc1f7d', '2b00b06f58ede4e365e3f7e33e65df99'], Digest::MD5.hexdigest(response.body) end end \ No newline at end of file