rmagick 7.2.0 → 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,17 +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 *);
23
- static void check_images_writable(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));
24
43
  static VALUE images_from_imagelist_protected(VALUE);
44
+ static VALUE imagelist_images(VALUE);
25
45
  static long imagelist_length(VALUE);
26
46
  static long check_imagelist_length(VALUE);
27
47
  static void imagelist_push(VALUE, VALUE);
@@ -71,14 +91,6 @@ destroy_list_at(void *ptr, intptr_t arg ATTRIBUTE_UNUSED)
71
91
  DestroyImageList(*(Image **)ptr);
72
92
  }
73
93
 
74
- #if defined(IMAGEMAGICK_6)
75
- static void
76
- restore_colorspace(void *ptr, intptr_t colorspace)
77
- {
78
- ((Image *)ptr)->colorspace = (ColorspaceType)colorspace;
79
- }
80
- #endif
81
-
82
94
 
83
95
  /**
84
96
  * Repeatedly display the images in the images array to an XWindow screen. The
@@ -119,12 +131,12 @@ ImageList_animate(int argc, VALUE *argv, VALUE self)
119
131
  info_obj = rm_info_new();
120
132
 
121
133
  // Convert the images array to an images sequence.
122
- VALUE clones;
134
+ VALUE linked;
123
135
  if (argc == 1)
124
136
  {
125
- check_images_writable(self);
137
+ check_images(self, rm_check_writable);
126
138
  }
127
- images = images_from_imagelist(self, &clones);
139
+ images = images_from_imagelist(self, &linked, LinkChange);
128
140
 
129
141
  if (argc == 1)
130
142
  {
@@ -136,22 +148,22 @@ ImageList_animate(int argc, VALUE *argv, VALUE self)
136
148
  }
137
149
  }
138
150
 
139
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
151
+ info = rm_info_get(info_obj);
140
152
  #if defined(IMAGEMAGICK_7)
141
153
  exception = AcquireExceptionInfo();
142
154
  DECLARE_GVL_CALL(call, AnimateImages, info, images, exception);
143
- call.update_each(rb_iv_get(self, "@images")).read(info_obj).split(images).release(exception).run<void>();
155
+ call.update_each(linked).read(info_obj).split(images).release(exception).run<void>();
144
156
  rm_split(images);
145
157
  CHECK_EXCEPTION();
146
158
  DestroyExceptionInfo(exception);
147
159
  #else
148
160
  DECLARE_GVL_CALL(call, AnimateImages, info, images);
149
- call.update_each(rb_iv_get(self, "@images")).read(info_obj).split(images).run<void>();
161
+ call.update_each(linked).read(info_obj).split(images).run<void>();
150
162
  rm_split(images);
151
163
  rm_check_image_exception(images, RetainOnError);
152
164
  #endif
153
165
 
154
- RB_GC_GUARD(clones);
166
+ RB_GC_GUARD(linked);
155
167
  RB_GC_GUARD(info_obj);
156
168
 
157
169
  return self;
@@ -172,8 +184,8 @@ ImageList_append(VALUE self, VALUE stack_arg)
172
184
  ExceptionInfo *exception;
173
185
 
174
186
  // Convert the image array to an image sequence.
175
- VALUE clones;
176
- images = images_from_imagelist(self, &clones);
187
+ VALUE linked;
188
+ images = images_from_imagelist(self, &linked);
177
189
 
178
190
  // If stack == true, stack rectangular images top-to-bottom,
179
191
  // otherwise left-to-right.
@@ -181,9 +193,9 @@ ImageList_append(VALUE self, VALUE stack_arg)
181
193
 
182
194
  exception = AcquireExceptionInfo();
183
195
  DECLARE_GVL_CALL(call, AppendImages, images, stack, exception);
184
- new_image = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
196
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
185
197
  rm_split(images);
186
- RB_GC_GUARD(clones);
198
+ RB_GC_GUARD(linked);
187
199
  rm_check_exception(exception, new_image, DestroyOnError);
188
200
  DestroyExceptionInfo(exception);
189
201
 
@@ -203,14 +215,14 @@ ImageList_average(VALUE self)
203
215
  ExceptionInfo *exception;
204
216
 
205
217
  // Convert the images array to an images sequence.
206
- VALUE clones;
207
- images = images_from_imagelist(self, &clones);
218
+ VALUE linked;
219
+ images = images_from_imagelist(self, &linked);
208
220
 
209
221
  exception = AcquireExceptionInfo();
210
222
  DECLARE_GVL_CALL(call, EvaluateImages, images, MeanEvaluateOperator, exception);
211
- new_image = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
223
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
212
224
  rm_split(images);
213
- RB_GC_GUARD(clones);
225
+ RB_GC_GUARD(linked);
214
226
  rm_check_exception(exception, new_image, DestroyOnError);
215
227
  DestroyExceptionInfo(exception);
216
228
 
@@ -233,14 +245,14 @@ ImageList_coalesce(VALUE self)
233
245
  ExceptionInfo *exception;
234
246
 
235
247
  // Convert the image array to an image sequence.
236
- VALUE clones;
237
- images = images_from_imagelist(self, &clones);
248
+ VALUE linked;
249
+ images = images_from_imagelist(self, &linked);
238
250
 
239
251
  exception = AcquireExceptionInfo();
240
252
  DECLARE_GVL_CALL(call, CoalesceImages, images, exception);
241
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
253
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
242
254
  rm_split(images);
243
- RB_GC_GUARD(clones);
255
+ RB_GC_GUARD(linked);
244
256
  rm_check_exception(exception, new_images, DestroyOnError);
245
257
  DestroyExceptionInfo(exception);
246
258
 
@@ -262,7 +274,7 @@ VALUE ImageList_combine(int argc, VALUE *argv, VALUE self)
262
274
  {
263
275
  #if defined(IMAGEMAGICK_6)
264
276
  ChannelType channel;
265
- ColorspaceType old_colorspace;
277
+ Image *head;
266
278
  #endif
267
279
  ColorspaceType colorspace;
268
280
  long len;
@@ -322,26 +334,38 @@ VALUE ImageList_combine(int argc, VALUE *argv, VALUE self)
322
334
  }
323
335
  #endif
324
336
 
325
- VALUE clones;
326
- #if defined(IMAGEMAGICK_6)
327
- check_images_writable(self);
328
- #endif
329
- images = images_from_imagelist(self, &clones);
337
+ VALUE linked;
338
+ images = images_from_imagelist(self, &linked);
330
339
  exception = AcquireExceptionInfo();
331
340
  #if defined(IMAGEMAGICK_6)
332
- old_colorspace = images->colorspace;
333
- SetImageColorspace(images, colorspace);
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;
334
357
  DECLARE_GVL_CALL(call, CombineImages, images, channel, exception);
335
- call.update(images).restore(restore_colorspace, images, old_colorspace);
358
+ call.read_each(linked).split(images).destroy(head);
336
359
  #else
337
360
  DECLARE_GVL_CALL(call, CombineImages, images, colorspace, exception);
361
+ call.read_each(linked).split(images);
338
362
  #endif
339
- new_image = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
363
+ new_image = call.release(exception).run<Image *>();
340
364
 
341
365
  rm_split(images);
342
- RB_GC_GUARD(clones);
366
+ RB_GC_GUARD(linked);
343
367
  #if defined(IMAGEMAGICK_6)
344
- images->colorspace = old_colorspace;
368
+ DestroyImage(head);
345
369
  #endif
346
370
  rm_check_exception(exception, new_image, DestroyOnError);
347
371
  DestroyExceptionInfo(exception);
@@ -385,15 +409,14 @@ ImageList_composite_layers(int argc, VALUE *argv, VALUE self)
385
409
  }
386
410
 
387
411
  // Convert ImageLists to image sequences.
388
- VALUE clones, source_clones = Qnil;
389
- dest = images_from_imagelist(self, &clones);
390
- 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);
391
415
  rm_split(dest);
392
- RB_GC_GUARD(clones);
393
416
 
394
417
  // new_images is not owned by any Ruby object yet, so resolving the source
395
418
  // list has to be able to unwind without abandoning it.
396
- struct ImagesFromImageList_Args source_args = { source_images, &source_clones, NULL };
419
+ struct ImagesFromImageList_Args source_args = { source_images, &source_linked, NULL, LinkRead, linked };
397
420
  int state = 0;
398
421
  rb_protect(images_from_imagelist_protected, (VALUE)&source_args, &state);
399
422
  if (state)
@@ -414,9 +437,10 @@ ImageList_composite_layers(int argc, VALUE *argv, VALUE self)
414
437
 
415
438
  exception = AcquireExceptionInfo();
416
439
  DECLARE_GVL_CALL(call, CompositeLayers, new_images, composite_op, source, geometry.x, geometry.y, exception);
417
- call.read_each(rb_iv_get(source_images, "@images")).split(source).destroy(new_images).release(exception).run<void>();
440
+ call.read_each(source_linked).read_each(linked).split(source).destroy(new_images).release(exception).run<void>();
418
441
  rm_split(source);
419
- RB_GC_GUARD(source_clones);
442
+ RB_GC_GUARD(source_linked);
443
+ RB_GC_GUARD(linked);
420
444
  rm_check_exception(exception, new_images, DestroyOnError);
421
445
  DestroyExceptionInfo(exception);
422
446
 
@@ -438,18 +462,18 @@ ImageList_deconstruct(VALUE self)
438
462
  Image *new_images, *images;
439
463
  ExceptionInfo *exception;
440
464
 
441
- VALUE clones;
442
- images = images_from_imagelist(self, &clones);
465
+ VALUE linked;
466
+ images = images_from_imagelist(self, &linked);
443
467
  exception = AcquireExceptionInfo();
444
468
  #if defined(IMAGEMAGICK_7)
445
469
  DECLARE_GVL_CALL(call, CompareImagesLayers, images, CompareAnyLayer, exception);
446
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
470
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
447
471
  #else
448
472
  DECLARE_GVL_CALL(call, DeconstructImages, images, exception);
449
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
473
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
450
474
  #endif
451
475
  rm_split(images);
452
- RB_GC_GUARD(clones);
476
+ RB_GC_GUARD(linked);
453
477
  rm_check_exception(exception, new_images, DestroyOnError);
454
478
  DestroyExceptionInfo(exception);
455
479
 
@@ -476,11 +500,11 @@ ImageList_display(VALUE self)
476
500
 
477
501
  // Create a new Info object to use with this call
478
502
  info_obj = rm_info_new();
479
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
503
+ info = rm_info_get(info_obj);
480
504
 
481
505
  // Convert the images array to an images sequence.
482
- VALUE clones;
483
- images = images_from_imagelist(self, &clones);
506
+ VALUE linked;
507
+ images = images_from_imagelist(self, &linked);
484
508
  #if defined(IMAGEMAGICK_7)
485
509
  exception = AcquireExceptionInfo();
486
510
  DisplayImages(info, images, exception);
@@ -493,7 +517,7 @@ ImageList_display(VALUE self)
493
517
  rm_check_image_exception(images, RetainOnError);
494
518
  #endif
495
519
 
496
- RB_GC_GUARD(clones);
520
+ RB_GC_GUARD(linked);
497
521
  RB_GC_GUARD(info_obj);
498
522
 
499
523
  return self;
@@ -511,15 +535,15 @@ ImageList_flatten_images(VALUE self)
511
535
  Image *images, *new_image;
512
536
  ExceptionInfo *exception;
513
537
 
514
- VALUE clones;
515
- images = images_from_imagelist(self, &clones);
538
+ VALUE linked;
539
+ images = images_from_imagelist(self, &linked);
516
540
  exception = AcquireExceptionInfo();
517
541
 
518
542
  DECLARE_GVL_CALL(call, MergeImageLayers, images, FlattenLayer, exception);
519
- new_image = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
543
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
520
544
 
521
545
  rm_split(images);
522
- RB_GC_GUARD(clones);
546
+ RB_GC_GUARD(linked);
523
547
  rm_check_exception(exception, new_image, DestroyOnError);
524
548
  DestroyExceptionInfo(exception);
525
549
 
@@ -544,7 +568,7 @@ VALUE
544
568
  ImageList_montage(VALUE self)
545
569
  {
546
570
  VALUE montage_obj;
547
- Montage *montage;
571
+ MagickMontage *montage;
548
572
  Image *new_images, *images;
549
573
  ExceptionInfo *exception;
550
574
 
@@ -555,11 +579,11 @@ ImageList_montage(VALUE self)
555
579
  rb_yield(montage_obj);
556
580
  }
557
581
 
558
- TypedData_Get_Struct(montage_obj, Montage, &rm_montage_data_type, montage);
582
+ TypedData_Get_Struct(montage_obj, MagickMontage, &rm_montage_data_type, montage);
559
583
 
560
- VALUE clones;
561
- check_images_writable(self);
562
- images = images_from_imagelist(self, &clones);
584
+ VALUE linked;
585
+ check_images(self, rm_check_writable);
586
+ images = images_from_imagelist(self, &linked, LinkChange);
563
587
 
564
588
  for (Image *image = images; image; image = GetNextImageInList(image))
565
589
  {
@@ -577,9 +601,9 @@ ImageList_montage(VALUE self)
577
601
 
578
602
  // MontageImage can return more than one image.
579
603
  DECLARE_GVL_CALL(call, MontageImages, images, montage->info, exception);
580
- new_images = call.update_each(rb_iv_get(self, "@images")).read(montage_obj).split(images).release(exception).run<Image *>();
604
+ new_images = call.update_each(linked).read(montage_obj).split(images).release(exception).run<Image *>();
581
605
  rm_split(images);
582
- RB_GC_GUARD(clones);
606
+ RB_GC_GUARD(linked);
583
607
  rm_check_exception(exception, new_images, DestroyOnError);
584
608
  DestroyExceptionInfo(exception);
585
609
 
@@ -612,13 +636,13 @@ ImageList_morph(VALUE self, VALUE nimages)
612
636
  }
613
637
 
614
638
  number_images = NUM2LONG(nimages);
615
- VALUE clones;
616
- images = images_from_imagelist(self, &clones);
639
+ VALUE linked;
640
+ images = images_from_imagelist(self, &linked);
617
641
  exception = AcquireExceptionInfo();
618
642
  DECLARE_GVL_CALL(call, MorphImages, images, number_images, exception);
619
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
643
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
620
644
  rm_split(images);
621
- RB_GC_GUARD(clones);
645
+ RB_GC_GUARD(linked);
622
646
  rm_check_exception(exception, new_images, DestroyOnError);
623
647
  DestroyExceptionInfo(exception);
624
648
 
@@ -637,15 +661,15 @@ ImageList_mosaic(VALUE self)
637
661
  Image *images, *new_image;
638
662
  ExceptionInfo *exception;
639
663
 
640
- VALUE clones;
641
- images = images_from_imagelist(self, &clones);
664
+ VALUE linked;
665
+ images = images_from_imagelist(self, &linked);
642
666
 
643
667
  exception = AcquireExceptionInfo();
644
668
  DECLARE_GVL_CALL(call, MergeImageLayers, images, MosaicLayer, exception);
645
- new_image = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
669
+ new_image = call.read_each(linked).split(images).release(exception).run<Image *>();
646
670
 
647
671
  rm_split(images);
648
- RB_GC_GUARD(clones);
672
+ RB_GC_GUARD(linked);
649
673
  rm_check_exception(exception, new_image, DestroyOnError);
650
674
  DestroyExceptionInfo(exception);
651
675
 
@@ -671,8 +695,8 @@ ImageList_optimize_layers(VALUE self, VALUE method)
671
695
  new_images2 = NULL; // defeat "unused variable" message
672
696
 
673
697
  VALUE_TO_ENUM(method, mthd, LayerMethod);
674
- VALUE clones;
675
- images = images_from_imagelist(self, &clones);
698
+ VALUE linked;
699
+ images = images_from_imagelist(self, &linked);
676
700
 
677
701
  exception = AcquireExceptionInfo();
678
702
  switch (mthd)
@@ -680,34 +704,34 @@ ImageList_optimize_layers(VALUE self, VALUE method)
680
704
  case CoalesceLayer:
681
705
  {
682
706
  DECLARE_GVL_CALL(call, CoalesceImages, images, exception);
683
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
707
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
684
708
  }
685
709
  break;
686
710
  case DisposeLayer:
687
711
  {
688
712
  DECLARE_GVL_CALL(call, DisposeImages, images, exception);
689
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
713
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
690
714
  }
691
715
  break;
692
716
  case OptimizeTransLayer:
693
717
  {
694
- new_images = clone_imagelist(images);
718
+ new_images = clone_linked_imagelist(images, linked, exception);
695
719
  DECLARE_GVL_CALL(call, OptimizeImageTransparency, new_images, exception);
696
- call.read_each(rb_iv_get(self, "@images")).split(images).destroy(new_images).release(exception).run<void>();
720
+ call.read_each(linked).split(images).destroy(new_images).release(exception).run<void>();
697
721
  }
698
722
  break;
699
723
  case RemoveDupsLayer:
700
724
  {
701
- new_images = clone_imagelist(images);
725
+ new_images = clone_linked_imagelist(images, linked, exception);
702
726
  DECLARE_GVL_CALL(call, RemoveDuplicateLayers, &new_images, exception);
703
- call.read_each(rb_iv_get(self, "@images")).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
727
+ call.read_each(linked).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
704
728
  }
705
729
  break;
706
730
  case RemoveZeroLayer:
707
731
  {
708
- new_images = clone_imagelist(images);
732
+ new_images = clone_linked_imagelist(images, linked, exception);
709
733
  DECLARE_GVL_CALL(call, RemoveZeroDelayLayers, &new_images, exception);
710
- call.read_each(rb_iv_get(self, "@images")).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
734
+ call.read_each(linked).split(images).cleanup(destroy_list_at, &new_images).release(exception).run<void>();
711
735
  }
712
736
  break;
713
737
  case CompositeLayer:
@@ -719,14 +743,14 @@ ImageList_optimize_layers(VALUE self, VALUE method)
719
743
  case OptimizeImageLayer:
720
744
  {
721
745
  DECLARE_GVL_CALL(call, OptimizeImageLayers, images, exception);
722
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
746
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
723
747
  }
724
748
  break;
725
749
  // and OptimizeLayer became a "General Purpose, GIF Animation Optimizer" (ref. mogrify.c)
726
750
  case OptimizeLayer:
727
751
  {
728
752
  DECLARE_GVL_CALL(coalesce_call, CoalesceImages, images, exception);
729
- new_images = coalesce_call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
753
+ new_images = coalesce_call.read_each(linked).split(images).release(exception).run<Image *>();
730
754
  rm_split(images);
731
755
  rm_check_exception(exception, new_images, DestroyOnError);
732
756
 
@@ -753,7 +777,7 @@ ImageList_optimize_layers(VALUE self, VALUE method)
753
777
  case OptimizePlusLayer:
754
778
  {
755
779
  DECLARE_GVL_CALL(call, OptimizePlusImageLayers, images, exception);
756
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
780
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
757
781
  }
758
782
  break;
759
783
  case CompareAnyLayer:
@@ -762,35 +786,36 @@ ImageList_optimize_layers(VALUE self, VALUE method)
762
786
  {
763
787
  #if defined(IMAGEMAGICK_7)
764
788
  DECLARE_GVL_CALL(call, CompareImagesLayers, images, mthd, exception);
765
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
789
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
766
790
  #else
767
791
  DECLARE_GVL_CALL(call, CompareImageLayers, images, mthd, exception);
768
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
792
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
769
793
  #endif
770
794
  }
771
795
  break;
772
796
  case MosaicLayer:
773
797
  {
774
798
  DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
775
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
799
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
776
800
  }
777
801
  break;
778
802
  case FlattenLayer:
779
803
  {
780
804
  DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
781
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
805
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
782
806
  }
783
807
  break;
784
808
  case MergeLayer:
785
809
  {
786
810
  DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
787
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
811
+ new_images = call.read_each(linked).split(images).release(exception).run<Image *>();
788
812
  }
789
813
  break;
790
814
  case TrimBoundsLayer:
791
815
  {
792
- DECLARE_GVL_CALL(call, MergeImageLayers, images, mthd, exception);
793
- new_images = call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
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>();
794
819
  }
795
820
  break;
796
821
  default:
@@ -801,7 +826,7 @@ ImageList_optimize_layers(VALUE self, VALUE method)
801
826
  }
802
827
 
803
828
  rm_split(images);
804
- RB_GC_GUARD(clones);
829
+ RB_GC_GUARD(linked);
805
830
  rm_check_exception(exception, new_images, DestroyOnError);
806
831
  DestroyExceptionInfo(exception);
807
832
 
@@ -864,69 +889,120 @@ rm_imagelist_from_images(Image *images)
864
889
 
865
890
 
866
891
  /**
867
- * Raise if an offloaded call is using any image in the imagelist.
892
+ * Run a check that raises on each image in the imagelist.
868
893
  *
869
894
  * No Ruby usage (internal function)
870
895
  *
871
896
  * @param imagelist the imagelist
897
+ * @param check rm_check_writable or rm_check_frozen
872
898
  */
