]> granicus.if.org Git - imagemagick/blob - coders/avs.c
...
[imagemagick] / coders / avs.c
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-2017 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://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 %
37 */
38 \f
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 \f
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteAVSImage(const ImageInfo *,Image *,ExceptionInfo *);
68 \f
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 */
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     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
162       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
163         break;
164     status=SetImageExtent(image,image->columns,image->rows,exception);
165     if (status == MagickFalse)
166       return(DestroyImageList(image));
167     pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
168     if (pixel_info == (MemoryInfo *) NULL)
169       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
170     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
171     length=(size_t) 4*image->columns;
172     for (y=0; y < (ssize_t) image->rows; y++)
173     {
174       count=ReadBlob(image,length,pixels);
175       if ((size_t) count != length)
176         {
177           pixel_info=RelinquishVirtualMemory(pixel_info);
178           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
179         }
180       p=pixels;
181       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
182       if (q == (Quantum *) NULL)
183         break;
184       for (x=0; x < (ssize_t) image->columns; x++)
185       {
186         SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
187         SetPixelRed(image,ScaleCharToQuantum(*p++),q);
188         SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
189         SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
190         if (GetPixelAlpha(image,q) != OpaqueAlpha)
191           image->alpha_trait=BlendPixelTrait;
192         q+=GetPixelChannels(image);
193       }
194       if (SyncAuthenticPixels(image,exception) == MagickFalse)
195         break;
196       if (image->previous == (Image *) NULL)
197         {
198           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
199             image->rows);
200           if (status == MagickFalse)
201             break;
202         }
203     }
204     pixel_info=RelinquishVirtualMemory(pixel_info);
205     if (EOFBlob(image) != MagickFalse)
206       {
207         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
208           image->filename);
209         break;
210       }
211     /*
212       Proceed to next image.
213     */
214     if (image_info->number_scenes != 0)
215       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
216         break;
217     width=ReadBlobMSBLong(image);
218     height=ReadBlobMSBLong(image);
219     if ((width != 0UL) && (height != 0UL))
220       {
221         /*
222           Allocate next image structure.
223         */
224         AcquireNextImage(image_info,image,exception);
225         if (GetNextImageInList(image) == (Image *) NULL)
226           {
227             image=DestroyImageList(image);
228             return((Image *) NULL);
229           }
230         image=SyncNextImageInList(image);
231         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
232           GetBlobSize(image));
233         if (status == MagickFalse)
234           break;
235       }
236   } while ((width != 0UL) && (height != 0UL));
237   (void) CloseBlob(image);
238   return(GetFirstImageInList(image));
239 }
240 \f
241 /*
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %   R e g i s t e r A V S I m a g e                                           %
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %
252 %  RegisterAVSImage() adds attributes for the AVS X image format to the list
253 %  of supported formats.  The attributes include the image format tag, a
254 %  method to read and/or write the format, whether the format supports the
255 %  saving of more than one frame to the same file or blob, whether the format
256 %  supports native in-memory I/O, and a brief description of the format.
257 %
258 %  The format of the RegisterAVSImage method is:
259 %
260 %      size_t RegisterAVSImage(void)
261 %
262 */
263 ModuleExport size_t RegisterAVSImage(void)
264 {
265   MagickInfo
266     *entry;
267
268   entry=AcquireMagickInfo("AVS","AVS","AVS X image");
269   entry->decoder=(DecodeImageHandler *) ReadAVSImage;
270   entry->encoder=(EncodeImageHandler *) WriteAVSImage;
271   (void) RegisterMagickInfo(entry);
272   return(MagickImageCoderSignature);
273 }
274 \f
275 /*
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 %                                                                             %
278 %                                                                             %
279 %                                                                             %
280 %   U n r e g i s t e r A V S I m a g e                                       %
281 %                                                                             %
282 %                                                                             %
283 %                                                                             %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %
286 %  UnregisterAVSImage() removes format registrations made by the
287 %  AVS module from the list of supported formats.
288 %
289 %  The format of the UnregisterAVSImage method is:
290 %
291 %      UnregisterAVSImage(void)
292 %
293 */
294 ModuleExport void UnregisterAVSImage(void)
295 {
296   (void) UnregisterMagickInfo("AVS");
297 }
298 \f
299 /*
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %                                                                             %
302 %                                                                             %
303 %                                                                             %
304 %   W r i t e A V S I m a g e                                                 %
305 %                                                                             %
306 %                                                                             %
307 %                                                                             %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %
310 %  WriteAVSImage() writes an image to a file in AVS X image format.
311 %
312 %  The format of the WriteAVSImage method is:
313 %
314 %      MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
315 %        Image *image,ExceptionInfo *exception)
316 %
317 %  A description of each parameter follows.
318 %
319 %    o image_info: the image info.
320 %
321 %    o image:  The image.
322 %
323 %    o exception: return any errors or warnings in this structure.
324 %
325 */
326 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
327   ExceptionInfo *exception)
328 {
329   MagickBooleanType
330     status;
331
332   MagickOffsetType
333     scene;
334
335   MemoryInfo
336     *pixel_info;
337
338   register const Quantum
339     *magick_restrict p;
340
341   register ssize_t
342     x;
343
344   register unsigned char
345     *magick_restrict q;
346
347   ssize_t
348     count,
349     y;
350
351   unsigned char
352     *pixels;
353
354   /*
355     Open output image file.
356   */
357   assert(image_info != (const ImageInfo *) NULL);
358   assert(image_info->signature == MagickCoreSignature);
359   assert(image != (Image *) NULL);
360   assert(image->signature == MagickCoreSignature);
361   if (image->debug != MagickFalse)
362     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
363   assert(exception != (ExceptionInfo *) NULL);
364   assert(exception->signature == MagickCoreSignature);
365   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
366   if (status == MagickFalse)
367     return(status);
368   scene=0;
369   do
370   {
371     /*
372       Write AVS header.
373     */
374     (void) TransformImageColorspace(image,sRGBColorspace,exception);
375     (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
376     (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
377     /*
378       Allocate memory for pixels.
379     */
380     pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
381     if (pixel_info == (MemoryInfo *) NULL)
382       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
383     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
384     /*
385       Convert MIFF to AVS raster pixels.
386     */
387     for (y=0; y < (ssize_t) image->rows; y++)
388     {
389       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
390       if (p == (const Quantum *) NULL)
391         break;
392       q=pixels;
393       for (x=0; x < (ssize_t) image->columns; x++)
394       {
395         *q++=ScaleQuantumToChar((Quantum) (image->alpha_trait ==
396           BlendPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
397         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
398         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
399         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
400         p+=GetPixelChannels(image);
401       }
402       count=WriteBlob(image,(size_t) (q-pixels),pixels);
403       if (count != (ssize_t) (q-pixels))
404         break;
405       if (image->previous == (Image *) NULL)
406         {
407           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
408             image->rows);
409           if (status == MagickFalse)
410             break;
411         }
412     }
413     pixel_info=RelinquishVirtualMemory(pixel_info);
414     if (GetNextImageInList(image) == (Image *) NULL)
415       break;
416     image=SyncNextImageInList(image);
417     status=SetImageProgress(image,SaveImagesTag,scene++,
418       GetImageListLength(image));
419     if (status == MagickFalse)
420       break;
421   } while (image_info->adjoin != MagickFalse);
422   (void) CloseBlob(image);
423   return(MagickTrue);
424 }