Coverage Report

Created: 2025-08-12 07:37

/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.43k
{
156
4.43k
  int
157
4.43k
    c;
158
159
4.43k
  char
160
4.43k
    *p;
161
162
  /*
163
    Read comment.
164
  */
165
4.43k
  (void) exception;
166
4.43k
  p=comment_info->comment+strlen(comment_info->comment);
167
145k
  for (c='#'; (c != EOF) && (c != (int) '\n') && (c != (int) '\r'); p++)
168
140k
  {
169
140k
    if ((size_t) (p-comment_info->comment+1) >= comment_info->extent)
170
9
      {
171
9
        comment_info->extent<<=1;
172
9
        comment_info->comment=(char *) ResizeQuantumMemory(
173
9
          comment_info->comment,comment_info->extent,
174
9
          sizeof(*comment_info->comment));
175
9
        if (comment_info->comment == (char *) NULL)
176
0
          return(-1);
177
9
        p=comment_info->comment+strlen(comment_info->comment);
178
9
      }
179
140k
    c=ReadBlobByte(image);
180
140k
    if (c != EOF)
181
140k
      {
182
140k
        *p=(char) c;
183
140k
        *(p+1)='\0';
184
140k
      }
185
140k
  }
186
4.43k
  return(c);
187
4.43k
}
188
189
static unsigned int PNMInteger(Image *image,CommentInfo *comment_info,
190
  const unsigned int base,ExceptionInfo *exception)
191
178k
{
192
178k
  int
193
178k
    c;
194
195
178k
  unsigned int
196
178k
    value;
197
198
  /*
199
    Skip any leading whitespace.
200
  */
201
178k
  do
202
191k
  {
203
191k
    c=ReadBlobByte(image);
204
191k
    if (c == EOF)
205
2.66k
      return(0);
206
189k
    if (c == (int) '#')
207
1.74k
      c=PNMComment(image,comment_info,exception);
208
189k
  } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
209
175k
  if (base == 2)
210
62.6k
    return((unsigned int) (c-(int) '0'));
211
  /*
212
    Evaluate number.
213
  */
214
112k
  value=0;
215
188k
  while (isdigit((int) ((unsigned char) c)) != 0)
216
75.3k
  {
217
75.3k
    if (value <= (unsigned int) (INT_MAX/10))
218
74.0k
      {
219
74.0k
        value*=10;
220
74.0k
        if (value <= (unsigned int) (INT_MAX-(c-(int) '0')))
221
73.5k
          value+=(unsigned int) (c-(int) '0');
222
74.0k
      }
223
75.3k
    c=ReadBlobByte(image);
224
75.3k
    if (c == EOF)
225
142
      return(0);
226
75.3k
  }
227
112k
  if (c == (int) '#')
228
908
    c=PNMComment(image,comment_info,exception);
229
112k
  return(value);
230
112k
}
231
232
static char *PNMString(Image *image,char *string,const size_t extent)
233
3.17k
{
234
3.17k
  int
235
3.17k
    c;
236
237
3.17k
  size_t
238
3.17k
    i;
239
240
29.5k
  for (i=0; i < (extent-1L); i++)
241
29.1k
  {
242
29.1k
    c=ReadBlobByte(image);
243
29.1k
    if (c == EOF)
244
459
      {
245
459
        if (i == 0)
246
144
          return((char *) NULL);
247
315
        break;
248
459
      }
249
28.6k
    string[i]=(char) c;
250
28.6k
    if (c == '\n' || c == '\r')
251
2.26k
      break;
252
28.6k
  }
253
3.03k
  string[i]='\0';
254
3.03k
  return(string);
255
3.17k
}
256
257
static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
258
16.9k
{
259
16.9k
#define ThrowPNMException(exception,message) \
260
6
{ \
261
10.4k
  if (comment_info.comment != (char *) NULL)  \
262
2.61k
    comment_info.comment=DestroyString(comment_info.comment); \
263
10.4k
  if (quantum_info != (QuantumInfo *) NULL) \
264
6
    quantum_info=DestroyQuantumInfo(quantum_info); \
265
10.4k
  ThrowReaderException((exception),(message)); \
266
0
}
267
268
16.9k
  char
269
16.9k
    format;
270
271
16.9k
  ColorspaceType
272
16.9k
    colorspace;
273
274
16.9k
  CommentInfo
275
16.9k
    comment_info;
276
277
16.9k
  const void
278
16.9k
    *stream;
279
280
16.9k
  double
281
16.9k
    quantum_scale;
282
283
16.9k
  Image
284
16.9k
    *image;
285
286
16.9k
  MagickBooleanType
287
16.9k
    is_gray = MagickTrue,
288
16.9k
    is_mono = MagickTrue,
289
16.9k
    status;
290
291
16.9k
  QuantumAny
292
16.9k
    max_value;
293
294
16.9k
  QuantumInfo
295
16.9k
    *quantum_info;
296
297
16.9k
  QuantumType
298
16.9k
    quantum_type;
299
300
16.9k
  size_t
301
16.9k
    depth,
302
16.9k
    extent;
303
304
16.9k
  ssize_t
305
16.9k
    count,
306
16.9k
    row,
307
16.9k
    y;
308
309
16.9k
  unsigned char
310
16.9k
    *pixels;
311
312
  /*
313
    Open image file.
314
  */
315
16.9k
  assert(image_info != (const ImageInfo *) NULL);
316
16.9k
  assert(image_info->signature == MagickCoreSignature);
317
16.9k
  assert(exception != (ExceptionInfo *) NULL);
318
16.9k
  assert(exception->signature == MagickCoreSignature);
319
16.9k
  if (IsEventLogging() != MagickFalse)
320
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
321
0
      image_info->filename);
322
16.9k
  image=AcquireImage(image_info,exception);
323
16.9k
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
324
16.9k
  if (status == MagickFalse)
325
4
    {
326
4
      image=DestroyImageList(image);
327
4
      return((Image *) NULL);
328
4
    }
329
  /*
330
    Read PNM image.
331
  */
332
16.9k
  comment_info.comment=(char *) NULL;
333
16.9k
  quantum_info=(QuantumInfo *) NULL;
334
16.9k
  count=ReadBlob(image,1,(unsigned char *) &format);
335
16.9k
  do
