File: rmpixel.cpp

package info (click to toggle)
ruby-rmagick 6.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,232 kB
  • sloc: cpp: 19,563; ruby: 17,147; sh: 88; javascript: 36; makefile: 13
file content (1322 lines) | stat: -rw-r--r-- 32,378 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
/**************************************************************************//**
 * Contains Pixel class methods.
 *
 * Copyright © 2002 - 2009 by Timothy P. Hunter
 *
 * Changes since Nov. 2009 copyright © by Benjamin Thomas and Omer Bar-or
 *
 * @file     rmpixel.cpp
 * @version  $Id: rmpixel.cpp,v 1.7 2009/12/21 10:34:58 baror Exp $
 * @author   Tim Hunter
 ******************************************************************************/

#include "rmagick.h"

#if defined(IMAGEMAGICK_6)
    #define QueryColorname QueryMagickColorname
#endif


static VALUE color_arg_rescue(VALUE, VALUE ATTRIBUTE_UNUSED) ATTRIBUTE_NORETURN;
static void Color_Name_to_PixelColor(PixelColor *, VALUE);
static void Pixel_destroy(void *);
static size_t Pixel_memsize(const void *);

const rb_data_type_t rm_pixel_data_type = {
    "Magick::Pixel",
    { NULL, Pixel_destroy, Pixel_memsize, },
    0, 0,
    RUBY_TYPED_FROZEN_SHAREABLE,
};


/**
 * Free the storage associated with a Pixel object.
 *
 * No Ruby usage (internal function)
 *
 * @param ptr the Pixel object to destroy
 */
static void
Pixel_destroy(void *ptr)
{
    Pixel *pixel = (Pixel *)ptr;
    xfree(pixel);
}


/**
  * Get Pixel object size.
  *
  * No Ruby usage (internal function)
  *
  * @param ptr pointer to the Pixel object
  */
static size_t
Pixel_memsize(const void *ptr)
{
    return sizeof(Pixel);
}


/**
 * Get Pixel red value.
 *
 * @return [Numeric] the red value
 */
VALUE
Pixel_red(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, red, int, &rm_pixel_data_type);
}

/**
 * Get Pixel green value.
 *
 * @return [Numeric] the green value
 */
VALUE
Pixel_green(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, green, int, &rm_pixel_data_type);
}

/**
 * Get Pixel blue value.
 *
 * @return [Numeric] the blue value
 */
VALUE
Pixel_blue(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, blue, int, &rm_pixel_data_type);
}

/**
 * Get Pixel alpha value.
 *
 * @return [Numeric] the alpha value
 */
VALUE
Pixel_alpha(VALUE self)
{
    Pixel *pixel;
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
#if defined(IMAGEMAGICK_7)
    return C_int_to_R_int(pixel->alpha);
#else
    return C_int_to_R_int(QuantumRange - pixel->opacity);
#endif
}

/**
 * Set Pixel red value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the red value
 * @return [Numeric] the given red value
 */
VALUE
Pixel_red_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->red));
}

/**
 * Set Pixel green value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the green value
 * @return [Numeric] the given green value
 */
VALUE
Pixel_green_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->green = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->green));
}

/**
 * Set Pixel blue value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the blue value
 * @return [Numeric] the given blue value
 */
VALUE
Pixel_blue_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->blue = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->blue));
}

/**
 * Set Pixel alpha value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the alpha value
 * @return [Numeric] the given alpha value
 */
VALUE
Pixel_alpha_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
#if defined(IMAGEMAGICK_7)
    pixel->alpha = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->alpha);
#else
    pixel->opacity = QuantumRange - APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(QuantumRange - pixel->opacity);
#endif
}

/**
 * Get Pixel cyan value.
 *
 * @return [Numeric] the cyan value
 */
VALUE
Pixel_cyan(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->red);
}

/**
 * Set Pixel cyan value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the cyan value
 * @return [Numeric] the given cyan value
 */
VALUE
Pixel_cyan_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->red);
}

