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.
@@ -18,32 +18,9 @@
18
18
 
19
19
  static VALUE color_arg_rescue(VALUE, VALUE ATTRIBUTE_UNUSED) ATTRIBUTE_NORETURN;
20
20
  static void Color_Name_to_PixelColor(PixelColor *, VALUE);
21
- static void Pixel_destroy(void *);
22
- static size_t Pixel_memsize(const void *);
23
-
24
- const rb_data_type_t rm_pixel_data_type = {
25
- "Magick::Pixel",
26
- { NULL, Pixel_destroy, Pixel_memsize, },
27
- 0, 0,
28
- RUBY_TYPED_FROZEN_SHAREABLE,
29
- };
30
-
31
-
32
- /**
33
- * Free the storage associated with a Pixel object.
34
- *
35
- * No Ruby usage (internal function)
36
- *
37
- * @param ptr the Pixel object to destroy
38
- */
39
- static void
40
- Pixel_destroy(void *ptr)
41
- {
42
- Pixel *pixel = (Pixel *)ptr;
43
- xfree(pixel);
44
- }
45
21
 
46
22
 
23
+ #if !defined(RUBY_TYPED_EMBEDDABLE)
47
24
  /**
48
25
  * Get Pixel object size.
49
26
  *
@@ -56,6 +33,21 @@ Pixel_memsize(const void *ptr)
56
33
  {
57
34
  return sizeof(Pixel);
58
35
  }
36
+ #endif
37
+
38
+
39
+ const rb_data_type_t rm_pixel_data_type = {
40
+ "Magick::Pixel",
41
+ #if defined(RUBY_TYPED_EMBEDDABLE)
42
+ { NULL, RUBY_TYPED_DEFAULT_FREE, NULL, },
43
+ 0, 0,
44
+ RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE,
45
+ #else
46
+ { NULL, RUBY_TYPED_DEFAULT_FREE, Pixel_memsize, },
47
+ 0, 0,
48
+ RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_FREE_IMMEDIATELY,
49
+ #endif
50
+ };
59
51
 
60
52
 
61
53
  /**
@@ -66,7 +58,7 @@ Pixel_memsize(const void *ptr)
66
58
  VALUE
67
59
  Pixel_red(VALUE self)
68
60
  {
69
- IMPLEMENT_TYPED_ATTR_READER(Pixel, red, int, &rm_pixel_data_type);
61
+ IMPLEMENT_TYPED_ATTR_READER(Pixel, red, Quantum, &rm_pixel_data_type);
70
62
  }
71
63
 
72
64
  /**
@@ -77,7 +69,7 @@ Pixel_red(VALUE self)
77
69
  VALUE
78
70
  Pixel_green(VALUE self)
79
71
  {
80
- IMPLEMENT_TYPED_ATTR_READER(Pixel, green, int, &rm_pixel_data_type);
72
+ IMPLEMENT_TYPED_ATTR_READER(Pixel, green, Quantum, &rm_pixel_data_type);
81
73
  }
82
74
 
83
75
  /**
@@ -88,7 +80,7 @@ Pixel_green(VALUE self)
88
80
  VALUE
89
81
  Pixel_blue(VALUE self)
90
82
  {
91
- IMPLEMENT_TYPED_ATTR_READER(Pixel, blue, int, &rm_pixel_data_type);
83
+ IMPLEMENT_TYPED_ATTR_READER(Pixel, blue, Quantum, &rm_pixel_data_type);
92
84
  }
93
85
 
94
86
  /**
@@ -102,9 +94,9 @@ Pixel_alpha(VALUE self)
102
94
  Pixel *pixel;
103
95
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
104
96
  #if defined(IMAGEMAGICK_7)
105
- return C_int_to_R_int(pixel->alpha);
97
+ return QUANTUM2NUM(pixel->alpha);
106
98
  #else
107
- return C_int_to_R_int(QuantumRange - pixel->opacity);
99
+ return QUANTUM2NUM(QuantumRange - pixel->opacity);
108
100
  #endif
109
101
  }
110
102
 
@@ -222,7 +214,7 @@ Pixel_cyan(VALUE self)
222
214
  Pixel *pixel;
223
215
 
224
216
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
225
- return INT2NUM(pixel->red);
217
+ return QUANTUM2NUM(pixel->red);
226
218
  }
227
219
 
228
220
  /**
@@ -260,7 +252,7 @@ Pixel_magenta(VALUE self)
260
252
  Pixel *pixel;
261
253
 
262
254
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
263
- return INT2NUM(pixel->green);
255
+ return QUANTUM2NUM(pixel->green);
264
256
  }
265
257
 
266
258
  /**
@@ -298,7 +290,7 @@ Pixel_yellow(VALUE self)
298
290
  Pixel *pixel;
299
291
 
300
292
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
301
- return INT2NUM(pixel->blue);
293
+ return QUANTUM2NUM(pixel->blue);
302
294
  }
303
295
 
304
296
  /**
@@ -336,7 +328,7 @@ Pixel_black(VALUE self)
336
328
  Pixel *pixel;
337
329
 
338
330
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
339
- return INT2NUM(pixel->black);
331
+ return QUANTUM2NUM(pixel->black);
340
332
  }
341
333
 
342
334
  /**
@@ -396,7 +388,7 @@ Color_to_PixelColor(PixelColor *pp, VALUE color)
396
388
  Pixel *pixel;
397
389
 
398
390
  // Allow color name or Pixel
399
- if (CLASS_OF(color) == Class_Pixel)
391
+ if (rb_typeddata_is_kind_of(color, &rm_pixel_data_type))
400
392
  {
401
393
  memset(pp, 0, sizeof(*pp));
402
394
  TypedData_Get_Struct(color, Pixel, &rm_pixel_data_type, pixel);
@@ -434,7 +426,7 @@ Color_to_Pixel(Pixel *pp, VALUE color)
434
426
 
435
427
  memset(pp, 0, sizeof(*pp));
436
428
  // Allow color name or Pixel
437
- if (CLASS_OF(color) == Class_Pixel)
429
+ if (rb_typeddata_is_kind_of(color, &rm_pixel_data_type))
438
430
  {
439
431
  Pixel *pixel;
440
432
 
@@ -495,9 +487,7 @@ Pixel_alloc(VALUE klass)
495
487
  {
496
488
  Pixel *pixel;
497
489
 
498
- pixel = ALLOC(Pixel);
499
- memset(pixel, '\0', sizeof(Pixel));
500
- return TypedData_Wrap_Struct(klass, &rm_pixel_data_type, pixel);
490
+ return TypedData_Make_Struct(klass, Pixel, &rm_pixel_data_type, pixel);
501
491
  }
502
492
 
503
493
 
@@ -511,7 +501,7 @@ Pixel_alloc(VALUE klass)
511
501
  VALUE
512
502
  Pixel_case_eq(VALUE self, VALUE other)
513
503
  {
514
- if (CLASS_OF(self) == CLASS_OF(other))
504
+ if (rb_obj_class(self) == rb_obj_class(other))
515
505
  {
516
506
  Pixel *self_pixel, *other_pixel;
517
507
 
@@ -521,10 +511,11 @@ Pixel_case_eq(VALUE self, VALUE other)
521
511
  && self_pixel->blue == other_pixel->blue
522
512
  && self_pixel->green == other_pixel->green
523
513
  #if defined(IMAGEMAGICK_7)
524
- && self_pixel->alpha == other_pixel->alpha) ? Qtrue : Qfalse;
514
+ && self_pixel->alpha == other_pixel->alpha
525
515
  #else
526
- && self_pixel->opacity == other_pixel->opacity) ? Qtrue : Qfalse;
516
+ && self_pixel->opacity == other_pixel->opacity
527
517
  #endif
518
+ && self_pixel->black == other_pixel->black) ? Qtrue : Qfalse;
528
519
  }
529
520
 
530
521
  return Qfalse;
@@ -544,7 +535,7 @@ Pixel_case_eq(VALUE self, VALUE other)
544
535
  VALUE
545
536
  Pixel_eql_q(VALUE self, VALUE other)
546
537
  {
547
- return NUM2INT(Pixel_spaceship(self, other)) == 0 ? Qtrue : Qfalse;
538
+ return Pixel_spaceship(self, other) == INT2FIX(0) ? Qtrue : Qfalse;
548
539
  }
549
540
 
550
541
 
@@ -624,7 +615,8 @@ Pixel_fcmp(int argc, VALUE *argv, VALUE self)
624
615
 
625
616
 
626
617
  /**
627
- * Construct a {Magick::Pixel} corresponding to the given color name.
618
+ * Construct a {Magick::Pixel} corresponding to the given color name. A CMYK color
619
+ * name is converted to RGB.
628
620
  *
629
621
  * - The "inverse" is {Image#to_color}, b/c the conversion of a pixel to a
630
622
  * color name requires both a color depth and if the opacity value has
@@ -653,6 +645,10 @@ Pixel_from_color(VALUE klass ATTRIBUTE_UNUSED, VALUE name)
653
645
  rb_raise(rb_eArgError, "invalid color name: %s", color);
654
646
  }
655
647
 
648
+ #if defined(IMAGEMAGICK_7)
649
+ rm_pixelinfo_cmyk_to_rgb(&pp);
650
+ #endif
651
+
656
652
  return Pixel_from_PixelColor(&pp);
657
653
  }
658
654
 
@@ -755,8 +751,8 @@ VALUE
755
751
  Pixel_from_MagickPixel(const MagickPixel *pp)
756
752
  {
757
753
  Pixel *pixel;
754
+ VALUE obj = TypedData_Make_Struct(Class_Pixel, Pixel, &rm_pixel_data_type, pixel);
758
755
 
759
- pixel = ALLOC(Pixel);
760
756
  pixel->red = pp->red;
761
757
  pixel->green = pp->green;
762
758
  pixel->blue = pp->blue;
@@ -767,39 +763,7 @@ Pixel_from_MagickPixel(const MagickPixel *pp)
767
763
  #endif
768
764
  pixel->black = pp->index;
769
765
 
770
- return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
771
- }
772
-
773
-
774
- /**
775
- * Create a Magick::Pixel object from a PixelPacket structure.
776
- *
777
- * No Ruby usage (internal function)
778
- *
779
- * Notes:
780
- * - Bypasses normal Pixel.new, Pixel#initialize methods
781
- *
782
- * @param pp the PixelPacket
783
- * @return a new Magick::Pixel object
784
- */
785
- VALUE
786
- Pixel_from_PixelPacket(const PixelPacket *pp)
787
- {
788
- Pixel *pixel;
789
-
790
- pixel = ALLOC(Pixel);
791
- pixel->red = pp->red;
792
- pixel->green = pp->green;
793
- pixel->blue = pp->blue;
794
- #if defined(IMAGEMAGICK_7)
795
- pixel->alpha = pp->alpha;
796
- pixel->black = pp->black;
797
- #else
798
- pixel->opacity = pp->opacity;
799
- pixel->black = 0;
800
- #endif
801
-
802
- return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
766
+ return obj;
803
767
  }
