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 "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"
64 static MagickBooleanType
65 WriteAVSImage(const ImageInfo *,Image *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d A V S I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadAVSImage() reads an AVS X 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.
82 % The format of the ReadAVSImage method is:
84 % Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
86 % A description of each parameter follows:
88 % o image_info: the image info.
90 % o exception: return any errors or warnings in this structure.
93 static Image *ReadAVSImage(const ImageInfo *image_info,ExceptionInfo *exception)
107 register unsigned char
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)
136 image=DestroyImageList(image);
137 return((Image *) NULL);
142 width=ReadBlobMSBLong(image);
143 height=ReadBlobMSBLong(image);
144 if (EOFBlob(image) != MagickFalse)
145 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
146 if ((width == 0UL) || (height == 0UL))
147 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151 Convert AVS raster image to pixel packets.
153 image->columns=width;
156 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
159 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
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++)
166 count=ReadBlob(image,length,pixels);
167 if ((size_t) count != length)
168 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
170 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
171 if (q == (PixelPacket *) NULL)
173 for (x=0; x < (ssize_t) image->columns; x++)
175 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
176 q->red=ScaleCharToQuantum(*p++);
177 q->green=ScaleCharToQuantum(*p++);
178 q->blue=ScaleCharToQuantum(*p++);
179 if (q->opacity != OpaqueOpacity)
180 image->matte=MagickTrue;
183 if (SyncAuthenticPixels(image,exception) == MagickFalse)
185 if ((image->previous == (Image *) NULL) &&
186 (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse))
189 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
190 if (EOFBlob(image) != MagickFalse)
192 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
197 Proceed to next image.
199 if (image_info->number_scenes != 0)
200 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
202 width=ReadBlobMSBLong(image);
203 height=ReadBlobMSBLong(image);
204 if ((width != 0UL) && (height != 0UL))
207 Allocate next image structure.
209 AcquireNextImage(image_info,image);
210 if (GetNextImageInList(image) == (Image *) NULL)
212 image=DestroyImageList(image);
213 return((Image *) NULL);
215 image=SyncNextImageInList(image);
216 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
218 if (status == MagickFalse)
221 } while ((width != 0UL) && (height != 0UL));
222 (void) CloseBlob(image);
223 return(GetFirstImageInList(image));
227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 % R e g i s t e r A V S I m a g e %
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % RegisterAVSImage() adds attributes for the AVS X image format to the list
238 % of supported formats. The attributes include the image format tag, a
239 % method to read and/or write the format, whether the format supports the
240 % saving of more than one frame to the same file or blob, whether the format
241 % supports native in-memory I/O, and a brief description of the format.
243 % The format of the RegisterAVSImage method is:
245 % size_t RegisterAVSImage(void)
248 ModuleExport size_t RegisterAVSImage(void)
253 entry=SetMagickInfo("AVS");
254 entry->decoder=(DecodeImageHandler *) ReadAVSImage;
255 entry->encoder=(EncodeImageHandler *) WriteAVSImage;
256 entry->description=ConstantString("AVS X image");
257 entry->module=ConstantString("AVS");
258 (void) RegisterMagickInfo(entry);
259 return(MagickImageCoderSignature);
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % U n r e g i s t e r A V S I m a g e %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % UnregisterAVSImage() removes format registrations made by the
274 % AVS module from the list of supported formats.
276 % The format of the UnregisterAVSImage method is:
278 % UnregisterAVSImage(void)
281 ModuleExport void UnregisterAVSImage(void)
283 (void) UnregisterMagickInfo("AVS");
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % W r i t e A V S I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % WriteAVSImage() writes an image to a file in AVS X image format.
299 % The format of the WriteAVSImage method is:
301 % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
303 % A description of each parameter follows.
305 % o image_info: the image info.
307 % o image: The image.
310 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
318 register const PixelPacket
324 register unsigned char
335 Open output image file.
337 assert(image_info != (const ImageInfo *) NULL);
338 assert(image_info->signature == MagickSignature);
339 assert(image != (Image *) NULL);
340 assert(image->signature == MagickSignature);
341 if (image->debug != MagickFalse)
342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
343 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
344 if (status == MagickFalse)
352 if (image->colorspace != RGBColorspace)
353 (void) TransformImageColorspace(image,RGBColorspace);
354 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
355 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
357 Allocate memory for pixels.
359 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
361 if (pixels == (unsigned char *) NULL)
362 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
364 Convert MIFF to AVS raster pixels.
366 for (y=0; y < (ssize_t) image->rows; y++)
368 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
369 if (p == (PixelPacket *) NULL)
372 for (x=0; x < (ssize_t) image->columns; x++)
374 *q++=ScaleQuantumToChar((Quantum) (QuantumRange-
375 (image->matte != MagickFalse ? p->opacity : OpaqueOpacity)));
376 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
377 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
378 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
381 count=WriteBlob(image,(size_t) (q-pixels),pixels);
382 if (count != (ssize_t) (q-pixels))
384 if ((image->previous == (Image *) NULL) &&
385 (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse))
388 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
389 if (GetNextImageInList(image) == (Image *) NULL)
391 image=SyncNextImageInList(image);
392 status=SetImageProgress(image,SaveImagesTag,scene++,
393 GetImageListLength(image));
394 if (status == MagickFalse)
396 } while (image_info->adjoin != MagickFalse);
397 (void) CloseBlob(image);