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