Coverage Report

Created: 2025-06-16 07:00

/src/imagemagick/coders/pnm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%                                                                             %
4
%                                                                             %
5
%                                                                             %
6
%                            PPPP   N   N  M   M                              %
7
%                            P   P  NN  N  MM MM                              %
8
%                            PPPP   N N N  M M M                              %
9
%                            P      N  NN  M   M                              %
10
%                            P      N   N  M   M                              %
11
%                                                                             %
12
%                                                                             %
13
%               Read/Write PBMPlus Portable Anymap Image Format               %
14
%                                                                             %
15
%                              Software Design                                %
16
%                                   Cristy                                    %
17
%                                 July 1992                                   %
18
%                                                                             %
19
%                                                                             %
20
%  Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization         %
21
%  dedicated to making software imaging solutions freely available.           %
22
%                                                                             %
23
%  You may not use this file except in compliance with the License.  You may  %
24
%  obtain a copy of the License at                                            %
25
%                                                                             %
26
%    https://imagemagick.org/script/license.php                               %
27
%                                                                             %
28
%  Unless required by applicable law or agreed to in writing, software        %
29
%  distributed under the License is distributed on an "AS IS" BASIS,          %
30
%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31
%  See the License for the specific language governing permissions and        %
32
%  limitations under the License.                                             %
33
%                                                                             %
34
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
%
36
%
37
*/
38

39
/*
40
  Include declarations.
41
*/
42
#include "MagickCore/studio.h"
43
#include "MagickCore/attribute.h"
44
#include "MagickCore/blob.h"
45
#include "MagickCore/blob-private.h"
46
#include "MagickCore/cache.h"
47
#include "MagickCore/color.h"
48
#include "MagickCore/color-private.h"
49
#include "MagickCore/colorspace.h"
50
#include "MagickCore/colorspace-private.h"
51
#include "MagickCore/exception.h"
52
#include "MagickCore/exception-private.h"
53
#include "MagickCore/image.h"
54
#include "MagickCore/image-private.h"
55
#include "MagickCore/list.h"
56
#include "MagickCore/magick.h"
57
#include "MagickCore/memory_.h"
58
#include "MagickCore/module.h"
59
#include "MagickCore/monitor.h"
60
#include "MagickCore/monitor-private.h"
61
#include "MagickCore/pixel-accessor.h"
62
#include "MagickCore/property.h"
63
#include "MagickCore/quantum-private.h"
64
#include "MagickCore/static.h"
65
#include "MagickCore/statistic.h"
66
#include "MagickCore/string_.h"
67
#include "MagickCore/string-private.h"
68
#include "coders/coders-private.h"
69

70
/*
71
  Typedef declarations.
72
*/
73
typedef struct _CommentInfo
74
{
75
  char
76
    *comment;
77
78
  size_t
79
    extent;
80
} CommentInfo;
81

82
/*
83
  Forward declarations.
84
*/
85
static MagickBooleanType
86
  WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
87

88
/*
89
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
%                                                                             %
91
%                                                                             %
92
%                                                                             %
93
%   I s P N M                                                                 %
94
%                                                                             %
95
%                                                                             %
96
%                                                                             %
97
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
%
99
%  IsPNM() returns MagickTrue if the image format type, identified by the
100
%  magick string, is PNM.
101
%
102
%  The format of the IsPNM method is:
103
%
104
%      MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
105
%
106
%  A description of each parameter follows:
107
%
108
%    o magick: compare image format pattern against these bytes.
109
%
110
%    o extent: Specifies the extent of the magick string.
111
%
112
*/
113
static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
114
0
{
115
0
  if (extent < 2)
116
0
    return(MagickFalse);
117
0
  if ((*magick == (unsigned char) 'P') &&
118
0
      ((magick[1] == '1') || (magick[1] == '2') || (magick[1] == '3') ||
119
0
       (magick[1] == '4') || (magick[1] == '5') || (magick[1] == '6') ||
120
0
       (magick[1] == '7') || (magick[1] == 'F') || (magick[1] == 'f') ||
121
0
       (magick[1] == 'H') || (magick[1] == 'h')))
122
0
    return(MagickTrue);
123
0
  return(MagickFalse);
124
0
}
125

126
/*
127
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128
%                                                                             %
129
%                                                                             %
130
%                                                                             %
131
%   R e a d P N M I m a g e                                                   %
132
%                                                                             %
133
%                                                                             %
134
%                                                                             %
135
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
%
137
%  ReadPNMImage() reads a Portable Anymap image file and returns it.
138
%  It allocates the memory necessary for the new Image structure and returns
139
%  a pointer to the new image.
140
%
141
%  The format of the ReadPNMImage method is:
142
%
143
%      Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
144
%
145
%  A description of each parameter follows:
146
%
147
%    o image_info: the image info.
148
%
149
%    o exception: return any errors or warnings in this structure.
150
%
151
*/
152
153
static int PNMComment(Image *image,CommentInfo *comment_info,
154
  ExceptionInfo *exception)
155
4.48k
{
156
4.48k
  int
157
4.48k
    c;
158
159
4.48k
  char
160
4.48k
    *p;
161
162
  /*
163
    Read comment.
164
  */
165
4.48k
  (void) exception;
166
4.48k
  p=comment_info->comment+strlen(comment_info->comment);
167
171k
  for (c='#'; (c != EOF) && (c != (int) '\n') && (c != (int) '\r'); p++)
168
167k
  {
169
167k
    if ((size_t) (p-comment_info->comment+1) >= comment_info->extent)
170
11
      {
171
11
        comment_info->extent<<=1;
172
11
        comment_info->comment=(char *) ResizeQuantumMemory(
173
11
          comment_info->comment,comment_info->extent,
174
11
          sizeof(*comment_info->comment));
175
11
        if (comment_info->comment == (char *) NULL)
176
0
          return(-1);
177
11
        p=comment_info->comment+strlen(comment_info->comment);
178
11
      }
179
167k
    c=ReadBlobByte(image);
180
167k
    if (c != EOF)
181
166k
      {
182
166k
        *p=(char) c;
183
166k
        *(p+1)='\0';
184
166k
      }
185
167k
  }
186
4.48k
  return(c);
187
4.48k
}
188
189
static unsigned int PNMInteger(Image *image,CommentInfo *comment_info,
190
  const unsigned int base,ExceptionInfo *exception)
191
208k
{
192
208k
  int
193
208k
    c;
194
195
208k
  unsigned int
196
208k
    value;
197
198
  /*
199
    Skip any leading whitespace.
200
  */
201
208k
  do
202
223k
  {
203
223k
    c=ReadBlobByte(image);
204
223k
    if (c == EOF)
205
2.86k
      return(0);
206
220k
    if (c == (int) '#')
207
1.91k
      c=PNMComment(image,comment_info,exception);
208
220k
  } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
209
205k
  if (base == 2)
210
72.6k
    return((unsigned int) (c-(int) '0'));
211
  /*
212
    Evaluate number.
213
  */
214
133k
  value=0;
215
221k
  while (isdigit((int) ((unsigned char) c)) != 0)
216
88.6k
  {
217
88.6k
    if (value <= (unsigned int) (INT_MAX/10))
218
87.2k
      {
219
87.2k
        value*=10;
220
87.2k
        if (value <= (unsigned int) (INT_MAX-(c-(int) '0')))
221
86.7k
          value+=(unsigned int) (c-(int) '0');
222
87.2k
      }
223
88.6k
    c=ReadBlobByte(image);
224
88.6k
    if (c == EOF)
225
171
      return(0);
226
88.6k
  }
227
132k
  if (c == (int) '#')
228
884
    c=PNMComment(image,comment_info,exception);
229
132k
  return(value);
230
133k
}
231
232
static char *PNMString(Image *image,char *string,const size_t extent)
233
3.86k
{
234
3.86k
  int
235
3.86k
    c;
236
237
3.86k
  size_t
238
3.86k
    i;
239
240
39.5k
  for (i=0; i < (extent-1L); i++)
241
38.9k
  {
242
38.9k
    c=ReadBlobByte(image);
243
38.9k
    if (c == EOF)
244
475
      {
245
475
        if (i == 0)
246
144
          return((char *) NULL);
247
331
        break;
248
475
      }
249
38.4k
    string[i]=(char) c;
250
38.4k
    if (c == '\n' || c == '\r')
251
2.76k
      break;
252
38.4k
  }
253
3.72k
  string[i]='\0';
254
3.72k
  return(string);
255
3.86k
}
256
257
static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
258
18.9k
{
259
18.9k
#define ThrowPNMException(exception,message) \
260
5
{ \
261
11.7k
  if (comment_info.comment != (char *) NULL)  \
262
2.77k
    comment_info.comment=DestroyString(comment_info.comment); \
263
11.7k
  if (quantum_info != (QuantumInfo *) NULL) \
264
5
    quantum_info=DestroyQuantumInfo(quantum_info); \
265
11.7k
  ThrowReaderException((exception),(message)); \
266
0
}
267
268
18.9k
  char
269
18.9k
    format;
270
271
18.9k
  ColorspaceType
272
18.9k
    colorspace;
273
274
18.9k
  CommentInfo
275
18.9k
    comment_info;
276
277
18.9k
  const void
278
18.9k
    *stream;
279
280
18.9k
  double
281
18.9k
    quantum_scale;
282
283
18.9k
  Image
284
18.9k
    *image;
285
286
18.9k
  MagickBooleanType
287
18.9k
    is_gray = MagickTrue,
288
18.9k
    is_mono = MagickTrue,
289
18.9k
    status;
290
291
18.9k
  QuantumAny
292
18.9k
    max_value;
293
294
18.9k
  QuantumInfo
295
18.9k
    *quantum_info;
296
297
18.9k
  QuantumType
298
18.9k
    quantum_type;
299
300
18.9k
  size_t
301
18.9k
    depth,
302
18.9k
    extent;
303
304
18.9k
  ssize_t
305
18.9k
    count,
306
18.9k
    row,
307
18.9k
    y;
308
309
18.9k
  unsigned char
310
18.9k
    *pixels;
311
312
  /*
313
    Open image file.
314
  */
315
18.9k
  assert(image_info != (const ImageInfo *) NULL);
316
18.9k
  assert(image_info->signature == MagickCoreSignature);
317
18.9k
  assert(exception != (ExceptionInfo *) NULL);
318
18.9k
  assert(exception->signature == MagickCoreSignature);
319
18.9k
  if (IsEventLogging() != MagickFalse)
320
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
321
0
      image_info->filename);
322
18.9k
  image=AcquireImage(image_info,exception);
323
18.9k
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
324
18.9k
  if (status == MagickFalse)
325
5
    {
326
5
      image=DestroyImageList(image);
327
5
      return((Image *) NULL);
328
5
    }
329
  /*
330
    Read PNM image.
331
  */
332
18.9k
  comment_info.comment=(char *) NULL;
333
18.9k
  quantum_info=(QuantumInfo *) NULL;
334
18.9k
  count=ReadBlob(image,1,(unsigned char *) &format);
335
18.9k
  do