/**
 * Get Pixel magenta value.
 *
 * @return [Numeric] the magenta value
 */
VALUE
Pixel_magenta(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->green);
}

/**
 * Set Pixel magenta value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the magenta value
 * @return [Numeric] the given magenta value
 */
VALUE
Pixel_magenta_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->green = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->green);
}

/**
 * Get Pixel yellow value.
 *
 * @return [Numeric] the yellow value
 */
VALUE
Pixel_yellow(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->blue);
}

/**
 * Set Pixel yellow value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the yellow value
 * @return [Numeric] the given yellow value
 */
VALUE
Pixel_yellow_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->blue = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->blue);
}

/**
 * Get Pixel black value.
 *
 * @return [Numeric] the black value
 */
VALUE
Pixel_black(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->black);
}

/**
 * Set Pixel black value.
 *
 * - Pixel is Observable. Setters call {Magick::Pixel#changed},
 *   {Magick::Pixel#notify_observers}
 * - Setters return their argument values for backward compatibility to when
 *   Pixel was a Struct class.
 *
 * @param v [Numeric] the black value
 * @return [Numeric] the given black value
 */
VALUE
Pixel_black_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->black = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->black);
}


/**
 * Raise ArgumentError if the color name cannot be converted to a string via
 * rb_str_to_str.
 *
 * No Ruby usage (internal function)
 *
 * @param arg the argument to convert
 * @return 0
 * @throw ArgumentError
 */
static VALUE
color_arg_rescue(VALUE arg, VALUE raised_exc ATTRIBUTE_UNUSED)
{
    rb_raise(rb_eTypeError, "argument must be color name or pixel (%s given)",
            rb_class2name(CLASS_OF(arg)));
}


/**
 * Convert either a String color name or a Magick::Pixel to a PixelColor.
 *
 * No Ruby usage (internal function)
 *
 * @param pp the PixelColor to modify
 * @param color the color name or Magick::Pixel
 */
void
Color_to_PixelColor(PixelColor *pp, VALUE color)
{
    Pixel *pixel;

    // Allow color name or Pixel
    if (CLASS_OF(color) == Class_Pixel)
    {
        memset(pp, 0, sizeof(*pp));
        TypedData_Get_Struct(color, Pixel, &rm_pixel_data_type, pixel);
        pp->red     = pixel->red;
        pp->green   = pixel->green;
        pp->blue    = pixel->blue;
#if defined(IMAGEMAGICK_7)
        pp->black   = pixel->black;
        rm_set_pixelinfo_alpha(pp, pixel->alpha);
#else
        pp->opacity = pixel->opacity;
#endif
    }
    else
    {
        // require 'to_str' here instead of just 'to_s'.
        color = rb_rescue(RESCUE_FUNC(rb_str_to_str), color, RESCUE_EXCEPTION_HANDLER_FUNC(color_arg_rescue), color);
        Color_Name_to_PixelColor(pp, color);
    }
}


/**
 * Convert either a String color name or a {Magick::Pixel} to a Pixel.
 *
 * No Ruby usage (internal function)
 *
 * @param pp the Pixel to modify
 * @param color the color name or Magick::Pixel
 */
void
Color_to_Pixel(Pixel *pp, VALUE color)
{
    PixelColor pixel_color;

    memset(pp, 0, sizeof(*pp));
    // Allow color name or Pixel
    if (CLASS_OF(color) == Class_Pixel)
    {
        Pixel *pixel;

        TypedData_Get_Struct(color, Pixel, &rm_pixel_data_type, pixel);
        memcpy(pp, pixel, sizeof(Pixel));
    }
    else
    {
        Color_to_PixelColor(&pixel_color, color);
        pp->red   = pixel_color.red;
        pp->green = pixel_color.green;
        pp->blue  = pixel_color.blue;
#if defined(IMAGEMAGICK_7)
        pp->alpha = pixel_color.alpha;
        pp->black = pixel_color.black;
#else
        pp->opacity = pixel_color.opacity;
#endif
    }
}


