~ubuntu-branches/ubuntu/intrepid/graphicsmagick/intrepid

« back to all changes in this revision

Viewing changes to coders/topol.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2006-05-06 16:28:08 UTC
  • Revision ID: james.westby@ubuntu.com-20060506162808-vt2ni3r5nytcszms
Tags: upstream-1.1.7
ImportĀ upstreamĀ versionĀ 1.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
% Copyright (C) 2003 GraphicsMagick Group
 
3
%
 
4
% This program is covered by multiple licenses, which are described in
 
5
% Copyright.txt. You should have received a copy of Copyright.txt with this
 
6
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
 
7
%
 
8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
9
%                                                                             %
 
10
%                                                                             %
 
11
%                    TTTTT   OOO    PPPP    OOO    L                          %
 
12
%                      T    O   O   P   P  O   O   L                          %
 
13
%                      T    O   O   PPPP   O   O   L                          %
 
14
%                      T    O   O   P      O   O   L                          %
 
15
%                      T     OOO    P       OOO    LLLL                       %
 
16
%                                                                             %
 
17
%                                                                             %
 
18
%                    Read/Write TOPOL X Image Raster Format.                  %
 
19
%                                                                             %
 
20
%                                                                             %
 
21
%                              Software Design                                %
 
22
%                              Jaroslav Fojtik                                %
 
23
%                                 June 2003                                   %
 
24
%                                                                             %
 
25
%                                                                             %
 
26
%                                                                             %
 
27
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
28
%
 
29
%
 
30
*/
 
31
 
 
32
/*
 
33
  Include declarations.
 
34
*/
 
35
#include "magick/studio.h"
 
36
#include "magick/blob.h"
 
37
#include "magick/cache.h"
 
38
#include "magick/color.h"
 
39
#include "magick/error.h"
 
40
#include "magick/list.h"
 
41
#include "magick/magick.h"
 
42
#include "magick/utility.h"
 
43
 
 
44
#if !defined(SWORD)
 
45
#  define SWORD unsigned int
 
46
#endif
 
47
#if !defined(SDWORD)
 
48
#  define SDWORD unsigned long
 
49
#endif
 
50
#if !defined(WORD)
 
51
#  define WORD  int
 
52
#endif
 
53
#if !defined(BYTE)
 
54
#  define BYTE  unsigned char
 
55
#endif
 
56
 
 
57
typedef struct
 
58
{
 
59
  char Name[20];
 
60
  SWORD Rows;
 
61
  SWORD Cols;
 
62
  SWORD TypSou;                 /* 0-binary, 1-8 bitu, 2-8 bits+PAL, 3-4 bits,
 
63
                                   4-4 bits+PAL, 5-24 bits, 6-16 bits, 7-32
 
64
                                   bits */
 
65
  SDWORD Zoom;
 
66
  SWORD Version;
 
67
  SWORD Komprese;               /* 0 - uncompressed (from release 1) */
 
68
  SWORD Stav;
 
69
  double xRasMin;
 
70
  double yRasMin;
 
71
  double xRasMax;
 
72
  double yRasMax;
 
73
  double Scale;                 /* from release 2 */
 
74
  WORD TileWidth;               /* tile width in pixels */
 
75
  WORD TileHeight;              /* tile height in pixels */
 
76
  SDWORD TileOffsets;           /* offset to array of longints that contains
 
77
                                   adresses of tiles in the raster (adreses
 
78
                                   are counted from 0) */
 
79
  SDWORD TileByteCounts;        /* offset to array of words, that contain amount of bytes stored in
 
80
                                   tiles. The tile size might vary depending on
 
81
                                   the value TileCompression */
 
82
  BYTE TileCompression;         /* 0 - uncompressed, 1 - variant TIFF
 
83
                                   Packbits, 2 - CCITT G3 */
 
84
 
 
85
  BYTE Dummy[423];
 
86
} RasHeader;
 
87
 
 
88
/*
 
89
typedef struct                  The palette record inside TopoL
 
90
{
 
91
   BYTE Flag;
 
92
   BYTE Red;
 
93
   BYTE Green;
 
94
   BYTE Blue;
 
95
} paletteRAS;*/
 
