1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             AAA   V   V  SSSSS                              %
7 %                            A   A  V   V  SS                                 %
8 %                            AAAAA  V   V   SSS                               %
9 %                            A   A   V V      SS                              %
10 %                            A   A    V    SSSSS                              %
11 %                                                                             %
12 %                                                                             %
13 %                        Read/Write AVS X Image Format                        %
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 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/module.h"
62 
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteAVSImage(const ImageInfo *,Image *,ExceptionInfo *);
68 
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e a d A V S I m a g e                                                   %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
80 %  ReadAVSImage() reads an AVS X image file and returns it.  It
81 %  allocates the memory necessary for the new Image structure and returns a
82 %  pointer to the new image.
83 %
84 %  The format of the ReadAVSImage method is:
85 %
86 %      Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o image_info: the image info.
91 %
92 %    o exception: return any errors or warnings in this structure.
93 %
94 */
ReadAVSImage(const ImageInfo * image_info,ExceptionInfo * exception)95 static Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 {
97   Image
98     *image;
99 
100   MagickBooleanType
101     status;
102 
103   MemoryInfo
104     *pixel_info;
105 
106   register Quantum
107     *q;
108 
109   register ssize_t
110     x;
111 
112   register unsigned char
113     *p;
114 
115   size_t
116     height,
117     length,
118     width;
119 
120   ssize_t
121     count,
122     y;
123 
124   unsigned char
125     *pixels;
126 
127   /*
128     Open image file.
129   */
130   assert(image_info != (const ImageInfo *) NULL);
131   assert(image_info->signature == MagickCoreSignature);
132   if (image_info->debug != MagickFalse)
133     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
134       image_info->filename);
135   assert(exception != (ExceptionInfo *) NULL);
136   assert(exception->signature == MagickCoreSignature);
137   image=AcquireImage(image_info,exception);
138   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
139   if (status == MagickFalse)
140     {
141       image=DestroyImageList(image);
142       return((Image *) NULL);
143     }
144   /*
145     Read AVS X image.
146   */
147   width=ReadBlobMSBLong(image);
148   height=ReadBlobMSBLong(image);
149   if (EOFBlob(image) != MagickFalse)
150     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151   if ((width == 0UL) || (height == 0UL))
152     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
153   do
154   {
155     /*
156       Convert AVS raster image to pixel packets.
157     */
158     image->columns=width;
159     image->rows=height;
160     image->depth=8;
161     image->alpha_trait=BlendPixelTrait;
162     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
163       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
164         break;
165     status=SetImageExtent(image,image->columns,image->rows,exception);
166     if (status == MagickFalse)
167       return(DestroyImageList(image));
168     pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
169     if (pixel_info == (MemoryInfo *) NULL)
170       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
171     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
172     length=(size_t) 4*image->columns;
173     for (y=0; y < (ssize_t) image->rows; y++)
174     {
175       count=ReadBlob(image,length,pixels);
176       if ((size_t) count != length)
177         {
178           pixel_info=RelinquishVirtualMemory(pixel_info);
179           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
180         }
181       p=pixels;
182       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
183       if (q == (Quantum *) NULL)
184         break;
185       for (x=0; x < (ssize_t) image->columns; x++)
186       {
187         SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
188         SetPixelRed(image,ScaleCharToQuantum(*p++),q);
189         SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
190         SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
191         q+=GetPixelChannels(image);
192       }
193       if (SyncAuthenticPixels(image,exception) == MagickFalse)
194         break;
195       if (image->previous == (Image *) NULL)
196         {
197           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
198             image->rows);
199           if (status == MagickFalse)
200             break;
201         }
202     }
203     pixel_info=RelinquishVirtualMemory(pixel_info);
204     if (EOFBlob(image) != MagickFalse)
205       {
206         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
207           image->filename);
208         break;
209       }
210     /*
211       Proceed to next image.
212     */
213     if (image_info->number_scenes != 0)
214       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
215         break;
216     width=ReadBlobMSBLong(image);
217     height=ReadBlobMSBLong(image);
218     if ((width != 0UL) && (height != 0UL))
219       {
220         /*
221           Allocate next image structure.
222         */
223         AcquireNextImage(image_info,image,exception);
224         if (GetNextImageInList(image) == (Image *) NULL)
225           {
226             status=MagickFalse;
227             break;
228           }
229         image=SyncNextImageInList(image);
230         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
231           GetBlobSize(image));
232         if (status == MagickFalse)
233           break;
234       }
235   } while ((width != 0UL) && (height != 0UL));
236   (void) CloseBlob(image);
237   if (status == MagickFalse)
238     return(DestroyImageList(image));
239   return(GetFirstImageInList(image));
240 }
241 
242 /*
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 %                                                                             %
245 %                                                                             %
246 %                                                                             %
247 %   R e g i s t e r A V S I m a g e                                           %
248 %                                                                             %
249 %                                                                             %
250 %                                                                             %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 %
253 %  RegisterAVSImage() adds attributes for the AVS X image format to the list
254 %  of supported formats.  The attributes include the image format tag, a
255 %  method to read and/or write the format, whether the format supports the
256 %  saving of more than one frame to the same file or blob, whether the format
257 %  supports native in-memory I/O, and a brief description of the format.
258 %
259 %  The format of the RegisterAVSImage method is:
260 %
261 %      size_t RegisterAVSImage(void)
262 %
263 */
RegisterAVSImage(void)264 ModuleExport size_t RegisterAVSImage(void)
265 {
266   MagickInfo
267     *entry;
268 
269   entry=AcquireMagickInfo("AVS","AVS","AVS X image");
270   entry->decoder=(DecodeImageHandler *) ReadAVSImage;
271   entry->encoder=(EncodeImageHandler *) WriteAVSImage;
272   (void) RegisterMagickInfo(entry);
273   return(MagickImageCoderSignature);
274 }
275 
276 /*
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %   U n r e g i s t e r A V S I m a g e                                       %
282 %                                                                             %
283 %                                                                             %
284 %                                                                             %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %
287 %  UnregisterAVSImage() removes format registrations made by the
288 %  AVS module from the list of supported formats.
289 %
290 %  The format of the UnregisterAVSImage method is:
291 %
292 %      UnregisterAVSImage(void)
293 %
294 */
UnregisterAVSImage(void)295 ModuleExport void UnregisterAVSImage(void)
296 {
297   (void) UnregisterMagickInfo("AVS");
298 }
299 
300 /*
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 %   W r i t e A V S I m a g e                                                 %
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 %
311 %  WriteAVSImage() writes an image to a file in AVS X image format.
312 %
313 %  The format of the WriteAVSImage method is:
314 %
315 %      MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
316 %        Image *image,ExceptionInfo *exception)
317 %
318 %  A description of each parameter follows.
319 %
320 %    o image_info: the image info.
321 %
322 %    o image:  The image.
323 %
324 %    o exception: return any errors or warnings in this structure.
325 %
326 */
WriteAVSImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)327 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
328   ExceptionInfo *exception)
329 {
330   MagickBooleanType
331     status;
332 
333   MagickOffsetType
334     scene;
335 
336   MemoryInfo
337     *pixel_info;
338 
339   register const Quantum
340     *magick_restrict p;
341 
342   register ssize_t
343     x;
344 
345   register unsigned char
346     *magick_restrict q;
347 
348   size_t
349     imageListLength;
350 
351   ssize_t
352     count,
353     y;
354 
355   unsigned char
356     *pixels;
357 
358   /*
359     Open output image file.
360   */
361   assert(image_info != (const ImageInfo *) NULL);
362   assert(image_info->signature == MagickCoreSignature);
363   assert(image != (Image *) NULL);
364   assert(image->signature == MagickCoreSignature);
365   if (image->debug != MagickFalse)
366     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
367   assert(exception != (ExceptionInfo *) NULL);
368   assert(exception->signature == MagickCoreSignature);
369   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
370   if (status == MagickFalse)
371     return(status);
372   scene=0;
373   imageListLength=GetImageListLength(image);
374   do
375   {
376     /*
377       Write AVS header.
378     */
379     (void) TransformImageColorspace(image,sRGBColorspace,exception);
380     (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
381     (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
382     /*
383       Allocate memory for pixels.
384     */
385     pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
386     if (pixel_info == (MemoryInfo *) NULL)
387       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
388     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
389     /*
390       Convert MIFF to AVS raster pixels.
391     */
392     for (y=0; y < (ssize_t) image->rows; y++)
393     {
394       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
395       if (p == (const Quantum *) NULL)
396         break;
397       q=pixels;
398       for (x=0; x < (ssize_t) image->columns; x++)
399       {
400         *q++=ScaleQuantumToChar((Quantum) (image->alpha_trait ==
401           BlendPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
402         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
403         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
404         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
405         p+=GetPixelChannels(image);
406       }
407       count=WriteBlob(image,(size_t) (q-pixels),pixels);
408       if (count != (ssize_t) (q-pixels))
409         break;
410       if (image->previous == (Image *) NULL)
411         {
412           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
413             image->rows);
414           if (status == MagickFalse)
415             break;
416         }
417     }
418     pixel_info=RelinquishVirtualMemory(pixel_info);
419     if (GetNextImageInList(image) == (Image *) NULL)
420       break;
421     image=SyncNextImageInList(image);
422     status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
423     if (status == MagickFalse)
424       break;
425   } while (image_info->adjoin != MagickFalse);
426   (void) CloseBlob(image);
427   return(MagickTrue);
428 }
429