336
23.1k
  {
337
    /*
338
      Initialize image structure.
339
    */
340
23.1k
    comment_info.comment=AcquireString(NULL);
341
23.1k
    comment_info.extent=MagickPathExtent;
342
23.1k
    if ((count != 1) || (format != 'P'))
343
22.9k
      ThrowPNMException(CorruptImageError,"ImproperImageHeader");
344
22.9k
    max_value=1;
345
22.9k
    colorspace=UndefinedColorspace;
346
22.9k
    quantum_type=UndefinedQuantum;
347
22.9k
    quantum_scale=1.0;
348
22.9k
    format=(char) ReadBlobByte(image);
349
22.9k
    if (format != '7')
350
18.5k
      {
351
        /*
352
          PBM, PGM, PPM, and PNM.
353
        */
354
18.5k
        if (ReadBlobByte(image) == '4')
355
2.31k
          image->alpha_trait=BlendPixelTrait;
356
18.5k
        image->columns=(size_t) PNMInteger(image,&comment_info,10,exception);
357
18.5k
        image->rows=(size_t) PNMInteger(image,&comment_info,10,exception);
358
18.5k
        if ((format == 'f') || (format == 'F') || (format == 'h') ||
359
18.5k
            (format == 'H'))
360
3.86k
          {
361
3.86k
            char
362
3.86k
              scale[32];
363
364
3.86k
            if (PNMString(image,scale,sizeof(scale)) != (char *) NULL)
365
3.72k
              quantum_scale=StringToDouble(scale,(char **) NULL);
366
3.86k
          }
367
14.6k
        else
368
14.6k
          {
369
14.6k
            if ((format == '1') || (format == '4'))
370
6.74k
              max_value=1;  /* bitmap */
371
7.89k
            else
372
7.89k
              max_value=(QuantumAny) PNMInteger(image,&comment_info,10,
373
7.89k
                exception);
374
14.6k
          }
375
18.5k
      }
376
4.48k
    else
377
4.48k
      {
378
4.48k
        char
379
4.48k
          keyword[MagickPathExtent],
380
4.48k
          value[MagickPathExtent];
381
382
4.48k
        int
383
4.48k
          c;
384
385
4.48k
        char
386
4.48k
          *p;
387
388
        /*
389
          PAM.
390
        */
391
72.4k
        for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
392
71.0k
        {
393
76.2k
          while (isspace((int) ((unsigned char) c)) != 0)
394
5.25k
            c=ReadBlobByte(image);
395
71.0k
          if (c == '#')
396
1.06k
            {
397
              /*
398
                Comment.
399
              */
400
2.75k
              while (c == '#')
401
1.68k
              {
402
1.68k
                c=PNMComment(image,&comment_info,exception);
403
1.68k
                c=ReadBlobByte(image);
404
1.68k
              }
405
2.21k
              while (isspace((int) ((unsigned char) c)) != 0)
406
1.15k
                c=ReadBlobByte(image);
407
1.06k
            }
408
71.0k
          p=keyword;
409
71.0k
          do
410
280k
          {
411
280k
            if ((size_t) (p-keyword) < (MagickPathExtent-1))
412
275k
              *p++=(char) c;
413
280k
            c=ReadBlobByte(image);
414
280k
          } while (isalnum((int) ((unsigned char) c)));
415
71.0k
          *p='\0';
416
71.0k
          if (LocaleCompare(keyword,"endhdr") == 0)
417
3.07k
            break;
418
89.6k
          while (isspace((int) ((unsigned char) c)) != 0)
419
21.6k
            c=ReadBlobByte(image);
420
67.9k
          p=value;
421
215k
          while (isalnum((int) ((unsigned char) c)) || (c == '_'))
422
147k
          {
423
147k
            if ((size_t) (p-value) < (MagickPathExtent-1))
424
147k
              *p++=(char) c;
425
147k
            c=ReadBlobByte(image);
426
147k
          }
427
67.9k
          *p='\0';
428
          /*
429
            Assign a value to the specified keyword.
430
          */
431
67.9k
          if (LocaleCompare(keyword,"depth") == 0)
432
403
            (void) StringToUnsignedLong(value);
433
67.9k
          if (LocaleCompare(keyword,"height") == 0)
434
4.16k
            image->rows=StringToUnsignedLong(value);
435
67.9k
          if (LocaleCompare(keyword,"maxval") == 0)
436
2.88k
            max_value=StringToUnsignedLong(value);
437
67.9k
          if ((quantum_type == UndefinedQuantum) &&
438
67.9k
              (LocaleCompare(keyword,"TUPLTYPE") == 0))
439
2.91k
            {
440
2.91k
              if (LocaleCompare(value,"BLACKANDWHITE") == 0)
441
35
                {
442
35
                  colorspace=GRAYColorspace;
443
35
                  quantum_type=GrayQuantum;
444
35
                }
445
2.91k
              if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
446
51
                {
447
51
                  colorspace=GRAYColorspace;
448
51
                  image->alpha_trait=BlendPixelTrait;
449
51
                  quantum_type=GrayAlphaQuantum;
450
51
                }
451
2.91k
              if (LocaleCompare(value,"GRAYSCALE") == 0)
452
378
                {
453
378
                  colorspace=GRAYColorspace;
454
378
                  quantum_type=GrayQuantum;
455
378
                }
456
2.91k
              if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
457
447
                {
458
447
                  colorspace=GRAYColorspace;
459
447
                  image->alpha_trait=BlendPixelTrait;
460
447
                  quantum_type=GrayAlphaQuantum;
461
447
                }
462
2.91k
              if (LocaleCompare(value,"RGB_ALPHA") == 0)
463
383
                {
464
383
                  image->alpha_trait=BlendPixelTrait;
465
383
                  quantum_type=RGBAQuantum;
466
383
                }
467
2.91k
              if (LocaleCompare(value,"CMYK") == 0)
468
766
                {
469
766
                  colorspace=CMYKColorspace;
470
766
                  quantum_type=CMYKQuantum;
471
766
                }
472
2.91k
              if (LocaleCompare(value,"CMYK_ALPHA") == 0)
473
419
                {
474
419
                  colorspace=CMYKColorspace;
475
419
                  image->alpha_trait=BlendPixelTrait;
476
419
                  quantum_type=CMYKAQuantum;
477
419
                }
478
2.91k
            }
479
67.9k
          if (LocaleCompare(keyword,"width") == 0)
480
4.30k
            image->columns=StringToUnsignedLong(value);
481
67.9k
        }
482
4.48k
      }
483
22.9k
    if (quantum_type == UndefinedQuantum)
484
20.5k
      quantum_type=RGBQuantum;
485
22.9k
    if ((image->columns == 0) || (image->rows == 0))
486
20.9k
      ThrowPNMException(CorruptImageError,"NegativeOrZeroImageSize");
487
20.9k
    if ((max_value == 0) || (max_value > 4294967295UL))
488
20.7k
      ThrowPNMException(CorruptImageError,"ImproperImageHeader");
489
116k
    for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
490
20.7k
    image->depth=depth;
491
20.7k
    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
492
0
      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
493
0
        break;
494
20.7k
    if ((MagickSizeType) (image->columns*image->rows/8) > GetBlobSize(image))
495
20.4k
      ThrowPNMException(CorruptImageError,"InsufficientImageDataInFile");
496
20.4k
    status=SetImageExtent(image,image->columns,image->rows,exception);
497
20.4k
    if (status == MagickFalse)
498
296
      {
499
296
        if (comment_info.comment != (char *) NULL)
500
296
          comment_info.comment=DestroyString(comment_info.comment);
501
296
        if (quantum_info != (QuantumInfo *) NULL)
502
0
          quantum_info=DestroyQuantumInfo(quantum_info);
503
296
        return(DestroyImageList(image));
504
296
      }
505
20.1k
    if (colorspace != UndefinedColorspace)
506
2.03k
      (void) SetImageColorspace(image,colorspace,exception);
507
20.1k
    (void) ResetImagePixels(image,exception);
508
    /*
509
      Convert PNM pixels to runextent-encoded MIFF packets.
510
    */
511
20.1k
    row=0;
512
20.1k
    y=0;
513
20.1k
    switch (format)
514
20.1k
    {
515
2.39k
      case '1':
516
2.39k
      {
517
        /*
518
          Convert PBM image to pixel packets.
519
        */
520
2.39k
        (void) SetImageColorspace(image,GRAYColorspace,exception);
521
32.1k
        for (y=0; y < (ssize_t) image->rows; y++)
522
30.0k
        {
523
30.0k
          ssize_t
524
30.0k
            x;
525
526
30.0k
          Quantum
527
30.0k
            *magick_restrict q;
528
529
30.0k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
530
30.0k
          if (q == (Quantum *) NULL)
531
0
            break;
532
102k
          for (x=0; x < (ssize_t) image->columns; x++)
533
72.8k
          {
534
72.8k
            SetPixelGray(image,PNMInteger(image,&comment_info,2,exception) ==
535
72.8k
              0 ? QuantumRange : 0,q);
536
72.8k
            if (EOFBlob(image) != MagickFalse)
537
326
              break;
538
72.5k
            q+=(ptrdiff_t) GetPixelChannels(image);
539
72.5k
          }
540
30.0k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
541
0
            break;
542
30.0k
          if (image->previous == (Image *) NULL)
543
22.6k
            {
544
22.6k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
545
22.6k
                image->rows);
546
22.6k
              if (status == MagickFalse)
547
0
                break;
548
22.6k
            }
549
30.0k
          if (EOFBlob(image) != MagickFalse)
550
326
            break;
551
30.0k
        }
552
2.39k
        image->type=BilevelType;
553
2.39k
        break;
554
0
      }
555
1.05k
      case '2':
556
1.05k
      {
557
1.05k
        Quantum
558
1.05k
          intensity;
559
560
        /*
561
          Convert PGM image to pixel packets.
562
        */
563
1.05k
        (void) SetImageColorspace(image,GRAYColorspace,exception);
564
9.89k
        for (y=0; y < (ssize_t) image->rows; y++)
565
9.03k
        {
566
9.03k
          ssize_t
567
9.03k
            x;
568
569
9.03k
          Quantum
570
9.03k
            *magick_restrict q;
571
572
9.03k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
573
9.03k
          if (q == (Quantum *) NULL)
574
0
            break;
575
28.5k
          for (x=0; x < (ssize_t) image->columns; x++)
576
19.7k
          {
577
19.7k
            intensity=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
578
19.7k
              exception),max_value);
579
19.7k
            if (EOFBlob(image) != MagickFalse)
580
199
              break;
581
19.5k
            SetPixelGray(image,intensity,q);
582
19.5k
            q+=(ptrdiff_t) GetPixelChannels(image);
583
19.5k
          }
584
9.03k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
585
0
            break;
586
9.03k
          if (image->previous == (Image *) NULL)
587
7.99k
            {
588
7.99k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
589
7.99k
                image->rows);
590
7.99k
              if (status == MagickFalse)
591
0
                break;
592
7.99k
            }
593
9.03k
          if (EOFBlob(image) != MagickFalse)
594
199
            break;
595
9.03k
        }
596
1.05k
        image->type=GrayscaleType;
597
1.05k
        break;
598
0
      }
599
767
      case '3':
600
767
      {
601
        /*
602
          Convert PNM image to pixel packets.
603
        */
604
8.13k
        for (y=0; y < (ssize_t) image->rows; y++)
605
7.82k
        {
606
7.82k
          Quantum
607
7.82k
            *magick_restrict q;
608
609
7.82k
          ssize_t
610
7.82k
            x;
611
612
7.82k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
613
7.82k
          if (q == (Quantum *) NULL)
614
0
            break;
615
31.3k
          for (x=0; x < (ssize_t) image->columns; x++)
616
23.9k
          {
617
23.9k
            Quantum
618
23.9k
              pixel;
619
620
23.9k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
621
23.9k
              exception),max_value);
622
23.9k
            if (EOFBlob(image) != MagickFalse)
623
357
              break;
624
23.5k
            SetPixelRed(image,pixel,q);
625
23.5k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
626
23.5k
              exception),max_value);
627
23.5k
            SetPixelGreen(image,pixel,q);
628
23.5k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
629
23.5k
              exception),max_value);
630
23.5k
            SetPixelBlue(image,pixel,q);
631
23.5k
            if ((is_gray != MagickFalse) &&
632
23.5k
                (IsPixelGray(image,q) == MagickFalse))
633
399
              is_gray=MagickFalse;
634
23.5k
            if ((is_mono != MagickFalse) &&
635
23.5k
                (IsPixelMonochrome(image,q) == MagickFalse))
636
399
              is_mono=MagickFalse;
637
23.5k
            q+=(ptrdiff_t) GetPixelChannels(image);
638
23.5k
          }
639
7.82k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
640
0
            break;
641
7.82k
          if (image->previous == (Image *) NULL)
642
6.43k
            {
643
6.43k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
644
6.43k
                image->rows);
645
6.43k
              if (status == MagickFalse)
646
0
                break;
647
6.43k
            }
648
7.82k
          if (EOFBlob(image) != MagickFalse)
649
452
            break;
650
7.82k
        }
651
767
        if (is_gray != MagickFalse)
652
309
          image->type=GrayscaleType;
653
767
        if (is_mono != MagickFalse)
654
309
          image->type=BilevelType;
655
767
        break;
656
0
      }
657
3.75k
      case '4':
658
3.75k
      {
659
        /*
660
          Convert PBM raw image to pixel packets.
661
        */
662
3.75k
        (void) SetImageColorspace(image,GRAYColorspace,exception);
663
3.75k
        quantum_type=GrayQuantum;
664
3.75k
        if (image->storage_class == PseudoClass)
665
0
          quantum_type=IndexQuantum;
666
3.75k
        quantum_info=AcquireQuantumInfo(image_info,image);
667
3.75k
        if (quantum_info == (QuantumInfo *) NULL)
668
3.75k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
669
3.75k
        SetQuantumMinIsWhite(quantum_info,MagickTrue);
670
3.75k
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
671
3.75k
        pixels=GetQuantumPixels(quantum_info);
672
99.2k
        for (y=0; y < (ssize_t) image->rows; y++)
673
95.6k
        {
674
95.6k
          MagickBooleanType
675
95.6k
            sync;
676
677
95.6k
          Quantum
678
95.6k
            *magick_restrict q;
679
680
95.6k
          ssize_t
681
95.6k
            offset;
682
683
95.6k
          size_t
684
95.6k
            length;
685
686
95.6k
          stream=ReadBlobStream(image,extent,pixels,&count);
687
95.6k
          if (count != (ssize_t) extent)
688
131
            break;
689
95.4k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
690
95.4k
              (image->previous == (Image *) NULL))
691
0
            {
692
0
              MagickBooleanType
693
0
                proceed;
694
695
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
696
0
                row,image->rows);
697
0
              if (proceed == MagickFalse)
698
0
                break;
699
0
            }
