1 /*
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 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 %    https://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 "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"
62 
63 /*
64   Forward declarations.
65 */
66 static MagickBooleanType
67   WriteMTVImage(const ImageInfo *,Image *,ExceptionInfo *);
68 
69 /*
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %                                                                             %
72 %                                                                             %
73 %                                                                             %
74 %   R e a d M T V I m a g e                                                   %
75 %                                                                             %
76 %                                                                             %
77 %                                                                             %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %
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
82 %  the new image.
83 %
84 %  The format of the ReadMTVImage method is:
85 %
86 %      Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o image_info: the image info.
91 %
92 %    o exception: return any errors or warnings in this structure.
93 %
94 */
ReadMTVImage(const ImageInfo * image_info,ExceptionInfo * exception)95 static Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 {
97   char
98     buffer[MagickPathExtent];
99 
100   Image
101     *image;
102 
103   MagickBooleanType
104     status;
105 
106   ssize_t
107     x;
108 
109   Quantum
110     *q;
111 
112   unsigned char
113     *p;
114 
115   ssize_t
116     count,
117     y;
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 == MagickCoreSignature);
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 == MagickCoreSignature);
136   image=AcquireImage(image_info,exception);
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) memset(buffer,0,sizeof(buffer));
147   (void) ReadBlobString(image,buffer);
148   count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
149   if (count != 2)
150     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
151   do
152   {
153     /*
154       Initialize image structure.
155     */
156     image->columns=columns;
157     image->rows=rows;
158     image->depth=8;
159     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
160       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
161         break;
162     status=SetImageExtent(image,image->columns,image->rows,exception);
163     if (status == MagickFalse)
164       return(DestroyImageList(image));
165     /*
166       Convert MTV raster image to pixel packets.
167     */
168     pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
169       3UL*sizeof(*pixels));
170     if (pixels == (unsigned char *) NULL)
171       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
172     for (y=0; y < (ssize_t) image->rows; y++)
173     {
174       count=(ssize_t) ReadBlob(image,(size_t) (3*image->columns),pixels);
175       if (count != (ssize_t) (3*image->columns))
176         {
177           pixels=(unsigned char *) RelinquishMagickMemory(pixels);
178           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
179         }
180       p=pixels;
181       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
182       if (q == (Quantum *) NULL)
183         break;
184       for (x=0; x < (ssize_t) image->columns; x++)
185       {
186         SetPixelRed(image,ScaleCharToQuantum(*p++),q);
187         SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
188         SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
189         SetPixelAlpha(image,OpaqueAlpha,q);
190         q+=GetPixelChannels(image);
191       }
192       if (SyncAuthenticPixels(image,exception) == MagickFalse)
193         break;
194       if (image->previous == (Image *) NULL)
195         {
196           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
197             image->rows);
198           if (status == MagickFalse)
199             break;
200         }
201     }
202     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
203     if (EOFBlob(image) != MagickFalse)
204       {
205         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
206           image->filename);
207         break;
208       }
209     /*
210       Proceed to next image.
211     */
212     if (image_info->number_scenes != 0)
213       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
214         break;
215     *buffer='\0';
216     if (ReadBlobString(image,buffer) == (char *) NULL)
217       break;
218     count=(ssize_t) sscanf(buffer,"%lu %lu\n",&columns,&rows);
219     if (count > 0)
220       {
221         /*
222           Allocate next image structure.
223         */
224         AcquireNextImage(image_info,image,exception);
225         if (GetNextImageInList(image) == (Image *) NULL)
226           {
227             status=MagickFalse;
228             break;
229           }
230         image=SyncNextImageInList(image);
231         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
232           GetBlobSize(image));
233         if (status == MagickFalse)
234           break;
235       }
236   } while (count > 0);
237   (void) CloseBlob(image);
238   if (status == MagickFalse)
239     return(DestroyImageList(image));
240   return(GetFirstImageInList(image));
241 }
242 
243 /*
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 %                                                                             %
246 %                                                                             %
247 %                                                                             %
248 %   R e g i s t e r M T V I m a g e                                           %
249 %                                                                             %
250 %                                                                             %
251 %                                                                             %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 %
254 %  RegisterMTVImage() adds attributes for the MTV image format to
255 %  the list of supported formats.  The attributes include the image format
256 %  tag, a method to read and/or write the format, whether the format
257 %  supports the saving of more than one frame to the same file or blob,
258 %  whether the format supports native in-memory I/O, and a brief
259 %  description of the format.
260 %
261 %  The format of the RegisterMTVImage method is:
262 %
263 %      size_t RegisterMTVImage(void)
264 %
265 */
RegisterMTVImage(void)266 ModuleExport size_t RegisterMTVImage(void)
267 {
268   MagickInfo
269     *entry;
270 
271   entry=AcquireMagickInfo("MTV","MTV","MTV Raytracing image format");
272   entry->decoder=(DecodeImageHandler *) ReadMTVImage;
273   entry->encoder=(EncodeImageHandler *) WriteMTVImage;
274   (void) RegisterMagickInfo(entry);
275   return(MagickImageCoderSignature);
276 }
277 
278 /*
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %                                                                             %
281 %                                                                             %
282 %                                                                             %
283 %   U n r e g i s t e r M T V I m a g e                                       %
284 %                                                                             %
285 %                                                                             %
286 %                                                                             %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 %
289 %  UnregisterMTVImage() removes format registrations made by the
290 %  MTV module from the list of supported formats.
291 %
292 %  The format of the UnregisterMTVImage method is:
293 %
294 %      UnregisterMTVImage(void)
295 %
296 */
UnregisterMTVImage(void)297 ModuleExport void UnregisterMTVImage(void)
298 {
299   (void) UnregisterMagickInfo("MTV");
300 }
301 
302 /*
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 %                                                                             %
305 %                                                                             %
306 %                                                                             %
307 %   W r i t e M T V I m a g e                                                 %
308 %                                                                             %
309 %                                                                             %
310 %                                                                             %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 %
313 %  WriteMTVImage() writes an image to a file in red, green, and blue MTV
314 %  rasterfile format.
315 %
316 %  The format of the WriteMTVImage method is:
317 %
318 %      MagickBooleanType WriteMTVImage(const ImageInfo *image_info,
319 %        Image *image,ExceptionInfo *exception)
320 %
321 %  A description of each parameter follows.
322 %
323 %    o image_info: the image info.
324 %
325 %    o image:  The image.
326 %
327 %    o exception: return any errors or warnings in this structure.
328 %
329 */
WriteMTVImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)330 static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image,
331   ExceptionInfo *exception)
332 {
333   char
334     buffer[MagickPathExtent];
335 
336   MagickBooleanType
337     status;
338 
339   MagickOffsetType
340     scene;
341 
342   const Quantum
343     *p;
344 
345   ssize_t
346     x;
347 
348   unsigned char
349     *q;
350 
351   size_t
352     imageListLength;
353 
354   ssize_t
355     y;
356 
357   unsigned char
358     *pixels;
359 
360   /*
361     Open output image file.
362   */
363   assert(image_info != (const ImageInfo *) NULL);
364   assert(image_info->signature == MagickCoreSignature);
365   assert(image != (Image *) NULL);
366   assert(image->signature == MagickCoreSignature);
367   if (image->debug != MagickFalse)
368     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
369   assert(exception != (ExceptionInfo *) NULL);
370   assert(exception->signature == MagickCoreSignature);
371   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
372   if (status == MagickFalse)
373     return(status);
374   scene=0;
375   imageListLength=GetImageListLength(image);
376   do
377   {
378     /*
379       Allocate memory for pixels.
380     */
381     (void) TransformImageColorspace(image,sRGBColorspace,exception);
382     pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
383       3UL*sizeof(*pixels));
384     if (pixels == (unsigned char *) NULL)
385       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
386     /*
387       Initialize raster file header.
388     */
389     (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",(double)
390       image->columns,(double) image->rows);
391     (void) WriteBlobString(image,buffer);
392     for (y=0; y < (ssize_t) image->rows; y++)
393     {
394       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
395       if (p == (const Quantum *) NULL)
396         break;
397       q=pixels;
398       for (x=0; x < (ssize_t) image->columns; x++)
399       {
400         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
401         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
402         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
403         p+=GetPixelChannels(image);
404       }
405       (void) WriteBlob(image,(size_t) (q-pixels),pixels);
406       if (image->previous == (Image *) NULL)
407         {
408           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
409             image->rows);
410           if (status == MagickFalse)
411             break;
412         }
413     }
414     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
415     if (GetNextImageInList(image) == (Image *) NULL)
416       break;
417     image=SyncNextImageInList(image);
418     status=SetImageProgress(image,SaveImagesTag,scene,imageListLength);
419     if (status == MagickFalse)
420       break;
421     scene++;
422   } while (image_info->adjoin != MagickFalse);
423   (void) CloseBlob(image);
424   return(MagickTrue);
425 }
426