Coverage Report

Created: 2025-07-23 08:18

/src/graphicsmagick/magick/import.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
  Copyright (C) 2003 - 2023 GraphicsMagick Group
3
  Copyright (C) 2002 ImageMagick Studio
4
5
  This program is covered by multiple licenses, which are described in
6
  Copyright.txt. You should have received a copy of Copyright.txt with this
7
  package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
8
9
  Methods to import image pixels from common representations
10
11
*/
12

13
/*
14
  Include declarations.
15
*/
16
#include "magick/studio.h"
17
#include "magick/bit_stream.h"
18
#include "magick/colormap.h"
19
#include "magick/constitute.h"
20
#include "magick/floats.h"
21
#include "magick/magick.h"
22
#include "magick/pixel_cache.h"
23
#include "magick/utility.h"
24

25
/*
26
  Type definitions
27
*/
28
static const PixelPacket BlackPixel = {0, 0, 0, OpaqueOpacity};
29
30
static const PixelPacket WhitePixel = {MaxRGB, MaxRGB, MaxRGB, OpaqueOpacity};
31

32
/*
33
  Macros
34
*/
35
36
#if defined(WORDS_BIGENDIAN)
37
#  define MyEndianType MSBEndian
38
#else
39
35.3M
#  define MyEndianType LSBEndian
40
#endif
41
42
#define ImportModulo8Quantum(quantum,quantum_size,p)            \
43
93.1k
  {                                                             \
44
93.1k
    register unsigned int                                       \
45
93.1k
      shift;                                                    \
46
93.1k
                                                                \
47
93.1k
    quantum=0;                                                  \
48
93.1k
    if (LSBEndian != endian)                                    \
49
93.1k
      {                                                         \
50
87.5k
        shift=quantum_size;                                     \
51
87.5k
        do                                                      \
52
91.7k
          {                                                     \
53
91.7k
            shift -= 8U;                                        \
54
91.7k
            quantum |= (((magick_uint32_t) *p++) << shift);     \
55
91.7k
          } while( shift > 0U);                                 \
56
87.5k
      }                                                         \
57
93.1k
    else                                                        \
58
93.1k
      {                                                         \
59
5.59k
        shift=0U;                                               \
60
21.2k
        while ( shift < quantum_size )                          \
61
15.6k
          {                                                     \
62
15.6k
            quantum |= (((magick_uint32_t) *p++) << shift);     \
63
15.6k
            shift += 8U;                                        \
64
15.6k
          }                                                     \
65
5.59k
      }                                                         \
66
93.1k
  }
67
#define ImportUInt8Quantum(quantum,p)                           \
68
1.39G
  {                                                             \
69
1.39G
    quantum=(magick_uint32_t) *p++;                             \
70
1.39G
  }
71
#define ImportUInt16Quantum(endian,quantum,p)                   \
72
271M
  {                                                             \
73
271M
    if (LSBEndian != endian)                                    \
74
271M
      {                                                         \
75
30.2M
        quantum=(((magick_uint32_t) *p++) << 8);                \
76
30.2M
        quantum|=((magick_uint32_t) *p++);                      \
77
30.2M
      }                                                         \
78
271M
    else                                                        \
79
271M
      {                                                         \
80
241M
        quantum=((magick_uint32_t) *p++);                       \
81
241M
        quantum|=(((magick_uint32_t) *p++) << 8);               \
82
241M
      }                                                         \
83
271M
  }
84
/*
85
  This algorithm has been compared with several others and did best
86
  overall on SPARC, PowerPC, and Intel Xeon.
87
*/
88
#define ImportUInt32Quantum(endian,quantum,p)                   \
89
296M
  {                                                             \
90
296M
    if (LSBEndian != endian)                                    \
91
296M
      {                                                         \
92
292M
        quantum=(((magick_uint32_t) *p++) << 24);               \
93
292M
        quantum|=(((magick_uint32_t) *p++) << 16);              \
94
292M
        quantum|=(((magick_uint32_t) *p++) << 8);               \
95
292M
        quantum|=((magick_uint32_t) *p++);                      \
96
292M
      }                                                         \
97
296M
    else                                                        \
98
296M
      {                                                         \
99
3.92M
        quantum=((magick_uint32_t) *p++);                       \
100
3.92M
        quantum|=(((magick_uint32_t) *p++) << 8);               \
101
3.92M
        quantum|=(((magick_uint32_t) *p++) << 16);              \
102
3.92M
        quantum|=(((magick_uint32_t) *p++) << 24);              \
103
3.92M
      }                                                         \
104
296M
  }
105
/*
106
  Import a 64-bit unsigned value into a 32-bit quantum type.  This
107
  approach is used since 64-bits is not supported internally and some
108
  CPUs may perform poorly when using a true 64-bit type.  In this case
109
  the least significant 32 bits are entirely ignored.
110
*/
111
#define ImportUInt64Quantum(endian,quantum,p)                   \
112
18.2k
  {                                                             \
113
18.2k
    if (LSBEndian != endian)                                    \
114
18.2k
      {                                                         \
115
16.2k
        quantum=(((magick_uint32_t) *p++) << 24);               \
116
16.2k
        quantum|=(((magick_uint32_t) *p++) << 16);              \
117
16.2k
        quantum|=(((magick_uint32_t) *p++) << 8);               \
118
16.2k
        quantum|=((magick_uint32_t) *p++);                      \
119
16.2k
        p+=4;                                                   \
120
16.2k
      }                                                         \
121
18.2k
    else                                                        \
122
18.2k
      {                                                         \
123
2.03k
        p+=4;                                                   \
124
2.03k
        quantum=((magick_uint32_t) *p++);                       \
125
2.03k
        quantum|=(((magick_uint32_t) *p++) << 8);               \
126
2.03k
        quantum|=(((magick_uint32_t) *p++) << 16);              \
127
2.03k
        quantum|=(((magick_uint32_t) *p++) << 24);              \
128
2.03k
      }                                                         \
129
18.2k
  }
130
#define ImportFloat16Quantum(endian,value,p)                    \
131
18.2M
  {                                                             \
132
18.2M
    float float_value;                                          \
133
18.2M
    unsigned char c[2];                                         \
134
18.2M
    if (MyEndianType == endian)                                 \
135
18.2M
      {                                                         \
136
18.2M
        c[0]=*p++;                                              \
137
18.2M
        c[1]=*p++;                                              \
138
18.2M
      }                                                         \
139
18.2M
    else                                                        \
140
18.2M
      {                                                         \
141
0
        c[1]=*p++;                                              \
142
0
        c[0]=*p++;                                              \
143
0
      }                                                         \
144
18.2M
    (void) _Gm_convert_fp16_to_fp32((const fp_16bits *)c,       \
145
18.2M
                                    &float_value);              \
146
18.2M
    value=float_value;                                          \
147
18.2M
  }
148
#define ImportFloat24Quantum(endian,value,p)                    \
149
1.36M
  {                                                             \
150
1.36M
    float float_value;                                          \
151
1.36M
    unsigned char c[3];                                         \
152
1.36M
    if (MyEndianType == endian)                                 \
153
1.36M
      {                                                         \
154
0
        c[0]=*p++;                                              \
155
0
        c[1]=*p++;                                              \
156
0
        c[2]=*p++;                                              \
157
0
      }                                                         \
158
1.36M
    else                                                        \
159
1.36M
      {                                                         \
160
1.36M
        c[2]=*p++;                                              \
161
1.36M
        c[1]=*p++;                                              \
162
1.36M
        c[0]=*p++;                                              \
163
1.36M
      }                                                         \
164
1.36M
    (void) _Gm_convert_fp24_to_fp32((const fp_24bits *)c,       \
165
1.36M
                                    &float_value,               \
166
1.36M
                                    RANGE_LIMITED);             \
167
1.36M
    value=float_value;                                          \
168
1.36M
}
169
#define ImportFloat32Quantum(endian,value,p)    \
170
13.2M
  {                                             \
171
13.2M
    if (MyEndianType == endian)                 \
172
13.2M
      {                                         \
173
7.48M
        value=*((float *) p);                   \
174
7.48M
        p += sizeof(float);                     \
175
7.48M
      }                                         \
176
13.2M
    else                                        \
177
13.2M
      {                                         \
178
5.80M
        union                                   \
179
5.80M
        {                                       \
180
5.80M
          float f;                              \
181
5.80M
          unsigned char c[4];                   \
182
5.80M
        } fu_;                                  \
183
5.80M
                                                \
184
5.80M
        fu_.c[3]=*p++;                          \
185
5.80M
        fu_.c[2]=*p++;                          \
186
5.80M
        fu_.c[1]=*p++;                          \
187
5.80M
        fu_.c[0]=*p++;                          \
188
5.80M
        value=fu_.f;                            \
189
5.80M
      }                                         \
190
13.2M
  }
191
#define ImportFloat64Quantum(endian,value,p)    \
192
2.41M
  {                                             \
193
2.41M
    if (MyEndianType == endian)                 \
194
2.41M
      {                                         \
195
2.10M
        value=*((double *) p);                  \
196
2.10M
        p += sizeof(double);                    \
197
2.10M
      }                                         \
198
2.41M
    else                                        \
199
2.41M
      {                                         \
200
312k
        union                                   \
201
312k
        {                                       \
202
312k
          double d;                             \
203
312k
          unsigned char c[8];                   \
204
312k
        } du_;                                  \
205
312k
                                                \
206
312k
        du_.c[7]=*p++;                          \
207
312k
        du_.c[6]=*p++;                          \
208
312k
        du_.c[5]=*p++;                          \
209
312k
        du_.c[4]=*p++;                          \
210
312k
        du_.c[3]=*p++;                          \
211
312k
        du_.c[2]=*p++;                          \
212
312k
        du_.c[1]=*p++;                          \
213
312k
        du_.c[0]=*p++;                          \
214
312k
        value=du_.d;                            \
215
312k
      }                                         \
216
2.41M
  }
217

218
/*
219
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
%                                                                             %
221
%                                                                             %
222
%                                                                             %
223
%   I m p o r t I m a g e P i x e l A r e a                                   %
224
%                                                                             %
225
%                                                                             %
226
%                                                                             %
227
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
%
229
%  ImportImagePixelArea() transfers one or more pixel components from a user
230
%  supplied buffer into the default image pixel cache view. By default,
231
%  values are read in network (big-endian) byte/bit order.  By setting the
232
%  'endian' member of ExportPixelAreaOptions, 16, 32 and 64-bit values may
233
%  be output as little (LSBEndian), big (MSBEndian), or host native
234
%  (NativeEndian) endian values.  This function is quite powerful in that
235
%  besides common native CPU type sizes, it can support any integer bit
236
%  depth from 1 to 32 (e.g. 13), 64-bits, as well as 32 and 64-bit float.
237
%
238
%  MagickPass is returned if the pixels are successfully transferred,
239
%  otherwise MagickFail.
240
%
241
%  The format of the ImportImagePixelArea method is:
242
%
243
%      MagickPassFail ImportImagePixelArea(Image *image,
244
%                                          const QuantumType quantum_type,
245
%                                          const unsigned int quantum_size,
246
%                                          const unsigned char *source,
247
%                                          const ImportPixelAreaOptions *options,
248
%                                          ImportPixelAreaInfo *import_info)
249
%
250
%  A description of each parameter follows:
251
%
252
%    o status: Method PushImagePixels returns MagickPass if the pixels are
253
%      successfully transferred, otherwise MagickFail.
254
%
255
%    o image: The image.
256
%
257
%    o quantum_type: Declare which pixel components to transfer (AlphaQuantum,
258
%        BlackQuantum, BlueQuantum, CMYKAQuantum, CMYKQuantum, CyanQuantum,
259
%        GrayAlphaQuantum, GrayQuantum, GreenQuantum, IndexAlphaQuantum,
260
%        IndexQuantum, MagentaQuantum, RGBAQuantum, RGBQuantum,
261
%        RedQuantum, YellowQuantum)
262
%
263
%    o quantum_size: Bits per quantum sample (range 1-32, and 64).
264
%
265
%    o source:  The pixel components are transferred from this buffer.
266
%
267
%    o options: Additional options specific to quantum_type (may be NULL).
268
%
269
%    o import_info : Populated with information regarding the pixels
270
%               imported (may be NULL)
271
%
272
*/
273
MagickExport MagickPassFail ImportImagePixelArea(Image *image,
274
  const QuantumType quantum_type,const unsigned int quantum_size,
275
  const unsigned char *source,const ImportPixelAreaOptions *options,
276
  ImportPixelAreaInfo *import_info)
277
20.5M
{
278
20.5M
  return ImportViewPixelArea(AccessDefaultCacheView(image),
279
20.5M
                             quantum_type,quantum_size,
280
20.5M
                             source,options,import_info);
281
20.5M
}
282

283
/*
284
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285
%                                                                             %
286
%                                                                             %
287
%                                                                             %
288
%   I m p o r t V i e w P i x e l A r e a                                     %
289
%                                                                             %
290
%                                                                             %
291
%                                                                             %
292
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
%
294
%  ImportViewPixelArea() transfers one or more pixel components from a user
295
%  supplied buffer into the specified image pixel cache view of an image. By
296
%  default, values are read in network (big-endian) byte/bit order.  By
297
%  setting the 'endian' member of ExportPixelAreaOptions, 16, 32 and 64-bit
298
%  values may be output as little (LSBEndian), big (MSBEndian), or host
299
%  native (NativeEndian) endian values.  This function is quite powerful in
300
%  that besides common native CPU type sizes, it can support any integer bit
301
%  depth from 1 to 32 (e.g. 13), 64-bits, as well as 32 and 64-bit float.
302
%
303
%  MagickPass is returned if the pixels are successfully transferred,
304
%  otherwise MagickFail.
305
%
306
%  The format of the ImportViewPixelArea method is:
307
%
308
%      MagickPassFail ImportViewPixelArea(ViewInfo *view,
309
%                                         const QuantumType quantum_type,
310
%                                         const unsigned int quantum_size,
311
%                                         const unsigned char *source,
312
%                                         const ImportPixelAreaOptions *options,
313
%                                         ImportPixelAreaInfo *import_info)
314
%
315
%  A description of each parameter follows:
316
%
317
%    o status: Method PushImagePixels returns MagickPass if the pixels are
318
%      successfully transferred, otherwise MagickFail.
319
%
320
%    o view: The pixel view to import pixels into.
321
%
322
%    o quantum_type: Declare which pixel components to transfer (AlphaQuantum,
323
%        BlackQuantum, BlueQuantum, CMYKAQuantum, CMYKQuantum, CyanQuantum,
324
%        GrayAlphaQuantum, GrayQuantum, GreenQuantum, IndexAlphaQuantum,
325
%        IndexQuantum, MagentaQuantum, RGBAQuantum, RGBQuantum,
326
%        RedQuantum, YellowQuantum)
327
%
328
%    o quantum_size: Bits per quantum sample (range 1-32, and 64).
329
%
330
%    o source:  The pixel components are transferred from this buffer.
331
%
332
%    o options: Additional options specific to quantum_type (may be NULL).
333
%
334
%    o import_info : Populated with information regarding the pixels
335
%               imported (may be NULL)
336
%
337
*/
338
static MagickPassFail
339
ImportIndexQuantumType(const unsigned char *source,
340
                       PixelPacket* restrict q,
341
                       IndexPacket * restrict indexes,
342
                       const unsigned long number_pixels,
343
                       const unsigned int quantum_size,
344
                       const QuantumSampleType sample_type,
345
                       const unsigned int unsigned_maxvalue,
346
                       const EndianType endian,
347
                       Image *image,
348
                       ImportPixelAreaInfo *import_info)
349
220k
{
350
220k
  const unsigned char * restrict
351
220k
    p;
352
353
220k
  register unsigned long
354
220k
    x;
355
356
220k
  register magick_uint32_t
357
220k
    index;
358
359
220k
  assert(image->colors <= MaxColormapSize);
360
361
220k
  if ((image->storage_class != PseudoClass) ||
362
220k
      (image->colors == 0) ||
363
220k
      (indexes == (IndexPacket *) NULL))
364
0
    ThrowBinaryException3(ImageError,ImageIsNotColormapped,
365
220k
                          UnableToImportImagePixels);
366
367
220k
  p=source;
368
220k
  if (sample_type == UnsignedQuantumSampleType)
369
220k
    {
370
220k
      switch (quantum_size)
371
220k
        {
372
5.11k
        case 1:
373
5.11k
          {
374
            /*
375
              Special fast support for two colors.
376
            */
377
5.11k
            register unsigned int
378
5.11k
              bit = 8U;
379
380
67.5k
            for (x = number_pixels ; x != 0 ; --x )
381
62.4k
              {
382
62.4k
                --bit;
383
62.4k
                index=(*p >> bit) & 0x01;
384
62.4k
                VerifyColormapIndex(image,index);
385
62.4k
                *indexes++=index;
386
62.4k
                *q++=image->colormap[index];
387
62.4k
                if (bit == 0)
388
6.81k
                  {
389
6.81k
                    bit=8;
390
6.81k
                    p++;
391
6.81k
                  }
392
62.4k
              }
393
5.11k
            break;
394
0
          }
395
3.51k
        case 4:
396
3.51k
          {
397
            /*
398
              Special fast support for 16 colors.
399
            */
400
3.51k
            register unsigned int
401
3.51k
              state = 0;
402
403
17.7k
            for (x = number_pixels ; x != 0 ; --x )
404
14.2k
              {
405
14.2k
                state ^= 1; /* Produces 1 0 1 0 ... */
406
14.2k
                index=(IndexPacket) ((state ? (*p >> 4) : (*p++)) & 0xf);
407
14.2k
                VerifyColormapIndex(image,index);
408
14.2k
                *indexes++=index;
409
14.2k
                *q++=image->colormap[index];
410
14.2k
              }
411
3.51k
            break;
412
0
          }
413
155k
        case 8:
414
155k
          {
415
155k
            if (unsigned_maxvalue <= (unsigned int) (image->colors-1))
416
86.2k
              {
417
                /*
418
                  Special case for when it is impossible to
419
                  overflow the colormap range.
420
                */
421
42.0M
                for (x = number_pixels; x != 0; --x)
422
42.0M
                  {
423
42.0M
                    ImportUInt8Quantum(index,p);
424
42.0M
                    *indexes++=index;
425
42.0M
                    *q++=image->colormap[index];
426
42.0M
                  }
427
86.2k
              }
428
69.7k
            else
429
69.7k
              {
430
3.13M
                for (x = number_pixels; x != 0; --x)
431
3.06M
                  {
432
3.06M
                    ImportUInt8Quantum(index,p);
433
3.06M
                    VerifyColormapIndex(image,index);
434
3.06M
                    *indexes++=index;
435
3.06M
                    *q++=image->colormap[index];
436
3.06M
                  }
437
69.7k
              }
438
155k
            break;
439
0
          }
440
17.5k
        case 16:
441
17.5k
          {
442
121k
            for (x = number_pixels; x != 0; --x)
443
103k
              {
444
103k
                ImportUInt16Quantum(endian,index,p);
445
103k
                VerifyColormapIndex(image,index);
446
103k
                *indexes++=index;
447
103k
                *q++=image->colormap[index];
448
103k
              }
449
17.5k
            break;
450
0
          }
451
0
        case 32:
452
0
          {
453
0
            for (x = number_pixels; x != 0; --x)
454
0
              {
455
0
                ImportUInt32Quantum(endian,index,p);
456
0
                VerifyColormapIndex(image,index);
457
0
                *indexes++=index;
458
0
                *q++=image->colormap[index];
459
0
              }
460
0
            break;
461
0
          }
462
0
        case 64:
463
0
          {
464
0
            for (x = number_pixels; x != 0; --x)
465
0
              {
466
0
                ImportUInt64Quantum(endian,index,p);
467
0
                VerifyColormapIndex(image,index);
468
0
                *indexes++=index;
469
0
                *q++=image->colormap[index];
470
0
              }
471
0
            break;
472
0
          }
473
38.3k
        default:
474
38.3k
          {
475
            /*
476
              Arbitrary sample size
477
            */
478
38.3k
            BitStreamReadHandle
479
38.3k
              stream;
480
481
38.3k
            MagickBitStreamInitializeRead(&stream,p);
482
5.44M
            for (x = number_pixels; x != 0; --x)
483
5.41M
              {
484
5.41M
                index=MagickBitStreamMSBRead(&stream,quantum_size);
485
5.41M
                VerifyColormapIndex(image,index);
486
5.41M
                *indexes++=index;
487
5.41M
                *q++=image->colormap[index];
488
5.41M
              }
489
38.3k
            break;
490
0
          }
491
220k
        }
492
220k
    }
493
494
220k
  if (import_info)
495
64.6k
    {
496
64.6k
      import_info->bytes_imported=p-source;
497
64.6k
    }
498
499
220k
  return MagickPass;
500
220k
}
501
502
static MagickPassFail
503
ImportIndexAlphaQuantumType(const unsigned char *source,
504
                            PixelPacket* restrict q,
505
                            IndexPacket * restrict indexes,
506
                            const unsigned long number_pixels,
507
                            const unsigned int quantum_size,
508
                            const QuantumSampleType sample_type,
509
                            const unsigned int unsigned_scale,
510
                            const EndianType endian,
511
                            Image *image,
512
                            ImportPixelAreaInfo *import_info)
