Skip to content

Commit 0277d4d

Browse files
committed
Merge branch 'ffi-experiment' of github.com:jcupitt/ruby-vips into ffi-experiment
2 parents 5d466d7 + 3e7aea1 commit 0277d4d

File tree

7 files changed

+104
-101
lines changed

7 files changed

+104
-101
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ env:
99
- PATH=$HOME/vips/bin:$PATH
1010
- LD_LIBRARY_PATH=$HOME/vips/lib:$LD_LIBRARY_PATH
1111
- PKG_CONFIG_PATH=$HOME/vips/lib/pkgconfig:$PKG_CONFIG_PATH
12-
- PYTHONPATH=$HOME/vips/lib/python2.7/site-packages:$PYTHONPATH
13-
- GI_TYPELIB_PATH=$HOME/vips/lib/girepository-1.0:$GI_TYPELIB_PATH
1412

1513
dist: trusty
1614

1715
addons:
1816
apt:
1917
packages:
20-
- libxml2-dev
18+
- libexpat1-dev
2119
- gettext
22-
- python-dev
2320
- liblcms1-dev
2421
- libmagickwand-dev
2522
- libopenexr-dev
@@ -41,7 +38,6 @@ cache:
4138

4239
language: ruby
4340
rvm:
44-
- 1.9
4541
- 2.0
4642
- 2.1
4743
- 2.2

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ which gives some more background.
4141
* OS X and Linux tested, Windows should work
4242
* libvips 8.2 or later
4343
* [ruby-ffi](https://github.com/ffi/ffi) 1.9 or later
44-
* Ruby or JRuby
44+
* Ruby 2.0+, JRuby should work
4545

4646
## Installation prerequisites
4747

@@ -95,7 +95,7 @@ im = Vips::Image.new_from_file filename
9595
# make the other pixels in the image by mirroring im up / down /
9696
# left / right, see
9797
# https://jcupitt.github.io/libvips/API/current/libvips-conversion.html#vips-embed
98-
im = im.embed 100, 100, 3000, 3000, :extend => :mirror
98+
im = im.embed 100, 100, 3000, 3000, extend: :mirror
9999

100100
# multiply the green (middle) band by 2, leave the other two alone
101101
im *= [1, 2, 1]
@@ -105,7 +105,7 @@ mask = Vips::Image.new_from_array [
105105
[-1, -1, -1],
106106
[-1, 16, -1],
107107
[-1, -1, -1]], 8
108-
im = im.conv mask
108+
im = im.conv mask, precision: :integer
109109

110110
# finally, write the result back to a file on disk
111111
im.write_to_file output_filename

lib/vips.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def self.set_log_domain domain
6161
# raise "usage: #{$PROGRAM_NAME}: input-file output-file"
6262
# end
6363
#
64-
# im = Vips::Image.new_from_file ARGV[0], :access => :sequential
64+
# im = Vips::Image.new_from_file ARGV[0], access: :sequential
6565
#
6666
# im *= [1, 2, 1]
6767
#
@@ -70,7 +70,7 @@ def self.set_log_domain domain
7070
# [-1, 16, -1],
7171
# [-1, -1, -1]
7272
# ], 8
73-
# im = im.conv mask
73+
# im = im.conv mask, precision: :integer
7474
#
7575
# im.write_to_file ARGV[1]
7676
# ```
@@ -81,7 +81,7 @@ def self.set_log_domain domain
8181
# Reading this example line by line, we have:
8282
#
8383
# ```ruby
84-
# im = Vips::Image.new_from_file ARGV[0], :access => :sequential
84+
# im = Vips::Image.new_from_file ARGV[0], access: :sequential
8585
# ```
8686
#
8787
# {Image.new_from_file} can load any image file supported by vips. In this
@@ -90,7 +90,9 @@ def self.set_log_domain domain
9090
# default mode is `:random`, this allows for full random access to image pixels,
9191
# but is slower and needs more memory. See {Access}
9292
# for full details
93-
# on the various modes available. You can also load formatted images from
93+
# on the various modes available.
94+
#
95+
# You can also load formatted images from
9496
# memory buffers, create images that wrap C-style memory arrays, or make images
9597
# from constants.
9698
#
@@ -112,13 +114,17 @@ def self.set_log_domain domain
112114
# [-1, 16, -1],
113115
# [-1, -1, -1]
114116
# ], 8
115-
# im = im.conv mask
117+
# im = im.conv mask, precision: :integer
116118
# ```
117119
#
118120
# {Image.new_from_array} creates an image from an array constant. The 8 at
119121
# the end sets the scale: the amount to divide the image by after
120-
# integer convolution. See the libvips API docs for `vips_conv()` (the operation
121-
# invoked by {Image#conv}) for details on the convolution operator.
122+
# integer convolution.
123+
#
124+
# See the libvips API docs for `vips_conv()` (the operation
125+
# invoked by {Image#conv}) for details on the convolution operator. By default,
126+
# it computes with a float mask, but `:integer` is fine for this case, and is
127+
# much faster.
122128
#
123129
# Finally:
124130
#
@@ -152,8 +158,10 @@ def self.set_log_domain domain
152158
# image is set to the value of `self`. Operations which do not take an input
153159
# image, such as {Image.black}, appear as class methods. The remainder of
154160
# the arguments you supply in the function call are used to set the other
155-
# required input arguments. If the final supplied argument is a hash, it is used
156-
# to set any optional input arguments. The result is the required output
161+
# required input arguments. Any trailing keyword arguments are used to set
162+
# options on the operation.
163+
#
164+
# The result is the required output
157165
# argument if there is only one result, or an array of values if the operation
158166
# produces several results. If the operation has optional output objects, they
159167
# are returned as a final hash.
@@ -169,7 +177,7 @@ def self.set_log_domain domain
169177
# You can ask it to return the position of the minimum with `:x` and `:y`.
170178
#
171179
# ```ruby
172-
# min_value, opts = min :x => true, :y => true
180+
# min_value, opts = min x: true, y: true
173181
# x_pos = opts['x']
174182
# y_pos = opts['y']
175183
# ```
@@ -180,7 +188,7 @@ def self.set_log_domain domain
180188
# You can also ask for the top *n* minimum, for example:
181189
#
182190
# ```ruby
183-
# min_value, opts = min :size => 10, :x_array => true, :y_array => true
191+
# min_value, opts = min size: 10, x_array: true, y_array: true
184192
# x_pos = opts['x_array']
185193
# y_pos = opts['y_array']
186194
# ```

lib/vips/image.rb

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,14 @@ def self.run_cmplx image, &block
9898
end
9999

100100
new_format = image.format == :double ? :dpcomplex : :complex
101-
image = image.copy :format => new_format,
102-
:bands => image.bands / 2
101+
image = image.copy format: new_format, bands: image.bands / 2
103102
end
104103

105104
image = block.(image)
106105

107106
if not Image::complex? original_format
108107
new_format = image.format == :dpcomplex ? :double : :float
109-
image = image.copy :format => new_format,
110-
:bands => image.bands * 2
108+
image = image.copy format: new_format, bands: image.bands * 2
111109
end
112110

113111
image
@@ -167,18 +165,26 @@ def inspect
167165
"#{interpretation}>"
168166
end
169167

170-
# Invoke a vips operation with {Vips::Operation::call}, using self as
168+
# To support keyword args, we need to tell Ruby that final image
169+
# arguments cannot be hashes of keywords.
170+
#
171+
# https://makandracards.com/makandra/36013-heads-up-ruby-implicitly-converts-a-hash-to-keyword-arguments
172+
def to_hash
173+
nil
174+
end
175+
176+
# Invoke a vips operation with {Vips::Operation.call}, using self as
171177
# the first input argument.
172178
#
173179
# @param name [String] vips operation to call
174180
# @return result of vips operation
175-
def method_missing name, *args
176-
Vips::Operation::call name.to_s, [self] + args
181+
def method_missing name, *args, **options
182+
Vips::Operation.call name.to_s, [self, *args], options
177183
end
178184

179-
# Invoke a vips operation with {Vips::Operation::call}.
180-
def self.method_missing name, *args
181-
Vips::Operation::call name.to_s, args
185+
# Invoke a vips operation with {Vips::Operation.call}.
186+
def self.method_missing name, *args, **options
187+
Vips::Operation.call name.to_s, args, options
182188
end
183189

184190
# Return a new {Image} for a file on disc. This method can load
@@ -192,7 +198,7 @@ def self.method_missing name, *args
192198
# You can also supply options as a hash, for example:
193199
#
194200
# ```
195-
# image = Vips::new_from_file "fred.jpg", :shrink => 2
201+
# image = Vips::new_from_file "fred.jpg", shrink: 2
196202
# ```
197203
#
198204
# The full set of options available depend upon the load operation that
@@ -227,7 +233,7 @@ def self.new_from_file name, opts = {}
227233
loader = Vips::vips_foreign_find_load filename
228234
raise Vips::Error if loader == nil
229235

230-
Operation::call loader, [filename, opts], option_string
236+
Operation.call loader, [filename], opts, option_string
231237
end
232238

233239
# Create a new {Image} for an image encoded, in a format such as
@@ -241,7 +247,7 @@ def self.new_from_file name, opts = {}
241247
# or alternatively:
242248
#
243249
# ```
244-
# image = Vips::new_from_from_buffer memory_buffer, "", :shrink => 2
250+
# image = Vips::new_from_from_buffer memory_buffer, "", shrink: 2
245251
# ```
246252
#
247253
# The options available depend on the file format. Try something like:
@@ -265,7 +271,7 @@ def self.new_from_buffer data, option_string, opts = {}
265271
loader = Vips::vips_foreign_find_load_buffer data, data.length
266272
raise Vips::Error if loader == nil
267273

268-
Vips::Operation::call loader, [data, opts], option_string
274+
Vips::Operation.call loader, [data], opts, option_string
269275
end
270276

271277
def self.matrix_from_array width, height, array
@@ -349,10 +355,9 @@ def self.new_from_array array, scale = 1, offset = 0
349355
# @return [Image] constant image
350356
def new_from_image value
351357
pixel = (Vips::Image.black(1, 1) + value).cast(format)
352-
image = pixel.embed 0, 0, width, height, :extend => :copy
353-
image.copy :interpretation => interpretation,
354-
:xres => xres, :yres => yres,
355-
:xoffset => xoffset, :yoffset => yoffset
358+
image = pixel.embed 0, 0, width, height, extend: :copy
359+
image.copy interpretation: interpretation,
360+
xres: xres, yres: yres, xoffset: xoffset, yoffset: yoffset
356361
end
357362

358363
# Write this image to a file. Save options may be encoded in the
@@ -365,7 +370,7 @@ def new_from_image value
365370
# or equivalently:
366371
#
367372
# ```
368-
# image.write_to_file "fred.jpg", :Q => 90
373+
# image.write_to_file "fred.jpg", Q: 90
369374
# ```
370375
#
371376
# The full set of save options depend on the selected saver. Try
@@ -392,7 +397,7 @@ def write_to_file name, opts = {}
392397
raise Vips::Error, "No known saver for '#{filename}'."
393398
end
394399

395-
Vips::Operation::call saver, [self, filename, opts]
400+
Vips::Operation.call saver, [self, filename], opts, option_string
396401

397402
write_gc
398403
end
@@ -407,7 +412,7 @@ def write_to_file name, opts = {}
407412
# or equivalently:
408413
#
409414
# ```
410-
# image.write_to_buffer ".jpg", :Q => 90
415+
# image.write_to_buffer ".jpg", Q: 90
411416
# ```
412417
#
413418
# The full set of save options depend on the selected saver. Try
@@ -430,7 +435,7 @@ def write_to_buffer format_string, opts = {}
430435
raise Vips::Error, "No known saver for '#{filename}'."
431436
end
432437

433-
buffer = Vips::Operation.call saver, [self, opts], option_string
438+
buffer = Vips::Operation.call saver, [self], opts, option_string
434439
raise Vips::Error if buffer == nil
435440

436441
write_gc
@@ -836,7 +841,7 @@ def [] index
836841
if index.is_a? Range
837842
n = index.end - index.begin
838843
n += 1 if not index.exclude_end?
839-
extract_band index.begin, :n => n
844+
extract_band index.begin, n: n
840845
elsif index.is_a? Numeric
841846
extract_band index
842847
else
@@ -953,7 +958,7 @@ def bandjoin other
953958
# @return [Real, Real, Real] maximum value, x coordinate of maximum, y
954959
# coordinate of maximum
955960
def maxpos
956-
v, opts = max :x => true, :y => true
961+
v, opts = max x: true, y: true
957962
x = opts['x']
958963
y = opts['y']
959964
return v, x, y
@@ -964,7 +969,7 @@ def maxpos
964969
# @return [Real, Real, Real] minimum value, x coordinate of minimum, y
965970
# coordinate of minimum
966971
def minpos
967-
v, opts = min :x => true, :y => true
972+
v, opts = min x: true, y: true
968973
x = opts['x']
969974
y = opts['y']
970975
return v, x, y
@@ -1194,13 +1199,13 @@ def ifthenelse(th, el, opts = {})
11941199
match_image = [th, el, self].find {|x| x.is_a? Vips::Image}
11951200

11961201
if not th.is_a? Vips::Image
1197-
th = Operation::imageize match_image, th
1202+
th = Operation.imageize match_image, th
11981203
end
11991204
if not el.is_a? Vips::Image
1200-
el = Operation::imageize match_image, el
1205+
el = Operation.imageize match_image, el
12011206
end
12021207

1203-
Vips::Operation.call "ifthenelse", [self, th, el, opts]
1208+
Vips::Operation.call "ifthenelse", [self, th, el], opts
12041209
end
12051210

12061211
# Scale an image to uchar. This is the vips `scale` operation, but

0 commit comments

Comments
 (0)