Class | Magick::ImageList |
In: |
lib/RMagick.rb
|
Parent: | Array |
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 1473 1473: def initialize(*filenames) 1474: @scene = nil 1475: filenames.each { |f| 1476: Magick::Image.read(f).each { |n| self << n } 1477: } 1478: if length > 0 1479: @scene = length - 1 # last image in array 1480: end 1481: self 1482: end
# File lib/RMagick.rb, line 1119 1119: def &(other) 1120: is_a_image_array other 1121: cfid = self[@scene].__id__ rescue nil 1122: a = self.class.new.replace super 1123: a.set_cf cfid 1124: return a 1125: end
# File lib/RMagick.rb, line 1127 1127: def *(n) 1128: unless n.kind_of? Integer 1129: raise ArgumentError, "Integer required (#{n.class} given)" 1130: end 1131: cfid = self[@scene].__id__ rescue nil 1132: a = self.class.new.replace super 1133: a.set_cf cfid 1134: return a 1135: end
# File lib/RMagick.rb, line 1137 1137: def +(other) 1138: cfid = self[@scene].__id__ rescue nil 1139: a = self.class.new.replace super 1140: a.set_cf cfid 1141: return a 1142: end
# File lib/RMagick.rb, line 1144 1144: def -(other) 1145: is_a_image_array other 1146: cfid = self[@scene].__id__ rescue nil 1147: a = self.class.new.replace super 1148: a.set_cf cfid 1149: return a 1150: end
# File lib/RMagick.rb, line 1152 1152: def <<(obj) 1153: is_a_image obj 1154: a = super 1155: @scene = length-1 1156: return a 1157: 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: 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: raise TypeError, "cannot convert nil into #{other.scene.class}" 1407: elsif ! @scene.nil? && other.scene.nil? 1408: 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 1088 1088: def [](*args) 1089: if (args.length > 1) || args[0].kind_of?(Range) 1090: self.class.new.replace super 1091: else 1092: super 1093: end 1094: end
# File lib/RMagick.rb, line 1096 1096: def []=(*args) 1097: if args.length == 3 # f[start,length] = [f1,f2...] 1098: args[2].kind_of?(Magick::Image) || is_a_image_array(args[2]) 1099: super 1100: args[0] = args[0] + length if (args[0] < 0) 1101: args[1] = length - args[0] if (args[0] + args[1] > length) 1102: if args[2].kind_of?(Magick::Image) 1103: @scene = args[0] 1104: else 1105: @scene = args[0] + args[2].length - 1 1106: end 1107: elsif args[0].kind_of? Range # f[first..last] = [f1,f2...] 1108: args[1].kind_of?(Magick::Image) || is_a_image_array(args[1]) 1109: super 1110: @scene = args[0].end 1111: else # f[index] = f1 1112: is_a_image args[1] 1113: super # index can be negative 1114: @scene = args[0] < 0 ? length + args[0] : args[0] 1115: end 1116: args.last # return value is always assigned value 1117: end
Enumerable (or Array) has a map method that conflicts with our own map method. RMagick.so has defined a synonym for that map called Array#ary_map. Here, we define Magick::ImageList#map to allow the use of the Enumerable/Array#map method on ImageList objects.
# File lib/RMagick.rb, line 1259 1259: def __map__(&block) 1260: cfid = self[@scene].__id__ rescue nil 1261: ensure_image = Proc.new do |img| 1262: rv = block.call(img) 1263: is_a_image rv 1264: return rv 1265: end 1266: a = self.class.new.replace __ary_map__(&ensure_image) 1267: a.set_cf cfid 1268: return a 1269: end
# File lib/RMagick.rb, line 1415 1415: def clone 1416: ditto = dup 1417: ditto.freeze if frozen? 1418: return ditto 1419: end
# File lib/RMagick.rb, line 1172 1172: def collect(&block) 1173: cfid = self[@scene].__id__ rescue nil 1174: a = self.class.new.replace super 1175: a.set_cf cfid 1176: return a 1177: end
# File lib/RMagick.rb, line 1179 1179: def collect!(&block) 1180: super 1181: is_a_image_array self 1182: self 1183: end
# File lib/RMagick.rb, line 1185 1185: def compact 1186: cfid = self[@scene].__id__ rescue nil 1187: a = self.class.new.replace super 1188: a.set_cf cfid 1189: return a 1190: end
# File lib/RMagick.rb, line 1192 1192: def compact! 1193: cfid = self[@scene].__id__ rescue nil 1194: a = super # returns nil if no changes were made 1195: set_cf cfid 1196: return a 1197: end
# File lib/RMagick.rb, line 1199 1199: def concat(other) 1200: is_a_image_array other 1201: a = super 1202: @scene = length-1 1203: return a 1204: end
Make a deep copy
# File lib/RMagick.rb, line 1422 1422: def copy 1423: ditto = self.class.new 1424: each { |f| ditto << f.copy } 1425: ditto.scene = @scene 1426: ditto.taint if tainted? 1427: return ditto 1428: end
Return the current image
# File lib/RMagick.rb, line 1431 1431: def cur_image 1432: if ! @scene 1433: raise IndexError, "no images in this list" 1434: end 1435: self[@scene] 1436: end
Set same delay for all images
# File lib/RMagick.rb, line 1439 1439: def delay=(d) 1440: if Integer(d) < 0 1441: raise ArgumentError, "delay must be greater than or equal to 0" 1442: end 1443: each { |f| f.delay = Integer(d) } 1444: end
# File lib/RMagick.rb, line 1206 1206: def delete(obj, &block) 1207: is_a_image obj 1208: cfid = self[@scene].__id__ rescue nil 1209: a = super 1210: set_cf cfid 1211: return a 1212: end
# File lib/RMagick.rb, line 1214 1214: def delete_at(ndx) 1215: cfid = self[@scene].__id__ rescue nil 1216: a = super 1217: set_cf cfid 1218: return a 1219: end
# File lib/RMagick.rb, line 1221 1221: def delete_if(&block) 1222: cfid = self[@scene].__id__ rescue nil 1223: a = super 1224: set_cf cfid 1225: return a 1226: end
# File lib/RMagick.rb, line 1453 1453: def dup 1454: ditto = self.class.new 1455: each {|img| ditto << img} 1456: ditto.scene = @scene 1457: ditto.taint if tainted? 1458: return ditto 1459: end
# File lib/RMagick.rb, line 1228 1228: def fill(*args, &block) 1229: is_a_image args[0] unless block_given? 1230: cfid = self[@scene].__id__ rescue nil 1231: super 1232: is_a_image_array self 1233: set_cf cfid 1234: return self 1235: end
# File lib/RMagick.rb, line 1237 1237: def find_all(&block) 1238: cfid = self[@scene].__id__ rescue nil 1239: a = super 1240: a.set_cf cfid 1241: return a 1242: end
# File lib/RMagick.rb, line 1461 1461: def from_blob(*blobs, &block) 1462: if (blobs.length == 0) 1463: raise ArgumentError, "no blobs given" 1464: end 1465: blobs.each { |b| 1466: Magick::Image.from_blob(b, &block).each { |n| self << n } 1467: } 1468: @scene = length - 1 1469: self 1470: end
# File lib/RMagick.rb, line 1245 1245: def insert(*args) 1246: raise(ArgumentError, "can't insert nil") unless args.length > 1 1247: is_a_image_array args[1,args.length-1] 1248: cfid = self[@scene].__id__ rescue nil 1249: super 1250: set_cf cfid 1251: return self 1252: end
Call inspect for all the images
# File lib/RMagick.rb, line 1485 1485: def inspect 1486: ins = '[' 1487: each {|image| ins << image.inspect << "\n"} 1488: ins.chomp("\n") + "]\nscene=#{@scene}" 1489: end
Set the number of iterations of an animated GIF
# File lib/RMagick.rb, line 1492 1492: def iterations=(n) 1493: n = Integer(n) 1494: if n < 0 || n > 65535 1495: raise ArgumentError, "iterations must be between 0 and 65535" 1496: end 1497: each {|f| f.iterations=n} 1498: self 1499: end
# File lib/RMagick.rb, line 1271 1271: def map!(&block) 1272: ensure_image = Proc.new do |img| 1273: rv = block.call(img) 1274: is_a_image rv 1275: return rv 1276: end 1277: super(&ensure_image) 1278: 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 1505 1505: def method_missing(methID, *args, &block) 1506: begin 1507: if @scene 1508: self[@scene].send(methID, *args, &block) 1509: else 1510: super 1511: end 1512: rescue NoMethodError 1513: raise NoMethodError, "undefined method `#{methID.id2name}' for #{self.class}" 1514: rescue Exception 1515: $@.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) } 1516: raise 1517: end 1518: end
Create a new image and add it to the end
# File lib/RMagick.rb, line 1532 1532: def new_image(cols, rows, *fill, &info_blk) 1533: self << Magick::Image.new(cols, rows, *fill, &info_blk) 1534: end
Ping files and concatenate the new images
# File lib/RMagick.rb, line 1537 1537: def ping(*files, &block) 1538: if (files.length == 0) 1539: raise ArgumentError, "no files given" 1540: end 1541: files.each { |f| 1542: Magick::Image.ping(f, &block).each { |n| self << n } 1543: } 1544: @scene = length - 1 1545: self 1546: end
# File lib/RMagick.rb, line 1280 1280: def pop 1281: cfid = self[@scene].__id__ rescue nil 1282: a = super # can return nil 1283: set_cf cfid 1284: return a 1285: end
# File lib/RMagick.rb, line 1287 1287: def push(*objs) 1288: objs.each { |o| is_a_image o } 1289: super 1290: @scene = length - 1 1291: self 1292: end
Read files and concatenate the new images
# File lib/RMagick.rb, line 1549 1549: def read(*files, &block) 1550: if (files.length == 0) 1551: raise ArgumentError, "no files given" 1552: end 1553: files.each { |f| 1554: Magick::Image.read(f, &block).each { |n| self << n } 1555: } 1556: @scene = length - 1 1557: self 1558: end
# File lib/RMagick.rb, line 1294 1294: def reject(&block) 1295: cfid = self[@scene].__id__ rescue nil 1296: a = self.class.new.replace super 1297: a.set_cf cfid 1298: return a 1299: end
# File lib/RMagick.rb, line 1301 1301: def reject!(&block) 1302: cfid = self[@scene].__id__ rescue nil 1303: a = super # can return nil 1304: set_cf cfid 1305: return a 1306: end
# File lib/RMagick.rb, line 1308 1308: def replace(other) 1309: is_a_image_array other 1310: # Since replace gets called so frequently when @scene == nil 1311: # test for it instead of letting rescue catch it. 1312: cfid = nil 1313: if @scene then 1314: cfid = self[@scene].__id__ rescue nil 1315: end 1316: super 1317: # set_cf will fail if the new list has fewer images 1318: # than the scene number indicates. 1319: @scene = self.length == 0 ? nil : 0 1320: set_cf cfid 1321: self 1322: end
# File lib/RMagick.rb, line 1522 1522: def respond_to?(methID, priv=false) 1523: return true if __respond_to__?(methID, priv) 1524: if @scene 1525: self[@scene].respond_to?(methID, priv) 1526: else 1527: super 1528: end 1529: end
# File lib/RMagick.rb, line 1324 1324: def reverse 1325: cfid = self[@scene].__id__ rescue nil 1326: a = self.class.new.replace super 1327: a.set_cf cfid 1328: return a 1329: end
# File lib/RMagick.rb, line 1331 1331: def reverse! 1332: cfid = self[@scene].__id__ rescue nil 1333: a = super 1334: set_cf cfid 1335: return a 1336: end
Allow scene to be set to nil
# File lib/RMagick.rb, line 1071 1071: def scene=(n) 1072: if n.nil? 1073: raise IndexError, "scene number out of bounds" unless length == 0 1074: @scene = nil 1075: return @scene 1076: elsif length == 0 1077: raise IndexError, "scene number out of bounds" 1078: end 1079: 1080: n = Integer(n) 1081: if n < 0 || n > length - 1 1082: raise IndexError, "scene number out of bounds" 1083: end 1084: @scene = n 1085: return @scene 1086: end
# File lib/RMagick.rb, line 1338 1338: def select(&block) 1339: cfid = self[@scene].__id__ rescue nil 1340: a = self.class.new.replace super 1341: a.set_cf cfid 1342: return a 1343: end
# File lib/RMagick.rb, line 1345 1345: def shift 1346: cfid = self[@scene].__id__ rescue nil 1347: a = super 1348: set_cf cfid 1349: return a 1350: end
# File lib/RMagick.rb, line 1356 1356: def slice!(*args) 1357: cfid = self[@scene].__id__ rescue nil 1358: if args.length > 1 || args[0].kind_of?(Range) 1359: a = self.class.new.replace super 1360: else 1361: a = super 1362: end 1363: set_cf cfid 1364: return a 1365: end
# File lib/RMagick.rb, line 1446 1446: def ticks_per_second=(t) 1447: if Integer(t) < 0 1448: raise ArgumentError, "ticks_per_second must be greater than or equal to 0" 1449: end 1450: each { |f| f.ticks_per_second = Integer(t) } 1451: end
# File lib/RMagick.rb, line 1367 1367: def uniq 1368: cfid = self[@scene].__id__ rescue nil 1369: a = self.class.new.replace super 1370: a.set_cf cfid 1371: return a 1372: end
# File lib/RMagick.rb, line 1374 1374: def uniq!(*args) 1375: cfid = self[@scene].__id__ rescue nil 1376: a = super 1377: set_cf cfid 1378: return a 1379: end
@scene -> new object
# File lib/RMagick.rb, line 1382 1382: def unshift(obj) 1383: is_a_image obj 1384: a = super 1385: @scene = 0 1386: return a 1387: end
# File lib/RMagick.rb, line 1159 1159: def |(other) 1160: is_a_image_array other 1161: cfid = self[@scene].__id__ rescue nil 1162: a = self.class.new.replace super 1163: a.set_cf cfid 1164: return a 1165: end
# File lib/RMagick.rb, line 1031 1031: def is_a_image(obj) 1032: unless obj.kind_of? Magick::Image 1033: raise ArgumentError, "Magick::Image required (#{obj.class} given)" 1034: end 1035: true 1036: end
Ensure array is always an array of Magick::Image objects
# File lib/RMagick.rb, line 1039 1039: def is_a_image_array(ary) 1040: unless ary.respond_to? :each 1041: raise ArgumentError, "Magick::ImageList or array of Magick::Images required (#{ary.class} given)" 1042: end 1043: ary.each { |obj| is_a_image obj } 1044: true 1045: end
Find old current image, update @scene cfid is the id of the old current image.
# File lib/RMagick.rb, line 1049 1049: def set_cf(cfid) 1050: if length == 0 1051: @scene = nil 1052: return 1053: # Don't bother looking for current image 1054: elsif @scene == nil || @scene >= length 1055: @scene = length - 1 1056: return 1057: elsif cfid != nil 1058: each_with_index do |f,i| 1059: if f.__id__ == cfid 1060: @scene = i 1061: return 1062: end 1063: end 1064: end 1065: @scene = length - 1 1066: end