96
 
 
97
static void InsertRow(int depth, unsigned char *p, long y, Image * image, unsigned char *MEZ)
 
98
{
 
99
  int
 
100
    bit;
 
101
 
 
102
  long
 
103
    x;
 
104
 
 
105
  register PixelPacket
 
106
    *q;
 
107
 
 
108
  IndexPacket
 
109
    index;
 
110
 
 
111
  register IndexPacket
 
112
    *indexes;
 
113
 
 
114
  switch (depth)
 
115
    {
 
116
    case 1:                     /* Convert bitmap scanline. */
 
117
      {
 
118
        q = SetImagePixels(image, 0, y, image->columns, 1);
 
119
        if (q == (PixelPacket *) NULL)
 
120
          break;
 
121
        indexes = GetIndexes(image);
 
122
        for (x = 0; x < ((long) image->columns - 7); x += 8)
 
123
          {
 
124
            for (bit = 0; bit < 8; bit++)
 
125
              {
 
126
                index = ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
 
127
                indexes[x + bit] = MEZ[index];
 
128
                *q++ = image->colormap[index];
 
129
              }
 
130
            p++;
 
131
          }
 
132
        if ((image->columns % 8) != 0)
 
133
          {
 
134
            for (bit = 0; bit < (long) (image->columns % 8); bit++)
 
135
              {
 
136
                index = ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
 
137
                indexes[x + bit] = MEZ[index];
 
138
                *q++ = image->colormap[index];
 
139
              }
 
140
            p++;
 
141
          }
 
142
        if (!SyncImagePixels(image))
 
143
          break;
 
144
        /* if (image->previous == (Image *) NULL)
 
145
           if (QuantumTick(y,image->rows))
 
146
           ProgressMonitor(LoadImageText,image->rows-y-1,image->rows); */
 
147
        break;
 
148
      }
 
149
    case 2:                     /* Convert PseudoColor scanline. */
 
150
      {
 
151
        q = SetImagePixels(image, 0, y, image->columns, 1);
 
152
        if (q == (PixelPacket *) NULL)
 
153
          break;
 
154
        indexes = GetIndexes(image);
 
155
        for (x = 0; x < ((long) image->columns - 1); x += 2)
 
156
          {
 
157
            index = (IndexPacket) ((*p >> 6) & 0x3);
 
158
            VerifyColormapIndex(image, index);
 
159
            indexes[x] = MEZ[index];
 
160
            *q++ = image->colormap[index];
 
161
            index = (IndexPacket) ((*p >> 4) & 0x3);
 
162
            VerifyColormapIndex(image, index);
 
163
            indexes[x] = MEZ[index];
 
164
            *q++ = image->colormap[index];
 
165
            index = (IndexPacket) ((*p >> 2) & 0x3);
 
166
            VerifyColormapIndex(image, index);
 
167
            indexes[x] = MEZ[index];
 
168
            *q++ = image->colormap[index];
 
169
            index = (IndexPacket) ((*p) & 0x3);
 
170
            VerifyColormapIndex(image, index);
 
171
            indexes[x + 1] = MEZ[index];
 
172
            *q++ = image->colormap[index];
 
173
            p++;
 
174
          }
 
175
        if ((image->columns % 4) != 0)
 
176
          {
 
177
            index = (IndexPacket) ((*p >> 6) & 0x3);
 
178
            VerifyColormapIndex(image, index);
 
179
            indexes[x] = MEZ[index];
 
180
            *q++ = image->colormap[index];
 
181
            if ((image->columns % 4) >= 1)
 
182
              {
 
183
                index = (IndexPacket) ((*p >> 4) & 0x3);
 
184
                VerifyColormapIndex(image, index);
 
185
                indexes[x] = MEZ[index];
 
186
                *q++ = image->colormap[index];
 
187
                if ((image->columns % 4) >= 2)
 
188
                  {
 
189
                    index = (IndexPacket) ((*p >> 2) & 0x3);
 
190
                    VerifyColormapIndex(image, index);
 
191
                    indexes[x] = MEZ[index];
 
192
                    *q++ = image->colormap[index];
 
193
                  }
 
194
              }
 
195
            p++;
 
196
          }
 
197
        if (!SyncImagePixels(image))
 
198
          break;
 
199
        /*         if (image->previous == (Image *) NULL)
 
200
                   if (QuantumTick(y,image->rows))
 
201
                   ProgressMonitor(LoadImageText,image->rows-y-1,image->rows); */
 
202
        break;
 
203
      }
 
204
 
 
205
    case 4:                     /* Convert PseudoColor scanline. */
 
206
      {
 
207
        q = SetImagePixels(image, 0, y, image->columns, 1);
 
208
        if (q == (PixelPacket *) NULL)
 
209
          break;
 
210
        indexes = GetIndexes(image);
 
211
        for (x = 0; x < ((long) image->columns - 1); x += 2)
 
212
          {
 
213
            index = (IndexPacket) ((*p >> 4) & 0xf);
 
214
            VerifyColormapIndex(image, index);
 
215
            indexes[x] = MEZ[index];
 
216
            *q++ = image->colormap[index];
 
217
            index = (IndexPacket) ((*p) & 0xf);
 
218
            VerifyColormapIndex(image, index);
 
219
            indexes[x + 1] = MEZ[index];
 
220
            *q++ = image->colormap[index];
 
221
            p++;
 
222
          }
 
223
        if ((image->columns % 2) != 0)
 
224
          {
 
225
            index = (IndexPacket) ((*p >> 4) & 0xf);
 
226
            VerifyColormapIndex(image, index);
 
227
            indexes[x] = MEZ[index];
 
228
            *q++ = image->colormap[index];
 
229
            p++;
 
230
          }
 
231
        if (!SyncImagePixels(image))
 
232
          break;
 
233
        /*         if (image->previous == (Image *) NULL)
 
234
                   if (QuantumTick(y,image->rows))
 
235
                   ProgressMonitor(LoadImageText,image->rows-y-1,image->rows); */
 
236
        break;
 
237
      }
 
238
    case 8:                     /* Convert PseudoColor scanline. */
 
239
      {
 
240
        q = SetImagePixels(image, 0, y, image->columns, 1);
 
241
        if (q == (PixelPacket *) NULL)
 
242
          break;
 
243
        indexes = GetIndexes(image);
 
244
 
 
245
        for (x = 0; x < (long) image->columns; x++)
 
246
          {
 
247
            index = (IndexPacket) (*p);
 
248
            VerifyColormapIndex(image, index);
 
249
            indexes[x] = MEZ[index];
 
250
            *q++ = image->colormap[index];
 
251
            p++;
 
252
          }
 
253
        if (!SyncImagePixels(image))
 
254
          break;
 
255
        /*           if (image->previous == (Image *) NULL)
 
256
                     if (QuantumTick(y,image->rows))
 
257
                     ProgressMonitor(LoadImageText,image->rows-y-1,image->rows); */
 
258
      }
 
259
      break;
 
260
    }
 
261
}
 
