Coverage Report

Created: 2026-07-20 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/imagemagick/coders/json.c
Line
Count
Source
1
/*
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%                                                                             %
4
%                                                                             %
5
%                                                                             %
6
%                        JJJJJ  SSSSS   OOO   N   N                           %
7
%                          J    SS     O   O  NN  N                           %
8
%                          J     SSS   O   O  N N N                           %
9
%                        J J       SS  O   O  N  NN                           %
10
%                        JJJ    SSSSS   OOO   N   N                           %
11
%                                                                             %
12
%                                                                             %
13
%                  Write Info About the Image in JSON Format.                 %
14
%                                                                             %
15
%                              Software Design                                %
16
%                                   Cristy                                    %
17
%                                January 2014                                 %
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/license/                                         %
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/artifact.h"
44
#include "MagickCore/attribute.h"
45
#include "MagickCore/blob.h"
46
#include "MagickCore/blob-private.h"
47
#include "MagickCore/cache.h"
48
#include "MagickCore/colorspace.h"
49
#include "MagickCore/colorspace-private.h"
50
#include "MagickCore/constitute.h"
51
#include "MagickCore/constitute-private.h"
52
#include "MagickCore/exception.h"
53
#include "MagickCore/exception-private.h"
54
#include "MagickCore/feature.h"
55
#include "MagickCore/image.h"
56
#include "MagickCore/image-private.h"
57
#include "MagickCore/list.h"
58
#include "MagickCore/locale-private.h"
59
#include "MagickCore/magick.h"
60
#include "MagickCore/memory_.h"
61
#include "MagickCore/monitor.h"
62
#include "MagickCore/monitor-private.h"
63
#include "MagickCore/option.h"
64
#include "MagickCore/pixel.h"
65
#include "MagickCore/pixel-accessor.h"
66
#include "MagickCore/prepress.h"
67
#include "MagickCore/property.h"
68
#include "MagickCore/quantum-private.h"
69
#include "MagickCore/registry.h"
70
#include "MagickCore/signature.h"
71
#include "MagickCore/static.h"
72
#include "MagickCore/statistic.h"
73
#include "MagickCore/string_.h"
74
#include "MagickCore/string-private.h"
75
#include "MagickCore/utility.h"
76
#include "MagickCore/version.h"
77
#include "MagickCore/module.h"
78
#include "coders/coders-private.h"
79
80
/*
81
  Typedef declarations.
82
*/
83
typedef struct _IPTCInfo
84
{
85
  long
86
    dataset,
87
    record;
88
89
  size_t
90
    values_length;
91
92
  char
93
    tag[32],
94
    ***values;
95
} IPTCInfo;
96

97
/*
98
  Forward declarations.
99
*/
100
static MagickBooleanType
101
  WriteJSONImage(const ImageInfo *,Image *,ExceptionInfo *);
102

103
/*
104
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
%                                                                             %
106
%                                                                             %
107
%                                                                             %
108
%   R e g i s t e r J S O N I m a g e                                         %
109
%                                                                             %
110
%                                                                             %
111
%                                                                             %
112
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
%
114
%  RegisterJSONImage() adds attributes for the JSON image format to
115
%  the list of supported formats.  The attributes include the image format
116
%  tag, a method to read and/or write the format, whether the format
117
%  supports the saving of more than one frame to the same file or blob,
118
%  whether the format supports native in-memory I/O, and a brief
119
%  description of the format.
120
%
121
%  The format of the RegisterJSONImage method is:
122
%
123
%      size_t RegisterJSONImage(void)
124
%
125
*/
126
ModuleExport size_t RegisterJSONImage(void)
127
7
{
128
7
  MagickInfo
129
7
    *entry;
130
131
7
  entry=AcquireMagickInfo("JSON","JSON","The image format and characteristics");
132
7
  entry->encoder=(EncodeImageHandler *) WriteJSONImage;
133
7
  entry->mime_type=ConstantString("application/json");
134
7
  entry->flags|=CoderEndianSupportFlag;
135
7
  entry->flags^=CoderBlobSupportFlag;
136
7
  (void) RegisterMagickInfo(entry);
137
7
  return(MagickImageCoderSignature);
138
7
}
139

140
/*
141
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
%                                                                             %
143
%                                                                             %
144
%                                                                             %
145
%   U n r e g i s t e r J S O N I m a g e                                     %
146
%                                                                             %
147
%                                                                             %
148
%                                                                             %
149
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
%
151
%  UnregisterJSONImage() removes format registrations made by the
152
%  JSON module from the list of supported formats.
153
%
154
%  The format of the UnregisterJSONImage method is:
155
%
156
%      UnregisterJSONImage(void)
157
%
158
*/
159
ModuleExport void UnregisterJSONImage(void)
160
0
{
161
0
  (void) UnregisterMagickInfo("JSON");
162
0
}
163

164
/*
165
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166
%                                                                             %
167
%                                                                             %
168
%                                                                             %
169
%   W r i t e J S O N I m a g e                                               %
170
%                                                                             %
171
%                                                                             %
172
%                                                                             %
173
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
%
175
%  WriteJSONImage writes the image attributes in the JSON format.
176
%
177
%  The format of the WriteJSONImage method is:
178
%
179
%      MagickBooleanType WriteJSONImage(const ImageInfo *image_info,
180
%        Image *image,ExceptionInfo *exception)
181
%
182
%  A description of each parameter follows.
183
%
184
%    o image_info: the image info.
185
%
186
%    o image:  The image.
187
%
188
%    o exception: return any errors or warnings in this structure.
189
%
190
*/
191
192
static void JSONFormatLocaleFile(FILE *file,const char *format,
193
  const char *value)
194
0
{
195
0
  char
196
0
    *escaped_json;
197
198
0
  char
199
0
    *q;
200
201
0
  const char
202
0
    *p;
203
204
0
  size_t
205
0
    length;
206
207
0
  assert(format != (const char *) NULL);
208
0
  if ((value == (char *) NULL) || (*value == '\0'))
209
0
    {
210
0
      (void) FormatLocaleFile(file,format,"null");
211
0
      return;
212
0
    }
213
0
  length=strlen(value)+2;
214
  /*
215
    Find all the chars that need escaping and increase the dest length counter.
216
  */
217
0
  for (p=value; *p != '\0'; p++)
218
0
  {
219
0
    switch (*p)
220
0
    {
221
0
      case '"':
222
0
      case '\b':
223
0
      case '\f':
224
0
      case '\n':
225
0
      case '\r':
226
0
      case '\t':
227
0
      case '\\':
228
0
      {
229
0
        if (~length < 1)
230
0
          return;
231
0
        length++;
232
0
        break;
233
0
      }
234
0
      default:
235
0
      {
236
0
        if (((int) *p >= 0x00) && ((int) *p <= 0x1f))
237
0
          length+=6;
238
0
        break;
239
0
      }
240
0
    }
241
0
  }
242
0
  escaped_json=(char *) NULL;
243
0
  if (~length >= (MagickPathExtent-1))
244
0
    escaped_json=(char *) AcquireQuantumMemory(length+MagickPathExtent,
245
0
      sizeof(*escaped_json));
246
0
  if (escaped_json == (char *) NULL)
247
0
    {
248
0
      (void) FormatLocaleFile(file,format,"null");
249
0
      return;
250
0
    }
251
0
  q=escaped_json;
252
0
  *q++='"';
253
0
  for (p=value; *p != '\0'; p++)
254
0
  {
255
0
    switch (*p)
256
0
    {
257
0
      case '"':
258
0
      {
259
0
        *q++='\\';
260
0
        *q++=(*p);
261
0
        break;
262
0
      }
263
0
      case '\b':
264
0
      {
265
0
        *q++='\\';
266
0
        *q++='b';
267
0
        break;
268
0
      }
269
0
      case '\f':
270
0
      {
271
0
        *q++='\\';
272
0
        *q++='f';
273
0
        break;
274
0
      }
275
0
      case '\n':
276
0
      {
277
0
        *q++='\\';
278
0
        *q++='n';
279
0
        break;
280
0
      }
281
0
      case '\r':
282
0
      {
283
0
        *q++='\\';
284
0
        *q++='r';
285
0
        break;
286
0
      }
287
0
      case '\t':
288
0
      {
289
0
        *q++='\\';
290
0
        *q++='t';
291
0
        break;
292
0
      }
293
0
      case '\\':
294
0
      {
295
0
        *q++='\\';
296
0
        *q++='\\';
297
0
        break;
298
0
      }
299
0
      default:
300
0
      {
301
0
        if (((int) *p >= 0x00) && ((int) *p <= 0x1f))
302
0
          {
303
0
            (void) FormatLocaleString(q,7,"\\u%04X",(int) *p);
304
0
            q+=(ptrdiff_t) 6;
305
0
            break;
306
0
          }
307
0
        *q++=(*p);
308
0
        break;
309
0
      }
310
0
    }
311
0
  }
312
0
  *q++='"';
313
0
  *q='\0';
314
0
  (void) FormatLocaleFile(file,format,escaped_json);
315
0
  (void) DestroyString(escaped_json);
316
0
}
317
318
static ChannelStatistics *GetLocationStatistics(const Image *image,
319
  const StatisticType type,ExceptionInfo *exception)
320
0
{
321
0
  ChannelStatistics
322
0
    *channel_statistics;
323
324
0
  ssize_t
325
0
    i;
326
327
0
  ssize_t
328
0
    y;
329
330
0
  assert(image != (Image *) NULL);
331
0
  assert(image->signature == MagickCoreSignature);
332
0
  if (IsEventLogging() != MagickFalse)
333
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
334
0
  channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
335
0
    MaxPixelChannels+1,sizeof(*channel_statistics));
336
0
  if (channel_statistics == (ChannelStatistics *) NULL)
337
0
    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
338
0
  (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
339
0
    sizeof(*channel_statistics));
340
0
  for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
341
0
  {
342
0
    switch (type)
343
0
    {
344
0
      case MaximumStatistic:
345
0
      default:
346
0
      {
347
0
        channel_statistics[i].maxima=(-MagickMaximumValue);
348
0
        break;
349
0
      }
350
0
      case MinimumStatistic:
351
0
      {
352
0
        channel_statistics[i].minima=MagickMaximumValue;
353
0
        break;
354
0
      }
355
0
    }
356
0
  }
357
0
  for (y=0; y < (ssize_t) image->rows; y++)