336
19.4k
  {
337
    /*
338
      Initialize image structure.
339
    */
340
19.4k
    comment_info.comment=AcquireString(NULL);
341
19.4k
    comment_info.extent=MagickPathExtent;
342
19.4k
    if ((count != 1) || (format != 'P'))
343
19.3k
      ThrowPNMException(CorruptImageError,"ImproperImageHeader");
344
19.3k
    max_value=1;
345
19.3k
    colorspace=UndefinedColorspace;
346
19.3k
    quantum_type=UndefinedQuantum;
347
19.3k
    quantum_scale=1.0;
348
19.3k
    format=(char) ReadBlobByte(image);
349
19.3k
    if (format != '7')
350
15.0k
      {
351
        /*
352
          PBM, PGM, PPM, and PNM.
353
        */
354
15.0k
        if (ReadBlobByte(image) == '4')
355
1.45k
          image->alpha_trait=BlendPixelTrait;
356
15.0k
        image->columns=(size_t) PNMInteger(image,&comment_info,10,exception);
357
15.0k
        image->rows=(size_t) PNMInteger(image,&comment_info,10,exception);
358
15.0k
        if ((format == 'f') || (format == 'F') || (format == 'h') ||
359
15.0k
            (format == 'H'))
360
3.17k
          {
361
3.17k
            char
362
3.17k
              scale[32];
363
364
3.17k
            if (PNMString(image,scale,sizeof(scale)) != (char *) NULL)
365
3.03k
              quantum_scale=StringToDouble(scale,(char **) NULL);
366
3.17k
          }
367
11.8k
        else
368
11.8k
          {
369
11.8k
            if ((format == '1') || (format == '4'))
370
5.08k
              max_value=1;  /* bitmap */
371
6.78k
            else
372
6.78k
              max_value=(QuantumAny) PNMInteger(image,&comment_info,10,
373
6.78k
                exception);
374
11.8k
          }
375
15.0k
      }
376
4.29k
    else
377
4.29k
      {
378
4.29k
        char
379
4.29k
          keyword[MagickPathExtent],
380
4.29k
          value[MagickPathExtent];
381
382
4.29k
        int
383
4.29k
          c;
384
385
4.29k
        char
386
4.29k
          *p;
387
388
        /*
389
          PAM.
390
        */
391
69.2k
        for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
392
68.0k
        {
393
72.9k
          while (isspace((int) ((unsigned char) c)) != 0)
394
4.92k
            c=ReadBlobByte(image);
395
68.0k
          if (c == '#')
396
1.12k
            {
397
              /*
398
                Comment.
399
              */
400
2.91k
              while (c == '#')
401
1.79k
              {
402
1.79k
                c=PNMComment(image,&comment_info,exception);
403
1.79k
                c=ReadBlobByte(image);
404
1.79k
              }
405
2.46k
              while (isspace((int) ((unsigned char) c)) != 0)
406
1.34k
                c=ReadBlobByte(image);
407
1.12k
            }
408
68.0k
          p=keyword;
409
68.0k
          do
410
290k
          {
411
290k
            if ((size_t) (p-keyword) < (MagickPathExtent-1))
412
286k
              *p++=(char) c;
413
290k
            c=ReadBlobByte(image);
414
290k
          } while (isalnum((int) ((unsigned char) c)));
415
68.0k
          *p='\0';
416
68.0k
          if (LocaleCompare(keyword,"endhdr") == 0)
417
3.02k
            break;
418
86.9k
          while (isspace((int) ((unsigned char) c)) != 0)
419
21.9k
            c=ReadBlobByte(image);
420
64.9k
          p=value;
421
250k
          while (isalnum((int) ((unsigned char) c)) || (c == '_'))
422
185k
          {
423
185k
            if ((size_t) (p-value) < (MagickPathExtent-1))
424
184k
              *p++=(char) c;
425
185k
            c=ReadBlobByte(image);
426
185k
          }
427
64.9k
          *p='\0';
428
          /*
429
            Assign a value to the specified keyword.
430
          */
431
64.9k
          if (LocaleCompare(keyword,"depth") == 0)
432
406
            (void) StringToUnsignedLong(value);
433
64.9k
          if (LocaleCompare(keyword,"height") == 0)
434
4.22k
            image->rows=StringToUnsignedLong(value);
435
64.9k
          if (LocaleCompare(keyword,"maxval") == 0)
436
2.82k
            max_value=StringToUnsignedLong(value);
437
64.9k
          if ((quantum_type == UndefinedQuantum) &&
438
64.9k
              (LocaleCompare(keyword,"TUPLTYPE") == 0))
439
2.91k
            {
440
2.91k
              if (LocaleCompare(value,"BLACKANDWHITE") == 0)
441
39
                {
442
39
                  colorspace=GRAYColorspace;
443
39
                  quantum_type=GrayQuantum;
444
39
                }
445
2.91k
              if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
446
52
                {
447
52
                  colorspace=GRAYColorspace;
448
52
                  image->alpha_trait=BlendPixelTrait;
449
52
                  quantum_type=GrayAlphaQuantum;
450
52
                }
451
2.91k
              if (LocaleCompare(value,"GRAYSCALE") == 0)
452
389
                {
453
389
                  colorspace=GRAYColorspace;
454
389
                  quantum_type=GrayQuantum;
455
389
                }
456
2.91k
              if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
457
413
                {
458
413
                  colorspace=GRAYColorspace;
459
413
                  image->alpha_trait=BlendPixelTrait;
460
413
                  quantum_type=GrayAlphaQuantum;
461
413
                }
462
2.91k
              if (LocaleCompare(value,"RGB_ALPHA") == 0)
463
373
                {
464
373
                  image->alpha_trait=BlendPixelTrait;
465
373
                  quantum_type=RGBAQuantum;
466
373
                }
467
2.91k
              if (LocaleCompare(value,"CMYK") == 0)
468
748
                {
469
748
                  colorspace=CMYKColorspace;
470
748
                  quantum_type=CMYKQuantum;
471
748
                }
472
2.91k
              if (LocaleCompare(value,"CMYK_ALPHA") == 0)
473
401
                {
474
401
                  colorspace=CMYKColorspace;
475
401
                  image->alpha_trait=BlendPixelTrait;
476
401
                  quantum_type=CMYKAQuantum;
477
401
                }
478
2.91k
            }
479
64.9k
          if (LocaleCompare(keyword,"width") == 0)
480
4.41k
            image->columns=StringToUnsignedLong(value);
481
64.9k
        }
482
4.29k
      }
483
19.3k
    if (quantum_type == UndefinedQuantum)
484
16.9k
      quantum_type=RGBQuantum;
485
19.3k
    if ((image->columns == 0) || (image->rows == 0))
486
17.3k
      ThrowPNMException(CorruptImageError,"NegativeOrZeroImageSize");
487
17.3k
    if ((max_value == 0) || (max_value > 4294967295UL))
488
17.1k
      ThrowPNMException(CorruptImageError,"ImproperImageHeader");
489
108k
    for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
490
17.1k
    image->depth=depth;
491
17.1k
    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
17.1k
    if ((MagickSizeType) (image->columns*image->rows/8) > GetBlobSize(image))
495
16.8k
      ThrowPNMException(CorruptImageError,"InsufficientImageDataInFile");
496
16.8k
    status=SetImageExtent(image,image->columns,image->rows,exception);
497
16.8k
    if (status == MagickFalse)
498
286
      {
499
286
        if (comment_info.comment != (char *) NULL)
500
286
          comment_info.comment=DestroyString(comment_info.comment);
501
286
        if (quantum_info != (QuantumInfo *) NULL)
502
0
          quantum_info=DestroyQuantumInfo(quantum_info);
503
286
        return(DestroyImageList(image));
504
286
      }
505
16.5k
    if (colorspace != UndefinedColorspace)
506
1.98k
      (void) SetImageColorspace(image,colorspace,exception);
507
16.5k
    (void) ResetImagePixels(image,exception);
508
    /*
509
      Convert PNM pixels to runextent-encoded MIFF packets.
510
    */
511
16.5k
    row=0;
512
16.5k
    y=0;
513
16.5k
    switch (format)
514
16.5k
    {
515
807
      case '1':
516
807
      {
517
        /*
518
          Convert PBM image to pixel packets.
519
        */
520
807
        (void) SetImageColorspace(image,GRAYColorspace,exception);
521
23.2k
        for (y=0; y < (ssize_t) image->rows; y++)
522
22.6k
        {
523
22.6k
          ssize_t
524
22.6k
            x;
525
526
22.6k
          Quantum
527
22.6k
            *magick_restrict q;
528
529
22.6k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
530
22.6k
          if (q == (Quantum *) NULL)
531
0
            break;
532
85.2k
          for (x=0; x < (ssize_t) image->columns; x++)
533
62.8k
          {
534
62.8k
            SetPixelGray(image,PNMInteger(image,&comment_info,2,exception) ==
535
62.8k
              0 ? QuantumRange : 0,q);
536
62.8k
            if (EOFBlob(image) != MagickFalse)
537
245
              break;
538
62.6k
            q+=(ptrdiff_t) GetPixelChannels(image);
539
62.6k
          }
540
22.6k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
541
0
            break;
542
22.6k
          if (image->previous == (Image *) NULL)
543
21.8k
            {
544
21.8k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
545
21.8k
                image->rows);
546
21.8k
              if (status == MagickFalse)
547
0
                break;
548
21.8k
            }
549
22.6k
          if (EOFBlob(image) != MagickFalse)
550
245
            break;
551
22.6k
        }
552
807
        image->type=BilevelType;
553
807
        break;
554
0
      }
555
314
      case '2':
556
314
      {
557
314
        Quantum
558
314
          intensity;
559
560
        /*
561
          Convert PGM image to pixel packets.
562
        */
563
314
        (void) SetImageColorspace(image,GRAYColorspace,exception);
564
4.87k
        for (y=0; y < (ssize_t) image->rows; y++)
565
4.72k
        {
566
4.72k
          ssize_t
567
4.72k
            x;
568
569
4.72k
          Quantum
570
4.72k
            *magick_restrict q;
571
572
4.72k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
573
4.72k
          if (q == (Quantum *) NULL)
574
0
            break;
575
19.3k
          for (x=0; x < (ssize_t) image->columns; x++)
576
14.7k
          {
577
14.7k
            intensity=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
578
14.7k
              exception),max_value);
579
14.7k
            if (EOFBlob(image) != MagickFalse)
580
161
              break;
581
14.6k
            SetPixelGray(image,intensity,q);
582
14.6k
            q+=(ptrdiff_t) GetPixelChannels(image);
583
14.6k
          }
584
4.72k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
585
0
            break;
586
4.72k
          if (image->previous == (Image *) NULL)
587
4.28k
            {
588
4.28k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
589
4.28k
                image->rows);
590
4.28k
              if (status == MagickFalse)
591
0
                break;
592
4.28k
            }
593
4.72k
          if (EOFBlob(image) != MagickFalse)
594
161
            break;
595
4.72k
        }
596
314
        image->type=GrayscaleType;
597
314
        break;
598
0
      }
599
643
      case '3':
600
643
      {
601
        /*
602
          Convert PNM image to pixel packets.
603
        */
604
6.79k
        for (y=0; y < (ssize_t) image->rows; y++)
605
6.51k
        {
606
6.51k
          Quantum
607
6.51k
            *magick_restrict q;
608
609
6.51k
          ssize_t
610
6.51k
            x;
611
612
6.51k
          q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
613
6.51k
          if (q == (Quantum *) NULL)
614
0
            break;
615
27.6k
          for (x=0; x < (ssize_t) image->columns; x++)
616
21.4k
          {
617
21.4k
            Quantum
618
21.4k
              pixel;
619
620
21.4k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
621
21.4k
              exception),max_value);
622
21.4k
            if (EOFBlob(image) != MagickFalse)
623
286
              break;
624
21.1k
            SetPixelRed(image,pixel,q);
625
21.1k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
626
21.1k
              exception),max_value);
627
21.1k
            SetPixelGreen(image,pixel,q);
628
21.1k
            pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10,
629
21.1k
              exception),max_value);
630
21.1k
            SetPixelBlue(image,pixel,q);
631
21.1k
            if ((is_gray != MagickFalse) &&
632
21.1k
                (IsPixelGray(image,q) == MagickFalse))
633
359
              is_gray=MagickFalse;
634
21.1k
            if ((is_mono != MagickFalse) &&
635
21.1k
                (IsPixelMonochrome(image,q) == MagickFalse))
636
359
              is_mono=MagickFalse;
637
21.1k
            q+=(ptrdiff_t) GetPixelChannels(image);
638
21.1k
          }
639
6.51k
          if (SyncAuthenticPixels(image,exception) == MagickFalse)
640
0
            break;
641
6.51k
          if (image->previous == (Image *) NULL)
642
5.54k
            {
643
5.54k
              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
644
5.54k
                image->rows);
645
5.54k
              if (status == MagickFalse)
646
0
                break;
647
5.54k
            }
648
6.51k
          if (EOFBlob(image) != MagickFalse)
649
357
            break;
650
6.51k
        }
651
643
        if (is_gray != MagickFalse)
652
250
          image->type=GrayscaleType;
653
643
        if (is_mono != MagickFalse)
654
250
          image->type=BilevelType;
655
643
        break;
656
0
      }
657
3.61k
      case '4':
658
3.61k
      {
659
        /*
660
          Convert PBM raw image to pixel packets.
661
        */
662
3.61k
        (void) SetImageColorspace(image,GRAYColorspace,exception);
663
3.61k
        quantum_type=GrayQuantum;
664
3.61k
        if (image->storage_class == PseudoClass)
665
0
          quantum_type=IndexQuantum;
666
3.61k
        quantum_info=AcquireQuantumInfo(image_info,image);
667
3.61k
        if (quantum_info == (QuantumInfo *) NULL)
668
3.61k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
669
3.61k
        SetQuantumMinIsWhite(quantum_info,MagickTrue);
670
3.61k
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
671
3.61k
        pixels=GetQuantumPixels(quantum_info);
672
97.4k
        for (y=0; y < (ssize_t) image->rows; y++)
673
93.9k
        {
674
93.9k
          MagickBooleanType
675
93.9k
            sync;
676
677
93.9k
          Quantum
678
93.9k
            *magick_restrict q;
679
680
93.9k
          ssize_t
681
93.9k
            offset;
682
683
93.9k
          size_t
684
93.9k
            length;
685
686
93.9k
          stream=ReadBlobStream(image,extent,pixels,&count);
687
93.9k
          if (count != (ssize_t) extent)
688
122
            break;
689
93.8k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
690
93.8k
              (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
93.8k
          offset=row++;
701
93.8k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
702
93.8k
          if (q == (Quantum *) NULL)
703
0
            break;
704
93.8k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
705
93.8k
            quantum_type,(unsigned char *) stream,exception);
706
93.8k
          if (length != extent)
707
0
            break;
708
93.8k
          sync=SyncAuthenticPixels(image,exception);
709
93.8k
          if (sync == MagickFalse)
710
0
            break;
711
93.8k
        }
712
3.61k
        quantum_info=DestroyQuantumInfo(quantum_info);
713
3.61k
        SetQuantumImageType(image,quantum_type);
714
3.61k
        break;
715
3.61k
      }
716
910
      case '5':
