rmagick 7.2.0 → 7.3.1

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,6 +11,7 @@
11
11
 
12
12
  #include "rmagick.h"
13
13
  #include <errno.h>
14
+ #include <cmath>
14
15
  #if defined(_WIN32)
15
16
  #include <Windows.h>
16
17
  #else
@@ -37,9 +38,7 @@ DEFINE_GVL_STUB5(CloneImage, const Image *, const size_t, const size_t, const Ma
37
38
  * Notes:
38
39
  * - Use when managing memory that ImageMagick may have allocated or may free.
39
40
  * - If malloc fails, it raises an exception.
40
- * - magick_safe_malloc and magick_safe_realloc prevent exceptions caused by
41
- * integer overflow. Added in 6.3.5-9 but backwards compatible with prior
42
- * releases.
41
+ * - magick_safe_malloc prevents exceptions caused by integer overflow.
43
42
  *
44
43
  * @param count the number of quantum elements to allocate
45
44
  * @param quantum the number of bytes in each quantum
@@ -95,36 +94,6 @@ magick_free(void *ptr)
95
94
  }
96
95
 
97
96
 
98
- /**
99
- * ImageMagick safe version of realloc.
100
- *
101
- * No Ruby usage (internal function)
102
- *
103
- * Notes:
104
- * - Use when managing memory that ImageMagick may have allocated or may free.
105
- * - If malloc fails, it raises an exception.
106
- * - magick_safe_malloc and magick_safe_realloc prevent exceptions caused by
107
- * integer overflow. Added in 6.3.5-9 but backwards compatible with prior
108
- * releases.
109
- *
110
- * @param memory the existing block of memory
111
- * @param count the number of quantum elements to allocate
112
- * @param quantum the number of bytes in each quantum
113
- * @return a pointer to a block of memory that is at least count*quantum in size
114
- */
115
- void *
116
- magick_safe_realloc(void *memory, const size_t count, const size_t quantum)
117
- {
118
- void *v;
119
- v = ResizeQuantumMemory(memory, count, quantum);
120
- if (!v)
121
- {
122
- rb_raise(rb_eNoMemError, "not enough memory to continue");
123
- }
124
- return v;
125
- }
126
-
127
-
128
97
  /**
129
98
  * Make a copy of a string in malloc'd memory.
130
99
  *
@@ -307,52 +276,78 @@ rm_check_ary_type(VALUE ary)
307
276
 
308
277
 
309
278
  /**
310
- * Raise an error if the image has been destroyed or an offloaded call is changing it.
279
+ * Check the receiver and the argument of initialize_copy as
280
+ * Object#initialize_copy does.
281
+ *
282
+ * No Ruby usage (internal function)
283
+ *
284
+ * @param self the copy
285
+ * @param orig the original object
286
+ * @return false if self is orig, true otherwise
287
+ */
288
+ bool
289
+ rm_check_init_copy(VALUE self, VALUE orig)
290
+ {
291
+ if (self == orig)
292
+ {
293
+ return false;
294
+ }
295
+ rb_check_frozen(self);
296
+ if (rb_obj_class(self) != rb_obj_class(orig))
297
+ {
298
+ rb_raise(rb_eTypeError, "initialize_copy should take same class object");
299
+ }
300
+ return true;
301
+ }
302
+
303
+
304
+ /**
305
+ * Raise an error if the image has been destroyed or a call in flight is changing it.
311
306
  *
312
307
  * No Ruby usage (internal function)
313
308
  *
314
309
  * @param obj the image
315
310
  * @return the C image structure for the image
316
311
  * @throw DestroyedImageError
317
- * @throw RuntimeError if an offloaded call is changing the image
312
+ * @throw RuntimeError if a call in flight is changing the image
318
313
  */
319
314
  Image *
320
315
  rm_check_readable(VALUE obj)
321
316
  {
322
317
  Image *image;
323
318
 
324
- TypedData_Get_Struct(obj, Image, &rm_image_data_type, image);
319
+ image = rm_image_get(obj);
325
320
  if (!image)
326
321
  {
327
322
  rb_raise(Class_DestroyedImageError, "destroyed image");
328
323
  }
329
- rm_gvl_check_readable(image);
324
+ rm_gvl_check_readable(obj);
330
325
 
331
326
  return image;
332
327
  }
333
328
 
334
329
 
335
330
  /**
336
- * Raise an error if the image has been destroyed or an offloaded call is using it.
331
+ * Raise an error if the image has been destroyed or a call in flight is using it.
337
332
  *
338
333
  * No Ruby usage (internal function)
339
334
  *
340
335
  * @param obj the image
341
336
  * @return the C image structure for the image
342
337
  * @throw DestroyedImageError
343
- * @throw RuntimeError if an offloaded call is using the image
338
+ * @throw RuntimeError if a call in flight is using the image
344
339
  */
345
340
  Image *
346
341
  rm_check_writable(VALUE obj)
347
342
  {
348
343
  Image *image = rm_check_readable(obj);
349
- rm_gvl_check_writable(image);
344
+ rm_gvl_check_writable(obj);
350
345
  return image;
351
346
  }
352
347
 
353
348
 
354
349
  /**
355
- * Raise an error if the image has been destroyed, is frozen or an offloaded call is using it.
350
+ * Raise an error if the image has been destroyed, is frozen or a call in flight is using it.
356
351
  *
357
352
  * No Ruby usage (internal function)
358
353
  *
@@ -364,13 +359,13 @@ rm_check_frozen(VALUE obj)
364
359
  {
365
360
  Image *image;
366
361
 
367
- TypedData_Get_Struct(obj, Image, &rm_image_data_type, image);
362
+ image = rm_image_get(obj);
368
363
  if (!image)
369
364
  {
370
365
  rb_raise(Class_DestroyedImageError, "destroyed image");
371
366
  }
372
367
  rb_check_frozen(obj);
373
- rm_gvl_check_writable(image);
368
+ rm_gvl_check_writable(obj);
374
369
  return image;
375
370
  }
376
371
 
@@ -389,6 +384,23 @@ rm_no_freeze(VALUE obj)
389
384
  }
390
385
 
391
386
 
387
+ /**
388
+ * Overrides freeze in classes whose objects a call can change.
389
+ *
390
+ * No Ruby usage (internal function)
391
+ *
392
+ * @param obj the object
393
+ * @return obj
394
+ * @raise [RuntimeError] if a call in another fiber or thread is changing obj
395
+ */
396
+ VALUE
397
+ rm_freeze(VALUE obj)
398
+ {
399
+ rm_gvl_check_readable(obj);
400
+ return rb_call_super(0, NULL);
401
+ }
402
+
403
+
392
404
  /**
393
405
  * Supply our own version of the "obsolete" rb_str2cstr.
394
406
  *
@@ -565,6 +577,10 @@ rescue_not_dbl(VALUE ignored ATTRIBUTE_UNUSED, VALUE raised_exc ATTRIBUTE_UNUSED
565
577
  int
566
578
  rm_check_num2dbl(VALUE obj)
567
579
  {
580
+ if (FIXNUM_P(obj) || RB_FLOAT_TYPE_P(obj))
581
+ {
582
+ return 1;
583
+ }
568
584
  return FIX2INT(rb_rescue(RESCUE_FUNC(check_num2dbl), obj, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_dbl), (VALUE)0));
569
585
  }
570
586
 
@@ -627,7 +643,8 @@ rm_fuzz_to_dbl(VALUE fuzz_arg)
627
643
  /**
628
644
  * Convert an application-supplied number to a Quantum. If the object is a Float,
629
645
  * truncate it before converting. A value outside 0 to QuantumRange is clamped
630
- * as ClampToQuantum does, so it is kept as is with HDRI.
646
+ * as ClampToQuantum does, so it is kept as is with HDRI. With HDRI, an Integer
647
+ * of any size is accepted, as rm_quantum2num returns one.
631
648
  *
632
649
  * No Ruby usage (internal function)
633
650
  *
@@ -646,13 +663,25 @@ rm_app2quantum(VALUE obj)
646
663
  VALUE v = obj;
647
664
  MagickRealType value;
648
665
 
666
+ if (FIXNUM_P(obj))
667
+ {
668
+ long n = FIX2LONG(obj);
669
+ if (n >= 0 && (unsigned long) n <= QuantumRange)
670
+ {
671
+ return (Quantum) n;
672
+ }
673
+ }
674
+
649
675
  if (TYPE(obj) == T_FLOAT)
650
676
  {
651
677
  v = rb_Integer(obj);
652
678
  }
653
679
  v = rb_to_int(v);
654
680
 
655
- if (RTEST(rb_funcall(v, rb_intern("negative?"), 0)))
681
+ #if defined(MAGICKCORE_HDRI_SUPPORT)
682
+ value = (MagickRealType) NUM2DBL(v);
683
+ #else
684
+ if (FIXNUM_P(v) ? FIX2LONG(v) < 0 : RBIGNUM_NEGATIVE_P(v))
656
685
  {
657
686
  #if MAGICKCORE_QUANTUM_DEPTH == 64
658
687
  value = (MagickRealType) NUM2LL(v);
@@ -668,11 +697,42 @@ rm_app2quantum(VALUE obj)
668
697
  value = (MagickRealType) NUM2UINT(v);
669
698
  #endif
670
699
  }
700
+ #endif
671
701
 
672
702
  return ClampToQuantum(value);
673
703
  }
674
704
 
675
705
 
706
+ #if defined(MAGICKCORE_HDRI_SUPPORT)
707
+ /**
708
+ * Convert a Quantum of HDRI to a Ruby Integer, truncating it toward zero.
709
+ *
710
+ * No Ruby usage (internal function)
711
+ *
712
+ * Notes:
713
+ * - NaN and infinity are 0, since an Integer cannot hold them.
714
+ * - The value is a double, so that a sum computed from Quantums keeps its
715
+ * precision.
716
+ *
717
+ * @param value the Quantum
718
+ * @return a Ruby Integer
719
+ */
720
+ VALUE
721
+ rm_quantum2num(double value)
722
+ {
723
+ if (!std::isfinite(value))
724
+ {
725
+ return INT2FIX(0);
726
+ }
727
+ if (fabs(value) < 9223372036854775808.0)
728
+ {
729
+ return LL2NUM((long long) value);
730
+ }
731
+ return rb_dbl2big(value);
732
+ }
733
+ #endif
734
+
735
+
676
736
  /**
677
737
  * Returns a pointer to an image structure initialized to default values
678
738
  *
@@ -717,17 +777,17 @@ rm_cur_image(VALUE img)
717
777
 
718
778
 
719
779
  /**
720
- * Map the color intensity to a named color.
780
+ * Map the color intensity in a colorspace to a named color.
721
781
  *
722
782
  * No Ruby usage (internal function)
723
783
  *
724
784
  * @param image the image
725
785
  * @param color the color intensity as a PixelColor
786
+ * @param colorspace the colorspace of the color intensity
726
787
  * @return the named color as a String
727
- * @see rm_pixelcolor_to_color_name_info
728
788
  */