358
0
  {
359
0
    const Quantum
360
0
      *magick_restrict p;
361
362
0
    ssize_t
363
0
      x;
364
365
0
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
366
0
    if (p == (const Quantum *) NULL)
367
0
      break;
368
0
    for (x=0; x < (ssize_t) image->columns; x++)
369
0
    {
370
0
      if (GetPixelReadMask(image,p) <= (QuantumRange/2))
371
0
        {
372
0
          p+=(ptrdiff_t) GetPixelChannels(image);
373
0
          continue;
374
0
        }
375
0
      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
376
0
      {
377
0
        PixelChannel channel = GetPixelChannelChannel(image,i);
378
0
        PixelTrait traits = GetPixelChannelTraits(image,channel);
379
0
        if (traits == UndefinedPixelTrait)
380
0
          continue;
381
0
        switch (type)
382
0
        {
383
0
          case MaximumStatistic:
384
0
          default:
385
0
          {
386
0
            if ((double) p[i] > channel_statistics[channel].maxima)
387
0
              channel_statistics[channel].maxima=(double) p[i];
388
0
            break;
389
0
          }
390
0
          case MinimumStatistic:
391
0
          {
392
0
            if ((double) p[i] < channel_statistics[channel].minima)
393
0
              channel_statistics[channel].minima=(double) p[i];
394
0
            break;
395
0
          }
396
0
        }
397
0
      }
398
0
      p+=(ptrdiff_t) GetPixelChannels(image);
399
0
    }
400
0
  }
401
0
  return(channel_statistics);
402
0
}
403
404
static ssize_t PrintChannelFeatures(FILE *file,const PixelChannel channel,
405
  const char *name,const MagickBooleanType separator,
406
  const ChannelFeatures *channel_features)
407
0
{
408
0
#define PrintFeature(feature) \
409
0
  GetMagickPrecision(),(feature)[0], \
410
0
  GetMagickPrecision(),(feature)[1], \
411
0
  GetMagickPrecision(),(feature)[2], \
412
0
  GetMagickPrecision(),(feature)[3], \
413
0
  GetMagickPrecision(),((feature)[0]+(feature)[1]+(feature)[2]+(feature)[3])/4.0 \
414
0
415
0
#define FeaturesFormat "      \"%s\": {\n" \
416
0
  "        \"angularSecondMoment\": {\n" \
417
0
  "          \"horizontal\": %.*g,\n" \
418
0
  "          \"vertical\": %.*g,\n" \
419
0
  "          \"leftDiagonal\": %.*g,\n" \
420
0
  "          \"rightDiagonal\": %.*g,\n" \
421
0
  "          \"average\": %.*g\n" \
422
0
  "        },\n" \
423
0
  "        \"contrast\": {\n" \
424
0
  "          \"horizontal\": %.*g,\n" \
425
0
  "          \"vertical\": %.*g,\n" \
426
0
  "          \"leftDiagonal\": %.*g,\n" \
427
0
  "          \"rightDiagonal\": %.*g,\n" \
428
0
  "          \"average\": %.*g\n" \
429
0
  "        },\n" \
430
0
  "        \"correlation\": {\n" \
431
0
  "          \"horizontal\": %.*g,\n" \
432
0
  "          \"vertical\": %.*g,\n" \
433
0
  "          \"leftDiagonal\": %.*g,\n" \
434
0
  "          \"rightDiagonal\": %.*g,\n" \
435
0
  "          \"average\": %.*g\n" \
436
0
  "        },\n" \
437
0
  "        \"sumOfSquaresVariance\": {\n" \
438
0
  "          \"horizontal\": %.*g,\n" \
439
0
  "          \"vertical\": %.*g,\n" \
440
0
  "          \"leftDiagonal\": %.*g,\n" \
441
0
  "          \"rightDiagonal\": %.*g,\n" \
442
0
  "          \"average\": %.*g\n" \
443
0
  "        },\n" \
444
0
  "        \"inverseDifferenceMoment\": {\n" \
445
0
  "          \"horizontal\": %.*g,\n" \
446
0
  "          \"vertical\": %.*g,\n" \
447
0
  "          \"leftDiagonal\": %.*g,\n" \
448
0
  "          \"rightDiagonal\": %.*g,\n" \
449
0
  "          \"average\": %.*g\n" \
450
0
  "        },\n" \
451
0
  "        \"sumAverage\": {\n" \
452
0
  "          \"horizontal\": %.*g,\n" \
453
0
  "          \"vertical\": %.*g,\n" \
454
0
  "          \"leftDiagonal\": %.*g,\n" \
455
0
  "          \"rightDiagonal\": %.*g,\n" \
456
0
  "          \"average\": %.*g\n" \
457
0
  "        },\n" \
458
0
  "        \"sumVariance\": {\n" \
459
0
  "          \"horizontal\": %.*g,\n" \
460
0
  "          \"vertical\": %.*g,\n" \
461
0
  "          \"leftDiagonal\": %.*g,\n" \
462
0
  "          \"rightDiagonal\": %.*g,\n" \
463
0
  "          \"average\": %.*g\n" \
464
0
  "        },\n" \
465
0
  "        \"sumEntropy\": {\n" \
466
0
  "          \"horizontal\": %.*g,\n" \
467
0
  "          \"vertical\": %.*g,\n" \
468
0
  "          \"leftDiagonal\": %.*g,\n" \
469
0
  "          \"rightDiagonal\": %.*g,\n" \
470
0
  "          \"average\": %.*g\n" \
471
0
  "        },\n" \
472
0
  "        \"entropy\": {\n" \
473
0
  "          \"horizontal\": %.*g,\n" \
474
0
  "          \"vertical\": %.*g,\n" \
475
0
  "          \"leftDiagonal\": %.*g,\n" \
476
0
  "          \"rightDiagonal\": %.*g,\n" \
477
0
  "          \"average\": %.*g\n" \
478
0
  "        },\n" \
479
0
  "        \"differenceVariance\": {\n" \
480
0
  "          \"horizontal\": %.*g,\n" \
481
0
  "          \"vertical\": %.*g,\n" \
482
0
  "          \"leftDiagonal\": %.*g,\n" \
483
0
  "          \"rightDiagonal\": %.*g,\n" \
484
0
  "          \"average\": %.*g\n" \
485
0
  "        },\n" \
486
0
  "        \"differenceEntropy\": {\n" \
487
0
  "          \"horizontal\": %.*g,\n" \
488
0
  "          \"vertical\": %.*g,\n" \
489
0
  "          \"leftDiagonal\": %.*g,\n" \
490
0
  "          \"rightDiagonal\": %.*g,\n" \
491
0
  "          \"average\": %.*g\n" \
492
0
  "        },\n" \
493
0
  "        \"informationMeasureOfCorrelation1\": {\n" \
494
0
  "          \"horizontal\": %.*g,\n" \
495
0
  "          \"vertical\": %.*g,\n" \
496
0
  "          \"leftDiagonal\": %.*g,\n" \
497
0
  "          \"rightDiagonal\": %.*g,\n" \
498
0
  "          \"average\": %.*g\n" \
499
0
  "        },\n" \
500
0
  "        \"informationMeasureOfCorrelation2\": {\n" \
501
0
  "          \"horizontal\": %.*g,\n" \
502
0
  "          \"vertical\": %.*g,\n" \
503
0
  "          \"leftDiagonal\": %.*g,\n" \
504
0
  "          \"rightDiagonal\": %.*g,\n" \
505
0
  "          \"average\": %.*g\n" \
506
0
  "        },\n" \
507
0
  "        \"maximumCorrelationCoefficient\": {\n" \
508
0
  "          \"horizontal\": %.*g,\n" \
509
0
  "          \"vertical\": %.*g,\n" \
510
0
  "          \"leftDiagonal\": %.*g,\n" \
511
0
  "          \"rightDiagonal\": %.*g,\n" \
512
0
  "          \"average\": %.*g\n" \
513
0
  "        }\n"
514
515
0
  char
516
0
    *buffer;
517
518
0
  ssize_t
519
0
    n;
520
521
0
  buffer=AcquireString((char *) NULL);
522
0
  n=FormatLocaleString(buffer,MagickPathExtent,FeaturesFormat,name,
523
0
    PrintFeature(channel_features[channel].angular_second_moment),
524
0
    PrintFeature(channel_features[channel].contrast),
525
0
    PrintFeature(channel_features[channel].correlation),
526
0
    PrintFeature(channel_features[channel].variance_sum_of_squares),
527
0
    PrintFeature(channel_features[channel].inverse_difference_moment),
528
0
    PrintFeature(channel_features[channel].sum_average),
529
0
    PrintFeature(channel_features[channel].sum_variance),
530
0
    PrintFeature(channel_features[channel].sum_entropy),
531
0
    PrintFeature(channel_features[channel].entropy),
532
0
    PrintFeature(channel_features[channel].difference_variance),
533
0
    PrintFeature(channel_features[channel].difference_entropy),
534
0
    PrintFeature(channel_features[channel].measure_of_correlation_1),
535
0
    PrintFeature(channel_features[channel].measure_of_correlation_2),
536
0
    PrintFeature(channel_features[channel].maximum_correlation_coefficient));
537
0
  (void) SubstituteString(&buffer,": -inf",": null");
538
0
  (void) SubstituteString(&buffer,": inf",": null");
539
0
  (void) SubstituteString(&buffer,": -nan",": null");
540
0
  (void) SubstituteString(&buffer,": nan",": null");
541
0
  n=FormatLocaleFile(file,"%s",buffer);
542
0
  buffer=DestroyString(buffer);
543
0
  (void) FormatLocaleFile(file,"      }");
544
0
  if (separator != MagickFalse)
545
0
    (void) FormatLocaleFile(file,",");
546
0
  (void) FormatLocaleFile(file,"\n");
547
0
  return(n);
548
0
}
549
550
static ssize_t PrintChannelLocations(FILE *file,const Image *image,
551
  const PixelChannel channel,const char *name,const StatisticType type,
552
  const size_t max_locations,const MagickBooleanType separator,
553
  const ChannelStatistics *channel_statistics)
