blob: f445aa53a73cdbc0bcb3a28fcf51a3b8727fac18 [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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
101 long
102 y;
103
104 MagickBooleanType
105 status;
106
107 register long
108 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
122 unsigned long
123 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");
168 for (y=0; y < (long) image->rows; y++)
169 {
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;
177 for (x=0; x < (long) image->columns; x++)
178 {
179 q->red=ScaleCharToQuantum(*p++);
180 q->green=ScaleCharToQuantum(*p++);
181 q->blue=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 {
189 status=SetImageProgress(image,LoadImageTag,y,image->rows);
190 if (status == MagickFalse)
191 break;
192 }
193 }
194 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
195 if (EOFBlob(image) != MagickFalse)
196 {
197 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
198 image->filename);
199 break;
200 }
201 /*
202 Proceed to next image.
203 */
204 if (image_info->number_scenes != 0)
205 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
206 break;
207 *buffer='\0';
208 (void) ReadBlobString(image,buffer);
209 count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
210 if (count > 0)
211 {
212 /*
213 Allocate next image structure.
214 */
215 AcquireNextImage(image_info,image);
216 if (GetNextImageInList(image) == (Image *) NULL)
217 {
218 image=DestroyImageList(image);
219 return((Image *) NULL);
220 }
221 image=SyncNextImageInList(image);
222 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
223 GetBlobSize(image));
224 if (status == MagickFalse)
225 break;
226 }
227 } while (count > 0);
228 (void) CloseBlob(image);
229 return(GetFirstImageInList(image));
230}
231
232/*
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234% %
235% %
236% %
237% R e g i s t e r M T V I m a g e %
238% %
239% %
240% %
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242%
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.
249%
250% The format of the RegisterMTVImage method is:
251%
252% unsigned long RegisterMTVImage(void)
253%
254*/
255ModuleExport unsigned long RegisterMTVImage(void)
256{
257 MagickInfo
258 *entry;
259
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);
267}
268
269/*
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271% %
272% %
273% %
274% U n r e g i s t e r M T V I m a g e %
275% %
276% %
277% %
278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279%
280% UnregisterMTVImage() removes format registrations made by the
281% MTV module from the list of supported formats.
282%
283% The format of the UnregisterMTVImage method is:
284%
285% UnregisterMTVImage(void)
286%
287*/
288ModuleExport void UnregisterMTVImage(void)
289{
290 (void) UnregisterMagickInfo("MTV");
291}
292
293/*
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295% %
296% %
297% %
298% W r i t e M T V I m a g e %
299% %
300% %
301% %
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303%
304% WriteMTVImage() writes an image to a file in red, green, and blue
305% MTV rasterfile format.
306%
307% The format of the WriteMTVImage method is:
308%
309% MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
310%
311% A description of each parameter follows.
312%
313% o image_info: the image info.
314%
315% o image: The image.
316%
317*/
318static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
319{
320 char
321 buffer[MaxTextExtent];
322
323 long
324 y;
325
326 MagickBooleanType
327 status;
328
329 MagickOffsetType
330 scene;
331
332 register const PixelPacket
333 *p;
334
335 register long
336 x;
337
338 register unsigned char
339 *q;
340
341 unsigned char
342 *pixels;
343
344 /*
345 Open output image file.
346 */
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)
355 return(status);
356 scene=0;
357 do
358 {
359 /*
360 Allocate memory for pixels.
361 */
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");
368 /*
369 Initialize raster file header.
370 */
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++)
375 {
376 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
377 if (p == (const PixelPacket *) NULL)
378 break;
379 q=pixels;
380 for (x=0; x < (long) image->columns; x++)
381 {
cristyce70c172010-01-07 17:15:30 +0000382 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
383 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
384 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000385 p++;
386 }
387 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
388 if (image->previous == (Image *) NULL)
389 {
390 status=SetImageProgress(image,SaveImageTag,y,image->rows);
391 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}