1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright by The HDF Group. *
3 * Copyright by the Board of Trustees of the University of Illinois. *
4 * All rights reserved. *
5 * *
6 * This file is part of HDF. The full HDF copyright notice, including *
7 * terms governing use, modification, and redistribution, is contained in *
8 * the COPYING file, which can be found at the root of the source code *
9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14
15 /*
16 * hdfgifwr.c - handles writing of GIF files.
17 *
18 * Contains:
19 * hdfWriteGIF(fp, pic, ptype, w, h, rmap, gmap, bmap, numcols, colorstyle,
20 * comment)
21 *
22 * Note: slightly brain-damaged, in that it'll only write non-interlaced
23 * GIF files (in the interests of speed, or something)
24 *
25 */
26
27
28
29 /*****************************************************************
30 * Portions of this code Copyright (C) 1989 by Michael Mauldin.
31 * Permission is granted to use this file in whole or in
32 * part for any purpose, educational, recreational or commercial,
33 * provided that this copyright notice is retained unchanged.
34 * This software is available to all free of charge by anonymous
35 * FTP and in the UUNET archives.
36 *
37 *
38 * Authors: Michael Mauldin (mlm@cs.cmu.edu)
39 * David Rowley (mgardi@watdcsu.waterloo.edu)
40 *
41 * Based on: compress.c - File compression ala IEEE Computer, June 1984.
42 *
43 * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
44 * Jim McKie (decvax!mcvax!jim)
45 * Steve Davies (decvax!vax135!petsd!peora!srd)
46 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
47 * James A. Woods (decvax!ihnp4!ames!jaw)
48 * Joe Orost (decvax!vax135!petsd!joe)
49 *****************************************************************/
50
51
52 #include <stdio.h>
53 #include "gif.h"
54 #include <stdlib.h>
55 #include <string.h>
56
57 typedef BYTE byte;
58 typedef long int count_int;
59
60 /* indicies into conv24MB */
61 #define CONV24_8BIT 0
62 #define CONV24_24BIT 1
63 #define CONV24_SEP1 2
64 #define CONV24_LOCK 3
65 #define CONV24_SEP2 4
66 #define CONV24_FAST 5
67 #define CONV24_SLOW 6
68 #define CONV24_BEST 7
69 #define CONV24_MAX 8
70
71 /* values 'picType' can take */
72 #define PIC8 CONV24_8BIT
73 #define PIC24 CONV24_24BIT
74
75 /* MONO returns total intensity of r,g,b components */
76 #define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 5) /*.33R+ .5G+ .17B*/
77
78 static int Width, Height;
79 static int curx, cury;
80 static long CountDown;
81 static int Interlace;
82
83 #ifdef __STDC__
84 static void putword(int, FILE *);
85 static void compress(int, FILE *, byte *, int);
86 static void output(int);
87 static void cl_block(void);
88 static void cl_hash(count_int);
89 static void char_init(void);
90 static void char_out(int);
91 static void flush_char(void);
92 #else
93 static void putword(), compress(), output(), cl_block(), cl_hash();
94 static void char_init(), char_out(), flush_char();
95 #endif
96
97 static byte pc2nc[256],r1[256],g1[256],b1[256];
98
xvbzero(s,len)99 void xvbzero(s, len)
100 char *s;
101 int len;
102 {
103 for ( ; len>0; len--) *s++ = 0;
104 }
105
106 /*************************************************************/
hdfWriteGIF(fp,pic,ptype,w,h,rmap,gmap,bmap,pc2ncmap,numcols,colorstyle,BitsPerPixel)107 int hdfWriteGIF(fp, pic, ptype, w, h, rmap, gmap, bmap, pc2ncmap, numcols, colorstyle, BitsPerPixel)
108 FILE *fp;
109 byte *pic;
110 int ptype, w,h;
111 byte *rmap, *gmap, *bmap , *pc2ncmap;
112 int numcols, colorstyle;
113 int BitsPerPixel;
114 {
115 int RWidth, RHeight;
116 int LeftOfs, TopOfs;
117 int ColorMapSize, InitCodeSize, Background;
118 int i;
119 byte *pic8;
120 pic8 = pic;
121
122 Interlace = 0;
123 Background = 0;
124
125 for (i=0; i<256; i++) {
126 pc2nc[i] = pc2ncmap[i];
127 r1[i] = rmap[i];
128 g1[i] = gmap[i];
129 b1[i] = bmap[i];
130 }
131
132 ColorMapSize = 1 << BitsPerPixel;
133
134 RWidth = Width = w;
135 RHeight = Height = h;
136 LeftOfs = TopOfs = 0;
137
138 CountDown = w * h; /* # of pixels we'll be doing */
139
140 if (BitsPerPixel <= 1) InitCodeSize = 2;
141 else InitCodeSize = BitsPerPixel;
142
143 curx = cury = 0;
144
145 if (!fp) {
146 fprintf(stderr, "WriteGIF: file not open for writing\n" );
147 return (1);
148 }
149
150 compress(InitCodeSize+1, fp, pic8, w*h);
151
152
153 if (ferror(fp)) return -1;
154 return (0);
155 }
156
157
158
159
160 /******************************/
putword(w,fp)161 static void putword(w, fp)
162 int w;
163 FILE *fp;
164 {
165 /* writes a 16-bit integer in GIF order (LSB first) */
166
167 fputc(w &0xff, fp);
168
169 fputc((w>>8)&0xff,fp);
170 }
171
172
173
174
175 /***********************************************************************/
176 static unsigned long cur_accum = 0;
177 static int cur_bits = 0;
178
179 #define MAXCODE(n_bits) ( (1 << (n_bits)) - 1)
180 #define min(a,b) ((a>b) ? b : a)
181 #define XV_BITS 12 /* BITS was already defined on some systems */
182 #define MSDOS 1
183 #define HSIZE 5003 /* 80% occupancy */
184
185 typedef unsigned char char_type;
186 static int n_bits; /* number of bits/code */
187 static int maxbits = XV_BITS; /* user settable max # bits/code */
188 static int maxcode; /* maximum code, given n_bits */
189 static int maxmaxcode = 1 << XV_BITS; /* NEVER generate this */
190
191 static count_int htab [HSIZE];
192 static unsigned short codetab [HSIZE];
193 #define HashTabOf(i) htab[i]
194 #define CodeTabOf(i) codetab[i]
195
196 static int hsize = HSIZE; /* for dynamic table sizing */
197
198 /*
199 * To save much memory, we overlay the table used by compress() with those
200 * used by decompress(). The tab_prefix table is the same size and type
201 * as the codetab. The tab_suffix table needs 2**BITS characters. We
202 * get this from the beginning of htab. The output stack uses the rest
203 * of htab, and contains characters. There is plenty of room for any
204 * possible stack (stack used to be 8000 characters).
205 */
206
207 #define tab_prefixof(i) CodeTabOf(i)
208 #define tab_suffixof(i) ((char_type *)(htab))[i]
209 #define de_stack ((char_type *)&tab_suffixof(1<<XV_BITS))
210
211 static int free_ent = 0; /* first unused entry */
212
213 /*
214 * block compression parameters -- after all codes are used up,
215 * and compression rate changes, start over.
216 */
217 static int clear_flg = 0;
218
219 static long int in_count = 1; /* length of input */
220 static long int out_count = 0; /* # of codes output (for debugging) */
221
222 /*
223 * compress stdin to stdout
224 *
225 * Algorithm: use open addressing double hashing (no chaining) on the
226 * prefix code / next character combination. We do a variant of Knuth's
227 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
228 * secondary probe. Here, the modular division first probe is gives way
229 * to a faster exclusive-or manipulation. Also do block compression with
230 * an adaptive reset, whereby the code table is cleared when the compression
231 * ratio decreases, but after the table fills. The variable-length output
232 * codes are re-sized at this point, and a special CLEAR code is generated
233 * for the decompressor. Late addition: construct the table according to
234 * file size for noticeable speed improvement on small files. Please direct
235 * questions about this implementation to ames!jaw.
236 */
237
238 static int g_init_bits;
239 static FILE *g_outfile;
240
241 static int ClearCode;
242 static int EOFCode;
243
244 /********************************************************/
compress(init_bits,outfile,data,len)245 static void compress(init_bits, outfile, data, len)
246 int init_bits;
247 FILE *outfile;
248 byte *data;
249 int len;
250 {
251 register long fcode;
252 register int i = 0;
253 register int c;
254 register int ent;
255 register int disp;
256 register int hsize_reg;
257 register int hshift;
258
259 /*
260 * Set up the globals: g_init_bits - initial number of bits
261 * g_outfile - pointer to output file
262 */
263 g_init_bits = init_bits;
264 g_outfile = outfile;
265
266 /* initialize 'compress' globals */
267 maxbits = XV_BITS;
268 maxmaxcode = 1<<XV_BITS;
269 xvbzero((char *) htab, sizeof(htab));
270 xvbzero((char *) codetab, sizeof(codetab));
271 hsize = HSIZE;
272 free_ent = 0;
273 clear_flg = 0;
274 in_count = 1;
275 out_count = 0;
276 cur_accum = 0;
277 cur_bits = 0;
278
279
280 /*
281 * Set up the necessary values
282 */
283 out_count = 0;
284 clear_flg = 0;
285 in_count = 1;
286 maxcode = MAXCODE(n_bits = g_init_bits);
287
288 ClearCode = (1 << (init_bits - 1));
289 EOFCode = ClearCode + 1;
290 free_ent = ClearCode + 2;
291
292 char_init();
293 ent = pc2nc[*data++]; len--;
294
295 hshift = 0;
296 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
297 hshift++;
298 hshift = 8 - hshift; /* set hash code range bound */
299
300 hsize_reg = hsize;
301 cl_hash( (count_int) hsize_reg); /* clear hash table */
302
303 output(ClearCode);
304
305 while (len) {
306 c = pc2nc[*data++]; len--;
307 in_count++;
308
309 fcode = (long) ( ( (long) c << maxbits) + ent);
310 i = (((int) c << hshift) ^ ent); /* xor hashing */
311
312 if ( HashTabOf (i) == fcode ) {
313 ent = CodeTabOf (i);
314 continue;
315 }
316
317 else if ( (long)HashTabOf (i) < 0 ) /* empty slot */
318 goto nomatch;
319
320 disp = hsize_reg - i; /* secondary hash (after G. Knott) */
321 if ( i == 0 )
322 disp = 1;
323
324 probe:
325 if ( (i -= disp) < 0 )
326 i += hsize_reg;
327
328 if ( HashTabOf (i) == fcode ) {
329 ent = CodeTabOf (i);
330 continue;
331 }
332
333 if ( (long)HashTabOf (i) >= 0 )
334 goto probe;
335
336 nomatch:
337 output(ent);
338 out_count++;
339 ent = c;
340
341 if ( free_ent < maxmaxcode ) {
342 CodeTabOf (i) = free_ent++; /* code -> hashtable */
343 HashTabOf (i) = fcode;
344 }
345 else
346 cl_block();
347 }
348
349 /* Put out the final code */
350 output(ent);
351 out_count++;
352 output(EOFCode);
353 }
354
355
356 /*****************************************************************
357 * TAG( output )
358 *
359 * Output the given code.
360 * Inputs:
361 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
362 * that n_bits =< (long)wordsize - 1.
363 * Outputs:
364 * Outputs code to the file.
365 * Assumptions:
366 * Chars are 8 bits long.
367 * Algorithm:
368 * Maintain a BITS character long buffer (so that 8 codes will
369 * fit in it exactly). Use the VAX insv instruction to insert each
370 * code in turn. When the buffer fills up empty it and start over.
371 */
372
373 static
374 unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
375 0x001F, 0x003F, 0x007F, 0x00FF,
376 0x01FF, 0x03FF, 0x07FF, 0x0FFF,
377 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
378
output(code)379 static void output(code)
380 int code;
381 {
382 cur_accum &= masks[cur_bits];
383
384 if (cur_bits > 0)
385 cur_accum |= ((long)code << cur_bits);
386 else
387 cur_accum = code;
388
389 cur_bits += n_bits;
390
391 while( cur_bits >= 8 ) {
392 char_out( (unsigned int) (cur_accum & 0xff) );
393 cur_accum >>= 8;
394 cur_bits -= 8;
395 }
396
397 /*
398 * If the next entry is going to be too big for the code size,
399 * then increase it, if possible.
400 */
401
402 if (free_ent > maxcode || clear_flg) {
403
404 if( clear_flg ) {
405 maxcode = MAXCODE (n_bits = g_init_bits);
406 clear_flg = 0;
407 }
408 else {
409 n_bits++;
410 if ( n_bits == maxbits )
411 maxcode = maxmaxcode;
412 else
413 maxcode = MAXCODE(n_bits);
414 }
415 }
416
417 if( code == EOFCode ) {
418 /* At EOF, write the rest of the buffer */
419 while( cur_bits > 0 ) {
420 char_out( (unsigned int)(cur_accum & 0xff) );
421 cur_accum >>= 8;
422 cur_bits -= 8;
423 }
424
425 flush_char();
426
427 fflush( g_outfile );
428
429 #ifdef FOO
430 if( ferror( g_outfile ) )
431 FatalError("unable to write GIF file");
432 #endif
433 }
434 }
435
436
437 /********************************/
cl_block()438 static void cl_block () /* table clear for block compress */
439 {
440 /* Clear out the hash table */
441
442 cl_hash ( (count_int) hsize );
443 free_ent = ClearCode + 2;
444 clear_flg = 1;
445
446 output(ClearCode);
447 }
448
449
450 /********************************/
cl_hash(hsize)451 static void cl_hash(hsize) /* reset code table */
452 register count_int hsize;
453 {
454 register count_int *htab_p = htab+hsize;
455 register long i;
456 register long m1 = -1;
457
458 i = hsize - 16;
459 do { /* might use Sys V memset(3) here */
460 *(htab_p-16) = m1;
461 *(htab_p-15) = m1;
462 *(htab_p-14) = m1;
463 *(htab_p-13) = m1;
464 *(htab_p-12) = m1;
465 *(htab_p-11) = m1;
466 *(htab_p-10) = m1;
467 *(htab_p-9) = m1;
468 *(htab_p-8) = m1;
469 *(htab_p-7) = m1;
470 *(htab_p-6) = m1;
471 *(htab_p-5) = m1;
472 *(htab_p-4) = m1;
473 *(htab_p-3) = m1;
474 *(htab_p-2) = m1;
475 *(htab_p-1) = m1;
476 htab_p -= 16;
477 } while ((i -= 16) >= 0);
478
479 for ( i += 16; i > 0; i-- )
480 *--htab_p = m1;
481 }
482
483
484 /******************************************************************************
485 *
486 * GIF Specific routines
487 *
488 ******************************************************************************/
489
490 /*
491 * Number of characters so far in this 'packet'
492 */
493 static int a_count;
494
495 /*
496 * Set up the 'byte output' routine
497 */
char_init()498 static void char_init()
499 {
500 a_count = 0;
501 }
502
503 /*
504 * Define the storage for the packet accumulator
505 */
506 static char accum[ 256 ];
507
508 /*
509 * Add a character to the end of the current packet, and if it is 254
510 * characters, flush the packet to disk.
511 */
char_out(c)512 static void char_out(c)
513 int c;
514 {
515 accum[ a_count++ ] = c;
516 if( a_count >= 254 )
517 flush_char();
518 }
519
520 /*
521 * Flush the packet to disk, and reset the accumulator
522 */
flush_char()523 static void flush_char()
524 {
525 if( a_count > 0 ) {
526 fputc( a_count, g_outfile );
527 fwrite( accum, 1, a_count, g_outfile );
528 a_count = 0;
529 }
530 }
531
532
533
534