Class: Magick::ImageList

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/rmagick_internal.rb,
ext/RMagick/rmmain.cpp

Overview

class Magick::Image

Defined Under Namespace

Classes: Montage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*filenames, &block) ⇒ ImageList

Initialize new instances



1545
1546
1547
1548
1549
1550
1551
1552
1553
# File 'lib/rmagick_internal.rb', line 1545

def initialize(*filenames, &block)
  @images = []
  @scene = nil
  filenames.each do |f|
    Magick::Image.read(f, &block).each { |n| @images << n }
  end

  @scene = length - 1 if length > 0 # last image in array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, *args) ⇒ 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.



1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
# File 'lib/rmagick_internal.rb', line 1606

def method_missing(meth_id, *args, &)
  if @scene
    img = @images[@scene]
    new_img = img.public_send(meth_id, *args, &)
    img.equal?(new_img) ? self : new_img
  else
    super
  end
rescue NoMethodError
  Kernel.raise NoMethodError, "undefined method `#{meth_id.id2name}' for #{self.class}"
rescue StandardError
  $ERROR_POSITION.delete_if { |s| /:in `send'$/.match(s) || /:in `method_missing'$/.match(s) }
  Kernel.raise
end

Instance Attribute Details

#sceneObject

Returns the value of attribute scene.



1233
1234
1235
# File 'lib/rmagick_internal.rb', line 1233

def scene
  @scene
end

Instance Method Details

#*(other) ⇒ Object



1310
1311
1312
1313
1314
1315
1316
1317
# File 'lib/rmagick_internal.rb', line 1310

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

#<<(obj) ⇒ Object



1319
1320
1321
1322
1323
1324
# File 'lib/rmagick_internal.rb', line 1319

def <<(obj)
  assert_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


1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
# File 'lib/rmagick_internal.rb', line 1331

def <=>(other)
  return unless other.is_a? self.class

  size = [length, other.length].min
  size.times do |x|
    r = self[x] <=> other[x]
    return r unless r.zero?
  end

  return 0 if @scene.nil? && other.scene.nil?
  return if @scene.nil? && !other.scene.nil?
  return if !@scene.nil? && other.scene.nil?

  r = scene <=> other.scene
  return r unless r.zero?

  length <=> other.length
end

#[](*args) ⇒ Object



1350
1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/rmagick_internal.rb', line 1350

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

#[]=(*args) ⇒ Object



1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
# File 'lib/rmagick_internal.rb', line 1360

def []=(*args)
  obj = @images.[]=(*args)
  if obj.respond_to?(:each)
    assert_image_array(obj)
    set_current obj.last.__id__
  elsif obj
    assert_image(obj)
    set_current obj.__id__
  else
    set_current nil
  end
end

#__respond_to__?Object

Ensure respond_to? answers correctly when we are delegating to Image



1702
# File 'lib/rmagick_internal.rb', line 1702

alias __respond_to__? respond_to?

#animateMagick::ImageList #animate(delay) {|info| ... } ⇒ Magick::ImageList

Repeatedly display the images in the images array to an XWindow screen. The delay argument is the number of 1/100ths of a second (0 to 65535) to delay between images.

Overloads:

  • #animate(delay) {|info| ... } ⇒ Magick::ImageList

    Parameters:

    • delay (Numeric)

      the length of time between each image in an animation

    Yields:

    • (info)

    Yield Parameters:

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/RMagick/rmilist.cpp', line 72

VALUE
ImageList_animate(int argc, VALUE *argv, VALUE self)
{
    Image *images;
    Info *info;
    VALUE info_obj;
    unsigned int delay;
#if defined(IMAGEMAGICK_7)
    ExceptionInfo *exception;
#endif

    if (argc == 1)
    {
        delay = NUM2UINT(argv[0]);
    }
    if (argc > 1)
    {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
    }

    // Create a new Info object to use with this call
    info_obj = rm_info_new();

    // Convert the images array to an images sequence.
    images = images_from_imagelist(self);

    if (argc == 1)
    {
        Image *img;

        for (img = images; img; img = GetNextImageInList(img))
        {
            img->delay = delay;
        }
    }

    TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
#if defined(IMAGEMAGICK_7)
    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(AnimateImages) args = { info, images, exception };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AnimateImages), &args);
    rm_split(images);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);
#else
    GVL_STRUCT_TYPE(AnimateImages) args = { info, images };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AnimateImages), &args);
    rm_split(images);
    rm_check_image_exception(images, RetainOnError);
#endif

    RB_GC_GUARD(info_obj);

    return self;
}

#append(stack_arg) ⇒ Magick::Image

Append all the images

Parameters:

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/RMagick/rmilist.cpp', line 135

VALUE
ImageList_append(VALUE self, VALUE stack_arg)
{
    Image *images, *new_image;
    MagickBooleanType stack;
    ExceptionInfo *exception;

    // Convert the image array to an image sequence.
    images = images_from_imagelist(self);

    // If stack == true, stack rectangular images top-to-bottom,
    // otherwise left-to-right.
    stack = (MagickBooleanType)RTEST(stack_arg);

    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(AppendImages) args = { images, stack, exception };
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AppendImages), &args);
    rm_split(images);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}

