]> granicus.if.org Git - imagemagick/blob - MagickCore/quantum-import.c
Add RobidouxSharp filter depreciate Bessel Filter and Static Gravity
[imagemagick] / MagickCore / quantum-import.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                QQQ   U   U   AAA   N   N  TTTTT  U   U  M   M               %
7 %               Q   Q  U   U  A   A  NN  N    T    U   U  MM MM               %
8 %               Q   Q  U   U  AAAAA  N N N    T    U   U  M M M               %
9 %               Q  QQ  U   U  A   A  N  NN    T    U   U  M   M               %
10 %                QQQQ   UUU   A   A  N   N    T     UUU   M   M               %
11 %                                                                             %
12 %                   IIIII  M   M  PPPP    OOO   RRRR   TTTTT                  %
13 %                     I    MM MM  P   P  O   O  R   R    T                    %
14 %                     I    M M M  PPPP   O   O  RRRR     T                    %
15 %                     I    M   M  P      O   O  R R      T                    %
16 %                   IIIII  M   M  P       OOO   R  R     T                    %
17 %                                                                             %
18 %                 MagickCore Methods to Import Quantum Pixels                 %
19 %                                                                             %
20 %                             Software Design                                 %
21 %                               John Cristy                                   %
22 %                               October 1998                                  %
23 %                                                                             %
24 %                                                                             %
25 %  Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization      %
26 %  dedicated to making software imaging solutions freely available.           %
27 %                                                                             %
28 %  You may not use this file except in compliance with the License.  You may  %
29 %  obtain a copy of the License at                                            %
30 %                                                                             %
31 %    http://www.imagemagick.org/script/license.php                            %
32 %                                                                             %
33 %  Unless required by applicable law or agreed to in writing, software        %
34 %  distributed under the License is distributed on an "AS IS" BASIS,          %
35 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
36 %  See the License for the specific language governing permissions and        %
37 %  limitations under the License.                                             %
38 %                                                                             %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %
41 %
42 */
43 \f
44 /*
45   Include declarations.
46 */
47 #include "MagickCore/studio.h"
48 #include "MagickCore/property.h"
49 #include "MagickCore/blob.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/color-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/cache.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/delegate.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/quantum.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/semaphore.h"
69 #include "MagickCore/statistic.h"
70 #include "MagickCore/stream.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/utility.h"
73 \f
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %   I m p o r t Q u a n t u m P i x e l s                                     %
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
85 %  ImportQuantumPixels() transfers one or more pixel components from a user
86 %  supplied buffer into the image pixel cache of an image.  The pixels are
87 %  expected in network byte order.  It returns MagickTrue if the pixels are
88 %  successfully transferred, otherwise MagickFalse.
89 %
90 %  The format of the ImportQuantumPixels method is:
91 %
92 %      size_t ImportQuantumPixels(const Image *image,CacheView *image_view,
93 %        QuantumInfo *quantum_info,const QuantumType quantum_type,
94 %        const unsigned char *pixels,ExceptionInfo *exception)
95 %
96 %  A description of each parameter follows:
97 %
98 %    o image: the image.
99 %
100 %    o image_view: the image cache view.
101 %
102 %    o quantum_info: the quantum info.
103 %
104 %    o quantum_type: Declare which pixel components to transfer (red, green,
105 %      blue, opacity, RGB, or RGBA).
106 %
107 %    o pixels:  The pixel components are transferred from this buffer.
108 %
109 %    o exception: return any errors or warnings in this structure.
110 %
111 */
112
113 static inline Quantum PushColormapIndex(const Image *image,const size_t index,
114   MagickBooleanType *range_exception)
115 {
116   if (index < image->colors)
117     return((Quantum) index);
118   *range_exception=MagickTrue;
119   return((Quantum) 0);
120 }
121
122 static inline const unsigned char *PushDoublePixel(QuantumInfo *quantum_info,
123   const unsigned char *pixels,double *pixel)
124 {
125   double
126     *p;
127
128   unsigned char
129     quantum[8];
130
131   if (quantum_info->endian != LSBEndian)
132     {
133       quantum[7]=(*pixels++);
134       quantum[6]=(*pixels++);
135       quantum[5]=(*pixels++);
136       quantum[5]=(*pixels++);
137       quantum[3]=(*pixels++);
138       quantum[2]=(*pixels++);
139       quantum[1]=(*pixels++);
140       quantum[0]=(*pixels++);
141       p=(double *) quantum;
142       *pixel=(*p);
143       *pixel-=quantum_info->minimum;
144       *pixel*=quantum_info->scale;
145       return(pixels);
146     }
147   quantum[0]=(*pixels++);
148   quantum[1]=(*pixels++);
149   quantum[2]=(*pixels++);
150   quantum[3]=(*pixels++);
151   quantum[4]=(*pixels++);
152   quantum[5]=(*pixels++);
153   quantum[6]=(*pixels++);
154   quantum[7]=(*pixels++);
155   p=(double *) quantum;
156   *pixel=(*p);
157   *pixel-=quantum_info->minimum;
158   *pixel*=quantum_info->scale;
159   return(pixels);
160 }
161
162 static inline const unsigned char *PushFloatPixel(QuantumInfo *quantum_info,
163   const unsigned char *pixels,float *pixel)
164 {
165   float
166     *p;
167
168   unsigned char
169     quantum[4];
170
171   if (quantum_info->endian != LSBEndian)
172     {
173       quantum[3]=(*pixels++);
174       quantum[2]=(*pixels++);
175       quantum[1]=(*pixels++);
176       quantum[0]=(*pixels++);
177       p=(float *) quantum;
178       *pixel=(*p);
179       *pixel-=quantum_info->minimum;
180       *pixel*=quantum_info->scale;
181       return(pixels);
182     }
183   quantum[0]=(*pixels++);
184   quantum[1]=(*pixels++);
185   quantum[2]=(*pixels++);
186   quantum[3]=(*pixels++);
187   p=(float *) quantum;
188   *pixel=(*p);
189   *pixel-=quantum_info->minimum;
190   *pixel*=quantum_info->scale;
191   return(pixels);
192 }
193
194 static inline const unsigned char *PushQuantumPixel(QuantumInfo *quantum_info,
195   const unsigned char *pixels,unsigned int *quantum)
196 {
197   register ssize_t
198     i;
199
200   register size_t
201     quantum_bits;
202
203   *quantum=(QuantumAny) 0;
204   for (i=(ssize_t) quantum_info->depth; i > 0L; )
205   {
206     if (quantum_info->state.bits == 0UL)
207       {
208         quantum_info->state.pixel=(*pixels++);
209         quantum_info->state.bits=8UL;
210       }
211     quantum_bits=(size_t) i;
212     if (quantum_bits > quantum_info->state.bits)
213       quantum_bits=quantum_info->state.bits;
214     i-=(ssize_t) quantum_bits;
215     quantum_info->state.bits-=quantum_bits;
216     *quantum=(unsigned int) ((*quantum << quantum_bits) |
217       ((quantum_info->state.pixel >> quantum_info->state.bits) &~ ((~0UL) <<
218       quantum_bits)));
219   }
220   return(pixels);
221 }
222
223 static inline const unsigned char *PushQuantumLongPixel(
224   QuantumInfo *quantum_info,const unsigned char *pixels,unsigned int *quantum)
225 {
226   register ssize_t
227     i;
228
229   register size_t
230     quantum_bits;
231
232   *quantum=0UL;
233   for (i=(ssize_t) quantum_info->depth; i > 0; )
234   {
235     if (quantum_info->state.bits == 0)
236       {
237         pixels=PushLongPixel(quantum_info->endian,pixels,
238           &quantum_info->state.pixel);
239         quantum_info->state.bits=32U;
240       }
241     quantum_bits=(size_t) i;
242     if (quantum_bits > quantum_info->state.bits)
243       quantum_bits=quantum_info->state.bits;
244     *quantum|=(((quantum_info->state.pixel >> (32U-quantum_info->state.bits)) &
245       quantum_info->state.mask[quantum_bits]) << (quantum_info->depth-i));
246     i-=(ssize_t) quantum_bits;
247     quantum_info->state.bits-=quantum_bits;
248   }
249   return(pixels);
250 }
251
252 static void ImportAlphaQuantum(const Image *image,QuantumInfo *quantum_info,
253   const MagickSizeType number_pixels,const unsigned char *restrict p,
254   Quantum *restrict q,ExceptionInfo *exception)
255 {
256   QuantumAny
257     range;
258
259   register ssize_t
260     x;
261
262   unsigned int
263     pixel;
264
265   switch (quantum_info->depth)
266   {
267     case 8:
268     {
269       unsigned char
270         pixel;
271
272       for (x=0; x < (ssize_t) number_pixels; x++)
273       {
274         p=PushCharPixel(p,&pixel);
275         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
276         p+=quantum_info->pad;
277         q+=GetPixelChannels(image);
278       }
279       break;
280     }
281     case 16:
282     {
283       unsigned short
284         pixel;
285
286       if (quantum_info->format == FloatingPointQuantumFormat)
287         {
288           for (x=0; x < (ssize_t) number_pixels; x++)
289           {
290             p=PushShortPixel(quantum_info->endian,p,&pixel);
291             SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
292               HalfToSinglePrecision(pixel)),q);
293             p+=quantum_info->pad;
294             q+=GetPixelChannels(image);
295           }
296           break;
297         }
298       for (x=0; x < (ssize_t) number_pixels; x++)
299       {
300         p=PushShortPixel(quantum_info->endian,p,&pixel);
301         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
302         p+=quantum_info->pad;
303         q+=GetPixelChannels(image);
304       }
305       break;
306     }
307     case 32:
308     {
309       unsigned int
310         pixel;
311
312       if (quantum_info->format == FloatingPointQuantumFormat)
313         {
314           float
315             pixel;
316
317           for (x=0; x < (ssize_t) number_pixels; x++)
318           {
319             p=PushFloatPixel(quantum_info,p,&pixel);
320             SetPixelAlpha(image,ClampToQuantum(pixel),q);
321             p+=quantum_info->pad;
322             q+=GetPixelChannels(image);
323           }
324           break;
325         }
326       for (x=0; x < (ssize_t) number_pixels; x++)
327       {
328         p=PushLongPixel(quantum_info->endian,p,&pixel);
329         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
330         p+=quantum_info->pad;
331         q+=GetPixelChannels(image);
332       }
333       break;
334     }
335     case 64:
336     {
337       if (quantum_info->format == FloatingPointQuantumFormat)
338         {
339           double
340             pixel;
341
342           for (x=0; x < (ssize_t) number_pixels; x++)
343           {
344             p=PushDoublePixel(quantum_info,p,&pixel);
345             SetPixelAlpha(image,ClampToQuantum(pixel),q);
346             p+=quantum_info->pad;
347             q+=GetPixelChannels(image);
348           }
349           break;
350         }
351     }
352     default:
353     {
354       range=GetQuantumRange(quantum_info->depth);
355       for (x=0; x < (ssize_t) number_pixels; x++)
356       {
357         p=PushQuantumPixel(quantum_info,p,&pixel);
358         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
359         p+=quantum_info->pad;
360         q+=GetPixelChannels(image);
361       }
362       break;
363     }
364   }
365 }
366
367 static void ImportBGRQuantum(const Image *image,QuantumInfo *quantum_info,
368   const MagickSizeType number_pixels,const unsigned char *restrict p,
369   Quantum *restrict q,ExceptionInfo *exception)
370 {
371   QuantumAny
372     range;
373
374   register ssize_t
375     x;
376
377   ssize_t
378     bit;
379
380   unsigned int
381     pixel;
382
383   switch (quantum_info->depth)
384   {
385     case 8:
386     {
387       unsigned char
388         pixel;
389
390       for (x=0; x < (ssize_t) number_pixels; x++)
391       {
392         p=PushCharPixel(p,&pixel);
393         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
394         p=PushCharPixel(p,&pixel);
395         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
396         p=PushCharPixel(p,&pixel);
397         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
398         SetPixelAlpha(image,OpaqueAlpha,q);
399         p+=quantum_info->pad;
400         q+=GetPixelChannels(image);
401       }
402       break;
403     }
404     case 10:
405     {
406       range=GetQuantumRange(quantum_info->depth);
407       if (quantum_info->pack == MagickFalse)
408         {
409           for (x=0; x < (ssize_t) number_pixels; x++)
410           {
411             p=PushLongPixel(quantum_info->endian,p,&pixel);
412             SetPixelRed(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,range),q);
413             SetPixelGreen(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
414               q);
415             SetPixelBlue(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,range),q);
416             p+=quantum_info->pad;
417             q+=GetPixelChannels(image);
418           }
419           break;
420         }
421       if (quantum_info->quantum == 32U)
422         {
423           for (x=0; x < (ssize_t) number_pixels; x++)
424           {
425             p=PushQuantumLongPixel(quantum_info,p,&pixel);
426             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
427             p=PushQuantumLongPixel(quantum_info,p,&pixel);
428             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
429             p=PushQuantumLongPixel(quantum_info,p,&pixel);
430             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
431             q+=GetPixelChannels(image);
432           }
433           break;
434         }
435       for (x=0; x < (ssize_t) number_pixels; x++)
436       {
437         p=PushQuantumPixel(quantum_info,p,&pixel);
438         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
439         p=PushQuantumPixel(quantum_info,p,&pixel);
440         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
441         p=PushQuantumPixel(quantum_info,p,&pixel);
442         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
443         q+=GetPixelChannels(image);
444       }
445       break;
446     }
447     case 12:
448     {
449       range=GetQuantumRange(quantum_info->depth);
450       if (quantum_info->pack == MagickFalse)
451         {
452           unsigned short
453             pixel;
454
455           for (x=0; x < (ssize_t) (3*number_pixels-1); x+=2)
456           {
457             p=PushShortPixel(quantum_info->endian,p,&pixel);
458             switch (x % 3)
459             {
460               default:
461               case 0:
462               {
463                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
464                   range),q);
465                 break;
466               }
467               case 1:
468               {
469                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
470                   range),q);
471                 break;
472               }
473               case 2:
474               {
475                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
476                   range),q);
477                 q+=GetPixelChannels(image);
478                 break;
479               }
480             }
481             p=PushShortPixel(quantum_info->endian,p,&pixel);
482             switch ((x+1) % 3)
483             {
484               default:
485               case 0:
486               {
487                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
488                   range),q);
489                 break;
490               }
491               case 1:
492               {
493                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
494                   range),q);
495                 break;
496               }
497               case 2:
498               {
499                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
500                   range),q);
501                 q+=GetPixelChannels(image);
502                 break;
503               }
504             }
505             p+=quantum_info->pad;
506           }
507           for (bit=0; bit < (ssize_t) (3*number_pixels % 2); bit++)
508           {
509             p=PushShortPixel(quantum_info->endian,p,&pixel);
510             switch ((x+bit) % 3)
511             {
512               default:
513               case 0:
514               {
515                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
516                   range),q);
517                 break;
518               }
519               case 1:
520               {
521                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
522                   range),q);
523                 break;
524               }
525               case 2:
526               {
527                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
528                   range),q);
529                 q+=GetPixelChannels(image);
530                 break;
531               }
532             }
533             p+=quantum_info->pad;
534           }
535           if (bit != 0)
536             p++;
537           break;
538         }
539       if (quantum_info->quantum == 32U)
540         {
541           for (x=0; x < (ssize_t) number_pixels; x++)
542           {
543             p=PushQuantumLongPixel(quantum_info,p,&pixel);
544             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
545             p=PushQuantumLongPixel(quantum_info,p,&pixel);
546             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
547             p=PushQuantumLongPixel(quantum_info,p,&pixel);
548             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
549             q+=GetPixelChannels(image);
550           }
551           break;
552         }
553       for (x=0; x < (ssize_t) number_pixels; x++)
554       {
555         p=PushQuantumPixel(quantum_info,p,&pixel);
556         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
557         p=PushQuantumPixel(quantum_info,p,&pixel);
558         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
559         p=PushQuantumPixel(quantum_info,p,&pixel);
560         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
561         q+=GetPixelChannels(image);
562       }
563       break;
564     }
565     case 16:
566     {
567       unsigned short
568         pixel;
569
570       if (quantum_info->format == FloatingPointQuantumFormat)
571         {
572           for (x=0; x < (ssize_t) number_pixels; x++)
573           {
574             p=PushShortPixel(quantum_info->endian,p,&pixel);
575             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
576               HalfToSinglePrecision(pixel)),q);
577             p=PushShortPixel(quantum_info->endian,p,&pixel);
578             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
579               HalfToSinglePrecision(pixel)),q);
580             p=PushShortPixel(quantum_info->endian,p,&pixel);
581             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
582               HalfToSinglePrecision(pixel)),q);
583             p+=quantum_info->pad;
584             q+=GetPixelChannels(image);
585           }
586           break;
587         }
588       for (x=0; x < (ssize_t) number_pixels; x++)
589       {
590         p=PushShortPixel(quantum_info->endian,p,&pixel);
591         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
592         p=PushShortPixel(quantum_info->endian,p,&pixel);
593         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
594         p=PushShortPixel(quantum_info->endian,p,&pixel);
595         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
596         p+=quantum_info->pad;
597         q+=GetPixelChannels(image);
598       }
599       break;
600     }
601     case 32:
602     {
603       unsigned int
604         pixel;
605
606       if (quantum_info->format == FloatingPointQuantumFormat)
607         {
608           float
609             pixel;
610
611           for (x=0; x < (ssize_t) number_pixels; x++)
612           {
613             p=PushFloatPixel(quantum_info,p,&pixel);
614             SetPixelRed(image,ClampToQuantum(pixel),q);
615             p=PushFloatPixel(quantum_info,p,&pixel);
616             SetPixelGreen(image,ClampToQuantum(pixel),q);
617             p=PushFloatPixel(quantum_info,p,&pixel);
618             SetPixelBlue(image,ClampToQuantum(pixel),q);
619             p+=quantum_info->pad;
620             q+=GetPixelChannels(image);
621           }
622           break;
623         }
624       for (x=0; x < (ssize_t) number_pixels; x++)
625       {
626         p=PushLongPixel(quantum_info->endian,p,&pixel);
627         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
628         p=PushLongPixel(quantum_info->endian,p,&pixel);
629         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
630         p=PushLongPixel(quantum_info->endian,p,&pixel);
631         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
632         p+=quantum_info->pad;
633         q+=GetPixelChannels(image);
634       }
635       break;
636     }
637     case 64:
638     {
639       if (quantum_info->format == FloatingPointQuantumFormat)
640         {
641           double
642             pixel;
643
644           for (x=0; x < (ssize_t) number_pixels; x++)
645           {
646             p=PushDoublePixel(quantum_info,p,&pixel);
647             SetPixelRed(image,ClampToQuantum(pixel),q);
648             p=PushDoublePixel(quantum_info,p,&pixel);
649             SetPixelGreen(image,ClampToQuantum(pixel),q);
650             p=PushDoublePixel(quantum_info,p,&pixel);
651             SetPixelBlue(image,ClampToQuantum(pixel),q);
652             p+=quantum_info->pad;
653             q+=GetPixelChannels(image);
654           }
655           break;
656         }
657     }
658     default:
659     {
660       range=GetQuantumRange(quantum_info->depth);
661       for (x=0; x < (ssize_t) number_pixels; x++)
662       {
663         p=PushQuantumPixel(quantum_info,p,&pixel);
664         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
665         p=PushQuantumPixel(quantum_info,p,&pixel);
666         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
667         p=PushQuantumPixel(quantum_info,p,&pixel);
668         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
669         q+=GetPixelChannels(image);
670       }
671       break;
672     }
673   }
674 }
675
676 static void ImportBGRAQuantum(const Image *image,QuantumInfo *quantum_info,
677   const MagickSizeType number_pixels,const unsigned char *restrict p,
678   Quantum *restrict q,ExceptionInfo *exception)
679 {
680   QuantumAny
681     range;
682
683   register ssize_t
684     x;
685
686   unsigned int
687     pixel;
688
689   switch (quantum_info->depth)
690   {
691     case 8:
692     {
693       unsigned char
694         pixel;
695
696       for (x=0; x < (ssize_t) number_pixels; x++)
697       {
698         p=PushCharPixel(p,&pixel);
699         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
700         p=PushCharPixel(p,&pixel);
701         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
702         p=PushCharPixel(p,&pixel);
703         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
704         p=PushCharPixel(p,&pixel);
705         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
706         p+=quantum_info->pad;
707         q+=GetPixelChannels(image);
708       }
709       break;
710     }
711     case 10:
712     {
713       pixel=0;
714       if (quantum_info->pack == MagickFalse)
715         {
716           register ssize_t
717             i;
718
719           size_t
720             quantum;
721
722           ssize_t
723             n;
724
725           n=0;
726           quantum=0;
727           for (x=0; x < (ssize_t) number_pixels; x++)
728           {
729             for (i=0; i < 4; i++)
730             {
731               switch (n % 3)
732               {
733                 case 0:
734                 {
735                   p=PushLongPixel(quantum_info->endian,p,&pixel);
736                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
737                     (((pixel >> 22) & 0x3ff) << 6)));
738                   break;
739                 }
740                 case 1:
741                 {
742                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
743                     (((pixel >> 12) & 0x3ff) << 6)));
744                   break;
745                 }
746                 case 2:
747                 {
748                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
749                     (((pixel >> 2) & 0x3ff) << 6)));
750                   break;
751                 }
752               }
753               switch (i)
754               {
755                 case 0: SetPixelRed(image,(Quantum) quantum,q); break;
756                 case 1: SetPixelGreen(image,(Quantum) quantum,q); break;
757                 case 2: SetPixelBlue(image,(Quantum) quantum,q); break;
758                 case 3: SetPixelAlpha(image,(Quantum) quantum,q); break;
759               }
760               n++;
761             }
762             p+=quantum_info->pad;
763             q+=GetPixelChannels(image);
764           }
765           break;
766         }
767       for (x=0; x < (ssize_t) number_pixels; x++)
768       {
769         p=PushQuantumPixel(quantum_info,p,&pixel);
770         SetPixelRed(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),q);
771         p=PushQuantumPixel(quantum_info,p,&pixel);
772         SetPixelGreen(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
773           q);
774         p=PushQuantumPixel(quantum_info,p,&pixel);
775         SetPixelBlue(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
776           q);
777         p=PushQuantumPixel(quantum_info,p,&pixel);
778         SetPixelAlpha(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
779           q);
780         q+=GetPixelChannels(image);
781       }
782       break;
783     }
784     case 16:
785     {
786       unsigned short
787         pixel;
788
789       if (quantum_info->format == FloatingPointQuantumFormat)
790         {
791           for (x=0; x < (ssize_t) number_pixels; x++)
792           {
793             p=PushShortPixel(quantum_info->endian,p,&pixel);
794             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
795               HalfToSinglePrecision(pixel)),q);
796             p=PushShortPixel(quantum_info->endian,p,&pixel);
797             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
798               HalfToSinglePrecision(pixel)),q);
799             p=PushShortPixel(quantum_info->endian,p,&pixel);
800             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
801               HalfToSinglePrecision(pixel)),q);
802             p=PushShortPixel(quantum_info->endian,p,&pixel);
803             SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
804               HalfToSinglePrecision(pixel)),q);
805             p+=quantum_info->pad;
806             q+=GetPixelChannels(image);
807           }
808           break;
809         }
810       for (x=0; x < (ssize_t) number_pixels; x++)
811       {
812         p=PushShortPixel(quantum_info->endian,p,&pixel);
813         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
814         p=PushShortPixel(quantum_info->endian,p,&pixel);
815         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
816         p=PushShortPixel(quantum_info->endian,p,&pixel);
817         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
818         p=PushShortPixel(quantum_info->endian,p,&pixel);
819         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
820         p+=quantum_info->pad;
821         q+=GetPixelChannels(image);
822       }
823       break;
824     }
825     case 32:
826     {
827       unsigned int
828         pixel;
829
830       if (quantum_info->format == FloatingPointQuantumFormat)
831         {
832           float
833             pixel;
834
835           for (x=0; x < (ssize_t) number_pixels; x++)
836           {
837             p=PushFloatPixel(quantum_info,p,&pixel);
838             SetPixelRed(image,ClampToQuantum(pixel),q);
839             p=PushFloatPixel(quantum_info,p,&pixel);
840             SetPixelGreen(image,ClampToQuantum(pixel),q);
841             p=PushFloatPixel(quantum_info,p,&pixel);
842             SetPixelBlue(image,ClampToQuantum(pixel),q);
843             p=PushFloatPixel(quantum_info,p,&pixel);
844             SetPixelAlpha(image,ClampToQuantum(pixel),q);
845             p+=quantum_info->pad;
846             q+=GetPixelChannels(image);
847           }
848           break;
849         }
850       for (x=0; x < (ssize_t) number_pixels; x++)
851       {
852         p=PushLongPixel(quantum_info->endian,p,&pixel);
853         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
854         p=PushLongPixel(quantum_info->endian,p,&pixel);
855         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
856         p=PushLongPixel(quantum_info->endian,p,&pixel);
857         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
858         p=PushLongPixel(quantum_info->endian,p,&pixel);
859         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
860         p+=quantum_info->pad;
861         q+=GetPixelChannels(image);
862       }
863       break;
864     }
865     case 64:
866     {
867       if (quantum_info->format == FloatingPointQuantumFormat)
868         {
869           double
870             pixel;
871
872           for (x=0; x < (ssize_t) number_pixels; x++)
873           {
874             p=PushDoublePixel(quantum_info,p,&pixel);
875             SetPixelRed(image,ClampToQuantum(pixel),q);
876             p=PushDoublePixel(quantum_info,p,&pixel);
877             SetPixelGreen(image,ClampToQuantum(pixel),q);
878             p=PushDoublePixel(quantum_info,p,&pixel);
879             SetPixelBlue(image,ClampToQuantum(pixel),q);
880             p=PushDoublePixel(quantum_info,p,&pixel);
881             SetPixelAlpha(image,ClampToQuantum(pixel),q);
882             p+=quantum_info->pad;
883             q+=GetPixelChannels(image);
884           }
885           break;
886         }
887     }
888     default:
889     {
890       range=GetQuantumRange(quantum_info->depth);
891       for (x=0; x < (ssize_t) number_pixels; x++)
892       {
893         p=PushQuantumPixel(quantum_info,p,&pixel);
894         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
895         p=PushQuantumPixel(quantum_info,p,&pixel);
896         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
897         p=PushQuantumPixel(quantum_info,p,&pixel);
898         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
899         p=PushQuantumPixel(quantum_info,p,&pixel);
900         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
901         q+=GetPixelChannels(image);
902       }
903       break;
904     }
905   }
906 }
907
908 static void ImportBlackQuantum(const Image *image,QuantumInfo *quantum_info,
909   const MagickSizeType number_pixels,const unsigned char *restrict p,
910   Quantum *restrict q,ExceptionInfo *exception)
911 {
912   QuantumAny
913     range;
914
915   register ssize_t
916     x;
917
918   unsigned int
919     pixel;
920
921   if (image->colorspace != CMYKColorspace)
922     {
923       (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
924         "ColorSeparatedImageRequired","'%s'",image->filename);
925       return;
926     }
927   switch (quantum_info->depth)
928   {
929     case 8:
930     {
931       unsigned char
932         pixel;
933
934       for (x=0; x < (ssize_t) number_pixels; x++)
935       {
936         p=PushCharPixel(p,&pixel);
937         SetPixelBlack(image,ScaleCharToQuantum(pixel),q);
938         p+=quantum_info->pad;
939         q+=GetPixelChannels(image);
940       }
941       break;
942     }
943     case 16:
944     {
945       unsigned short
946         pixel;
947
948       if (quantum_info->format == FloatingPointQuantumFormat)
949         {
950           for (x=0; x < (ssize_t) number_pixels; x++)
951           {
952             p=PushShortPixel(quantum_info->endian,p,&pixel);
953             SetPixelBlack(image,ClampToQuantum((MagickRealType) QuantumRange*
954               HalfToSinglePrecision(pixel)),q);
955             p+=quantum_info->pad;
956             q+=GetPixelChannels(image);
957           }
958           break;
959         }
960       for (x=0; x < (ssize_t) number_pixels; x++)
961       {
962         p=PushShortPixel(quantum_info->endian,p,&pixel);
963         SetPixelBlack(image,ScaleShortToQuantum(pixel),q);
964         p+=quantum_info->pad;
965         q+=GetPixelChannels(image);
966       }
967       break;
968     }
969     case 32:
970     {
971       unsigned int
972         pixel;
973
974       if (quantum_info->format == FloatingPointQuantumFormat)
975         {
976           float
977             pixel;
978
979           for (x=0; x < (ssize_t) number_pixels; x++)
980           {
981             p=PushFloatPixel(quantum_info,p,&pixel);
982             SetPixelBlack(image,ClampToQuantum(pixel),q);
983             p+=quantum_info->pad;
984             q+=GetPixelChannels(image);
985           }
986           break;
987         }
988       for (x=0; x < (ssize_t) number_pixels; x++)
989       {
990         p=PushLongPixel(quantum_info->endian,p,&pixel);
991         SetPixelBlack(image,ScaleLongToQuantum(pixel),q);
992         p+=quantum_info->pad;
993         q+=GetPixelChannels(image);
994       }
995       break;
996     }
997     case 64:
998     {
999       if (quantum_info->format == FloatingPointQuantumFormat)
1000         {
1001           double
1002             pixel;
1003
1004           for (x=0; x < (ssize_t) number_pixels; x++)
1005           {
1006             p=PushDoublePixel(quantum_info,p,&pixel);
1007             SetPixelBlack(image,ClampToQuantum(pixel),q);
1008             p+=quantum_info->pad;
1009             q+=GetPixelChannels(image);
1010           }
1011           break;
1012         }
1013     }
1014     default:
1015     {
1016       range=GetQuantumRange(quantum_info->depth);
1017       for (x=0; x < (ssize_t) number_pixels; x++)
1018       {
1019         p=PushQuantumPixel(quantum_info,p,&pixel);
1020         SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1021         p+=quantum_info->pad;
1022         q+=GetPixelChannels(image);
1023       }
1024       break;
1025     }
1026   }
1027 }
1028
1029 static void ImportBlueQuantum(const Image *image,QuantumInfo *quantum_info,
1030   const MagickSizeType number_pixels,const unsigned char *restrict p,
1031   Quantum *restrict q,ExceptionInfo *exception)
1032 {
1033   QuantumAny
1034     range;
1035
1036   register ssize_t
1037     x;
1038
1039   unsigned int
1040     pixel;
1041
1042   switch (quantum_info->depth)
1043   {
1044     case 8:
1045     {
1046       unsigned char
1047         pixel;
1048
1049       for (x=0; x < (ssize_t) number_pixels; x++)
1050       {
1051         p=PushCharPixel(p,&pixel);
1052         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
1053         p+=quantum_info->pad;
1054         q+=GetPixelChannels(image);
1055       }
1056       break;
1057     }
1058     case 16:
1059     {
1060       unsigned short
1061         pixel;
1062
1063       if (quantum_info->format == FloatingPointQuantumFormat)
1064         {
1065           for (x=0; x < (ssize_t) number_pixels; x++)
1066           {
1067             p=PushShortPixel(quantum_info->endian,p,&pixel);
1068             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
1069               HalfToSinglePrecision(pixel)),q);
1070             p+=quantum_info->pad;
1071             q+=GetPixelChannels(image);
1072           }
1073           break;
1074         }
1075       for (x=0; x < (ssize_t) number_pixels; x++)
1076       {
1077         p=PushShortPixel(quantum_info->endian,p,&pixel);
1078         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
1079         p+=quantum_info->pad;
1080         q+=GetPixelChannels(image);
1081       }
1082       break;
1083     }
1084     case 32:
1085     {
1086       unsigned int
1087         pixel;
1088
1089       if (quantum_info->format == FloatingPointQuantumFormat)
1090         {
1091           float
1092             pixel;
1093
1094           for (x=0; x < (ssize_t) number_pixels; x++)
1095           {
1096             p=PushFloatPixel(quantum_info,p,&pixel);
1097             SetPixelBlue(image,ClampToQuantum(pixel),q);
1098             p+=quantum_info->pad;
1099             q+=GetPixelChannels(image);
1100           }
1101           break;
1102         }
1103       for (x=0; x < (ssize_t) number_pixels; x++)
1104       {
1105         p=PushLongPixel(quantum_info->endian,p,&pixel);
1106         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
1107         p+=quantum_info->pad;
1108         q+=GetPixelChannels(image);
1109       }
1110       break;
1111     }
1112     case 64:
1113     {
1114       if (quantum_info->format == FloatingPointQuantumFormat)
1115         {
1116           double
1117             pixel;
1118
1119           for (x=0; x < (ssize_t) number_pixels; x++)
1120           {
1121             p=PushDoublePixel(quantum_info,p,&pixel);
1122             SetPixelBlue(image,ClampToQuantum(pixel),q);
1123             p+=quantum_info->pad;
1124             q+=GetPixelChannels(image);
1125           }
1126           break;
1127         }
1128     }
1129     default:
1130     {
1131       range=GetQuantumRange(quantum_info->depth);
1132       for (x=0; x < (ssize_t) number_pixels; x++)
1133       {
1134         p=PushQuantumPixel(quantum_info,p,&pixel);
1135         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1136         p+=quantum_info->pad;
1137         q+=GetPixelChannels(image);
1138       }
1139       break;
1140     }
1141   }
1142 }
1143
1144 static void ImportCbYCrYQuantum(const Image *image,QuantumInfo *quantum_info,
1145   const MagickSizeType number_pixels,const unsigned char *restrict p,
1146   Quantum *restrict q,ExceptionInfo *exception)
1147 {
1148   QuantumAny
1149     range;
1150
1151   register ssize_t
1152     x;
1153
1154   unsigned int
1155     pixel;
1156
1157   switch (quantum_info->depth)
1158   {
1159     case 10:
1160     {
1161       Quantum
1162         cbcr[4];
1163
1164       pixel=0;
1165       if (quantum_info->pack == MagickFalse)
1166         {
1167           register ssize_t
1168             i;
1169
1170           size_t
1171             quantum;
1172
1173           ssize_t
1174             n;
1175
1176           n=0;
1177           quantum=0;
1178           for (x=0; x < (ssize_t) number_pixels; x+=2)
1179           {
1180             for (i=0; i < 4; i++)
1181             {
1182               switch (n % 3)
1183               {
1184                 case 0:
1185                 {
1186                   p=PushLongPixel(quantum_info->endian,p,&pixel);
1187                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
1188                     (((pixel >> 22) & 0x3ff) << 6)));
1189                   break;
1190                 }
1191                 case 1:
1192                 {
1193                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
1194                     (((pixel >> 12) & 0x3ff) << 6)));
1195                   break;
1196                 }
1197                 case 2:
1198                 {
1199                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
1200                     (((pixel >> 2) & 0x3ff) << 6)));
1201                   break;
1202                 }
1203               }
1204               cbcr[i]=(Quantum) (quantum);
1205               n++;
1206             }
1207             p+=quantum_info->pad;
1208             SetPixelRed(image,cbcr[1],q);
1209             SetPixelGreen(image,cbcr[0],q);
1210             SetPixelBlue(image,cbcr[2],q);
1211             q+=GetPixelChannels(image);
1212             SetPixelRed(image,cbcr[3],q);
1213             SetPixelGreen(image,cbcr[0],q);
1214             SetPixelBlue(image,cbcr[2],q);
1215             q+=GetPixelChannels(image);
1216           }
1217           break;
1218         }
1219     }
1220     default:
1221     {
1222       range=GetQuantumRange(quantum_info->depth);
1223       for (x=0; x < (ssize_t) number_pixels; x++)
1224       {
1225         p=PushQuantumPixel(quantum_info,p,&pixel);
1226         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1227         p=PushQuantumPixel(quantum_info,p,&pixel);
1228         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1229         q+=GetPixelChannels(image);
1230       }
1231       break;
1232     }
1233   }
1234 }
1235
1236 static void ImportCMYKQuantum(const Image *image,QuantumInfo *quantum_info,
1237   const MagickSizeType number_pixels,const unsigned char *restrict p,
1238   Quantum *restrict q,ExceptionInfo *exception)
1239 {
1240   QuantumAny
1241     range;
1242
1243   register ssize_t
1244     x;
1245
1246   unsigned int
1247     pixel;
1248
1249   if (image->colorspace != CMYKColorspace)
1250     {
1251       (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1252         "ColorSeparatedImageRequired","'%s'",image->filename);
1253       return;
1254     }
1255   switch (quantum_info->depth)
1256   {
1257     case 8:
1258     {
1259       unsigned char
1260         pixel;
1261
1262       for (x=0; x < (ssize_t) number_pixels; x++)
1263       {
1264         p=PushCharPixel(p,&pixel);
1265         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
1266         p=PushCharPixel(p,&pixel);
1267         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
1268         p=PushCharPixel(p,&pixel);
1269         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
1270         p=PushCharPixel(p,&pixel);
1271         SetPixelBlack(image,ScaleCharToQuantum(pixel),q);
1272         p+=quantum_info->pad;
1273         q+=GetPixelChannels(image);
1274       }
1275       break;
1276     }
1277     case 16:
1278     {
1279       unsigned short
1280         pixel;
1281
1282       if (quantum_info->format == FloatingPointQuantumFormat)
1283         {
1284           for (x=0; x < (ssize_t) number_pixels; x++)
1285           {
1286             p=PushShortPixel(quantum_info->endian,p,&pixel);
1287             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
1288               HalfToSinglePrecision(pixel)),q);
1289             p=PushShortPixel(quantum_info->endian,p,&pixel);
1290             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
1291               HalfToSinglePrecision(pixel)),q);
1292             p=PushShortPixel(quantum_info->endian,p,&pixel);
1293             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
1294               HalfToSinglePrecision(pixel)),q);
1295             p=PushShortPixel(quantum_info->endian,p,&pixel);
1296             SetPixelBlack(image,ClampToQuantum((MagickRealType) QuantumRange*
1297               HalfToSinglePrecision(pixel)),q);
1298             p+=quantum_info->pad;
1299             q+=GetPixelChannels(image);
1300           }
1301           break;
1302         }
1303       for (x=0; x < (ssize_t) number_pixels; x++)
1304       {
1305         p=PushShortPixel(quantum_info->endian,p,&pixel);
1306         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
1307         p=PushShortPixel(quantum_info->endian,p,&pixel);
1308         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
1309         p=PushShortPixel(quantum_info->endian,p,&pixel);
1310         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
1311         p=PushShortPixel(quantum_info->endian,p,&pixel);
1312         SetPixelBlack(image,ScaleShortToQuantum(pixel),q);
1313         p+=quantum_info->pad;
1314         q+=GetPixelChannels(image);
1315       }
1316       break;
1317     }
1318     case 32:
1319     {
1320       unsigned int
1321         pixel;
1322
1323       if (quantum_info->format == FloatingPointQuantumFormat)
1324         {
1325           float
1326             pixel;
1327
1328           for (x=0; x < (ssize_t) number_pixels; x++)
1329           {
1330             p=PushFloatPixel(quantum_info,p,&pixel);
1331             SetPixelRed(image,ClampToQuantum(pixel),q);
1332             p=PushFloatPixel(quantum_info,p,&pixel);
1333             SetPixelGreen(image,ClampToQuantum(pixel),q);
1334             p=PushFloatPixel(quantum_info,p,&pixel);
1335             SetPixelBlue(image,ClampToQuantum(pixel),q);
1336             p=PushFloatPixel(quantum_info,p,&pixel);
1337             SetPixelBlack(image,ClampToQuantum(pixel),q);
1338             p+=quantum_info->pad;
1339             q+=GetPixelChannels(image);
1340           }
1341           break;
1342         }
1343       for (x=0; x < (ssize_t) number_pixels; x++)
1344       {
1345         p=PushLongPixel(quantum_info->endian,p,&pixel);
1346         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
1347         p=PushLongPixel(quantum_info->endian,p,&pixel);
1348         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
1349         p=PushLongPixel(quantum_info->endian,p,&pixel);
1350         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
1351         p=PushLongPixel(quantum_info->endian,p,&pixel);
1352         SetPixelBlack(image,ScaleLongToQuantum(pixel),q);
1353         p+=quantum_info->pad;
1354         q+=GetPixelChannels(image);
1355       }
1356       break;
1357     }
1358     case 64:
1359     {
1360       if (quantum_info->format == FloatingPointQuantumFormat)
1361         {
1362           double
1363             pixel;
1364
1365           for (x=0; x < (ssize_t) number_pixels; x++)
1366           {
1367             p=PushDoublePixel(quantum_info,p,&pixel);
1368             SetPixelRed(image,ClampToQuantum(pixel),q);
1369             p=PushDoublePixel(quantum_info,p,&pixel);
1370             SetPixelGreen(image,ClampToQuantum(pixel),q);
1371             p=PushDoublePixel(quantum_info,p,&pixel);
1372             SetPixelBlue(image,ClampToQuantum(pixel),q);
1373             p=PushDoublePixel(quantum_info,p,&pixel);
1374             SetPixelBlack(image,ClampToQuantum(pixel),q);
1375             p+=quantum_info->pad;
1376             q+=GetPixelChannels(image);
1377           }
1378           break;
1379         }
1380     }
1381     default:
1382     {
1383       range=GetQuantumRange(quantum_info->depth);
1384       for (x=0; x < (ssize_t) number_pixels; x++)
1385       {
1386         p=PushQuantumPixel(quantum_info,p,&pixel);
1387         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1388         p=PushQuantumPixel(quantum_info,p,&pixel);
1389         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1390         p=PushQuantumPixel(quantum_info,p,&pixel);
1391         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1392         p=PushQuantumPixel(quantum_info,p,&pixel);
1393         SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1394         q+=GetPixelChannels(image);
1395       }
1396       break;
1397     }
1398   }
1399 }
1400
1401 static void ImportCMYKAQuantum(const Image *image,QuantumInfo *quantum_info,
1402   const MagickSizeType number_pixels,const unsigned char *restrict p,
1403   Quantum *restrict q,ExceptionInfo *exception)
1404 {
1405   QuantumAny
1406     range;
1407
1408   register ssize_t
1409     x;
1410
1411   unsigned int
1412     pixel;
1413
1414   if (image->colorspace != CMYKColorspace)
1415     {
1416       (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1417         "ColorSeparatedImageRequired","'%s'",image->filename);
1418       return;
1419     }
1420   switch (quantum_info->depth)
1421   {
1422     case 8:
1423     {
1424       unsigned char
1425         pixel;
1426
1427       for (x=0; x < (ssize_t) number_pixels; x++)
1428       {
1429         p=PushCharPixel(p,&pixel);
1430         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
1431         p=PushCharPixel(p,&pixel);
1432         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
1433         p=PushCharPixel(p,&pixel);
1434         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
1435         p=PushCharPixel(p,&pixel);
1436         SetPixelBlack(image,ScaleCharToQuantum(pixel),q);
1437         p=PushCharPixel(p,&pixel);
1438         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
1439         p+=quantum_info->pad;
1440         q+=GetPixelChannels(image);
1441       }
1442       break;
1443     }
1444     case 16:
1445     {
1446       unsigned short
1447         pixel;
1448
1449       if (quantum_info->format == FloatingPointQuantumFormat)
1450         {
1451           for (x=0; x < (ssize_t) number_pixels; x++)
1452           {
1453             p=PushShortPixel(quantum_info->endian,p,&pixel);
1454             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
1455               HalfToSinglePrecision(pixel)),q);
1456             p=PushShortPixel(quantum_info->endian,p,&pixel);
1457             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
1458               HalfToSinglePrecision(pixel)),q);
1459             p=PushShortPixel(quantum_info->endian,p,&pixel);
1460             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
1461               HalfToSinglePrecision(pixel)),q);
1462             p=PushShortPixel(quantum_info->endian,p,&pixel);
1463             SetPixelBlack(image,ClampToQuantum((MagickRealType) QuantumRange*
1464               HalfToSinglePrecision(pixel)),q);
1465             p=PushShortPixel(quantum_info->endian,p,&pixel);
1466             SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
1467               HalfToSinglePrecision(pixel)),q);
1468             p+=quantum_info->pad;
1469             q+=GetPixelChannels(image);
1470           }
1471           break;
1472         }
1473       for (x=0; x < (ssize_t) number_pixels; x++)
1474       {
1475         p=PushShortPixel(quantum_info->endian,p,&pixel);
1476         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
1477         p=PushShortPixel(quantum_info->endian,p,&pixel);
1478         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
1479         p=PushShortPixel(quantum_info->endian,p,&pixel);
1480         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
1481         p=PushShortPixel(quantum_info->endian,p,&pixel);
1482         SetPixelBlack(image,ScaleShortToQuantum(pixel),q);
1483         p=PushShortPixel(quantum_info->endian,p,&pixel);
1484         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
1485         p+=quantum_info->pad;
1486         q+=GetPixelChannels(image);
1487       }
1488       break;
1489     }
1490     case 32:
1491     {
1492       unsigned int
1493         pixel;
1494
1495       if (quantum_info->format == FloatingPointQuantumFormat)
1496         {
1497           float
1498             pixel;
1499
1500           for (x=0; x < (ssize_t) number_pixels; x++)
1501           {
1502             p=PushFloatPixel(quantum_info,p,&pixel);
1503             SetPixelRed(image,ClampToQuantum(pixel),q);
1504             p=PushFloatPixel(quantum_info,p,&pixel);
1505             SetPixelGreen(image,ClampToQuantum(pixel),q);
1506             p=PushFloatPixel(quantum_info,p,&pixel);
1507             SetPixelBlue(image,ClampToQuantum(pixel),q);
1508             p=PushFloatPixel(quantum_info,p,&pixel);
1509             SetPixelBlack(image,ClampToQuantum(pixel),q);
1510             p=PushFloatPixel(quantum_info,p,&pixel);
1511             SetPixelAlpha(image,ClampToQuantum(pixel),q);
1512             p+=quantum_info->pad;
1513             q+=GetPixelChannels(image);
1514           }
1515           break;
1516         }
1517       for (x=0; x < (ssize_t) number_pixels; x++)
1518       {
1519         p=PushLongPixel(quantum_info->endian,p,&pixel);
1520         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
1521         p=PushLongPixel(quantum_info->endian,p,&pixel);
1522         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
1523         p=PushLongPixel(quantum_info->endian,p,&pixel);
1524         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
1525         p=PushLongPixel(quantum_info->endian,p,&pixel);
1526         SetPixelBlack(image,ScaleLongToQuantum(pixel),q);
1527         p=PushLongPixel(quantum_info->endian,p,&pixel);
1528         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
1529         p+=quantum_info->pad;
1530         q+=GetPixelChannels(image);
1531       }
1532       break;
1533     }
1534     case 64:
1535     {
1536       if (quantum_info->format == FloatingPointQuantumFormat)
1537         {
1538           double
1539             pixel;
1540
1541           for (x=0; x < (ssize_t) number_pixels; x++)
1542           {
1543             p=PushDoublePixel(quantum_info,p,&pixel);
1544             SetPixelRed(image,ClampToQuantum(pixel),q);
1545             p=PushDoublePixel(quantum_info,p,&pixel);
1546             SetPixelGreen(image,ClampToQuantum(pixel),q);
1547             p=PushDoublePixel(quantum_info,p,&pixel);
1548             SetPixelBlue(image,ClampToQuantum(pixel),q);
1549             p=PushDoublePixel(quantum_info,p,&pixel);
1550             SetPixelBlack(image,ClampToQuantum(pixel),q);
1551             p=PushDoublePixel(quantum_info,p,&pixel);
1552             SetPixelAlpha(image,ClampToQuantum(pixel),q);
1553             p=PushDoublePixel(quantum_info,p,&pixel);
1554             p+=quantum_info->pad;
1555             q+=GetPixelChannels(image);
1556           }
1557           break;
1558         }
1559     }
1560     default:
1561     {
1562       range=GetQuantumRange(quantum_info->depth);
1563       for (x=0; x < (ssize_t) number_pixels; x++)
1564       {
1565         p=PushQuantumPixel(quantum_info,p,&pixel);
1566         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
1567         p=PushQuantumPixel(quantum_info,p,&pixel);
1568         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
1569         p=PushQuantumPixel(quantum_info,p,&pixel);
1570         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
1571         p=PushQuantumPixel(quantum_info,p,&pixel);
1572         SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
1573         p=PushQuantumPixel(quantum_info,p,&pixel);
1574         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1575         q+=GetPixelChannels(image);
1576       }
1577       break;
1578     }
1579   }
1580 }
1581
1582 static void ImportGrayQuantum(const Image *image,QuantumInfo *quantum_info,
1583   const MagickSizeType number_pixels,const unsigned char *restrict p,
1584   Quantum *restrict q,ExceptionInfo *exception)
1585 {
1586   QuantumAny
1587     range;
1588
1589   register ssize_t
1590     x;
1591
1592   ssize_t
1593     bit;
1594
1595   unsigned int
1596     pixel;
1597
1598   switch (quantum_info->depth)
1599   {
1600     case 1:
1601     {
1602       register Quantum
1603         black,
1604         white;
1605
1606       black=0;
1607       white=(Quantum) QuantumRange;
1608       if (quantum_info->min_is_white != MagickFalse)
1609         {
1610           black=(Quantum) QuantumRange;
1611           white=0;
1612         }
1613       for (x=0; x < ((ssize_t) number_pixels-7); x+=8)
1614       {
1615         for (bit=0; bit < 8; bit++)
1616         {
1617           SetPixelGray(image,((*p) & (1 << (7-bit))) == 0 ? black : white,q);
1618           q+=GetPixelChannels(image);
1619         }
1620         p++;
1621       }
1622       for (bit=0; bit < (ssize_t) (number_pixels % 8); bit++)
1623       {
1624         SetPixelGray(image,((*p) & (0x01 << (7-bit))) == 0 ? black : white,q);
1625         q+=GetPixelChannels(image);
1626       }
1627       if (bit != 0)
1628         p++;
1629       break;
1630     }
1631     case 4:
1632     {
1633       register unsigned char
1634         pixel;
1635
1636       range=GetQuantumRange(quantum_info->depth);
1637       for (x=0; x < ((ssize_t) number_pixels-1); x+=2)
1638       {
1639         pixel=(unsigned char) ((*p >> 4) & 0xf);
1640         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1641         q+=GetPixelChannels(image);
1642         pixel=(unsigned char) ((*p) & 0xf);
1643         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1644         p++;
1645         q+=GetPixelChannels(image);
1646       }
1647       for (bit=0; bit < (ssize_t) (number_pixels % 2); bit++)
1648       {
1649         pixel=(unsigned char) (*p++ >> 4);
1650         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1651         q+=GetPixelChannels(image);
1652       }
1653       break;
1654     }
1655     case 8:
1656     {
1657       unsigned char
1658         pixel;
1659
1660       if (quantum_info->min_is_white != MagickFalse)
1661         {
1662           for (x=0; x < (ssize_t) number_pixels; x++)
1663           {
1664             p=PushCharPixel(p,&pixel);
1665             SetPixelGray(image,ScaleCharToQuantum(pixel),q);
1666             SetPixelAlpha(image,OpaqueAlpha,q);
1667             p+=quantum_info->pad;
1668             q+=GetPixelChannels(image);
1669           }
1670           break;
1671         }
1672       for (x=0; x < (ssize_t) number_pixels; x++)
1673       {
1674         p=PushCharPixel(p,&pixel);
1675         SetPixelGray(image,ScaleCharToQuantum(pixel),q);
1676         SetPixelAlpha(image,OpaqueAlpha,q);
1677         p+=quantum_info->pad;
1678         q+=GetPixelChannels(image);
1679       }
1680       break;
1681     }
1682     case 10:
1683     {
1684       range=GetQuantumRange(quantum_info->depth);
1685       if (quantum_info->pack == MagickFalse)
1686         {
1687           if (image->endian != LSBEndian)
1688             {
1689               for (x=0; x < (ssize_t) (number_pixels-2); x+=3)
1690               {
1691                 p=PushLongPixel(quantum_info->endian,p,&pixel);
1692                 SetPixelGray(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,
1693                   range),q);
1694                 q+=GetPixelChannels(image);
1695                 SetPixelGray(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,
1696                   range),q);
1697                 q+=GetPixelChannels(image);
1698                 SetPixelGray(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,
1699                   range),q);
1700                 p+=quantum_info->pad;
1701                 q+=GetPixelChannels(image);
1702               }
1703               p=PushLongPixel(quantum_info->endian,p,&pixel);
1704               if (x++ < (ssize_t) (number_pixels-1))
1705                 {
1706                   SetPixelGray(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,
1707                     range),q);
1708                   q+=GetPixelChannels(image);
1709                 }
1710               if (x++ < (ssize_t) number_pixels)
1711                 {
1712                   SetPixelGray(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,
1713                     range),q);
1714                   q+=GetPixelChannels(image);
1715                 }
1716               break;
1717             }
1718           for (x=0; x < (ssize_t) (number_pixels-2); x+=3)
1719           {
1720             p=PushLongPixel(quantum_info->endian,p,&pixel);
1721             SetPixelGray(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,range),
1722               q);
1723             q+=GetPixelChannels(image);
1724             SetPixelGray(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
1725               q);
1726             q+=GetPixelChannels(image);
1727             SetPixelGray(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,range),
1728               q);
1729             p+=quantum_info->pad;
1730             q+=GetPixelChannels(image);
1731           }
1732           p=PushLongPixel(quantum_info->endian,p,&pixel);
1733           if (x++ < (ssize_t) (number_pixels-1))
1734             {
1735               SetPixelGray(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,range),
1736                 q);
1737               q+=GetPixelChannels(image);
1738             }
1739           if (x++ < (ssize_t) number_pixels)
1740             {
1741               SetPixelGray(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
1742                 q);
1743               q+=GetPixelChannels(image);
1744             }
1745           break;
1746         }
1747       for (x=0; x < (ssize_t) number_pixels; x++)
1748       {
1749         p=PushQuantumPixel(quantum_info,p,&pixel);
1750         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1751         p+=quantum_info->pad;
1752         q+=GetPixelChannels(image);
1753       }
1754       break;
1755     }
1756     case 12:
1757     {
1758       range=GetQuantumRange(quantum_info->depth);
1759       if (quantum_info->pack == MagickFalse)
1760         {
1761           unsigned short
1762             pixel;
1763
1764           for (x=0; x < (ssize_t) (number_pixels-1); x+=2)
1765           {
1766             p=PushShortPixel(quantum_info->endian,p,&pixel);
1767             SetPixelGray(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
1768               range),q);
1769             q+=GetPixelChannels(image);
1770             p=PushShortPixel(quantum_info->endian,p,&pixel);
1771             SetPixelGray(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
1772               range),q);
1773             p+=quantum_info->pad;
1774             q+=GetPixelChannels(image);
1775           }
1776           for (bit=0; bit < (ssize_t) (number_pixels % 2); bit++)
1777           {
1778             p=PushShortPixel(quantum_info->endian,p,&pixel);
1779             SetPixelGray(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
1780               range),q);
1781             p+=quantum_info->pad;
1782             q+=GetPixelChannels(image);
1783           }
1784           if (bit != 0)
1785             p++;
1786           break;
1787         }
1788       for (x=0; x < (ssize_t) number_pixels; x++)
1789       {
1790         p=PushQuantumPixel(quantum_info,p,&pixel);
1791         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1792         p+=quantum_info->pad;
1793         q+=GetPixelChannels(image);
1794       }
1795       break;
1796     }
1797     case 16:
1798     {
1799       unsigned short
1800         pixel;
1801
1802       if (quantum_info->min_is_white != MagickFalse)
1803         {
1804           for (x=0; x < (ssize_t) number_pixels; x++)
1805           {
1806             p=PushShortPixel(quantum_info->endian,p,&pixel);
1807             SetPixelGray(image,ScaleShortToQuantum(pixel),q);
1808             p+=quantum_info->pad;
1809             q+=GetPixelChannels(image);
1810           }
1811           break;
1812         }
1813       if (quantum_info->format == FloatingPointQuantumFormat)
1814         {
1815           for (x=0; x < (ssize_t) number_pixels; x++)
1816           {
1817             p=PushShortPixel(quantum_info->endian,p,&pixel);
1818             SetPixelGray(image,ClampToQuantum((MagickRealType) QuantumRange*
1819               HalfToSinglePrecision(pixel)),q);
1820             p+=quantum_info->pad;
1821             q+=GetPixelChannels(image);
1822           }
1823           break;
1824         }
1825       for (x=0; x < (ssize_t) number_pixels; x++)
1826       {
1827         p=PushShortPixel(quantum_info->endian,p,&pixel);
1828         SetPixelGray(image,ScaleShortToQuantum(pixel),q);
1829         p+=quantum_info->pad;
1830         q+=GetPixelChannels(image);
1831       }
1832       break;
1833     }
1834     case 32:
1835     {
1836       unsigned int
1837         pixel;
1838
1839       if (quantum_info->format == FloatingPointQuantumFormat)
1840         {
1841           float
1842             pixel;
1843
1844           for (x=0; x < (ssize_t) number_pixels; x++)
1845           {
1846             p=PushFloatPixel(quantum_info,p,&pixel);
1847             SetPixelGray(image,ClampToQuantum(pixel),q);
1848             p+=quantum_info->pad;
1849             q+=GetPixelChannels(image);
1850           }
1851           break;
1852         }
1853       for (x=0; x < (ssize_t) number_pixels; x++)
1854       {
1855         p=PushLongPixel(quantum_info->endian,p,&pixel);
1856         SetPixelGray(image,ScaleLongToQuantum(pixel),q);
1857         p+=quantum_info->pad;
1858         q+=GetPixelChannels(image);
1859       }
1860       break;
1861     }
1862     case 64:
1863     {
1864       if (quantum_info->format == FloatingPointQuantumFormat)
1865         {
1866           double
1867             pixel;
1868
1869           for (x=0; x < (ssize_t) number_pixels; x++)
1870           {
1871             p=PushDoublePixel(quantum_info,p,&pixel);
1872             SetPixelGray(image,ClampToQuantum(pixel),q);
1873             p+=quantum_info->pad;
1874             q+=GetPixelChannels(image);
1875           }
1876           break;
1877         }
1878     }
1879     default:
1880     {
1881       range=GetQuantumRange(quantum_info->depth);
1882       for (x=0; x < (ssize_t) number_pixels; x++)
1883       {
1884         p=PushQuantumPixel(quantum_info,p,&pixel);
1885         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1886         p+=quantum_info->pad;
1887         q+=GetPixelChannels(image);
1888       }
1889       break;
1890     }
1891   }
1892 }
1893
1894 static void ImportGrayAlphaQuantum(const Image *image,QuantumInfo *quantum_info,
1895   const MagickSizeType number_pixels,const unsigned char *restrict p,
1896   Quantum *restrict q,ExceptionInfo *exception)
1897 {
1898   QuantumAny
1899     range;
1900
1901   register ssize_t
1902     x;
1903
1904   ssize_t
1905     bit;
1906
1907   unsigned int
1908     pixel;
1909
1910   switch (quantum_info->depth)
1911   {
1912     case 1:
1913     {
1914       register unsigned char
1915         pixel;
1916
1917       for (x=((ssize_t) number_pixels-3); x > 0; x-=4)
1918       {
1919         for (bit=0; bit < 8; bit+=2)
1920         {
1921           pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ? 0x00 : 0x01);
1922           SetPixelGray(image,(Quantum) (pixel == 0 ? 0 : QuantumRange),q);
1923           SetPixelAlpha(image,((*p) & (1UL << (unsigned char) (6-bit))) == 0 ?
1924             TransparentAlpha : OpaqueAlpha,q);
1925           q+=GetPixelChannels(image);
1926         }
1927         p++;
1928       }
1929       if ((number_pixels % 4) != 0)
1930         for (bit=3; bit >= (ssize_t) (4-(number_pixels % 4)); bit-=2)
1931         {
1932           pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ? 0x00 : 0x01);
1933           SetPixelGray(image,(Quantum) (pixel != 0 ? 0 : QuantumRange),q);
1934           SetPixelAlpha(image,((*p) & (1UL << (unsigned char) (6-bit))) == 0 ?
1935             TransparentAlpha : OpaqueAlpha,q);
1936           q+=GetPixelChannels(image);
1937         }
1938       if (bit != 0)
1939         p++;
1940       break;
1941     }
1942     case 4:
1943     {
1944       register unsigned char
1945         pixel;
1946
1947       range=GetQuantumRange(quantum_info->depth);
1948       for (x=0; x < (ssize_t) number_pixels; x++)
1949       {
1950         pixel=(unsigned char) ((*p >> 4) & 0xf);
1951         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1952         pixel=(unsigned char) ((*p) & 0xf);
1953         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1954         p++;
1955         q+=GetPixelChannels(image);
1956       }
1957       break;
1958     }
1959     case 8:
1960     {
1961       unsigned char
1962         pixel;
1963
1964       for (x=0; x < (ssize_t) number_pixels; x++)
1965       {
1966         p=PushCharPixel(p,&pixel);
1967         SetPixelGray(image,ScaleCharToQuantum(pixel),q);
1968         p=PushCharPixel(p,&pixel);
1969         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
1970         p+=quantum_info->pad;
1971         q+=GetPixelChannels(image);
1972       }
1973       break;
1974     }
1975     case 10:
1976     {
1977       range=GetQuantumRange(quantum_info->depth);
1978       for (x=0; x < (ssize_t) number_pixels; x++)
1979       {
1980         p=PushQuantumPixel(quantum_info,p,&pixel);
1981         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1982         p=PushQuantumPixel(quantum_info,p,&pixel);
1983         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1984         p+=quantum_info->pad;
1985         q+=GetPixelChannels(image);
1986       }
1987       break;
1988     }
1989     case 12:
1990     {
1991       range=GetQuantumRange(quantum_info->depth);
1992       for (x=0; x < (ssize_t) number_pixels; x++)
1993       {
1994         p=PushQuantumPixel(quantum_info,p,&pixel);
1995         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
1996         p=PushQuantumPixel(quantum_info,p,&pixel);
1997         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
1998         p+=quantum_info->pad;
1999         q+=GetPixelChannels(image);
2000       }
2001       break;
2002     }
2003     case 16:
2004     {
2005       unsigned short
2006         pixel;
2007
2008       if (quantum_info->format == FloatingPointQuantumFormat)
2009         {
2010           for (x=0; x < (ssize_t) number_pixels; x++)
2011           {
2012             p=PushShortPixel(quantum_info->endian,p,&pixel);
2013             SetPixelGray(image,ClampToQuantum((MagickRealType) QuantumRange*
2014               HalfToSinglePrecision(pixel)),q);
2015             p=PushShortPixel(quantum_info->endian,p,&pixel);
2016             SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
2017               HalfToSinglePrecision(pixel)),q);
2018             p+=quantum_info->pad;
2019             q+=GetPixelChannels(image);
2020           }
2021           break;
2022         }
2023       for (x=0; x < (ssize_t) number_pixels; x++)
2024       {
2025         p=PushShortPixel(quantum_info->endian,p,&pixel);
2026         SetPixelGray(image,ScaleShortToQuantum(pixel),q);
2027         p=PushShortPixel(quantum_info->endian,p,&pixel);
2028         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
2029         p+=quantum_info->pad;
2030         q+=GetPixelChannels(image);
2031       }
2032       break;
2033     }
2034     case 32:
2035     {
2036       unsigned int
2037         pixel;
2038
2039       if (quantum_info->format == FloatingPointQuantumFormat)
2040         {
2041           float
2042             pixel;
2043
2044           for (x=0; x < (ssize_t) number_pixels; x++)
2045           {
2046             p=PushFloatPixel(quantum_info,p,&pixel);
2047             SetPixelGray(image,ClampToQuantum(pixel),q);
2048             p=PushFloatPixel(quantum_info,p,&pixel);
2049             SetPixelAlpha(image,ClampToQuantum(pixel),q);
2050             p+=quantum_info->pad;
2051             q+=GetPixelChannels(image);
2052           }
2053           break;
2054         }
2055       for (x=0; x < (ssize_t) number_pixels; x++)
2056       {
2057         p=PushLongPixel(quantum_info->endian,p,&pixel);
2058         SetPixelGray(image,ScaleLongToQuantum(pixel),q);
2059         p=PushLongPixel(quantum_info->endian,p,&pixel);
2060         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
2061         p+=quantum_info->pad;
2062         q+=GetPixelChannels(image);
2063       }
2064       break;
2065     }
2066     case 64:
2067     {
2068       if (quantum_info->format == FloatingPointQuantumFormat)
2069         {
2070           double
2071             pixel;
2072
2073           for (x=0; x < (ssize_t) number_pixels; x++)
2074           {
2075             p=PushDoublePixel(quantum_info,p,&pixel);
2076             SetPixelGray(image,ClampToQuantum(pixel),q);
2077             p=PushDoublePixel(quantum_info,p,&pixel);
2078             SetPixelAlpha(image,ClampToQuantum(pixel),q);
2079             p+=quantum_info->pad;
2080             q+=GetPixelChannels(image);
2081           }
2082           break;
2083         }
2084     }
2085     default:
2086     {
2087       range=GetQuantumRange(quantum_info->depth);
2088       for (x=0; x < (ssize_t) number_pixels; x++)
2089       {
2090         p=PushQuantumPixel(quantum_info,p,&pixel);
2091         SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
2092         p=PushQuantumPixel(quantum_info,p,&pixel);
2093         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
2094         p+=quantum_info->pad;
2095         q+=GetPixelChannels(image);
2096       }
2097       break;
2098     }
2099   }
2100 }
2101
2102 static void ImportGreenQuantum(const Image *image,QuantumInfo *quantum_info,
2103   const MagickSizeType number_pixels,const unsigned char *restrict p,
2104   Quantum *restrict q,ExceptionInfo *exception)
2105 {
2106   QuantumAny
2107     range;
2108
2109   register ssize_t
2110     x;
2111
2112   unsigned int
2113     pixel;
2114
2115   switch (quantum_info->depth)
2116   {
2117     case 8:
2118     {
2119       unsigned char
2120         pixel;
2121
2122       for (x=0; x < (ssize_t) number_pixels; x++)
2123       {
2124         p=PushCharPixel(p,&pixel);
2125         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
2126         p+=quantum_info->pad;
2127         q+=GetPixelChannels(image);
2128       }
2129       break;
2130     }
2131     case 16:
2132     {
2133       unsigned short
2134         pixel;
2135
2136       if (quantum_info->format == FloatingPointQuantumFormat)
2137         {
2138           for (x=0; x < (ssize_t) number_pixels; x++)
2139           {
2140             p=PushShortPixel(quantum_info->endian,p,&pixel);
2141             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
2142               HalfToSinglePrecision(pixel)),q);
2143             p+=quantum_info->pad;
2144             q+=GetPixelChannels(image);
2145           }
2146           break;
2147         }
2148       for (x=0; x < (ssize_t) number_pixels; x++)
2149       {
2150         p=PushShortPixel(quantum_info->endian,p,&pixel);
2151         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
2152         p+=quantum_info->pad;
2153         q+=GetPixelChannels(image);
2154       }
2155       break;
2156     }
2157     case 32:
2158     {
2159       unsigned int
2160         pixel;
2161
2162       if (quantum_info->format == FloatingPointQuantumFormat)
2163         {
2164           float
2165             pixel;
2166
2167           for (x=0; x < (ssize_t) number_pixels; x++)
2168           {
2169             p=PushFloatPixel(quantum_info,p,&pixel);
2170             SetPixelGreen(image,ClampToQuantum(pixel),q);
2171             p+=quantum_info->pad;
2172             q+=GetPixelChannels(image);
2173           }
2174           break;
2175         }
2176       for (x=0; x < (ssize_t) number_pixels; x++)
2177       {
2178         p=PushLongPixel(quantum_info->endian,p,&pixel);
2179         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
2180         p+=quantum_info->pad;
2181         q+=GetPixelChannels(image);
2182       }
2183       break;
2184     }
2185     case 64:
2186     {
2187       if (quantum_info->format == FloatingPointQuantumFormat)
2188         {
2189           double
2190             pixel;
2191
2192           for (x=0; x < (ssize_t) number_pixels; x++)
2193           {
2194             p=PushDoublePixel(quantum_info,p,&pixel);
2195             SetPixelGreen(image,ClampToQuantum(pixel),q);
2196             p+=quantum_info->pad;
2197             q+=GetPixelChannels(image);
2198           }
2199           break;
2200         }
2201     }
2202     default:
2203     {
2204       range=GetQuantumRange(quantum_info->depth);
2205       for (x=0; x < (ssize_t) number_pixels; x++)
2206       {
2207         p=PushQuantumPixel(quantum_info,p,&pixel);
2208         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
2209         p+=quantum_info->pad;
2210         q+=GetPixelChannels(image);
2211       }
2212       break;
2213     }
2214   }
2215 }
2216
2217 static void ImportIndexQuantum(const Image *image,QuantumInfo *quantum_info,
2218   const MagickSizeType number_pixels,const unsigned char *restrict p,
2219   Quantum *restrict q,ExceptionInfo *exception)
2220 {
2221   MagickBooleanType
2222     range_exception;
2223
2224   register ssize_t
2225     x;
2226
2227   ssize_t
2228     bit;
2229
2230   unsigned int
2231     pixel;
2232
2233   if (image->storage_class != PseudoClass)
2234     {
2235       (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
2236         "ColormappedImageRequired","'%s'",image->filename);
2237       return;
2238     }
2239   range_exception=MagickFalse;
2240   switch (quantum_info->depth)
2241   {
2242     case 1:
2243     {
2244       register unsigned char
2245         pixel;
2246
2247       for (x=0; x < ((ssize_t) number_pixels-7); x+=8)
2248       {
2249         for (bit=0; bit < 8; bit++)
2250         {
2251           if (quantum_info->min_is_white == MagickFalse)
2252             pixel=(unsigned char) (((*p) & (1 << (7-bit))) == 0 ?
2253               0x00 : 0x01);
2254           else
2255             pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ?
2256               0x00 : 0x01);
2257           SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),
2258             q);
2259           SetPixelInfoPixel(image,image->colormap+(ssize_t)
2260             GetPixelIndex(image,q),q);
2261           q+=GetPixelChannels(image);
2262         }
2263         p++;
2264       }
2265       for (bit=0; bit < (ssize_t) (number_pixels % 8); bit++)
2266       {
2267         if (quantum_info->min_is_white == MagickFalse)
2268           pixel=(unsigned char) (((*p) & (1 << (7-bit))) == 0 ? 0x00 : 0x01);
2269         else
2270           pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ? 0x00 : 0x01);
2271         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2272         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2273           GetPixelIndex(image,q),q);
2274         q+=GetPixelChannels(image);
2275       }
2276       break;
2277     }
2278     case 4:
2279     {
2280       register unsigned char
2281         pixel;
2282
2283       for (x=0; x < ((ssize_t) number_pixels-1); x+=2)
2284       {
2285         pixel=(unsigned char) ((*p >> 4) & 0xf);
2286         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2287         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2288           GetPixelIndex(image,q),q);
2289         q+=GetPixelChannels(image);
2290         pixel=(unsigned char) ((*p) & 0xf);
2291         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2292         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2293           GetPixelIndex(image,q),q);
2294         p++;
2295         q+=GetPixelChannels(image);
2296       }
2297       for (bit=0; bit < (ssize_t) (number_pixels % 2); bit++)
2298       {
2299         pixel=(unsigned char) ((*p++ >> 4) & 0xf);
2300         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2301         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2302           GetPixelIndex(image,q),q);
2303         q+=GetPixelChannels(image);
2304       }
2305       break;
2306     }
2307     case 8:
2308     {
2309       unsigned char
2310         pixel;
2311
2312       for (x=0; x < (ssize_t) number_pixels; x++)
2313       {
2314         p=PushCharPixel(p,&pixel);
2315         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2316         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2317           GetPixelIndex(image,q),q);
2318         p+=quantum_info->pad;
2319         q+=GetPixelChannels(image);
2320       }
2321       break;
2322     }
2323     case 16:
2324     {
2325       unsigned short
2326         pixel;
2327
2328       if (quantum_info->format == FloatingPointQuantumFormat)
2329         {
2330           for (x=0; x < (ssize_t) number_pixels; x++)
2331           {
2332             p=PushShortPixel(quantum_info->endian,p,&pixel);
2333             SetPixelIndex(image,PushColormapIndex(image,ClampToQuantum(
2334               (MagickRealType) QuantumRange*HalfToSinglePrecision(pixel)),
2335               &range_exception),q);
2336             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2337               GetPixelIndex(image,q),q);
2338             p+=quantum_info->pad;
2339             q+=GetPixelChannels(image);
2340           }
2341           break;
2342         }
2343       for (x=0; x < (ssize_t) number_pixels; x++)
2344       {
2345         p=PushShortPixel(quantum_info->endian,p,&pixel);
2346         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2347         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2348           GetPixelIndex(image,q),q);
2349         p+=quantum_info->pad;
2350         q+=GetPixelChannels(image);
2351       }
2352       break;
2353     }
2354     case 32:
2355     {
2356       unsigned int
2357         pixel;
2358
2359       if (quantum_info->format == FloatingPointQuantumFormat)
2360         {
2361           float
2362             pixel;
2363
2364           for (x=0; x < (ssize_t) number_pixels; x++)
2365           {
2366             p=PushFloatPixel(quantum_info,p,&pixel);
2367             SetPixelIndex(image,PushColormapIndex(image,ClampToQuantum(pixel),
2368               &range_exception),q);
2369             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2370               GetPixelIndex(image,q),q);
2371             p+=quantum_info->pad;
2372             q+=GetPixelChannels(image);
2373           }
2374           break;
2375         }
2376       for (x=0; x < (ssize_t) number_pixels; x++)
2377       {
2378         p=PushLongPixel(quantum_info->endian,p,&pixel);
2379         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2380         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2381           GetPixelIndex(image,q),q);
2382         p+=quantum_info->pad;
2383         q+=GetPixelChannels(image);
2384       }
2385       break;
2386     }
2387     case 64:
2388     {
2389       if (quantum_info->format == FloatingPointQuantumFormat)
2390         {
2391           double
2392             pixel;
2393
2394           for (x=0; x < (ssize_t) number_pixels; x++)
2395           {
2396             p=PushDoublePixel(quantum_info,p,&pixel);
2397             SetPixelIndex(image,PushColormapIndex(image,ClampToQuantum(pixel),
2398               &range_exception),q);
2399             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2400               GetPixelIndex(image,q),q);
2401             p+=quantum_info->pad;
2402             q+=GetPixelChannels(image);
2403           }
2404           break;
2405         }
2406     }
2407     default:
2408     {
2409       for (x=0; x < (ssize_t) number_pixels; x++)
2410       {
2411         p=PushQuantumPixel(quantum_info,p,&pixel);
2412         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2413         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2414           GetPixelIndex(image,q),q);
2415         p+=quantum_info->pad;
2416         q+=GetPixelChannels(image);
2417       }
2418       break;
2419     }
2420   }
2421   if (range_exception != MagickFalse)
2422     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
2423       "InvalidColormapIndex","'%s'",image->filename);
2424 }
2425
2426 static void ImportIndexAlphaQuantum(const Image *image,
2427   QuantumInfo *quantum_info,const MagickSizeType number_pixels,
2428   const unsigned char *restrict p,Quantum *restrict q,ExceptionInfo *exception)
2429 {
2430   MagickBooleanType
2431     range_exception;
2432
2433   QuantumAny
2434     range;
2435
2436   register ssize_t
2437     x;
2438
2439   ssize_t
2440     bit;
2441
2442   unsigned int
2443     pixel;
2444
2445   if (image->storage_class != PseudoClass)
2446     {
2447       (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
2448         "ColormappedImageRequired","'%s'",image->filename);
2449       return;
2450     }
2451   range_exception=MagickFalse;
2452   switch (quantum_info->depth)
2453   {
2454     case 1:
2455     {
2456       register unsigned char
2457         pixel;
2458
2459       for (x=((ssize_t) number_pixels-3); x > 0; x-=4)
2460       {
2461         for (bit=0; bit < 8; bit+=2)
2462         {
2463           if (quantum_info->min_is_white == MagickFalse)
2464             pixel=(unsigned char) (((*p) & (1 << (7-bit))) == 0 ? 0x00 : 0x01);
2465           else
2466             pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ? 0x00 : 0x01);
2467           SetPixelGray(image,(Quantum) (pixel == 0 ? 0 : QuantumRange),q);
2468           SetPixelAlpha(image,((*p) & (1UL << (unsigned char) (6-bit))) == 0 ?
2469             TransparentAlpha : OpaqueAlpha,q);
2470           SetPixelIndex(image,(Quantum) (pixel == 0 ? 0 : 1),q);
2471           q+=GetPixelChannels(image);
2472         }
2473       }
2474       if ((number_pixels % 4) != 0)
2475         for (bit=3; bit >= (ssize_t) (4-(number_pixels % 4)); bit-=2)
2476         {
2477           if (quantum_info->min_is_white == MagickFalse)
2478             pixel=(unsigned char) (((*p) & (1 << (7-bit))) == 0 ? 0x00 : 0x01);
2479           else
2480             pixel=(unsigned char) (((*p) & (1 << (7-bit))) != 0 ? 0x00 : 0x01);
2481           SetPixelIndex(image,(Quantum) (pixel == 0 ? 0 : 1),q);
2482           SetPixelGray(image,(Quantum) (pixel == 0 ? 0 : QuantumRange),q);
2483           SetPixelAlpha(image,((*p) & (1UL << (unsigned char) (6-bit))) == 0 ?
2484             TransparentAlpha : OpaqueAlpha,q);
2485           q+=GetPixelChannels(image);
2486         }
2487       break;
2488     }
2489     case 4:
2490     {
2491       register unsigned char
2492         pixel;
2493
2494       range=GetQuantumRange(quantum_info->depth);
2495       for (x=0; x < (ssize_t) number_pixels; x++)
2496       {
2497         pixel=(unsigned char) ((*p >> 4) & 0xf);
2498         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2499         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2500           GetPixelIndex(image,q),q);
2501         pixel=(unsigned char) ((*p) & 0xf);
2502         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
2503         p++;
2504         q+=GetPixelChannels(image);
2505       }
2506       break;
2507     }
2508     case 8:
2509     {
2510       unsigned char
2511         pixel;
2512
2513       for (x=0; x < (ssize_t) number_pixels; x++)
2514       {
2515         p=PushCharPixel(p,&pixel);
2516         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2517         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2518           GetPixelIndex(image,q),q);
2519         p=PushCharPixel(p,&pixel);
2520         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
2521         p+=quantum_info->pad;
2522         q+=GetPixelChannels(image);
2523       }
2524       break;
2525     }
2526     case 16:
2527     {
2528       unsigned short
2529         pixel;
2530
2531       if (quantum_info->format == FloatingPointQuantumFormat)
2532         {
2533           for (x=0; x < (ssize_t) number_pixels; x++)
2534           {
2535             p=PushShortPixel(quantum_info->endian,p,&pixel);
2536             SetPixelIndex(image,PushColormapIndex(image,ClampToQuantum(
2537               (MagickRealType) QuantumRange*HalfToSinglePrecision(pixel)),
2538               &range_exception),q);
2539             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2540               GetPixelIndex(image,q),q);
2541             p=PushShortPixel(quantum_info->endian,p,&pixel);
2542             SetPixelAlpha(image,ClampToQuantum((MagickRealType)
2543               QuantumRange*HalfToSinglePrecision(pixel)),q);
2544             p+=quantum_info->pad;
2545             q+=GetPixelChannels(image);
2546           }
2547           break;
2548         }
2549       for (x=0; x < (ssize_t) number_pixels; x++)
2550       {
2551         p=PushShortPixel(quantum_info->endian,p,&pixel);
2552         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2553         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2554           GetPixelIndex(image,q),q);
2555         p=PushShortPixel(quantum_info->endian,p,&pixel);
2556         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
2557         p+=quantum_info->pad;
2558         q+=GetPixelChannels(image);
2559       }
2560       break;
2561     }
2562     case 32:
2563     {
2564       unsigned int
2565         pixel;
2566
2567       if (quantum_info->format == FloatingPointQuantumFormat)
2568         {
2569           float
2570             pixel;
2571
2572           for (x=0; x < (ssize_t) number_pixels; x++)
2573           {
2574             p=PushFloatPixel(quantum_info,p,&pixel);
2575             SetPixelIndex(image,PushColormapIndex(image,
2576               ClampToQuantum(pixel),&range_exception),q);
2577             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2578               GetPixelIndex(image,q),q);
2579             p=PushFloatPixel(quantum_info,p,&pixel);
2580             SetPixelAlpha(image,ClampToQuantum(pixel),q);
2581             p+=quantum_info->pad;
2582             q+=GetPixelChannels(image);
2583           }
2584           break;
2585         }
2586       for (x=0; x < (ssize_t) number_pixels; x++)
2587       {
2588         p=PushLongPixel(quantum_info->endian,p,&pixel);
2589         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2590         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2591           GetPixelIndex(image,q),q);
2592         p=PushLongPixel(quantum_info->endian,p,&pixel);
2593         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
2594         p+=quantum_info->pad;
2595         q+=GetPixelChannels(image);
2596       }
2597       break;
2598     }
2599     case 64:
2600     {
2601       if (quantum_info->format == FloatingPointQuantumFormat)
2602         {
2603           double
2604             pixel;
2605
2606           for (x=0; x < (ssize_t) number_pixels; x++)
2607           {
2608             p=PushDoublePixel(quantum_info,p,&pixel);
2609             SetPixelIndex(image,PushColormapIndex(image,ClampToQuantum(pixel),
2610               &range_exception),q);
2611             SetPixelInfoPixel(image,image->colormap+(ssize_t)
2612               GetPixelIndex(image,q),q);
2613             p=PushDoublePixel(quantum_info,p,&pixel);
2614             SetPixelAlpha(image,ClampToQuantum(pixel),q);
2615             p+=quantum_info->pad;
2616             q+=GetPixelChannels(image);
2617           }
2618           break;
2619         }
2620     }
2621     default:
2622     {
2623       range=GetQuantumRange(quantum_info->depth);
2624       for (x=0; x < (ssize_t) number_pixels; x++)
2625       {
2626         p=PushQuantumPixel(quantum_info,p,&pixel);
2627         SetPixelIndex(image,PushColormapIndex(image,pixel,&range_exception),q);
2628         SetPixelInfoPixel(image,image->colormap+(ssize_t)
2629           GetPixelIndex(image,q),q);
2630         p=PushQuantumPixel(quantum_info,p,&pixel);
2631         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
2632         p+=quantum_info->pad;
2633         q+=GetPixelChannels(image);
2634       }
2635       break;
2636     }
2637   }
2638   if (range_exception != MagickFalse)
2639     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
2640       "InvalidColormapIndex","'%s'",image->filename);
2641 }
2642
2643 static void ImportOpacityQuantum(const Image *image,QuantumInfo *quantum_info,
2644   const MagickSizeType number_pixels,const unsigned char *restrict p,
2645   Quantum *restrict q,ExceptionInfo *exception)
2646 {
2647   QuantumAny
2648     range;
2649
2650   register ssize_t
2651     x;
2652
2653   unsigned int
2654     pixel;
2655
2656   switch (quantum_info->depth)
2657   {
2658     case 8:
2659     {
2660       unsigned char
2661         pixel;
2662
2663       for (x=0; x < (ssize_t) number_pixels; x++)
2664       {
2665         p=PushCharPixel(p,&pixel);
2666         SetPixelOpacity(image,ScaleCharToQuantum(pixel),q);
2667         p+=quantum_info->pad;
2668         q+=GetPixelChannels(image);
2669       }
2670       break;
2671     }
2672     case 16:
2673     {
2674       unsigned short
2675         pixel;
2676
2677       if (quantum_info->format == FloatingPointQuantumFormat)
2678         {
2679           for (x=0; x < (ssize_t) number_pixels; x++)
2680           {
2681             p=PushShortPixel(quantum_info->endian,p,&pixel);
2682             SetPixelOpacity(image,ClampToQuantum((MagickRealType) QuantumRange*
2683               HalfToSinglePrecision(pixel)),q);
2684             p+=quantum_info->pad;
2685             q+=GetPixelChannels(image);
2686           }
2687           break;
2688         }
2689       for (x=0; x < (ssize_t) number_pixels; x++)
2690       {
2691         p=PushShortPixel(quantum_info->endian,p,&pixel);
2692         SetPixelOpacity(image,ScaleShortToQuantum(pixel),q);
2693         p+=quantum_info->pad;
2694         q+=GetPixelChannels(image);
2695       }
2696       break;
2697     }
2698     case 32:
2699     {
2700       unsigned int
2701         pixel;
2702
2703       if (quantum_info->format == FloatingPointQuantumFormat)
2704         {
2705           float
2706             pixel;
2707
2708           for (x=0; x < (ssize_t) number_pixels; x++)
2709           {
2710             p=PushFloatPixel(quantum_info,p,&pixel);
2711             SetPixelOpacity(image,ClampToQuantum(pixel),q);
2712             p+=quantum_info->pad;
2713             q+=GetPixelChannels(image);
2714           }
2715           break;
2716         }
2717       for (x=0; x < (ssize_t) number_pixels; x++)
2718       {
2719         p=PushLongPixel(quantum_info->endian,p,&pixel);
2720         SetPixelOpacity(image,ScaleLongToQuantum(pixel),q);
2721         p+=quantum_info->pad;
2722         q+=GetPixelChannels(image);
2723       }
2724       break;
2725     }
2726     case 64:
2727     {
2728       if (quantum_info->format == FloatingPointQuantumFormat)
2729         {
2730           double
2731             pixel;
2732
2733           for (x=0; x < (ssize_t) number_pixels; x++)
2734           {
2735             p=PushDoublePixel(quantum_info,p,&pixel);
2736             SetPixelOpacity(image,ClampToQuantum(pixel),q);
2737             p+=quantum_info->pad;
2738             q+=GetPixelChannels(image);
2739           }
2740           break;
2741         }
2742     }
2743     default:
2744     {
2745       range=GetQuantumRange(quantum_info->depth);
2746       for (x=0; x < (ssize_t) number_pixels; x++)
2747       {
2748         p=PushQuantumPixel(quantum_info,p,&pixel);
2749         SetPixelOpacity(image,ScaleAnyToQuantum(pixel,range),q);
2750         p+=quantum_info->pad;
2751         q+=GetPixelChannels(image);
2752       }
2753       break;
2754     }
2755   }
2756 }
2757 static void ImportRedQuantum(const Image *image,QuantumInfo *quantum_info,
2758   const MagickSizeType number_pixels,const unsigned char *restrict p,
2759   Quantum *restrict q,ExceptionInfo *exception)
2760 {
2761   QuantumAny
2762     range;
2763
2764   register ssize_t
2765     x;
2766
2767   unsigned int
2768     pixel;
2769
2770   switch (quantum_info->depth)
2771   {
2772     case 8:
2773     {
2774       unsigned char
2775         pixel;
2776
2777       for (x=0; x < (ssize_t) number_pixels; x++)
2778       {
2779         p=PushCharPixel(p,&pixel);
2780         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
2781         p+=quantum_info->pad;
2782         q+=GetPixelChannels(image);
2783       }
2784       break;
2785     }
2786     case 16:
2787     {
2788       unsigned short
2789         pixel;
2790
2791       if (quantum_info->format == FloatingPointQuantumFormat)
2792         {
2793           for (x=0; x < (ssize_t) number_pixels; x++)
2794           {
2795             p=PushShortPixel(quantum_info->endian,p,&pixel);
2796             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
2797               HalfToSinglePrecision(pixel)),q);
2798             p+=quantum_info->pad;
2799             q+=GetPixelChannels(image);
2800           }
2801           break;
2802         }
2803       for (x=0; x < (ssize_t) number_pixels; x++)
2804       {
2805         p=PushShortPixel(quantum_info->endian,p,&pixel);
2806         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
2807         p+=quantum_info->pad;
2808         q+=GetPixelChannels(image);
2809       }
2810       break;
2811     }
2812     case 32:
2813     {
2814       unsigned int
2815         pixel;
2816
2817       if (quantum_info->format == FloatingPointQuantumFormat)
2818         {
2819           float
2820             pixel;
2821
2822           for (x=0; x < (ssize_t) number_pixels; x++)
2823           {
2824             p=PushFloatPixel(quantum_info,p,&pixel);
2825             SetPixelRed(image,ClampToQuantum(pixel),q);
2826             p+=quantum_info->pad;
2827             q+=GetPixelChannels(image);
2828           }
2829           break;
2830         }
2831       for (x=0; x < (ssize_t) number_pixels; x++)
2832       {
2833         p=PushLongPixel(quantum_info->endian,p,&pixel);
2834         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
2835         p+=quantum_info->pad;
2836         q+=GetPixelChannels(image);
2837       }
2838       break;
2839     }
2840     case 64:
2841     {
2842       if (quantum_info->format == FloatingPointQuantumFormat)
2843         {
2844           double
2845             pixel;
2846
2847           for (x=0; x < (ssize_t) number_pixels; x++)
2848           {
2849             p=PushDoublePixel(quantum_info,p,&pixel);
2850             SetPixelRed(image,ClampToQuantum(pixel),q);
2851             p+=quantum_info->pad;
2852             q+=GetPixelChannels(image);
2853           }
2854           break;
2855         }
2856     }
2857     default:
2858     {
2859       range=GetQuantumRange(quantum_info->depth);
2860       for (x=0; x < (ssize_t) number_pixels; x++)
2861       {
2862         p=PushQuantumPixel(quantum_info,p,&pixel);
2863         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2864         p+=quantum_info->pad;
2865         q+=GetPixelChannels(image);
2866       }
2867       break;
2868     }
2869   }
2870 }
2871
2872 static void ImportRGBQuantum(const Image *image,QuantumInfo *quantum_info,
2873   const MagickSizeType number_pixels,const unsigned char *restrict p,
2874   Quantum *restrict q,ExceptionInfo *exception)
2875 {
2876   QuantumAny
2877     range;
2878
2879   register ssize_t
2880     x;
2881
2882   ssize_t
2883     bit;
2884
2885   unsigned int
2886     pixel;
2887
2888   switch (quantum_info->depth)
2889   {
2890     case 8:
2891     {
2892       unsigned char
2893         pixel;
2894
2895       for (x=0; x < (ssize_t) number_pixels; x++)
2896       {
2897         p=PushCharPixel(p,&pixel);
2898         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
2899         p=PushCharPixel(p,&pixel);
2900         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
2901         p=PushCharPixel(p,&pixel);
2902         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
2903         SetPixelAlpha(image,OpaqueAlpha,q);
2904         p+=quantum_info->pad;
2905         q+=GetPixelChannels(image);
2906       }
2907       break;
2908     }
2909     case 10:
2910     {
2911       range=GetQuantumRange(quantum_info->depth);
2912       if (quantum_info->pack == MagickFalse)
2913         {
2914           for (x=0; x < (ssize_t) number_pixels; x++)
2915           {
2916             p=PushLongPixel(quantum_info->endian,p,&pixel);
2917             SetPixelRed(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,range),q);
2918             SetPixelGreen(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
2919               q);
2920             SetPixelBlue(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,range),q);
2921             p+=quantum_info->pad;
2922             q+=GetPixelChannels(image);
2923           }
2924           break;
2925         }
2926       if (quantum_info->quantum == 32U)
2927         {
2928           for (x=0; x < (ssize_t) number_pixels; x++)
2929           {
2930             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2931             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2932             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2933             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
2934             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2935             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
2936             q+=GetPixelChannels(image);
2937           }
2938           break;
2939         }
2940       for (x=0; x < (ssize_t) number_pixels; x++)
2941       {
2942         p=PushQuantumPixel(quantum_info,p,&pixel);
2943         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2944         p=PushQuantumPixel(quantum_info,p,&pixel);
2945         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
2946         p=PushQuantumPixel(quantum_info,p,&pixel);
2947         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
2948         q+=GetPixelChannels(image);
2949       }
2950       break;
2951     }
2952     case 12:
2953     {
2954       range=GetQuantumRange(quantum_info->depth);
2955       if (quantum_info->pack == MagickFalse)
2956         {
2957           unsigned short
2958             pixel;
2959
2960           for (x=0; x < (ssize_t) (3*number_pixels-1); x+=2)
2961           {
2962             p=PushShortPixel(quantum_info->endian,p,&pixel);
2963             switch (x % 3)
2964             {
2965               default:
2966               case 0:
2967               {
2968                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2969                   range),q);
2970                 break;
2971               }
2972               case 1:
2973               {
2974                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2975                   range),q);
2976                 break;
2977               }
2978               case 2:
2979               {
2980                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2981                   range),q);
2982                 q+=GetPixelChannels(image);
2983                 break;
2984               }
2985             }
2986             p=PushShortPixel(quantum_info->endian,p,&pixel);
2987             switch ((x+1) % 3)
2988             {
2989               default:
2990               case 0:
2991               {
2992                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2993                   range),q);
2994                 break;
2995               }
2996               case 1:
2997               {
2998                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2999                   range),q);
3000                 break;
3001               }
3002               case 2:
3003               {
3004                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3005                   range),q);
3006                 q+=GetPixelChannels(image);
3007                 break;
3008               }
3009             }
3010             p+=quantum_info->pad;
3011           }
3012           for (bit=0; bit < (ssize_t) (3*number_pixels % 2); bit++)
3013           {
3014             p=PushShortPixel(quantum_info->endian,p,&pixel);
3015             switch ((x+bit) % 3)
3016             {
3017               default:
3018               case 0:
3019               {
3020                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3021                   range),q);
3022                 break;
3023               }
3024               case 1:
3025               {
3026                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3027                   range),q);
3028                 break;
3029               }
3030               case 2:
3031               {
3032                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3033                   range),q);
3034                 q+=GetPixelChannels(image);
3035                 break;
3036               }
3037             }
3038             p+=quantum_info->pad;
3039           }
3040           if (bit != 0)
3041             p++;
3042           break;
3043         }
3044       if (quantum_info->quantum == 32U)
3045         {
3046           for (x=0; x < (ssize_t) number_pixels; x++)
3047           {
3048             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3049             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3050             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3051             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3052             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3053             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3054             q+=GetPixelChannels(image);
3055           }
3056           break;
3057         }
3058       for (x=0; x < (ssize_t) number_pixels; x++)
3059       {
3060         p=PushQuantumPixel(quantum_info,p,&pixel);
3061         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3062         p=PushQuantumPixel(quantum_info,p,&pixel);
3063         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3064         p=PushQuantumPixel(quantum_info,p,&pixel);
3065         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3066         q+=GetPixelChannels(image);
3067       }
3068       break;
3069     }
3070     case 16:
3071     {
3072       unsigned short
3073         pixel;
3074
3075       if (quantum_info->format == FloatingPointQuantumFormat)
3076         {
3077           for (x=0; x < (ssize_t) number_pixels; x++)
3078           {
3079             p=PushShortPixel(quantum_info->endian,p,&pixel);
3080             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
3081               HalfToSinglePrecision(pixel)),q);
3082             p=PushShortPixel(quantum_info->endian,p,&pixel);
3083             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
3084               HalfToSinglePrecision(pixel)),q);
3085             p=PushShortPixel(quantum_info->endian,p,&pixel);
3086             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
3087               HalfToSinglePrecision(pixel)),q);
3088             p+=quantum_info->pad;
3089             q+=GetPixelChannels(image);
3090           }
3091           break;
3092         }
3093       for (x=0; x < (ssize_t) number_pixels; x++)
3094       {
3095         p=PushShortPixel(quantum_info->endian,p,&pixel);
3096         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
3097         p=PushShortPixel(quantum_info->endian,p,&pixel);
3098         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
3099         p=PushShortPixel(quantum_info->endian,p,&pixel);
3100         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
3101         p+=quantum_info->pad;
3102         q+=GetPixelChannels(image);
3103       }
3104       break;
3105     }
3106     case 32:
3107     {
3108       unsigned int
3109         pixel;
3110
3111       if (quantum_info->format == FloatingPointQuantumFormat)
3112         {
3113           float
3114             pixel;
3115
3116           for (x=0; x < (ssize_t) number_pixels; x++)
3117           {
3118             p=PushFloatPixel(quantum_info,p,&pixel);
3119             SetPixelRed(image,ClampToQuantum(pixel),q);
3120             p=PushFloatPixel(quantum_info,p,&pixel);
3121             SetPixelGreen(image,ClampToQuantum(pixel),q);
3122             p=PushFloatPixel(quantum_info,p,&pixel);
3123             SetPixelBlue(image,ClampToQuantum(pixel),q);
3124             p+=quantum_info->pad;
3125             q+=GetPixelChannels(image);
3126           }
3127           break;
3128         }
3129       for (x=0; x < (ssize_t) number_pixels; x++)
3130       {
3131         p=PushLongPixel(quantum_info->endian,p,&pixel);
3132         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
3133         p=PushLongPixel(quantum_info->endian,p,&pixel);
3134         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
3135         p=PushLongPixel(quantum_info->endian,p,&pixel);
3136         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
3137         p+=quantum_info->pad;
3138         q+=GetPixelChannels(image);
3139       }
3140       break;
3141     }
3142     case 64:
3143     {
3144       if (quantum_info->format == FloatingPointQuantumFormat)
3145         {
3146           double
3147             pixel;
3148
3149           for (x=0; x < (ssize_t) number_pixels; x++)
3150           {
3151             p=PushDoublePixel(quantum_info,p,&pixel);
3152             SetPixelRed(image,ClampToQuantum(pixel),q);
3153             p=PushDoublePixel(quantum_info,p,&pixel);
3154             SetPixelGreen(image,ClampToQuantum(pixel),q);
3155             p=PushDoublePixel(quantum_info,p,&pixel);
3156             SetPixelBlue(image,ClampToQuantum(pixel),q);
3157             p+=quantum_info->pad;
3158             q+=GetPixelChannels(image);
3159           }
3160           break;
3161         }
3162     }
3163     default:
3164     {
3165       range=GetQuantumRange(quantum_info->depth);
3166       for (x=0; x < (ssize_t) number_pixels; x++)
3167       {
3168         p=PushQuantumPixel(quantum_info,p,&pixel);
3169         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3170         p=PushQuantumPixel(quantum_info,p,&pixel);
3171         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3172         p=PushQuantumPixel(quantum_info,p,&pixel);
3173         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3174         q+=GetPixelChannels(image);
3175       }
3176       break;
3177     }
3178   }
3179 }
3180
3181 static void ImportRGBAQuantum(const Image *image,QuantumInfo *quantum_info,
3182   const MagickSizeType number_pixels,const unsigned char *restrict p,
3183   Quantum *restrict q,ExceptionInfo *exception)
3184 {
3185   QuantumAny
3186     range;
3187
3188   register ssize_t
3189     x;
3190
3191   unsigned int
3192     pixel;
3193
3194   switch (quantum_info->depth)
3195   {
3196     case 8:
3197     {
3198       unsigned char
3199         pixel;
3200
3201       for (x=0; x < (ssize_t) number_pixels; x++)
3202       {
3203         p=PushCharPixel(p,&pixel);
3204         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
3205         p=PushCharPixel(p,&pixel);
3206         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
3207         p=PushCharPixel(p,&pixel);
3208         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
3209         p=PushCharPixel(p,&pixel);
3210         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
3211         p+=quantum_info->pad;
3212         q+=GetPixelChannels(image);
3213       }
3214       break;
3215     }
3216     case 10:
3217     {
3218       pixel=0;
3219       if (quantum_info->pack == MagickFalse)
3220         {
3221           register ssize_t
3222             i;
3223
3224           size_t
3225             quantum;
3226
3227           ssize_t
3228             n;
3229
3230           n=0;
3231           quantum=0;
3232           for (x=0; x < (ssize_t) number_pixels; x++)
3233           {
3234             for (i=0; i < 4; i++)
3235             {
3236               switch (n % 3)
3237               {
3238                 case 0:
3239                 {
3240                   p=PushLongPixel(quantum_info->endian,p,&pixel);
3241                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3242                     (((pixel >> 22) & 0x3ff) << 6)));
3243                   break;
3244                 }
3245                 case 1:
3246                 {
3247                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3248                     (((pixel >> 12) & 0x3ff) << 6)));
3249                   break;
3250                 }
3251                 case 2:
3252                 {
3253                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3254                     (((pixel >> 2) & 0x3ff) << 6)));
3255                   break;
3256                 }
3257               }
3258               switch (i)
3259               {
3260                 case 0: SetPixelRed(image,(Quantum) quantum,q); break;
3261                 case 1: SetPixelGreen(image,(Quantum) quantum,q); break;
3262                 case 2: SetPixelBlue(image,(Quantum) quantum,q); break;
3263                 case 3: SetPixelAlpha(image,(Quantum) quantum,q); break;
3264               }
3265               n++;
3266             }
3267             p+=quantum_info->pad;
3268             q+=GetPixelChannels(image);
3269           }
3270           break;
3271         }
3272       for (x=0; x < (ssize_t) number_pixels; x++)
3273       {
3274         p=PushQuantumPixel(quantum_info,p,&pixel);
3275         SetPixelRed(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),q);
3276         p=PushQuantumPixel(quantum_info,p,&pixel);
3277         SetPixelGreen(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3278           q);
3279         p=PushQuantumPixel(quantum_info,p,&pixel);
3280         SetPixelBlue(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3281           q);
3282         p=PushQuantumPixel(quantum_info,p,&pixel);
3283         SetPixelAlpha(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3284           q);
3285         q+=GetPixelChannels(image);
3286       }
3287       break;
3288     }
3289     case 16:
3290     {
3291       unsigned short
3292         pixel;
3293
3294       if (quantum_info->format == FloatingPointQuantumFormat)
3295         {
3296           for (x=0; x < (ssize_t) number_pixels; x++)
3297           {
3298             p=PushShortPixel(quantum_info->endian,p,&pixel);
3299             SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
3300               HalfToSinglePrecision(pixel)),q);
3301             p=PushShortPixel(quantum_info->endian,p,&pixel);
3302             SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
3303               HalfToSinglePrecision(pixel)),q);
3304             p=PushShortPixel(quantum_info->endian,p,&pixel);
3305             SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
3306               HalfToSinglePrecision(pixel)),q);
3307             p=PushShortPixel(quantum_info->endian,p,&pixel);
3308             SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
3309               HalfToSinglePrecision(pixel)),q);
3310             p+=quantum_info->pad;
3311             q+=GetPixelChannels(image);
3312           }
3313           break;
3314         }
3315       for (x=0; x < (ssize_t) number_pixels; x++)
3316       {
3317         p=PushShortPixel(quantum_info->endian,p,&pixel);
3318         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
3319         p=PushShortPixel(quantum_info->endian,p,&pixel);
3320         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
3321         p=PushShortPixel(quantum_info->endian,p,&pixel);
3322         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
3323         p=PushShortPixel(quantum_info->endian,p,&pixel);
3324         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
3325         p+=quantum_info->pad;
3326         q+=GetPixelChannels(image);
3327       }
3328       break;
3329     }
3330     case 32:
3331     {
3332       unsigned int
3333         pixel;
3334
3335       if (quantum_info->format == FloatingPointQuantumFormat)
3336         {
3337           float
3338             pixel;
3339
3340           for (x=0; x < (ssize_t) number_pixels; x++)
3341           {
3342             p=PushFloatPixel(quantum_info,p,&pixel);
3343             SetPixelRed(image,ClampToQuantum(pixel),q);
3344             p=PushFloatPixel(quantum_info,p,&pixel);
3345             SetPixelGreen(image,ClampToQuantum(pixel),q);
3346             p=PushFloatPixel(quantum_info,p,&pixel);
3347             SetPixelBlue(image,ClampToQuantum(pixel),q);
3348             p=PushFloatPixel(quantum_info,p,&pixel);
3349             SetPixelAlpha(image,ClampToQuantum(pixel),q);
3350             p+=quantum_info->pad;
3351             q+=GetPixelChannels(image);
3352           }
3353           break;
3354         }
3355       for (x=0; x < (ssize_t) number_pixels; x++)
3356       {
3357         p=PushLongPixel(quantum_info->endian,p,&pixel);
3358         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
3359         p=PushLongPixel(quantum_info->endian,p,&pixel);
3360         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
3361         p=PushLongPixel(quantum_info->endian,p,&pixel);
3362         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
3363         p=PushLongPixel(quantum_info->endian,p,&pixel);
3364         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
3365         p+=quantum_info->pad;
3366         q+=GetPixelChannels(image);
3367       }
3368       break;
3369     }
3370     case 64:
3371     {
3372       if (quantum_info->format == FloatingPointQuantumFormat)
3373         {
3374           double
3375             pixel;
3376
3377           for (x=0; x < (ssize_t) number_pixels; x++)
3378           {
3379             p=PushDoublePixel(quantum_info,p,&pixel);
3380             SetPixelRed(image,ClampToQuantum(pixel),q);
3381             p=PushDoublePixel(quantum_info,p,&pixel);
3382             SetPixelGreen(image,ClampToQuantum(pixel),q);
3383             p=PushDoublePixel(quantum_info,p,&pixel);
3384             SetPixelBlue(image,ClampToQuantum(pixel),q);
3385             p=PushDoublePixel(quantum_info,p,&pixel);
3386             SetPixelAlpha(image,ClampToQuantum(pixel),q);
3387             p+=quantum_info->pad;
3388             q+=GetPixelChannels(image);
3389           }
3390           break;
3391         }
3392     }
3393     default:
3394     {
3395       range=GetQuantumRange(quantum_info->depth);
3396       for (x=0; x < (ssize_t) number_pixels; x++)
3397       {
3398         p=PushQuantumPixel(quantum_info,p,&pixel);
3399         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3400         p=PushQuantumPixel(quantum_info,p,&pixel);
3401         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3402         p=PushQuantumPixel(quantum_info,p,&pixel);
3403         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3404         p=PushQuantumPixel(quantum_info,p,&pixel);
3405         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
3406         q+=GetPixelChannels(image);
3407       }
3408       break;
3409     }
3410   }
3411 }
3412
3413 MagickExport size_t ImportQuantumPixels(const Image *image,
3414   CacheView *image_view,QuantumInfo *quantum_info,
3415   const QuantumType quantum_type,const unsigned char *pixels,
3416   ExceptionInfo *exception)
3417 {
3418   MagickSizeType
3419     number_pixels;
3420
3421   register const unsigned char
3422     *restrict p;
3423
3424   register ssize_t
3425     x;
3426
3427   register Quantum
3428     *restrict q;
3429
3430   size_t
3431     extent;
3432
3433   assert(image != (Image *) NULL);
3434   assert(image->signature == MagickSignature);
3435   if (image->debug != MagickFalse)
3436     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3437   assert(quantum_info != (QuantumInfo *) NULL);
3438   assert(quantum_info->signature == MagickSignature);
3439   if (pixels == (const unsigned char *) NULL)
3440     pixels=GetQuantumPixels(quantum_info);
3441   x=0;
3442   p=pixels;
3443   if (image_view == (CacheView *) NULL)
3444     {
3445       number_pixels=GetImageExtent(image);
3446       q=GetAuthenticPixelQueue(image);
3447     }
3448   else
3449     {
3450       number_pixels=GetCacheViewExtent(image_view);
3451       q=GetCacheViewAuthenticPixelQueue(image_view);
3452     }
3453   ResetQuantumState(quantum_info);
3454   extent=GetQuantumExtent(image,quantum_info,quantum_type);
3455   switch (quantum_type)
3456   {
3457     case AlphaQuantum:
3458     {
3459       ImportAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3460       break;
3461     }
3462     case BGRQuantum:
3463     {
3464       ImportBGRQuantum(image,quantum_info,number_pixels,p,q,exception);
3465       break;
3466     }
3467     case BGRAQuantum:
3468     case BGROQuantum:
3469     {
3470       ImportBGRAQuantum(image,quantum_info,number_pixels,p,q,exception);
3471       break;
3472     }
3473     case BlackQuantum:
3474     {
3475       ImportBlackQuantum(image,quantum_info,number_pixels,p,q,exception);
3476       break;
3477     }
3478     case BlueQuantum:
3479     case YellowQuantum:
3480     {
3481       ImportBlueQuantum(image,quantum_info,number_pixels,p,q,exception);
3482       break;
3483     }
3484     case CMYKQuantum:
3485     {
3486       ImportCMYKQuantum(image,quantum_info,number_pixels,p,q,exception);
3487       break;
3488     }
3489     case CMYKAQuantum:
3490     case CMYKOQuantum:
3491     {
3492       ImportCMYKAQuantum(image,quantum_info,number_pixels,p,q,exception);
3493       break;
3494     }
3495     case CbYCrYQuantum:
3496     {
3497       ImportCbYCrYQuantum(image,quantum_info,number_pixels,p,q,exception);
3498       break;
3499     }
3500     case GrayQuantum:
3501     {
3502       ImportGrayQuantum(image,quantum_info,number_pixels,p,q,exception);
3503       break;
3504     }
3505     case GrayAlphaQuantum:
3506     {
3507       ImportGrayAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3508       break;
3509     }
3510     case GreenQuantum:
3511     case MagentaQuantum:
3512     {
3513       ImportGreenQuantum(image,quantum_info,number_pixels,p,q,exception);
3514       break;
3515     }
3516     case IndexQuantum:
3517     {
3518       ImportIndexQuantum(image,quantum_info,number_pixels,p,q,exception);
3519       break;
3520     }
3521     case IndexAlphaQuantum:
3522     {
3523       ImportIndexAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3524       break;
3525     }
3526     case OpacityQuantum:
3527     {
3528       ImportOpacityQuantum(image,quantum_info,number_pixels,p,q,exception);
3529       break;
3530     }
3531     case RedQuantum:
3532     case CyanQuantum:
3533     {
3534       ImportRedQuantum(image,quantum_info,number_pixels,p,q,exception);
3535       break;
3536     }
3537     case RGBQuantum:
3538     case CbYCrQuantum:
3539     {
3540       ImportRGBQuantum(image,quantum_info,number_pixels,p,q,exception);
3541       break;
3542     }
3543     case RGBAQuantum:
3544     case RGBOQuantum:
3545     case CbYCrAQuantum:
3546     {
3547       ImportRGBAQuantum(image,quantum_info,number_pixels,p,q,exception);
3548       break;
3549     }
3550     default:
3551       break;
3552   }
3553   if ((quantum_type == CbYCrQuantum) || (quantum_type == CbYCrAQuantum))
3554     {
3555       Quantum
3556         quantum;
3557
3558       register Quantum
3559         *restrict q;
3560
3561       q=GetAuthenticPixelQueue(image);
3562       if (image_view != (CacheView *) NULL)
3563         q=GetCacheViewAuthenticPixelQueue(image_view);
3564       for (x=0; x < (ssize_t) number_pixels; x++)
3565       {
3566         quantum=GetPixelRed(image,q);
3567         SetPixelRed(image,GetPixelGreen(image,q),q);
3568         SetPixelGreen(image,quantum,q);
3569         q+=GetPixelChannels(image);
3570       }
3571     }
3572   if ((quantum_type == RGBOQuantum) || (quantum_type == CMYKOQuantum))
3573     {
3574       register Quantum
3575         *restrict q;
3576
3577       q=GetAuthenticPixelQueue(image);
3578       if (image_view != (CacheView *) NULL)
3579         q=GetCacheViewAuthenticPixelQueue(image_view);
3580       for (x=0; x < (ssize_t) number_pixels; x++)
3581       {
3582         SetPixelAlpha(image,GetPixelAlpha(image,q),q);
3583         q+=GetPixelChannels(image);
3584       }
3585     }
3586   if (quantum_info->alpha_type == DisassociatedQuantumAlpha)
3587     {
3588       MagickRealType
3589         gamma,
3590         Sa;
3591
3592       register Quantum
3593         *restrict q;
3594
3595       /*
3596         Disassociate alpha.
3597       */
3598       q=GetAuthenticPixelQueue(image);
3599       if (image_view != (CacheView *) NULL)
3600         q=GetCacheViewAuthenticPixelQueue(image_view);
3601       for (x=0; x < (ssize_t) number_pixels; x++)
3602       {
3603         register ssize_t
3604           i;
3605
3606         if (GetPixelMask(image,q) != 0)
3607           {
3608             q+=GetPixelChannels(image);
3609             continue;
3610           }
3611         Sa=QuantumScale*GetPixelAlpha(image,q);
3612         gamma=1.0/(fabs(Sa) <= MagickEpsilon ? 1.0 : Sa);
3613         for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3614         {
3615           PixelChannel
3616             channel;
3617
3618           PixelTrait
3619             traits;
3620
3621           channel=GetPixelChannelMapChannel(image,i);
3622           traits=GetPixelChannelMapTraits(image,channel);
3623           if ((traits & UpdatePixelTrait) == 0)
3624             continue;
3625           q[i]=ClampToQuantum(gamma*q[i]);
3626         }
3627         q+=GetPixelChannels(image);
3628       }
3629     }
3630   return(extent);
3631 }