554
0
{
555
0
  double
556
0
    target;
557
558
0
  ExceptionInfo
559
0
    *exception;
560
561
0
  ssize_t
562
0
    n,
563
0
    y;
564
565
0
  switch (type)
566
0
  {
567
0
    case MaximumStatistic:
568
0
    default:
569
0
    {
570
0
      target=channel_statistics[channel].maxima;
571
0
      break;
572
0
    }
573
0
    case MinimumStatistic:
574
0
    {
575
0
      target=channel_statistics[channel].minima;
576
0
      break;
577
0
    }
578
0
  }
579
0
  (void) FormatLocaleFile(file,"      \"%s\": {\n        \"intensity\": "
580
0
    "%.*g,\n",name,GetMagickPrecision(),QuantumScale*target);
581
0
  exception=AcquireExceptionInfo();
582
0
  n=0;
583
0
  for (y=0; y < (ssize_t) image->rows; y++)
584
0
  {
585
0
    const Quantum
586
0
      *p;
587
588
0
    ssize_t
589
0
      offset,
590
0
      x;
591
592
0
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
593
0
    if (p == (const Quantum *) NULL)
594
0
      break;
595
0
    for (x=0; x < (ssize_t) image->columns; x++)
596
0
    {
597
0
      MagickBooleanType
598
0
        match;
599
600
0
      PixelTrait traits = GetPixelChannelTraits(image,channel);
601
0
      if (traits == UndefinedPixelTrait)
602
0
        continue;
603
0
      offset=GetPixelChannelOffset(image,channel);
604
0
      match=fabs((double) p[offset]-target) < 0.5 ? MagickTrue : MagickFalse;
605
0
      if (match != MagickFalse)
606
0
        {
607
0
          if ((max_locations != 0) && (n >= (ssize_t) max_locations))
608
0
            break;
609
0
          if (n != 0)
610
0
            (void) FormatLocaleFile(file,",\n");
611
0
          (void) FormatLocaleFile(file,"        \"location%.17g\": {\n"
612
0
            "          \"x\": %.17g,\n          \"y\": %.17g\n"
613
0
            "        }",(double) n,(double) x,(double) y);
614
0
          n++;
615
0
        }
616
0
      p+=(ptrdiff_t) GetPixelChannels(image);
617
0
    }
618
0
    if (x < (ssize_t) image->columns)
619
0
      break;
620
0
  }
621
0
  (void) FormatLocaleFile(file,"\n      }");
622
0
  if (separator != MagickFalse)
623
0
    (void) FormatLocaleFile(file,",");
624
0
  (void) FormatLocaleFile(file,"\n");
625
0
  return(n);
626
0
}
627
628
static ssize_t PrintChannelMoments(FILE *file,const PixelChannel channel,
629
  const char *name,const MagickBooleanType separator,
630
  const ChannelMoments *channel_moments)
631
0
{
632
0
  ssize_t
633
0
    i;
634
635
0
  ssize_t
636
0
    n;
637
638
0
  n=FormatLocaleFile(file,"      \"%s\": {\n",name);
639
0
  n+=FormatLocaleFile(file,"        \"centroid\": {\n "
640
0
    "          \"x\": %.*g,\n"
641
0
    "           \"y\": %.*g\n        },\n",
642
0
    GetMagickPrecision(),channel_moments[channel].centroid.x,
643
0
    GetMagickPrecision(),channel_moments[channel].centroid.y);
644
0
  n+=FormatLocaleFile(file,"        \"ellipseSemiMajorMinorAxis\": {\n"
645
0
    "          \"x\": %.*g,\n"
646
0
    "          \"y\": %.*g\n        },\n",
647
0
    GetMagickPrecision(),channel_moments[channel].ellipse_axis.x,
648
0
    GetMagickPrecision(),channel_moments[channel].ellipse_axis.y);
649
0
  n+=FormatLocaleFile(file,"        \"ellipseAngle\": %.*g,\n",
650
0
    GetMagickPrecision(),channel_moments[channel].ellipse_angle);
651
0
  n+=FormatLocaleFile(file,"        \"ellipseEccentricity\": %.*g,\n",
652
0
    GetMagickPrecision(),channel_moments[channel].ellipse_eccentricity);
653
0
  n+=FormatLocaleFile(file,"        \"ellipseIntensity\": %.*g,\n",
654
0
    GetMagickPrecision(),channel_moments[channel].ellipse_intensity);
655
0
  for (i=0; i < 7; i++)
656
0
    n+=FormatLocaleFile(file,"        \"I%.17g\": %.*g,\n",i+1.0,
657
0
      GetMagickPrecision(),channel_moments[channel].invariant[i]);
658
0
  n+=FormatLocaleFile(file,"        \"I%.17g\": %.*g\n",i+1.0,
659
0
    GetMagickPrecision(),channel_moments[channel].invariant[i]);
660
0
  (void) FormatLocaleFile(file,"      }");
661
0
  if (separator != MagickFalse)
662
0
    (void) FormatLocaleFile(file,",");
663
0
  (void) FormatLocaleFile(file,"\n");
664
0
  return(n);
665
0
}
666
667
static ssize_t PrintChannelPerceptualHash(Image *image,FILE *file,
668
  const ChannelPerceptualHash *channel_phash)
669
0
{
670
0
  ssize_t
671
0
    i;
672
673
0
  ssize_t
674
0
    n = 0;
675
676
0
  (void) FormatLocaleFile(file,"      \"colorspaces\": [ ");
677
0
  for (i=0; i < (ssize_t) channel_phash[0].number_colorspaces; i++)
678
0
  {
679
0
    (void) FormatLocaleFile(file,"\"%s\"",CommandOptionToMnemonic(
680
0
      MagickColorspaceOptions,(ssize_t) channel_phash[0].colorspace[i]));
681
0
    if (i < (ssize_t) (channel_phash[0].number_colorspaces-1))
682
0
      (void) FormatLocaleFile(file,", ");
683
0
  }
684
0
  (void) FormatLocaleFile(file,"],\n");
685
0
  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
686
0
  {
687
0
    ssize_t
688
0
      j;
689
690
0
    PixelChannel channel = GetPixelChannelChannel(image,i);
691
0
    PixelTrait traits = GetPixelChannelTraits(image,channel);
692
0
    if (traits == UndefinedPixelTrait)
693
0
      continue;
694
0
    n=FormatLocaleFile(file,"      \"Channel%.17g\": {\n",(double) channel);
695
0
    for (j=0; j < MaximumNumberOfPerceptualHashes; j++)
696
0
    {
697
0
      ssize_t
698
0
        k;
699
700
0
      n+=FormatLocaleFile(file,"        \"PH%.17g\": [",(double) j+1);
701
0
      for (k=0; k < (ssize_t) channel_phash[0].number_colorspaces; k++)
702
0
      {
703
0
        n+=FormatLocaleFile(file,"%.*g",GetMagickPrecision(),
704
0
          channel_phash[channel].phash[k][j]);
705
0
        if (k < (ssize_t) (channel_phash[0].number_colorspaces-1))
706
0
          n+=FormatLocaleFile(file,", ");
707
0
      }
708
0
      n+=FormatLocaleFile(file,"]");
709
0
      if (j < (MaximumNumberOfPerceptualHashes-1))
710
0
        n+=FormatLocaleFile(file,",\n");
711
0
    }
712
0
    if (i < (ssize_t) (GetPixelChannels(image)-1))
713
0
      n+=FormatLocaleFile(file,"\n      },\n");
714
0
  }
715
0
  n+=FormatLocaleFile(file,"\n      }\n");
716
0
  return(n);
717
0
}
718
719
static ssize_t PrintChannelStatistics(FILE *file,const PixelChannel channel,
720
  const char *name,const double scale,const MagickBooleanType separator,
721
  const ChannelStatistics *channel_statistics)
