gd_gif_out.c 21 KB

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