2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read PSX TIM Image Format %
20 % Copyright 1999-2018 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 % https://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 "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"
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 % R e a d T I M I m a g e %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
77 % Contributed by os@scee.sony.co.uk.
79 % The format of the ReadTIMImage method is:
81 % Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
83 % A description of each parameter follows:
85 % o image_info: the image info.
87 % o exception: return any errors or warnings in this structure.
90 static Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 typedef struct _TIMInfo
121 register unsigned char
144 assert(image_info != (const ImageInfo *) NULL);
145 assert(image_info->signature == MagickCoreSignature);
146 if (image_info->debug != MagickFalse)
147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
148 image_info->filename);
149 assert(exception != (ExceptionInfo *) NULL);
150 assert(exception->signature == MagickCoreSignature);
151 image=AcquireImage(image_info,exception);
152 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
153 if (status == MagickFalse)
155 image=DestroyImageList(image);
156 return((Image *) NULL);
159 Determine if this a TIM file.
161 tim_info.id=ReadBlobLSBLong(image);
165 Verify TIM identifier.
167 if (tim_info.id != 0x00000010)
168 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
169 tim_info.flag=ReadBlobLSBLong(image);
170 has_clut=tim_info.flag & (1 << 3) ? 1 : 0;
171 pixel_mode=tim_info.flag & 0x07;
172 switch ((int) pixel_mode)
174 case 0: bits_per_pixel=4; break;
175 case 1: bits_per_pixel=8; break;
176 case 2: bits_per_pixel=16; break;
177 case 3: bits_per_pixel=24; break;
178 default: bits_per_pixel=4; break;
187 Read TIM raster colormap.
189 (void)ReadBlobLSBLong(image);
190 (void)ReadBlobLSBShort(image);
191 (void)ReadBlobLSBShort(image);
192 width=ReadBlobLSBShort(image);
193 height=ReadBlobLSBShort(image);
194 image->columns=width;
196 if (AcquireImageColormap(image,pixel_mode == 1 ? 256UL : 16UL,exception) == MagickFalse)
197 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
198 tim_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
199 2UL*sizeof(*tim_colormap));
200 if (tim_colormap == (unsigned char *) NULL)
201 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
202 count=ReadBlob(image,2*image->colors,tim_colormap);
203 if (count != (ssize_t) (2*image->colors))
205 tim_colormap=(unsigned char *) RelinquishMagickMemory(tim_colormap);
206 ThrowReaderException(CorruptImageError,
207 "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 if (image_size > GetBlobSize(image))
236 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
237 bytes_per_line=width*2;
238 width=(width*16)/bits_per_pixel;
239 image->columns=width;
241 status=SetImageExtent(image,image->columns,image->rows,exception);
242 if (status == MagickFalse)
243 return(DestroyImageList(image));
244 status=ResetImagePixels(image,exception);
245 if (status == MagickFalse)
246 return(DestroyImageList(image));
247 tim_pixels=(unsigned char *) AcquireQuantumMemory(image_size,
248 sizeof(*tim_pixels));
249 if (tim_pixels == (unsigned char *) NULL)
250 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
251 count=ReadBlob(image,image_size,tim_pixels);
252 if (count != (ssize_t) (image_size))
254 tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
255 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
258 Convert TIM raster image to pixel packets.
260 switch (bits_per_pixel)
265 Convert PseudoColor scanline.
267 for (y=(ssize_t) image->rows-1; y >= 0; y--)
269 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
270 if (q == (Quantum *) NULL)
272 p=tim_pixels+y*bytes_per_line;
273 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
275 SetPixelIndex(image,(*p) & 0x0f,q);
276 q+=GetPixelChannels(image);
277 SetPixelIndex(image,(*p >> 4) & 0x0f,q);
279 q+=GetPixelChannels(image);
281 if ((image->columns % 2) != 0)
283 SetPixelIndex(image,(*p >> 4) & 0x0f,q);
285 q+=GetPixelChannels(image);
287 if (SyncAuthenticPixels(image,exception) == MagickFalse)
289 if (image->previous == (Image *) NULL)
291 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
293 if (status == MagickFalse)
302 Convert PseudoColor scanline.
304 for (y=(ssize_t) image->rows-1; y >= 0; y--)
306 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
307 if (q == (Quantum *) NULL)
309 p=tim_pixels+y*bytes_per_line;
310 for (x=0; x < (ssize_t) image->columns; x++)
312 SetPixelIndex(image,*p++,q);
313 q+=GetPixelChannels(image);
315 if (SyncAuthenticPixels(image,exception) == MagickFalse)
317 if (image->previous == (Image *) NULL)
319 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
321 if (status == MagickFalse)
330 Convert DirectColor scanline.
332 for (y=(ssize_t) image->rows-1; y >= 0; y--)
334 p=tim_pixels+y*bytes_per_line;
335 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
336 if (q == (Quantum *) NULL)
338 for (x=0; x < (ssize_t) image->columns; x++)
342 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
343 (1UL*word >> 10) & 0x1f)),q);
344 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
345 (1UL*word >> 5) & 0x1f)),q);
346 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
347 (1UL*word >> 0) & 0x1f)),q);
348 q+=GetPixelChannels(image);
350 if (SyncAuthenticPixels(image,exception) == MagickFalse)
352 if (image->previous == (Image *) NULL)
354 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
356 if (status == MagickFalse)
365 Convert DirectColor scanline.
367 for (y=(ssize_t) image->rows-1; y >= 0; y--)
369 p=tim_pixels+y*bytes_per_line;
370 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
371 if (q == (Quantum *) NULL)
373 for (x=0; x < (ssize_t) image->columns; x++)
375 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
376 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
377 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
378 q+=GetPixelChannels(image);
380 if (SyncAuthenticPixels(image,exception) == MagickFalse)
382 if (image->previous == (Image *) NULL)
384 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
386 if (status == MagickFalse)
394 tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
395 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
398 if (image->storage_class == PseudoClass)
399 (void) SyncImage(image,exception);
400 tim_pixels=(unsigned char *) RelinquishMagickMemory(tim_pixels);
401 if (EOFBlob(image) != MagickFalse)
403 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
408 Proceed to next image.
410 if (image_info->number_scenes != 0)
411 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
413 tim_info.id=ReadBlobLSBLong(image);
414 if (tim_info.id == 0x00000010)
417 Allocate next image structure.
419 AcquireNextImage(image_info,image,exception);
420 if (GetNextImageInList(image) == (Image *) NULL)
425 image=SyncNextImageInList(image);
426 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
428 if (status == MagickFalse)
431 } while (tim_info.id == 0x00000010);
432 (void) CloseBlob(image);
433 if (status == MagickFalse)
434 return(DestroyImageList(image));
435 return(GetFirstImageInList(image));
439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443 % R e g i s t e r T I M I m a g e %
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 % RegisterTIMImage() adds attributes for the TIM image format to
450 % the list of supported formats. The attributes include the image format
451 % tag, a method to read and/or write the format, whether the format
452 % supports the saving of more than one frame to the same file or blob,
453 % whether the format supports native in-memory I/O, and a brief
454 % description of the format.
456 % The format of the RegisterTIMImage method is:
458 % size_t RegisterTIMImage(void)
461 ModuleExport size_t RegisterTIMImage(void)
466 entry=AcquireMagickInfo("TIM","TIM","PSX TIM");
467 entry->decoder=(DecodeImageHandler *) ReadTIMImage;
468 (void) RegisterMagickInfo(entry);
469 return(MagickImageCoderSignature);
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % U n r e g i s t e r T I M I m a g e %
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 % UnregisterTIMImage() removes format registrations made by the
484 % TIM module from the list of supported formats.
486 % The format of the UnregisterTIMImage method is:
488 % UnregisterTIMImage(void)
491 ModuleExport void UnregisterTIMImage(void)
493 (void) UnregisterMagickInfo("TIM");