xref: /imagick/imagickpixel_class.c (revision f0650b3b)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5 / Imagick	                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2006-2013 Mikko Koppanen, Scott MacVicar               |
6    | ImageMagick (c) ImageMagick Studio LLC                               |
7    +----------------------------------------------------------------------+
8    | This source file is subject to version 3.01 of the PHP license,      |
9    | that is bundled with this package in the file LICENSE, and is        |
10    | available through the world-wide-web at the following url:           |
11    | http://www.php.net/license/3_01.txt                                  |
12    | If you did not receive a copy of the PHP license and are unable to   |
13    | obtain it through the world-wide-web, please send a note to          |
14    | license@php.net so we can mail you a copy immediately.               |
15    +----------------------------------------------------------------------+
16    | Author: Mikko Kopppanen <mkoppanen@php.net>                          |
17    |         Scott MacVicar <scottmac@php.net>                            |
18    +----------------------------------------------------------------------+
19 */
20 
21 #include "php_imagick.h"
22 #include "php_imagick_defs.h"
23 #include "php_imagick_macros.h"
24 #include "php_imagick_helpers.h"
25 
26 #if MagickLibVersion > 0x628
27 /* {{{ proto array ImagickPixel::getHSL()
28 	Returns the normalized HSL color of the pixel wand in an array with the keys "hue", "saturation", and "luminosity".
29 */
PHP_METHOD(ImagickPixel,getHSL)30 PHP_METHOD(ImagickPixel, getHSL)
31 {
32 	php_imagickpixel_object *internp;
33 	double hue, saturation, luminosity;
34 
35 	if (zend_parse_parameters_none() == FAILURE) {
36 		return;
37 	}
38 
39 	internp = Z_IMAGICKPIXEL_P(getThis());
40 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
41 		return;
42 	}
43 
44 	PixelGetHSL(internp->pixel_wand, &hue, &saturation, &luminosity);
45 
46 	array_init(return_value);
47 	add_assoc_double(return_value, "hue", hue);
48 	add_assoc_double(return_value, "saturation", saturation);
49 	add_assoc_double(return_value, "luminosity", luminosity);
50 	return;
51 }
52 /* }}} */
53 
54 /* {{{ proto bool ImagickPixel::setHSL(float hue, float saturation, float luminosity)
55 	Sets the normalized HSL color of the pixel wand.
56 */
PHP_METHOD(ImagickPixel,setHSL)57 PHP_METHOD(ImagickPixel, setHSL)
58 {
59 	php_imagickpixel_object *internp;
60 	double hue, saturation, luminosity;
61 
62 	/* Parse parameters given to function */
63 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd", &hue, &saturation, &luminosity) == FAILURE) {
64 		return;
65 	}
66 
67 	internp = Z_IMAGICKPIXEL_P(getThis());
68 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
69 		return;
70 	}
71 
72 	PixelSetHSL(internp->pixel_wand, hue, saturation, luminosity);
73 	RETURN_TRUE;
74 }
75 /* }}} */
76 
77 /* {{{ proto Quantum ImagickPixel::getColorValueQuantum(int color)
78 	Gets the quantum value of a color in the ImagickPixel. Quantum is a float if ImageMagick was compiled with HDRI
79 	otherwise an integer.
80 */
PHP_METHOD(ImagickPixel,getColorValueQuantum)81 PHP_METHOD(ImagickPixel, getColorValueQuantum)
82 {
83 	php_imagickpixel_object *internp;
84 	im_long color;
85 	Quantum color_value;
86 
87 	/* Parse parameters given to function */
88 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
89 		return;
90 	}
91 
92 	internp = Z_IMAGICKPIXEL_P(getThis());
93 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
94 		return;
95 	}
96 
97 	switch (color) {
98 
99 		case PHP_IMAGICK_COLOR_BLACK:
100 			color_value = PixelGetBlackQuantum(internp->pixel_wand);
101 		break;
102 
103 		case PHP_IMAGICK_COLOR_BLUE:
104 			color_value = PixelGetBlueQuantum(internp->pixel_wand);
105 		break;
106 
107 		case PHP_IMAGICK_COLOR_CYAN:
108 			color_value = PixelGetCyanQuantum(internp->pixel_wand);
109 		break;
110 
111 		case PHP_IMAGICK_COLOR_GREEN:
112 			color_value = PixelGetGreenQuantum(internp->pixel_wand);
113 		break;
114 
115 		case PHP_IMAGICK_COLOR_RED:
116 			color_value = PixelGetRedQuantum(internp->pixel_wand);
117 		break;
118 
119 		case PHP_IMAGICK_COLOR_YELLOW:
120 			color_value = PixelGetYellowQuantum(internp->pixel_wand);
121 		break;
122 
123 		case PHP_IMAGICK_COLOR_MAGENTA:
124 			color_value = PixelGetMagentaQuantum(internp->pixel_wand);
125 		break;
126 
127 #if MagickLibVersion < 0x700
128 		case PHP_IMAGICK_COLOR_OPACITY:
129 			color_value = PixelGetOpacityQuantum(internp->pixel_wand);
130 		break;
131 #endif
132 
133 		case PHP_IMAGICK_COLOR_ALPHA:
134 			color_value = PixelGetAlphaQuantum(internp->pixel_wand);
135 		break;
136 
137 		default:
138 			php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
139 			return;
140 		break;
141 	}
142 
143 #if MAGICKCORE_HDRI_ENABLE
144 	RETVAL_DOUBLE(color_value);
145 #else
146 	RETVAL_LONG(color_value);
147 #endif
148 }
149 /* }}} */
150 
151 /* {{{ proto bool ImagickPixel::setColorValueQuantum(int color, float value)
152 	Sets the quantum color of the ImagickPixel.
153 */
PHP_METHOD(ImagickPixel,setColorValueQuantum)154 PHP_METHOD(ImagickPixel, setColorValueQuantum)
155 {
156 	php_imagickpixel_object *internp;
157 	im_long color;
158 
159 #if MAGICKCORE_HDRI_ENABLE
160 	double color_value;
161 	/* Parse parameters given to function */
162 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &color, &color_value) == FAILURE) {
163 		return;
164 	}
165 #else
166 	im_long color_value;
167 	/* Parse parameters given to function */
168 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &color, &color_value) == FAILURE) {
169 		return;
170 	}
171 #endif
172 
173 	internp = Z_IMAGICKPIXEL_P(getThis());
174 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
175 		return;
176 	}
177 
178 	switch (color) {
179 
180 		case PHP_IMAGICK_COLOR_BLACK:
181 			PixelSetBlackQuantum(internp->pixel_wand, color_value);
182 		break;
183 
184 		case PHP_IMAGICK_COLOR_BLUE:
185 			PixelSetBlueQuantum(internp->pixel_wand, color_value);
186 		break;
187 
188 		case PHP_IMAGICK_COLOR_CYAN:
189 			PixelSetCyanQuantum(internp->pixel_wand, color_value);
190 		break;
191 
192 		case PHP_IMAGICK_COLOR_GREEN:
193 			PixelSetGreenQuantum(internp->pixel_wand, color_value);
194 		break;
195 
196 		case PHP_IMAGICK_COLOR_RED:
197 			PixelSetRedQuantum(internp->pixel_wand, color_value);
198 		break;
199 
200 		case PHP_IMAGICK_COLOR_YELLOW:
201 			PixelSetYellowQuantum(internp->pixel_wand, color_value);
202 		break;
203 
204 		case PHP_IMAGICK_COLOR_MAGENTA:
205 			PixelSetMagentaQuantum(internp->pixel_wand, color_value);
206 		break;
207 
208 #if MagickLibVersion < 0x700
209 		case PHP_IMAGICK_COLOR_OPACITY:
210 			PixelSetOpacityQuantum(internp->pixel_wand, color_value);
211 		break;
212 #endif
213 
214 		case PHP_IMAGICK_COLOR_ALPHA:
215 			PixelSetAlphaQuantum(internp->pixel_wand, color_value);
216 		break;
217 
218 		default:
219 			php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
220 			return;
221 		break;
222 	}
223 	RETVAL_TRUE;
224 }
225 /* }}} */
226 
227 /* {{{ proto bool ImagickPixel::getIndex()
228 	Gets the colormap index of the pixel wand
229 */
PHP_METHOD(ImagickPixel,getIndex)230 PHP_METHOD(ImagickPixel, getIndex)
231 {
232 	php_imagickpixel_object *internp;
233 
234 	if (zend_parse_parameters_none() == FAILURE) {
235 		return;
236 	}
237 
238 	internp = Z_IMAGICKPIXEL_P(getThis());
239 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
240 		return;
241 	}
242 
243 	RETVAL_LONG(PixelGetIndex(internp->pixel_wand));
244 }
245 /* }}} */
246 
247 /* {{{ proto bool ImagickPixel::setIndex(int pixel_packet)
248 	Sets the colormap index of the pixel wand
249 */
PHP_METHOD(ImagickPixel,setIndex)250 PHP_METHOD(ImagickPixel, setIndex)
251 {
252 	php_imagickpixel_object *internp;
253 #if MAGICKCORE_HDRI_ENABLE
254 	double index;
255 
256 	/* Parse parameters given to function */
257 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &index) == FAILURE) {
258 #else
259 	im_long index;
260 
261 	/* Parse parameters given to function */
262 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
263 #endif
264 		return;
265 	}
266 
267 	internp = Z_IMAGICKPIXEL_P(getThis());
268 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
269 		return;
270 	}
271 
272 	PixelSetIndex(internp->pixel_wand, index);
273 	RETURN_TRUE;
274 }
275 /* }}} */
276 #endif
277 
278 /* {{{ proto ImagickPixel ImagickPixel::__construct([string color] )
279    The ImagickPixel constructor
280 */
281 PHP_METHOD(ImagickPixel, __construct)
282 {
283 	php_imagickpixel_object *internp;
284 	char *color_name = NULL;
285 	IM_LEN_TYPE color_name_len = 0;
286 
287 	// This suppresses an 'unused parameter' warning.
288 	(void)return_value;
289 
290 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &color_name, &color_name_len) == FAILURE) {
291 		return;
292 	}
293 
294 	internp = Z_IMAGICKPIXEL_P(getThis());
295 
296 	if (internp->pixel_wand != NULL) {
297 		DestroyPixelWand(internp->pixel_wand);
298 	}
299 
300 	internp->pixel_wand = NewPixelWand();
301 
302 	if (!internp->pixel_wand) {
303 		php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Failed to allocate PixelWand structure" TSRMLS_CC);
304 		return;
305 	}
306 
307 	/* If color was given as parameter, set it here.*/
308 	if (color_name && color_name_len) {
309 		if (PixelSetColor(internp->pixel_wand, color_name) == MagickFalse) {
310 			php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unable to construct ImagickPixel" TSRMLS_CC);
311 			return;
312 		}
313 	}
314 }
315 /* }}} */
316 
317 /* {{{ proto bool ImagickPixel::setColor(string color)
318 	Sets the color of the pixel wand with a string (e.g. "blue", "#0000ff", "rgb(0,0,255)", "cmyk(100,100,100,10)", etc.).
319 */
320 PHP_METHOD(ImagickPixel, setColor)
321 {
322 	char *color_name;
323 	IM_LEN_TYPE color_name_len;
324 
325 	php_imagickpixel_object *internp;
326 	MagickBooleanType status;
327 
328 	/* Parse parameters given to function */
329 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &color_name, &color_name_len) == FAILURE) {
330 		return;
331 	}
332 
333 	internp = Z_IMAGICKPIXEL_P(getThis());
334 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
335 		return;
336 	}
337 
338 	status = PixelSetColor(internp->pixel_wand, color_name);
339 
340 	if (status == MagickFalse) {
341 		php_imagick_convert_imagickpixel_exception (internp->pixel_wand, "Unable to set ImagickPixel color" TSRMLS_CC);
342 		return;
343 	}
344 
345 	RETURN_TRUE;
346 }
347 /* }}} */
348 
349 /* {{{ proto bool ImagickPixel::clear()
350 	Clears resources associated with the wand.
351 */
352 PHP_METHOD(ImagickPixel, clear)
353 {
354 	php_imagickpixel_object *internp;
355 
356 	if (zend_parse_parameters_none() == FAILURE) {
357 		return;
358 	}
359 
360 	internp = Z_IMAGICKPIXEL_P(getThis());
361 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
362 		return;
363 	}
364 
365 	ClearPixelWand(internp->pixel_wand);
366 	RETURN_TRUE;
367 }
368 /* }}} */
369 
370 /* {{{ proto bool ImagickPixel::isSimilar(float fuzz)
371 	Returns true if the distance between two colors is less than the specified distance.
372 */
373 static
374 void s_is_pixelwand_similar(INTERNAL_FUNCTION_PARAMETERS, zend_bool use_quantum)
375 {
376 	zval *param;
377 	double fuzz;
378 	php_imagickpixel_object *internp;
379 	MagickBooleanType status;
380 	PixelWand *color_wand;
381 	zend_bool allocated;
382 
383 	/* Parse parameters given to function */
384 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zd", &param, &fuzz) == FAILURE) {
385 		return;
386 	}
387 
388 	internp = Z_IMAGICKPIXEL_P(getThis());
389 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
390 		return;
391 	}
392 
393 	color_wand = php_imagick_zval_to_pixelwand (param, IMAGICKPIXEL_CLASS, &allocated TSRMLS_CC);
394 	if (!color_wand)
395 		return;
396 
397 	status = IsPixelWandSimilar(internp->pixel_wand, color_wand, (use_quantum ? (QuantumRange * fuzz) : fuzz));
398 	if (allocated)
399 		color_wand = DestroyPixelWand (color_wand);
400 
401 	if (status == MagickFalse) {
402 		RETURN_FALSE;
403 	}
404 
405 	RETURN_TRUE;
406 }
407 
408 
409 /* {{{ proto bool ImagickPixel::isPixelSimilarQuantum(ImagickPixel pixel, float fuzz)
410 	Returns true if the distance between two colors is less than the specified distance.
411 	The fuzz value should be in the range 0-QuantumRange.
412 	The maximum value represents the longest possible distance in the colorspace.
413 	e.g. from RGB(0, 0, 0) to RGB(255, 255, 255) for the RGB colorspace
414 */
415 PHP_METHOD(ImagickPixel, isPixelSimilarQuantum)
416 {
417 	s_is_pixelwand_similar (INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
418 }
419 /* }}} */
420 
421 /* {{{ proto bool ImagickPixel::isPixelSimilar(ImagickPixel pixel, float fuzz)
422 	Returns true if the distance between two colors is less than the specified distance.
423 	The fuzz value should be in the range 0-1.
424 	The maximum value represents the longest possible distance in the colorspace.
425 	e.g. from RGB(0, 0, 0) to RGB(255, 255, 255) for the RGB colorspace
426 */
427 PHP_METHOD(ImagickPixel, isPixelSimilar)
428 {
429 	s_is_pixelwand_similar (INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
430 }
431 /* }}} */
432 
433 /* {{{ proto float ImagickPixel::getColorValue(int color)
434 	Gets the normalized value of a color in the ImagickPixel.
435 */
436 PHP_METHOD(ImagickPixel, getColorValue)
437 {
438 	php_imagick_color_t color_enum;
439 	php_imagickpixel_object *internp;
440 	im_long color;
441 	double color_value = 0;
442 
443 	/* Parse parameters given to function */
444 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
445 		return;
446 	}
447 
448 	internp = Z_IMAGICKPIXEL_P(getThis());
449 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
450 		return;
451 	}
452 
453 	if (color <= PHP_IMAGICK_COLOR_MIN || color >= PHP_IMAGICK_COLOR_MAX) {
454 		php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
455 		return;
456 	}
457 
458 	color_enum = color;
459 
460 	switch (color_enum) {
461 
462 		case PHP_IMAGICK_COLOR_BLACK:
463 			color_value = PixelGetBlack(internp->pixel_wand);
464 		break;
465 
466 		case PHP_IMAGICK_COLOR_BLUE:
467 			color_value = PixelGetBlue(internp->pixel_wand);
468 		break;
469 
470 		case PHP_IMAGICK_COLOR_CYAN:
471 			color_value = PixelGetCyan(internp->pixel_wand);
472 		break;
473 
474 		case PHP_IMAGICK_COLOR_GREEN:
475 			color_value = PixelGetGreen(internp->pixel_wand);
476 		break;
477 
478 		case PHP_IMAGICK_COLOR_RED:
479 			color_value = PixelGetRed(internp->pixel_wand);
480 		break;
481 
482 		case PHP_IMAGICK_COLOR_YELLOW:
483 			color_value = PixelGetYellow(internp->pixel_wand);
484 		break;
485 
486 		case PHP_IMAGICK_COLOR_MAGENTA:
487 			color_value = PixelGetMagenta(internp->pixel_wand);
488 		break;
489 
490 #if MagickLibVersion < 0x700
491 		case PHP_IMAGICK_COLOR_OPACITY:
492 			color_value = PixelGetOpacity(internp->pixel_wand);
493 		break;
494 #endif
495 
496 		case PHP_IMAGICK_COLOR_ALPHA:
497 			color_value = PixelGetAlpha(internp->pixel_wand);
498 		break;
499 
500 #if MagickLibVersion > 0x628
501 		case PHP_IMAGICK_COLOR_FUZZ:
502 			color_value = PixelGetFuzz(internp->pixel_wand);
503 		break;
504 #endif
505 
506 		default:
507 			php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
508 			return;
509 		break;
510 	}
511 	RETVAL_DOUBLE(color_value);
512 }
513 /* }}} */
514 
515 /* {{{ proto float ImagickPixel::setColorValue(int color, float value )
516 	Sets the normalized color of the ImagickPixel.
517 */
518 PHP_METHOD(ImagickPixel, setColorValue)
519 {
520 	php_imagick_color_t color_enum;
521 	php_imagickpixel_object *internp;
522 	im_long color;
523 	double color_value;
524 
525 	/* Parse parameters given to function */
526 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &color, &color_value) == FAILURE) {
527 		return;
528 	}
529 
530 	internp = Z_IMAGICKPIXEL_P(getThis());
531 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
532 		return;
533 	}
534 
535 	if (color <= PHP_IMAGICK_COLOR_MIN || color >= PHP_IMAGICK_COLOR_MAX) {
536 		php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
537 		return;
538 	}
539 
540 	color_enum = color;
541 
542 	switch (color_enum) {
543 
544 		case PHP_IMAGICK_COLOR_BLACK:
545 			PixelSetBlack(internp->pixel_wand, color_value);
546 		break;
547 
548 		case PHP_IMAGICK_COLOR_BLUE:
549 			PixelSetBlue(internp->pixel_wand, color_value);
550 		break;
551 
552 		case PHP_IMAGICK_COLOR_CYAN:
553 			PixelSetCyan(internp->pixel_wand, color_value);
554 		break;
555 
556 		case PHP_IMAGICK_COLOR_GREEN:
557 			PixelSetGreen(internp->pixel_wand, color_value);
558 		break;
559 
560 		case PHP_IMAGICK_COLOR_RED:
561 			PixelSetRed(internp->pixel_wand, color_value);
562 		break;
563 
564 		case PHP_IMAGICK_COLOR_YELLOW:
565 			PixelSetYellow(internp->pixel_wand, color_value);
566 		break;
567 
568 		case PHP_IMAGICK_COLOR_MAGENTA:
569 			PixelSetMagenta(internp->pixel_wand, color_value);
570 		break;
571 
572 #if MagickLibVersion < 0x700
573 		case PHP_IMAGICK_COLOR_OPACITY:
574 			PixelSetOpacity(internp->pixel_wand, color_value);
575 		break;
576 #endif
577 
578 		case PHP_IMAGICK_COLOR_ALPHA:
579 			PixelSetAlpha(internp->pixel_wand, color_value);
580 		break;
581 
582 #if MagickLibVersion > 0x628
583 		case PHP_IMAGICK_COLOR_FUZZ:
584 			PixelSetFuzz(internp->pixel_wand, color_value);
585 		break;
586 #endif
587 
588 		default:
589 			php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Unknown color type" TSRMLS_CC);
590 			return;
591 		break;
592 	}
593 	RETVAL_TRUE;
594 }
595 /* }}} */
596 
597 /* {{{ proto array ImagickPixel::getColor([int normalization])
598 	Returns the color of the pixel in an array
599 	normalization - 0 - values returned in the range 0,255 and will be ints, except
600 		for legacy reasons alpha which is 0-1
601 	normalization - 1 - values returned in the range 0,1 and will be floats
602 	normalization - 2 - values returned in the range 0,255 and will be ints including alpha
603 	values i.e. float if ImageMagick was compiled with HDRI, or integers normally.
604 */
605 PHP_METHOD(ImagickPixel, getColor)
606 {
607 	php_imagickpixel_object *internp;
608 	im_long normalization = 0;
609 	double red, green, blue, alpha;
610 
611 	/* Parse parameters given to function */
612 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &normalization) == FAILURE) {
613 		return;
614 	}
615 
616 	internp = Z_IMAGICKPIXEL_P(getThis());
617 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
618 		return;
619 	}
620 	array_init(return_value);
621 
622 	red   = PixelGetRed(internp->pixel_wand);
623 	green = PixelGetGreen(internp->pixel_wand);
624 	blue  = PixelGetBlue(internp->pixel_wand);
625 	alpha = PixelGetAlpha(internp->pixel_wand);
626 
627 	switch (normalization){
628 		//values returned in the range 0,255 and will be ints
629 		case(0): {
630 			//Leave like this for legacy code
631 			//TODO fix the alpha not being normalised at next major/minor verysion
632 			red   *= 255;
633 			green *= 255;
634 			blue  *= 255;
635 
636 			//values are always >=0, so the rounding below may not be necessary
637 			add_assoc_long(return_value, "r", (long) (red   > 0.0 ? red   + 0.5 : red   - 0.5));
638 			add_assoc_long(return_value, "g", (long) (green > 0.0 ? green + 0.5 : green - 0.5));
639 			add_assoc_long(return_value, "b", (long) (blue  > 0.0 ? blue  + 0.5 : blue  - 0.5));
640 			add_assoc_long(return_value, "a", alpha);
641 			break;
642 		}
643 
644 		//values returned in the range 0,1 and will be floats
645 		case(1): {
646 			add_assoc_double(return_value, "r", red);
647 			add_assoc_double(return_value, "g", green);
648 			add_assoc_double(return_value, "b", blue);
649 			add_assoc_double(return_value, "a", alpha);
650 			break;
651 		}
652 
653 		case(2): {
654 			red   *= 255;
655 			green *= 255;
656 			blue  *= 255;
657 			alpha *= 255;
658 
659 			//values are always >=0, so the rounding below may not be necessary
660 			add_assoc_long(return_value, "r", (long) (red   > 0.0 ? red   + 0.5 : red   - 0.5));
661 			add_assoc_long(return_value, "g", (long) (green > 0.0 ? green + 0.5 : green - 0.5));
662 			add_assoc_long(return_value, "b", (long) (blue  > 0.0 ? blue  + 0.5 : blue  - 0.5));
663 			add_assoc_long(return_value, "a", (long) (alpha  > 0.0 ? alpha  + 0.5 : alpha  - 0.5));
664 			break;
665 		}
666 	}
667 
668 	return;
669 }
670 /* }}} */
671 
672 
673 /* {{{ proto array ImagickPixel::getColorQuantum()
674 	Returns the color of the pixel in an array as Quantum values. If ImageMagick was compiled
675 	as HDRI these will be floats, otherwise they will be integers
676 */
677 PHP_METHOD(ImagickPixel, getColorQuantum)
678 {
679 	php_imagickpixel_object *internp;
680 	Quantum red, green, blue, alpha;
681 
682 	if (zend_parse_parameters_none() == FAILURE) {
683 		return;
684 	}
685 
686 	internp = Z_IMAGICKPIXEL_P(getThis());
687 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
688 		return;
689 	}
690 	array_init(return_value);
691 
692 	red   = PixelGetRedQuantum(internp->pixel_wand);
693 	green = PixelGetGreenQuantum(internp->pixel_wand);
694 	blue  = PixelGetBlueQuantum(internp->pixel_wand)  ;
695 	alpha = PixelGetAlphaQuantum(internp->pixel_wand);
696 
697 
698 #if MAGICKCORE_HDRI_ENABLE
699 	add_assoc_double(return_value, "r", red);
700 	add_assoc_double(return_value, "g", green);
701 	add_assoc_double(return_value, "b", blue);
702 	add_assoc_double(return_value, "a", alpha);
703 #else
704 	add_assoc_long(return_value, "r", (long)red);
705 	add_assoc_long(return_value, "g", (long)green);
706 	add_assoc_long(return_value, "b", (long)blue);
707 	add_assoc_long(return_value, "a", (long)alpha);
708 #endif
709 
710 	return;
711 }
712 /* }}} */
713 
714 /* {{{ proto array ImagickPixel::getColorAsString(void )
715         Returns the color as a string
716 */
717 PHP_METHOD(ImagickPixel, getColorAsString)
718 {
719 	php_imagickpixel_object *internp;
720 	char *color_string;
721 
722 	if (zend_parse_parameters_none() == FAILURE) {
723 		return;
724 	}
725 
726 	internp = Z_IMAGICKPIXEL_P(getThis());
727 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
728 		return;
729 	}
730 
731 	color_string = PixelGetColorAsString(internp->pixel_wand);
732 	IM_ZVAL_STRING(return_value, color_string);
733 
734 	IMAGICK_FREE_MAGICK_MEMORY(color_string);
735 	return;
736 }
737 /* }}} */
738 
739 /* {{{ proto ImagickPixel ImagickPixel::clone(void)
740         Clones the ImagickPixel
741 */
742 PHP_METHOD(ImagickPixel, clone)
743 {
744 	php_imagickpixel_object *internp;
745 	php_imagickpixel_object *intern_return;
746 	PixelWand *pixel_wand;
747 
748 	if (zend_parse_parameters_none() == FAILURE) {
749 		return;
750 	}
751 
752 	IMAGICK_METHOD_DEPRECATED("ImagickPixel", "clone");
753 
754 	internp = Z_IMAGICKPIXEL_P(getThis());
755 
756 	pixel_wand = php_imagick_clone_pixelwand (internp->pixel_wand);
757 	if (!pixel_wand) {
758 		php_imagick_throw_exception (IMAGICKPIXEL_CLASS, "Failed to allocate" TSRMLS_CC);
759 		return;
760 	}
761 
762 	object_init_ex(return_value, php_imagickpixel_sc_entry);
763 	intern_return = Z_IMAGICKPIXEL_P(return_value);
764 
765 	php_imagick_replace_pixelwand(intern_return, pixel_wand);
766 	return;
767 }
768 /* }}} */
769 
770 /* {{{ proto int ImagickPixel::getColorCount()
771 	Returns the color count associated with this color.
772 */
773 PHP_METHOD(ImagickPixel, getColorCount)
774 {
775 	php_imagickpixel_object *internp;
776 
777 	if (zend_parse_parameters_none() == FAILURE) {
778 		return;
779 	}
780 
781 	internp = Z_IMAGICKPIXEL_P(getThis());
782 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
783 		return;
784 	}
785 
786 	RETVAL_LONG(PixelGetColorCount(internp->pixel_wand));
787 }
788 /* }}} */
789 
790 /* {{{ proto int ImagickPixel::setColorCount(int colorCount)
791 	Sets the color count associated with this color.
792 */
793 PHP_METHOD(ImagickPixel, setColorCount)
794 {
795 	php_imagickpixel_object *internp;
796 	im_long color_count;
797 
798 	/* Parse parameters given to function */
799 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color_count) == FAILURE) {
800 		return;
801 	}
802 
803 	internp = Z_IMAGICKPIXEL_P(getThis());
804 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
805 		return;
806 	}
807 
808     PixelSetColorCount(internp->pixel_wand, color_count);
809 	RETVAL_TRUE;
810 }
811 /* }}} */
812 
813 #if MagickLibVersion >= 0x693
814 /* {{{ proto bool ImagickPixel::setColorFromPixel(ImagickPixel $srcPixel)
815 	Sets the color count associated with this color from another ImagickPixel object.
816 */
817 PHP_METHOD(ImagickPixel, setColorFromPixel)
818 {
819 	php_imagickpixel_object *internp, *src_pixel;
820 	zval *objvar;
821 
822 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &objvar, php_imagickpixel_sc_entry) == FAILURE) {
823 		return;
824 	}
825 
826 	internp = Z_IMAGICKPIXEL_P(getThis());
827 	if (php_imagickpixel_ensure_not_null(internp->pixel_wand) == 0) {
828 		return;
829 	}
830 	src_pixel = Z_IMAGICKPIXEL_P(objvar);
831 	if (php_imagickpixel_ensure_not_null(src_pixel->pixel_wand) == 0) {
832 		return;
833 	}
834 
835 	PixelSetColorFromWand(internp->pixel_wand, src_pixel->pixel_wand);
836 
837 	RETVAL_TRUE;
838 }
839 /* }}} */
840 
841 #endif //#if MagickLibVersion >= 0x693
842