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