804
768
 
805
769
 
@@ -818,8 +782,8 @@ VALUE
818
782
  Pixel_from_PixelColor(const PixelColor *pp)
819
783
  {
820
784
  Pixel *pixel;
785
+ VALUE obj = TypedData_Make_Struct(Class_Pixel, Pixel, &rm_pixel_data_type, pixel);
821
786
 
822
- pixel = ALLOC(Pixel);
823
787
  pixel->red = pp->red;
824
788
  pixel->green = pp->green;
825
789
  pixel->blue = pp->blue;
@@ -831,7 +795,7 @@ Pixel_from_PixelColor(const PixelColor *pp)
831
795
  pixel->black = 0;
832
796
  #endif
833
797
 
834
- return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
798
+ return obj;
835
799
  }
836
800
 
837
801
 
@@ -856,6 +820,7 @@ Pixel_hash(VALUE self)
856
820
  #else
857
821
  hash += ScaleQuantumToChar(QuantumRange - pixel->opacity);
858
822
  #endif
823
+ hash ^= ScaleQuantumToChar(pixel->black) << 1;
859
824
 
860
825
  return UINT2NUM(hash >> 1);
861
826
  }
@@ -874,6 +839,7 @@ Pixel_init_copy(VALUE self, VALUE orig)
874
839
  {
875
840
  Pixel *copy, *original;
876
841
 
842
+ rb_check_frozen(self);
877
843
  TypedData_Get_Struct(orig, Pixel, &rm_pixel_data_type, original);
878
844
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, copy);
879
845
 
