Module: SafeImage::JpegliBackend

Defined in:
lib/safe_image/jpegli_backend.rb

Constant Summary collapse

DIRECT_INPUTS =
%w[png].freeze
CHROMA_SUBSAMPLING =
%w[420 422 444].freeze
DEFAULT_QUALITY =
85

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/safe_image/jpegli_backend.rb', line 15

def available?
  Runner.available?("cjpegli")
end

.convert(input:, output:, quality: DEFAULT_QUALITY, chroma_subsampling: :auto, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/safe_image/jpegli_backend.rb', line 23

def convert(input:, output:, quality: DEFAULT_QUALITY, chroma_subsampling: :auto, timeout: Runner::DEFAULT_TIMEOUT)
  raise UnsupportedFormatError, "cjpegli is not installed" unless available?

  input = PathSafety.ensure_regular_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  ensure_jpeg_output!(output)

  input_format = normalized_ext(input)
  unless DIRECT_INPUTS.include?(input_format)
    raise UnsupportedFormatError, "cjpegli direct input format is unsupported: #{input_format.inspect}"
  end

  quality = validate_quality!(quality)
  chroma_subsampling = validate_chroma_subsampling!(chroma_subsampling, input_format: input_format)
  encode(input: input, output: output, quality: quality, chroma_subsampling: chroma_subsampling, timeout: timeout, input_format: input_format)
end

.encode(input:, output:, quality: DEFAULT_QUALITY, chroma_subsampling: "420", timeout: Runner::DEFAULT_TIMEOUT, input_format: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/safe_image/jpegli_backend.rb', line 40

def encode(input:, output:, quality: DEFAULT_QUALITY, chroma_subsampling: "420", timeout: Runner::DEFAULT_TIMEOUT, input_format: nil)
  raise UnsupportedFormatError, "cjpegli is not installed" unless available?

  input = PathSafety.ensure_regular_file!(input)
  output_path = PathSafety.ensure_safe_output_path!(output)
  ensure_jpeg_output!(output_path)
  output_path.dirname.mkpath

  input_format ||= normalized_ext(input)
  quality = validate_quality!(quality)
  chroma_subsampling = validate_chroma_subsampling!(chroma_subsampling, input_format: input_format)

  tmp = Tempfile.new([output_path.basename(".*").to_s, ".cjpegli.jpg"], output_path.dirname.to_s)
  tmp_path = Pathname.new(tmp.path)
  tmp.close

  begin
    argv = [
      "cjpegli",
      input.to_s,
      tmp_path.to_s,
      "--quality=#{quality}",
      "--chroma_subsampling=#{chroma_subsampling}"
    ]
    Runner.run!(argv, timeout: timeout, read: [input.to_s], write: [output_path.dirname.to_s])
    raise Error, "cjpegli did not create output" unless tmp_path.file? && File.size(tmp_path).positive?
    FileUtils.mv(tmp_path, output_path)

    info = Native.probe(output_path.to_s)
    {
      input_format: input_format,
      output_format: "jpg",
      width: info.fetch(:width),
      height: info.fetch(:height),
      duration_ms: info.fetch(:duration_ms),
      encoder: "cjpegli",
      chroma_subsampling: chroma_subsampling
    }
  ensure
    FileUtils.rm_f(tmp_path)
  end
end

.ensure_jpeg_output!(output) ⇒ Object



99
100
101
102
# File 'lib/safe_image/jpegli_backend.rb', line 99

def ensure_jpeg_output!(output)
  ext = normalized_ext(output)
  raise UnsupportedFormatError, "cjpegli only outputs jpg/jpeg, got #{ext.inspect}" unless ext == "jpg"
end

.normalized_ext(path) ⇒ Object



104
105
106
107
# File 'lib/safe_image/jpegli_backend.rb', line 104

def normalized_ext(path)
  ext = File.extname(path.to_s).delete_prefix(".").downcase
  ext == "jpeg" ? "jpg" : ext
end

.suitable_direct_input?(input) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/safe_image/jpegli_backend.rb', line 19

def suitable_direct_input?(input)
  DIRECT_INPUTS.include?(normalized_ext(input))
end

.validate_chroma_subsampling!(value, input_format: nil) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
# File 'lib/safe_image/jpegli_backend.rb', line 90

def validate_chroma_subsampling!(value, input_format: nil)
  value = :auto if value.nil?
  value = "444" if value.to_sym == :auto && input_format.to_s == "png"
  value = "420" if value.to_sym == :auto
  value = value.to_s
  raise ArgumentError, "chroma_subsampling must be one of #{CHROMA_SUBSAMPLING.join(", ")}" unless CHROMA_SUBSAMPLING.include?(value)
  value
end

.validate_quality!(quality) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
# File 'lib/safe_image/jpegli_backend.rb', line 83

def validate_quality!(quality)
  quality = DEFAULT_QUALITY if quality.nil?
  quality = Integer(quality)
  raise ArgumentError, "quality must be 1..100" unless (1..100).cover?(quality)
  quality
end