forked from Imagick/imagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagickpixeliterator_class.c
635 lines (513 loc) · 17.4 KB
/
imagickpixeliterator_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
/*
+----------------------------------------------------------------------+
| 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
static
long s_count_pixeliterator_rows(php_imagickpixeliterator_object *internpix)
{
long rows = 0, tmp;
PixelWand **row;
PixelResetIterator(internpix->pixel_iterator);
while ((row = PixelGetNextIteratorRow(internpix->pixel_iterator, &tmp)) != NULL)
rows++;
return rows;
}
#endif
void php_imagick_pixel_iterator_new (PixelIterator *pixel_it, zval *return_value TSRMLS_DC)
{
php_imagickpixeliterator_object *internpix;
object_init_ex(return_value, php_imagickpixeliterator_sc_entry);
internpix = Z_IMAGICKPIXELITERATOR_P(return_value);
internpix->pixel_iterator = pixel_it;
internpix->initialized = 1;
#if MagickLibVersion <= 0x628
internpix->rows = s_count_pixeliterator_rows (internpix);
#endif
}
/* {{{ proto ImagickPixelIterator ImagickPixelIterator::__construct(Imagick source)
The ImagickPixelIterator constructor
*/
PHP_METHOD(ImagickPixelIterator, __construct)
{
zval *magick_object;
php_imagickpixeliterator_object *internpix;
php_imagick_object *intern;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|llll", &magick_object, php_imagick_sc_entry) == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
intern = Z_IMAGICK_P(magick_object);
if (!intern->magick_wand) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Invalid Imagick object passed" TSRMLS_CC);
return;
}
if (php_imagick_ensure_not_empty (intern->magick_wand) == 0)
return;
internpix->pixel_iterator = NewPixelIterator(intern->magick_wand);
if (!internpix->pixel_iterator) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Can not allocate ImagickPixelIterator" TSRMLS_CC);
return;
}
#if MagickLibVersion <= 0x628
internpix->rows = s_count_pixeliterator_rows (internpix);
#endif
internpix->initialized = 1;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::resetIterator()
Resets the pixel iterator. Use it in conjunction with PixelGetNextIteratorRow() to iterate over all the pixels in a pixel container.
*/
PHP_METHOD(ImagickPixelIterator, resetIterator)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
RETURN_THROWS();
}
PixelResetIterator(internpix->pixel_iterator);
#if MagickLibVersion <= 0x628
internpix->iterator_position = 0;
#endif
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::syncIterator()
Syncs the pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, syncIterator)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
PixelSyncIterator(internpix->pixel_iterator);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::setIteratorFirstRow()
Sets the pixel iterator to the first pixel row.
*/
PHP_METHOD(ImagickPixelIterator, setIteratorFirstRow)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
PixelSetFirstIteratorRow(internpix->pixel_iterator);
#if MagickLibVersion <= 0x628
internpix->iterator_position = 0;
#endif
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::setIteratorLastRow()
Sets the pixel iterator to the last pixel row.
*/
PHP_METHOD(ImagickPixelIterator, setIteratorLastRow)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
PixelSetLastIteratorRow(internpix->pixel_iterator);
#if MagickLibVersion <= 0x628
internpix->iterator_position = (internpix->rows - 1);
#endif
RETURN_TRUE;
}
/* }}} */
/* {{{ proto ImagickPixelIterator ImagickPixelIterator::getPixelIterator(Imagick source)
Returns a new pixel iterator, static method. This is equivalent to Imagick::getPixelIterator
which probably ought to be used in preference to this method.
*/
PHP_METHOD(ImagickPixelIterator, getPixelIterator)
{
PixelIterator *pixel_it;
zval *magick_object;
php_imagick_object *intern;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &magick_object, php_imagick_sc_entry) == FAILURE) {
return;
}
intern = Z_IMAGICK_P(magick_object);
if (!intern->magick_wand) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Invalid Imagick object passed" TSRMLS_CC);
return;
}
if (php_imagick_ensure_not_empty (intern->magick_wand) == 0)
return;
pixel_it = NewPixelIterator (intern->magick_wand);
if (!pixel_it) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Can not allocate ImagickPixelIterator" TSRMLS_CC);
return;
}
php_imagick_pixel_iterator_new (pixel_it, return_value TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto ImagickPixelIterator ImagickPixelIterator::newPixelIterator(Imagick source)
Returns a new pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, newPixelIterator)
{
PixelIterator *pixel_it;
zval *magick_object;
php_imagickpixeliterator_object *internpix;
php_imagick_object *intern;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &magick_object, php_imagick_sc_entry) == FAILURE) {
return;
}
IMAGICK_METHOD_DEPRECATED_USE_INSTEAD("ImagickPixelIterator", "newPixelIterator", "ImagickPixelIterator", "getPixelIterator");
internpix = Z_IMAGICKPIXELITERATOR_P(getThis() );
intern = Z_IMAGICK_P(magick_object);
if (!intern->magick_wand) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Invalid Imagick object passed" TSRMLS_CC);
return;
}
if (php_imagick_ensure_not_empty (intern->magick_wand) == 0)
return;
pixel_it = NewPixelIterator(intern->magick_wand);
if (!pixel_it) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Can not allocate ImagickPixelIterator" TSRMLS_CC);
return;
}
if (internpix->initialized && internpix->pixel_iterator)
DestroyPixelIterator (internpix->pixel_iterator);
internpix->pixel_iterator = pixel_it;
#if MagickLibVersion <= 0x628
internpix->rows = s_count_pixeliterator_rows (internpix);
#endif
internpix->initialized = 1;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto ImagickPixelIterator ImagickPixelIterator::getPixelRegionIterator(Imagick source, int x, int y, int columns, int rows)
Returns a new pixel region iterator, static method. This is equivalent to the non-static
Imagick::getPixelRegionIterator which ought to be used in preference to this method.
*/
PHP_METHOD(ImagickPixelIterator, getPixelRegionIterator)
{
PixelIterator *pixel_it;
zval *magick_object;
php_imagick_object *intern;
long x, y, columns, rows;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ollll", &magick_object, php_imagick_sc_entry, &x, &y, &columns, &rows) == FAILURE) {
return;
}
intern = Z_IMAGICK_P(magick_object);
if (!intern->magick_wand) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Invalid Imagick object passed" TSRMLS_CC);
return;
}
if (php_imagick_ensure_not_empty (intern->magick_wand) == 0)
return;
pixel_it = NewPixelRegionIterator(intern->magick_wand, x, y, columns, rows);
if (!pixel_it) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Can not allocate ImagickPixelIterator" TSRMLS_CC);
return;
}
php_imagick_pixel_iterator_new (pixel_it, return_value TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::newPixelRegionIterator(Imagick source, int x, int y, int columns, int rows)
Returns a new pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, newPixelRegionIterator)
{
PixelIterator *pixel_it;
zval *magick_object;
php_imagickpixeliterator_object *internpix;
php_imagick_object *intern;
long x, y, columns, rows;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ollll", &magick_object, php_imagick_sc_entry, &x, &y, &columns, &rows) == FAILURE) {
return;
}
IMAGICK_METHOD_DEPRECATED_USE_INSTEAD("ImagickPixelIterator", "newPixelRegionIterator", "ImagickPixelIterator", "getPixelRegionIterator");
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
intern = Z_IMAGICK_P(magick_object);
if (!intern->magick_wand) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Invalid Imagick object passed" TSRMLS_CC);
return;
}
if (php_imagick_ensure_not_empty (intern->magick_wand) == 0)
return;
pixel_it = NewPixelRegionIterator(intern->magick_wand, x, y, columns, rows);
if (!pixel_it) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "Can not allocate ImagickPixelIterator" TSRMLS_CC);
return;
}
if (internpix->initialized && internpix->pixel_iterator)
DestroyPixelIterator (internpix->pixel_iterator);
internpix->pixel_iterator = pixel_it;
#if MagickLibVersion <= 0x628
internpix->rows = s_count_pixeliterator_rows (internpix);
#endif
internpix->initialized = 1;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ImagickPixelIterator::getIteratorRow()
Returns the crurent pixel iterator row.
*/
PHP_METHOD(ImagickPixelIterator, getIteratorRow)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
#if MagickLibVersion <= 0x628
ZVAL_LONG(return_value, internpix->iterator_position);
#else
ZVAL_LONG(return_value, (long) PixelGetIteratorRow(internpix->pixel_iterator));
#endif
return;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::setIteratorRow(int row)
Set the pixel iterator row.
*/
PHP_METHOD(ImagickPixelIterator, setIteratorRow)
{
php_imagickpixeliterator_object *internpix;
MagickBooleanType status;
im_long row;
/* Parse parameters given to function */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &row) == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
status = PixelSetIteratorRow(internpix->pixel_iterator, row);
if (status == MagickFalse) {
php_imagick_convert_imagickpixeliterator_exception(internpix->pixel_iterator, "Unable to set iterator row" TSRMLS_CC);
return;
}
#if MagickLibVersion <= 0x628
internpix->iterator_position = row;
#endif
RETURN_TRUE;
}
/* }}} */
static
void s_pixelwands_to_zval (PixelWand **wand_array, unsigned long num_wands, zval *return_value TSRMLS_DC)
{
php_imagickpixel_object *internp;
#if PHP_VERSION_ID >= 70000
zval obj;
#else
zval *obj;
#endif
unsigned long i;
array_init(return_value);
for (i = 0; i < num_wands; i++) {
#if PHP_VERSION_ID >= 70000
object_init_ex(&obj, php_imagickpixel_sc_entry);
internp = Z_IMAGICKPIXEL_P(&obj);
#else
MAKE_STD_ZVAL(obj);
object_init_ex(obj, php_imagickpixel_sc_entry);
internp = Z_IMAGICKPIXEL_P(obj);
#endif
internp->initialized_via_iterator = 1;
php_imagick_replace_pixelwand(internp, wand_array[i]);
#if PHP_VERSION_ID >= 70000
add_next_index_zval(return_value, &obj);
#else
add_next_index_zval(return_value, obj);
#endif
}
}
/* {{{ proto array ImagickPixelIterator::getPreviousIteratorRow()
Returns the previous row as an array of pixel wands from the pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, getPreviousIteratorRow)
{
php_imagickpixeliterator_object *internpix;
PixelWand **wand_array;
size_t num_wands;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
wand_array = PixelGetPreviousIteratorRow(internpix->pixel_iterator, &num_wands);
#if MagickLibVersion <= 0x628
if (internpix->iterator_position > 0) {
internpix->iterator_position--;
}
#endif
if (!wand_array) {
RETURN_NULL();
}
s_pixelwands_to_zval (wand_array, num_wands, return_value TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto array ImagickPixelIterator::getCurrentIteratorRow()
Returns the current row as an array of pixel wands from the pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, getCurrentIteratorRow)
{
php_imagickpixeliterator_object *internpix;
PixelWand **wand_array;
size_t num_wands;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
RETURN_THROWS();
}
#if MagickLibVersion <= 0x628
if (internpix->iterator_position >= internpix->rows) {
RETURN_NULL();
} else if (internpix->iterator_position != 0) {
long tmp;
(void)PixelGetPreviousIteratorRow(internpix->pixel_iterator, &tmp);
}
wand_array = PixelGetNextIteratorRow(internpix->pixel_iterator, &num_wands);
#else
wand_array = PixelGetCurrentIteratorRow(internpix->pixel_iterator, &num_wands);
#endif
if (!wand_array) {
RETURN_NULL();
}
s_pixelwands_to_zval (wand_array, num_wands, return_value TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto array ImagickPixelIterator::getNextIteratorRow()
Returns the next row as an array of pixel wands from the pixel iterator.
*/
PHP_METHOD(ImagickPixelIterator, getNextIteratorRow)
{
php_imagickpixeliterator_object *internpix;
PixelWand **wand_array;
size_t num_wands;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
wand_array = PixelGetNextIteratorRow(internpix->pixel_iterator, &num_wands);
#if MagickLibVersion <= 0x628
internpix->iterator_position++;
#endif
if (!wand_array) {
RETURN_NULL();
}
s_pixelwands_to_zval (wand_array, num_wands, return_value TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::clear()
Clear resources associated with a PixelIterator.
*/
PHP_METHOD(ImagickPixelIterator, clear)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
/* Assertion failure with earlier versions */
#if MagickLibVersion <= 0x628
RETURN_TRUE;
#else
ClearPixelIterator(internpix->pixel_iterator);
RETURN_TRUE;
#endif
}
/* }}} */
/* {{{ proto bool ImagickPixelIterator::valid()
The if the current iterator row is valid
*/
PHP_METHOD(ImagickPixelIterator, valid)
{
php_imagickpixeliterator_object *internpix;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
internpix = Z_IMAGICKPIXELITERATOR_P(getThis());
if (!internpix->initialized) {
php_imagick_throw_exception (IMAGICKPIXELITERATOR_CLASS, "ImagickPixelIterator is not initialized correctly" TSRMLS_CC);
return;
}
#if MagickLibVersion <= 0x628
if (internpix->iterator_position < internpix->rows) {
RETURN_TRUE;
}
#else
/* Test if the current row is valid */
if (PixelSetIteratorRow(internpix->pixel_iterator, PixelGetIteratorRow(internpix->pixel_iterator))) {
RETURN_TRUE;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* END OF PIXELITERATOR */