#averageMagick::Image

Average all images together

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/RMagick/rmilist.cpp', line 165

VALUE
ImageList_average(VALUE self)
{
    Image *images, *new_image;
    ExceptionInfo *exception;

    // Convert the images array to an images sequence.
    images = images_from_imagelist(self);

    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(EvaluateImages) args = { images, MeanEvaluateOperator, exception };
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(EvaluateImages), &args);
    rm_split(images);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}

#clearObject



1390
1391
1392
1393
# File 'lib/rmagick_internal.rb', line 1390

def clear
  @scene = nil
  @images.clear
end

#cloneObject



1395
1396
1397
1398
1399
# File 'lib/rmagick_internal.rb', line 1395

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

#coalesceMagick::ImageList

Composites a set of images while respecting any page offsets and disposal methods.

  • Respects the delay, matte, and start_loop fields in each image.

Returns:

  • (Magick::ImageList)

    a new image with the coalesced image sequence return stored in the images array



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/RMagick/rmilist.cpp', line 193

VALUE
ImageList_coalesce(VALUE self)
{
    Image *images, *new_images;
    ExceptionInfo *exception;

    // Convert the image array to an image sequence.
    images = images_from_imagelist(self);

    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(CoalesceImages) args = { images, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args);
    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_imagelist_from_images(new_images);
}

#collectObject Also known as: map, __map__

override Enumerable#collect



1402
1403
1404
1405
1406
1407
1408
1409
# File 'lib/rmagick_internal.rb', line 1402

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

#collect!Object Also known as: map!, __map__!



1411
1412
1413
1414
1415
# File 'lib/rmagick_internal.rb', line 1411

def collect!(&)
  @images.map!(&)
  assert_image_array @images
  self
end

#combineMagick::Image #combine(colorspace) ⇒ Magick::Image

Combines the images using the specified colorspace.

Overloads:

  • #combine(colorspace) ⇒ Magick::Image

    Parameters:

    • colorspace (Magick::ColorspaceType)

      the colorspace

Returns:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'ext/RMagick/rmilist.cpp', line 223

VALUE ImageList_combine(int argc, VALUE *argv, VALUE self)
{
#if defined(IMAGEMAGICK_6)
    ChannelType channel;
    ColorspaceType old_colorspace;
#endif
    ColorspaceType colorspace;
    long len;
    Image *images, *new_image;
    ExceptionInfo *exception;

    len = check_imagelist_length(self);

    switch (argc)
    {
        case 1:
            VALUE_TO_ENUM(argv[0], colorspace, ColorspaceType);
            break;
        case 0:
            colorspace = sRGBColorspace;
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (expected 1, got %d)", argc);
            break;
    }

#if defined(IMAGEMAGICK_7)
    if (len > 5)
    {
        rb_raise(rb_eArgError, "invalid number of images in this image list");
    }
    if (len == 5 && colorspace != CMYKColorspace)
    {
        rb_raise(rb_eArgError, "invalid number of images in this image list");
    }
#else
    channel = RedChannel;
    switch (len)
    {
        case 5:
            if (colorspace == CMYKColorspace)
                channel = (ChannelType)(channel | AlphaChannel);
            else
                rb_raise(rb_eArgError, "invalid number of images in this image list");
        case 4:
            if (colorspace == CMYKColorspace)
                channel = (ChannelType)(channel | IndexChannel);
            else
                channel = (ChannelType)(channel | AlphaChannel);
        case 3:
            channel = (ChannelType)(channel | GreenChannel);
            channel = (ChannelType)(channel | BlueChannel);
            break;
        case 2:
            channel = (ChannelType)(channel | AlphaChannel);
            break;
        case 1:
            break;
        default:
            rb_raise(rb_eArgError, "invalid number of images in this image list");
            break;
    }
#endif

    images = images_from_imagelist(self);
    exception = AcquireExceptionInfo();
#if defined(IMAGEMAGICK_6)
    old_colorspace = images->colorspace;
    SetImageColorspace(images, colorspace);
    GVL_STRUCT_TYPE(CombineImages) args = { images, channel, exception };
#else
    GVL_STRUCT_TYPE(CombineImages) args = { images, colorspace, exception };
#endif
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CombineImages), &args);

    rm_split(images);
#if defined(IMAGEMAGICK_6)
    images->colorspace = old_colorspace;
#endif
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}

#compactObject



1440
1441
1442
1443
1444
1445
1446
1447
# File 'lib/rmagick_internal.rb', line 1440

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

#compact!Object



1449
1450
1451
1452
1453
1454
# File 'lib/rmagick_internal.rb', line 1449

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

#composite_layers(images) ⇒ Magick::ImageList #composite_layers(images, composite_op) ⇒ Magick::ImageList

An image from source images is composited over an image from receiver’s list until one list is finished.