262
 
 
263
static double ReadBlobLSBdouble(Image * image)
 
264
{
 
265
  typedef union
 
266
  {
 
267
    double d;
 
268
    char chars[8];
 
269
  } dbl;
 
270
 
 
271
  static unsigned long
 
272
    lsb_first = 1;
 
273
 
 
274
  dbl
 
275
    buffer;
 
276
 
 
277
  char
 
278
    c;
 
279
 
 
280
  assert(image != (Image *) NULL);
 
281
  assert(image->signature == MagickSignature);
 
282
 
 
283
  if (ReadBlob(image, 8, (unsigned char *) &buffer) == 0)
 
284
    return (0.0);
 
285
 
 
286
  if (*(char *) &lsb_first == 1)
 
287
    return (buffer.d);
 
288
 
 
289
  c = buffer.chars[0];
 
290
  buffer.chars[0] = buffer.chars[7];
 
291
  buffer.chars[7] = c;
 
292
  c = buffer.chars[1];
 
293
  buffer.chars[1] = buffer.chars[6];
 
294
  buffer.chars[6] = c;
 
295
  c = buffer.chars[2];
 
296
  buffer.chars[2] = buffer.chars[5];
 
297
  buffer.chars[5] = c;
 
298
  c = buffer.chars[3];
 
299
  buffer.chars[3] = buffer.chars[4];
 
300
  buffer.chars[4] = c;
 
301
  return (buffer.d);
 
302
}
 