717
910
      {
718
        /*
719
          Convert PGM raw image to pixel packets.
720
        */
721
910
        (void) SetImageColorspace(image,GRAYColorspace,exception);
722
910
        quantum_type=GrayQuantum;
723
910
        extent=(image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
724
910
          image->columns;
725
910
        quantum_info=AcquireQuantumInfo(image_info,image);
726
910
        if (quantum_info == (QuantumInfo *) NULL)
727
910
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
728
910
        pixels=GetQuantumPixels(quantum_info);
729
11.5k
        for (y=0; y < (ssize_t) image->rows; y++)
730
11.2k
        {
731
11.2k
          const unsigned char
732
11.2k
            *magick_restrict p;
733
734
11.2k
          MagickBooleanType
735
11.2k
            sync;
736
737
11.2k
          Quantum
738
11.2k
            *magick_restrict q;
739
740
11.2k
          ssize_t
741
11.2k
            offset,
742
11.2k
            x;
743
744
11.2k
          stream=ReadBlobStream(image,extent,pixels,&count);
745
11.2k
          if (count != (ssize_t) extent)
746
618
            break;
747
10.6k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
748
10.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
10.6k
          offset=row++;
759
10.6k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
760
10.6k
          if (q == (Quantum *) NULL)
761
0
            break;
762
10.6k
          p=(unsigned char *) stream;
763
10.6k
          switch (image->depth)
764
10.6k
          {
765
799
            case 8:
766
1.42k
            case 16:
767
1.42k
            case 32:
768
1.42k
            {
769
1.42k
              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
770
1.42k
                quantum_type,(unsigned char *) stream,exception);
771
1.42k
              break;
772
1.42k
            }
773
9.22k
            default:
774
9.22k
            {
775
9.22k
              switch (image->depth)
776
9.22k
              {
777
508
                case 1:
778
1.19k
                case 2:
779
1.92k
                case 3:
780
2.71k
                case 4:
781
3.29k
                case 5:
782
3.98k
                case 6:
783
5.26k
                case 7:
784
5.26k
                {
785
5.26k
                  unsigned char
786
5.26k
                    pixel;
787
788
17.8k
                  for (x=0; x < (ssize_t) image->columns; x++)
789
12.5k
                  {
790
12.5k
                    p=PushCharPixel(p,&pixel);
791
12.5k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
792
12.5k
                    q+=(ptrdiff_t) GetPixelChannels(image);
793
12.5k
                  }
794
5.26k
                  break;
795
3.98k
                }
796
520
                case 9:
797
1.18k
                case 10:
798
1.71k
                case 11:
799
2.10k
                case 12:
800
2.55k
                case 13:
801
2.92k
                case 14:
802
3.32k
                case 15:
803
3.32k
                {
804
3.32k
                  unsigned short
805
3.32k
                    pixel;
806
807
9.14k
                  for (x=0; x < (ssize_t) image->columns; x++)
808
5.82k
                  {
809
5.82k
                    p=PushShortPixel(MSBEndian,p,&pixel);
810
5.82k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
811
5.82k
                    q+=(ptrdiff_t) GetPixelChannels(image);
812
5.82k
                  }
813
3.32k
                  break;
814
2.92k
                }
815
642
                default:
816
642
                {
817
642
                  unsigned int
818
642
                    pixel;
819
820
2.77k
                  for (x=0; x < (ssize_t) image->columns; x++)
821
2.12k
                  {
822
2.12k
                    p=PushLongPixel(MSBEndian,p,&pixel);
823
2.12k
                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
824
2.12k
                    q+=(ptrdiff_t) GetPixelChannels(image);
825
2.12k
                  }
826
642
                  break;
827
2.92k
                }
828
9.22k
              }
829
9.22k
              break;
830
9.22k
            }
831
10.6k
          }
832
10.6k
          sync=SyncAuthenticPixels(image,exception);
833
10.6k
          if (sync == MagickFalse)
834
0
            break;
835
10.6k
        }
836
910
        quantum_info=DestroyQuantumInfo(quantum_info);
837
910
        SetQuantumImageType(image,quantum_type);
838
910
        break;
839
910
      }
840
4.45k
      case '6':