700
95.4k
          offset=row++;
701
95.4k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
702
95.4k
          if (q == (Quantum *) NULL)
703
0
            break;
704
95.4k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
705
95.4k
            quantum_type,(unsigned char *) stream,exception);
706
95.4k
          if (length != extent)
707
0
            break;
708
95.4k
          sync=SyncAuthenticPixels(image,exception);
709
95.4k
          if (sync == MagickFalse)
710
0
            break;
711
95.4k
        }
712
3.75k
        quantum_info=DestroyQuantumInfo(quantum_info);
713
3.75k
        SetQuantumImageType(image,quantum_type);
714
3.75k
        break;
715
3.75k
      }
716
1.25k
      case '5':
717
1.25k
      {
718
        /*
719
          Convert PGM raw image to pixel packets.
720
        */
721
1.25k
        (void) SetImageColorspace(image,GRAYColorspace,exception);
722
1.25k
        quantum_type=GrayQuantum;
723
1.25k
        extent=(image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
724
1.25k
          image->columns;
725
1.25k
        quantum_info=AcquireQuantumInfo(image_info,image);
726
1.25k
        if (quantum_info == (QuantumInfo *) NULL)
727
1.25k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
728
1.25k
        pixels=GetQuantumPixels(quantum_info);
729
17.8k
        for (y=0; y < (ssize_t) image->rows; y++)
730
17.5k
        {
731
17.5k
          const unsigned char
732
17.5k
            *magick_restrict p;
733
734
17.5k
          MagickBooleanType
735
17.5k
            sync;
736
737
17.5k
          Quantum
738
17.5k
            *magick_restrict q;
739
740
17.5k
          ssize_t
741
17.5k
            offset,
742
17.5k
            x;
743
744
17.5k
          stream=ReadBlobStream(image,extent,pixels,&count);
745
17.5k
          if (count != (ssize_t) extent)
746
910
            break;
747
16.6k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
748
16.6k
              (image->previous == (Image *) NULL))
749
0
            {
750
0
              MagickBooleanType
751
0
                proceed;
752
753
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
754
0
                row,image->rows);
755
0
              if (proceed == MagickFalse)
756
0
                break;
757
0
            }
758
16.6k
          offset=row++;
759
16.6k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
760
16.6k
          if (q == (Quantum *) NULL)
761
0
            break;
762
16.6k
          p=(unsigned char *) stream;
763
16.6k
          switch (image->depth)
764
16.6k
          {
765
1.05k
            case 8:
766
1.92k
            case 16:
767
1.92k
            case 32:
768
1.92k
            {
769
1.92k
              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
770
1.92k
                quantum_type,(unsigned char *) stream,exception);
771
1.92k
              break;
772
1.92k
            }
773
14.7k
            default:
774
14.7k
            {
775
14.7k
              switch (image->depth)
776
14.7k
              {
777
981
                case 1:
778
1.99k
                case 2:
779
3.55k
                case 3:
780
6.71k
                case 4:
781
7.54k
                case 5:
782
8.50k
                case 6:
783
9.38k
                case 7:
784
9.38k
                {
785
9.38k
                  unsigned char
786
9.38k
                    pixel;
787
788
26.8k
                  for (x=0; x < (ssize_t) image->columns; x++)
789
17.4k
                  {
790
17.4k
                    p=PushCharPixel(p,&pixel);
791
17.4k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
792
17.4k
                    q+=(ptrdiff_t) GetPixelChannels(image);
793
17.4k
                  }
794
9.38k
                  break;
795
8.50k
                }
796
632
                case 9:
797
1.65k
                case 10:
798
2.25k
                case 11:
799
2.72k
                case 12:
800
3.20k
                case 13:
801
3.75k
                case 14:
802
4.35k
                case 15:
803
4.35k
                {
804
4.35k
                  unsigned short
805
4.35k
                    pixel;
806
807
11.8k
                  for (x=0; x < (ssize_t) image->columns; x++)
808
7.52k
                  {
809
7.52k
                    p=PushShortPixel(MSBEndian,p,&pixel);
810
7.52k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
811
7.52k
                    q+=(ptrdiff_t) GetPixelChannels(image);
812
7.52k
                  }
813
4.35k
                  break;
814
3.75k
                }
815
973
                default:
816
973
                {
817
973
                  unsigned int
818
973
                    pixel;
819
820
3.34k
                  for (x=0; x < (ssize_t) image->columns; x++)
821
2.37k
                  {
822
2.37k
                    p=PushLongPixel(MSBEndian,p,&pixel);
823
2.37k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
824
2.37k
                    q+=(ptrdiff_t) GetPixelChannels(image);
825
2.37k
                  }
826
973
                  break;
827
3.75k
                }
828
14.7k
              }
829
14.7k
              break;
830
14.7k
            }
831
16.6k
          }
832
16.6k
          sync=SyncAuthenticPixels(image,exception);
833
16.6k
          if (sync == MagickFalse)
834
0
            break;
835
16.6k
        }
836
1.25k
        quantum_info=DestroyQuantumInfo(quantum_info);
837
1.25k
        SetQuantumImageType(image,quantum_type);
838
1.25k
        break;
839
1.25k
      }
840
4.30k
      case '6':
841
4.30k
      {
842
        /*
843
          Convert PNM raster image to pixel packets.
844
        */
845
4.30k
        quantum_type=RGBQuantum;
846
4.30k
        extent=3*(size_t) (image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
847
4.30k
          image->columns;
848
4.30k
        quantum_info=AcquireQuantumInfo(image_info,image);
849
4.30k
        if (quantum_info == (QuantumInfo *) NULL)
850
4.30k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
851
4.30k
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
852
4.30k
        pixels=GetQuantumPixels(quantum_info);
853
30.9k
        for (y=0; y < (ssize_t) image->rows; y++)
854
29.6k
        {
855
29.6k
          const unsigned char
856
29.6k
            *magick_restrict p;
857
858
29.6k
          MagickBooleanType
859
29.6k
            sync;
860
861
29.6k
          Quantum
862
29.6k
            *magick_restrict q;
863
864
29.6k
          ssize_t
865
29.6k
            offset,
866
29.6k
            x;
867
868
29.6k
          stream=ReadBlobStream(image,extent,pixels,&count);
869
29.6k
          if (count != (ssize_t) extent)
870
3.04k
            break;
871
26.6k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
872
26.6k
              (image->previous == (Image *) NULL))
873
0
            {
874
0
              MagickBooleanType
875
0
                proceed;
876
877
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
878
0
                row,image->rows);
879
0
              if (proceed == MagickFalse)
880
0
                break;
881
0
            }
882
26.6k
          offset=row++;
883
26.6k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
884
26.6k
          if (q == (Quantum *) NULL)
885
0
            break;
886
26.6k
          p=(unsigned char *) stream;
887
26.6k
          switch (image->depth)
888
26.6k
          {
889
1.08k
            case 1:
890
2.21k
            case 2:
891
5.28k
            case 3:
892
7.67k
            case 4:
893
8.60k
            case 5:
894
11.1k
            case 6:
895
12.1k
            case 7:
896
12.1k
            {
897
12.1k
              unsigned char
898
12.1k
                pixel;
899
900
42.2k
              for (x=0; x < (ssize_t) image->columns; x++)
901
30.1k
              {
902
30.1k
                p=PushCharPixel(p,&pixel);
903
30.1k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
904
30.1k
                p=PushCharPixel(p,&pixel);
905
30.1k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
906
30.1k
                p=PushCharPixel(p,&pixel);
907
30.1k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
908
30.1k
                SetPixelAlpha(image,OpaqueAlpha,q);
909
30.1k
                if ((is_gray != MagickFalse) &&
910
30.1k
                    (IsPixelGray(image,q) == MagickFalse))
911
1.25k
                  is_gray=MagickFalse;
912
30.1k
                if ((is_mono != MagickFalse) &&
913
30.1k
                    (IsPixelMonochrome(image,q) == MagickFalse))
914
1.25k
                  is_mono=MagickFalse;
915
30.1k
                q+=(ptrdiff_t) GetPixelChannels(image);
916
30.1k
              }
917
12.1k
              break;
918
11.1k
            }
919
1.64k
            case 8:
920
1.64k
            {
921
5.92k
              for (x=0; x < (ssize_t) image->columns; x++)
922
4.27k
              {
923
4.27k
                SetPixelRed(image,ScaleCharToQuantum(*p++),q);
924
4.27k
                SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
925
4.27k
                SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
926
4.27k
                SetPixelAlpha(image,OpaqueAlpha,q);
927
4.27k
                if ((is_gray != MagickFalse) &&
928
4.27k
                    (IsPixelGray(image,q) == MagickFalse))
929
129
                  is_gray=MagickFalse;
930
4.27k
                if ((is_mono != MagickFalse) &&
931
4.27k
                    (IsPixelMonochrome(image,q) == MagickFalse))
932
129
                  is_mono=MagickFalse;
933
4.27k
                q+=(ptrdiff_t) GetPixelChannels(image);
934
4.27k
              }
935
1.64k
              break;
936
11.1k
            }
937
2.01k
            case 9:
938
3.44k
            case 10:
939
4.16k
            case 11:
940
4.78k
            case 12:
941
5.35k
            case 13:
942
6.64k
            case 14:
943
7.05k
            case 15:
944
7.05k
            {
945
7.05k
              unsigned short
946
7.05k
                pixel;
947
948
23.3k
              for (x=0; x < (ssize_t) image->columns; x++)
949
16.3k
              {
950
16.3k
                p=PushShortPixel(MSBEndian,p,&pixel);
951
16.3k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
952
16.3k
                p=PushShortPixel(MSBEndian,p,&pixel);
953
16.3k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
954
16.3k
                p=PushShortPixel(MSBEndian,p,&pixel);
955
16.3k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
956
16.3k
                SetPixelAlpha(image,OpaqueAlpha,q);
957
16.3k
                if ((is_gray != MagickFalse) &&
958
16.3k
                    (IsPixelGray(image,q) == MagickFalse))
959
1.02k
                  is_gray=MagickFalse;
960
16.3k
                if ((is_mono != MagickFalse) &&
961
16.3k
                    (IsPixelMonochrome(image,q) == MagickFalse))
962
1.02k
                  is_mono=MagickFalse;
963
16.3k
                q+=(ptrdiff_t) GetPixelChannels(image);
964
16.3k
              }
965
7.05k
              break;
966
6.64k
            }
967
2.66k
            case 16:
968
2.66k
            {
969
2.66k
              unsigned short
970
2.66k
                pixel;
971
972
10.7k
              for (x=0; x < (ssize_t) image->columns; x++)
973
8.05k
              {
974
8.05k
                p=PushShortPixel(MSBEndian,p,&pixel);
975
8.05k
                SetPixelRed(image,ScaleShortToQuantum(pixel),q);
976
8.05k
                p=PushShortPixel(MSBEndian,p,&pixel);
977
8.05k
                SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
978
8.05k
                p=PushShortPixel(MSBEndian,p,&pixel);
979
8.05k
                SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
980
8.05k
                SetPixelAlpha(image,OpaqueAlpha,q);
981
8.05k
                if ((is_gray != MagickFalse) &&
982
8.05k
                    (IsPixelGray(image,q) == MagickFalse))
983
578
                  is_gray=MagickFalse;
984
8.05k
                if ((is_mono != MagickFalse) &&
985
8.05k
                    (IsPixelMonochrome(image,q) == MagickFalse))
986
578
                  is_mono=MagickFalse;
987
8.05k
                q+=(ptrdiff_t) GetPixelChannels(image);
988
8.05k
              }
989
2.66k
              break;
990
6.64k
            }
991
0
            case 32:
992
0
            {
993
0
              unsigned int
994
0
                pixel;
995
996
0
              for (x=0; x < (ssize_t) image->columns; x++)
997
0
              {
998
0
                p=PushLongPixel(MSBEndian,p,&pixel);
999
0
                SetPixelRed(image,ScaleLongToQuantum(pixel),q);
1000
0
                p=PushLongPixel(MSBEndian,p,&pixel);
1001
0
                SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
1002
0
                p=PushLongPixel(MSBEndian,p,&pixel);
1003
0
                SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
1004
0
                SetPixelAlpha(image,OpaqueAlpha,q);
1005
0
                if ((is_gray != MagickFalse) &&
1006
0
                    (IsPixelGray(image,q) == MagickFalse))
1007
0
                  is_gray=MagickFalse;
1008
0
                if ((is_mono != MagickFalse) &&
1009
0
                    (IsPixelMonochrome(image,q) == MagickFalse))
1010
0
                  is_mono=MagickFalse;
1011
0
                q+=(ptrdiff_t) GetPixelChannels(image);
1012
0
              }
1013
0
              break;
1014
6.64k
            }
1015
3.17k
            default:
1016
3.17k
            {
1017
3.17k
              unsigned int
1018
3.17k
                pixel;
1019
1020
10.7k
              for (x=0; x < (ssize_t) image->columns; x++)
1021
7.61k
              {
1022
7.61k
                p=PushLongPixel(MSBEndian,p,&pixel);
1023
7.61k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1024
7.61k
                p=PushLongPixel(MSBEndian,p,&pixel);
1025
7.61k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1026
7.61k
                p=PushLongPixel(MSBEndian,p,&pixel);
1027
7.61k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1028
7.61k
                SetPixelAlpha(image,OpaqueAlpha,q);
1029
7.61k
                if ((is_gray != MagickFalse) &&
1030
7.61k
                    (IsPixelGray(image,q) == MagickFalse))
1031
581
                  is_gray=MagickFalse;
1032
7.61k
                if ((is_mono != MagickFalse) &&
1033
7.61k
                    (IsPixelMonochrome(image,q) == MagickFalse))
1034
581
                  is_mono=MagickFalse;
1035
7.61k
                q+=(ptrdiff_t) GetPixelChannels(image);
1036
7.61k
              }
1037
3.17k
              break;
1038
6.64k
            }
1039
26.6k
          }
1040
26.6k
          sync=SyncAuthenticPixels(image,exception);
1041
26.6k
          if (sync == MagickFalse)
1042
0
            break;
1043
26.6k
        }
