]> granicus.if.org Git - imagemagick/blob - coders/tim.c
The PSD reader tries to create a merged image if the merged image is corrupted.
[imagemagick] / coders / tim.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            TTTTT  IIIII  M   M                              %
7 %                              T      I    MM MM                              %
8 %                              T      I    M M M                              %
9 %                              T      I    M   M                              %
10 %                              T    IIIII  M   M                              %
11 %                                                                             %
12 %                                                                             %
13 %                           Read PSX TIM Image Format                         %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 \f
62 /*
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %  R e a d T I M I m a g e                                                    %
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %
73 %  ReadTIMImage() reads a PSX TIM image file and returns it.  It
74 %  allocates the memory necessary for the new Image structure and returns a
75 %  pointer to the new image.
76 %
77 %  Contributed by os@scee.sony.co.uk.
78 %
79 %  The format of the ReadTIMImage method is:
80 %
81 %      Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
82 %
83 %  A description of each parameter follows:
84 %
85 %    o image_info: the image info.
86 %
87 %    o exception: return any errors or warnings in this structure.
88 %
89 */
90 static Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 {
92   typedef struct _TIMInfo
93   {
94     size_t
95       id,
96       flag;
97   } TIMInfo;
98
99   TIMInfo
100     tim_info;
101
102   Image
103     *image;
104
105   int
106     bits_per_pixel,
107     has_clut;
108
109   MagickBooleanType
110     status;
111
112   register ssize_t
113     x;
114
115   register Quantum
116     *q;
117
118   register ssize_t
119     i;
120
121   register unsigned char
122     *p;
123
124   size_t
125     bytes_per_line,
126     height,
127     image_size,
128     pixel_mode,
129     width;
130
131   ssize_t
132     count,
133     y;
134
135   unsigned char
136     *tim_data,
137     *tim_pixels;
138
139   unsigned short
140     word;
141
142   /*
143     Open image file.
144   */
145   assert(image_info != (const ImageInfo *) NULL);
146   assert(image_info->signature == MagickSignature);
147   if (image_info->debug != MagickFalse)
148     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
149       image_info->filename);
150   assert(exception != (ExceptionInfo *) NULL);
151   assert(exception->signature == MagickSignature);
152   image=AcquireImage(image_info,exception);
153   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
154   if (status == MagickFalse)
155     {
156       image=DestroyImageList(image);
157       return((Image *) NULL);
158     }
159   /*
160     Determine if this a TIM file.
161   */
162   tim_info.id=ReadBlobLSBLong(image);
163   do
164   {
165     /*
166       Verify TIM identifier.
167     */
168     if (tim_info.id != 0x00000010)
169       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
170     tim_info.flag=ReadBlobLSBLong(image);
171     has_clut=tim_info.flag & (1 << 3) ? 1 : 0;
172     pixel_mode=tim_info.flag & 0x07;
173     switch ((int) pixel_mode)
174     {
175       case 0: bits_per_pixel=4; break;
176       case 1: bits_per_pixel=8; break;
177       case 2: bits_per_pixel=16; break;
178       case 3: bits_per_pixel=24; break;
179       default: bits_per_pixel=4; break;
180     }
181     image->depth=8;
182     if (has_clut)
183       {
184         unsigned char
185           *tim_colormap;
186
187         /*
188           Read TIM raster colormap.
189         */
190         (void)ReadBlobLSBLong(image);
191         (void)ReadBlobLSBShort(image);
192         (void)ReadBlobLSBShort(image);
193         width=ReadBlobLSBShort(image);
194         height=ReadBlobLSBShort(image);
195         image->columns=width;
196         image->rows=height;
197         if (AcquireImageColormap(image,pixel_mode == 1 ? 256UL : 16UL,exception) == MagickFalse)
198           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
199         tim_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
200           2UL*sizeof(*tim_colormap));
201         if (tim_colormap == (unsigned char *) NULL)
202           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
203         count=ReadBlob(image,2*image->colors,tim_colormap);
204         if (count != (ssize_t) (2*image->colors))
205           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
206         p=tim_colormap;
207         for (i=0; i < (ssize_t) image->colors; i++)
208         {
209           word=(*p++);
210           word|=(unsigned short) (*p++ << 8);
211           image->colormap[i].blue=ScaleCharToQuantum(
212             ScaleColor5to8(1UL*(word >> 10) & 0x1f));
213           image->colormap[i].green=ScaleCharToQuantum(
214             ScaleColor5to8(1UL*(word >> 5) & 0x1f));
215           image->colormap[i].red=ScaleCharToQuantum(
216             ScaleColor5to8(1UL*word & 0x1f));
217         }
218         tim_colormap=(unsigned char *) RelinquishMagickMemory(tim_colormap);
219       }
220     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
221       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
222         break;
223     /*
224       Read image data.
225     */
226     (void) ReadBlobLSBLong(image);
227     (void) ReadBlobLSBShort(image);
228     (void) ReadBlobLSBShort(image);
229     width=ReadBlobLSBShort(image);
230     height=ReadBlobLSBShort(image);
231     image_size=2*width*height;
232     bytes_per_line=width*2;
233     width=(width*16)/bits_per_pixel;
234     tim_data=(unsigned char *) AcquireQuantumMemory(image_size,
235       sizeof(*tim_data));
236     if (tim_data == (unsigned char *) NULL)
237       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
238     count=ReadBlob(image,image_size,tim_data);
239     if (count != (ssize_t) (image_size))
240       ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
241     tim_pixels=tim_data;
242     /*
243       Initialize image structure.
244     */
245     image->columns=width;
246     image->rows=height;
247     /*
248       Convert TIM raster image to pixel packets.
249     */
250     switch (bits_per_pixel)
251     {
252       case 4:
253       {
254         /*
255           Convert PseudoColor scanline.
256         */
257         for (y=(ssize_t) image->rows-1; y >= 0; y--)
258         {
259           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
260           if (q == (Quantum *) NULL)
261             break;
262           p=tim_pixels+y*bytes_per_line;
263           for (x=0; x < ((ssize_t) image->columns-1); x+=2)
264           {
265             SetPixelIndex(image,(*p) & 0x0f,q);
266             q+=GetPixelChannels(image);
267             SetPixelIndex(image,(*p >> 4) & 0x0f,q);
268             p++;
269             q+=GetPixelChannels(image);
270           }
271           if ((image->columns % 2) != 0)
272             {
273               SetPixelIndex(image,(*p >> 4) & 0x0f,q);
274               p++;
275               q+=GetPixelChannels(image);
276             }
277           if (SyncAuthenticPixels(image,exception) == MagickFalse)
278             break;
279           if (image->previous == (Image *) NULL)
280             {
281               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
282                 image->rows);
283               if (status == MagickFalse)
284                 break;
285             }
286         }
287         break;
288       }
289       case 8:
290       {
291         /*
292           Convert PseudoColor scanline.
293         */
294         for (y=(ssize_t) image->rows-1; y >= 0; y--)
295         {
296           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
297           if (q == (Quantum *) NULL)
298             break;
299           p=tim_pixels+y*bytes_per_line;
300           for (x=0; x < (ssize_t) image->columns; x++)
301           {
302             SetPixelIndex(image,*p++,q);
303             q+=GetPixelChannels(image);
304           }
305           if (SyncAuthenticPixels(image,exception) == MagickFalse)
306             break;
307           if (image->previous == (Image *) NULL)
308             {
309               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
310                 image->rows);
311               if (status == MagickFalse)
312                 break;
313             }
314         }
315         break;
316       }
317       case 16:
318       {
319         /*
320           Convert DirectColor scanline.
321         */
322         for (y=(ssize_t) image->rows-1; y >= 0; y--)
323         {
324           p=tim_pixels+y*bytes_per_line;
325           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
326           if (q == (Quantum *) NULL)
327             break;
328           for (x=0; x < (ssize_t) image->columns; x++)
329           {
330             word=(*p++);
331             word|=(*p++ << 8);
332             SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
333               (1UL*word >> 10) & 0x1f)),q);
334             SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
335               (1UL*word >> 5) & 0x1f)),q);
336             SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
337               (1UL*word >> 0) & 0x1f)),q);
338             q+=GetPixelChannels(image);
339           }
340           if (SyncAuthenticPixels(image,exception) == MagickFalse)
341             break;
342           if (image->previous == (Image *) NULL)
343             {
344               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
345                 image->rows);
346               if (status == MagickFalse)
347                 break;
348             }
349         }
350         break;
351       }
352       case 24:
353       {
354         /*
355           Convert DirectColor scanline.
356         */
357         for (y=(ssize_t) image->rows-1; y >= 0; y--)
358         {
359           p=tim_pixels+y*bytes_per_line;
360           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
361           if (q == (Quantum *) NULL)
362             break;
363           for (x=0; x < (ssize_t) image->columns; x++)
364           {
365             SetPixelRed(image,ScaleCharToQuantum(*p++),q);
366             SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
367             SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
368             q+=GetPixelChannels(image);
369           }
370           if (SyncAuthenticPixels(image,exception) == MagickFalse)
371             break;
372           if (image->previous == (Image *) NULL)
373             {
374               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
375                 image->rows);
376               if (status == MagickFalse)
377                 break;
378             }
379         }
380         break;
381       }
382       default:
383         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
384     }
385     if (image->storage_class == PseudoClass)
386       (void) SyncImage(image,exception);
387     tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
388     if (EOFBlob(image) != MagickFalse)
389       {
390         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
391           image->filename);
392         break;
393       }
394     /*
395       Proceed to next image.
396     */
397     tim_info.id=ReadBlobLSBLong(image);
398     if (tim_info.id == 0x00000010)
399       {
400         /*
401           Allocate next image structure.
402         */
403         AcquireNextImage(image_info,image,exception);
404         if (GetNextImageInList(image) == (Image *) NULL)
405           {
406             image=DestroyImageList(image);
407             return((Image *) NULL);
408           }
409         image=SyncNextImageInList(image);
410         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
411           GetBlobSize(image));
412         if (status == MagickFalse)
413           break;
414       }
415   } while (tim_info.id == 0x00000010);
416   (void) CloseBlob(image);
417   return(GetFirstImageInList(image));
418 }
419 \f
420 /*
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 %                                                                             %
423 %                                                                             %
424 %                                                                             %
425 %   R e g i s t e r T I M I m a g e                                           %
426 %                                                                             %
427 %                                                                             %
428 %                                                                             %
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 %
431 %  RegisterTIMImage() adds attributes for the TIM image format to
432 %  the list of supported formats.  The attributes include the image format
433 %  tag, a method to read and/or write the format, whether the format
434 %  supports the saving of more than one frame to the same file or blob,
435 %  whether the format supports native in-memory I/O, and a brief
436 %  description of the format.
437 %
438 %  The format of the RegisterTIMImage method is:
439 %
440 %      size_t RegisterTIMImage(void)
441 %
442 */
443 ModuleExport size_t RegisterTIMImage(void)
444 {
445   MagickInfo
446     *entry;
447
448   entry=SetMagickInfo("TIM");
449   entry->decoder=(DecodeImageHandler *) ReadTIMImage;
450   entry->description=ConstantString("PSX TIM");
451   entry->module=ConstantString("TIM");
452   (void) RegisterMagickInfo(entry);
453   return(MagickImageCoderSignature);
454 }
455 \f
456 /*
457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 %                                                                             %
459 %                                                                             %
460 %                                                                             %
461 %   U n r e g i s t e r T I M I m a g e                                       %
462 %                                                                             %
463 %                                                                             %
464 %                                                                             %
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 %
467 %  UnregisterTIMImage() removes format registrations made by the
468 %  TIM module from the list of supported formats.
469 %
470 %  The format of the UnregisterTIMImage method is:
471 %
472 %      UnregisterTIMImage(void)
473 %
474 */
475 ModuleExport void UnregisterTIMImage(void)
476 {
477   (void) UnregisterMagickInfo("TIM");
478 }