841
4.45k
      {
842
        /*
843
          Convert PNM raster image to pixel packets.
844
        */
845
4.45k
        quantum_type=RGBQuantum;
846
4.45k
        extent=3*(size_t) (image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
847
4.45k
          image->columns;
848
4.45k
        quantum_info=AcquireQuantumInfo(image_info,image);
849
4.45k
        if (quantum_info == (QuantumInfo *) NULL)
850
4.45k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
851
4.45k
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
852
4.45k
        pixels=GetQuantumPixels(quantum_info);
853
28.5k
        for (y=0; y < (ssize_t) image->rows; y++)
854
27.1k
        {
855
27.1k
          const unsigned char
856
27.1k
            *magick_restrict p;
857
858
27.1k
          MagickBooleanType
859
27.1k
            sync;
860
861
27.1k
          Quantum
862
27.1k
            *magick_restrict q;
863
864
27.1k
          ssize_t
865
27.1k
            offset,
866
27.1k
            x;
867
868
27.1k
          stream=ReadBlobStream(image,extent,pixels,&count);
869
27.1k
          if (count != (ssize_t) extent)
870
2.98k
            break;
871
24.1k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
872
24.1k
              (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
24.1k
          offset=row++;
883
24.1k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
884
24.1k
          if (q == (Quantum *) NULL)
885
0
            break;
886
24.1k
          p=(unsigned char *) stream;
887
24.1k
          switch (image->depth)
888
24.1k
          {
889
870
            case 1:
890
2.23k
            case 2:
891
4.97k
            case 3:
892
5.96k
            case 4:
893
6.36k
            case 5:
894
10.2k
            case 6:
895
11.1k
            case 7:
896
11.1k
            {
897
11.1k
              unsigned char
898
11.1k
                pixel;
899
900
45.1k
              for (x=0; x < (ssize_t) image->columns; x++)
901
34.0k
              {
902
34.0k
                p=PushCharPixel(p,&pixel);
903
34.0k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
904
34.0k
                p=PushCharPixel(p,&pixel);
905
34.0k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
906
34.0k
                p=PushCharPixel(p,&pixel);
907
34.0k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
908
34.0k
                SetPixelAlpha(image,OpaqueAlpha,q);
909
34.0k
                if ((is_gray != MagickFalse) &&
910
34.0k
                    (IsPixelGray(image,q) == MagickFalse))
911
1.44k
                  is_gray=MagickFalse;
912
34.0k
                if ((is_mono != MagickFalse) &&
913
34.0k
                    (IsPixelMonochrome(image,q) == MagickFalse))
914
1.45k
                  is_mono=MagickFalse;
915
34.0k
                q+=(ptrdiff_t) GetPixelChannels(image);
916
34.0k
              }
917
11.1k
              break;
918
10.2k
            }
919
1.25k
            case 8:
920
1.25k
            {
921
4.16k
              for (x=0; x < (ssize_t) image->columns; x++)
922
2.91k
              {
923
2.91k
                SetPixelRed(image,ScaleCharToQuantum(*p++),q);
924
2.91k
                SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
925
2.91k
                SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
926
2.91k
                SetPixelAlpha(image,OpaqueAlpha,q);
927
2.91k
                if ((is_gray != MagickFalse) &&
928
2.91k
                    (IsPixelGray(image,q) == MagickFalse))
929
88
                  is_gray=MagickFalse;
930
2.91k
                if ((is_mono != MagickFalse) &&
931
2.91k
                    (IsPixelMonochrome(image,q) == MagickFalse))
932
89
                  is_mono=MagickFalse;
933
2.91k
                q+=(ptrdiff_t) GetPixelChannels(image);
934
2.91k
              }
935
1.25k
              break;
936
10.2k
            }
937
1.75k
            case 9:
938
2.95k
            case 10:
939
3.49k
            case 11:
940
3.95k
            case 12:
941
4.88k
            case 13:
942
5.93k
            case 14:
943
6.44k
            case 15:
944
6.44k
            {
945
6.44k
              unsigned short
946
6.44k
                pixel;
947
948
22.3k
              for (x=0; x < (ssize_t) image->columns; x++)
949
15.9k
              {
950
15.9k
                p=PushShortPixel(MSBEndian,p,&pixel);
951
15.9k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
952
15.9k
                p=PushShortPixel(MSBEndian,p,&pixel);
953
15.9k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
954
15.9k
                p=PushShortPixel(MSBEndian,p,&pixel);
955
15.9k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
956
15.9k
                SetPixelAlpha(image,OpaqueAlpha,q);
957
15.9k
                if ((is_gray != MagickFalse) &&
958
15.9k
                    (IsPixelGray(image,q) == MagickFalse))
959
1.02k
                  is_gray=MagickFalse;
960
15.9k
                if ((is_mono != MagickFalse) &&
961
15.9k
                    (IsPixelMonochrome(image,q) == MagickFalse))
962
1.02k
                  is_mono=MagickFalse;
963
15.9k
                q+=(ptrdiff_t) GetPixelChannels(image);
964
15.9k
              }
965
6.44k
              break;
966
5.93k
            }
967
2.43k
            case 16:
968
2.43k
            {
969
2.43k
              unsigned short
970
2.43k
                pixel;
971
972
10.3k
              for (x=0; x < (ssize_t) image->columns; x++)
973
7.92k
              {
974
7.92k
                p=PushShortPixel(MSBEndian,p,&pixel);
975
7.92k
                SetPixelRed(image,ScaleShortToQuantum(pixel),q);
976
7.92k
                p=PushShortPixel(MSBEndian,p,&pixel);
977
7.92k
                SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
978
7.92k
                p=PushShortPixel(MSBEndian,p,&pixel);
979
7.92k
                SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
980
7.92k
                SetPixelAlpha(image,OpaqueAlpha,q);
981
7.92k
                if ((is_gray != MagickFalse) &&
982
7.92k
                    (IsPixelGray(image,q) == MagickFalse))
983
535
                  is_gray=MagickFalse;
984
7.92k
                if ((is_mono != MagickFalse) &&
985
7.92k
                    (IsPixelMonochrome(image,q) == MagickFalse))
986
535
                  is_mono=MagickFalse;
987
7.92k
                q+=(ptrdiff_t) GetPixelChannels(image);
988
7.92k
              }
989
2.43k
              break;
990
5.93k
            }
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
5.93k
            }
1015
2.87k
            default:
1016
2.87k
            {
1017
2.87k
              unsigned int
1018
2.87k
                pixel;
1019
1020
10.9k
              for (x=0; x < (ssize_t) image->columns; x++)
1021
8.07k
              {
1022
8.07k
                p=PushLongPixel(MSBEndian,p,&pixel);
1023
8.07k
                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1024
8.07k
                p=PushLongPixel(MSBEndian,p,&pixel);
1025
8.07k
                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
1026
8.07k
                p=PushLongPixel(MSBEndian,p,&pixel);
1027
8.07k
                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
1028
8.07k
                SetPixelAlpha(image,OpaqueAlpha,q);
1029
8.07k
                if ((is_gray != MagickFalse) &&
1030
8.07k
                    (IsPixelGray(image,q) == MagickFalse))
1031
686
                  is_gray=MagickFalse;
1032
8.07k
                if ((is_mono != MagickFalse) &&
1033
8.07k
                    (IsPixelMonochrome(image,q) == MagickFalse))
1034
686
                  is_mono=MagickFalse;
1035
8.07k
                q+=(ptrdiff_t) GetPixelChannels(image);
1036
8.07k
              }
1037
2.87k
              break;
1038
5.93k
            }
1039
24.1k
          }
1040
24.1k
          sync=SyncAuthenticPixels(image,exception);
1041
24.1k
          if (sync == MagickFalse)
1042
0
            break;
1043
24.1k
        }
1044
4.45k
        if (is_gray != MagickFalse)
1045
601
          image->type=GrayscaleType;
1046
4.45k
        if (is_mono != MagickFalse)
1047
599
          image->type=BilevelType;
1048
4.45k
        quantum_info=DestroyQuantumInfo(quantum_info);
1049
4.45k
        break;
1050
4.45k
      }
1051
3.09k
      case '7':
1052
3.09k
      {
1053
3.09k
        size_t
1054
3.09k
          channels;
1055
1056
        /*
1057
          Convert PAM raster image to pixel packets.
1058
        */
1059
3.09k
        switch (quantum_type)
1060
3.09k
        {
1061
411
          case GrayQuantum:
1062
862
          case GrayAlphaQuantum:
1063
862
          {
1064
862
            channels=1;
1065
862
            break;
1066
411
          }
1067
730
          case CMYKQuantum:
1068
1.12k
          case CMYKAQuantum:
1069
1.12k
          {
1070
1.12k
            channels=4;
1071
1.12k
            break;
1072
730
          }
1073
1.11k
          default:
1074
1.11k
          {
1075
1.11k
            channels=3;
1076
1.11k
            break;
1077
730
          }
1078
3.09k
        }
1079
3.09k
        if (image->alpha_trait != UndefinedPixelTrait)
1080
1.20k
          channels++;
1081
3.09k
        extent=channels*(image->depth <= 8 ? 1 : image->depth <= 16 ? 2 : 4)*
1082
3.09k
          image->columns;
1083
3.09k
        quantum_info=AcquireQuantumInfo(image_info,image);
1084
3.09k
        if (quantum_info == (QuantumInfo *) NULL)
1085
3.09k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1086
3.09k
        pixels=GetQuantumPixels(quantum_info);
1087
55.3k
        for (y=0; y < (ssize_t) image->rows; y++)
1088
54.2k
        {
1089
54.2k
          const unsigned char
1090
54.2k
            *magick_restrict p;
1091
1092
54.2k
          MagickBooleanType
1093
54.2k
            sync;
1094
1095
54.2k
          Quantum
1096
54.2k
            *magick_restrict q;
1097
1098
54.2k
          ssize_t
1099
54.2k
            offset,
1100
54.2k
            x;
1101
1102
54.2k
          stream=ReadBlobStream(image,extent,pixels,&count);
1103
54.2k
          if (count != (ssize_t) extent)
1104
1.95k
            break;
1105
52.2k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1106
52.2k
              (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
52.2k
          offset=row++;
1117
52.2k
          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
1118
52.2k
          if (q == (Quantum *) NULL)
1119
0
            break;
1120
52.2k
          p=(unsigned char *) stream;
1121
52.2k
          switch (image->depth)
1122
52.2k
          {
1123
3.41k
            case 8:
1124
10.1k
            case 16:
1125
15.0k
            case 32:
1126
15.0k
            {
1127
15.0k
              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1128
15.0k
                quantum_type,(unsigned char *) stream,exception);
1129
15.0k
              break;
1130
10.1k
            }
1131
37.2k
            default:
1132
37.2k
            {
1133
37.2k
              switch (quantum_type)
1134
37.2k
              {
1135
7.63k
                case GrayQuantum:
1136
12.2k
                case GrayAlphaQuantum:
1137
12.2k
                {
1138
12.2k
                  switch (image->depth)
1139
12.2k
                  {
1140
1.89k
                    case 1:
1141
2.46k
                    case 2:
1142
2.90k
                    case 3:
1143
3.49k
                    case 4:
1144
3.98k
                    case 5:
1145
4.63k
                    case 6:
1146
5.18k
                    case 7:
1147
5.18k
                    case 8:
1148
5.18k
                    {
1149
5.18k
                      unsigned char
1150
5.18k
                        pixel;
1151
 
1152
17.2k
                      for (x=0; x < (ssize_t) image->columns; x++)
1153
12.0k
                      {
1154
12.0k
                        p=PushCharPixel(p,&pixel);
1155
12.0k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1156
12.0k
                          q);
1157
12.0k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1158
12.0k
                        if (image->alpha_trait != UndefinedPixelTrait)
1159
3.73k
                          {
1160
3.73k
                            p=PushCharPixel(p,&pixel);
1161
3.73k
                            if (image->depth != 1)
1162
1.60k
                              SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1163
1.60k
                                max_value),q);
1164
2.13k
                            else
1165
2.13k
                              SetPixelAlpha(image,QuantumRange-
1166
2.13k
                                ScaleAnyToQuantum(pixel,max_value),q);
1167
3.73k
                          }
1168
12.0k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1169
12.0k
                      }
1170
5.18k
                      break;
1171
5.18k
                    }
1172
1.23k
                    case 9:
1173
1.84k
                    case 10:
1174
2.25k
                    case 11:
1175
3.18k
                    case 12:
1176
3.66k
                    case 13:
1177
4.07k
                    case 14:
1178
4.72k
                    case 15:
1179
4.72k
                    case 16:
1180
4.72k
                    {
1181
4.72k
                      unsigned short
1182
4.72k
                        pixel;
1183
1184
11.9k
                      for (x=0; x < (ssize_t) image->columns; x++)
1185
7.19k
                      {
1186
7.19k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1187
7.19k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1188
7.19k
                          q);
1189
7.19k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1190
7.19k
                        if (image->alpha_trait != UndefinedPixelTrait)
1191
2.36k
                          {
1192
2.36k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1193
2.36k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1194
2.36k
                              max_value),q);
1195
2.36k
                          }
1196
7.19k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1197
7.19k
                      }
1198
4.72k
                      break;
1199
4.72k
                    }
1200
2.37k
                    default:
1201
2.37k
                    {
1202
2.37k
                      unsigned int
1203
2.37k
                        pixel;
1204
1205
5.86k
                      for (x=0; x < (ssize_t) image->columns; x++)
1206
3.49k
                      {
1207
3.49k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1208
3.49k
                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
1209
3.49k
                          q);
1210
3.49k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1211
3.49k
                        if (image->alpha_trait != UndefinedPixelTrait)
1212
1.26k
                          {
1213
1.26k
                            p=PushLongPixel(MSBEndian,p,&pixel);
1214
1.26k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1215
1.26k
                              max_value),q);
1216
1.26k
                          }
1217
3.49k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1218
3.49k
                      }
1219
2.37k
                    }
1220
12.2k
                  }
1221
12.2k
                  break;
1222
12.2k
                }
1223
12.2k
                case CMYKQuantum:
1224
13.6k
                case CMYKAQuantum:
1225
13.6k
                {
1226
13.6k
                  switch (image->depth)
1227
13.6k
                  {
1228
2.85k
                    case 1:
1229
3.26k
                    case 2:
1230
3.80k
                    case 3:
1231
4.21k
                    case 4:
1232
4.66k
                    case 5:
1233
5.23k
                    case 6:
1234
5.78k
                    case 7:
1235
5.78k
                    case 8:
1236
5.78k
                    {
1237
5.78k
                      unsigned char
1238
5.78k
                        pixel;
1239
1240
14.6k
                      for (x=0; x < (ssize_t) image->columns; x++)
1241
8.88k
                      {
1242
8.88k
                        p=PushCharPixel(p,&pixel);
1243
8.88k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1244
8.88k
                        p=PushCharPixel(p,&pixel);
1245
8.88k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1246
8.88k
                          q);
1247
8.88k
                        p=PushCharPixel(p,&pixel);
1248
8.88k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1249
8.88k
                          q);
1250
8.88k
                        p=PushCharPixel(p,&pixel);
1251
8.88k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1252
8.88k
                          q);
1253
8.88k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1254
8.88k
                        if (image->alpha_trait != UndefinedPixelTrait)
1255
2.83k
                          {
1256
2.83k
                            p=PushCharPixel(p,&pixel);
1257
2.83k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1258
2.83k
                              max_value),q);
1259
2.83k
                          }
1260
8.88k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1261
8.88k
                      }
1262
5.78k
                      break;
1263
5.78k
                    }
1264
664
                    case 9:
1265
1.49k
                    case 10:
1266
1.99k
                    case 11:
1267
3.65k
                    case 12:
1268
4.05k
                    case 13:
1269
4.58k
                    case 14:
1270
5.64k
                    case 15:
1271
5.64k
                    case 16:
1272
5.64k
                    {
1273
5.64k
                      unsigned short
1274
5.64k
                        pixel;
1275
1276
14.0k
                      for (x=0; x < (ssize_t) image->columns; x++)
1277
8.37k
                      {
1278
8.37k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1279
8.37k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1280
8.37k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1281
8.37k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1282
8.37k
                          q);
1283
8.37k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1284
8.37k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1285
8.37k
                          q);
1286
8.37k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1287
8.37k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1288
8.37k
                          q);
1289
8.37k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1290
8.37k
                        if (image->alpha_trait != UndefinedPixelTrait)
1291
2.90k
                          {
1292
2.90k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1293
2.90k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1294
2.90k
                              max_value),q);
1295
2.90k
                          }
1296
8.37k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1297
8.37k
                      }
1298
5.64k
                      break;
1299
5.64k
                    }
1300
2.25k
                    default:
1301
2.25k
                    {
1302
2.25k
                      unsigned int
1303
2.25k
                        pixel;
1304
1305
5.75k
                      for (x=0; x < (ssize_t) image->columns; x++)
1306
3.49k
                      {
1307
3.49k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1308
3.49k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),
1309
3.49k
                          q);
1310
3.49k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1311
3.49k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1312
3.49k
                          q);
1313
3.49k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1314
3.49k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1315
3.49k
                          q);
1316
3.49k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1317
3.49k
                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
1318
3.49k
                          q);
1319
3.49k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1320
3.49k
                        if (image->alpha_trait != UndefinedPixelTrait)
1321
1.77k
                          {
1322
1.77k
                            p=PushLongPixel(MSBEndian,p,&pixel);
1323
1.77k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1324
1.77k
                              max_value),q);
1325
1.77k
                          }
1326
3.49k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1327
3.49k
                      }
