rmagick 7.1.7 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,16 +11,37 @@
11
11
 
12
12
  #include "rmagick.h"
13
13
 
14
+ enum LinkMode
15
+ {
16
+ LinkRead,
17
+ LinkChange,
18
+ LinkWrite
19
+ };
20
+
14
21
  struct ImagesFromImageList_Args
15
22
  {
16
23
  VALUE imagelist;
17
- VALUE *clones;
24
+ VALUE *linked;
18
25
  Image *images;
26
+ LinkMode mode;
27
+ VALUE held;
19
28
  };
20
29
 
21
- static Image *clone_imagelist(Image *);
22
- static Image *images_from_imagelist(VALUE, VALUE *);
30
+ struct LinkedImages_Args
31
+ {
32
+ Image *images;
33
+ Image *image;
34
+ Info *info;
35
+ Image *new_images;
36
+ VALUE image_ary;
37
+ };
38
+
39
+ static Image *clone_linked_imagelist(Image *, VALUE, ExceptionInfo *);
40
+ static void sync_linked_image_options(Image *, Image *, Info *, VALUE, ExceptionInfo *);
41
+ static Image *images_from_imagelist(VALUE, VALUE *, LinkMode mode = LinkRead, VALUE held = Qnil);
42
+ static void check_images(VALUE, Image *(*)(VALUE));
23
43
  static VALUE images_from_imagelist_protected(VALUE);
44
+ static VALUE imagelist_images(VALUE);
24
45
  static long imagelist_length(VALUE);
25
46
  static long check_imagelist_length(VALUE);
26
47
  static void imagelist_push(VALUE, VALUE);
@@ -62,6 +83,15 @@ DEFINE_GVL_VOID_STUB2(RemoveDuplicateLayers, Image **, ExceptionInfo *);
62
83
  DEFINE_GVL_VOID_STUB2(RemoveZeroDelayLayers, Image **, ExceptionInfo *);
63
84
 
64
85
 