873
899
  static void
874
- check_images_writable(VALUE imagelist)
900
+ check_images(VALUE imagelist, Image *(*check)(VALUE))
875
901
  {
876
- VALUE images = rb_iv_get(imagelist, "@images");
902
+ VALUE images = imagelist_images(imagelist);
877
903
 
878
904
  for (long i = 0; i < RARRAY_LEN(images); i++)
879
905
  {
880
- rm_check_writable(rb_ary_entry(images, i));
906
+ check(rb_ary_entry(images, i));
881
907
  }
882
908
  }
883
909
 
884
910
 
885
- /**
886
- * Convert an array of Image *s to an ImageMagick scene sequence (i.e. a
887
- * doubly-linked list of Images).
888
- *
889
- * No Ruby usage (internal function)
890
- *
891
- * @param imagelist the imagelist
892
- * @return a pointer to the head of the scene sequence list
893
- * @see rm_imagelist_from_images
894
- */
911
+ // Check an image of the imagelist as the method uses it
895
912
  static Image *
896
- images_from_imagelist(VALUE imagelist, VALUE *clones)
913
+ check_link(VALUE obj, LinkMode mode)
897
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;
898
928
  long x, len;
899
- Image *head = NULL;
900
929
  VALUE images, t;
901
930
 
902
- *clones = rb_ary_new();
903
-
904
- len = check_imagelist_length(imagelist);
931
+ len = check_imagelist_length(args->imagelist);
905
932
 
