Home | History | Annotate | Download | only in coders
      1 /*
      2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      3 %                                                                             %
      4 %                                                                             %
      5 %                                                                             %
      6 %                            V   V  IIIII  DDDD                               %
      7 %                            V   V    I    D   D                              %
      8 %                            V   V    I    D   D                              %
      9 %                             V V     I    D   D                              %
     10 %                              V    IIIII  DDDD                               %
     11 %                                                                             %
     12 %                                                                             %
     13 %             Return a Visual Image Directory for matching images.            %
     14 %                                                                             %
     15 %                              Software Design                                %
     16 %                                   Cristy                                    %
     17 %                                 July 1992                                   %
     18 %                                                                             %
     19 %                                                                             %
     20 %  Copyright 1999-2019 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 /*
     41   Include declarations.
     42 */
     43 #include "MagickCore/studio.h"
     44 #include "MagickCore/property.h"
     45 #include "MagickCore/blob.h"
     46 #include "MagickCore/blob-private.h"
     47 #include "MagickCore/constitute.h"
     48 #include "MagickCore/exception.h"
     49 #include "MagickCore/exception-private.h"
     50 #include "MagickCore/geometry.h"
     51 #include "MagickCore/image.h"
     52 #include "MagickCore/image-private.h"
     53 #include "MagickCore/list.h"
     54 #include "MagickCore/log.h"
     55 #include "MagickCore/magick.h"
     56 #include "MagickCore/memory_.h"
     57 #include "MagickCore/monitor.h"
     58 #include "MagickCore/monitor-private.h"
     59 #include "MagickCore/montage.h"
     60 #include "MagickCore/quantum-private.h"
     61 #include "MagickCore/resize.h"
     62 #include "MagickCore/static.h"
     63 #include "MagickCore/string_.h"
     64 #include "MagickCore/module.h"
     65 #include "MagickCore/utility.h"
     66 
     67 /*
     69   Forward declarations.
     70 */
     71 static MagickBooleanType
     72   WriteVIDImage(const ImageInfo *,Image *,ExceptionInfo *);
     73 
     74 /*
     76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     77 %                                                                             %
     78 %                                                                             %
     79 %                                                                             %
     80 %   R e a d V I D I m a g e                                                   %
     81 %                                                                             %
     82 %                                                                             %
     83 %                                                                             %
     84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     85 %
     86 %  ReadVIDImage reads one of more images and creates a Visual Image
     87 %  Directory file.  It allocates the memory necessary for the new Image
     88 %  structure and returns a pointer to the new image.
     89 %
     90 %  The format of the ReadVIDImage method is:
     91 %
     92 %      Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception)
     93 %
     94 %  A description of each parameter follows:
     95 %
     96 %    o image_info: the image info.
     97 %
     98 %    o exception: return any errors or warnings in this structure.
     99 %
    100 */
    101 static Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception)
    102 {
    103 #define ClientName  "montage"
    104 
    105   char
    106     **filelist,
    107     *label,
    108     **list;
    109 
    110   Image
    111     *image,
    112     *images,
    113     *montage_image,
    114     *next_image,
    115     *thumbnail_image;
    116 
    117   ImageInfo
    118     *read_info;
    119 
    120   int
    121     number_files;
    122 
    123   MagickBooleanType
    124     status;
    125 
    126   MontageInfo
    127     *montage_info;
    128 
    129   RectangleInfo
    130     geometry;
    131 
    132   register ssize_t
    133     i;
    134 
    135   /*
    136     Expand the filename.
    137   */
    138   assert(image_info != (const ImageInfo *) NULL);
    139   assert(image_info->signature == MagickCoreSignature);
    140   if (image_info->debug != MagickFalse)
    141     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
    142       image_info->filename);
    143   assert(exception != (ExceptionInfo *) NULL);
    144   assert(exception->signature == MagickCoreSignature);
    145   image=AcquireImage(image_info,exception);
    146   list=(char **) AcquireMagickMemory(sizeof(*filelist));
    147   if (list == (char **) NULL)
    148     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
    149   list[0]=ConstantString(image_info->filename);
    150   filelist=list;
    151   number_files=1;
    152   status=ExpandFilenames(&number_files,&filelist);
    153   list[0]=DestroyString(list[0]);
    154   list=(char **) RelinquishMagickMemory(list);
    155   if ((status == MagickFalse) || (number_files == 0))
    156     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
    157   image=DestroyImage(image);
    158   /*
    159     Read each image and convert them to a tile.
    160   */
    161   images=NewImageList();
    162   read_info=CloneImageInfo(image_info);
    163   SetImageInfoBlob(read_info,(void *) NULL,0);
    164   (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
    165     (void *) NULL);
    166   if (read_info->size == (char *) NULL)
    167     (void) CloneString(&read_info->size,DefaultTileGeometry);
    168   for (i=0; i < (ssize_t) number_files; i++)
    169   {
    170     if (image_info->debug != MagickFalse)
    171       (void) LogMagickEvent(CoderEvent,GetMagickModule(),"name: %s",
    172         filelist[i]);
    173     (void) CopyMagickString(read_info->filename,filelist[i],MagickPathExtent);
    174     filelist[i]=DestroyString(filelist[i]);
    175     *read_info->magick='\0';
    176     next_image=ReadImage(read_info,exception);
    177     CatchException(exception);
    178     if (next_image == (Image *) NULL)
    179       break;
    180     label=InterpretImageProperties((ImageInfo *) image_info,next_image,
    181       DefaultTileLabel,exception);
    182     (void) SetImageProperty(next_image,"label",label,exception);
    183     label=DestroyString(label);
    184     if (image_info->debug != MagickFalse)
    185       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
    186         "geometry: %.20gx%.20g",(double) next_image->columns,(double)
    187         next_image->rows);
    188     SetGeometry(next_image,&geometry);
    189     (void) ParseMetaGeometry(read_info->size,&geometry.x,&geometry.y,
    190       &geometry.width,&geometry.height);
    191     thumbnail_image=ThumbnailImage(next_image,geometry.width,geometry.height,
    192       exception);
    193     if (thumbnail_image != (Image *) NULL)
    194       {
    195         next_image=DestroyImage(next_image);
    196         next_image=thumbnail_image;
    197       }
    198     if (image_info->debug != MagickFalse)
    199       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
    200         "thumbnail geometry: %.20gx%.20g",(double) next_image->columns,(double)
    201         next_image->rows);
    202     AppendImageToList(&images,next_image);
    203     status=SetImageProgress(images,LoadImagesTag,i,number_files);
    204     if (status == MagickFalse)
    205       break;
    206   }
    207   read_info=DestroyImageInfo(read_info);
    208   filelist=(char **) RelinquishMagickMemory(filelist);
    209   if (images == (Image *) NULL)
    210     ThrowReaderException(CorruptImageError,
    211       "ImageFileDoesNotContainAnyImageData");
    212   /*
    213     Create the visual image directory.
    214   */
    215   montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
    216   if (image_info->debug != MagickFalse)
    217     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"creating montage");
    218   montage_image=MontageImageList(image_info,montage_info,
    219     GetFirstImageInList(images),exception);
    220   montage_info=DestroyMontageInfo(montage_info);
    221   images=DestroyImageList(images);
    222   return(montage_image);
    223 }
    224 
    225 /*
    227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    228 %                                                                             %
    229 %                                                                             %
    230 %                                                                             %
    231 %   R e g i s t e r V I D I m a g e                                           %
    232 %                                                                             %
    233 %                                                                             %
    234 %                                                                             %
    235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    236 %
    237 %  RegisterVIDImage() adds attributes for the VID image format to
    238 %  the list of supported formats.  The attributes include the image format
    239 %  tag, a method to read and/or write the format, whether the format
    240 %  supports the saving of more than one frame to the same file or blob,
    241 %  whether the format supports native in-memory I/O, and a brief
    242 %  description of the format.
    243 %
    244 %  The format of the RegisterVIDImage method is:
    245 %
    246 %      size_t RegisterVIDImage(void)
    247 %
    248 */
    249 ModuleExport size_t RegisterVIDImage(void)
    250 {
    251   MagickInfo
    252     *entry;
    253 
    254   entry=AcquireMagickInfo("VID","VID","Visual Image Directory");
    255   entry->decoder=(DecodeImageHandler *) ReadVIDImage;
    256   entry->encoder=(EncodeImageHandler *) WriteVIDImage;
    257   entry->format_type=ImplicitFormatType;
    258   (void) RegisterMagickInfo(entry);
    259   return(MagickImageCoderSignature);
    260 }
    261 
    262 /*
    264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    265 %                                                                             %
    266 %                                                                             %
    267 %                                                                             %
    268 %   U n r e g i s t e r V I D I m a g e                                       %
    269 %                                                                             %
    270 %                                                                             %
    271 %                                                                             %
    272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    273 %
    274 %  UnregisterVIDImage() removes format registrations made by the
    275 %  VID module from the list of supported formats.
    276 %
    277 %  The format of the UnregisterVIDImage method is:
    278 %
    279 %      UnregisterVIDImage(void)
    280 %
    281 */
    282 ModuleExport void UnregisterVIDImage(void)
    283 {
    284   (void) UnregisterMagickInfo("VID");
    285 }
    286 
    287 /*
    289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    290 %                                                                             %
    291 %                                                                             %
    292 %                                                                             %
    293 %   W r i t e V I D I m a g e                                                 %
    294 %                                                                             %
    295 %                                                                             %
    296 %                                                                             %
    297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    298 %
    299 %  WriteVIDImage() writes an image to a file in VID X image format.
    300 %
    301 %  The format of the WriteVIDImage method is:
    302 %
    303 %      MagickBooleanType WriteVIDImage(const ImageInfo *image_info,
    304 %        Image *image,ExceptionInfo *exception)
    305 %
    306 %  A description of each parameter follows.
    307 %
    308 %    o image_info: the image info.
    309 %
    310 %    o image:  The image.
    311 %
    312 %    o exception: return any errors or warnings in this structure.
    313 %
    314 */
    315 static MagickBooleanType WriteVIDImage(const ImageInfo *image_info,Image *image,
    316   ExceptionInfo *exception)
    317 {
    318   Image
    319     *montage_image;
    320 
    321   ImageInfo
    322     *write_info;
    323 
    324   MagickBooleanType
    325     status;
    326 
    327   MontageInfo
    328     *montage_info;
    329 
    330   register Image
    331     *p;
    332 
    333   /*
    334     Create the visual image directory.
    335   */
    336   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
    337     (void) SetImageProperty(p,"label",DefaultTileLabel,exception);
    338   montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
    339   montage_image=MontageImageList(image_info,montage_info,image,exception);
    340   montage_info=DestroyMontageInfo(montage_info);
    341   if (montage_image == (Image *) NULL)
    342     return(MagickFalse);
    343   (void) CopyMagickString(montage_image->filename,image_info->filename,
    344     MagickPathExtent);
    345   write_info=CloneImageInfo(image_info);
    346   *write_info->magick='\0';
    347   (void) SetImageInfo(write_info,1,exception);
    348   if ((*write_info->magick == '\0') ||
    349       (LocaleCompare(write_info->magick,"VID") == 0))
    350     (void) FormatLocaleString(montage_image->filename,MagickPathExtent,
    351       "miff:%s",write_info->filename);
    352   status=WriteImage(write_info,montage_image,exception);
    353   montage_image=DestroyImage(montage_image);
    354   write_info=DestroyImageInfo(write_info);
    355   return(status);
    356 }
    357