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