Class: Magick::ImageList

Inherits:
Array
  • Object
show all
Includes:
Comparable
Defined in:
lib/RMagick.rb,
ext/RMagick/rmmain.c

Overview

class Magick::Image

Defined Under Namespace

Classes: Montage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*filenames) ⇒ ImageList

Initialize new instances



1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
# File 'lib/RMagick.rb', line 1472

def initialize(*filenames)
    @scene = nil
    filenames.each { |f|
        Magick::Image.read(f).each { |n| self << n }
        }
    if length > 0
        @scene = length - 1     # last image in array
    end
    self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methID, *args, &block) ⇒ Object

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.



1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/RMagick.rb', line 1504

def method_missing(methID, *args, &block)
    begin
        if @scene
            self[@scene].send(methID, *args, &block)
        else
            super
        end
    rescue NoMethodError
      raise NoMethodError, "undefined method `#{methID.id2name}' for #{self.class}"
    rescue Exception
        $@.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) }
        raise
    end
end

Instance Attribute Details

#sceneObject

Returns the value of attribute scene.



1026
1027
1028
# File 'lib/RMagick.rb', line 1026

def scene
  @scene
end

Instance Method Details

#&(other) ⇒ Object



1118
1119
1120
1121
1122
1123
1124
# File 'lib/RMagick.rb', line 1118

def &(other)
    is_a_image_array other
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#*(n) ⇒ Object



1126
1127
1128
1129
1130
1131
1132
1133
1134
# File 'lib/RMagick.rb', line 1126

def *(n)
    unless n.kind_of? Integer
        raise ArgumentError, "Integer required (#{n.class} given)"
    end
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#+(other) ⇒ Object



1136
1137
1138
1139
1140
1141
# File 'lib/RMagick.rb', line 1136

def +(other)
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#-(other) ⇒ Object



1143
1144
1145
1146
1147
1148
1149
# File 'lib/RMagick.rb', line 1143

def -(other)
    is_a_image_array other
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#<<(obj) ⇒ Object



1151
1152
1153
1154
1155
1156
# File 'lib/RMagick.rb', line 1151

def <<(obj)
    is_a_image obj
    a = super
    @scene = length-1
    return a
end

#<=>(other) ⇒ Object

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


1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# File 'lib/RMagick.rb', line 1393

def <=>(other)
    unless other.kind_of? self.class
       raise TypeError, "#{self.class} required (#{other.class} given)"
    end
    size = [self.length, other.length].min
    size.times do |x|
        r = self[x] <=> other[x]
        return r unless r == 0
    end
    if @scene.nil? && other.scene.nil?
        return 0
    elsif @scene.nil? && ! other.scene.nil?
        raise TypeError, "cannot convert nil into #{other.scene.class}"
    elsif ! @scene.nil? && other.scene.nil?
        raise TypeError, "cannot convert nil into #{self.scene.class}"
    end
    r = self.scene <=> other.scene
    return r unless r == 0
    return self.length <=> other.length
end

#[](*args) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
# File 'lib/RMagick.rb', line 1087

def [](*args)
    if (args.length > 1) || args[0].kind_of?(Range)
        self.class.new.replace super
    else
        super
    end
end

#[]=(*args) ⇒ Object



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/RMagick.rb', line 1095

def []=(*args)
    if args.length == 3             # f[start,length] = [f1,f2...]
        args[2].kind_of?(Magick::Image) || is_a_image_array(args[2])
        super
        args[0] = args[0] + length if (args[0] < 0)
        args[1] = length - args[0] if (args[0] + args[1] > length)
        if args[2].kind_of?(Magick::Image)
            @scene = args[0]
        else
            @scene = args[0] + args[2].length - 1
        end
    elsif args[0].kind_of? Range    # f[first..last] = [f1,f2...]
        args[1].kind_of?(Magick::Image) || is_a_image_array(args[1])
        super
        @scene = args[0].end
    else                            # f[index] = f1
        is_a_image args[1]
        super                       # index can be negative
        @scene = args[0] < 0 ? length + args[0] : args[0]
    end
    args.last                       # return value is always assigned value
end

#__map__(&block) ⇒ Object

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.



1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
# File 'lib/RMagick.rb', line 1258

def __map__(&block)
    cfid = self[@scene].__id__ rescue nil
    ensure_image = Proc.new do |img|
        rv = block.call(img)
        is_a_image rv
        return rv
    end
    a = self.class.new.replace __ary_map__(&ensure_image)
    a.set_cf cfid
    return a
end

#__respond_to__?Object

Ensure respond_to? answers correctly when we are delegating to Image



1520
# File 'lib/RMagick.rb', line 1520

alias_method :__respond_to__?, :respond_to?

#animateObject

#appendObject

#averageObject

#clearObject



1166
1167
1168
1169
# File 'lib/RMagick.rb', line 1166

def clear
    @scene = nil
    super
end

#cloneObject



1414
1415
1416
1417
1418
# File 'lib/RMagick.rb', line 1414

def clone
    ditto = dup
    ditto.freeze if frozen?
    return ditto
end

#coalesceObject

