class Magick::ImageList
Attributes
Public Class Methods
Source
# File lib/rmagick_internal.rb, line 1594 def initialize(*filenames, &block) @images = [] @scene = nil filenames.each do |f| Magick::Image.read(f, &block).each { |n| @images << n } end @scene = length - 1 if length > 0 # last image in array end
Initialize new instances
Public Instance Methods
Source
# File lib/rmagick_internal.rb, line 1372 def *(other) Kernel.raise ArgumentError, "Integer required (#{other.class} given)" unless other.is_a? Integer current = get_current ilist = self.class.new (@images * other).each { |image| ilist << image } ilist.set_current current ilist end
Source
# File lib/rmagick_internal.rb, line 1381 def <<(obj) assert_image obj @images << obj @scene = @images.length - 1 self end
Source
# File lib/rmagick_internal.rb, line 1393 def <=>(other) return unless other.is_a? self.class size = [length, other.length].min size.times do |x| r = self[x] <=> other[x] return r unless r.zero? end return 0 if @scene.nil? && other.scene.nil? return if @scene.nil? && !other.scene.nil? return if !@scene.nil? && other.scene.nil? r = scene <=> other.scene return r unless r.zero? length <=> other.length end
Compare ImageLists Compare each image in turn until the result of a comparison is not 0. If all comparisons return 0, then
return if A.scene != B.scene return A.length <=> B.length
Source
# File lib/rmagick_internal.rb, line 1412 def [](*args) a = @images[*args] if a.respond_to?(:each) ilist = self.class.new a.each { |image| ilist << image } a = ilist end a end
Source
# File lib/rmagick_internal.rb, line 1422 def []=(*args) obj = @images.[]=(*args) if obj.respond_to?(:each) assert_image_array(obj) set_current obj.last.__id__ elsif obj assert_image(obj) set_current obj.__id__ else set_current nil end end
Ensure respond_to? answers correctly when we are delegating to Image
Source
# File lib/rmagick_internal.rb, line 1458 def collect(&) current = get_current a = @images.map(&) ilist = self.class.new a.each { |image| ilist << image } ilist.set_current current ilist end
override Enumerable#collect
Source
# File lib/rmagick_internal.rb, line 1467 def collect!(&) @images.map!(&) assert_image_array @images self end
Source
# File lib/rmagick_internal.rb, line 1496 def compact current = get_current ilist = self.class.new a = @images.compact a.each { |image| ilist << image } ilist.set_current current ilist end
Source
# File lib/rmagick_internal.rb, line 1505 def compact! current = get_current a = @images.compact! # returns nil if no changes were made set_current current a.nil? ? nil : self end
Source
# File lib/rmagick_internal.rb, line 1512 def concat(other) assert_image_array other other.each { |image| @images << image } @scene = length - 1 self end
Source
# File lib/rmagick_internal.rb, line 1474 def copy ditto = self.class.new @images.each { |f| ditto << f.copy } ditto.scene = @scene ditto end
Make a deep copy
Source
# File lib/rmagick_internal.rb, line 1482 def cur_image Kernel.raise IndexError, 'no images in this list' unless @scene @images[@scene] end
Return the current image
Source
# File lib/rmagick_internal.rb, line 1520 def delay=(d) raise ArgumentError, 'delay must be greater than or equal to 0' if Integer(d) < 0 @images.each { |f| f.delay = Integer(d) } end
Set same delay for all images
Source
# File lib/rmagick_internal.rb, line 1526 def delete(obj, &) assert_image obj current = get_current a = @images.delete(obj, &) set_current current a end
Source
# File lib/rmagick_internal.rb, line 1534 def delete_at(ndx) current = get_current a = @images.delete_at(ndx) set_current current a end
Source
# File lib/rmagick_internal.rb, line 1541 def delete_if(&) current = get_current @images.delete_if(&) set_current current self end
Source
# File lib/rmagick_internal.rb, line 1859 def destroy! @images.each(&:destroy!) self end
Source
# File lib/rmagick_internal.rb, line 1864 def destroyed? @images.all?(&:destroyed?) end
Source
# File lib/rmagick_internal.rb, line 1548 def eql?(other) begin assert_image_array other rescue ArgumentError return false end eql = other.eql?(@images) begin # "other" is another ImageList eql &&= @scene == other.scene rescue NoMethodError # "other" is a plain Array end eql end
Source
# File lib/rmagick_internal.rb, line 1564 def fill(*args, &block) assert_image args[0] unless block current = get_current @images.fill(*args, &block) assert_image_array self set_current current self end
Source
# File lib/rmagick_internal.rb, line 1574 def find_all(&) current = get_current a = @images.select(&) ilist = self.class.new a.each { |image| ilist << image } ilist.set_current current ilist end
Override Enumerable’s find_all
Source
# File lib/rmagick_internal.rb, line 1584 def from_blob(*blobs, &block) Kernel.raise ArgumentError, 'no blobs given' if blobs.empty? blobs.each do |b| Magick::Image.from_blob(b, &block).each { |n| @images << n } end @scene = length - 1 self end
Source
# File lib/rmagick_internal.rb, line 1606 def initialize_copy(other) super @images = @images.dup end
Called by clone and dup, after the instance variables are copied. The copy has its own array of the same images.
Source
# File lib/rmagick_internal.rb, line 1611 def insert(index, *args) args.each { |image| assert_image image } current = get_current @images.insert(index, *args) set_current current self end
Source
# File lib/rmagick_internal.rb, line 1620 def inspect img = @images.map(&:inspect) '[' + img.join(",\n") + "]\nscene=#{@scene}" end
Call inspect for all the images
Source
# File lib/rmagick_internal.rb, line 1626 def iterations=(n) n = Integer(n) Kernel.raise ArgumentError, 'iterations must be between 0 and 65535' if n < 0 || n > 65_535 @images.each { |f| f.iterations = n } end
Set the number of iterations of an animated GIF
Source
# File lib/rmagick_internal.rb, line 1632 def last(*args) if args.empty? a = @images.last else a = @images.last(*args) ilist = self.class.new a.each { |img| ilist << img } @scene = a.length - 1 a = ilist end a end
Source
# File lib/rmagick_internal.rb, line 1646 def marshal_dump ary = [@scene] @images.each { |i| ary << Marshal.dump(i) } ary end
Custom marshal/unmarshal for Ruby 1.8.
Source
# File lib/rmagick_internal.rb, line 1652 def marshal_load(ary) images = ary.drop(1).map! { |a| Marshal.load(a) } # rubocop:disable Security/MarshalLoad @scene = ary.first @images = images end
Source
# File lib/rmagick_internal.rb, line 1662 def method_missing(meth_id, *args, &) if @scene img = @images[@scene] new_img = img.public_send(meth_id, *args, &) img.equal?(new_img) ? self : new_img else super end rescue NoMethodError Kernel.raise NoMethodError, "undefined method `#{meth_id.id2name}' for #{self.class}" rescue StandardError $ERROR_POSITION.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) } Kernel.raise end
The ImageList class supports the Magick::Image class methods by simply sending the method to the current image. If the method isn’t explicitly supported, send it to the current image in the array. If there are no images, send it up the line. Catch a NameError and emit a useful message.
Source
# File lib/rmagick_internal.rb, line 1678 def new_image(cols, rows, *fill, &) self << Magick::Image.new(cols, rows, *fill, &) end
Create a new image and add it to the end
Source
# File lib/rmagick_internal.rb, line 1682 def partition(&) a = @images.partition(&) t = self.class.new a[0].each { |img| t << img } t.set_current nil f = self.class.new a[1].each { |img| f << img } f.set_current nil [t, f] end
Source
# File lib/rmagick_internal.rb, line 1694 def ping(*files, &block) Kernel.raise ArgumentError, 'no files given' if files.empty? files.each do |f| Magick::Image.ping(f, &block).each { |n| @images << n } end @scene = length - 1 self end
Ping files and concatenate the new images
Source
# File lib/rmagick_internal.rb, line 1703 def pop current = get_current a = @images.pop # can return nil set_current current a end
Source
# File lib/rmagick_internal.rb, line 1710 def push(*objs) objs.each do |image| assert_image image @images << image end @scene = length - 1 self end
Source
# File lib/rmagick_internal.rb, line 1720 def read(*files, &block) Kernel.raise ArgumentError, 'no files given' if files.empty? files.each do |f| Magick::Image.read(f, &block).each { |n| @images << n } end @scene = length - 1 self end
Read files and concatenate the new images
Source
# File lib/rmagick_internal.rb, line 1730 def reject(&) current = get_current ilist = self.class.new a = @images.reject(&) a.each { |image| ilist << image } ilist.set_current current ilist end
override Enumerable’s reject
Source
# File lib/rmagick_internal.rb, line 1739 def reject!(&) current = get_current a = @images.reject!(&) @images = a unless a.nil? set_current current a.nil? ? nil : self end
Source
# File lib/rmagick_internal.rb, line 1747 def replace(other) assert_image_array other current = get_current @images.clear other.each { |image| @images << image } @scene = length.zero? ? nil : 0 set_current current self end
Source
# File lib/rmagick_internal.rb, line 1759 def respond_to?(meth_id, priv = false) return true if __respond_to__?(meth_id, priv) if @scene @images[@scene].respond_to?(meth_id, priv) else super end end
Source
# File lib/rmagick_internal.rb, line 1769 def reverse current = get_current a = self.class.new @images.reverse_each { |image| a << image } a.set_current current a end
Source
# File lib/rmagick_internal.rb, line 1777 def reverse! current = get_current @images.reverse! set_current current self end
Source
# File lib/rmagick_internal.rb, line 1784 def reverse_each(&) @images.reverse_each(&) self end
Source
# File lib/rmagick_internal.rb, line 1342 def scene=(n) if n.nil? Kernel.raise IndexError, 'scene number out of bounds' unless @images.empty? @scene = nil return elsif @images.empty? Kernel.raise IndexError, 'scene number out of bounds' end n = Integer(n) Kernel.raise IndexError, 'scene number out of bounds' if n < 0 || n > length - 1 @scene = n end
Allow scene to be set to nil
Source
# File lib/rmagick_internal.rb, line 1789 def shift current = get_current a = @images.shift set_current current a end
Source
# File lib/rmagick_internal.rb, line 1796 def slice(*args) slice = @images.slice(*args) if slice ilist = self.class.new if slice.respond_to?(:each) slice.each { |image| ilist << image } else ilist << slice end else ilist = nil end ilist end
Source
# File lib/rmagick_internal.rb, line 1811 def slice!(*args) current = get_current a = @images.slice!(*args) set_current current a end
Source
# File lib/rmagick_internal.rb, line 1447 def sort!(*args, &) @images.sort!(*args, &) self end
Source
# File lib/rmagick_internal.rb, line 1818 def ticks_per_second=(t) Kernel.raise ArgumentError, 'ticks_per_second must be greater than or equal to 0' if Integer(t) < 0 @images.each { |f| f.ticks_per_second = Integer(t) } end
Source
# File lib/rmagick_internal.rb, line 1827 def uniq current = get_current a = self.class.new @images.uniq.each { |image| a << image } a.set_current current a end
Source
# File lib/rmagick_internal.rb, line 1835 def uniq!(*_args) current = get_current a = @images.uniq! set_current current a.nil? ? nil : self end
Source
# File lib/rmagick_internal.rb, line 1843 def unshift(obj) assert_image obj @images.unshift(obj) @scene = 0 self end
@scene -> new object
Source
# File lib/rmagick_internal.rb, line 1850 def values_at(*args) a = self.class.new @images.values_at(*args).each { |image| a << image } a.scene = a.length - 1 a end
Protected Instance Methods
Source
# File lib/rmagick_internal.rb, line 1307 def assert_image(obj) Kernel.raise ArgumentError, "Magick::Image required (#{obj.class} given)" unless obj.is_a? Magick::Image end
Source
# File lib/rmagick_internal.rb, line 1312 def assert_image_array(ary) Kernel.raise ArgumentError, "Magick::ImageList or array of Magick::Images required (#{ary.class} given)" unless ary.respond_to? :each ary.each { |obj| assert_image obj } end
Ensure array is always an array of Magick::Image objects
Source
# File lib/rmagick_internal.rb, line 1319 def set_current(current) if length.zero? self.scene = nil return # Don't bother looking for current image elsif scene.nil? || scene >= length self.scene = length - 1 return elsif !current.nil? # Find last instance of "current" in the list. # If "current" isn't in the list, set current to last image. self.scene = length - 1 each_with_index do |f, i| self.scene = i if f.__id__ == current end return end self.scene = length - 1 end
Find old current image, update scene number current is the id of the old current image.
Private Instance Methods
Source
# File lib/rmagick_internal.rb, line 1299 def get_current @images[@scene].__id__ rescue StandardError nil end