2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AAI X Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
64 static MagickBooleanType
65 WriteAAIImage(const ImageInfo *,Image *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d A A I I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadAAIImage() reads an AAI Dune 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.
82 % The format of the ReadAAIImage method is:
84 % Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
86 % A description of each parameter follows:
88 % o image_info: the image info.
90 % o exception: return any errors or warnings in this structure.
93 static Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
107 register unsigned char
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)
136 image=DestroyImageList(image);
137 return((Image *) NULL);
142 width=ReadBlobLSBLong(image);
143 height=ReadBlobLSBLong(image);
144 if (EOFBlob(image) != MagickFalse)
145 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
146 if ((width == 0UL) || (height == 0UL))
147 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151 Convert AAI raster image to pixel packets.
153 image->columns=width;
156 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
159 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
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++)
166 count=ReadBlob(image,length,pixels);
167 if ((size_t) count != length)
168 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
170 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
171 if (q == (PixelPacket *) NULL)
173 for (x=0; x < (ssize_t) image->columns; x++)
175 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
176 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
177 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
180 SetAlphaPixelComponent(q,ScaleCharToQuantum(*p++));
181 if (q->opacity != OpaqueOpacity)
182 image->matte=MagickTrue;
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
187 if (image->previous == (Image *) NULL)
189 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
191 if (status == MagickFalse)
195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
196 if (EOFBlob(image) != MagickFalse)
198 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
203 Proceed to next image.
205 if (image_info->number_scenes != 0)
206 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
208 width=ReadBlobLSBLong(image);
209 height=ReadBlobLSBLong(image);
210 if ((width != 0UL) && (height != 0UL))
213 Allocate next image structure.
215 AcquireNextImage(image_info,image);
216 if (GetNextImageInList(image) == (Image *) NULL)
218 image=DestroyImageList(image);
219 return((Image *) NULL);
221 image=SyncNextImageInList(image);
222 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
224 if (status == MagickFalse)
227 } while ((width != 0UL) && (height != 0UL));
228 (void) CloseBlob(image);
229 return(GetFirstImageInList(image));
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % R e g i s t e r A A I I m a g e %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % RegisterAAIImage() adds attributes for the AAI Dune image format to the list
244 % of supported formats. The attributes include the image format tag, a
245 % method to read and/or write the format, whether the format supports the
246 % saving of more than one frame to the same file or blob, whether the format
247 % supports native in-memory I/O, and a brief description of the format.
249 % The format of the RegisterAAIImage method is:
251 % size_t RegisterAAIImage(void)
254 ModuleExport size_t RegisterAAIImage(void)
259 entry=SetMagickInfo("AAI");
260 entry->decoder=(DecodeImageHandler *) ReadAAIImage;
261 entry->encoder=(EncodeImageHandler *) WriteAAIImage;
262 entry->description=ConstantString("AAI Dune image");
263 entry->module=ConstantString("AAI");
264 (void) RegisterMagickInfo(entry);
265 return(MagickImageCoderSignature);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % U n r e g i s t e r A A I I m a g e %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % UnregisterAAIImage() removes format registrations made by the
280 % AAI module from the list of supported formats.
282 % The format of the UnregisterAAIImage method is:
284 % UnregisterAAIImage(void)
287 ModuleExport void UnregisterAAIImage(void)
289 (void) UnregisterMagickInfo("AAI");
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % W r i t e A A I I m a g e %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % WriteAAIImage() writes an image to a file in AAI Dune image format.
305 % The format of the WriteAAIImage method is:
307 % MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image)
309 % A description of each parameter follows.
311 % o image_info: the image info.
313 % o image: The image.
316 static MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image)
324 register const PixelPacket
330 register unsigned char
341 Open output image file.
343 assert(image_info != (const ImageInfo *) NULL);
344 assert(image_info->signature == MagickSignature);
345 assert(image != (Image *) NULL);
346 assert(image->signature == MagickSignature);
347 if (image->debug != MagickFalse)
348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
349 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
350 if (status == MagickFalse)
358 if (image->colorspace != RGBColorspace)
359 (void) TransformImageColorspace(image,RGBColorspace);
360 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
361 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
363 Allocate memory for pixels.
365 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
367 if (pixels == (unsigned char *) NULL)
368 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
370 Convert MIFF to AAI raster pixels.
372 for (y=0; y < (ssize_t) image->rows; y++)
374 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
375 if (p == (PixelPacket *) NULL)
378 for (x=0; x < (ssize_t) image->columns; x++)
380 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
381 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
382 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
383 *q=ScaleQuantumToChar((Quantum) (QuantumRange-(image->matte !=
384 MagickFalse ? GetOpacityPixelComponent(p) : OpaqueOpacity)));
390 count=WriteBlob(image,(size_t) (q-pixels),pixels);
391 if (count != (ssize_t) (q-pixels))
393 if (image->previous == (Image *) NULL)
395 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
397 if (status == MagickFalse)
401 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
402 if (GetNextImageInList(image) == (Image *) NULL)
404 image=SyncNextImageInList(image);
405 status=SetImageProgress(image,SaveImagesTag,scene++,
406 GetImageListLength(image));
407 if (status == MagickFalse)
409 } while (image_info->adjoin != MagickFalse);
410 (void) CloseBlob(image);