Class | Magick::ImageList |
In: |
lib/RMagick.rb
|
Parent: | Object |
length | -> | size |
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 1577 1577: def initialize(*filenames) 1578: @images = [] 1579: @scene = nil 1580: filenames.each { |f| 1581: Magick::Image.read(f).each { |n| @images << n } 1582: } 1583: if length > 0 1584: @scene = length - 1 # last image in array 1585: end 1586: self 1587: end
# File lib/RMagick.rb, line 1345 1345: def *(n) 1346: unless n.kind_of? Integer 1347: Kernel.raise ArgumentError, "Integer required (#{n.class} given)" 1348: end 1349: current = get_current() 1350: ilist = self.class.new 1351: (@images * n).each {|image| ilist << image} 1352: ilist.set_current current 1353: return ilist 1354: end
# File lib/RMagick.rb, line 1356 1356: def <<(obj) 1357: is_an_image obj 1358: @images << obj 1359: @scene = @images.length - 1 1360: self 1361: 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 1368 1368: def <=>(other) 1369: unless other.kind_of? self.class 1370: Kernel.raise TypeError, "#{self.class} required (#{other.class} given)" 1371: end 1372: size = [self.length, other.length].min 1373: size.times do |x| 1374: r = self[x] <=> other[x] 1375: return r unless r == 0 1376: end 1377: if @scene.nil? && other.scene.nil? 1378: return 0 1379: elsif @scene.nil? && ! other.scene.nil? 1380: Kernel.raise TypeError, "cannot convert nil into #{other.scene.class}" 1381: elsif ! @scene.nil? && other.scene.nil? 1382: Kernel.raise TypeError, "cannot convert nil into #{self.scene.class}" 1383: end 1384: r = self.scene <=> other.scene 1385: return r unless r == 0 1386: return self.length <=> other.length 1387: end
# File lib/RMagick.rb, line 1389 1389: def [](*args) 1390: a = @images[*args] 1391: if a.respond_to?(:each) then 1392: ilist = self.class.new 1393: a.each {|image| ilist << image} 1394: a = ilist 1395: end 1396: return a 1397: end
# File lib/RMagick.rb, line 1399 1399: def []=(*args) 1400: obj = @images.[]=(*args) 1401: if obj && obj.respond_to?(:each) then 1402: is_an_image_array(obj) 1403: set_current obj.last.__id__ 1404: elsif obj 1405: is_an_image(obj) 1406: set_current obj.__id__ 1407: else 1408: set_current nil 1409: end 1410: return obj 1411: end
# File lib/RMagick.rb, line 1429 1429: def clone 1430: ditto = dup 1431: ditto.freeze if frozen? 1432: return ditto 1433: end
override Enumerable#collect
# File lib/RMagick.rb, line 1436 1436: def collect(&block) 1437: current = get_current() 1438: a = @images.collect(&block) 1439: ilist = self.class.new 1440: a.each {|image| ilist << image} 1441: ilist.set_current current 1442: return ilist 1443: end
# File lib/RMagick.rb, line 1445 1445: def collect!(&block) 1446: @images.collect!(&block) 1447: is_an_image_array @images 1448: self 1449: end
# File lib/RMagick.rb, line 1473 1473: def compact 1474: current = get_current() 1475: ilist = self.class.new 1476: a = @images.compact 1477: a.each {|image| ilist << image} 1478: ilist.set_current current 1479: return ilist 1480: end
# File lib/RMagick.rb, line 1482 1482: def compact! 1483: current = get_current() 1484: a = @images.compact! # returns nil if no changes were made 1485: set_current current 1486: return a.nil? ? nil : self 1487: end
# File lib/RMagick.rb, line 1489 1489: def concat(other) 1490: is_an_image_array other 1491: other.each {|image| @images << image} 1492: @scene = length-1 1493: return self 1494: end
Return the current image
# File lib/RMagick.rb, line 1461 1461: def cur_image 1462: if ! @scene 1463: Kernel.raise IndexError, "no images in this list" 1464: end 1465: @images[@scene] 1466: end
Set same delay for all images
# File lib/RMagick.rb, line 1497 1497: def delay=(d) 1498: if Integer(d) < 0 1499: raise ArgumentError, "delay must be greater than or equal to 0" 1500: end 1501: @images.each { |f| f.delay = Integer(d) } 1502: end
# File lib/RMagick.rb, line 1504 1504: def delete(obj, &block) 1505: is_an_image obj 1506: current = get_current() 1507: a = @images.delete(obj, &block) 1508: set_current current 1509: return a 1510: end
# File lib/RMagick.rb, line 1512 1512: def delete_at(ndx) 1513: current = get_current() 1514: a = @images.delete_at(ndx) 1515: set_current current 1516: return a 1517: end
# File lib/RMagick.rb, line 1519 1519: def delete_if(&block) 1520: current = get_current() 1521: @images.delete_if(&block) 1522: set_current current 1523: self 1524: end
# File lib/RMagick.rb, line 1526 1526: def dup 1527: ditto = self.class.new 1528: @images.each {|img| ditto << img} 1529: ditto.scene = @scene 1530: ditto.taint if tainted? 1531: return ditto 1532: end
# File lib/RMagick.rb, line 1534 1534: def eql?(other) 1535: is_an_image_array other 1536: eql = other.eql?(@images) 1537: begin # "other" is another ImageList 1538: eql &&= @scene == other.scene 1539: rescue NoMethodError 1540: # "other" is a plain Array 1541: end 1542: return eql 1543: end
# File lib/RMagick.rb, line 1545 1545: def fill(*args, &block) 1546: is_an_image args[0] unless block_given? 1547: current = get_current() 1548: @images.fill(*args, &block) 1549: is_an_image_array self 1550: set_current current 1551: self 1552: end
# File lib/RMagick.rb, line 1565 1565: def from_blob(*blobs, &block) 1566: if (blobs.length == 0) 1567: Kernel.raise ArgumentError, "no blobs given" 1568: end 1569: blobs.each { |b| 1570: Magick::Image.from_blob(b, &block).each { |n| @images << n } 1571: } 1572: @scene = length - 1 1573: self 1574: end
# File lib/RMagick.rb, line 1589 1589: def insert(index, *args) 1590: args.each {|image| is_an_image image} 1591: current = get_current() 1592: @images.insert(index, *args) 1593: set_current current 1594: return self 1595: end
Set the number of iterations of an animated GIF
# File lib/RMagick.rb, line 1605 1605: def iterations=(n) 1606: n = Integer(n) 1607: if n < 0 || n > 65535 1608: Kernel.raise ArgumentError, "iterations must be between 0 and 65535" 1609: end 1610: @images.each {|f| f.iterations=n} 1611: self 1612: end
# File lib/RMagick.rb, line 1614 1614: def last(*args) 1615: if args.length == 0 1616: a = @images.last 1617: else 1618: a = @images.last(*args) 1619: ilist = self.class.new 1620: a.each {|img| ilist << img} 1621: @scene = a.length - 1 1622: a = ilist 1623: end 1624: return a 1625: 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 1631 1631: def method_missing(methID, *args, &block) 1632: begin 1633: if @scene 1634: @images[@scene].send(methID, *args, &block) 1635: else 1636: super 1637: end 1638: rescue NoMethodError 1639: Kernel.raise NoMethodError, "undefined method `#{methID.id2name}' for #{self.class}" 1640: rescue Exception 1641: $@.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) } 1642: Kernel.raise 1643: end 1644: end
# File lib/RMagick.rb, line 1651 1651: def partition(&block) 1652: a = @images.partition(&block) 1653: t = self.class.new 1654: a[0].each { |img| t << img} 1655: t.set_current nil 1656: f = self.class.new 1657: a[1].each { |img| f << img} 1658: f.set_current nil 1659: [t, f] 1660: end
Ping files and concatenate the new images
# File lib/RMagick.rb, line 1663 1663: def ping(*files, &block) 1664: if (files.length == 0) 1665: Kernel.raise ArgumentError, "no files given" 1666: end 1667: files.each { |f| 1668: Magick::Image.ping(f, &block).each { |n| @images << n } 1669: } 1670: @scene = length - 1 1671: self 1672: end
# File lib/RMagick.rb, line 1674 1674: def pop 1675: current = get_current() 1676: a = @images.pop # can return nil 1677: set_current current 1678: return a 1679: end
# File lib/RMagick.rb, line 1681 1681: def push(*objs) 1682: objs.each do |image| 1683: is_an_image image 1684: @images << image 1685: end 1686: @scene = length - 1 1687: self 1688: end
Read files and concatenate the new images
# File lib/RMagick.rb, line 1691 1691: def read(*files, &block) 1692: if (files.length == 0) 1693: Kernel.raise ArgumentError, "no files given" 1694: end 1695: files.each { |f| 1696: Magick::Image.read(f, &block).each { |n| @images << n } 1697: } 1698: @scene = length - 1 1699: self 1700: end
# File lib/RMagick.rb, line 1712 1712: def reject!(&block) 1713: current = get_current() 1714: a = @images.reject!(&block) 1715: @images = a if !a.nil? 1716: set_current current 1717: return a.nil? ? nil : self 1718: end
# File lib/RMagick.rb, line 1720 1720: def replace(other) 1721: is_an_image_array other 1722: current = get_current() 1723: @images.clear 1724: other.each {|image| @images << image} 1725: @scene = self.length == 0 ? nil : 0 1726: set_current current 1727: self 1728: end
# File lib/RMagick.rb, line 1732 1732: def respond_to?(methID, priv=false) 1733: return true if __respond_to__?(methID, priv) 1734: if @scene 1735: @images[@scene].respond_to?(methID, priv) 1736: else 1737: super 1738: end 1739: end
# File lib/RMagick.rb, line 1741 1741: def reverse 1742: current = get_current() 1743: a = self.class.new 1744: @images.reverse_each {|image| a << image} 1745: a.set_current current 1746: return a 1747: end
# File lib/RMagick.rb, line 1749 1749: def reverse! 1750: current = get_current() 1751: @images.reverse! 1752: set_current current 1753: self 1754: end
# File lib/RMagick.rb, line 1756 1756: def reverse_each 1757: @images.reverse_each {|image| yield(image)} 1758: self 1759: end
Allow scene to be set to nil
# File lib/RMagick.rb, line 1305 1305: def scene=(n) 1306: if n.nil? 1307: Kernel.raise IndexError, "scene number out of bounds" unless @images.length == 0 1308: @scene = nil 1309: return @scene 1310: elsif @images.length == 0 1311: Kernel.raise IndexError, "scene number out of bounds" 1312: end 1313: 1314: n = Integer(n) 1315: if n < 0 || n > length - 1 1316: Kernel.raise IndexError, "scene number out of bounds" 1317: end 1318: @scene = n 1319: return @scene 1320: end
# File lib/RMagick.rb, line 1761 1761: def shift 1762: current = get_current() 1763: a = @images.shift 1764: set_current current 1765: return a 1766: end
# File lib/RMagick.rb, line 1768 1768: def slice(*args) 1769: current = get_current() 1770: slice = @images.slice(*args) 1771: if slice 1772: ilist = self.class.new 1773: if slice.respond_to?(:each) then 1774: slice.each {|image| ilist << image} 1775: else 1776: ilist << slice 1777: end 1778: else 1779: ilist = nil 1780: end 1781: return ilist 1782: end
# File lib/RMagick.rb, line 1784 1784: def slice!(*args) 1785: current = get_current() 1786: a = @images.slice!(*args) 1787: set_current current 1788: return a 1789: end
# File lib/RMagick.rb, line 1791 1791: def ticks_per_second=(t) 1792: if Integer(t) < 0 1793: Kernel.raise ArgumentError, "ticks_per_second must be greater than or equal to 0" 1794: end 1795: @images.each { |f| f.ticks_per_second = Integer(t) } 1796: end
# File lib/RMagick.rb, line 1798 1798: def to_a 1799: a = Array.new 1800: @images.each {|image| a << image} 1801: return a 1802: end
# File lib/RMagick.rb, line 1804 1804: def uniq 1805: current = get_current() 1806: a = self.class.new 1807: @images.uniq.each {|image| a << image} 1808: a.set_current current 1809: return a 1810: end
# File lib/RMagick.rb, line 1812 1812: def uniq!(*args) 1813: current = get_current() 1814: a = @images.uniq! 1815: set_current current 1816: return a.nil? ? nil : self 1817: end
# File lib/RMagick.rb, line 1827 1827: def values_at(*args) 1828: a = @images.values_at(*args) 1829: a = self.class.new 1830: @images.values_at(*args).each {|image| a << image} 1831: a.scene = a.length - 1 1832: return a 1833: end
# File lib/RMagick.rb, line 1262 1262: def is_an_image(obj) 1263: unless obj.kind_of? Magick::Image 1264: Kernel.raise ArgumentError, "Magick::Image required (#{obj.class} given)" 1265: end 1266: true 1267: end
Ensure array is always an array of Magick::Image objects
# File lib/RMagick.rb, line 1270 1270: def is_an_image_array(ary) 1271: unless ary.respond_to? :each 1272: Kernel.raise ArgumentError, "Magick::ImageList or array of Magick::Images required (#{ary.class} given)" 1273: end 1274: ary.each { |obj| is_an_image obj } 1275: true 1276: end
Find old current image, update scene number current is the id of the old current image.
# File lib/RMagick.rb, line 1280 1280: def set_current(current) 1281: if length() == 0 1282: self.scene = nil 1283: return 1284: # Don't bother looking for current image 1285: elsif scene() == nil || scene() >= length() 1286: self.scene = length() - 1 1287: return 1288: elsif current != nil 1289: # Find last instance of "current" in the list. 1290: # If "current" isn't in the list, set current to last image. 1291: self.scene = length() - 1 1292: each_with_index do |f,i| 1293: if f.__id__ == current 1294: self.scene = i 1295: end 1296: end 1297: return 1298: end 1299: self.scene = length() - 1 1300: end