blob: a06bf5242671c56551916031e92378393d6a21c3 [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% %
Cristyd8420112021-01-01 14:52:00 -050020% Copyright 1999-2021 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% %
Cristy83d74de2018-10-13 10:17:25 -040026% https://imagemagick.org/script/license.php %
cristy397bede2011-01-18 23:59:59 +000027% %
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
Cristyf2dc1dd2020-12-28 13:59:26 -0500104 ssize_t
cristy397bede2011-01-18 23:59:59 +0000105 x;
106
Cristyf2dc1dd2020-12-28 13:59:26 -0500107 Quantum
cristy397bede2011-01-18 23:59:59 +0000108 *q;
109
Cristyf2dc1dd2020-12-28 13:59:26 -0500110 unsigned char
cristy397bede2011-01-18 23:59:59 +0000111 *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);
cristye1c94d92015-06-28 12:16:33 +0000129 assert(image_info->signature == MagickCoreSignature);
cristy397bede2011-01-18 23:59:59 +0000130 if (image_info->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132 image_info->filename);
133 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000134 assert(exception->signature == MagickCoreSignature);
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);
cristy771c8842015-01-09 12:13:22 +0000173 if (count != (ssize_t) length)
Cristy448492b2017-04-26 16:22:19 -0400174 {
175 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
176 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
177 }
cristy397bede2011-01-18 23:59:59 +0000178 p=pixels;
179 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000180 if (q == (Quantum *) NULL)
cristy397bede2011-01-18 23:59:59 +0000181 break;
182 for (x=0; x < (ssize_t) image->columns; x++)
183 {
cristy4c08aed2011-07-01 19:47:50 +0000184 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
185 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
186 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy520bce82011-01-19 19:41:27 +0000187 if (*p == 254)
188 *p=255;
cristy4c08aed2011-07-01 19:47:50 +0000189 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
190 if (GetPixelAlpha(image,q) != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +0000191 image->alpha_trait=BlendPixelTrait;
cristyed231572011-07-14 02:18:59 +0000192 q+=GetPixelChannels(image);
cristy397bede2011-01-18 23:59:59 +0000193 }
194 if (SyncAuthenticPixels(image,exception) == MagickFalse)
195 break;
cristybdadf312011-04-23 23:16:21 +0000196 if (image->previous == (Image *) NULL)
197 {
198 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
199 image->rows);
200 if (status == MagickFalse)
201 break;
202 }
cristy397bede2011-01-18 23:59:59 +0000203 }
204 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
205 if (EOFBlob(image) != MagickFalse)
206 {
207 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
208 image->filename);
209 break;
210 }
211 /*
212 Proceed to next image.
213 */
214 if (image_info->number_scenes != 0)
215 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
216 break;
217 width=ReadBlobLSBLong(image);
218 height=ReadBlobLSBLong(image);
219 if ((width != 0UL) && (height != 0UL))
220 {
221 /*
222 Allocate next image structure.
223 */
cristy9950d572011-10-01 18:22:35 +0000224 AcquireNextImage(image_info,image,exception);
cristy397bede2011-01-18 23:59:59 +0000225 if (GetNextImageInList(image) == (Image *) NULL)
226 {
Cristy3b48d202018-07-01 17:11:51 -0400227 status=MagickFalse;
228 break;
cristy397bede2011-01-18 23:59:59 +0000229 }
230 image=SyncNextImageInList(image);
231 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
232 GetBlobSize(image));
233 if (status == MagickFalse)
234 break;
235 }
236 } while ((width != 0UL) && (height != 0UL));
237 (void) CloseBlob(image);
Cristy3b48d202018-07-01 17:11:51 -0400238 if (status == MagickFalse)
239 return(DestroyImageList(image));
cristy397bede2011-01-18 23:59:59 +0000240 return(GetFirstImageInList(image));
241}
242
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245% %
246% %
247% %
248% R e g i s t e r A A I I m a g e %
249% %
250% %
251% %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
254% RegisterAAIImage() adds attributes for the AAI Dune image format to the list
255% of supported formats. The attributes include the image format tag, a
256% method to read and/or write the format, whether the format supports the
257% saving of more than one frame to the same file or blob, whether the format
258% supports native in-memory I/O, and a brief description of the format.
259%
260% The format of the RegisterAAIImage method is:
261%
262% size_t RegisterAAIImage(void)
263%
264*/
265ModuleExport size_t RegisterAAIImage(void)
266{
267 MagickInfo
268 *entry;
269
dirk06b627a2015-04-06 18:59:17 +0000270 entry=AcquireMagickInfo("AAI","AAI","AAI Dune image");
cristy397bede2011-01-18 23:59:59 +0000271 entry->decoder=(DecodeImageHandler *) ReadAAIImage;
272 entry->encoder=(EncodeImageHandler *) WriteAAIImage;
cristy397bede2011-01-18 23:59:59 +0000273 (void) RegisterMagickInfo(entry);
274 return(MagickImageCoderSignature);
275}
276
277/*
278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279% %
280% %
281% %
282% U n r e g i s t e r A A I I m a g e %
283% %
284% %
285% %
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287%
288% UnregisterAAIImage() removes format registrations made by the
289% AAI module from the list of supported formats.
290%
291% The format of the UnregisterAAIImage method is:
292%
293% UnregisterAAIImage(void)
294%
295*/
296ModuleExport void UnregisterAAIImage(void)
297{
298 (void) UnregisterMagickInfo("AAI");
299}
300
301/*
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303% %
304% %
305% %
306% W r i t e A A I I m a g e %
307% %
308% %
309% %
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311%
312% WriteAAIImage() writes an image to a file in AAI Dune image format.
313%
314% The format of the WriteAAIImage method is:
315%
cristy1e178e72011-08-28 19:44:34 +0000316% MagickBooleanType WriteAAIImage(const ImageInfo *image_info,
317% Image *image,ExceptionInfo *exception)
cristy397bede2011-01-18 23:59:59 +0000318%
319% A description of each parameter follows.
320%
321% o image_info: the image info.
322%
323% o image: The image.
324%
cristy1e178e72011-08-28 19:44:34 +0000325% o exception: return any errors or warnings in this structure.
326%
cristy397bede2011-01-18 23:59:59 +0000327*/
cristy1e178e72011-08-28 19:44:34 +0000328static MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image,
329 ExceptionInfo *exception)
cristy397bede2011-01-18 23:59:59 +0000330{
331 MagickBooleanType
332 status;
333
334 MagickOffsetType
335 scene;
336
Cristyf2dc1dd2020-12-28 13:59:26 -0500337 const Quantum
dirk05d2ff72015-11-18 23:13:43 +0100338 *magick_restrict p;
cristy397bede2011-01-18 23:59:59 +0000339
Cristyf2dc1dd2020-12-28 13:59:26 -0500340 ssize_t
cristy397bede2011-01-18 23:59:59 +0000341 x;
342
Cristyf2dc1dd2020-12-28 13:59:26 -0500343 unsigned char
dirk05d2ff72015-11-18 23:13:43 +0100344 *magick_restrict q;
cristy397bede2011-01-18 23:59:59 +0000345
Cristyc45b2bb2018-04-07 12:32:12 -0400346 size_t
347 imageListLength;
348
cristy397bede2011-01-18 23:59:59 +0000349 ssize_t
350 count,
351 y;
352
353 unsigned char
354 *pixels;
355
356 /*
357 Open output image file.
358 */
359 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000360 assert(image_info->signature == MagickCoreSignature);
cristy397bede2011-01-18 23:59:59 +0000361 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000362 assert(image->signature == MagickCoreSignature);
cristy397bede2011-01-18 23:59:59 +0000363 if (image->debug != MagickFalse)
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000365 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000366 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +0000367 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy397bede2011-01-18 23:59:59 +0000368 if (status == MagickFalse)
369 return(status);
370 scene=0;
Cristyc45b2bb2018-04-07 12:32:12 -0400371 imageListLength=GetImageListLength(image);
cristy397bede2011-01-18 23:59:59 +0000372 do
373 {
374 /*
375 Write AAI header.
376 */
cristyaf8d3912014-02-21 14:50:33 +0000377 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy397bede2011-01-18 23:59:59 +0000378 (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
379 (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
380 /*
381 Allocate memory for pixels.
382 */
cristybc4a1d42014-12-15 21:47:00 +0000383 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
cristy397bede2011-01-18 23:59:59 +0000384 4*sizeof(*pixels));
385 if (pixels == (unsigned char *) NULL)
386 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
387 /*
388 Convert MIFF to AAI raster pixels.
389 */
390 for (y=0; y < (ssize_t) image->rows; y++)
391 {
cristy1e178e72011-08-28 19:44:34 +0000392 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000393 if (p == (const Quantum *) NULL)
cristy397bede2011-01-18 23:59:59 +0000394 break;
395 q=pixels;
396 for (x=0; x < (ssize_t) image->columns; x++)
397 {
cristy4c08aed2011-07-01 19:47:50 +0000398 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
399 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
400 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
Cristyf8d9cb82015-09-11 11:51:22 -0400401 *q=ScaleQuantumToChar((Quantum) (image->alpha_trait !=
402 UndefinedPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
cristy397bede2011-01-18 23:59:59 +0000403 if (*q == 255)
404 *q=254;
cristyed231572011-07-14 02:18:59 +0000405 p+=GetPixelChannels(image);
cristy397bede2011-01-18 23:59:59 +0000406 q++;
407 }
408 count=WriteBlob(image,(size_t) (q-pixels),pixels);
409 if (count != (ssize_t) (q-pixels))
410 break;
cristybdadf312011-04-23 23:16:21 +0000411 if (image->previous == (Image *) NULL)
412 {
413 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
414 image->rows);
415 if (status == MagickFalse)
416 break;
417 }
cristy397bede2011-01-18 23:59:59 +0000418 }
419 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
420 if (GetNextImageInList(image) == (Image *) NULL)
421 break;
422 image=SyncNextImageInList(image);
Cristyc45b2bb2018-04-07 12:32:12 -0400423 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
cristy397bede2011-01-18 23:59:59 +0000424 if (status == MagickFalse)
425 break;
426 } while (image_info->adjoin != MagickFalse);
427 (void) CloseBlob(image);
428 return(MagickTrue);
429}