722
0
{
723
0
#define StatisticsFormat "      \"%s\": {\n        \"min\": %.*g,\n"  \
724
0
  "        \"max\": %.*g,\n        \"mean\": %.*g,\n        \"median\": %.*g,\n        "  \
725
0
  "\"standardDeviation\": %.*g,\n        \"kurtosis\": %.*g,\n        "\
726
0
  "\"skewness\": %.*g,\n        \"entropy\": %.*g\n      }"
727
728
0
  char
729
0
    *buffer;
730
731
0
  ssize_t
732
0
    n;
733
734
0
  buffer=AcquireString((char *) NULL);
735
0
  n=FormatLocaleString(buffer,MagickPathExtent,StatisticsFormat,name,
736
0
    GetMagickPrecision(),
737
0
    channel_statistics[channel].minima == MagickMaximumValue ? 0.0 : (double)
738
0
    ClampToQuantum(scale*channel_statistics[channel].minima),
739
0
    GetMagickPrecision(),
740
0
    channel_statistics[channel].maxima == -MagickMaximumValue ? 0.0 :
741
0
    (double) ClampToQuantum(scale*channel_statistics[channel].maxima),
742
0
    GetMagickPrecision(),scale*channel_statistics[channel].mean,
743
0
    GetMagickPrecision(),scale*channel_statistics[channel].median,
744
0
    GetMagickPrecision(),
745
0
    IsNaN(channel_statistics[channel].standard_deviation) != 0 ? MagickEpsilon :
746
0
    scale*channel_statistics[channel].standard_deviation,GetMagickPrecision(),
747
0
    channel_statistics[channel].kurtosis,GetMagickPrecision(),
748
0
    channel_statistics[channel].skewness,GetMagickPrecision(),
749
0
    channel_statistics[channel].entropy);
750
0
  (void) SubstituteString(&buffer,": -inf",": null");
751
0
  (void) SubstituteString(&buffer,": inf",": null");
752
0
  (void) SubstituteString(&buffer,": -nan",": null");
753
0
  (void) SubstituteString(&buffer,": nan",": null");
754
0
  n=FormatLocaleFile(file,"%s",buffer);
755
0
  buffer=DestroyString(buffer);
756
0
  if (separator != MagickFalse)
757
0
    (void) FormatLocaleFile(file,",");
758
0
  (void) FormatLocaleFile(file,"\n");
759
0
  return(n);
760
0
}
761
762
static void EncodeIptcProfile(FILE *file,const StringInfo *profile)
763
0
{
764
0
  char
765
0
    *attribute,
766
0
    **attribute_list;
767
768
0
  const char
769
0
    *tag;
770
771
0
  IPTCInfo
772
0
    *value,
773
0
    **values;
774
775
0
  long
776
0
    dataset,
777
0
    record,
778
0
    sentinel;
779
780
0
  ssize_t
781
0
    i,
782
0
    j,
783
0
    k;
784
785
0
  size_t
786
0
    count,
787
0
    length,
788
0
    profile_length;
789
790
0
  values=(IPTCInfo **) NULL;
791
0
  count=0;
792
0
  profile_length=GetStringInfoLength(profile);
793
0
  for (i=0; i < (ssize_t) profile_length; i+=(ssize_t) length)
794
0
  {
795
0
    length=1;
796
0
    sentinel=GetStringInfoDatum(profile)[i++];
797
0
    if (sentinel != 0x1c)
798
0
      continue;
799
0
    dataset=GetStringInfoDatum(profile)[i++];
800
0
    record=GetStringInfoDatum(profile)[i++];
801
0
    value=(IPTCInfo *) NULL;
802
0
    for (j=0; j < (ssize_t) count; j++)
803
0
    {
804
0
      if ((values[j]->record == record) && (values[j]->dataset == dataset))
805
0
        value=values[j];
806
0
    }
807
0
    if (value == (IPTCInfo *) NULL)
808
0
      {
809
0
        values=(IPTCInfo **) ResizeQuantumMemory(values,count+1,
810
0
          sizeof(*values));
811
0
        if (values == (IPTCInfo **) NULL)
812
0
          break;
813
0
        value=(IPTCInfo *) AcquireMagickMemory(sizeof(*value));
814
0
        if (value == (IPTCInfo *) NULL)
815
0
          break;
816
        /* Check the tag length in IPTCInfo when a new tag is added */
817
0
        switch (record)
818
0
        {
819
0
          case 5: tag="Image Name"; break;
820
0
          case 7: tag="Edit Status"; break;
821
0
          case 10: tag="Priority"; break;
822
0
          case 15: tag="Category"; break;
823
0
          case 20: tag="Supplemental Category"; break;
824
0
          case 22: tag="Fixture Identifier"; break;
825
0
          case 25: tag="Keyword"; break;
826
0
          case 30: tag="Release Date"; break;
827
0
          case 35: tag="Release Time"; break;
828
0
          case 40: tag="Special Instructions"; break;
829
0
          case 45: tag="Reference Service"; break;
830
0
          case 47: tag="Reference Date"; break;
831
0
          case 50: tag="Reference Number"; break;
832
0
          case 55: tag="Created Date"; break;
833
0
          case 60: tag="Created Time"; break;
834
0
          case 65: tag="Originating Program"; break;
835
0
          case 70: tag="Program Version"; break;
836
0
          case 75: tag="Object Cycle"; break;
837
0
          case 80: tag="Byline"; break;
838
0
          case 85: tag="Byline Title"; break;
839
0
          case 90: tag="City"; break;
840
0
          case 92: tag="Sub-Location"; break;
841
0
          case 95: tag="Province State"; break;
842
0
          case 100: tag="Country Code"; break;
843
0
          case 101: tag="Country"; break;
844
0
          case 103: tag="Original Transmission Reference"; break;
845
0
          case 105: tag="Headline"; break;
846
0
          case 110: tag="Credit"; break;
847
0
          case 115: tag="Src"; break;
848
0
          case 116: tag="Copyright String"; break;
849
0
          case 120: tag="Caption"; break;
850
0
          case 121: tag="Local Caption"; break;
851
0
          case 122: tag="Caption Writer"; break;
852
0
          case 200: tag="Custom Field 1"; break;
853
0
          case 201: tag="Custom Field 2"; break;
854
0
          case 202: tag="Custom Field 3"; break;
855
0
          case 203: tag="Custom Field 4"; break;
856
0
          case 204: tag="Custom Field 5"; break;
857
0
          case 205: tag="Custom Field 6"; break;
858
0
          case 206: tag="Custom Field 7"; break;
859
0
          case 207: tag="Custom Field 8"; break;
860
0
          case 208: tag="Custom Field 9"; break;
861
0
          case 209: tag="Custom Field 10"; break;
862
0
          case 210: tag="Custom Field 11"; break;
863
0
          case 211: tag="Custom Field 12"; break;
864
0
          case 212: tag="Custom Field 13"; break;
865
0
          case 213: tag="Custom Field 14"; break;
866
0
          case 214: tag="Custom Field 15"; break;
867
0
          case 215: tag="Custom Field 16"; break;
868
0
          case 216: tag="Custom Field 17"; break;
869
0
          case 217: tag="Custom Field 18"; break;
870
0
          case 218: tag="Custom Field 19"; break;
871
0
          case 219: tag="Custom Field 20"; break;
872
0
          default: tag="Unknown"; break;
873
0
        }
874
0
        (void) CopyMagickString(value->tag,tag,strlen(tag)+1);
875
0
        value->record=record;
876
0
        value->dataset=dataset;
877
0
        value->values=(char ***) NULL;
878
0
        value->values_length=0;
879
0
        values[count++]=value;
880
0
      }
881
0
    length=((size_t) GetStringInfoDatum(profile)[i++] << 8);
882
0
    length|=GetStringInfoDatum(profile)[i++];
883
0
    attribute=(char *) NULL;
884
0
    if (~length >= (MagickPathExtent-1))
885
0
      attribute=(char *) AcquireQuantumMemory(length+MagickPathExtent,
886
0
        sizeof(*attribute));
887
0
    if (attribute != (char *) NULL)
888
0
      {
889
0
        (void) CopyMagickString(attribute,(char *)
890
0
          GetStringInfoDatum(profile)+i,length+1);
891
0
        attribute_list=StringToList(attribute);
892
0
        if (attribute_list != (char **) NULL)
893
0
          {
894
0
            value->values=(char ***) ResizeQuantumMemory(value->values,
895
0
              value->values_length+1,
896
0
              sizeof(*value->values));
897
0
            if (value->values == (char ***) NULL)
898
0
              break;
899
0
            value->values[value->values_length++]=attribute_list;
900
0
          }
901
0
        attribute=DestroyString(attribute);
902
0
      }
903
0
  }
904
0
  if (values != (IPTCInfo **) NULL)
905
0
    {
906
0
      for (i=0; i < (ssize_t) count; i++)
907
0
      {
908
0
        value=values[i];
909
0
        (void) FormatLocaleFile(file,"        \"%s[%.17g,%.17g]\": ",
910
0
          value->tag,(double) value->dataset,(double) value->record);
911
0
        if (value->values_length == 0)
912
0
          (void) FormatLocaleFile(file,"null,");
913
0
        else
914
0
          {
915
0
            (void) FormatLocaleFile(file,"[");
916
0
            for (j=0; j < (ssize_t) value->values_length; j++)
917
0
            {
918
0
              for (k=0; value->values[j][k] != (char *) NULL; k++)
919
0
              {
920
0
                if (j > 0 || k > 0)
921
0
                  (void) FormatLocaleFile(file,",");
922
0
                JSONFormatLocaleFile(file,"%s",value->values[j][k]);
923
0
                value->values[j][k]=(char *) RelinquishMagickMemory(
924
0
                  value->values[j][k]);
925
0
              }
926
0
              value->values[j]=(char **) RelinquishMagickMemory(
927
0
                value->values[j]);
928
0
            }
929
0
            value->values=(char ***) RelinquishMagickMemory(value->values);
930
0
            (void) FormatLocaleFile(file,"],\n");
931
0
          }
932
0
        values[i]=(IPTCInfo *) RelinquishMagickMemory(values[i]);
933
0
      }
934
0
      values=(IPTCInfo **) RelinquishMagickMemory(values);
935
0
    }
936
0
}
937
938
static MagickBooleanType EncodeImageAttributes(Image *image,FILE *file,
939
  ExceptionInfo *exception)