/**
 * Convert a color name to a PixelColor
 *
 * No Ruby usage (internal function)
 *
 * @param color the PixelColor to modify
 * @param name_arg the coor name
 * @throw ArgumentError
 */
static void
Color_Name_to_PixelColor(PixelColor *color, VALUE name_arg)
{
    MagickBooleanType okay;
    char *name;
    ExceptionInfo *exception;

    exception = AcquireExceptionInfo();
    name = StringValueCStr(name_arg);
    okay = QueryColorCompliance(name, AllCompliance, color, exception);
    DestroyExceptionInfo(exception);
    if (!okay)
    {
        rb_raise(rb_eArgError, "invalid color name %s", name);
    }
}



/**
 * Allocate a Pixel object.
 *
 * @return [Magick::Pixel] a new Magick::Pixel object
 */
VALUE
Pixel_alloc(VALUE klass)
{
    Pixel *pixel;

    pixel = ALLOC(Pixel);
    memset(pixel, '\0', sizeof(Pixel));
    return TypedData_Wrap_Struct(klass, &rm_pixel_data_type, pixel);
}


/**
 * "Case equal" operator for Pixel.
 *
 * @param other [Object] the other object
 * @return [Boolean] true or false
 */

VALUE
Pixel_case_eq(VALUE self, VALUE other)
{
    if (CLASS_OF(self) == CLASS_OF(other))
    {
        Pixel *self_pixel, *other_pixel;

        TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, self_pixel);
        TypedData_Get_Struct(other, Pixel, &rm_pixel_data_type, other_pixel);
        return (self_pixel->red == other_pixel->red
            && self_pixel->blue == other_pixel->blue
            && self_pixel->green == other_pixel->green
#if defined(IMAGEMAGICK_7)
            && self_pixel->alpha == other_pixel->alpha) ? Qtrue : Qfalse;
#else
            && self_pixel->opacity == other_pixel->opacity) ? Qtrue : Qfalse;
#endif
    }

    return Qfalse;
}


/**
 * Clone a Pixel.
 *
 * @return [Magick::Pixel] a clone object
 * @see #dup
 * @see #initialize_copy
 */
VALUE
Pixel_clone(VALUE self)
{
    VALUE clone;

    clone = Pixel_dup(self);
    if (OBJ_FROZEN(self))
    {
        OBJ_FREEZE(clone);
    }

    RB_GC_GUARD(clone);

    return clone;
}


/**
 * Duplicate a Pixel.
 *
 * @return [Magick::Pixel] a duplicated object
 * @see #clone
 * @see #initialize_copy
 */
VALUE
Pixel_dup(VALUE self)
{
    Pixel *pixel;
    VALUE dup;

    pixel = ALLOC(Pixel);
    memset(pixel, '\0', sizeof(Pixel));
    dup = TypedData_Wrap_Struct(CLASS_OF(self), &rm_pixel_data_type, pixel);
    RB_GC_GUARD(dup);

    return rb_funcall(dup, rm_ID_initialize_copy, 1, self);
}


/**
 * Equality. Returns true only if receiver and other are the same object.
 *
 * @param other [Object] the other object
 * @return [Boolean] true if other is the same value, otherwise false
 */
VALUE
Pixel_eql_q(VALUE self, VALUE other)
{
    return NUM2INT(Pixel_spaceship(self, other)) == 0 ? Qtrue : Qfalse;
}


/**
 * Compare pixel values for equality.
 *
 * @overload fcmp(other, fuzz = 0.0, colorspace = Magick::RGBColorspace)
 *   @param other [Magick::Pixel, String] The pixel to which the receiver is compared
 *   @param fuzz [Numeric] The amount of fuzz to allow before the colors are considered to be different
 *   @param colorspace [Magick::ColorspaceType] The colorspace
 *   @return [Boolean] true if equal, otherwise false
 */
