2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AAI X Image Format %
20 % Copyright 1999-2012 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 "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"
67 static MagickBooleanType
68 WriteAAIImage(const ImageInfo *,Image *,ExceptionInfo *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d A A I I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
85 % The format of the ReadAAIImage method is:
87 % Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
96 static Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception)
110 register unsigned char
128 assert(image_info != (const ImageInfo *) NULL);
129 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
135 image=AcquireImage(image_info,exception);
136 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
137 if (status == MagickFalse)
139 image=DestroyImageList(image);
140 return((Image *) NULL);
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");
154 Convert AAI raster image to pixel packets.
156 image->columns=width;
159 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
160 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
162 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
164 if (pixels == (unsigned char *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 length=(size_t) 4*image->columns;
167 for (y=0; y < (ssize_t) image->rows; y++)
169 count=ReadBlob(image,length,pixels);
170 if ((size_t) count != length)
171 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
173 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
174 if (q == (Quantum *) NULL)
176 for (x=0; x < (ssize_t) image->columns; x++)
178 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
179 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
180 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
183 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
184 if (GetPixelAlpha(image,q) != OpaqueAlpha)
185 image->matte=MagickTrue;
186 q+=GetPixelChannels(image);
188 if (SyncAuthenticPixels(image,exception) == MagickFalse)
190 if (image->previous == (Image *) NULL)
192 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
194 if (status == MagickFalse)
198 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
199 if (EOFBlob(image) != MagickFalse)
201 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
206 Proceed to next image.
208 if (image_info->number_scenes != 0)
209 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
211 width=ReadBlobLSBLong(image);
212 height=ReadBlobLSBLong(image);
213 if ((width != 0UL) && (height != 0UL))
216 Allocate next image structure.
218 AcquireNextImage(image_info,image,exception);
219 if (GetNextImageInList(image) == (Image *) NULL)
221 image=DestroyImageList(image);
222 return((Image *) NULL);
224 image=SyncNextImageInList(image);
225 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
227 if (status == MagickFalse)
230 } while ((width != 0UL) && (height != 0UL));
231 (void) CloseBlob(image);
232 return(GetFirstImageInList(image));
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % R e g i s t e r A A I I m a g e %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % RegisterAAIImage() adds attributes for the AAI Dune 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.
252 % The format of the RegisterAAIImage method is:
254 % size_t RegisterAAIImage(void)
257 ModuleExport size_t RegisterAAIImage(void)
262 entry=SetMagickInfo("AAI");
263 entry->decoder=(DecodeImageHandler *) ReadAAIImage;
264 entry->encoder=(EncodeImageHandler *) WriteAAIImage;
265 entry->description=ConstantString("AAI Dune image");
266 entry->module=ConstantString("AAI");
267 (void) RegisterMagickInfo(entry);
268 return(MagickImageCoderSignature);
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % U n r e g i s t e r A A I I m a g e %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % UnregisterAAIImage() removes format registrations made by the
283 % AAI module from the list of supported formats.
285 % The format of the UnregisterAAIImage method is:
287 % UnregisterAAIImage(void)
290 ModuleExport void UnregisterAAIImage(void)
292 (void) UnregisterMagickInfo("AAI");
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % W r i t e A A I I m a g e %
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % WriteAAIImage() writes an image to a file in AAI Dune image format.
308 % The format of the WriteAAIImage method is:
310 % MagickBooleanType WriteAAIImage(const ImageInfo *image_info,
311 % Image *image,ExceptionInfo *exception)
313 % A description of each parameter follows.
315 % o image_info: the image info.
317 % o image: The image.
319 % o exception: return any errors or warnings in this structure.
322 static MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image,
323 ExceptionInfo *exception)
331 register const Quantum
337 register unsigned char
348 Open output image file.
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)
367 if (IsRGBColorspace(image->colorspace) == MagickFalse)
368 (void) TransformImageColorspace(image,RGBColorspace,exception);
369 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
370 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
372 Allocate memory for pixels.
374 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
376 if (pixels == (unsigned char *) NULL)
377 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
379 Convert MIFF to AAI raster pixels.
381 for (y=0; y < (ssize_t) image->rows; y++)
383 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
384 if (p == (const Quantum *) NULL)
387 for (x=0; x < (ssize_t) image->columns; x++)
389 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
390 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
391 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
392 *q=ScaleQuantumToChar((Quantum) (image->matte != MagickFalse ?
393 GetPixelAlpha(image,p) : OpaqueAlpha));
396 p+=GetPixelChannels(image);
399 count=WriteBlob(image,(size_t) (q-pixels),pixels);
400 if (count != (ssize_t) (q-pixels))
402 if (image->previous == (Image *) NULL)
404 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
406 if (status == MagickFalse)
410 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
411 if (GetNextImageInList(image) == (Image *) NULL)
413 image=SyncNextImageInList(image);
414 status=SetImageProgress(image,SaveImagesTag,scene++,
415 GetImageListLength(image));
416 if (status == MagickFalse)
418 } while (image_info->adjoin != MagickFalse);
419 (void) CloseBlob(image);