Overloads:

Returns:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/RMagick/rmilist.cpp', line 322

VALUE
ImageList_composite_layers(int argc, VALUE *argv, VALUE self)
{
    VALUE source_images;
    Image *dest, *source, *new_images;
    RectangleInfo geometry;
    CompositeOperator composite_op = OverCompositeOp;
    ExceptionInfo *exception;

    switch (argc)
    {
        case 2:
            VALUE_TO_ENUM(argv[1], composite_op, CompositeOperator);
        case 1:
            source_images = argv[0];
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (expected 1 or 2, got %d)", argc);
            break;
    }

    // Convert ImageLists to image sequences.
    dest = images_from_imagelist(self);
    new_images = clone_imagelist(dest);
    rm_split(dest);

    source = images_from_imagelist(source_images);

    SetGeometry(new_images, &geometry);
    ParseAbsoluteGeometry(new_images->geometry, &geometry);

    geometry.width  = source->page.width != 0 ? source->page.width : source->columns;
    geometry.height = source->page.height != 0 ? source->page.height : source->rows;
    GravityAdjustGeometry(new_images->page.width  != 0 ? new_images->page.width  : new_images->columns,
                          new_images->page.height != 0 ? new_images->page.height : new_images->rows,
                          new_images->gravity, &geometry);

    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(CompositeLayers) args = { new_images, composite_op, source, geometry.x, geometry.y, exception };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompositeLayers), &args);
    rm_split(source);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    RB_GC_GUARD(source_images);

    return rm_imagelist_from_images(new_images);
}

#concat(other) ⇒ Object



1456
1457
1458
1459
1460
1461
# File 'lib/rmagick_internal.rb', line 1456

def concat(other)
  assert_image_array other
  other.each { |image| @images << image }
  @scene = length - 1
  self
end

#copyObject

Make a deep copy



1418
1419
1420
1421
1422
1423
# File 'lib/rmagick_internal.rb', line 1418

def copy
  ditto = self.class.new
  @images.each { |f| ditto << f.copy }
  ditto.scene = @scene
  ditto
end

#cur_imageObject

Return the current image



1426
1427
1428
1429
# File 'lib/rmagick_internal.rb', line 1426

def cur_image
  Kernel.raise IndexError, 'no images in this list' unless @scene
  @images[@scene]
end

#deconstructMagick::ImageList

Compare each image with the next in a sequence and returns the maximum bounding region of any pixel differences it discovers.

Returns:



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/RMagick/rmilist.cpp', line 378

VALUE
ImageList_deconstruct(VALUE self)
{
    Image *new_images, *images;
    ExceptionInfo *exception;

    images = images_from_imagelist(self);
    exception = AcquireExceptionInfo();
#if defined(IMAGEMAGICK_7)
    GVL_STRUCT_TYPE(CompareImagesLayers) args = { images, CompareAnyLayer, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImagesLayers), &args);
#else
    GVL_STRUCT_TYPE(DeconstructImages) args = { images, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(DeconstructImages), &args);
#endif
    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_imagelist_from_images(new_images);
}

#delay=(d) ⇒ Object

Set same delay for all images

Raises:

  • (ArgumentError)


1464
1465
1466
1467
1468
# File 'lib/rmagick_internal.rb', line 1464

def delay=(d)
  raise ArgumentError, 'delay must be greater than or equal to 0' if Integer(d) < 0

  @images.each { |f| f.delay = Integer(d) }
end

#delete(obj) ⇒ Object



1470
1471
1472
1473
1474
1475
1476
# File 'lib/rmagick_internal.rb', line 1470

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

#delete_at(ndx) ⇒ Object



1478
1479
1480
1481
1482
1483
# File 'lib/rmagick_internal.rb', line 1478

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

#delete_ifObject



1485
1486
1487
1488
1489
1490
# File 'lib/rmagick_internal.rb', line 1485

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

#destroy!Object



1803
1804
1805
1806
# File 'lib/rmagick_internal.rb', line 1803

def destroy!
  @images.each(&:destroy!)
  self
end

#destroyed?Boolean

Returns:

  • (Boolean)


1808
1809
1810
# File 'lib/rmagick_internal.rb', line 1808

def destroyed?
  @images.all?(&:destroyed?)
end

#display {|info| ... } ⇒ Magick::ImageList Also known as: __display__

Display all the images to an X window screen.

Yields:

  • (info)

Yield Parameters:

Returns:



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'ext/RMagick/rmilist.cpp', line 408

VALUE
ImageList_display(VALUE self)
{
    Image *images;
    Info *info;
    VALUE info_obj;
#if defined(IMAGEMAGICK_7)
    ExceptionInfo *exception;
#endif

    // Create a new Info object to use with this call
    info_obj = rm_info_new();
    TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);

    // Convert the images array to an images sequence.
    images = images_from_imagelist(self);
#if defined(IMAGEMAGICK_7)
    exception = AcquireExceptionInfo();
    DisplayImages(info, images, exception);
    rm_split(images);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);