940
0
{
941
0
  char
942
0
    color[MagickPathExtent],
943
0
    format[MagickPathExtent],
944
0
    key[MagickPathExtent];
945
946
0
  ChannelFeatures
947
0
    *channel_features;
948
949
0
  ChannelMoments
950
0
    *channel_moments;
951
952
0
  ChannelPerceptualHash
953
0
    *channel_phash;
954
955
0
  ChannelStatistics
956
0
    *channel_statistics;
957
958
0
  const char
959
0
    *artifact,
960
0
    *locate,
961
0
    *name,
962
0
    *property,
963
0
    *registry,
964
0
    *value;
965
966
0
  const MagickInfo
967
0
    *magick_info;
968
969
0
  double
970
0
    elapsed_time,
971
0
    scale,
972
0
    user_time,
973
0
    version;
974
975
0
  ImageType
976
0
    type;
977
978
0
  MagickBooleanType
979
0
    ping;
980
981
0
  size_t
982
0
    depth,
983
0
    distance;
984
985
0
  ssize_t
986
0
    i,
987
0
    x,
988
0
    y;
989
990
0
  struct stat
991
0
    properties;
992
993
0
  assert(image != (Image *) NULL);
994
0
  assert(image->signature == MagickCoreSignature);
995
0
  if (IsEventLogging() != MagickFalse)
996
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
997
0
  *format='\0';
998
0
  elapsed_time=GetElapsedTime(&image->timer);
999
0
  user_time=GetUserTime(&image->timer);
1000
0
  GetTimerInfo(&image->timer);
1001
0
  ping=MagickTrue;
1002
0
  if (GetVirtualPixels(image,0,0,1,1,exception) != (const Quantum *) NULL)
1003
0
    ping=MagickFalse;
1004
0
  (void) ping;
1005
0
  (void) SignatureImage(image,exception);
1006
0
  (void) FormatLocaleFile(file,"{\n");
1007
0
  version=1.0;
1008
0
  artifact=GetImageArtifact(image,"json:version");
1009
0
  if (artifact != (const char *) NULL)
1010
0
    version=StringToDouble(artifact,(char **) NULL);
1011
0
  if (version >= 1.0)
1012
0
    (void) FormatLocaleFile(file,"  \"version\": \"%.1f\",\n",version);
1013
0
  if (*image->magick_filename == '\0')
1014
0
    JSONFormatLocaleFile(file,"  \"image\": {\n    \"name\": %s,\n",
1015
0
      image->filename);
1016
0
  else
1017
0
    {
1018
0
      JSONFormatLocaleFile(file,"  \"image\": {\n    \"name\": %s,\n",
1019
0
        image->magick_filename);
1020
0
      if (LocaleCompare(image->magick_filename,image->filename) != 0)
1021
0
        {   
1022
0
          char
1023
0
            filename[MagickPathExtent];
1024
          
1025
0
          GetPathComponent(image->magick_filename,TailPath,filename);
1026
0
          JSONFormatLocaleFile(file,"    \"baseName\": %s,\n",filename);
1027
0
        }
1028
0
    }
1029
0
  properties=(*GetBlobProperties(image));
1030
0
  if (properties.st_mode != 0)
1031
0
    (void) FormatLocaleFile(file,"    \"permissions\": %d%d%d,\n",
1032
0
      (properties.st_mode >> 6) & 0x07,(properties.st_mode >> 3) & 0x07,
1033
0
      (properties.st_mode >> 0) & 0x07);
1034
0
  JSONFormatLocaleFile(file,"    \"format\": %s,\n",image->magick);
1035
0
  magick_info=GetMagickInfo(image->magick,exception);
1036
0
  if ((magick_info != (const MagickInfo *) NULL) &&
1037
0
      (GetMagickDescription(magick_info) != (const char *) NULL))
1038
0
    JSONFormatLocaleFile(file,"    \"formatDescription\": %s,\n",
1039
0
      GetMagickDescription(magick_info));
1040
0
  if ((magick_info != (const MagickInfo *) NULL) &&
1041
0
      (GetMagickMimeType(magick_info) != (const char *) NULL))
1042
0
    JSONFormatLocaleFile(file,"    \"mimeType\": %s,\n",GetMagickMimeType(
1043
0
      magick_info));
1044
0
  JSONFormatLocaleFile(file,"    \"class\": %s,\n",CommandOptionToMnemonic(
1045
0
    MagickClassOptions,(ssize_t) image->storage_class));
1046
0
  (void) FormatLocaleFile(file,"    \"geometry\": {\n"
1047
0
    "      \"width\": %g,\n      \"height\": %g,\n"
1048
0
    "      \"x\": %g,\n      \"y\": %g\n    },\n",
1049
0
    (double) image->columns,(double) image->rows,(double) image->tile_offset.x,
1050
0
    (double) image->tile_offset.y);
1051
0
  if ((image->magick_columns != 0) || (image->magick_rows != 0))
1052
0
    if ((image->magick_columns != image->columns) ||
1053
0
        (image->magick_rows != image->rows))
1054
0
      (void) FormatLocaleFile(file,"    \"baseGeometry\": {\n"
1055
0
        "      \"width\": %g,\n      \"height\": %g\n    },\n",(double)
1056
0
        image->magick_columns,(double) image->magick_rows);
1057
0
  if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
1058
0
    {
1059
0
      (void) FormatLocaleFile(file,"    \"resolution\": {\n"
1060
0
        "      \"x\": %g,\n      \"y\": %g\n    },\n",image->resolution.x,
1061
0
        image->resolution.y);
1062
0
      (void) FormatLocaleFile(file,"    \"printSize\": {\n"
1063
0
        "      \"x\": %.*g,\n      \"y\": %.*g\n    },\n",GetMagickPrecision(),
1064
0
        image->columns/image->resolution.x,GetMagickPrecision(),(double)
1065
0
        image->rows/image->resolution.y);
1066
0
    }
1067
0
  JSONFormatLocaleFile(file,"    \"units\": %s,\n",CommandOptionToMnemonic(
1068
0
    MagickResolutionOptions,(ssize_t) image->units));
1069
0
  type=IdentifyImageCoderType(image,exception);
1070
0
  JSONFormatLocaleFile(file,"    \"type\": %s,\n",CommandOptionToMnemonic(
1071
0
    MagickTypeOptions,(ssize_t) type));
1072
0
  if (image->type != type)
1073
0
    JSONFormatLocaleFile(file,"    \"baseType\": %s,\n",
1074
0
      CommandOptionToMnemonic(MagickTypeOptions,(ssize_t) image->type));
1075
0
  if (version < 1.0)
1076
0
    JSONFormatLocaleFile(file,"    \"endianess\": %s,\n",
1077
0
      CommandOptionToMnemonic(MagickEndianOptions,(ssize_t) image->endian));
1078
0
  else
1079
0
    JSONFormatLocaleFile(file,"    \"endianness\": %s,\n",
1080
0
      CommandOptionToMnemonic(MagickEndianOptions,(ssize_t) image->endian));
1081
0
  locate=GetImageArtifact(image,"identify:locate");
1082
0
  if (locate == (const char *) NULL)
1083
0
    locate=GetImageArtifact(image,"json:locate");
1084
0
  if (locate != (const char *) NULL)
1085
0
    {
1086
0
      const char
1087
0
        *limit;
1088
1089
0
      size_t
1090
0
        max_locations;
1091
1092
0
      StatisticType
1093
0
        statistic_type;
1094
1095
      /*
1096
        Display minimum, maximum, or mean pixel locations.
1097
      */
1098
0
      statistic_type=(StatisticType) ParseCommandOption(MagickStatisticOptions,
1099
0
        MagickFalse,locate);
1100
0
      limit=GetImageArtifact(image,"identify:limit");
1101
0
      if (limit == (const char *) NULL)
1102
0
        limit=GetImageArtifact(image,"json:limit");
1103
0
      max_locations=0;
1104
0
      if (limit != (const char *) NULL)
1105
0
        max_locations=StringToUnsignedLong(limit);
1106
0
      channel_statistics=GetLocationStatistics(image,statistic_type,exception);
1107
0
      if (channel_statistics == (ChannelStatistics *) NULL)
1108
0
        return(MagickFalse);
1109
0
      (void) FormatLocaleFile(file,"    \"channel%s\": {\n",locate);
1110
0
      if (image->alpha_trait != UndefinedPixelTrait)
1111
0
        (void) PrintChannelLocations(file,image,AlphaPixelChannel,"alpha",
1112
0
          statistic_type,max_locations,MagickTrue,channel_statistics);
1113
0
      switch (image->colorspace)
1114
0
      {
1115
0
        case RGBColorspace:
1116
0
        default:
1117
0
        {
1118
0
          (void) PrintChannelLocations(file,image,RedPixelChannel,"red",
1119
0
            statistic_type,max_locations,MagickTrue,channel_statistics);
1120
0
          (void) PrintChannelLocations(file,image,GreenPixelChannel,"green",
1121
0
            statistic_type,max_locations,MagickTrue,channel_statistics);
1122
0
          (void) PrintChannelLocations(file,image,BluePixelChannel,"blue",
1123
0
            statistic_type,max_locations,MagickFalse,channel_statistics);
1124
0
          break;
1125
0
        }
1126
0
        case CMYKColorspace:
1127
0
        {
1128
0
          (void) PrintChannelLocations(file,image,CyanPixelChannel,"cyan",
1129
0
            statistic_type,max_locations,MagickTrue,channel_statistics);
1130
0
          (void) PrintChannelLocations(file,image,MagentaPixelChannel,
1131
0
            "magenta",statistic_type,max_locations,MagickTrue,
1132
0
            channel_statistics);
1133
0
          (void) PrintChannelLocations(file,image,YellowPixelChannel,"yellow",
1134
0
            statistic_type,max_locations,MagickTrue,channel_statistics);
1135
0
          (void) PrintChannelLocations(file,image,BlackPixelChannel,"black",
1136
0
            statistic_type,max_locations,MagickFalse,channel_statistics);
1137
0
          break;
1138
0
        }
1139
0
        case LinearGRAYColorspace:
1140
0
        case GRAYColorspace:
1141
0
        {
1142
0
          (void) PrintChannelLocations(file,image,GrayPixelChannel,"gray",
1143
0
            statistic_type,max_locations,MagickFalse,channel_statistics);
1144
0
          break;
1145
0
        }
1146
0
      }
1147
0
      (void) FormatLocaleFile(file,"    },\n");
1148
0
      channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1149
0
        channel_statistics);
1150
0
    }
1151
  /*
1152
    Detail channel depth and extrema.
1153
  */
1154
0
  JSONFormatLocaleFile(file,"    \"colorspace\": %s,\n",
1155
0
    CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t)
1156
0
    image->colorspace));
1157
0
  channel_statistics=(ChannelStatistics *) NULL;
1158
0
  channel_moments=(ChannelMoments *) NULL;
1159
0
  channel_phash=(ChannelPerceptualHash *) NULL;
1160
0
  channel_features=(ChannelFeatures *) NULL;
1161
0
  channel_statistics=GetImageStatistics(image,exception);
1162
0
  if (channel_statistics == (ChannelStatistics *) NULL)
1163
0
    return(MagickFalse);
1164
0
  artifact=GetImageArtifact(image,"identify:moments");
1165
0
  if (artifact == (const char *) NULL)
1166
0
    artifact=GetImageArtifact(image,"json:moments");
1167
0
  if (artifact != (const char *) NULL)
1168
0
    {
1169
0
      channel_moments=GetImageMoments(image,exception);
1170
0
      channel_phash=GetImagePerceptualHash(image,exception);
1171
0
    }
1172
0
  artifact=GetImageArtifact(image,"identify:features");
1173
0
  if (artifact == (const char *) NULL)
1174
0
    artifact=GetImageArtifact(image,"json:features");
1175
0
  if (artifact != (const char *) NULL)
1176
0
    {
1177
0
      distance=StringToUnsignedLong(artifact);
1178
0
      channel_features=GetImageFeatures(image,distance,exception);
1179
0
    }
1180
0
  depth=GetImageDepth(image,exception);
1181
0
  (void) FormatLocaleFile(file,"    \"depth\": %g,\n",(double) depth);
1182
0
  (void) FormatLocaleFile(file,"    \"baseDepth\": %g,\n",(double)
1183
0
    image->depth);
1184
0
  (void) FormatLocaleFile(file,"    \"channelDepth\": {\n");
1185
0
  if (image->alpha_trait != UndefinedPixelTrait)
1186
0
    (void) FormatLocaleFile(file,"      \"alpha\": %.17g,\n",(double)
1187
0
      channel_statistics[AlphaPixelChannel].depth);
1188
0
  switch (image->colorspace)
