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