1044
4.30k
        if (is_gray != MagickFalse)
1045
667
          image->type=GrayscaleType;
1046
4.30k
        if (is_mono != MagickFalse)
1047
665
          image->type=BilevelType;
1048
4.30k
        quantum_info=DestroyQuantumInfo(quantum_info);
1049
4.30k
        break;
1050
4.30k
      }
1051
3.16k
      case '7':
1052
3.16k
      {
1053
3.16k
        size_t
1054
3.16k
          channels;
1055
1056
        /*
1057
          Convert PAM raster image to pixel packets.
1058
        */
1059
3.16k
        switch (quantum_type)
1060
3.16k
        {
1061
398
          case GrayQuantum:
1062
879
          case GrayAlphaQuantum:
1063
879
          {
1064
879
            channels=1;
1065
879
            break;
1066
398
          }
1067
744
          case CMYKQuantum:
1068
1.15k
          case CMYKAQuantum:
1069
1.15k
          {
1070
1.15k
            channels=4;
1071
1.15k
            break;
1072
744
          }
1073
1.13k
          default:
1074
1.13k
          {
1075
1.13k
            channels=3;
1076
1.13k
            break;
1077
744
          }
1078
3.16k
        }
1079
3.16k
        if (image->alpha_trait != UndefinedPixelTrait)
1080
1.26k
          channels++;
1081
3.16k
        extent=channels*(image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
1082
3.16k
          image->columns;
1083
3.16k
        quantum_info=AcquireQuantumInfo(image_info,image);
1084
3.16k
        if (quantum_info == (QuantumInfo *) NULL)
1085
3.16k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1086
3.16k
        pixels=GetQuantumPixels(quantum_info);
1087
57.2k
        for (y=0; y < (ssize_t) image->rows; y++)
1088
56.0k
        {
1089
56.0k
          const unsigned char
1090
56.0k
            *magick_restrict p;
1091
1092
56.0k
          MagickBooleanType
1093
56.0k
            sync;
1094
1095
56.0k
          Quantum
1096
56.0k
            *magick_restrict q;
1097
1098
56.0k
          ssize_t
1099
56.0k
            offset,
1100
56.0k
            x;
1101
1102
56.0k
          stream=ReadBlobStream(image,extent,pixels,&count);
1103
56.0k
          if (count != (ssize_t) extent)
1104
1.98k
            break;
1105
54.0k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1106
54.0k
              (image->previous == (Image *) NULL))
1107
0
            {
1108
0
              MagickBooleanType
1109
0
                proceed;
1110
1111
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1112
0
                row,image->rows);
1113
0
              if (proceed == MagickFalse)
1114
0
                break;
1115
0
            }
1116
54.0k
          offset=row++;
1117
54.0k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1118
54.0k
          if (q == (Quantum *) NULL)
1119
0
            break;
1120
54.0k
          p=(unsigned char *) stream;
1121
54.0k
          switch (image->depth)
1122
54.0k
          {
1123
3.28k
            case 8:
1124
9.70k
            case 16:
1125
14.8k
            case 32:
1126
14.8k
            {
1127
14.8k
              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1128
14.8k
                quantum_type,(unsigned char *) stream,exception);
1129
14.8k
              break;
1130
9.70k
            }
1131
39.2k
            default:
1132
39.2k
            {
1133
39.2k
              switch (quantum_type)
1134
39.2k
              {
1135
7.53k
                case GrayQuantum:
1136
13.4k
                case GrayAlphaQuantum:
1137
13.4k
                {
1138
13.4k
                  switch (image->depth)
1139
13.4k
                  {
1140
2.39k
                    case 1:
1141
2.90k
                    case 2:
1142
3.47k
                    case 3:
1143
4.53k
                    case 4:
1144
5.02k
                    case 5:
1145
5.70k
                    case 6:
1146
6.61k
                    case 7:
1147
6.61k
                    case 8:
1148
6.61k
                    {
1149
6.61k
                      unsigned char
1150
6.61k
                        pixel;
1151
 
1152
16.1k
                      for (x=0; x < (ssize_t) image->columns; x++)
1153
9.51k
                      {
1154
9.51k
                        p=PushCharPixel(p,&pixel);
1155
9.51k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1156
9.51k
                          q);
1157
9.51k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1158
9.51k
                        if (image->alpha_trait != UndefinedPixelTrait)
1159
5.06k
                          {
1160
5.06k
                            p=PushCharPixel(p,&pixel);
1161
5.06k
                            if (image->depth != 1)
1162
2.91k
                              SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1163
2.91k
                                max_value),q);
1164
2.15k
                            else
1165
2.15k
                              SetPixelAlpha(image,QuantumRange-
1166
2.15k
                                ScaleAnyToQuantum(pixel,max_value),q);
1167
5.06k
                          }
1168
9.51k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1169
9.51k
                      }
1170
6.61k
                      break;
1171
6.61k
                    }
1172
974
                    case 9:
1173
1.63k
                    case 10:
1174
2.11k
                    case 11:
1175
3.22k
                    case 12:
1176
3.99k
                    case 13:
1177
4.40k
                    case 14:
1178
5.09k
                    case 15:
1179
5.09k
                    case 16:
1180
5.09k
                    {
1181
5.09k
                      unsigned short
1182
5.09k
                        pixel;
1183
1184
16.8k
                      for (x=0; x < (ssize_t) image->columns; x++)
1185
11.7k
                      {
1186
11.7k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1187
11.7k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1188
11.7k
                          q);
1189
11.7k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1190
11.7k
                        if (image->alpha_trait != UndefinedPixelTrait)
1191
2.64k
                          {
1192
2.64k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1193
2.64k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1194
2.64k
                              max_value),q);
1195
2.64k
                          }
1196
11.7k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1197
11.7k
                      }
1198
5.09k
                      break;
1199
5.09k
                    }
1200
1.78k
                    default:
1201
1.78k
                    {
1202
1.78k
                      unsigned int
1203
1.78k
                        pixel;
1204
1205
4.72k
                      for (x=0; x < (ssize_t) image->columns; x++)
1206
2.94k
                      {
1207
2.94k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1208
2.94k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1209
2.94k
                          q);
1210
2.94k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1211
2.94k
                        if (image->alpha_trait != UndefinedPixelTrait)
1212
1.08k
                          {
1213
1.08k
                            p=PushLongPixel(MSBEndian,p,&pixel);
1214
1.08k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1215
1.08k
                              max_value),q);
1216
1.08k
                          }
1217
2.94k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1218
2.94k
                      }
1219
1.78k
                    }
1220
13.4k
                  }
1221
13.4k
                  break;
1222
13.4k
                }
1223
13.4k
                case CMYKQuantum:
1224
13.6k
                case CMYKAQuantum:
1225
13.6k
                {
1226
13.6k
                  switch (image->depth)
1227
13.6k
                  {
1228
2.00k
                    case 1:
1229
2.43k
                    case 2:
1230
2.95k
                    case 3:
1231
3.38k
                    case 4:
1232
3.79k
                    case 5:
1233
4.98k
                    case 6:
1234
5.71k
                    case 7:
1235
5.71k
                    case 8:
1236
5.71k
                    {
1237
5.71k
                      unsigned char
1238
5.71k
                        pixel;
1239
1240
14.4k
                      for (x=0; x < (ssize_t) image->columns; x++)
1241
8.72k
                      {
1242
8.72k
                        p=PushCharPixel(p,&pixel);
1243
8.72k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1244
8.72k
                        p=PushCharPixel(p,&pixel);
1245
8.72k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1246
8.72k
                          q);
1247
8.72k
                        p=PushCharPixel(p,&pixel);
1248
8.72k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1249
8.72k
                          q);
1250
8.72k
                        p=PushCharPixel(p,&pixel);
1251
8.72k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1252
8.72k
                          q);
1253
8.72k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1254
8.72k
                        if (image->alpha_trait != UndefinedPixelTrait)
1255
1.66k
                          {
1256
1.66k
                            p=PushCharPixel(p,&pixel);
1257
1.66k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1258
1.66k
                              max_value),q);
1259
1.66k
                          }
1260
8.72k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1261
8.72k
                      }
1262
5.71k
                      break;
1263
5.71k
                    }
1264
702
                    case 9:
1265
2.36k
                    case 10:
1266
2.80k
                    case 11:
1267
3.67k
                    case 12:
1268
4.23k
                    case 13:
1269
4.52k
                    case 14:
1270
5.49k
                    case 15:
1271
5.49k
                    case 16:
1272
5.49k
                    {
1273
5.49k
                      unsigned short
1274
5.49k
                        pixel;
1275
1276
13.0k
                      for (x=0; x < (ssize_t) image->columns; x++)
1277
7.58k
                      {
1278
7.58k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1279
7.58k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1280
7.58k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1281
7.58k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1282
7.58k
                          q);
1283
7.58k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1284
7.58k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1285
7.58k
                          q);
1286
7.58k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1287
7.58k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1288
7.58k
                          q);
1289
7.58k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1290
7.58k
                        if (image->alpha_trait != UndefinedPixelTrait)
1291
1.64k
                          {
1292
1.64k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1293
1.64k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1294
1.64k
                              max_value),q);
1295
1.64k
                          }
1296
7.58k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1297
7.58k
                      }
1298
5.49k
                      break;
1299
5.49k
                    }
1300
2.47k
                    default:
1301
2.47k
                    {
1302
2.47k
                      unsigned int
1303
2.47k
                        pixel;
1304
1305
6.40k
                      for (x=0; x < (ssize_t) image->columns; x++)
1306
3.92k
                      {
1307
3.92k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1308
3.92k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),
1309
3.92k
                          q);
1310
3.92k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1311
3.92k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1312
3.92k
                          q);
1313
3.92k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1314
3.92k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1315
3.92k
                          q);
1316
3.92k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1317
3.92k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1318
3.92k
                          q);
1319
3.92k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1320
3.92k
                        if (image->alpha_trait != UndefinedPixelTrait)
1321
1.57k
                          {
1322
1.57k
                            p=PushLongPixel(MSBEndian,p,&pixel);
1323
1.57k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1324
1.57k
                              max_value),q);
1325
1.57k
                          }
1326
3.92k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1327
3.92k
                      }
1328
2.47k
                      break;
1329
5.49k
                    }
1330
13.6k
                  }
1331
13.6k
                  break;
1332
13.6k
                }
1333
13.6k
                default:
