Class: Magick::ImageList

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
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



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

def initialize(*filenames)
    @images = []
    @scene = nil
    filenames.each { |f|
        Magick::Image.read(f).each { |n| @images << 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.



1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
# File 'lib/RMagick.rb', line 1600

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

Instance Attribute Details

#sceneObject

Returns the value of attribute scene.



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

def scene
  @scene
end

Instance Method Details

#*(n) ⇒ Object



1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/RMagick.rb', line 1315

def *(n)
    unless n.kind_of? Integer
        Kernel.raise ArgumentError, "Integer required (#{n.class} given)"
    end
    current = get_current()
    ilist = self.class.new
    (@images * n).each {|image| ilist << image}
    ilist.set_current current
    return ilist
end

#<<(obj) ⇒ Object



1326
1327
1328
1329
1330
1331
# File 'lib/RMagick.rb', line 1326

def <<(obj)
    is_an_image obj
    @images << obj
    @scene = @images.length - 1
    self
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


1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
# File 'lib/RMagick.rb', line 1338

def <=>(other)
    unless other.kind_of? self.class
       Kernel.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?
        Kernel.raise TypeError, "cannot convert nil into #{other.scene.class}"
    elsif ! @scene.nil? && other.scene.nil?
        Kernel.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



1359
1360
1361
1362
1363
1364
1365
1366
1367
# File 'lib/RMagick.rb', line 1359

def [](*args)
    a = @images[*args]
    if a.respond_to?(:each) then
        ilist = self.class.new
        a.each {|image| ilist << image}
        a = ilist
    end
    return a
end

#[]=(*args) ⇒ Object



1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
# File 'lib/RMagick.rb', line 1369

def []=(*args)
    obj = @images.[]=(*args)
    if obj && obj.respond_to?(:each) then
        is_an_image_array(obj)
        set_current obj.last.__id__
    elsif obj
        is_an_image(obj)
        set_current obj.__id__
    else
        set_current nil
    end
    return obj
end

#__respond_to__?Object

Ensure respond_to? answers correctly when we are delegating to Image



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

alias_method :__respond_to__?, :respond_to?

#animateObject

#appendObject

#averageObject

#clearObject



1393
1394
1395
1396
# File 'lib/RMagick.rb', line 1393

def clear
    @scene = nil
    @images.clear
end

#cloneObject



1398
1399
1400
1401
1402
# File 'lib/RMagick.rb', line 1398

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

#coalesceObject

#collect(&block) ⇒ Object Also known as: __map__

override Enumerable#collect



1405
1406
1407
1408
1409
1410
1411
1412
# File 'lib/RMagick.rb', line 1405

def collect(&block)
    current = get_current()
    a = @images.collect(&block)
    ilist = self.class.new
    a.each {|image| ilist << image}
    ilist.set_current current
    return ilist
end

#collect!(&block) ⇒ Object Also known as: map!, __map__!



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

def collect!(&block)
    @images.collect!(&block)
    is_an_image_array @images
    self
end

#compactObject



1442
1443
1444
1445
1446
1447
1448
1449
# File 'lib/RMagick.rb', line 1442

def compact
    current = get_current()
    ilist = self.class.new
    a = @images.compact
    a.each {|image| ilist << image}
    ilist.set_current current
    return ilist
end

#compact!Object



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

def compact!
    current = get_current()
    a = @images.compact!    # returns nil if no changes were made
    set_current current
    return a.nil? ? nil : self
end

#composite_layersObject

#concat(other) ⇒ Object



1458
1459
1460
1461
1462
1463
# File 'lib/RMagick.rb', line 1458

def concat(other)
    is_an_image_array other
    other.each {|image| @images << image}
    @scene = length-1
    return self
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
    @images.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
        Kernel.raise IndexError, "no images in this list"
    end
    @images[@scene]
end

#deconstructObject

#delay=(d) ⇒ Object

Set same delay for all images



1466
1467
1468
1469
1470
1471
# File 'lib/RMagick.rb', line 1466

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

#delete(obj, &block) ⇒ Object



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

def delete(obj, &block)
    is_an_image obj
    current = get_current()
    a = @images.delete(obj, &block)
    set_current current
    return a
end

#delete_at(ndx) ⇒ Object



1481
1482
1483
1484
1485
1486
# File 'lib/RMagick.rb', line 1481

def delete_at(ndx)
    current = get_current()
    a = @images.delete_at(ndx)
    set_current current
    return a
end

#delete_if(&block) ⇒ Object



1488
1489
1490
1491
1492
1493
# File 'lib/RMagick.rb', line 1488

def delete_if(&block)
    current = get_current()
    @images.delete_if(&block)
    set_current current
    self
end

#displayObject Also known as: __display__

#dupObject



1495
1496
1497
1498
1499
1500
1501
# File 'lib/RMagick.rb', line 1495

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
# File 'lib/RMagick.rb', line 1503

def eql?(other)
  is_an_image_array other
  eql = other.eql?(@images)
  begin # "other" is another ImageList
    eql &&= @scene == other.scene
  rescue NoMethodError
    # "other" is a plain Array
  end
  return eql
end

#fill(*args, &block) ⇒ Object



1514
1515
1516
1517
1518
1519
1520
1521
# File 'lib/RMagick.rb', line 1514

def fill(*args, &block)
    is_an_image args[0] unless block_given?
    current = get_current()
    @images.fill(*args, &block)
    is_an_image_array self
    set_current current
    self
end

#find_all(&block) ⇒ Object Also known as: select

Override Enumerable’s find_all



1524
1525
1526
1527
1528
1529
1530
1531
# File 'lib/RMagick.rb', line 1524

def find_all(&block)
    current = get_current()
    a = @images.find_all(&block)
    ilist = self.class.new
    a.each {|image| ilist << image}
    ilist.set_current current
    return ilist
end

#flatten_imagesObject

#from_blob(*blobs, &block) ⇒ Object



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

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

#fxObject

#insert(index, *args) ⇒ Object



1558
1559
1560
1561
1562
1563
1564
# File 'lib/RMagick.rb', line 1558

def insert(index, *args)
    args.each {|image| is_an_image image}
    current = get_current()
    @images.insert(index, *args)
    set_current current
    return self
end

#inspectObject

Call inspect for all the images



1567
1568
1569
1570
1571
# File 'lib/RMagick.rb', line 1567

def inspect
    img = []
    @images.each {|image| img << image.inspect }
    img = "[" + img.join(",\n") + "]\nscene=#{@scene}"
end

#iterations=(n) ⇒ Object

Set the number of iterations of an animated GIF



1574
1575
1576
1577
1578
1579
1580
1581
# File 'lib/RMagick.rb', line 1574

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

#last(*args) ⇒ Object



1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
# File 'lib/RMagick.rb', line 1583

def last(*args)
    if args.length == 0
      a = @images.last
    else
      a = @images.last(*args)
      ilist = self.class.new
      a.each {|img| ilist << img}
      @scene = a.length - 1
      a = ilist
    end
    return a
end

#mapObject

#montageObject

#morphObject

#mosaicObject

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

Create a new image and add it to the end



1616
1617
1618
# File 'lib/RMagick.rb', line 1616

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

#optimize_layersObject

#partition(&block) ⇒ Object



1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
# File 'lib/RMagick.rb', line 1620

def partition(&block)
  a = @images.partition(&block)
  t = self.class.new
  a[0].each { |img| t << img}
  t.set_current nil
  f = self.class.new
  a[1].each { |img| f << img}
  f.set_current nil
  [t, f]
end

#ping(*files, &block) ⇒ Object

Ping files and concatenate the new images



1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
# File 'lib/RMagick.rb', line 1632

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

#popObject



1643
1644
1645
1646
1647
1648
# File 'lib/RMagick.rb', line 1643

def pop
    current = get_current()
    a = @images.pop       # can return nil
    set_current current
    return a
end

#push(*objs) ⇒ Object



1650
1651
1652
1653
1654
1655
1656
1657
# File 'lib/RMagick.rb', line 1650

def push(*objs)
    objs.each do |image|
        is_an_image image
        @images << image
    end
    @scene = length - 1
    self
end

#quantizeObject

#read(*files, &block) ⇒ Object

Read files and concatenate the new images



1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'lib/RMagick.rb', line 1660

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

#reject(&block) ⇒ Object

override Enumerable’s reject



1672
1673
1674
1675
1676
1677
1678
1679
# File 'lib/RMagick.rb', line 1672

def reject(&block)
    current = get_current()
    ilist = self.class.new
    a = @images.reject(&block)
    a.each {|image| ilist << image}
    ilist.set_current current
    return ilist
end

#reject!(&block) ⇒ Object



1681
1682
1683
1684
1685
1686
1687
# File 'lib/RMagick.rb', line 1681

def reject!(&block)
    current = get_current()
    a = @images.reject!(&block)
    @images = a if !a.nil?
    set_current current
    return a.nil? ? nil : self
end

#replace(other) ⇒ Object



1689
1690
1691
1692
1693
1694
1695
1696
1697
# File 'lib/RMagick.rb', line 1689

def replace(other)
    is_an_image_array other
    current = get_current()
    @images.clear
    other.each {|image| @images << image}
    @scene = self.length == 0 ? nil : 0
    set_current current
    self
end

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

Returns:

  • (Boolean)


1701
1702
1703
1704
1705
1706
1707
1708
# File 'lib/RMagick.rb', line 1701

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

#reverseObject



1710
1711
1712
1713
1714
1715
1716
# File 'lib/RMagick.rb', line 1710

def reverse
    current = get_current()
    a = self.class.new
    @images.reverse_each {|image| a << image}
    a.set_current current
    return a
end

#reverse!Object



1718
1719
1720
1721
1722
1723
# File 'lib/RMagick.rb', line 1718

def reverse!
    current = get_current()
    @images.reverse!
    set_current current
    self
end

#reverse_eachObject



1725
1726
1727
1728
# File 'lib/RMagick.rb', line 1725

def reverse_each
    @images.reverse_each {|image| yield(image)}
    self
end

#shiftObject



1730
1731
1732
1733
1734
1735
# File 'lib/RMagick.rb', line 1730

def shift
    current = get_current()
    a = @images.shift
    set_current current
    return a
end

#slice(*args) ⇒ Object



1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# File 'lib/RMagick.rb', line 1737

def slice(*args)
    current = get_current()
    slice = @images.slice(*args)
    if slice
        ilist = self.class.new
        if slice.respond_to?(:each) then
            slice.each {|image| ilist << image}
        else
            ilist << slice
        end
    else
        ilist = nil
    end
    return ilist
end

#slice!(*args) ⇒ Object



1753
1754
1755
1756
1757
1758
# File 'lib/RMagick.rb', line 1753

def slice!(*args)
    current = get_current()
    a = @images.slice!(*args)
    set_current current
    return a
end

#ticks_per_second=(t) ⇒ Object



1760
1761
1762
1763
1764
1765
# File 'lib/RMagick.rb', line 1760

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

#to_aObject



1767
1768
1769
1770
1771
# File 'lib/RMagick.rb', line 1767

def to_a
    a = Array.new
    @images.each {|image| a << image}
    return a
end

#to_blobObject

#uniqObject



1773
1774
1775
1776
1777
1778
1779
# File 'lib/RMagick.rb', line 1773

def uniq
    current = get_current()
    a = self.class.new
    @images.uniq.each {|image| a << image}
    a.set_current current
    return a
end

#uniq!(*args) ⇒ Object



1781
1782
1783
1784
1785
1786
# File 'lib/RMagick.rb', line 1781

def uniq!(*args)
    current = get_current()
    a = @images.uniq!
    set_current current
    return a.nil? ? nil : self
end

#unshift(obj) ⇒ Object



1789
1790
1791
1792
1793
1794
# File 'lib/RMagick.rb', line 1789

def unshift(obj)
    is_an_image obj
    @images.unshift(obj)
    @scene = 0
    self
end

#values_at(*args) ⇒ Object Also known as: indexes, indices



1796
1797
1798
1799
1800
1801
1802
# File 'lib/RMagick.rb', line 1796

def values_at(*args)
    a = @images.values_at(*args)
    a = self.class.new
    @images.values_at(*args).each {|image| a << image}
    a.scene = a.length - 1
    return a
end

#writeObject