303
 
 
304
/* This function reads one block of unsigned shortS */
 
305
static void ReadBlobWordLSB(Image * image, size_t len, unsigned short *data)
 
306
{
 
307
  while (len >= 2)
 
308
    {
 
309
      *data++ = ReadBlobLSBShort(image);
 
310
      len -= 2;
 
311
    }
 
312
  if (len > 0)
 
313
    (void) SeekBlob(image, len, SEEK_CUR);
 
314
}
 
315
 
 
316
/* This function reads one block of unsigned longS */
 
317
static void ReadBlobDwordLSB(Image * image, size_t len, unsigned long *data)
 
318
{
 
319
  while (len >= 2)
 
320
    {
 
321
      *data++ = ReadBlobLSBLong(image);
 
322
      len -= 2;
 
323
    }
 
324
  if (len > 0)
 
325
    (void) SeekBlob(image, len, SEEK_CUR);
 
326
}
 
327
 
 
328
/*
 
329
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
330
%                                                                             %
 
331
%                                                                             %
 
332
%                                                                             %
 
333
%   R e a d T O P O L I m a g e                                               %
 
334
%                                                                             %
 
335
%                                                                             %
 
336
%                                                                             %
 
337
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
338
%
 
339
%  Method ReadTOPOLImage reads an TOPOL X image file and returns it.  It
 
340
%  allocates the memory necessary for the new Image structure and returns a
 
341
%  pointer to the new image.
 
342
%
 
343
%  The format of the ReadTOPOLImage method is:
 
344
%
 
345
%      Image *ReadTOPOLImage(const ImageInfo *image_info,ExceptionInfo *exception)
 
346
%
 
347
%  A description of each parameter follows:
 
348
%
 
349
%    o image:  Method ReadTOPOLImage returns a pointer to the image after
 
350
%      reading. A null image is returned if there is a memory shortage or if
 
351
%      the image cannot be read.
 
352
%
 
353
%    o image_info: The image info.
 
354
%
 
355
%    o exception: return any errors or warnings in this structure.
 
356
%
 
357
%
 
358
*/
 
359
static Image *ReadTOPOLImage(const ImageInfo * image_info, ExceptionInfo * exception)
 