1189
0
  {
1190
0
    case RGBColorspace:
1191
0
    default:
1192
0
    {
1193
0
      (void) FormatLocaleFile(file,"      \"red\": %.17g,\n",(double)
1194
0
        channel_statistics[RedChannel].depth);
1195
0
      (void) FormatLocaleFile(file,"      \"green\": %.17g,\n",(double)
1196
0
        channel_statistics[GreenChannel].depth);
1197
0
      (void) FormatLocaleFile(file,"      \"blue\": %.17g\n",(double)
1198
0
        channel_statistics[BlueChannel].depth);
1199
0
      break;
1200
0
    }
1201
0
    case CMYKColorspace:
1202
0
    {
1203
0
      (void) FormatLocaleFile(file,"      \"cyan\": %.17g,\n",(double)
1204
0
        channel_statistics[CyanChannel].depth);
1205
0
      (void) FormatLocaleFile(file,"      \"magenta\": %.17g,\n",(double)
1206
0
        channel_statistics[MagentaChannel].depth);
1207
0
      (void) FormatLocaleFile(file,"      \"yellow\": %.17g,\n",(double)
1208
0
        channel_statistics[YellowChannel].depth);
1209
0
      (void) FormatLocaleFile(file,"      \"black\": %.17g\n",(double)
1210
0
        channel_statistics[BlackChannel].depth);
1211
0
      break;
1212
0
    }
1213
0
    case LinearGRAYColorspace:
1214
0
    case GRAYColorspace:
1215
0
    {
1216
0
      (void) FormatLocaleFile(file,"      \"gray\": %.17g\n",(double)
1217
0
        channel_statistics[GrayChannel].depth);
1218
0
      break;
1219
0
    }
1220
0
  }
1221
0
  (void) FormatLocaleFile(file,"    },\n");
1222
0
  scale=1;
1223
0
  if (image->depth <= MAGICKCORE_QUANTUM_DEPTH)
1224
0
    scale=(double) (QuantumRange/((size_t) QuantumRange >> ((size_t)
1225
0
      MAGICKCORE_QUANTUM_DEPTH-image->depth)));
1226
0
  if (channel_statistics != (ChannelStatistics *) NULL)
1227
0
    {
1228
0
      (void) FormatLocaleFile(file,"    \"pixels\": %.17g,\n",
1229
0
        channel_statistics[CompositePixelChannel].area);
1230
0
      if ((image->colorspace != LinearGRAYColorspace) &&
1231
0
          (image->colorspace != GRAYColorspace))
1232
0
        {
1233
0
          (void) FormatLocaleFile(file,"    \"imageStatistics\": {\n");
1234
0
          (void) PrintChannelStatistics(file,(PixelChannel) MaxPixelChannels,
1235
0
            "Overall",1.0/scale,MagickFalse,channel_statistics);
1236
0
          (void) FormatLocaleFile(file,"    },\n");
1237
0
        }
1238
0
      (void) FormatLocaleFile(file,"    \"channelStatistics\": {\n");
1239
0
      if (image->alpha_trait != UndefinedPixelTrait)
1240
0
        (void) PrintChannelStatistics(file,AlphaPixelChannel,"alpha",1.0/scale,
1241
0
          MagickTrue,channel_statistics);
1242
0
      switch (image->colorspace)
1243
0
      {
1244
0
        case RGBColorspace:
1245
0
        default:
1246
0
        {
1247
0
          (void) PrintChannelStatistics(file,RedPixelChannel,"red",1.0/scale,
1248
0
            MagickTrue,channel_statistics);
1249
0
          (void) PrintChannelStatistics(file,GreenPixelChannel,"green",1.0/
1250
0
            scale,MagickTrue,channel_statistics);
1251
0
          (void) PrintChannelStatistics(file,BluePixelChannel,"blue",1.0/scale,
1252
0
            MagickFalse,channel_statistics);
1253
0
          break;
1254
0
        }
1255
0
        case CMYKColorspace:
1256
0
        {
1257
0
          (void) PrintChannelStatistics(file,CyanPixelChannel,"cyan",1.0/scale,
1258
0
            MagickTrue,channel_statistics);
1259
0
          (void) PrintChannelStatistics(file,MagentaPixelChannel,"magenta",1.0/
1260
0
            scale,MagickTrue,channel_statistics);
1261
0
          (void) PrintChannelStatistics(file,YellowPixelChannel,"yellow",1.0/
1262
0
            scale,MagickTrue,channel_statistics);
1263
0
          (void) PrintChannelStatistics(file,BlackPixelChannel,"black",1.0/
1264
0
            scale,MagickFalse,channel_statistics);
1265
0
          break;
1266
0
        }
1267
0
        case LinearGRAYColorspace:
1268
0
        case GRAYColorspace:
1269
0
        {
1270
0
          (void) PrintChannelStatistics(file,GrayPixelChannel,"gray",1.0/scale,
1271
0
            MagickFalse,channel_statistics);
1272
0
          break;
1273
0
        }
1274
0
      }
1275
0
      (void) FormatLocaleFile(file,"    },\n");
1276
0
      channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1277
0
        channel_statistics);
1278
0
    }
1279
0
  if (channel_moments != (ChannelMoments *) NULL)
1280
0
    {
1281
0
      (void) FormatLocaleFile(file,"    \"channelMoments\": {\n");
1282
0
      if (image->alpha_trait != UndefinedPixelTrait)
1283
0
        (void) PrintChannelMoments(file,AlphaPixelChannel,"alpha",MagickTrue,
1284
0
          channel_moments);
1285
0
      switch (image->colorspace)
1286
0
      {
1287
0
        case RGBColorspace:
1288
0
        default:
1289
0
        {
1290
0
          (void) PrintChannelMoments(file,RedPixelChannel,"red",MagickTrue,
1291
0
            channel_moments);
1292
0
          (void) PrintChannelMoments(file,GreenPixelChannel,"green",MagickTrue,
1293
0
            channel_moments);
1294
0
          (void) PrintChannelMoments(file,BluePixelChannel,"blue",MagickFalse,
1295
0
            channel_moments);
1296
0
          break;
1297
0
        }
1298
0
        case CMYKColorspace:
1299
0
        {
1300
0
          (void) PrintChannelMoments(file,CyanPixelChannel,"cyan",MagickTrue,
1301
0
            channel_moments);
1302
0
          (void) PrintChannelMoments(file,MagentaPixelChannel,"magenta",
1303
0
            MagickTrue,channel_moments);
1304
0
          (void) PrintChannelMoments(file,YellowPixelChannel,"yellow",
1305
0
            MagickTrue,channel_moments);
1306
0
          (void) PrintChannelMoments(file,BlackPixelChannel,"black",
1307
0
            MagickFalse,channel_moments);
1308
0
          break;
1309
0
        }
1310
0
        case LinearGRAYColorspace:
1311
0
        case GRAYColorspace:
1312
0
        {
1313
0
          (void) PrintChannelMoments(file,GrayPixelChannel,"gray",MagickFalse,
1314
0
            channel_moments);
1315
0
          break;
1316
0
        }
1317
0
      }
1318
0
      (void) FormatLocaleFile(file,"    },\n");
1319
0
      channel_moments=(ChannelMoments *) RelinquishMagickMemory(
1320
0
        channel_moments);
1321
0
    }
1322
0
  if (channel_phash != (ChannelPerceptualHash *) NULL)
1323
0
    {
1324
0
      (void) FormatLocaleFile(file,"    \"channelPerceptualHash\": {\n");
1325
0
      (void) PrintChannelPerceptualHash(image,file,channel_phash);
1326
0
      (void) FormatLocaleFile(file,"    },\n");
1327
0
      channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1328
0
        channel_phash);
1329
0
    }
1330
0
  if (channel_features != (ChannelFeatures *) NULL)
1331
0
    {
1332
0
      (void) FormatLocaleFile(file,"    \"channelFeatures\": {\n");
1333
0
      if (image->alpha_trait != UndefinedPixelTrait)
1334
0
        (void) PrintChannelFeatures(file,AlphaPixelChannel,"alpha",MagickTrue,
1335
0
          channel_features);
1336
0
      switch (image->colorspace)
1337
0
      {
1338
0
        case RGBColorspace:
1339
0
        default:
1340
0
        {
1341
0
          (void) PrintChannelFeatures(file,RedPixelChannel,"red",MagickTrue,
1342
0
            channel_features);
1343
0
          (void) PrintChannelFeatures(file,GreenPixelChannel,"green",
1344
0
            MagickTrue,channel_features);
1345
0
          (void) PrintChannelFeatures(file,BluePixelChannel,"blue",MagickFalse,
1346
0
            channel_features);
1347
0
          break;
1348
0
        }
1349
0
        case CMYKColorspace:
1350
0
        {
1351
0
          (void) PrintChannelFeatures(file,CyanPixelChannel,"cyan",MagickTrue,
1352
0
            channel_features);
1353
0
          (void) PrintChannelFeatures(file,MagentaPixelChannel,"magenta",
1354
0
            MagickTrue,channel_features);
1355
0
          (void) PrintChannelFeatures(file,YellowPixelChannel,"yellow",
1356
0
            MagickTrue,channel_features);
1357
0
          (void) PrintChannelFeatures(file,BlackPixelChannel,"black",
1358
0
            MagickFalse,channel_features);
1359
0
          break;
1360
0
        }
1361
0
        case LinearGRAYColorspace:
1362
0
        case GRAYColorspace:
1363
0
        {
1364
0
          (void) PrintChannelFeatures(file,GrayPixelChannel,"gray",MagickFalse,
1365
0
            channel_features);
1366
0
          break;
1367
0
        }
1368
0
      }
1369
0
      (void) FormatLocaleFile(file,"    },\n");
1370
0
      channel_features=(ChannelFeatures *) RelinquishMagickMemory(
1371
0
        channel_features);
1372
0
    }
1373
0
    if (image->colorspace == CMYKColorspace)
1374
0
      (void) FormatLocaleFile(file,"    \"totalInkDensity\": \"%.*g%%\",\n",
1375
0
        GetMagickPrecision(),100.0*GetImageTotalInkDensity(image,exception)/
1376
0
        (double) QuantumRange);
1377
0
    x=0;
1378
0
    if (image->alpha_trait != UndefinedPixelTrait)