86
+ // Destroy the image list that *ptr points to when the cleanup runs, since the
87
+ // call may replace the first image of the list.
88
+ static void
89
+ destroy_list_at(void *ptr, intptr_t arg ATTRIBUTE_UNUSED)
90
+ {
91
+ DestroyImageList(*(Image **)ptr);
92
+ }
93
+
94
+
65
95
  /**
66
96
  * Repeatedly display the images in the images array to an XWindow screen. The
67
97
  * +delay+ argument is the number of 1/100ths of a second (0 to 65535) to delay
@@ -101,8 +131,12 @@ ImageList_animate(int argc, VALUE *argv, VALUE self)
101
131
  info_obj = rm_info_new();
102
132
 
103
133
  // Convert the images array to an images sequence.
104
- VALUE clones;
105
- images = images_from_imagelist(self, &clones);
134
+ VALUE linked;
135
+ if (argc == 1)
136
+ {
137
+ check_images(self, rm_check_writable);
138
+ }
139
+ images = images_from_imagelist(self, &linked, LinkChange);
106
140
 
107
141
  if (argc == 1)
108
142
  {
@@ -114,22 +148,22 @@ ImageList_animate(int argc, VALUE *argv, VALUE self)
114
148
  }
115
149
  }
116
150
 
117
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
151
+ info = rm_info_get(info_obj);
118
152
  #if defined(IMAGEMAGICK_7)
119
153
  exception = AcquireExceptionInfo();
120
- GVL_STRUCT_TYPE(AnimateImages) args = { info, images, exception };
121
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AnimateImages), &args);
154
+ DECLARE_GVL_CALL(call, AnimateImages, info, images, exception);
155
+ call.update_each(linked).read(info_obj).split(images).release(exception).run<void>();
122
156
  rm_split(images);
123
157
  CHECK_EXCEPTION();
124
158
  DestroyExceptionInfo(exception);
125
159
  #else
126
- GVL_STRUCT_TYPE(AnimateImages) args = { info, images };
127
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AnimateImages), &args);
160
+ DECLARE_GVL_CALL(call, AnimateImages, info, images);
161
+ call.update_each(linked).read(info_obj).split(images).run<void>();
128
162
  rm_split(images);
129
163
  rm_check_image_exception(images, RetainOnError);
130
164
  #endif
131
165
 
132
- RB_GC_GUARD(clones);
166
+ RB_GC_GUARD(linked);
133
167
  RB_GC_GUARD(info_obj);
134
168
 
135
169
  return self;
@@ -150,18 +184,18 @@ ImageList_append(VALUE self, VALUE stack_arg)
150
184
  ExceptionInfo *exception;
151
185
 
152
186
  // Convert the image array to an image sequence.
153
- VALUE clones;
154
- images = images_from_imagelist(self, &clones);
187
+ VALUE linked;
188
+ images = images_from_imagelist(self, &linked);
155
189
 
156
190
  // If stack == true, stack rectangular images top-to-bottom,
157
191
  // otherwise left-to-right.
158
192
  stack = (MagickBooleanType)RTEST(stack_arg);
159
193
 
160
194
  exception = AcquireExceptionInfo();
161
- GVL_STRUCT_TYPE(AppendImages) args = { images, stack, exception };
162
- new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(AppendImages), &args);
195
+ DECLARE_GVL_CALL(call, AppendImages, images, stack, exception);
196
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
163
197
  rm_split(images);
164
- RB_GC_GUARD(clones);
198
+ RB_GC_GUARD(linked);
165
199
  rm_check_exception(exception, new_image, DestroyOnError);
166
200
  DestroyExceptionInfo(exception);
167
201
 
@@ -181,14 +215,14 @@ ImageList_average(VALUE self)
181
215
  ExceptionInfo *exception;
182
216
 
183
217
  // Convert the images array to an images sequence.
184
- VALUE clones;
185
- images = images_from_imagelist(self, &clones);
218
+ VALUE linked;
219
+ images = images_from_imagelist(self, &linked);
186
220
 
187
221
  exception = AcquireExceptionInfo();
188
- GVL_STRUCT_TYPE(EvaluateImages) args = { images, MeanEvaluateOperator, exception };
189
- new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(EvaluateImages), &args);
222
+ DECLARE_GVL_CALL(call, EvaluateImages, images, MeanEvaluateOperator, exception);
223
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
190
224
  rm_split(images);
191
- RB_GC_GUARD(clones);
225
+ RB_GC_GUARD(linked);
192
226
  rm_check_exception(exception, new_image, DestroyOnError);
193
227
  DestroyExceptionInfo(exception);
194
228
 
@@ -211,14 +245,14 @@ ImageList_coalesce(VALUE self)
211
245
  ExceptionInfo *exception;
212
246
 
213
247
  // Convert the image array to an image sequence.
214
- VALUE clones;
215
- images = images_from_imagelist(self, &clones);
248
+ VALUE linked;
249
+ images = images_from_imagelist(self, &linked);
216
250
 
217
251
  exception = AcquireExceptionInfo();
218
- GVL_STRUCT_TYPE(CoalesceImages) args = { images, exception };
219
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args);
252
+ DECLARE_GVL_CALL(call, CoalesceImages, images, exception);
253
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
220
254
  rm_split(images);
221
- RB_GC_GUARD(clones);
255
+ RB_GC_GUARD(linked);
222
256
  rm_check_exception(exception, new_images, DestroyOnError);
223
257
  DestroyExceptionInfo(exception);
224
258
 
@@ -240,7 +274,7 @@ VALUE ImageList_combine(int argc, VALUE *argv, VALUE self)
240
274
  {
241
275
  #if defined(IMAGEMAGICK_6)
242
276
  ChannelType channel;
243
- ColorspaceType old_colorspace;
277
+ Image *head;
244
278
  #endif
245
279
  ColorspaceType colorspace;
246
280
  long len;
@@ -300,22 +334,38 @@ VALUE ImageList_combine(int argc, VALUE *argv, VALUE self)
300
334
  }
301
335
  #endif
302
336
 
303
- VALUE clones;
304
- images = images_from_imagelist(self, &clones);
337
+ VALUE linked;
338
+ images = images_from_imagelist(self, &linked);
305
339
  exception = AcquireExceptionInfo();
306
340
  #if defined(IMAGEMAGICK_6)
307
- old_colorspace = images->colorspace;
308
- SetImageColorspace(images, colorspace);
309
- GVL_STRUCT_TYPE(CombineImages) args = { images, channel, exception };
341
+ head = CloneImage(images, 0, 0, MagickTrue, exception);
342
+ if (!head)
343
+ {
344
+ rm_split(images);
345
+ rm_check_exception(exception, NULL, DestroyOnError);
346
+ DestroyExceptionInfo(exception);
347
+ rb_raise(rb_eNoMemError, "not enough memory to continue");
348
+ }
349
+ SetImageColorspace(head, colorspace);
350
+ head->next = images->next;
351
+ if (head->next)
352
+ {
353
+ head->next->previous = head;
354
+ }
355
+ images->next = NULL;
356
+ images = head;
357
+ DECLARE_GVL_CALL(call, CombineImages, images, channel, exception);
358
+ call.read_each(linked).split(images).destroy(head);
310
359
  #else
311
- GVL_STRUCT_TYPE(CombineImages) args = { images, colorspace, exception };
360
+ DECLARE_GVL_CALL(call, CombineImages, images, colorspace, exception);
361
+ call.read_each(linked).split(images);
312
362
  #endif
313
- new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CombineImages), &args);
363
+ new_image = call.release(exception).run<Image *>();
314
364
 
315
365
  rm_split(images);
316
- RB_GC_GUARD(clones);
366
+ RB_GC_GUARD(linked);
317
367
  #if defined(IMAGEMAGICK_6)
318
- images->colorspace = old_colorspace;
368
+ DestroyImage(head);
319
369
  #endif
320
370
  rm_check_exception(exception, new_image, DestroyOnError);
321
371
  DestroyExceptionInfo(exception);
@@ -359,15 +409,14 @@ ImageList_composite_layers(int argc, VALUE *argv, VALUE self)
359
409
  }
360
410
 
361
411
  // Convert ImageLists to image sequences.
362
- VALUE clones, source_clones = Qnil;
363
- dest = images_from_imagelist(self, &clones);
364
- new_images = clone_imagelist(dest);
412
+ VALUE linked, source_linked = Qnil;
413
+ dest = images_from_imagelist(self, &linked);
414
+ new_images = clone_linked_imagelist(dest, linked, NULL);
365
415
  rm_split(dest);
366
- RB_GC_GUARD(clones);
367
416
 
368
417
  // new_images is not owned by any Ruby object yet, so resolving the source
369
418
  // list has to be able to unwind without abandoning it.
370
- struct ImagesFromImageList_Args source_args = { source_images, &source_clones, NULL };
419
+ struct ImagesFromImageList_Args source_args = { source_images, &source_linked, NULL, LinkRead, linked };
371
420
  int state = 0;
372
421
  rb_protect(images_from_imagelist_protected, (VALUE)&source_args, &state);
373
422
  if (state)
@@ -387,10 +436,11 @@ ImageList_composite_layers(int argc, VALUE *argv, VALUE self)
387
436
  new_images->gravity, &geometry);
388
437
 
389
438
  exception = AcquireExceptionInfo();
390
- GVL_STRUCT_TYPE(CompositeLayers) args = { new_images, composite_op, source, geometry.x, geometry.y, exception };
391
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompositeLayers), &args);
439
+ DECLARE_GVL_CALL(call, CompositeLayers, new_images, composite_op, source, geometry.x, geometry.y, exception);
440
+ call.read_each(source_linked).read_each(linked).split(source).destroy(new_images).release(exception).run<void>();
392
441
  rm_split(source);
393
- RB_GC_GUARD(source_clones);
442
+ RB_GC_GUARD(source_linked);
443
+ RB_GC_GUARD(linked);
394
444
  rm_check_exception(exception, new_images, DestroyOnError);
395
445
  DestroyExceptionInfo(exception);
396
446
 
@@ -412,18 +462,18 @@ ImageList_deconstruct(VALUE self)
412
462
  Image *new_images, *images;
413
463
  ExceptionInfo *exception;
414
464
 
415
- VALUE clones;
416
- images = images_from_imagelist(self, &clones);
465
+ VALUE linked;
466
+ images = images_from_imagelist(self, &linked);
417
467
  exception = AcquireExceptionInfo();
418
468
  #if defined(IMAGEMAGICK_7)
419
- GVL_STRUCT_TYPE(CompareImagesLayers) args = { images, CompareAnyLayer, exception };
420
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImagesLayers), &args);
469
+ DECLARE_GVL_CALL(call, CompareImagesLayers, images, CompareAnyLayer, exception);
470
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
421
471
  #else
422
- GVL_STRUCT_TYPE(DeconstructImages) args = { images, exception };
423
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(DeconstructImages), &args);
472
+ DECLARE_GVL_CALL(call, DeconstructImages, images, exception);
473
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
424
474
  #endif
425
475
  rm_split(images);
426
- RB_GC_GUARD(clones);
476
+ RB_GC_GUARD(linked);
427
477
  rm_check_exception(exception, new_images, DestroyOnError);
428
478
  DestroyExceptionInfo(exception);
429
479
 
@@ -450,11 +500,11 @@ ImageList_display(VALUE self)
450
500
 
451
501
  // Create a new Info object to use with this call
452
502
  info_obj = rm_info_new();
453
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
503
+ info = rm_info_get(info_obj);
454
504
 
455
505
  // Convert the images array to an images sequence.
456
- VALUE clones;
457
- images = images_from_imagelist(self, &clones);
506
+ VALUE linked;
507
+ images = images_from_imagelist(self, &linked);
458
508
  #if defined(IMAGEMAGICK_7)
459
509
  exception = AcquireExceptionInfo();
460
510
  DisplayImages(info, images, exception);
@@ -467,7 +517,7 @@ ImageList_display(VALUE self)
467
517
  rm_check_image_exception(images, RetainOnError);
468
518
  #endif
469
519
 
470
- RB_GC_GUARD(clones);
520
+ RB_GC_GUARD(linked);
471
521
  RB_GC_GUARD(info_obj);
472
522
 
473
523
  return self;
@@ -485,15 +535,15 @@ ImageList_flatten_images(VALUE self)
485
535
  Image *images, *new_image;
486
536
  ExceptionInfo *exception;
487
537
 
488
- VALUE clones;
489
- images = images_from_imagelist(self, &clones);
538
+ VALUE linked;
539
+ images = images_from_imagelist(self, &linked);
490
540
  exception = AcquireExceptionInfo();
491
541
 
492
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, FlattenLayer, exception };
493
- new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
542
+ DECLARE_GVL_CALL(call, MergeImageLayers, images, FlattenLayer, exception);
543
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
494
544
 
495
545
  rm_split(images);
496
- RB_GC_GUARD(clones);
546
+ RB_GC_GUARD(linked);
497
547
  rm_check_exception(exception, new_image, DestroyOnError);
498
548
  DestroyExceptionInfo(exception);
499
549
 
@@ -518,7 +568,7 @@ VALUE
518
568
  ImageList_montage(VALUE self)
519
569
  {
520
570
  VALUE montage_obj;
521
- Montage *montage;
571
+ MagickMontage *montage;
522
572
  Image *new_images, *images;
523
573
  ExceptionInfo *exception;
524
574
 
@@ -529,10 +579,11 @@ ImageList_montage(VALUE self)
529
579
  rb_yield(montage_obj);
530
580
  }
531
581
 
532
- TypedData_Get_Struct(montage_obj, Montage, &rm_montage_data_type, montage);
582
+ TypedData_Get_Struct(montage_obj, MagickMontage, &rm_montage_data_type, montage);
533
583
 
534
- VALUE clones;
535
- images = images_from_imagelist(self, &clones);
584
+ VALUE linked;
585
+ check_images(self, rm_check_writable);
586
+ images = images_from_imagelist(self, &linked, LinkChange);
536
587
 
537
588
  for (Image *image = images; image; image = GetNextImageInList(image))
538
589
  {
@@ -549,10 +600,10 @@ ImageList_montage(VALUE self)
549
600
  exception = AcquireExceptionInfo();
550
601
 
551
602
  // MontageImage can return more than one image.
552
- GVL_STRUCT_TYPE(MontageImages) args = { images, montage->info, exception };
553
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MontageImages), &args);
603
+ DECLARE_GVL_CALL(call, MontageImages, images, montage->info, exception);
604
+ new_images = call.update_each(linked).read(montage_obj).split(images).release(exception).run<Image *>();
554
605
  rm_split(images);
555
- RB_GC_GUARD(clones);
606
+ RB_GC_GUARD(linked);
556
607
  rm_check_exception(exception, new_images, DestroyOnError);
557
608
  DestroyExceptionInfo(exception);
558
609
 
@@ -585,13 +636,13 @@ ImageList_morph(VALUE self, VALUE nimages)
585
636
  }
586
637
 
587
638
  number_images = NUM2LONG(nimages);
588
- VALUE clones;
589
- images = images_from_imagelist(self, &clones);
639
+ VALUE linked;
640
+ images = images_from_imagelist(self, &linked);
590
641
  exception = AcquireExceptionInfo();
591
- GVL_STRUCT_TYPE(MorphImages) args = { images, number_images, exception };
592
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MorphImages), &args);
642
+ DECLARE_GVL_CALL(call, MorphImages, images, number_images, exception);
643
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
593
644
  rm_split(images);
594
- RB_GC_GUARD(clones);
645
+ RB_GC_GUARD(linked);
595
646
  rm_check_exception(exception, new_images, DestroyOnError);
596
647
  DestroyExceptionInfo(exception);
597
648
 
@@ -610,15 +661,15 @@ ImageList_mosaic(VALUE self)
610
661
  Image *images, *new_image;
611
662
  ExceptionInfo *exception;
612
663
 
613
- VALUE clones;
614
- images = images_from_imagelist(self, &clones);
664
+ VALUE linked;
665
+ images = images_from_imagelist(self, &linked);
615
666
 
616
667
  exception = AcquireExceptionInfo();
617
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, MosaicLayer, exception };
618
- new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
668
+ DECLARE_GVL_CALL(call, MergeImageLayers, images, MosaicLayer, exception);
669
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
619
670
 
620
671
  rm_split(images);
621
- RB_GC_GUARD(clones);
672
+ RB_GC_GUARD(linked);
622
673
  rm_check_exception(exception, new_image, DestroyOnError);
623
674
  DestroyExceptionInfo(exception);
624
675
 
@@ -644,43 +695,43 @@ ImageList_optimize_layers(VALUE self, VALUE method)
644
695
  new_images2 = NULL; // defeat "unused variable" message
645
696
 
646
697
  VALUE_TO_ENUM(method, mthd, LayerMethod);
647
- VALUE clones;
648
- images = images_from_imagelist(self, &clones);
698
+ VALUE linked;
699
+ images = images_from_imagelist(self, &linked);
649
700
 
650
701
  exception = AcquireExceptionInfo();
651
702
  switch (mthd)
652
703
  {
653
704
  case CoalesceLayer:
654
705
  {
655
- GVL_STRUCT_TYPE(CoalesceImages) args = { images, exception };
656
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args);
706
+ DECLARE_GVL_CALL(call, CoalesceImages, images, exception);
707
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
657
708
  }
658
709
  break;
659
710
  case DisposeLayer:
660
711
  {
661
- GVL_STRUCT_TYPE(DisposeImages) args = { images, exception };
662
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(DisposeImages), &args);
712
+ DECLARE_GVL_CALL(call, DisposeImages, images, exception);
713
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
663
714
  }
664
715
  break;
665
716
  case OptimizeTransLayer:
666
717
  {
667
- new_images = clone_imagelist(images);
668
- GVL_STRUCT_TYPE(OptimizeImageTransparency) args = { new_images, exception };
669
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageTransparency), &args);
718
+ new_images = clone_linked_imagelist(images, linked, exception);
719
+ DECLARE_GVL_CALL(call, OptimizeImageTransparency, new_images, exception);
720
+ call.read_each(linked).split(images).destroy(new_images).release(exception).run<void>();
670
721
  }
671
722
  break;
672
723
  case RemoveDupsLayer:
673
724
  {
674
- new_images = clone_imagelist(images);
675
- GVL_STRUCT_TYPE(RemoveDuplicateLayers) args = { &new_images, exception };
676
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemoveDuplicateLayers), &args);
725
+ new_images = clone_linked_imagelist(images, linked, exception);
726
+ DECLARE_GVL_CALL(call, RemoveDuplicateLayers, &new_images, exception);
727
+ call.read_each(linked).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
677
728
  }
678
729
  break;
679
730
  case RemoveZeroLayer:
680
731
  {
681
- new_images = clone_imagelist(images);
682
- GVL_STRUCT_TYPE(RemoveZeroDelayLayers) args = { &new_images, exception };
683
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemoveZeroDelayLayers), &args);
732
+ new_images = clone_linked_imagelist(images, linked, exception);
733
+ DECLARE_GVL_CALL(call, RemoveZeroDelayLayers, &new_images, exception);
734
+ call.read_each(linked).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
684
735
  }
685
736
  break;
686
737
  case CompositeLayer:
@@ -691,42 +742,42 @@ ImageList_optimize_layers(VALUE self, VALUE method)
691
742
  // In 6.3.4-ish, OptimizeImageLayer replaced OptimizeLayer
692
743
  case OptimizeImageLayer:
693
744
  {
694
- GVL_STRUCT_TYPE(OptimizeImageLayers) args = { images, exception };
695
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageLayers), &args);
745
+ DECLARE_GVL_CALL(call, OptimizeImageLayers, images, exception);
746
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
696
747
  }
697
748
  break;
698
749
  // and OptimizeLayer became a "General Purpose, GIF Animation Optimizer" (ref. mogrify.c)
699
750
  case OptimizeLayer:
700
751
  {
701
- GVL_STRUCT_TYPE(CoalesceImages) args_CoalesceImages = { images, exception };
702
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CoalesceImages), &args_CoalesceImages);
752
+ DECLARE_GVL_CALL(coalesce_call, CoalesceImages, images, exception);
753
+ new_images = coalesce_call.read_each(linked).split(images).release(exception).run<Image *>();
703
754
  rm_split(images);
704
755
  rm_check_exception(exception, new_images, DestroyOnError);
705
756
 
706
- GVL_STRUCT_TYPE(OptimizeImageLayers) args_OptimizeImageLayers = { new_images, exception };
707
- new_images2 = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageLayers), &args_OptimizeImageLayers);
757
+ DECLARE_GVL_CALL(optimize_call, OptimizeImageLayers, new_images, exception);
758
+ new_images2 = optimize_call.destroy(new_images).release(exception).run<Image *>();
708
759
  DestroyImageList(new_images);
709
760
  rm_check_exception(exception, new_images2, DestroyOnError);
710
761
 
711
762
  new_images = new_images2;
712
- GVL_STRUCT_TYPE(OptimizeImageTransparency) args_OptimizeImageTransparency = { new_images, exception };
713
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizeImageTransparency), &args_OptimizeImageTransparency);
763
+ DECLARE_GVL_CALL(transparency_call, OptimizeImageTransparency, new_images, exception);
764
+ transparency_call.destroy(new_images).release(exception).run<void>();
714
765
  rm_check_exception(exception, new_images, DestroyOnError);
715
766
  // mogrify supports -dither here. We don't.
716
767
  GetQuantizeInfo(&quantize_info);
717
768
  #if defined(IMAGEMAGICK_7)
718
- GVL_STRUCT_TYPE(RemapImages) args_RemapImages = { &quantize_info, new_images, NULL, exception };
769
+ DECLARE_GVL_CALL(remap_call, RemapImages, &quantize_info, new_images, NULL, exception);
719
770
  #else
720
- GVL_STRUCT_TYPE(RemapImages) args_RemapImages = { &quantize_info, new_images, NULL };
771
+ DECLARE_GVL_CALL(remap_call, RemapImages, &quantize_info, new_images, NULL);
721
772
  #endif
722
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args_RemapImages);
773
+ remap_call.destroy(new_images).release(exception).run<void>();
723
774
 
724
775
  }
725
776
  break;
726
777
  case OptimizePlusLayer:
727
778
  {
728
- GVL_STRUCT_TYPE(OptimizePlusImageLayers) args = { images, exception };
729
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(OptimizePlusImageLayers), &args);
779
+ DECLARE_GVL_CALL(call, OptimizePlusImageLayers, images, exception);
780
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
730
781
  }
731
782
  break;
732
783
  case CompareAnyLayer:
@@ -734,36 +785,37 @@ ImageList_optimize_layers(VALUE self, VALUE method)
734
785
  case CompareOverlayLayer:
735
786
  {
736
787
  #if defined(IMAGEMAGICK_7)
737
- GVL_STRUCT_TYPE(CompareImagesLayers) args = { images, mthd, exception };
738
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImagesLayers), &args);
788
+ DECLARE_GVL_CALL(call, CompareImagesLayers, images, mthd, exception);
789
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
739
790
  #else
740
- GVL_STRUCT_TYPE(CompareImageLayers) args = { images, mthd, exception };
741
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CompareImageLayers), &args);
791
+ DECLARE_GVL_CALL(call, CompareImageLayers, images, mthd, exception);
792
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
742
793
  #endif
743
794
  }
744
795
  break;
745
796
  case MosaicLayer:
746
797
  {
747
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
748
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
798
+ DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
799
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
749
800
  }
750
801
  break;
751
802
  case FlattenLayer:
752
803
  {
753
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
754
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
804
+ DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
805
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
755
806
  }
756
807
  break;
757
808
  case MergeLayer:
758
809
  {
759
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
760
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
810
+ DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
811
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
761
812
  }
762
813
  break;
763
814
  case TrimBoundsLayer:
764
815
  {
765
- GVL_STRUCT_TYPE(MergeImageLayers) args = { images, mthd, exception };
766
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(MergeImageLayers), &args);
816
+ new_images = clone_linked_imagelist(images, linked, exception);
817
+ DECLARE_GVL_CALL(call, MergeImageLayers, new_images, mthd, exception);
818
+ call.read_each(linked).split(images).destroy(new_images).release(exception).run<void>();
767
819
  }
768
820
  break;
769
821
  default:
@@ -774,7 +826,7 @@ ImageList_optimize_layers(VALUE self, VALUE method)
774
826
  }
775
827
 
776
828
  rm_split(images);
777
- RB_GC_GUARD(clones);
829
+ RB_GC_GUARD(linked);
778
830
  rm_check_exception(exception, new_images, DestroyOnError);
779
831
  DestroyExceptionInfo(exception);
780
832
 
@@ -837,50 +889,120 @@ rm_imagelist_from_images(Image *images)
837
889
 
838
890
 
839
891
  /**
840
- * Convert an array of Image *s to an ImageMagick scene sequence (i.e. a
841
- * doubly-linked list of Images).
892
+ * Run a check that raises on each image in the imagelist.
842
893
  *
843
894
  * No Ruby usage (internal function)
844
895
  *
845
896
  * @param imagelist the imagelist
846
- * @return a pointer to the head of the scene sequence list
847
- * @see rm_imagelist_from_images
897
+ * @param check rm_check_writable or rm_check_frozen
848
898
  */