513
9.94k
{
514
9.94k
  const unsigned char * restrict
515
9.94k
    p;
516
517
9.94k
  register unsigned long
518
9.94k
    x;
519
520
9.94k
  register magick_uint32_t
521
9.94k
    index,
522
9.94k
    unsigned_value;
523
524
9.94k
   unsigned int
525
9.94k
     sample_bits;
526
527
9.94k
  assert(image->colors <= MaxColormapSize);
528
529
9.94k
  if ((image->storage_class != PseudoClass) ||
530
9.94k
      (image->colors == 0) ||
531
9.94k
      (indexes == (IndexPacket *) NULL))
532
0
    ThrowBinaryException3(ImageError,ImageIsNotColormapped,
533
9.94k
                          UnableToImportImagePixels);
534
535
9.94k
  sample_bits=quantum_size;
536
9.94k
  p=source;
537
538
9.94k
  if (sample_type == UnsignedQuantumSampleType)
539
9.94k
    {
540
9.94k
      switch(quantum_size)
541
9.94k
        {
542
8.96k
        case 8:
543
8.96k
          {
544
4.60M
            for (x = number_pixels; x != 0; --x)
545
4.59M
              {
546
4.59M
                ImportUInt8Quantum(index,p);
547
4.59M
                VerifyColormapIndex(image,index);
548
4.59M
                *indexes++=index;
549
4.59M
                *q=image->colormap[index];
550
551
4.59M
                ImportUInt8Quantum(unsigned_value,p);
552
4.59M
                SetOpacitySample(q,MaxRGB-ScaleCharToQuantum(unsigned_value));
553
4.59M
                q++;
554
4.59M
              }
555
8.96k
            break;
556
0
          }
557
0
        case 16:
558
0
          {
559
0
            for (x = number_pixels; x != 0; --x)
560
0
              {
561
0
                ImportUInt16Quantum(endian,index,p);
562
0
                VerifyColormapIndex(image,index);
563
0
                *indexes++=index;
564
0
                *q=image->colormap[index];
565
566
0
                ImportUInt16Quantum(endian,unsigned_value,p);
567
0
                SetOpacitySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
568
0
                q++;
569
0
              }
570
0
            break;
571
0
          }
572
0
        case 32:
573
0
          {
574
0
            for (x = number_pixels; x != 0; --x)
575
0
              {
576
0
                ImportUInt32Quantum(endian,index,p);
577
0
                VerifyColormapIndex(image,index);
578
0
                *indexes++=index;
579
0
                *q=image->colormap[index];
580
581
0
                ImportUInt32Quantum(endian,unsigned_value,p);
582
0
                SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
583
0
                q++;
584
0
              }
585
0
            break;
586
0
          }
587
0
        case 64:
588
0
          {
589
0
            for (x = number_pixels; x != 0; --x)
590
0
              {
591
0
                ImportUInt64Quantum(endian,index,p);
592
0
                VerifyColormapIndex(image,index);
593
0
                *indexes++=index;
594
0
                *q=image->colormap[index];
595
596
0
                ImportUInt64Quantum(endian,unsigned_value,p);
597
0
                SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
598
0
                q++;
599
0
              }
600
0
            break;
601
0
          }
602
975
        default:
603
975
          {
604
            /*
605
              Arbitrary sample size
606
            */
607
975
            BitStreamReadHandle
608
975
              stream;
609
610
975
            MagickBitStreamInitializeRead(&stream,p);
611
3.80k
            for (x = number_pixels; x != 0; --x)
612
2.83k
              {
613
2.83k
                index=MagickBitStreamMSBRead(&stream,quantum_size);
614
2.83k
                VerifyColormapIndex(image,index);
615
2.83k
                *indexes++=index;
616
2.83k
                *q=image->colormap[index];
617
618
2.83k
                unsigned_value=MagickBitStreamMSBRead(&stream,quantum_size);
619
2.83k
                if (QuantumDepth >  sample_bits)
620
2.83k
                  unsigned_value *= unsigned_scale;
621
0
                else if (QuantumDepth <  sample_bits)
622
0
                  unsigned_value /= unsigned_scale;
623
2.83k
                SetOpacitySample(q,MaxRGB-unsigned_value);
624
2.83k
                q++;
625
2.83k
              }
626
975
            break;
627
0
          }
628
9.94k
        }
629
9.94k
    }
630
631
9.94k
  if (import_info)
632
9.91k
    {
633
9.91k
      import_info->bytes_imported=p-source;
634
9.91k
    }
635
636
9.94k
  return MagickPass;
637
9.94k
}
638
639
static MagickPassFail
640
ImportGrayQuantumType(const unsigned char *source,
641
                      PixelPacket* restrict q,
642
                      IndexPacket * restrict indexes,
643
                      const unsigned long number_pixels,
644
                      const unsigned int quantum_size,
645
                      const QuantumSampleType sample_type,
646
                      const unsigned int unsigned_scale,
647
                      const unsigned int unsigned_maxvalue,
648
                      const MagickBool grayscale_miniswhite,
649
                      const double double_minvalue,
650
                      const double double_scale,
651
                      const EndianType endian,
652
                      Image *image,
653
                      ImportPixelAreaInfo *import_info)
654
15.3M
{
655
15.3M
  const unsigned char * restrict
656
15.3M
    p;
657
658
15.3M
  register unsigned long
659
15.3M
    x;
660
661
15.3M
  double
662
15.3M
    double_value;
663
664
15.3M
  register magick_uint32_t
665
15.3M
    index,
666
15.3M
    unsigned_value;
667
668
15.3M
  unsigned int
669
15.3M
    sample_bits;
670
671
15.3M
  sample_bits=quantum_size;
672
15.3M
  p=source;
673
674
15.3M
  if (sample_type == UnsignedQuantumSampleType)
675
15.1M
    {
676
15.1M
      if (DirectClass == image->storage_class)
677
13.1M
        {
678
          /*
679
            DirectClass representation.
680
          */
681
13.1M
          switch (quantum_size)
682
13.1M
            {
683
10.4M
            case 1:
684
10.4M
              {
685
                /*
686
                  Special fast support for bi-level gray.
687
                */
688
10.4M
                register int
689
10.4M
                  bit = 8;
690
691
10.4M
                PixelPacket
692
10.4M
                  min_val,
693
10.4M
                  max_val;
694
695
10.4M
                if (grayscale_miniswhite)
696
5.14M
                  {
697
5.14M
                    min_val=WhitePixel;
698
5.14M
                    max_val=BlackPixel;
699
5.14M
                  }
700
5.31M
                else
701
5.31M
                  {
702
5.31M
                    min_val=BlackPixel;
703
5.31M
                    max_val=WhitePixel;
704
5.31M
                  }
705
706
4.26G
                for (x = number_pixels ; x != 0 ; --x )
707
4.25G
                  {
708
4.25G
                    --bit;
709
4.25G
                    *q++=(((*p >> bit) & 0x01) ? max_val : min_val);
710
4.25G
                    if (bit == 0)
711
528M
                      {
712
528M
                        bit=8;
713
528M
                        p++;
714
528M
                      }
715
4.25G
                  }
716
10.4M
                if (bit != 8)
717
8.56M
                  p++;
718
10.4M
                break;
719
0
              }
720
877k
            case 8:
721
877k
              {
722
877k
                if (grayscale_miniswhite)
723
261k
                  {
724
119M
                    for (x = number_pixels; x != 0; --x)
725
118M
                      {
726
118M
                        SetGraySample(q,MaxRGB-ScaleCharToQuantum(*p++));
727
118M
                        SetOpacitySample(q,OpaqueOpacity);
728
118M
                        q++;
729
118M
                      }
730
261k
                  }
731
616k
                else
732
616k
                  {
733
139M
                    for (x = number_pixels; x != 0; --x)
734
138M
                      {
735
138M
                        SetGraySample(q,ScaleCharToQuantum(*p++));
736
138M
                        SetOpacitySample(q,OpaqueOpacity);
737
138M
                        q++;
738
138M
                      }
739
616k
                  }
740
877k
                break;
741
0
              }
742
53.6k
            case 16:
743
53.6k
              {
744
53.6k
                if (grayscale_miniswhite)
745
12.9k
                  {
746
738k
                    for (x = number_pixels; x != 0; --x)
747
725k
                      {
748
725k
                        ImportUInt16Quantum(endian,unsigned_value,p);
749
725k
                        SetGraySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
750
725k
                        SetOpacitySample(q,OpaqueOpacity);
751
725k
                        q++;
752
725k
                      }
753
12.9k
                  }
754
40.6k
                else
755
40.6k
                  {
756
15.2M
                    for (x = number_pixels; x != 0; --x)
757
15.2M
                      {
758
15.2M
                        ImportUInt16Quantum(endian,unsigned_value,p);
759
15.2M
                        SetGraySample(q,ScaleShortToQuantum(unsigned_value));
760
15.2M
                        SetOpacitySample(q,OpaqueOpacity);
761
15.2M
                        q++;
762
15.2M
                      }
763
40.6k
                  }
764
53.6k
                break;
765
0
              }
766
90.5k
            case 32:
767
90.5k
              {
768
90.5k
                if (grayscale_miniswhite)
769
1.69k
                  {
770
107k
                    for (x = number_pixels; x != 0; --x)
771
105k
                      {
772
105k
                        ImportUInt32Quantum(endian,unsigned_value,p);
773
105k
                        SetGraySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
774
105k
                        SetOpacitySample(q,OpaqueOpacity);
775
105k
                        q++;
776
105k
                      }
777
1.69k
                  }
778
88.8k
                else
779
88.8k
                  {
780
96.1M
                    for (x = number_pixels; x != 0; --x)
781
96.0M
                      {
782
96.0M
                        ImportUInt32Quantum(endian,unsigned_value,p);
783
96.0M
                        SetGraySample(q,ScaleLongToQuantum(unsigned_value));
784
96.0M
                        SetOpacitySample(q,OpaqueOpacity);
785
96.0M
                        q++;
786
96.0M
                      }
787
88.8k
                  }
788
90.5k
                break;
789
0
              }
790
9.31k
            case 64:
791
9.31k
              {
792
9.31k
                if (grayscale_miniswhite)
793
0
                  {
794
0
                    for (x = number_pixels; x != 0; --x)
795
0
                      {
796
0
                        ImportUInt64Quantum(endian,unsigned_value,p);
797
0
                        SetGraySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
798
0
                        SetOpacitySample(q,OpaqueOpacity);
799
0
                        q++;
800
0
                      }
801
0
                  }
802
9.31k
                else
803
9.31k
                  {
804
23.4k
                    for (x = number_pixels; x != 0; --x)
805
14.1k
                      {
806
14.1k
                        ImportUInt64Quantum(endian,unsigned_value,p);
807
14.1k
                        SetGraySample(q,ScaleLongToQuantum(unsigned_value));
808
14.1k
                        SetOpacitySample(q,OpaqueOpacity);
809
14.1k
                        q++;
810
14.1k
                      }
811
9.31k
                  }
812
9.31k
                break;
813
0
              }
814
1.69M
            default:
815
1.69M
              {
816
1.69M
                BitStreamReadHandle
817
1.69M
                  stream;
818
819
1.69M
                MagickBitStreamInitializeRead(&stream,p);
820
261M
                for (x = number_pixels; x != 0; --x)
821
259M
                  {
822
259M
                    unsigned_value=MagickBitStreamMSBRead(&stream,quantum_size);
823
259M
                    if (QuantumDepth >  sample_bits)
824
258M
                      unsigned_value *= unsigned_scale;
825
1.10M
                    else if (QuantumDepth <  sample_bits)
826
1.10M
                      unsigned_value /= unsigned_scale;
827
259M
                    if (grayscale_miniswhite)
828
86.8M
                      unsigned_value = MaxRGB-unsigned_value;
829
259M
                    SetGraySample(q,unsigned_value);
830
259M
                    SetOpacitySample(q,OpaqueOpacity);
831
259M
                    q++;
832
259M
                  }
833
1.69M
                break;
834
0
              }
835
13.1M
            }
836
13.1M
        }
837
2.00M
      else
838
2.00M
        {
839
          /*
840
            PseudoClass representation.
841
842
            Note that this implementation assumes that the
843
            colormap is written in ascending levels of intensity
844
            as produced by AllocateImageColormap().  Some old
845
            code may assume that 'miniswhite' inverts the
846
            colormap order as well.
847
          */
848
2.00M
          assert(image->colors <= MaxColormapSize);
849
850
2.00M
          if ((image->storage_class != PseudoClass) ||
851
2.00M
              (image->colors == 0) ||
852
2.00M
              (indexes == (IndexPacket *) NULL))
853
0
            ThrowBinaryException3(ImageError,ImageIsNotColormapped,
854
2.00M
                                  UnableToImportImagePixels);
855
856
2.00M
          switch (quantum_size)
857
2.00M
            {
858
1.55M
            case 1:
859
1.55M
              {
860
                /*
861
                  Special fast support for bi-level gray.
862
                */
863
1.55M
                register int
864
1.55M
                  bit = 8;
865
866
1.34G
                for (x = number_pixels ; x != 0 ; --x )
867
1.34G
                  {
868
1.34G
                    --bit;
869
1.34G
                    index=(*p >> bit) & 0x01;
870
1.34G
                    if (grayscale_miniswhite)
871
1.27G
                      index ^= 0x01;
872
1.34G
                    *indexes++=index;
873
1.34G
                    *q++=image->colormap[index];
874
1.34G
                    if (bit == 0)
875
167M
                      {
876
167M
                        bit=8;
877
167M
                        p++;
878
167M
                      }
879
1.34G
                  }
880
1.55M
                if (bit != 8)
881
1.13M
                  p++;
882
1.55M
                break;
883
0
              }
884
408k
            case 8:
885
408k
              {
886
64.8M
                for (x = number_pixels; x != 0; --x)
887
64.4M
                  {
888
64.4M
                    ImportUInt8Quantum(index,p);
889
64.4M
                    VerifyColormapIndex(image,index);
890
64.4M
                    if (grayscale_miniswhite)
891
0
                      index=(image->colors-1)-index;
892
64.4M
                    *indexes++=index;
893
64.4M
                    *q++=image->colormap[index];
894
64.4M
                  }
895
408k
                break;
896
0
              }
897
43.6k
            case 16:
898
43.6k
              {
899
4.98M
                for (x = number_pixels; x != 0; --x)
900
4.93M
                  {
901
4.93M
                    ImportUInt16Quantum(endian,index,p);
902
4.93M
                    VerifyColormapIndex(image,index);
903
4.93M
                    if (grayscale_miniswhite)
904
0
                      index=(image->colors-1)-index;
905
4.93M
                    *indexes++=index;
906
4.93M
                    *q++=image->colormap[index];
907
4.93M
                  }
908
43.6k
                break;
909
0
              }
910
0
            case 32:
911
0
              {
912
0
                for (x = number_pixels; x != 0; --x)
913
0
                  {
914
0
                    ImportUInt32Quantum(endian,index,p);
915
0
                    VerifyColormapIndex(image,index);
916
0
                    if (grayscale_miniswhite)
917
0
                      index=(image->colors-1)-index;
918
0
                    *indexes++=index;
919
0
                    *q++=image->colormap[index];
920
0
                  }
921
0
                break;
922
0
              }
923
0
            case 64:
924
0
              {
925
0
                for (x = number_pixels; x != 0; --x)
926
0
                  {
927
0
                    ImportUInt64Quantum(endian,index,p);
928
0
                    VerifyColormapIndex(image,index);
929
0
                    if (grayscale_miniswhite)
930
0
                      index=(image->colors-1)-index;
931
0
                    *indexes++=index;
932
0
                    *q++=image->colormap[index];
933
0
                  }
934
0
                break;
935
0
              }
936
0
            default:
937
0
              {
938
                /*
939
                  Arbitrary sample size
940
                */
941
0
                BitStreamReadHandle
942
0
                  stream;
943
944
0
                register unsigned int
945
0
                  indexes_scale = 1U;
946
947
0
                if (unsigned_maxvalue > (image->colors-1))
948
0
                  indexes_scale=(unsigned_maxvalue/(image->colors-1));
949
950
0
                MagickBitStreamInitializeRead(&stream,p);
951
0
                for (x = number_pixels; x != 0; --x)
952
0
                  {
953
0
                    index=MagickBitStreamMSBRead(&stream,quantum_size);
954
0
                    index /= indexes_scale;
955
0
                    VerifyColormapIndex(image,index);
956
0
                    if (grayscale_miniswhite)
957
0
                      index=(image->colors-1)-index;
958
0
                    *indexes++=index;
959
0
                    *q++=image->colormap[index];
960
0
                  }
961
0
                break;
962
0
              }
963
2.00M
            }
964
2.00M
        }
965
15.1M
    }
966
109k
  else if (sample_type == FloatQuantumSampleType)
967
109k
    {
968
109k
      switch (quantum_size)
969
109k
        {
970
4.44k
        case 16:
971
4.44k
          {
972
435k
            for (x = number_pixels; x != 0; --x)
973
431k
              {
974
431k
                ImportFloat16Quantum(endian,double_value,p);
975
431k
                double_value -= double_minvalue;
976
431k
                double_value *= double_scale;
977
431k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
978
431k
                q++;
979
431k
              }
980
4.44k
            break;
981
0
          }
982
2.36k
        case 24:
983
2.36k
          {
984
110k
            for (x = number_pixels; x != 0; --x)
985
108k
              {
986
108k
                ImportFloat24Quantum(endian,double_value,p);
987
108k
                double_value -= double_minvalue;
988
108k
                double_value *= double_scale;
989
108k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
990
108k
                q++;
991
108k
              }
992
2.36k
            break;
993
0
          }
994
51.1k
        case 32:
995
51.1k
          {
996
7.81M
            for (x = number_pixels; x != 0; --x)
997
7.76M
              {
998
7.76M
                ImportFloat32Quantum(endian,double_value,p);
999
7.76M
                double_value -= double_minvalue;
1000
7.76M
                double_value *= double_scale;
1001
7.76M
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1002
7.76M
                q++;
1003
7.76M
              }
1004
51.1k
            break;
1005
0
          }
1006
51.8k
        case 64:
1007
51.8k
          {
1008
1.27M
            for (x = number_pixels; x != 0; --x)
1009
1.22M
              {
1010
1.22M
                ImportFloat64Quantum(endian,double_value,p);
1011
1.22M
                double_value -= double_minvalue;
1012
1.22M
                double_value *= double_scale;
1013
1.22M
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1014
1.22M
                q++;
1015
1.22M
              }
1016
51.8k
            break;
1017
0
          }
1018
0
        default:
1019
0
          break;
1020
109k
        }
1021
109k
    }
1022
1023
15.3M
  if (import_info)
1024
6.96M
    {
1025
6.96M
      import_info->bytes_imported=p-source;
1026
6.96M
    }
1027
1028
15.3M
  return MagickPass;
1029
15.3M
}
1030
1031
static MagickPassFail
1032
ImportGrayAlphaQuantumType(const unsigned char *source,
1033
                           PixelPacket* restrict q,
1034
                           IndexPacket * restrict indexes,
1035
                           const unsigned long number_pixels,
1036
                           const unsigned int quantum_size,
1037
                           const QuantumSampleType sample_type,
1038
                           const unsigned int unsigned_scale,
1039
                           const unsigned int unsigned_maxvalue,
1040
                           const MagickBool grayscale_miniswhite,
1041
                           const double double_minvalue,
1042
                           const double double_scale,
1043
                           const EndianType endian,
1044
                           Image *image,
1045
                           ImportPixelAreaInfo *import_info)
