blob: dc9012a35df5c6e346259f9b120032da7602e1a9 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% AAA V V SSSSS %
7% A A V V SS %
8% AAAAA V V SSS %
9% A A V V SS %
10% A A V SSSSS %
11% %
12% %
13% Read/Write AVS X Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +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-accessor.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/static.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Forward declarations.
65*/
66static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000067 WriteAVSImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000068
69/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71% %
72% %
73% %
74% R e a d A V S I m a g e %
75% %
76% %
77% %
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79%
80% ReadAVSImage() reads an AVS X 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 ReadAVSImage method is:
85%
86% Image *ReadAVSImage(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 *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
96{
97 Image
98 *image;
99
cristy3ed852e2009-09-05 21:47:34 +0000100 MagickBooleanType
101 status;
102
cristy4c08aed2011-07-01 19:47:50 +0000103 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000104 *q;
105
cristybdadf312011-04-23 23:16:21 +0000106 register ssize_t
107 x;
108
cristy3ed852e2009-09-05 21:47:34 +0000109 register unsigned char
110 *p;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 size_t
cristye179ce22010-09-16 23:46:34 +0000113 height,
114 length,
115 width;
116
117 ssize_t
118 count,
119 y;
cristy3ed852e2009-09-05 21:47:34 +0000120
121 unsigned char
122 *pixels;
123
cristy3ed852e2009-09-05 21:47:34 +0000124 /*
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);
cristy9950d572011-10-01 18:22:35 +0000134 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000135 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136 if (status == MagickFalse)
137 {
138 image=DestroyImageList(image);
139 return((Image *) NULL);
140 }
141 /*
142 Read AVS X image.
143 */
144 width=ReadBlobMSBLong(image);
145 height=ReadBlobMSBLong(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 AVS 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;
cristybb503372010-05-27 20:51:26 +0000166 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000167 {
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);
cristyacd2ed22011-08-30 01:44:23 +0000173 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000174 break;
cristybb503372010-05-27 20:51:26 +0000175 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000176 {
cristy4c08aed2011-07-01 19:47:50 +0000177 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
178 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
179 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
180 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
181 if (GetPixelAlpha(image,q) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000182 image->matte=MagickTrue;
cristyed231572011-07-14 02:18:59 +0000183 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000184 }
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
186 break;
cristybdadf312011-04-23 23:16:21 +0000187 if (image->previous == (Image *) NULL)
188 {
189 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
190 image->rows);
191 if (status == MagickFalse)
192 break;
193 }
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
196 if (EOFBlob(image) != MagickFalse)
197 {
198 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
199 image->filename);
200 break;
201 }
202 /*
203 Proceed to next image.
204 */
205 if (image_info->number_scenes != 0)
206 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
207 break;
208 width=ReadBlobMSBLong(image);
209 height=ReadBlobMSBLong(image);
210 if ((width != 0UL) && (height != 0UL))
211 {
212 /*
213 Allocate next image structure.
214 */
cristy9950d572011-10-01 18:22:35 +0000215 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000216 if (GetNextImageInList(image) == (Image *) NULL)
217 {
218 image=DestroyImageList(image);
219 return((Image *) NULL);
220 }
221 image=SyncNextImageInList(image);
222 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
223 GetBlobSize(image));
224 if (status == MagickFalse)
225 break;
226 }
227 } while ((width != 0UL) && (height != 0UL));
228 (void) CloseBlob(image);
229 return(GetFirstImageInList(image));
230}
231
232/*
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234% %
235% %
236% %
237% R e g i s t e r A V S I m a g e %
238% %
239% %
240% %
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242%
243% RegisterAVSImage() adds attributes for the AVS X image format to the list
244% of supported formats. The attributes include the image format tag, a
245% method to read and/or write the format, whether the format supports the
246% saving of more than one frame to the same file or blob, whether the format
247% supports native in-memory I/O, and a brief description of the format.
248%
249% The format of the RegisterAVSImage method is:
250%
cristybb503372010-05-27 20:51:26 +0000251% size_t RegisterAVSImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000252%
253*/
cristybb503372010-05-27 20:51:26 +0000254ModuleExport size_t RegisterAVSImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000255{
256 MagickInfo
257 *entry;
258
259 entry=SetMagickInfo("AVS");
260 entry->decoder=(DecodeImageHandler *) ReadAVSImage;
261 entry->encoder=(EncodeImageHandler *) WriteAVSImage;
262 entry->description=ConstantString("AVS X image");
263 entry->module=ConstantString("AVS");
264 (void) RegisterMagickInfo(entry);
265 return(MagickImageCoderSignature);
266}
267
268/*
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270% %
271% %
272% %
273% U n r e g i s t e r A V S I m a g e %
274% %
275% %
276% %
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278%
279% UnregisterAVSImage() removes format registrations made by the
280% AVS module from the list of supported formats.
281%
282% The format of the UnregisterAVSImage method is:
283%
284% UnregisterAVSImage(void)
285%
286*/
287ModuleExport void UnregisterAVSImage(void)
288{
289 (void) UnregisterMagickInfo("AVS");
290}
291
292/*
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294% %
295% %
296% %
297% W r i t e A V S I m a g e %
298% %
299% %
300% %
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302%
303% WriteAVSImage() writes an image to a file in AVS X image format.
304%
305% The format of the WriteAVSImage method is:
306%
cristy1e178e72011-08-28 19:44:34 +0000307% MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
308% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000309%
310% A description of each parameter follows.
311%
312% o image_info: the image info.
313%
314% o image: The image.
315%
cristy1e178e72011-08-28 19:44:34 +0000316% o exception: return any errors or warnings in this structure.
317%
cristy3ed852e2009-09-05 21:47:34 +0000318*/
cristy1e178e72011-08-28 19:44:34 +0000319static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
320 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000321{
322 MagickBooleanType
323 status;
324
325 MagickOffsetType
326 scene;
327
cristy4c08aed2011-07-01 19:47:50 +0000328 register const Quantum
cristy3f2302b2010-03-11 03:16:37 +0000329 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000330
cristybb503372010-05-27 20:51:26 +0000331 register ssize_t
cristy3f2302b2010-03-11 03:16:37 +0000332 x;
cristy3ed852e2009-09-05 21:47:34 +0000333
334 register unsigned char
cristy3f2302b2010-03-11 03:16:37 +0000335 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000336
337 ssize_t
cristyeda60832010-09-17 00:00:24 +0000338 count,
339 y;
cristy3ed852e2009-09-05 21:47:34 +0000340
341 unsigned char
342 *pixels;
343
344 /*
345 Open output image file.
346 */
347 assert(image_info != (const ImageInfo *) NULL);
348 assert(image_info->signature == MagickSignature);
349 assert(image != (Image *) NULL);
350 assert(image->signature == MagickSignature);
351 if (image->debug != MagickFalse)
352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000353 assert(exception != (ExceptionInfo *) NULL);
354 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000355 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000356 if (status == MagickFalse)
357 return(status);
358 scene=0;
359 do
360 {
361 /*
362 Write AVS header.
363 */
cristy510d06a2011-07-06 23:43:54 +0000364 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000365 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000366 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
367 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
368 /*
369 Allocate memory for pixels.
370 */
371 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
372 4*sizeof(*pixels));
373 if (pixels == (unsigned char *) NULL)
374 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
375 /*
376 Convert MIFF to AVS raster pixels.
377 */
cristybb503372010-05-27 20:51:26 +0000378 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000379 {
cristy1e178e72011-08-28 19:44:34 +0000380 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000381 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000382 break;
383 q=pixels;
cristybb503372010-05-27 20:51:26 +0000384 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000385 {
cristy4c08aed2011-07-01 19:47:50 +0000386 *q++=ScaleQuantumToChar((Quantum) (image->matte != MagickFalse ?
387 GetPixelAlpha(image,p) : OpaqueAlpha));
388 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
389 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
390 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +0000391 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000392 }
393 count=WriteBlob(image,(size_t) (q-pixels),pixels);
394 if (count != (ssize_t) (q-pixels))
395 break;
cristybdadf312011-04-23 23:16:21 +0000396 if (image->previous == (Image *) NULL)
397 {
398 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
399 image->rows);
400 if (status == MagickFalse)
401 break;
402 }
cristy3ed852e2009-09-05 21:47:34 +0000403 }
404 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
405 if (GetNextImageInList(image) == (Image *) NULL)
406 break;
407 image=SyncNextImageInList(image);
408 status=SetImageProgress(image,SaveImagesTag,scene++,
409 GetImageListLength(image));
410 if (status == MagickFalse)
411 break;
412 } while (image_info->adjoin != MagickFalse);
413 (void) CloseBlob(image);
414 return(MagickTrue);
415}