899
+ static void
900
+ check_images(VALUE imagelist, Image *(*check)(VALUE))
901
+ {
902
+ VALUE images = imagelist_images(imagelist);
903
+
904
+ for (long i = 0; i < RARRAY_LEN(images); i++)
905
+ {
906
+ check(rb_ary_entry(images, i));
907
+ }
908
+ }
909
+
910
+
911
+ // Check an image of the imagelist as the method uses it
849
912
  static Image *
850
- images_from_imagelist(VALUE imagelist, VALUE *clones)
913
+ check_link(VALUE obj, LinkMode mode)
851
914
  {
915
+ if (mode != LinkRead && !OBJ_FROZEN(obj))
916
+ {
917
+ return rm_check_writable(obj);
918
+ }
919
+ return rm_check_readable(obj);
920
+ }
921
+
922
+
923
+ // Link the images of the imagelist, keeping the head so far in args->images
924
+ static VALUE
925
+ link_images(VALUE arg)
926
+ {
927
+ struct ImagesFromImageList_Args *args = (struct ImagesFromImageList_Args *)arg;
852
928
  long x, len;
853
- Image *head = NULL;
854
929
  VALUE images, t;
855
930
 
856
- *clones = rb_ary_new();
857
-
858
- len = check_imagelist_length(imagelist);
931
+ len = check_imagelist_length(args->imagelist);
859
932
 
860
- images = rb_iv_get(imagelist, "@images");
933
+ images = rb_ary_dup(rb_iv_get(args->imagelist, "@images"));
861
934
  for (x = 0; x < len; x++)
862
935
  {
863
936
  Image *image;
864
937
 
865
938
  t = rb_ary_entry(images, x);
866
- image = rm_check_destroyed(t);
867
- // avoid a loop in this linked imagelist, issue #202
868
- if (head == image || GetPreviousImageInList(image) != NULL)
939
+ image = check_link(t, args->mode);
940
+ // Avoid a loop in this linked imagelist, issue #202. A frozen image can
941
+ // be shared with another Ractor, which may be linking it into a list of
942
+ // its own. A method that reads the images links copies, since another
943
+ // call may read an image meanwhile and must see it alone.
944
+ if (args->mode == LinkRead || OBJ_FROZEN(t) || args->images == image || GetPreviousImageInList(image) != NULL || GetNextImageInList(image) != NULL)
869
945
  {
870
- image = rm_clone_image(image);
946
+ // ImageMagick reads the pixels to write through the default nexus of
947
+ // the pixel cache, so a frozen image to write gets a cache of its own.
948
+ bool changes = args->mode != LinkRead;
949
+
950
+ image = args->mode == LinkWrite && OBJ_FROZEN(t) ? rm_copy_image(image, images, args->held, changes)
951
+ : rm_clone_image(image, images, args->held, changes);
871
952
 
872
953
  // Wrap raw ImageMagick object by Ruby object to destroy using Ruby's GC.
873
- // The wrapper is kept in *clones so that the GC cannot reclaim it, and
874
- // with it the Image, while the list below is still in use.
875
- rb_ary_push(*clones, rm_image_new(image));
954
+ rb_ary_push(*args->linked, rm_image_wrap(image));
876
955
  }
877
- AppendImageToList(&head, image);
956
+ // *linked keeps the objects whose images are linked, so that the GC
957
+ // cannot reclaim them and the caller can register them. It keeps t
958
+ // too when its image is cloned, as the clone shares its pixel cache.
959
+ rb_ary_push(*args->linked, t);
960
+ AppendImageToList(&args->images, image);
878
961
  }
879
962
 
880
963
  RB_GC_GUARD(images);
881
964
  RB_GC_GUARD(t);
882
965
 
883
- return head;
966
+ return Qnil;
967
+ }
968
+
969
+ /**
970
+ * Convert an array of Image *s to an ImageMagick scene sequence (i.e. a
971
+ * doubly-linked list of Images).
972
+ *
973
+ * No Ruby usage (internal function)
974
+ *
975
+ * @param imagelist the imagelist
976
+ * @param linked set to an Array of the Image objects whose images the list
977
+ * uses, for the caller to register
978
+ * @param mode whether the caller reads, changes or writes the images. The
979
+ * copies of frozen images to write get pixel caches of their own.
980
+ * @param held another Image object that the caller uses, an Array of them, or
981
+ * nil
982
+ * @return a pointer to the head of the scene sequence list
983
+ * @see rm_imagelist_from_images
984
+ */
985
+ static Image *
986
+ images_from_imagelist(VALUE imagelist, VALUE *linked, LinkMode mode, VALUE held)
987
+ {
988
+ struct ImagesFromImageList_Args args = { imagelist, linked, NULL, mode, held };
989
+ int state = 0;
990
+
991
+ *linked = rb_ary_new();
992
+
993
+ // An exception part way, such as an interrupt in rm_clone_image, would
994
+ // leave the images linked to copies that the GC then frees.
995
+ rb_protect(link_images, (VALUE)&args, &state);
996
+ if (state)
997
+ {
998
+ if (args.images)
999
+ {
1000
+ rm_split(args.images);
1001
+ }
1002
+ rb_jump_tag(state);
1003
+ }
1004
+
1005
+ return args.images;
884
1006
  }
