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 1613 1613: def initialize(*filenames, &block) 1614: @images = [] 1615: @scene = nil 1616: filenames.each { |f| 1617: Magick::Image.read(f, &block).each { |n| @images << n } 1618: } 1619: if length > 0 1620: @scene = length - 1 # last image in array 1621: end 1622: self 1623: end
# File lib/RMagick.rb, line 1371 1371: def *(n) 1372: unless n.kind_of? Integer 1373: Kernel.raise ArgumentError, "Integer required (#{n.class} given)" 1374: end 1375: current = get_current() 1376: ilist = self.class.new 1377: (@images * n).each {|image| ilist << image} 1378: ilist.set_current current 1379: return ilist 1380: end
# File lib/RMagick.rb, line 1382 1382: def <<(obj) 1383: is_an_image obj 1384: @images << obj 1385: @scene = @images.length - 1 1386: self 1387: 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 1394 1394: def <=>(other) 1395: unless other.kind_of? self.class 1396: Kernel.raise TypeError, "#{self.class} required (#{other.class} given)" 1397: end 1398: size = [self.length, other.length].min 1399: size.times do |x| 1400: r = self[x] <=> other[x] 1401: return r unless r == 0 1402: end 1403: if @scene.nil? && other.scene.nil? 1404: return 0 1405: elsif @scene.nil? && ! other.scene.nil? 1406: Kernel.raise TypeError, "cannot convert nil into #{other.scene.class}" 1407: elsif ! @scene.nil? && other.scene.nil? 1408: Kernel.raise TypeError, "cannot convert nil into #{self.scene.class}" 1409: end 1410: r = self.scene <=> other.scene 1411: return r unless r == 0 1412: return self.length <=> other.length 1413: end
# File lib/RMagick.rb, line 1415 1415: def [](*args) 1416: a = @images[*args] 1417: if a.respond_to?(:each) then 1418: ilist = self.class.new 1419: a.each {|image| ilist << image} 1420: a = ilist 1421: end 1422: return a 1423: end
# File lib/RMagick.rb, line 1425 1425: def []=(*args) 1426: obj = @images.[]=(*args) 1427: if obj && obj.respond_to?(:each) then 1428: is_an_image_array(obj) 1429: set_current obj.last.__id__ 1430: elsif obj 1431: is_an_image(obj) 1432: set_current obj.__id__ 1433: else 1434: set_current nil 1435: end 1436: return obj 1437: end
# File lib/RMagick.rb, line 1462 1462: def clone 1463: ditto = dup 1464: ditto.freeze if frozen? 1465: return ditto 1466: end
override Enumerable#collect
# File lib/RMagick.rb, line 1469 1469: def collect(&block) 1470: current = get_current() 1471: a = @images.collect(&block) 1472: ilist = self.class.new 1473: a.each {|image| ilist << image} 1474: ilist.set_current current 1475: return ilist 1476: end
# File lib/RMagick.rb, line 1478 1478: def collect!(&block) 1479: @images.collect!(&block) 1480: is_an_image_array @images 1481: self 1482: end
# File lib/RMagick.rb, line 1509 1509: def compact 1510: current = get_current() 1511: ilist = self.class.new 1512: a = @images.compact 1513: a.each {|image| ilist << image} 1514: ilist.set_current current 1515: return ilist 1516: end
# File lib/RMagick.rb, line 1518 1518: def compact! 1519: current = get_current() 1520: a = @images.compact! # returns nil if no changes were made 1521: set_current current 1522: return a.nil? ? nil : self 1523: end
# File lib/RMagick.rb, line 1525 1525: def concat(other) 1526: is_an_image_array other 1527: other.each {|image| @images << image} 1528: @scene = length-1 1529: return self 1530: end
Return the current image
# File lib/RMagick.rb, line 1494 1494: def cur_image 1495: if ! @scene 1496: Kernel.raise IndexError, "no images in this list" 1497: end 1498: @images[@scene] 1499: end
Set same delay for all images
# File lib/RMagick.rb, line 1533 1533: def delay=(d) 1534: if Integer(d) < 0 1535: raise ArgumentError, "delay must be greater than or equal to 0" 1536: end 1537: @images.each { |f| f.delay = Integer(d) } 1538: end
# File lib/RMagick.rb, line 1540 1540: def delete(obj, &block) 1541: is_an_image obj 1542: current = get_current() 1543: a = @images.delete(obj, &block) 1544: set_current current 1545: return a 1546: end
# File lib/RMagick.rb, line 1548 1548: def delete_at(ndx) 1549: current = get_current() 1550: a = @images.delete_at(ndx) 1551: set_current current 1552: return a 1553: end
# File lib/RMagick.rb, line 1555 1555: def delete_if(&block) 1556: current = get_current() 1557: @images.delete_if(&block) 1558: set_current current 1559: self 1560: end
# File lib/RMagick.rb, line 1562 1562: def dup 1563: ditto = self.class.new 1564: @images.each {|img| ditto << img} 1565: ditto.scene = @scene 1566: ditto.taint if tainted? 1567: return ditto 1568: end
# File lib/RMagick.rb, line 1570 1570: def eql?(other) 1571: is_an_image_array other 1572: eql = other.eql?(@images) 1573: begin # "other" is another ImageList 1574: eql &&= @scene == other.scene 1575: rescue NoMethodError 1576: # "other" is a plain Array 1577: end 1578: return eql 1579: end
# File lib/RMagick.rb, line 1581 1581: def fill(*args, &block) 1582: is_an_image args[0] unless block_given? 1583: current = get_current() 1584: @images.fill(*args, &block) 1585: is_an_image_array self 1586: set_current current 1587: self 1588: end
# File lib/RMagick.rb, line 1601 1601: def from_blob(*blobs, &block) 1602: if (blobs.length == 0) 1603: Kernel.raise ArgumentError, "no blobs given" 1604: end 1605: blobs.each { |b| 1606: Magick::Image.from_blob(b, &block).each { |n| @images << n } 1607: } 1608: @scene = length - 1 1609: self 1610: end
# File lib/RMagick.rb, line 1625 1625: def insert(index, *args) 1626: args.each {|image| is_an_image image} 1627: current = get_current() 1628: @images.insert(index, *args) 1629: set_current current 1630: return self 1631: end
Set the number of iterations of an animated GIF
# File lib/RMagick.rb, line 1641 1641: def iterations=(n) 1642: n = Integer(n) 1643: if n < 0 || n > 65535 1644: Kernel.raise ArgumentError, "iterations must be between 0 and 65535" 1645: end 1646: @images.each {|f| f.iterations=n} 1647: self 1648: end
# File lib/RMagick.rb, line 1650 1650: def last(*args) 1651: if args.length == 0 1652: a = @images.last 1653: else 1654: a = @images.last(*args) 1655: ilist = self.class.new 1656: a.each {|img| ilist << img} 1657: @scene = a.length - 1 1658: a = ilist 1659: end 1660: return a 1661: end
Custom marshal/unmarshal for Ruby 1.8.
# File lib/RMagick.rb, line 1664 1664: def marshal_dump() 1665: ary = [@scene] 1666: @images.each {|i| ary << Marshal.dump(i)} 1667: ary 1668: end
# File lib/RMagick.rb, line 1670 1670: def marshal_load(ary) 1671: @scene = ary.shift 1672: @images = [] 1673: ary.each {|a| @images << Marshal.load(a)} 1674: 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 1680 1680: def method_missing(methID, *args, &block) 1681: begin 1682: if @scene 1683: @images[@scene].send(methID, *args, &block) 1684: else 1685: super 1686: end 1687: rescue NoMethodError 1688: Kernel.raise NoMethodError, "undefined method `#{methID.id2name}' for #{self.class}" 1689: rescue Exception 1690: $@.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) } 1691: Kernel.raise 1692: end 1693: end
# File lib/RMagick.rb, line 1700 1700: def partition(&block) 1701: a = @images.partition(&block) 1702: t = self.class.new 1703: a[0].each { |img| t << img} 1704: t.set_current nil 1705: f = self.class.new 1706: a[1].each { |img| f << img} 1707: f.set_current nil 1708: [t, f] 1709: end
Ping files and concatenate the new images
# File lib/RMagick.rb, line 1712 1712: def ping(*files, &block) 1713: if (files.length == 0) 1714: Kernel.raise ArgumentError, "no files given" 1715: end 1716: files.each { |f| 1717: Magick::Image.ping(f, &block).each { |n| @images << n } 1718: } 1719: @scene = length - 1 1720: self 1721: end
# File lib/RMagick.rb, line 1723 1723: def pop 1724: current = get_current() 1725: a = @images.pop # can return nil 1726: set_current current 1727: return a 1728: end
# File lib/RMagick.rb, line 1730 1730: def push(*objs) 1731: objs.each do |image| 1732: is_an_image image 1733: @images << image 1734: end 1735: @scene = length - 1 1736: self 1737: end
Read files and concatenate the new images
# File lib/RMagick.rb, line 1740 1740: def read(*files, &block) 1741: if (files.length == 0) 1742: Kernel.raise ArgumentError, "no files given" 1743: end 1744: files.each { |f| 1745: Magick::Image.read(f, &block).each { |n| @images << n } 1746: } 1747: @scene = length - 1 1748: self 1749: end
# File lib/RMagick.rb, line 1761 1761: def reject!(&block) 1762: current = get_current() 1763: a = @images.reject!(&block) 1764: @images = a if !a.nil? 1765: set_current current 1766: return a.nil? ? nil : self 1767: end
# File lib/RMagick.rb, line 1769 1769: def replace(other) 1770: is_an_image_array other 1771: current = get_current() 1772: @images.clear 1773: other.each {|image| @images << image} 1774: @scene = self.length == 0 ? nil : 0 1775: set_current current 1776: self 1777: end
# File lib/RMagick.rb, line 1781 1781: def respond_to?(methID, priv=false) 1782: return true if __respond_to__?(methID, priv) 1783: if @scene 1784: @images[@scene].respond_to?(methID, priv) 1785: else 1786: super 1787: end 1788: end
# File lib/RMagick.rb, line 1790 1790: def reverse 1791: current = get_current() 1792: a = self.class.new 1793: @images.reverse_each {|image| a << image} 1794: a.set_current current 1795: return a 1796: end
# File lib/RMagick.rb, line 1798 1798: def reverse! 1799: current = get_current() 1800: @images.reverse! 1801: set_current current 1802: self 1803: end
# File lib/RMagick.rb, line 1805 1805: def reverse_each 1806: @images.reverse_each {|image| yield(image)} 1807: self 1808: end
Allow scene to be set to nil
# File lib/RMagick.rb, line 1331 1331: def scene=(n) 1332: if n.nil? 1333: Kernel.raise IndexError, "scene number out of bounds" unless @images.length == 0 1334: @scene = nil 1335: return @scene 1336: elsif @images.length == 0 1337: Kernel.raise IndexError, "scene number out of bounds" 1338: end 1339: 1340: n = Integer(n) 1341: if n < 0 || n > length - 1 1342: Kernel.raise IndexError, "scene number out of bounds" 1343: end 1344: @scene = n 1345: return @scene 1346: end
# File lib/RMagick.rb, line 1810 1810: def shift 1811: current = get_current() 1812: a = @images.shift 1813: set_current current 1814: return a 1815: end
# File lib/RMagick.rb, line 1817 1817: def slice(*args) 1818: current = get_current() 1819: slice = @images.slice(*args) 1820: if slice 1821: ilist = self.class.new 1822: if slice.respond_to?(:each) then 1823: slice.each {|image| ilist << image} 1824: else 1825: ilist << slice 1826: end 1827: else 1828: ilist = nil 1829: end 1830: return ilist 1831: end
# File lib/RMagick.rb, line 1833 1833: def slice!(*args) 1834: current = get_current() 1835: a = @images.slice!(*args) 1836: set_current current 1837: return a 1838: end
# File lib/RMagick.rb, line 1840 1840: def ticks_per_second=(t) 1841: if Integer(t) < 0 1842: Kernel.raise ArgumentError, "ticks_per_second must be greater than or equal to 0" 1843: end 1844: @images.each { |f| f.ticks_per_second = Integer(t) } 1845: end
# File lib/RMagick.rb, line 1847 1847: def to_a 1848: a = Array.new 1849: @images.each {|image| a << image} 1850: return a 1851: end
# File lib/RMagick.rb, line 1853 1853: def uniq 1854: current = get_current() 1855: a = self.class.new 1856: @images.uniq.each {|image| a << image} 1857: a.set_current current 1858: return a 1859: end
# File lib/RMagick.rb, line 1861 1861: def uniq!(*args) 1862: current = get_current() 1863: a = @images.uniq! 1864: set_current current 1865: return a.nil? ? nil : self 1866: end
# File lib/RMagick.rb, line 1876 1876: def values_at(*args) 1877: a = @images.values_at(*args) 1878: a = self.class.new 1879: @images.values_at(*args).each {|image| a << image} 1880: a.scene = a.length - 1 1881: return a 1882: end
# File lib/RMagick.rb, line 1288 1288: def is_an_image(obj) 1289: unless obj.kind_of? Magick::Image 1290: Kernel.raise ArgumentError, "Magick::Image required (#{obj.class} given)" 1291: end 1292: true 1293: end
Ensure array is always an array of Magick::Image objects
# File lib/RMagick.rb, line 1296 1296: def is_an_image_array(ary) 1297: unless ary.respond_to? :each 1298: Kernel.raise ArgumentError, "Magick::ImageList or array of Magick::Images required (#{ary.class} given)" 1299: end 1300: ary.each { |obj| is_an_image obj } 1301: true 1302: end
Find old current image, update scene number current is the id of the old current image.
# File lib/RMagick.rb, line 1306 1306: def set_current(current) 1307: if length() == 0 1308: self.scene = nil 1309: return 1310: # Don't bother looking for current image 1311: elsif scene() == nil || scene() >= length() 1312: self.scene = length() - 1 1313: return 1314: elsif current != nil 1315: # Find last instance of "current" in the list. 1316: # If "current" isn't in the list, set current to last image. 1317: self.scene = length() - 1 1318: each_with_index do |f,i| 1319: if f.__id__ == current 1320: self.scene = i 1321: end 1322: end 1323: return 1324: end 1325: self.scene = length() - 1 1326: end