1379
0
      {
1380
0
        const Quantum
1381
0
          *p;
1382
1383
0
        p=(const Quantum *) NULL;
1384
0
        for (y=0; y < (ssize_t) image->rows; y++)
1385
0
        {
1386
0
          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1387
0
          if (p == (const Quantum *) NULL)
1388
0
            break;
1389
0
          for (x=0; x < (ssize_t) image->columns; x++)
1390
0
          {
1391
0
            if (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)
1392
0
              break;
1393
0
            p+=(ptrdiff_t) GetPixelChannels(image);
1394
0
          }
1395
0
          if (x < (ssize_t) image->columns)
1396
0
            break;
1397
0
        }
1398
0
        if ((x < (ssize_t) image->columns) || (y < (ssize_t) image->rows))
1399
0
          {
1400
0
            PixelInfo
1401
0
              pixel;
1402
1403
0
            GetPixelInfo(image,&pixel);
1404
0
            GetPixelInfoPixel(image,p,&pixel);
1405
0
            GetColorTuple(&pixel,MagickTrue,color);
1406
0
            (void) FormatLocaleFile(file,"    \"alpha\": \"%s\",\n",color);
1407
0
          }
1408
0
      }
1409
0
  if (image->storage_class == PseudoClass)
1410
0
    {
1411
0
      PixelInfo
1412
0
        *magick_restrict p;
1413
1414
0
      (void) FormatLocaleFile(file,"    \"colormapEntries\": %.17g,\n",
1415
0
        (double) image->colors);
1416
0
      (void) FormatLocaleFile(file,"    \"colormap\": [\n      ");
1417
0
      p=image->colormap;
1418
0
      for (i=0; i < (ssize_t) image->colors; i++)
1419
0
      {
1420
0
        GetColorTuple(p,MagickTrue,color);
1421
0
        (void) FormatLocaleFile(file,"\"%s\"",color);
1422
0
        if (i < (ssize_t) (image->colors-1))
1423
0
          (void) FormatLocaleFile(file,",");
1424
0
        if (((i+1) % 5) == 0)
1425
0
          (void) FormatLocaleFile(file,"\n      ");
1426
0
        p++;
1427
0
      }
1428
0
      (void) FormatLocaleFile(file,"\n    ],\n");
1429
0
    }
1430
0
  if (image->error.mean_error_per_pixel != 0.0)
1431
0
    (void) FormatLocaleFile(file,"    \"meanErrorPerPixel\": %g,\n",
1432
0
      image->error.mean_error_per_pixel);
1433
0
  if (image->error.normalized_mean_error != 0.0)
1434
0
    (void) FormatLocaleFile(file,"    \"normalizedMeanError\": %g,\n",
1435
0
      image->error.normalized_mean_error);
1436
0
  if (image->error.normalized_maximum_error != 0.0)
1437
0
    (void) FormatLocaleFile(file,"    \"normalizedMaximumError\": %g,\n",
1438
0
      image->error.normalized_maximum_error);
1439
0
  JSONFormatLocaleFile(file,"    \"renderingIntent\": %s,\n",
1440
0
    CommandOptionToMnemonic(MagickIntentOptions,(ssize_t)
1441
0
    image->rendering_intent));
1442
0
  if (image->gamma != 0.0)
1443
0
    (void) FormatLocaleFile(file,"    \"gamma\": %g,\n",image->gamma);
1444
0
  if ((image->chromaticity.red_primary.x != 0.0) ||
1445
0
      (image->chromaticity.green_primary.x != 0.0) ||
1446
0
      (image->chromaticity.blue_primary.x != 0.0) ||
1447
0
      (image->chromaticity.white_point.x != 0.0))
1448
0
    {
1449
      /*
1450
        Display image chromaticity.
1451
      */
1452
0
      (void) FormatLocaleFile(file,"    \"chromaticity\": {\n");
1453
0
      (void) FormatLocaleFile(file,"      \"redPrimary\": {\n"
1454
0
        "        \"x\": %g,\n        \"y\": %g\n      },\n",
1455
0
        image->chromaticity.red_primary.x,image->chromaticity.red_primary.y);
1456
0
      (void) FormatLocaleFile(file,"      \"greenPrimary\": {\n"
1457
0
        "        \"x\": %g,\n        \"y\": %g\n      },\n",
1458
0
        image->chromaticity.green_primary.x,
1459
0
        image->chromaticity.green_primary.y);
1460
0
      (void) FormatLocaleFile(file,"      \"bluePrimary\": {\n"
1461
0
        "        \"x\": %g,\n        \"y\": %g\n      },\n",
1462
0
        image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y);
1463
0
      (void) FormatLocaleFile(file,"      \"whitePrimary\": {\n"
1464
0
        "        \"x\": %g,\n        \"y\": %g\n      }\n",
1465
0
        image->chromaticity.white_point.x,image->chromaticity.white_point.y);
1466
0
      (void) FormatLocaleFile(file,"    },\n");
1467
0
    }
1468
0
  if ((image->extract_info.width*image->extract_info.height) != 0)
1469
0
    (void) FormatLocaleFile(file,"    \"tileGeometry\": {\n"
1470
0
      "      \"width\": %.17g,\n      \"height\": %.17g,\n"
1471
0
      "      \"x\": %.17g,\n      \"y\": %.17g\n    },\n",
1472
0
      (double) image->extract_info.width,(double) image->extract_info.height,
1473
0
      (double) image->extract_info.x,(double) image->extract_info.y);
1474
0
  GetColorTuple(&image->matte_color,MagickTrue,color);
1475
0
  (void) FormatLocaleFile(file,"    \"matteColor\": \"%s\",\n",color);
1476
0
  GetColorTuple(&image->background_color,MagickTrue,color);
1477
0
  (void) FormatLocaleFile(file,"    \"backgroundColor\": \"%s\",\n",color);
1478
0
  GetColorTuple(&image->border_color,MagickTrue,color);
1479
0
  (void) FormatLocaleFile(file,"    \"borderColor\": \"%s\",\n",color);
1480
0
  GetColorTuple(&image->transparent_color,MagickTrue,color);
1481
0
  (void) FormatLocaleFile(file,"    \"transparentColor\": \"%s\",\n",color);
1482
0
  JSONFormatLocaleFile(file,"    \"interlace\": %s,\n",CommandOptionToMnemonic(
1483
0
    MagickInterlaceOptions,(ssize_t) image->interlace));
1484
0
  JSONFormatLocaleFile(file,"    \"intensity\": %s,\n",CommandOptionToMnemonic(
1485
0
    MagickPixelIntensityOptions,(ssize_t) image->intensity));
1486
0
  JSONFormatLocaleFile(file,"    \"compose\": %s,\n",
1487
0
    CommandOptionToMnemonic(MagickComposeOptions,(ssize_t) image->compose));
1488
0
  if ((image->page.width != 0) || (image->page.height != 0) ||
1489
0
      (image->page.x != 0) || (image->page.y != 0))
1490
0
    (void) FormatLocaleFile(file,"    \"pageGeometry\": {\n"
1491
0
      "      \"width\": %.17g,\n      \"height\": %.17g,\n"
1492
0
      "      \"x\": %.17g,\n      \"y\": %.17g\n    },\n",
1493
0
      (double) image->page.width,(double) image->page.height,
1494
0
      (double) image->page.x,(double) image->page.y);
1495
0
  if ((image->page.x != 0) || (image->page.y != 0))
1496
0
    (void) FormatLocaleFile(file,"    \"originGeometry\": \"%+.20g%+.20g\",\n",
1497
0
      (double) image->page.x,(double) image->page.y);
1498
0
  JSONFormatLocaleFile(file,"    \"dispose\": %s,\n",
1499
0
    CommandOptionToMnemonic(MagickDisposeOptions,(ssize_t) image->dispose));
1500
0
  if (image->delay != 0)
1501
0
    (void) FormatLocaleFile(file,"    \"delay\": \"%.17gx%.17g\",\n",
1502
0
      (double) image->delay,(double) image->ticks_per_second);
1503
0
  if (image->iterations != 1)
1504
0
    (void) FormatLocaleFile(file,"    \"iterations\": %.17g,\n",(double)
1505
0
      image->iterations);
1506
0
  if ((image->next != (Image *) NULL) || (image->previous != (Image *) NULL))
1507
0
    (void) FormatLocaleFile(file,"    \"scene\": %.17g,\n    \"scenes\": "
1508
0
      "%.17g,\n",(double) image->scene,(double) GetImageListLength(image));
1509
0
  else
1510
0
    if (image->scene != 0)
1511
0
      (void) FormatLocaleFile(file,"    \"scene\": %.17g,\n",(double)
1512
0
        image->scene);
1513
0
  JSONFormatLocaleFile(file,"    \"compression\": %s,\n",
1514
0
    CommandOptionToMnemonic(MagickCompressOptions,(ssize_t)
1515
0
    image->compression));
1516
0
  if (image->quality != UndefinedCompressionQuality)
1517
0
    (void) FormatLocaleFile(file,"    \"quality\": %.17g,\n",(double)
1518
0
      image->quality);
1519
0
  JSONFormatLocaleFile(file,"    \"orientation\": %s,\n",
1520
0
    CommandOptionToMnemonic(MagickOrientationOptions,(ssize_t)
1521
0
    image->orientation));
1522
0
  if (image->montage != (char *) NULL)
1523
0
    JSONFormatLocaleFile(file,"    \"montage\": \"%s\",\n",image->montage);
1524
0
  if (image->directory != (char *) NULL)
