summaryrefslogtreecommitdiffstats
path: root/src/gifenc.cpp
blob: 74d800eea364089c4b8035f74347e2e5a2162239 (plain)
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
/******************************************************************************
 *
 * 
 *
 *
 * Copyright (C) 1997-2000 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * All output generated with Doxygen is not covered by this license.
 *
 * The GIF compression code below is based on the file ppmtogif.c of the
 * netpbm package. The original copyright message follows:
 *
 * ---------------------------------------------------------------------------
 * 
 * Copyright (C) 1989 by Jef Poskanzer.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  This software is provided "as is" without express or
 * implied warranty.
 *
 * ---------------------------------------------------------------------------
 *
 * The Graphics Interchange Format(c) is the Copyright property of
 * CompuServe Incorporated.  GIF(sm) is a Service Mark property of
 * CompuServe Incorporated.
 */

#include "gifenc.h"

static const unsigned int masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
                                      0x001F, 0x003F, 0x007F, 0x00FF,
                                      0x01FF, 0x03FF, 0x07FF, 0x0FFF,
                                      0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };

GifEncoder::GifEncoder(Byte *rawBytes,Color *p,int w,int h, Byte d,
                       int t)
  : colorResolution(8),globalPaletteFlag(0x80),bits(12),
    maxMaxCode(1<<bits)
{
  width = w;
  height = h;
  depth = d;
  transIndex = t;
  palette = p;
  data = rawBytes;
  dataPtr = data;
}

GifEncoder::~GifEncoder()
{
}

void GifEncoder::writeGIF(QFile &fp)
{
  // Write the Magic header
  fp.writeBlock( transIndex < 0 ? "GIF87a" : "GIF89a", 6 );

  // Write the logical screen descriptor
  putWord( width,   fp );
  putWord( height,  fp );
  Byte pack = globalPaletteFlag | ((colorResolution-1)<<4) | (depth-1);
  putByte( pack,    fp );
  putByte( 0,       fp ); // the background color
  putByte( 0,       fp ); // no aspect ration defined

  // Write global color table
  int i; for ( i=0 ; i< (1<<depth) ; i++)
  {
    putByte(palette[i].red,  fp);
    putByte(palette[i].green,fp);
    putByte(palette[i].blue, fp);
  }
  
  if ( transIndex >= 0)
  {
    // Write graphic control extension (needed for GIF transparancy)
    putByte( 0x21, fp); // extension introducer
    putByte( 0xf9, fp); // graphic control label
    putByte(    4, fp); // block size
    putByte(    1, fp); // announce transparacy value
    putWord(    0, fp); // zero delay time
    putByte( transIndex, fp); // write transparant index
    putByte(    0, fp); // end block
  }
  
  // Write the image descriptor
  putByte(   0x2c, fp); // image separator
  putWord(      0, fp); // image left position
  putWord(      0, fp); // image top position
  putWord(  width, fp); // image width
  putWord( height, fp); // image height
  putByte(      0, fp); // no local color table, no interlacing
 
  // Write table based image data
  Byte initCodeSize = depth<=1 ? 2 : depth;
  putByte( initCodeSize, fp); // LZW Minimum Code Size
  compress( initCodeSize+1, fp);
  putByte( 0, fp); // end of blocks
 
  // Write GIF Trailer
  putByte( 0x3b, fp);
}

void GifEncoder::compress( int ibits, QFile &outfile )
{
  int i;
  int entry;

  initBits  = ibits;
  numPixels = width*height;
  dataPtr   = data;
  clearFlag = FALSE;
  nBits     = initBits;
  maxCode   = (1<<nBits) -1;
  ClearCode = (1 << (initBits - 1));
  EOFCode   = ClearCode + 1;
  freeEntry = ClearCode + 2;
  aCount    = 0;
  curAccum  = 0;
  curBits   = 0;

  entry = nextPixel();

  int hshift = 0;
  int fcode;
  for ( fcode = hashTableSize;  fcode < 65536L; fcode *= 2L ) ++hshift;
  hshift = 8 - hshift;                /* set hash code range bound */

  clearHashTable();                   /* clear hash table */

  writeCode( ClearCode,outfile );

  int c;
  while ( (c = nextPixel()) != EOF ) 
  {  
    fcode = (c << bits) + entry;
    i = (c << hshift) ^ entry;    /* xor hashing */

    bool found=FALSE;
    if (htab[i]==fcode) 
    {
      entry = codetab[i];
      found=TRUE;
    } 
    else if (htab[i]>=0)
    {
      int disp = hashTableSize - i;
      if (i==0) disp=1;
      do
      {
        if ((i-=disp)<0) i+=hashTableSize;
        if (htab[i]==fcode)
        {
          entry=codetab[i];
          found=TRUE;
        }
      } while (htab[i]>0 && !found);
    }
    if (!found)
    {
      writeCode( entry, outfile );
      entry = c;
      if ( freeEntry < maxMaxCode ) 
      {
        codetab[i] = freeEntry++; /* code -> hashtable */
        htab[i]    = fcode;
      } 
      else
      {
        clearHashTable();
        freeEntry = ClearCode + 2;
        clearFlag = TRUE;
        writeCode( ClearCode, outfile );
      }
    }
  }
  writeCode( entry, outfile );
  writeCode( EOFCode, outfile );
}

void GifEncoder::putWord( Word w, QFile &fp )
{
  fp.putch( w & 0xff );
  fp.putch( (w>>8) & 0xff );
}

void GifEncoder::putByte( Byte b, QFile &fp )
{
  fp.putch( b );
}

void GifEncoder::writeCode( int code, QFile &outfile )
{
  curAccum &= masks[ curBits ];

  if ( curBits > 0 ) 
  {
    curAccum |= (code << curBits);
  }
  else
  {
    curAccum = code;
  }

  curBits += nBits;

  while( curBits >= 8 ) 
  {
    writeChar( (Byte)(curAccum & 0xff),outfile );
    curAccum >>= 8;
    curBits -= 8;
  }

  /*
   * If the next entry is going to be too big for the code size,
   * then increase it, if possible.
   */
  if ( freeEntry > maxCode || clearFlag ) 
  {
    if( clearFlag ) 
    {
      nBits = initBits;
      maxCode = (1<<nBits)-1;
      clearFlag = FALSE;
    } 
    else 
    {
      ++nBits;
      if ( nBits == bits )
        maxCode = maxMaxCode;
      else
        maxCode = (1<<nBits)-1;
    }
  }

  if ( code == EOFCode ) 
  {
    /* At EOF, write the rest of the buffer.  */
    while( curBits > 0 ) 
    {
      writeChar( (Byte)(curAccum & 0xff), outfile );
      curAccum >>= 8;
      curBits -= 8;
    }

    writePacket(outfile);
  }
}

/*
 * Add a character to the end of the current packet, and if it is 254
 * characters, flush the packet to disk.
 */
void GifEncoder::writeChar( Byte c, QFile &fp )
{
  accum[ aCount++ ] = c;
  if( aCount >= 254 ) writePacket(fp);
}

/*
 * Flush the packet to disk, and reset the accumulator
 */
void GifEncoder::writePacket(QFile &fp)
{
  if ( aCount > 0 ) 
  {
    fp.putch( aCount );
    fp.writeBlock( (const char *)accum, aCount );
    aCount = 0;
  }
}

void GifEncoder::clearHashTable()          /* reset code table */
{
  int *htab_p = htab;
  int i; for (i=0;i<hashTableSize;i++) *htab_p++ = -1;
}