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-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38
39 /*
40 Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/property.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/geometry.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/log.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/montage.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/resize.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/utility.h"
65
66 /*
67 Forward declarations.
68 */
69 static MagickBooleanType
70 WriteVIDImage(const ImageInfo *,Image *,ExceptionInfo *);
71
72 /*
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % %
75 % %
76 % %
77 % R e a d V I D I m a g e %
78 % %
79 % %
80 % %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %
83 % ReadVIDImage reads one of more images and creates a Visual Image
84 % Directory file. It allocates the memory necessary for the new Image
85 % structure and returns a pointer to the new image.
86 %
87 % The format of the ReadVIDImage method is:
88 %
89 % Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 %
91 % A description of each parameter follows:
92 %
93 % o image_info: the image info.
94 %
95 % o exception: return any errors or warnings in this structure.
96 %
97 */
ReadVIDImage(const ImageInfo * image_info,ExceptionInfo * exception)98 static Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception)
99 {
100 #define ClientName "montage"
101
102 char
103 **filelist,
104 *label,
105 **list;
106
107 Image
108 *image,
109 *images,
110 *montage_image,
111 *next_image,
112 *thumbnail_image;
113
114 ImageInfo
115 *read_info;
116
117 int
118 number_files;
119
120 MagickBooleanType
121 status;
122
123 MontageInfo
124 *montage_info;
125
126 RectangleInfo
127 geometry;
128
129 ssize_t
130 i;
131
132 /*
133 Expand the filename.
134 */
135 assert(image_info != (const ImageInfo *) NULL);
136 assert(image_info->signature == MagickCoreSignature);
137 if (image_info->debug != MagickFalse)
138 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
139 image_info->filename);
140 assert(exception != (ExceptionInfo *) NULL);
141 assert(exception->signature == MagickCoreSignature);
142 image=AcquireImage(image_info,exception);
143 list=(char **) AcquireMagickMemory(sizeof(*filelist));
144 if (list == (char **) NULL)
145 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
146 list[0]=ConstantString(image_info->filename);
147 filelist=list;
148 number_files=1;
149 status=ExpandFilenames(&number_files,&filelist);
150 list[0]=DestroyString(list[0]);
151 list=(char **) RelinquishMagickMemory(list);
152 if ((status == MagickFalse) || (number_files == 0))
153 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
154 image=DestroyImage(image);
155 /*
156 Read each image and convert them to a tile.
157 */
158 images=NewImageList();
159 read_info=CloneImageInfo(image_info);
160 SetImageInfoBlob(read_info,(void *) NULL,0);
161 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
162 (void *) NULL);
163 if (read_info->size == (char *) NULL)
164 (void) CloneString(&read_info->size,DefaultTileGeometry);
165 for (i=0; i < (ssize_t) number_files; i++)
166 {
167 if (image_info->debug != MagickFalse)
168 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"name: %s",
169 filelist[i]);
170 (void) CopyMagickString(read_info->filename,filelist[i],MagickPathExtent);
171 filelist[i]=DestroyString(filelist[i]);
172 *read_info->magick='\0';
173 next_image=ReadImage(read_info,exception);
174 CatchException(exception);
175 if (next_image == (Image *) NULL)
176 break;
177 label=InterpretImageProperties((ImageInfo *) image_info,next_image,
178 DefaultTileLabel,exception);
179 if (label != (char *) NULL)
180 {
181 (void) SetImageProperty(next_image,"label",label,exception);
182 label=DestroyString(label);
183 }
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 /*
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % %
228 % %
229 % %
230 % R e g i s t e r V I D I m a g e %
231 % %
232 % %
233 % %
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %
236 % RegisterVIDImage() adds attributes for the VID image format to
237 % the list of supported formats. The attributes include the image format
238 % tag, a method to read and/or write the format, whether the format
239 % supports the saving of more than one frame to the same file or blob,
240 % whether the format supports native in-memory I/O, and a brief
241 % description of the format.
242 %
243 % The format of the RegisterVIDImage method is:
244 %
245 % size_t RegisterVIDImage(void)
246 %
247 */
RegisterVIDImage(void)248 ModuleExport size_t RegisterVIDImage(void)
249 {
250 MagickInfo
251 *entry;
252
253 entry=AcquireMagickInfo("VID","VID","Visual Image Directory");
254 entry->decoder=(DecodeImageHandler *) ReadVIDImage;
255 entry->encoder=(EncodeImageHandler *) WriteVIDImage;
256 entry->format_type=ImplicitFormatType;
257 (void) RegisterMagickInfo(entry);
258 return(MagickImageCoderSignature);
259 }
260
261 /*
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 % %
264 % %
265 % %
266 % U n r e g i s t e r V I D I m a g e %
267 % %
268 % %
269 % %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 %
272 % UnregisterVIDImage() removes format registrations made by the
273 % VID module from the list of supported formats.
274 %
275 % The format of the UnregisterVIDImage method is:
276 %
277 % UnregisterVIDImage(void)
278 %
279 */
UnregisterVIDImage(void)280 ModuleExport void UnregisterVIDImage(void)
281 {
282 (void) UnregisterMagickInfo("VID");
283 }
284
285 /*
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % %
288 % %
289 % %
290 % W r i t e V I D I m a g e %
291 % %
292 % %
293 % %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 %
296 % WriteVIDImage() writes an image to a file in VID X image format.
297 %
298 % The format of the WriteVIDImage method is:
299 %
300 % MagickBooleanType WriteVIDImage(const ImageInfo *image_info,
301 % Image *image,ExceptionInfo *exception)
302 %
303 % A description of each parameter follows.
304 %
305 % o image_info: the image info.
306 %
307 % o image: The image.
308 %
309 % o exception: return any errors or warnings in this structure.
310 %
311 */
WriteVIDImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)312 static MagickBooleanType WriteVIDImage(const ImageInfo *image_info,Image *image,
313 ExceptionInfo *exception)
314 {
315 Image
316 *montage_image;
317
318 ImageInfo
319 *write_info;
320
321 MagickBooleanType
322 status;
323
324 MontageInfo
325 *montage_info;
326
327 Image
328 *p;
329
330 /*
331 Create the visual image directory.
332 */
333 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
334 (void) SetImageProperty(p,"label",DefaultTileLabel,exception);
335 montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL);
336 montage_image=MontageImageList(image_info,montage_info,image,exception);
337 montage_info=DestroyMontageInfo(montage_info);
338 if (montage_image == (Image *) NULL)
339 return(MagickFalse);
340 (void) CopyMagickString(montage_image->filename,image_info->filename,
341 MagickPathExtent);
342 write_info=CloneImageInfo(image_info);
343 *write_info->magick='\0';
344 (void) SetImageInfo(write_info,1,exception);
345 if ((*write_info->magick == '\0') ||
346 (LocaleCompare(write_info->magick,"VID") == 0))
347 (void) FormatLocaleString(montage_image->filename,MagickPathExtent,
348 "miff:%s",write_info->filename);
349 status=WriteImage(write_info,montage_image,exception);
350 montage_image=DestroyImage(montage_image);
351 write_info=DestroyImageInfo(write_info);
352 return(status);
353 }
354