#else
    DisplayImages(info, images);
    rm_split(images);
    rm_check_image_exception(images, RetainOnError);
#endif

    RB_GC_GUARD(info_obj);

    return self;
}

#dupObject



1492
1493
1494
1495
1496
1497
# File 'lib/rmagick_internal.rb', line 1492

def dup
  ditto = self.class.new
  @images.each { |img| ditto << img }
  ditto.scene = @scene
  ditto
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/rmagick_internal.rb', line 1499

def eql?(other)
  begin
    assert_image_array other
  rescue ArgumentError
    return false
  end

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

#fill(*args, &block) ⇒ Object



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

def fill(*args, &block)
  assert_image args[0] unless block
  current = get_current
  @images.fill(*args, &block)
  assert_image_array self
  set_current current
  self
end

#find_allObject Also known as: select

Override Enumerable’s find_all



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

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

#flatten_imagesMagick::ImageList

Merge all the images into a single image.

Returns:



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'ext/RMagick/rmilist.cpp', line 447

VALUE
ImageList_flatten_images(VALUE self)
{
    Image *images, *new_image;
    ExceptionInfo *exception;

    images = images_from_imagelist(self);
    exception = AcquireExceptionInfo();

    GVL_STRUCT_TYPE(MergeImageLayers) args = { images, FlattenLayer, exception };
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);

    rm_split(images);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}

#from_blob(*blobs, &block) ⇒ Object



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

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

#insert(index, *args) ⇒ Object



1555
1556
1557
1558
1559
1560
1561
# File 'lib/rmagick_internal.rb', line 1555

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

#inspectObject

Call inspect for all the images



1564
1565
1566
1567
# File 'lib/rmagick_internal.rb', line 1564

def inspect
  img = @images.map(&:inspect)
  '[' + img.join(",\n") + "]\nscene=#{@scene}"
end

#iterations=(n) ⇒ Object

Set the number of iterations of an animated GIF



1570
1571
1572
1573
1574
# File 'lib/rmagick_internal.rb', line 1570

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

#last(*args) ⇒ Object



1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
# File 'lib/rmagick_internal.rb', line 1576

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

#marshal_dumpObject

Custom marshal/unmarshal for Ruby 1.8.



1590
1591
1592
1593
1594
# File 'lib/rmagick_internal.rb', line 1590

def marshal_dump
  ary = [@scene]
  @images.each { |i| ary << Marshal.dump(i) }
  ary
end

#marshal_load(ary) ⇒ Object



1596
1597
1598
1599
1600
# File 'lib/rmagick_internal.rb', line 1596

def marshal_load(ary)
  @scene = ary.shift
  @images = []
  ary.each { |a| @images << Marshal.load(a) } # rubocop:disable Security/MarshalLoad
end

#montageMagick::ImageList #montage {|opt| ... } ⇒ Magick::ImageList

Tile one or more thumbnails across an image canvas.

Overloads:

Returns:



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'ext/RMagick/rmilist.cpp', line 480

VALUE
ImageList_montage(VALUE self)
{
    VALUE montage_obj;
    Montage *montage;
    Image *new_images, *images;
    ExceptionInfo *exception;

    // Create a new instance of the Magick::ImageList::Montage class
    montage_obj = rm_montage_new();
    if (rb_block_given_p())
    {
        rb_yield(montage_obj);
    }

    TypedData_Get_Struct(montage_obj, Montage, &rm_montage_data_type, montage);

    images = images_from_imagelist(self);

    for (Image *image = images; image; image = GetNextImageInList(image))
    {
        if (montage->compose != UndefinedCompositeOp)
        {
            image->compose = montage->compose;
        }
        image->background_color = montage->info->background_color;
        image->border_color = montage->info->border_color;
        image->matte_color = montage->info->matte_color;
        image->gravity = montage->info->gravity;
    }

    exception = AcquireExceptionInfo();

    // MontageImage can return more than one image.
    GVL_STRUCT_TYPE(MontageImages) args = { images, montage->info, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MontageImages), &args);
    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    RB_GC_GUARD(montage_obj);

    return rm_imagelist_from_images(new_images);
}

#morph(nimages) ⇒ Magick::ImageList

Note:

Sets @scenes to 0

Requires a minimum of two images. The first image is transformed into the second by a number of intervening images as specified by “nimages”.

Parameters:

  • nimages (Numeric)

    the number of images

Returns:

  • (Magick::ImageList)

    a new image list with the images array set to the morph sequence.



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'ext/RMagick/rmilist.cpp', line 534

VALUE
ImageList_morph(VALUE self, VALUE nimages)
{
    Image *images, *new_images;
    ExceptionInfo *exception;
    size_t number_images;


    // Use a signed long so we can test for a negative argument.
    if (NUM2LONG(nimages) <= 0)
    {
        rb_raise(rb_eArgError, "number of intervening images must be > 0");
    }

    number_images = NUM2LONG(nimages);
    images = images_from_imagelist(self);
    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(MorphImages) args = { images, number_images, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MorphImages), &args);
    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_imagelist_from_images(new_images);
}