360
{
 
361
  Image
 
362
    *image,
 
363
    *palette;
 
364
 
 
365
  ImageInfo
 
366
    *clone_info;
 
367
 
 
368
  RasHeader
 
369
    Header;
 
370
 
 
371
  /* paletteRAS
 
372
     Pal; */
 
373
 
 
374
  int
 
375
    depth,
 
376
    status;
 
377
 
 
378
  long
 
379
    i,
 
380
    j,
 
381
    ldblk;
 
382
 
 
383
  unsigned char
 
384
    *BImgBuff = NULL,
 
385
    MEZ[256];
 
386
 
 
387
 
 
388
  palette = NULL;
 
389
  clone_info = NULL;
 
390
 
 
391
  /*
 
392
    Open image file.
 
393
  */
 
394
  assert(image_info != (const ImageInfo *) NULL);
 
395
  assert(image_info->signature == MagickSignature);
 
396
  assert(exception != (ExceptionInfo *) NULL);
 
397
  assert(exception->signature == MagickSignature);
 
398
  image = AllocateImage(image_info);
 
399
  status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
 
400
  if (status == False)
 
401
    ThrowReaderException(FileOpenError, UnableToOpenFile, image);
 
402
 
 
403
  /*
 
404
    Read TopoL RAS header.
 
405
  */
 
406
  memset(&Header, 0, sizeof(Header));
 
407
  ReadBlob(image, 20, &Header.Name);
 
408
  Header.Rows = ReadBlobLSBShort(image);
 
409
  Header.Cols = ReadBlobLSBShort(image);
 
410
  Header.TypSou = ReadBlobLSBShort(image);
 
411
  Header.Zoom = ReadBlobLSBLong(image);
 
412
  Header.Version = ReadBlobLSBShort(image);
 
413
  if (Header.Version >= 1)
 
414
    {
 
415
      Header.Komprese = ReadBlobLSBShort(image);
 
416
      Header.Stav = ReadBlobLSBShort(image);
 
417
      Header.xRasMin = ReadBlobLSBdouble(image);
 
418
      Header.yRasMin = ReadBlobLSBdouble(image);
 
419
      Header.xRasMax = ReadBlobLSBdouble(image);
 
420
      Header.yRasMax = ReadBlobLSBdouble(image);
 
421
      if (Header.Version >= 2)  /* from release 2 */
 
422
        {
 
423
          Header.Scale = ReadBlobLSBdouble(image);
 
424
          Header.TileWidth = ReadBlobLSBShort(image);
 
425
          Header.TileHeight = ReadBlobLSBShort(image);
 
426
          Header.TileOffsets = ReadBlobLSBLong(image);
 
427
          Header.TileByteCounts = ReadBlobLSBLong(image);
 
428
          Header.TileCompression = ReadBlobByte(image);
 
429
          /* BYTE Dummy[423]; */
 
430
        }
 
431
    }
 
432
 
 
433
  for (i = 0; i < sizeof(Header.Name); i++)
 
434
    {
 
435
      if (Header.Name[i] < ' ')
 
436
        TOPOL_KO:ThrowReaderException(CorruptImageError,ImproperImageHeader,
 
437
                                      image);
 
438
    }
 
439
  if (Header.Komprese != 0 || (Header.Version >= 2 && Header.TileCompression != 0))
 
440
    ThrowReaderException(CorruptImageError, UnrecognizedImageCompression, image);
 
441
  if (Header.Rows == 0 || Header.Cols == 0)
 
442
    goto TOPOL_KO;
 
443
  if (Header.Version > 2)
 
444
    ThrowReaderException(CorruptImageError, InvalidFileFormatVersion, image); /* unknown version */
 
445
 
 
446
  switch (Header.TypSou)
 
447
    {
 
448
    case 0:
 
449
      image->colors = 2;
 
450
      depth = 1;
 
451
      break;
 
452
    case 1:
 
453
    case 2:
 
454
      image->colors = 256;
 
455
      depth = 8;
 
456
      break;
 
457
    case 3:
 
458
    case 4:
 
459
      image->colors = 16;
 
460
      depth = 4;
 
461
      break;
 
462
    case 5:
 
463
      image->colors = 0;
 
464
      depth = 24;
 
465
      break;                    /* ????????? 24 bits */
 
466
    case 6:
 
467
      image->colors = 0;
 
468
      depth = 16;
 
469
      break;                    /* ????????? 16 bits */
 
470
    case 7:
 
471
      image->colors = 0;
 
472
      depth = 32;
 
473
      break;                    /* ????????? 32 bits */
 
474
    default:
 
475
      goto TOPOL_KO;
 
476
    }
 
477
 
 
478
  image->columns = Header.Cols;
 
479
  image->rows = Header.Rows;
 
480
 
 
481
  /* ----- Handle the reindexing mez file ----- */
 
482
  for(i=0;i<255;i++)
 
483
    {
 
484
      MEZ[i]=(unsigned char) i;
 
485
    }
 
486
    
 
487
  if(Header.TypSou>=5) goto NoMEZ;    
 
488
    
 
489
  if ((clone_info=CloneImageInfo(image_info)) == NULL) goto NoMEZ;
 
490
  
 
491
  i=(long) strlen(clone_info->filename);
 
492
  j=i;
 
493
  while(--i>0)
 
494
    {
 
495
      if(clone_info->filename[i]=='.') 
 
496
        {
 
497
          break;
 
498
        }
 
499
      if(clone_info->filename[i]=='/' || clone_info->filename[i]=='\\' ||
 
500
         clone_info->filename[i]==':' ) 
 
501
        {
 
502
          i=j;
 
503
          break;
 
504
        }
 
505
    }
 
506
  
 
507
  (void) strcpy(clone_info->filename+i,".MEZ");
 
508
  if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL)
 
509
    {
 
510
      (void) strcpy(clone_info->filename+i,".mez");
 
511
      if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL)
 