1328
2.25k
                      break;
1329
5.64k
                    }
1330
13.6k
                  }
1331
13.6k
                  break;
1332
13.6k
                }
1333
13.6k
                default:
1334
11.2k
                {
1335
11.2k
                  switch (image->depth)
1336
11.2k
                  {
1337
1.78k
                    case 1:
1338
2.23k
                    case 2:
1339
2.63k
                    case 3:
1340
3.05k
                    case 4:
1341
3.47k
                    case 5:
1342
4.04k
                    case 6:
1343
4.63k
                    case 7:
1344
4.63k
                    case 8:
1345
4.63k
                    {
1346
4.63k
                      unsigned char
1347
4.63k
                        pixel;
1348
1349
13.8k
                      for (x=0; x < (ssize_t) image->columns; x++)
1350
9.17k
                      {
1351
9.17k
                        p=PushCharPixel(p,&pixel);
1352
9.17k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1353
9.17k
                        p=PushCharPixel(p,&pixel);
1354
9.17k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1355
9.17k
                          q);
1356
9.17k
                        p=PushCharPixel(p,&pixel);
1357
9.17k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1358
9.17k
                          q);
1359
9.17k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1360
9.17k
                        if (image->alpha_trait != UndefinedPixelTrait)
1361
1.52k
                          {
1362
1.52k
                            p=PushCharPixel(p,&pixel);
1363
1.52k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1364
1.52k
                              max_value),q);
1365
1.52k
                          }
1366
9.17k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1367
9.17k
                      }
1368
4.63k
                      break;
1369
4.63k
                    }
1370
772
                    case 9:
1371
1.40k
                    case 10:
1372
1.83k
                    case 11:
1373
2.46k
                    case 12:
1374
3.02k
                    case 13:
1375
3.63k
                    case 14:
1376
4.48k
                    case 15:
1377
4.48k
                    case 16:
1378
4.48k
                    {
1379
4.48k
                      unsigned short
1380
4.48k
                        pixel;
1381
1382
12.2k
                      for (x=0; x < (ssize_t) image->columns; x++)
1383
7.76k
                      {
1384
7.76k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1385
7.76k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
1386
7.76k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1387
7.76k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1388
7.76k
                          q);
1389
7.76k
                        p=PushShortPixel(MSBEndian,p,&pixel);
1390
7.76k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1391
7.76k
                          q);
1392
7.76k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1393
7.76k
                        if (image->alpha_trait != UndefinedPixelTrait)
1394
1.21k
                          {
1395
1.21k
                            p=PushShortPixel(MSBEndian,p,&pixel);
1396
1.21k
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1397
1.21k
                              max_value),q);
1398
1.21k
                          }
1399
7.76k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1400
7.76k
                      }
1401
4.48k
                      break;
1402
4.48k
                    }
1403
2.15k
                    default:
1404
2.15k
                    {
1405
2.15k
                      unsigned int
1406
2.15k
                        pixel;
1407
1408
5.58k
                      for (x=0; x < (ssize_t) image->columns; x++)
1409
3.42k
                      {
1410
3.42k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1411
3.42k
                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),
1412
3.42k
                          q);
1413
3.42k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1414
3.42k
                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
1415
3.42k
                          q);
1416
3.42k
                        p=PushLongPixel(MSBEndian,p,&pixel);
1417
3.42k
                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
1418
3.42k
                          q);
1419
3.42k
                        SetPixelAlpha(image,OpaqueAlpha,q);
1420
3.42k
                        if (image->alpha_trait != UndefinedPixelTrait)
1421
964
                          {
1422
964
                            p=PushLongPixel(MSBEndian,p,&pixel);
1423
964
                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
1424
964
                              max_value),q);
1425
964
                          }
1426
3.42k
                        q+=(ptrdiff_t) GetPixelChannels(image);
1427
3.42k
                      }
1428
2.15k
                      break;
1429
4.48k
                    }
1430
11.2k
                  }
1431
11.2k
                  break;
1432
11.2k
                }
1433
37.2k
              }
1434
37.2k
            }
1435
52.2k
          }
1436
52.2k
          sync=SyncAuthenticPixels(image,exception);
1437
52.2k
          if (sync == MagickFalse)
1438
0
            break;
1439
52.2k
        }
1440
3.09k
        quantum_info=DestroyQuantumInfo(quantum_info);
1441
3.09k
        SetQuantumImageType(image,quantum_type);
1442
3.09k
        break;
1443
3.09k
      }
1444
599
      case 'F':
1445
928
      case 'f':
1446
928
      {
1447
        /*
1448
          Convert PFM raster image to pixel packets.
1449
        */
1450
928
        if (format != 'f')
1451
599
          quantum_type=image->alpha_trait != UndefinedPixelTrait ? RGBAQuantum :
1452
599
            RGBQuantum;
1453
329
        else
1454
329
          {
1455
329
            (void) SetImageColorspace(image,GRAYColorspace,exception);
1456
329
            quantum_type=GrayQuantum;
1457
329
          }
1458
928
        image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1459
928
        image->depth=32;
1460
928
        quantum_info=AcquireQuantumInfo(image_info,image);
1461
928
        if (quantum_info == (QuantumInfo *) NULL)
1462
928
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1463
928
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1464
928
        if (status == MagickFalse)
1465
928
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1466
928
        SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1467
928
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
1468
928
        pixels=GetQuantumPixels(quantum_info);
1469
7.65k
        for (y=0; y < (ssize_t) image->rows; y++)
1470
7.25k
        {
1471
7.25k
          MagickBooleanType
1472
7.25k
            sync;
1473
1474
7.25k
          Quantum
1475
7.25k
            *magick_restrict q;
1476
1477
7.25k
          size_t
1478
7.25k
            length;
1479
1480
7.25k
          ssize_t
1481
7.25k
            offset;
1482
1483
7.25k
          stream=ReadBlobStream(image,extent,pixels,&count);
1484
7.25k
          if (count != (ssize_t) extent)
1485
525
            break;
1486
6.72k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1487
6.72k
              (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
6.72k
          offset=row++;
1498
6.72k
          q=QueueAuthenticPixels(image,0,((ssize_t) image->rows-offset-1),
1499
6.72k
            image->columns,1,exception);
1500
6.72k
          if (q == (Quantum *) NULL)
1501
0
            break;
1502
6.72k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1503
6.72k
            quantum_type,(unsigned char *) stream,exception);
1504
6.72k
          if (length != extent)
1505
0
            break;
1506
6.72k
          sync=SyncAuthenticPixels(image,exception);
1507
6.72k
          if (sync == MagickFalse)
1508
0
            break;
1509
6.72k
        }
1510
928
        quantum_info=DestroyQuantumInfo(quantum_info);
1511
928
        SetQuantumImageType(image,quantum_type);
1512
928
        break;
1513
928
      }
1514
844
      case 'H':
1515
1.80k
      case 'h':
1516
1.80k
      {
1517
        /*
1518
          Convert PFM raster image to pixel packets.
1519
        */
1520
1.80k
        if (format != 'h')
1521
844
          quantum_type=image->alpha_trait != UndefinedPixelTrait ? RGBAQuantum :
1522
844
            RGBQuantum;
1523
965
        else
1524
965
          {
1525
965
            (void) SetImageColorspace(image,GRAYColorspace,exception);
1526
965
            quantum_type=GrayQuantum;
1527
965
          }
1528
1.80k
        image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
1529
1.80k
        image->depth=16;
1530
1.80k
        quantum_info=AcquireQuantumInfo(image_info,image);
1531
1.80k
        if (quantum_info == (QuantumInfo *) NULL)
1532
1.80k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1533
1.80k
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1534
1.80k
        if (status == MagickFalse)
1535
1.80k
          ThrowPNMException(ResourceLimitError,"MemoryAllocationFailed");
1536
1.80k
        SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
1537
1.80k
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
1538
1.80k
        pixels=GetQuantumPixels(quantum_info);
1539
16.6k
        for (y=0; y < (ssize_t) image->rows; y++)
1540
15.7k
        {
1541
15.7k
          MagickBooleanType
1542
15.7k
            sync;
1543
1544
15.7k
          Quantum
1545
15.7k
            *magick_restrict q;
1546
1547
15.7k
          size_t
1548
15.7k
            length;
1549
1550
15.7k
          ssize_t
1551
15.7k
            offset;
1552
1553
15.7k
          stream=ReadBlobStream(image,extent,pixels,&count);
1554
15.7k
          if (count != (ssize_t) extent)
1555
903
            break;
1556
14.7k
          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1557
14.7k
              (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
14.7k
          offset=row++;
1568
14.7k
          q=QueueAuthenticPixels(image,0,((ssize_t) image->rows-offset-1),
1569
14.7k
            image->columns,1,exception);
1570
14.7k
          if (q == (Quantum *) NULL)
1571
0
            break;
1572
14.7k
          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1573
14.7k
            quantum_type,(unsigned char *) stream,exception);
1574
14.7k
          if (length != extent)
1575
0
            break;
1576
14.7k
          sync=SyncAuthenticPixels(image,exception);
1577
14.7k
          if (sync == MagickFalse)
1578
0
            break;
1579
14.7k
        }
1580
1.80k
        quantum_info=DestroyQuantumInfo(quantum_info);
1581
1.80k
        SetQuantumImageType(image,quantum_type);
1582
1.80k
        break;
1583
1.80k
      }
1584
6
      default:
1585
6
        ThrowPNMException(CorruptImageError,"ImproperImageHeader");
1586
16.5k
    }
1587
16.5k
    if (*comment_info.comment != '\0')
1588
728
      (void) SetImageProperty(image,"comment",comment_info.comment,exception);
1589
16.5k
    comment_info.comment=DestroyString(comment_info.comment);
1590
16.5k
    if (y < (ssize_t) image->rows)
1591
8.69k
      ThrowPNMException(CorruptImageError,"UnableToReadImageData");
1592
8.69k
    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
8.69k
    if (image_info->number_scenes != 0)
1602
189
      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1603
1
        break;
1604
8.69k
    if ((format == '1') || (format == '2') || (format == '3'))
1605
1.00k
      do
1606
11.8k
      {
1607
        /*
1608
          Skip to end of line.
1609
        */
1610
11.8k
        count=ReadBlob(image,1,(unsigned char *) &format);
1611
11.8k
        if (count != 1)
1612
657
          break;
1613
11.2k
        if (format == 'P')
1614
199
          break;
1615
11.2k
      } while (format != '\n');
1616
0
    count=ReadBlob(image,1,(unsigned char *) &format);
1617
8.69k
    if ((count == 1) && (format == 'P'))
1618
2.50k
      {
1619
        /*
1620
          Allocate next image structure.
1621
        */
1622
2.50k
        AcquireNextImage(image_info,image,exception);
1623
2.50k
        if (GetNextImageInList(image) == (Image *) NULL)
1624
0
          {
1625
0
            status=MagickFalse;
1626
0
            break;
1627
0
          }
1628
2.50k
        image=SyncNextImageInList(image);
1629
2.50k
        status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1630
2.50k
          GetBlobSize(image));