1046
65.5k
{
1047
65.5k
  const unsigned char * restrict
1048
65.5k
    p;
1049
1050
65.5k
  register unsigned long
1051
65.5k
    x;
1052
1053
65.5k
  double
1054
65.5k
    double_value;
1055
1056
65.5k
  register magick_uint32_t
1057
65.5k
    index,
1058
65.5k
    unsigned_value;
1059
1060
65.5k
  unsigned int
1061
65.5k
    sample_bits;
1062
1063
65.5k
  sample_bits=quantum_size;
1064
65.5k
  p=source;
1065
1066
65.5k
  if (sample_type == UnsignedQuantumSampleType)
1067
35.8k
    {
1068
35.8k
      if (DirectClass == image->storage_class)
1069
35.8k
        {
1070
          /*
1071
            DirectClass representation.
1072
          */
1073
35.8k
          switch (quantum_size)
1074
35.8k
            {
1075
13.5k
            case 8:
1076
13.5k
              {
1077
13.5k
                if (grayscale_miniswhite)
1078
4.94k
                  {
1079
1.31M
                    for (x = number_pixels; x != 0; --x)
1080
1.30M
                      {
1081
1.30M
                        ImportUInt8Quantum(unsigned_value,p);
1082
1.30M
                        SetGraySample(q,MaxRGB-ScaleCharToQuantum(unsigned_value));
1083
1.30M
                        ImportUInt8Quantum(unsigned_value,p);
1084
1.30M
                        SetOpacitySample(q,MaxRGB-ScaleCharToQuantum(unsigned_value));
1085
1.30M
                        q++;
1086
1.30M
                      }
1087
4.94k
                  }
1088
8.61k
                else
1089
8.61k
                  {
1090
2.44M
                    for (x = number_pixels; x != 0; --x)
1091
2.43M
                      {
1092
2.43M
                        ImportUInt8Quantum(unsigned_value,p);
1093
2.43M
                        SetGraySample(q,ScaleCharToQuantum(unsigned_value));
1094
2.43M
                        ImportUInt8Quantum(unsigned_value,p);
1095
2.43M
                        SetOpacitySample(q,MaxRGB-ScaleCharToQuantum(unsigned_value));
1096
2.43M
                        q++;
1097
2.43M
                      }
1098
8.61k
                  }
1099
13.5k
                break;
1100
0
              }
1101
6.28k
            case 16:
1102
6.28k
              {
1103
6.28k
                if (grayscale_miniswhite)
1104
1.09k
                  {
1105
6.80k
                    for (x = number_pixels; x != 0; --x)
1106
5.71k
                      {
1107
5.71k
                        ImportUInt16Quantum(endian,unsigned_value,p);
1108
5.71k
                        SetGraySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
1109
5.71k
                        ImportUInt16Quantum(endian,unsigned_value,p);
1110
5.71k
                        SetOpacitySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
1111
5.71k
                        q++;
1112
5.71k
                      }
1113
1.09k
                  }
1114
5.19k
                else
1115
5.19k
                  {
1116
188k
                    for (x = number_pixels; x != 0; --x)
1117
182k
                      {
1118
182k
                        ImportUInt16Quantum(endian,unsigned_value,p);
1119
182k
                        SetGraySample(q,ScaleShortToQuantum(unsigned_value));
1120
182k
                        ImportUInt16Quantum(endian,unsigned_value,p);
1121
182k
                        SetOpacitySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
1122
182k
                        q++;
1123
182k
                      }
1124
5.19k
                  }
1125
6.28k
                break;
1126
0
              }
1127
5.39k
            case 32:
1128
5.39k
              {
1129
5.39k
                if (grayscale_miniswhite)
1130
3.12k
                  {
1131
431k
                    for (x = number_pixels; x != 0; --x)
1132
428k
                      {
1133
428k
                        ImportUInt32Quantum(endian,unsigned_value,p);
1134
428k
                        SetGraySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1135
428k
                        ImportUInt32Quantum(endian,unsigned_value,p);
1136
428k
                        SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1137
428k
                        q++;
1138
428k
                      }
1139
3.12k
                  }
1140
2.27k
                else
1141
2.27k
                  {
1142
217k
                    for (x = number_pixels; x != 0; --x)
1143
214k
                      {
1144
214k
                        ImportUInt32Quantum(endian,unsigned_value,p);
1145
214k
                        SetGraySample(q,ScaleLongToQuantum(unsigned_value));
1146
214k
                        ImportUInt32Quantum(endian,unsigned_value,p);
1147
214k
                        SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1148
214k
                        q++;
1149
214k
                      }
1150
2.27k
                  }
1151
5.39k
                break;
1152
0
              }
1153
0
            case 64:
1154
0
              {
1155
0
                if (grayscale_miniswhite)
1156
0
                  {
1157
0
                    for (x = number_pixels; x != 0; --x)
1158
0
                      {
1159
0
                        ImportUInt64Quantum(endian,unsigned_value,p);
1160
0
                        SetGraySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1161
0
                        ImportUInt64Quantum(endian,unsigned_value,p);
1162
0
                        SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1163
0
                        q++;
1164
0
                      }
1165
0
                  }
1166
0
                else
1167
0
                  {
1168
0
                    for (x = number_pixels; x != 0; --x)
1169
0
                      {
1170
0
                        ImportUInt64Quantum(endian,unsigned_value,p);
1171
0
                        SetGraySample(q,ScaleLongToQuantum(unsigned_value));
1172
0
                        ImportUInt64Quantum(endian,unsigned_value,p);
1173
0
                        SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
1174
0
                        q++;
1175
0
                      }
1176
0
                  }
1177
0
                break;
1178
0
              }
1179
10.5k
            default:
1180
10.5k
              {
1181
                /*
1182
                  Arbitrary Depth.
1183
                */
1184
10.5k
                BitStreamReadHandle
1185
10.5k
                  stream;
1186
1187
10.5k
                MagickBitStreamInitializeRead(&stream,p);
1188
1.54M
                for (x = number_pixels; x != 0; --x)
1189
1.53M
                  {
1190
1.53M
                    unsigned_value=MagickBitStreamMSBRead(&stream,quantum_size);
1191
1.53M
                    if (QuantumDepth >  sample_bits)
1192
1.52M
                      unsigned_value *= unsigned_scale;
1193
7.56k
                    else if (QuantumDepth <  sample_bits)
1194
7.56k
                      unsigned_value /= unsigned_scale;
1195
1.53M
                    if (grayscale_miniswhite)
1196
554k
                      unsigned_value = MaxRGB-unsigned_value;
1197
1.53M
                    SetGraySample(q,unsigned_value);
1198
1199
1.53M
                    unsigned_value=MagickBitStreamMSBRead(&stream,quantum_size);
1200
1.53M
                    if (QuantumDepth >  sample_bits)
1201
1.52M
                      unsigned_value *= unsigned_scale;
1202
7.56k
                    else if (QuantumDepth <  sample_bits)
1203
7.56k
                      unsigned_value /= unsigned_scale;
1204
1.53M
                    SetOpacitySample(q,MaxRGB-unsigned_value);
1205
1.53M
                    q++;
1206
1.53M
                  }
1207
10.5k
                break;
1208
0
              }
1209
35.8k
            }
1210
35.8k
        }
1211
0
      else
1212
0
        {
1213
          /*
1214
            PseudoClass representation.
1215
          */
1216
          /*
1217
            Input is organized as a gray level followed by opacity level
1218
            Colormap array is pre-stuffed with ascending or descending gray
1219
            levels according to the gray quantum representation.
1220
          */
1221
0
          register unsigned int
1222
0
            indexes_scale = 1U;
1223
1224
0
          assert(image->colors <= MaxColormapSize);
1225
1226
0
          if ((image->storage_class != PseudoClass) ||
1227
0
              (image->colors == 0) ||
1228
0
              (indexes == (IndexPacket *) NULL))
1229
0
            ThrowBinaryException3(ImageError,ImageIsNotColormapped,
1230
0
                                  UnableToImportImagePixels);
1231
1232
0
          if (unsigned_maxvalue > (image->colors-1))
1233
0
            indexes_scale=(unsigned_maxvalue/(image->colors-1));
1234
1235
0
          if ( (quantum_size >= 8) && (quantum_size % 8U == 0U) )
1236
0
            {
1237
              /*
1238
                Modulo-8 sample sizes
1239
              */
1240
0
              if (indexes_scale == 1U)
1241
0
                {
1242
0
                  for (x = number_pixels; x != 0; --x)
1243
0
                    {
1244
0
                      ImportModulo8Quantum(index,quantum_size,p);
1245
0
                      VerifyColormapIndex(image,index);
1246
0
                      if (grayscale_miniswhite)
1247
0
                        index=(image->colors-1)-index;
1248
0
                      *indexes++=index;
1249
0
                      *q=image->colormap[index];
1250
1251
0
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
1252
0
                      if (QuantumDepth >  sample_bits)
1253
0
                        unsigned_value *= unsigned_scale;
1254
0
                      else if (QuantumDepth <  sample_bits)
1255
0
                        unsigned_value /= unsigned_scale;
1256
0
                      SetOpacitySample(q,MaxRGB-unsigned_value);
1257
0
                      q++;
1258
0
                    }
1259
0
                }
1260
0
              else
1261
0
                {
1262
0
                  for (x = number_pixels; x != 0; --x)
1263
0
                    {
1264
0
                      ImportModulo8Quantum(index,quantum_size,p);
1265
0
                      index /= indexes_scale;
1266
0
                      VerifyColormapIndex(image,index);
1267
0
                      if (grayscale_miniswhite)
1268
0
                        index=(image->colors-1)-index;
1269
0
                      *indexes++=index;
1270
0
                      *q=image->colormap[index];
1271
1272
0
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
1273
0
                      if (QuantumDepth >  sample_bits)
1274
0
                        unsigned_value *= unsigned_scale;
1275
0
                      else if (QuantumDepth <  sample_bits)
1276
0
                        unsigned_value /= unsigned_scale;
1277
0
                      SetOpacitySample(q,MaxRGB-unsigned_value);
1278
0
                      q++;
1279
0
                    }
1280
0
                }
1281
0
            }
1282
0
          else
1283
0
            {
1284
              /*
1285
                Arbitrary sample size
1286
              */
1287
0
              BitStreamReadHandle
1288
0
                stream;
1289
1290
0
              MagickBitStreamInitializeRead(&stream,p);
1291
0
              for (x = number_pixels; x != 0; --x)
1292
0
                {
1293
0
                  index=MagickBitStreamMSBRead(&stream,quantum_size);
1294
0
                  index /= indexes_scale;
1295
0
                  VerifyColormapIndex(image,index);
1296
0
                  if (grayscale_miniswhite)
1297
0
                    index=(image->colors-1)-index;
1298
0
                  *indexes++=index;
1299
0
                  *q=image->colormap[index];
1300
1301
0
                  unsigned_value=MagickBitStreamMSBRead(&stream,quantum_size);
1302
0
                  if (QuantumDepth >  sample_bits)
1303
0
                    unsigned_value *= unsigned_scale;
1304
0
                  else if (QuantumDepth <  sample_bits)
1305
0
                    unsigned_value /= unsigned_scale;
1306
0
                  SetOpacitySample(q,MaxRGB-unsigned_value);
1307
0
                  q++;
1308
0
                }
1309
0
            }
1310
0
        }
1311
35.8k
    }
1312
29.7k
  else if (sample_type == FloatQuantumSampleType)
1313
29.7k
    {
1314
29.7k
      switch (quantum_size)
1315
29.7k
        {
1316
7.50k
        case 16:
1317
7.50k
          {
1318
412k
            for (x = number_pixels; x != 0; --x)
1319
405k
              {
1320
405k
                ImportFloat16Quantum(endian,double_value,p);
1321
405k
                double_value -= double_minvalue;
1322
405k
                double_value *= double_scale;
1323
405k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1324
405k
                ImportFloat16Quantum(endian,double_value,p);
1325
405k
                double_value -= double_minvalue;
1326
405k
                double_value *= double_scale;
1327
405k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
1328
405k
                q++;
1329
405k
              }
1330
7.50k
            break;
1331
0
          }
1332
3.61k
        case 24:
1333
3.61k
          {
1334
105k
            for (x = number_pixels; x != 0; --x)
1335
101k
              {
1336
101k
                ImportFloat24Quantum(endian,double_value,p);
1337
101k
                double_value -= double_minvalue;
1338
101k
                double_value *= double_scale;
1339
101k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1340
101k
                ImportFloat24Quantum(endian,double_value,p);
1341
101k
                double_value -= double_minvalue;
1342
101k
                double_value *= double_scale;
1343
101k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
1344
101k
                q++;
1345
101k
              }
1346
3.61k
            break;
1347
0
          }
1348
4.52k
        case 32:
1349
4.52k
          {
1350
73.7k
            for (x = number_pixels; x != 0; --x)
1351
69.2k
              {
1352
69.2k
                ImportFloat32Quantum(endian,double_value,p);
1353
69.2k
                double_value -= double_minvalue;
1354
69.2k
                double_value *= double_scale;
1355
69.2k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1356
69.2k
                ImportFloat32Quantum(endian,double_value,p);
1357
69.2k
                double_value -= double_minvalue;
1358
69.2k
                double_value *= double_scale;
1359
69.2k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
1360
69.2k
                q++;
1361
69.2k
              }
1362
4.52k
            break;
1363
0
          }
1364
14.1k
        case 64:
1365
14.1k
          {
1366
178k
            for (x = number_pixels; x != 0; --x)
1367
164k
              {
1368
164k
                ImportFloat64Quantum(endian,double_value,p);
1369
164k
                double_value -= double_minvalue;
1370
164k
                double_value *= double_scale;
1371
164k
                SetGraySample(q,RoundDoubleToQuantumN(double_value));
1372
164k
                ImportFloat64Quantum(endian,double_value,p);
1373
164k
                double_value -= double_minvalue;
1374
164k
                double_value *= double_scale;
1375
164k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
1376
164k
                q++;
1377
164k
              }
1378
14.1k
            break;
1379
0
          }
1380
0
        default:
1381
0
          break;
1382
29.7k
        }
1383
29.7k
    }
1384
1385
65.5k
  if (import_info)
1386
58.5k
    {
1387
58.5k
      import_info->bytes_imported=p-source;
1388
58.5k
    }
1389
1390
65.5k
  return MagickPass;
1391
65.5k
}
1392
1393
static MagickPassFail
1394
ImportRedQuantumType(const unsigned char *source,
1395
                     PixelPacket* restrict q,
1396
                     const unsigned long number_pixels,
1397
                     const unsigned int quantum_size,
1398
                     const QuantumSampleType sample_type,
1399
                     const unsigned int unsigned_scale,
1400
                     const double double_minvalue,
1401
                     const double double_scale,
1402
                     const EndianType endian,
1403
                     ImportPixelAreaInfo *import_info)