512
        {
 
513
        DestroyImageInfo(clone_info);
 
514
        clone_info=NULL;
 
515
        goto NoMEZ;            
 
516
        }
 
517
    }
 
518
 
 
519
  if( (palette=AllocateImage(clone_info))==NULL ) goto NoMEZ;
 
520
  status=OpenBlob(clone_info,palette,ReadBinaryBlobMode,exception);
 
521
  if (status == False) goto NoMEZ; 
 
522
 
 
523
  ldblk=(long) GetBlobSize(palette);
 
524
  if(ldblk>sizeof(MEZ)) ldblk=sizeof(MEZ);
 
525
  ReadBlob(palette, ldblk, MEZ); 
 
526
            
 
527
NoMEZ:          /*Clean up palette and clone_info*/
 
528
  if (palette != NULL) {DestroyImage(palette);palette=NULL;}
 
529
  if (clone_info != NULL) 
 
530
    {
 
531
    DestroyImageInfo(clone_info);
 
532
    clone_info=NULL;
 
533
    }
 
534
 
 
535
  /* ----- Do something with palette ----- */
 
536
  if ((clone_info=CloneImageInfo(image_info)) == NULL) goto NoPalette;
 
537
 
 
538
  i=(long) strlen(clone_info->filename);
 
539
  j=i;
 
540
  while(--i>0)
 
541
    {
 
542
      if(clone_info->filename[i]=='.') 
 
543
        {
 
544
          break;
 
545
        }
 
546
      if(clone_info->filename[i]=='/' || clone_info->filename[i]=='\\' ||
 
547
         clone_info->filename[i]==':' ) 
 
548
        {
 
549
          i=j;
 
550
          break;
 
551
        }
 
552
    }
 
553
  
 
554
  (void) strcpy(clone_info->filename+i,".PAL");
 
555
  if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL)
 
556
    {
 
557
      (void) strcpy(clone_info->filename+i,".pal");
 
558
      if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL)
 
559
        {
 
560
          clone_info->filename[i]=0;
 
561
          if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) 
 
562
            {
 
563
              DestroyImageInfo(clone_info);
 
564
              clone_info=NULL;
 
565
              goto NoPalette;
 
566
            }
 
567
        }
 
568
    }
 
569
 
 
570
  if( (palette=AllocateImage(clone_info))==NULL ) goto NoPalette;
 
571
  status=OpenBlob(clone_info,palette,ReadBinaryBlobMode,exception);
 
572
  if (status == False)
 
573
    {
 
574
ErasePalette:     
 
575
      DestroyImage(palette);
 
576
      palette=NULL;
 
577
      goto NoPalette;
 
578
    }
 
579
 
 
580
     
 
581
  if(palette!=NULL)
 
582
    {
 
583
    ldblk=ReadBlobByte(palette);                /*size of palette*/
 
584
    if(ldblk==EOF) goto ErasePalette;
 
585
    image->colors=ldblk+1;    
 
586
    if (!AllocateImageColormap(image, image->colors)) goto NoMemory;
 
587
    
 
588
    for(i=0;i<=ldblk;i++)
 
589
        {     
 
590
        j=ReadBlobByte(palette);        /* Flag */
 
591
        if(j==EOF) break;               /* unexpected end of file */
 
592
        if(j<=ldblk)
 
593
          {
 
594
          image->colormap[j].red=ScaleCharToQuantum(ReadBlobByte(palette));
 
595
          image->colormap[j].green=ScaleCharToQuantum(ReadBlobByte(palette));
 
596
          image->colormap[j].blue=ScaleCharToQuantum(ReadBlobByte(palette));       
 
597
          }
 
598
        else
 
599
          {
 
600
          SeekBlob(palette, 3, SEEK_CUR);
 
601
          fprintf(stderr,"TopoL: Wrong index inside palette %d!",(int)j);
 
602
          } 
 
603
        }
 
604
    }
 
605
 
 
606
 
 
607
NoPalette:
 
608
  if (palette == NULL && image->colors != 0)
 
