forked from Imagick/imagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagickpixel_class.c
834 lines (680 loc) Β· 21.8 KB
/
imagickpixel_class.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Imagick |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2013 Mikko Koppanen, Scott MacVicar |
| ImageMagick (c) ImageMagick Studio LLC |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <[email protected]> |
| Scott MacVicar <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_imagick.h"
#include "php_imagick_defs.h"
#include "php_imagick_macros.h"
#include "php_imagick_helpers.h"
#if MagickLibVersion > 0x628
/* {{{ proto array ImagickPixel::getHSL()
Returns the normalized HSL color of the pixel wand in an array with the keys "hue", "saturation", and "luminosity".
*/
PHP_METHOD(imagickpixel, gethsl)
{
php_imagickpixel_object *internp;
double hue, saturation, luminosity;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
PixelGetHSL(internp->pixel_wand, &hue, &saturation, &luminosity);
array_init(return_value);
add_assoc_double(return_value, "hue", hue);
add_assoc_double(return_value, "saturation", saturation);
add_assoc_double(return_value, "luminosity", luminosity);
return;
}
/* }}} */
/* {{{ proto bool ImagickPixel::setHSL(float hue, float saturation, float luminosity)
Sets the normalized HSL color of the pixel wand.
*/
PHP_METHOD(imagickpixel, sethsl)
{
php_imagickpixel_object *internp;
double hue, saturation, luminosity;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd", &hue, &saturation, &luminosity) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
PixelSetHSL(internp->pixel_wand, hue, saturation, luminosity);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto Quantum ImagickPixel::getColorValueQuantum(int color)
Gets the quantum value of a color in the ImagickPixel. Quantum is a float if ImageMagick was compiled with HDRI
otherwise an integer.
*/
PHP_METHOD(imagickpixel, getcolorvaluequantum)
{
php_imagickpixel_object *internp;
im_long color;
Quantum color_value;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
switch (color) {
case PHP_IMAGICK_COLOR_BLACK:
color_value = PixelGetBlackQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_BLUE:
color_value = PixelGetBlueQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_CYAN:
color_value = PixelGetCyanQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_GREEN:
color_value = PixelGetGreenQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_RED:
color_value = PixelGetRedQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_YELLOW:
color_value = PixelGetYellowQuantum(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_MAGENTA:
color_value = PixelGetMagentaQuantum(internp->pixel_wand);
break;
#if MagickLibVersion < 0x700
case PHP_IMAGICK_COLOR_OPACITY:
color_value = PixelGetOpacityQuantum(internp->pixel_wand);
break;
#endif
case PHP_IMAGICK_COLOR_ALPHA:
color_value = PixelGetAlphaQuantum(internp->pixel_wand);
break;
default:
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
break;
}
#if MAGICKCORE_HDRI_ENABLE
RETVAL_DOUBLE(color_value);
#else
RETVAL_LONG(color_value);
#endif
}
/* }}} */
/* {{{ proto bool ImagickPixel::setColorValueQuantum(int color, float value)
Sets the quantum color of the ImagickPixel.
*/
PHP_METHOD(imagickpixel, setcolorvaluequantum)
{
php_imagickpixel_object *internp;
im_long color;
#if MAGICKCORE_HDRI_ENABLE
double color_value;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &color, &color_value) == FAILURE) {
return;
}
#else
im_long color_value;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &color, &color_value) == FAILURE) {
return;
}
#endif
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
switch (color) {
case PHP_IMAGICK_COLOR_BLACK:
PixelSetBlackQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_BLUE:
PixelSetBlueQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_CYAN:
PixelSetCyanQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_GREEN:
PixelSetGreenQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_RED:
PixelSetRedQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_YELLOW:
PixelSetYellowQuantum(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_MAGENTA:
PixelSetMagentaQuantum(internp->pixel_wand, color_value);
break;
#if MagickLibVersion < 0x700
case PHP_IMAGICK_COLOR_OPACITY:
PixelSetOpacityQuantum(internp->pixel_wand, color_value);
break;
#endif
case PHP_IMAGICK_COLOR_ALPHA:
PixelSetAlphaQuantum(internp->pixel_wand, color_value);
break;
default:
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
break;
}
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixel::getIndex()
Gets the colormap index of the pixel wand
*/
PHP_METHOD(imagickpixel, getindex)
{
php_imagickpixel_object *internp;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
RETVAL_LONG(PixelGetIndex(internp->pixel_wand));
}
/* }}} */
/* {{{ proto bool ImagickPixel::setIndex(int pixel_packet)
Sets the colormap index of the pixel wand
*/
PHP_METHOD(imagickpixel, setindex)
{
php_imagickpixel_object *internp;
im_long index;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
PixelSetIndex(internp->pixel_wand, index);
RETURN_TRUE;
}
/* }}} */
#endif
/* {{{ proto ImagickPixel ImagickPixel::__construct([string color] )
The ImagickPixel constructor
*/
PHP_METHOD(imagickpixel, __construct)
{
php_imagickpixel_object *internp;
char *color_name = NULL;
IM_LEN_TYPE color_name_len = 0;
// This suppresses an 'unused parameter' warning.
(void)return_value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &color_name, &color_name_len) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (internp->pixel_wand != NULL) {
DestroyPixelWand(internp->pixel_wand);
}
internp->pixel_wand = NewPixelWand();
if (!internp->pixel_wand) {
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Failed to allocate PixelWand structure" TSRMLS_CC);
return;
}
/* If color was given as parameter, set it here.*/
if (color_name && color_name_len) {
if (PixelSetColor(internp->pixel_wand, color_name) == MagickFalse) {
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unable to construct ImagickPixel" TSRMLS_CC);
return;
}
}
}
/* }}} */
/* {{{ proto bool ImagickPixel::setColor(string color)
Sets the color of the pixel wand with a string (e.g. "blue", "#0000ff", "rgb(0,0,255)", "cmyk(100,100,100,10)", etc.).
*/
PHP_METHOD(imagickpixel, setcolor)
{
char *color_name;
IM_LEN_TYPE color_name_len;
php_imagickpixel_object *internp;
MagickBooleanType status;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &color_name, &color_name_len) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
status = PixelSetColor(internp->pixel_wand, color_name);
if (status == MagickFalse) {
php_imagick_convert_imagickpixel_exception (internp->pixel_wand, "Unable to set ImagickPixel color" TSRMLS_CC);
return;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixel::clear()
Clears resources associated with the wand.
*/
PHP_METHOD(imagickpixel, clear)
{
php_imagickpixel_object *internp;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
ClearPixelWand(internp->pixel_wand);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixel::isSimilar(float fuzz)
Returns true if the distance between two colors is less than the specified distance.
*/
static
void s_is_pixelwand_similar(INTERNAL_FUNCTION_PARAMETERS, zend_bool use_quantum)
{
zval *param;
double fuzz;
php_imagickpixel_object *internp;
MagickBooleanType status;
PixelWand *color_wand;
zend_bool allocated;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zd", ¶m, &fuzz) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
color_wand = php_imagick_zval_to_pixelwand (param, IMAGICKPIXEL_CLASS, &allocated TSRMLS_CC);
if (!color_wand)
return;
status = IsPixelWandSimilar(internp->pixel_wand, color_wand, (use_quantum ? (QuantumRange * fuzz) : fuzz));
if (allocated)
color_wand = DestroyPixelWand (color_wand);
if (status == MagickFalse) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* {{{ proto bool ImagickPixel::isPixelSimilarQuantum(ImagickPixel pixel, float fuzz)
Returns true if the distance between two colors is less than the specified distance.
The fuzz value should be in the range 0-QuantumRange.
The maximum value represents the longest possible distance in the colorspace.
e.g. from RGB(0, 0, 0) to RGB(255, 255, 255) for the RGB colorspace
*/
PHP_METHOD(imagickpixel, ispixelsimilarquantum)
{
s_is_pixelwand_similar (INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto bool ImagickPixel::isPixelSimilar(ImagickPixel pixel, float fuzz)
Returns true if the distance between two colors is less than the specified distance.
The fuzz value should be in the range 0-1.
The maximum value represents the longest possible distance in the colorspace.
e.g. from RGB(0, 0, 0) to RGB(255, 255, 255) for the RGB colorspace
*/
PHP_METHOD(imagickpixel, ispixelsimilar)
{
s_is_pixelwand_similar (INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto float ImagickPixel::getColorValue(int color)
Gets the normalized value of a color in the ImagickPixel.
*/
PHP_METHOD(imagickpixel, getcolorvalue)
{
php_imagick_color_t color_enum;
php_imagickpixel_object *internp;
im_long color;
double color_value = 0;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
if (color <= PHP_IMAGICK_COLOR_MIN || color >= PHP_IMAGICK_COLOR_MAX) {
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
}
color_enum = color;
switch (color_enum) {
case PHP_IMAGICK_COLOR_BLACK:
color_value = PixelGetBlack(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_BLUE:
color_value = PixelGetBlue(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_CYAN:
color_value = PixelGetCyan(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_GREEN:
color_value = PixelGetGreen(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_RED:
color_value = PixelGetRed(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_YELLOW:
color_value = PixelGetYellow(internp->pixel_wand);
break;
case PHP_IMAGICK_COLOR_MAGENTA:
color_value = PixelGetMagenta(internp->pixel_wand);
break;
#if MagickLibVersion < 0x700
case PHP_IMAGICK_COLOR_OPACITY:
color_value = PixelGetOpacity(internp->pixel_wand);
break;
#endif
case PHP_IMAGICK_COLOR_ALPHA:
color_value = PixelGetAlpha(internp->pixel_wand);
break;
#if MagickLibVersion > 0x628
case PHP_IMAGICK_COLOR_FUZZ:
color_value = PixelGetFuzz(internp->pixel_wand);
break;
#endif
default:
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
break;
}
RETVAL_DOUBLE(color_value);
}
/* }}} */
/* {{{ proto float ImagickPixel::setColorValue(int color, float value )
Sets the normalized color of the ImagickPixel.
*/
PHP_METHOD(imagickpixel, setcolorvalue)
{
php_imagick_color_t color_enum;
php_imagickpixel_object *internp;
im_long color;
double color_value;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &color, &color_value) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
if (color <= PHP_IMAGICK_COLOR_MIN || color >= PHP_IMAGICK_COLOR_MAX) {
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
}
color_enum = color;
switch (color_enum) {
case PHP_IMAGICK_COLOR_BLACK:
PixelSetBlack(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_BLUE:
PixelSetBlue(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_CYAN:
PixelSetCyan(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_GREEN:
PixelSetGreen(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_RED:
PixelSetRed(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_YELLOW:
PixelSetYellow(internp->pixel_wand, color_value);
break;
case PHP_IMAGICK_COLOR_MAGENTA:
PixelSetMagenta(internp->pixel_wand, color_value);
break;
#if MagickLibVersion < 0x700
case PHP_IMAGICK_COLOR_OPACITY:
PixelSetOpacity(internp->pixel_wand, color_value);
break;
#endif
case PHP_IMAGICK_COLOR_ALPHA:
PixelSetAlpha(internp->pixel_wand, color_value);
break;
#if MagickLibVersion > 0x628
case PHP_IMAGICK_COLOR_FUZZ:
PixelSetFuzz(internp->pixel_wand, color_value);
break;
#endif
default:
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
return;
break;
}
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto array ImagickPixel::getColor([int normalization])
Returns the color of the pixel in an array
normalization - 0 - values returned in the range 0,255 and will be ints, except
for legacy reasons alpha which is 0-1
normalization - 1 - values returned in the range 0,1 and will be floats
normalization - 2 - values returned in the range 0,255 and will be ints including alpha
values i.e. float if ImageMagick was compiled with HDRI, or integers normally.
*/
PHP_METHOD(imagickpixel, getcolor)
{
php_imagickpixel_object *internp;
im_long normalization = 0;
double red, green, blue, alpha;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &normalization) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
array_init(return_value);
red = PixelGetRed(internp->pixel_wand);
green = PixelGetGreen(internp->pixel_wand);
blue = PixelGetBlue(internp->pixel_wand);
alpha = PixelGetAlpha(internp->pixel_wand);
switch (normalization){
//values returned in the range 0,255 and will be ints
case(0): {
//Leave like this for legacy code
//TODO fix the alpha not being normalised at next major/minor verysion
red *= 255;
green *= 255;
blue *= 255;
//values are always >=0, so the rounding below may not be necessary
add_assoc_long(return_value, "r", (long) (red > 0.0 ? red + 0.5 : red - 0.5));
add_assoc_long(return_value, "g", (long) (green > 0.0 ? green + 0.5 : green - 0.5));
add_assoc_long(return_value, "b", (long) (blue > 0.0 ? blue + 0.5 : blue - 0.5));
add_assoc_long(return_value, "a", alpha);
break;
}
//values returned in the range 0,1 and will be floats
case(1): {
add_assoc_double(return_value, "r", red);
add_assoc_double(return_value, "g", green);
add_assoc_double(return_value, "b", blue);
add_assoc_double(return_value, "a", alpha);
break;
}
case(2): {
red *= 255;
green *= 255;
blue *= 255;
alpha *= 255;
//values are always >=0, so the rounding below may not be necessary
add_assoc_long(return_value, "r", (long) (red > 0.0 ? red + 0.5 : red - 0.5));
add_assoc_long(return_value, "g", (long) (green > 0.0 ? green + 0.5 : green - 0.5));
add_assoc_long(return_value, "b", (long) (blue > 0.0 ? blue + 0.5 : blue - 0.5));
add_assoc_long(return_value, "a", (long) (alpha > 0.0 ? alpha + 0.5 : alpha - 0.5));
break;
}
}
return;
}
/* }}} */
/* {{{ proto array ImagickPixel::getColorQuantum()
Returns the color of the pixel in an array as Quantum values. If ImageMagick was compiled
as HDRI these will be floats, otherwise they will be integers
*/
PHP_METHOD(imagickpixel, getcolorquantum)
{
php_imagickpixel_object *internp;
Quantum red, green, blue, alpha;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
array_init(return_value);
red = PixelGetRedQuantum(internp->pixel_wand);
green = PixelGetGreenQuantum(internp->pixel_wand);
blue = PixelGetBlueQuantum(internp->pixel_wand) ;
alpha = PixelGetAlphaQuantum(internp->pixel_wand);
#if MAGICKCORE_HDRI_ENABLE
add_assoc_double(return_value, "r", red);
add_assoc_double(return_value, "g", green);
add_assoc_double(return_value, "b", blue);
add_assoc_double(return_value, "a", alpha);
#else
add_assoc_long(return_value, "r", (long)red);
add_assoc_long(return_value, "g", (long)green);
add_assoc_long(return_value, "b", (long)blue);
add_assoc_long(return_value, "a", (long)alpha);
#endif
return;
}
/* }}} */
/* {{{ proto array ImagickPixel::getColorAsString(void )
Returns the color as a string
*/
PHP_METHOD(imagickpixel, getcolorasstring)
{
php_imagickpixel_object *internp;
char *color_string;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
color_string = PixelGetColorAsString(internp->pixel_wand);
IM_ZVAL_STRING(return_value, color_string);
IMAGICK_FREE_MAGICK_MEMORY(color_string);
return;
}
/* }}} */
/* {{{ proto ImagickPixel ImagickPixel::clone(void)
Clones the ImagickPixel
*/
PHP_METHOD(imagickpixel, clone)
{
php_imagickpixel_object *internp;
php_imagickpixel_object *intern_return;
PixelWand *pixel_wand;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
IMAGICK_METHOD_DEPRECATED("ImagickPixel", "clone");
internp = Z_IMAGICKPIXEL_P(getThis());
pixel_wand = php_imagick_clone_pixelwand (internp->pixel_wand);
if (!pixel_wand) {
php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Failed to allocate" TSRMLS_CC);
return;
}
object_init_ex(return_value, php_imagickpixel_sc_entry);
intern_return = Z_IMAGICKPIXEL_P(return_value);
php_imagick_replace_pixelwand(intern_return, pixel_wand);
return;
}
/* }}} */
/* {{{ proto int ImagickPixel::getColorCount()
Returns the color count associated with this color.
*/
PHP_METHOD(imagickpixel, getcolorcount)
{
php_imagickpixel_object *internp;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
RETVAL_LONG(PixelGetColorCount(internp->pixel_wand));
}
/* }}} */
/* {{{ proto int ImagickPixel::setColorCount(int colorCount)
Sets the color count associated with this color.
*/
PHP_METHOD(imagickpixel, setcolorcount)
{
php_imagickpixel_object *internp;
im_long color_count;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color_count) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
PixelSetColorCount(internp->pixel_wand, color_count);
RETVAL_TRUE;
}
/* }}} */
#if MagickLibVersion >= 0x693
/* {{{ proto bool ImagickPixel::setColorFromPixel(ImagickPixel $srcPixel)
Sets the color count associated with this color from another ImagickPixel object.
*/
PHP_METHOD(imagickpixel, setcolorfrompixel)
{
php_imagickpixel_object *internp, *src_pixel;
zval *objvar;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &objvar, php_imagickpixel_sc_entry) == FAILURE) {
return;
}
internp = Z_IMAGICKPIXEL_P(getThis());
if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
return;
}
src_pixel = Z_IMAGICKPIXEL_P(objvar);
if (php_imagickpixel_ensure_not_null(src_pixel->pixel_wand) == 0) {
return;
}
PixelSetColorFromWand(internp->pixel_wand, src_pixel->pixel_wand);
RETVAL_TRUE;
}
/* }}} */
#endif //#if MagickLibVersion >= 0x693