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