/
rmutil.c
3403 lines (2912 loc) · 86.1 KB
/
rmutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: rmutil.c,v 1.83 2006/09/27 21:26:36 rmagick Exp $ */
/*============================================================================\
| Copyright (C) 2006 by Timothy P. Hunter
| Name: rmutil.c
| Author: Tim Hunter
| Purpose: Utility functions for RMagick
\============================================================================*/
#include "rmagick.h"
#include <errno.h>
static const char *ComplianceType_name(ComplianceType *);
static const char *StyleType_name(StyleType);
static const char *StretchType_name(StretchType);
static void Color_Name_to_PixelPacket(PixelPacket *, VALUE);
static VALUE Enum_type_values(VALUE);
static VALUE Enum_type_inspect(VALUE);
static void handle_exception(ExceptionInfo *, Image *, ErrorRetention);
/*
Extern: magick_malloc, magick_free, magick_realloc
Purpose: ****Magick versions of standard memory routines.
Notes: use when managing memory that ****Magick may have
allocated or may free.
If malloc fails, it raises an exception
*/
void *magick_malloc(const size_t size)
{
void *ptr;
#if defined(HAVE_ACQUIREMAGICKMEMORY)
ptr = AcquireMagickMemory(size);
#else
ptr = AcquireMemory(size);
#endif
if (!ptr)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return ptr;
}
void magick_free(void *ptr)
{
#if defined(HAVE_ACQUIREMAGICKMEMORY)
RelinquishMagickMemory(ptr);
#else
void *v = ptr;
LiberateMemory(&v);
#endif
}
void *magick_realloc(void *ptr, const size_t size)
{
void *v;
#if defined(HAVE_ACQUIREMAGICKMEMORY)
v = ResizeMagickMemory(ptr, size);
#else
v = ptr;
ReacquireMemory(&v, size);
#endif
if (!v)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return v;
}
/*
Extern: magick_clone_string
Purpose: make a copy of a string in malloc'd memory
Notes: Any existing string pointed to by *new_str is freed.
CloneString asserts if no memory. No need to check
its return value.
*/
void magick_clone_string(char **new_str, const char *str)
{
CloneString(new_str, str);
}
/*
Extern: rm_string_value_ptr(VALUE*)
Purpose: emulate Ruby 1.8's rb_string_value_ptr
Notes: This is essentially 1.8's rb_string_value_ptr
with a few minor changes to make it work in 1.6.
Always called via STRING_PTR
*/
#if !defined StringValuePtr
char *
rm_string_value_ptr(volatile VALUE *ptr)
{
volatile VALUE s = *ptr;
// If VALUE is not a string, call to_str on it
if (TYPE(s) != T_STRING)
{
s = rb_str_to_str(s);
*ptr = s;
}
// If ptr == NULL, allocate a 1 char array
if (!RSTRING(s)->ptr)
{
RSTRING(s)->ptr = ALLOC_N(char, 1);
(RSTRING(s)->ptr)[0] = 0;
RSTRING(s)->orig = 0;
}
return RSTRING(s)->ptr;
}
#endif
/*
Extern: rm_string_value_ptr_len
Purpose: safe replacement for rb_str2cstr
Returns: stores string length in 2nd arg, returns ptr to C string
Notes: Uses rb/rm_string_value_ptr to ensure correct String
argument.
Always called via STRING_PTR_LEN
*/
char *rm_string_value_ptr_len(volatile VALUE *ptr, long *len)
{
volatile VALUE v = *ptr;
char *str;
str = STRING_PTR(v);
*ptr = v;
*len = RSTRING(v)->len;
return str;
}
/*
* Extern: rm_strcasecmp(s1, s2)
* Purpose: compare s1 and s2 ignoring case
* Returns: same as strcmp(3)
*/
int
rm_strcasecmp(const char *s1, const char *s2)
{
while (*s1 && *s2)
{
if (toupper(*s1) != toupper(*s2))
{
break;
}
s1 += 1;
s2 += 1;
}
return (int)(*s1 - *s2);
}
/*
* Extern: rm_check_ary_len(ary, len)
* Purpose: raise exception if array too short
*/
void
rm_check_ary_len(VALUE ary, int len)
{
if (RARRAY(ary)->len < len)
{
rb_raise(rb_eIndexError, "not enough elements in array - expecting %d, got %d",
len, RARRAY(ary)->len);
}
}
/*
Extern: rm_check_frozen
Purpose: backport rb_check_frozen for 1.6.x releases
*/
void
rm_check_frozen(VALUE obj)
{
if (OBJ_FROZEN(obj))
{
rb_error_frozen(rb_class2name(CLASS_OF(obj)));
}
}
/*
Extern: rm_no_freeze(obj)
Purpose: overrides freeze in classes that can't be frozen.
*/
VALUE
rm_no_freeze(VALUE obj)
{
rb_raise(rb_eTypeError, "can't freeze %s", rb_class2name(CLASS_OF(obj)));
}
/*
* Static: arg_is_number
* Purpose: Try to convert the argument to a double,
* raise an exception if fail.
*/
static VALUE
arg_is_number(VALUE arg)
{
double d;
d = NUM2DBL(arg);
d = d; // satisfy icc
return arg;
}
/*
* Static: rescue_not_str
* Purpose: called when `rb_str_to_str' raised an exception below
*/
static VALUE
rescue_not_str(VALUE arg)
{
rb_raise(rb_eTypeError, "argument must be a number or a string in the form 'NN%' (%s given)",
rb_class2name(CLASS_OF(arg)));
}
/*
* Extern: rm_percentage(obj)
* Purpose: Return a double between 0.0 and 1.0, inclusive.
* If the argument is a number convert to a Float object,
* otherwise it's supposed to be a string in the form "NN%".
* Convert to a number and then to a Float.
*/
double
rm_percentage(VALUE arg)
{
double pct;
long pct_long;
char *pct_str, *end;
int not_num;
// Try to convert the argument to a number. If failure, sets not_num to non-zero.
rb_protect(arg_is_number, arg, ¬_num);
if (not_num)
{
arg = rb_rescue(rb_str_to_str, arg, rescue_not_str, arg);
pct_str = STRING_PTR(arg);
errno = 0;
pct_long = strtol(pct_str, &end, 10);
if (errno == ERANGE)
{
rb_raise(rb_eRangeError, "`%s' out of range", pct_str);
}
if (*end != '\0' && *end != '%')
{
rb_raise(rb_eArgError, "expected percentage, got `%s'", pct_str);
}
if (*end == '%' && pct_long != 0)
{
pct = ((double)pct_long) / 100.0;
}
else
{
pct = (double) pct_long;
}
if (pct < 0.0)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", pct_str);
}
}
else
{
pct = NUM2DBL(arg);
if (pct < 0.0)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%g')", pct);
}
}
return pct;
}
/*
Static: check_num2dbl
Purpose: return 0 if rb_num2dbl doesn't raise an exception
*/
static VALUE
check_num2dbl(VALUE obj)
{
rb_num2dbl(obj);
return INT2FIX(1);
}
/*
Static: rescue_not_dbl
Purpose: called if rb_num2dbl raises an exception
*/
static VALUE
rescue_not_dbl(VALUE ignored)
{
return INT2FIX(0);
}
/*
Extern: rm_check_num2dbl
Purpose: Return 1 if the object can be converted to a double, 0 otherwise.
*/
int rm_check_num2dbl(VALUE obj)
{
return FIX2INT(rb_rescue(check_num2dbl, obj, rescue_not_dbl, (VALUE)0));
}
/*
* Extern: rm_str_to_pct
* Purpose: Given a string in the form NN% return the corresponding double.
*
*/
double rm_str_to_pct(VALUE str)
{
long pct;
char *pct_str, *end;
str = rb_rescue(rb_str_to_str, str, rescue_not_str, str);
pct_str = STRING_PTR(str);
errno = 0;
pct = strtol(pct_str, &end, 10);
if (errno == ERANGE)
{
rb_raise(rb_eRangeError, "`%s' out of range", pct_str);
}
if (*end != '%')
{
rb_raise(rb_eArgError, "expected percentage, got `%s'", pct_str);
}
if (pct < 0L)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", pct_str);
}
return pct / 100.0;
}
/*
* Extern: rm_fuzz_to_dbl(obj)
* Purpose: If the argument is a number, convert it to a double.
* Otherwise it's supposed to be a string in the form 'NN%'.
* Return a percentage of MaxRGB.
* Notes: Called from Image#fuzz= and Info#fuzz=
*/
double
rm_fuzz_to_dbl(VALUE fuzz_arg)
{
double fuzz;
char *fuzz_str, *end;
int not_num;
// Try to convert the argument to a number. If failure, sets not_num to non-zero.
rb_protect(arg_is_number, fuzz_arg, ¬_num);
if (not_num)
{
// Convert to string, issue error message if failure.
fuzz_arg = rb_rescue(rb_str_to_str, fuzz_arg, rescue_not_str, fuzz_arg);
fuzz_str = STRING_PTR(fuzz_arg);
errno = 0;
fuzz = strtod(fuzz_str, &end);
if (errno == ERANGE)
{
rb_raise(rb_eRangeError, "`%s' out of range", fuzz_str);
}
if(*end == '%')
{
if (fuzz < 0.0)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", fuzz_arg);
}
fuzz = (fuzz * MaxRGB) / 100.0;
}
else if(*end != '\0')
{
rb_raise(rb_eArgError, "expected percentage, got `%s'", fuzz_str);
}
}
else
{
fuzz = NUM2DBL(fuzz_arg);
if (fuzz < 0.0)
{
rb_raise(rb_eArgError, "fuzz may not be negative (got `%g')", fuzz_arg);
}
}
return fuzz;
}
/*
Extern: ImageList_cur_image
Purpose: Sends the "cur_image" method to the object. If 'img'
is an ImageList, then cur_image is self[@scene].
If 'img' is an image, then cur_image is simply
'self'.
Returns: the return value from "cur_image"
*/
VALUE
ImageList_cur_image(VALUE img)
{
return rb_funcall(img, ID_cur_image, 0);
}
/*
Method: Magick::PrimaryInfo#to_s
Purpose: Create a string representation of a Magick::PrimaryInfo
*/
VALUE
PrimaryInfo_to_s(VALUE self)
{
PrimaryInfo pi;
char buff[100];
PrimaryInfo_to_PrimaryInfo(&pi, self);
sprintf(buff, "x=%g, y=%g, z=%g", pi.x, pi.y, pi.z);
return rb_str_new2(buff);
}
/*
Method: Magick::Chromaticity#to_s
Purpose: Create a string representation of a Magick::Chromaticity
*/
VALUE
ChromaticityInfo_to_s(VALUE self)
{
ChromaticityInfo ci;
char buff[200];
ChromaticityInfo_to_ChromaticityInfo(&ci, self);
sprintf(buff, "red_primary=(x=%g,y=%g) "
"green_primary=(x=%g,y=%g) "
"blue_primary=(x=%g,y=%g) "
"white_point=(x=%g,y=%g) ",
ci.red_primary.x, ci.red_primary.y,
ci.green_primary.x, ci.green_primary.y,
ci.blue_primary.x, ci.blue_primary.y,
ci.white_point.x, ci.white_point.y);
return rb_str_new2(buff);
}
/*
Method: Magick::Pixel#to_s
Purpose: Create a string representation of a Magick::Pixel
*/
VALUE
Pixel_to_s(VALUE self)
{
Pixel *pixel;
char buff[100];
Data_Get_Struct(self, Pixel, pixel);
sprintf(buff, "red=%d, green=%d, blue=%d, opacity=%d"
, pixel->red, pixel->green, pixel->blue, pixel->opacity);
return rb_str_new2(buff);
}
/*
Method: Magick::Pixel.from_color(string)
Purpose: Construct an Magick::Pixel corresponding to the given color name.
Notes: the "inverse" is Image *#to_color, b/c the conversion of a pixel
to a color name requires both a color depth and if the opacity
value has meaning (i.e. whether image->matte == True or not).
Also see Magick::Pixel#to_color, below.
*/
VALUE
Pixel_from_color(VALUE class, VALUE name)
{
PixelPacket pp;
ExceptionInfo exception;
MagickBooleanType okay;
class = class; // defeat "never referenced" message from icc
GetExceptionInfo(&exception);
okay = QueryColorDatabase(STRING_PTR(name), &pp, &exception);
CHECK_EXCEPTION()
DestroyExceptionInfo(&exception);
if (!okay)
{
rb_raise(rb_eArgError, "invalid color name: %s", STRING_PTR(name));
}
return Pixel_from_PixelPacket(&pp);
}
/*
Method: Magick::Pixel#to_color(compliance=Magick::???Compliance,
matte=False
depth=QuantumDepth)
Purpose: return the color name corresponding to the pixel values
Notes: the conversion respects the value of the 'opacity' field
in the Pixel.
*/
VALUE
Pixel_to_color(int argc, VALUE *argv, VALUE self)
{
Info *info;
Image *image;
Pixel *pixel;
char name[MaxTextExtent];
ExceptionInfo exception;
ComplianceType compliance = AllCompliance;
unsigned int matte = False;
unsigned int depth = QuantumDepth;
switch (argc)
{
case 3:
depth = NUM2UINT(argv[2]);
// Ensure depth is appropriate for the way xMagick was compiled.
switch (depth)
{
case 8:
#if QuantumDepth == 16 || QuantumDepth == 32
case 16:
#endif
#if QuantumDepth == 32
case 32:
#endif
break;
default:
rb_raise(rb_eArgError, "invalid depth (%d)", depth);
break;
}
case 2:
matte = RTEST(argv[1]);
case 1:
VALUE_TO_ENUM(argv[0], compliance, ComplianceType);
case 0:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 2)", argc);
}
Data_Get_Struct(self, Pixel, pixel);
info = CloneImageInfo(NULL);
image = AllocateImage(info);
image->depth = depth;
image->matte = matte;
DestroyImageInfo(info);
GetExceptionInfo(&exception);
(void) QueryColorname(image, pixel, compliance, name, &exception);
DestroyImage(image);
CHECK_EXCEPTION()
DestroyExceptionInfo(&exception);
// Always return a string, even if it's ""
return rb_str_new2(name);
}
/*
Method: Pixel#to_HSL
Purpose: Converts an RGB pixel to the array
[hue, saturation, luminosity].
*/
VALUE
Pixel_to_HSL(VALUE self)
{
Pixel *pixel;
double hue, saturation, luminosity;
volatile VALUE hsl;
Data_Get_Struct(self, Pixel, pixel);
TransformHSL(pixel->red, pixel->green, pixel->blue, &hue, &saturation, &luminosity);
hsl = rb_ary_new3(3, rb_float_new(hue), rb_float_new(saturation),
rb_float_new(luminosity));
return hsl;
}
/*
Method: Pixel.from_HSL
Purpose: Constructs an RGB pixel from the array
[hue, saturation, luminosity].
*/
VALUE
Pixel_from_HSL(VALUE class, VALUE hsl)
{
PixelPacket rgb = {0};
double hue, saturation, luminosity;
class = class; // defeat "never referenced" message from icc
hsl = rb_Array(hsl); // Ensure array
if (RARRAY(hsl)->len < 3)
{
rb_raise(rb_eArgError, "array argument must have at least 3 elements");
}
hue = NUM2DBL(rb_ary_entry(hsl, 0));
saturation = NUM2DBL(rb_ary_entry(hsl, 1));
luminosity = NUM2DBL(rb_ary_entry(hsl, 2));
HSLTransform(hue, saturation, luminosity,
&rgb.red, &rgb.green, &rgb.blue);
return Pixel_from_PixelPacket(&rgb);
}
/*
Method: Pixel#eql?
Purpose: For use with Hash
*/
VALUE
Pixel_eql_q(VALUE self, VALUE other)
{
return NUM2INT(Pixel_spaceship(self, other)) == 0 ? Qtrue : Qfalse;
}
/*
Method: Pixel#fcmp(other[, fuzz[, colorspace]])
Purpose: Compare pixel values for equality
*/
VALUE
Pixel_fcmp(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_FUZZYCOLORCOMPARE)
Image *image;
Info *info;
#endif
Pixel *this, *that;
ColorspaceType colorspace = RGBColorspace;
double fuzz = 0.0;
unsigned int equal;
switch (argc)
{
case 3:
VALUE_TO_ENUM(argv[2], colorspace, ColorspaceType);
case 2:
fuzz = NUM2DBL(argv[1]);
case 1:
// Allow 1 argument
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 to 3)", argc);
break;
}
Data_Get_Struct(self, Pixel, this);
Data_Get_Struct(argv[0], Pixel, that);
#if defined(HAVE_FUZZYCOLORCOMPARE) || defined(HAVE_ISCOLORSIMILAR)
// The FuzzyColorCompare function expects to get the
// colorspace and fuzz parameters from an Image structure.
info = CloneImageInfo(NULL);
if (!info)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
image = AllocateImage(info);
if (!image)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
DestroyImageInfo(info);
image->colorspace = colorspace;
image->fuzz = fuzz;
#if defined(HAVE_ISCOLORSIMILAR)
equal = IsColorSimilar(image, this, that);
#else
equal = FuzzyColorCompare(image, this, that);
#endif
DestroyImage(image);
#else
equal = FuzzyColorMatch(this, that, fuzz);
#endif
return equal ? Qtrue : Qfalse;
}
/*
Method: Pixel#hash
Notes: INT2FIX left-shifts 1 bit. Sacrifice 1 bit
from the opacity attribute to the FIXNUM_FLAG.
*/
VALUE
Pixel_hash(VALUE self)
{
Pixel *pixel;
unsigned int hash;
Data_Get_Struct(self, Pixel, pixel);
hash = ScaleQuantumToChar(pixel->red) << 24;
hash += ScaleQuantumToChar(pixel->green) << 16;
hash += ScaleQuantumToChar(pixel->blue) << 8;
hash += ScaleQuantumToChar(pixel->opacity);
hash >>= 1;
return INT2FIX(hash);
}
/*
Method: Pixel#intensity
Purpose: Return the "intensity" of a pixel
*/
VALUE
Pixel_intensity(VALUE self)
{
Pixel *pixel;
unsigned long intensity;
Data_Get_Struct(self, Pixel, pixel);
intensity = (unsigned long)
(0.299*pixel->red) + (0.587*pixel->green) + (0.114*pixel->blue);
return ULONG2NUM(intensity);
}
/*
Methods: Pixel RGBA attribute accessors
Purpose: Get/set Pixel attributes
Note: Pixel is Observable. Setters call changed, notify_observers
Note: Setters return their argument values for backward compatibility
to when Pixel was a Struct class.
*/
DEF_ATTR_READER(Pixel, red, int)
DEF_ATTR_READER(Pixel, green, int)
DEF_ATTR_READER(Pixel, blue, int)
DEF_ATTR_READER(Pixel, opacity, int)
DEF_PIXEL_CHANNEL_WRITER(red)
DEF_PIXEL_CHANNEL_WRITER(green)
DEF_PIXEL_CHANNEL_WRITER(blue)
DEF_PIXEL_CHANNEL_WRITER(opacity)
/*
Methods: Pixel CMYK attribute accessors
Purpose: Get/set Pixel attributes
Note: Pixel is Observable. Setters call changed, notify_observers
Note: Setters return their argument values for backward compatibility
to when Pixel was a Struct class.
*/
DEF_PIXEL_CMYK_CHANNEL_ACCESSOR(cyan, red)
DEF_PIXEL_CMYK_CHANNEL_ACCESSOR(magenta, green)
DEF_PIXEL_CMYK_CHANNEL_ACCESSOR(yellow, blue)
DEF_PIXEL_CMYK_CHANNEL_ACCESSOR(black, opacity)
/*
Method: Pixel#<=>
Purpose: Support Comparable mixin
*/
VALUE Pixel_spaceship(VALUE self, VALUE other)
{
Pixel *this, *that;
Data_Get_Struct(self, Pixel, this);
Data_Get_Struct(other, Pixel, that);
if (this->red != that->red)
{
return INT2NUM((this->red - that->red)/abs(this->red - that->red));
}
else if(this->green != that->green)
{
return INT2NUM((this->green - that->green)/abs(this->green - that->green));
}
else if(this->blue != that->blue)
{
return INT2NUM((this->blue - that->blue)/abs(this->blue - that->blue));
}
else if(this->opacity != that->opacity)
{
return INT2NUM((this->opacity - that->opacity)/abs(this->opacity - that->opacity));
}
// Values are equal, check class.
return rb_funcall(CLASS_OF(self), rb_intern("<=>"), 1, CLASS_OF(other));
}
/*
Static: destroy_Pixel
Purpose: Free the storage associated with a Pixel object
*/
static void
destroy_Pixel(Pixel *pixel)
{
xfree(pixel);
}
#if defined(HAVE_RB_DEFINE_ALLOC_FUNC)
/*
Extern: Pixel_alloc
Purpose: Allocate a Pixel object
*/
VALUE
Pixel_alloc(VALUE class)
{
Pixel *pixel;
pixel = ALLOC(Pixel);
memset(pixel, '\0', sizeof(Pixel));
return Data_Wrap_Struct(class, NULL, destroy_Pixel, pixel);
}
#else
/*
Method: Pixel.new
Purpose: Construct a new Pixel object
*/
VALUE
Pixel_new(int argc, VALUE *argv, VALUE class)
{
Pixel *pixel;
volatile VALUE pixel_obj;
pixel = ALLOC(Pixel);
memset(pixel, '\0', sizeof(Pixel));
pixel_obj = Data_Wrap_Struct(class, NULL, destroy_Pixel, pixel);
rb_obj_call_init(pixel_obj, argc, argv);
return pixel_obj;
}
#endif
/*
Method: Pixel#initialize(red=0,green=0,blue=0,opacity=0)
Notes: For backward compatibility, arguments may be nil.
*/
VALUE
Pixel_initialize(int argc, VALUE *argv, VALUE self)
{
Pixel *pixel;
Data_Get_Struct(self, Pixel, pixel);
switch(argc)
{
case 4:
if (argv[3] != Qnil)
{
pixel->opacity = (Quantum) NUM2UINT(argv[3]);
}
case 3:
if (argv[2] != Qnil)
{
pixel->blue = (Quantum) NUM2UINT(argv[2]);
}
case 2:
if (argv[1] != Qnil)
{
pixel->green = (Quantum) NUM2UINT(argv[1]);
}
case 1:
if (argv[0] != Qnil)
{
pixel->red = (Quantum) NUM2UINT(argv[0]);
}
case 0:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 4)", argc);
}
return self;
}
/*
Method: Pixel#===
Purpose: "Case equal" operator for Pixel
*/
VALUE Pixel_case_eq(VALUE self, VALUE other)
{
Pixel *this, *that;
if (CLASS_OF(self) == CLASS_OF(other))
{
Data_Get_Struct(self, Pixel, this);
Data_Get_Struct(other, Pixel, that);
return (this->red == that->red
&& this->blue == that->blue
&& this->green == that->green
&& this->opacity == that->opacity) ? Qtrue : Qfalse;
}
return Qfalse;
}
VALUE
Pixel_dup(VALUE self)
{
Pixel *pixel;
volatile VALUE dup;
pixel = ALLOC(Pixel);
memset(pixel, '\0', sizeof(Pixel));
dup = Data_Wrap_Struct(CLASS_OF(self), NULL, destroy_Pixel, pixel);
if (rb_obj_tainted(self))
{
rb_obj_taint(dup);
}
return rb_funcall(dup, ID_initialize_copy, 1, self);
}
/*
Method: Pixel#clone
Notes: see dup, init_copy
*/
VALUE
Pixel_clone(VALUE self)
{
volatile VALUE clone;
clone = Pixel_dup(self);
if (OBJ_FROZEN(self))
{
rb_obj_freeze(clone);
}
return clone;
}
/*
Method: Pixel#initialize_copy
Purpose: initialize clone, dup methods
*/
VALUE Pixel_init_copy(VALUE self, VALUE orig)
{
Pixel *copy, *original;
Data_Get_Struct(orig, Pixel, original);
Data_Get_Struct(self, Pixel, copy);
*copy = *original;
return self;
}
/*
Method: Magick::Rectangle#to_s
Purpose: Create a string representation of a Magick::Rectangle
*/
VALUE
RectangleInfo_to_s(VALUE self)
{
RectangleInfo rect;
char buff[100];
Rectangle_to_RectangleInfo(&rect, self);
sprintf(buff, "width=%lu, height=%lu, x=%ld, y=%ld"
, rect.width, rect.height, rect.x, rect.y);
return rb_str_new2(buff);
}
/*
Method: Magick::SegmentInfo#to_s
Purpose: Create a string representation of a Magick::Segment
*/
VALUE
SegmentInfo_to_s(VALUE self)
{
SegmentInfo segment;
char buff[100];
Segment_to_SegmentInfo(&segment, self);
sprintf(buff, "x1=%g, y1=%g, x2=%g, y2=%g"
, segment.x1, segment.y1, segment.x2, segment.y2);
return rb_str_new2(buff);
}