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