1334
12.0k
                {
1335
12.0k
                  switch (image->depth)
1336
12.0k
                  {
1337
2.07k
                    case 1:
1338
2.52k
                    case 2:
1339
2.96k
                    case 3:
1340
3.37k
                    case 4:
1341
3.78k
                    case 5:
1342
4.32k
                    case 6:
1343
5.16k
                    case 7:
1344
5.16k
                    case 8:
1345
5.16k
                    {
1346
5.16k
                      unsigned char
1347
5.16k
                        pixel;
1348
1349
14.1k
                      for (x=0; x < (ssize_t) image->columns; x++)
1350
9.02k
                      {
1351
9.02k
                        p=PushCharPixel(p,&pixel);
1352
9.02k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1353
9.02k
                        p=PushCharPixel(p,&pixel);
1354
9.02k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1355
9.02k
                          q);
1356
9.02k
                        p=PushCharPixel(p,&pixel);
1357
9.02k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1358
9.02k
                          q);
1359
9.02k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1360
9.02k
                        if (image->alpha_trait != UndefinedPixelTrait)
1361
1.56k
                          {
1362
1.56k
                            p=PushCharPixel(p,&pixel);
1363
1.56k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1364
1.56k
                              max_value),q);
1365
1.56k
                          }
1366
9.02k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1367
9.02k
                      }
1368
5.16k
                      break;
1369
5.16k
                    }
1370
881
                    case 9:
1371
1.47k
                    case 10:
1372
1.89k
                    case 11:
1373
2.63k
                    case 12:
1374
3.43k
                    case 13:
1375
3.87k
                    case 14:
1376
4.50k
                    case 15:
1377
4.50k
                    case 16:
1378
4.50k
                    {
1379
4.50k
                      unsigned short
1380
4.50k
                        pixel;
1381
1382
12.3k
                      for (x=0; x < (ssize_t) image->columns; x++)
1383
7.79k
                      {
1384
7.79k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1385
7.79k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1386
7.79k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1387
7.79k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1388
7.79k
                          q);
1389
7.79k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1390
7.79k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1391
7.79k
                          q);
1392
7.79k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1393
7.79k
                        if (image->alpha_trait != UndefinedPixelTrait)
1394
1.27k
                          {
1395
1.27k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1396
1.27k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1397
1.27k
                              max_value),q);
1398
1.27k
                          }
1399
7.79k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1400
7.79k
                      }
1401
4.50k
                      break;
1402
4.50k
                    }
1403
2.41k
                    default:
1404
2.41k
                    {
1405
2.41k
                      unsigned int
1406
2.41k
                        pixel;
1407
1408
6.50k
                      for (x=0; x < (ssize_t) image->columns; x++)
1409
4.08k
                      {
1410
4.08k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1411
4.08k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),
1412
4.08k
                          q);
1413
4.08k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1414
4.08k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1415
4.08k
                          q);
1416
4.08k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1417
4.08k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1418
4.08k
                          q);
1419
4.08k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1420
4.08k
                        if (image->alpha_trait != UndefinedPixelTrait)
1421
1.01k
                          {
1422
1.01k
                            p=PushLongPixel(MSBEndian,p,&pixel);
1423
1.01k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1424
1.01k
                              max_value),q);
1425
1.01k
                          }
1426
4.08k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1427
4.08k
                      }
1428
2.41k
                      break;
1429
4.50k
                    }
1430
12.0k
                  }
1431
12.0k
                  break;
1432
12.0k
                }
1433
39.2k
              }
1434
39.2k
            }
1435
54.0k
          }
1436
54.0k
          sync=SyncAuthenticPixels(image,exception);
1437
54.0k
          if (sync == MagickFalse)
1438
0
            break;
1439
54.0k
        }
1440
3.16k
        quantum_info=DestroyQuantumInfo(quantum_info);
1441
3.16k
        SetQuantumImageType(image,quantum_type);
1442
3.16k
        break;
1443
3.16k
      }
1444
708
      case 'F':
1445
1.18k
      case 'f':
1446
1.18k
      {
1447
        /*
1448
          Convert PFM raster image to pixel packets.
1449
        */
1450
1.18k
        if (format != 'f')
1451
708
          quantum_type=image->alpha_trait != UndefinedPixelTrait ? RGBAQuantum :
1452
708
            RGBQuantum;
1453
475
        else
1454
475
          {
1455
475
            (void) SetImageColorspace(image,GRAYColorspace,exception);
1456
475
            quantum_type=GrayQuantum;
1457
475
          }
1458
1.18k
        image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1459
1.18k
        image->depth=32;
1460
1.18k
        quantum_info=AcquireQuantumInfo(image_info,image);
1461
1.18k
        if (quantum_info == (QuantumInfo *) NULL)
1462
1.18k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1463
1.18k
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1464
1.18k
        if (status == MagickFalse)
1465
1.18k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1466
1.18k
        SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1467
1.18k
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
1468
1.18k
        pixels=GetQuantumPixels(quantum_info);
1469
8.61k
        for (y=0; y < (ssize_t) image->rows; y++)
1470
8.17k
        {
1471
8.17k
          MagickBooleanType
1472
8.17k
            sync;
1473
1474
8.17k
          Quantum
1475
8.17k
            *magick_restrict q;
1476
1477
8.17k
          size_t
1478
8.17k
            length;
1479
1480
8.17k
          ssize_t
1481
8.17k
            offset;
1482
1483
8.17k
          stream=ReadBlobStream(image,extent,pixels,&count);
1484
8.17k
          if (count != (ssize_t) extent)
1485
751
            break;
1486
7.42k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1487
7.42k
              (image->previous == (Image *) NULL))
1488
0
            {
1489
0
              MagickBooleanType
1490
0
                proceed;
1491
1492
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1493
0
                row,image->rows);
1494
0
              if (proceed == MagickFalse)
1495
0
                break;
1496
0
            }
1497
7.42k
          offset=row++;
1498
7.42k
          q=QueueAuthenticPixels(image,0,((ssize_t) image->rows-offset-1),
1499
7.42k
            image->columns,1,exception);
1500
7.42k
          if (q == (Quantum *) NULL)
1501
0
            break;
1502
7.42k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1503
7.42k
            quantum_type,(unsigned char *) stream,exception);
1504
7.42k
          if (length != extent)
1505
0
            break;
1506
7.42k
          sync=SyncAuthenticPixels(image,exception);
1507
7.42k
          if (sync == MagickFalse)
1508
0
            break;
1509
7.42k
        }
1510
1.18k
        quantum_info=DestroyQuantumInfo(quantum_info);
1511
1.18k
        SetQuantumImageType(image,quantum_type);
1512
1.18k
        break;
1513
1.18k
      }
1514
1.08k
      case 'H':
1515
2.23k
      case 'h':
1516
2.23k
      {
1517
        /*
1518
          Convert PFM raster image to pixel packets.
1519
        */
1520
2.23k
        if (format != 'h')
1521
1.08k
          quantum_type=image->alpha_trait != UndefinedPixelTrait ? RGBAQuantum :
1522
1.08k
            RGBQuantum;
1523
1.14k
        else
1524
1.14k
          {
1525
1.14k
            (void) SetImageColorspace(image,GRAYColorspace,exception);
1526
1.14k
            quantum_type=GrayQuantum;
1527
1.14k
          }
1528
2.23k
        image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1529
2.23k
        image->depth=16;
1530
2.23k
        quantum_info=AcquireQuantumInfo(image_info,image);
1531
2.23k
        if (quantum_info == (QuantumInfo *) NULL)
1532
2.23k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1533
2.23k
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1534
2.23k
        if (status == MagickFalse)
1535
2.23k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1536
2.23k
        SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1537
2.23k
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
1538
2.23k
        pixels=GetQuantumPixels(quantum_info);
1539
19.6k
        for (y=0; y < (ssize_t) image->rows; y++)
1540
18.6k
        {
1541
18.6k
          MagickBooleanType
1542
18.6k
            sync;
1543
1544
18.6k
          Quantum
1545
18.6k
            *magick_restrict q;
1546
1547
18.6k
          size_t
1548
18.6k
            length;
1549
1550
18.6k
          ssize_t
1551
18.6k
            offset;
1552
1553
18.6k
          stream=ReadBlobStream(image,extent,pixels,&count);
1554
18.6k
          if (count != (ssize_t) extent)
1555
1.17k
            break;
1556
17.4k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1557
17.4k
              (image->previous == (Image *) NULL))
1558
0
            {
1559
0
              MagickBooleanType
1560
0
                proceed;
1561
1562
0
              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1563
0
                row,image->rows);
1564
0
              if (proceed == MagickFalse)
1565
0
                break;
1566
0
            }
1567
17.4k
          offset=row++;
1568
17.4k
          q=QueueAuthenticPixels(image,0,((ssize_t) image->rows-offset-1),
1569
17.4k
            image->columns,1,exception);
1570
17.4k
          if (q == (Quantum *) NULL)
1571
0
            break;
1572
17.4k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1573
17.4k
            quantum_type,(unsigned char *) stream,exception);
1574
17.4k
          if (length != extent)
1575
0
            break;
1576
17.4k
          sync=SyncAuthenticPixels(image,exception);
1577
17.4k
          if (sync == MagickFalse)
1578
0
            break;
1579
17.4k
        }
1580
2.23k
        quantum_info=DestroyQuantumInfo(quantum_info);
1581
2.23k
        SetQuantumImageType(image,quantum_type);
1582
2.23k
        break;
1583
2.23k
      }
1584
5
      default:
1585
5
        ThrowPNMException(CorruptImageError,"ImproperImageHeader");
1586
20.1k
    }
1587
20.1k
    if (*comment_info.comment != '\0')
1588
838
      (void) SetImageProperty(image,"comment",comment_info.comment,exception);
1589
20.1k
    comment_info.comment=DestroyString(comment_info.comment);
1590
20.1k
    if (y < (ssize_t) image->rows)
1591
11.1k
      ThrowPNMException(CorruptImageError,"UnableToReadImageData");
1592
11.1k
    if (EOFBlob(image) != MagickFalse)
1593
0
      {
1594
0
        (void) ThrowMagickException(exception,GetMagickModule(),
1595
0
          CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
1596
0
        break;
1597
0
      }
1598
    /*
1599
      Proceed to next image.
1600
    */
1601
11.1k
    if (image_info->number_scenes != 0)
1602
664
      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1603
8
        break;
1604
11.1k
    if ((format == '1') || (format == '2') || (format == '3'))
1605
3.23k
      do
1606
24.7k
      {
1607
        /*
1608
          Skip to end of line.
1609
        */
1610
24.7k
        count=ReadBlob(image,1,(unsigned char *) &format);
1611
24.7k
        if (count != 1)
1612
1.19k
          break;
1613
23.5k
        if (format == 'P')
1614
245
          break;
1615
23.5k
      } while (format != '\n');
1616
0
    count=ReadBlob(image,1,(unsigned char *) &format);
1617
11.1k
    if ((count == 1) && (format == 'P'))
1618
4.19k
      {
1619
        /*
1620
          Allocate next image structure.
1621
        */
1622
4.19k
        AcquireNextImage(image_info,image,exception);
1623
4.19k
        if (GetNextImageInList(image) == (Image *) NULL)
1624
0
          {
1625
0
            status=MagickFalse;
1626
0
            break;
1627
0
          }
1628
4.19k
        image=SyncNextImageInList(image);
1629
4.19k
        status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1630
4.19k
          GetBlobSize(image));
1631
4.19k
        if (status == MagickFalse)
1632
0
          break;
1633
4.19k
      }
1634
11.1k
  } while ((count == 1) && (format == 'P'));
1635
6.95k
  (void) CloseBlob(image);
1636
6.95k
  if (comment_info.comment != (char *) NULL)
1637
0
    comment_info.comment=DestroyString(comment_info.comment);
1638
6.95k
  if (quantum_info != (QuantumInfo *) NULL)
1639
0
    quantum_info=DestroyQuantumInfo(quantum_info);
1640
6.95k
  if (status == MagickFalse)
1641
0
    return(DestroyImageList(image));
1642
6.95k
  return(GetFirstImageInList(image));
1643
6.95k
}
1644