VALUE
Pixel_fcmp(int argc, VALUE *argv, VALUE self)
{
    double fuzz = 0.0;
    unsigned int equal;
    ColorspaceType colorspace = RGBColorspace;
    PixelColor self_pixel_color, other_pixel_color;
#if defined(IMAGEMAGICK_6)
    Image *image;
    Info *info;
#endif

    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;
    }

    Color_to_PixelColor(&self_pixel_color, self);
    Color_to_PixelColor(&other_pixel_color, argv[0]);

#if defined(IMAGEMAGICK_7)
    self_pixel_color.fuzz = fuzz;
    self_pixel_color.colorspace = colorspace;
    other_pixel_color.fuzz = fuzz;
    other_pixel_color.colorspace = colorspace;
    equal = IsFuzzyEquivalencePixelInfo(&self_pixel_color, &other_pixel_color);
#else
    // The IsColorSimilar 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 = rm_acquire_image(info);

    // Delete Info now in case we have to raise an exception
    DestroyImageInfo(info);

    if (!image)
    {
        rb_raise(rb_eNoMemError, "not enough memory to continue");
    }

    image->colorspace = colorspace;
    image->fuzz = fuzz;

    equal = IsColorSimilar(image, &self_pixel_color, &other_pixel_color);
    DestroyImage(image);
#endif

    return equal ? Qtrue : Qfalse;
}


/**
 * Construct an {Magick::Pixel} corresponding to the given color name.
 *
 * - 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.
 *
 * @param name [String] the color name
 * @return [Magick::Pixel] a new Magic::Pixel object
 * @see Magick::Image#to_color
 * @see Magick::Pixel#to_color
 */
VALUE
Pixel_from_color(VALUE klass ATTRIBUTE_UNUSED, VALUE name)
{
    PixelColor pp;
    ExceptionInfo *exception;
    MagickBooleanType okay;

    exception = AcquireExceptionInfo();
    okay = QueryColorCompliance(StringValueCStr(name), AllCompliance, &pp, exception);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);

    if (!okay)
    {
        rb_raise(rb_eArgError, "invalid color name: %s", StringValueCStr(name));
    }

    return Pixel_from_PixelColor(&pp);
}


/**
 * Construct an RGB pixel.
 *
 * - 0 <= +hue+ < 360 OR "0%" <= +hue+ < "100%"
 * - 0 <= +saturation+ <= 255 OR "0%" <= +saturation+ <= "100%"
 * - 0 <= +lightness+ <= 255 OR "0%" <= +lightness+ <= "100%"
 * - 0 <= +alpha+ <= 1 (0 is transparent, 1 is opaque) OR "0%" <= +alpha+ <= "100%"
 *
 * @overload from_hsla(hue, saturation, lightness, alpha = 1.0)
 *   @param hue [Numeric, String] A value in the range.
 *   @param saturation [Numeric, String] A value in the range.
 *   @param lightness [Numeric, String] A value in the range.
 *   @param alpha [Numeric, String] The alpha value.
 *   @return [Magick::Pixel] a new Magick::Pixel object
 */