1631
2.50k
        if (status == MagickFalse)
1632
0
          break;
1633
2.50k
      }
1634
8.69k
  } while ((count == 1) && (format == 'P'));
1635
6.19k
  (void) CloseBlob(image);
1636
6.19k
  if (comment_info.comment != (char *) NULL)
1637
0
    comment_info.comment=DestroyString(comment_info.comment);
1638
6.19k
  if (quantum_info != (QuantumInfo *) NULL)
1639
0
    quantum_info=DestroyQuantumInfo(quantum_info);
1640
6.19k
  if (status == MagickFalse)
1641
0
    return(DestroyImageList(image));
1642
6.19k
  return(GetFirstImageInList(image));
1643
6.19k
}
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.81k
{
1778
1.81k
  char
1779
1.81k
    buffer[MagickPathExtent],
1780
1.81k
    format,
1781
1.81k
    magick[MagickPathExtent];
1782
1783
1.81k
  const char
1784
1.81k
    *value;
1785
1786
1.81k
  MagickBooleanType
1787
1.81k
    status;
1788
1789
1.81k
  MagickOffsetType
1790
1.81k
    scene;
1791
1792
1.81k
  Quantum
1793
1.81k
    index;
1794
1795
1.81k
  QuantumAny
1796
1.81k
    pixel;
1797
1798
1.81k
  QuantumInfo
1799
1.81k
    *quantum_info;
1800
1801
1.81k
  QuantumType
1802
1.81k
    quantum_type;
1803
1804
1.81k
  size_t
1805
1.81k
    extent,
1806
1.81k
    number_scenes,
1807
1.81k
    packet_size;
1808
1809
1.81k
  ssize_t
1810
1.81k
    count,
1811
1.81k
    y;
1812
1813
1.81k
  unsigned char
1814
1.81k
    *q;
1815
1816
  /*
1817
    Open output image file.
1818
  */
1819
1.81k
  assert(image_info != (const ImageInfo *) NULL);
1820
1.81k
  assert(image_info->signature == MagickCoreSignature);
1821
1.81k
  assert(image != (Image *) NULL);
1822
1.81k
  assert(image->signature == MagickCoreSignature);
1823
1.81k
  assert(exception != (ExceptionInfo *) NULL);
1824
1.81k
  assert(exception->signature == MagickCoreSignature);
1825
1.81k
  if (IsEventLogging() != MagickFalse)
1826
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1827
1.81k
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1828
1.81k
  if (status == MagickFalse)
1829
0
    return(status);
1830
1.81k
  scene=0;
1831
1.81k
  number_scenes=GetImageListLength(image);
1832
1.81k
  do
1833
1.81k
  {
1834
1.81k
    QuantumAny
1835
1.81k
      max_value;
1836
1837
    /*
1838
      Write PNM file header.
1839
    */
1840
1.81k
    packet_size=3;
1841
1.81k
    quantum_type=RGBQuantum;
1842
1.81k
    (void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
1843
1.81k
    max_value=GetQuantumRange(image->depth);
1844
1.81k
    switch (magick[1])
1845
1.81k
    {
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.81k
      case 'N':
1889
1.81k
      case 'n':
1890
1.81k
      {
1891
1.81k
        ImageType
1892
1.81k
          type;
1893
1894
1.81k
        format='6';
1895
1.81k
        if (image_info->type == TrueColorType)
1896
0
          break;
1897
1.81k
        type=IdentifyImageCoderGrayType(image,exception);
1898
1.81k
        if (IsGrayImageType(type) != MagickFalse)
1899
1.04k
          {
1900
1.04k
            format='5';
1901
1.04k
            if (image_info->compression == NoCompression)
1902
0
              format='2';
1903
1.04k
            if (type == BilevelType)
1904
551
              {
1905
551
                format='4';
1906
551
                if (image_info->compression == NoCompression)
1907
0
                  format='1';
1908
551
              }
1909
1.04k
          }
1910
1.81k
        break;
1911
1.81k
      }
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.81k
      }
1919
1.81k
    }
1920
1.81k
    (void) FormatLocaleString(buffer,MagickPathExtent,"P%c\n",format);
1921
1.81k
    (void) WriteBlobString(image,buffer);
1922
1.81k
    value=GetImageProperty(image,"comment",exception);
1923
1.81k
    if (value != (const char *) NULL)
1924
112
      {
1925
112
        const char
1926
112
          *p;
1927
1928
        /*
1929
          Write comments to file.
1930
        */
1931
112
        (void) WriteBlobByte(image,'#');
1932
68.2k
        for (p=value; *p != '\0'; p++)
1933
68.1k
        {
1934
68.1k
          (void) WriteBlobByte(image,(unsigned char) *p);
1935
68.1k
          if ((*p == '\n') || (*p == '\r'))
1936
989
            (void) WriteBlobByte(image,'#');
1937
68.1k
        }
1938
112
        (void) WriteBlobByte(image,'\n');
1939
112
      }
1940
1.81k
    if (format != '7')
1941
1.81k
      {
1942
1.81k
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",
1943
1.81k
          (double) image->columns,(double) image->rows);
1944
1.81k
        (void) WriteBlobString(image,buffer);
1945
1.81k
      }
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.81k
    switch (format)
2006
1.81k
    {
2007
0
      case '1':
2008
0
      {
2009
0
        unsigned char
2010
0
          pixels[70];
2011
2012
        /*
2013
          Convert image to a PBM image.
2014
        */
2015
0
        (void) SetImageType(image,BilevelType,exception);
2016
0
        extent=1;
2017
0
        q=pixels;
2018
0
        for (y=0; y < (ssize_t) image->rows; y++)
2019
0
        {
2020
0
          const Quantum
2021
0
            *magick_restrict p;
2022
2023
0
          ssize_t
2024
0
            x;
2025
2026
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2027
0
          if (p == (const Quantum *) NULL)
2028
0
            break;
2029
0
          for (x=0; x < (ssize_t) image->columns; x++)
2030
0
          {
2031
0
            if (q != pixels)
2032
0
              {
2033
0
                if ((size_t) (q-pixels+(ssize_t) extent+1) < sizeof(pixels))
2034
0
                  *q++=' ';
2035
0
                else
2036
0
                  {
2037
0
                    *q++='\n';
2038
0
                    (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2039
0
                    q=pixels;
2040
0
                  }
2041
0
              }
2042
0
            *q++=(unsigned char) (GetPixelLuma(image,p) >= ((double)
2043
0
              QuantumRange/2.0) ? '0' : '1');
2044
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2045
0
          }
2046
0
          if (image->previous == (Image *) NULL)
2047
0
            {
2048
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2049
0
                image->rows);
2050
0
              if (status == MagickFalse)
2051
0
                break;
2052
0
            }
2053
0
        }
2054
0
        if (q != pixels)
2055
0
          {
2056
0
            *q++='\n';
2057
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2058
0
          }
2059
0
        break;
2060
0
      }
2061
0
      case '2':
2062
0
      {
2063
0
        unsigned char
2064
0
          pixels[70];
2065
2066
        /*
2067
          Convert image to a PGM image.
2068
        */
2069
0
        if (image->depth <= 8)
2070
0
          (void) WriteBlobString(image,"255\n");
2071
0
        else
2072
0
          if (image->depth <= 16)
2073
0
            (void) WriteBlobString(image,"65535\n");
2074
0
          else
2075
0
            (void) WriteBlobString(image,"4294967295\n");
2076
0
        q=pixels;
2077
0
        for (y=0; y < (ssize_t) image->rows; y++)
2078
0
        {
2079
0
          const Quantum
2080
0
            *magick_restrict p;
2081
2082
0
          ssize_t
2083
0
            x;
2084
2085
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2086
0
          if (p == (const Quantum *) NULL)
2087
0
            break;
2088
0
          for (x=0; x < (ssize_t) image->columns; x++)
2089
0
          {
2090
0
            index=ClampToQuantum(GetPixelLuma(image,p));
2091
0
            if (image->depth <= 8)
2092
0
              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,"%u",
2093
0
                ScaleQuantumToChar(index));
2094
0
            else
2095
0
              if (image->depth <= 16)
2096
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2097
0
                  "%u",ScaleQuantumToShort(index));
2098
0
              else
2099
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2100
0
                  "%u",ScaleQuantumToLong(index));
2101
0
            extent=(size_t) count;
2102
0
            if (q != pixels)
2103
0
              {
2104
0
                if ((size_t) (q-pixels+(ssize_t) extent+1) < sizeof(pixels))
2105
0
                  *q++=' ';
2106
0
                else
2107
0
                  {
2108
0
                    *q++='\n';
2109
0
                    (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2110
0
                    q=pixels;
2111
0
                  }
2112
0
              }
2113
0
            (void) memcpy((char *) q,buffer,extent);
2114
0
            q+=(ptrdiff_t) extent;
2115
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2116
0
          }
2117
0
          if (image->previous == (Image *) NULL)
2118
0
            {
2119
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2120
0
                image->rows);
2121
0
              if (status == MagickFalse)
2122
0
                break;
2123
0
            }
2124
0
        }
2125
0
        if (q != pixels)
2126
0
          {
2127
0
            *q++='\n';
2128
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2129
0
          }
2130
0
        break;
2131
0
      }
2132
0
      case '3':
2133
0
      {
2134
0
        unsigned char
2135
0
          pixels[70];
2136
2137
        /*
2138
          Convert image to a PNM image.
2139
        */
2140
0
        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2141
0
          (void) TransformImageColorspace(image,sRGBColorspace,exception);
2142
0
        if (image->depth <= 8)
2143
0
          (void) WriteBlobString(image,"255\n");
2144
0
        else
2145
0
          if (image->depth <= 16)
2146
0
            (void) WriteBlobString(image,"65535\n");
2147
0
          else
2148
0
            (void) WriteBlobString(image,"4294967295\n");
2149
0
        q=pixels;
2150
0
        for (y=0; y < (ssize_t) image->rows; y++)
2151
0
        {
2152
0
          const Quantum
2153
0
            *magick_restrict p;
2154
2155
0
          ssize_t
2156
0
            x;
2157
2158
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2159
0
          if (p == (const Quantum *) NULL)
2160
0
            break;
2161
0
          for (x=0; x < (ssize_t) image->columns; x++)
2162
0
          {
2163
0
            if (image->depth <= 8)
2164
0
              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2165
0
                "%u %u %u",ScaleQuantumToChar(GetPixelRed(image,p)),
2166
0
                ScaleQuantumToChar(GetPixelGreen(image,p)),
2167
0
                ScaleQuantumToChar(GetPixelBlue(image,p)));
2168
0
            else
2169
0
              if (image->depth <= 16)
2170
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2171
0
                  "%u %u %u",ScaleQuantumToShort(GetPixelRed(image,p)),
2172
0
                  ScaleQuantumToShort(GetPixelGreen(image,p)),
2173
0
                  ScaleQuantumToShort(GetPixelBlue(image,p)));
2174
0
              else
2175
0
                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
2176
0
                  "%u %u %u",ScaleQuantumToLong(GetPixelRed(image,p)),
2177
0
                  ScaleQuantumToLong(GetPixelGreen(image,p)),
2178
0
                  ScaleQuantumToLong(GetPixelBlue(image,p)));
