]> granicus.if.org Git - imagemagick/blob - coders/mtv.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[imagemagick] / coders / mtv.c
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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 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 %    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 \f
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 \f
61 /*
62   Forward declarations.
63 */
64 static MagickBooleanType
65   WriteMTVImage(const ImageInfo *,Image *);
66 \f
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 */
93 static Image *ReadMTVImage(const ImageInfo *image_info,ExceptionInfo *exception)
94 {
95   char
96     buffer[MaxTextExtent];
97
98   Image
99     *image;
100
101   MagickBooleanType
102     status;
103
104   register ssize_t
105     x;
106
107   register PixelPacket
108     *q;
109
110   register unsigned char
111     *p;
112
113   ssize_t
114     count,
115     y;
116
117   unsigned char
118     *pixels;
119
120   unsigned long
121     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");
166     for (y=0; y < (ssize_t) image->rows; y++)
167     {
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;
175       for (x=0; x < (ssize_t) image->columns; x++)
176       {
177         SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
178         SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
179         SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
180         SetOpacityPixelComponent(q,OpaqueOpacity);
181         q++;
182       }
183       if (SyncAuthenticPixels(image,exception) == MagickFalse)
184         break;
185       if (image->previous == (Image *) NULL)
186         {
187           status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
188             image->rows);
189           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 \f
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 %
251 %      size_t RegisterMTVImage(void)
252 %
253 */
254 ModuleExport size_t RegisterMTVImage(void)
255 {
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 \f
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 */
287 ModuleExport void UnregisterMTVImage(void)
288 {
289   (void) UnregisterMagickInfo("MTV");
290 }
291 \f
292 /*
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 %                                                                             %
295 %                                                                             %
296 %                                                                             %
297 %   W r i t e M T V I m a g e                                                 %
298 %                                                                             %
299 %                                                                             %
300 %                                                                             %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %
303 %  WriteMTVImage() writes an image to a file in red, green, and blue MTV
304 %  rasterfile format.
305 %
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 */
317 static MagickBooleanType WriteMTVImage(const ImageInfo *image_info,Image *image)
318 {
319   char
320     buffer[MaxTextExtent];
321
322   MagickBooleanType
323     status;
324
325   MagickOffsetType
326     scene;
327
328   register const PixelPacket
329     *p;
330
331   register ssize_t
332     x;
333
334   register unsigned char
335     *q;
336
337   ssize_t
338     y;
339
340   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     */
370     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",(double)
371       image->columns,(double) image->rows);
372     (void) WriteBlobString(image,buffer);
373     for (y=0; y < (ssize_t) image->rows; y++)
374     {
375       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
376       if (p == (const PixelPacket *) NULL)
377         break;
378       q=pixels;
379       for (x=0; x < (ssize_t) image->columns; x++)
380       {
381         *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
382         *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
383         *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
384         p++;
385       }
386       (void) WriteBlob(image,(size_t) (q-pixels),pixels);
387       if (image->previous == (Image *) NULL)
388         {
389           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
390                 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 }