lib/vips/image.rb in vips-8.8.0.3 vs lib/vips/image.rb in vips-8.8.2
- old
+ new
@@ -79,26 +79,24 @@
end
end
class Struct < Vips::Object::Struct
include ImageLayout
-
end
class ManagedStruct < Vips::Object::ManagedStruct
include ImageLayout
-
end
class GenericPtr < FFI::Struct
layout :value, :pointer
end
# handy for overloads ... want to be able to apply a function to an
# array or to a scalar
def self.smap x, &block
- x.is_a?(Array) ? x.map {|y| smap(y, &block)} : block.(x)
+ x.is_a?(Array) ? x.map { |y| smap(y, &block) } : block.(x)
end
def self.complex? format
format_number = GObject::GValue.from_nick BAND_FORMAT_TYPE, format
Vips::vips_band_format_iscomplex(format_number) != 0
@@ -176,19 +174,19 @@
end
public
def inspect
- "#<Image #{width}x#{height} #{format}, #{bands} bands, " +
- "#{interpretation}>"
+ "#<Image #{width}x#{height} #{format}, #{bands} bands, #{interpretation}>"
end
def respond_to? name, include_all = false
# To support keyword args, we need to tell Ruby that final image
# arguments cannot be hashes of keywords.
#
- # https://makandracards.com/makandra/36013-heads-up-ruby-implicitly-converts-a-hash-to-keyword-arguments
+ # https://makandracards.com/makandra/
+ # 36013-heads-up-ruby-implicitly-converts-a-hash-to-keyword-arguments
return false if name == :to_hash
# respond to all vips operations by nickname
return true if Vips::type_find("VipsOperation", name.to_s) != 0
@@ -345,23 +343,24 @@
end
if array[0].is_a? Array
height = array.length
width = array[0].length
- unless array.all? {|x| x.is_a? Array}
+ unless array.all? { |x| x.is_a? Array }
raise Vips::Error, "Not a 2D array."
end
- unless array.all? {|x| x.length == width}
+ unless array.all? { |x| x.length == width }
raise Vips::Error, "Array not rectangular."
end
+
array = array.flatten
else
height = 1
width = array.length
end
- unless array.all? {|x| x.is_a? Numeric}
+ unless array.all? { |x| x.is_a? Numeric }
raise Vips::Error, "Not all array elements are Numeric."
end
image = Vips::Image.matrix_from_array width, height, array
raise Vips::Error if image == nil
@@ -383,12 +382,12 @@
# @param value [Real, Array<Real>] value to put in each pixel
# @return [Image] constant image
def new_from_image value
pixel = (Vips::Image.black(1, 1) + value).cast(format)
image = pixel.embed 0, 0, width, height, extend: :copy
- image.copy interpretation: interpretation,
- xres: xres, yres: yres, xoffset: xoffset, yoffset: yoffset
+ image.copy interpretation: interpretation, xres: xres, yres: yres,
+ xoffset: xoffset, yoffset: yoffset
end
# Write this image to a file. Save options may be encoded in the
# filename or given as a hash. For example:
#
@@ -455,14 +454,12 @@
#
# @param format_string [String] save format plus options
# @macro vips.saveopts
# @return [String] the image saved in the specified format
def write_to_buffer format_string, **opts
- filename =
- Vips::p2str(Vips::vips_filename_get_filename format_string)
- option_string =
- Vips::p2str(Vips::vips_filename_get_options format_string)
+ filename = Vips::p2str(Vips::vips_filename_get_filename format_string)
+ option_string = Vips::p2str(Vips::vips_filename_get_options format_string)
saver = Vips::vips_foreign_find_save_buffer filename
if saver == nil
raise Vips::Error, "No known saver for '#{filename}'."
end
@@ -478,10 +475,11 @@
#
# @return [String] the pixels as a huge binary string
def write_to_memory
len = Vips::SizeStruct.new
ptr = Vips::vips_image_write_to_memory self, len
+ raise Vips::Error if ptr == nil
# wrap up as an autopointer
ptr = FFI::AutoPointer.new(ptr, GLib::G_FREE)
ptr.get_bytes 0, len[:value]
@@ -770,11 +768,11 @@
#
# @param other [Image, Real, Array<Real>] Thing to subtract from self
# @return [Image] result of subtraction
def - other
other.is_a?(Vips::Image) ?
- subtract(other) : linear(1, Image::smap(other) {|x| x * -1})
+ subtract(other) : linear(1, Image::smap(other) { |x| x * -1 })
end
# Multiply an image, constant or array.
#
# @param other [Image, Real, Array<Real>] Thing to multiply by self
@@ -788,11 +786,11 @@
#
# @param other [Image, Real, Array<Real>] Thing to divide self by
# @return [Image] result of division
def / other
other.is_a?(Vips::Image) ?
- divide(other) : linear(Image::smap(other) {|x| 1.0 / x}, 0)
+ divide(other) : linear(Image::smap(other) { |x| 1.0 / x }, 0)
end
# Remainder after integer division with an image, constant or array.
#
# @param other [Image, Real, Array<Real>] self modulo this
@@ -959,20 +957,20 @@
# as a Ruby array of the correct type
memory = write_to_memory
# make the template for unpack
template = {
- :char => 'c',
- :uchar => 'C',
- :short => 's_',
- :ushort => 'S_',
- :int => 'i_',
- :uint => 'I_',
- :float => 'f',
- :double => 'd',
- :complex => 'f',
- :dpcomplex => 'd'
+ char: 'c',
+ uchar: 'C',
+ short: 's_',
+ ushort: 'S_',
+ int: 'i_',
+ uint: 'I_',
+ float: 'f',
+ double: 'd',
+ complex: 'f',
+ dpcomplex: 'd'
}[format] + '*'
# and unpack into something like [1, 2, 3, 4 ..]
array = memory.unpack(template)
@@ -1029,11 +1027,11 @@
# Split an n-band image into n separate images.
#
# @return [Array<Image>] Array of n one-band images
def bandsplit
- (0...bands).map {|i| extract_band i}
+ (0...bands).map { |i| extract_band i }
end
# Join a set of images bandwise.
#
# @param other [Image, Array<Image>, Real, Array<Real>] bands to append
@@ -1042,11 +1040,11 @@
unless other.is_a? Array
other = [other]
end
# if other is just Numeric, we can use bandjoin_const
- not_all_real = !other.all?{|x| x.is_a? Numeric}
+ not_all_real = !other.all? { |x| x.is_a? Numeric }
if not_all_real
Vips::Image.bandjoin([self] + other)
else
bandjoin_const other
@@ -1128,11 +1126,11 @@
# bands are treated as (x, y) coordinates.
#
# @see xyz
# @return [Image] image converted to polar coordinates
def polar
- Image::run_cmplx(self) {|x| x.complex :polar}
+ Image::run_cmplx(self) { |x| x.complex :polar }
end
# Return an image with polar pixels converted to rectangular.
#
# The image
@@ -1141,11 +1139,11 @@
# bands are treated as (x, y) coordinates.
#
# @see xyz
# @return [Image] image converted to rectangular coordinates
def rect
- Image::run_cmplx(self) {|x| x.complex :rect}
+ Image::run_cmplx(self) { |x| x.complex :rect }
end
# Return the complex conjugate of an image.
#
# The image
@@ -1153,11 +1151,11 @@
# or must have an even number of bands, in which case pairs of
# bands are treated as (x, y) coordinates.
#
# @return [Image] complex conjugate
def conj
- Image::run_cmplx(self) {|x| x.complex :conj}
+ Image::run_cmplx(self) { |x| x.complex :conj }
end
# Calculate the cross phase of two images.
#
# @param other [Image, Real, Array<Real>] cross phase with this
@@ -1303,11 +1301,11 @@
# @param el [Image, Real, Array<Real>] false values
# @param opts [Hash] set of options
# @option opts [Boolean] :blend (false) Blend smoothly between th and el
# @return [Image] merged image
def ifthenelse(th, el, **opts)
- match_image = [th, el, self].find {|x| x.is_a? Vips::Image}
+ match_image = [th, el, self].find { |x| x.is_a? Vips::Image }
unless th.is_a? Vips::Image
th = Operation.imageize match_image, th
end
unless el.is_a? Vips::Image
@@ -1323,16 +1321,14 @@
# @param opts [Hash] Set of options
# @return [Vips::Image] Output image
def scaleimage **opts
Vips::Image.scale self, opts
end
-
end
end
module Vips
-
# This method generates yard comments for all the dynamically bound
# vips operations.
#
# Regenerate with something like:
#
@@ -1346,36 +1342,37 @@
# these have hand-written methods, see above
no_generate = ["scale", "bandjoin", "composite", "ifthenelse"]
# map gobject's type names to Ruby
map_go_to_ruby = {
- "gboolean" => "Boolean",
- "gint" => "Integer",
- "gdouble" => "Float",
- "gfloat" => "Float",
- "gchararray" => "String",
- "VipsImage" => "Vips::Image",
- "VipsInterpolate" => "Vips::Interpolate",
- "VipsArrayDouble" => "Array<Double>",
- "VipsArrayInt" => "Array<Integer>",
- "VipsArrayImage" => "Array<Image>",
- "VipsArrayString" => "Array<String>",
+ "gboolean" => "Boolean",
+ "gint" => "Integer",
+ "gdouble" => "Float",
+ "gfloat" => "Float",
+ "gchararray" => "String",
+ "VipsImage" => "Vips::Image",
+ "VipsInterpolate" => "Vips::Interpolate",
+ "VipsArrayDouble" => "Array<Double>",
+ "VipsArrayInt" => "Array<Integer>",
+ "VipsArrayImage" => "Array<Image>",
+ "VipsArrayString" => "Array<String>",
}
generate_operation = lambda do |gtype, nickname, op|
op_flags = op.get_flags
return if (op_flags & OPERATION_DEPRECATED) != 0
return if no_generate.include? nickname
+
description = Vips::vips_object_get_description op
# find and classify all the arguments the operator can take
required_input = []
optional_input = []
required_output = []
optional_output = []
member_x = nil
- op.argument_map do |pspec, argument_class, argument_instance|
+ op.argument_map do |pspec, argument_class, _argument_instance|
arg_flags = argument_class[:flags]
next if (arg_flags & ARGUMENT_CONSTRUCT) == 0
next if (arg_flags & ARGUMENT_DEPRECATED) != 0
name = pspec[:name].tr("-", "_")
@@ -1386,19 +1383,21 @@
type_name = GObject::g_type_name gtype
if map_go_to_ruby.include? type_name
type_name = map_go_to_ruby[type_name]
end
if fundamental == GObject::GFLAGS_TYPE ||
- fundamental == GObject::GENUM_TYPE
+ fundamental == GObject::GENUM_TYPE
type_name = "Vips::" + type_name[/Vips(.*)/, 1]
end
blurb = GObject::g_param_spec_get_blurb pspec
- value = {:name => name,
- :flags => arg_flags,
- :gtype => gtype,
- :type_name => type_name,
- :blurb => blurb}
+ value = {
+ name: name,
+ flags: arg_flags,
+ gtype: gtype,
+ type_name: type_name,
+ blurb: blurb
+ }
if (arg_flags & ARGUMENT_INPUT) != 0
if (arg_flags & ARGUMENT_REQUIRED) != 0
# note the first required input image, if any ... we
# will be a method of this instance
@@ -1412,39 +1411,37 @@
end
end
# MODIFY INPUT args count as OUTPUT as well
if (arg_flags & ARGUMENT_OUTPUT) != 0 ||
- ((arg_flags & ARGUMENT_INPUT) != 0 &&
- (arg_flags & ARGUMENT_MODIFY) != 0)
+ ((arg_flags & ARGUMENT_INPUT) != 0 &&
+ (arg_flags & ARGUMENT_MODIFY) != 0)
if (arg_flags & ARGUMENT_REQUIRED) != 0
required_output << value
else
optional_output << value
end
end
-
end
print "# @!method "
print "self." unless member_x
print "#{nickname}("
- print required_input.map{|x| x[:name]}.join(", ")
+ print required_input.map { |x| x[:name] }.join(", ")
print ", " if required_input.length > 0
puts "**opts)"
puts "# #{description.capitalize}."
required_input.each do |arg|
- puts "# @param #{arg[:name]} [#{arg[:type_name]}] " +
- "#{arg[:blurb]}"
+ puts "# @param #{arg[:name]} [#{arg[:type_name]}] #{arg[:blurb]}"
end
puts "# @param opts [Hash] Set of options"
optional_input.each do |arg|
puts "# @option opts [#{arg[:type_name]}] :#{arg[:name]} " +
- "#{arg[:blurb]}"
+ "#{arg[:blurb]}"
end
optional_output.each do |arg|
print "# @option opts [#{arg[:type_name]}] :#{arg[:name]}"
puts " Output #{arg[:blurb]}"
end
@@ -1452,37 +1449,38 @@
print "# @return ["
if required_output.length == 0
print "nil"
elsif required_output.length == 1
print required_output.first[:type_name]
- elsif
- print "Array<"
- print required_output.map{|x| x[:type_name]}.join(", ")
+ else
+ print "Array<"
+ print required_output.map { |x| x[:type_name] }.join(", ")
print ">"
end
if optional_output.length > 0
print ", Hash<Symbol => Object>"
end
print "] "
- print required_output.map{|x| x[:blurb]}.join(", ")
+ print required_output.map { |x| x[:blurb] }.join(", ")
if optional_output.length > 0
print ", " if required_output.length > 0
print "Hash of optional output items"
end
puts ""
puts ""
end
- generate_class = lambda do |gtype, a|
+ generate_class = lambda do |gtype, _|
nickname = Vips::nickname_find gtype
if nickname
begin
# can fail for abstract types
op = Vips::Operation.new nickname
- rescue
+ rescue Vips::Error
+ nil
end
generate_operation.(gtype, nickname, op) if op
end
@@ -1496,7 +1494,6 @@
generate_class.(GObject::g_type_from_name("VipsOperation"), nil)
puts " end"
puts "end"
end
-
end