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 "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 WriteMTVImage(const ImageInfo *,Image *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e a d M T V I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % ReadMTVImage() reads a MTV image file and returns it. It allocates
81 % the memory necessary for the new Image structure and returns a pointer to
84 % The format of the ReadMTVImage method is:
86 % Image *ReadMTVImage(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 *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
98 buffer[MaxTextExtent];
112 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 (void) ReadBlobString(image,buffer);
147 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
149 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
153 Initialize image structure.
155 image->columns=columns;
158 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
159 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
162 Convert MTV raster image to pixel packets.
164 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
165 3UL*sizeof(*pixels));
166 if (pixels == (unsigned char *) NULL)
167 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
168 for (y=0; y < (ssize_t) image->rows; y++)
170 count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
171 if (count != (ssize_t) (3*image->columns))
172 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
174 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
175 if (q == (const Quantum *) NULL)
177 for (x=0; x < (ssize_t) image->columns; x++)
179 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
180 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
181 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
182 SetPixelAlpha(image,OpaqueAlpha,q);
183 q+=GetPixelChannels(image);
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
187 if (image->previous == (Image *) NULL)
189 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
191 if (status == MagickFalse)
195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
196 if (EOFBlob(image) != MagickFalse)
198 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
203 Proceed to next image.
205 if (image_info->number_scenes != 0)
206 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
209 (void) ReadBlobString(image,buffer);
210 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
214 Allocate next image structure.
216 AcquireNextImage(image_info,image);
217 if (GetNextImageInList(image) == (Image *) NULL)
219 image=DestroyImageList(image);
220 return((Image *) NULL);
222 image=SyncNextImageInList(image);
223 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
225 if (status == MagickFalse)
229 (void) CloseBlob(image);
230 return(GetFirstImageInList(image));
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % R e g i s t e r M T V I m a g e %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % RegisterMTVImage() adds attributes for the MTV image format to
245 % the list of supported formats. The attributes include the image format
246 % tag, a method to read and/or write the format, whether the format
247 % supports the saving of more than one frame to the same file or blob,
248 % whether the format supports native in-memory I/O, and a brief
249 % description of the format.
251 % The format of the RegisterMTVImage method is:
253 % size_t RegisterMTVImage(void)
256 ModuleExport size_t RegisterMTVImage(void)
261 entry=SetMagickInfo("MTV");
262 entry->decoder=(DecodeImageHandler *) ReadMTVImage;
263 entry->encoder=(EncodeImageHandler *) WriteMTVImage;
264 entry->description=ConstantString("MTV Raytracing image format");
265 entry->module=ConstantString("MTV");
266 (void) RegisterMagickInfo(entry);
267 return(MagickImageCoderSignature);
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % U n r e g i s t e r M T V I m a g e %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % UnregisterMTVImage() removes format registrations made by the
282 % MTV module from the list of supported formats.
284 % The format of the UnregisterMTVImage method is:
286 % UnregisterMTVImage(void)
289 ModuleExport void UnregisterMTVImage(void)
291 (void) UnregisterMagickInfo("MTV");
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % W r i t e M T V I m a g e %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % WriteMTVImage() writes an image to a file in red, green, and blue MTV
308 % The format of the WriteMTVImage method is:
310 % MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
312 % A description of each parameter follows.
314 % o image_info: the image info.
316 % o image: The image.
319 static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
322 buffer[MaxTextExtent];
330 register const Quantum
336 register unsigned char
346 Open output image file.
348 assert(image_info != (const ImageInfo *) NULL);
349 assert(image_info->signature == MagickSignature);
350 assert(image != (Image *) NULL);
351 assert(image->signature == MagickSignature);
352 if (image->debug != MagickFalse)
353 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
354 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
355 if (status == MagickFalse)
361 Allocate memory for pixels.
363 if (IsRGBColorspace(image->colorspace) == MagickFalse)
364 (void) TransformImageColorspace(image,RGBColorspace);
365 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
366 3UL*sizeof(*pixels));
367 if (pixels == (unsigned char *) NULL)
368 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
370 Initialize raster file header.
372 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",(double)
373 image->columns,(double) image->rows);
374 (void) WriteBlobString(image,buffer);
375 for (y=0; y < (ssize_t) image->rows; y++)
377 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
378 if (p == (const Quantum *) NULL)
381 for (x=0; x < (ssize_t) image->columns; x++)
383 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
384 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
385 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
386 p+=GetPixelChannels(image);
388 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
389 if (image->previous == (Image *) NULL)
391 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
393 if (status == MagickFalse)
397 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
398 if (GetNextImageInList(image) == (Image *) NULL)
400 image=SyncNextImageInList(image);
401 status=SetImageProgress(image,SaveImagesTag,scene,
402 GetImageListLength(image));
403 if (status == MagickFalse)
406 } while (image_info->adjoin != MagickFalse);
407 (void) CloseBlob(image);