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