#collect(&block) ⇒ Object



1171
1172
1173
1174
1175
1176
# File 'lib/RMagick.rb', line 1171

def collect(&block)
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#collect!(&block) ⇒ Object



1178
1179
1180
1181
1182
# File 'lib/RMagick.rb', line 1178

def collect!(&block)
    super
    is_a_image_array self
    self
end

#compactObject



1184
1185
1186
1187
1188
1189
# File 'lib/RMagick.rb', line 1184

def compact
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#compact!Object



1191
1192
1193
1194
1195
1196
# File 'lib/RMagick.rb', line 1191

def compact!
    cfid = self[@scene].__id__ rescue nil
    a = super          # returns nil if no changes were made
    set_cf cfid
    return a
end

#concat(other) ⇒ Object



1198
1199
1200
1201
1202
1203
# File 'lib/RMagick.rb', line 1198

def concat(other)
    is_a_image_array other
    a = super
    @scene = length-1
    return a
end

#copyObject

Make a deep copy



1421
1422
1423
1424
1425
1426
1427
# File 'lib/RMagick.rb', line 1421

def copy
    ditto = self.class.new
    each { |f| ditto << f.copy }
    ditto.scene = @scene
    ditto.taint if tainted?
    return ditto
end

#cur_imageObject

Return the current image



1430
1431
1432
1433
1434
1435
# File 'lib/RMagick.rb', line 1430

def cur_image
    if ! @scene
        raise IndexError, "no images in this list"
    end
    self[@scene]
end

#deconstructObject

#delay=(d) ⇒ Object

Set same delay for all images



1438
1439
1440
1441
1442
1443
# File 'lib/RMagick.rb', line 1438

def delay=(d)
    if Integer(d) < 0
        raise ArgumentError, "delay must be greater than or equal to 0"
    end
    each { |f| f.delay = Integer(d) }
end

#delete(obj, &block) ⇒ Object



1205
1206
1207
1208
1209
1210
1211
# File 'lib/RMagick.rb', line 1205

def delete(obj, &block)
    is_a_image obj
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#delete_at(ndx) ⇒ Object



1213
1214
1215
1216
1217
1218
# File 'lib/RMagick.rb', line 1213

def delete_at(ndx)
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#delete_if(&block) ⇒ Object



1220
1221
1222
1223
1224
1225
# File 'lib/RMagick.rb', line 1220

def delete_if(&block)
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#displayObject Also known as: __display__

#dupObject



1452
1453
1454
1455
1456
1457
1458
# File 'lib/RMagick.rb', line 1452

def dup
    ditto = self.class.new
    each {|img| ditto << img}
    ditto.scene = @scene
    ditto.taint if tainted?
    return ditto
end

#fill(*args, &block) ⇒ Object



1227
1228
1229
1230
1231
1232
1233
1234
# File 'lib/RMagick.rb', line 1227

def fill(*args, &block)
    is_a_image args[0] unless block_given?
    cfid = self[@scene].__id__ rescue nil
    super
    is_a_image_array self
    set_cf cfid
    return self
end

#find_all(&block) ⇒ Object



1236
1237
1238
1239
1240
1241
# File 'lib/RMagick.rb', line 1236

def find_all(&block)
    cfid = self[@scene].__id__ rescue nil
    a = super
    a.set_cf cfid
    return a
end

#flatten_imagesObject

#from_blob(*blobs, &block) ⇒ Object



1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
# File 'lib/RMagick.rb', line 1460

def from_blob(*blobs, &block)
    if (blobs.length == 0)
        raise ArgumentError, "no blobs given"
    end
    blobs.each { |b|
        Magick::Image.from_blob(b, &block).each { |n| self << n  }
        }
    @scene = length - 1
    self
end

#insert(*args) ⇒ Object

Raises:

  • (ArgumentError)


1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/RMagick.rb', line 1244

def insert(*args)
    raise(ArgumentError, "can't insert nil") unless args.length > 1
    is_a_image_array args[1,args.length-1]
    cfid = self[@scene].__id__ rescue nil
    super
    set_cf cfid
    return self
end

#inspectObject

Call inspect for all the images



1484
1485
1486
1487
1488
# File 'lib/RMagick.rb', line 1484

def inspect
    ins = '['
    each {|image| ins << image.inspect << "\n"}
    ins.chomp("\n") + "]\nscene=#{@scene}"
end

#iterations=(n) ⇒ Object

Set the number of iterations of an animated GIF



1491
1492
1493
1494
1495
1496
1497
1498
# File 'lib/RMagick.rb', line 1491

def iterations=(n)
    n = Integer(n)
    if n < 0 || n > 65535
        raise ArgumentError, "iterations must be between 0 and 65535"
    end
    each {|f| f.iterations=n}
    self
end

#mapObject Also known as: __ary_map__

#map!(&block) ⇒ Object



1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/RMagick.rb', line 1270

def map!(&block)
    ensure_image = Proc.new do |img|
        rv = block.call(img)
        is_a_image rv
        return rv
    end
    super(&ensure_image)
end

#montageObject

#morphObject

#mosaicObject