VALUE
Pixel_from_hsla(int argc, VALUE *argv, VALUE klass ATTRIBUTE_UNUSED)
{
    double h, s, l, a = 1.0;
    MagickPixel pp;
    ExceptionInfo *exception;
    char name[50];
    MagickBooleanType alpha = MagickFalse;

    switch (argc)
    {
        case 4:
            a = rm_percentage(argv[3], 1.0);
            alpha = MagickTrue;
        case 3:
            // saturation and lightness are out of 255 in new ImageMagicks and
            // out of 100 in old ImageMagicks. Compromise: always use %.
            l = rm_percentage(argv[2], 255.0);
            s = rm_percentage(argv[1], 255.0);
            h = rm_percentage(argv[0], 360.0);
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 3 or 4)", argc);
            break;
    }

    if (alpha && (a < 0.0 || a > 1.0))
    {
        rb_raise(rb_eRangeError, "alpha %g out of range [0.0, 1.0]", a);
    }
    if (l < 0.0 || l > 255.0)
    {
        rb_raise(rb_eRangeError, "lightness %g out of range [0.0, 255.0]", l);
    }
    if (s < 0.0 || s > 255.0)
    {
        rb_raise(rb_eRangeError, "saturation %g out of range [0.0, 255.0]", s);
    }
    if (h < 0.0 || h >= 360.0)
    {
        rb_raise(rb_eRangeError, "hue %g out of range [0.0, 360.0)", h);
    }

    memset(name, 0, sizeof(name));
    if (alpha)
    {
        snprintf(name, sizeof(name), "hsla(%-2.1f,%-2.1f,%-2.1f,%-2.1f)", h, s, l, a);
    }
    else
    {
        snprintf(name, sizeof(name), "hsl(%-2.1f,%-2.1f,%-2.1f)", h, s, l);
    }

    exception = AcquireExceptionInfo();

#if defined(IMAGEMAGICK_7)
    QueryColorCompliance(name, AllCompliance, &pp, exception);
#else
    QueryMagickColor(name, &pp, exception);
#endif
    CHECK_EXCEPTION();

    DestroyExceptionInfo(exception);

    return Pixel_from_MagickPixel(&pp);
}


/**
 * Create a Magick::Pixel object from a MagickPixel structure.
 *
 * No Ruby usage (internal function)
 *
 * Notes:
 *   - Bypasses normal Pixel.new, Pixel#initialize methods
 *
 * @param pp the MagickPixel
 * @return a new Magick::Pixel object
 */
VALUE
Pixel_from_MagickPixel(const MagickPixel *pp)
{
    Pixel *pixel;

    pixel          = ALLOC(Pixel);
    pixel->red     = pp->red;
    pixel->green   = pp->green;
    pixel->blue    = pp->blue;
#if defined(IMAGEMAGICK_7)
    pixel->alpha   = pp->alpha;
#else
    pixel->opacity = pp->opacity;
#endif
    pixel->black   = pp->index;

    return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
}


/**
 * Create a Magick::Pixel object from a PixelPacket structure.
 *
 * No Ruby usage (internal function)
 *
 * Notes:
 *   - Bypasses normal Pixel.new, Pixel#initialize methods
 *
 * @param pp the PixelPacket
 * @return a new Magick::Pixel object
 */
VALUE
Pixel_from_PixelPacket(const PixelPacket *pp)
{
    Pixel *pixel;

    pixel          = ALLOC(Pixel);
    pixel->red     = pp->red;
    pixel->green   = pp->green;
    pixel->blue    = pp->blue;
#if defined(IMAGEMAGICK_7)
    pixel->alpha   = pp->alpha;
    pixel->black   = pp->black;
#else
    pixel->opacity = pp->opacity;
    pixel->black   = 0;
#endif

    return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
}


/**
 * Create a Magick::Pixel object from a PixelColor structure.
 *
 * No Ruby usage (internal function)
 *
 * Notes:
 *   - Bypasses normal Pixel.new, Pixel#initialize methods
 *
 * @param pp the PixelColor
 * @return a new Magick::Pixel object
 */
VALUE
Pixel_from_PixelColor(const PixelColor *pp)
{
    Pixel *pixel;

    pixel          = ALLOC(Pixel);
    pixel->red     = pp->red;
    pixel->green   = pp->green;
    pixel->blue    = pp->blue;
#if defined(IMAGEMAGICK_7)
    pixel->alpha   = pp->alpha;
    pixel->black   = pp->black;
#else
    pixel->opacity = pp->opacity;
    pixel->black   = 0;
#endif

    return TypedData_Wrap_Struct(Class_Pixel, &rm_pixel_data_type, pixel);
}


/**
 * Compute a hash-code.
 *
 * @return [Numeric] the hash of self
 */
