2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read PSX TIM Image Format %
20 % Copyright 1999-2009 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/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/image.h"
49 #include "magick/image-private.h"
50 #include "magick/list.h"
51 #include "magick/magick.h"
52 #include "magick/memory_.h"
53 #include "magick/monitor.h"
54 #include "magick/monitor-private.h"
55 #include "magick/quantum-private.h"
56 #include "magick/static.h"
57 #include "magick/string_.h"
58 #include "magick/module.h"
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 % R e a d T I M I m a g e %
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % ReadTIMImage() reads a PSX TIM image file and returns it. It
72 % allocates the memory necessary for the new Image structure and returns a
73 % pointer to the new image.
75 % Contributed by os@scee.sony.co.uk.
77 % The format of the ReadTIMImage method is:
79 % Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
81 % A description of each parameter follows:
83 % o image_info: the image info.
85 % o exception: return any errors or warnings in this structure.
88 static Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 typedef struct _TIMInfo
125 register unsigned char
148 assert(image_info != (const ImageInfo *) NULL);
149 assert(image_info->signature == MagickSignature);
150 if (image_info->debug != MagickFalse)
151 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
152 image_info->filename);
153 assert(exception != (ExceptionInfo *) NULL);
154 assert(exception->signature == MagickSignature);
155 image=AcquireImage(image_info);
156 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
157 if (status == MagickFalse)
159 image=DestroyImageList(image);
160 return((Image *) NULL);
163 Determine if this a TIM file.
165 tim_info.id=ReadBlobLSBLong(image);
169 Verify TIM identifier.
171 if (tim_info.id != 0x00000010)
172 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
173 tim_info.flag=ReadBlobLSBLong(image);
174 has_clut=tim_info.flag & (1 << 3) ? 1 : 0;
175 pixel_mode=tim_info.flag & 0x07;
176 switch ((int) pixel_mode)
178 case 0: bits_per_pixel=4; break;
179 case 1: bits_per_pixel=8; break;
180 case 2: bits_per_pixel=16; break;
181 case 3: bits_per_pixel=24; break;
182 default: bits_per_pixel=4; break;
190 Read TIM raster colormap.
192 (void)ReadBlobLSBLong(image);
193 (void)ReadBlobLSBShort(image);
194 (void)ReadBlobLSBShort(image);
195 width=ReadBlobLSBShort(image);
196 height=ReadBlobLSBShort(image);
197 image->columns=width;
199 if (AcquireImageColormap(image,pixel_mode == 1 ? 256UL : 16UL) == MagickFalse)
200 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
201 tim_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
202 2UL*sizeof(*tim_colormap));
203 if (tim_colormap == (unsigned char *) NULL)
204 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
205 count=ReadBlob(image,2*image->colors,tim_colormap);
206 if (count != (ssize_t) (2*image->colors))
207 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
209 for (i=0; i < (long) image->colors; i++)
212 word|=(unsigned short) (*p++ << 8);
213 image->colormap[i].blue=ScaleCharToQuantum(
214 ScaleColor5to8(1UL*(word >> 10) & 0x1f));
215 image->colormap[i].green=ScaleCharToQuantum(
216 ScaleColor5to8(1UL*(word >> 5) & 0x1f));
217 image->colormap[i].red=ScaleCharToQuantum(
218 ScaleColor5to8(1UL*word & 0x1f));
220 tim_colormap=(unsigned char *) RelinquishMagickMemory(tim_colormap);
222 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
223 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
228 (void) ReadBlobLSBLong(image);
229 (void) ReadBlobLSBShort(image);
230 (void) ReadBlobLSBShort(image);
231 width=ReadBlobLSBShort(image);
232 height=ReadBlobLSBShort(image);
233 image_size=2*width*height;
234 bytes_per_line=width*2;
235 width=(width*16)/bits_per_pixel;
236 tim_data=(unsigned char *) AcquireQuantumMemory(image_size,
238 if (tim_data == (unsigned char *) NULL)
239 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
240 count=ReadBlob(image,image_size,tim_data);
241 if (count != (ssize_t) (image_size))
242 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
245 Initialize image structure.
247 image->columns=width;
250 Convert TIM raster image to pixel packets.
252 switch (bits_per_pixel)
257 Convert PseudoColor scanline.
259 for (y=(long) image->rows-1; y >= 0; y--)
261 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
262 if (q == (PixelPacket *) NULL)
264 indexes=GetAuthenticIndexQueue(image);
265 p=tim_pixels+y*bytes_per_line;
266 for (x=0; x < ((long) image->columns-1); x+=2)
268 indexes[x]=(IndexPacket) ((*p) & 0x0f);
269 indexes[x+1]=(IndexPacket) ((*p >> 4) & 0x0f);
272 if ((image->columns % 2) != 0)
274 indexes[x]=(IndexPacket) ((*p >> 4) & 0x0f);
277 if (SyncAuthenticPixels(image,exception) == MagickFalse)
279 if (image->previous == (Image *) NULL)
281 status=SetImageProgress(image,LoadImageTag,y,image->rows);
282 if (status == MagickFalse)
291 Convert PseudoColor scanline.
293 for (y=(long) image->rows-1; y >= 0; y--)
295 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
296 if (q == (PixelPacket *) NULL)
298 indexes=GetAuthenticIndexQueue(image);
299 p=tim_pixels+y*bytes_per_line;
300 for (x=0; x < (long) image->columns; x++)
302 if (SyncAuthenticPixels(image,exception) == MagickFalse)
304 if (image->previous == (Image *) NULL)
306 status=SetImageProgress(image,LoadImageTag,y,image->rows);
307 if (status == MagickFalse)
316 Convert DirectColor scanline.
318 for (y=(long) image->rows-1; y >= 0; y--)
320 p=tim_pixels+y*bytes_per_line;
321 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
322 if (q == (PixelPacket *) NULL)
324 for (x=0; x < (long) image->columns; x++)
328 q->blue=ScaleCharToQuantum(ScaleColor5to8((1UL*word >> 10) & 0x1f));
329 q->green=ScaleCharToQuantum(ScaleColor5to8((1UL*word >> 5) & 0x1f));
330 q->red=ScaleCharToQuantum(ScaleColor5to8(1UL*word & 0x1f));
333 if (SyncAuthenticPixels(image,exception) == MagickFalse)
335 if (image->previous == (Image *) NULL)
337 status=SetImageProgress(image,LoadImageTag,y,image->rows);
338 if (status == MagickFalse)
347 Convert DirectColor scanline.
349 for (y=(long) image->rows-1; y >= 0; y--)
351 p=tim_pixels+y*bytes_per_line;
352 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
353 if (q == (PixelPacket *) NULL)
355 for (x=0; x < (long) image->columns; x++)
357 q->red=ScaleCharToQuantum(*p++);
358 q->green=ScaleCharToQuantum(*p++);
359 q->blue=ScaleCharToQuantum(*p++);
362 if (SyncAuthenticPixels(image,exception) == MagickFalse)
364 if (image->previous == (Image *) NULL)
366 status=SetImageProgress(image,LoadImageTag,y,image->rows);
367 if (status == MagickFalse)
374 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
376 if (image->storage_class == PseudoClass)
377 (void) SyncImage(image);
378 tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
379 if (EOFBlob(image) != MagickFalse)
381 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
386 Proceed to next image.
388 tim_info.id=ReadBlobLSBLong(image);
389 if (tim_info.id == 0x00000010)
392 Allocate next image structure.
394 AcquireNextImage(image_info,image);
395 if (GetNextImageInList(image) == (Image *) NULL)
397 image=DestroyImageList(image);
398 return((Image *) NULL);
400 image=SyncNextImageInList(image);
401 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
403 if (status == MagickFalse)
406 } while (tim_info.id == 0x00000010);
407 (void) CloseBlob(image);
408 return(GetFirstImageInList(image));
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % R e g i s t e r T I M I m a g e %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % RegisterTIMImage() adds attributes for the TIM image format to
423 % the list of supported formats. The attributes include the image format
424 % tag, a method to read and/or write the format, whether the format
425 % supports the saving of more than one frame to the same file or blob,
426 % whether the format supports native in-memory I/O, and a brief
427 % description of the format.
429 % The format of the RegisterTIMImage method is:
431 % unsigned long RegisterTIMImage(void)
434 ModuleExport unsigned long RegisterTIMImage(void)
439 entry=SetMagickInfo("TIM");
440 entry->decoder=(DecodeImageHandler *) ReadTIMImage;
441 entry->description=ConstantString("PSX TIM");
442 entry->module=ConstantString("TIM");
443 (void) RegisterMagickInfo(entry);
444 return(MagickImageCoderSignature);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % U n r e g i s t e r T I M I m a g e %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % UnregisterTIMImage() removes format registrations made by the
459 % TIM module from the list of supported formats.
461 % The format of the UnregisterTIMImage method is:
463 % UnregisterTIMImage(void)
466 ModuleExport void UnregisterTIMImage(void)
468 (void) UnregisterMagickInfo("TIM");