906
- images = rb_iv_get(imagelist, "@images");
933
+ images = rb_ary_dup(rb_iv_get(args->imagelist, "@images"));
907
934
  for (x = 0; x < len; x++)
908
935
  {
909
936
  Image *image;
910
937
 
911
938
  t = rb_ary_entry(images, x);
912
- image = rm_check_readable(t);
913
- // avoid a loop in this linked imagelist, issue #202
914
- if (head == image || GetPreviousImageInList(image) != NULL || GetNextImageInList(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)
915
945
  {
916
- 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);
917
952
 
918
953
  // Wrap raw ImageMagick object by Ruby object to destroy using Ruby's GC.
919
- // The wrapper is kept in *clones so that the GC cannot reclaim it, and
920
- // with it the Image, while the list below is still in use.
921
- rb_ary_push(*clones, rm_image_new(image));
954
+ rb_ary_push(*args->linked, rm_image_wrap(image));
922
955
  }
923
- 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);
924
961
  }
925
962
 
926
963
  RB_GC_GUARD(images);
927
964
  RB_GC_GUARD(t);
928
965
 
929
- 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;
930
1006
  }
931
1007
 
932
1008
 
@@ -944,12 +1020,26 @@ images_from_imagelist_protected(VALUE arg)
944
1020
  {
945
1021
  struct ImagesFromImageList_Args *args = (struct ImagesFromImageList_Args *)arg;
946
1022
 
947
- args->images = images_from_imagelist(args->imagelist, args->clones);
1023
+ args->images = images_from_imagelist(args->imagelist, args->linked, args->mode, args->held);
948
1024
 
949
1025
  return Qnil;
950
1026
  }