VALUE
Pixel_hash(VALUE self)
{
    Pixel *pixel;
    unsigned int hash;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    hash  = ScaleQuantumToChar(pixel->red)   << 24;
    hash += ScaleQuantumToChar(pixel->green) << 16;
    hash += ScaleQuantumToChar(pixel->blue)  << 8;
#if defined(IMAGEMAGICK_7)
    hash += ScaleQuantumToChar(pixel->alpha);
#else
    hash += ScaleQuantumToChar(QuantumRange - pixel->opacity);
#endif

    return UINT2NUM(hash >> 1);
}


/**
 * Initialize clone, dup methods.
 *
 * @param orig [Magick::Pixel] the original Pixel
 * @return [Magick::Pixel] self
 * @see #clone
 * @see #dup
 */
VALUE
Pixel_init_copy(VALUE self, VALUE orig)
{
    Pixel *copy, *original;

    TypedData_Get_Struct(orig, Pixel, &rm_pixel_data_type, original);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, copy);

    *copy = *original;

    return self;
}


/**
 * Initialize Pixel object.
 *
 * @overload initialize(red = 0, green = 0, blue = 0, opacity = 0)
 *   @param red [Numeric] The red value
 *   @param green [Numeric] The green value
 *   @param blue [Numeric] The blue value
 *   @param opacity [Numeric] The opacity value
 *   @return [Magick::Pixel] self
 */
VALUE
Pixel_initialize(int argc, VALUE *argv, VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

#if defined(IMAGEMAGICK_7)
    pixel->alpha = OpaqueAlpha;
#endif

    switch(argc)
    {
        case 4:
#if defined(IMAGEMAGICK_7)
            if (argv[3] != Qnil)
            {
                pixel->alpha = APP2QUANTUM(argv[3]);
            }
#else
            if (argv[3] != Qnil)
            {
                pixel->opacity = APP2QUANTUM(argv[3]);
            }
#endif
        case 3:
            if (argv[2] != Qnil)
            {
                pixel->blue = APP2QUANTUM(argv[2]);
            }
        case 2:
            if (argv[1] != Qnil)
            {
                pixel->green = APP2QUANTUM(argv[1]);
            }
        case 1:
            if (argv[0] != Qnil)
            {
                pixel->red = APP2QUANTUM(argv[0]);
            }
        case 0:
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 4)", argc);
    }

    return self;
}


/**
 * Return the "intensity" of a pixel.
 *
 * @return [Numeric] the intensity
 */
VALUE
Pixel_intensity(VALUE self)
{
    Pixel *pixel;
    Quantum intensity;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    intensity = ROUND_TO_QUANTUM((0.299*pixel->red)
                                + (0.587*pixel->green)
                                + (0.114*pixel->blue));

    return QUANTUM2NUM((unsigned long) intensity);
}


/**
 * Support Marshal.dump.
 *
 * @return [Hash] a representing the dumped pixel
 */
VALUE
Pixel_marshal_dump(VALUE self)
{
    Pixel *pixel;
    VALUE dpixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    dpixel = rb_hash_new();
    rb_hash_aset(dpixel, CSTR2SYM("red"), QUANTUM2NUM(pixel->red));
    rb_hash_aset(dpixel, CSTR2SYM("green"), QUANTUM2NUM(pixel->green));
    rb_hash_aset(dpixel, CSTR2SYM("blue"), QUANTUM2NUM(pixel->blue));
#if defined(IMAGEMAGICK_7)
    rb_hash_aset(dpixel, CSTR2SYM("alpha"), QUANTUM2NUM(pixel->alpha));
#else
    rb_hash_aset(dpixel, CSTR2SYM("opacity"), QUANTUM2NUM(pixel->opacity));
#endif

    RB_GC_GUARD(dpixel);

    return dpixel;
}


/**
 * Support Marshal.load.
 *
 * @param dpixel [Hash] the dumped pixel
 * @return self
 */
VALUE
Pixel_marshal_load(VALUE self, VALUE dpixel)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("red")));
    pixel->green = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("green")));
    pixel->blue = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("blue")));
