ruby-webp 0.1.0

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.
File without changes
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Joel Bryan Juliano <joelbryan.juliano@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,56 @@
1
+ README for ruby-webp
2
+ =====================
3
+
4
+ Ruby-webp is a gem providing a ruby interface to Google WebP Codec.
5
+
6
+ To install, type 'gem install ruby-webp'
7
+
8
+ Usage:
9
+
10
+ require 'rubygems'
11
+ require 'ruby-webp'
12
+
13
+ # Encoder Example 1
14
+
15
+ cwebp = RubyWebP::Encoder.new
16
+
17
+ cwebp.compression_factor = 70
18
+ cwebp.input_file = "picture.png"
19
+ cwebp.output_file = "picture.webp"
20
+ cwebp.show_config
21
+ => "cwebp -q 70 picture.png -o picture.webp"
22
+ cwebp.encode
23
+
24
+ # Encoder Example 2
25
+
26
+ cwebp = RubyWebP::Encoder.new
27
+
28
+ cwebp.spatial_noise_shaping = 70
29
+ cwebp.deblocking_filter = 50
30
+ cwebp.strong_filtering = true
31
+ cwebp.auto_filter = true
32
+ cwebp.target_size = 60000
33
+ cwebp.input_file = "picture.png"
34
+ cwebp.output_file = "picture.webp"
35
+ cwebp.show_config
36
+ => "cwebp -sns 70 -f 50 -strong -af -size 60000 picture.png -o picture.webp"
37
+ cwebp.encode
38
+
39
+ # Decoder Example 1
40
+
41
+ dwebp = RubyWebP::Decoder.new
42
+ dwebp.input_file = "picture.webp"
43
+ dwebp.ouput_file = "output.png"
44
+ dwebp.show_config
45
+ => "dwebp picture.webp -o output.png"
46
+ dwebp.decode
47
+
48
+ # Decoder Example 2
49
+
50
+ dwebp = RubyWebP::Decoder.new
51
+ dwebp.input_file = "picture.webp"
52
+ dwebp.output_ppm = true
53
+ dwebp.output_file = "output.ppm"
54
+ dwebp.show_config
55
+ => "dwebp picture.webp -ppm -o output.ppm"
56
+ dwebp.decode
@@ -0,0 +1,12 @@
1
+ # = ruby-webp - A gem providing a ruby interface to Google WebP Codec.
2
+ #
3
+ # Homepage:: http://github.com/jjuliano/ruby-webp
4
+ # Author:: Joel Bryan Juliano
5
+ # Copyright:: (cc) 2011 Joel Bryan Juliano
6
+ # License:: MIT
7
+ #
8
+
9
+ require 'tempfile'
10
+
11
+ Dir[File.join(File.dirname(__FILE__), 'ruby-webp/**/*.rb')].sort.reverse.each { |lib| require lib }
12
+
@@ -0,0 +1,128 @@
1
+ # = ruby-webp - A gem providing a ruby interface to Google WebP Codec.
2
+ #
3
+ # Homepage:: http://github.com/jjuliano/ruby-webp
4
+ # Author:: Joel Bryan Juliano
5
+ # Copyright:: (cc) 2011 Joel Bryan Juliano
6
+ # License:: MIT
7
+ #
8
+
9
+ #
10
+ # class WebP::Decoder.new( array, str, array)
11
+ #
12
+ # Decompresses WebP image files into PNG, PGM or PPM images.
13
+ #
14
+ class WebP::Decoder
15
+
16
+ # Specify the name of the input file.
17
+ attr_accessor :input_file, :input
18
+
19
+ # Specify the name of the output file (as PNG format by default).
20
+ attr_accessor :output_file, :output, :o
21
+
22
+ # Change the output format to PPM.
23
+ attr_accessor :output_format_ppm, :output_ppm, :ppm
24
+
25
+ # Change the output format to PGM. The output consist of luma/chroma
26
+ # samples instead of RGB, using the ICM4 layout. This option is mainly for
27
+ # verification and debugging purpose.
28
+ attr_accessor :output_format_pgm, :output_pgm, :pgm
29
+
30
+ # Print extra information.
31
+ attr_accessor :verbose, :extra_information, :v
32
+
33
+ # Sets the executable path, otherwise the environment path will be used.
34
+ attr_accessor :path_to_dwebp
35
+
36
+ # Returns a new WebP::Decoder Object
37
+ def initialize()
38
+ end
39
+
40
+ # Print usage summary.
41
+ def help
42
+ execute_string("-h")
43
+ end
44
+
45
+ # Print the version number (as major.minor.revision) and exit.
46
+ def version
47
+ execute_string("-version")
48
+ end
49
+
50
+ def show_config
51
+ if (option_string()) == "dwebp "
52
+ raise "No options specified"
53
+ else
54
+ option_string()
55
+ end
56
+ end
57
+
58
+ # Execute and decode
59
+ def decode
60
+ execute_string(option_string())
61
+ end
62
+
63
+ alias :usage_summary :help
64
+ alias :run :decode
65
+ alias :start :decode
66
+ alias :show :show_config
67
+ alias :config :show_config
68
+
69
+ private
70
+
71
+ # Add a parameters to an execute string
72
+ def execute_string(param_string)
73
+ tmp = Tempfile.new('tmp')
74
+ command_string = option_string() + "#{param_string} " + " 2> " + tmp.path
75
+ success = system(command_string)
76
+ if success
77
+ begin
78
+ while (line = tmp.readline)
79
+ line.chomp
80
+ selected_string = line
81
+ end
82
+ rescue EOFError
83
+ tmp.close
84
+ end
85
+ return selected_string
86
+ else
87
+ tmp.close!
88
+ return success
89
+ end
90
+ end
91
+
92
+ def option_string()
93
+
94
+ unless @path_to_dwebp
95
+ ostring = "dwebp "
96
+ else
97
+ ostring = @path_to_dwebp + " "
98
+ end
99
+
100
+ if (@verbose || @extra_information || @v)
101
+ ostring += "-v "
102
+ end
103
+
104
+ if (@output_format_ppm || @output_ppm || @ppm)
105
+ ostring += "-ppm "
106
+ end
107
+
108
+ if (@output_format_pgm || @output_pgm || @pgm)
109
+ ostring += "-pgm "
110
+ end
111
+
112
+ if (params = (@input_file || @input))
113
+ case
114
+ when params.nil?:
115
+ raise "No input file specified."
116
+ end
117
+ ostring += params.to_s + " "
118
+ end
119
+
120
+ if (params = (@output_file || @output || @o))
121
+ ostring += "-o " + params.to_s + " "
122
+ end
123
+
124
+ return ostring
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,375 @@
1
+ # = ruby-webp - A gem providing a ruby interface to Google WebP Codec.
2
+ #
3
+ # Homepage:: http://github.com/jjuliano/ruby-webp
4
+ # Author:: Joel Bryan Juliano
5
+ # Copyright:: (cc) 2011 Joel Bryan Juliano
6
+ # License:: MIT
7
+ #
8
+
9
+ #
10
+ # class WebP::Encoder.new( array, str, array)
11
+ #
12
+ # Compresses image using the WebP format. Input format can be either
13
+ # PNG, JPEG, or raw Y'CbCr samples. When using PNG, the transparency
14
+ # information (alpha channel) is currently discarded.
15
+ #
16
+ class WebP::Encoder
17
+
18
+ # Specify the name of the input file.
19
+ attr_accessor :input_file, :input
20
+
21
+ # Specify the name of the output WebP file. If omitted, cwebp will
22
+ # perform compression but only report statistics.
23
+ attr_accessor :output_file, :output, :o
24
+
25
+ # Specify the compression factor between 0 and 100. A small factor
26
+ # produces smaller file with lower quality. Best quality is achieved
27
+ # using a value of 100. The default is 75.
28
+ attr_accessor :compression_factor, :q
29
+
30
+ # Specify the strength of the deblocking filter, between 0 (no filtering)
31
+ # and 100 (maximum filtering). A value of 0 will turn off any filtering.
32
+ # Higher value will increase the strength of the filtering process
33
+ # applied after decoding the picture. The higher the smoother the picture
34
+ # will appear. Typical values are usually in the range of 20 to 50.
35
+ attr_accessor :deblocking_filter, :f
36
+
37
+ # Specify a set of pre-defined parameters to suit a particular type of
38
+ # source material. Possible values are: default, photo, picture, drawing,
39
+ # icon, text.
40
+ attr_accessor :preset, :preset_default, :preset_photo, :preset_picture, :preset_drawing, :preset_icon, :preset_text
41
+
42
+ # Specify the amplitude of the spatial noise shaping. Spatial noise
43
+ # shaping (or sns for short) refers to a general collection of built-in
44
+ # algorithms used to decide which area of the picture should use
45
+ # relatively less bits, and where else to better transfer these bits. The
46
+ # possible range goes from 0 (algorithm is off) to 100 (the maximal
47
+ # effect). The default value is 80.
48
+ attr_accessor :spatial_noise_shaping, :sns
49
+
50
+ # Specify the compression method to use. This parameter controls the
51
+ # tradeoff between encoding speed and the compressed file size and
52
+ # quality. Possible values range from 0 to 6. Default value is 4. When
53
+ # higher values are used, the encoder will spend more time inspecting
54
+ # additional encoding possibilities and decide on the quality gain. Lower
55
+ # value can result is faster processing time at the expense of larger
56
+ # filesize and lower compression quality.
57
+ attr_accessor :compression_method, :method, :m
58
+
59
+ # Turns auto-filter on. This algorithm will spend additional time
60
+ # optimizing the filtering strength to reach a well-balanced quality.
61
+ attr_accessor :auto_filter, :af
62
+
63
+ # Specify the sharpness of the filtering (if used). Range is 0 (sharpest)
64
+ # to 7 (least sharp)Turns auto-filter on. This algorithm will spend
65
+ # additional time optimizing the filtering strength to reach a
66
+ # well-balanced quality.
67
+ attr_accessor :sharpness
68
+
69
+ # Use a stronger filtering than the default one (if filtering is being
70
+ # used thanks to the -f option). Strong filtering is off by default.
71
+ attr_accessor :strong_filtering, :strong
72
+
73
+ # Change the number of partitions to use during the segmentation of the
74
+ # sns algorithm. Segments should be in range 1 to 4. Default value is 4.
75
+ attr_accessor :segments
76
+
77
+ # Specify a target size (in bytes) to try and reach for the compressed
78
+ # output. Compressor will make several passes of partial encoding in
79
+ # order to get as close as possible to this target.
80
+ attr_accessor :target_size, :size
81
+
82
+ # Specify a target PSNR (in dB) to try and reach for the compressed output.
83
+ # Compressor will make several passes of partial encoding in order to get
84
+ # as close as possible to this target.
85
+ attr_accessor :peak_signal_to_noise_ratio, :psnr
86
+
87
+ # Set a maximum number of pass to use during the dichotomy used by options
88
+ # :size or :psnr. Maximum value is 10.
89
+ attr_accessor :pass
90
+
91
+ # Crop the source to a rectangle with top-left corner at coordinates
92
+ # (x_position, y_position) and size width x height. This cropping area must
93
+ # be fully contained within the source rectangle.
94
+ attr_accessor :crop
95
+
96
+ # Specify that the input file actually consists of raw Y'CbCr samples
97
+ # following the ITU-R BT.601 recommendation, in 4:2:0 linear format. The
98
+ # luma plane has size width x height.
99
+ attr_accessor :input_luma_plane_size, :luma_plane_size, :s
100
+
101
+ # Output additional ASCII-map of encoding information. Possible map values
102
+ # range from 1 to 6. This is only meant to help debugging.
103
+ attr_accessor :map
104
+
105
+ # Specify a pre-processing filter. This option is a placeholder and has
106
+ # currently no effect.
107
+ attr_accessor :pre_processing_filter, :pre
108
+
109
+ # Print extra information (encoding time in particular).
110
+ attr_accessor :verbose, :extra_information, :encoding_time, :v
111
+
112
+ # Do not print anything.
113
+ attr_accessor :quiet
114
+
115
+ # Only print brief information (output file size and PSNR) for testing
116
+ # purposes.
117
+ attr_accessor :brief_information, :short
118
+
119
+ # Sets the executable path, otherwise the environment path will be used.
120
+ attr_accessor :path_to_cwebp
121
+
122
+ # Returns a new WebP::Encoder Object
123
+ def initialize()
124
+ end
125
+
126
+ # A short usage summary.
127
+ def short_help
128
+ execute_string("-help")
129
+ end
130
+
131
+ # A summary of all the possible options.
132
+ def long_help
133
+ execute_string("-longhelp")
134
+ end
135
+
136
+ # Print the version number (as major.minor.revision) and exit.
137
+ def version
138
+ execute_string("-version")
139
+ end
140
+
141
+ def show_config
142
+ if (option_string()) == "cwebp "
143
+ raise "No options specified"
144
+ else
145
+ option_string()
146
+ end
147
+ end
148
+
149
+ # Execute and encode
150
+ def encode
151
+ execute_string(option_string())
152
+ end
153
+
154
+ alias :help :short_help
155
+ alias :shorthelp :short_help
156
+ alias :longhelp :long_help
157
+ alias :run :encode
158
+ alias :start :encode
159
+ alias :show :show_config
160
+ alias :config :show_config
161
+
162
+ private
163
+
164
+ # Add a parameters to an execute string
165
+ def execute_string(param_string)
166
+ tmp = Tempfile.new('tmp')
167
+ command_string = option_string() + "#{param_string} " + " 2> " + tmp.path
168
+ success = system(command_string)
169
+ if success
170
+ begin
171
+ while (line = tmp.readline)
172
+ line.chomp
173
+ selected_string = line
174
+ end
175
+ rescue EOFError
176
+ tmp.close
177
+ end
178
+ return selected_string
179
+ else
180
+ tmp.close!
181
+ return success
182
+ end
183
+ end
184
+
185
+ def option_string()
186
+
187
+ unless @path_to_cwebp
188
+ ostring = "cwebp "
189
+ else
190
+ ostring = @path_to_cwebp + " "
191
+ end
192
+
193
+ if (params = (@preset || @preset_default || @preset_photo || @preset_picture || @preset_drawing || @preset_icon || @preset_text))
194
+ case
195
+ when @preset_default:
196
+ params = "default"
197
+ when @preset_photo:
198
+ params = "photo"
199
+ when @preset_picture:
200
+ params = "picture"
201
+ when @preset_drawing:
202
+ params = "drawing"
203
+ when @preset_icon:
204
+ params = "icon"
205
+ when @preset_text:
206
+ params = "text"
207
+ end
208
+ ostring += "-preset " + params.to_s + " "
209
+ end
210
+
211
+ if (params = (@compression_factor || @q))
212
+ case
213
+ when (params =~ /[0-9]/).nil?:
214
+ raise "Specify the compression factor between 0 and 100."
215
+ when params > 100:
216
+ params = 100
217
+ end
218
+ ostring += "-q " + params.to_f.to_s + " "
219
+ end
220
+
221
+ if (params = (@deblocking_filter || @f))
222
+ case
223
+ when (params =~ /[0-9]/).nil?:
224
+ raise "Specify the strength of the deblocking filter, between 0 (no filtering) and 100 (maximum filtering)."
225
+ when params > 100:
226
+ params = 100
227
+ end
228
+ ostring += "-f " + params.to_i.to_s + " "
229
+ end
230
+
231
+ if (params = (@spatial_noise_shaping || @sns))
232
+ case
233
+ when (params =~ /[0-9]/).nil?:
234
+ raise "The possible range goes from 0 (algorithm is off) to 100 (the maximal effect)."
235
+ when params > 100:
236
+ params = 100
237
+ end
238
+ ostring += "-sns " + params.to_i.to_s + " "
239
+ end
240
+
241
+ if (params = (@compression_method || @method || @m))
242
+ case
243
+ when (params =~ /[0-9]/).nil?:
244
+ raise "Possible values range from 0 to 6."
245
+ when params > 6:
246
+ params = 6
247
+ end
248
+ ostring += "-m " + params.to_i.to_s + " "
249
+ end
250
+
251
+ if (@auto_filter || @af)
252
+ ostring += "-af "
253
+ end
254
+
255
+ if @sharpness
256
+ case
257
+ when (@sharpness =~ /[0-9]/).nil?:
258
+ raise "Range is 0 (sharpest) to 7 (least sharp)."
259
+ when @sharpness > 7:
260
+ @sharpness = 7
261
+ end
262
+ ostring += "-sharpness " + @sharpness.to_i.to_s + " "
263
+ end
264
+
265
+ if (@strong_filtering || @strong)
266
+ ostring += "-strong "
267
+ end
268
+
269
+ if @segments
270
+ case
271
+ when (@segments =~ /[0-9]/).nil?:
272
+ raise "Segments should be in range 1 to 4."
273
+ when @segments > 4:
274
+ @segments = 4
275
+ when @segments < 1:
276
+ @segments = 1
277
+ end
278
+ ostring += "-segments " + @segments.to_i.to_s + " "
279
+ end
280
+
281
+ if (params = (@target_size || @size))
282
+ case
283
+ when (params =~ /[0-9]/).nil?:
284
+ raise "Specify a target size (in bytes) to try and reach for the compressed output."
285
+ end
286
+ ostring += "-size " + params.to_i.to_s + " "
287
+ end
288
+
289
+ if (params = (@peak_signal_to_noise_ratio || @psnr))
290
+ case
291
+ when (params =~ /[0-9]/).nil?:
292
+ raise "Specify a target PSNR (in dB) to try and reach for the compressed output."
293
+ end
294
+ ostring += "-psnr " + params.to_f.to_s + " "
295
+ end
296
+
297
+ if @pass
298
+ case
299
+ when (@pass =~ /[0-9]/).nil?:
300
+ raise "Set a maximum number of pass to use during the dichotomy used by options @size or @psnr."
301
+ when @pass > 10:
302
+ @pass = 10
303
+ end
304
+ ostring += "-pass " + @pass.to_i.to_s + " "
305
+ end
306
+
307
+ if @crop
308
+ case
309
+ when (@crop.scan(/ /).size) > 3:
310
+ raise "Crop the source to a rectangle with top-left corner at coordinates (x_position, y_position) and size width x height. Crop must be 'x_position y_position width height' enclosed in string quotes."
311
+ end
312
+ ostring += "-crop " + @crop.to_s + " "
313
+ end
314
+
315
+ if (params = (@input_luma_plane_size || @luma_plane_size || @s))
316
+ case
317
+ when (params.scan(/ /).size) > 1:
318
+ raise "Specify that the input file actually consists of raw Y'CbCr samples following the ITU-R BT.601 recommendation, in 4:2:0 linear format. The luma plane has size width x height. Luma plane size must be 'width height' enclosed in string quotes."
319
+ end
320
+ ostring += "-s " + params.to_s + " "
321
+ end
322
+
323
+ if @map
324
+ case
325
+ when (@map =~ /[0-9]/).nil?:
326
+ raise "Possible map values range from 1 to 6."
327
+ when @map > 6:
328
+ @map = 6
329
+ when @map < 1:
330
+ @map = 1
331
+ end
332
+ ostring += "-map " + @map.to_i.to_s + " "
333
+ end
334
+
335
+ if (params = (@pre_processing_filter || @pre))
336
+ case
337
+ when (params =~ /[0-9]/).nil?:
338
+ raise "Specify a pre-processing filter."
339
+ end
340
+ ostring += "-pre " + params.to_i.to_s + " "
341
+ end
342
+
343
+ if (@verbose || @extra_information || @encoding_time || @v)
344
+ ostring += "-v "
345
+ end
346
+
347
+ if @quiet
348
+ ostring += "-quiet "
349
+ end
350
+
351
+ if (@brief_information || @short)
352
+ ostring += "-short "
353
+ end
354
+
355
+ if (params = (@input_file || @input))
356
+ case
357
+ when params.nil?:
358
+ raise "No input file specified."
359
+ end
360
+ ostring += params.to_s + " "
361
+ end
362
+
363
+ if (params = (@output_file || @output || @o))
364
+ case (params =~ /webp/)
365
+ when nil:
366
+ params << ".webp"
367
+ end
368
+ ostring += "-o " + params.to_s + " "
369
+ end
370
+
371
+ return ostring
372
+
373
+ end
374
+
375
+ end