blob: 66f0c702b902d61831f9c3a8dc731d11c0a01a5e [file] [log] [blame]
cristy397bede2011-01-18 23:59:59 +00001/*
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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy397bede2011-01-18 23:59:59 +000017% July 1992 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy397bede2011-01-18 23:59:59 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000047#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristy397bede2011-01-18 23:59:59 +000063
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000068 WriteAAIImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy397bede2011-01-18 23:59:59 +000069
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*/
96static 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
cristy4c08aed2011-07-01 19:47:50 +0000107 register Quantum
cristy397bede2011-01-18 23:59:59 +0000108 *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 == 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);
cristy9950d572011-10-01 18:22:35 +0000135 image=AcquireImage(image_info,exception);
cristy397bede2011-01-18 23:59:59 +0000136 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;
cristyacabb842014-12-14 23:36:33 +0000162 status=SetImageExtent(image,image->columns,image->rows,exception);
163 if (status == MagickFalse)
164 return(DestroyImageList(image));
cristy397bede2011-01-18 23:59:59 +0000165 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 ((size_t) count != length)
174 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
175 p=pixels;
176 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000177 if (q == (Quantum *) NULL)
cristy397bede2011-01-18 23:59:59 +0000178 break;
179 for (x=0; x < (ssize_t) image->columns; x++)
180 {
cristy4c08aed2011-07-01 19:47:50 +0000181 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
182 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
183 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy520bce82011-01-19 19:41:27 +0000184 if (*p == 254)
185 *p=255;
cristy4c08aed2011-07-01 19:47:50 +0000186 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
187 if (GetPixelAlpha(image,q) != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +0000188 image->alpha_trait=BlendPixelTrait;
cristyed231572011-07-14 02:18:59 +0000189 q+=GetPixelChannels(image);
cristy397bede2011-01-18 23:59:59 +0000190 }
191 if (SyncAuthenticPixels(image,exception) == MagickFalse)
192 break;
cristybdadf312011-04-23 23:16:21 +0000193 if (image->previous == (Image *) NULL)
194 {
195 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
196 image->rows);
197 if (status == MagickFalse)
198 break;
199 }
cristy397bede2011-01-18 23:59:59 +0000200 }
201 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
202 if (EOFBlob(image) != MagickFalse)
203 {
204 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
205 image->filename);
206 break;
207 }
208 /*
209 Proceed to next image.
210 */
211 if (image_info->number_scenes != 0)
212 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
213 break;
214 width=ReadBlobLSBLong(image);
215 height=ReadBlobLSBLong(image);
216 if ((width != 0UL) && (height != 0UL))
217 {
218 /*
219 Allocate next image structure.
220 */
cristy9950d572011-10-01 18:22:35 +0000221 AcquireNextImage(image_info,image,exception);
cristy397bede2011-01-18 23:59:59 +0000222 if (GetNextImageInList(image) == (Image *) NULL)
223 {
224 image=DestroyImageList(image);
225 return((Image *) NULL);
226 }
227 image=SyncNextImageInList(image);
228 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
229 GetBlobSize(image));
230 if (status == MagickFalse)
231 break;
232 }
233 } while ((width != 0UL) && (height != 0UL));
234 (void) CloseBlob(image);
235 return(GetFirstImageInList(image));
236}
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
243% R e g i s t e r A A I I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% RegisterAAIImage() adds attributes for the AAI Dune image format to the list
250% of supported formats. The attributes include the image format tag, a
251% method to read and/or write the format, whether the format supports the
252% saving of more than one frame to the same file or blob, whether the format
253% supports native in-memory I/O, and a brief description of the format.
254%
255% The format of the RegisterAAIImage method is:
256%
257% size_t RegisterAAIImage(void)
258%
259*/
260ModuleExport size_t RegisterAAIImage(void)
261{
262 MagickInfo
263 *entry;
264
265 entry=SetMagickInfo("AAI");
266 entry->decoder=(DecodeImageHandler *) ReadAAIImage;
267 entry->encoder=(EncodeImageHandler *) WriteAAIImage;
268 entry->description=ConstantString("AAI Dune image");
269 entry->module=ConstantString("AAI");
270 (void) RegisterMagickInfo(entry);
271 return(MagickImageCoderSignature);
272}
273
274/*
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276% %
277% %
278% %
279% U n r e g i s t e r A A I I m a g e %
280% %
281% %
282% %
283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284%
285% UnregisterAAIImage() removes format registrations made by the
286% AAI module from the list of supported formats.
287%
288% The format of the UnregisterAAIImage method is:
289%
290% UnregisterAAIImage(void)
291%
292*/
293ModuleExport void UnregisterAAIImage(void)
294{
295 (void) UnregisterMagickInfo("AAI");
296}
297
298/*
299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300% %
301% %
302% %
303% W r i t e A A I I m a g e %
304% %
305% %
306% %
307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308%
309% WriteAAIImage() writes an image to a file in AAI Dune image format.
310%
311% The format of the WriteAAIImage method is:
312%
cristy1e178e72011-08-28 19:44:34 +0000313% MagickBooleanType WriteAAIImage(const ImageInfo *image_info,
314% Image *image,ExceptionInfo *exception)
cristy397bede2011-01-18 23:59:59 +0000315%
316% A description of each parameter follows.
317%
318% o image_info: the image info.
319%
320% o image: The image.
321%
cristy1e178e72011-08-28 19:44:34 +0000322% o exception: return any errors or warnings in this structure.
323%
cristy397bede2011-01-18 23:59:59 +0000324*/
cristy1e178e72011-08-28 19:44:34 +0000325static MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image,
326 ExceptionInfo *exception)
cristy397bede2011-01-18 23:59:59 +0000327{
328 MagickBooleanType
329 status;
330
331 MagickOffsetType
332 scene;
333
cristy4c08aed2011-07-01 19:47:50 +0000334 register const Quantum
cristy397bede2011-01-18 23:59:59 +0000335 *restrict p;
336
337 register ssize_t
338 x;
339
340 register unsigned char
341 *restrict q;
342
343 ssize_t
344 count,
345 y;
346
347 unsigned char
348 *pixels;
349
350 /*
351 Open output image file.
352 */
353 assert(image_info != (const ImageInfo *) NULL);
354 assert(image_info->signature == MagickSignature);
355 assert(image != (Image *) NULL);
356 assert(image->signature == MagickSignature);
357 if (image->debug != MagickFalse)
358 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000359 assert(exception != (ExceptionInfo *) NULL);
360 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000361 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy397bede2011-01-18 23:59:59 +0000362 if (status == MagickFalse)
363 return(status);
364 scene=0;
365 do
366 {
367 /*
368 Write AAI header.
369 */
cristyaf8d3912014-02-21 14:50:33 +0000370 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy397bede2011-01-18 23:59:59 +0000371 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
372 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
373 /*
374 Allocate memory for pixels.
375 */
cristybc4a1d42014-12-15 21:47:00 +0000376 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
cristy397bede2011-01-18 23:59:59 +0000377 4*sizeof(*pixels));
378 if (pixels == (unsigned char *) NULL)
379 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
380 /*
381 Convert MIFF to AAI raster pixels.
382 */
383 for (y=0; y < (ssize_t) image->rows; y++)
384 {
cristy1e178e72011-08-28 19:44:34 +0000385 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000386 if (p == (const Quantum *) NULL)
cristy397bede2011-01-18 23:59:59 +0000387 break;
388 q=pixels;
389 for (x=0; x < (ssize_t) image->columns; x++)
390 {
cristy4c08aed2011-07-01 19:47:50 +0000391 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
392 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
393 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy35553db2014-11-23 15:43:29 +0000394 *q=ScaleQuantumToChar((Quantum) (image->alpha_trait == BlendPixelTrait ?
cristy4c08aed2011-07-01 19:47:50 +0000395 GetPixelAlpha(image,p) : OpaqueAlpha));
cristy397bede2011-01-18 23:59:59 +0000396 if (*q == 255)
397 *q=254;
cristyed231572011-07-14 02:18:59 +0000398 p+=GetPixelChannels(image);
cristy397bede2011-01-18 23:59:59 +0000399 q++;
400 }
401 count=WriteBlob(image,(size_t) (q-pixels),pixels);
402 if (count != (ssize_t) (q-pixels))
403 break;
cristybdadf312011-04-23 23:16:21 +0000404 if (image->previous == (Image *) NULL)
405 {
406 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
407 image->rows);
408 if (status == MagickFalse)
409 break;
410 }
cristy397bede2011-01-18 23:59:59 +0000411 }
412 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
413 if (GetNextImageInList(image) == (Image *) NULL)
414 break;
415 image=SyncNextImageInList(image);
416 status=SetImageProgress(image,SaveImagesTag,scene++,
417 GetImageListLength(image));
418 if (status == MagickFalse)
419 break;
420 } while (image_info->adjoin != MagickFalse);
421 (void) CloseBlob(image);
422 return(MagickTrue);
423}