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