@@ -890,7 +856,7 @@ Pixel_init_copy(VALUE self, VALUE orig)
890
856
  * @param red [Numeric] The red value
891
857
  * @param green [Numeric] The green value
892
858
  * @param blue [Numeric] The blue value
893
- * @param opacity [Numeric] The opacity value
859
+ * @param opacity [Numeric] The opacity value. The alpha is set to QuantumRange minus this value.
894
860
  * @return [Magick::Pixel] self
895
861
  */
896
862
  VALUE
@@ -898,6 +864,7 @@ Pixel_initialize(int argc, VALUE *argv, VALUE self)
898
864
  {
899
865
  Pixel *pixel;
900
866
 
867
+ rb_check_frozen(self);
901
868
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
902
869
 
903
870
  #if defined(IMAGEMAGICK_7)
@@ -910,7 +877,7 @@ Pixel_initialize(int argc, VALUE *argv, VALUE self)
910
877
  #if defined(IMAGEMAGICK_7)
911
878
  if (argv[3] != Qnil)
912
879
  {
913
- pixel->alpha = APP2QUANTUM(argv[3]);
880
+ pixel->alpha = QuantumRange - APP2QUANTUM(argv[3]);
914
881
  }
915
882
  #else
916
883
  if (argv[3] != Qnil)
@@ -952,15 +919,13 @@ VALUE
952
919
  Pixel_intensity(VALUE self)
953
920
  {
954
921
  Pixel *pixel;
955
- Quantum intensity;
922
+ double intensity;
956
923
 
957
924
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
958
925
 
959
- intensity = ROUND_TO_QUANTUM((0.299*pixel->red)
960
- + (0.587*pixel->green)
961
- + (0.114*pixel->blue));
926
+ intensity = (0.299*pixel->red) + (0.587*pixel->green) + (0.114*pixel->blue);
962
927
 
963
- return QUANTUM2NUM((unsigned long) intensity);
928
+ return QUANTUM2NUM(floor(intensity + 0.5));
964
929
  }
965
930
 
966
931
 
@@ -985,6 +950,7 @@ Pixel_marshal_dump(VALUE self)
985
950
  #else
986
951
  rb_hash_aset(dpixel, CSTR2SYM("opacity"), QUANTUM2NUM(pixel->opacity));
987
952
  #endif
953
+ rb_hash_aset(dpixel, CSTR2SYM("black"), QUANTUM2NUM(pixel->black));
988
954
 
989
955
  RB_GC_GUARD(dpixel);
990
956
 
@@ -1001,24 +967,57 @@ Pixel_marshal_dump(VALUE self)
1001
967
  VALUE
1002
968
  Pixel_marshal_load(VALUE self, VALUE dpixel)
1003
969
  {
1004
- Pixel *pixel;
970
+ Pixel *pixel, loaded;
1005
971
 
1006
- TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
972
+ rb_check_frozen(self);
1007
973
 
1008
974
  Check_Type(dpixel, T_HASH);
1009
975
 
1010
- pixel->red = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("red")));
1011
- pixel->green = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("green")));
1012
- pixel->blue = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("blue")));
976
+ loaded.red = APP2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("red")));
977
+ loaded.green = APP2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("green")));
978
+ loaded.blue = APP2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("blue")));
1013
979
  #if defined(IMAGEMAGICK_7)
