2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write MTV Raytracer 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 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];
113 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 < (long) 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 == (PixelPacket *) NULL)
177 for (x=0; x < (long) image->columns; x++)
179 q->red=ScaleCharToQuantum(*p++);
180 q->green=ScaleCharToQuantum(*p++);
181 q->blue=ScaleCharToQuantum(*p++);
182 SetOpacityPixelComponent(q,OpaqueOpacity);
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
187 if (image->previous == (Image *) NULL)
189 status=SetImageProgress(image,LoadImageTag,y,image->rows);
190 if (status == MagickFalse)
194 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
195 if (EOFBlob(image) != MagickFalse)
197 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
202 Proceed to next image.
204 if (image_info->number_scenes != 0)
205 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
208 (void) ReadBlobString(image,buffer);
209 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
213 Allocate next image structure.
215 AcquireNextImage(image_info,image);
216 if (GetNextImageInList(image) == (Image *) NULL)
218 image=DestroyImageList(image);
219 return((Image *) NULL);
221 image=SyncNextImageInList(image);
222 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
224 if (status == MagickFalse)
228 (void) CloseBlob(image);
229 return(GetFirstImageInList(image));
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % R e g i s t e r M T V I m a g e %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % RegisterMTVImage() adds attributes for the MTV image format to
244 % the list of supported formats. The attributes include the image format
245 % tag, a method to read and/or write the format, whether the format
246 % supports the saving of more than one frame to the same file or blob,
247 % whether the format supports native in-memory I/O, and a brief
248 % description of the format.
250 % The format of the RegisterMTVImage method is:
252 % unsigned long RegisterMTVImage(void)
255 ModuleExport unsigned long RegisterMTVImage(void)
260 entry=SetMagickInfo("MTV");
261 entry->decoder=(DecodeImageHandler *) ReadMTVImage;
262 entry->encoder=(EncodeImageHandler *) WriteMTVImage;
263 entry->description=ConstantString("MTV Raytracing image format");
264 entry->module=ConstantString("MTV");
265 (void) RegisterMagickInfo(entry);
266 return(MagickImageCoderSignature);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % U n r e g i s t e r M T V I m a g e %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % UnregisterMTVImage() removes format registrations made by the
281 % MTV module from the list of supported formats.
283 % The format of the UnregisterMTVImage method is:
285 % UnregisterMTVImage(void)
288 ModuleExport void UnregisterMTVImage(void)
290 (void) UnregisterMagickInfo("MTV");
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % W r i t e M T V I m a g e %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % WriteMTVImage() writes an image to a file in red, green, and blue
305 % MTV rasterfile format.
307 % The format of the WriteMTVImage method is:
309 % MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
311 % A description of each parameter follows.
313 % o image_info: the image info.
315 % o image: The image.
318 static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
321 buffer[MaxTextExtent];
332 register const PixelPacket
338 register unsigned char
345 Open output image file.
347 assert(image_info != (const ImageInfo *) NULL);
348 assert(image_info->signature == MagickSignature);
349 assert(image != (Image *) NULL);
350 assert(image->signature == MagickSignature);
351 if (image->debug != MagickFalse)
352 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
353 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
354 if (status == MagickFalse)
360 Allocate memory for pixels.
362 if (image->colorspace != RGBColorspace)
363 (void) TransformImageColorspace(image,RGBColorspace);
364 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
365 3UL*sizeof(*pixels));
366 if (pixels == (unsigned char *) NULL)
367 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
369 Initialize raster file header.
371 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n",
372 image->columns,image->rows);
373 (void) WriteBlobString(image,buffer);
374 for (y=0; y < (long) image->rows; y++)
376 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
377 if (p == (const PixelPacket *) NULL)
380 for (x=0; x < (long) image->columns; x++)
382 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
383 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
384 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
387 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
388 if (image->previous == (Image *) NULL)
390 status=SetImageProgress(image,SaveImageTag,y,image->rows);
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);