951
1027
 
952
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
+
953
1043
  /**
954
1044
  * return the # of images in an imagelist.
955
1045
  *
@@ -961,11 +1051,7 @@ images_from_imagelist_protected(VALUE arg)
961
1051
  static long
962
1052
  imagelist_length(VALUE imagelist)
963
1053
  {
964
- VALUE images = rb_iv_get(imagelist, "@images");
965
- if (!RB_TYPE_P(images, T_ARRAY))
966
- {
967
- rb_raise(Class_ImageMagickError, "@images is not of type Array");
968
- }
1054
+ VALUE images = imagelist_images(imagelist);
969
1055
 
970
1056
  RB_GC_GUARD(images);
971
1057
 
@@ -1018,10 +1104,11 @@ imagelist_push(VALUE imagelist, VALUE image)
1018
1104
  * No Ruby usage (internal function)
1019
1105
  *
1020
1106
  * @param images the images
1107
+ * @param image_ary the Image objects whose images are linked
1021
1108
  * @return a new array of images
1022
1109
  */
1023
1110
  static Image *
1024
- clone_imagelist(Image *images)
1111
+ clone_imagelist(Image *images, VALUE image_ary)
1025
1112
  {
1026
1113
  Image *new_imagelist = NULL, *image;
1027
1114
  ExceptionInfo *exception;
@@ -1034,9 +1121,9 @@ clone_imagelist(Image *images)
1034
1121
  Image *clone;
1035
1122
 
1036
1123
  DECLARE_GVL_CALL(call, CloneImage, image, 0, 0, MagickTrue, exception);
1037
- clone = call.keep_thread().run<Image *>();
1038
- rm_check_exception(exception, new_imagelist, DestroyOnError);
1124
+ clone = call.read_each(image_ary).keep_thread().destroy(new_imagelist).release(exception).run<Image *>();
1039
1125
  AppendImageToList(&new_imagelist, clone);
1126
+ rm_check_exception(exception, new_imagelist, DestroyOnError);
1040
1127
  image = GetNextImageInList(image);
1041
1128
  }
