/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% CCCC OOO M M PPPP AAA RRRR EEEEE %
% C O O MM MM P P A A R R E %
% C O O M M M PPPP AAAAA RRRR EEE %
% C O O M M P A A R R E %
% CCCC OOO M M P A A R R EEEEE %
% %
% %
% Image Comparison Methods %
% %
% Software Design %
% John Cristy %
% December 2003 %
% %
% %
% Copyright 1999-2007 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% http://www.imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
*/
/*
Include declarations.
*/
#include "wand/studio.h"
#include "wand/MagickWand.h"
#include "wand/mogrify-private.h"
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% C o m p a r e I m a g e C o m m a n d %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CompareImageCommand() compares two images and returns the difference between
% them as a distortion metric and as a new image visually annotating their
% differences.
%
% The format of the CompareImageCommand method is:
%
% MagickBooleanType CompareImageCommand(ImageInfo *image_info,int argc,
% char **argv,char **metadata,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image_info: The image info.
%
% o argc: The number of elements in the argument vector.
%
% o argv: A text array containing the command line arguments.
%
% o metadata: any metadata is returned here.
%
% o exception: Return any errors or warnings in this structure.
%
*/
static void CompareUsage(void)
{
const char
**p;
static const char
*settings[]=
{
"-alpha option activate, deactivate, reset, or set the alpha channel",
"-authenticate value decrypt image with this password",
"-channel type apply option to select image channels",
"-colorspace type alternate image colorspace",
"-compress type type of pixel compression when writing the image",
"-debug events display copious debugging information",
"-define format:option",
" define one or more image format options",
"-density geometry horizontal and vertical density of the image",
"-depth value image depth",
"-extract geometry extract area from image",
"-format \"string\" output formatted image characteristics",
"-fuzz distance colors within this distance are considered equal",
"-help print program options",
"-identify identify the format and characteristics of the image",
"-interlace type type of image interlacing scheme",
"-limit type value pixel cache resource limit",
"-log format format of debugging information",
"-metric type measure differences between images with this metric",
"-monitor monitor progress",
"-profile filename add, delete, or apply an image profile",
"-quality value JPEG/MIFF/PNG compression level",
"-quiet suppress all warning messages",
"-quantize colorspace reduce colors in this colorspace",
"-regard-warnings pay attention to warning messages",
"-sampling-factor geometry",
" horizontal and vertical sampling factor",
"-seed value seed a new sequence of pseudo-random numbers",
"-set attribute value set an image attribute",
"-size geometry width and height of image",
"-transparent-color color",
" transparent color",
"-type type image type",
"-verbose print detailed information about the image",
"-version print version information",
"-virtual-pixel method",
" virtual pixel access method",
(char *) NULL
};
(void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
(void) printf("Copyright: %s\n\n",GetMagickCopyright());
(void) printf("Usage: %s [options ...] image reconstruct difference\n",
GetClientName());
(void) printf("\nImage Settings:\n");
for (p=settings; *p != (char *) NULL; p++)
(void) printf(" %s\n",*p);
(void) printf(
"\nBy default, the image format of `file' is determined by its magic\n");
(void) printf(
"number. To specify a particular image format, precede the filename\n");
(void) printf(
"with an image format name and a colon (i.e. ps:image) or specify the\n");
(void) printf(
"image type as the filename suffix (i.e. image.ps). Specify 'file' as\n");
(void) printf("'-' for standard input or output.\n");
exit(0);
}
WandExport MagickBooleanType CompareImageCommand(ImageInfo *image_info,
int argc,char **argv,char **metadata,ExceptionInfo *exception)
{
#define DestroyCompare() \
{ \
for ( ; k >= 0; k--) \
image_stack[k]=DestroyImageList(image_stack[k]); \
for (i=0; i < (long) argc; i++) \
argv[i]=DestroyString(argv[i]); \
argv=(char **) RelinquishMagickMemory(argv); \
}
#define ThrowCompareException(asperity,tag,option) \
{ \
if (exception->severity < (asperity)) \
(void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
"`%s'",option); \
DestroyCompare(); \
return(MagickFalse); \
}
#define ThrowCompareInvalidArgumentException(option,argument) \
{ \
(void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
"InvalidArgument","`%s': %s",argument,option); \
DestroyCompare(); \
return(MagickFalse); \
}
char
*filename,
*option;
const char
*format;
ChannelType
channel;
double
distortion;
Image
*difference_image,
*image,
*image_stack[MaxImageStackDepth+1],
*reconstruct_image;
long
j,
k;
MagickBooleanType
fire,
pend;
MagickStatusType
status;
MetricType
metric;
register long
i;
/*
Set defaults.
*/
assert(image_info != (ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
if (image_info->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(exception != (ExceptionInfo *) NULL);
if (argc == 2)
{
option=argv[1];
if ((LocaleCompare("version",option+1) == 0) ||
(LocaleCompare("-version",option+1) == 0))
{
(void) fprintf(stdout,"Version: %s\n",
GetMagickVersion((unsigned long *) NULL));
(void) fprintf(stdout,"Copyright: %s\n\n",GetMagickCopyright());
return(MagickTrue);
}
}
if (argc < 3)
CompareUsage();
channel=AllChannels;
difference_image=NewImageList();
distortion=0.0;
format=(char *) NULL;
j=1;
k=0;
image_stack[k]=NewImageList();
metric=UndefinedMetric;
option=(char *) NULL;
pend=MagickFalse;
reconstruct_image=NewImageList();
status=MagickTrue;
/*
Compare an image.
*/
ReadCommandlLine(argc,&argv);
status=ExpandFilenames(&argc,&argv);
if (status == MagickFalse)
{
char
*message;
message=GetExceptionMessage(errno);
ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed",
message);
message=DestroyString(message);
}
for (i=1; i < (long) (argc-1); i++)
{
option=argv[i];
if (LocaleCompare(option,"(") == 0)
{
if (k == MaxImageStackDepth)
ThrowCompareException(OptionError,"ParenthesisNestedTooDeeply",
option);
MogrifyImageStack(image_stack[k],MagickTrue,pend);
k++;
image_stack[k]=NewImageList();
continue;
}
if (LocaleCompare(option,")") == 0)
{
if (k == 0)
ThrowCompareException(OptionError,"UnableToParseExpression",option);
if (image_stack[k] != (Image *) NULL)
{
MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
AppendImageToList(&image_stack[k-1],image_stack[k]);
}
k--;
continue;
}
if (IsMagickOption(option) == MagickFalse)
{
/*
Read input image.
*/
MogrifyImageStack(image_stack[k],MagickTrue,pend);
filename=argv[i];
if ((LocaleCompare(filename,"--") == 0) && (i < (argc-1)))
filename=argv[++i];
(void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
image=ReadImage(image_info,exception);
status&=(image != (Image *) NULL) &&
(exception->severity < ErrorException);
if (image == (Image *) NULL)
continue;
AppendImageToList(&image_stack[k],image);
continue;
}
pend=image_stack[k] != (Image *) NULL ? MagickTrue : MagickFalse;
switch (*(option+1))
{
case 'a':
{
if (LocaleCompare("alpha",option+1) == 0)
{
long
type;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
type=ParseMagickOption(MagickAlphaOptions,MagickFalse,argv[i]);
if (type < 0)
ThrowCompareException(OptionError,"UnrecognizedAlphaChannelType",
argv[i]);
break;
}
if (LocaleCompare("authenticate",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option);
}
case 'c':
{
if (LocaleCompare("cache",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
if (LocaleCompare("channel",option+1) == 0)
{
long
channel;
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
channel=ParseChannelOption(argv[i]);
if (channel < 0)
ThrowCompareException(OptionError,"UnrecognizedChannelType",
argv[i]);
break;
}
if (LocaleCompare("colorspace",option+1) == 0)
{
long
colorspace;
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
colorspace=ParseMagickOption(MagickColorspaceOptions,MagickFalse,
argv[i]);
if (colorspace < 0)
ThrowCompareException(OptionError,"UnrecognizedColorspace",
argv[i]);
break;
}
if (LocaleCompare("compress",option+1) == 0)
{
long
compress;
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
compress=ParseMagickOption(MagickCompressOptions,MagickFalse,
argv[i]);
if (compress < 0)
ThrowCompareException(OptionError,"UnrecognizedImageCompression",
argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'd':
{
if (LocaleCompare("debug",option+1) == 0)
{
LogEventType
event_mask;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
event_mask=SetLogEventMask(argv[i]);
if (event_mask == UndefinedEvents)
ThrowCompareException(OptionError,"UnrecognizedEventType",
argv[i]);
break;
}
if (LocaleCompare("define",option+1) == 0)
{
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (*option == '+')
{
const char
*define;
define=GetImageOption(image_info,argv[i]);
if (define == (const char *) NULL)
ThrowCompareException(OptionError,"NoSuchOption",argv[i]);
break;
}
break;
}
if (LocaleCompare("density",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
if (LocaleCompare("depth",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'e':
{
if (LocaleCompare("extract",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'f':
{
if (LocaleCompare("format",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
format=argv[i];
break;
}
if (LocaleCompare("fuzz",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'h':
{
if ((LocaleCompare("help",option+1) == 0) ||
(LocaleCompare("-help",option+1) == 0))
CompareUsage();
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'i':
{
if (LocaleCompare("identify",option+1) == 0)
break;
if (LocaleCompare("interlace",option+1) == 0)
{
long
interlace;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
interlace=ParseMagickOption(MagickInterlaceOptions,MagickFalse,
argv[i]);
if (interlace < 0)
ThrowCompareException(OptionError,"UnrecognizedInterlaceType",
argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'l':
{
if (LocaleCompare("limit",option+1) == 0)
{
long
resource;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
resource=ParseMagickOption(MagickResourceOptions,MagickFalse,
argv[i]);
if (resource < 0)
ThrowCompareException(OptionError,"UnrecognizedResourceType",
argv[i]);
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if ((LocaleCompare("unlimited",argv[i]) != 0) &&
(IsGeometry(argv[i]) == MagickFalse))
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
if (LocaleCompare("log",option+1) == 0)
{
if (*option == '+')
break;
i++;
if ((i == (long) argc) || (strchr(argv[i],'%') == (char *) NULL))
ThrowCompareException(OptionError,"MissingArgument",option);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'm':
{
if (LocaleCompare("matte",option+1) == 0)
break;
if (LocaleCompare("metric",option+1) == 0)
{
long
type;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
type=ParseMagickOption(MagickMetricOptions,MagickTrue,argv[i]);
if (type < 0)
ThrowCompareException(OptionError,"UnrecognizedMetricType",
argv[i]);
metric=(MetricType) type;
break;
}
if (LocaleCompare("monitor",option+1) == 0)
break;
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'p':
{
if (LocaleCompare("profile",option+1) == 0)
{
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'q':
{
if (LocaleCompare("quality",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
if (LocaleCompare("quantize",option+1) == 0)
{
long
colorspace;
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
colorspace=ParseMagickOption(MagickColorspaceOptions,
MagickFalse,argv[i]);
if (colorspace < 0)
ThrowCompareException(OptionError,"UnrecognizedColorspace",
argv[i]);
break;
}
if (LocaleCompare("quiet",option+1) == 0)
break;
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'r':
{
if (LocaleCompare("regard-warnings",option+1) == 0)
break;
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 's':
{
if (LocaleCompare("sampling-factor",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
if (LocaleCompare("seed",option+1) == 0)
{
unsigned long
seed;
if (*option == '+')
{
seed=(unsigned long) time((time_t *) NULL);
SeedRandomReservoir(seed);
break;
}
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
seed=(unsigned long) atol(argv[i]);
SeedRandomReservoir(seed);
break;
}
if (LocaleCompare("set",option+1) == 0)
{
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
break;
}
if (LocaleCompare("size",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
if (IsGeometry(argv[i]) == MagickFalse)
ThrowCompareInvalidArgumentException(option,argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 't':
{
if (LocaleCompare("transparent-color",option+1) == 0)
{
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
break;
}
if (LocaleCompare("type",option+1) == 0)
{
long
type;
if (*option == '+')
break;
i++;
if (i == (long) argc)
ThrowCompareException(OptionError,"MissingArgument",option);
type=ParseMagickOption(MagickTypeOptions,MagickFalse,argv[i]);
if (type < 0)
ThrowCompareException(OptionError,"UnrecognizedImageType",
argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case 'v':
{
if (LocaleCompare("verbose",option+1) == 0)
break;
if ((LocaleCompare("version",option+1) == 0) ||
(LocaleCompare("-version",option+1) == 0))
{
(void) fprintf(stdout,"Version: %s\n",
GetMagickVersion((unsigned long *) NULL));
(void) fprintf(stdout,"Copyright: %s\n\n",GetMagickCopyright());
break;
}
if (LocaleCompare("virtual-pixel",option+1) == 0)
{
long
method;
if (*option == '+')
break;
i++;
if (i == (long) (argc-1))
ThrowCompareException(OptionError,"MissingArgument",option);
method=ParseMagickOption(MagickVirtualPixelOptions,MagickFalse,
argv[i]);
if (method < 0)
ThrowCompareException(OptionError,
"UnrecognizedVirtualPixelMethod",argv[i]);
break;
}
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
case '?':
break;
default:
ThrowCompareException(OptionError,"UnrecognizedOption",option)
}
fire=(MagickBooleanType) ParseMagickOption(MagickMogrifyOptions,
MagickFalse,option+1);
if (fire == MagickTrue)
MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
}
if (k != 0)
ThrowCompareException(OptionError,"UnbalancedParenthesis",argv[i]);
if (i-- != (long) (argc-1))
ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]);
if ((image_stack[k] == (Image *) NULL) ||
(GetImageListLength(image_stack[k]) < 2))
ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]);
MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
image=GetImageFromList(image_stack[k],0);
reconstruct_image=GetImageFromList(image_stack[k],1);
difference_image=CompareImageChannels(image,reconstruct_image,channel,
metric,&distortion,exception);
if (difference_image == (Image *) NULL)
status=0;
else
{
if (image_info->verbose != MagickFalse)
(void) IsImagesEqual(image,reconstruct_image);
status&=WriteImages(image_info,difference_image,argv[argc-1],exception);
if ((metadata != (char **) NULL) && (format != (char *) NULL))
{
char
*text;
text=InterpretImageProperties(image_info,difference_image,format);
if (text == (char *) NULL)
{
char
*message;
message=GetExceptionMessage(errno);
ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed",
message);
message=DestroyString(message);
}
(void) ConcatenateString(&(*metadata),text);
(void) ConcatenateString(&(*metadata),"\n");
text=DestroyString(text);
}
difference_image=DestroyImageList(difference_image);
switch (metric)
{
case MeanAbsoluteErrorMetric:
case RootMeanSquaredErrorMetric:
case PeakAbsoluteErrorMetric:
{
(void) fprintf(stderr,"%g (%g)\n",distortion,(double) (QuantumScale*
distortion));
break;
}
case AbsoluteErrorMetric:
case MeanSquaredErrorMetric:
case PeakSignalToNoiseRatioMetric:
{
(void) fprintf(stderr,"%g\n",distortion);
break;
}
case MeanErrorPerPixelMetric:
{
(void) fprintf(stderr,"%g (%g, %g)\n",distortion,
image->error.normalized_mean_error,
image->error.normalized_maximum_error);
break;
}
case UndefinedMetric:
break;
}
}
DestroyCompare();
return(status != 0 ? MagickTrue : MagickFalse);
}
|