#if defined(IMAGEMAGICK_7)
    pixel->alpha = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("alpha")));
#else
    pixel->opacity = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("opacity")));
#endif
    return self;
}


/**
 * Support Comparable mixin.
 *
 * @param other [Object] the other Pixel
 * @return [-1, 0, 1, nil] the result of compare
 */
VALUE
Pixel_spaceship(VALUE self, VALUE other)
{
    Pixel *self_pixel, *other_pixel;

    if (CLASS_OF(self) != CLASS_OF(other)) {
        return Qnil;
    }

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, self_pixel);
    TypedData_Get_Struct(other, Pixel, &rm_pixel_data_type, other_pixel);

    if (self_pixel->red != other_pixel->red)
    {
        return INT2NUM((self_pixel->red - other_pixel->red)/abs((int)(self_pixel->red - other_pixel->red)));
    }
    else if(self_pixel->green != other_pixel->green)
    {
        return INT2NUM((self_pixel->green - other_pixel->green)/abs((int)(self_pixel->green - other_pixel->green)));
    }
    else if(self_pixel->blue != other_pixel->blue)
    {
        return INT2NUM((self_pixel->blue - other_pixel->blue)/abs((int)(self_pixel->blue - other_pixel->blue)));
    }
#if defined(IMAGEMAGICK_7)
    else if(self_pixel->alpha != other_pixel->alpha)
    {
        return INT2NUM((self_pixel->alpha - other_pixel->alpha)/abs((int)(self_pixel->alpha - other_pixel->alpha)));
    }
#else
    else if(self_pixel->opacity != other_pixel->opacity)
    {
        return INT2NUM(((QuantumRange - self_pixel->opacity) - (QuantumRange - other_pixel->opacity))/abs((int)((QuantumRange - self_pixel->opacity) - (QuantumRange - other_pixel->opacity))));
    }
#endif

    // Values are equal.
    return INT2NUM(0);
}


/**
 * Return [+hue+, +saturation+, +lightness+, +alpha+] in the same ranges as
 * {Magick::Pixel.from_hsla}.
 *
 * @return [Array<Float>] an array with hsla data
 * @see Pixel.from_hsla
 */
VALUE
Pixel_to_hsla(VALUE self)
{
    double hue, sat, lum, alpha;
    Pixel *pixel;
    VALUE hsla;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    ConvertRGBToHSL(pixel->red, pixel->green, pixel->blue, &hue, &sat, &lum);
    hue *= 360.0;
    sat *= 255.0;
    lum *= 255.0;

#if defined(IMAGEMAGICK_7)
    if (pixel->alpha == OpaqueAlpha)
    {
        alpha = 1.0;
    }
    else if (pixel->alpha == TransparentAlpha)
    {
        alpha = 0.0;
    }
    else
    {
        alpha = (double)(pixel->alpha) / (double)QuantumRange;
    }
#else
    if (pixel->opacity == OpaqueOpacity)
    {
        alpha = 1.0;
    }
    else if (pixel->opacity == TransparentOpacity)
    {
        alpha = 0.0;
    }
    else
    {
        alpha = (double)(QuantumRange - pixel->opacity) / (double)QuantumRange;
    }
#endif

    hsla = rb_ary_new3(4, rb_float_new(hue), rb_float_new(sat), rb_float_new(lum), rb_float_new(alpha));

    RB_GC_GUARD(hsla);

    return hsla;
}


/**
 * Convert a Pixel to a MagickPixel.
 *
 * No Ruby usage (internal function)
 *
 * Notes:
 *   - Same code as the private function SetMagickPixelPacket in ImageMagick.
 *
 * @param pixel the pixel
 * @param pp the MagickPixel to be modified
 */
