image_pack 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImagePack
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
data/lib/image_pack.rb CHANGED
@@ -46,6 +46,63 @@ module ImagePack
46
46
  configuration
47
47
  end
48
48
 
49
+ def build_info
50
+ {
51
+ version: VERSION,
52
+ mozjpeg: defined?(NATIVE_MOZJPEG_VERSION) ? NATIVE_MOZJPEG_VERSION : nil,
53
+ simd: defined?(NATIVE_SIMD) ? NATIVE_SIMD : nil
54
+ }
55
+ end
56
+
57
+ def compress_bytes(bytes, **options)
58
+ raise InvalidArgumentError, "bytes must be a String" unless bytes.is_a?(String)
59
+
60
+ compress(bytes.b, **options)
61
+ end
62
+
63
+ def compress_file(path, **options)
64
+ pathname = Pathname(path)
65
+ raise InvalidArgumentError, "input path does not exist: #{pathname}" unless pathname.file?
66
+
67
+ compress(pathname, **options)
68
+ end
69
+
70
+ def optimize_bytes(bytes, **options)
71
+ raise InvalidArgumentError, "bytes must be a String" unless bytes.is_a?(String)
72
+
73
+ optimize_jpeg(bytes.b, **options)
74
+ end
75
+
76
+ def optimize_file(path, **options)
77
+ pathname = Pathname(path)
78
+ raise InvalidArgumentError, "input path does not exist: #{pathname}" unless pathname.file?
79
+
80
+ optimize_jpeg(pathname, **options)
81
+ end
82
+
83
+ def optimize_jpeg(input,
84
+ output: nil,
85
+ progressive: true,
86
+ strip_metadata: false,
87
+ execution: nil,
88
+ cancellable: false)
89
+ execution ||= configuration.execution
90
+ validate_execution!(execution)
91
+ validate_cancellable!(:lossless_optimize, execution, cancellable)
92
+
93
+ normalized_input_kind = input_kind!(input)
94
+ normalized_output_kind = output_kind!(output)
95
+ has_scheduler = fiber_scheduler_active?
96
+
97
+ __optimize_jpeg(input, normalized_input_kind,
98
+ output, normalized_output_kind,
99
+ progressive ? 1 : 0,
100
+ strip_metadata ? 1 : 0,
101
+ execution,
102
+ cancellable ? 1 : 0,
103
+ has_scheduler ? 1 : 0)
104
+ end
105
+
49
106
  def compress(input,
50
107
  output: nil,
51
108
  algo: DEFAULT_ALGO,
@@ -118,29 +175,15 @@ module ImagePack
118
175
  normalized_output_kind = output_kind!(output)
119
176
  has_scheduler = fiber_scheduler_active?
120
177
 
121
- if min_ssim
122
- seed_jpeg = __compress_pixels(buffer,
123
- width.to_i, height.to_i, channels.to_i,
124
- nil, :return_string,
125
- ALGO_TO_NATIVE.fetch(algo), 95,
126
- progressive ? 1 : 0,
127
- :direct,
128
- 0,
129
- 0)
130
- return compress(seed_jpeg,
131
- output: output,
132
- algo: algo,
133
- quality: quality,
134
- min_ssim: min_ssim,
135
- progressive: progressive,
136
- execution: execution,
137
- cancellable: cancellable)
178
+ if min_ssim && channels.to_i == 4
179
+ raise UnsupportedError, "min_ssim is not supported for RGBA input"
138
180
  end
139
181
 
140
182
  __compress_pixels(buffer,
141
183
  width.to_i, height.to_i, channels.to_i,
142
184
  output, normalized_output_kind,
143
185
  ALGO_TO_NATIVE.fetch(algo), quality.to_i,
186
+ min_ssim ? min_ssim.to_f : 0.0,
144
187
  progressive ? 1 : 0,
145
188
  execution,
146
189
  cancellable ? 1 : 0,
@@ -161,7 +204,6 @@ module ImagePack
161
204
  elsif input.bytesize < 4096 && !input.include?("\0") && File.file?(input)
162
205
  :path
163
206
  else
164
- input.force_encoding(Encoding::ASCII_8BIT) unless input.frozen?
165
207
  :bytes
166
208
  end
167
209
  when Pathname
@@ -182,7 +224,7 @@ module ImagePack
182
224
  return :return_string if output.nil?
183
225
  return :path if output.is_a?(String) || output.is_a?(Pathname)
184
226
 
185
- raise InvalidArgumentError, "output must be nil, String path, or Pathname in v0.2.0"
227
+ raise InvalidArgumentError, "output must be nil, String path, or Pathname in v0.2.2"
186
228
  end
187
229
 
188
230
  def validate_algo!(algo)
@@ -223,14 +265,8 @@ module ImagePack
223
265
  raise InvalidArgumentError, "channels must be 1, 3 or 4" unless [1, 3, 4].include?(channels)
224
266
  end
225
267
 
226
- def validate_cancellable!(algo, execution, cancellable)
268
+ def validate_cancellable!(_algo, execution, cancellable)
227
269
  return unless cancellable
228
-
229
- if algo == :jpeg_turbo
230
- raise InvalidArgumentError,
231
- "cancellable: true is only supported with algo: :mozjpeg in v0.2.0"
232
- end
233
-
234
270
  return unless execution == :direct
235
271
 
236
272
  raise InvalidArgumentError,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_pack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Haydarov
@@ -402,7 +402,6 @@ files:
402
402
  - ext/image_pack/vendor/mozjpeg/wrtarga.c
403
403
  - ext/image_pack/vendor/mozjpeg/yuvjpeg.c
404
404
  - lib/image_pack.rb
405
- - lib/image_pack/backend.rb
406
405
  - lib/image_pack/configuration.rb
407
406
  - lib/image_pack/errors.rb
408
407
  - lib/image_pack/version.rb
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ImagePack
4
- module Backend
5
- JPEG_TURBO = :jpeg_turbo
6
- MOZJPEG = :mozjpeg
7
- end
8
- end