File: gif.cpp

package info (click to toggle)
povray 1%3A3.6.1-12
  • links: PTS
  • area: non-free
  • in suites: lenny, squeeze
  • size: 31,084 kB
  • ctags: 20,310
  • sloc: ansic: 110,032; cpp: 86,573; sh: 13,595; pascal: 5,942; asm: 2,994; makefile: 1,753; ada: 1,637
file content (399 lines) | stat: -rw-r--r-- 8,840 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/****************************************************************************
 *                  gif.cpp
 *
 * Gif-format file reader.
 *
 * NOTE:  Portions of this module were written by Steve Bennett and are used
 *        here with his permission.
 *
 * from Persistence of Vision(tm) Ray Tracer version 3.6.
 * Copyright 1991-2003 Persistence of Vision Team
 * Copyright 2003-2004 Persistence of Vision Raytracer Pty. Ltd.
 *---------------------------------------------------------------------------
 * NOTICE: This source code file is provided so that users may experiment
 * with enhancements to POV-Ray and to port the software to platforms other
 * than those supported by the POV-Ray developers. There are strict rules
 * regarding how you are permitted to use this file. These rules are contained
 * in the distribution and derivative versions licenses which should have been
 * provided with this file.
 *
 * These licences may be found online, linked from the end-user license
 * agreement that is located at http://www.povray.org/povlegal.html
 *---------------------------------------------------------------------------
 * This program is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 *---------------------------------------------------------------------------
 * $File: //depot/povray/3.6-release/source/gif.cpp $
 * $Revision: #3 $
 * $Change: 3032 $
 * $DateTime: 2004/08/02 18:43:41 $
 * $Author: chrisc $
 * $Log$
 *****************************************************************************/

/*
 * The following routines were borrowed freely from FRACTINT, and represent
 * a generalized GIF file decoder.  This once seemed the best, most universal
 * format for reading in Bitmapped images, until Unisys began enforcing
 * its patent on the LZ compression that GIF uses.  POV-Ray, as freeware, is
 * exempt from GIF licensing fees.  GIF is a Copyright of Compuserve, Inc.
 *
 * Swiped and converted to entirely "C" coded routines by AAC for the most
 * in future portability!
 */

#include "frame.h"
#include "gif.h"
#include "gifdecod.h"
#include "povray.h"
#include "pov_util.h"
#include "fileinputoutput.h"

BEGIN_POV_NAMESPACE

USING_POV_BASE_NAMESPACE

/*****************************************************************************
* Local preprocessor defines
******************************************************************************/



/*****************************************************************************
* Local typedefs
******************************************************************************/



/*****************************************************************************
* Local variables
******************************************************************************/

static IMAGE *Current_Image; // GLOBAL VARIABLE
static int Bitmap_Line; // GLOBAL VARIABLE
static IStream *Bit_File; // GLOBAL VARIABLE
unsigned char *decoderline  /*  [2049] */ ;  /* write-line routines use this */ // GLOBAL VARIABLE

static IMAGE_COLOUR *gif_colour_map; // GLOBAL VARIABLE
static int colourmap_size; // GLOBAL VARIABLE



/*****************************************************************************
* Static functions
******************************************************************************/



/*****************************************************************************
*
* FUNCTION
*
*   out_line
*
* INPUT
*   
* OUTPUT
*   
* RETURNS
*   
* AUTHOR
*
*   POV-Ray Team
*   
* DESCRIPTION
*
*   -
*
* CHANGES
*
*   -
*
******************************************************************************/

int out_line (unsigned char *pixels, int linelen)
{
  register int x;
  register unsigned char *line;

  if (Bitmap_Line == Current_Image->iheight)
  {
    Warning (0, "Extra data at end of GIF image.");
    return (0) ;
  }

  line = Current_Image->data.map_lines[Bitmap_Line++];

  for (x = 0; x < linelen; x++)
  {
    if ((int)(*pixels) > Current_Image->Colour_Map_Size)
    {
      Error ("Error - GIF image map color out of range.");
    }

    line[x] = *pixels;

    pixels++;
  }

  return (0);
}




/*****************************************************************************
*
* FUNCTION
*
*   gif_get_byte
*
* INPUT
*   
* OUTPUT
*   
* RETURNS
*   
* AUTHOR
*
*   POV-Ray Team
*   
* DESCRIPTION
*
*   Get byte from file, return the next byte or an error.
*
* CHANGES
*
*   -
*
******************************************************************************/

int gif_get_byte()
{
  register int byte;

  if ((byte = Bit_File->Read_Byte ()) != EOF)
  {
    return (byte);
  }
  else
  {
    Error ("Error reading data from GIF image.");
  }

  /* Keep the compiler happy. */

  return(0);
}



