blob: 306173c7a9df6b5b45f66f09ef8009afc85c6706 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M TTTTT V V %
7% MM MM T V V %
8% M M M T V V %
9% M M T V V %
10% M M T V %
11% %
12% %
13% Read/Write MTV Raytracer Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
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"
60
61/*
62 Forward declarations.
63*/
64static MagickBooleanType
65 WriteMTVImage(const ImageInfo *,Image *);
66
67/*
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69% %
70% %
71% %
72% R e a d M T V I m a g e %
73% %
74% %
75% %
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77%
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
80% the new image.
81%
82% The format of the ReadMTVImage method is:
83%
84% Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
85%
86% A description of each parameter follows:
87%
88% o image_info: the image info.
89%
90% o exception: return any errors or warnings in this structure.
91%
92*/
93static Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
94{
95 char
96 buffer[MaxTextExtent];
97
98 Image
99 *image;
100
cristybb503372010-05-27 20:51:26 +0000101 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000102 y;
103
104 MagickBooleanType
105 status;
106
cristybb503372010-05-27 20:51:26 +0000107 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000108 x;
109
110 register PixelPacket
111 *q;
112
113 register unsigned char
114 *p;
115
116 ssize_t
117 count;
118
119 unsigned char
120 *pixels;
121
cristyf2faecf2010-05-28 19:19:36 +0000122 unsigned long
cristy3ed852e2009-09-05 21:47:34 +0000123 columns,
124 rows;
125
126 /*
127 Open image file.
128 */
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)
139 {
140 image=DestroyImageList(image);
141 return((Image *) NULL);
142 }
143 /*
144 Read MTV image.
145 */
146 (void) ReadBlobString(image,buffer);
147 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
148 if (count <= 0)
149 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
150 do
151 {
152 /*
153 Initialize image structure.
154 */
155 image->columns=columns;
156 image->rows=rows;
157 image->depth=8;
158 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
159 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
160 break;
161 /*
162 Convert MTV raster image to pixel packets.
163 */
164 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
165 3UL*sizeof(*pixels));
166 if (pixels == (unsigned char *) NULL)
167 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000168 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000169 {
170 count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
171 if (count != (ssize_t) (3*image->columns))
172 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
173 p=pixels;
174 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
175 if (q == (PixelPacket *) NULL)
176 break;
cristybb503372010-05-27 20:51:26 +0000177 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000178 {
cristy5fde9aa2011-04-23 01:31:53 +0000179 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
180 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
181 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
cristyce70c172010-01-07 17:15:30 +0000182 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +0000183 q++;
184 }
185 if (SyncAuthenticPixels(image,exception) == MagickFalse)
186 break;
187 if (image->previous == (Image *) NULL)
188 {
cristycee97112010-05-28 00:44:52 +0000189 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
190 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000191 if (status == MagickFalse)
192 break;
193 }
194 }
195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
196 if (EOFBlob(image) != MagickFalse)
197 {
198 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
199 image->filename);
200 break;
201 }
202 /*
203 Proceed to next image.
204 */
205 if (image_info->number_scenes != 0)
206 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
207 break;
208 *buffer='\0';
209 (void) ReadBlobString(image,buffer);
210 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
211 if (count > 0)
212 {
213 /*
214 Allocate next image structure.
215 */
216 AcquireNextImage(image_info,image);
217 if (GetNextImageInList(image) == (Image *) NULL)
218 {
219 image=DestroyImageList(image);
220 return((Image *) NULL);
221 }
222 image=SyncNextImageInList(image);
223 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
224 GetBlobSize(image));
225 if (status == MagickFalse)
226 break;
227 }
228 } while (count > 0);
229 (void) CloseBlob(image);
230 return(GetFirstImageInList(image));
231}
232
233/*
234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235% %
236% %
237% %
238% R e g i s t e r M T V I m a g e %
239% %
240% %
241% %
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243%
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.
250%
251% The format of the RegisterMTVImage method is:
252%
cristybb503372010-05-27 20:51:26 +0000253% size_t RegisterMTVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000254%
255*/
cristybb503372010-05-27 20:51:26 +0000256ModuleExport size_t RegisterMTVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000257{
258 MagickInfo
259 *entry;
260
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);
268}
269
270/*
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272% %
273% %
274% %
275% U n r e g i s t e r M T V I m a g e %
276% %
277% %
278% %
279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280%
281% UnregisterMTVImage() removes format registrations made by the
282% MTV module from the list of supported formats.
283%
284% The format of the UnregisterMTVImage method is:
285%
286% UnregisterMTVImage(void)
287%
288*/
289ModuleExport void UnregisterMTVImage(void)
290{
291 (void) UnregisterMagickInfo("MTV");
292}
293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296% %
297% %
298% %
299% W r i t e M T V I m a g e %
300% %
301% %
302% %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305% WriteMTVImage() writes an image to a file in red, green, and blue
306% MTV rasterfile format.
307%
308% The format of the WriteMTVImage method is:
309%
310% MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
311%
312% A description of each parameter follows.
313%
314% o image_info: the image info.
315%
316% o image: The image.
317%
318*/
319static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
320{
321 char
322 buffer[MaxTextExtent];
323
cristybb503372010-05-27 20:51:26 +0000324 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000325 y;
326
327 MagickBooleanType
328 status;
329
330 MagickOffsetType
331 scene;
332
333 register const PixelPacket
334 *p;
335
cristybb503372010-05-27 20:51:26 +0000336 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000337 x;
338
339 register unsigned char
340 *q;
341
342 unsigned char
343 *pixels;
344
345 /*
346 Open output image file.
347 */
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)
356 return(status);
357 scene=0;
358 do
359 {
360 /*
361 Allocate memory for pixels.
362 */
363 if (image->colorspace != RGBColorspace)
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");
369 /*
370 Initialize raster file header.
371 */
cristye8c25f92010-06-03 00:53:06 +0000372 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n",(double)
373 image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000374 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +0000375 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000376 {
377 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
378 if (p == (const PixelPacket *) NULL)
379 break;
380 q=pixels;
cristybb503372010-05-27 20:51:26 +0000381 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000382 {
cristyce70c172010-01-07 17:15:30 +0000383 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
384 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
385 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000386 p++;
387 }
388 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
389 if (image->previous == (Image *) NULL)
390 {
cristycee97112010-05-28 00:44:52 +0000391 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
392 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000393 if (status == MagickFalse)
394 break;
395 }
396 }
397 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
398 if (GetNextImageInList(image) == (Image *) NULL)
399 break;
400 image=SyncNextImageInList(image);
401 status=SetImageProgress(image,SaveImagesTag,scene,
402 GetImageListLength(image));
403 if (status == MagickFalse)
404 break;
405 scene++;
406 } while (image_info->adjoin != MagickFalse);
407 (void) CloseBlob(image);
408 return(MagickTrue);
409}