1404
452k
{
1405
452k
  const unsigned char * restrict
1406
452k
    p;
1407
1408
452k
  register unsigned long
1409
452k
    x;
1410
1411
452k
  double
1412
452k
    double_value;
1413
1414
452k
  register magick_uint32_t
1415
452k
    unsigned_value;
1416
1417
452k
  unsigned int
1418
452k
    sample_bits;
1419
1420
452k
  sample_bits=quantum_size;
1421
452k
  p=source;
1422
1423
452k
  if (sample_type == UnsignedQuantumSampleType)
1424
439k
    {
1425
439k
      switch (quantum_size)
1426
439k
        {
1427
414k
        case 8:
1428
414k
          {
1429
122M
            for (x = number_pixels; x != 0; --x)
1430
122M
              {
1431
122M
                SetRedSample(q,ScaleCharToQuantum(*p++));
1432
122M
                q++;
1433
122M
              }
1434
414k
            break;
1435
0
          }
1436
3.17k
        case 16:
1437
3.17k
          {
1438
256k
            for (x = number_pixels; x != 0; --x)
1439
252k
              {
1440
252k
                ImportUInt16Quantum(endian,unsigned_value,p);
1441
252k
                SetRedSample(q,ScaleShortToQuantum(unsigned_value));
1442
252k
                q++;
1443
252k
              }
1444
3.17k
            break;
1445
0
          }
1446
3.27k
        case 32:
1447
3.27k
          {
1448
281k
            for (x = number_pixels; x != 0; --x)
1449
278k
              {
1450
278k
                ImportUInt32Quantum(endian,unsigned_value,p);
1451
278k
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
1452
278k
                q++;
1453
278k
              }
1454
3.27k
            break;
1455
0
          }
1456
772
        case 64:
1457
772
          {
1458
2.78k
            for (x = number_pixels; x != 0; --x)
1459
2.01k
              {
1460
2.01k
                ImportUInt64Quantum(endian,unsigned_value,p);
1461
2.01k
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
1462
2.01k
                q++;
1463
2.01k
              }
1464
772
            break;
1465
0
          }
1466
18.1k
        default:
1467
18.1k
          {
1468
            /*
1469
              Arbitrary sample size
1470
            */
1471
18.1k
            BitStreamReadHandle
1472
18.1k
              stream;
1473
1474
18.1k
            MagickBitStreamInitializeRead(&stream,p);
1475
18.1k
            if (QuantumDepth >=  sample_bits)
1476
16.6k
              {
1477
                /* Scale up */
1478
15.1M
                for (x = number_pixels; x != 0; --x)
1479
15.1M
                  {
1480
15.1M
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
1481
15.1M
                    q++;
1482
15.1M
                  }
1483
16.6k
              }
1484
1.56k
            else
1485
1.56k
              {
1486
                /* Scale down */
1487
4.82k
                for (x = number_pixels; x != 0; --x)
1488
3.25k
                  {
1489
3.25k
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
1490
3.25k
                    q++;
1491
3.25k
                  }
1492
1.56k
              }
1493
18.1k
            break;
1494
0
          }
1495
439k
        }
1496
439k
    }
1497
12.3k
  else if (sample_type == FloatQuantumSampleType)
1498
12.3k
    {
1499
12.3k
      switch (quantum_size)
1500
12.3k
        {
1501
2.42k
        case 16:
1502
2.42k
          {
1503
12.8k
            for (x = number_pixels; x != 0; --x)
1504
10.3k
              {
1505
10.3k
                ImportFloat16Quantum(endian,double_value,p);
1506
10.3k
                double_value -= double_minvalue;
1507
10.3k
                double_value *= double_scale;
1508
10.3k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
1509
10.3k
                q++;
1510
10.3k
              }
1511
2.42k
            break;
1512
0
          }
1513
1.26k
        case 24:
1514
1.26k
          {
1515
22.3k
            for (x = number_pixels; x != 0; --x)
1516
21.1k
              {
1517
21.1k
                ImportFloat24Quantum(endian,double_value,p);
1518
21.1k
                double_value -= double_minvalue;
1519
21.1k
                double_value *= double_scale;
1520
21.1k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
1521
21.1k
                q++;
1522
21.1k
              }
1523
1.26k
            break;
1524
0
          }
1525
4.32k
        case 32:
1526
4.32k
          {
1527
97.1k
            for (x = number_pixels; x != 0; --x)
1528
92.8k
              {
1529
92.8k
                ImportFloat32Quantum(endian,double_value,p);
1530
92.8k
                double_value -= double_minvalue;
1531
92.8k
                double_value *= double_scale;
1532
92.8k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
1533
92.8k
                q++;
1534
92.8k
              }
1535
4.32k
            break;
1536
0
          }
1537
4.28k
        case 64:
1538
4.28k
          {
1539
49.8k
            for (x = number_pixels; x != 0; --x)
1540
45.6k
              {
1541
45.6k
                ImportFloat64Quantum(endian,double_value,p);
1542
45.6k
                double_value -= double_minvalue;
1543
45.6k
                double_value *= double_scale;
1544
45.6k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
1545
45.6k
                q++;
1546
45.6k
              }
1547
4.28k
            break;
1548
0
          }
1549
0
        default:
1550
0
          break;
1551
12.3k
        }
1552
12.3k
    }
1553
1554
452k
  if (import_info)
1555
23.0k
    {
1556
23.0k
      import_info->bytes_imported=p-source;
1557
23.0k
    }
1558
1559
452k
  return MagickPass;
1560
452k
}
1561
1562
static MagickPassFail
1563
ImportGreenQuantumType(const unsigned char *source,
1564
                       PixelPacket* restrict q,
1565
                       const unsigned long number_pixels,
1566
                       const unsigned int quantum_size,
1567
                       const QuantumSampleType sample_type,
1568
                       const unsigned int unsigned_scale,
1569
                       const double double_minvalue,
1570
                       const double double_scale,
1571
                       const EndianType endian,
1572
                       ImportPixelAreaInfo *import_info)
1573
203k
{
1574
203k
  const unsigned char * restrict
1575
203k
    p;
1576
1577
203k
  register unsigned long
1578
203k
    x;
1579
1580
203k
  double
1581
203k
    double_value;
1582
1583
203k
  register magick_uint32_t
1584
203k
    unsigned_value;
1585
1586
203k
  unsigned int
1587
203k
    sample_bits;
1588
1589
203k
  sample_bits=quantum_size;
1590
203k
  p=source;
1591
1592
203k
  if (sample_type == UnsignedQuantumSampleType)
1593
193k
    {
1594
193k
      switch (quantum_size)
1595
193k
        {
1596
182k
        case 8:
1597
182k
          {
1598
51.0M
            for (x = number_pixels; x != 0; --x)
1599
50.8M
              {
1600
50.8M
                SetGreenSample(q,ScaleCharToQuantum(*p++));
1601
50.8M
                q++;
1602
50.8M
              }
1603
182k
            break;
1604
0
          }
1605
2.58k
        case 16:
1606
2.58k
          {
1607
10.7k
            for (x = number_pixels; x != 0; --x)
1608
8.17k
              {
1609
8.17k
                ImportUInt16Quantum(endian,unsigned_value,p);
1610
8.17k
                SetGreenSample(q,ScaleShortToQuantum(unsigned_value));
1611
8.17k
                q++;
1612
8.17k
              }
1613
2.58k
            break;
1614
0
          }
1615
1.41k
        case 32:
1616
1.41k
          {
1617
6.60k
            for (x = number_pixels; x != 0; --x)
1618
5.19k
              {
1619
5.19k
                ImportUInt32Quantum(endian,unsigned_value,p);
1620
5.19k
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
1621
5.19k
                q++;
1622
5.19k
              }
1623
1.41k
            break;
1624
0
          }
1625
549
        case 64:
1626
549
          {
1627
1.89k
            for (x = number_pixels; x != 0; --x)
1628
1.34k
              {
1629
1.34k
                ImportUInt64Quantum(endian,unsigned_value,p);
1630
1.34k
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
1631
1.34k
                q++;
1632
1.34k
              }
1633
549
            break;
1634
0
          }
1635
6.17k
        default:
1636
6.17k
          {
1637
            /*
1638
              Arbitrary sample size
1639
            */
1640
6.17k
            BitStreamReadHandle
1641
6.17k
              stream;
1642
1643
6.17k
            MagickBitStreamInitializeRead(&stream,p);
1644
6.17k
            if (QuantumDepth >=  sample_bits)
1645
4.61k
              {
1646
                /* Scale up */
1647
547k
                for (x = number_pixels; x != 0; --x)
1648
542k
                  {
1649
542k
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
1650
542k
                    q++;
1651
542k
                  }
1652
4.61k
              }
1653
1.55k
            else
1654
1.55k
              {
1655
                /* Scale down */
1656
4.80k
                for (x = number_pixels; x != 0; --x)
1657
3.24k
                  {
1658
3.24k
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
1659
3.24k
                    q++;
1660
3.24k
                  }
1661
1.55k
              }
1662
6.17k
            break;
1663
0
          }
1664
193k
        }
1665
193k
    }
1666
10.1k
  else if (sample_type == FloatQuantumSampleType)
1667
10.1k
    {
1668
10.1k
      switch (quantum_size)
1669
10.1k
        {
1670
2.20k
        case 16:
1671
2.20k
          {
1672
11.9k
            for (x = number_pixels; x != 0; --x)
1673
9.70k
              {
1674
9.70k
                ImportFloat16Quantum(endian,double_value,p);
1675
9.70k
                double_value -= double_minvalue;
1676
9.70k
                double_value *= double_scale;
1677
9.70k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
1678
9.70k
                q++;
1679
9.70k
              }
1680
2.20k
            break;
1681
0
          }
1682
1.14k
        case 24:
1683
1.14k
          {
1684
12.5k
            for (x = number_pixels; x != 0; --x)
1685
11.3k
              {
1686
11.3k
                ImportFloat24Quantum(endian,double_value,p);
1687
11.3k
                double_value -= double_minvalue;
1688
11.3k
                double_value *= double_scale;
1689
11.3k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
1690
11.3k
                q++;
1691
11.3k
              }
1692
1.14k
            break;
1693
0
          }
1694
3.12k
        case 32:
1695
3.12k
          {
1696
16.9k
            for (x = number_pixels; x != 0; --x)
1697
13.8k
              {
1698
13.8k
                ImportFloat32Quantum(endian,double_value,p);
1699
13.8k
                double_value -= double_minvalue;
1700
13.8k
                double_value *= double_scale;
1701
13.8k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
1702
13.8k
                q++;
1703
13.8k
              }
1704
3.12k
            break;
1705
0
          }
1706
3.67k
        case 64:
1707
3.67k
          {
1708
48.2k
            for (x = number_pixels; x != 0; --x)
1709
44.5k
              {
1710
44.5k
                ImportFloat64Quantum(endian,double_value,p);
1711
44.5k
                double_value -= double_minvalue;
1712
44.5k
                double_value *= double_scale;
1713
44.5k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
1714
44.5k
                q++;
1715
44.5k
              }
1716
3.67k
            break;
1717
0
          }
1718
0
        default:
1719
0
          break;
1720
10.1k
        }
1721
10.1k
    }
1722
1723
203k
  if (import_info)
1724
19.8k
    {
1725
19.8k
      import_info->bytes_imported=p-source;
1726
19.8k
    }
1727
1728
203k
  return MagickPass;
1729
203k
}
1730
1731
static MagickPassFail
1732
ImportBlueQuantumType(const unsigned char *source,
1733
                      PixelPacket* restrict q,
1734
                      const unsigned long number_pixels,
1735
                      const unsigned int quantum_size,
1736
                      const QuantumSampleType sample_type,
1737
                      const unsigned int unsigned_scale,
1738
                      const double double_minvalue,
1739
                      const double double_scale,
1740
                      const EndianType endian,
1741
                      ImportPixelAreaInfo *import_info)
1742
123k
{
1743
123k
  const unsigned char * restrict
1744
123k
    p;
1745
1746
123k
  register unsigned long
1747
123k
    x;
1748
1749
123k
  double
1750
123k
    double_value;
1751
1752
123k
  register magick_uint32_t
1753
123k
    unsigned_value;
1754
1755
123k
  unsigned int
1756
123k
    sample_bits;
1757
1758
123k
  sample_bits=quantum_size;
1759
123k
  p=source;
1760
1761
123k
  if (sample_type == UnsignedQuantumSampleType)
1762
115k
    {
1763
115k
      switch (quantum_size)
1764
115k
        {
1765
106k
        case 8:
1766
106k
          {
1767
31.8M
            for (x = number_pixels; x != 0; --x)
1768
31.7M
              {
1769
31.7M
                SetBlueSample(q,ScaleCharToQuantum(*p++));
1770
31.7M
                q++;
1771
31.7M
              }
1772
106k
            break;
1773
0
          }
1774
1.89k
        case 16:
1775
1.89k
          {
1776
8.34k
            for (x = number_pixels; x != 0; --x)
1777
6.44k
              {
1778
6.44k
                ImportUInt16Quantum(endian,unsigned_value,p);
1779
6.44k
                SetBlueSample(q,ScaleShortToQuantum(unsigned_value));
1780
6.44k
                q++;
1781
6.44k
              }
1782
1.89k
            break;
1783
0
          }
1784
1.17k
        case 32:
1785
1.17k
          {
1786
5.72k
            for (x = number_pixels; x != 0; --x)
1787
4.54k
              {
1788
4.54k
                ImportUInt32Quantum(endian,unsigned_value,p);
1789
4.54k
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
1790
4.54k
                q++;
1791
4.54k
              }
1792
1.17k
            break;
1793
0
          }
1794
264
        case 64:
1795
264
          {
1796
988
            for (x = number_pixels; x != 0; --x)
1797
724
              {
1798
724
                ImportUInt64Quantum(endian,unsigned_value,p);
1799
724
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
1800
724
                q++;
1801
724
              }
1802
264
            break;
1803
0
          }
1804
5.39k
        default:
1805
5.39k
          {
1806
            /*
1807
              Arbitrary sample size
1808
            */
1809
5.39k
            BitStreamReadHandle
1810
5.39k
              stream;
1811
1812
5.39k
            MagickBitStreamInitializeRead(&stream,p);
1813
5.39k
            if (QuantumDepth >=  sample_bits)
1814
4.05k
              {
1815
                /* Scale up */
1816
540k
                for (x = number_pixels; x != 0; --x)
1817
536k
                  {
1818
536k
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
1819
536k
                    q++;
1820
536k
                  }
1821
4.05k
              }
1822
1.34k
            else
1823
1.34k
              {
1824
                /* Scale down */
1825
4.35k
                for (x = number_pixels; x != 0; --x)
1826
3.01k
                  {
1827
3.01k
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
1828
3.01k
                    q++;
1829
3.01k
                  }
1830
1.34k
              }
1831
5.39k
            break;
1832
0
          }
1833
115k
        }
1834
1835
115k
    }
1836
8.37k
  else if (sample_type == FloatQuantumSampleType)
1837
8.37k
    {
1838
8.37k
      switch (quantum_size)
1839
8.37k
        {
1840
1.91k
        case 16:
1841
1.91k
          {
1842
10.8k
            for (x = number_pixels; x != 0; --x)
1843
8.92k
              {
1844
8.92k
                ImportFloat16Quantum(endian,double_value,p);
1845
8.92k
                double_value -= double_minvalue;
1846
8.92k
                double_value *= double_scale;
1847
8.92k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
1848
8.92k
                q++;
1849
8.92k
              }
1850
1.91k
            break;
1851
0
          }
1852
999
        case 24:
1853
999
          {
1854
11.8k
            for (x = number_pixels; x != 0; --x)
1855
10.8k
              {
1856
10.8k
                ImportFloat24Quantum(endian,double_value,p);
1857
10.8k
                double_value -= double_minvalue;
1858
10.8k
                double_value *= double_scale;
1859
10.8k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
1860
10.8k
                q++;
1861
10.8k
              }
1862
999
            break;
1863
0
          }
1864
2.65k
        case 32:
1865
2.65k
          {
1866
15.0k
            for (x = number_pixels; x != 0; --x)
1867
12.4k
              {
1868
12.4k
                ImportFloat32Quantum(endian,double_value,p);
1869
12.4k
                double_value -= double_minvalue;
1870
12.4k
                double_value *= double_scale;
1871
12.4k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
1872
12.4k
                q++;
1873
12.4k
              }
1874
2.65k
            break;
1875
0
          }
1876
2.81k
        case 64:
1877
2.81k
          {
1878
43.5k
            for (x = number_pixels; x != 0; --x)
1879
40.6k
              {
1880
40.6k
                ImportFloat64Quantum(endian,double_value,p);
1881
40.6k
                double_value -= double_minvalue;
1882
40.6k
                double_value *= double_scale;
1883
40.6k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
1884
40.6k
                q++;
1885
40.6k
              }
1886
2.81k
            break;
1887
0
          }
1888
0
        default:
1889
0
          break;
1890
8.37k
        }
1891
8.37k
    }
1892
1893
123k
  if (import_info)
1894
17.3k
    {
1895
17.3k
      import_info->bytes_imported=p-source;
1896
17.3k
    }
1897
1898
123k
  return MagickPass;
1899
123k
}
1900
1901
static MagickPassFail
1902
ImportAlphaQuantumType(const unsigned char *source,
1903
                       PixelPacket* restrict q,
1904
                       IndexPacket * restrict indexes,
1905
                       const unsigned long number_pixels,
1906
                       const unsigned int quantum_size,
1907
                       const QuantumSampleType sample_type,
1908
                       const unsigned int unsigned_scale,
1909
                       const double double_minvalue,
1910
                       const double double_scale,
1911
                       const EndianType endian,
1912
                       Image *image,
1913
                       ImportPixelAreaInfo *import_info)
