-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageobject.c
8645 lines (7681 loc) · 263 KB
/
imageobject.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
/* Author: Travis Oliphant
* Copyright: 2003
*
* Ported to GraphicsMagick by Christian Klein
*
* See LICENSE for explanation of terms
*/
#include <Python.h>
#include <setjmp.h>
#include <Numeric/arrayobject.h>
#include <magick/api.h>
/* Compatibility
*/
#ifndef ThrowBinaryException
#define ThrowBinaryException(severity,tag,context) \
{ \
if (image != (Image *) NULL) \
(void) ThrowException(&image->exception, severity, tag, context); \
return(MagickFalse); \
}
#endif
/* End
*/
static PyObject *PyMagickError;
static jmp_buf error_jmp;
static int ptype;
static size_t _qsize;
#if !defined(DegreesToRadians)
#define DegreesToRadians(x) ((x)*3.14159265358979323846/180.0)
#endif
#define False 0
#define True 1
#define DATA(arr) (((PyArrayObject *)(arr))->data)
#define ELSIZE(arr) (((PyArrayObject *)(arr))->descr->elsize)
#define TYPE(arr) (((PyArrayObject *)(arr))->descr->type_num)
#define RANK(arr) (((PyArrayObject *)(arr))->nd)
#define DIMS(arr) (((PyArrayObject *)(arr))->dimensions)
#define DIM(arr,n) ((((PyArrayObject *)(arr))->dimensions)[(n)])
#define ASARR(arr) ((PyArrayObject *)(arr))
#define ASIM(im) ((PyMImageObject *)(im))
#define ASDI(di) ((PyDrawInfoObject *)(di))
#define STRIDES(arr) (((PyArrayObject *)(arr))->strides)
#define STRIDE(arr,n) ((((PyArrayObject *)(arr))->strides)[(n)])
#define ERRMSG(s) {PyErr_SetString(PyMagickError, (s)); goto fail;}
#define ERRMSG4(st,ind) PyErr_Format(PyMagickError, \
"Undefined %d in %s", ind, st ); \
return NULL
#define STR2PYSTR(str) \
((str) ? PyString_FromString((str)) : PyString_FromString(""))
#define ENUM2STR(str,val,attr) \
if (((val) >= 0) && ((val) < (long) NumberOf((str))-1)) \
return STR2PYSTR((str)[(val)]); \
ERRMSG4((attr), (val))
static ExceptionInfo exception;
#define PyMagickErr(exc) ((exc).severity != UndefinedException)
#define ERR(exc) { \
if ((exc).severity < ErrorException) { \
fprintf(stderr, "Exception %d: %.512s%s%.512s%s", \
(exc).severity, \
((exc).reason ? (exc).reason : "ERROR"), \
((exc).description ? " (" : ""), \
((exc).description ? (exc).description : ""), \
((exc).description ? ")" : "")); \
SetExceptionInfo(&(exc),UndefinedException); \
} \
else { \
PyErr_Format( PyMagickError, \
"Exception %d: %.512s%s%.512s%s", \
(exc).severity, \
((exc).reason ? (exc).reason : "ERROR"), \
((exc).description ? " (" : ""), \
((exc).description ? (exc).description : ""), \
((exc).description ? ")" : "")); \
SetExceptionInfo(&(exc),UndefinedException); \
goto fail; \
} \
}
#define CLEAR_ERR if (PyErr_Occurred()) PyErr_Clear()
#define CHECK_ERR if PyMagickErr(exception) ERR(exception)
#define CHECK_ERR_IM(im) if PyMagickErr((im)->exception) ERR((im)->exception)
#define ThrowImage2Exception(severity,tag,context) \
{ \
(void) ThrowException(exception, severity,tag, context);\
return((Image *) NULL); \
}
#define NumberOf(array) (sizeof((array))/sizeof(*(array)))
staticforward PyTypeObject MImage_Type;
staticforward PyTypeObject DrawInfo_Type;
#define PyMImage_Check(v) ((v)->ob_type == &MImage_Type)
#define PyDrawInfo_Check(v) ((v)->ob_type == &DrawInfo_Type)
#define DRAWALLOCSIZE 10000
typedef struct {
PyObject_HEAD
Image *ims; /* Can be a single image or a linked list of images */
} PyMImageObject;
typedef struct {
PyObject_HEAD
DrawInfo *info;
char *prim;
long alloc;
long len;
} PyDrawInfoObject;
/*
Static declarations from PerlMagick + Additions
*/
static char
*AlignTypes[] =
{
"Undefined", "Left", "Center", "Right", (char *) NULL
}, /*
*BooleanTypes[] =
{
"False", "True", (char *) NULL
}, */
*ChannelTypes[] =
{
"Undefined", "Red", "Cyan", "Green", "Magenta", "Blue", "Yellow",
"Opacity", "Black", "Matte", (char *) NULL
},
*ClassTypes[] =
{
"Undefined", "DirectClass", "PseudoClass", (char *) NULL
},
*ClipPathUnitss[] =
{
"UserSpace", "UserSpaceOnUse", "ObjectBoundingBox", (char *) NULL
},
*ColorspaceTypes[] =
{
"Undefined", "RGB", "Gray", "Transparent", "OHTA", "XYZ", "YCbCr",
"YCC", "YIQ", "YPbPr", "YUV", "CMYK", "sRGB", (char *) NULL
}, /*
*ComplianceTypes[] =
{
"Undefined", "No", "SVG", "X11", "XPM", "All", (char *) NULL
}, */
*CompositeTypes[] =
{
"Undefined", "Over", "In", "Out", "Atop", "Xor", "Plus", "Minus",
"Add", "Subtract", "Difference", "Multiply", "Bumpmap", "Copy",
"CopyRed", "CopyGreen", "CopyBlue", "CopyOpacity", "Clear", "Dissolve",
"Displace", "Modulate", "Threshold", "No", "Darken", "Lighten",
"Hue", "Saturate", "Colorize", "Luminize", "Screen", "Overlay",
"ReplaceMatte", (char *) NULL
},
*CompressionTypes[] =
{
"Undefined", "None", "BZip", "Fax", "Group4", "JPEG", "LosslessJPEG",
"LZW", "RLE", "Zip", (char *) NULL
},
*DisposeTypes[] =
{
"Undefined", "None", "Background", "Previous", (char *) NULL
},
*DecorationTypes[] =
{
"No", "Underline", "Overline", "LineThrough", (char *)NULL
},
*EndianTypes[] =
{
"Undefined", "LSB", "MSB", (char *) NULL
},
*FillRules[] =
{
"Undefined", "EvenOdd", "NonZero", (char *)NULL
},
*FilterTypess[] =
{
"Undefined", "Point", "Box", "Triangle", "Hermite", "Hanning",
"Hamming", "Blackman", "Gaussian", "Quadratic", "Cubic", "Catrom",
"Mitchell", "Lanczos", "Bessel", "Sinc", (char *) NULL
}, /*
*GradientTypes[] =
{
"Undefined", "Linear", "Radial", (char *)NULL
}, */
*GravityTypes[] =
{
"Forget", "NorthWest", "North", "NorthEast", "West", "Center",
"East", "SouthWest", "South", "SouthEast", "Static", (char *) NULL
},
*ImageTypes[] =
{
"Undefined", "Bilevel", "Grayscale", "GrayscaleMatte", "Palette",
"PaletteMatte", "TrueColor", "TrueColorMatte", "ColorSeparation",
"ColorSeparationMatte", "Optimize", (char *) NULL
},
*IntentTypes[] =
{
"Undefined", "Saturation", "Perceptual", "Absolute", "Relative",
(char *) NULL
},
*InterlaceTypes[] =
{
"Undefined", "None", "Line", "Plane", "Partition", (char *) NULL
}, /*
*LogEventTypes[] =
{
"No", "Configure", "Annotate", "Render", "Locale", "Coder",
"X11", "Cache", "Blob", "All", (char *) NULL
}, */
*LineCapTypes[] =
{
"Undefined", "Butt", "Round", "Square", (char *)NULL
},
*LineJoinTypes[] =
{
"Undefined", "Miter", "Round", "Bevel", (char *)NULL
}, /*
*MethodTypes[] =
{
"Point", "Replace", "Floodfill", "FillToBorder", "Reset", (char *) NULL
},
*ModeTypes[] =
{
"Undefined", "Frame", "Unframe", "Concatenate", (char *) NULL
}, */
*NoiseTypes[] =
{
"Uniform", "Gaussian", "Multiplicative", "Impulse", "Laplacian",
"Poisson", (char *) NULL
},
*PreviewTypes[] =
{
"Undefined", "Rotate", "Shear", "Roll", "Hue", "Saturation",
"Brightness", "Gamma", "Spiff", "Dull", "Grayscale", "Quantize",
"Despeckle", "ReduceNoise", "AddNoise", "Sharpen", "Blur",
"Threshold", "EdgeDetect", "Spread", "Solarize", "Shade", "Raise",
"Segment", "Swirl", "Implode", "Wave", "OilPaint", "Charcoal",
"JPEG", (char *) NULL
}, /*
*PrimitiveTypes[] =
{
"Undefined", "point", "line", "rectangle", "roundRectangle", "arc",
"ellipse", "circle", "polyline", "polygon", "bezier", "path", "color",
"matte", "text", "image", (char *) NULL
}, */
*ResolutionTypes[] =
{
"Undefined", "PixelsPerInch", "PixelsPerCentimeter", (char *) NULL
}, /*
*SpreadTypes[] =
{
"Undefined", "PadSpread", "Reflect", "Repeat", (char *)NULL
}, */
*StretchTypes[] =
{
"Normal", "UltraCondensed", "ExtraCondensed", "Condensed",
"SemiCondensed", "SemiExpanded", "Expanded", "ExtraExpanded",
"UltraExpanded", "Any", (char *) NULL
},
*StyleTypes[] =
{
"Normal", "Italic", "Oblique", "Any", (char *) NULL
},
*VirtualPixelMethods[] =
{
"Undefined", "", "Constant", "Edge", "Mirror", "Tile",
(char *) NULL
};
#define strEQ(str1, str2) (!strcmp((str1),(str2)))
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% s t r E Q c a s e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method strEQcase compares two strings and returns 0 if they are the
% same or if the second string runs out first. The comparison is case
% insensitive.
%
% The format of the strEQcase routine is:
%
% int strEQcase(const char *p,const char *q)
%
% A description of each parameter follows:
%
% o status: Method strEQcase returns zero if strings p and q are the
% same or if the second string runs out first.
%
% o p: a character string.
%
% o q: a character string.
%
%
*/
# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
static int strEQcase(const char *p,const char *q)
{
char
c;
register int
i;
for (i=0 ; (c=(*q)) != 0; i++)
{
if ((isUPPER(c) ? toLOWER(c) : c) != (isUPPER(*p) ? toLOWER(*p) : *p))
return(0);
p++;
q++;
}
return(i);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% L o o k u p S t r %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method LookupStr searches through a list of strings matching it to string
% and return its index in the list, or -1 for not found .
%
% The format of the LookupStr routine is:
%
% int LookupStr(char **list,const char *string)
%
% A description of each parameter follows:
%
% o status: Method LookupStr returns the index of string in the list
% otherwise -1.
%
% o list: a list of strings.
%
% o string: a character string.
%
%
*/
static int LookupStr(char **list,const char *string)
{
int
longest,
offset;
register char
**p;
offset=(-1);
longest=0;
for (p=list; *p; p++)
if (strEQcase(string,*p) > longest)
{
offset=p-list;
longest=strEQcase(string,*p);
}
return(offset);
}
/* Forward declarations */
static int mimage_setattr(PyMImageObject *, char *, PyObject *);
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% P y M a g i c k E r r o r H a n d l e r %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Method PyMagickErrorHandler replaces ImageMagick's fatal error handler.
% This stores the message in a Python variable,and longjmp's to return the
% error. Note that this doesn't exit but returns control to Python;
%
% The format of the PyMagickErrorHandler routine is:
%
% PyMagickErrorHandler(const ExceptionType severity,const char *reason,
% const char *qualifier)
%
% A description of each parameter follows:
%
% o severity: The severity of the exception.
%
% o reason: The reason of the exception.
%
% o description: The exception description.
%
%
*/
static void PyMagickErrorHandler(const ExceptionType severity,
const char *reason,
const char *description)
{
char
text[MaxTextExtent];
FormatString(text,"Exception %d: %.512s%s%.512s%s",severity,
reason ? reason : "ERROR",
description ? " (" : "",
description ? description : "",
description ? ")" : "");
PyErr_SetString(PyMagickError, text);
longjmp(error_jmp, (int) severity);
}
static StorageType
arraytype_to_storagetype(int type_num)
{
switch(type_num) {
case PyArray_CHAR:
case PyArray_UBYTE:
return CharPixel;
case PyArray_USHORT:
return ShortPixel;
case PyArray_UINT:
case PyArray_LONG:
return IntegerPixel;
case PyArray_FLOAT:
return FloatPixel;
case PyArray_DOUBLE:
return DoublePixel;
}
return CharPixel;
}
static int
ConstitutePaletteColormap(Image *image, const char *colorspace,
StorageType ctype, const void *cmap,
const unsigned long colors)
{
register long x,i;
size_t length, clen;
PixelPacket *q;
assert(image != (Image *) NULL);
assert(image->signature == MagickSignature);
if (colors > MaxColormapSize) {
ThrowBinaryException(OptionError,"UnableToConstitutePalette",
"Selected Palette too large.");
}
image->storage_class=PseudoClass;
image->colors=colors;
length=image->colors*sizeof(PixelPacket);
if (image->colormap != (PixelPacket *) NULL)
MagickRealloc((void **) &image->colormap, length);
else
image->colormap=(PixelPacket *) MagickMalloc(length);
if (image->colormap == (PixelPacket *) NULL)
return(False);
/* Pre-initialize to Opaque */
for (x=0; x < (long) image->colors; x++) {
image->colormap[x].opacity = OpaqueOpacity;
}
clen = strlen(colorspace);
image->colorspace=RGBColorspace;
for (i=0; i < (long) clen; i++) {
switch (colorspace[i])
{
case 'a':
case 'A':
image->matte=True;
break;
case 'c':
case 'C':
case 'm':
case 'M':
case 'y':
case 'Y':
case 'k':
case 'K':
image->colorspace=CMYKColorspace;
break;
default:
break;
}
}
switch (ctype)
{
case CharPixel:
{
register unsigned char *p;
p = (unsigned char *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = ScaleCharToQuantum(*p++);
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = ScaleCharToQuantum(*p++);
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = ScaleCharToQuantum(*p++);
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = ScaleCharToQuantum(*p++);
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
case ShortPixel:
{
register unsigned short *p;
p = (unsigned short *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = ScaleShortToQuantum(*p++);
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = ScaleShortToQuantum(*p++);
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = ScaleShortToQuantum(*p++);
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = ScaleShortToQuantum(*p++);
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
case IntegerPixel:
{
register unsigned int *p;
p = (unsigned int *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = ScaleLongToQuantum(*p++);
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = ScaleLongToQuantum(*p++);
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = ScaleLongToQuantum(*p++);
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = ScaleLongToQuantum(*p++);
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
case LongPixel:
{
register unsigned long *p;
p = (unsigned long *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = ScaleLongToQuantum(*p++);
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = ScaleLongToQuantum(*p++);
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = ScaleLongToQuantum(*p++);
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = ScaleLongToQuantum(*p++);
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
case FloatPixel:
{
register float *p;
p = (float *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = (Quantum) ((float) MaxRGB*(*p++));
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = (Quantum) ((float) MaxRGB*(*p++));
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = (Quantum) ((float) MaxRGB*(*p++));
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = (Quantum) ((float) MaxRGB*(*p++));
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
case DoublePixel:
{
register double *p;
p = (double *) cmap;
q = image->colormap;
for (x=0; x < (long) image->colors; x++) {
for (i=0; i< (long) clen; i++) {
switch(colorspace[i])
{
case 'r':
case 'R':
case 'c':
case 'C':
q->red = (Quantum) ((double) MaxRGB*(*p++));
break;
case 'g':
case 'G':
case 'm':
case 'M':
q->green = (Quantum) ((double) MaxRGB*(*p++));
break;
case 'b':
case 'B':
case 'y':
case 'Y':
q->blue = (Quantum) ((double) MaxRGB*(*p++));
break;
case 'a':
case 'A':
case 'k':
case 'K':
q->opacity = (Quantum) ((double) MaxRGB*(*p++));
break;
default:
MagickFree(image->colormap);
return(False);
}
}
q++;
}
break;
}
default:
MagickFree(image->colormap);
return(False);
}
return(True);
}
#define ScaleCharToRange(val,N) ((Quantum) ((val) * (N) / 256UL))
#define ScaleShortToRange(val,N) ((Quantum) ((val) * (N) / 65536UL))
#define ScaleIntToRange(val,N) ((Quantum) (((val) != 429467295UL) ? ((val) * (N) / 4294967295UL) : N-1 ))
#define ScaleLongToRange(val,N) ((Quantum) (((val) != 429467295UL) ? ((val) * (N) / 4294967295UL) : N-1))
static Image*
ConstitutePaletteImage(const unsigned long width,
const unsigned long height,
const StorageType type,
const void *pixels,
const char *colorspace,
const StorageType ctype,
const void *cmap,
const unsigned long colors,
ExceptionInfo *exception)
{
Image
*image;
long
y, N;
PixelPacket
*q;
register IndexPacket
*indexes;
register long
x;
/*
Allocate image structure.
*/
assert(pixels != (void *) NULL);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
SetExceptionInfo(exception,UndefinedException);
image=AllocateImage((ImageInfo *) NULL);
if (image == (Image *) NULL)
return((Image *) NULL);
if ((width == 0) || (height == 0))
ThrowImage2Exception(OptionError,"UnableToConstituteImage",
"NonzeroWidthAndHeightRequired");
image->columns=width;
image->rows=height;
if (!ConstitutePaletteColormap(image,colorspace,ctype,cmap,colors))
ThrowImage2Exception(ResourceLimitError,"MemoryAllocationFailed",
"UnableToConstituteImage");
N = image->colors;
/* What to do if value in pixels surpasses size of colormap?
Scale the pixel value range (as defined by the type)
to the colormap size range.
*/
switch (type)
{
case CharPixel:
{
register unsigned char
*p;
p=(unsigned char *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=ScaleCharToRange(*p++,N);
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
case ShortPixel:
{
register unsigned short
*p;
p=(unsigned short *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=ScaleShortToRange(*p++,N);
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
case IntegerPixel:
{
register unsigned int
*p;
p=(unsigned int *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=ScaleIntToRange(*p,N); p++;
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
case LongPixel:
{
register unsigned long
*p;
p=(unsigned long *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=ScaleLongToRange(*p,N); p++;
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
case FloatPixel:
{
register float
*p;
p=(float *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=(Quantum) ((float) (N-1)*(*p++));
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
case DoublePixel:
{
register double
*p;
p=(double *) pixels;
for (y=0; y < (long) image->rows; y++)
{
q=SetImagePixels(image,0,y,image->columns,1);
if (q == (PixelPacket *) NULL)
break;
indexes=GetIndexes(image);
for (x=0; x < (long) image->columns; x++)
{
indexes[x]=(Quantum) ((double) (N-1)*(*p++));
q->red=image->colormap[indexes[x]].red;
q->green=image->colormap[indexes[x]].green;
q->blue=image->colormap[indexes[x]].blue;
q->opacity=image->colormap[indexes[x]].opacity;
q++;
}
if (!SyncImagePixels(image))
break;
}
break;
}
default:
{
DestroyImage(image);
ThrowImage2Exception(OptionError,"UnrecognizedPixelMap", colorspace)
}
}