1645
/*
1646
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1647
%                                                                             %
1648
%                                                                             %
1649
%                                                                             %
1650
%   R e g i s t e r P N M I m a g e                                           %
1651
%                                                                             %
1652
%                                                                             %
1653
%                                                                             %
1654
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655
%
1656
%  RegisterPNMImage() adds properties for the PNM image format to
1657
%  the list of supported formats.  The properties include the image format
1658
%  tag, a method to read and/or write the format, whether the format
1659
%  supports the saving of more than one frame to the same file or blob,
1660
%  whether the format supports native in-memory I/O, and a brief
1661
%  description of the format.
1662
%
1663
%  The format of the RegisterPNMImage method is:
1664
%
1665
%      size_t RegisterPNMImage(void)
1666
%
1667
*/
1668
ModuleExport size_t RegisterPNMImage(void)
1669
10
{
1670
10
  MagickInfo
1671
10
    *entry;
1672
1673
10
  entry=AcquireMagickInfo("PNM","PAM","Common 2-dimensional bitmap format");
1674
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1675
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1676
10
  entry->mime_type=ConstantString("image/x-portable-anymap");
1677
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1678
10
  (void) RegisterMagickInfo(entry);
1679
10
  entry=AcquireMagickInfo("PNM","PBM",
1680
10
    "Portable bitmap format (black and white)");
1681
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1682
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1683
10
  entry->mime_type=ConstantString("image/x-portable-bitmap");
1684
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1685
10
  (void) RegisterMagickInfo(entry);
1686
10
  entry=AcquireMagickInfo("PNM","PFM","Portable float format");
1687
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1688
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1689
10
  entry->flags|=CoderEndianSupportFlag;
1690
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1691
10
  (void) RegisterMagickInfo(entry);
1692
10
  entry=AcquireMagickInfo("PNM","PGM","Portable graymap format (gray scale)");
1693
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1694
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1695
10
  entry->mime_type=ConstantString("image/x-portable-greymap");
1696
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1697
10
  (void) RegisterMagickInfo(entry);
1698
10
  entry=AcquireMagickInfo("PNM","PHM","Portable half float format");
1699
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1700
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1701
10
  entry->flags|=CoderEndianSupportFlag;
1702
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1703
10
  (void) RegisterMagickInfo(entry);
1704
10
  entry=AcquireMagickInfo("PNM","PNM","Portable anymap");
1705
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1706
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1707
10
  entry->magick=(IsImageFormatHandler *) IsPNM;
1708
10
  entry->mime_type=ConstantString("image/x-portable-pixmap");
1709
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1710
10
  (void) RegisterMagickInfo(entry);
1711
10
  entry=AcquireMagickInfo("PNM","PPM","Portable pixmap format (color)");
1712
10
  entry->decoder=(DecodeImageHandler *) ReadPNMImage;
1713
10
  entry->encoder=(EncodeImageHandler *) WritePNMImage;
1714
10
  entry->mime_type=ConstantString("image/x-portable-pixmap");
1715
10
  entry->flags|=CoderDecoderSeekableStreamFlag;
1716
10
  (void) RegisterMagickInfo(entry);
1717
10
  return(MagickImageCoderSignature);
1718
10
}
1719

1720
/*
1721
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1722
%                                                                             %
1723
%                                                                             %
1724
%                                                                             %
1725
%   U n r e g i s t e r P N M I m a g e                                       %
1726
%                                                                             %
1727
%                                                                             %
1728
%                                                                             %
1729
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730
%
1731
%  UnregisterPNMImage() removes format registrations made by the
1732
%  PNM module from the list of supported formats.
1733
%
1734
%  The format of the UnregisterPNMImage method is:
1735
%
1736
%      UnregisterPNMImage(void)
1737
%
1738
*/
1739
ModuleExport void UnregisterPNMImage(void)
1740
0
{
1741
0
  (void) UnregisterMagickInfo("PAM");
1742
0
  (void) UnregisterMagickInfo("PBM");
1743
0
  (void) UnregisterMagickInfo("PGM");
1744
0
  (void) UnregisterMagickInfo("PNM");
1745
0
  (void) UnregisterMagickInfo("PPM");
1746
0
}
1747

1748
/*
1749
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750
%                                                                             %
1751
%                                                                             %
1752
%                                                                             %
1753
%   W r i t e P N M I m a g e                                                 %
1754
%                                                                             %
1755
%                                                                             %
1756
%                                                                             %
1757
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1758
%
1759
%  WritePNMImage() writes an image to a file in the PNM rasterfile format.
1760
%
1761
%  The format of the WritePNMImage method is:
1762
%
1763
%      MagickBooleanType WritePNMImage(const ImageInfo *image_info,
1764
%        Image *image,ExceptionInfo *exception)
1765
%
1766
%  A description of each parameter follows.
1767
%
1768
%    o image_info: the image info.
1769
%
1770
%    o image:  The image.
1771
%
1772
%    o exception: return any errors or warnings in this structure.
1773
%
1774
*/
1775
static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
1776
  ExceptionInfo *exception)
1777
1.93k
{
1778
1.93k
  char
1779
1.93k
    buffer[MagickPathExtent],
1780
1.93k
    format,
1781
1.93k
    magick[MagickPathExtent];
1782
1783
1.93k
  const char
1784
1.93k
    *value;
1785
1786
1.93k
  MagickBooleanType
1787
1.93k
    status;
1788
1789
1.93k
  MagickOffsetType
1790
1.93k
    scene;
1791
1792
1.93k
  Quantum
1793
1.93k
    index;
1794
1795
1.93k
  QuantumAny
1796
1.93k
    pixel;
1797
1798
1.93k
  QuantumInfo
1799
1.93k
    *quantum_info;
1800
1801
1.93k
  QuantumType
1802
1.93k
    quantum_type;
1803
1804
1.93k
  size_t
1805
1.93k
    extent,
1806
1.93k
    number_scenes,
1807
1.93k
    packet_size;
1808
1809
1.93k
  ssize_t
1810
1.93k
    count,
1811
1.93k
    y;
1812
1813
1.93k
  unsigned char
1814
1.93k
    *q;
1815
1816
  /*
1817
    Open output image file.
1818
  */
1819
1.93k
  assert(image_info != (const ImageInfo *) NULL);
1820
1.93k
  assert(image_info->signature == MagickCoreSignature);
1821
1.93k
  assert(image != (Image *) NULL);
1822
1.93k
  assert(image->signature == MagickCoreSignature);
1823
1.93k
  assert(exception != (ExceptionInfo *) NULL);
1824
1.93k
  assert(exception->signature == MagickCoreSignature);
1825
1.93k
  if (IsEventLogging() != MagickFalse)
1826
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1827
1.93k
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1828
1.93k
  if (status == MagickFalse)
1829
0
    return(status);
1830
1.93k
  scene=0;
1831
1.93k
  number_scenes=GetImageListLength(image);
1832
1.93k
  do
1833
1.93k
  {
1834
1.93k
    QuantumAny
1835
1.93k
      max_value;
1836
1837
    /*
1838
      Write PNM file header.
1839
    */
1840
1.93k
    packet_size=3;
1841
1.93k
    quantum_type=RGBQuantum;
1842
1.93k
    (void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
1843
1.93k
    max_value=GetQuantumRange(image->depth);
1844
1.93k
    switch (magick[1])
1845
1.93k
    {
1846
0
      case 'A':
1847
0
      case 'a':
1848
0
      {
1849
0
        format='7';
1850
0
        break;
1851
0
      }
1852
0
      case 'B':
1853
0
      case 'b':
1854
0
      {
1855
0
        format='4';
1856
0
        if (image_info->compression == NoCompression)
1857
0
          format='1';
1858
0
        break;
1859
0
      }
1860
0
      case 'F':
1861
0
      case 'f':
1862
0
      {
1863
0
        format='F';
1864
0
        if (image_info->type == TrueColorType)
1865
0
          break;
1866
0
        if (IdentifyImageCoderGray(image,exception) != MagickFalse)
1867
0
          format='f';
1868
0
        break;
1869
0
      }
1870
0
      case 'G':
1871
0
      case 'g':
1872
0
      {
1873
0
        format='5';
1874
0
        if (image_info->compression == NoCompression)
1875
0
          format='2';
1876
0
        break;
1877
0
      }
1878
0
      case 'H':
1879
0
      case 'h':
1880
0
      {
1881
0
        format='H';
1882
0
        if (image_info->type == TrueColorType)
1883
0
          break;
1884
0
        if (IdentifyImageCoderGray(image,exception) != MagickFalse)
1885
0
          format='h';
1886
0
        break;
1887
0
      }
1888
1.93k
      case 'N':
1889
1.93k
      case 'n':
1890
1.93k
      {
1891
1.93k
        ImageType
1892
1.93k
          type;
1893
1894
1.93k
        format='6';
1895
1.93k
        if (image_info->type == TrueColorType)
1896
0
          break;
1897
1.93k
        type=IdentifyImageCoderGrayType(image,exception);
1898
1.93k
        if (IsGrayImageType(type) != MagickFalse)
1899
1.12k
          {
1900
1.12k
            format='5';
1901
1.12k
            if (image_info->compression == NoCompression)
1902
0
              format='2';
1903
1.12k
            if (type == BilevelType)
1904
556
              {
1905
556
                format='4';
1906
556
                if (image_info->compression == NoCompression)
1907
0
                  format='1';
1908
556
              }
1909
1.12k
          }
1910
1.93k
        break;
1911
1.93k
      }
1912
0
      default:
1913
0
      {
1914
0
        format='6';
1915
0
        if (image_info->compression == NoCompression)
1916
0
          format='3';
1917
0
        break;
1918
1.93k
      }
1919
1.93k
    }
1920
1.93k
    (void) FormatLocaleString(buffer,MagickPathExtent,"P%c\n",format);
1921
1.93k
    (void) WriteBlobString(image,buffer);
1922
1.93k
    value=GetImageProperty(image,"comment",exception);
1923
1.93k
    if (value != (const char *) NULL)
1924
128
      {
1925
128
        const char
1926
128
          *p;
1927
1928
        /*
1929
          Write comments to file.
1930
        */
1931
128
        (void) WriteBlobByte(image,'#');
1932
93.0k
        for (p=value; *p != '\0'; p++)
1933
92.9k
        {
1934
92.9k
          (void) WriteBlobByte(image,(unsigned char) *p);
1935
92.9k
          if ((*p == '\n') || (*p == '\r'))
1936
936
            (void) WriteBlobByte(image,'#');
1937
92.9k
        }
1938
128
        (void) WriteBlobByte(image,'\n');
1939
128
      }
1940
1.93k
    if (format != '7')
1941
1.93k
      {
1942
1.93k
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",
1943
1.93k
          (double) image->columns,(double) image->rows);
1944
1.93k
        (void) WriteBlobString(image,buffer);
1945
1.93k
      }
1946
0
    else
1947
0
      {
1948
0
        char
1949
0
          type[MagickPathExtent];
1950
1951
        /*
1952
          PAM header.
1953
        */
1954
0
        (void) FormatLocaleString(buffer,MagickPathExtent,
1955
0
          "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
1956
0
          image->rows);
1957
0
        (void) WriteBlobString(image,buffer);
1958
0
        quantum_type=GetQuantumType(image,exception);
1959
0
        switch (quantum_type)
1960
0
        {
1961
0
          case CMYKQuantum:
1962
0
          case CMYKAQuantum:
1963
0
          {
1964
0
            packet_size=4;
1965
0
            (void) CopyMagickString(type,"CMYK",MagickPathExtent);
1966
0
            break;
1967
0
          }
1968
0
          case GrayQuantum:
1969
0
          case GrayAlphaQuantum:
1970
0
          {
1971
0
            packet_size=1;
1972
0
            (void) CopyMagickString(type,"GRAYSCALE",MagickPathExtent);
1973
0
            if (GetQuantumRange(image->depth) == 1)
1974
0
              (void) CopyMagickString(type,"BLACKANDWHITE",MagickPathExtent);
1975
0
            break;
1976
0
          }
1977
0
          default:
1978
0
          {
1979
0
            quantum_type=RGBQuantum;
1980
0
            if (image->alpha_trait != UndefinedPixelTrait)
1981
0
              quantum_type=RGBAQuantum;
1982
0
            packet_size=3;
1983
0
            (void) CopyMagickString(type,"RGB",MagickPathExtent);
1984
0
            break;
1985
0
          }
1986
0
        }
1987
0
        if (image->alpha_trait != UndefinedPixelTrait)
1988
0
          {
1989
0
            packet_size++;
1990
0
            (void) ConcatenateMagickString(type,"_ALPHA",MagickPathExtent);
1991
0
          }
1992
0
        if (image->depth > 32)
1993
0
          image->depth=32;
1994
0
        (void) FormatLocaleString(buffer,MagickPathExtent,
1995
0
          "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
1996
0
          ((MagickOffsetType) GetQuantumRange(image->depth)));
1997
0
        (void) WriteBlobString(image,buffer);
1998
0
        (void) FormatLocaleString(buffer,MagickPathExtent,
1999
0
          "TUPLTYPE %s\nENDHDR\n",type);
2000
0
        (void) WriteBlobString(image,buffer);
2001
0
      }
2002
    /*
2003
      Convert runextent encoded to PNM raster pixels.
2004
    */
2005
1.93k
    switch (format)
2006
1.93k
    {
2007
0
      case '1':
2008
0
      {
2009
0
        unsigned char
2010
0
          pixels[2048];
2011
2012
        /*
2013
          Convert image to a PBM image.
2014
        */
2015
0
        (void) SetImageType(image,BilevelType,exception);
2016
0
        q=pixels;
2017
0
        for (y=0; y < (ssize_t) image->rows; y++)
2018
0
        {
2019
0
          const Quantum
2020
0
            *magick_restrict p;
2021
2022
0
          ssize_t
2023
0
            x;
2024
2025
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2026
0
          if (p == (const Quantum *) NULL)
2027
0
            break;
2028
0
          for (x=0; x < (ssize_t) image->columns; x++)
2029
0
          {
2030
0
            *q++=(unsigned char) (GetPixelLuma(image,p) >= ((double)
2031
0
              QuantumRange/2.0) ? '0' : '1');
2032
0
            if ((q-pixels+2) >= (ssize_t) sizeof(pixels))
2033
0
              {
2034
0
                *q++='\n';
2035
0
                (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2036
0
                q=pixels;
2037
0
              }
2038
0
            *q++=' ';
2039
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2040
0
          }
2041
0
          *q++='\n';
2042
0
          (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2043
0
          q=pixels;
2044
0
          if (image->previous == (Image *) NULL)
2045
0
            {
2046
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2047
0
                image->rows);
2048
0
              if (status == MagickFalse)
2049
0
                break;
2050
0
            }
2051
0
        }
2052
0
        if (q != pixels)
2053
0
          {
2054
0
            *q++='\n';
2055
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2056
0
          }
2057
0
        break;
2058
0
      }
2059
0
      case '2':
2060
0
      {
2061
0
        unsigned char
2062
0
          pixels[2048];
2063
2064
        /*
2065
          Convert image to a PGM image.
2066
        */
2067
0
        if (image->depth <= 8)
2068
0
          (void) WriteBlobString(image,"255\n");
2069
0
        else
2070
0
          if (image->depth <= 16)
2071
0
            (void) WriteBlobString(image,"65535\n");
2072
0
          else
2073
0
            (void) WriteBlobString(image,"4294967295\n");
2074
0
        q=pixels;
2075
0
        for (y=0; y < (ssize_t) image->rows; y++)
2076
0
        {
2077
0
          const Quantum
2078
0
            *magick_restrict p;
2079
2080
0
          ssize_t
2081
0
            x;
2082
2083
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2084
0
          if (p == (const Quantum *) NULL)
2085
0
            break;
2086
0
          for (x=0; x < (ssize_t) image->columns; x++)
2087
0
          {
2088
0
            index=ClampToQuantum(GetPixelLuma(image,p));
2089
0
            if (image->depth <= 8)
2090
0
              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,"%u ",
2091
0
                ScaleQuantumToChar(index));
2092
0
            else
2093
0
              if (image->depth <= 16)
2094
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2095
0
                  "%u ",ScaleQuantumToShort(index));
2096
0
              else
2097
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2098
0
                  "%u ",ScaleQuantumToLong(index));
2099
0
            extent=(size_t) count;
2100
0
            if ((size_t) (q-pixels+(ssize_t) extent+1) >= sizeof(pixels))
2101
0
              {
2102
0
                *q++='\n';
2103
0
                (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2104
0
                q=pixels;
2105
0
              }
2106
0
            (void) memcpy((char *) q,buffer,extent);
2107
0
            q+=(ptrdiff_t) extent;
2108
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2109
0
          }
2110
0
          *q++='\n';
2111
0
          (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2112
0
          q=pixels;
2113
0
          if (image->previous == (Image *) NULL)
2114
0
            {
2115
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2116
0
                image->rows);
2117
0
              if (status == MagickFalse)
2118
0
                break;
2119
0
            }
2120
0
        }
2121
0
        if (q != pixels)
2122
0
          {
2123
0
            *q++='\n';
2124
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2125
0
          }
2126
0
        break;
2127
0
      }
2128
0
      case '3':
2129
0
      {
2130
0
        unsigned char
2131
0
          pixels[2048];
2132
2133
        /*
2134
          Convert image to a PNM image.
2135
        */
2136
0
        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2137
0
          (void) TransformImageColorspace(image,sRGBColorspace,exception);
2138
0
        if (image->depth <= 8)
2139
0
          (void) WriteBlobString(image,"255\n");
2140
0
        else
2141
0
          if (image->depth <= 16)
2142
0
            (void) WriteBlobString(image,"65535\n");
2143
0
          else
2144
0
            (void) WriteBlobString(image,"4294967295\n");
2145
0
        q=pixels;
2146
0
        for (y=0; y < (ssize_t) image->rows; y++)
2147
0
        {
2148
0
          const Quantum
2149
0
            *magick_restrict p;
2150
2151
0
          ssize_t
2152
0
            x;
2153
2154
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2155
0
          if (p == (const Quantum *) NULL)
2156
0
            break;
2157
0
          for (x=0; x < (ssize_t) image->columns; x++)
2158
0
          {
2159
0
            if (image->depth <= 8)
2160
0
              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2161
0
                "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
2162
0
                ScaleQuantumToChar(GetPixelGreen(image,p)),
2163
0
                ScaleQuantumToChar(GetPixelBlue(image,p)));
2164
0
            else
2165
0
              if (image->depth <= 16)
2166
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2167
0
                  "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
2168
0
                  ScaleQuantumToShort(GetPixelGreen(image,p)),
2169
0
                  ScaleQuantumToShort(GetPixelBlue(image,p)));
2170
0
              else
2171
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2172
0
                  "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
2173
0
                  ScaleQuantumToLong(GetPixelGreen(image,p)),
2174
0
                  ScaleQuantumToLong(GetPixelBlue(image,p)));
2175
0
            extent=(size_t) count;
2176
0
            if ((size_t) (q-pixels+(ssize_t) extent+2) >= sizeof(pixels))
2177
0
              {
2178
0
                *q++='\n';
2179
0
                (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2180
0
                q=pixels;
2181
0
              }
2182
0
            (void) memcpy((char *) q,buffer,extent);
2183
0
            q+=(ptrdiff_t) extent;
2184
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2185
0
          }
2186
0
          *q++='\n';
2187
0
          (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2188
0
          q=pixels;
2189
0
          if (image->previous == (Image *) NULL)
2190
0
            {
2191
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2192
0
                image->rows);
2193
0
              if (status == MagickFalse)
2194
0
                break;
2195
0
            }
2196
0
        }
2197
0
        if (q != pixels)
2198
0
          {
2199
0
            *q++='\n';
2200
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2201
0
          }
2202
0
        break;
2203
0
      }