/*****************************************************************************
*
* FUNCTION
*
*   Read_Gif_Image
*
* INPUT
*   
* OUTPUT
*   
* RETURNS
*   
* AUTHOR
*
*   POV-Ray Team
*   
* DESCRIPTION
*
*   Main GIF file decoder.
*
* CHANGES
*
*   -
*
******************************************************************************/

void Read_Gif_Image(IMAGE *Image, char *filename)
{
  register int i, j, status;
  unsigned finished, planes;
  unsigned char buffer[16];

  status = 0;

  Current_Image = Image;

  if ((Bit_File = Locate_File(filename, POV_File_Image_GIF,NULL,true)) == NULL)
  {
    Error ("Error opening GIF image.");
  }

  /* Get the screen description. */

  for (i = 0; i < 13; i++)
  {
    buffer[i] = (unsigned char)gif_get_byte();
  }

  /* Use updated GIF specs. */

  if (strncmp((char *) buffer,"GIF",3) == 0)  /* Allow only GIF87 and GIF89 */
  {
    if ((buffer[3] != '8') || ((buffer[4] != '7') && (buffer[4]) != '9') ||
        (buffer[5] < 'A') || (buffer[5]) > 'z')
    {
      Error ("Unsupported GIF version %c%c%c.",
             buffer[3], buffer[4], buffer[5]);
    }
  }
  else
  {
    Error ("File is not in GIF format.");
  }

  planes = ((unsigned)buffer[10] & 0x0F) + 1;

  colourmap_size = (int)(1 << planes);

  gif_colour_map = (IMAGE_COLOUR *)POV_CALLOC(colourmap_size, sizeof(IMAGE_COLOUR), "GIF color map");

  /* Color map (better be!) */

  if ((buffer[10] & 0x80) == 0)
  {
    Error ("Error in GIF color map.");
  }

  for (i = 0; i < colourmap_size ; i++)
  {
    gif_colour_map[i].Red    = (unsigned char)gif_get_byte();
    gif_colour_map[i].Green  = (unsigned char)gif_get_byte();
    gif_colour_map[i].Blue   = (unsigned char)gif_get_byte();
    gif_colour_map[i].Filter = 0;
    gif_colour_map[i].Transmit = 0;
  }

  /* Now display one or more GIF objects. */

  finished = false;

  while (!finished)
  {
    switch (gif_get_byte())
    {
      /* End of the GIF dataset. */

      case ';':

        finished = true;
        status = 0;

        break;

      /* GIF Extension Block. */

      case '!':

        /* Read (and ignore) the ID. */

        gif_get_byte();

        /* Get data len. */

        while ((i = gif_get_byte()) > 0)
        {
          for (j = 0; j < i; j++)
          {
            /* Flush data. */

            gif_get_byte();
          }
        }

        break;

      /* Start of image object. Get description. */

      case ',':

        for (i = 0; i < 9; i++)
        {
          /* EOF test (?).*/

          if ((j = gif_get_byte()) < 0)
          {
            status = -1;

            break;
          }

          buffer[i] = (unsigned char) j;
        }

        /* Check "interlaced" bit. */

        if (j & 0x40)
        {
          Error ("Interlacing in GIF image unsupported.");
        }

        if (status < 0)
        {
          finished = true;

          break;
        }

        Image->iwidth  = buffer[4] | (buffer[5] << 8);
        Image->iheight = buffer[6] | (buffer[7] << 8);

        Image->width = (DBL) Image->iwidth;
        Image->height = (DBL) Image->iheight;

        Bitmap_Line = 0;

        Image->Colour_Map_Size = colourmap_size;
        Image->Colour_Map = gif_colour_map;

        Image->data.map_lines = (unsigned char **)POV_MALLOC(Image->iheight * sizeof(unsigned char *), "GIF image");

        for (i = 0 ; i < Image->iheight ; i++)
        {
          Image->data.map_lines[i] = (unsigned char *)POV_CALLOC(Image->iwidth, sizeof(unsigned char), "GIF image line");
        }

        /* Setup the color palette for the image. */

        decoderline = (unsigned char *)POV_CALLOC(Image->iwidth, sizeof(unsigned char), "GIF decoder line");

        /* Put bytes in Buf. */

        status = decoder(Image->iwidth);

        POV_FREE (decoderline);

        decoderline = NULL;

        finished = true;

        break;

      default:

        status = -1;

        finished = true;

        break;
    }
  }

  if (Bit_File != NULL)
  {
     delete Bit_File;
  }
}

END_POV_NAMESPACE