1014
- pixel->alpha = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("alpha")));
980
+ loaded.alpha = APP2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("alpha")));
1015
981
  #else
1016
- pixel->opacity = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("opacity")));
982
+ loaded.opacity = APP2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("opacity")));
1017
983
  #endif
984
+ loaded.black = APP2QUANTUM(rb_hash_lookup2(dpixel, CSTR2SYM("black"), INT2FIX(0)));
985
+
986
+ rb_check_frozen(self);
987
+ TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
988
+ *pixel = loaded;
1018
989
  return self;
1019
990
  }
1020
991
 
1021
992
 
993
+ /**
994
+ * Compare two channel values as Float#<=> does.
995
+ *
996
+ * No Ruby usage (internal function)
997
+ *
998
+ * @param a the first value
999
+ * @param b the second value
1000
+ * @return -1, 0 or 1, or nil when either is NaN
1001
+ */
1002
+ static VALUE
1003
+ compare_quantum(Quantum a, Quantum b)
1004
+ {
1005
+ if (a < b)
1006
+ {
1007
+ return INT2FIX(-1);
1008
+ }
1009
+ if (a > b)
1010
+ {
1011
+ return INT2FIX(1);
1012
+ }
1013
+ if (a == b)
1014
+ {
1015
+ return INT2FIX(0);
1016
+ }
1017
+ return Qnil;
1018
+ }
1019
+
1020
+
1022
1021
  /**
1023
1022
  * Support Comparable mixin.
1024
1023
  *
@@ -1029,40 +1028,41 @@ VALUE
1029
1028
  Pixel_spaceship(VALUE self, VALUE other)
1030
1029
  {
1031
1030
  Pixel *self_pixel, *other_pixel;
1031
+ const VALUE equal = INT2FIX(0);
1032
+ VALUE result;
1032
1033
 
1033
- if (CLASS_OF(self) != CLASS_OF(other)) {
1034
+ if (rb_obj_class(self) != rb_obj_class(other)) {
1034
1035
  return Qnil;
1035
1036
  }
1036
1037
 
1037
1038
  TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, self_pixel);
1038
1039
  TypedData_Get_Struct(other, Pixel, &rm_pixel_data_type, other_pixel);
1039
1040
 
1040
- if (self_pixel->red != other_pixel->red)
1041
+ result = compare_quantum(self_pixel->red, other_pixel->red);
1042
+ if (result != equal)
1041
1043
  {
1042
- return INT2NUM((self_pixel->red - other_pixel->red)/abs((int)(self_pixel->red - other_pixel->red)));
1044
+ return result;
1043
1045
  }
1044
- else if(self_pixel->green != other_pixel->green)
1046
+ result = compare_quantum(self_pixel->green, other_pixel->green);
1047
+ if (result != equal)
1045
1048
  {
1046
- return INT2NUM((self_pixel->green - other_pixel->green)/abs((int)(self_pixel->green - other_pixel->green)));
1049
+ return result;
1047
1050
  }
1048
- else if(self_pixel->blue != other_pixel->blue)
1051
+ result = compare_quantum(self_pixel->blue, other_pixel->blue);
1052
+ if (result != equal)
1049
1053
  {
1050
- return INT2NUM((self_pixel->blue - other_pixel->blue)/abs((int)(self_pixel->blue - other_pixel->blue)));
1054
+ return result;
1051
1055
  }
1052
1056
  #if defined(IMAGEMAGICK_7)
1053
- else if(self_pixel->alpha != other_pixel->alpha)
1054
- {
1055
- return INT2NUM((self_pixel->alpha - other_pixel->alpha)/abs((int)(self_pixel->alpha - other_pixel->alpha)));
1056
- }
1057
+ result = compare_quantum(self_pixel->alpha, other_pixel->alpha);
1057
1058
  #else
1058
- else if(self_pixel->opacity != other_pixel->opacity)
1059
+ result = compare_quantum(other_pixel->opacity, self_pixel->opacity);
1060
+ #endif
1061
+ if (result != equal)
1059
1062
  {
1060
- return INT2NUM(((QuantumRange - self_pixel->opacity) - (QuantumRange - other_pixel->opacity))/abs((int)((QuantumRange - self_pixel->opacity) - (QuantumRange - other_pixel->opacity))));
1063
+ return result;
1061
1064
  }
1062
- #endif
1063
-
1064
- // Values are equal.
1065
- return INT2NUM(0);
1065
+ return compare_quantum(self_pixel->black, other_pixel->black);
1066
1066
  }
1067
1067
 
1068
1068
 
@@ -1184,11 +1184,14 @@ Pixel_to_color(int argc, VALUE *argv, VALUE self)
1184
1184
  switch (depth)
1185
1185
  {
1186
1186
  case 8:
1187
- #if MAGICKCORE_QUANTUM_DEPTH == 16 || MAGICKCORE_QUANTUM_DEPTH == 32
1187
+ #if MAGICKCORE_QUANTUM_DEPTH == 16 || MAGICKCORE_QUANTUM_DEPTH == 32 || MAGICKCORE_QUANTUM_DEPTH == 64
1188
1188
  case 16:
1189
1189
  #endif
1190
- #if MAGICKCORE_QUANTUM_DEPTH == 32
1190
+ #if MAGICKCORE_QUANTUM_DEPTH == 32 || MAGICKCORE_QUANTUM_DEPTH == 64
1191
1191
  case 32:
1192
+ #endif
1193
+ #if MAGICKCORE_QUANTUM_DEPTH == 64
1194
+ case 64:
1192
1195
  #endif
1193
1196
  break;
1194
1197
  default: