2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read PSX TIM Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.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"
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 % R e a d T I M I m a g e %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % ReadTIMImage() reads a PSX TIM image file and returns it. It
73 % allocates the memory necessary for the new Image structure and returns a
74 % pointer to the new image.
76 % Contributed by os@scee.sony.co.uk.
78 % The format of the ReadTIMImage method is:
80 % Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
82 % A description of each parameter follows:
84 % o image_info: the image info.
86 % o exception: return any errors or warnings in this structure.
89 static Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
91 typedef struct _TIMInfo
126 register unsigned char
149 assert(image_info != (const ImageInfo *) NULL);
150 assert(image_info->signature == MagickSignature);
151 if (image_info->debug != MagickFalse)
152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
153 image_info->filename);
154 assert(exception != (ExceptionInfo *) NULL);
155 assert(exception->signature == MagickSignature);
156 image=AcquireImage(image_info);
157 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
158 if (status == MagickFalse)
160 image=DestroyImageList(image);
161 return((Image *) NULL);
164 Determine if this a TIM file.
166 tim_info.id=ReadBlobLSBLong(image);
170 Verify TIM identifier.
172 if (tim_info.id != 0x00000010)
173 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
174 tim_info.flag=ReadBlobLSBLong(image);
175 has_clut=tim_info.flag & (1 << 3) ? 1 : 0;
176 pixel_mode=tim_info.flag & 0x07;
177 switch ((int) pixel_mode)
179 case 0: bits_per_pixel=4; break;
180 case 1: bits_per_pixel=8; break;
181 case 2: bits_per_pixel=16; break;
182 case 3: bits_per_pixel=24; break;
183 default: bits_per_pixel=4; break;
191 Read TIM raster colormap.
193 (void)ReadBlobLSBLong(image);
194 (void)ReadBlobLSBShort(image);
195 (void)ReadBlobLSBShort(image);
196 width=ReadBlobLSBShort(image);
197 height=ReadBlobLSBShort(image);
198 image->columns=width;
200 if (AcquireImageColormap(image,pixel_mode == 1 ? 256UL : 16UL) == MagickFalse)
201 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
202 tim_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
203 2UL*sizeof(*tim_colormap));
204 if (tim_colormap == (unsigned char *) NULL)
205 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
206 count=ReadBlob(image,2*image->colors,tim_colormap);
207 if (count != (ssize_t) (2*image->colors))
208 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
210 for (i=0; i < (ssize_t) image->colors; i++)
213 word|=(unsigned short) (*p++ << 8);
214 image->colormap[i].blue=ScaleCharToQuantum(
215 ScaleColor5to8(1UL*(word >> 10) & 0x1f));
216 image->colormap[i].green=ScaleCharToQuantum(
217 ScaleColor5to8(1UL*(word >> 5) & 0x1f));
218 image->colormap[i].red=ScaleCharToQuantum(
219 ScaleColor5to8(1UL*word & 0x1f));
221 tim_colormap=(unsigned char *) RelinquishMagickMemory(tim_colormap);
223 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
224 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
229 (void) ReadBlobLSBLong(image);
230 (void) ReadBlobLSBShort(image);
231 (void) ReadBlobLSBShort(image);
232 width=ReadBlobLSBShort(image);
233 height=ReadBlobLSBShort(image);
234 image_size=2*width*height;
235 bytes_per_line=width*2;
236 width=(width*16)/bits_per_pixel;
237 tim_data=(unsigned char *) AcquireQuantumMemory(image_size,
239 if (tim_data == (unsigned char *) NULL)
240 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
241 count=ReadBlob(image,image_size,tim_data);
242 if (count != (ssize_t) (image_size))
243 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
246 Initialize image structure.
248 image->columns=width;
251 Convert TIM raster image to pixel packets.
253 switch (bits_per_pixel)
258 Convert PseudoColor scanline.
260 for (y=(ssize_t) image->rows-1; y >= 0; y--)
262 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
263 if (q == (PixelPacket *) NULL)
265 indexes=GetAuthenticIndexQueue(image);
266 p=tim_pixels+y*bytes_per_line;
267 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
269 indexes[x]=(IndexPacket) ((*p) & 0x0f);
270 indexes[x+1]=(IndexPacket) ((*p >> 4) & 0x0f);
273 if ((image->columns % 2) != 0)
275 indexes[x]=(IndexPacket) ((*p >> 4) & 0x0f);
278 if (SyncAuthenticPixels(image,exception) == MagickFalse)
280 if (image->previous == (Image *) NULL)
282 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
284 if (status == MagickFalse)
293 Convert PseudoColor scanline.
295 for (y=(ssize_t) image->rows-1; y >= 0; y--)
297 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
298 if (q == (PixelPacket *) NULL)
300 indexes=GetAuthenticIndexQueue(image);
301 p=tim_pixels+y*bytes_per_line;
302 for (x=0; x < (ssize_t) image->columns; x++)
304 if (SyncAuthenticPixels(image,exception) == MagickFalse)
306 if (image->previous == (Image *) NULL)
308 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
310 if (status == MagickFalse)
319 Convert DirectColor scanline.
321 for (y=(ssize_t) image->rows-1; y >= 0; y--)
323 p=tim_pixels+y*bytes_per_line;
324 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
325 if (q == (PixelPacket *) NULL)
327 for (x=0; x < (ssize_t) image->columns; x++)
331 q->blue=ScaleCharToQuantum(ScaleColor5to8((1UL*word >> 10) & 0x1f));
332 q->green=ScaleCharToQuantum(ScaleColor5to8((1UL*word >> 5) & 0x1f));
333 q->red=ScaleCharToQuantum(ScaleColor5to8(1UL*word & 0x1f));
336 if (SyncAuthenticPixels(image,exception) == MagickFalse)
338 if (image->previous == (Image *) NULL)
340 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
342 if (status == MagickFalse)
351 Convert DirectColor scanline.
353 for (y=(ssize_t) image->rows-1; y >= 0; y--)
355 p=tim_pixels+y*bytes_per_line;
356 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
357 if (q == (PixelPacket *) NULL)
359 for (x=0; x < (ssize_t) image->columns; x++)
361 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
362 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
363 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
366 if (SyncAuthenticPixels(image,exception) == MagickFalse)
368 if (image->previous == (Image *) NULL)
370 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
372 if (status == MagickFalse)
379 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
381 if (image->storage_class == PseudoClass)
382 (void) SyncImage(image);
383 tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
384 if (EOFBlob(image) != MagickFalse)
386 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
391 Proceed to next image.
393 tim_info.id=ReadBlobLSBLong(image);
394 if (tim_info.id == 0x00000010)
397 Allocate next image structure.
399 AcquireNextImage(image_info,image);
400 if (GetNextImageInList(image) == (Image *) NULL)
402 image=DestroyImageList(image);
403 return((Image *) NULL);
405 image=SyncNextImageInList(image);
406 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
408 if (status == MagickFalse)
411 } while (tim_info.id == 0x00000010);
412 (void) CloseBlob(image);
413 return(GetFirstImageInList(image));
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % R e g i s t e r T I M I m a g e %
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % RegisterTIMImage() adds attributes for the TIM image format to
428 % the list of supported formats. The attributes include the image format
429 % tag, a method to read and/or write the format, whether the format
430 % supports the saving of more than one frame to the same file or blob,
431 % whether the format supports native in-memory I/O, and a brief
432 % description of the format.
434 % The format of the RegisterTIMImage method is:
436 % size_t RegisterTIMImage(void)
439 ModuleExport size_t RegisterTIMImage(void)
444 entry=SetMagickInfo("TIM");
445 entry->decoder=(DecodeImageHandler *) ReadTIMImage;
446 entry->description=ConstantString("PSX TIM");
447 entry->module=ConstantString("TIM");
448 (void) RegisterMagickInfo(entry);
449 return(MagickImageCoderSignature);
453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % U n r e g i s t e r T I M I m a g e %
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % UnregisterTIMImage() removes format registrations made by the
464 % TIM module from the list of supported formats.
466 % The format of the UnregisterTIMImage method is:
468 % UnregisterTIMImage(void)
471 ModuleExport void UnregisterTIMImage(void)
473 (void) UnregisterMagickInfo("TIM");