2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write MTV Raytracer 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 WriteMTVImage(const ImageInfo *,Image *);
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % R e a d M T V I m a g e %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % ReadMTVImage() reads a MTV image file and returns it. It allocates
79 % the memory necessary for the new Image structure and returns a pointer to
82 % The format of the ReadMTVImage method is:
84 % Image *ReadMTVImage(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 *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 buffer[MaxTextExtent];
110 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);
135 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
136 if (status == MagickFalse)
138 image=DestroyImageList(image);
139 return((Image *) NULL);
144 (void) ReadBlobString(image,buffer);
145 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
147 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151 Initialize image structure.
153 image->columns=columns;
156 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
160 Convert MTV raster image to pixel packets.
162 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
163 3UL*sizeof(*pixels));
164 if (pixels == (unsigned char *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 for (y=0; y < (ssize_t) image->rows; y++)
168 count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
169 if (count != (ssize_t) (3*image->columns))
170 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
173 if (q == (PixelPacket *) NULL)
175 for (x=0; x < (ssize_t) image->columns; x++)
177 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
178 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
179 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
180 SetOpacityPixelComponent(q,OpaqueOpacity);
183 if (SyncAuthenticPixels(image,exception) == MagickFalse)
185 if (image->previous == (Image *) NULL)
187 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
189 if (status == 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))
207 (void) ReadBlobString(image,buffer);
208 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
212 Allocate next image structure.
214 AcquireNextImage(image_info,image);
215 if (GetNextImageInList(image) == (Image *) NULL)
217 image=DestroyImageList(image);
218 return((Image *) NULL);
220 image=SyncNextImageInList(image);
221 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
223 if (status == MagickFalse)
227 (void) CloseBlob(image);
228 return(GetFirstImageInList(image));
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % R e g i s t e r M T V I m a g e %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % RegisterMTVImage() adds attributes for the MTV image format to
243 % the list of supported formats. The attributes include the image format
244 % tag, a method to read and/or write the format, whether the format
245 % supports the saving of more than one frame to the same file or blob,
246 % whether the format supports native in-memory I/O, and a brief
247 % description of the format.
249 % The format of the RegisterMTVImage method is:
251 % size_t RegisterMTVImage(void)
254 ModuleExport size_t RegisterMTVImage(void)
259 entry=SetMagickInfo("MTV");
260 entry->decoder=(DecodeImageHandler *) ReadMTVImage;
261 entry->encoder=(EncodeImageHandler *) WriteMTVImage;
262 entry->description=ConstantString("MTV Raytracing image format");
263 entry->module=ConstantString("MTV");
264 (void) RegisterMagickInfo(entry);
265 return(MagickImageCoderSignature);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % U n r e g i s t e r M T V I m a g e %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % UnregisterMTVImage() removes format registrations made by the
280 % MTV module from the list of supported formats.
282 % The format of the UnregisterMTVImage method is:
284 % UnregisterMTVImage(void)
287 ModuleExport void UnregisterMTVImage(void)
289 (void) UnregisterMagickInfo("MTV");
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % W r i t e M T V I m a g e %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % WriteMTVImage() writes an image to a file in red, green, and blue MTV
306 % The format of the WriteMTVImage method is:
308 % MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
310 % A description of each parameter follows.
312 % o image_info: the image info.
314 % o image: The image.
317 static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
320 buffer[MaxTextExtent];
328 register const PixelPacket
334 register unsigned char
344 Open output image file.
346 assert(image_info != (const ImageInfo *) NULL);
347 assert(image_info->signature == MagickSignature);
348 assert(image != (Image *) NULL);
349 assert(image->signature == MagickSignature);
350 if (image->debug != MagickFalse)
351 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
352 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
353 if (status == MagickFalse)
359 Allocate memory for pixels.
361 if (image->colorspace != RGBColorspace)
362 (void) TransformImageColorspace(image,RGBColorspace);
363 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
364 3UL*sizeof(*pixels));
365 if (pixels == (unsigned char *) NULL)
366 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
368 Initialize raster file header.
370 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",(double)
371 image->columns,(double) image->rows);
372 (void) WriteBlobString(image,buffer);
373 for (y=0; y < (ssize_t) image->rows; y++)
375 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
376 if (p == (const PixelPacket *) NULL)
379 for (x=0; x < (ssize_t) image->columns; x++)
381 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
382 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
383 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
386 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
387 if (image->previous == (Image *) NULL)
389 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
391 if (status == MagickFalse)
395 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
396 if (GetNextImageInList(image) == (Image *) NULL)
398 image=SyncNextImageInList(image);
399 status=SetImageProgress(image,SaveImagesTag,scene,
400 GetImageListLength(image));
401 if (status == MagickFalse)
404 } while (image_info->adjoin != MagickFalse);
405 (void) CloseBlob(image);