#mosaicMagick::Image

Merge all the images into a single image.

Returns:



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'ext/RMagick/rmilist.cpp', line 566

VALUE
ImageList_mosaic(VALUE self)
{
    Image *images, *new_image;
    ExceptionInfo *exception;

    images = images_from_imagelist(self);

    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(MergeImageLayers) args = { images, MosaicLayer, exception };
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);

    rm_split(images);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}

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

Create a new image and add it to the end



1622
1623
1624
# File 'lib/rmagick_internal.rb', line 1622

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

#optimize_layers(method) ⇒ Magick::ImageList

Optimizes or compares the images in the list. Equivalent to the -layers option in ImageMagick’s mogrify command.

Parameters:

  • method (Magick::LayerMethod)

    the method to use

Returns:



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'ext/RMagick/rmilist.cpp', line 593

VALUE
ImageList_optimize_layers(VALUE self, VALUE method)
{
    Image *images, *new_images, *new_images2;
    LayerMethod mthd;
    ExceptionInfo *exception;
    QuantizeInfo quantize_info;

    new_images2 = NULL;     // defeat "unused variable" message

    VALUE_TO_ENUM(method, mthd, LayerMethod);
    images = images_from_imagelist(self);

    exception = AcquireExceptionInfo();
    switch (mthd)
    {
        case CoalesceLayer:
            {
                GVL_STRUCT_TYPE(CoalesceImages) args = { images, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args);
            }
            break;
        case DisposeLayer:
            {
                GVL_STRUCT_TYPE(DisposeImages) args = { images, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(DisposeImages), &args);
            }
            break;
        case OptimizeTransLayer:
            {
                new_images = clone_imagelist(images);
                GVL_STRUCT_TYPE(OptimizeImageTransparency) args = { new_images, exception };
                CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageTransparency), &args);
            }
            break;
        case RemoveDupsLayer:
            {
                new_images = clone_imagelist(images);
                GVL_STRUCT_TYPE(RemoveDuplicateLayers) args = { &new_images, exception };
                CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemoveDuplicateLayers), &args);
            }
            break;
        case RemoveZeroLayer:
            {
                new_images = clone_imagelist(images);
                GVL_STRUCT_TYPE(RemoveZeroDelayLayers) args = { &new_images, exception };
                CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemoveZeroDelayLayers), &args);
            }
            break;
        case CompositeLayer:
            rm_split(images);
            DestroyExceptionInfo(exception);
            rb_raise(rb_eNotImpError, "Magick::CompositeLayer is not supported. Use the composite_layers method instead.");
            break;
            // In 6.3.4-ish, OptimizeImageLayer replaced OptimizeLayer
        case OptimizeImageLayer:
            {
                GVL_STRUCT_TYPE(OptimizeImageLayers) args = { images, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageLayers), &args);
            }
            break;
            // and OptimizeLayer became a "General Purpose, GIF Animation Optimizer" (ref. mogrify.c)
        case OptimizeLayer:
            {
                GVL_STRUCT_TYPE(CoalesceImages) args_CoalesceImages = { images, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args_CoalesceImages);
                rm_split(images);
                rm_check_exception(exception, new_images, DestroyOnError);

                GVL_STRUCT_TYPE(OptimizeImageLayers) args_OptimizeImageLayers = { new_images, exception };
                new_images2 = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageLayers), &args_OptimizeImageLayers);
                DestroyImageList(new_images);
                rm_check_exception(exception, new_images2, DestroyOnError);

                new_images = new_images2;
                GVL_STRUCT_TYPE(OptimizeImageTransparency) args_OptimizeImageTransparency = { new_images, exception };
                CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageTransparency), &args_OptimizeImageTransparency);
                rm_check_exception(exception, new_images, DestroyOnError);
                // mogrify supports -dither here. We don't.
                GetQuantizeInfo(&quantize_info);
#if defined(IMAGEMAGICK_7)
                GVL_STRUCT_TYPE(RemapImages) args_RemapImages = { &quantize_info, new_images, NULL, exception };
#else
                GVL_STRUCT_TYPE(RemapImages) args_RemapImages = { &quantize_info, new_images, NULL };
#endif
                CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args_RemapImages);

            }
            break;
        case OptimizePlusLayer:
            {
                GVL_STRUCT_TYPE(OptimizePlusImageLayers) args = { images, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizePlusImageLayers), &args);
            }
            break;
        case CompareAnyLayer:
        case CompareClearLayer:
        case CompareOverlayLayer:
            {
#if defined(IMAGEMAGICK_7)
                GVL_STRUCT_TYPE(CompareImagesLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImagesLayers), &args);
#else
                GVL_STRUCT_TYPE(CompareImageLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImageLayers), &args);