1042
1129
 
@@ -1045,6 +1132,63 @@ clone_imagelist(Image *images)
1045
1132
  }
1046
1133
 
1047
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
+
1048
1192
  /**
1049
1193
  * Analyzes the colors within a set of reference images and chooses a fixed number of colors to represent the set.
1050
1194
  * The goal of the algorithm is to minimize the difference between the input and output images while minimizing the processing time.
@@ -1109,16 +1253,20 @@ ImageList_quantize(int argc, VALUE *argv, VALUE self)
1109
1253
 
1110
1254
 
1111
1255
  // Convert image array to image sequence, clone image sequence.
1112
- VALUE clones;
1113
- images = images_from_imagelist(self, &clones);
1256
+ VALUE linked;
1257
+ images = images_from_imagelist(self, &linked);
1114
1258
  exception = AcquireExceptionInfo();
1115
1259
  DECLARE_GVL_CALL(clone_call, CloneImageList, images, exception);
1116
- new_images = clone_call.read_each(rb_iv_get(self, "@images")).split(images).release(exception).run<Image *>();
1260
+ new_images = clone_call.read_each(linked).split(images).release(exception).run<Image *>();
1117
1261
  rm_split(images);
1118
- RB_GC_GUARD(clones);
1262
+ RB_GC_GUARD(linked);
1119
1263
  rm_check_exception(exception, new_images, DestroyOnError);
1120
1264
 
1121
- rm_ensure_result(new_images);
1265
+ if (!new_images)
1266
+ {
1267
+ DestroyExceptionInfo(exception);
1268
+ rm_ensure_result(new_images);
1269
+ }
1122
1270
 
1123
1271
  #if defined(IMAGEMAGICK_7)
1124
1272
  DECLARE_GVL_CALL(quantize_call, QuantizeImages, &quantize_info, new_images, exception);
@@ -1128,6 +1276,9 @@ ImageList_quantize(int argc, VALUE *argv, VALUE self)
1128
1276
  quantize_call.destroy(new_images).release(exception).run<void>();
1129
1277
  rm_check_exception(exception, new_images, DestroyOnError);
1130
1278
  DestroyExceptionInfo(exception);
1279
+ #if defined(IMAGEMAGICK_6)
1280
+ rm_check_image_exception(new_images, DestroyOnError);
1281
+ #endif
1131
1282
 
1132
1283
  // Create new ImageList object, convert mapped image sequence to images,
1133
1284
  // append to images array.
@@ -1188,25 +1339,25 @@ ImageList_remap(int argc, VALUE *argv, VALUE self)
1188
1339
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
1189
1340
  }
1190
1341
 
1191
- VALUE clones;
1192
- check_images_writable(self);
1193
- 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);
1194
1345
 
1195
1346
  #if defined(IMAGEMAGICK_7)
1196
1347
  exception = AcquireExceptionInfo();
1197
1348
  DECLARE_GVL_CALL(call, RemapImages, &quantize_info, images, remap_image, exception);
1198
- call.update_each(rb_iv_get(self, "@images")).read(remap_obj).split(images).release(exception).run<void>();
1349
+ call.update_each(linked).read(remap_obj).split(images).release(exception).run<void>();
1199
1350
  rm_split(images);
1200
1351
  CHECK_EXCEPTION();
1201
1352
  DestroyExceptionInfo(exception);
1202
1353
  #else
1203
1354
  DECLARE_GVL_CALL(call, RemapImages, &quantize_info, images, remap_image);
1204
- call.update_each(rb_iv_get(self, "@images")).read(remap_obj).split(images).run<void>();
1355
+ call.update_each(linked).read(remap_obj).split(images).run<void>();
1205
1356
  rm_split(images);
1206
1357
  rm_check_image_exception(images, RetainOnError);
1207
1358
  #endif
1208
1359
 
1209
- RB_GC_GUARD(clones);
1360
+ RB_GC_GUARD(linked);
1210
1361
  RB_GC_GUARD(remap_obj);
1211
1362
 
1212
1363
  return self;
@@ -1237,12 +1388,12 @@ ImageList_to_blob(VALUE self)
1237
1388
  ExceptionInfo *exception;
1238
1389
 
1239
1390
  info_obj = rm_info_new();
1240
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
1391
+ info = rm_info_get(info_obj);
1241
1392
 
1242
1393
  // Convert the images array to an images sequence.
1243
- VALUE clones;
1244
- check_images_writable(self);
1245
- images = images_from_imagelist(self, &clones);
1394
+ VALUE linked;
1395
+ check_images(self, rm_check_writable);
1396
+ images = images_from_imagelist(self, &linked, LinkWrite);
1246
1397
 
1247
1398
  exception = AcquireExceptionInfo();
1248
1399
  SetImageInfo(info, MagickTrue, exception);
@@ -1259,7 +1410,7 @@ ImageList_to_blob(VALUE self)
1259
1410
 
1260
1411
  for (image = images; image; image = GetNextImageInList(image))
1261
1412
  {
1262
- rm_sync_image_options(image, info);
1413
+ sync_linked_image_options(images, image, info, linked, exception);
1263
1414
  }
1264
1415
 
1265
1416
  // Unconditionally request multi-images support. The worst that
@@ -1267,7 +1418,7 @@ ImageList_to_blob(VALUE self)
1267
1418
  // doesn't support multi-image files.
1268
1419
  info->adjoin = MagickTrue;
1269
1420
  DECLARE_GVL_CALL(call, ImagesToBlob, info, images, &length, exception);
1270
- blob = call.update_each(rb_iv_get(self, "@images")).read(info_obj).split(images).release(exception).free_result().run<void *>();
1421
+ blob = call.update_each(linked).read(info_obj).split(images).release(exception).free_result().run<void *>();
1271
1422
  if (blob && exception->severity >= ErrorException)
1272
1423
  {
1273
1424
  magick_free((void*)blob);
@@ -1275,7 +1426,7 @@ ImageList_to_blob(VALUE self)
1275
1426
  length = 0;
1276
1427
  }
1277
1428
  rm_split(images);
1278
- RB_GC_GUARD(clones);
1429
+ RB_GC_GUARD(linked);
1279
1430
  CHECK_EXCEPTION();
1280
1431
  DestroyExceptionInfo(exception);
1281
1432
 
@@ -1316,7 +1467,7 @@ ImageList_write(VALUE self, VALUE file)
1316
1467
  ExceptionInfo *exception;
1317
1468
 
1318
1469
  info_obj = rm_info_new();
1319
- TypedData_Get_Struct(info_obj, Info, &rm_info_data_type, info);
1470
+ info = rm_info_get(info_obj);
1320
1471
 
1321
1472
 
1322
1473
  if (TYPE(file) == T_FILE)
@@ -1341,10 +1492,9 @@ ImageList_write(VALUE self, VALUE file)
1341
1492
  }
1342
1493
 
1343
1494
  // Convert the images array to an images sequence.
1344
- VALUE clones, written;
1345
- check_images_writable(self);
1346
- written = rb_ary_dup(rb_iv_get(self, "@images"));
1347
- images = images_from_imagelist(self, &clones);
1495
+ VALUE linked;
1496
+ check_images(self, rm_check_writable);
1497
+ images = images_from_imagelist(self, &linked, LinkWrite);
1348
1498
 
1349
1499
  // Copy the filename into each image. Set a scene number to be used if
1350
1500
  // writing multiple files. (Ref: ImageMagick's utilities/convert.c
@@ -1363,25 +1513,26 @@ ImageList_write(VALUE self, VALUE file)
1363
1513
  rm_check_exception(exception, images, RetainOnError);
1364
1514
  #if defined(IMAGEMAGICK_6)
1365
1515
  DestroyExceptionInfo(exception);
1516
+ exception = NULL;
1366
1517
  #endif
1367
1518
 
1368
1519
  // Tell WriteImage if we want a multi-images file.
1369
- if (imagelist_length(self) > 1L && m && GetMagickAdjoin(m))
1520
+ if (GetImageListLength(images) > 1L && m && GetMagickAdjoin(m))
1370
1521
  {
1371
1522
  info->adjoin = MagickTrue;
1372
1523
  }
1373
1524
 
1374
1525
  for (img = images; img; img = GetNextImageInList(img))
1375
1526
  {
1376
- rm_sync_image_options(img, info);
1527
+ sync_linked_image_options(images, img, info, linked, exception);
1377
1528
  #if defined(IMAGEMAGICK_7)
1378
1529
  DECLARE_GVL_CALL(call, WriteImage, info, img, exception);
1379
- call.update_each(written).read(info_obj).split(images).release(exception);
1530
+ call.update_each(linked).read(info_obj).split(images).release(exception);
1380
1531
  call.keep_thread(info->file != NULL).run<void>();
1381
1532
  rm_check_exception(exception, img, RetainOnError);
1382
1533
  #else
1383
1534
  DECLARE_GVL_CALL(call, WriteImage, info, img);
1384
- call.update_each(written).read(info_obj).split(images);
1535
+ call.update_each(linked).read(info_obj).split(images);
1385
1536
  call.keep_thread(info->file != NULL).run<void>();
1386
1537
  // images will be split before raising an exception
1387
1538
  rm_check_image_exception(images, RetainOnError);
@@ -1397,8 +1548,7 @@ ImageList_write(VALUE self, VALUE file)
1397
1548
  #endif
1398
1549
 
1399
1550
  rm_split(images);
1400
- RB_GC_GUARD(clones);
1401
- RB_GC_GUARD(written);
1551
+ RB_GC_GUARD(linked);
1402
1552
 
1403
1553
  RB_GC_GUARD(info_obj);
1404
1554