2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AVS X Image Format %
20 % Copyright 1999-2010 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)
110 register unsigned char
129 assert(image_info != (const ImageInfo *) NULL);
130 assert(image_info->signature == MagickSignature);
131 if (image_info->debug != MagickFalse)
132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
133 image_info->filename);
134 assert(exception != (ExceptionInfo *) NULL);
135 assert(exception->signature == MagickSignature);
136 image=AcquireImage(image_info);
137 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
138 if (status == MagickFalse)
140 image=DestroyImageList(image);
141 return((Image *) NULL);
146 width=ReadBlobMSBLong(image);
147 height=ReadBlobMSBLong(image);
148 if (EOFBlob(image) != MagickFalse)
149 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
150 if ((width == 0UL) || (height == 0UL))
151 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
155 Convert AVS raster image to pixel packets.
157 image->columns=width;
160 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
161 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
163 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
165 if (pixels == (unsigned char *) NULL)
166 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
167 length=(size_t) 4*image->columns;
168 for (y=0; y < (long) image->rows; y++)
170 count=ReadBlob(image,length,pixels);
171 if ((size_t) count != length)
172 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
174 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
175 if (q == (PixelPacket *) NULL)
177 for (x=0; x < (long) image->columns; x++)
179 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
180 q->red=ScaleCharToQuantum(*p++);
181 q->green=ScaleCharToQuantum(*p++);
182 q->blue=ScaleCharToQuantum(*p++);
183 if (q->opacity != OpaqueOpacity)
184 image->matte=MagickTrue;
187 if (SyncAuthenticPixels(image,exception) == MagickFalse)
189 if ((image->previous == (Image *) NULL) &&
190 (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse))
193 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
194 if (EOFBlob(image) != MagickFalse)
196 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
201 Proceed to next image.
203 if (image_info->number_scenes != 0)
204 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
206 width=ReadBlobMSBLong(image);
207 height=ReadBlobMSBLong(image);
208 if ((width != 0UL) && (height != 0UL))
211 Allocate next image structure.
213 AcquireNextImage(image_info,image);
214 if (GetNextImageInList(image) == (Image *) NULL)
216 image=DestroyImageList(image);
217 return((Image *) NULL);
219 image=SyncNextImageInList(image);
220 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
222 if (status == MagickFalse)
225 } while ((width != 0UL) && (height != 0UL));
226 (void) CloseBlob(image);
227 return(GetFirstImageInList(image));
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % R e g i s t e r A V S I m a g e %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % RegisterAVSImage() adds attributes for the AVS X image format to the list
242 % of supported formats. The attributes include the image format tag, a
243 % method to read and/or write the format, whether the format supports the
244 % saving of more than one frame to the same file or blob, whether the format
245 % supports native in-memory I/O, and a brief description of the format.
247 % The format of the RegisterAVSImage method is:
249 % unsigned long RegisterAVSImage(void)
252 ModuleExport unsigned long RegisterAVSImage(void)
257 entry=SetMagickInfo("AVS");
258 entry->decoder=(DecodeImageHandler *) ReadAVSImage;
259 entry->encoder=(EncodeImageHandler *) WriteAVSImage;
260 entry->description=ConstantString("AVS X image");
261 entry->module=ConstantString("AVS");
262 (void) RegisterMagickInfo(entry);
263 return(MagickImageCoderSignature);
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % U n r e g i s t e r A V S I m a g e %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % UnregisterAVSImage() removes format registrations made by the
278 % AVS module from the list of supported formats.
280 % The format of the UnregisterAVSImage method is:
282 % UnregisterAVSImage(void)
285 ModuleExport void UnregisterAVSImage(void)
287 (void) UnregisterMagickInfo("AVS");
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % W r i t e A V S I m a g e %
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % WriteAVSImage() writes an image to a file in AVS X image format.
303 % The format of the WriteAVSImage method is:
305 % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
307 % A description of each parameter follows.
309 % o image_info: the image info.
311 % o image: The image.
314 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image)
322 register const PixelPacket
329 register unsigned char
339 Open output image file.
341 assert(image_info != (const ImageInfo *) NULL);
342 assert(image_info->signature == MagickSignature);
343 assert(image != (Image *) NULL);
344 assert(image->signature == MagickSignature);
345 if (image->debug != MagickFalse)
346 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
347 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
348 if (status == MagickFalse)
356 if (image->colorspace != RGBColorspace)
357 (void) TransformImageColorspace(image,RGBColorspace);
358 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
359 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
361 Allocate memory for pixels.
363 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
365 if (pixels == (unsigned char *) NULL)
366 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
368 Convert MIFF to AVS raster pixels.
370 for (y=0; y < (long) image->rows; y++)
372 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
373 if (p == (PixelPacket *) NULL)
376 for (x=0; x < (long) image->columns; x++)
378 *q++=ScaleQuantumToChar((Quantum) (QuantumRange-
379 (image->matte != MagickFalse ? p->opacity : OpaqueOpacity)));
380 *q++=ScaleQuantumToChar(GetRedSample(p));
381 *q++=ScaleQuantumToChar(GetGreenSample(p));
382 *q++=ScaleQuantumToChar(GetBlueSample(p));
385 count=WriteBlob(image,(size_t) (q-pixels),pixels);
386 if (count != (ssize_t) (q-pixels))
388 if ((image->previous == (Image *) NULL) &&
389 (SetImageProgress(image,SaveImageTag,y,image->rows) == MagickFalse))
392 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
393 if (GetNextImageInList(image) == (Image *) NULL)
395 image=SyncNextImageInList(image);
396 status=SetImageProgress(image,SaveImagesTag,scene++,
397 GetImageListLength(image));
398 if (status == MagickFalse)
400 } while (image_info->adjoin != MagickFalse);
401 (void) CloseBlob(image);