/
rmimage.c
8823 lines (7564 loc) · 225 KB
/
rmimage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: rmimage.c,v 1.138 2006/01/12 23:46:03 rmagick Exp $ */
/*============================================================================\
| Copyright (C) 2006 by Timothy P. Hunter
| Name: rmimage.c
| Author: Tim Hunter
| Purpose: Image class method definitions for RMagick
\============================================================================*/
#include "rmagick.h"
// magick_config.h in GraphicsMagick doesn't define HasX11
#if defined(HAVE_XIMPORTIMAGE)
#if !defined(HasX11)
#define HasX11
#endif
#include "magick/xwindow.h" // XImageInfo
#endif
typedef Image *(effector_t)(const Image *, const double, const double, ExceptionInfo *);
typedef Image *(flipper_t)(const Image *, ExceptionInfo *);
typedef Image *(magnifier_t)(const Image *, ExceptionInfo *);
typedef Image *(reader_t)(const Info *, ExceptionInfo *);
typedef Image *(scaler_t)(const Image *, const unsigned long, const unsigned long, ExceptionInfo *);
typedef unsigned int (thresholder_t)(Image *, const char *);
typedef Image *(xformer_t)(const Image *, const RectangleInfo *, ExceptionInfo *);
static VALUE effect_image(VALUE, int, VALUE *, effector_t *);
static VALUE rd_image(VALUE, VALUE, reader_t);
static VALUE scale(int, int, VALUE *, VALUE, scaler_t *);
static VALUE cropper(int, int, VALUE *, VALUE);
static VALUE xform_image(int, VALUE, VALUE, VALUE, VALUE, VALUE, xformer_t);
static VALUE threshold_image(int, VALUE *, VALUE, thresholder_t);
static VALUE array_from_images(Image *);
static ChannelType extract_channels(int *, VALUE *);
static void raise_ChannelType_error(VALUE);
static ImageAttribute *Next_Attribute;
/*
Method: Image#adaptive_threshold(width=3, height=3, offset=0)
Purpose: selects an individual threshold for each pixel based on
the range of intensity values in its local neighborhood.
This allows for thresholding of an image whose global
intensity histogram doesn't contain distinctive peaks.
Returns: a new image
*/
VALUE
Image_adaptive_threshold(int argc, VALUE *argv, VALUE self)
{
Image *image, *new_image;
unsigned long width = 3, height = 3, offset = 0;
ExceptionInfo exception;
switch (argc)
{
case 3:
offset = NUM2ULONG(argv[2]);
case 2:
height = NUM2ULONG(argv[1]);
case 1:
width = NUM2ULONG(argv[0]);
case 0:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 3)", argc);
}
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
new_image = AdaptiveThresholdImage(image, width, height, offset, &exception);
HANDLE_ERROR
return rm_image_new(new_image);
}
/*
Method: Image#add_noise(noise_type)
Purpose: add random noise to a copy of the image
Returns: a new image
*/
VALUE
Image_add_noise(VALUE self, VALUE noise)
{
Image *image, *new_image;
NoiseType noise_type;
ExceptionInfo exception;
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
VALUE_TO_ENUM(noise, noise_type, NoiseType);
new_image = AddNoiseImage(image, noise_type, &exception);
HANDLE_ERROR
return rm_image_new(new_image);
}
/*
Method: Image#add_noise_channel(noise_type[,channel...])
Purpose: add random noise to a copy of the image
Returns: a new image
*/
VALUE
Image_add_noise_channel(int argc, VALUE *argv, VALUE self)
{
Image *image, *new_image;
NoiseType noise_type;
ExceptionInfo exception;
ChannelType channels;
channels = extract_channels(&argc, argv);
// There must be 1 remaining argument.
if (argc == 0)
{
rb_raise(rb_eArgError, "missing noise type argument");
}
else if (argc > 1)
{
raise_ChannelType_error(argv[argc-1]);
}
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
VALUE_TO_ENUM(argv[0], noise_type, NoiseType);
channels &= ~OpacityChannel;
new_image = AddNoiseImageChannel(image, channels, noise_type, &exception);
HANDLE_ERROR
return rm_image_new(new_image);
}
/*
Method: Image#affine_transform(affine_matrix)
Purpose: transforms an image as dictated by the affine matrix argument
Returns: a new image
*/
VALUE
Image_affine_transform(VALUE self, VALUE affine)
{
Image *image, *new_image;
ExceptionInfo exception;
AffineMatrix matrix;
Data_Get_Struct(self, Image, image);
// Convert Magick::AffineMatrix to AffineMatrix structure.
AffineMatrix_to_AffineMatrix(&matrix, affine);
GetExceptionInfo(&exception);
new_image = AffineTransformImage(image, &matrix, &exception);
HANDLE_ERROR
return rm_image_new(new_image);
}
/*
Method: Image#["key"]
Image#[:key]
Purpose: Return the image property associated with "key"
Returns: property value or nil if key doesn't exist
Notes: Use Image#[]= (aset) to establish more properties
or change the value of an existing property.
*/
VALUE
Image_aref(VALUE self, VALUE key_arg)
{
Image *image;
char *key;
const ImageAttribute *attr;
switch (TYPE(key_arg))
{
case T_NIL:
return Qnil;
case T_SYMBOL:
key = rb_id2name(SYM2ID(key_arg));
break;
default:
key = STRING_PTR(key_arg);
if (*key == '\0')
{
return Qnil;
}
break;
}
Data_Get_Struct(self, Image, image);
attr = GetImageAttribute(image, key);
return attr ? rb_str_new2(attr->value) : Qnil;
}
/*
Method: Image#["key"] = attr
Image#[:key] = attr
Purpose: Update or add image attribute "key"
Returns: self
Notes: Specify attr=nil to remove the key from the list.
SetImageAttribute normally APPENDS the new value
to any existing value. Since this usage is tremendously
counter-intuitive, this function always deletes the
existing value before setting the new value.
There's no use checking the return value since
SetImageAttribute returns "False" for many reasons,
some legitimate.
*/
VALUE
Image_aset(VALUE self, VALUE key_arg, VALUE attr_arg)
{
Image *image;
char *key, *attr;
const ImageAttribute *attribute;
unsigned int okay;
rm_check_frozen(self);
attr = attr_arg == Qnil ? NULL : STRING_PTR(attr_arg);
switch (TYPE(key_arg))
{
case T_NIL:
return self;
case T_SYMBOL:
key = rb_id2name(SYM2ID(key_arg));
break;
default:
key = STRING_PTR(key_arg);
if (*key == '\0')
{
return self;
}
break;
}
Data_Get_Struct(self, Image, image);
// If we're currently looping over the attributes via
// Image_properties (below) then check to see if we're
// about to delete the next attribute. If so, change
// the "next" pointer to point to the attribute following
// this one. (No, this isn't thread-safe!)
if (Next_Attribute)
{
attribute = GetImageAttribute(image, key);
if (attribute && attribute == Next_Attribute)
{
Next_Attribute = attribute->next;
}
}
// Delete existing value. SetImageAttribute returns False if
// the attribute doesn't exist - we don't care.
(void) SetImageAttribute(image, key, NULL);
// Set new value
if (attr)
{
okay = SetImageAttribute(image, key, attr);
if (!okay)
{
rb_warning("RMagick: SetImageAttribute failed "
"(probably out of memory)");
}
}
return self;
}
/*
Method: Image#background_color
Purpose: Return the name of the background color as a String.
*/
VALUE
Image_background_color(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
return PixelPacket_to_Color_Name(image, &image->background_color);
}
/*
Method: Image#background_color=
Purpose: Set the the background color to the specified color spec.
*/
VALUE
Image_background_color_eq(VALUE self, VALUE color)
{
Image *image;
rm_check_frozen(self);
Data_Get_Struct(self, Image, image);
Color_to_PixelPacket(&image->background_color, color);
return self;
}
/*
Method: Image#base_columns
Purpose: Return the number of rows (before transformations)
*/
VALUE Image_base_columns(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
return INT2FIX(image->magick_columns);
}
/*
Method: Image#base_filename
Purpose: Return the image filename (before transformations)
Notes: If there is no base filename, return the current filename.
*/
VALUE Image_base_filename(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
if (*image->magick_filename)
{
return rb_str_new2(image->magick_filename);
}
else
{
return rb_str_new2(image->filename);
}
}
/*
Method: Image#base_rows
Purpose: Return the number of rows (before transformations)
*/
VALUE Image_base_rows(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
return INT2FIX(image->magick_rows);
}
/*
* Method: Image#bilevel_channel(threshold, channel=AllChannels)
* Returns a new image
*/
VALUE
Image_bilevel_channel(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_BILEVELIMAGECHANNEL)
Image *image, *new_image;
ChannelType channels;
ExceptionInfo exception;
channels = extract_channels(&argc, argv);
if (argc > 1)
{
raise_ChannelType_error(argv[argc-1]);
}
if (argc == 0)
{
rb_raise(rb_eArgError, "no threshold specified");
}
GetExceptionInfo(&exception);
Data_Get_Struct(self, Image, image);
new_image = CloneImage(image, 0, 0, True, &exception);
(void)BilevelImageChannel(new_image, channels, NUM2DBL(argv[0]));
return rm_image_new(new_image);
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
/*
* Method: Image#black_threshold(red_channel [, green_channel
* [, blue_channel [, opacity_channel]]]);
* Purpose: Call BlackThresholdImage
*/
VALUE
Image_black_threshold(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_BLACKTHRESHOLDIMAGE)
return threshold_image(argc, argv, self, BlackThresholdImage);
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
DEF_ATTR_ACCESSOR(Image, blur, dbl)
/*
* Method: Image#blur_channel(radius = 0.0, sigma = 1.0, channel=AllChannels)
* Purpose: Call BlurImageChannel
*/
VALUE
Image_blur_channel(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_BLURIMAGECHANNEL)
Image *image, *new_image;
ExceptionInfo exception;
ChannelType channels;
double radius = 0.0, sigma = 1.0;
Data_Get_Struct(self, Image, image);
channels = extract_channels(&argc, argv);
// There can be 0, 1, or 2 remaining arguments.
switch (argc)
{
case 2:
sigma = NUM2DBL(argv[1]);
case 1:
radius = NUM2DBL(argv[0]);
case 0:
break;
default:
raise_ChannelType_error(argv[argc-1]);
}
GetExceptionInfo(&exception);
new_image = BlurImageChannel(image, channels, radius, sigma, &exception);
HANDLE_ERROR
return rm_image_new(new_image);
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
/*
Method: Image#blur_image(radius=0.0, sigma=1.0)
Purpose: Blur the image
Notes: The "blur" name is used for the attribute
*/
VALUE
Image_blur_image(int argc, VALUE *argv, VALUE self)
{
return effect_image(self, argc, argv, BlurImage);
}
/*
Method: Image#border(width, height, color)
Image#border!(width, height, color)
Purpose: surrounds the image with a border of the specified width,
height, and named color
*/
static VALUE border(
int bang,
VALUE self,
VALUE width,
VALUE height,
VALUE color)
{
Image *image, *new_image;
PixelPacket old_border;
ExceptionInfo exception;
RectangleInfo rect = {0};
Data_Get_Struct(self, Image, image);
rect.width = NUM2UINT(width);
rect.height = NUM2UINT(height);
// Save current border color - we'll want to restore it afterwards.
old_border = image->border_color;
Color_to_PixelPacket(&image->border_color, color);
GetExceptionInfo(&exception);
new_image = BorderImage(image, &rect, &exception);
HANDLE_ERROR
if (bang)
{
new_image->border_color = old_border;
DATA_PTR(self) = new_image;
DestroyImage(image);
return self;
}
image->border_color = old_border;
return rm_image_new(new_image);
}
VALUE
Image_border_bang(
VALUE self,
VALUE width,
VALUE height,
VALUE color)
{
rm_check_frozen(self);
return border(True, self, width, height, color);
}
VALUE
Image_border(
VALUE self,
VALUE width,
VALUE height,
VALUE color)
{
return border(False, self, width, height, color);
}
/*
Method: Image#border_color
Purpose: Return the name of the border color as a String.
*/
VALUE
Image_border_color(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
return PixelPacket_to_Color_Name(image, &image->border_color);
}
/*
Method: Image#border_color=
Purpose: Set the the border color
*/
VALUE
Image_border_color_eq(VALUE self, VALUE color)
{
Image *image;
rm_check_frozen(self);
Data_Get_Struct(self, Image, image);
Color_to_PixelPacket(&image->border_color, color);
return self;
}
/*
Method: Image#bounding_box
Purpose: returns the bounding box of an image canvas
*/
VALUE Image_bounding_box(VALUE self)
{
Image *image;
RectangleInfo box;
ExceptionInfo exception;
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
box = GetImageBoundingBox(image, &exception);
HANDLE_ERROR
return Rectangle_from_RectangleInfo(&box);
}
/*
Method: Image.capture(silent=false,
frame=false,
descend=false,
screen=false,
borders=false) { optional parms }
Purpose: do a screen capture
*/
VALUE
Image_capture(
int argc,
VALUE *argv,
VALUE self)
{
#ifdef HAVE_XIMPORTIMAGE
Image *image;
ImageInfo *image_info;
volatile VALUE info_obj;
XImportInfo ximage_info;
self = self; // Suppress "never referenced" message from icc
XGetImportInfo(&ximage_info);
switch (argc)
{
case 5:
ximage_info.borders = RTEST(argv[4]);
case 4:
ximage_info.screen = RTEST(argv[3]);
case 3:
ximage_info.descend = RTEST(argv[2]);
case 2:
ximage_info.frame = RTEST(argv[1]);
case 1:
ximage_info.silent = RTEST(argv[0]);
case 0:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 5)", argc);
break;
}
// Get optional parms.
// Set info->filename = "root", window ID number or window name,
// or nothing to do an interactive capture
// Set info->server_name to the server name
// Also info->colorspace, depth, dither, interlace, type
info_obj = rm_info_new();
Data_Get_Struct(info_obj, Info, image_info);
// If an error occurs, IM will call our error handler and we raise an exception.
image = XImportImage(image_info, &ximage_info);
assert(image);
return rm_image_new(image);
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
/*
Method: Image#change_geometry(geometry_string) { |cols, rows, image| }
Purpose: parse geometry string, compute new image geometry
*/
VALUE
Image_change_geometry(VALUE self, VALUE geom_arg)
{
#if defined(HAVE_PARSESIZEGEOMETRY)
Image *image;
RectangleInfo rect = {0};
volatile VALUE geom_str;
char *geometry;
unsigned int flags;
volatile VALUE ary;
Data_Get_Struct(self, Image, image);
geom_str = rb_funcall(geom_arg, ID_to_s, 0);
geometry = STRING_PTR(geom_str);
flags = ParseSizeGeometry(image, geometry, &rect);
if (flags == NoValue)
{
rb_raise(rb_eArgError, "invalid geometry string `%s'", geometry);
}
ary = rb_ary_new2(3);
rb_ary_store(ary, 0, ULONG2NUM(rect.width));
rb_ary_store(ary, 1, ULONG2NUM(rect.height));
rb_ary_store(ary, 2, self);
return rb_yield(ary);
#else
Image *image;
char *geometry;
unsigned int flags;
long x, y;
unsigned long width, height;
volatile VALUE ary;
volatile VALUE geom_str;
Data_Get_Struct(self, Image, image);
geom_str = rb_funcall(geom_arg, ID_to_s, 0);
geometry = STRING_PTR(geom_str);
width = image->columns;
height = image->rows;
flags = GetMagickGeometry(geometry, &x, &y, &width, &height);
if (flags == NoValue)
{
rb_raise(rb_eArgError, "invalid geometry string `%s'", geometry);
}
ary = rb_ary_new2(3);
rb_ary_store(ary, 0, ULONG2NUM(width));
rb_ary_store(ary, 1, ULONG2NUM(height));
rb_ary_store(ary, 2, self);
return rb_yield(ary);
#endif
}
/*
Method: Image#changed?
Purpose: Return true if any pixel in the image has been altered since
the image was constituted.
*/
VALUE
Image_changed_q(VALUE self)
{
Image *image;
Data_Get_Struct(self, Image, image);
return IsTaintImage(image) ? Qtrue : Qfalse;
}
/*
Method: Image#channel
Purpose: Extract a channel from the image. A channel is a particular color
component of each pixel in the image.
*/
VALUE
Image_channel(VALUE self, VALUE channel_arg)
{
Image *image, *new_image;
ChannelType channel;
ExceptionInfo exception;
Data_Get_Struct(self, Image, image);
VALUE_TO_ENUM(channel_arg, channel, ChannelType);
GetExceptionInfo(&exception);
new_image = CloneImage(image, 0, 0, True, &exception);
HANDLE_ERROR
#if defined(HAVE_SEPARATEIMAGECHANNEL)
(void) SeparateImageChannel(new_image, channel);
#else
(void) ChannelImage(new_image, channel);
#endif
HANDLE_ERROR_IMG(new_image)
return rm_image_new(new_image);
}
/*
Method: Image#channel_depth(channel_depth=AllChannels)
Purpose: GetImageChannelDepth
*/
VALUE
Image_channel_depth(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_GETIMAGECHANNELDEPTH)
Image *image;
ChannelType channels;
unsigned long channel_depth;
ExceptionInfo exception;
channels = extract_channels(&argc, argv);
// Ensure all arguments consumed.
if (argc > 0)
{
raise_ChannelType_error(argv[argc-1]);
}
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
channel_depth = GetImageChannelDepth(image, channels, &exception);
HANDLE_ERROR
return ULONG2NUM(channel_depth);
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
/*
Method: Image#channel_extrema(channel=AllChannels)
Purpose: Returns an array [min, max] where 'min' and 'max'
are the minimum and maximum values of all channels.
Notes: GM's implementation is very different from ImageMagick.
This method follows the IM API very closely and then
shoehorn's the GM API to more-or-less fit. Note that
IM allows you to specify more than one channel argument.
GM does not.
*/
VALUE
Image_channel_extrema(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_GETIMAGECHANNELEXTREMA) // ImageMagick 6.0.0
Image *image;
ChannelType channels;
ExceptionInfo exception;
unsigned long min, max;
unsigned int okay;
volatile VALUE ary;
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
channels = extract_channels(&argc, argv);
// Ensure all arguments consumed.
if (argc > 0)
{
raise_ChannelType_error(argv[argc-1]);
}
okay = GetImageChannelExtrema(image, channels, &min, &max, &exception);
if (!okay)
{
rb_raise(rb_eRuntimeError, "GetImageChannelExtrema failed.");
}
HANDLE_ERROR
ary = rb_ary_new2(2);
rb_ary_store(ary, 0, ULONG2NUM(min));
rb_ary_store(ary, 1, ULONG2NUM(max));
return ary;
#elif defined(HAVE_GETIMAGESTATISTICS) // GraphicsMagick 1.1
Image *image;
ChannelType channel;
ImageStatistics stats;
ExceptionInfo exception;
volatile VALUE ary;
volatile VALUE type_name;
MagickPassFail okay;
if (argc == 0)
{
rb_raise(rb_eArgError, "GraphicsMagick requires at least one channel argument.");
}
else if (argc > 1)
{
rb_raise(rb_eArgError, "GraphicsMagick does not support multi-channel statistics."
" Specify only 1 channel.");
}
VALUE_TO_ENUM(argv[0], channel, ChannelType);
if (channel == AllChannels)
{
rb_raise(rb_eArgError, "GraphicsMagick does not support multi-channel statistics."
" Specify only 1 channel.");
}
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
okay = GetImageStatistics(image, &stats, &exception);
HANDLE_ERROR
if (okay == MagickFail)
{
rb_raise(rb_eRuntimeError, "GetImageStatistics failed.");
}
ary = rb_ary_new2(2);
switch(channel)
{
case RedChannel:
case CyanChannel:
rb_ary_store(ary, 0, ULONG2NUM((unsigned long)(MaxRGB*stats.red.minimum)));
rb_ary_store(ary, 1, ULONG2NUM((unsigned long)(MaxRGB*stats.red.maximum)));
break;
case GreenChannel:
case MagentaChannel:
rb_ary_store(ary, 0, ULONG2NUM((unsigned long)(MaxRGB*stats.green.minimum)));
rb_ary_store(ary, 1, ULONG2NUM((unsigned long)(MaxRGB*stats.green.maximum)));
break;
case BlueChannel:
case YellowChannel:
rb_ary_store(ary, 0, ULONG2NUM((unsigned long)(MaxRGB*stats.blue.minimum)));
rb_ary_store(ary, 1, ULONG2NUM((unsigned long)(MaxRGB*stats.blue.maximum)));
break;
case OpacityChannel:
case BlackChannel:
case MatteChannel:
rb_ary_store(ary, 0, ULONG2NUM((unsigned long)(MaxRGB*stats.opacity.minimum)));
rb_ary_store(ary, 1, ULONG2NUM((unsigned long)(MaxRGB*stats.opacity.maximum)));
break;
default:
type_name = Enum_to_s(argv[0]);
rb_raise(rb_eArgError, "unsupported channel type: %s",
STRING_PTR(type_name));
}
return ary;
#else
rm_not_implemented();
return (VALUE)0;
#endif
}
/*
* Method: Image#channel_mean(channel=AllChannels)
* Returns An array [mean, std. deviation]
*/
VALUE
Image_channel_mean(int argc, VALUE *argv, VALUE self)
{
#if defined(HAVE_GETIMAGECHANNELMEAN) // ImageMagick 6.0.0
Image *image;
ChannelType channels;
ExceptionInfo exception;
double mean, stddev;
unsigned int okay;
volatile VALUE ary;
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
channels = extract_channels(&argc, argv);
// Ensure all arguments consumed.
if (argc > 0)
{
raise_ChannelType_error(argv[argc-1]);
}
okay = GetImageChannelMean(image, channels, &mean, &stddev, &exception);
if (!okay)
{
rb_raise(rb_eRuntimeError, "GetImageChannelMean failed.");
}
HANDLE_ERROR
ary = rb_ary_new2(2);
rb_ary_store(ary, 0, rb_float_new(mean));
rb_ary_store(ary, 1, rb_float_new(stddev));
return ary;
#elif defined(HAVE_GETIMAGESTATISTICS) // GraphicsMagick 1.1
Image *image;
ChannelType channel;
ImageStatistics stats;
ExceptionInfo exception;
volatile VALUE ary;
volatile VALUE type_name;
MagickPassFail okay;
if (argc == 0)
{
rb_raise(rb_eArgError, "GraphicsMagick requires at least one channel argument.");
}
else if (argc > 1)
{
rb_raise(rb_eArgError, "GraphicsMagick does not support multi-channel statistics."
" Specify only 1 channel.");
}
VALUE_TO_ENUM(argv[0], channel, ChannelType);
if (channel == AllChannels)
{
rb_raise(rb_eArgError, "GraphicsMagick does not support multi-channel statistics."
" Specify only 1 channel.");
}
Data_Get_Struct(self, Image, image);
GetExceptionInfo(&exception);
okay = GetImageStatistics(image, &stats, &exception);
HANDLE_ERROR
if (okay == MagickFail)
{
rb_raise(rb_eRuntimeError, "GetImageStatistics failed.");
}
ary = rb_ary_new2(2);
switch(channel)
{
case RedChannel:
case CyanChannel:
rb_ary_store(ary, 0, rb_float_new(stats.red.mean));
rb_ary_store(ary, 1, rb_float_new(stats.red.standard_deviation));
break;
case GreenChannel:
case MagentaChannel:
rb_ary_store(ary, 0, rb_float_new(stats.green.mean));
rb_ary_store(ary, 1, rb_float_new(stats.green.standard_deviation));
break;
case BlueChannel:
case YellowChannel:
rb_ary_store(ary, 0, rb_float_new(stats.blue.mean));
rb_ary_store(ary, 1, rb_float_new(stats.blue.standard_deviation));
break;
case OpacityChannel:
case BlackChannel:
case MatteChannel:
rb_ary_store(ary, 0, rb_float_new(stats.opacity.mean));
rb_ary_store(ary, 1, rb_float_new(stats.opacity.standard_deviation));
break;