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.
@@ -12,6 +12,49 @@
12
12
  #include "rmagick.h"
13
13
 
14
14
 
15
+ /**
16
+ * Call #values on a Struct and make sure it returned an Array.
17
+ *
18
+ * No Ruby usage (internal function)
19
+ *
20
+ * @param st the Ruby Struct object
21
+ * @return the Array returned by st.values
22
+ * @throw TypeError if st.values does not return an Array
23
+ */
24
+ static VALUE
25
+ struct_values(VALUE st)
26
+ {
27
+ VALUE values = rb_funcall(st, rm_ID_values, 0);
28
+
29
+ if (!RB_TYPE_P(values, T_ARRAY))
30
+ {
31
+ rb_raise(rb_eTypeError, "%s#values must return an Array (%s given)",
32
+ rb_obj_classname(st), rb_obj_classname(values));
33
+ }
34
+ return values;
35
+ }
36
+
37
+
38
+ /**
39
+ * Raise TypeError unless the given object is an instance of the given Struct
40
+ * class or of one of its subclasses.
41
+ *
42
+ * No Ruby usage (internal function)
43
+ *
44
+ * @param obj the object to check
45
+ * @param klass the expected Struct class
46
+ * @throw TypeError if obj is not a klass
47
+ */
48
+ static void
49
+ check_struct_type(VALUE obj, VALUE klass)
50
+ {
51
+ if (!RTEST(rb_obj_is_kind_of(obj, klass)))
52
+ {
53
+ rb_raise(rb_eTypeError, "type mismatch: %s given", rb_obj_classname(obj));
54
+ }
55
+ }
56
+
57
+
15
58
  /**
16
59
  * Given a C AffineMatrix, create the equivalent AffineMatrix object.
17
60
  *
@@ -54,12 +97,8 @@ Export_AffineMatrix(AffineMatrix *am, VALUE st)
54
97
  {
55
98
  VALUE values, v;
56
99
 
57
- if (CLASS_OF(st) != Class_AffineMatrix)
58
- {
59
- rb_raise(rb_eTypeError, "type mismatch: %s given",
60
- rb_class2name(CLASS_OF(st)));
61
- }
62
- values = rb_funcall(st, rm_ID_values, 0);
100
+ check_struct_type(st, Class_AffineMatrix);
101
+ values = struct_values(st);
63
102
  v = rb_ary_entry(values, 0);
64
103
  am->sx = v == Qnil ? 1.0 : NUM2DBL(v);
65
104
  v = rb_ary_entry(values, 1);
@@ -89,15 +128,16 @@ Export_AffineMatrix(AffineMatrix *am, VALUE st)
89
128
  VALUE
90
129
  ChromaticityInfo_new(ChromaticityInfo *ci)
91
130
  {
131
+ ChromaticityInfo chromaticity = *ci;
92
132
  VALUE red_primary;
93
133
  VALUE green_primary;
94
134
  VALUE blue_primary;
95
135
  VALUE white_point;
96
136
 
97
- red_primary = Import_PrimaryInfo(&ci->red_primary);
98
- green_primary = Import_PrimaryInfo(&ci->green_primary);
99
- blue_primary = Import_PrimaryInfo(&ci->blue_primary);
100
- white_point = Import_PrimaryInfo(&ci->white_point);
137
+ red_primary = Import_PrimaryInfo(&chromaticity.red_primary);
138
+ green_primary = Import_PrimaryInfo(&chromaticity.green_primary);
139
+ blue_primary = Import_PrimaryInfo(&chromaticity.blue_primary);
140
+ white_point = Import_PrimaryInfo(&chromaticity.white_point);
101
141
 
102
142
  RB_GC_GUARD(red_primary);
103
143
  RB_GC_GUARD(green_primary);
@@ -125,21 +165,18 @@ Export_ChromaticityInfo(ChromaticityInfo *ci, VALUE chrom)
125
165
  VALUE red_primary, green_primary, blue_primary, white_point;
126
166
  VALUE entry_members, x, y;
127
167
 
128
- if (CLASS_OF(chrom) != Class_Chromaticity)
129
- {
130
- rb_raise(rb_eTypeError, "type mismatch: %s given",
131
- rb_class2name(CLASS_OF(chrom)));
132
- }
168
+ check_struct_type(chrom, Class_Chromaticity);
133
169
 
134
170
  // Get the struct members in an array
135
- chrom_members = rb_funcall(chrom, rm_ID_values, 0);
171
+ chrom_members = struct_values(chrom);
136
172
  red_primary = rb_ary_entry(chrom_members, 0);
137
173
  green_primary = rb_ary_entry(chrom_members, 1);
138
174
  blue_primary = rb_ary_entry(chrom_members, 2);
139
175
  white_point = rb_ary_entry(chrom_members, 3);
140
176
 
141
177
  // Get the red_primary PrimaryInfo members in an array
142
- entry_members = rb_funcall(red_primary, rm_ID_values, 0);
178
+ check_struct_type(red_primary, Class_Primary);
179
+ entry_members = struct_values(red_primary);
143
180
  x = rb_ary_entry(entry_members, 0); // red_primary.x
144
181
  ci->red_primary.x = x == Qnil ? 0.0 : NUM2DBL(x);
145
182
  y = rb_ary_entry(entry_members, 1); // red_primary.y
@@ -147,7 +184,8 @@ Export_ChromaticityInfo(ChromaticityInfo *ci, VALUE chrom)
147
184
  ci->red_primary.z = 0.0;
148
185
 
149
186
  // Get the green_primary PrimaryInfo members in an array
150
- entry_members = rb_funcall(green_primary, rm_ID_values, 0);
187
+ check_struct_type(green_primary, Class_Primary);
188
+ entry_members = struct_values(green_primary);
151
189
  x = rb_ary_entry(entry_members, 0); // green_primary.x
152
190
  ci->green_primary.x = x == Qnil ? 0.0 : NUM2DBL(x);
153
191
  y = rb_ary_entry(entry_members, 1); // green_primary.y
@@ -155,7 +193,8 @@ Export_ChromaticityInfo(ChromaticityInfo *ci, VALUE chrom)
155
193
  ci->green_primary.z = 0.0;
156
194
 
157
195
  // Get the blue_primary PrimaryInfo members in an array
158
- entry_members = rb_funcall(blue_primary, rm_ID_values, 0);
196
+ check_struct_type(blue_primary, Class_Primary);
197
+ entry_members = struct_values(blue_primary);
159
198
  x = rb_ary_entry(entry_members, 0); // blue_primary.x
160
199
  ci->blue_primary.x = x == Qnil ? 0.0 : NUM2DBL(x);
161
200
  y = rb_ary_entry(entry_members, 1); // blue_primary.y
@@ -163,7 +202,8 @@ Export_ChromaticityInfo(ChromaticityInfo *ci, VALUE chrom)
163
202
  ci->blue_primary.z = 0.0;
164
203
 
165
204
  // Get the white_point PrimaryInfo members in an array
166
- entry_members = rb_funcall(white_point, rm_ID_values, 0);
205
+ check_struct_type(white_point, Class_Primary);
206
+ entry_members = struct_values(white_point);
167
207
  x = rb_ary_entry(entry_members, 0); // white_point.x
168
208
  ci->white_point.x = x == Qnil ? 0.0 : NUM2DBL(x);
169
209
  y = rb_ary_entry(entry_members, 1); // white_point.y
@@ -222,7 +262,7 @@ Import_ColorInfo(const ColorInfo *ci)
222
262
  VALUE compliance;
223
263
  VALUE color;
224
264
 
225
- name = rb_str_new2(ci->name);
265
+ name = ci->name ? rb_str_new2(ci->name) : Qnil;
226
266
 
227
267
  compliance_type = ci->compliance;
228
268
  compliance = ComplianceType_find(compliance_type);
@@ -249,22 +289,18 @@ void
249
289
  Export_ColorInfo(ColorInfo *ci, VALUE st)
250
290
  {
251
291
  PixelColor pixel;
252
- VALUE members, m;
253
-
254
- if (CLASS_OF(st) != Class_Color)
255
- {
256
- rb_raise(rb_eTypeError, "type mismatch: %s given",
257
- rb_class2name(CLASS_OF(st)));
258
- }
292
+ VALUE members, m, name;
259
293
 
260
294
  memset(ci, '\0', sizeof(ColorInfo));
261
295
 
262
- members = rb_funcall(st, rm_ID_values, 0);
296
+ check_struct_type(st, Class_Color);
263
297
 
264
- m = rb_ary_entry(members, 0);
265
- if (m != Qnil)
298
+ members = struct_values(st);
299
+
300
+ name = rb_ary_entry(members, 0);
301
+ if (name != Qnil)
266
302
  {
267
- CloneString((char **)&(ci->name), StringValueCStr(m));
303
+ StringValueCStr(name);
268
304
  }
269
305
  m = rb_ary_entry(members, 1);
270
306
  if (m != Qnil)
@@ -289,8 +325,17 @@ Export_ColorInfo(ColorInfo *ci, VALUE st)
289
325
  ci->color.index = (MagickRealType) 0;
290
326
  }
291
327
 
328
+ // Copy the name last, so that nothing is left to free when a member
329
+ // above raises. The pointer is taken again because to_str or to_int above
330
+ // can change the String.
331
+ if (name != Qnil)
332
+ {
333
+ CloneString((char **)&(ci->name), StringValueCStr(name));
334
+ }
335
+
292
336
  RB_GC_GUARD(members);
293
337
  RB_GC_GUARD(m);
338
+ RB_GC_GUARD(name);
294
339
  }
295
340
 
296
341
 
@@ -309,19 +354,21 @@ Export_ColorInfo(ColorInfo *ci, VALUE st)
309
354
  void
310
355
  Color_to_MagickPixel(Image *image, MagickPixel *mpp, VALUE color)
311
356
  {
312
- PixelColor pp;
357
+ Pixel pixel;
313
358
 
314
359
  // image can be NULL
315
360
  rm_init_magickpixel(image, mpp);
316
361
 
317
- Color_to_PixelColor(&pp, color);
318
- mpp->red = (MagickRealType) pp.red;
319
- mpp->green = (MagickRealType) pp.green;
320
- mpp->blue = (MagickRealType) pp.blue;
362
+ Color_to_Pixel(&pixel, color);
363
+ mpp->red = (MagickRealType) pixel.red;
364
+ mpp->green = (MagickRealType) pixel.green;
365
+ mpp->blue = (MagickRealType) pixel.blue;
321
366
  #if defined(IMAGEMAGICK_7)
322
- mpp->alpha = (MagickRealType) pp.alpha;
367
+ mpp->alpha = (MagickRealType) pixel.alpha;
368
+ mpp->black = (MagickRealType) pixel.black;
323
369
  #else
324
- mpp->opacity = (MagickRealType) pp.opacity;
370
+ mpp->opacity = (MagickRealType) pixel.opacity;
371
+ mpp->index = (MagickRealType) pixel.black;
325
372
  #endif
326
373
  }
327
374
 
@@ -362,7 +409,7 @@ Color_to_s(VALUE self)
362
409
  #else
363
410
  "color.red=%g, color.green=%g, color.blue=%g, color.alpha=%g ",
364
411
  #endif
365
- ci.name,
412
+ ci.name ? ci.name : "",
366
413
  ComplianceType_name(&ci.compliance),
367
414
  ci.color.red, ci.color.green, ci.color.blue,
368
415
  #if defined(IMAGEMAGICK_7)
@@ -391,7 +438,7 @@ Import_TypeInfo(const TypeInfo *ti)
391
438
  VALUE encoding, foundry, format;
392
439
 
393
440
  name = rb_str_new2(ti->name);
394
- family = rb_str_new2(ti->family);
441
+ family = ti->family ? rb_str_new2(ti->family) : Qnil;
395
442
  style = StyleType_find(ti->style);
396
443
  stretch = StretchType_find(ti->stretch);
397
444
  weight = ULONG2NUM(ti->weight);
@@ -428,47 +475,69 @@ void
428
475
  Export_TypeInfo(TypeInfo *ti, VALUE st)
429
476
  {
430
477
  VALUE members, m;
478
+ VALUE strings[6];
479
+ // The members of Magick::Font that are copied as strings.
480
+ static const int string_members[6] = { 0, 1, 2, 6, 7, 8 };
481
+ char **fields[6] = {
482
+ (char **)&(ti->name), (char **)&(ti->description), (char **)&(ti->family),
483
+ (char **)&(ti->encoding), (char **)&(ti->foundry), (char **)&(ti->format)
484
+ };
485
+ long weight;
486
+ int i;
487
+
488
+ memset(ti, '\0', sizeof(TypeInfo));
431
489
 
432
- if (CLASS_OF(st) != Class_Font)
490
+ check_struct_type(st, Class_Font);
491
+
492
+ members = struct_values(st);
493
+
494
+ for (i = 0; i < 6; i++)
433
495
  {
434
- rb_raise(rb_eTypeError, "type mismatch: %s given",
435
- rb_class2name(CLASS_OF(st)));
496
+ strings[i] = rb_ary_entry(members, string_members[i]);
497
+ if (strings[i] != Qnil)
498
+ {
499
+ StringValueCStr(strings[i]);
500
+ }
436
501
  }
437
502
 
438
- memset(ti, '\0', sizeof(TypeInfo));
439
-
440
- members = rb_funcall(st, rm_ID_values, 0);
441
- m = rb_ary_entry(members, 0);
503
+ m = rb_ary_entry(members, 3);
442
504
  if (m != Qnil)
443
505
  {
444
- CloneString((char **)&(ti->name), StringValueCStr(m));
506
+ VALUE_TO_ENUM(m, ti->style, StyleType);
445
507
  }
446
- m = rb_ary_entry(members, 1);
508
+ m = rb_ary_entry(members, 4);
447
509
  if (m != Qnil)
448
510
  {
449
- CloneString((char **)&(ti->description), StringValueCStr(m));
511
+ VALUE_TO_ENUM(m, ti->stretch, StretchType);
450
512
  }
451
- m = rb_ary_entry(members, 2);
513
+ m = rb_ary_entry(members, 5);
452
514
  if (m != Qnil)
453
515
  {
454
- CloneString((char **)&(ti->family), StringValueCStr(m));
516
+ weight = NUM2LONG(m);
517
+ if (weight < 0)
518
+ {
519
+ rb_raise(rb_eArgError, "weight must be >= 0 (%ld given)", weight);
520
+ }
521
+ ti->weight = (size_t)weight;
455
522
  }
456
- m = rb_ary_entry(members, 3); ti->style = m == Qnil ? UndefinedStyle : (StyleType)FIX2INT(Enum_to_i(m));
457
- m = rb_ary_entry(members, 4); ti->stretch = m == Qnil ? UndefinedStretch : (StretchType)FIX2INT(Enum_to_i(m));
458
- m = rb_ary_entry(members, 5); ti->weight = m == Qnil ? 0 : FIX2INT(m);
459
523
 
460
- m = rb_ary_entry(members, 6);
461
- if (m != Qnil)
462
- CloneString((char **)&(ti->encoding), StringValueCStr(m));
463
- m = rb_ary_entry(members, 7);
464
- if (m != Qnil)
465
- CloneString((char **)&(ti->foundry), StringValueCStr(m));
466
- m = rb_ary_entry(members, 8);
467
- if (m != Qnil)
468
- CloneString((char **)&(ti->format), StringValueCStr(m));
524
+ // Copy the strings last, so that nothing is left to free when a member
525
+ // above raises. The pointers are taken again because to_str or to_int
526
+ // above can change the Strings.
527
+ for (i = 0; i < 6; i++)
528
+ {
529
+ if (strings[i] != Qnil)
530
+ {
531
+ CloneString(fields[i], StringValueCStr(strings[i]));
532
+ }
533
+ }
469
534
 
470
535
  RB_GC_GUARD(members);
471
536
  RB_GC_GUARD(m);
537
+ for (i = 0; i < 6; i++)
538
+ {
539
+ RB_GC_GUARD(strings[i]);
540
+ }
472
541
  }
473
542
 
474
543
 
@@ -529,9 +598,9 @@ Font_to_s(VALUE self)
529
598
  "name=%s, description=%s, "
530
599
  "family=%s, style=%s, stretch=%s, weight=%s, "
531
600
  "encoding=%s, foundry=%s, format=%s",
532
- ti.name,
533
- ti.description,
534
- ti.family,
601
+ ti.name ? ti.name : "",
602
+ ti.description ? ti.description : "",
603
+ ti.family ? ti.family : "",
535
604
  StyleType_name(ti.style),
536
605
  StretchType_name(ti.stretch),
537
606
  weight,
@@ -574,12 +643,8 @@ Export_PointInfo(PointInfo *pi, VALUE sp)
574
643
  {
575
644
  VALUE members, m;
576
645
 
577
- if (CLASS_OF(sp) != Class_Point)
578
- {
579
- rb_raise(rb_eTypeError, "type mismatch: %s given",
580
- rb_class2name(CLASS_OF(sp)));
581
- }
582
- members = rb_funcall(sp, rm_ID_values, 0);
646
+ check_struct_type(sp, Class_Point);
647
+ members = struct_values(sp);
583
648
  m = rb_ary_entry(members, 0);
584
649
  pi->x = m == Qnil ? 0.0 : NUM2DBL(m);
585
650
  m = rb_ary_entry(members, 1);
@@ -619,12 +684,8 @@ Export_PrimaryInfo(PrimaryInfo *pi, VALUE sp)
619
684
  {
620
685
  VALUE members, m;
621
686
 
622
- if (CLASS_OF(sp) != Class_Primary)
623
- {
624
- rb_raise(rb_eTypeError, "type mismatch: %s given",
625
- rb_class2name(CLASS_OF(sp)));
626
- }
627
- members = rb_funcall(sp, rm_ID_values, 0);
687
+ check_struct_type(sp, Class_Primary);
688
+ members = struct_values(sp);
628
689
  m = rb_ary_entry(members, 0);
629
690
  pi->x = m == Qnil ? 0.0 : NUM2DBL(m);
630
691
  m = rb_ary_entry(members, 1);
@@ -697,12 +758,8 @@ Export_RectangleInfo(RectangleInfo *rect, VALUE sr)
697
758
  {
698
759
  VALUE members, m;
699
760
 
700
- if (CLASS_OF(sr) != Class_Rectangle)
701
- {
702
- rb_raise(rb_eTypeError, "type mismatch: %s given",
703
- rb_class2name(CLASS_OF(sr)));
704
- }
705
- members = rb_funcall(sr, rm_ID_values, 0);
761
+ check_struct_type(sr, Class_Rectangle);
762
+ members = struct_values(sr);
706
763
  m = rb_ary_entry(members, 0);
707
764
  rect->width = m == Qnil ? 0 : NUM2ULONG(m);
708
765
  m = rb_ary_entry(members, 1);
@@ -775,13 +832,9 @@ Export_SegmentInfo(SegmentInfo *segment, VALUE s)
775
832
  {
776
833
  VALUE members, m;
777
834
 
778
- if (CLASS_OF(s) != Class_Segment)
779
- {
780
- rb_raise(rb_eTypeError, "type mismatch: %s given",
781
- rb_class2name(CLASS_OF(s)));
782
- }
835
+ check_struct_type(s, Class_Segment);
783
836
 
784
- members = rb_funcall(s, rm_ID_values, 0);
837
+ members = struct_values(s);
785
838
  m = rb_ary_entry(members, 0);
786
839
  segment->x1 = m == Qnil ? 0.0 : NUM2DBL(m);
787
840
  m = rb_ary_entry(members, 1);
@@ -871,12 +924,8 @@ Export_TypeMetric(TypeMetric *tm, VALUE st)
871
924
  VALUE members, m;
872
925
  VALUE pixels_per_em;
873
926
 
874
- if (CLASS_OF(st) != Class_TypeMetric)
875
- {
876
- rb_raise(rb_eTypeError, "type mismatch: %s given",
877
- rb_class2name(CLASS_OF(st)));
878
- }
879
- members = rb_funcall(st, rm_ID_values, 0);
927
+ check_struct_type(st, Class_TypeMetric);
928
+ members = struct_values(st);
880
929
 
881
930
  pixels_per_em = rb_ary_entry(members, 0);
882
931
  Export_PointInfo(&tm->pixels_per_em, pixels_per_em);