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,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,31 +276,78 @@ rm_check_ary_type(VALUE ary)
307
276
 
308
277
 
309
278
  /**
310
- * Raise an error if the image has been destroyed.
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
312
+ * @throw RuntimeError if a call in flight is changing the image
317
313
  */
318
314
  Image *
319
- rm_check_destroyed(VALUE obj)
315
+ rm_check_readable(VALUE obj)
320
316
  {
321
317
  Image *image;
322
318
 
323
- TypedData_Get_Struct(obj, Image, &rm_image_data_type, image);
319
+ image = rm_image_get(obj);
324
320
  if (!image)
325
321
  {
326
322
  rb_raise(Class_DestroyedImageError, "destroyed image");
327
323
  }
324
+ rm_gvl_check_readable(obj);
328
325
 
329
326
  return image;
330
327
  }
331
328
 
332
329
 
333
330
  /**
334
- * Raise an error if the image has been destroyed or is frozen.
331
+ * Raise an error if the image has been destroyed or a call in flight is using it.
332
+ *
333
+ * No Ruby usage (internal function)
334
+ *
335
+ * @param obj the image
336
+ * @return the C image structure for the image
337
+ * @throw DestroyedImageError
338
+ * @throw RuntimeError if a call in flight is using the image
339
+ */
340
+ Image *
341
+ rm_check_writable(VALUE obj)
342
+ {
343
+ Image *image = rm_check_readable(obj);
344
+ rm_gvl_check_writable(obj);
345
+ return image;
346
+ }
347
+
348
+
349
+ /**
350
+ * Raise an error if the image has been destroyed, is frozen or a call in flight is using it.
335
351
  *
336
352
  * No Ruby usage (internal function)
337
353
  *
@@ -341,8 +357,15 @@ rm_check_destroyed(VALUE obj)
341
357
  Image *
342
358
  rm_check_frozen(VALUE obj)
343
359
  {
344
- Image *image = rm_check_destroyed(obj);
360
+ Image *image;
361
+
362
+ image = rm_image_get(obj);
363
+ if (!image)
364
+ {
365
+ rb_raise(Class_DestroyedImageError, "destroyed image");
366
+ }
345
367
  rb_check_frozen(obj);
368
+ rm_gvl_check_writable(obj);
346
369
  return image;
347
370
  }
348
371
 
@@ -361,6 +384,23 @@ rm_no_freeze(VALUE obj)
361
384
  }
362
385
 
363
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
+
364
404
  /**
365
405
  * Supply our own version of the "obsolete" rb_str2cstr.
366
406
  *
@@ -537,6 +577,10 @@ rescue_not_dbl(VALUE ignored ATTRIBUTE_UNUSED, VALUE raised_exc ATTRIBUTE_UNUSED
537
577
  int
538
578
  rm_check_num2dbl(VALUE obj)
539
579
  {
580
+ if (FIXNUM_P(obj) || RB_FLOAT_TYPE_P(obj))
581
+ {
582
+ return 1;
583
+ }
540
584
  return FIX2INT(rb_rescue(RESCUE_FUNC(check_num2dbl), obj, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_dbl), (VALUE)0));
541
585
  }
542
586
 
@@ -598,7 +642,9 @@ rm_fuzz_to_dbl(VALUE fuzz_arg)
598
642
 
599
643
  /**
600
644
  * Convert an application-supplied number to a Quantum. If the object is a Float,
601
- * truncate it before converting.
645
+ * truncate it before converting. A value outside 0 to QuantumRange is clamped
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.
602
648
  *
603
649
  * No Ruby usage (internal function)
604
650
  *
@@ -615,16 +661,78 @@ Quantum
615
661
  rm_app2quantum(VALUE obj)
616
662
  {
617
663
  VALUE v = obj;
664
+ MagickRealType value;
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
+ }
618
674
 
619
675
  if (TYPE(obj) == T_FLOAT)
620
676
  {
621
677
  v = rb_Integer(obj);
622
678
  }
679
+ v = rb_to_int(v);
623
680
 
624
- return NUM2QUANTUM(v);
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))
685
+ {
686
+ #if MAGICKCORE_QUANTUM_DEPTH == 64
687
+ value = (MagickRealType) NUM2LL(v);
688
+ #else
689
+ value = (MagickRealType) NUM2INT(v);
690
+ #endif
691
+ }
692
+ else
693
+ {
694
+ #if MAGICKCORE_QUANTUM_DEPTH == 64
695
+ value = (MagickRealType) NUM2ULL(v);
696
+ #else
697
+ value = (MagickRealType) NUM2UINT(v);
698
+ #endif
699
+ }
700
+ #endif
701
+
702
+ return ClampToQuantum(value);
625
703
  }
626
704
 
627
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
+
628
736
  /**
629
737
  * Returns a pointer to an image structure initialized to default values
630
738
  *
@@ -669,17 +777,17 @@ rm_cur_image(VALUE img)
669
777
 
670
778
 
671
779
  /**
672
- * Map the color intensity to a named color.
780
+ * Map the color intensity in a colorspace to a named color.
673
781
  *
674
782
  * No Ruby usage (internal function)
675
783
  *
676
784
  * @param image the image
677
785
  * @param color the color intensity as a PixelColor
786
+ * @param colorspace the colorspace of the color intensity
678
787
  * @return the named color as a String
679
- * @see rm_pixelcolor_to_color_name_info
680
788
  */
681
- VALUE
682
- rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
789
+ static VALUE
790
+ pixelcolor_to_color_name(Image *image, PixelColor *color, ColorspaceType colorspace)
683
791
  {
684
792
  MagickPixel mpp;
685
793
  char tuple[MaxTextExtent];
@@ -687,7 +795,6 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
687
795
  #if defined(IMAGEMAGICK_7)
688
796
  mpp = *color;
689
797
  mpp.alpha_trait = BlendPixelTrait;
690
- mpp.colorspace = image->colorspace;
691
798
  #else
692
799
  rm_init_magickpixel(image, &mpp);
693
800
  mpp.red = GetPixelRed(color);
@@ -697,6 +804,7 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
697
804
  mpp.index = (MagickRealType) 0.0;
698
805
  mpp.matte = MagickTrue;
699
806
  #endif
807
+ mpp.colorspace = colorspace;
700
808
  mpp.depth = image->depth;
701
809
 
702
810
  GetColorTuple(&mpp, MagickTrue, tuple);
@@ -710,16 +818,59 @@ rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
710
818
  *
711
819
  * No Ruby usage (internal function)
712
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
+ *
713
839
  * Notes:
714
- * - 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
715
868
  * destroy the Image.
716
- * - The default depth is always used, and the matte value is set to False,
717
- * which means "don't use the alpha channel".
718
869
  *
719
870
  * @param info the info
720
871
  * @param color the color intensity as a PixelColor
721
872
  * @return the named color as a String
722
- * @see rm_pixelcolor_to_color_name
873
+ * @see rm_srgb_pixelcolor_to_color_name
723
874
  */
724
875
  VALUE
725
876
  rm_pixelcolor_to_color_name_info(Info *info, PixelColor *color)
@@ -739,7 +890,7 @@ rm_pixelcolor_to_color_name_info(Info *info, PixelColor *color)
739
890
  image->matte = MagickFalse;
740
891
  #endif
741
892
 
742
- color_name = rm_pixelcolor_to_color_name(image, color);
893
+ color_name = rm_srgb_pixelcolor_to_color_name(image, color);
743
894
  DestroyImage(image);
744
895
 
745
896
  return color_name;
@@ -805,32 +956,13 @@ rm_set_magickpixel(MagickPixel *pp, const char *color)
805
956
  void
806
957
  rm_write_temp_image(Image *image, char *temp_name, size_t temp_name_l)
807
958
  {
808
-
809
- #define TMPNAM_CLASS_VAR "@@_tmpnam_"
810
-
959
+ static std::atomic<unsigned long> last_id(0);
811
960
  MagickBooleanType okay;
812
961
  ExceptionInfo *exception;
813
- VALUE id_value;
814
- int id;
815
962
 
816
963
  exception = AcquireExceptionInfo();
817
964
 
818
-
819
- // 'id' is always the value of its previous use
820
- if (rb_cvar_defined(Module_Magick, rb_intern(TMPNAM_CLASS_VAR)) == Qtrue)
821
- {
822
- id_value = rb_cv_get(Module_Magick, TMPNAM_CLASS_VAR);
823
- id = FIX2INT(id_value);
824
- }
825
- else
826
- {
827
- id = 0;
828
- rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
829
- }
830
-
831
- id += 1;
832
- rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
833
- snprintf(temp_name, temp_name_l, "mpri:%d", id);
965
+ snprintf(temp_name, temp_name_l, "mpri:rmagick-%lu", ++last_id);
834
966
 
835
967
  // Omit "mpri:" from filename to form the key
836
968
  okay = (MagickBooleanType)SetImageRegistry(ImageRegistryType, temp_name+5, image, exception);
@@ -840,8 +972,64 @@ rm_write_temp_image(Image *image, char *temp_name, size_t temp_name_l)
840
972
  {
841
973
  rb_raise(rb_eRuntimeError, "SetImageRegistry failed.");
842
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
+ }
843
994
 
844
- RB_GC_GUARD(id_value);
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
+ }
1027
+
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;
845
1033
  }
846
1034
 
847
1035
 
@@ -857,7 +1045,7 @@ rm_delete_temp_image(char *temp_name)
857
1045
  {
858
1046
  MagickBooleanType okay = DeleteImageRegistry(temp_name+5);
859
1047
 
860
- if (!okay)
1048
+ if (!okay && !rb_during_gc())
861
1049
  {
862
1050
  rb_warn("DeleteImageRegistry failed for `%s'", temp_name);
863
1051
  }
@@ -892,6 +1080,29 @@ rm_magick_error(const char *msg)
892
1080
  }
893
1081
 
894
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
+
895
1106
  /**
896
1107
  * Sets the alpha channel of a pixel color
897
1108
  *
@@ -1041,33 +1252,132 @@ void rm_set_user_artifact(Image *images, Info *info)
1041
1252
  * No Ruby usage (internal function)
1042
1253
  *
1043
1254
  * Notes:
1044
- * - Creates an instance of Magick::OptionalMethodArguments, then yields to a
1045
- * 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.
1046
1259
  *
1047
1260
  * @param img the image
1261
+ * @return an array of [artifact, value] pairs, or nil without a block
1048
1262
  */
1049
- void
1263
+ VALUE
1050
1264
  rm_get_optional_arguments(VALUE img)
1051
1265
  {
1052
1266
  VALUE optional_method_arguments;
1053
1267
  VALUE opt_args;
1054
1268
  VALUE argv[1];
1055
1269
 
1056
- // opt_args = Magick::OptionalMethodArguments.new(img)
1057
- // opt_args.instance_eval { block }
1058
- if (rb_block_given_p())
1270
+ if (!rb_block_given_p())
1059
1271
  {
1060
- optional_method_arguments = rb_const_get_from(Module_Magick, rb_intern("OptionalMethodArguments"));
1061
- argv[0] = img;
1062
- opt_args = rb_class_new_instance(1, argv, optional_method_arguments);
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);
1063
1278
 
1064
- rb_yield(opt_args);
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++)
1285
+ {
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);
1290
+
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));
1065
1301
  }
1066
1302
 
1067
1303
  RB_GC_GUARD(optional_method_arguments);
1068
- RB_GC_GUARD(opt_args);
1304
+ RB_GC_GUARD(pairs);
1069
1305
 
1070
- 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;
1071
1381
  }
1072
1382
 
1073
1383
 
@@ -1107,9 +1417,11 @@ static void copy_options(Image *image, Info *info)
1107
1417
  *
1108
1418
  * @param image the Image structure to modify
1109
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
1110
1422
  * @see SyncImageSettings in mogrify.c in ImageMagick
1111
1423
  */
1112
- void rm_sync_image_options(Image *image, Info *info)
1424
+ void rm_sync_image_options(Image *image, Info *info, VALUE owner)
1113
1425
  {
1114
1426
  MagickStatusType flags;
1115
1427
  GeometryInfo geometry_info;
@@ -1136,13 +1448,13 @@ void rm_sync_image_options(Image *image, Info *info)
1136
1448
  {
1137
1449
  #if defined(IMAGEMAGICK_7)
1138
1450
  exception = AcquireExceptionInfo();
1139
- GVL_STRUCT_TYPE(TransformImageColorspace) args = { image, info->colorspace, exception };
1140
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(TransformImageColorspace), &args);
1451
+ DECLARE_GVL_CALL(call, TransformImageColorspace, image, info->colorspace, exception);
1452
+ call.hold(owner, true).keep_thread().release(exception).run<void>();
1141
1453
  CHECK_EXCEPTION();
1142
1454
  DestroyExceptionInfo(exception);
1143
1455
  #else
1144
- GVL_STRUCT_TYPE(TransformImageColorspace) args = { image, info->colorspace };
1145
- CALL_FUNC_WITHOUT_GVL(GVL_FUNC(TransformImageColorspace), &args);
1456
+ DECLARE_GVL_CALL(call, TransformImageColorspace, image, info->colorspace);
1457
+ call.hold(owner, true).keep_thread().run<void>();
1146
1458
  rm_check_image_exception(image, RetainOnError);
1147
1459
  #endif
1148
1460
  }
@@ -1552,23 +1864,31 @@ rm_exif_by_number(Image *image)
1552
1864
  * the caller do the trace if desired.
1553
1865
  *
1554
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
1555
1872
  * @return the cloned image
1556
1873
  */
1557
1874
  Image *
1558
- rm_clone_image(Image *image)
1875
+ rm_clone_image(Image *image, VALUE owner, VALUE held, bool changes)
1559
1876
  {
1560
1877
  Image *clone;
1561
1878
  ExceptionInfo *exception;
1562
1879
 
1563
1880
  exception = AcquireExceptionInfo();
1564
- GVL_STRUCT_TYPE(CloneImage) args = { image, 0, 0, MagickTrue, exception };
1565
- clone = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CloneImage), &args);
1881
+ DECLARE_GVL_CALL(call, CloneImage, image, 0, 0, MagickTrue, exception);
1882
+ clone = call.hold(owner, changes).hold(held).keep_thread().release(exception).run<Image *>();
1566
1883
  if (!clone)
1567
1884
  {
1568
1885
  rb_raise(rb_eNoMemError, "not enough memory to continue");
1569
1886
  }
1570
1887
  rm_check_exception(exception, clone, DestroyOnError);
1571
1888
  DestroyExceptionInfo(exception);
1889
+ #if defined(IMAGEMAGICK_6)
1890
+ ClearMagickException(&clone->exception);
1891
+ #endif
1572
1892
 
1573
1893
  return clone;
1574
1894
  }
@@ -1599,6 +1919,25 @@ rm_split(Image *image)
1599
1919
  }
1600
1920
 
1601
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
+
1602
1941
  #if defined(IMAGEMAGICK_6)
1603
1942
  /**
1604
1943
  * If an ExceptionInfo struct in a list of images indicates a warning, issue a