2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AVS X Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #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/colorspace-private.h"
48 #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"
66 static MagickBooleanType
67 WriteAVSImage(const ImageInfo *,Image *,ExceptionInfo *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d A V S I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
84 % The format of the ReadAVSImage method is:
86 % Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
88 % A description of each parameter follows:
90 % o image_info: the image info.
92 % o exception: return any errors or warnings in this structure.
95 static Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
109 register unsigned char
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,exception);
135 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136 if (status == MagickFalse)
138 image=DestroyImageList(image);
139 return((Image *) NULL);
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");
153 Convert AVS raster image to pixel packets.
155 image->columns=width;
158 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
159 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
161 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
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++)
168 count=ReadBlob(image,length,pixels);
169 if ((size_t) count != length)
170 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
173 if (q == (Quantum *) NULL)
175 for (x=0; x < (ssize_t) image->columns; x++)
177 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)
182 image->matte=MagickTrue;
183 q+=GetPixelChannels(image);
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
187 if (image->previous == (Image *) NULL)
189 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
191 if (status == MagickFalse)
195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
196 if (EOFBlob(image) != MagickFalse)
198 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
203 Proceed to next image.
205 if (image_info->number_scenes != 0)
206 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
208 width=ReadBlobMSBLong(image);
209 height=ReadBlobMSBLong(image);
210 if ((width != 0UL) && (height != 0UL))
213 Allocate next image structure.
215 AcquireNextImage(image_info,image,exception);
216 if (GetNextImageInList(image) == (Image *) NULL)
218 image=DestroyImageList(image);
219 return((Image *) NULL);
221 image=SyncNextImageInList(image);
222 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
224 if (status == MagickFalse)
227 } while ((width != 0UL) && (height != 0UL));
228 (void) CloseBlob(image);
229 return(GetFirstImageInList(image));
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % R e g i s t e r A V S I m a g e %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
249 % The format of the RegisterAVSImage method is:
251 % size_t RegisterAVSImage(void)
254 ModuleExport size_t RegisterAVSImage(void)
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);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % U n r e g i s t e r A V S I m a g e %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % UnregisterAVSImage() removes format registrations made by the
280 % AVS module from the list of supported formats.
282 % The format of the UnregisterAVSImage method is:
284 % UnregisterAVSImage(void)
287 ModuleExport void UnregisterAVSImage(void)
289 (void) UnregisterMagickInfo("AVS");
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % W r i t e A V S I m a g e %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % WriteAVSImage() writes an image to a file in AVS X image format.
305 % The format of the WriteAVSImage method is:
307 % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
308 % Image *image,ExceptionInfo *exception)
310 % A description of each parameter follows.
312 % o image_info: the image info.
314 % o image: The image.
316 % o exception: return any errors or warnings in this structure.
319 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
320 ExceptionInfo *exception)
328 register const Quantum
334 register unsigned char
345 Open output image file.
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);
353 assert(exception != (ExceptionInfo *) NULL);
354 assert(exception->signature == MagickSignature);
355 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
356 if (status == MagickFalse)
364 if (IsRGBColorspace(image->colorspace) == MagickFalse)
365 (void) TransformImageColorspace(image,RGBColorspace);
366 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
367 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
369 Allocate memory for pixels.
371 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
373 if (pixels == (unsigned char *) NULL)
374 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
376 Convert MIFF to AVS raster pixels.
378 for (y=0; y < (ssize_t) image->rows; y++)
380 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
381 if (p == (const Quantum *) NULL)
384 for (x=0; x < (ssize_t) image->columns; x++)
386 *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));
391 p+=GetPixelChannels(image);
393 count=WriteBlob(image,(size_t) (q-pixels),pixels);
394 if (count != (ssize_t) (q-pixels))
396 if (image->previous == (Image *) NULL)
398 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
400 if (status == MagickFalse)
404 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
405 if (GetNextImageInList(image) == (Image *) NULL)
407 image=SyncNextImageInList(image);
408 status=SetImageProgress(image,SaveImagesTag,scene++,
409 GetImageListLength(image));
410 if (status == MagickFalse)
412 } while (image_info->adjoin != MagickFalse);
413 (void) CloseBlob(image);