]> granicus.if.org Git - imagemagick/blob - coders/aai.c
https://github.com/ImageMagick/ImageMagick/issues/463
[imagemagick] / coders / aai.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             AAA    AAA   IIIII                              %
7 %                            A   A  A   A    I                                %
8 %                            AAAAA  AAAAA    I                                %
9 %                            A   A  A   A    I                                %
10 %                            A   A  A   A  IIIII                              %
11 %                                                                             %
12 %                                                                             %
13 %                        Read/Write AAI 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.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
63 \f
64 /*
65   Forward declarations.
66 */
67 static MagickBooleanType
68   WriteAAIImage(const ImageInfo *,Image *,ExceptionInfo *);
69 \f
70 /*
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %   R e a d A A I I m a g e                                                   %
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %
81 %  ReadAAIImage() reads an AAI Dune image file and returns it.  It
82 %  allocates the memory necessary for the new Image structure and returns a
83 %  pointer to the new image.
84 %
85 %  The format of the ReadAAIImage method is:
86 %
87 %      Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o image_info: the image info.
92 %
93 %    o exception: return any errors or warnings in this structure.
94 %
95 */
96 static Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
97 {
98   Image
99     *image;
100
101   MagickBooleanType
102     status;
103
104   register ssize_t
105     x;
106
107   register Quantum
108     *q;
109
110   register unsigned char
111     *p;
112
113   size_t
114     height,
115     length,
116     width;
117
118   ssize_t
119     count,
120     y;
121
122   unsigned char
123     *pixels;
124
125   /*
126     Open image file.
127   */
128   assert(image_info != (const ImageInfo *) NULL);
129   assert(image_info->signature == MagickCoreSignature);
130   if (image_info->debug != MagickFalse)
131     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132       image_info->filename);
133   assert(exception != (ExceptionInfo *) NULL);
134   assert(exception->signature == MagickCoreSignature);
135   image=AcquireImage(image_info,exception);
136   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
137   if (status == MagickFalse)
138     {
139       image=DestroyImageList(image);
140       return((Image *) NULL);
141     }
142   /*
143     Read AAI Dune image.
144   */
145   width=ReadBlobLSBLong(image);
146   height=ReadBlobLSBLong(image);
147   if (EOFBlob(image) != MagickFalse)
148     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
149   if ((width == 0UL) || (height == 0UL))
150     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151   do
152   {
153     /*
154       Convert AAI raster image to pixel packets.
155     */
156     image->columns=width;
157     image->rows=height;
158     image->depth=8;
159     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
160       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
161         break;
162     status=SetImageExtent(image,image->columns,image->rows,exception);
163     if (status == MagickFalse)
164       return(DestroyImageList(image));
165     pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
166       4*sizeof(*pixels));
167     if (pixels == (unsigned char *) NULL) 
168       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
169     length=(size_t) 4*image->columns;
170     for (y=0; y < (ssize_t) image->rows; y++)
171     {
172       count=ReadBlob(image,length,pixels);
173       if (count != (ssize_t) length)
174         {
175           pixels=(unsigned char *) RelinquishMagickMemory(pixels);
176           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
177         }
178       p=pixels;
179       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
180       if (q == (Quantum *) NULL)
181         break;
182       for (x=0; x < (ssize_t) image->columns; x++)
183       {
184         SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
185         SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
186         SetPixelRed(image,ScaleCharToQuantum(*p++),q);
187         if (*p == 254)
188           *p=255;
189         SetPixelAlpha(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     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
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=ReadBlobLSBLong(image);
218     height=ReadBlobLSBLong(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 A I I m a g e                                           %
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %
252 %  RegisterAAIImage() adds attributes for the AAI Dune 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 RegisterAAIImage method is:
259 %
260 %      size_t RegisterAAIImage(void)
261 %
262 */
263 ModuleExport size_t RegisterAAIImage(void)
264 {
265   MagickInfo
266     *entry;
267
268   entry=AcquireMagickInfo("AAI","AAI","AAI Dune image");
269   entry->decoder=(DecodeImageHandler *) ReadAAIImage;
270   entry->encoder=(EncodeImageHandler *) WriteAAIImage;
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 A I I m a g e                                       %
281 %                                                                             %
282 %                                                                             %
283 %                                                                             %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %
286 %  UnregisterAAIImage() removes format registrations made by the
287 %  AAI module from the list of supported formats.
288 %
289 %  The format of the UnregisterAAIImage method is:
290 %
291 %      UnregisterAAIImage(void)
292 %
293 */
294 ModuleExport void UnregisterAAIImage(void)
295 {
296   (void) UnregisterMagickInfo("AAI");
297 }
298 \f
299 /*
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %                                                                             %
302 %                                                                             %
303 %                                                                             %
304 %   W r i t e A A I I m a g e                                                 %
305 %                                                                             %
306 %                                                                             %
307 %                                                                             %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %
310 %  WriteAAIImage() writes an image to a file in AAI Dune image format.
311 %
312 %  The format of the WriteAAIImage method is:
313 %
314 %      MagickBooleanType WriteAAIImage(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 WriteAAIImage(const ImageInfo *image_info,Image *image,
327   ExceptionInfo *exception)
328 {
329   MagickBooleanType
330     status;
331
332   MagickOffsetType
333     scene;
334
335   register const Quantum
336     *magick_restrict p;
337
338   register ssize_t
339     x;
340
341   register unsigned char
342     *magick_restrict q;
343
344   ssize_t
345     count,
346     y;
347
348   unsigned char
349     *pixels;
350
351   /*
352     Open output image file.
353   */
354   assert(image_info != (const ImageInfo *) NULL);
355   assert(image_info->signature == MagickCoreSignature);
356   assert(image != (Image *) NULL);
357   assert(image->signature == MagickCoreSignature);
358   if (image->debug != MagickFalse)
359     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
360   assert(exception != (ExceptionInfo *) NULL);
361   assert(exception->signature == MagickCoreSignature);
362   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
363   if (status == MagickFalse)
364     return(status);
365   scene=0;
366   do
367   {
368     /*
369       Write AAI header.
370     */
371     (void) TransformImageColorspace(image,sRGBColorspace,exception);
372     (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
373     (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
374     /*
375       Allocate memory for pixels.
376     */
377     pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
378       4*sizeof(*pixels));
379     if (pixels == (unsigned char *) NULL)
380       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
381     /*
382       Convert MIFF to AAI raster pixels.
383     */
384     for (y=0; y < (ssize_t) image->rows; y++)
385     {
386       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
387       if (p == (const Quantum *) NULL)
388         break;
389       q=pixels;
390       for (x=0; x < (ssize_t) image->columns; x++)
391       {
392         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
393         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
394         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
395         *q=ScaleQuantumToChar((Quantum) (image->alpha_trait !=
396           UndefinedPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
397         if (*q == 255)
398           *q=254;
399         p+=GetPixelChannels(image);
400         q++;
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     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
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 }