2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AVS X Image Format %
20 % Copyright 1999-2017 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)
112 register unsigned char
130 assert(image_info != (const ImageInfo *) NULL);
131 assert(image_info->signature == MagickCoreSignature);
132 if (image_info->debug != MagickFalse)
133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
134 image_info->filename);
135 assert(exception != (ExceptionInfo *) NULL);
136 assert(exception->signature == MagickCoreSignature);
137 image=AcquireImage(image_info,exception);
138 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
139 if (status == MagickFalse)
141 image=DestroyImageList(image);
142 return((Image *) NULL);
147 width=ReadBlobMSBLong(image);
148 height=ReadBlobMSBLong(image);
149 if (EOFBlob(image) != MagickFalse)
150 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151 if ((width == 0UL) || (height == 0UL))
152 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
156 Convert AVS raster image to pixel packets.
158 image->columns=width;
161 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
162 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
164 status=SetImageExtent(image,image->columns,image->rows,exception);
165 if (status == MagickFalse)
166 return(DestroyImageList(image));
167 pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
168 if (pixel_info == (MemoryInfo *) NULL)
169 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
170 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
171 length=(size_t) 4*image->columns;
172 for (y=0; y < (ssize_t) image->rows; y++)
174 count=ReadBlob(image,length,pixels);
175 if ((size_t) count != length)
176 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
178 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
179 if (q == (Quantum *) NULL)
181 for (x=0; x < (ssize_t) image->columns; x++)
183 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
184 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
185 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
186 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
187 if (GetPixelAlpha(image,q) != OpaqueAlpha)
188 image->alpha_trait=BlendPixelTrait;
189 q+=GetPixelChannels(image);
191 if (SyncAuthenticPixels(image,exception) == MagickFalse)
193 if (image->previous == (Image *) NULL)
195 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
197 if (status == MagickFalse)
201 pixel_info=RelinquishVirtualMemory(pixel_info);
202 if (EOFBlob(image) != MagickFalse)
204 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
209 Proceed to next image.
211 if (image_info->number_scenes != 0)
212 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
214 width=ReadBlobMSBLong(image);
215 height=ReadBlobMSBLong(image);
216 if ((width != 0UL) && (height != 0UL))
219 Allocate next image structure.
221 AcquireNextImage(image_info,image,exception);
222 if (GetNextImageInList(image) == (Image *) NULL)
224 image=DestroyImageList(image);
225 return((Image *) NULL);
227 image=SyncNextImageInList(image);
228 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
230 if (status == MagickFalse)
233 } while ((width != 0UL) && (height != 0UL));
234 (void) CloseBlob(image);
235 return(GetFirstImageInList(image));
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % R e g i s t e r A V S I m a g e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % RegisterAVSImage() adds attributes for the AVS X image format to the list
250 % of supported formats. The attributes include the image format tag, a
251 % method to read and/or write the format, whether the format supports the
252 % saving of more than one frame to the same file or blob, whether the format
253 % supports native in-memory I/O, and a brief description of the format.
255 % The format of the RegisterAVSImage method is:
257 % size_t RegisterAVSImage(void)
260 ModuleExport size_t RegisterAVSImage(void)
265 entry=AcquireMagickInfo("AVS","AVS","AVS X image");
266 entry->decoder=(DecodeImageHandler *) ReadAVSImage;
267 entry->encoder=(EncodeImageHandler *) WriteAVSImage;
268 (void) RegisterMagickInfo(entry);
269 return(MagickImageCoderSignature);
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % U n r e g i s t e r A V S I m a g e %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % UnregisterAVSImage() removes format registrations made by the
284 % AVS module from the list of supported formats.
286 % The format of the UnregisterAVSImage method is:
288 % UnregisterAVSImage(void)
291 ModuleExport void UnregisterAVSImage(void)
293 (void) UnregisterMagickInfo("AVS");
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % W r i t e A V S I m a g e %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % WriteAVSImage() writes an image to a file in AVS X image format.
309 % The format of the WriteAVSImage method is:
311 % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
312 % Image *image,ExceptionInfo *exception)
314 % A description of each parameter follows.
316 % o image_info: the image info.
318 % o image: The image.
320 % o exception: return any errors or warnings in this structure.
323 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
324 ExceptionInfo *exception)
335 register const Quantum
341 register unsigned char
352 Open output image file.
354 assert(image_info != (const ImageInfo *) NULL);
355 assert(image_info->signature == MagickCoreSignature);
356 assert(image != (Image *) NULL);
357 assert(image->signature == MagickCoreSignature);
358 if (image->debug != MagickFalse)
359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
360 assert(exception != (ExceptionInfo *) NULL);
361 assert(exception->signature == MagickCoreSignature);
362 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
363 if (status == MagickFalse)
371 (void) TransformImageColorspace(image,sRGBColorspace,exception);
372 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
373 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
375 Allocate memory for pixels.
377 pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
378 if (pixel_info == (MemoryInfo *) NULL)
379 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
380 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
382 Convert MIFF to AVS raster pixels.
384 for (y=0; y < (ssize_t) image->rows; y++)
386 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
387 if (p == (const Quantum *) NULL)
390 for (x=0; x < (ssize_t) image->columns; x++)
392 *q++=ScaleQuantumToChar((Quantum) (image->alpha_trait ==
393 BlendPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
394 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
395 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
396 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
397 p+=GetPixelChannels(image);
399 count=WriteBlob(image,(size_t) (q-pixels),pixels);
400 if (count != (ssize_t) (q-pixels))
402 if (image->previous == (Image *) NULL)
404 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
406 if (status == MagickFalse)
410 pixel_info=RelinquishVirtualMemory(pixel_info);
411 if (GetNextImageInList(image) == (Image *) NULL)
413 image=SyncNextImageInList(image);
414 status=SetImageProgress(image,SaveImagesTag,scene++,
415 GetImageListLength(image));
416 if (status == MagickFalse)
418 } while (image_info->adjoin != MagickFalse);
419 (void) CloseBlob(image);