1525
0
    {
1526
0
      Image
1527
0
        *tile;
1528
1529
0
      ImageInfo
1530
0
        *image_info;
1531
1532
0
      char
1533
0
        *p,
1534
0
        *q;
1535
1536
0
      WarningHandler
1537
0
        handler;
1538
1539
      /*
1540
        Display visual image directory.
1541
      */
1542
0
      image_info=AcquireImageInfo();
1543
0
      (void) CloneString(&image_info->size,"64x64");
1544
0
      (void) FormatLocaleFile(file,"    \"montageDirectory\": [");
1545
0
      for (p=image->directory; *p != '\0'; p++)
1546
0
      {
1547
0
        q=p;
1548
0
        while ((*q != '\xff') && (*q != '\0') &&
1549
0
               ((size_t) (q-p+1) < sizeof(image_info->filename)))
1550
0
          q++;
1551
0
        (void) CopyMagickString(image_info->filename,p,(size_t) (q-p+1));
1552
0
        p=q;
1553
0
        JSONFormatLocaleFile(file,"{\n       \"name\": %s",
1554
0
          image_info->filename);
1555
0
        handler=SetWarningHandler((WarningHandler) NULL);
1556
0
        tile=StrictReadImage(image_info,exception);
1557
0
        (void) SetWarningHandler(handler);
1558
0
        if (tile == (Image *) NULL)
1559
0
          {
1560
0
            (void) FormatLocaleFile(file,"    }");
1561
0
            continue;
1562
0
          }
1563
0
        (void) FormatLocaleFile(file,",\n       \"info\": \"%.17gx%.17g %s\"",
1564
0
          (double) tile->magick_columns,(double) tile->magick_rows,
1565
0
          tile->magick);
1566
0
        (void) SignatureImage(tile,exception);
1567
0
        ResetImagePropertyIterator(tile);
1568
0
        property=GetNextImageProperty(tile);
1569
0
        while (property != (const char *) NULL)
1570
0
        {
1571
0
          JSONFormatLocaleFile(file,",\n       %s: ",property);
1572
0
          value=GetImageProperty(tile,property,exception);
1573
0
          JSONFormatLocaleFile(file,"%s",value);
1574
0
          property=GetNextImageProperty(tile);
1575
0
        }
1576
0
        tile=DestroyImageList(tile);
1577
0
        if (*p != '\0')
1578
0
          (void) FormatLocaleFile(file,"\n    },");
1579
0
        else
1580
0
          (void) FormatLocaleFile(file,"\n    }");
1581
0
      }
1582
0
      (void) FormatLocaleFile(file,"],\n");
1583
0
      image_info=DestroyImageInfo(image_info);
1584
0
    }
1585
0
  ResetImagePropertyIterator(image);
1586
0
  property=GetNextImageProperty(image);
1587
0
  if (property != (const char *) NULL)
1588
0
    {
1589
0
      size_t
1590
0
        n;
1591
1592
      /*
1593
        Display image properties.
1594
      */
1595
0
      n=0;
1596
0
      (void) FormatLocaleFile(file,"    \"properties\": {\n");
1597
0
      while (property != (const char *) NULL)
1598
0
      {
1599
0
        if (n++ != 0)
1600
0
          (void) FormatLocaleFile(file,",\n");
1601
0
        JSONFormatLocaleFile(file,"      %s: ",property);
1602
0
        value=GetImageProperty(image,property,exception);
1603
0
        JSONFormatLocaleFile(file,"%s",value);
1604
0
        property=GetNextImageProperty(image);
1605
0
      }
1606
0
      (void) FormatLocaleFile(file,"\n    },\n");
1607
0
    }
1608
0
  (void) FormatLocaleString(key,MagickPathExtent,"8BIM:1999,2998:#1");
1609
0
  value=GetImageProperty(image,key,exception);
1610
0
  if (value != (const char *) NULL)
1611
0
    {
1612
      /*
1613
        Display clipping path.
1614
      */
1615
0
      JSONFormatLocaleFile(file,"    \"clipping path\": %s,\n",value);
1616
0
    }
1617
0
  ResetImageProfileIterator(image);
1618
0
  name=GetNextImageProfile(image);
1619
0
  if (name != (char *) NULL)
1620
0
    {
1621
0
      const StringInfo
1622
0
        *profile;
1623
1624
0
      size_t
1625
0
        n;
1626
1627
      /*
1628
        Identify image profiles.
1629
      */
1630
0
      n=0;
1631
0
      (void) FormatLocaleFile(file,"    \"profiles\": {\n");
1632
0
      while (name != (char *) NULL)
1633
0
      {
1634
0
        profile=GetImageProfile(image,name);
1635
0
        if (profile == (StringInfo *) NULL)
1636
0
          continue;
1637
0
        if (n++ != 0)
1638
0
          (void) FormatLocaleFile(file,",\n");
1639
0
        JSONFormatLocaleFile(file,"      %s: {\n",name);
1640
0
        if (LocaleCompare(name,"iptc") == 0)
1641
0
          EncodeIptcProfile(file,profile);
1642
0
        (void) FormatLocaleFile(file,"        \"length\": %.17g",(double)
1643
0
          GetStringInfoLength(profile));
1644
0
        (void) FormatLocaleFile(file,"\n      }");
1645
0
        name=GetNextImageProfile(image);
1646
0
      }
1647
0
      (void) FormatLocaleFile(file,"\n    },\n");
1648
0
    }
1649
0
  ResetImageArtifactIterator(image);
1650
0
  artifact=GetNextImageArtifact(image);
1651
0
  if (artifact != (const char *) NULL)
1652
0
    {
1653
0
      ssize_t
1654
0
        n;
1655
1656
      /*
1657
        Display image artifacts.
1658
      */
1659
0
      n=0;
1660
0
      (void) FormatLocaleFile(file,"    \"artifacts\": {\n");
1661
0
      while (artifact != (const char *) NULL)
1662
0
      {
1663
0
        if (n++ != 0)
1664
0
          (void) FormatLocaleFile(file,",\n");
1665
0
        JSONFormatLocaleFile(file,"      %s: ",artifact);
1666
0
        value=GetImageArtifact(image,artifact);
1667
0
        JSONFormatLocaleFile(file,"%s",value);
1668
0
        artifact=GetNextImageArtifact(image);
1669
0
      }
1670
0
      (void) FormatLocaleFile(file,"\n    },\n");
1671
0
    }
1672
0
  ResetImageRegistryIterator();
1673
0
  registry=GetNextImageRegistry();
1674
0
  if (registry != (const char *) NULL)
1675
0
    {
1676
0
      ssize_t
1677
0
        n;
1678
1679
      /*
1680
        Display image registry.
1681
      */
1682
0
      (void) FormatLocaleFile(file,"    \"registry\": {\n");
1683
0
      n=0;
1684
0
      while (registry != (const char *) NULL)
1685
0
      {
1686
0
        char *registry_value = (char *) GetImageRegistry(StringRegistryType,
1687
0
          registry,exception);
1688
0
        if (n++ != 0)
1689
0
          (void) FormatLocaleFile(file,",\n");
1690
0
        JSONFormatLocaleFile(file,"      %s: ",registry);
1691
0
        if (registry_value != (char *) NULL)
1692
0
         {
1693
0
           JSONFormatLocaleFile(file,"%s",registry_value);
1694
0
           registry_value=DestroyString(registry_value);
1695
0
         }
1696
0
        registry=GetNextImageRegistry();
1697
0
      }
1698
0
      (void) FormatLocaleFile(file,"    },\n");
1699
0
    }
1700
0
  (void) FormatLocaleFile(file,"    \"tainted\": %s,\n",
1701
0
    image->taint != MagickFalse ? "true" : "false");
1702
0
  (void) FormatMagickSize(image->extent,MagickFalse,"B",MagickPathExtent,
1703
0
    format);
1704
0
  JSONFormatLocaleFile(file,"    \"filesize\": %s,\n",format);
1705
0
  (void) FormatMagickSize((MagickSizeType) image->columns*image->rows,
1706
0
    MagickFalse,"B",MagickPathExtent,format);
1707
0
  if (strlen(format) > 1)
1708
0
    format[strlen(format)-1]='\0';
1709
0
  JSONFormatLocaleFile(file,"    \"numberPixels\": %s,\n",format);
1710
0
  (void) FormatMagickSize((MagickSizeType) ((double) image->columns*image->rows/
1711
0
    elapsed_time+0.5),MagickFalse,"B",MagickPathExtent,format);
1712
0
  JSONFormatLocaleFile(file,"    \"pixelsPerSecond\": %s,\n",format);
1713
0
  (void) FormatLocaleFile(file,"    \"userTime\": \"%0.3fu\",\n",user_time);
1714
0
  (void) FormatLocaleFile(file,"    \"elapsedTime\": \"%lu:%02lu.%03lu\",\n",
1715
0
    (unsigned long) (elapsed_time/60.0),(unsigned long) ceil(fmod(
1716
0
    elapsed_time,60.0)),(unsigned long) (1000.0*(elapsed_time-floor(
1717
0
    elapsed_time))));
1718
0
  JSONFormatLocaleFile(file,"    \"version\": %s\n",GetMagickVersion(
1719
0
    (size_t *) NULL));
1720
0
  (void) FormatLocaleFile(file,"  }\n}");
1721
0
  (void) fflush(file);
1722
0
  return(ferror(file) != 0 ? MagickFalse : MagickTrue);
1723
0
}
1724
1725
static MagickBooleanType WriteJSONImage(const ImageInfo *image_info,
1726
  Image *image,ExceptionInfo *exception)
1727
0
{
1728
0
  FILE
1729
0
    *file;
1730
1731
0
  MagickBooleanType
1732
0
    status;
1733
1734
0
  MagickOffsetType
1735
0
    scene;
1736
1737
0
  size_t
1738
0
    number_scenes;
1739
1740
  /*
1741
    Open output image file.
1742
  */
1743
0
  assert(image_info != (const ImageInfo *) NULL);
1744
0
  assert(image_info->signature == MagickCoreSignature);
1745
0
  assert(image != (Image *) NULL);
1746
0
  assert(image->signature == MagickCoreSignature);
1747
0
  if (IsEventLogging() != MagickFalse)
1748
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1749
0
  status=OpenBlob(image_info,image,WriteBlobMode,exception);
1750
0
  if (status == MagickFalse)
1751
0
    return(status);
1752
0
  file=GetBlobFileHandle(image);
1753
0
  if (file == (FILE *) NULL)
1754
0
    file=stdout;
1755
0
  scene=0;
1756
0
  number_scenes=GetImageListLength(image);
1757
0
  do
1758
0
  {
1759
0
    if (scene == 0)
1760
0
      (void) WriteBlobString(image,"[");
1761
0
    image->magick_columns=image->columns;
1762
0
    image->magick_rows=image->rows;
1763
0
    status=EncodeImageAttributes(image,file,exception);
1764
0
    if (status == MagickFalse)
1765
0
      break;
1766
0
    if (GetNextImageInList(image) == (Image *) NULL)
1767
0
      {
1768
0
        (void) WriteBlobString(image,"]");
1769
0
        break;
1770
0
      }
1771
0
    (void) WriteBlobString(image,",\n");
1772
0
    image=SyncNextImageInList(image);
1773
0
    status=SetImageProgress(image,SaveImagesTag,scene++,number_scenes);
1774
0
    if (status == MagickFalse)
1775
0
      break;
1776
0
  } while (image_info->adjoin != MagickFalse);
1777
0
  if (CloseBlob(image) == MagickFalse)
1778
0
    status=MagickFalse;
1779
0
  return(status);
1780
0
}