2204
556
      case '4':
2205
556
      {
2206
556
        unsigned char
2207
556
          *pixels;
2208
2209
        /*
2210
          Convert image to a PBM image.
2211
        */
2212
556
        (void) SetImageType(image,BilevelType,exception);
2213
556
        image->depth=1;
2214
556
        quantum_info=AcquireQuantumInfo(image_info,image);
2215
556
        if (quantum_info == (QuantumInfo *) NULL)
2216
556
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2217
556
        SetQuantumMinIsWhite(quantum_info,MagickTrue);
2218
556
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2219
556
        pixels=GetQuantumPixels(quantum_info);
2220
17.8k
        for (y=0; y < (ssize_t) image->rows; y++)
2221
17.2k
        {
2222
17.2k
          const Quantum
2223
17.2k
            *magick_restrict p;
2224
2225
17.2k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2226
17.2k
          if (p == (const Quantum *) NULL)
2227
0
            break;
2228
17.2k
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2229
17.2k
            GrayQuantum,pixels,exception);
2230
17.2k
          count=WriteBlob(image,extent,pixels);
2231
17.2k
          if (count != (ssize_t) extent)
2232
0
            break;
2233
17.2k
          if (image->previous == (Image *) NULL)
2234
17.2k
            {
2235
17.2k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2236
17.2k
                image->rows);
2237
17.2k
              if (status == MagickFalse)
2238
0
                break;
2239
17.2k
            }
2240
17.2k
        }
2241
556
        quantum_info=DestroyQuantumInfo(quantum_info);
2242
556
        break;
2243
556
      }
2244
565
      case '5':
2245
565
      {
2246
565
        unsigned char
2247
565
          *pixels;
2248
2249
        /*
2250
          Convert image to a PGM image.
2251
        */
2252
565
        if (image->depth > 32)
2253
0
          image->depth=32;
2254
565
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2255
565
          ((MagickOffsetType) GetQuantumRange(image->depth)));
2256
565
        (void) WriteBlobString(image,buffer);
2257
565
        quantum_info=AcquireQuantumInfo(image_info,image);
2258
565
        if (quantum_info == (QuantumInfo *) NULL)
2259
565
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2260
565
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2261
565
        pixels=GetQuantumPixels(quantum_info);
2262
565
        extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
2263
11.1k
        for (y=0; y < (ssize_t) image->rows; y++)
2264
10.6k
        {
2265
10.6k
          const Quantum
2266
10.6k
            *magick_restrict p;
2267
2268
10.6k
          ssize_t
2269
10.6k
            x;
2270
2271
10.6k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2272
10.6k
          if (p == (const Quantum *) NULL)
2273
0
            break;
2274
10.6k
          q=pixels;
2275
10.6k
          switch (image->depth)
2276
10.6k
          {
2277
577
            case 8:
2278
1.20k
            case 16:
2279
1.83k
            case 32:
2280
1.83k
            {
2281
1.83k
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2282
1.83k
                GrayQuantum,pixels,exception);
2283
1.83k
              break;
2284
1.20k
            }
2285
8.79k
            default:
2286
8.79k
            {
2287
8.79k
              if (image->depth <= 8)
2288
6.18k
                {
2289
16.8k
                  for (x=0; x < (ssize_t) image->columns; x++)
2290
10.6k
                  {
2291
10.6k
                    if (IsPixelGray(image,p) == MagickFalse)
2292
0
                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2293
0
                        image,p)),max_value);
2294
10.6k
                    else
2295
10.6k
                      {
2296
10.6k
                        if (image->depth == 8)
2297
0
                          pixel=ScaleQuantumToChar(GetPixelRed(image,p));
2298
10.6k
                        else
2299
10.6k
                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2300
10.6k
                            max_value);
2301
10.6k
                      }
2302
10.6k
                    q=PopCharPixel((unsigned char) pixel,q);
2303
10.6k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2304
10.6k
                  }
2305
6.18k
                  extent=(size_t) (q-pixels);
2306
6.18k
                  break;
2307
6.18k
                }
2308
2.60k
              if (image->depth <= 16)
2309
1.45k
                {
2310
8.35k
                  for (x=0; x < (ssize_t) image->columns; x++)
2311
6.89k
                  {
2312
6.89k
                    if (IsPixelGray(image,p) == MagickFalse)
2313
0
                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2314
0
                        p)),max_value);
2315
6.89k
                    else
2316
6.89k
                      {
2317
6.89k
                        if (image->depth == 16)
2318
0
                          pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2319
6.89k
                        else
2320
6.89k
                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2321
6.89k
                            max_value);
2322
6.89k
                      }
2323
6.89k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2324
6.89k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2325
6.89k
                  }
2326
1.45k
                  extent=(size_t) (q-pixels);
2327
1.45k
                  break;
2328
1.45k
                }
2329
9.10k
              for (x=0; x < (ssize_t) image->columns; x++)
2330
7.95k
              {
2331
7.95k
                if (IsPixelGray(image,p) == MagickFalse)
2332
0
                  pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2333
0
                    max_value);
2334
7.95k
                else
2335
7.95k
                  {
2336
7.95k
                    if (image->depth == 16)
2337
0
                      pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2338
7.95k
                    else
2339
7.95k
                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2340
7.95k
                  }
2341
7.95k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2342
7.95k
                p+=(ptrdiff_t) GetPixelChannels(image);
2343
7.95k
              }
2344
1.15k
              extent=(size_t) (q-pixels);
2345
1.15k
              break;
2346
2.60k
            }
2347
10.6k
          }
2348
10.6k
          count=WriteBlob(image,extent,pixels);
2349
10.6k
          if (count != (ssize_t) extent)
2350
0
            break;
2351
10.6k
          if (image->previous == (Image *) NULL)
2352
10.6k
            {
2353
10.6k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2354
10.6k
                image->rows);
2355
10.6k
              if (status == MagickFalse)
2356
0
                break;
2357
10.6k
            }
2358
10.6k
        }
2359
565
        quantum_info=DestroyQuantumInfo(quantum_info);
2360
565
        break;
2361
565
      }
2362
811
      case '6':
2363
811
      {
2364
811
        unsigned char
2365
811
          *pixels;
2366
2367
        /*
2368
          Convert image to a PNM image.
2369
        */
2370
811
        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2371
237
          (void) TransformImageColorspace(image,sRGBColorspace,exception);
2372
811
        if (image->depth > 32)
2373
0
          image->depth=32;
2374
811
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2375
811
          ((MagickOffsetType) GetQuantumRange(image->depth)));
