| Class | Magick::ImageList |
| In: |
lib/RMagick.rb
|
| Parent: | Object |
| length | -> | size |
| remap | -> | affinity |
| ImageMagic used affinity in 6.4.3, switch to remap in 6.4.4. | ||
| respond_to? | -> | __respond_to__? |
| Ensure respond_to? answers correctly when we are delegating to Image | ||
| scene | [R] |
Initialize new instances
# File lib/RMagick.rb, line 1620
1620: def initialize(*filenames, &block)
1621: @images = []
1622: @scene = nil
1623: filenames.each { |f|
1624: Magick::Image.read(f, &block).each { |n| @images << n }
1625: }
1626: if length > 0
1627: @scene = length - 1 # last image in array
1628: end
1629: self
1630: end
# File lib/RMagick.rb, line 1378
1378: def *(n)
1379: unless n.kind_of? Integer
1380: Kernel.raise ArgumentError, "Integer required (#{n.class} given)"
1381: end
1382: current = get_current()
1383: ilist = self.class.new
1384: (@images * n).each {|image| ilist << image}
1385: ilist.set_current current
1386: return ilist
1387: end
# File lib/RMagick.rb, line 1389
1389: def <<(obj)
1390: is_an_image obj
1391: @images << obj
1392: @scene = @images.length - 1
1393: self
1394: 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
# File lib/RMagick.rb, line 1401
1401: def <=>(other)
1402: unless other.kind_of? self.class
1403: Kernel.raise TypeError, "#{self.class} required (#{other.class} given)"
1404: end
1405: size = [self.length, other.length].min
1406: size.times do |x|
1407: r = self[x] <=> other[x]
1408: return r unless r == 0
1409: end
1410: if @scene.nil? && other.scene.nil?
1411: return 0
1412: elsif @scene.nil? && ! other.scene.nil?
1413: Kernel.raise TypeError, "cannot convert nil into #{other.scene.class}"
1414: elsif ! @scene.nil? && other.scene.nil?
1415: Kernel.raise TypeError, "cannot convert nil into #{self.scene.class}"
1416: end
1417: r = self.scene <=> other.scene
1418: return r unless r == 0
1419: return self.length <=> other.length
1420: end
# File lib/RMagick.rb, line 1422
1422: def [](*args)
1423: a = @images[*args]
1424: if a.respond_to?(:each) then
1425: ilist = self.class.new
1426: a.each {|image| ilist << image}
1427: a = ilist
1428: end
1429: return a
1430: end
# File lib/RMagick.rb, line 1432
1432: def []=(*args)
1433: obj = @images.[]=(*args)
1434: if obj && obj.respond_to?(:each) then
1435: is_an_image_array(obj)
1436: set_current obj.last.__id__
1437: elsif obj
1438: is_an_image(obj)
1439: set_current obj.__id__
1440: else
1441: set_current nil
1442: end
1443: return obj
1444: end
# File lib/RMagick.rb, line 1469
1469: def clone
1470: ditto = dup
1471: ditto.freeze if frozen?
1472: return ditto
1473: end
override Enumerable#collect
# File lib/RMagick.rb, line 1476
1476: def collect(&block)
1477: current = get_current()
1478: a = @images.collect(&block)
1479: ilist = self.class.new
1480: a.each {|image| ilist << image}
1481: ilist.set_current current
1482: return ilist
1483: end
# File lib/RMagick.rb, line 1485
1485: def collect!(&block)
1486: @images.collect!(&block)
1487: is_an_image_array @images
1488: self
1489: end
# File lib/RMagick.rb, line 1516
1516: def compact
1517: current = get_current()
1518: ilist = self.class.new
1519: a = @images.compact
1520: a.each {|image| ilist << image}
1521: ilist.set_current current
1522: return ilist
1523: end
# File lib/RMagick.rb, line 1525
1525: def compact!
1526: current = get_current()
1527: a = @images.compact! # returns nil if no changes were made
1528: set_current current
1529: return a.nil? ? nil : self
1530: end
# File lib/RMagick.rb, line 1532
1532: def concat(other)
1533: is_an_image_array other
1534: other.each {|image| @images << image}
1535: @scene = length-1
1536: return self
1537: end
Return the current image
# File lib/RMagick.rb, line 1501
1501: def cur_image
1502: if ! @scene
1503: Kernel.raise IndexError, "no images in this list"
1504: end
1505: @images[@scene]
1506: end
Set same delay for all images
# File lib/RMagick.rb, line 1540
1540: def delay=(d)
1541: if Integer(d) < 0
1542: raise ArgumentError, "delay must be greater than or equal to 0"
1543: end
1544: @images.each { |f| f.delay = Integer(d) }
1545: end
# File lib/RMagick.rb, line 1547
1547: def delete(obj, &block)
1548: is_an_image obj
1549: current = get_current()
1550: a = @images.delete(obj, &block)
1551: set_current current
1552: return a
1553: end
# File lib/RMagick.rb, line 1555
1555: def delete_at(ndx)
1556: current = get_current()
1557: a = @images.delete_at(ndx)
1558: set_current current
1559: return a
1560: end
# File lib/RMagick.rb, line 1562
1562: def delete_if(&block)
1563: current = get_current()
1564: @images.delete_if(&block)
1565: set_current current
1566: self
1567: end
# File lib/RMagick.rb, line 1569
1569: def dup
1570: ditto = self.class.new
1571: @images.each {|img| ditto << img}
1572: ditto.scene = @scene
1573: ditto.taint if tainted?
1574: return ditto
1575: end
# File lib/RMagick.rb, line 1577
1577: def eql?(other)
1578: is_an_image_array other
1579: eql = other.eql?(@images)
1580: begin # "other" is another ImageList
1581: eql &&= @scene == other.scene
1582: rescue NoMethodError
1583: # "other" is a plain Array
1584: end
1585: return eql
1586: end
# File lib/RMagick.rb, line 1588
1588: def fill(*args, &block)
1589: is_an_image args[0] unless block_given?
1590: current = get_current()
1591: @images.fill(*args, &block)
1592: is_an_image_array self
1593: set_current current
1594: self
1595: end
# File lib/RMagick.rb, line 1608
1608: def from_blob(*blobs, &block)
1609: if (blobs.length == 0)
1610: Kernel.raise ArgumentError, "no blobs given"
1611: end
1612: blobs.each { |b|
1613: Magick::Image.from_blob(b, &block).each { |n| @images << n }
1614: }
1615: @scene = length - 1
1616: self
1617: end
# File lib/RMagick.rb, line 1632
1632: def insert(index, *args)
1633: args.each {|image| is_an_image image}
1634: current = get_current()
1635: @images.insert(index, *args)
1636: set_current current
1637: return self
1638: end
Set the number of iterations of an animated GIF
# File lib/RMagick.rb, line 1648
1648: def iterations=(n)
1649: n = Integer(n)
1650: if n < 0 || n > 65535
1651: Kernel.raise ArgumentError, "iterations must be between 0 and 65535"
1652: end
1653: @images.each {|f| f.iterations=n}
1654: self
1655: end
# File lib/RMagick.rb, line 1657
1657: def last(*args)
1658: if args.length == 0
1659: a = @images.last
1660: else
1661: a = @images.last(*args)
1662: ilist = self.class.new
1663: a.each {|img| ilist << img}
1664: @scene = a.length - 1
1665: a = ilist
1666: end
1667: return a
1668: end
Custom marshal/unmarshal for Ruby 1.8.
# File lib/RMagick.rb, line 1671
1671: def marshal_dump()
1672: ary = [@scene]
1673: @images.each {|i| ary << Marshal.dump(i)}
1674: ary
1675: end
# File lib/RMagick.rb, line 1677
1677: def marshal_load(ary)
1678: @scene = ary.shift
1679: @images = []
1680: ary.each {|a| @images << Marshal.load(a)}
1681: 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.
# File lib/RMagick.rb, line 1687
1687: def method_missing(methID, *args, &block)
1688: begin
1689: if @scene
1690: @images[@scene].send(methID, *args, &block)
1691: else
1692: super
1693: end
1694: rescue NoMethodError
1695: Kernel.raise NoMethodError, "undefined method `#{methID.id2name}' for #{self.class}"
1696: rescue Exception
1697: $@.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) }
1698: Kernel.raise
1699: end
1700: end
# File lib/RMagick.rb, line 1707
1707: def partition(&block)
1708: a = @images.partition(&block)
1709: t = self.class.new
1710: a[0].each { |img| t << img}
1711: t.set_current nil
1712: f = self.class.new
1713: a[1].each { |img| f << img}
1714: f.set_current nil
1715: [t, f]
1716: end
Ping files and concatenate the new images
# File lib/RMagick.rb, line 1719
1719: def ping(*files, &block)
1720: if (files.length == 0)
1721: Kernel.raise ArgumentError, "no files given"
1722: end
1723: files.each { |f|
1724: Magick::Image.ping(f, &block).each { |n| @images << n }
1725: }
1726: @scene = length - 1
1727: self
1728: end
# File lib/RMagick.rb, line 1730
1730: def pop
1731: current = get_current()
1732: a = @images.pop # can return nil
1733: set_current current
1734: return a
1735: end
# File lib/RMagick.rb, line 1737
1737: def push(*objs)
1738: objs.each do |image|
1739: is_an_image image
1740: @images << image
1741: end
1742: @scene = length - 1
1743: self
1744: end
Read files and concatenate the new images
# File lib/RMagick.rb, line 1747
1747: def read(*files, &block)
1748: if (files.length == 0)
1749: Kernel.raise ArgumentError, "no files given"
1750: end
1751: files.each { |f|
1752: Magick::Image.read(f, &block).each { |n| @images << n }
1753: }
1754: @scene = length - 1
1755: self
1756: end
# File lib/RMagick.rb, line 1768
1768: def reject!(&block)
1769: current = get_current()
1770: a = @images.reject!(&block)
1771: @images = a if !a.nil?
1772: set_current current
1773: return a.nil? ? nil : self
1774: end
# File lib/RMagick.rb, line 1776
1776: def replace(other)
1777: is_an_image_array other
1778: current = get_current()
1779: @images.clear
1780: other.each {|image| @images << image}
1781: @scene = self.length == 0 ? nil : 0
1782: set_current current
1783: self
1784: end
# File lib/RMagick.rb, line 1788
1788: def respond_to?(methID, priv=false)
1789: return true if __respond_to__?(methID, priv)
1790: if @scene
1791: @images[@scene].respond_to?(methID, priv)
1792: else
1793: super
1794: end
1795: end
# File lib/RMagick.rb, line 1797
1797: def reverse
1798: current = get_current()
1799: a = self.class.new
1800: @images.reverse_each {|image| a << image}
1801: a.set_current current
1802: return a
1803: end
# File lib/RMagick.rb, line 1805
1805: def reverse!
1806: current = get_current()
1807: @images.reverse!
1808: set_current current
1809: self
1810: end
# File lib/RMagick.rb, line 1812
1812: def reverse_each
1813: @images.reverse_each {|image| yield(image)}
1814: self
1815: end
Allow scene to be set to nil
# File lib/RMagick.rb, line 1338
1338: def scene=(n)
1339: if n.nil?
1340: Kernel.raise IndexError, "scene number out of bounds" unless @images.length == 0
1341: @scene = nil
1342: return @scene
1343: elsif @images.length == 0
1344: Kernel.raise IndexError, "scene number out of bounds"
1345: end
1346:
1347: n = Integer(n)
1348: if n < 0 || n > length - 1
1349: Kernel.raise IndexError, "scene number out of bounds"
1350: end
1351: @scene = n
1352: return @scene
1353: end
# File lib/RMagick.rb, line 1817
1817: def shift
1818: current = get_current()
1819: a = @images.shift
1820: set_current current
1821: return a
1822: end
# File lib/RMagick.rb, line 1824
1824: def slice(*args)
1825: current = get_current()
1826: slice = @images.slice(*args)
1827: if slice
1828: ilist = self.class.new
1829: if slice.respond_to?(:each) then
1830: slice.each {|image| ilist << image}
1831: else
1832: ilist << slice
1833: end
1834: else
1835: ilist = nil
1836: end
1837: return ilist
1838: end
# File lib/RMagick.rb, line 1840
1840: def slice!(*args)
1841: current = get_current()
1842: a = @images.slice!(*args)
1843: set_current current
1844: return a
1845: end
# File lib/RMagick.rb, line 1847
1847: def ticks_per_second=(t)
1848: if Integer(t) < 0
1849: Kernel.raise ArgumentError, "ticks_per_second must be greater than or equal to 0"
1850: end
1851: @images.each { |f| f.ticks_per_second = Integer(t) }
1852: end
# File lib/RMagick.rb, line 1854
1854: def to_a
1855: a = Array.new
1856: @images.each {|image| a << image}
1857: return a
1858: end
# File lib/RMagick.rb, line 1860
1860: def uniq
1861: current = get_current()
1862: a = self.class.new
1863: @images.uniq.each {|image| a << image}
1864: a.set_current current
1865: return a
1866: end
# File lib/RMagick.rb, line 1868
1868: def uniq!(*args)
1869: current = get_current()
1870: a = @images.uniq!
1871: set_current current
1872: return a.nil? ? nil : self
1873: end
# File lib/RMagick.rb, line 1883
1883: def values_at(*args)
1884: a = @images.values_at(*args)
1885: a = self.class.new
1886: @images.values_at(*args).each {|image| a << image}
1887: a.scene = a.length - 1
1888: return a
1889: end
# File lib/RMagick.rb, line 1295
1295: def is_an_image(obj)
1296: unless obj.kind_of? Magick::Image
1297: Kernel.raise ArgumentError, "Magick::Image required (#{obj.class} given)"
1298: end
1299: true
1300: end
Ensure array is always an array of Magick::Image objects
# File lib/RMagick.rb, line 1303
1303: def is_an_image_array(ary)
1304: unless ary.respond_to? :each
1305: Kernel.raise ArgumentError, "Magick::ImageList or array of Magick::Images required (#{ary.class} given)"
1306: end
1307: ary.each { |obj| is_an_image obj }
1308: true
1309: end
Find old current image, update scene number current is the id of the old current image.
# File lib/RMagick.rb, line 1313
1313: def set_current(current)
1314: if length() == 0
1315: self.scene = nil
1316: return
1317: # Don't bother looking for current image
1318: elsif scene() == nil || scene() >= length()
1319: self.scene = length() - 1
1320: return
1321: elsif current != nil
1322: # Find last instance of "current" in the list.
1323: # If "current" isn't in the list, set current to last image.
1324: self.scene = length() - 1
1325: each_with_index do |f,i|
1326: if f.__id__ == current
1327: self.scene = i
1328: end
1329: end
1330: return
1331: end
1332: self.scene = length() - 1
1333: end