#new_image(cols, rows, *fill, &info_blk) ⇒ Object

Create a new image and add it to the end



1531
1532
1533
# File 'lib/RMagick.rb', line 1531

def new_image(cols, rows, *fill, &info_blk)
    self << Magick::Image.new(cols, rows, *fill, &info_blk)
end

#ping(*files, &block) ⇒ Object

Ping files and concatenate the new images



1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
# File 'lib/RMagick.rb', line 1536

def ping(*files, &block)
    if (files.length == 0)
        raise ArgumentError, "no files given"
    end
    files.each { |f|
        Magick::Image.ping(f, &block).each { |n| self << n }
        }
    @scene = length - 1
    self
end

#popObject



1279
1280
1281
1282
1283
1284
# File 'lib/RMagick.rb', line 1279

def pop
    cfid = self[@scene].__id__ rescue nil
    a = super       # can return nil
    set_cf cfid
    return a
end

#push(*objs) ⇒ Object



1286
1287
1288
1289
1290
1291
# File 'lib/RMagick.rb', line 1286

def push(*objs)
    objs.each { |o| is_a_image o }
    super
    @scene = length - 1
    self
end

#quantizeObject

#read(*files, &block) ⇒ Object

Read files and concatenate the new images



1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
# File 'lib/RMagick.rb', line 1548

def read(*files, &block)
    if (files.length == 0)
        raise ArgumentError, "no files given"
    end
    files.each { |f|
        Magick::Image.read(f, &block).each { |n| self << n }
        }
    @scene = length - 1
    self
end

#reject(&block) ⇒ Object



1293
1294
1295
1296
1297
1298
# File 'lib/RMagick.rb', line 1293

def reject(&block)
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#reject!(&block) ⇒ Object



1300
1301
1302
1303
1304
1305
# File 'lib/RMagick.rb', line 1300

def reject!(&block)
    cfid = self[@scene].__id__ rescue nil
    a = super       # can return nil
    set_cf cfid
    return a
end

#replace(other) ⇒ Object



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/RMagick.rb', line 1307

def replace(other)
    is_a_image_array other
    # Since replace gets called so frequently when @scene == nil
    # test for it instead of letting rescue catch it.
    cfid = nil
    if @scene then
        cfid = self[@scene].__id__ rescue nil
    end
    super
    # set_cf will fail if the new list has fewer images
    # than the scene number indicates.
    @scene = self.length == 0 ? nil : 0
    set_cf cfid
    self
end

#respond_to?(methID, priv = false) ⇒ Boolean

Returns:

  • (Boolean)


1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/RMagick.rb', line 1521

def respond_to?(methID, priv=false)
    return true if __respond_to__?(methID, priv)
    if @scene
        self[@scene].respond_to?(methID, priv)
    else
        super
    end
end

#reverseObject



1323
1324
1325
1326
1327
1328
# File 'lib/RMagick.rb', line 1323

def reverse
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#reverse!Object



1330
1331
1332
1333
1334
1335
# File 'lib/RMagick.rb', line 1330

def reverse!
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#select(&block) ⇒ Object



1337
1338
1339
1340
1341
1342
# File 'lib/RMagick.rb', line 1337

def select(&block)
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#shiftObject



1344
1345
1346
1347
1348
1349
# File 'lib/RMagick.rb', line 1344

def shift
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#slice(*args) ⇒ Object



1351
1352
1353
# File 'lib/RMagick.rb', line 1351

def slice(*args)
    self[*args]
end

#slice!(*args) ⇒ Object



1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/RMagick.rb', line 1355

def slice!(*args)
    cfid = self[@scene].__id__ rescue nil
    if args.length > 1 || args[0].kind_of?(Range)
        a = self.class.new.replace super
    else
        a = super
    end
    set_cf cfid
    return a
end

#ticks_per_second=(t) ⇒ Object



1445
1446
1447
1448
1449
1450
# File 'lib/RMagick.rb', line 1445

def ticks_per_second=(t)
    if Integer(t) < 0
        raise ArgumentError, "ticks_per_second must be greater than or equal to 0"
    end
    each { |f| f.ticks_per_second = Integer(t) }
end

#to_blobObject

#uniqObject



1366
1367
1368
1369
1370
1371
# File 'lib/RMagick.rb', line 1366

def uniq
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end

#uniq!(*args) ⇒ Object



1373
1374
1375
1376
1377
1378
# File 'lib/RMagick.rb', line 1373

def uniq!(*args)
    cfid = self[@scene].__id__ rescue nil
    a = super
    set_cf cfid
    return a
end

#unshift(obj) ⇒ Object



1381
1382
1383
1384
1385
1386
# File 'lib/RMagick.rb', line 1381

def unshift(obj)
    is_a_image obj
    a = super
    @scene = 0
    return a
end

#writeObject

#|(other) ⇒ Object



1158
1159
1160
1161
1162
1163
1164
# File 'lib/RMagick.rb', line 1158

def |(other)
    is_a_image_array other
    cfid = self[@scene].__id__ rescue nil
    a = self.class.new.replace super
    a.set_cf cfid
    return a
end