2376
811
        (void) WriteBlobString(image,buffer);
2377
811
        quantum_info=AcquireQuantumInfo(image_info,image);
2378
811
        if (quantum_info == (QuantumInfo *) NULL)
2379
811
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2380
811
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2381
811
        pixels=GetQuantumPixels(quantum_info);
2382
811
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
2383
13.4k
        for (y=0; y < (ssize_t) image->rows; y++)
2384
12.6k
        {
2385
12.6k
          const Quantum
2386
12.6k
            *magick_restrict p;
2387
2388
12.6k
          ssize_t
2389
12.6k
            x;
2390
2391
12.6k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2392
12.6k
          if (p == (const Quantum *) NULL)
2393
0
            break;
2394
12.6k
          q=pixels;
2395
12.6k
          switch (image->depth)
2396
12.6k
          {
2397
1.32k
            case 8:
2398
2.63k
            case 16:
2399
4.04k
            case 32:
2400
4.04k
            {
2401
4.04k
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2402
4.04k
                quantum_type,pixels,exception);
2403
4.04k
              break;
2404
2.63k
            }
2405
8.59k
            default:
2406
8.59k
            {
2407
8.59k
              if (image->depth <= 8)
2408
3.90k
                {
2409
13.7k
                  for (x=0; x < (ssize_t) image->columns; x++)
2410
9.82k
                  {
2411
9.82k
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2412
9.82k
                    q=PopCharPixel((unsigned char) pixel,q);
2413
9.82k
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2414
9.82k
                    q=PopCharPixel((unsigned char) pixel,q);
2415
9.82k
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2416
9.82k
                    q=PopCharPixel((unsigned char) pixel,q);
2417
9.82k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2418
9.82k
                  }
2419
3.90k
                  extent=(size_t) (q-pixels);
2420
3.90k
                  break;
2421
3.90k
                }
2422
4.68k
              if (image->depth <= 16)
2423
1.81k
                {
2424
7.03k
                  for (x=0; x < (ssize_t) image->columns; x++)
2425
5.22k
                  {
2426
5.22k
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2427
5.22k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2428
5.22k
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2429
5.22k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2430
5.22k
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2431
5.22k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2432
5.22k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2433
5.22k
                  }
2434
1.81k
                  extent=(size_t) (q-pixels);
2435
1.81k
                  break;
2436
1.81k
                }
2437
8.40k
              for (x=0; x < (ssize_t) image->columns; x++)
2438
5.54k
              {
2439
5.54k
                pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2440
5.54k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2441
5.54k
                pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2442
5.54k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2443
5.54k
                pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2444
5.54k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2445
5.54k
                p+=(ptrdiff_t) GetPixelChannels(image);
2446
5.54k
              }
2447
2.86k
              extent=(size_t) (q-pixels);
2448
2.86k
              break;
2449
4.68k
            }
2450
12.6k
          }
2451
12.6k
          count=WriteBlob(image,extent,pixels);
2452
12.6k
          if (count != (ssize_t) extent)
2453
0
            break;
2454
12.6k
          if (image->previous == (Image *) NULL)
2455
12.6k
            {
2456
12.6k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2457
12.6k
                image->rows);
2458
12.6k
              if (status == MagickFalse)
2459
0
                break;
2460
12.6k
            }
2461
12.6k
        }
2462
811
        quantum_info=DestroyQuantumInfo(quantum_info);
2463
811
        break;
2464
811
      }
2465
0
      case '7':
2466
0
      {
2467
0
        unsigned char
2468
0
          *pixels;
2469
2470
        /*
2471
          Convert image to a PAM.
2472
        */
2473
0
        if (image->depth > 32)
2474
0
          image->depth=32;
2475
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2476
0
        if (quantum_info == (QuantumInfo *) NULL)
2477
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2478
0
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2479
0
        pixels=GetQuantumPixels(quantum_info);
2480
0
        for (y=0; y < (ssize_t) image->rows; y++)
2481
0
        {
2482
0
          const Quantum
2483
0
            *magick_restrict p;
2484
2485
0
          ssize_t
2486
0
            x;
2487
2488
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2489
0
          if (p == (const Quantum *) NULL)
2490
0
            break;
2491
0
          q=pixels;
2492
0
          switch (image->depth)
2493
0
          {
2494
0
            case 8:
2495
0
            case 16:
2496
0
            case 32:
2497
0
            {
2498
0
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2499
0
                quantum_type,pixels,exception);
2500
0
              break;
2501
0
            }
2502
0
            default:
2503
0
            {
2504
0
              switch (quantum_type)
2505
0
              {
2506
0
                case GrayQuantum:
2507
0
                case GrayAlphaQuantum:
2508
0
                {
2509
0
                  if (image->depth <= 8)
2510
0
                    {
2511
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2512
0
                      {
2513
0
                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2514
0
                          image,p)),max_value);
2515
0
                        q=PopCharPixel((unsigned char) pixel,q);
2516
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2517
0
                          {
2518
0
                            pixel=(unsigned char) ScaleQuantumToAny(
2519
0
                              GetPixelAlpha(image,p),max_value);
2520
0
                            q=PopCharPixel((unsigned char) pixel,q);
2521
0
                          }
2522
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2523
0
                      }
2524
0
                      break;
2525
0
                    }
2526
0
                  if (image->depth <= 16)
2527
0
                    {
2528
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2529
0
                      {
2530
0
                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2531
0
                          image,p)),max_value);
2532
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2533
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2534
0
                          {
2535
0
                            pixel=(unsigned char) ScaleQuantumToAny(
2536
0
                              GetPixelAlpha(image,p),max_value);
2537
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2538
0
                          }
2539
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2540
0
                      }
2541
0
                      break;
2542
0
                    }
2543
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2544
0
                  {
2545
0
                    pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2546
0
                      p)),max_value);
2547
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2548
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2549
0
                      {
2550
0
                        pixel=(unsigned char) ScaleQuantumToAny(
2551
0
                          GetPixelAlpha(image,p),max_value);
2552
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2553
0
                      }
2554
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2555
0
                  }
2556
0
                  break;
2557
0
                }
2558
0
                case CMYKQuantum:
2559
0
                case CMYKAQuantum:
2560
0
                {
2561
0
                  if (image->depth <= 8)
2562
0
                    {
2563
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2564
0
                      {
2565
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2566
0
                        q=PopCharPixel((unsigned char) pixel,q);
2567
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2568
0
                          max_value);
2569
0
                        q=PopCharPixel((unsigned char) pixel,q);
2570
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2571
0
                          max_value);
2572
0
                        q=PopCharPixel((unsigned char) pixel,q);
2573
0
                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2574
0
                          max_value);
2575
0
                        q=PopCharPixel((unsigned char) pixel,q);
2576
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2577
0
                          {
2578
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2579
0
                              max_value);
2580
0
                            q=PopCharPixel((unsigned char) pixel,q);
2581
0
                          }
2582
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2583
0
                      }
2584
0
                      break;
2585
0
                    }
2586
0
                  if (image->depth <= 16)
2587
0
                    {
2588
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2589
0
                      {
2590
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2591
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2592
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2593
0
                          max_value);
2594
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2595
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2596
0
                          max_value);
2597
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2598
0
                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2599
0
                          max_value);
2600
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2601
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2602
0
                          {
2603
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2604
0
                              max_value);
2605
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2606
0
                          }
2607
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2608
0
                      }
2609
0
                      break;
2610
0
                    }
2611
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2612
0
                  {
2613
0
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2614
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2615
0
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2616
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2617
0
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2618
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2619
0
                    pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2620
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2621
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2622
0
                      {
2623
0
                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2624
0
                          max_value);
2625
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2626
0
                      }
2627
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2628
0
                  }
2629
0
                  break;
2630
0
                }
2631
0
                default:
2632
0
                {
2633
0
                  if (image->depth <= 8)
2634
0
                    {
2635
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2636
0
                      {
2637
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2638
0
                        q=PopCharPixel((unsigned char) pixel,q);
2639
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2640
0
                          max_value);
2641
0
                        q=PopCharPixel((unsigned char) pixel,q);
2642
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2643
0
                          max_value);
2644
0
                        q=PopCharPixel((unsigned char) pixel,q);
2645
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2646
0
                          {
2647
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2648
0
                              max_value);
2649
0
                            q=PopCharPixel((unsigned char) pixel,q);
2650
0
                          }
2651
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2652
0
                      }
2653
0
                      break;
2654
0
                    }
2655
0
                  if (image->depth <= 16)
2656
0
                    {
2657
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2658
0
                      {
2659
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2660
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2661
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2662
0
                          max_value);
2663
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2664
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2665
0
                          max_value);
2666
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2667
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2668
0
                          {
2669
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2670
0
                              max_value);
2671
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2672
0
                          }
2673
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2674
0
                      }
2675
0
                      break;
2676
0
                    }
2677
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2678
0
                  {
2679
0
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2680
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2681
0
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2682
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2683
0
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2684
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2685
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2686
0
                      {
2687
0
                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2688
0
                          max_value);
2689
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2690
0
                      }
2691
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2692
0
                  }
2693
0
                  break;
2694
0
                }
2695
0
              }
2696
0
              extent=(size_t) (q-pixels);
2697
0
              break;
2698
0
            }
2699
0
          }
2700
0
          count=WriteBlob(image,extent,pixels);
2701
0
          if (count != (ssize_t) extent)
2702
0
            break;
2703
0
          if (image->previous == (Image *) NULL)
2704
0
            {
2705
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2706
0
                image->rows);
2707
0
              if (status == MagickFalse)
2708
0
                break;
2709
0
            }
2710
0
        }
2711
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2712
0
        break;
2713
0
      }
2714
0
      case 'F':
2715
0
      case 'f':
2716
0
      {
2717
0
        unsigned char
2718
0
          *pixels;
2719
2720
0
        (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2721
0
          "1.0\n");
2722
0
        image->depth=32;
2723
0
        quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2724
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2725
0
        if (quantum_info == (QuantumInfo *) NULL)
2726
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2727
0
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2728
0
        if (status == MagickFalse)
2729
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2730
0
        pixels=GetQuantumPixels(quantum_info);
2731
0
        for (y=(ssize_t) image->rows-1; y >= 0; y--)
2732
0
        {
2733
0
          const Quantum
2734
0
            *magick_restrict p;
2735
2736
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2737
0
          if (p == (const Quantum *) NULL)
2738
0
            break;
2739
0
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2740
0
            quantum_type,pixels,exception);
2741
0
          (void) WriteBlob(image,extent,pixels);
2742
0
          if (image->previous == (Image *) NULL)
2743
0
            {
2744
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2745
0
                image->rows);
2746
0
              if (status == MagickFalse)
2747
0
                break;
2748
0
            }
2749
0
        }
2750
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2751
0
        break;
2752
0
      }
2753
0
      case 'H':
2754
0
      case 'h':
2755
0
      {
2756
0
        unsigned char
2757
0
          *pixels;
2758
2759
0
        (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2760
0
          "1.0\n");
2761
0
        image->depth=16;
2762
0
        quantum_type=format == 'h' ? GrayQuantum : RGBQuantum;
2763
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2764
0
        if (quantum_info == (QuantumInfo *) NULL)
2765
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2766
0
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2767
0
        if (status == MagickFalse)
2768
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2769
0
        pixels=GetQuantumPixels(quantum_info);
2770
0
        for (y=(ssize_t) image->rows-1; y >= 0; y--)
2771
0
        {
2772
0
          const Quantum
2773
0
            *magick_restrict p;
2774
2775
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2776
0
          if (p == (const Quantum *) NULL)
2777
0
            break;
2778
0
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2779
0
            quantum_type,pixels,exception);
2780
0
          (void) WriteBlob(image,extent,pixels);
2781
0
          if (image->previous == (Image *) NULL)
2782
0
            {
2783
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2784
0
                image->rows);
2785
0
              if (status == MagickFalse)
2786
0
                break;
2787
0
            }
2788
0
        }
2789
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2790
0
        break;
2791
0
      }
2792
0
      default:
2793
0
        break;
2794
1.93k
    }
2795
1.93k
    if (GetNextImageInList(image) == (Image *) NULL)
2796
1.93k
      break;
2797
0
    image=SyncNextImageInList(image);
2798
0
    status=SetImageProgress(image,SaveImagesTag,scene++,number_scenes);
2799
0
    if (status == MagickFalse)
2800
0
      break;
2801
0
  } while (image_info->adjoin != MagickFalse);
2802
1.93k
  if (CloseBlob(image) == MagickFalse)
2803
0
    status=MagickFalse;
2804
1.93k
  return(status);
2805
1.93k
}