Home | History | Annotate | Download | only in utilities
      1 /*
      2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      3 %                                                                             %
      4 %                                                                             %
      5 %                                                                             %
      6 %                 M   M   AAA    GGGG  IIIII   CCCC  K   K                    %
      7 %                 MM MM  A   A  G        I    C      K  K                     %
      8 %                 M M M  AAAAA  G GGG    I    C      KKK                      %
      9 %                 M   M  A   A  G   G    I    C      K  K                     %
     10 %                 M   M  A   A   GGGG  IIIII   CCCC  K   K                    %
     11 %                                                                             %
     12 %                                                                             %
     13 %       Perform "Magick" on Images via the Command Line Interface             %
     14 %                                                                             %
     15 %                             Dragon Computing                                %
     16 %                             Anthony Thyssen                                 %
     17 %                               January 2012                                  %
     18 %                                                                             %
     19 %                                                                             %
     20 %  Copyright 1999-2016 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 %    http://www.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 %  Read CLI arguments, script files, and pipelines, to provide options that
     37 %  manipulate images from many different formats.
     38 %
     39 */
     40 
     41 /*
     43   Include declarations.
     44 */
     45 #include "MagickWand/studio.h"
     46 #include "MagickWand/MagickWand.h"
     47 
     48 /*
     50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     51 %                                                                             %
     52 %                                                                             %
     53 %                                                                             %
     54 %  M a i n                                                                    %
     55 %                                                                             %
     56 %                                                                             %
     57 %                                                                             %
     58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     59 %
     60 %
     61 */
     62 
     63 static int MagickMain(int argc,char **argv)
     64 {
     65 #define MagickCommandSize(name,use_metadata,command) \
     66   { (name), sizeof(name)-1, (use_metadata), (command) }
     67 
     68   typedef struct _CommandInfo
     69   {
     70     const char
     71       *client_name;
     72 
     73     size_t
     74       extent;
     75 
     76     MagickBooleanType
     77       use_metadata;
     78 
     79     MagickCommand
     80       command;
     81   } CommandInfo;
     82 
     83   const CommandInfo
     84     MagickCommands[] =
     85     {
     86       MagickCommandSize("magick", MagickFalse, MagickImageCommand),
     87       MagickCommandSize("convert", MagickFalse, ConvertImageCommand),
     88       MagickCommandSize("composite", MagickFalse, CompositeImageCommand),
     89       MagickCommandSize("identify", MagickTrue, IdentifyImageCommand),
     90       MagickCommandSize("animate", MagickFalse, AnimateImageCommand),
     91       MagickCommandSize("compare", MagickTrue, CompareImagesCommand),
     92       MagickCommandSize("conjure", MagickFalse, ConjureImageCommand),
     93       MagickCommandSize("display", MagickFalse, DisplayImageCommand),
     94       MagickCommandSize("import", MagickFalse, ImportImageCommand),
     95       MagickCommandSize("mogrify", MagickFalse, MogrifyImageCommand),
     96       MagickCommandSize("montage", MagickFalse, MontageImageCommand),
     97       MagickCommandSize("stream", MagickFalse, StreamImageCommand)
     98     };
     99 
    100   char
    101     client_name[MagickPathExtent],
    102     *metadata;
    103 
    104   ExceptionInfo
    105     *exception;
    106 
    107   ImageInfo
    108     *image_info;
    109 
    110   int
    111     exit_code,
    112     offset;
    113 
    114   MagickBooleanType
    115     status;
    116 
    117   register ssize_t
    118     i;
    119 
    120   MagickCoreGenesis(*argv,MagickTrue);
    121   exception=AcquireExceptionInfo();
    122   image_info=AcquireImageInfo();
    123   GetPathComponent(argv[0],TailPath,client_name);
    124   for (i=0; i < (ssize_t) (sizeof(MagickCommands)/sizeof(MagickCommands[0])); i++)
    125   {
    126     offset=LocaleNCompare(MagickCommands[i].client_name,client_name,
    127       MagickCommands[i].extent);
    128     if (offset == 0)
    129       break;
    130   }
    131   i%=(sizeof(MagickCommands)/sizeof(MagickCommands[0]));
    132   if ((i == 0) && (argc > 1))
    133     {
    134       for (i=1; i < (ssize_t) (sizeof(MagickCommands)/sizeof(MagickCommands[0])); i++)
    135       {
    136         offset=LocaleCompare(MagickCommands[i].client_name,argv[1]);
    137         if (offset == 0)
    138           {
    139             argc--;
    140             argv++;
    141             break;
    142           }
    143       }
    144       i%=(sizeof(MagickCommands)/sizeof(MagickCommands[0]));
    145     }
    146   metadata=(char *) NULL;
    147   status=MagickCommandGenesis(image_info,MagickCommands[i].command,argc,argv,
    148     MagickCommands[i].use_metadata ? &metadata : (char **) NULL,exception);
    149   if (metadata != (char *) NULL)
    150     {
    151       (void) fputs(metadata,stdout);
    152       metadata=DestroyString(metadata);
    153     }
    154   if (MagickCommands[i].command != CompareImagesCommand)
    155     exit_code=status != MagickFalse ? 0 : 1;
    156   else
    157     {
    158       if (status == MagickFalse)
    159         exit_code=2;
    160       else
    161       {
    162         const char
    163           *option;
    164 
    165         option=GetImageOption(image_info,"compare:dissimilar");
    166         exit_code=IsStringTrue(option) ? 1 : 0;
    167       }
    168     }
    169   image_info=DestroyImageInfo(image_info);
    170   exception=DestroyExceptionInfo(exception);
    171   MagickCoreTerminus();
    172   return(exit_code);
    173 }
    174 
    175 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
    176 int main(int argc,char **argv)
    177 {
    178   return(MagickMain(argc,argv));
    179 }
    180 #else
    181 int wmain(int argc,wchar_t *argv[])
    182 {
    183   char
    184     **utf8;
    185 
    186   int
    187     status;
    188 
    189   register int
    190     i;
    191 
    192   utf8=NTArgvToUTF8(argc,argv);
    193   status=MagickMain(argc,utf8);
    194   for (i=0; i < argc; i++)
    195     utf8[i]=DestroyString(utf8[i]);
    196   utf8=(char **) RelinquishMagickMemory(utf8);
    197   return(status);
    198 }
    199 #endif
    200