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