729
- VALUE
730
- rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
789
+ static VALUE
790
+ pixelcolor_to_color_name(Image *image, PixelColor *color, ColorspaceType colorspace)
731
791
  {
732
792
  MagickPixel mpp;
733
793
  char tuple[MaxTextExtent];
@@ -735,7 +795,6 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
735
795
  #if defined(IMAGEMAGICK_7)
736
796
  mpp = *color;
737
797
  mpp.alpha_trait = BlendPixelTrait;
738
- mpp.colorspace = image->colorspace;
739
798
  #else
740
799
  rm_init_magickpixel(image, &mpp);
741
800
  mpp.red = GetPixelRed(color);
@@ -745,6 +804,7 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
745
804
  mpp.index = (MagickRealType) 0.0;
746
805
  mpp.matte = MagickTrue;
747
806
  #endif
807
+ mpp.colorspace = colorspace;
748
808
  mpp.depth = image->depth;
749
809
 
750
810
  GetColorTuple(&mpp, MagickTrue, tuple);
@@ -758,16 +818,59 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
758
818
  *
759
819
  * No Ruby usage (internal function)
760
820
  *
821
+ * @param image the image
822
+ * @param color the color intensity as a PixelColor
823
+ * @return the named color as a String
824
+ * @see rm_pixelcolor_to_color_name_info
825
+ */
826
+ VALUE
827
+ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
828
+ {
829
+ return pixelcolor_to_color_name(image, color, image->colorspace);
830
+ }
831
+
832
+
833
+ /**
834
+ * Map the color intensity to a named color in sRGB, whatever the colorspace of the
835
+ * image is.
836
+ *
837
+ * No Ruby usage (internal function)
838
+ *
761
839
  * Notes:
762
- * - Simply create an Image from the Info, call QueryColorname, and then
840
+ * - A color in CMYK is converted to sRGB first. ImageMagick 7 keeps a CMYK
841
+ * color name in CMYK.
842
+ *
843
+ * @param image the image
844
+ * @param color the color intensity as a PixelColor
845
+ * @return the named color as a String
846
+ * @see rm_pixelcolor_to_color_name
847
+ */
848
+ VALUE
849
+ rm_srgb_pixelcolor_to_color_name(Image *image, PixelColor *color)
850
+ {
851
+ PixelColor srgb_color = *color;
852
+
853
+ #if defined(IMAGEMAGICK_7)
854
+ rm_pixelinfo_cmyk_to_rgb(&srgb_color);
855
+ #endif
856
+ return pixelcolor_to_color_name(image, &srgb_color, sRGBColorspace);
857
+ }
858
+
859
+
860
+ /**
861
+ * Map the color intensity to a named color in sRGB, as
862
+ * rm_srgb_pixelcolor_to_color_name does.
863
+ *
864
+ * No Ruby usage (internal function)
865
+ *
866
+ * Notes:
867
+ * - Create an Image from the Info to use the depth of the Info, and then
763
868
  * destroy the Image.
764
- * - The default depth is always used, and the matte value is set to False,
765
- * which means "don't use the alpha channel".
766
869
  *
767
870
  * @param info the info
768
871
  * @param color the color intensity as a PixelColor
769
872
  * @return the named color as a String
770
- * @see rm_pixelcolor_to_color_name
873
+ * @see rm_srgb_pixelcolor_to_color_name
771
874
  */
772
875
  VALUE
773
876
  rm_pixelcolor_to_color_name_info(Info *info, PixelColor *color)
@@ -787,7 +890,7 @@ rm_pixelcolor_to_color_name_info(Info *info, PixelColor *color)
787
890
  image->matte = MagickFalse;
788
891
  #endif
789
892
 
790
- color_name = rm_pixelcolor_to_color_name(image, color);
893
+ color_name = rm_srgb_pixelcolor_to_color_name(image, color);
791
894
  DestroyImage(image);
792
895
 
793
896
  return color_name;
@@ -853,32 +956,13 @@ rm_set_magickpixel(MagickPixel *pp, const char *color)
853
956
  void
854
957
  rm_write_temp_image(Image *image, char *temp_name, size_t temp_name_l)
855
958
  {
856
-
857
- #define TMPNAM_CLASS_VAR "@@_tmpnam_"
858
-
959
+ static std::atomic<unsigned long> last_id(0);
859
960
  MagickBooleanType okay;
860
961
  ExceptionInfo *exception;
861
- VALUE id_value;
862
- int id;
863
962
 
864
963
  exception = AcquireExceptionInfo();
865
964
 
866
-
867
- // 'id' is always the value of its previous use
868
- if (rb_cvar_defined(Module_Magick, rb_intern(TMPNAM_CLASS_VAR)) == Qtrue)
869
- {
870
- id_value = rb_cv_get(Module_Magick, TMPNAM_CLASS_VAR);
871
- id = FIX2INT(id_value);
872
- }
873
- else
874
- {
875
- id = 0;
876
- rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
877
- }
878
-
879
- id += 1;
880
- rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
881
- snprintf(temp_name, temp_name_l, "mpri:%d", id);
965
+ snprintf(temp_name, temp_name_l, "mpri:rmagick-%lu", ++last_id);
882
966
 
883
967
  // Omit "mpri:" from filename to form the key
884
968
  okay = (MagickBooleanType)SetImageRegistry(ImageRegistryType, temp_name+5, image, exception);
@@ -888,8 +972,64 @@ rm_write_temp_image(Image *image, char *temp_name, size_t temp_name_l)
888
972
  {
889
973
  rb_raise(rb_eRuntimeError, "SetImageRegistry failed.");
890
974
  }
975
+ }
976
+
977
+
978
+ //! the arguments of write_temp_image
979
+ struct WriteTempImageArgs
980
+ {
981
+ Image *image; /**< the image to register */
982
+ char *temp_name; /**< the buffer for its name */
983
+ size_t temp_name_l; /**< the length of temp_name */
984
+ };
985
+
986
+ static VALUE
987
+ write_temp_image(VALUE arg)
988
+ {
989
+ struct WriteTempImageArgs *args = (struct WriteTempImageArgs *)arg;
990
+
991
+ rm_write_temp_image(args->image, args->temp_name, args->temp_name_l);
992
+ return Qnil;
993
+ }
994
+
995
+ static VALUE
996
+ destroy_temp_image_copy(VALUE image)
997
+ {
998
+ DestroyImageList((Image *)image);
999
+ return Qnil;
1000
+ }
1001
+
1002
+
1003
+ /**
1004
+ * Register a copy of a temporary image under a new name.
1005
+ *
1006
+ * No Ruby usage (internal function)
1007
+ *
1008
+ * @param temp_name the name of the temporary image
1009
+ * @param new_name the buffer for the name of the copy
1010
+ * @param new_name_l the length of new_name
1011
+ * @return false if the registry has no image of temp_name
1012
+ */
1013
+ bool
1014
+ rm_copy_temp_image(const char *temp_name, char *new_name, size_t new_name_l)
1015
+ {
1016
+ ExceptionInfo *exception;
1017
+ Image *image;
1018
+ struct WriteTempImageArgs args;
1019
+
1020
+ exception = AcquireExceptionInfo();
1021
+ image = (Image *)GetImageRegistry(ImageRegistryType, temp_name+5, exception);
1022
+ DestroyExceptionInfo(exception);
1023
+ if (!image)
1024
+ {
1025
+ return false;
1026
+ }
891
1027
 
892
- RB_GC_GUARD(id_value);
1028
+ args.image = image;
1029
+ args.temp_name = new_name;
1030
+ args.temp_name_l = new_name_l;
1031
+ rb_ensure(write_temp_image, (VALUE)&args, destroy_temp_image_copy, (VALUE)image);
1032
+ return true;
893
1033
  }
894
1034
 
895
1035
 
@@ -905,7 +1045,7 @@ rm_delete_temp_image(char *temp_name)
905
1045
  {
906
1046
  MagickBooleanType okay = DeleteImageRegistry(temp_name+5);
907
1047
 
908
- if (!okay)
1048
+ if (!okay && !rb_during_gc())
909
1049
  {
910
1050
  rb_warn("DeleteImageRegistry failed for `%s'", temp_name);
911
1051
  }
@@ -940,6 +1080,29 @@ rm_magick_error(const char *msg)
940
1080
  }
941
1081
 
942
1082
  #if defined(IMAGEMAGICK_7)
1083
+ /**
1084
+ * Convert a color in CMYK to RGB with the formula of ConvertCMYKToRGB in
1085
+ * ImageMagick. A color in another colorspace is left as it is.
1086
+ *
1087
+ * No Ruby usage (internal function)
1088
+ *
1089
+ * @param pixel the pixel color to convert
1090
+ */
1091
+ void
1092
+ rm_pixelinfo_cmyk_to_rgb(PixelInfo *pixel)
1093
+ {
1094
+ if (pixel->colorspace != CMYKColorspace)
1095
+ {
1096
+ return;
1097
+ }
1098
+ pixel->red = ClampToQuantum(QuantumRange - (QuantumScale * pixel->red * (QuantumRange - pixel->black) + pixel->black));
1099
+ pixel->green = ClampToQuantum(QuantumRange - (QuantumScale * pixel->green * (QuantumRange - pixel->black) + pixel->black));
1100
+ pixel->blue = ClampToQuantum(QuantumRange - (QuantumScale * pixel->blue * (QuantumRange - pixel->black) + pixel->black));
1101
+ pixel->black = 0;
1102
+ pixel->colorspace = sRGBColorspace;
1103
+ }
1104
+
1105
+
943
1106
  /**
944
1107
  * Sets the alpha channel of a pixel color
945
1108
  *
@@ -1089,33 +1252,132 @@ void rm_set_user_artifact(Image *images, Info *info)
1089
1252
  * No Ruby usage (internal function)
1090
1253
  *
1091
1254
  * Notes:
1092
- * - Creates an instance of Magick::OptionalMethodArguments, then yields to a
1093
- * block in the context of the instance.
1255
+ * - Creates an instance of Magick::OptionalMethodArguments, then yields it
1256
+ * to the block. The image is not changed; pass the result to
1257
+ * rm_set_optional_arguments to set the artifacts on the image that the
1258
+ * method uses.
1094
1259
  *
1095
1260
  * @param img the image
1261
+ * @return an array of [artifact, value] pairs, or nil without a block
1096
1262
  */
1097
- void
1263
+ VALUE
1098
1264
  rm_get_optional_arguments(VALUE img)
1099
1265
  {
1100
1266
  VALUE optional_method_arguments;
1101
1267
  VALUE opt_args;
1102
1268
  VALUE argv[1];
1103
1269
 
1104
- // opt_args = Magick::OptionalMethodArguments.new(img)
1105
- // opt_args.instance_eval { block }
1106
- if (rb_block_given_p())
1270
+ if (!rb_block_given_p())
1271
+ {
1272
+ return Qnil;
1273
+ }
1274
+
1275
+ optional_method_arguments = rb_const_get_from(Module_Magick, rb_intern("OptionalMethodArguments"));
1276
+ argv[0] = img;
1277
+ opt_args = rb_class_new_instance(1, argv, optional_method_arguments);
1278
+
1279
+ rb_yield(opt_args);
1280
+
1281
+ VALUE pairs = rb_funcall(rb_iv_get(opt_args, "@artifacts"), rb_intern("to_a"), 0);
1282
+ Check_Type(pairs, T_ARRAY);
1283
+ VALUE artifacts = rb_ary_new2(RARRAY_LEN(pairs));
1284
+ for (long i = 0; i < RARRAY_LEN(pairs); i++)
1107
1285
  {
1108
- optional_method_arguments = rb_const_get_from(Module_Magick, rb_intern("OptionalMethodArguments"));
1109
- argv[0] = img;
1110
- opt_args = rb_class_new_instance(1, argv, optional_method_arguments);
1286
+ VALUE pair = rb_ary_entry(pairs, i);
1287
+ Check_Type(pair, T_ARRAY);
1288
+ VALUE key = rb_ary_entry(pair, 0);
1289
+ VALUE value = rb_ary_entry(pair, 1);
1111
1290
 
1112
- rb_yield(opt_args);
1291
+ Check_Type(key, T_STRING);
1292
+ key = rb_str_new_frozen(key);
1293
+ StringValueCStr(key);
1294
+ if (!NIL_P(value))
1295
+ {
1296
+ Check_Type(value, T_STRING);
1297
+ value = rb_str_new_frozen(value);
1298
+ StringValueCStr(value);
1299
+ }
1300
+ rb_ary_push(artifacts, rb_assoc_new(key, value));
1113
1301
  }
1114
1302
 
1115
1303
  RB_GC_GUARD(optional_method_arguments);
1116
- RB_GC_GUARD(opt_args);
1304
+ RB_GC_GUARD(pairs);
1117
1305
 
1118
- return;
1306
+ return artifacts;
1307
+ }
1308
+
1309
+
1310
+ /**
1311
+ * Set the artifacts that rm_get_optional_arguments collected on an image.
1312
+ *
1313
+ * No Ruby usage (internal function)
1314
+ *
1315
+ * @param image the image
1316
+ * @param artifacts the result of rm_get_optional_arguments
1317
+ * @return MagickFalse if ImageMagick could not set an artifact
1318
+ */
1319
+ MagickBooleanType
1320
+ rm_set_optional_arguments(Image *image, VALUE artifacts)
1321
+ {
1322
+ if (NIL_P(artifacts))
1323
+ {
1324
+ return MagickTrue;
1325
+ }
1326
+
1327
+ for (long i = 0; i < RARRAY_LEN(artifacts); i++)
1328
+ {
1329
+ VALUE pair = rb_ary_entry(artifacts, i);
1330
+ VALUE key = rb_ary_entry(pair, 0);
1331
+ VALUE value = rb_ary_entry(pair, 1);
1332
+
1333
+ if (NIL_P(value))
1334
+ {
1335
+ DeleteImageArtifact(image, RSTRING_PTR(key));
1336
+ }
1337
+ else if (!SetImageArtifact(image, RSTRING_PTR(key), RSTRING_PTR(value)))
1338
+ {
1339
+ return MagickFalse;
1340
+ }
1341
+ }
1342
+ return MagickTrue;
1343
+ }
1344
+
1345
+
1346
+ /**
1347
+ * Give the result of a method the values that the source image has for the
1348
+ * artifacts that rm_get_optional_arguments collected, so that the options of
1349
+ * the block apply only to the call.
1350
+ *
1351
+ * No Ruby usage (internal function)
1352
+ *
1353
+ * @param result the image that the method returns
1354
+ * @param source the image that the method was called on
1355
+ * @param artifacts the result of rm_get_optional_arguments
1356
+ * @return MagickFalse if ImageMagick could not set an artifact
1357
+ */
1358
+ MagickBooleanType
1359
+ rm_restore_optional_arguments(Image *result, Image *source, VALUE artifacts)
1360
+ {
1361
+ if (NIL_P(artifacts))
1362
+ {
1363
+ return MagickTrue;
1364
+ }
1365
+
1366
+ for (long i = 0; i < RARRAY_LEN(artifacts); i++)
1367
+ {
1368
+ const char *key = RSTRING_PTR(rb_ary_entry(rb_ary_entry(artifacts, i), 0));
1369
+ const char *value = GetImageArtifact(source, key);
1370
+
1371
+ if (!value)
1372
+ {
1373
+ DeleteImageArtifact(result, key);
1374
+ }
1375
+ else if (!SetImageArtifact(result, key, value))
1376
+ {
1377
+ return MagickFalse;
1378
+ }
1379
+ }
1380
+ return MagickTrue;
1119
1381
  }
1120
1382
 
1121
1383
 
@@ -1155,9 +1417,11 @@ static void copy_options(Image *image, Info *info)
1155
1417
  *
1156
1418
  * @param image the Image structure to modify
1157
1419
  * @param info the Info structure
1420
+ * @param owner the Image object that holds image, an Array of the Image objects
1421
+ * whose images are linked with it, or nil
1158
1422
  * @see SyncImageSettings in mogrify.c in ImageMagick
1159
1423
  */
1160
- void rm_sync_image_options(Image *image, Info *info)
1424
+ void rm_sync_image_options(Image *image, Info *info, VALUE owner)
1161
1425
  {
1162
1426
  MagickStatusType flags;
1163
1427
  GeometryInfo geometry_info;
@@ -1185,12 +1449,12 @@ void rm_sync_image_options(Image *image, Info *info)
1185
1449
  #if defined(IMAGEMAGICK_7)
1186
1450
  exception = AcquireExceptionInfo();
1187
1451
  DECLARE_GVL_CALL(call, TransformImageColorspace, image, info->colorspace, exception);
1188
- call.keep_thread().run<void>();
1452
+ call.hold(owner, true).keep_thread().release(exception).run<void>();
1189
1453
  CHECK_EXCEPTION();
1190
1454
  DestroyExceptionInfo(exception);
1191
1455
  #else
1192
1456
  DECLARE_GVL_CALL(call, TransformImageColorspace, image, info->colorspace);
1193
- call.keep_thread().run<void>();
1457
+ call.hold(owner, true).keep_thread().run<void>();
1194
1458
  rm_check_image_exception(image, RetainOnError);
1195
1459
  #endif
1196
1460
  }
@@ -1600,23 +1864,31 @@ rm_exif_by_number(Image *image)
1600
1864
  * the caller do the trace if desired.
1601
1865
  *
1602
1866
  * @param image the image to clone
1867
+ * @param owner the Image object that holds image, an Array of the Image objects
1868
+ * whose images are linked with it, or nil
1869
+ * @param held another Image object whose image the caller uses after the clone,
1870
+ * an Array of them, or nil
1871
+ * @param changes whether the caller changes the images of owner after the clone
1603
1872
  * @return the cloned image
1604
1873
  */
1605
1874
  Image *
1606
- rm_clone_image(Image *image)
1875
+ rm_clone_image(Image *image, VALUE owner, VALUE held, bool changes)
1607
1876
  {
1608
1877
  Image *clone;
1609
1878
  ExceptionInfo *exception;
1610
1879
 
1611
1880
  exception = AcquireExceptionInfo();
1612
1881
  DECLARE_GVL_CALL(call, CloneImage, image, 0, 0, MagickTrue, exception);
1613
- clone = call.keep_thread().run<Image *>();
1882
+ clone = call.hold(owner, changes).hold(held).keep_thread().release(exception).run<Image *>();
1614
1883
  if (!clone)
1615
1884
  {
1616
1885
  rb_raise(rb_eNoMemError, "not enough memory to continue");
1617
1886
  }
1618
1887
  rm_check_exception(exception, clone, DestroyOnError);
1619
1888
  DestroyExceptionInfo(exception);
1889
+ #if defined(IMAGEMAGICK_6)
1890
+ ClearMagickException(&clone->exception);
1891
+ #endif
1620
1892
 
1621
1893
  return clone;
1622
1894
  }
@@ -1647,6 +1919,25 @@ rm_split(Image *image)
1647
1919
  }
1648
1920
 
1649
1921
 
1922
+ /**
1923
+ * Keep the first image of a list and destroy the others, such as the layers
1924
+ * that ImageMagick reads from a PSD file.
1925
+ *
1926
+ * No Ruby usage (internal function)
1927
+ *
1928
+ * @param images the list, or NULL
1929
+ * @return the first image, or NULL
1930
+ */
1931
+ Image *
1932
+ rm_first_image(Image *images)
1933
+ {
1934
+ Image *image = RemoveFirstImageFromList(&images);
1935
+
1936
+ DestroyImageList(images);
1937
+ return image;
1938
+ }
1939
+
1940
+
1650
1941
  #if defined(IMAGEMAGICK_6)
1651
1942
  /**
1652
1943
  * If an ExceptionInfo struct in a list of images indicates a warning, issue a