]> 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-2008 ImageMagick Studio LLC, a non-profit organization      %
26 %  dedicated to making software imaging solutions freely available.           %
27 %                                                                             %
28 %  You may not use this file except in compliance with the License.  You may  %
29 %  obtain a copy of the License at                                            %
30 %                                                                             %
31 %    http://www.imagemagick.org/script/license.php                            %
32 %                                                                             %
33 %  Unless required by applicable law or agreed to in writing, software        %
34 %  distributed under the License is distributed on an "AS IS" BASIS,          %
35 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
36 %  See the License for the specific language governing permissions and        %
37 %  limitations under the License.                                             %
38 %                                                                             %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %
41 %
42 */
43 \f
44 /*
45   Include declarations.
46 */
47 #include "MagickCore/studio.h"
48 #include "MagickCore/property.h"
49 #include "MagickCore/blob.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/color-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/cache.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/delegate.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/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[7]=(*pixels++);
135       quantum[6]=(*pixels++);
136       quantum[5]=(*pixels++);
137       quantum[5]=(*pixels++);
138       quantum[3]=(*pixels++);
139       quantum[2]=(*pixels++);
140       quantum[1]=(*pixels++);
141       quantum[0]=(*pixels++);
142       p=(double *) quantum;
143       *pixel=(*p);
144       *pixel-=quantum_info->minimum;
145       *pixel*=quantum_info->scale;
146       return(pixels);
147     }
148   quantum[0]=(*pixels++);
149   quantum[1]=(*pixels++);
150   quantum[2]=(*pixels++);
151   quantum[3]=(*pixels++);
152   quantum[4]=(*pixels++);
153   quantum[5]=(*pixels++);
154   quantum[6]=(*pixels++);
155   quantum[7]=(*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[3]=(*pixels++);
175       quantum[2]=(*pixels++);
176       quantum[1]=(*pixels++);
177       quantum[0]=(*pixels++);
178       p=(float *) quantum;
179       *pixel=(*p);
180       *pixel-=quantum_info->minimum;
181       *pixel*=quantum_info->scale;
182       return(pixels);
183     }
184   quantum[0]=(*pixels++);
185   quantum[1]=(*pixels++);
186   quantum[2]=(*pixels++);
187   quantum[3]=(*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 >> 2) & 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 >> 22) & 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 >> 2) & 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 >> 22) & 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 >> 2) & 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 >> 22) & 0x3ff,range),
1737                 q);
1738               q+=GetPixelChannels(image);
1739             }
1740           if (x++ < (ssize_t) number_pixels)
1741             {
1742               SetPixelGray(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
1743                 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 static void ImportRedQuantum(const Image *image,QuantumInfo *quantum_info,
2759   const MagickSizeType number_pixels,const unsigned char *restrict p,
2760   Quantum *restrict q,ExceptionInfo *exception)
2761 {
2762   QuantumAny
2763     range;
2764
2765   register ssize_t
2766     x;
2767
2768   unsigned int
2769     pixel;
2770
2771   switch (quantum_info->depth)
2772   {
2773     case 8:
2774     {
2775       unsigned char
2776         pixel;
2777
2778       for (x=0; x < (ssize_t) number_pixels; x++)
2779       {
2780         p=PushCharPixel(p,&pixel);
2781         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
2782         p+=quantum_info->pad;
2783         q+=GetPixelChannels(image);
2784       }
2785       break;
2786     }
2787     case 16:
2788     {
2789       unsigned short
2790         pixel;
2791
2792       if (quantum_info->format == FloatingPointQuantumFormat)
2793         {
2794           for (x=0; x < (ssize_t) number_pixels; x++)
2795           {
2796             p=PushShortPixel(quantum_info->endian,p,&pixel);
2797             SetPixelRed(image,ClampToQuantum(QuantumRange*
2798               HalfToSinglePrecision(pixel)),q);
2799             p+=quantum_info->pad;
2800             q+=GetPixelChannels(image);
2801           }
2802           break;
2803         }
2804       for (x=0; x < (ssize_t) number_pixels; x++)
2805       {
2806         p=PushShortPixel(quantum_info->endian,p,&pixel);
2807         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
2808         p+=quantum_info->pad;
2809         q+=GetPixelChannels(image);
2810       }
2811       break;
2812     }
2813     case 32:
2814     {
2815       unsigned int
2816         pixel;
2817
2818       if (quantum_info->format == FloatingPointQuantumFormat)
2819         {
2820           float
2821             pixel;
2822
2823           for (x=0; x < (ssize_t) number_pixels; x++)
2824           {
2825             p=PushFloatPixel(quantum_info,p,&pixel);
2826             SetPixelRed(image,ClampToQuantum(pixel),q);
2827             p+=quantum_info->pad;
2828             q+=GetPixelChannels(image);
2829           }
2830           break;
2831         }
2832       for (x=0; x < (ssize_t) number_pixels; x++)
2833       {
2834         p=PushLongPixel(quantum_info->endian,p,&pixel);
2835         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
2836         p+=quantum_info->pad;
2837         q+=GetPixelChannels(image);
2838       }
2839       break;
2840     }
2841     case 64:
2842     {
2843       if (quantum_info->format == FloatingPointQuantumFormat)
2844         {
2845           double
2846             pixel;
2847
2848           for (x=0; x < (ssize_t) number_pixels; x++)
2849           {
2850             p=PushDoublePixel(quantum_info,p,&pixel);
2851             SetPixelRed(image,ClampToQuantum(pixel),q);
2852             p+=quantum_info->pad;
2853             q+=GetPixelChannels(image);
2854           }
2855           break;
2856         }
2857     }
2858     default:
2859     {
2860       range=GetQuantumRange(quantum_info->depth);
2861       for (x=0; x < (ssize_t) number_pixels; x++)
2862       {
2863         p=PushQuantumPixel(quantum_info,p,&pixel);
2864         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2865         p+=quantum_info->pad;
2866         q+=GetPixelChannels(image);
2867       }
2868       break;
2869     }
2870   }
2871 }
2872
2873 static void ImportRGBQuantum(const Image *image,QuantumInfo *quantum_info,
2874   const MagickSizeType number_pixels,const unsigned char *restrict p,
2875   Quantum *restrict q,ExceptionInfo *exception)
2876 {
2877   QuantumAny
2878     range;
2879
2880   register ssize_t
2881     x;
2882
2883   ssize_t
2884     bit;
2885
2886   unsigned int
2887     pixel;
2888
2889   switch (quantum_info->depth)
2890   {
2891     case 8:
2892     {
2893       unsigned char
2894         pixel;
2895
2896       for (x=0; x < (ssize_t) number_pixels; x++)
2897       {
2898         p=PushCharPixel(p,&pixel);
2899         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
2900         p=PushCharPixel(p,&pixel);
2901         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
2902         p=PushCharPixel(p,&pixel);
2903         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
2904         SetPixelAlpha(image,OpaqueAlpha,q);
2905         p+=quantum_info->pad;
2906         q+=GetPixelChannels(image);
2907       }
2908       break;
2909     }
2910     case 10:
2911     {
2912       range=GetQuantumRange(quantum_info->depth);
2913       if (quantum_info->pack == MagickFalse)
2914         {
2915           for (x=0; x < (ssize_t) number_pixels; x++)
2916           {
2917             p=PushLongPixel(quantum_info->endian,p,&pixel);
2918             SetPixelRed(image,ScaleAnyToQuantum((pixel >> 22) & 0x3ff,range),q);
2919             SetPixelGreen(image,ScaleAnyToQuantum((pixel >> 12) & 0x3ff,range),
2920               q);
2921             SetPixelBlue(image,ScaleAnyToQuantum((pixel >> 2) & 0x3ff,range),q);
2922             p+=quantum_info->pad;
2923             q+=GetPixelChannels(image);
2924           }
2925           break;
2926         }
2927       if (quantum_info->quantum == 32U)
2928         {
2929           for (x=0; x < (ssize_t) number_pixels; x++)
2930           {
2931             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2932             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2933             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2934             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
2935             p=PushQuantumLongPixel(quantum_info,p,&pixel);
2936             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
2937             q+=GetPixelChannels(image);
2938           }
2939           break;
2940         }
2941       for (x=0; x < (ssize_t) number_pixels; x++)
2942       {
2943         p=PushQuantumPixel(quantum_info,p,&pixel);
2944         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
2945         p=PushQuantumPixel(quantum_info,p,&pixel);
2946         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
2947         p=PushQuantumPixel(quantum_info,p,&pixel);
2948         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
2949         q+=GetPixelChannels(image);
2950       }
2951       break;
2952     }
2953     case 12:
2954     {
2955       range=GetQuantumRange(quantum_info->depth);
2956       if (quantum_info->pack == MagickFalse)
2957         {
2958           unsigned short
2959             pixel;
2960
2961           for (x=0; x < (ssize_t) (3*number_pixels-1); x+=2)
2962           {
2963             p=PushShortPixel(quantum_info->endian,p,&pixel);
2964             switch (x % 3)
2965             {
2966               default:
2967               case 0:
2968               {
2969                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2970                   range),q);
2971                 break;
2972               }
2973               case 1:
2974               {
2975                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2976                   range),q);
2977                 break;
2978               }
2979               case 2:
2980               {
2981                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2982                   range),q);
2983                 q+=GetPixelChannels(image);
2984                 break;
2985               }
2986             }
2987             p=PushShortPixel(quantum_info->endian,p,&pixel);
2988             switch ((x+1) % 3)
2989             {
2990               default:
2991               case 0:
2992               {
2993                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
2994                   range),q);
2995                 break;
2996               }
2997               case 1:
2998               {
2999                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3000                   range),q);
3001                 break;
3002               }
3003               case 2:
3004               {
3005                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3006                   range),q);
3007                 q+=GetPixelChannels(image);
3008                 break;
3009               }
3010             }
3011             p+=quantum_info->pad;
3012           }
3013           for (bit=0; bit < (ssize_t) (3*number_pixels % 2); bit++)
3014           {
3015             p=PushShortPixel(quantum_info->endian,p,&pixel);
3016             switch ((x+bit) % 3)
3017             {
3018               default:
3019               case 0:
3020               {
3021                 SetPixelRed(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3022                   range),q);
3023                 break;
3024               }
3025               case 1:
3026               {
3027                 SetPixelGreen(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3028                   range),q);
3029                 break;
3030               }
3031               case 2:
3032               {
3033                 SetPixelBlue(image,ScaleAnyToQuantum((QuantumAny) (pixel >> 4),
3034                   range),q);
3035                 q+=GetPixelChannels(image);
3036                 break;
3037               }
3038             }
3039             p+=quantum_info->pad;
3040           }
3041           if (bit != 0)
3042             p++;
3043           break;
3044         }
3045       if (quantum_info->quantum == 32U)
3046         {
3047           for (x=0; x < (ssize_t) number_pixels; x++)
3048           {
3049             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3050             SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3051             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3052             SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3053             p=PushQuantumLongPixel(quantum_info,p,&pixel);
3054             SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3055             q+=GetPixelChannels(image);
3056           }
3057           break;
3058         }
3059       for (x=0; x < (ssize_t) number_pixels; x++)
3060       {
3061         p=PushQuantumPixel(quantum_info,p,&pixel);
3062         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3063         p=PushQuantumPixel(quantum_info,p,&pixel);
3064         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3065         p=PushQuantumPixel(quantum_info,p,&pixel);
3066         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3067         q+=GetPixelChannels(image);
3068       }
3069       break;
3070     }
3071     case 16:
3072     {
3073       unsigned short
3074         pixel;
3075
3076       if (quantum_info->format == FloatingPointQuantumFormat)
3077         {
3078           for (x=0; x < (ssize_t) number_pixels; x++)
3079           {
3080             p=PushShortPixel(quantum_info->endian,p,&pixel);
3081             SetPixelRed(image,ClampToQuantum(QuantumRange*
3082               HalfToSinglePrecision(pixel)),q);
3083             p=PushShortPixel(quantum_info->endian,p,&pixel);
3084             SetPixelGreen(image,ClampToQuantum(QuantumRange*
3085               HalfToSinglePrecision(pixel)),q);
3086             p=PushShortPixel(quantum_info->endian,p,&pixel);
3087             SetPixelBlue(image,ClampToQuantum(QuantumRange*
3088               HalfToSinglePrecision(pixel)),q);
3089             p+=quantum_info->pad;
3090             q+=GetPixelChannels(image);
3091           }
3092           break;
3093         }
3094       for (x=0; x < (ssize_t) number_pixels; x++)
3095       {
3096         p=PushShortPixel(quantum_info->endian,p,&pixel);
3097         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
3098         p=PushShortPixel(quantum_info->endian,p,&pixel);
3099         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
3100         p=PushShortPixel(quantum_info->endian,p,&pixel);
3101         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
3102         p+=quantum_info->pad;
3103         q+=GetPixelChannels(image);
3104       }
3105       break;
3106     }
3107     case 32:
3108     {
3109       unsigned int
3110         pixel;
3111
3112       if (quantum_info->format == FloatingPointQuantumFormat)
3113         {
3114           float
3115             pixel;
3116
3117           for (x=0; x < (ssize_t) number_pixels; x++)
3118           {
3119             p=PushFloatPixel(quantum_info,p,&pixel);
3120             SetPixelRed(image,ClampToQuantum(pixel),q);
3121             p=PushFloatPixel(quantum_info,p,&pixel);
3122             SetPixelGreen(image,ClampToQuantum(pixel),q);
3123             p=PushFloatPixel(quantum_info,p,&pixel);
3124             SetPixelBlue(image,ClampToQuantum(pixel),q);
3125             p+=quantum_info->pad;
3126             q+=GetPixelChannels(image);
3127           }
3128           break;
3129         }
3130       for (x=0; x < (ssize_t) number_pixels; x++)
3131       {
3132         p=PushLongPixel(quantum_info->endian,p,&pixel);
3133         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
3134         p=PushLongPixel(quantum_info->endian,p,&pixel);
3135         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
3136         p=PushLongPixel(quantum_info->endian,p,&pixel);
3137         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
3138         p+=quantum_info->pad;
3139         q+=GetPixelChannels(image);
3140       }
3141       break;
3142     }
3143     case 64:
3144     {
3145       if (quantum_info->format == FloatingPointQuantumFormat)
3146         {
3147           double
3148             pixel;
3149
3150           for (x=0; x < (ssize_t) number_pixels; x++)
3151           {
3152             p=PushDoublePixel(quantum_info,p,&pixel);
3153             SetPixelRed(image,ClampToQuantum(pixel),q);
3154             p=PushDoublePixel(quantum_info,p,&pixel);
3155             SetPixelGreen(image,ClampToQuantum(pixel),q);
3156             p=PushDoublePixel(quantum_info,p,&pixel);
3157             SetPixelBlue(image,ClampToQuantum(pixel),q);
3158             p+=quantum_info->pad;
3159             q+=GetPixelChannels(image);
3160           }
3161           break;
3162         }
3163     }
3164     default:
3165     {
3166       range=GetQuantumRange(quantum_info->depth);
3167       for (x=0; x < (ssize_t) number_pixels; x++)
3168       {
3169         p=PushQuantumPixel(quantum_info,p,&pixel);
3170         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3171         p=PushQuantumPixel(quantum_info,p,&pixel);
3172         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3173         p=PushQuantumPixel(quantum_info,p,&pixel);
3174         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3175         q+=GetPixelChannels(image);
3176       }
3177       break;
3178     }
3179   }
3180 }
3181
3182 static void ImportRGBAQuantum(const Image *image,QuantumInfo *quantum_info,
3183   const MagickSizeType number_pixels,const unsigned char *restrict p,
3184   Quantum *restrict q,ExceptionInfo *exception)
3185 {
3186   QuantumAny
3187     range;
3188
3189   register ssize_t
3190     x;
3191
3192   unsigned int
3193     pixel;
3194
3195   switch (quantum_info->depth)
3196   {
3197     case 8:
3198     {
3199       unsigned char
3200         pixel;
3201
3202       for (x=0; x < (ssize_t) number_pixels; x++)
3203       {
3204         p=PushCharPixel(p,&pixel);
3205         SetPixelRed(image,ScaleCharToQuantum(pixel),q);
3206         p=PushCharPixel(p,&pixel);
3207         SetPixelGreen(image,ScaleCharToQuantum(pixel),q);
3208         p=PushCharPixel(p,&pixel);
3209         SetPixelBlue(image,ScaleCharToQuantum(pixel),q);
3210         p=PushCharPixel(p,&pixel);
3211         SetPixelAlpha(image,ScaleCharToQuantum(pixel),q);
3212         p+=quantum_info->pad;
3213         q+=GetPixelChannels(image);
3214       }
3215       break;
3216     }
3217     case 10:
3218     {
3219       pixel=0;
3220       if (quantum_info->pack == MagickFalse)
3221         {
3222           register ssize_t
3223             i;
3224
3225           size_t
3226             quantum;
3227
3228           ssize_t
3229             n;
3230
3231           n=0;
3232           quantum=0;
3233           for (x=0; x < (ssize_t) number_pixels; x++)
3234           {
3235             for (i=0; i < 4; i++)
3236             {
3237               switch (n % 3)
3238               {
3239                 case 0:
3240                 {
3241                   p=PushLongPixel(quantum_info->endian,p,&pixel);
3242                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3243                     (((pixel >> 22) & 0x3ff) << 6)));
3244                   break;
3245                 }
3246                 case 1:
3247                 {
3248                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3249                     (((pixel >> 12) & 0x3ff) << 6)));
3250                   break;
3251                 }
3252                 case 2:
3253                 {
3254                   quantum=(size_t) (ScaleShortToQuantum((unsigned short)
3255                     (((pixel >> 2) & 0x3ff) << 6)));
3256                   break;
3257                 }
3258               }
3259               switch (i)
3260               {
3261                 case 0: SetPixelRed(image,(Quantum) quantum,q); break;
3262                 case 1: SetPixelGreen(image,(Quantum) quantum,q); break;
3263                 case 2: SetPixelBlue(image,(Quantum) quantum,q); break;
3264                 case 3: SetPixelAlpha(image,(Quantum) quantum,q); break;
3265               }
3266               n++;
3267             }
3268             p+=quantum_info->pad;
3269             q+=GetPixelChannels(image);
3270           }
3271           break;
3272         }
3273       for (x=0; x < (ssize_t) number_pixels; x++)
3274       {
3275         p=PushQuantumPixel(quantum_info,p,&pixel);
3276         SetPixelRed(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),q);
3277         p=PushQuantumPixel(quantum_info,p,&pixel);
3278         SetPixelGreen(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3279           q);
3280         p=PushQuantumPixel(quantum_info,p,&pixel);
3281         SetPixelBlue(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3282           q);
3283         p=PushQuantumPixel(quantum_info,p,&pixel);
3284         SetPixelAlpha(image,ScaleShortToQuantum((unsigned short) (pixel << 6)),
3285           q);
3286         q+=GetPixelChannels(image);
3287       }
3288       break;
3289     }
3290     case 16:
3291     {
3292       unsigned short
3293         pixel;
3294
3295       if (quantum_info->format == FloatingPointQuantumFormat)
3296         {
3297           for (x=0; x < (ssize_t) number_pixels; x++)
3298           {
3299             p=PushShortPixel(quantum_info->endian,p,&pixel);
3300             SetPixelRed(image,ClampToQuantum(QuantumRange*
3301               HalfToSinglePrecision(pixel)),q);
3302             p=PushShortPixel(quantum_info->endian,p,&pixel);
3303             SetPixelGreen(image,ClampToQuantum(QuantumRange*
3304               HalfToSinglePrecision(pixel)),q);
3305             p=PushShortPixel(quantum_info->endian,p,&pixel);
3306             SetPixelBlue(image,ClampToQuantum(QuantumRange*
3307               HalfToSinglePrecision(pixel)),q);
3308             p=PushShortPixel(quantum_info->endian,p,&pixel);
3309             SetPixelAlpha(image,ClampToQuantum(QuantumRange*
3310               HalfToSinglePrecision(pixel)),q);
3311             p+=quantum_info->pad;
3312             q+=GetPixelChannels(image);
3313           }
3314           break;
3315         }
3316       for (x=0; x < (ssize_t) number_pixels; x++)
3317       {
3318         p=PushShortPixel(quantum_info->endian,p,&pixel);
3319         SetPixelRed(image,ScaleShortToQuantum(pixel),q);
3320         p=PushShortPixel(quantum_info->endian,p,&pixel);
3321         SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
3322         p=PushShortPixel(quantum_info->endian,p,&pixel);
3323         SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
3324         p=PushShortPixel(quantum_info->endian,p,&pixel);
3325         SetPixelAlpha(image,ScaleShortToQuantum(pixel),q);
3326         p+=quantum_info->pad;
3327         q+=GetPixelChannels(image);
3328       }
3329       break;
3330     }
3331     case 32:
3332     {
3333       unsigned int
3334         pixel;
3335
3336       if (quantum_info->format == FloatingPointQuantumFormat)
3337         {
3338           float
3339             pixel;
3340
3341           for (x=0; x < (ssize_t) number_pixels; x++)
3342           {
3343             p=PushFloatPixel(quantum_info,p,&pixel);
3344             SetPixelRed(image,ClampToQuantum(pixel),q);
3345             p=PushFloatPixel(quantum_info,p,&pixel);
3346             SetPixelGreen(image,ClampToQuantum(pixel),q);
3347             p=PushFloatPixel(quantum_info,p,&pixel);
3348             SetPixelBlue(image,ClampToQuantum(pixel),q);
3349             p=PushFloatPixel(quantum_info,p,&pixel);
3350             SetPixelAlpha(image,ClampToQuantum(pixel),q);
3351             p+=quantum_info->pad;
3352             q+=GetPixelChannels(image);
3353           }
3354           break;
3355         }
3356       for (x=0; x < (ssize_t) number_pixels; x++)
3357       {
3358         p=PushLongPixel(quantum_info->endian,p,&pixel);
3359         SetPixelRed(image,ScaleLongToQuantum(pixel),q);
3360         p=PushLongPixel(quantum_info->endian,p,&pixel);
3361         SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
3362         p=PushLongPixel(quantum_info->endian,p,&pixel);
3363         SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
3364         p=PushLongPixel(quantum_info->endian,p,&pixel);
3365         SetPixelAlpha(image,ScaleLongToQuantum(pixel),q);
3366         p+=quantum_info->pad;
3367         q+=GetPixelChannels(image);
3368       }
3369       break;
3370     }
3371     case 64:
3372     {
3373       if (quantum_info->format == FloatingPointQuantumFormat)
3374         {
3375           double
3376             pixel;
3377
3378           for (x=0; x < (ssize_t) number_pixels; x++)
3379           {
3380             p=PushDoublePixel(quantum_info,p,&pixel);
3381             SetPixelRed(image,ClampToQuantum(pixel),q);
3382             p=PushDoublePixel(quantum_info,p,&pixel);
3383             SetPixelGreen(image,ClampToQuantum(pixel),q);
3384             p=PushDoublePixel(quantum_info,p,&pixel);
3385             SetPixelBlue(image,ClampToQuantum(pixel),q);
3386             p=PushDoublePixel(quantum_info,p,&pixel);
3387             SetPixelAlpha(image,ClampToQuantum(pixel),q);
3388             p+=quantum_info->pad;
3389             q+=GetPixelChannels(image);
3390           }
3391           break;
3392         }
3393     }
3394     default:
3395     {
3396       range=GetQuantumRange(quantum_info->depth);
3397       for (x=0; x < (ssize_t) number_pixels; x++)
3398       {
3399         p=PushQuantumPixel(quantum_info,p,&pixel);
3400         SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
3401         p=PushQuantumPixel(quantum_info,p,&pixel);
3402         SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
3403         p=PushQuantumPixel(quantum_info,p,&pixel);
3404         SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
3405         p=PushQuantumPixel(quantum_info,p,&pixel);
3406         SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
3407         q+=GetPixelChannels(image);
3408       }
3409       break;
3410     }
3411   }
3412 }
3413
3414 MagickExport size_t ImportQuantumPixels(const Image *image,
3415   CacheView *image_view,QuantumInfo *quantum_info,
3416   const QuantumType quantum_type,const unsigned char *pixels,
3417   ExceptionInfo *exception)
3418 {
3419   MagickSizeType
3420     number_pixels;
3421
3422   register const unsigned char
3423     *restrict p;
3424
3425   register ssize_t
3426     x;
3427
3428   register Quantum
3429     *restrict q;
3430
3431   size_t
3432     extent;
3433
3434   assert(image != (Image *) NULL);
3435   assert(image->signature == MagickSignature);
3436   if (image->debug != MagickFalse)
3437     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3438   assert(quantum_info != (QuantumInfo *) NULL);
3439   assert(quantum_info->signature == MagickSignature);
3440   if (pixels == (const unsigned char *) NULL)
3441     pixels=GetQuantumPixels(quantum_info);
3442   x=0;
3443   p=pixels;
3444   if (image_view == (CacheView *) NULL)
3445     {
3446       number_pixels=GetImageExtent(image);
3447       q=GetAuthenticPixelQueue(image);
3448     }
3449   else
3450     {
3451       number_pixels=GetCacheViewExtent(image_view);
3452       q=GetCacheViewAuthenticPixelQueue(image_view);
3453     }
3454   ResetQuantumState(quantum_info);
3455   extent=GetQuantumExtent(image,quantum_info,quantum_type);
3456   switch (quantum_type)
3457   {
3458     case AlphaQuantum:
3459     {
3460       ImportAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3461       break;
3462     }
3463     case BGRQuantum:
3464     {
3465       ImportBGRQuantum(image,quantum_info,number_pixels,p,q,exception);
3466       break;
3467     }
3468     case BGRAQuantum:
3469     case BGROQuantum:
3470     {
3471       ImportBGRAQuantum(image,quantum_info,number_pixels,p,q,exception);
3472       break;
3473     }
3474     case BlackQuantum:
3475     {
3476       ImportBlackQuantum(image,quantum_info,number_pixels,p,q,exception);
3477       break;
3478     }
3479     case BlueQuantum:
3480     case YellowQuantum:
3481     {
3482       ImportBlueQuantum(image,quantum_info,number_pixels,p,q,exception);
3483       break;
3484     }
3485     case CMYKQuantum:
3486     {
3487       ImportCMYKQuantum(image,quantum_info,number_pixels,p,q,exception);
3488       break;
3489     }
3490     case CMYKAQuantum:
3491     case CMYKOQuantum:
3492     {
3493       ImportCMYKAQuantum(image,quantum_info,number_pixels,p,q,exception);
3494       break;
3495     }
3496     case CbYCrYQuantum:
3497     {
3498       ImportCbYCrYQuantum(image,quantum_info,number_pixels,p,q,exception);
3499       break;
3500     }
3501     case GrayQuantum:
3502     {
3503       ImportGrayQuantum(image,quantum_info,number_pixels,p,q,exception);
3504       break;
3505     }
3506     case GrayAlphaQuantum:
3507     {
3508       ImportGrayAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3509       break;
3510     }
3511     case GreenQuantum:
3512     case MagentaQuantum:
3513     {
3514       ImportGreenQuantum(image,quantum_info,number_pixels,p,q,exception);
3515       break;
3516     }
3517     case IndexQuantum:
3518     {
3519       ImportIndexQuantum(image,quantum_info,number_pixels,p,q,exception);
3520       break;
3521     }
3522     case IndexAlphaQuantum:
3523     {
3524       ImportIndexAlphaQuantum(image,quantum_info,number_pixels,p,q,exception);
3525       break;
3526     }
3527     case OpacityQuantum:
3528     {
3529       ImportOpacityQuantum(image,quantum_info,number_pixels,p,q,exception);
3530       break;
3531     }
3532     case RedQuantum:
3533     case CyanQuantum:
3534     {
3535       ImportRedQuantum(image,quantum_info,number_pixels,p,q,exception);
3536       break;
3537     }
3538     case RGBQuantum:
3539     case CbYCrQuantum:
3540     {
3541       ImportRGBQuantum(image,quantum_info,number_pixels,p,q,exception);
3542       break;
3543     }
3544     case RGBAQuantum:
3545     case RGBOQuantum:
3546     case CbYCrAQuantum:
3547     {
3548       ImportRGBAQuantum(image,quantum_info,number_pixels,p,q,exception);
3549       break;
3550     }
3551     default:
3552       break;
3553   }
3554   if ((quantum_type == CbYCrQuantum) || (quantum_type == CbYCrAQuantum))
3555     {
3556       Quantum
3557         quantum;
3558
3559       register Quantum
3560         *restrict q;
3561
3562       q=GetAuthenticPixelQueue(image);
3563       if (image_view != (CacheView *) NULL)
3564         q=GetCacheViewAuthenticPixelQueue(image_view);
3565       for (x=0; x < (ssize_t) number_pixels; x++)
3566       {
3567         quantum=GetPixelRed(image,q);
3568         SetPixelRed(image,GetPixelGreen(image,q),q);
3569         SetPixelGreen(image,quantum,q);
3570         q+=GetPixelChannels(image);
3571       }
3572     }
3573   if ((quantum_type == RGBOQuantum) || (quantum_type == CMYKOQuantum))
3574     {
3575       register Quantum
3576         *restrict q;
3577
3578       q=GetAuthenticPixelQueue(image);
3579       if (image_view != (CacheView *) NULL)
3580         q=GetCacheViewAuthenticPixelQueue(image_view);
3581       for (x=0; x < (ssize_t) number_pixels; x++)
3582       {
3583         SetPixelAlpha(image,GetPixelAlpha(image,q),q);
3584         q+=GetPixelChannels(image);
3585       }
3586     }
3587   if (quantum_info->alpha_type == DisassociatedQuantumAlpha)
3588     {
3589       double
3590         gamma,
3591         Sa;
3592
3593       register Quantum
3594         *restrict q;
3595
3596       /*
3597         Disassociate alpha.
3598       */
3599       q=GetAuthenticPixelQueue(image);
3600       if (image_view != (CacheView *) NULL)
3601         q=GetCacheViewAuthenticPixelQueue(image_view);
3602       for (x=0; x < (ssize_t) number_pixels; x++)
3603       {
3604         register ssize_t
3605           i;
3606
3607         if (GetPixelMask(image,q) != 0)
3608           {
3609             q+=GetPixelChannels(image);
3610             continue;
3611           }
3612         Sa=QuantumScale*GetPixelAlpha(image,q);
3613         gamma=MagickEpsilonReciprocal(Sa);
3614         for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3615         {
3616           PixelChannel
3617             channel;
3618
3619           PixelTrait
3620             traits;
3621
3622           channel=GetPixelChannelMapChannel(image,i);
3623           traits=GetPixelChannelMapTraits(image,channel);
3624           if ((traits & UpdatePixelTrait) == 0)
3625             continue;
3626           q[i]=ClampToQuantum(gamma*q[i]);
3627         }
3628         q+=GetPixelChannels(image);
3629       }
3630     }
3631   return(extent);
3632 }