1914
13.2k
{
1915
13.2k
  const unsigned char * restrict
1916
13.2k
    p;
1917
1918
13.2k
  register unsigned long
1919
13.2k
    x;
1920
1921
13.2k
  double
1922
13.2k
    double_value;
1923
1924
13.2k
  register magick_uint32_t
1925
13.2k
    unsigned_value;
1926
1927
13.2k
  unsigned int
1928
13.2k
    sample_bits;
1929
1930
13.2k
  sample_bits=quantum_size;
1931
13.2k
  p=source;
1932
1933
13.2k
  if (image->colorspace == CMYKColorspace)
1934
4.59k
    {
1935
4.59k
      if (indexes == (IndexPacket *) NULL)
1936
0
          ThrowBinaryException3(ImageError,CMYKAImageLacksAlphaChannel,
1937
4.59k
                                UnableToImportImagePixels);
1938
1939
4.59k
      if (sample_type == UnsignedQuantumSampleType)
1940
1.27k
        {
1941
1.27k
          if ( (quantum_size >= 8) && (quantum_size % 8U == 0U) )
1942
880
            {
1943
              /*
1944
                Modulo-8 sample sizes
1945
              */
1946
880
              if( QuantumDepth == sample_bits)
1947
195
                {
1948
                  /* Unity scale */
1949
1.86k
                  for (x = number_pixels; x != 0; --x)
1950
1.66k
                    {
1951
1.66k
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
1952
1.66k
                      *indexes++=(IndexPacket) MaxRGB-unsigned_value;
1953
1.66k
                    }
1954
195
                }
1955
685
              else if (QuantumDepth >  sample_bits)
1956
115
                {
1957
                  /* Scale up */
1958
512
                  for (x = number_pixels; x != 0; --x)
1959
397
                    {
1960
397
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
1961
397
                      *indexes++=(IndexPacket) MaxRGB-unsigned_value*unsigned_scale;
1962
397
                    }
1963
115
                }
1964
570
              else
1965
570
                {
1966
                  /* Scale down */
1967
2.99k
                  for (x = number_pixels; x != 0; --x)
1968
2.42k
                    {
1969
2.42k
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
1970
2.42k
                      *indexes++=(IndexPacket) MaxRGB-unsigned_value/unsigned_scale;
1971
2.42k
                    }
1972
570
                }
1973
880
            }
1974
395
          else
1975
395
            {
1976
              /*
1977
                Arbitrary sample size
1978
              */
1979
395
              BitStreamReadHandle
1980
395
                stream;
1981
1982
395
              MagickBitStreamInitializeRead(&stream,p);
1983
395
              if (QuantumDepth >=  sample_bits)
1984
324
                {
1985
                  /* Scale up */
1986
2.87k
                  for (x = number_pixels; x != 0; --x)
1987
2.55k
                    {
1988
2.55k
                      *indexes++=(IndexPacket) MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale;
1989
2.55k
                    }
1990
324
                }
1991
71
              else
1992
71
                {
1993
                  /* Scale down */
1994
335
                  for (x = number_pixels; x != 0; --x)
1995
264
                    {
1996
264
                      *indexes++=(IndexPacket) MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale;
1997
264
                    }
1998
71
                }
1999
395
            }
2000
1.27k
        }
2001
3.31k
      else if (sample_type == FloatQuantumSampleType)
2002
3.31k
        {
2003
3.31k
          switch (quantum_size)
2004
3.31k
            {
2005
731
            case 16:
2006
731
              {
2007
6.23k
                for (x = number_pixels; x != 0; --x)
2008
5.50k
                  {
2009
5.50k
                    ImportFloat16Quantum(endian,double_value,p);
2010
5.50k
                    double_value -= double_minvalue;
2011
5.50k
                    double_value *= double_scale;
2012
5.50k
                    *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
2013
5.50k
                  }
2014
731
                break;
2015
0
              }
2016
389
            case 24:
2017
389
              {
2018
3.23k
                for (x = number_pixels; x != 0; --x)
2019
2.84k
                  {
2020
2.84k
                    ImportFloat24Quantum(endian,double_value,p);
2021
2.84k
                    double_value -= double_minvalue;
2022
2.84k
                    double_value *= double_scale;
2023
2.84k
                    *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
2024
2.84k
                  }
2025
389
                break;
2026
0
              }
2027
956
            case 32:
2028
956
              {
2029
5.39k
                for (x = number_pixels; x != 0; --x)
2030
4.44k
                  {
2031
4.44k
                    ImportFloat32Quantum(endian,double_value,p);
2032
4.44k
                    double_value -= double_minvalue;
2033
4.44k
                    double_value *= double_scale;
2034
4.44k
                    *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
2035
4.44k
                  }
2036
956
                break;
2037
0
              }
2038
1.24k
            case 64:
2039
1.24k
              {
2040
4.98k
                for (x = number_pixels; x != 0; --x)
2041
3.74k
                  {
2042
3.74k
                    ImportFloat64Quantum(endian,double_value,p);
2043
3.74k
                    double_value -= double_minvalue;
2044
3.74k
                    double_value *= double_scale;
2045
3.74k
                    *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
2046
3.74k
                  }
2047
1.24k
                break;
2048
0
              }
2049
0
            default:
2050
0
              break;
2051
3.31k
            }
2052
3.31k
        }
2053
4.59k
    }
2054
8.66k
  else
2055
8.66k
    {
2056
      /* RGB */
2057
8.66k
      if (sample_type == UnsignedQuantumSampleType)
2058
5.80k
        {
2059
5.80k
          if ( (quantum_size >= 8) && (quantum_size % 8U == 0U) )
2060
1.98k
            {
2061
              /*
2062
                Modulo-8 sample sizes
2063
              */
2064
1.98k
              if(QuantumDepth == sample_bits)
2065
370
                {
2066
                  /* Unity scale */
2067
2.06k
                  for (x = number_pixels; x != 0; --x)
2068
1.69k
                    {
2069
1.69k
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
2070
1.69k
                      SetOpacitySample(q,MaxRGB-unsigned_value);
2071
1.69k
                      q++;
2072
1.69k
                    }
2073
370
                }
2074
1.61k
              else if (QuantumDepth >  sample_bits)
2075
1.11k
                {
2076
                  /* Scale up */
2077
86.1k
                  for (x = number_pixels; x != 0; --x)
2078
85.0k
                    {
2079
85.0k
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
2080
85.0k
                      SetOpacitySample(q,MaxRGB-unsigned_value*unsigned_scale);
2081
85.0k
                      q++;
2082
85.0k
                    }
2083
1.11k
                }
2084
503
              else
2085
503
                {
2086
                  /* Scale down */
2087
2.40k
                  for (x = number_pixels; x != 0; --x)
2088
1.90k
                    {
2089
1.90k
                      ImportModulo8Quantum(unsigned_value,quantum_size,p);
2090
1.90k
                      SetOpacitySample(q,MaxRGB-unsigned_value/unsigned_scale);
2091
1.90k
                      q++;
2092
1.90k
                    }
2093
503
                }
2094
1.98k
            }
2095
3.82k
          else
2096
3.82k
            {
2097
              /*
2098
                Arbitrary sample size
2099
              */
2100
3.82k
              BitStreamReadHandle
2101
3.82k
                stream;
2102
2103
3.82k
              MagickBitStreamInitializeRead(&stream,p);
2104
3.82k
              if (QuantumDepth >=  sample_bits)
2105
2.27k
                {
2106
                  /* Scale up */
2107
30.9k
                  for (x = number_pixels; x != 0; --x)
2108
28.6k
                    {
2109
28.6k
                      SetOpacitySample(q,MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2110
28.6k
                      q++;
2111
28.6k
                    }
2112
2.27k
                }
2113
1.54k
              else
2114
1.54k
                {
2115
                  /* Scale down */
2116
13.0k
                  for (x = number_pixels; x != 0; --x)
2117
11.5k
                    {
2118
11.5k
                      SetOpacitySample(q,MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2119
11.5k
                      q++;
2120
11.5k
                    }
2121
1.54k
                }
2122
3.82k
            }
2123
5.80k
        }
2124
2.86k
      else if (sample_type == FloatQuantumSampleType)
2125
2.86k
        {
2126
2.86k
          switch (quantum_size)
2127
2.86k
            {
2128
560
            case 16:
2129
560
              {
2130
4.09k
                for (x = number_pixels; x != 0; --x)
2131
3.53k
                  {
2132
3.53k
                    ImportFloat16Quantum(endian,double_value,p);
2133
3.53k
                    double_value -= double_minvalue;
2134
3.53k
                    double_value *= double_scale;
2135
3.53k
                    SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2136
3.53k
                    q++;
2137
3.53k
                  }
2138
560
                break;
2139
0
              }
2140
348
            case 24:
2141
348
              {
2142
4.48k
                for (x = number_pixels; x != 0; --x)
2143
4.13k
                  {
2144
4.13k
                    ImportFloat24Quantum(endian,double_value,p);
2145
4.13k
                    double_value -= double_minvalue;
2146
4.13k
                    double_value *= double_scale;
2147
4.13k
                    SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2148
4.13k
                    q++;
2149
4.13k
                  }
2150
348
                break;
2151
0
              }
2152
1.20k
            case 32:
2153
1.20k
              {
2154
3.21k
                for (x = number_pixels; x != 0; --x)
2155
2.01k
                  {
2156
2.01k
                    ImportFloat32Quantum(endian,double_value,p);
2157
2.01k
                    double_value -= double_minvalue;
2158
2.01k
                    double_value *= double_scale;
2159
2.01k
                    SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2160
2.01k
                    q++;
2161
2.01k
                  }
2162
1.20k
                break;
2163
0
              }
2164
747
            case 64:
2165
747
              {
2166
7.94k
                for (x = number_pixels; x != 0; --x)
2167
7.19k
                  {
2168
7.19k
                    ImportFloat64Quantum(endian,double_value,p);
2169
7.19k
                    double_value -= double_minvalue;
2170
7.19k
                    double_value *= double_scale;
2171
7.19k
                    SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2172
7.19k
                    q++;
2173
7.19k
                  }
2174
747
                break;
2175
0
              }
2176
0
            default:
2177
0
              break;
2178
2.86k
            }
2179
2.86k
        }
2180
8.66k
    }
2181
2182
13.2k
  if (import_info)
2183
13.2k
    {
2184
13.2k
      import_info->bytes_imported=p-source;
2185
13.2k
    }
2186
2187
13.2k
  return MagickPass;
2188
13.2k
}
2189
2190
static MagickPassFail
2191
ImportBlackQuantumType(const unsigned char *source,
2192
                       PixelPacket* restrict q,
2193
                       const unsigned long number_pixels,
2194
                       const unsigned int quantum_size,
2195
                       const QuantumSampleType sample_type,
2196
                       const unsigned int unsigned_scale,
2197
                       const double double_minvalue,
2198
                       const double double_scale,
2199
                       const EndianType endian,
2200
                       ImportPixelAreaInfo *import_info)
2201
50.1k
{
2202
50.1k
  const unsigned char * restrict
2203
50.1k
    p;
2204
2205
50.1k
  register unsigned long
2206
50.1k
    x;
2207
2208
50.1k
  double
2209
50.1k
    double_value;
2210
2211
50.1k
  register magick_uint32_t
2212
50.1k
    unsigned_value;
2213
2214
50.1k
  unsigned int
2215
50.1k
    sample_bits;
2216
2217
50.1k
  sample_bits=quantum_size;
2218
50.1k
  p=source;
2219
2220
50.1k
  if (sample_type == UnsignedQuantumSampleType)
2221
44.0k
    {
2222
44.0k
      switch (quantum_size)
2223
44.0k
        {
2224
40.4k
        case 8:
2225
40.4k
          {
2226
13.5M
            for (x = number_pixels; x != 0; --x)
2227
13.4M
              {
2228
13.4M
                SetBlackSample(q,ScaleCharToQuantum(*p++));
2229
13.4M
                q++;
2230
13.4M
              }
2231
40.4k
            break;
2232
0
          }
2233
981
        case 16:
2234
981
          {
2235
4.39k
            for (x = number_pixels; x != 0; --x)
2236
3.41k
              {
2237
3.41k
                ImportUInt16Quantum(endian,unsigned_value,p);
2238
3.41k
                SetBlackSample(q,ScaleShortToQuantum(unsigned_value));
2239
3.41k
                q++;
2240
3.41k
              }
2241
981
            break;
2242
0
          }
2243
902
        case 32:
2244
902
          {
2245
4.72k
            for (x = number_pixels; x != 0; --x)
2246
3.82k
              {
2247
3.82k
                ImportUInt32Quantum(endian,unsigned_value,p);
2248
3.82k
                SetBlackSample(q,ScaleLongToQuantum(unsigned_value));
2249
3.82k
                q++;
2250
3.82k
              }
2251
902
            break;
2252
0
          }
2253
0
        case 64:
2254
0
          {
2255
0
            for (x = number_pixels; x != 0; --x)
2256
0
              {
2257
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2258
0
                SetBlackSample(q,ScaleLongToQuantum(unsigned_value));
2259
0
                q++;
2260
0
              }
2261
0
            break;
2262
0
          }
2263
1.73k
        default:
2264
1.73k
          {
2265
            /*
2266
              Arbitrary sample size
2267
            */
2268
1.73k
            BitStreamReadHandle
2269
1.73k
              stream;
2270
2271
1.73k
            MagickBitStreamInitializeRead(&stream,p);
2272
1.73k
            if (QuantumDepth >=  sample_bits)
2273
865
              {
2274
                /* Scale up */
2275
5.37k
                for (x = number_pixels; x != 0; --x)
2276
4.50k
                  {
2277
4.50k
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2278
4.50k
                    q++;
2279
4.50k
                  }
2280
865
              }
2281
872
            else
2282
872
              {
2283
                /* Scale down */
2284
3.08k
                for (x = number_pixels; x != 0; --x)
2285
2.20k
                  {
2286
2.20k
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2287
2.20k
                    q++;
2288
2.20k
                  }
2289
872
              }
2290
1.73k
            break;
2291
0
          }
2292
44.0k
        }
2293
44.0k
    }
2294
6.12k
  else if (sample_type == FloatQuantumSampleType)
2295
6.12k
    {
2296
6.12k
      switch (quantum_size)
2297
6.12k
        {
2298
1.65k
        case 16:
2299
1.65k
          {
2300
9.51k
            for (x = number_pixels; x != 0; --x)
2301
7.85k
              {
2302
7.85k
                ImportFloat16Quantum(endian,double_value,p);
2303
7.85k
                double_value -= double_minvalue;
2304
7.85k
                double_value *= double_scale;
2305
7.85k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
2306
7.85k
                q++;
2307
7.85k
              }
2308
1.65k
            break;
2309
0
          }
2310
748
        case 24:
2311
748
          {
2312
7.96k
            for (x = number_pixels; x != 0; --x)
2313
7.21k
              {
2314
7.21k
                ImportFloat24Quantum(endian,double_value,p);
2315
7.21k
                double_value -= double_minvalue;
2316
7.21k
                double_value *= double_scale;
2317
7.21k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
2318
7.21k
                q++;
2319
7.21k
              }
2320
748
            break;
2321
0
          }
2322
1.79k
        case 32:
2323
1.79k
          {
2324
10.5k
            for (x = number_pixels; x != 0; --x)
2325
8.77k
              {
2326
8.77k
                ImportFloat32Quantum(endian,double_value,p);
2327
8.77k
                double_value -= double_minvalue;
2328
8.77k
                double_value *= double_scale;
2329
8.77k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
2330
8.77k
                q++;
2331
8.77k
              }
2332
1.79k
            break;
2333
0
          }
2334
1.92k
        case 64:
2335
1.92k
          {
2336
40.7k
            for (x = number_pixels; x != 0; --x)
2337
38.8k
              {
2338
38.8k
                ImportFloat64Quantum(endian,double_value,p);
2339
38.8k
                double_value -= double_minvalue;
2340
38.8k
                double_value *= double_scale;
2341
38.8k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
2342
38.8k
                q++;
2343
38.8k
              }
2344
1.92k
            break;
2345
0
          }
2346
0
        default:
2347
0
          break;
2348
6.12k
        }
2349
6.12k
    }
2350
2351
50.1k
  if (import_info)
2352
10.8k
    {
2353
10.8k
      import_info->bytes_imported=p-source;
2354
10.8k
    }
2355
2356
50.1k
  return MagickPass;
2357
50.1k
}
2358
2359
static MagickPassFail
2360
ImportRGBQuantumType(const unsigned char *source,
2361
                     PixelPacket* restrict q,
2362
                     const unsigned long number_pixels,
2363
                     const unsigned int quantum_size,
2364
                     const QuantumSampleType sample_type,
2365
                     const unsigned int unsigned_scale,
2366
                     const double double_minvalue,
2367
                     const double double_scale,
2368
                     const EndianType endian,
2369
                     ImportPixelAreaInfo *import_info)
2370
3.63M
{
2371
3.63M
  const unsigned char * restrict
2372
3.63M
    p;
2373
2374
3.63M
  register unsigned long
2375
3.63M
    x;
2376
2377
3.63M
  double
2378
3.63M
    double_value;
2379
2380
3.63M
  register magick_uint32_t
2381
3.63M
    unsigned_value;
2382
2383
3.63M
  unsigned int
2384
3.63M
    sample_bits;
2385
2386
3.63M
  sample_bits=quantum_size;
2387
3.63M
  p=source;
2388
2389
3.63M
  if (sample_type == UnsignedQuantumSampleType)
2390
3.61M
    {
2391
3.61M
      switch (quantum_size)
2392
3.61M
        {
2393
3.08M
        case 8:
2394
3.08M
          {
2395
424M
            for (x = number_pixels; x != 0; --x)
2396
421M
              {
2397
421M
                ImportUInt8Quantum(unsigned_value,p);
2398
421M
                SetRedSample(q,ScaleCharToQuantum(unsigned_value));
2399
421M
                ImportUInt8Quantum(unsigned_value,p);
2400
421M
                SetGreenSample(q,ScaleCharToQuantum(unsigned_value));
2401
421M
                ImportUInt8Quantum(unsigned_value,p);
2402
421M
                SetBlueSample(q,ScaleCharToQuantum(unsigned_value));
2403
421M
                SetOpacitySample(q,OpaqueOpacity);
2404
421M
                q++;
2405
421M
              }
2406
3.08M
            break;
2407
0
          }
2408
111k
        case 16:
2409
111k
          {
2410
12.1M
            for (x = number_pixels; x != 0; --x)
2411
11.9M
              {
2412
11.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2413
11.9M
                SetRedSample(q,ScaleShortToQuantum(unsigned_value));
2414
11.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2415
11.9M
                SetGreenSample(q,ScaleShortToQuantum(unsigned_value));
2416
11.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2417
11.9M
                SetBlueSample(q,ScaleShortToQuantum(unsigned_value));
2418
11.9M
                SetOpacitySample(q,OpaqueOpacity);
2419
11.9M
                q++;
2420
11.9M
              }
2421
111k
            break;
2422
0
          }
2423
263k
        case 32:
2424
263k
          {
2425
65.7M
            for (x = number_pixels; x != 0; --x)
2426
65.4M
              {
2427
65.4M
                ImportUInt32Quantum(endian,unsigned_value,p);
2428
65.4M
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
2429
65.4M
                ImportUInt32Quantum(endian,unsigned_value,p);
2430
65.4M
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
2431
65.4M
                ImportUInt32Quantum(endian,unsigned_value,p);
2432
65.4M
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
2433
65.4M
                SetOpacitySample(q,OpaqueOpacity);
2434
65.4M
                q++;
2435
65.4M
              }
2436
263k
            break;
2437
0
          }
2438
0
        case 64:
2439
0
          {
2440
0
            for (x = number_pixels; x != 0; --x)
2441
0
              {
2442
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2443
0
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
2444
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2445
0
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
2446
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2447
0
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
2448
0
                SetOpacitySample(q,OpaqueOpacity);
2449
0
                q++;
2450
0
              }
2451
0
            break;
2452
0
          }
2453
154k
        default:
2454
154k
          {
2455
            /*
2456
              Arbitrary sample size
2457
            */
2458
154k
            BitStreamReadHandle
2459
154k
              stream;
2460
2461
154k
            MagickBitStreamInitializeRead(&stream,p);
2462
154k
            if (QuantumDepth >=  sample_bits)
2463
152k
              {
2464
                /* Scale up */
2465
37.0M
                for (x = number_pixels; x != 0; --x)
2466
36.9M
                  {
2467
36.9M
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2468
36.9M
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2469
36.9M
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2470
36.9M
                    SetOpacitySample(q,OpaqueOpacity);
2471
36.9M
                    q++;
2472
36.9M
                  }
2473
152k
              }
2474
1.76k
            else if (QuantumDepth <  sample_bits)
2475
1.76k
              {
2476
                /* Scale down */
2477
6.16k
                for (x = number_pixels; x != 0; --x)
2478
4.40k
                  {
2479
4.40k
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2480
4.40k
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2481
4.40k
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2482
4.40k
                    SetOpacitySample(q,OpaqueOpacity);
2483
4.40k
                    q++;
2484
4.40k
                  }
2485
1.76k
              }
2486
154k
            break;
2487
0
          }
2488
3.61M
        }
2489
3.61M
    }
2490
21.0k
  else if (sample_type == FloatQuantumSampleType)
2491
21.0k
    {
2492
21.0k
      switch (quantum_size)
2493
21.0k
        {
2494
1.74k
        case 16:
2495
1.74k
          {
2496
13.6k
            for (x = number_pixels; x != 0; --x)
2497
11.8k
              {
2498
11.8k
                ImportFloat16Quantum(endian,double_value,p);
2499
11.8k
                double_value -= double_minvalue;
2500
11.8k
                double_value *= double_scale;
2501
11.8k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2502
11.8k
                ImportFloat16Quantum(endian,double_value,p);
2503
11.8k
                double_value -= double_minvalue;
2504
11.8k
                double_value *= double_scale;
2505
11.8k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2506
11.8k
                ImportFloat16Quantum(endian,double_value,p);
2507
11.8k
                double_value -= double_minvalue;
2508
11.8k
                double_value *= double_scale;
2509
11.8k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2510
11.8k
                SetOpacitySample(q,OpaqueOpacity);
2511
11.8k
                q++;
2512
11.8k
              }
2513
1.74k
            break;
2514
0
          }
2515
2.57k
        case 24:
2516
2.57k
          {
2517
27.4k
            for (x = number_pixels; x != 0; --x)
2518
24.9k
              {
2519
24.9k
                ImportFloat24Quantum(endian,double_value,p);
2520
24.9k
                double_value -= double_minvalue;
2521
24.9k
                double_value *= double_scale;
2522
24.9k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2523
24.9k
                ImportFloat24Quantum(endian,double_value,p);
2524
24.9k
                double_value -= double_minvalue;
2525
24.9k
                double_value *= double_scale;
2526
24.9k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2527
24.9k
                ImportFloat24Quantum(endian,double_value,p);
2528
24.9k
                double_value -= double_minvalue;
2529
24.9k
                double_value *= double_scale;
2530
24.9k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2531
24.9k
                SetOpacitySample(q,OpaqueOpacity);
2532
24.9k
                q++;
2533
24.9k
              }
2534
2.57k
            break;
2535
0
          }
2536
13.0k
        case 32:
2537
13.0k
          {
2538
1.39M
            for (x = number_pixels; x != 0; --x)
2539
1.38M
              {
2540
1.38M
                ImportFloat32Quantum(endian,double_value,p);
2541
1.38M
                double_value -= double_minvalue;
2542
1.38M
                double_value *= double_scale;
2543
1.38M
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2544
1.38M
                ImportFloat32Quantum(endian,double_value,p);
2545
1.38M
                double_value -= double_minvalue;
2546
1.38M
                double_value *= double_scale;
2547
1.38M
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2548
1.38M
                ImportFloat32Quantum(endian,double_value,p);
2549
1.38M
                double_value -= double_minvalue;
2550
1.38M
                double_value *= double_scale;
2551
1.38M
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2552
1.38M
                SetOpacitySample(q,OpaqueOpacity);
2553
1.38M
                q++;
2554
1.38M
              }
2555
13.0k
            break;
2556
0
          }
2557
3.70k
        case 64:
2558
3.70k
          {
2559
55.0k
            for (x = number_pixels; x != 0; --x)
2560
51.2k
              {
2561
51.2k
                ImportFloat64Quantum(endian,double_value,p);
2562
51.2k
                double_value -= double_minvalue;
2563
51.2k
                double_value *= double_scale;
2564
51.2k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2565
51.2k
                ImportFloat64Quantum(endian,double_value,p);
2566
51.2k
                double_value -= double_minvalue;
2567
51.2k
                double_value *= double_scale;
2568
51.2k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2569
51.2k
                ImportFloat64Quantum(endian,double_value,p);
2570
51.2k
                double_value -= double_minvalue;
2571
51.2k
                double_value *= double_scale;
2572
51.2k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2573
51.2k
                SetOpacitySample(q,OpaqueOpacity);
2574
51.2k
                q++;
2575
51.2k
              }
2576
3.70k
            break;
2577
0
          }
2578
0
        default:
2579
0
          break;
2580
21.0k
        }
2581
21.0k
    }
2582
2583
3.63M
  if (import_info)
2584
2.13M
    {
2585
2.13M
      import_info->bytes_imported=p-source;
2586
2.13M
    }
2587
2588
3.63M
  return MagickPass;
2589
3.63M
}
2590
2591
static MagickPassFail
2592
ImportRGBAQuantumType(const unsigned char *source,
2593
                      PixelPacket* restrict q,
2594
                      const unsigned long number_pixels,
2595
                      const unsigned int quantum_size,
2596
                      const QuantumSampleType sample_type,
2597
                      const unsigned int unsigned_scale,
2598
                      const double double_minvalue,
2599
                      const double double_scale,
2600
                      const EndianType endian,
2601
                      ImportPixelAreaInfo *import_info)
2602
212k
{
2603
212k
  const unsigned char * restrict
2604
212k
    p;
2605
2606
212k
  register unsigned long
2607
212k
    x;
2608
2609
212k
  double
2610
212k
    double_value;
2611
2612
212k
  register magick_uint32_t
2613
212k
    unsigned_value;
2614
2615
212k
  unsigned int
2616
212k
    sample_bits;
2617
2618
212k
  sample_bits=quantum_size;
2619
212k
  p=source;
2620
2621
212k
  if (sample_type == UnsignedQuantumSampleType)
2622
196k
    {
2623
196k
      switch (quantum_size)
2624
196k
        {
2625
96.7k
        case 8:
2626
96.7k
          {
2627
12.8M
            for (x = number_pixels; x != 0; --x)
2628
12.7M
              {
2629
12.7M
                SetRedSample(q,ScaleCharToQuantum(*p++));
2630
12.7M
                SetGreenSample(q,ScaleCharToQuantum(*p++));
2631
12.7M
                SetBlueSample(q,ScaleCharToQuantum(*p++));
2632
12.7M
                SetOpacitySample(q,MaxRGB-ScaleCharToQuantum(*p++));
2633
12.7M
                q++;
2634
12.7M
              }
2635
96.7k
            break;
2636
0
          }
2637
18.0k
        case 16:
2638
18.0k
          {
2639
2.37M
            for (x = number_pixels; x != 0; --x)
2640
2.35M
              {
2641
2.35M
                ImportUInt16Quantum(endian,unsigned_value,p);
2642
2.35M
                SetRedSample(q,ScaleShortToQuantum(unsigned_value));
2643
2.35M
                ImportUInt16Quantum(endian,unsigned_value,p);
2644
2.35M
                SetGreenSample(q,ScaleShortToQuantum(unsigned_value));
2645
2.35M
                ImportUInt16Quantum(endian,unsigned_value,p);
2646
2.35M
                SetBlueSample(q,ScaleShortToQuantum(unsigned_value));
2647
2.35M
                ImportUInt16Quantum(endian,unsigned_value,p);
2648
2.35M
                SetOpacitySample(q,MaxRGB-ScaleShortToQuantum(unsigned_value));
2649
2.35M
                q++;
2650
2.35M
              }
2651
18.0k
            break;
2652
0
          }
2653
20.5k
        case 32:
2654
20.5k
          {
2655
110k
            for (x = number_pixels; x != 0; --x)
2656
89.7k
              {
2657
89.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
2658
89.7k
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
2659
89.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
2660
89.7k
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
2661
89.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
2662
89.7k
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
2663
89.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
2664
89.7k
                SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
2665
89.7k
                q++;
2666
89.7k
              }
2667
20.5k
            break;
2668
0
          }
2669
0
        case 64:
2670
0
          {
2671
0
            for (x = number_pixels; x != 0; --x)
2672
0
              {
2673
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2674
0
                SetRedSample(q,ScaleLongToQuantum(unsigned_value));
2675
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2676
0
                SetGreenSample(q,ScaleLongToQuantum(unsigned_value));
2677
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2678
0
                SetBlueSample(q,ScaleLongToQuantum(unsigned_value));
2679
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2680
0
                SetOpacitySample(q,MaxRGB-ScaleLongToQuantum(unsigned_value));
2681
0
                q++;
2682
0
              }
2683
0
            break;
2684
0
          }
2685
60.8k
        default:
2686
60.8k
          {
2687
            /*
2688
              Arbitrary sample size
2689
            */
2690
60.8k
            BitStreamReadHandle
2691
60.8k
              stream;
2692
2693
60.8k
            MagickBitStreamInitializeRead(&stream,p);
2694
60.8k
            if (QuantumDepth >=  sample_bits)
2695
59.3k
              {
2696
                /* Scale up */
2697
14.2M
                for (x = number_pixels; x != 0; --x)
2698
14.1M
                  {
2699
14.1M
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2700
14.1M
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2701
14.1M
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2702
14.1M
                    SetOpacitySample(q,MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2703
14.1M
                    q++;
2704
14.1M
                  }
2705
59.3k
              }
2706
1.41k
            else if (QuantumDepth <  sample_bits)
2707
1.41k
              {
2708
                /* Scale down */
2709
33.1k
                for (x = number_pixels; x != 0; --x)
2710
31.7k
                  {
2711
31.7k
                    SetRedSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2712
31.7k
                    SetGreenSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2713
31.7k
                    SetBlueSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2714
31.7k
                    SetOpacitySample(q,MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2715
31.7k
                    q++;
2716
31.7k
                  }
2717
1.41k
              }
2718
60.8k
            break;
2719
0
          }
2720
196k
        }
2721
196k
    }
2722
16.3k
  else if (sample_type == FloatQuantumSampleType)
2723
16.3k
    {
2724
16.3k
      switch (quantum_size)
2725
16.3k
        {
2726
2.37k
        case 16:
2727
2.37k
          {
2728
18.6k
            for (x = number_pixels; x != 0; --x)
2729
16.2k
              {
2730
16.2k
                ImportFloat16Quantum(endian,double_value,p);
2731
16.2k
                double_value -= double_minvalue;
2732
16.2k
                double_value *= double_scale;
2733
16.2k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2734
16.2k
                ImportFloat16Quantum(endian,double_value,p);
2735
16.2k
                double_value -= double_minvalue;
2736
16.2k
                double_value *= double_scale;
2737
16.2k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2738
16.2k
                ImportFloat16Quantum(endian,double_value,p);
2739
16.2k
                double_value -= double_minvalue;
2740
16.2k
                double_value *= double_scale;
2741
16.2k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2742
16.2k
                ImportFloat16Quantum(endian,double_value,p);
2743
16.2k
                double_value -= double_minvalue;
2744
16.2k
                double_value *= double_scale;
2745
16.2k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2746
16.2k
                q++;
2747
16.2k
              }
2748
2.37k
            break;
2749
0
          }
2750
3.42k
        case 24:
2751
3.42k
          {
2752
15.0k
            for (x = number_pixels; x != 0; --x)
2753
11.6k
              {
2754
11.6k
                ImportFloat24Quantum(endian,double_value,p);
2755
11.6k
                double_value -= double_minvalue;
2756
11.6k
                double_value *= double_scale;
2757
11.6k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2758
11.6k
                ImportFloat24Quantum(endian,double_value,p);
2759
11.6k
                double_value -= double_minvalue;
2760
11.6k
                double_value *= double_scale;
2761
11.6k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2762
11.6k
                ImportFloat24Quantum(endian,double_value,p);
2763
11.6k
                double_value -= double_minvalue;
2764
11.6k
                double_value *= double_scale;
2765
11.6k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2766
11.6k
                ImportFloat24Quantum(endian,double_value,p);
2767
11.6k
                double_value -= double_minvalue;
2768
11.6k
                double_value *= double_scale;
2769
11.6k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2770
11.6k
                q++;
2771
11.6k
              }
2772
3.42k
            break;
2773
0
          }
2774
5.73k
        case 32:
2775
5.73k
          {
2776
26.0k
            for (x = number_pixels; x != 0; --x)
2777
20.3k
              {
2778
20.3k
                ImportFloat32Quantum(endian,double_value,p);
2779
20.3k
                double_value -= double_minvalue;
2780
20.3k
                double_value *= double_scale;
2781
20.3k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2782
20.3k
                ImportFloat32Quantum(endian,double_value,p);
2783
20.3k
                double_value -= double_minvalue;
2784
20.3k
                double_value *= double_scale;
2785
20.3k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2786
20.3k
                ImportFloat32Quantum(endian,double_value,p);
2787
20.3k
                double_value -= double_minvalue;
2788
20.3k
                double_value *= double_scale;
2789
20.3k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2790
20.3k
                ImportFloat32Quantum(endian,double_value,p);
2791
20.3k
                double_value -= double_minvalue;
2792
20.3k
                double_value *= double_scale;
2793
20.3k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2794
20.3k
                q++;
2795
20.3k
              }
2796
5.73k
            break;
2797
0
          }
2798
4.84k
        case 64:
2799
4.84k
          {
2800
17.3k
            for (x = number_pixels; x != 0; --x)
2801
12.5k
              {
2802
12.5k
                ImportFloat64Quantum(endian,double_value,p);
2803
12.5k
                double_value -= double_minvalue;
2804
12.5k
                double_value *= double_scale;
2805
12.5k
                SetRedSample(q,RoundDoubleToQuantumN(double_value));
2806
12.5k
                ImportFloat64Quantum(endian,double_value,p);
2807
12.5k
                double_value -= double_minvalue;
2808
12.5k
                double_value *= double_scale;
2809
12.5k
                SetGreenSample(q,RoundDoubleToQuantumN(double_value));
2810
12.5k
                ImportFloat64Quantum(endian,double_value,p);
2811
12.5k
                double_value -= double_minvalue;
2812
12.5k
                double_value *= double_scale;
2813
12.5k
                SetBlueSample(q,RoundDoubleToQuantumN(double_value));
2814
12.5k
                ImportFloat64Quantum(endian,double_value,p);
2815
12.5k
                double_value -= double_minvalue;
2816
12.5k
                double_value *= double_scale;
2817
12.5k
                SetOpacitySample(q,MaxRGB-RoundDoubleToQuantumN(double_value));
2818
12.5k
                q++;
2819
12.5k
              }
2820
4.84k
            break;
2821
0
          }
2822
0
        default:
2823
0
          break;
2824
16.3k
        }
2825
16.3k
    }
2826
2827
212k
  if (import_info)
2828
99.6k
    {
2829
99.6k
      import_info->bytes_imported=p-source;
2830
99.6k
    }
2831
2832
212k
  return MagickPass;
2833
212k
}
2834
2835
static MagickPassFail
2836
ImportCMYKQuantumType(const unsigned char *source,
2837
                      PixelPacket* restrict q,
2838
                      const unsigned long number_pixels,
2839
                      const unsigned int quantum_size,
2840
                      const QuantumSampleType sample_type,
2841
                      const unsigned int unsigned_scale,
2842
                      const double double_minvalue,
2843
                      const double double_scale,
2844
                      const EndianType endian,
2845
                      ImportPixelAreaInfo *import_info)
2846
140k
{
2847
140k
  const unsigned char * restrict
2848
140k
    p;
2849
2850
140k
  register unsigned long
2851
140k
    x;
2852
2853
140k
  double
2854
140k
    double_value;
2855
2856
140k
  register magick_uint32_t
2857
140k
    unsigned_value;
2858
2859
140k
  unsigned int
2860
140k
    sample_bits;
2861
2862
140k
  sample_bits=quantum_size;
2863
140k
  p=source;
2864
2865
140k
  if (sample_type == UnsignedQuantumSampleType)
2866
121k
    {
2867
121k
      switch (quantum_size)
2868
121k
        {
2869
19.4k
        case 8:
2870
19.4k
          {
2871
3.80M
            for (x = number_pixels; x != 0; --x)
2872
3.78M
              {
2873
3.78M
                SetCyanSample(q,ScaleCharToQuantum(*p++));
2874
3.78M
                SetMagentaSample(q,ScaleCharToQuantum(*p++));
2875
3.78M
                SetYellowSample(q,ScaleCharToQuantum(*p++));
2876
3.78M
                SetBlackSample(q,ScaleCharToQuantum(*p++));
2877
3.78M
                q++;
2878
3.78M
              }
2879
19.4k
            break;
2880
0
          }
2881
54.3k
        case 16:
2882
54.3k
          {
2883
51.0M
            for (x = number_pixels; x != 0; --x)
2884
50.9M
              {
2885
50.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2886
50.9M
                SetCyanSample(q,ScaleShortToQuantum(unsigned_value));
2887
50.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2888
50.9M
                SetMagentaSample(q,ScaleShortToQuantum(unsigned_value));
2889
50.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2890
50.9M
                SetYellowSample(q,ScaleShortToQuantum(unsigned_value));
2891
50.9M
                ImportUInt16Quantum(endian,unsigned_value,p);
2892
50.9M
                SetBlackSample(q,unsigned_value);
2893
50.9M
                q++;
2894
50.9M
              }
2895
54.3k
            break;
2896
0
          }
2897
5.12k
        case 32:
2898
5.12k
          {
2899
447k
            for (x = number_pixels; x != 0; --x)
2900
442k
              {
2901
442k
                ImportUInt32Quantum(endian,unsigned_value,p);
2902
442k
                SetCyanSample(q,ScaleLongToQuantum(unsigned_value));
2903
442k
                ImportUInt32Quantum(endian,unsigned_value,p);
2904
442k
                SetMagentaSample(q,ScaleLongToQuantum(unsigned_value));
2905
442k
                ImportUInt32Quantum(endian,unsigned_value,p);
2906
442k
                SetYellowSample(q,ScaleLongToQuantum(unsigned_value));
2907
442k
                ImportUInt32Quantum(endian,unsigned_value,p);
2908
442k
                SetBlackSample(q,unsigned_value);
2909
442k
                q++;
2910
442k
              }
2911
5.12k
            break;
2912
0
          }
2913
0
        case 64:
2914
0
          {
2915
0
            for (x = number_pixels; x != 0; --x)
2916
0
              {
2917
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2918
0
                SetCyanSample(q,ScaleLongToQuantum(unsigned_value));
2919
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2920
0
                SetMagentaSample(q,ScaleLongToQuantum(unsigned_value));
2921
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2922
0
                SetYellowSample(q,ScaleLongToQuantum(unsigned_value));
2923
0
                ImportUInt64Quantum(endian,unsigned_value,p);
2924
0
                SetBlackSample(q,unsigned_value);
2925
0
                q++;
2926
0
              }
2927
0
            break;
2928
0
          }
2929
42.8k
        default:
2930
42.8k
          {
2931
            /*
2932
              Arbitrary sample size
2933
            */
2934
42.8k
            BitStreamReadHandle
2935
42.8k
              stream;
2936
2937
42.8k
            MagickBitStreamInitializeRead(&stream,p);
2938
42.8k
            if (QuantumDepth >=  sample_bits)
2939
39.7k
              {
2940
                /* Scale up */
2941
26.9M
                for (x = number_pixels; x != 0; --x)
2942
26.9M
                  {
2943
26.9M
                    SetCyanSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2944
26.9M
                    SetMagentaSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2945
26.9M
                    SetYellowSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2946
26.9M
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
2947
26.9M
                    q++;
2948
26.9M
                  }
2949
39.7k
              }
2950
3.05k
            else if (QuantumDepth <  sample_bits)
2951
3.05k
              {
2952
                /* Scale down */
2953
2.10M
                for (x = number_pixels; x != 0; --x)
2954
2.10M
                  {
2955
2.10M
                    SetCyanSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2956
2.10M
                    SetMagentaSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2957
2.10M
                    SetYellowSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2958
2.10M
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
2959
2.10M
                    q++;
2960
2.10M
                  }
2961
3.05k
              }
2962
42.8k
            break;
2963
0
          }
2964
121k
        }
2965
121k
    }
2966
19.0k
  else if (sample_type == FloatQuantumSampleType)
2967
19.0k
    {
2968
19.0k
      switch (quantum_size)
2969
19.0k
        {
2970
7.20k
        case 16:
2971
7.20k
          {
2972
4.20M
            for (x = number_pixels; x != 0; --x)
2973
4.19M
              {
2974
4.19M
                ImportFloat16Quantum(endian,double_value,p);
2975
4.19M
                double_value -= double_minvalue;
2976
4.19M
                double_value *= double_scale;
2977
4.19M
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
2978
4.19M
                ImportFloat16Quantum(endian,double_value,p);
2979
4.19M
                double_value -= double_minvalue;
2980
4.19M
                double_value *= double_scale;
2981
4.19M
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
2982
4.19M
                ImportFloat16Quantum(endian,double_value,p);
2983
4.19M
                double_value -= double_minvalue;
2984
4.19M
                double_value *= double_scale;
2985
4.19M
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
2986
4.19M
                ImportFloat16Quantum(endian,double_value,p);
2987
4.19M
                double_value -= double_minvalue;
2988
4.19M
                double_value *= double_scale;
2989
4.19M
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
2990
4.19M
                q++;
2991
4.19M
              }
2992
7.20k
            break;
2993
0
          }
2994
2.69k
        case 24:
2995
2.69k
          {
2996
205k
            for (x = number_pixels; x != 0; --x)
2997
202k
              {
2998
202k
                ImportFloat24Quantum(endian,double_value,p);
2999
202k
                double_value -= double_minvalue;
3000
202k
                double_value *= double_scale;
3001
202k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3002
202k
                ImportFloat24Quantum(endian,double_value,p);
3003
202k
                double_value -= double_minvalue;
3004
202k
                double_value *= double_scale;
3005
202k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3006
202k
                ImportFloat24Quantum(endian,double_value,p);
3007
202k
                double_value -= double_minvalue;
3008
202k
                double_value *= double_scale;
3009
202k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3010
202k
                ImportFloat24Quantum(endian,double_value,p);
3011
202k
                double_value -= double_minvalue;
3012
202k
                double_value *= double_scale;
3013
202k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3014
202k
                q++;
3015
202k
              }
3016
2.69k
            break;
3017
0
          }
3018
4.51k
        case 32:
3019
4.51k
          {
3020
33.4k
            for (x = number_pixels; x != 0; --x)
3021
28.9k
              {
3022
28.9k
                ImportFloat32Quantum(endian,double_value,p);
3023
28.9k
                double_value -= double_minvalue;
3024
28.9k
                double_value *= double_scale;
3025
28.9k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3026
28.9k
                ImportFloat32Quantum(endian,double_value,p);
3027
28.9k
                double_value -= double_minvalue;
3028
28.9k
                double_value *= double_scale;
3029
28.9k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3030
28.9k
                ImportFloat32Quantum(endian,double_value,p);
3031
28.9k
                double_value -= double_minvalue;
3032
28.9k
                double_value *= double_scale;
3033
28.9k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3034
28.9k
                ImportFloat32Quantum(endian,double_value,p);
3035
28.9k
                double_value -= double_minvalue;
3036
28.9k
                double_value *= double_scale;
3037
28.9k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3038
28.9k
                q++;
3039
28.9k
              }
3040
4.51k
            break;
3041
0
          }
3042
4.61k
        case 64:
3043
4.61k
          {
3044
102k
            for (x = number_pixels; x != 0; --x)
3045
98.0k
              {
3046
98.0k
                ImportFloat64Quantum(endian,double_value,p);
3047
98.0k
                double_value -= double_minvalue;
3048
98.0k
                double_value *= double_scale;
3049
98.0k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3050
98.0k
                ImportFloat64Quantum(endian,double_value,p);
3051
98.0k
                double_value -= double_minvalue;
3052
98.0k
                double_value *= double_scale;
3053
98.0k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3054
98.0k
                ImportFloat64Quantum(endian,double_value,p);
3055
98.0k
                double_value -= double_minvalue;
3056
98.0k
                double_value *= double_scale;
3057
98.0k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3058
98.0k
                ImportFloat64Quantum(endian,double_value,p);
3059
98.0k
                double_value -= double_minvalue;
3060
98.0k
                double_value *= double_scale;
3061
98.0k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3062
98.0k
                q++;
3063
98.0k
              }
3064
4.61k
            break;
3065
0
          }
3066
0
        default:
3067
0
          break;
3068
19.0k
        }
3069
19.0k
    }
3070
3071
140k
  if (import_info)
3072
67.2k
    {
3073
67.2k
      import_info->bytes_imported=p-source;
3074
67.2k
    }
3075
3076
140k
  return MagickPass;
3077
140k
}
3078
3079
static MagickPassFail
3080
ImportCMYKAQuantumType(const unsigned char *source,
3081
                       PixelPacket* restrict q,
3082
                       IndexPacket * restrict indexes,
3083
                       const unsigned long number_pixels,
3084
                       const unsigned int quantum_size,
3085
                       const QuantumSampleType sample_type,
3086
                       const unsigned int unsigned_scale,
3087
                       const double double_minvalue,
3088
                       const double double_scale,
3089
                       const EndianType endian,
3090
                       Image *image,
3091
                       ImportPixelAreaInfo *import_info)
3092
38.2k
{
3093
38.2k
  const unsigned char * restrict
3094
38.2k
    p;
3095
3096
38.2k
  register unsigned long
3097
38.2k
    x;
3098
3099
38.2k
  double
3100
38.2k
    double_value;
3101
3102
38.2k
  register magick_uint32_t
3103
38.2k
    unsigned_value;
3104
3105
38.2k
  unsigned int
3106
38.2k
    sample_bits;
3107
3108
38.2k
  sample_bits=quantum_size;
3109
38.2k
  p=source;
3110
3111
38.2k
  if (indexes == (IndexPacket *) NULL)
3112
0
    ThrowBinaryException3(ImageError,CMYKAImageLacksAlphaChannel,
3113
38.2k
                          UnableToImportImagePixels);
3114
3115
38.2k
  if (sample_type == UnsignedQuantumSampleType)
3116
22.0k
    {
3117
22.0k
      switch (quantum_size)
3118
22.0k
        {
3119
5.22k
        case 8:
3120
5.22k
          {
3121
995k
            for (x = number_pixels; x != 0; --x)
3122
990k
              {
3123
990k
                SetCyanSample(q,ScaleCharToQuantum(*p++));
3124
990k
                SetMagentaSample(q,ScaleCharToQuantum(*p++));
3125
990k
                SetYellowSample(q,ScaleCharToQuantum(*p++));
3126
990k
                SetBlackSample(q,ScaleCharToQuantum(*p++));
3127
990k
                *indexes++=(IndexPacket) MaxRGB-ScaleCharToQuantum(*p++);
3128
990k
                q++;
3129
990k
              }
3130
5.22k
            break;
3131
0
          }
3132
3.51k
        case 16:
3133
3.51k
          {
3134
84.6k
            for (x = number_pixels; x != 0; --x)
3135
81.1k
              {
3136
81.1k
                ImportUInt16Quantum(endian,unsigned_value,p);
3137
81.1k
                SetCyanSample(q,ScaleShortToQuantum(unsigned_value));
3138
81.1k
                ImportUInt16Quantum(endian,unsigned_value,p);
3139
81.1k
                SetMagentaSample(q,ScaleShortToQuantum(unsigned_value));
3140
81.1k
                ImportUInt16Quantum(endian,unsigned_value,p);
3141
81.1k
                SetYellowSample(q,ScaleShortToQuantum(unsigned_value));
3142
81.1k
                ImportUInt16Quantum(endian,unsigned_value,p);
3143
81.1k
                SetBlackSample(q,ScaleShortToQuantum(unsigned_value));
3144
81.1k
                ImportUInt16Quantum(endian,unsigned_value,p);
3145
81.1k
                *indexes++=(IndexPacket) MaxRGB-ScaleShortToQuantum(unsigned_value);
3146
81.1k
                q++;
3147
81.1k
              }
3148
3.51k
            break;
3149
0
          }
3150
2.46k
        case 32:
3151
2.46k
          {
3152
37.2k
            for (x = number_pixels; x != 0; --x)
3153
34.7k
              {
3154
34.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
3155
34.7k
                SetCyanSample(q,ScaleLongToQuantum(unsigned_value));
3156
34.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
3157
34.7k
                SetMagentaSample(q,ScaleLongToQuantum(unsigned_value));
3158
34.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
3159
34.7k
                SetYellowSample(q,ScaleLongToQuantum(unsigned_value));
3160
34.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
3161
34.7k
                SetBlackSample(q,ScaleLongToQuantum(unsigned_value));
3162
34.7k
                ImportUInt32Quantum(endian,unsigned_value,p);
3163
34.7k
                *indexes++=(IndexPacket) MaxRGB-ScaleLongToQuantum(unsigned_value);
3164
34.7k
                q++;
3165
34.7k
              }
3166
2.46k
            break;
3167
0
          }
3168
0
        case 64:
3169
0
          {
3170
0
            for (x = number_pixels; x != 0; --x)
3171
0
              {
3172
0
                ImportUInt64Quantum(endian,unsigned_value,p);
3173
0
                SetCyanSample(q,ScaleLongToQuantum(unsigned_value));
3174
0
                ImportUInt64Quantum(endian,unsigned_value,p);
3175
0
                SetMagentaSample(q,ScaleLongToQuantum(unsigned_value));
3176
0
                ImportUInt64Quantum(endian,unsigned_value,p);
3177
0
                SetYellowSample(q,ScaleLongToQuantum(unsigned_value));
3178
0
                ImportUInt64Quantum(endian,unsigned_value,p);
3179
0
                SetBlackSample(q,ScaleLongToQuantum(unsigned_value));
3180
0
                ImportUInt64Quantum(endian,unsigned_value,p);
3181
0
                *indexes++=(IndexPacket) MaxRGB-ScaleLongToQuantum(unsigned_value);
3182
0
                q++;
3183
0
              }
3184
0
            break;
3185
0
          }
3186
10.8k
        default:
3187
10.8k
          {
3188
            /*
3189
              Arbitrary sample size
3190
            */
3191
10.8k
            BitStreamReadHandle
3192
10.8k
              stream;
3193
3194
10.8k
            MagickBitStreamInitializeRead(&stream,p);
3195
10.8k
            if (QuantumDepth >=  sample_bits)
3196
9.96k
              {
3197
                /* Scale up */
3198
899k
                for (x = number_pixels; x != 0; --x)
3199
889k
                  {
3200
889k
                    SetCyanSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
3201
889k
                    SetMagentaSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
3202
889k
                    SetYellowSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
3203
889k
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale);
3204
889k
                    *indexes++=(IndexPacket) MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)*unsigned_scale;
3205
889k
                    q++;
3206
889k
                  }
3207
9.96k
              }
3208
861
            else if (QuantumDepth <  sample_bits)
3209
861
              {
3210
                /* Scale down */
3211
4.88k
                for (x = number_pixels; x != 0; --x)
3212
4.02k
                  {
3213
4.02k
                    SetCyanSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
3214
4.02k
                    SetMagentaSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
3215
4.02k
                    SetYellowSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
3216
4.02k
                    SetBlackSample(q,MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale);
3217
4.02k
                    *indexes++=(IndexPacket) MaxRGB-MagickBitStreamMSBRead(&stream,quantum_size)/unsigned_scale;
3218
4.02k
                    q++;
3219
4.02k
                  }
3220
861
              }
3221
10.8k
            break;
3222
0
          }
3223
22.0k
        }
3224
22.0k
    }
3225
16.1k
  else if (sample_type == FloatQuantumSampleType)
3226
16.1k
    {
3227
16.1k
      switch (quantum_size)
3228
16.1k
        {
3229
1.96k
        case 16:
3230
1.96k
          {
3231
23.5k
            for (x = number_pixels; x != 0; --x)
3232
21.6k
              {
3233
21.6k
                ImportFloat16Quantum(endian,double_value,p);
3234
21.6k
                double_value -= double_minvalue;
3235
21.6k
                double_value *= double_scale;
3236
21.6k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3237
21.6k
                ImportFloat16Quantum(endian,double_value,p);
3238
21.6k
                double_value -= double_minvalue;
3239
21.6k
                double_value *= double_scale;
3240
21.6k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3241
21.6k
                ImportFloat16Quantum(endian,double_value,p);
3242
21.6k
                double_value -= double_minvalue;
3243
21.6k
                double_value *= double_scale;
3244
21.6k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3245
21.6k
                ImportFloat16Quantum(endian,double_value,p);
3246
21.6k
                double_value -= double_minvalue;
3247
21.6k
                double_value *= double_scale;
3248
21.6k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3249
21.6k
                ImportFloat16Quantum(endian,double_value,p);
3250
21.6k
                double_value -= double_minvalue;
3251
21.6k
                double_value *= double_scale;
3252
21.6k
                *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
3253
21.6k
                q++;
3254
21.6k
              }
3255
1.96k
            break;
3256
0
          }
3257
3.03k
        case 24:
3258
3.03k
          {
3259
15.7k
            for (x = number_pixels; x != 0; --x)
3260
12.6k
              {
3261
12.6k
                ImportFloat24Quantum(endian,double_value,p);
3262
12.6k
                double_value -= double_minvalue;
3263
12.6k
                double_value *= double_scale;
3264
12.6k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3265
12.6k
                ImportFloat24Quantum(endian,double_value,p);
3266
12.6k
                double_value -= double_minvalue;
3267
12.6k
                double_value *= double_scale;
3268
12.6k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3269
12.6k
                ImportFloat24Quantum(endian,double_value,p);
3270
12.6k
                double_value -= double_minvalue;
3271
12.6k
                double_value *= double_scale;
3272
12.6k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3273
12.6k
                ImportFloat24Quantum(endian,double_value,p);
3274
12.6k
                double_value -= double_minvalue;
3275
12.6k
                double_value *= double_scale;
3276
12.6k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3277
12.6k
                ImportFloat24Quantum(endian,double_value,p);
3278
12.6k
                double_value -= double_minvalue;
3279
12.6k
                double_value *= double_scale;
3280
12.6k
                *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
3281
12.6k
                q++;
3282
12.6k
              }
3283
3.03k
            break;
3284
0
          }
3285
5.71k
        case 32:
3286
5.71k
          {
3287
28.1k
            for (x = number_pixels; x != 0; --x)
3288
22.3k
              {
3289
22.3k
                ImportFloat32Quantum(endian,double_value,p);
3290
22.3k
                double_value -= double_minvalue;
3291
22.3k
                double_value *= double_scale;
3292
22.3k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3293
22.3k
                ImportFloat32Quantum(endian,double_value,p);
3294
22.3k
                double_value -= double_minvalue;
3295
22.3k
                double_value *= double_scale;
3296
22.3k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3297
22.3k
                ImportFloat32Quantum(endian,double_value,p);
3298
22.3k
                double_value -= double_minvalue;
3299
22.3k
                double_value *= double_scale;
3300
22.3k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3301
22.3k
                ImportFloat32Quantum(endian,double_value,p);
3302
22.3k
                double_value -= double_minvalue;
3303
22.3k
                double_value *= double_scale;
3304
22.3k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3305
22.3k
                ImportFloat32Quantum(endian,double_value,p);
3306
22.3k
                double_value -= double_minvalue;
3307
22.3k
                double_value *= double_scale;
3308
22.3k
                *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
3309
22.3k
                q++;
3310
22.3k
              }
3311
5.71k
            break;
3312
0
          }
3313
5.48k
        case 64:
3314
5.48k
          {
3315
22.0k
            for (x = number_pixels; x != 0; --x)
3316
16.6k
              {
3317
16.6k
                ImportFloat64Quantum(endian,double_value,p);
3318
16.6k
                double_value -= double_minvalue;
3319
16.6k
                double_value *= double_scale;
3320
16.6k
                SetCyanSample(q,RoundDoubleToQuantumN(double_value));
3321
16.6k
                ImportFloat64Quantum(endian,double_value,p);
3322
16.6k
                double_value -= double_minvalue;
3323
16.6k
                double_value *= double_scale;
3324
16.6k
                SetMagentaSample(q,RoundDoubleToQuantumN(double_value));
3325
16.6k
                ImportFloat64Quantum(endian,double_value,p);
3326
16.6k
                double_value -= double_minvalue;
3327
16.6k
                double_value *= double_scale;
3328
16.6k
                SetYellowSample(q,RoundDoubleToQuantumN(double_value));
3329
16.6k
                ImportFloat64Quantum(endian,double_value,p);
3330
16.6k
                double_value -= double_minvalue;
3331
16.6k
                double_value *= double_scale;
3332
16.6k
                SetBlackSample(q,RoundDoubleToQuantumN(double_value));
3333
16.6k
                ImportFloat64Quantum(endian,double_value,p);
3334
16.6k
                double_value -= double_minvalue;
3335
16.6k
                double_value *= double_scale;
3336
16.6k
                *indexes++=(IndexPacket) MaxRGB-RoundDoubleToQuantumN(double_value);
3337
16.6k
                q++;
3338
16.6k
              }
3339
5.48k
            break;
3340
0
          }
3341
0
        default:
3342
0
          break;
3343
16.1k
        }
3344
16.1k
    }
3345
3346
38.2k
  if (import_info)
3347
30.4k
    {
3348
30.4k
      import_info->bytes_imported=p-source;
3349
30.4k
    }
3350
3351
38.2k
  return MagickPass;
3352
38.2k
}
3353
3354
static MagickPassFail
3355
ImportCIEXYZQuantumType(const unsigned char *source,
3356
                        PixelPacket* restrict q,
3357
                        const unsigned long number_pixels,
3358
                        const unsigned int quantum_size,
3359
                        const QuantumSampleType sample_type,
3360
                        const EndianType endian,
3361
                        ImportPixelAreaInfo *import_info)
3362
6.89k
{
3363
6.89k
  const unsigned char * restrict
3364
6.89k
    p;
3365
3366
6.89k
  register unsigned long
3367
6.89k
    x;
3368
3369
6.89k
  p=source;
3370
3371
6.89k
  if (sample_type == FloatQuantumSampleType)
3372
6.89k
    {
3373
6.89k
      double
3374
6.89k
        red,
3375
6.89k
        green,
3376
6.89k
        blue,
3377
6.89k
        x_sample,
3378
6.89k
        y_sample,
3379
6.89k
        z_sample;
3380
3381
96.6k
      for (x = number_pixels; x != 0; --x)
3382
89.7k
        {
3383
89.7k
          switch (quantum_size)
3384
89.7k
            {
3385
0
            default:
3386
89.7k
            case 32:
3387
89.7k
              {
3388
89.7k
                ImportFloat32Quantum(endian,x_sample,p);
3389
89.7k
                ImportFloat32Quantum(endian,y_sample,p);
3390
89.7k
                ImportFloat32Quantum(endian,z_sample,p);
3391
89.7k
                break;
3392
0
              }
3393
0
            case 64:
3394
0
              {
3395
0
                ImportFloat64Quantum(endian,x_sample,p);
3396
0
                ImportFloat64Quantum(endian,y_sample,p);
3397
0
                ImportFloat64Quantum(endian,z_sample,p);
3398
0
                break;
3399
0
              }
3400
89.7k
            }
3401
3402
          /* Assume CCIR-709 primaries */
3403
89.7k
          red   = 2.690*x_sample  + -1.276*y_sample + -0.414*z_sample;
3404
89.7k
          green = -1.022*x_sample +  1.978*y_sample +  0.044*z_sample;
3405
89.7k
          blue  = 0.061*x_sample  + -0.224*y_sample +  1.163*z_sample;
3406
3407
          /* assume 2.0 gamma for speed */
3408
89.7k
          SetRedSample(q,(Quantum) ((red <= 0.0) ? 0.0 : (red >= 1.0) ? MaxRGB :
3409
89.7k
                                    ((MaxRGB * sqrt(red))+0.5)));
3410
89.7k
          SetGreenSample(q,(Quantum) ((green <= 0.0) ? 0.0 : (green >= 1.0) ? MaxRGB :
3411
89.7k
                                      ((MaxRGB * sqrt(green))+0.5)));
3412
89.7k
          SetBlueSample(q,(Quantum) ((blue <= 0.0) ? 0.0 : (blue >= 1.0) ? MaxRGB :
3413
89.7k
                                     ((MaxRGB * sqrt(blue))+0.5)));
3414
89.7k
          SetOpacitySample(q,OpaqueOpacity);
3415
89.7k
          q++;
3416
89.7k
        }
3417
6.89k
    }
3418
3419
6.89k
  if (import_info)
3420
2.64k
    {
3421
2.64k
      import_info->bytes_imported=p-source;
3422
2.64k
    }
3423
3424
6.89k
  return MagickPass;
3425
6.89k
}
3426
3427
static MagickPassFail
3428
ImportCIEYQuantumType(const unsigned char *source,
3429
                      PixelPacket* restrict q,
3430
                      const unsigned long number_pixels,
3431
                      const unsigned int quantum_size,
3432
                      const QuantumSampleType sample_type,
3433
                      const EndianType endian,
3434
                      ImportPixelAreaInfo *import_info)
3435
23.8k
{
3436
23.8k
  const unsigned char * restrict
3437
23.8k
    p;
3438
3439
23.8k
  register unsigned long
3440
23.8k
    x;
3441
3442
23.8k
  double
3443
23.8k
    double_value;
3444
3445
23.8k
  p=source;
3446
3447
23.8k
  if (sample_type == FloatQuantumSampleType)
3448
23.8k
    {
3449
552k
      for (x = number_pixels; x != 0; --x)
3450
528k
        {
3451
528k
          switch (quantum_size)
3452
528k
            {
3453
0
            default:
3454
528k
            case 32:
3455
528k
              {
3456
528k
                ImportFloat32Quantum(endian,double_value,p);
3457
528k
                break;
3458
0
              }
3459
0
            case 64:
3460
0
              {
3461
0
                ImportFloat64Quantum(endian,double_value,p);
3462
0
                break;
3463
0
              }
3464
528k
            }
3465
          /* assume 2.0 gamma for speed */
3466
528k
          SetGraySample(q,(Quantum) ((double_value <= 0.0) ? 0.0 :
3467
528k
                                     (double_value >= 1.0) ? MaxRGB :
3468
528k
                                     ((MaxRGB * sqrt(double_value))+0.5)));
3469
528k
          q++;
3470
528k
        }
3471
23.8k
    }
3472
3473
23.8k
  if (import_info)
3474
14.8k
    {
3475
14.8k
      import_info->bytes_imported=p-source;
3476
14.8k
    }
3477
3478
23.8k
  return MagickPass;
3479
23.8k
}
3480
3481
MagickExport MagickPassFail
3482
ImportViewPixelArea(ViewInfo *view,
3483
                    const QuantumType quantum_type,
3484
                    const unsigned int quantum_size,
3485
                    const unsigned char *source,
3486
                    const ImportPixelAreaOptions *options,
3487
                    ImportPixelAreaInfo *import_info)
3488
20.5M
{
3489
20.5M
  Image
3490
20.5M
    *image;
3491
3492
20.5M
  unsigned int
3493
20.5M
    unsigned_scale = 1U;
3494
3495
20.5M
  IndexPacket
3496
20.5M
    * restrict indexes;
3497
3498
20.5M
  PixelPacket
3499
20.5M
    * restrict q;
3500
3501
20.5M
  MagickBool
3502
20.5M
    grayscale_miniswhite = MagickFalse;
3503
3504
20.5M
  QuantumSampleType
3505
20.5M
    sample_type = UnsignedQuantumSampleType;
3506
3507
20.5M
  unsigned int
3508
20.5M
    unsigned_maxvalue=MaxRGB,
3509
20.5M
    sample_bits;
3510
3511
20.5M
  unsigned long
3512
20.5M
    number_pixels;
3513
3514
20.5M
  double
3515
20.5M
    double_maxvalue=1.0,
3516
20.5M
    double_minvalue=0.0,
3517
20.5M
    double_scale;
3518
3519
20.5M
  EndianType
3520
20.5M
    endian=MSBEndian;
3521
3522
20.5M
  MagickPassFail
3523
20.5M
    status=MagickPass;
3524
3525
20.5M
  assert(view != (ViewInfo *) NULL);
3526
20.5M
  assert(source != (const unsigned char *) NULL);
3527
20.5M
  assert((options == (const ImportPixelAreaOptions *) NULL) ||
3528
20.5M
         (options->signature == MagickSignature));
3529
3530
  /*
3531
    Transfer any special options.
3532
  */
3533
20.5M
  sample_bits=quantum_size;
3534
20.5M
  if (options)
3535
19.4M
    {
3536
19.4M
      sample_type=options->sample_type;
3537
19.4M
      double_minvalue=options->double_minvalue;
3538
19.4M
      double_maxvalue=options->double_maxvalue;
3539
19.4M
      grayscale_miniswhite=options->grayscale_miniswhite;
3540
3541
19.4M
      switch (options->endian)
3542
19.4M
        {
3543
18.4M
        case MSBEndian:
3544
18.4M
        case UndefinedEndian:
3545
18.4M
          {
3546
18.4M
            endian=MSBEndian;
3547
18.4M
            break;
3548
18.4M
          }
3549
64.3k
        case LSBEndian:
3550
64.3k
          {
3551
64.3k
            endian=LSBEndian;
3552
64.3k
            break;
3553
18.4M
          }
3554
866k
        case NativeEndian:
3555
866k
          {
3556
#if defined(WORDS_BIGENDIAN)
3557
            endian=MSBEndian;
3558
#else
3559
866k
            endian=LSBEndian;
3560
866k
#endif
3561
866k
            break;
3562
18.4M
          }
3563
19.4M
        }
3564
19.4M
    }
3565
3566
20.5M
  if (import_info)
3567
9.53M
    {
3568
9.53M
      import_info->bytes_imported=0;
3569
9.53M
    }
3570
3571
20.5M
  if (!(((sample_type == FloatQuantumSampleType) &&
3572
20.5M
         ((quantum_size == 16) || (quantum_size == 24) ||
3573
292k
          (quantum_size == 32) || (quantum_size == 64))) ||
3574
20.5M
        ((sample_type == UnsignedQuantumSampleType) &&
3575
20.2M
         (((quantum_size > 0) && (quantum_size <= 32)) ||
3576
20.2M
          (quantum_size == 64)))))
3577
6.80k
    {
3578
6.80k
      char quantum_size_str[MaxTextExtent];
3579
6.80k
      FormatString(quantum_size_str,"%u",quantum_size);
3580
6.80k
      status=0;
3581
6.80k
      ThrowException(&GetCacheViewImage(view)->exception,CoderError,
3582
6.80k
                     UnsupportedBitsPerSample,quantum_size_str);
3583
6.80k
      return MagickFail;
3584
6.80k
    }
3585
3586
3587
  /* printf("quantum_type=%d  quantum_size=%u\n",(int) quantum_type, quantum_size); */
3588
3589
20.5M
  {
3590
20.5M
    const double scale_denominator = double_maxvalue-double_minvalue;
3591
20.5M
    if (scale_denominator < MagickEpsilon)
3592
6.17k
        double_scale = 0.0;
3593
20.5M
    else
3594
20.5M
      double_scale=MaxRGBDouble/(scale_denominator);
3595
20.5M
  }
3596
20.5M
  if ((sample_type != FloatQuantumSampleType) && (sample_bits <= 32U))
3597
20.2M
    {
3598
      /* Maximum value which may be represented by a sample */
3599
20.2M
      unsigned_maxvalue=MaxValueGivenBits(sample_bits);
3600
3601
20.2M
      if (QuantumDepth == sample_bits)
3602
318k
        {
3603
318k
        }
3604
19.8M
      else if (QuantumDepth > sample_bits)
3605
19.4M
        {
3606
          /* Multiply to scale up */
3607
19.4M
          unsigned_scale=(MaxRGB / (MaxRGB >> (QuantumDepth-sample_bits)));
3608
19.4M
        }
3609
417k
#if QuantumDepth < 32
3610
417k
      else if (QuantumDepth < sample_bits)
3611
417k
        {
3612
          /* Divide to scale down */
3613
417k
          unsigned_scale=(unsigned_maxvalue/MaxRGB);
3614
417k
        }
3615
20.2M
#endif
3616
20.2M
    }
3617
3618
20.5M
  image=GetCacheViewImage(view);
3619
20.5M
  number_pixels=(long) GetCacheViewArea(view);
3620
20.5M
  q=AccessCacheViewPixels(view);
3621
20.5M
  indexes=GetCacheViewIndexes(view);
3622
20.5M
  switch (quantum_type)
3623
20.5M
    {
3624
0
    case UndefinedQuantum:
3625
0
      {
3626
0
        status=MagickFail;
3627
0
        break;
3628
0
      }
3629
220k
    case IndexQuantum:
3630
220k
      {
3631
220k
        status=ImportIndexQuantumType(source,q,indexes,number_pixels,quantum_size,
3632
220k
                                      sample_type,unsigned_maxvalue,endian,image,
3633
220k
                                      import_info);
3634
220k
        break;
3635
0
      }
3636
9.94k
    case IndexAlphaQuantum:
3637
9.94k
      {
3638
9.94k
        status=ImportIndexAlphaQuantumType(source,q,indexes,number_pixels,
3639
9.94k
                                           quantum_size,sample_type,unsigned_scale,
3640
9.94k
                                           endian,
3641
9.94k
                                           image,
3642
9.94k
                                           import_info);
3643
9.94k
        break;
3644
0
      }
3645
15.3M
    case GrayQuantum:
3646
15.3M
      {
3647
15.3M
        status=ImportGrayQuantumType(source,q,indexes,number_pixels,quantum_size,
3648
15.3M
                                     sample_type,unsigned_scale,unsigned_maxvalue,
3649
15.3M
                                     grayscale_miniswhite,double_minvalue,
3650
15.3M
                                     double_scale,endian,image,import_info);
3651
15.3M
        break;
3652
0
      }
3653
65.5k
    case GrayAlphaQuantum:
3654
65.5k
      {
3655
65.5k
        status=ImportGrayAlphaQuantumType(source,q,indexes,number_pixels,quantum_size,
3656
65.5k
                                          sample_type,unsigned_scale,unsigned_maxvalue,
3657
65.5k
                                          grayscale_miniswhite,double_minvalue,double_scale,
3658
65.5k
                                          endian,image,import_info);
3659
65.5k
        break;
3660
0
      }
3661
127k
    case RedQuantum:
3662
452k
    case CyanQuantum:
3663
452k
      {
3664
452k
        status=ImportRedQuantumType(source,q,number_pixels,quantum_size,sample_type,
3665
452k
                                    unsigned_scale,double_minvalue,double_scale,endian,
3666
452k
                                    import_info);
3667
3668
452k
        break;
3669
127k
      }
3670
73.7k
    case GreenQuantum:
3671
203k
    case MagentaQuantum:
3672
203k
      {
3673
203k
        status=ImportGreenQuantumType(source,q,number_pixels,quantum_size,sample_type,
3674
203k
                                      unsigned_scale,double_minvalue,double_scale,endian,
3675
203k
                                      import_info);
3676
203k
        break;
3677
73.7k
      }
3678
44.3k
    case BlueQuantum:
3679
123k
    case YellowQuantum:
3680
123k
      {
3681
123k
        status=ImportBlueQuantumType(source,q,number_pixels,quantum_size,sample_type,
3682
123k
                                     unsigned_scale,double_minvalue,double_scale,endian,
3683
123k
                                     import_info);
3684
123k
        break;
3685
44.3k
      }
3686
13.2k
    case AlphaQuantum:
3687
13.2k
      {
3688
13.2k
        status=ImportAlphaQuantumType(source,q,indexes,number_pixels,quantum_size,
3689
13.2k
                                      sample_type,unsigned_scale,double_minvalue,
3690
13.2k
                                      double_scale,endian,image,import_info);
3691
13.2k
        break;
3692
44.3k
      }
3693
50.1k
    case BlackQuantum:
3694
50.1k
      {
3695
50.1k
        status=ImportBlackQuantumType(source,q,number_pixels,quantum_size,sample_type,
3696
50.1k
                                      unsigned_scale,double_minvalue,double_scale,endian,
3697
50.1k
                                      import_info);
3698
50.1k
        break;
3699
44.3k
      }
3700
3.63M
    case RGBQuantum:
3701
3.63M
      {
3702
3.63M
        status=ImportRGBQuantumType(source,q,number_pixels,quantum_size,sample_type,
3703
3.63M
                                    unsigned_scale,double_minvalue,double_scale,endian,
3704
3.63M
                                    import_info);
3705
3.63M
        break;
3706
44.3k
      }
3707
212k
    case RGBAQuantum:
3708
212k
      {
3709
212k
        status=ImportRGBAQuantumType(source,q,number_pixels,quantum_size,sample_type,
3710
212k
                                     unsigned_scale,double_minvalue,double_scale,endian,
3711
212k
                                     import_info);
3712
212k
        break;
3713
44.3k
      }
3714
140k
    case CMYKQuantum:
3715
140k
      {
3716
140k
        status=ImportCMYKQuantumType(source,q,number_pixels,quantum_size,
3717
140k
                                     sample_type,unsigned_scale,double_minvalue,
3718
140k
                                     double_scale,endian,import_info);
3719
140k
        break;
3720
44.3k
      }
3721
38.2k
    case CMYKAQuantum:
3722
38.2k
      {
3723
38.2k
        status=ImportCMYKAQuantumType(source,q,indexes,number_pixels,quantum_size,
3724
38.2k
                                      sample_type,unsigned_scale,double_minvalue,
3725
38.2k
                                      double_scale,endian,image,import_info);
3726
38.2k
        break;
3727
44.3k
      }
3728
6.89k
    case CIEXYZQuantum:
3729
6.89k
      {
3730
6.89k
        status=ImportCIEXYZQuantumType(source,q,number_pixels,quantum_size,sample_type,
3731
6.89k
                                       endian,import_info);
3732
6.89k
        break;
3733
44.3k
      }
3734
23.8k
    case CIEYQuantum:
3735
23.8k
      {
3736
23.8k
        status=ImportCIEYQuantumType(source,q,number_pixels,quantum_size,sample_type,
3737
23.8k
                                     endian,import_info);
3738
23.8k
        break;
3739
44.3k
      }
3740
20.5M
    }
3741
3742
20.5M
  return(status);
3743
20.5M
}
3744

3745
/*
3746
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3747
%                                                                             %
3748
%                                                                             %
3749
%                                                                             %
3750
%   I m p o r t P i x e l A r e a O p t i o n s I n i t                       %
3751
%                                                                             %
3752
%                                                                             %
3753
%                                                                             %
3754
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3755
%
3756
%  ImportPixelAreaOptionsInit() initializes the options structure which is
3757
%  optionally passed to ImportPixelArea()
3758
%
3759
%  The format of the ImportPixelAreaOptionsInit method is:
3760
%
3761
%      void ImportPixelAreaOptionsInit(ImportPixelAreaOptions *options)
3762
%
3763
%  A description of each parameter follows:
3764
%
3765
%    o options: Options structure to initialize.
3766
%
3767
*/
3768
MagickExport void ImportPixelAreaOptionsInit(ImportPixelAreaOptions *options)
3769
96.0k
{
3770
96.0k
  assert(options != (ImportPixelAreaOptions *) NULL);
3771
96.0k
  (void) memset((void *) options, 0, sizeof(ImportPixelAreaOptions));
3772
96.0k
  options->sample_type=UnsignedQuantumSampleType;
3773
96.0k
  options->double_minvalue=0.0;
3774
96.0k
  options->double_maxvalue=1.0;
3775
96.0k
  options->grayscale_miniswhite=MagickFalse;
3776
96.0k
  options->endian=MSBEndian;
3777
96.0k
  options->signature=MagickSignature;
3778
96.0k
}