885
1007
 
886
1008
 
@@ -898,12 +1020,26 @@ images_from_imagelist_protected(VALUE arg)
898
1020
  {
899
1021
  struct ImagesFromImageList_Args *args = (struct ImagesFromImageList_Args *)arg;
900
1022
 
901
- args->images = images_from_imagelist(args->imagelist, args->clones);
1023
+ args->images = images_from_imagelist(args->imagelist, args->linked, args->mode, args->held);
902
1024
 
903
1025
  return Qnil;
904
1026
  }
905
1027
 
906
1028
 
1029
+ // Return @images of the imagelist, raising unless it is an Array
1030
+ static VALUE
1031
+ imagelist_images(VALUE imagelist)
1032
+ {
1033
+ VALUE images = rb_iv_get(imagelist, "@images");
1034
+ if (!RB_TYPE_P(images, T_ARRAY))
1035
+ {
1036
+ rb_raise(Class_ImageMagickError, "@images is not of type Array");
1037
+ }
1038
+
1039
+ return images;
1040
+ }
1041
+
1042
+
907
1043
  /**
908
1044
  * return the # of images in an imagelist.
909
1045
  *
@@ -915,11 +1051,7 @@ images_from_imagelist_protected(VALUE arg)
915
1051
  static long
916
1052
  imagelist_length(VALUE imagelist)
917
1053
  {
918
- VALUE images = rb_iv_get(imagelist, "@images");
919
- if (!RB_TYPE_P(images, T_ARRAY))
920
- {
921
- rb_raise(Class_ImageMagickError, "@images is not of type Array");
922
- }
1054
+ VALUE images = imagelist_images(imagelist);
923
1055
 
924
1056
  RB_GC_GUARD(images);
925
1057
 
@@ -972,10 +1104,11 @@ imagelist_push(VALUE imagelist, VALUE image)
972
1104
  * No Ruby usage (internal function)
973
1105
  *
974
1106
  * @param images the images
1107
+ * @param image_ary the Image objects whose images are linked
975
1108
  * @return a new array of images
976
1109
  */
