Module: ImagePack

Defined in:
lib/image_pack.rb,
lib/image_pack/errors.rb,
lib/image_pack/version.rb,
lib/image_pack/configuration.rb,
ext/image_pack/image_pack.c

Defined Under Namespace

Classes: CancelledError, Configuration, EncodeError, Error, InvalidArgumentError, InvalidImageError, LimitExceededError, OutOfMemoryError, QualityConstraintError, UnsupportedError

Constant Summary collapse

ALGOS =
%i[jpeg_turbo mozjpeg fast size].freeze
ALGO_TO_NATIVE =
{ jpeg_turbo: :jpeg_turbo, mozjpeg: :mozjpeg, fast: :jpeg_turbo, size: :mozjpeg }.freeze
EXECUTION_MODES =
%i[direct nogvl offload auto].freeze
DEFAULT_QUALITY =
82
DEFAULT_ALGO =
:mozjpeg
VERSION =
"0.2.3"
NATIVE_MOZJPEG_VERSION =
rb_str_new_cstr(VERSION)
NATIVE_SIMD =
Qfalse
NATIVE_OFFLOAD_SAFE =
Qfalse

Class Method Summary collapse

Class Method Details

.build_infoObject



49
50
51
52
53
54
55
56
# File 'lib/image_pack.rb', line 49

def build_info
  {
    version: VERSION,
    mozjpeg: defined?(NATIVE_MOZJPEG_VERSION) ? NATIVE_MOZJPEG_VERSION : nil,
    simd: defined?(NATIVE_SIMD) ? NATIVE_SIMD : nil,
    offload_safe: offload_safe?
  }
end

