blob: a9132fd83ad5a02bab8e96232eda50a78a052ac3 [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
cristy3ed852e2009-09-05 21:47:34 +0000101 MagickBooleanType
102 status;
103
cristybb503372010-05-27 20:51:26 +0000104 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000105 x;
106
107 register PixelPacket
108 *q;
109
110 register unsigned char
111 *p;
112
113 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000114 count,
115 y;
cristy3ed852e2009-09-05 21:47:34 +0000116
117 unsigned char
118 *pixels;
119
cristyf2faecf2010-05-28 19:19:36 +0000120 unsigned long
cristy3ed852e2009-09-05 21:47:34 +0000121 columns,
122 rows;
123
124 /*
125 Open image file.
126 */
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)
137 {
138 image=DestroyImageList(image);
139 return((Image *) NULL);
140 }
141 /*
142 Read MTV image.
143 */
144 (void) ReadBlobString(image,buffer);
145 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
146 if (count <= 0)
147 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
148 do
149 {
150 /*
151 Initialize image structure.
152 */
153 image->columns=columns;
154 image->rows=rows;
155 image->depth=8;
156 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
157 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
158 break;
159 /*
160 Convert MTV raster image to pixel packets.
161 */
162 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
163 3UL*sizeof(*pixels));
164 if (pixels == (unsigned char *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000166 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000167 {
168 count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
169 if (count != (ssize_t) (3*image->columns))
170 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
171 p=pixels;
172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
173 if (q == (PixelPacket *) NULL)
174 break;
cristybb503372010-05-27 20:51:26 +0000175 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000176 {
cristy5fde9aa2011-04-23 01:31:53 +0000177 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
178 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
179 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
cristyce70c172010-01-07 17:15:30 +0000180 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +0000181 q++;
182 }
183 if (SyncAuthenticPixels(image,exception) == MagickFalse)
184 break;
185 if (image->previous == (Image *) NULL)
186 {
cristycee97112010-05-28 00:44:52 +0000187 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +0000188 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000189 if (status == MagickFalse)
190 break;
191 }
192 }
193 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
194 if (EOFBlob(image) != MagickFalse)
195 {
196 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
197 image->filename);
198 break;
199 }
200 /*
201 Proceed to next image.
202 */
203 if (image_info->number_scenes != 0)
204 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
205 break;
206 *buffer='\0';
207 (void) ReadBlobString(image,buffer);
208 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
209 if (count > 0)
210 {
211 /*
212 Allocate next image structure.
213 */
214 AcquireNextImage(image_info,image);
215 if (GetNextImageInList(image) == (Image *) NULL)
216 {
217 image=DestroyImageList(image);
218 return((Image *) NULL);
219 }
220 image=SyncNextImageInList(image);
221 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
222 GetBlobSize(image));
223 if (status == MagickFalse)
224 break;
225 }
226 } while (count > 0);
227 (void) CloseBlob(image);
228 return(GetFirstImageInList(image));
229}
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
236% R e g i s t e r M T V I m a g e %
237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
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.
248%
249% The format of the RegisterMTVImage method is:
250%
cristybb503372010-05-27 20:51:26 +0000251% size_t RegisterMTVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000252%
253*/
cristybb503372010-05-27 20:51:26 +0000254ModuleExport size_t RegisterMTVImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000255{
256 MagickInfo
257 *entry;
258
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);
266}
267
268/*
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270% %
271% %
272% %
273% U n r e g i s t e r M T V I m a g e %
274% %
275% %
276% %
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278%
279% UnregisterMTVImage() removes format registrations made by the
280% MTV module from the list of supported formats.
281%
282% The format of the UnregisterMTVImage method is:
283%
284% UnregisterMTVImage(void)
285%
286*/
287ModuleExport void UnregisterMTVImage(void)
288{
289 (void) UnregisterMagickInfo("MTV");
290}
291
292/*
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294% %
295% %
296% %
297% W r i t e M T V I m a g e %
298% %
299% %
300% %
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302%
cristyaff6d802011-04-26 01:46:31 +0000303% WriteMTVImage() writes an image to a file in red, green, and blue MTV
304% rasterfile format.
cristy3ed852e2009-09-05 21:47:34 +0000305%
306% The format of the WriteMTVImage method is:
307%
308% MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
309%
310% A description of each parameter follows.
311%
312% o image_info: the image info.
313%
314% o image: The image.
315%
316*/
317static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
318{
319 char
320 buffer[MaxTextExtent];
321
cristy3ed852e2009-09-05 21:47:34 +0000322 MagickBooleanType
323 status;
324
325 MagickOffsetType
326 scene;
327
328 register const PixelPacket
329 *p;
330
cristybb503372010-05-27 20:51:26 +0000331 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000332 x;
333
334 register unsigned char
335 *q;
336
cristyaff6d802011-04-26 01:46:31 +0000337 ssize_t
338 y;
339
cristy3ed852e2009-09-05 21:47:34 +0000340 unsigned char
341 *pixels;
342
343 /*
344 Open output image file.
345 */
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)
354 return(status);
355 scene=0;
356 do
357 {
358 /*
359 Allocate memory for pixels.
360 */
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");
367 /*
368 Initialize raster file header.
369 */
cristye8c25f92010-06-03 00:53:06 +0000370 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n",(double)
371 image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000372 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +0000373 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000374 {
375 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
376 if (p == (const PixelPacket *) NULL)
377 break;
378 q=pixels;
cristybb503372010-05-27 20:51:26 +0000379 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000380 {
cristyce70c172010-01-07 17:15:30 +0000381 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
382 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
383 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000384 p++;
385 }
386 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
387 if (image->previous == (Image *) NULL)
388 {
cristycee97112010-05-28 00:44:52 +0000389 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
390 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000391 if (status == MagickFalse)
392 break;
393 }
394 }
395 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
396 if (GetNextImageInList(image) == (Image *) NULL)
397 break;
398 image=SyncNextImageInList(image);
399 status=SetImageProgress(image,SaveImagesTag,scene,
400 GetImageListLength(image));
401 if (status == MagickFalse)
402 break;
403 scene++;
404 } while (image_info->adjoin != MagickFalse);
405 (void) CloseBlob(image);
406 return(MagickTrue);
407}