2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write AVS X Image Format %
20 % Copyright 1999-2018 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 % https://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 image->alpha_trait=BlendPixelTrait;
162 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
163 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
165 status=SetImageExtent(image,image->columns,image->rows,exception);
166 if (status == MagickFalse)
167 return(DestroyImageList(image));
168 pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
169 if (pixel_info == (MemoryInfo *) NULL)
170 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
171 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
172 length=(size_t) 4*image->columns;
173 for (y=0; y < (ssize_t) image->rows; y++)
175 count=ReadBlob(image,length,pixels);
176 if ((size_t) count != length)
178 pixel_info=RelinquishVirtualMemory(pixel_info);
179 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
182 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
183 if (q == (Quantum *) NULL)
185 for (x=0; x < (ssize_t) image->columns; x++)
187 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
188 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
189 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
190 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
191 q+=GetPixelChannels(image);
193 if (SyncAuthenticPixels(image,exception) == MagickFalse)
195 if (image->previous == (Image *) NULL)
197 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
199 if (status == MagickFalse)
203 pixel_info=RelinquishVirtualMemory(pixel_info);
204 if (EOFBlob(image) != MagickFalse)
206 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
211 Proceed to next image.
213 if (image_info->number_scenes != 0)
214 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
216 width=ReadBlobMSBLong(image);
217 height=ReadBlobMSBLong(image);
218 if ((width != 0UL) && (height != 0UL))
221 Allocate next image structure.
223 AcquireNextImage(image_info,image,exception);
224 if (GetNextImageInList(image) == (Image *) NULL)
226 image=DestroyImageList(image);
227 return((Image *) NULL);
229 image=SyncNextImageInList(image);
230 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
232 if (status == MagickFalse)
235 } while ((width != 0UL) && (height != 0UL));
236 (void) CloseBlob(image);
237 return(GetFirstImageInList(image));
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % R e g i s t e r A V S I m a g e %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % RegisterAVSImage() adds attributes for the AVS X image format to the list
252 % of supported formats. The attributes include the image format tag, a
253 % method to read and/or write the format, whether the format supports the
254 % saving of more than one frame to the same file or blob, whether the format
255 % supports native in-memory I/O, and a brief description of the format.
257 % The format of the RegisterAVSImage method is:
259 % size_t RegisterAVSImage(void)
262 ModuleExport size_t RegisterAVSImage(void)
267 entry=AcquireMagickInfo("AVS","AVS","AVS X image");
268 entry->decoder=(DecodeImageHandler *) ReadAVSImage;
269 entry->encoder=(EncodeImageHandler *) WriteAVSImage;
270 (void) RegisterMagickInfo(entry);
271 return(MagickImageCoderSignature);
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % U n r e g i s t e r A V S I m a g e %
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % UnregisterAVSImage() removes format registrations made by the
286 % AVS module from the list of supported formats.
288 % The format of the UnregisterAVSImage method is:
290 % UnregisterAVSImage(void)
293 ModuleExport void UnregisterAVSImage(void)
295 (void) UnregisterMagickInfo("AVS");
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % W r i t e A V S I m a g e %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % WriteAVSImage() writes an image to a file in AVS X image format.
311 % The format of the WriteAVSImage method is:
313 % MagickBooleanType WriteAVSImage(const ImageInfo *image_info,
314 % Image *image,ExceptionInfo *exception)
316 % A description of each parameter follows.
318 % o image_info: the image info.
320 % o image: The image.
322 % o exception: return any errors or warnings in this structure.
325 static MagickBooleanType WriteAVSImage(const ImageInfo *image_info,Image *image,
326 ExceptionInfo *exception)
337 register const Quantum
343 register unsigned char
354 Open output image file.
356 assert(image_info != (const ImageInfo *) NULL);
357 assert(image_info->signature == MagickCoreSignature);
358 assert(image != (Image *) NULL);
359 assert(image->signature == MagickCoreSignature);
360 if (image->debug != MagickFalse)
361 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
362 assert(exception != (ExceptionInfo *) NULL);
363 assert(exception->signature == MagickCoreSignature);
364 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
365 if (status == MagickFalse)
373 (void) TransformImageColorspace(image,sRGBColorspace,exception);
374 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
375 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
377 Allocate memory for pixels.
379 pixel_info=AcquireVirtualMemory(image->columns,4*sizeof(*pixels));
380 if (pixel_info == (MemoryInfo *) NULL)
381 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
382 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
384 Convert MIFF to AVS raster pixels.
386 for (y=0; y < (ssize_t) image->rows; y++)
388 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
389 if (p == (const Quantum *) NULL)
392 for (x=0; x < (ssize_t) image->columns; x++)
394 *q++=ScaleQuantumToChar((Quantum) (image->alpha_trait ==
395 BlendPixelTrait ? GetPixelAlpha(image,p) : OpaqueAlpha));
396 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
397 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
398 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
399 p+=GetPixelChannels(image);
401 count=WriteBlob(image,(size_t) (q-pixels),pixels);
402 if (count != (ssize_t) (q-pixels))
404 if (image->previous == (Image *) NULL)
406 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
408 if (status == MagickFalse)
412 pixel_info=RelinquishVirtualMemory(pixel_info);
413 if (GetNextImageInList(image) == (Image *) NULL)
415 image=SyncNextImageInList(image);
416 status=SetImageProgress(image,SaveImagesTag,scene++,
417 GetImageListLength(image));
418 if (status == MagickFalse)
420 } while (image_info->adjoin != MagickFalse);
421 (void) CloseBlob(image);