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