609
    {
 
610
      if (!AllocateImageColormap(image, image->colors))
 
611
        {
 
612
NoMemory:
 
613
          ThrowReaderException(ResourceLimitError, MemoryAllocationFailed, image);
 
614
        }
 
615
 
 
616
      for (i = 0; i < (long) image->colors; i++)
 
617
        {
 
618
          image->colormap[i].red = ScaleCharToQuantum(i);
 
619
          image->colormap[i].green = ScaleCharToQuantum(i);
 
620
          image->colormap[i].blue = ScaleCharToQuantum(i);
 
621
        }
 
622
    }
 
623
 
 
624
  /* ----- Load TopoL raster ----- */
 
625
  ldblk = (long) ((depth * image->columns + 7) / 8);
 
626
  BImgBuff = (unsigned char *) AcquireMemory(ldblk);    /*Ldblk was set in the check phase */
 
627
  if (BImgBuff == NULL)
 
628
    goto NoMemory;
 
629
 
 
630
  (void) SeekBlob(image, 512 /*sizeof(Header)*/, SEEK_SET);
 
631
  for (i = 0; i < (int) Header.Rows; i++)
 
632
    {
 
633
      switch (Header.TypSou)
 
634
        {
 
635
        case 6:
 
636
          ReadBlobWordLSB(image, ldblk, (unsigned short *) BImgBuff);
 
637
          break;
 
638
        case 7:
 
639
          ReadBlobDwordLSB(image, ldblk, (unsigned long *) BImgBuff);
 
640
          break;
 
641
        default:
 
642
          (void) ReadBlob(image, ldblk, (char *) BImgBuff);
 
643
        }
 
644
      InsertRow(depth, BImgBuff, i, image, MEZ);
 
645
    }
 
646
 
 
647
  /* Finish: */
 
648
  if (BImgBuff != NULL)
 
649
    free(BImgBuff);
 
650
  if (palette != NULL)
 
651
    DestroyImage(palette);
 
652
  if (clone_info != NULL)
 
653
    DestroyImageInfo(clone_info);
 
654
  /* if (EOFBlob(image))
 
655
     ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image); */
 
656
  CloseBlob(image);
 
657
  return (image);
 
658
}
 
659
 
 
660
/*
 
661
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
662
%                                                                             %
 
663
%                                                                             %
 
664
%                                                                             %
 
665
%   R e g i s t e r T O P O L I m a g e                                           %
 
666
%                                                                             %
 
667
%                                                                             %
 
668
%                                                                             %
 
669
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
670
%
 
671
%  Method RegisterTOPOLImage adds attributes for the TOPOL image format to
 
672
%  the list of supported formats.  The attributes include the image format
 
673
%  tag, a method to read and/or write the format, whether the format
 
674
%  supports the saving of more than one frame to the same file or blob,
 
675
%  whether the format supports native in-memory I/O, and a brief
 
676
%  description of the format.
 
677
%
 
678
%  The format of the RegisterTOPOLImage method is:
 
679
%
 
680
%      RegisterTOPOLImage(void)
 
681
%
 
682
*/
 
683
ModuleExport void RegisterTOPOLImage(void)
 
684
{
 
685
  MagickInfo * entry;
 
686
 
 
687
  entry = SetMagickInfo("TOPOL");
 
688
  entry->decoder = (DecoderHandler) ReadTOPOLImage;
 
689
  entry->seekable_stream = True;
 
690
  entry->description = AcquireString("TOPOL X Image");
 
691
  entry->module = AcquireString("TOPOL");
 
692
  (void) RegisterMagickInfo(entry);
 
693
}
 
694
 
 
695
/*
 
696
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
697
%                                                                             %
 
698
%                                                                             %
 
699
%                                                                             %
 
700
%   U n r e g i s t e r T O P O L I m a g e                                   %
 
701
%                                                                             %
 
702
%                                                                             %
 
703
%                                                                             %
 
704
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
705
%
 
706
%  Method UnregisterTOPOLImage removes format registrations made by the
 
707
%  TOPOL module from the list of supported formats.
 
708
%
 
709
%  The format of the UnregisterTOPOLImage method is:
 
710
%
 
711
%      UnregisterTOPOLImage(void)
 
712
%
 
713
*/
 
714
ModuleExport void UnregisterTOPOLImage(void)
 
715
{
 
716
  (void) UnregisterMagickInfo("TOPOL");
 
717
}