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
SUBSAMPLING_MODES =
%i[auto 420 422 444].freeze
TUNE_MODES =
%i[hvs ssim ms_ssim psnr].freeze
EFFORT_MODES =
%i[default fast max].freeze
SUBSAMPLING_TO_NATIVE =
{ auto: 0, "420": 1, "422": 2, "444": 3 }.freeze
TUNE_TO_NATIVE =
{ hvs: 0, ssim: 1, ms_ssim: 2, psnr: 3 }.freeze
EFFORT_TO_NATIVE =
{ default: 0, fast: 1, max: 2 }.freeze
DEFAULT_QUALITY =
82
DEFAULT_ALGO =
:mozjpeg
AUTO_444_QUALITY =
90
VERSION =
"0.2.7"
NATIVE_MOZJPEG_VERSION =
rb_str_new_cstr(IMAGE_PACK_MOZJPEG_VERSION)
NATIVE_SIMD =
Qfalse
NATIVE_OFFLOAD_SAFE =
Qfalse

Class Method Summary collapse

Class Method Details

.build_infoObject



56
57
58
59
60
61
62
63
# File 'lib/image_pack.rb', line 56

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, mozjpeg_scan_opt: nil, progressive: nil, strip_metadata: true, strip_icc: false, subsampling: :auto, tune: :hvs, effort: :default, scale: 1, execution: nil, cancellable: false, report: false, strict: false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
# File 'lib/image_pack.rb', line 141

def compress(input,
             output: nil,
             algo: DEFAULT_ALGO,
             quality: nil,
             min_ssim: nil,
             mozjpeg_trellis: true,
             mozjpeg_scan_opt: nil,
             progressive: nil,
             strip_metadata: true,
             strip_icc: false,
             subsampling: :auto,
             tune: :hvs,
             effort: :default,
             scale: 1,
             execution: nil,
             cancellable: false,
             report: false,
             strict: false)
  warn_if_scalar!
  validate_algo!(algo)
  validate_min_ssim!(min_ssim)
  validate_boolean!(:mozjpeg_trellis, mozjpeg_trellis)
  scan_opt_given = !mozjpeg_scan_opt.nil?
  mozjpeg_scan_opt = true unless scan_opt_given
  validate_boolean!(:mozjpeg_scan_opt, mozjpeg_scan_opt)
  progressive = default_progressive_for(algo, progressive)
  validate_boolean!(:progressive, progressive)
  validate_boolean!(:strip_metadata, )
  validate_boolean!(:strip_icc, strip_icc)
  validate_boolean!(:cancellable, cancellable)
  validate_boolean!(:report, report)
  validate_boolean!(:strict, strict)
  subsampling = validate_subsampling!(subsampling)
  tune = validate_tune!(tune)
  effort = validate_effort!(effort)
  scale_num, scale_denom = validate_scale!(scale)
  mozjpeg_scan_opt = false if effort == :fast && !scan_opt_given
  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,
                  report ? 1 : 0,
                  strict ? 1 : 0,
                  mozjpeg_scan_opt ? 1 : 0,
                  strip_icc ? 1 : 0,
                  SUBSAMPLING_TO_NATIVE.fetch(subsampling),
                  TUNE_TO_NATIVE.fetch(tune),
                  EFFORT_TO_NATIVE.fetch(effort),
                  scale_num, scale_denom)
end

.compress_bytes(bytes, **options) ⇒ Object



78
79
80
81
82
# File 'lib/image_pack.rb', line 78

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



84
85
86
87
88
89
# File 'lib/image_pack.rb', line 84

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, mozjpeg_scan_opt: nil, progressive: nil, drop_alpha: nil, exact_size: false, subsampling: :auto, tune: :hvs, effort: :default, execution: nil, cancellable: false, report: false, strict: false) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/image_pack.rb', line 211

def compress_pixels(buffer,
                    width:,
                    height:,
                    channels:,
                    output: nil,
                    algo: DEFAULT_ALGO,
                    quality: nil,
                    min_ssim: nil,
                    mozjpeg_trellis: true,
                    mozjpeg_scan_opt: nil,
                    progressive: nil,
                    drop_alpha: nil,
                    exact_size: false,
                    subsampling: :auto,
                    tune: :hvs,
                    effort: :default,
                    execution: nil,
                    cancellable: false,
                    report: false,
                    strict: false)
  warn_if_scalar!
  validate_pixel_buffer!(buffer)
  validate_algo!(algo)
  validate_min_ssim!(min_ssim)
  validate_boolean!(:mozjpeg_trellis, mozjpeg_trellis)
  scan_opt_given = !mozjpeg_scan_opt.nil?
  mozjpeg_scan_opt = true unless scan_opt_given
  validate_boolean!(:mozjpeg_scan_opt, mozjpeg_scan_opt)
  progressive = default_progressive_for(algo, progressive)
  validate_boolean!(:progressive, progressive)
  validate_drop_alpha!(drop_alpha)
  validate_boolean!(:exact_size, exact_size)
  validate_boolean!(:cancellable, cancellable)
  validate_boolean!(:report, report)
  validate_boolean!(:strict, strict)
  subsampling = validate_subsampling!(subsampling)
  tune = validate_tune!(tune)
  effort = validate_effort!(effort)
  mozjpeg_scan_opt = false if effort == :fast && !scan_opt_given
  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,
                    report ? 1 : 0,
                    strict ? 1 : 0,
                    mozjpeg_scan_opt ? 1 : 0,
                    SUBSAMPLING_TO_NATIVE.fetch(subsampling),
                    TUNE_TO_NATIVE.fetch(tune),
                    EFFORT_TO_NATIVE.fetch(effort))
end

.configurationObject



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

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



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

def configure
  return configuration unless block_given?

  yield(configuration)
  configuration
end

.inspect_image(input) ⇒ Object



298
299
300
301
# File 'lib/image_pack.rb', line 298

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

.offload_safe?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/image_pack.rb', line 65

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

.optimize_bytes(bytes, **options) ⇒ Object



91
92
93
94
95
# File 'lib/image_pack.rb', line 91

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



97
98
99
100
101
102
# File 'lib/image_pack.rb', line 97

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, strip_icc: false, trim: false, execution: nil, cancellable: false, strict: false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
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
# File 'lib/image_pack.rb', line 104

def optimize_jpeg(input,
                  output: nil,
                  progressive: true,
                  strip_metadata: false,
                  strip_icc: false,
                  trim: false,
                  execution: nil,
                  cancellable: false,
                  strict: false)
  warn_if_scalar!
  validate_boolean!(:progressive, progressive)
  validate_boolean!(:strip_metadata, )
  validate_boolean!(:strip_icc, strip_icc)
  validate_boolean!(:trim, trim)
  validate_boolean!(:cancellable, cancellable)
  validate_boolean!(:strict, strict)
  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,
                  strict ? 1 : 0,
                  strip_icc ? 1 : 0,
                  trim ? 1 : 0)
end

.warn_if_scalar!Object



69
70
71
72
73
74
75
76
# File 'lib/image_pack.rb', line 69

def warn_if_scalar!
  return if !defined?(NATIVE_SIMD) || NATIVE_SIMD
  return if @scalar_warned

  @scalar_warned = true
  warn "ImagePack: this native build is scalar (no SIMD). On x86_64 install nasm " \
       "and reinstall the gem for ~3-4x faster JPEG encode/decode."
end