gd_gif_out.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "gd.h"
  6. /* Code drawn from ppmtogif.c, from the pbmplus package
  7. **
  8. ** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
  9. ** Lempel-Zim compression based on "compress".
  10. **
  11. ** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
  12. **
  13. ** Copyright (C) 1989 by Jef Poskanzer.
  14. **
  15. ** Permission to use, copy, modify, and distribute this software and its
  16. ** documentation for any purpose and without fee is hereby granted, provided
  17. ** that the above copyright notice appear in all copies and that both that
  18. ** copyright notice and this permission notice appear in supporting
  19. ** documentation. This software is provided "as is" without express or
  20. ** implied warranty.
  21. **
  22. ** The Graphics Interchange Format(c) is the Copyright property of
  23. ** CompuServe Incorporated. GIF(sm) is a Service Mark property of
  24. ** CompuServe Incorporated.
  25. */
  26. /*
  27. * a code_int must be able to hold 2**GIFBITS values of type int, and also -1
  28. */
  29. typedef int code_int;
  30. #ifdef SIGNED_COMPARE_SLOW
  31. typedef unsigned long int count_int;
  32. typedef unsigned short int count_short;
  33. #else /*SIGNED_COMPARE_SLOW*/
  34. typedef long int count_int;
  35. #endif /*SIGNED_COMPARE_SLOW*/
  36. /* 2.0.28: threadsafe */
  37. #define maxbits GIFBITS
  38. /* should NEVER generate this code */
  39. #define maxmaxcode ((code_int)1 << GIFBITS)
  40. #define HSIZE 5003 /* 80% occupancy */
  41. #define hsize HSIZE /* Apparently invariant, left over from
  42. compress */
  43. typedef struct {
  44. int Width, Height;
  45. int curx, cury;
  46. long CountDown;
  47. int Pass;
  48. int Interlace;
  49. int n_bits; /* number of bits/code */
  50. code_int maxcode; /* maximum code, given n_bits */
  51. count_int htab [HSIZE];
  52. unsigned short codetab [HSIZE];
  53. code_int free_ent; /* first unused entry */
  54. /*
  55. * block compression parameters -- after all codes are used up,
  56. * and compression rate changes, start over.
  57. */
  58. int clear_flg;
  59. int offset;
  60. long int in_count; /* length of input */
  61. long int out_count; /* # of codes output (for debugging) */
  62. int g_init_bits;
  63. gdIOCtx * g_outfile;
  64. int ClearCode;
  65. int EOFCode;
  66. unsigned long cur_accum;
  67. int cur_bits;
  68. /*
  69. * Number of characters so far in this 'packet'
  70. */
  71. int a_count;
  72. /*
  73. * Define the storage for the packet accumulator
  74. */
  75. char accum[ 256 ];
  76. } GifCtx;
  77. static int gifPutWord(int w, gdIOCtx *out);
  78. static int colorstobpp(int colors);
  79. static void BumpPixel (GifCtx *ctx);
  80. static int GIFNextPixel (gdImagePtr im, GifCtx *ctx);
  81. static void GIFEncode (gdIOCtxPtr fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im);
  82. static void compress (int init_bits, gdIOCtx *outfile, gdImagePtr im, GifCtx *ctx);
  83. static void output (code_int code, GifCtx *ctx);
  84. static void cl_block (GifCtx *ctx);
  85. static void cl_hash (register count_int chsize, GifCtx *ctx);
  86. static void char_init (GifCtx *ctx);
  87. static void char_out (int c, GifCtx *ctx);
  88. static void flush_char (GifCtx *ctx);
  89. void * gdImageGifPtr (gdImagePtr im, int *size)
  90. {
  91. void *rv;
  92. gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
  93. gdImageGifCtx (im, out);
  94. rv = gdDPExtractData (out, size);
  95. out->gd_free (out);
  96. return rv;
  97. }
  98. void gdImageGif (gdImagePtr im, FILE * outFile)
  99. {
  100. gdIOCtx *out = gdNewFileCtx (outFile);
  101. gdImageGifCtx (im, out);
  102. out->gd_free (out);
  103. }
  104. void gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
  105. {
  106. gdImagePtr pim = 0, tim = im;
  107. int interlace, BitsPerPixel;
  108. interlace = im->interlace;
  109. if (im->trueColor) {
  110. /* Expensive, but the only way that produces an
  111. acceptable result: mix down to a palette
  112. based temporary image. */
  113. pim = gdImageCreatePaletteFromTrueColor(im, 1, 256);
  114. if (!pim) {
  115. return;
  116. }
  117. tim = pim;
  118. }
  119. BitsPerPixel = colorstobpp(tim->colorsTotal);
  120. /* All set, let's do it. */
  121. GIFEncode(
  122. out, tim->sx, tim->sy, tim->interlace, 0, tim->transparent, BitsPerPixel,
  123. tim->red, tim->green, tim->blue, tim);
  124. if (pim) {
  125. /* Destroy palette based temporary image. */
  126. gdImageDestroy( pim);
  127. }
  128. }
  129. static int
  130. colorstobpp(int colors)
  131. {
  132. int bpp = 0;
  133. if ( colors <= 2 )
  134. bpp = 1;
  135. else if ( colors <= 4 )
  136. bpp = 2;
  137. else if ( colors <= 8 )
  138. bpp = 3;
  139. else if ( colors <= 16 )
  140. bpp = 4;
  141. else if ( colors <= 32 )
  142. bpp = 5;
  143. else if ( colors <= 64 )
  144. bpp = 6;
  145. else if ( colors <= 128 )
  146. bpp = 7;
  147. else if ( colors <= 256 )
  148. bpp = 8;
  149. return bpp;
  150. }
  151. /*****************************************************************************
  152. *
  153. * GIFENCODE.C - GIF Image compression interface
  154. *
  155. * GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
  156. * BitsPerPixel, Red, Green, Blue, gdImagePtr )
  157. *
  158. *****************************************************************************/
  159. #define TRUE 1
  160. #define FALSE 0
  161. /*
  162. * Bump the 'curx' and 'cury' to point to the next pixel
  163. */
  164. static void
  165. BumpPixel(GifCtx *ctx)
  166. {
  167. /*
  168. * Bump the current X position
  169. */
  170. ++(ctx->curx);
  171. /*
  172. * If we are at the end of a scan line, set curx back to the beginning
  173. * If we are interlaced, bump the cury to the appropriate spot,
  174. * otherwise, just increment it.
  175. */
  176. if( ctx->curx == ctx->Width ) {
  177. ctx->curx = 0;
  178. if( !ctx->Interlace )
  179. ++(ctx->cury);
  180. else {
  181. switch( ctx->Pass ) {
  182. case 0:
  183. ctx->cury += 8;
  184. if( ctx->cury >= ctx->Height ) {
  185. ++(ctx->Pass);
  186. ctx->cury = 4;
  187. }
  188. break;
  189. case 1:
  190. ctx->cury += 8;
  191. if( ctx->cury >= ctx->Height ) {
  192. ++(ctx->Pass);
  193. ctx->cury = 2;
  194. }
  195. break;
  196. case 2:
  197. ctx->cury += 4;
  198. if( ctx->cury >= ctx->Height ) {
  199. ++(ctx->Pass);
  200. ctx->cury = 1;
  201. }
  202. break;
  203. case 3:
  204. ctx->cury += 2;
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. /*
  211. * Return the next pixel from the image
  212. */
  213. static int
  214. GIFNextPixel(gdImagePtr im, GifCtx *ctx)
  215. {
  216. int r;
  217. if( ctx->CountDown == 0 )
  218. return EOF;
  219. --(ctx->CountDown);
  220. r = gdImageGetPixel(im, ctx->curx, ctx->cury);
  221. BumpPixel(ctx);
  222. return r;
  223. }
  224. /* public */
  225. static void
  226. GIFEncode(gdIOCtxPtr fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im)
  227. {
  228. int B;
  229. int RWidth, RHeight;
  230. int LeftOfs, TopOfs;
  231. int Resolution;
  232. int ColorMapSize;
  233. int InitCodeSize;
  234. int i;
  235. GifCtx ctx;
  236. memset(&ctx, 0, sizeof(ctx));
  237. ctx.Interlace = GInterlace;
  238. ctx.in_count = 1;
  239. ColorMapSize = 1 << BitsPerPixel;
  240. RWidth = ctx.Width = GWidth;
  241. RHeight = ctx.Height = GHeight;
  242. LeftOfs = TopOfs = 0;
  243. Resolution = BitsPerPixel;
  244. /*
  245. * Calculate number of bits we are expecting
  246. */
  247. ctx.CountDown = (long)ctx.Width * (long)ctx.Height;
  248. /*
  249. * Indicate which pass we are on (if interlace)
  250. */
  251. ctx.Pass = 0;
  252. /*
  253. * The initial code size
  254. */
  255. if( BitsPerPixel <= 1 )
  256. InitCodeSize = 2;
  257. else
  258. InitCodeSize = BitsPerPixel;
  259. /*
  260. * Set up the current x and y position
  261. */
  262. ctx.curx = ctx.cury = 0;
  263. /*
  264. * Write the Magic header
  265. */
  266. gdPutBuf(Transparent < 0 ? "GIF87a" : "GIF89a", 6, fp );
  267. /*
  268. * Write out the screen width and height
  269. */
  270. gifPutWord( RWidth, fp );
  271. gifPutWord( RHeight, fp );
  272. /*
  273. * Indicate that there is a global colour map
  274. */
  275. B = 0x80; /* Yes, there is a color map */
  276. /*
  277. * OR in the resolution
  278. */
  279. B |= (Resolution - 1) << 5;
  280. /*
  281. * OR in the Bits per Pixel
  282. */
  283. B |= (BitsPerPixel - 1);
  284. /*
  285. * Write it out
  286. */
  287. gdPutC( B, fp );
  288. /*
  289. * Write out the Background colour
  290. */
  291. gdPutC( Background, fp );
  292. /*
  293. * Byte of 0's (future expansion)
  294. */
  295. gdPutC( 0, fp );
  296. /*
  297. * Write out the Global Colour Map
  298. */
  299. for( i=0; i<ColorMapSize; ++i ) {
  300. gdPutC( Red[i], fp );
  301. gdPutC( Green[i], fp );
  302. gdPutC( Blue[i], fp );
  303. }
  304. /*
  305. * Write out extension for transparent colour index, if necessary.
  306. */
  307. if ( Transparent >= 0 ) {
  308. gdPutC( '!', fp );
  309. gdPutC( 0xf9, fp );
  310. gdPutC( 4, fp );
  311. gdPutC( 1, fp );
  312. gdPutC( 0, fp );
  313. gdPutC( 0, fp );
  314. gdPutC( (unsigned char) Transparent, fp );
  315. gdPutC( 0, fp );
  316. }
  317. /*
  318. * Write an Image separator
  319. */
  320. gdPutC( ',', fp );
  321. /*
  322. * Write the Image header
  323. */
  324. gifPutWord( LeftOfs, fp );
  325. gifPutWord( TopOfs, fp );
  326. gifPutWord( ctx.Width, fp );
  327. gifPutWord( ctx.Height, fp );
  328. /*
  329. * Write out whether or not the image is interlaced
  330. */
  331. if( ctx.Interlace )
  332. gdPutC( 0x40, fp );
  333. else
  334. gdPutC( 0x00, fp );
  335. /*
  336. * Write out the initial code size
  337. */
  338. gdPutC( InitCodeSize, fp );
  339. /*
  340. * Go and actually compress the data
  341. */
  342. compress( InitCodeSize+1, fp, im, &ctx );
  343. /*
  344. * Write out a Zero-length packet (to end the series)
  345. */
  346. gdPutC( 0, fp );
  347. /*
  348. * Write the GIF file terminator
  349. */
  350. gdPutC( ';', fp );
  351. }
  352. /***************************************************************************
  353. *
  354. * GIFCOMPR.C - GIF Image compression routines
  355. *
  356. * Lempel-Ziv compression based on 'compress'. GIF modifications by
  357. * David Rowley (mgardi@watdcsu.waterloo.edu)
  358. *
  359. ***************************************************************************/
  360. /*
  361. * General DEFINEs
  362. */
  363. #define GIFBITS 12
  364. #ifdef NO_UCHAR
  365. typedef char char_type;
  366. #else /*NO_UCHAR*/
  367. typedef unsigned char char_type;
  368. #endif /*NO_UCHAR*/
  369. /*
  370. *
  371. * GIF Image compression - modified 'compress'
  372. *
  373. * Based on: compress.c - File compression ala IEEE Computer, June 1984.
  374. *
  375. * By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
  376. * Jim McKie (decvax!mcvax!jim)
  377. * Steve Davies (decvax!vax135!petsd!peora!srd)
  378. * Ken Turkowski (decvax!decwrl!turtlevax!ken)
  379. * James A. Woods (decvax!ihnp4!ames!jaw)
  380. * Joe Orost (decvax!vax135!petsd!joe)
  381. *
  382. */
  383. #include <ctype.h>
  384. #define ARGVAL() (*++(*argv) || (--argc && *++argv))
  385. #ifdef COMPATIBLE /* But wrong! */
  386. # define MAXCODE(n_bits) ((code_int) 1 << (n_bits) - 1)
  387. #else /*COMPATIBLE*/
  388. # define MAXCODE(n_bits) (((code_int) 1 << (n_bits)) - 1)
  389. #endif /*COMPATIBLE*/
  390. #define HashTabOf(i) ctx->htab[i]
  391. #define CodeTabOf(i) ctx->codetab[i]
  392. /*
  393. * To save much memory, we overlay the table used by compress() with those
  394. * used by decompress(). The tab_prefix table is the same size and type
  395. * as the codetab. The tab_suffix table needs 2**GIFBITS characters. We
  396. * get this from the beginning of htab. The output stack uses the rest
  397. * of htab, and contains characters. There is plenty of room for any
  398. * possible stack (stack used to be 8000 characters).
  399. */
  400. #define tab_prefixof(i) CodeTabOf(i)
  401. #define tab_suffixof(i) ((char_type*)(htab))[i]
  402. #define de_stack ((char_type*)&tab_suffixof((code_int)1<<GIFBITS))
  403. /*
  404. * compress stdin to stdout
  405. *
  406. * Algorithm: use open addressing double hashing (no chaining) on the
  407. * prefix code / next character combination. We do a variant of Knuth's
  408. * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  409. * secondary probe. Here, the modular division first probe is gives way
  410. * to a faster exclusive-or manipulation. Also do block compression with
  411. * an adaptive reset, whereby the code table is cleared when the compression
  412. * ratio decreases, but after the table fills. The variable-length output
  413. * codes are re-sized at this point, and a special CLEAR code is generated
  414. * for the decompressor. Late addition: construct the table according to
  415. * file size for noticeable speed improvement on small files. Please direct
  416. * questions about this implementation to ames!jaw.
  417. */
  418. static void
  419. output(code_int code, GifCtx *ctx);
  420. static void
  421. compress(int init_bits, gdIOCtxPtr outfile, gdImagePtr im, GifCtx *ctx)
  422. {
  423. register long fcode;
  424. register code_int i /* = 0 */;
  425. register int c;
  426. register code_int ent;
  427. register code_int disp;
  428. register code_int hsize_reg;
  429. register int hshift;
  430. /*
  431. * Set up the globals: g_init_bits - initial number of bits
  432. * g_outfile - pointer to output file
  433. */
  434. ctx->g_init_bits = init_bits;
  435. ctx->g_outfile = outfile;
  436. /*
  437. * Set up the necessary values
  438. */
  439. ctx->offset = 0;
  440. ctx->out_count = 0;
  441. ctx->clear_flg = 0;
  442. ctx->in_count = 1;
  443. ctx->maxcode = MAXCODE(ctx->n_bits = ctx->g_init_bits);
  444. ctx->ClearCode = (1 << (init_bits - 1));
  445. ctx->EOFCode = ctx->ClearCode + 1;
  446. ctx->free_ent = ctx->ClearCode + 2;
  447. char_init(ctx);
  448. ent = GIFNextPixel( im, ctx );
  449. hshift = 0;
  450. for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
  451. ++hshift;
  452. hshift = 8 - hshift; /* set hash code range bound */
  453. hsize_reg = hsize;
  454. cl_hash( (count_int) hsize_reg, ctx ); /* clear hash table */
  455. output( (code_int)ctx->ClearCode, ctx );
  456. #ifdef SIGNED_COMPARE_SLOW
  457. while ( (c = GIFNextPixel( im )) != (unsigned) EOF ) {
  458. #else /*SIGNED_COMPARE_SLOW*/
  459. while ( (c = GIFNextPixel( im, ctx )) != EOF ) { /* } */
  460. #endif /*SIGNED_COMPARE_SLOW*/
  461. ++(ctx->in_count);
  462. fcode = (long) (((long) c << maxbits) + ent);
  463. i = (((code_int)c << hshift) ^ ent); /* xor hashing */
  464. if ( HashTabOf (i) == fcode ) {
  465. ent = CodeTabOf (i);
  466. continue;
  467. } else if ( (long)HashTabOf (i) < 0 ) /* empty slot */
  468. goto nomatch;
  469. disp = hsize_reg - i; /* secondary hash (after G. Knott) */
  470. if ( i == 0 )
  471. disp = 1;
  472. probe:
  473. if ( (i -= disp) < 0 )
  474. i += hsize_reg;
  475. if ( HashTabOf (i) == fcode ) {
  476. ent = CodeTabOf (i);
  477. continue;
  478. }
  479. if ( (long)HashTabOf (i) > 0 )
  480. goto probe;
  481. nomatch:
  482. output ( (code_int) ent, ctx );
  483. ++(ctx->out_count);
  484. ent = c;
  485. #ifdef SIGNED_COMPARE_SLOW
  486. if ( (unsigned) ctx->free_ent < (unsigned) maxmaxcode) {
  487. #else /*SIGNED_COMPARE_SLOW*/
  488. if ( ctx->free_ent < maxmaxcode ) { /* } */
  489. #endif /*SIGNED_COMPARE_SLOW*/
  490. CodeTabOf (i) = ctx->free_ent++; /* code -> hashtable */
  491. HashTabOf (i) = fcode;
  492. } else
  493. cl_block(ctx);
  494. }
  495. /*
  496. * Put out the final code.
  497. */
  498. output( (code_int)ent, ctx );
  499. ++(ctx->out_count);
  500. output( (code_int) ctx->EOFCode, ctx );
  501. }
  502. /*****************************************************************
  503. * TAG( output )
  504. *
  505. * Output the given code.
  506. * Inputs:
  507. * code: A n_bits-bit integer. If == -1, then EOF. This assumes
  508. * that n_bits =< (long)wordsize - 1.
  509. * Outputs:
  510. * Outputs code to the file.
  511. * Assumptions:
  512. * Chars are 8 bits long.
  513. * Algorithm:
  514. * Maintain a GIFBITS character long buffer (so that 8 codes will
  515. * fit in it exactly). Use the VAX insv instruction to insert each
  516. * code in turn. When the buffer fills up empty it and start over.
  517. */
  518. static const unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
  519. 0x001F, 0x003F, 0x007F, 0x00FF,
  520. 0x01FF, 0x03FF, 0x07FF, 0x0FFF,
  521. 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
  522. /* Arbitrary value to mark output is done. When we see EOFCode, then we don't
  523. * expect to see any more data. If we do (e.g. corrupt image inputs), cur_bits
  524. * might be negative, so flag it to return early.
  525. */
  526. #define CUR_BITS_FINISHED -1000
  527. static void
  528. output(code_int code, GifCtx *ctx)
  529. {
  530. if (ctx->cur_bits == CUR_BITS_FINISHED) {
  531. return;
  532. }
  533. ctx->cur_accum &= masks[ ctx->cur_bits ];
  534. if( ctx->cur_bits > 0 )
  535. ctx->cur_accum |= ((long)code << ctx->cur_bits);
  536. else
  537. ctx->cur_accum = code;
  538. ctx->cur_bits += ctx->n_bits;
  539. while( ctx->cur_bits >= 8 ) {
  540. char_out( (unsigned int)(ctx->cur_accum & 0xff), ctx );
  541. ctx->cur_accum >>= 8;
  542. ctx->cur_bits -= 8;
  543. }
  544. /*
  545. * If the next entry is going to be too big for the code size,
  546. * then increase it, if possible.
  547. */
  548. if ( ctx->free_ent > ctx->maxcode || ctx->clear_flg ) {
  549. if( ctx->clear_flg ) {
  550. ctx->maxcode = MAXCODE (ctx->n_bits = ctx->g_init_bits);
  551. ctx->clear_flg = 0;
  552. } else {
  553. ++(ctx->n_bits);
  554. if ( ctx->n_bits == maxbits )
  555. ctx->maxcode = maxmaxcode;
  556. else
  557. ctx->maxcode = MAXCODE(ctx->n_bits);
  558. }
  559. }
  560. if( code == ctx->EOFCode ) {
  561. /*
  562. * At EOF, write the rest of the buffer.
  563. */
  564. while( ctx->cur_bits > 0 ) {
  565. char_out( (unsigned int)(ctx->cur_accum & 0xff), ctx);
  566. ctx->cur_accum >>= 8;
  567. ctx->cur_bits -= 8;
  568. }
  569. /* Flag that it's done to prevent re-entry. */
  570. ctx->cur_bits = CUR_BITS_FINISHED;
  571. flush_char(ctx);
  572. }
  573. }
  574. /*
  575. * Clear out the hash table
  576. */
  577. static void
  578. cl_block (GifCtx *ctx) /* table clear for block compress */
  579. {
  580. cl_hash ( (count_int) hsize, ctx );
  581. ctx->free_ent = ctx->ClearCode + 2;
  582. ctx->clear_flg = 1;
  583. output( (code_int)ctx->ClearCode, ctx);
  584. }
  585. static void
  586. cl_hash(register count_int chsize, GifCtx *ctx) /* reset code table */
  587. {
  588. register count_int *htab_p = ctx->htab+chsize;
  589. register long i;
  590. register long m1 = -1;
  591. i = chsize - 16;
  592. do { /* might use Sys V memset(3) here */
  593. *(htab_p-16) = m1;
  594. *(htab_p-15) = m1;
  595. *(htab_p-14) = m1;
  596. *(htab_p-13) = m1;
  597. *(htab_p-12) = m1;
  598. *(htab_p-11) = m1;
  599. *(htab_p-10) = m1;
  600. *(htab_p-9) = m1;
  601. *(htab_p-8) = m1;
  602. *(htab_p-7) = m1;
  603. *(htab_p-6) = m1;
  604. *(htab_p-5) = m1;
  605. *(htab_p-4) = m1;
  606. *(htab_p-3) = m1;
  607. *(htab_p-2) = m1;
  608. *(htab_p-1) = m1;
  609. htab_p -= 16;
  610. } while ((i -= 16) >= 0);
  611. for ( i += 16; i > 0; --i )
  612. *--htab_p = m1;
  613. }
  614. /******************************************************************************
  615. *
  616. * GIF Specific routines
  617. *
  618. ******************************************************************************/
  619. /*
  620. * Set up the 'byte output' routine
  621. */
  622. static void
  623. char_init(GifCtx *ctx)
  624. {
  625. ctx->a_count = 0;
  626. }
  627. /*
  628. * Add a character to the end of the current packet, and if it is 254
  629. * characters, flush the packet to disk.
  630. */
  631. static void
  632. char_out(int c, GifCtx *ctx)
  633. {
  634. ctx->accum[ ctx->a_count++ ] = c;
  635. if( ctx->a_count >= 254 )
  636. flush_char(ctx);
  637. }
  638. /*
  639. * Flush the packet to disk, and reset the accumulator
  640. */
  641. static void
  642. flush_char(GifCtx *ctx)
  643. {
  644. if( ctx->a_count > 0 ) {
  645. gdPutC( ctx->a_count, ctx->g_outfile );
  646. gdPutBuf( ctx->accum, ctx->a_count, ctx->g_outfile );
  647. ctx->a_count = 0;
  648. }
  649. }
  650. static int gifPutWord(int w, gdIOCtx *out)
  651. {
  652. /* Byte order is little-endian */
  653. gdPutC(w & 0xFF, out);
  654. gdPutC((w >> 8) & 0xFF, out);
  655. return 0;
  656. }