2179
0
            extent=(size_t) count;
2180
0
            if (q != pixels)
2181
0
              {
2182
0
                if ((size_t) (q-pixels+(ssize_t) extent+1) < sizeof(pixels))
2183
0
                  *q++=' ';
2184
0
                else
2185
0
                  {
2186
0
                    *q++='\n';
2187
0
                    (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2188
0
                    q=pixels;
2189
0
                  }
2190
0
              }
2191
0
            (void) memcpy((char *) q,buffer,extent);
2192
0
            q+=(ptrdiff_t) extent;
2193
0
            p+=(ptrdiff_t) GetPixelChannels(image);
2194
0
          }
2195
0
          if (image->previous == (Image *) NULL)
2196
0
            {
2197
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2198
0
                image->rows);
2199
0
              if (status == MagickFalse)
2200
0
                break;
2201
0
            }
2202
0
        }
2203
0
        if (q != pixels)
2204
0
          {
2205
0
            *q++='\n';
2206
0
            (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2207
0
          }
2208
0
        break;
2209
0
      }
2210
551
      case '4':
2211
551
      {
2212
551
        unsigned char
2213
551
          *pixels;
2214
2215
        /*
2216
          Convert image to a PBM image.
2217
        */
2218
551
        (void) SetImageType(image,BilevelType,exception);
2219
551
        image->depth=1;
2220
551
        quantum_info=AcquireQuantumInfo(image_info,image);
2221
551
        if (quantum_info == (QuantumInfo *) NULL)
2222
551
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2223
551
        SetQuantumMinIsWhite(quantum_info,MagickTrue);
2224
551
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2225
551
        pixels=GetQuantumPixels(quantum_info);
2226
18.4k
        for (y=0; y < (ssize_t) image->rows; y++)
2227
17.8k
        {
2228
17.8k
          const Quantum
2229
17.8k
            *magick_restrict p;
2230
2231
17.8k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2232
17.8k
          if (p == (const Quantum *) NULL)
2233
0
            break;
2234
17.8k
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2235
17.8k
            GrayQuantum,pixels,exception);
2236
17.8k
          count=WriteBlob(image,extent,pixels);
2237
17.8k
          if (count != (ssize_t) extent)
2238
0
            break;
2239
17.8k
          if (image->previous == (Image *) NULL)
2240
17.8k
            {
2241
17.8k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2242
17.8k
                image->rows);
2243
17.8k
              if (status == MagickFalse)
2244
0
                break;
2245
17.8k
            }
2246
17.8k
        }
2247
551
        quantum_info=DestroyQuantumInfo(quantum_info);
2248
551
        break;
2249
551
      }
2250
490
      case '5':
2251
490
      {
2252
490
        unsigned char
2253
490
          *pixels;
2254
2255
        /*
2256
          Convert image to a PGM image.
2257
        */
2258
490
        if (image->depth > 32)
2259
0
          image->depth=32;
2260
490
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2261
490
          ((MagickOffsetType) GetQuantumRange(image->depth)));
2262
490
        (void) WriteBlobString(image,buffer);
2263
490
        quantum_info=AcquireQuantumInfo(image_info,image);
2264
490
        if (quantum_info == (QuantumInfo *) NULL)
2265
490
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2266
490
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2267
490
        pixels=GetQuantumPixels(quantum_info);
2268
490
        extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
2269
5.65k
        for (y=0; y < (ssize_t) image->rows; y++)
2270
5.16k
        {
2271
5.16k
          const Quantum
2272
5.16k
            *magick_restrict p;
2273
2274
5.16k
          ssize_t
2275
5.16k
            x;
2276
2277
5.16k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2278
5.16k
          if (p == (const Quantum *) NULL)
2279
0
            break;
2280
5.16k
          q=pixels;
2281
5.16k
          switch (image->depth)
2282
5.16k
          {
2283
563
            case 8:
2284
1.17k
            case 16:
2285
1.75k
            case 32:
2286
1.75k
            {
2287
1.75k
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2288
1.75k
                GrayQuantum,pixels,exception);
2289
1.75k
              break;
2290
1.17k
            }
2291
3.41k
            default:
2292
3.41k
            {
2293
3.41k
              if (image->depth <= 8)
2294
851
                {
2295
6.57k
                  for (x=0; x < (ssize_t) image->columns; x++)
2296
5.71k
                  {
2297
5.71k
                    if (IsPixelGray(image,p) == MagickFalse)
2298
0
                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2299
0
                        image,p)),max_value);
2300
5.71k
                    else
2301
5.71k
                      {
2302
5.71k
                        if (image->depth == 8)
2303
0
                          pixel=ScaleQuantumToChar(GetPixelRed(image,p));
2304
5.71k
                        else
2305
5.71k
                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2306
5.71k
                            max_value);
2307
5.71k
                      }
2308
5.71k
                    q=PopCharPixel((unsigned char) pixel,q);
2309
5.71k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2310
5.71k
                  }
2311
851
                  extent=(size_t) (q-pixels);
2312
851
                  break;
2313
851
                }
2314
2.56k
              if (image->depth <= 16)
2315
1.05k
                {
2316
4.51k
                  for (x=0; x < (ssize_t) image->columns; x++)
2317
3.46k
                  {
2318
3.46k
                    if (IsPixelGray(image,p) == MagickFalse)
2319
0
                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2320
0
                        p)),max_value);
2321
3.46k
                    else
2322
3.46k
                      {
2323
3.46k
                        if (image->depth == 16)
2324
0
                          pixel=ScaleQuantumToShort(GetPixelRed(image,p));
2325
3.46k
                        else
2326
3.46k
                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
2327
3.46k
                            max_value);
2328
3.46k
                      }
2329
3.46k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2330
3.46k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2331
3.46k
                  }
2332
1.05k
                  extent=(size_t) (q-pixels);
2333
1.05k
                  break;
2334
1.05k
                }
2335
9.95k
              for (x=0; x < (ssize_t) image->columns; x++)
2336
8.44k
              {
2337
8.44k
                if (IsPixelGray(image,p) == MagickFalse)
2338
0
                  pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
2339
0
                    max_value);
2340
8.44k
                else
2341
8.44k
                  {
2342
8.44k
                    if (image->depth == 16)
2343
0
                      pixel=ScaleQuantumToLong(GetPixelRed(image,p));
2344
8.44k
                    else
2345
8.44k
                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2346
8.44k
                  }
2347
8.44k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2348
8.44k
                p+=(ptrdiff_t) GetPixelChannels(image);
2349
8.44k
              }
2350
1.50k
              extent=(size_t) (q-pixels);
2351
1.50k
              break;
2352
2.56k
            }
2353
5.16k
          }
2354
5.16k
          count=WriteBlob(image,extent,pixels);
2355
5.16k
          if (count != (ssize_t) extent)
2356
0
            break;
2357
5.16k
          if (image->previous == (Image *) NULL)
2358
5.16k
            {
2359
5.16k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2360
5.16k
                image->rows);
2361
5.16k
              if (status == MagickFalse)
2362
0
                break;
2363
5.16k
            }
2364
5.16k
        }
2365
490
        quantum_info=DestroyQuantumInfo(quantum_info);
2366
490
        break;
2367
490
      }
2368
770
      case '6':
2369
770
      {
2370
770
        unsigned char
2371
770
          *pixels;
2372
2373
        /*
2374
          Convert image to a PNM image.
2375
        */
2376
770
        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2377
227
          (void) TransformImageColorspace(image,sRGBColorspace,exception);
2378
770
        if (image->depth > 32)
2379
0
          image->depth=32;
2380
770
        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2381
770
          ((MagickOffsetType) GetQuantumRange(image->depth)));
2382
770
        (void) WriteBlobString(image,buffer);
2383
770
        quantum_info=AcquireQuantumInfo(image_info,image);
2384
770
        if (quantum_info == (QuantumInfo *) NULL)
2385
770
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2386
770
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2387
770
        pixels=GetQuantumPixels(quantum_info);
2388
770
        extent=GetQuantumExtent(image,quantum_info,quantum_type);
2389
11.5k
        for (y=0; y < (ssize_t) image->rows; y++)
2390
10.7k
        {
2391
10.7k
          const Quantum
2392
10.7k
            *magick_restrict p;
2393
2394
10.7k
          ssize_t
2395
10.7k
            x;
2396
2397
10.7k
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2398
10.7k
          if (p == (const Quantum *) NULL)
2399
0
            break;
2400
10.7k
          q=pixels;
2401
10.7k
          switch (image->depth)
2402
10.7k
          {
2403
1.03k
            case 8:
2404
2.44k
            case 16:
2405
3.78k
            case 32:
2406
3.78k
            {
2407
3.78k
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2408
3.78k
                quantum_type,pixels,exception);
2409
3.78k
              break;
2410
2.44k
            }
2411
6.96k
            default:
2412
6.96k
            {
2413
6.96k
              if (image->depth <= 8)
2414
2.97k
                {
2415
10.4k
                  for (x=0; x < (ssize_t) image->columns; x++)
2416
7.43k
                  {
2417
7.43k
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2418
7.43k
                    q=PopCharPixel((unsigned char) pixel,q);
2419
7.43k
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2420
7.43k
                    q=PopCharPixel((unsigned char) pixel,q);
2421
7.43k
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2422
7.43k
                    q=PopCharPixel((unsigned char) pixel,q);
2423
7.43k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2424
7.43k
                  }
2425
2.97k
                  extent=(size_t) (q-pixels);
2426
2.97k
                  break;
2427
2.97k
                }
2428
3.98k
              if (image->depth <= 16)
2429
1.62k
                {
2430
7.09k
                  for (x=0; x < (ssize_t) image->columns; x++)
2431
5.46k
                  {
2432
5.46k
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2433
5.46k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2434
5.46k
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2435
5.46k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2436
5.46k
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2437
5.46k
                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2438
5.46k
                    p+=(ptrdiff_t) GetPixelChannels(image);
2439
5.46k
                  }
2440
1.62k
                  extent=(size_t) (q-pixels);
2441
1.62k
                  break;
2442
1.62k
                }
2443
6.64k
              for (x=0; x < (ssize_t) image->columns; x++)
2444
4.27k
              {
2445
4.27k
                pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2446
4.27k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2447
4.27k
                pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2448
4.27k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2449
4.27k
                pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2450
4.27k
                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2451
4.27k
                p+=(ptrdiff_t) GetPixelChannels(image);
2452
4.27k
              }
2453
2.36k
              extent=(size_t) (q-pixels);
2454
2.36k
              break;
2455
3.98k
            }
2456
10.7k
          }