#endif
            }
            break;
        case MosaicLayer:
            {
                GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
            }
            break;
        case FlattenLayer:
            {
                GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
            }
            break;
        case MergeLayer:
            {
                GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
            }
            break;
        case TrimBoundsLayer:
            {
                GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
                new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
            }
            break;
        default:
            rm_split(images);
            DestroyExceptionInfo(exception);
            rb_raise(rb_eArgError, "undefined layer method");
            break;
    }

    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_imagelist_from_images(new_images);
}

#partitionObject



1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
# File 'lib/rmagick_internal.rb', line 1626

def partition(&)
  a = @images.partition(&)
  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



1638
1639
1640
1641
1642
1643
1644
1645
# File 'lib/rmagick_internal.rb', line 1638

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

#popObject



1647
1648
1649
1650
1651
1652
# File 'lib/rmagick_internal.rb', line 1647

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

#push(*objs) ⇒ Object



1654
1655
1656
1657
1658
1659
1660
1661
# File 'lib/rmagick_internal.rb', line 1654

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

#quantize(number_colors = 256, colorspace = Magick::RGBColorsapce, dither = true, tree_depth = 0, measure_error = false) ⇒ Magick::ImageList

Analyzes the colors within a set of reference images and chooses a fixed number of colors to represent the set. The goal of the algorithm is to minimize the difference between the input and output images while minimizing the processing time.

Note:

Sets @scene to the same value as self.scene

Returns a new ImageList with quantized images.

Parameters:

  • number_colors (Numeric) (defaults to: 256)

    the maximum number of colors to use in the output images.

  • colorspace (Magick::ColorspaceType) (defaults to: Magick::RGBColorsapce)

    the colorspace to quantize in.

  • dither (Magick::DitherMethod, Boolean) (defaults to: true)

    a DitherMethod value or true if you want apply dither.

  • tree_depth (Numeric) (defaults to: 0)

    specify the tree depth to use while quantizing.

  • measure_error (Boolean) (defaults to: false)

    calculate quantization errors when quantizing the image.

Returns:



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'ext/RMagick/rmilist.cpp', line 946

VALUE
ImageList_quantize(int argc, VALUE *argv, VALUE self)
{
    Image *images, *new_images;
    Image *new_image;
    QuantizeInfo quantize_info;
    ExceptionInfo *exception;
    VALUE new_imagelist, scene;

    GetQuantizeInfo(&quantize_info);

    switch (argc)
    {
        case 5:
            quantize_info.measure_error = (MagickBooleanType) RTEST(argv[4]);
        case 4:
            quantize_info.tree_depth = (unsigned long)NUM2INT(argv[3]);
        case 3:
#if defined(IMAGEMAGICK_7)
            if (rb_obj_is_kind_of(argv[2], Class_DitherMethod))
            {
                VALUE_TO_ENUM(argv[2], quantize_info.dither_method, DitherMethod);
            }
            else
            {
                quantize_info.dither_method = RTEST(argv[2]) ? UndefinedDitherMethod : NoDitherMethod;
            }
#else
            if (rb_obj_is_kind_of(argv[2], Class_DitherMethod))
            {
                VALUE_TO_ENUM(argv[2], quantize_info.dither_method, DitherMethod);
                quantize_info.dither = (MagickBooleanType)(quantize_info.dither_method != NoDitherMethod);
            }
            else
            {
                quantize_info.dither = (MagickBooleanType) RTEST(argv[2]);
            }
#endif
        case 2:
            VALUE_TO_ENUM(argv[1], quantize_info.colorspace, ColorspaceType);
        case 1:
            quantize_info.number_colors = NUM2ULONG(argv[0]);
        case 0:
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 5)", argc);
            break;
    }


    // Convert image array to image sequence, clone image sequence.
    images = images_from_imagelist(self);
    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(CloneImageList) args_CloneImageList = { images, exception };
    new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CloneImageList), &args_CloneImageList);
    rm_split(images);
    rm_check_exception(exception, new_images, DestroyOnError);

    rm_ensure_result(new_images);

#if defined(IMAGEMAGICK_7)
    GVL_STRUCT_TYPE(QuantizeImages) args_QuantizeImages = { &quantize_info, new_images, exception };
#else
    GVL_STRUCT_TYPE(QuantizeImages) args_QuantizeImages = { &quantize_info, new_images };
#endif
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(QuantizeImages), &args_QuantizeImages);
    rm_check_exception(exception, new_images, DestroyOnError);
    DestroyExceptionInfo(exception);

    // Create new ImageList object, convert mapped image sequence to images,
    // append to images array.
    new_imagelist = ImageList_new();
    while ((new_image = RemoveFirstImageFromList(&new_images)))
    {
        imagelist_push(new_imagelist, rm_image_new(new_image));
    }

    // Set @scene in new ImageList object to same value as in self.
    scene = rb_iv_get(self, "@scene");
    rb_iv_set(new_imagelist, "@scene", scene);

    RB_GC_GUARD(new_imagelist);
    RB_GC_GUARD(scene);

    return new_imagelist;
}

#read(*files, &block) ⇒ Object

Read files and concatenate the new images



1664
1665
1666
1667
1668
1669
1670
1671
# File 'lib/rmagick_internal.rb', line 1664

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

#rejectObject

override Enumerable’s reject



1674
1675
1676
1677
1678
1679
1680
1681
# File 'lib/rmagick_internal.rb', line 1674

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

#reject!Object



1683
1684
1685
1686
1687
1688
1689
# File 'lib/rmagick_internal.rb', line 1683

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

#remap(remap_image = nil, dither_method = Magick::RiemersmaDitherMethod) ⇒ Magick::ImageList Also known as: affinity

Reduce the colors used in the image list to the set of colors in remap_image.

Note:

Modifies images in-place.

Returns self.

Parameters:

  • remap_image (Magick::Image, Magick::ImageList) (defaults to: nil)

    Either an imagelist or an image. If an imagelist, uses the current image.

  • dither_method (Magick::DitherMethod) (defaults to: Magick::RiemersmaDitherMethod)

    a DitherMethod value.

Returns:



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'ext/RMagick/rmilist.cpp', line 1044

VALUE
ImageList_remap(int argc, VALUE *argv, VALUE self)
{
    Image *images, *remap_image = NULL;
    QuantizeInfo quantize_info;
#if defined(IMAGEMAGICK_7)
    ExceptionInfo *exception;
#endif

    if (argc > 0 && argv[0] != Qnil)
    {
        VALUE t = rm_cur_image(argv[0]);
        remap_image = rm_check_destroyed(t);
        RB_GC_GUARD(t);
    }

    GetQuantizeInfo(&quantize_info);

    if (argc > 1)
    {
        VALUE_TO_ENUM(argv[1], quantize_info.dither_method, DitherMethod);
#if defined(IMAGEMAGICK_6)
        quantize_info.dither = MagickTrue;
#endif
    }
    if (argc > 2)
    {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
    }

    images = images_from_imagelist(self);

#if defined(IMAGEMAGICK_7)
    exception = AcquireExceptionInfo();
    GVL_STRUCT_TYPE(RemapImages) args = { &quantize_info, images, remap_image, exception };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args);
    rm_split(images);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);
#else
    GVL_STRUCT_TYPE(RemapImages) args = { &quantize_info, images, remap_image };
    CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args);
    rm_split(images);
    rm_check_image_exception(images, RetainOnError);
#endif

    return self;
}

#replace(other) ⇒ Object



1691
1692
1693
1694
1695
1696
1697
1698
1699
# File 'lib/rmagick_internal.rb', line 1691

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

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

Returns:

  • (Boolean)


1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/rmagick_internal.rb', line 1703

def respond_to?(meth_id, priv = false)
  return true if __respond_to__?(meth_id, priv)

  if @scene
    @images[@scene].respond_to?(meth_id, priv)
  else
    super
  end
end

#reverseObject



1713
1714
1715
1716
1717
1718
1719
# File 'lib/rmagick_internal.rb', line 1713

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

#reverse!Object



1721
1722
1723
1724
1725
1726
# File 'lib/rmagick_internal.rb', line 1721

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

#reverse_eachObject



1728
1729
1730
1731
# File 'lib/rmagick_internal.rb', line 1728

def reverse_each(&)
  @images.reverse_each(&)
  self
end

#shiftObject



1733
1734
1735
1736
1737
1738
# File 'lib/rmagick_internal.rb', line 1733

def shift
  current = get_current
  a = @images.shift
  set_current current
  a
end

#slice(*args) ⇒ Object



1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
# File 'lib/rmagick_internal.rb', line 1740

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

#slice!(*args) ⇒ Object



1755
1756
1757
1758
1759
1760
# File 'lib/rmagick_internal.rb', line 1755

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

#sort!(*args) ⇒ Object



1385
1386
1387
1388
# File 'lib/rmagick_internal.rb', line 1385

def sort!(*args, &)
  @images.sort!(*args, &)
  self
end

#ticks_per_second=(t) ⇒ Object



1762
1763
1764
1765
# File 'lib/rmagick_internal.rb', line 1762

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

#to_aObject



1767
1768
1769
# File 'lib/rmagick_internal.rb', line 1767

def to_a
  @images.map { |image| image }
end

#to_blobString #to_blob {|info| ... } ⇒ String

Return the imagelist as a blob (a String).

Overloads:

  • #to_blob {|info| ... } ⇒ String

    Runs an info parm block if present - the user can specify the image format and depth

    Yields:

    • (info)

    Yield Parameters:

Returns:

  • (String)

    the blob



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'ext/RMagick/rmilist.cpp', line 1106

VALUE
ImageList_to_blob(VALUE self)
{
    Image *images, *image;
    Info *info;
    VALUE info_obj;
    VALUE blob_str;
    void *blob = NULL;
    size_t length = 0;
    ExceptionInfo *exception;

    info_obj = rm_info_new();
    TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);

    // Convert the images array to an images sequence.
    images = images_from_imagelist(self);

    exception = AcquireExceptionInfo();
    SetImageInfo(info, MagickTrue, exception);
    rm_check_exception(exception, images, RetainOnError);

    if (*info->magick != '\0')
    {
        Image *img;
        for (img = images; img; img = GetNextImageInList(img))
        {
            strlcpy(img->magick, info->magick, sizeof(img->magick));
        }
    }

    for (image = images; image; image = GetNextImageInList(image))
    {
        rm_sync_image_options(image, info);
    }

    // Unconditionally request multi-images support. The worst that
    // can happen is that there's only one image or the format
    // doesn't support multi-image files.
    info->adjoin = MagickTrue;
    GVL_STRUCT_TYPE(ImagesToBlob) args = { info, images, &length, exception };
    blob = CALL_FUNC_WITHOUT_GVL(GVL_FUNC(ImagesToBlob), &args);
    if (blob && exception->severity >= ErrorException)
    {
        magick_free((void*)blob);
        blob = NULL;
        length = 0;
    }
    rm_split(images);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);


    if (length == 0 || !blob)
    {
        return Qnil;
    }

    blob_str = rb_str_new((const char *)blob, (long)length);
    magick_free((void*)blob);

    RB_GC_GUARD(info_obj);
    RB_GC_GUARD(blob_str);

    return blob_str;
}

#uniqObject



1771
1772
1773
1774
1775
1776
1777
# File 'lib/rmagick_internal.rb', line 1771

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

#uniq!(*_args) ⇒ Object



1779
1780
1781
1782
1783
1784
# File 'lib/rmagick_internal.rb', line 1779

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

#unshift(obj) ⇒ Object



1787
1788
1789
1790
1791
1792
# File 'lib/rmagick_internal.rb', line 1787

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

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



1794
1795
1796
1797
1798
1799
# File 'lib/rmagick_internal.rb', line 1794

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

#write(file) {|info| ... } ⇒ Object

Write all the images to the specified file. If the file format supports multi-image files, and the ‘images’ array contains more than one image, then the images will be written as a single multi-image file. Otherwise each image will be written to a separate file.

Parameters:

  • file (File, String)

    the file

Yields:

  • (info)

Yield Parameters:



1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
# File 'ext/RMagick/rmilist.cpp', line 1183

VALUE
ImageList_write(VALUE self, VALUE file)
{
    Image *images, *img;
    Info *info;
    const MagickInfo *m;
    VALUE info_obj;
    unsigned long scene;
    ExceptionInfo *exception;

    info_obj = rm_info_new();
    TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);


    if (TYPE(file) == T_FILE)
    {
        rb_io_t *fptr;

        // Ensure file is open - raise error if not
        GetOpenFile(file, fptr);
        rb_io_check_writable(fptr);

        add_format_prefix(info, rm_io_path(file));
#if defined(_WIN32) && !defined(IMAGEMAGICK_GREATER_THAN_EQUAL_7_1_2)
        SetImageInfoFile(info, NULL);
#else
        SetImageInfoFile(info, rb_io_stdio_file(fptr));
#endif
    }
    else
    {
        add_format_prefix(info, file);
        SetImageInfoFile(info, NULL);
    }

    // Convert the images array to an images sequence.
    images = images_from_imagelist(self);

    // Copy the filename into each image. Set a scene number to be used if
    // writing multiple files. (Ref: ImageMagick's utilities/convert.c
    for (scene = 0, img = images; img; img = GetNextImageInList(img))
    {
        img->scene = scene++;
        strlcpy(img->filename, info->filename, sizeof(img->filename));
    }

    // Find out if the format supports multi-images files.
    exception = AcquireExceptionInfo();
    SetImageInfo(info, MagickTrue, exception);
    rm_check_exception(exception, images, RetainOnError);

    m = GetMagickInfo(info->magick, exception);
    rm_check_exception(exception, images, RetainOnError);
#if defined(IMAGEMAGICK_6)
    DestroyExceptionInfo(exception);
#endif

    // Tell WriteImage if we want a multi-images file.
    if (imagelist_length(self) > 1L && m && GetMagickAdjoin(m))
    {
        info->adjoin = MagickTrue;
    }

    for (img = images; img; img = GetNextImageInList(img))
    {
        rm_sync_image_options(img, info);
#if defined(IMAGEMAGICK_7)
        GVL_STRUCT_TYPE(WriteImage) args = { info, img, exception };
        CALL_FUNC_WITHOUT_GVL(GVL_FUNC(WriteImage), &args);
        rm_check_exception(exception, img, RetainOnError);
#else
        GVL_STRUCT_TYPE(WriteImage) args = { info, img };
        CALL_FUNC_WITHOUT_GVL(GVL_FUNC(WriteImage), &args);
        // images will be split before raising an exception
        rm_check_image_exception(images, RetainOnError);
#endif
        if (info->adjoin)
        {
            break;
        }
    }

#if defined(IMAGEMAGICK_7)
    DestroyExceptionInfo(exception);
#endif

    rm_split(images);

    RB_GC_GUARD(info_obj);

    return self;
}