2
% Copyright (C) 2003 GraphicsMagick Group
3
% Copyright (C) 2002 ImageMagick Studio
4
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
6
% This program is covered by multiple licenses, which are described in
7
% Copyright.txt. You should have received a copy of Copyright.txt with this
8
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
% Read PSX TIM Image Format. %
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
#include "magick/studio.h"
39
#include "magick/blob.h"
40
#include "magick/cache.h"
41
#include "magick/magick.h"
42
#include "magick/monitor.h"
43
#include "magick/utility.h"
46
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
% R e a d T I M I m a g e %
54
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56
% Method ReadTIMImage reads a PSX TIM image file and returns it. It
57
% allocates the memory necessary for the new Image structure and returns a
58
% pointer to the new image.
60
% Contributed by os@scee.sony.co.uk.
62
% The format of the ReadTIMImage method is:
64
% Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
66
% A description of each parameter follows:
68
% o image: Method ReadTIMImage returns a pointer to the image after
69
% reading. A null image is returned if there is a memory shortage or
70
% if the image cannot be read.
72
% o image_info: Specifies a pointer to a ImageInfo structure.
74
% o exception: return any errors or warnings in this structure.
78
static Image *ReadTIMImage(const ImageInfo *image_info,ExceptionInfo *exception)
80
typedef struct _TIMInfo
112
register unsigned char
135
assert(image_info != (const ImageInfo *) NULL);
136
assert(image_info->signature == MagickSignature);
137
assert(exception != (ExceptionInfo *) NULL);
138
assert(exception->signature == MagickSignature);
139
image=AllocateImage(image_info);
140
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
142
ThrowReaderException(FileOpenError,UnableToOpenFile,image);
144
Determine if this is a TIM file.
146
tim_info.id=ReadBlobLSBLong(image);
150
Verify TIM identifier.
152
if (tim_info.id != 0x00000010)
153
ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
154
tim_info.flag=ReadBlobLSBLong(image);
155
has_clut=!!(tim_info.flag & (1 << 3));
156
pixel_mode=tim_info.flag & 0x07;
157
switch ((int) pixel_mode)
159
case 0: bits_per_pixel=4; break;
160
case 1: bits_per_pixel=8; break;
161
case 2: bits_per_pixel=16; break;
162
case 3: bits_per_pixel=24; break;
163
default: bits_per_pixel=4; break;
171
Read TIM raster colormap.
173
(void)ReadBlobLSBLong(image);
174
(void)ReadBlobLSBShort(image);
175
(void)ReadBlobLSBShort(image);
176
width=ReadBlobLSBShort(image);
177
height=ReadBlobLSBShort(image);
178
if (!AllocateImageColormap(image,pixel_mode == 1 ? 256 : 16))
179
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
181
tim_colormap=MagickAllocateMemory(unsigned char *,image->colors*2);
182
if (tim_colormap == (unsigned char *) NULL)
183
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
185
(void) ReadBlob(image,2*image->colors,(char *) tim_colormap);
187
for (i=0; i < (long) image->colors; i++)
190
word|=(unsigned short) (*p++ << 8);
191
image->colormap[i].blue=ScaleCharToQuantum(ScaleColor5to8((word >> 10) & 0x1f));
192
image->colormap[i].green=ScaleCharToQuantum(ScaleColor5to8((word >> 5) & 0x1f));
193
image->colormap[i].red=ScaleCharToQuantum(ScaleColor5to8(word & 0x1f));
195
MagickFreeMemory(tim_colormap);
197
if (image_info->ping && (image_info->subrange != 0))
198
if (image->scene >= (image_info->subimage+image_info->subrange-1))
203
(void) ReadBlobLSBLong(image);
204
(void) ReadBlobLSBShort(image);
205
(void) ReadBlobLSBShort(image);
206
width=ReadBlobLSBShort(image);
207
height=ReadBlobLSBShort(image);
208
image_size=2*width*height;
209
bytes_per_line=width*2;
210
width=(width*16)/bits_per_pixel;
211
tim_data=MagickAllocateMemory(unsigned char *,image_size);
212
if (tim_data == (unsigned char *) NULL)
213
ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
214
(void) ReadBlob(image,image_size,(char *) tim_data);
217
Initialize image structure.
219
image->columns=width;
222
Convert TIM raster image to pixel packets.
224
switch (bits_per_pixel)
229
Convert PseudoColor scanline.
231
for (y=(long) image->rows-1; y >= 0; y--)
233
q=SetImagePixels(image,0,y,image->columns,1);
234
if (q == (PixelPacket *) NULL)
236
indexes=GetIndexes(image);
237
p=tim_pixels+y*bytes_per_line;
238
for (x=0; x < ((long) image->columns-1); x+=2)
240
indexes[x]=(*p) & 0xf;
241
indexes[x+1]=(*p >> 4) & 0xf;
244
if ((image->columns % 2) != 0)
246
indexes[x]=(*p >> 4) & 0xf;
249
if (!SyncImagePixels(image))
251
if (QuantumTick(y,image->rows))
253
status=MagickMonitor(LoadImageText,image->rows-y-1,image->rows,
264
Convert PseudoColor scanline.
266
for (y=(long) image->rows-1; y >= 0; y--)
268
q=SetImagePixels(image,0,y,image->columns,1);
269
if (q == (PixelPacket *) NULL)
271
indexes=GetIndexes(image);
272
p=tim_pixels+y*bytes_per_line;
273
for (x=0; x < (long) image->columns; x++)
275
if (!SyncImagePixels(image))
277
if (QuantumTick(y,image->rows))
279
status=MagickMonitor(LoadImageText,image->rows-y-1,image->rows,
290
Convert DirectColor scanline.
292
for (y=(long) image->rows-1; y >= 0; y--)
294
p=tim_pixels+y*bytes_per_line;
295
q=SetImagePixels(image,0,y,image->columns,1);
296
if (q == (PixelPacket *) NULL)
298
for (x=0; x < (long) image->columns; x++)
302
q->blue=ScaleCharToQuantum(ScaleColor5to8((word >> 10) & 0x1f));
303
q->green=ScaleCharToQuantum(ScaleColor5to8((word >> 5) & 0x1f));
304
q->red=ScaleCharToQuantum(ScaleColor5to8(word & 0x1f));
307
if (!SyncImagePixels(image))
309
if (QuantumTick(y,image->rows))
311
status=MagickMonitor(LoadImageText,image->rows-y-1,image->rows,
322
Convert DirectColor scanline.
324
for (y=(long) image->rows-1; y >= 0; y--)
326
p=tim_pixels+y*bytes_per_line;
327
q=SetImagePixels(image,0,y,image->columns,1);
328
if (q == (PixelPacket *) NULL)
330
for (x=0; x < (long) image->columns; x++)
332
q->red=ScaleCharToQuantum(*p++);
333
q->green=ScaleCharToQuantum(*p++);
334
q->blue=ScaleCharToQuantum(*p++);
337
if (!SyncImagePixels(image))
339
if (QuantumTick(y,image->rows))
341
status=MagickMonitor(LoadImageText,image->rows-y-1,image->rows,
350
ThrowReaderException(CorruptImageError,ImproperImageHeader,image)
352
if (image->storage_class == PseudoClass)
354
MagickFreeMemory(tim_pixels);
357
ThrowException(exception,CorruptImageError,UnexpectedEndOfFile,
362
Proceed to next image.
364
tim_info.id=ReadBlobLSBLong(image);
365
if (tim_info.id == 0x00000010)
368
Allocate next image structure.
370
AllocateNextImage(image_info,image);
371
if (image->next == (Image *) NULL)
373
DestroyImageList(image);
374
return((Image *) NULL);
376
image=SyncNextImageInList(image);
377
status=MagickMonitor(LoadImagesText,TellBlob(image),GetBlobSize(image),
382
} while (tim_info.id == 0x00000010);
383
while (image->previous != (Image *) NULL)
384
image=image->previous;
390
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394
% R e g i s t e r T I M I m a g e %
398
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400
% Method RegisterTIMImage adds attributes for the TIM image format to
401
% the list of supported formats. The attributes include the image format
402
% tag, a method to read and/or write the format, whether the format
403
% supports the saving of more than one frame to the same file or blob,
404
% whether the format supports native in-memory I/O, and a brief
405
% description of the format.
407
% The format of the RegisterTIMImage method is:
409
% RegisterTIMImage(void)
412
ModuleExport void RegisterTIMImage(void)
417
entry=SetMagickInfo("TIM");
418
entry->decoder=(DecoderHandler) ReadTIMImage;
419
entry->description=AcquireString("PSX TIM");
420
entry->module=AcquireString("TIM");
421
(void) RegisterMagickInfo(entry);
425
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429
% U n r e g i s t e r T I M I m a g e %
433
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435
% Method UnregisterTIMImage removes format registrations made by the
436
% TIM module from the list of supported formats.
438
% The format of the UnregisterTIMImage method is:
440
% UnregisterTIMImage(void)
443
ModuleExport void UnregisterTIMImage(void)
445
(void) UnregisterMagickInfo("TIM");