2457
10.7k
          count=WriteBlob(image,extent,pixels);
2458
10.7k
          if (count != (ssize_t) extent)
2459
0
            break;
2460
10.7k
          if (image->previous == (Image *) NULL)
2461
10.7k
            {
2462
10.7k
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2463
10.7k
                image->rows);
2464
10.7k
              if (status == MagickFalse)
2465
0
                break;
2466
10.7k
            }
2467
10.7k
        }
2468
770
        quantum_info=DestroyQuantumInfo(quantum_info);
2469
770
        break;
2470
770
      }
2471
0
      case '7':
2472
0
      {
2473
0
        unsigned char
2474
0
          *pixels;
2475
2476
        /*
2477
          Convert image to a PAM.
2478
        */
2479
0
        if (image->depth > 32)
2480
0
          image->depth=32;
2481
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2482
0
        if (quantum_info == (QuantumInfo *) NULL)
2483
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2484
0
        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
2485
0
        pixels=GetQuantumPixels(quantum_info);
2486
0
        for (y=0; y < (ssize_t) image->rows; y++)
2487
0
        {
2488
0
          const Quantum
2489
0
            *magick_restrict p;
2490
2491
0
          ssize_t
2492
0
            x;
2493
2494
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2495
0
          if (p == (const Quantum *) NULL)
2496
0
            break;
2497
0
          q=pixels;
2498
0
          switch (image->depth)
2499
0
          {
2500
0
            case 8:
2501
0
            case 16:
2502
0
            case 32:
2503
0
            {
2504
0
              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2505
0
                quantum_type,pixels,exception);
2506
0
              break;
2507
0
            }
2508
0
            default:
2509
0
            {
2510
0
              switch (quantum_type)
2511
0
              {
2512
0
                case GrayQuantum:
2513
0
                case GrayAlphaQuantum:
2514
0
                {
2515
0
                  if (image->depth <= 8)
2516
0
                    {
2517
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2518
0
                      {
2519
0
                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2520
0
                          image,p)),max_value);
2521
0
                        q=PopCharPixel((unsigned char) pixel,q);
2522
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2523
0
                          {
2524
0
                            pixel=(unsigned char) ScaleQuantumToAny(
2525
0
                              GetPixelAlpha(image,p),max_value);
2526
0
                            q=PopCharPixel((unsigned char) pixel,q);
2527
0
                          }
2528
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2529
0
                      }
2530
0
                      break;
2531
0
                    }
2532
0
                  if (image->depth <= 16)
2533
0
                    {
2534
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2535
0
                      {
2536
0
                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
2537
0
                          image,p)),max_value);
2538
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2539
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2540
0
                          {
2541
0
                            pixel=(unsigned char) ScaleQuantumToAny(
2542
0
                              GetPixelAlpha(image,p),max_value);
2543
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2544
0
                          }
2545
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2546
0
                      }
2547
0
                      break;
2548
0
                    }
2549
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2550
0
                  {
2551
0
                    pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
2552
0
                      p)),max_value);
2553
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2554
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2555
0
                      {
2556
0
                        pixel=(unsigned char) ScaleQuantumToAny(
2557
0
                          GetPixelAlpha(image,p),max_value);
2558
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2559
0
                      }
2560
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2561
0
                  }
2562
0
                  break;
2563
0
                }
2564
0
                case CMYKQuantum:
2565
0
                case CMYKAQuantum:
2566
0
                {
2567
0
                  if (image->depth <= 8)
2568
0
                    {
2569
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2570
0
                      {
2571
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2572
0
                        q=PopCharPixel((unsigned char) pixel,q);
2573
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2574
0
                          max_value);
2575
0
                        q=PopCharPixel((unsigned char) pixel,q);
2576
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2577
0
                          max_value);
2578
0
                        q=PopCharPixel((unsigned char) pixel,q);
2579
0
                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2580
0
                          max_value);
2581
0
                        q=PopCharPixel((unsigned char) pixel,q);
2582
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2583
0
                          {
2584
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2585
0
                              max_value);
2586
0
                            q=PopCharPixel((unsigned char) pixel,q);
2587
0
                          }
2588
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2589
0
                      }
2590
0
                      break;
2591
0
                    }
2592
0
                  if (image->depth <= 16)
2593
0
                    {
2594
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2595
0
                      {
2596
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2597
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2598
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2599
0
                          max_value);
2600
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2601
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2602
0
                          max_value);
2603
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2604
0
                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
2605
0
                          max_value);
2606
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2607
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2608
0
                          {
2609
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2610
0
                              max_value);
2611
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2612
0
                          }
2613
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2614
0
                      }
2615
0
                      break;
2616
0
                    }
2617
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2618
0
                  {
2619
0
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2620
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2621
0
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2622
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2623
0
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2624
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2625
0
                    pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
2626
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2627
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2628
0
                      {
2629
0
                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2630
0
                          max_value);
2631
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2632
0
                      }
2633
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2634
0
                  }
2635
0
                  break;
2636
0
                }
2637
0
                default:
2638
0
                {
2639
0
                  if (image->depth <= 8)
2640
0
                    {
2641
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2642
0
                      {
2643
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2644
0
                        q=PopCharPixel((unsigned char) pixel,q);
2645
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2646
0
                          max_value);
2647
0
                        q=PopCharPixel((unsigned char) pixel,q);
2648
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2649
0
                          max_value);
2650
0
                        q=PopCharPixel((unsigned char) pixel,q);
2651
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2652
0
                          {
2653
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2654
0
                              max_value);
2655
0
                            q=PopCharPixel((unsigned char) pixel,q);
2656
0
                          }
2657
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2658
0
                      }
2659
0
                      break;
2660
0
                    }
2661
0
                  if (image->depth <= 16)
2662
0
                    {
2663
0
                      for (x=0; x < (ssize_t) image->columns; x++)
2664
0
                      {
2665
0
                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2666
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2667
0
                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
2668
0
                          max_value);
2669
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2670
0
                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
2671
0
                          max_value);
2672
0
                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2673
0
                        if (image->alpha_trait != UndefinedPixelTrait)
2674
0
                          {
2675
0
                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2676
0
                              max_value);
2677
0
                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
2678
0
                          }
2679
0
                        p+=(ptrdiff_t) GetPixelChannels(image);
2680
0
                      }
2681
0
                      break;
2682
0
                    }
2683
0
                  for (x=0; x < (ssize_t) image->columns; x++)
2684
0
                  {
2685
0
                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
2686
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2687
0
                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
2688
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2689
0
                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
2690
0
                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2691
0
                    if (image->alpha_trait != UndefinedPixelTrait)
2692
0
                      {
2693
0
                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
2694
0
                          max_value);
2695
0
                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
2696
0
                      }
2697
0
                    p+=(ptrdiff_t) GetPixelChannels(image);
2698
0
                  }
2699
0
                  break;
2700
0
                }
2701
0
              }
2702
0
              extent=(size_t) (q-pixels);
2703
0
              break;
2704
0
            }
2705
0
          }
2706
0
          count=WriteBlob(image,extent,pixels);
2707
0
          if (count != (ssize_t) extent)
2708
0
            break;
2709
0
          if (image->previous == (Image *) NULL)
2710
0
            {
2711
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2712
0
                image->rows);
2713
0
              if (status == MagickFalse)
2714
0
                break;
2715
0
            }
2716
0
        }
2717
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2718
0
        break;
2719
0
      }
2720
0
      case 'F':
2721
0
      case 'f':
2722
0
      {
2723
0
        unsigned char
2724
0
          *pixels;
2725
2726
0
        (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2727
0
          "1.0\n");
2728
0
        image->depth=32;
2729
0
        quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
2730
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2731
0
        if (quantum_info == (QuantumInfo *) NULL)
2732
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2733
0
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2734
0
        if (status == MagickFalse)
2735
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2736
0
        pixels=GetQuantumPixels(quantum_info);
2737
0
        for (y=(ssize_t) image->rows-1; y >= 0; y--)
2738
0
        {
2739
0
          const Quantum
2740
0
            *magick_restrict p;
2741
2742
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2743
0
          if (p == (const Quantum *) NULL)
2744
0
            break;
2745
0
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2746
0
            quantum_type,pixels,exception);
2747
0
          (void) WriteBlob(image,extent,pixels);
2748
0
          if (image->previous == (Image *) NULL)
2749
0
            {
2750
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2751
0
                image->rows);
2752
0
              if (status == MagickFalse)
2753
0
                break;
2754
0
            }
2755
0
        }
2756
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2757
0
        break;
2758
0
      }
2759
0
      case 'H':
2760
0
      case 'h':
2761
0
      {
2762
0
        unsigned char
2763
0
          *pixels;
2764
2765
0
        (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
2766
0
          "1.0\n");
2767
0
        image->depth=16;
2768
0
        quantum_type=format == 'h' ? GrayQuantum : RGBQuantum;
2769
0
        quantum_info=AcquireQuantumInfo(image_info,image);
2770
0
        if (quantum_info == (QuantumInfo *) NULL)
2771
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2772
0
        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2773
0
        if (status == MagickFalse)
2774
0
          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2775
0
        pixels=GetQuantumPixels(quantum_info);
2776
0
        for (y=(ssize_t) image->rows-1; y >= 0; y--)
2777
0
        {
2778
0
          const Quantum
2779
0
            *magick_restrict p;
2780
2781
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2782
0
          if (p == (const Quantum *) NULL)
2783
0
            break;
2784
0
          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2785
0
            quantum_type,pixels,exception);
2786
0
          (void) WriteBlob(image,extent,pixels);
2787
0
          if (image->previous == (Image *) NULL)
2788
0
            {
2789
0
              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2790
0
                image->rows);
2791
0
              if (status == MagickFalse)
2792
0
                break;
2793
0
            }
2794
0
        }
2795
0
        quantum_info=DestroyQuantumInfo(quantum_info);
2796
0
        break;
2797
0
      }
2798
0
      default:
2799
0
        break;
2800
1.81k
    }
2801
1.81k
    if (GetNextImageInList(image) == (Image *) NULL)
2802
1.81k
      break;
2803
0
    image=SyncNextImageInList(image);
2804
0
    status=SetImageProgress(image,SaveImagesTag,scene++,number_scenes);
2805
0
    if (status == MagickFalse)
2806
0
      break;
2807
0
  } while (image_info->adjoin != MagickFalse);
2808
1.81k
  if (CloseBlob(image) == MagickFalse)
2809
0
    status=MagickFalse;
2810
1.81k
  return(status);
2811
1.81k
}