static void
rm_set_magick_pixel_packet(Pixel *pixel, MagickPixel *pp)
{
    pp->red     = (MagickRealType) pixel->red;
    pp->green   = (MagickRealType) pixel->green;
    pp->blue    = (MagickRealType) pixel->blue;
#if defined(IMAGEMAGICK_7)
    pp->alpha   = (MagickRealType) pixel->alpha;
#else
    pp->opacity = (MagickRealType) pixel->opacity;
#endif
    pp->index   = (MagickRealType) 0.0;
}


/**
 * Return the color name corresponding to the pixel values.
 *
 * @overload to_color(compliance = Magick::AllCompliance, alpha = false, depth = Magick::MAGICKCORE_QUANTUM_DEPTH, hex = true)
 *   @param compliance [Magick::ComplianceType] A ComplianceType constant
 *   @param alpha [Boolean] If false, the pixel's alpha attribute is ignored
 *   @param depth [Numeric] An image depth
 *   @param hex [Boolean] If true, represent the color name in hex format
 *   @return [String] the color name as a String
 */
VALUE
Pixel_to_color(int argc, VALUE *argv, VALUE self)
{
    Info *info;
    Image *image;
    Pixel *pixel;
    MagickPixel mpp;
    MagickBooleanType hex = MagickTrue;
    char name[MaxTextExtent];
    ExceptionInfo *exception;
    ComplianceType compliance = AllCompliance;
    MagickBooleanType alpha = MagickFalse;
    unsigned int depth = MAGICKCORE_QUANTUM_DEPTH;

    switch (argc)
    {
        case 4:
            hex = (MagickBooleanType)RTEST(argv[3]);
        case 3:
            depth = NUM2UINT(argv[2]);

            // Ensure depth is appropriate for the way xMagick was compiled.
            switch (depth)
            {
                case 8:
#if MAGICKCORE_QUANTUM_DEPTH == 16 || MAGICKCORE_QUANTUM_DEPTH == 32
                case 16:
#endif
#if MAGICKCORE_QUANTUM_DEPTH == 32
                case 32:
#endif
                    break;
                default:
                    rb_raise(rb_eArgError, "invalid depth (%d)", depth);
                    break;
            }
       case 2:
            alpha = (MagickBooleanType)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);
    }

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    info = CloneImageInfo(NULL);
    image = rm_acquire_image(info);
    DestroyImageInfo(info);

    if (!image)
    {
        rb_raise(rb_eNoMemError, "not enough memory to continue.");
    }

    exception = AcquireExceptionInfo();

    image->depth = depth;
#if defined(IMAGEMAGICK_7)
    if (alpha)
    {
        image->alpha_trait = BlendPixelTrait;
    }
#else
    image->matte = alpha;
#endif

    rm_init_magickpixel(image, &mpp);
    rm_set_magick_pixel_packet(pixel, &mpp);

    // Support for hex-format color names moved out of QueryMagickColorname
    // in 6.4.1-9. The 'hex' argument was removed as well.
    if (hex)
    {
        if (compliance == XPMCompliance)
        {
#if defined(IMAGEMAGICK_7)
            mpp.alpha_trait = UndefinedPixelTrait;
#else
            mpp.matte = MagickFalse;
#endif
            mpp.depth = (unsigned long) min(1.0 * image->depth, 16.0);
        }
        GetColorTuple(&mpp, MagickTrue, name);
    }
    else
    {
        QueryColorname(image, &mpp, compliance, name, exception);
    }

    DestroyImage(image);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);

    // Always return a string, even if it's ""
    return rb_str_new2(name);
}


/**
 * Return a string representation of a {Magick::Pixel} object.
 *
 * @return [String] the string
 */
VALUE
Pixel_to_s(VALUE self)
{
    Pixel *pixel;
    char buff[100];

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    snprintf(buff, sizeof(buff), "red=" QuantumFormat ", green=" QuantumFormat ", blue=" QuantumFormat ", alpha=" QuantumFormat,
            pixel->red, pixel->green, pixel->blue,
#if defined(IMAGEMAGICK_7)
            pixel->alpha);
#else
            (QuantumRange - pixel->opacity));
#endif
    return rb_str_new2(buff);
}