977
1110
  static Image *
978
- clone_imagelist(Image *images)
1111
+ clone_imagelist(Image *images, VALUE image_ary)
979
1112
  {
980
1113
  Image *new_imagelist = NULL, *image;
981
1114
  ExceptionInfo *exception;
@@ -987,10 +1120,10 @@ clone_imagelist(Image *images)
987
1120
  {
988
1121
  Image *clone;
989
1122
 
990
- GVL_STRUCT_TYPE(CloneImage) args = { image, 0, 0, MagickTrue, exception };
991
- clone = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CloneImage), &args);
992
- rm_check_exception(exception, new_imagelist, DestroyOnError);
1123
+ DECLARE_GVL_CALL(call, CloneImage, image, 0, 0, MagickTrue, exception);
1124
+ clone = call.read_each(image_ary).keep_thread().destroy(new_imagelist).release(exception).run<Image *>();
993
1125
  AppendImageToList(&new_imagelist, clone);
1126
+ rm_check_exception(exception, new_imagelist, DestroyOnError);
994
1127
  image = GetNextImageInList(image);
995
1128
  }
996
1129
 
@@ -999,6 +1132,63 @@ clone_imagelist(Image *images)
999
1132
  }
1000
1133
 
1001
1134
 
1135
+ static VALUE
1136
+ clone_imagelist_protected(VALUE arg)
1137
+ {
1138
+ struct LinkedImages_Args *args = (struct LinkedImages_Args *)arg;
1139
+
1140
+ args->new_images = clone_imagelist(args->images, args->image_ary);
1141
+ return Qnil;
1142
+ }
1143
+
1144
+
1145
+ static VALUE
1146
+ sync_image_options_protected(VALUE arg)
1147
+ {
1148
+ struct LinkedImages_Args *args = (struct LinkedImages_Args *)arg;
1149
+
1150
+ rm_sync_image_options(args->image, args->info, args->image_ary);
1151
+ return Qnil;
1152
+ }
1153
+
1154
+
1155
+ static void
1156
+ call_while_linked(VALUE (*fn)(VALUE), struct LinkedImages_Args *args, ExceptionInfo *exception)
1157
+ {
1158
+ int state = 0;
1159
+
1160
+ rb_protect(fn, (VALUE)args, &state);
1161
+ if (state)
1162
+ {
1163
+ rm_split(args->images);
1164
+ if (exception)
1165
+ {
1166
+ DestroyExceptionInfo(exception);
1167
+ }
1168
+ rb_jump_tag(state);
1169
+ }
1170
+ }
1171
+
1172
+
1173
+ static Image *
1174
+ clone_linked_imagelist(Image *images, VALUE image_ary, ExceptionInfo *exception)
1175
+ {
1176
+ struct LinkedImages_Args args = { images, NULL, NULL, NULL, image_ary };
1177
+
1178
+ call_while_linked(clone_imagelist_protected, &args, exception);
1179
+ return args.new_images;
1180
+ }
1181
+
1182
+
1183
+ static void
1184
+ sync_linked_image_options(Image *images, Image *image, Info *info, VALUE image_ary, ExceptionInfo *exception)
1185
+ {
1186
+ struct LinkedImages_Args args = { images, image, info, NULL, image_ary };
1187
+
1188
+ call_while_linked(sync_image_options_protected, &args, exception);
1189
+ }
1190
+
1191
+
1002
1192
  /**
1003
1193
  * Analyzes the colors within a set of reference images and chooses a fixed number of colors to represent the set.
1004
1194
  * The goal of the algorithm is to minimize the difference between the input and output images while minimizing the processing time.
@@ -1063,25 +1253,32 @@ ImageList_quantize(int argc, VALUE *argv, VALUE self)
1063
1253
 
1064
1254
 
1065
1255
  // Convert image array to image sequence, clone image sequence.
1066
- VALUE clones;
1067
- images = images_from_imagelist(self, &clones);
1256
+ VALUE linked;
1257
+ images = images_from_imagelist(self, &linked);
1068
1258
  exception = AcquireExceptionInfo();
1069
- GVL_STRUCT_TYPE(CloneImageList) args_CloneImageList = { images, exception };
1070
- new_images = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CloneImageList), &args_CloneImageList);
1259
+ DECLARE_GVL_CALL(clone_call, CloneImageList, images, exception);
1260
+ new_images = clone_call.read_each(linked).split(images).release(exception).run<Image *>();
1071
1261
  rm_split(images);
1072
- RB_GC_GUARD(clones);
1262
+ RB_GC_GUARD(linked);
1073
1263
  rm_check_exception(exception, new_images, DestroyOnError);
1074
1264
 
1075
- rm_ensure_result(new_images);
1265
+ if (!new_images)
1266
+ {
1267
+ DestroyExceptionInfo(exception);
1268
+ rm_ensure_result(new_images);
1269
+ }
1076
1270
 
1077
1271
  #if defined(IMAGEMAGICK_7)
1078
- GVL_STRUCT_TYPE(QuantizeImages) args_QuantizeImages = { &quantize_info, new_images, exception };
1272
+ DECLARE_GVL_CALL(quantize_call, QuantizeImages, &quantize_info, new_images, exception);
1079
1273
  #else
1080
- GVL_STRUCT_TYPE(QuantizeImages) args_QuantizeImages = { &quantize_info, new_images };
1274
+ DECLARE_GVL_CALL(quantize_call, QuantizeImages, &quantize_info, new_images);
1081
1275
  #endif
1082
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(QuantizeImages), &args_QuantizeImages);
1276
+ quantize_call.destroy(new_images).release(exception).run<void>();
1083
1277
  rm_check_exception(exception, new_images, DestroyOnError);
1084
1278
  DestroyExceptionInfo(exception);
1279
+ #if defined(IMAGEMAGICK_6)
1280
+ rm_check_image_exception(new_images, DestroyOnError);
1281
+ #endif
1085
1282
 
1086
1283
  // Create new ImageList object, convert mapped image sequence to images,
1087
1284
  // append to images array.
@@ -1116,6 +1313,7 @@ VALUE
1116
1313
  ImageList_remap(int argc, VALUE *argv, VALUE self)
1117
1314
  {
1118
1315
  Image *images, *remap_image = NULL;
1316
+ VALUE remap_obj = Qnil;
1119
1317
  QuantizeInfo quantize_info;
1120
1318
  #if defined(IMAGEMAGICK_7)
1121
1319
  ExceptionInfo *exception;
@@ -1123,9 +1321,8 @@ ImageList_remap(int argc, VALUE *argv, VALUE self)
1123
1321
 
1124
1322
  if (argc > 0 && argv[0] != Qnil)
1125
1323
  {
1126
- VALUE t = rm_cur_image(argv[0]);
1127
- remap_image = rm_check_destroyed(t);
1128
- RB_GC_GUARD(t);
1324
+ remap_obj = rm_cur_image(argv[0]);
1325
+ remap_image = rm_check_readable(remap_obj);
1129
1326
  }
1130
1327
 
1131
1328
  GetQuantizeInfo(&quantize_info);
@@ -1142,24 +1339,26 @@ ImageList_remap(int argc, VALUE *argv, VALUE self)
1142
1339
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
1143
1340
  }
1144
1341
 
1145
- VALUE clones;
1146
- images = images_from_imagelist(self, &clones);
1342
+ VALUE linked;
1343
+ check_images(self, rm_check_frozen);
1344
+ images = images_from_imagelist(self, &linked, LinkChange, remap_obj);
1147
1345
 
1148
1346
  #if defined(IMAGEMAGICK_7)
1149
1347
  exception = AcquireExceptionInfo();
1150
- GVL_STRUCT_TYPE(RemapImages) args = { &quantize_info, images, remap_image, exception };
1151
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args);
1348
+ DECLARE_GVL_CALL(call, RemapImages, &quantize_info, images, remap_image, exception);
1349
+ call.update_each(linked).read(remap_obj).split(images).release(exception).run<void>();
1152
1350
  rm_split(images);
1153
1351
  CHECK_EXCEPTION();
1154
1352
  DestroyExceptionInfo(exception);
1155
1353
  #else
1156
- GVL_STRUCT_TYPE(RemapImages) args = { &quantize_info, images, remap_image };
1157
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(RemapImages), &args);
1354
+ DECLARE_GVL_CALL(call, RemapImages, &quantize_info, images, remap_image);
1355
+ call.update_each(linked).read(remap_obj).split(images).run<void>();
1158
1356
  rm_split(images);
1159
1357
  rm_check_image_exception(images, RetainOnError);
1160
1358
  #endif
1161
1359
 
1162
- RB_GC_GUARD(clones);
1360
+ RB_GC_GUARD(linked);
1361
+ RB_GC_GUARD(remap_obj);
1163
1362
 
1164
1363
  return self;
1165
1364
  }
@@ -1189,11 +1388,12 @@ ImageList_to_blob(VALUE self)
1189
1388
  ExceptionInfo *exception;
1190
1389
 
1191
1390
  info_obj = rm_info_new();
1192
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
1391
+ info = rm_info_get(info_obj);
1193
1392
 
1194
1393
  // Convert the images array to an images sequence.
1195
- VALUE clones;
1196
- images = images_from_imagelist(self, &clones);
1394
+ VALUE linked;
1395
+ check_images(self, rm_check_writable);
1396
+ images = images_from_imagelist(self, &linked, LinkWrite);
1197
1397
 
1198
1398
  exception = AcquireExceptionInfo();
1199
1399
  SetImageInfo(info, MagickTrue, exception);
@@ -1210,15 +1410,15 @@ ImageList_to_blob(VALUE self)
1210
1410
 
1211
1411
  for (image = images; image; image = GetNextImageInList(image))
1212
1412
  {
1213
- rm_sync_image_options(image, info);
1413
+ sync_linked_image_options(images, image, info, linked, exception);
1214
1414
  }
1215
1415
 
1216
1416
  // Unconditionally request multi-images support. The worst that
1217
1417
  // can happen is that there's only one image or the format
1218
1418
  // doesn't support multi-image files.
1219
1419
  info->adjoin = MagickTrue;
1220
- GVL_STRUCT_TYPE(ImagesToBlob) args = { info, images, &length, exception };
1221
- blob = CALL_FUNC_WITHOUT_GVL(GVL_FUNC(ImagesToBlob), &args);
1420
+ DECLARE_GVL_CALL(call, ImagesToBlob, info, images, &length, exception);
1421
+ blob = call.update_each(linked).read(info_obj).split(images).release(exception).free_result().run<void *>();
1222
1422
  if (blob && exception->severity >= ErrorException)
1223
1423
  {
1224
1424
  magick_free((void*)blob);
@@ -1226,7 +1426,7 @@ ImageList_to_blob(VALUE self)
1226
1426
  length = 0;
1227
1427
  }
1228
1428
  rm_split(images);
1229
- RB_GC_GUARD(clones);
1429
+ RB_GC_GUARD(linked);
1230
1430
  CHECK_EXCEPTION();
1231
1431
  DestroyExceptionInfo(exception);
1232
1432
 
@@ -1267,7 +1467,7 @@ ImageList_write(VALUE self, VALUE file)
1267
1467
  ExceptionInfo *exception;
1268
1468
 
1269
1469
  info_obj = rm_info_new();
1270
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
1470
+ info = rm_info_get(info_obj);
1271
1471
 
1272
1472
 
1273
1473
  if (TYPE(file) == T_FILE)
@@ -1292,8 +1492,9 @@ ImageList_write(VALUE self, VALUE file)
1292
1492
  }
1293
1493
 
1294
1494
  // Convert the images array to an images sequence.
1295
- VALUE clones;
1296
- images = images_from_imagelist(self, &clones);
1495
+ VALUE linked;
1496
+ check_images(self, rm_check_writable);
1497
+ images = images_from_imagelist(self, &linked, LinkWrite);
1297
1498
 
1298
1499
  // Copy the filename into each image. Set a scene number to be used if
1299
1500
  // writing multiple files. (Ref: ImageMagick's utilities/convert.c
@@ -1312,24 +1513,27 @@ ImageList_write(VALUE self, VALUE file)
1312
1513
  rm_check_exception(exception, images, RetainOnError);
1313
1514
  #if defined(IMAGEMAGICK_6)
1314
1515
  DestroyExceptionInfo(exception);
1516
+ exception = NULL;
1315
1517
  #endif
1316
1518
 
1317
1519
  // Tell WriteImage if we want a multi-images file.
1318
- if (imagelist_length(self) > 1L && m && GetMagickAdjoin(m))
1520
+ if (GetImageListLength(images) > 1L && m && GetMagickAdjoin(m))
1319
1521
  {
1320
1522
  info->adjoin = MagickTrue;
1321
1523
  }
1322
1524
 
1323
1525
  for (img = images; img; img = GetNextImageInList(img))
1324
1526
  {
1325
- rm_sync_image_options(img, info);
1527
+ sync_linked_image_options(images, img, info, linked, exception);
1326
1528
  #if defined(IMAGEMAGICK_7)
1327
- GVL_STRUCT_TYPE(WriteImage) args = { info, img, exception };
1328
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(WriteImage), &args);
1529
+ DECLARE_GVL_CALL(call, WriteImage, info, img, exception);
1530
+ call.update_each(linked).read(info_obj).split(images).release(exception);
1531
+ call.keep_thread(info->file != NULL).run<void>();
1329
1532
  rm_check_exception(exception, img, RetainOnError);
1330
1533
  #else
1331
- GVL_STRUCT_TYPE(WriteImage) args = { info, img };
1332
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(WriteImage), &args);
1534
+ DECLARE_GVL_CALL(call, WriteImage, info, img);
1535
+ call.update_each(linked).read(info_obj).split(images);
1536
+ call.keep_thread(info->file != NULL).run<void>();
1333
1537
  // images will be split before raising an exception
1334
1538
  rm_check_image_exception(images, RetainOnError);
1335
1539
  #endif
@@ -1344,7 +1548,7 @@ ImageList_write(VALUE self, VALUE file)
1344
1548
  #endif
1345
1549
 
1346
1550
  rm_split(images);
1347
- RB_GC_GUARD(clones);
1551
+ RB_GC_GUARD(linked);
1348
1552
 
1349
1553
  RB_GC_GUARD(info_obj);
1350
1554