.compress(input, output: nil, algo: DEFAULT_ALGO, quality: nil, min_ssim: nil, mozjpeg_trellis: true, progressive: false, strip_metadata: true, execution: nil, cancellable: false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/image_pack.rb', line 115

def compress(input,
             output: nil,
             algo: DEFAULT_ALGO,
             quality: nil,
             min_ssim: nil,
             mozjpeg_trellis: true,
             progressive: false,
             strip_metadata: true,
             execution: nil,
             cancellable: false)
  validate_algo!(algo)
  validate_min_ssim!(min_ssim)
  validate_boolean!(:mozjpeg_trellis, mozjpeg_trellis)
  validate_boolean!(:progressive, progressive)
  validate_boolean!(:strip_metadata, )
  validate_boolean!(:cancellable, cancellable)
  quality_was_given = !quality.nil?
  effective_quality = quality_was_given ? quality : DEFAULT_QUALITY
  effective_quality = 1 if min_ssim && !quality_was_given
  validate_quality!(effective_quality)
  execution ||= configuration.execution
  validate_execution!(execution)
  validate_execution_supported!(execution)
  validate_cancellable!(algo, execution, cancellable)

  normalized_input_kind = input_kind!(input)
  normalized_output_kind = output_kind!(output)
  has_scheduler = fiber_scheduler_active?

  __compress_jpeg(input, normalized_input_kind,
                  output, normalized_output_kind,
                  ALGO_TO_NATIVE.fetch(algo), effective_quality,
                  min_ssim ? min_ssim.to_f : 0.0,
                  mozjpeg_trellis ? 1 : 0,
                  progressive ? 1 : 0,
                   ? 1 : 0,
                  execution,
                  cancellable ? 1 : 0,
                  has_scheduler ? 1 : 0)
end

.compress_bytes(bytes, **options) ⇒ Object



62
63
64
65
66
# File 'lib/image_pack.rb', line 62

def compress_bytes(bytes, **options)
  raise InvalidArgumentError, "bytes must be a String" unless bytes.is_a?(String)

  compress(bytes.b, **options)
end

.compress_file(path, **options) ⇒ Object



68
69
70
71
72
73
# File 'lib/image_pack.rb', line 68

def compress_file(path, **options)
  pathname = Pathname(path)
  raise InvalidArgumentError, "input path does not exist: #{pathname}" unless pathname.file?

  compress(pathname, **options)
end

.compress_pixels(buffer, width:, height:, channels:, output: nil, algo: DEFAULT_ALGO, quality: nil, min_ssim: nil, mozjpeg_trellis: true, progressive: false, drop_alpha: nil, exact_size: false, execution: nil, cancellable: false) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/image_pack.rb', line 156

def compress_pixels(buffer,
                    width:,
                    height:,
                    channels:,
                    output: nil,
                    algo: DEFAULT_ALGO,
                    quality: nil,
                    min_ssim: nil,
                    mozjpeg_trellis: true,
                    progressive: false,
                    drop_alpha: nil,
                    exact_size: false,
                    execution: nil,
                    cancellable: false)
  validate_pixel_buffer!(buffer)
  validate_algo!(algo)
  validate_min_ssim!(min_ssim)
  validate_boolean!(:mozjpeg_trellis, mozjpeg_trellis)
  validate_boolean!(:progressive, progressive)
  validate_drop_alpha!(drop_alpha)
  validate_boolean!(:exact_size, exact_size)
  validate_boolean!(:cancellable, cancellable)
  quality_was_given = !quality.nil?
  effective_quality = quality_was_given ? quality : DEFAULT_QUALITY
  effective_quality = 1 if min_ssim && !quality_was_given
  validate_quality!(effective_quality)
  validate_dimensions!(width, height, channels)
  execution ||= configuration.execution
  validate_execution!(execution)
  validate_execution_supported!(execution)
  validate_cancellable!(algo, execution, cancellable)

  if channels == 4
    case drop_alpha
    when nil
      warn "ImagePack.compress_pixels: RGBA input has its alpha channel " \
           "discarded (JPEG cannot store alpha). Pass drop_alpha: true to " \
           "silence this warning, or drop_alpha: false to raise instead."
    when false
      raise UnsupportedError,
            "JPEG cannot store an alpha channel. Pass drop_alpha: true to drop it explicitly."
    end
  end

  normalized_output_kind = output_kind!(output)
  has_scheduler = fiber_scheduler_active?

  if min_ssim && channels == 4
    raise UnsupportedError, "min_ssim is not supported for RGBA input"
  end

  __compress_pixels(buffer,
                    width, height, channels,
                    output, normalized_output_kind,
                    ALGO_TO_NATIVE.fetch(algo), effective_quality,
                    min_ssim ? min_ssim.to_f : 0.0,
                    mozjpeg_trellis ? 1 : 0,
                    progressive ? 1 : 0,
                    exact_size ? 1 : 0,
                    execution,
                    cancellable ? 1 : 0,
                    has_scheduler ? 1 : 0)
end

.configurationObject



38
39
40
# File 'lib/image_pack.rb', line 38

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



42
43
44
45
46
47
# File 'lib/image_pack.rb', line 42

def configure
  return configuration unless block_given?

  yield(configuration)
  configuration
end

.inspect_image(input) ⇒ Object



220
221
222
# File 'lib/image_pack.rb', line 220

def inspect_image(input)
  __inspect_image(input, input_kind!(input))
end

.offload_safe?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/image_pack.rb', line 58

def offload_safe?
  defined?(NATIVE_OFFLOAD_SAFE) && NATIVE_OFFLOAD_SAFE == true
end

.optimize_bytes(bytes, **options) ⇒ Object



75
76
77
78
79
# File 'lib/image_pack.rb', line 75

def optimize_bytes(bytes, **options)
  raise InvalidArgumentError, "bytes must be a String" unless bytes.is_a?(String)

  optimize_jpeg(bytes.b, **options)
end

.optimize_file(path, **options) ⇒ Object



81
82
83
84
85
86
# File 'lib/image_pack.rb', line 81

def optimize_file(path, **options)
  pathname = Pathname(path)
  raise InvalidArgumentError, "input path does not exist: #{pathname}" unless pathname.file?

  optimize_jpeg(pathname, **options)
end

.optimize_jpeg(input, output: nil, progressive: true, strip_metadata: false, execution: nil, cancellable: false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/image_pack.rb', line 88

def optimize_jpeg(input,
                  output: nil,
                  progressive: true,
                  strip_metadata: false,
                  execution: nil,
                  cancellable: false)
  validate_boolean!(:progressive, progressive)
  validate_boolean!(:strip_metadata, )
  validate_boolean!(:cancellable, cancellable)
  execution ||= configuration.execution
  validate_execution!(execution)
  validate_execution_supported!(execution)
  validate_cancellable!(:lossless_optimize, execution, cancellable)

  normalized_input_kind = input_kind!(input)
  normalized_output_kind = output_kind!(output)
  has_scheduler = fiber_scheduler_active?

  __optimize_jpeg(input, normalized_input_kind,
                  output, normalized_output_kind,
                  progressive ? 1 : 0,
                   ? 1 : 0,
                  execution,
                  cancellable ? 1 : 0,
                  has_scheduler ? 1 : 0)
end