View | Details | Raw Unified | Return to bug 53582
Collapse All | Expand All

(-)graphics/gd/Makefile (-27 / +23 lines)
Lines 6-57 Link Here
6
#
6
#
7
7
8
PORTNAME=	gd
8
PORTNAME=	gd
9
PORTVERSION=	1.8.4
9
PORTVERSION=	2.0.15
10
PORTREVISION=	6
11
CATEGORIES+=	graphics
10
CATEGORIES+=	graphics
12
MASTER_SITES=	http://www.boutell.com/gd/http/ \
11
MASTER_SITES=	http://www.boutell.com/gd/http/ \
13
		ftp://ftp.boutell.com/pub/boutell/gd/ \
12
		ftp://ftp.boutell.com/pub/boutell/gd/ \
14
		http://virtual-estates.net/~mi/port-stuff/ \
15
		${MASTER_SITE_RINGSERVER}
13
		${MASTER_SITE_RINGSERVER}
16
MASTER_SITE_SUBDIR=	graphics/gd
14
MASTER_SITE_SUBDIR=	graphics/gd
17
DISTFILES=	${DISTNAME}${EXTRACT_SUFX} gd_gif_in.c.bz2
18
EXTRACT_ONLY=	${DISTNAME}${EXTRACT_SUFX}
19
15
20
MAINTAINER?=	ports@FreeBSD.org
16
MAINTAINER?=	ports@FreeBSD.org
21
COMMENT?=	A graphics library for fast image creation
17
COMMENT?=	A graphics library for fast image creation
22
18
23
BUILD_DEPENDS=	freetype-config:${PORTSDIR}/print/freetype2     # XXX
19
LIB_DEPENDS=	jpeg.9:${PORTSDIR}/graphics/jpeg \
24
LIB_DEPENDS=	jpeg:${PORTSDIR}/graphics/jpeg \
20
		png.5:${PORTSDIR}/graphics/png \
25
		png:${PORTSDIR}/graphics/png \
21
		freetype.9:${PORTSDIR}/print/freetype2
26
		freetype:${PORTSDIR}/print/freetype2
27
.if !exists(/usr/bin/bzip2)
28
BUILD_DEPENDS+=	bzip2:${PORTSDIR}/archivers/bzip2
29
.endif
30
31
FREETYPE_CONFIG?=       ${LOCALBASE}/bin/freetype-config
32
22
33
USE_REINPLACE=	yes
23
MAKE_ENV=	WRKSRC="${WRKSRC}"
34
MAKE_ENV=	FREETYPE_CONFIG="${FREETYPE_CONFIG}"
24
MAKEFILE=	${FILESDIR}/Makefile.bsd
35
INSTALLS_SHLIB=	yes
25
INSTALLS_SHLIB=	yes
36
26
37
.if defined(WITH_X11)
27
.if !defined(WITHOUT_X11)
38
USE_XPM=	yes
28
USE_XPM=	yes
39
.endif
29
.endif
40
30
31
.if defined(HAVE_UNISYS_LICENSE) || \
32
    (defined(USA_RESIDENT) && ${USA_RESIDENT} == "YES")
33
WITH_LZW=	yes
34
.endif
35
36
.if defined(GD_FONTS)
37
USE_PERL5_BUILD=	yes
38
SCRIPTS_ENV+=	GD_FONTS="${GD_FONTS}"
39
.endif
40
41
pre-everything::
41
pre-everything::
42
.if !defined(WITH_X11)
42
.if !defined(WITH_LZW)
43
	@${ECHO_MSG}
43
	@${ECHO_MSG} "************************************************************"
44
	@${ECHO_MSG} "If you want to compile with Xpm support,"
44
	@${ECHO_MSG} "*      If you live outside US and Canada, you can define   *"
45
	@${ECHO_MSG} "hit Ctrl-C right now and use \"make WITH_X11=yes\""
45
	@${ECHO_MSG} "*      WITH_LZW to enable GIF compression support.         *"
46
	@${ECHO_MSG}
46
	@${ECHO_MSG} "************************************************************"
47
.endif
47
.endif
48
48
49
post-extract:
49
post-extract:
50
	@${BZIP2_CMD} -dc ${DISTDIR}/${DIST_SUBDIR}/gd_gif_in.c.bz2 \
50
	@${CP} ${FILESDIR}/g* ${WRKSRC}
51
		> ${WRKSRC}/gd_gif_in.c
52
53
post-patch:
54
	@${REINPLACE_CMD} -e 's|<malloc.h>|<stdlib.h>|g' ${WRKSRC}/gdcache.h
55
51
56
post-install:
52
post-install:
57
.if !defined(NOPORTDOCS)
53
.if !defined(NOPORTDOCS)
(-)graphics/gd/distinfo (-2 / +1 lines)
Lines 1-2 Link Here
1
MD5 (gd-1.8.4.tar.gz) = 2aef76e79f206f9798233f025f5dd28d
1
MD5 (gd-2.0.15.tar.gz) = 6ededf633b4fd054662ec123c7825fbb
2
MD5 (gd_gif_in.c.bz2) = 54132aea09471bb997672436dd9a1a96
(-)graphics/gd/files/Makefile.bsd (+60 lines)
Line 0 Link Here
1
PROGS!=	${MAKE} -V BIN_PROGRAMS -f ${WRKSRC}/makefile.sample
2
PROGS+=	gd2togif gdcmpgif giftogd2
3
PROGS+=	annotate	# new utility, not included in makefile.sample
4
TESTS=	gdtest gddemo gd2time gdtestft testac fontsizetest fontwheeltest
5
SRCS=	gd.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c gd_io_file.c \
6
	gd_io_ss.c gd_jpeg.c gd_png.c gd_ss.c gd_topal.c \
7
	gd_wbmp.c gdcache.c gdfontg.c gdfontl.c gdfontmb.c \
8
	gdfonts.c gdfontt.c gdft.c gdhelpers.c gdhelpers.h \
9
	gdkanji.c gdtables.c gdxpm.c jisx0208.h \
10
	wbmp.c wbmp.h
11
SRCS+=	gd_gif_in.c gd_gif_out.c gd_biggif_out.c gd_lzw_out.c
12
MAKEDIR=/usr/share/mk
13
LIB=gd
14
SHLIB_MAJOR=4
15
SHLIB_MINOR=0
16
INCS=	gd.h gd_io.h gdcache.h gdfontg.h gdfontl.h gdfontmb.h \
17
	gdfonts.h gdfontt.h
18
SHLIB_NAME!=	${MAKE} -V SHLIB_NAME LIB=${LIB} \
19
	SHLIB_MAJOR=${SHLIB_MAJOR} SHLIB_MINOR=${SHLIB_MINOR} -f ${MAKEDIR}/bsd.lib.mk
20
21
CFLAGS+=-I${.CURDIR} -I${LOCALBASE}/include/freetype2/ \
22
	-I${LOCALBASE}/include/freetype2/freetype -I${LOCALBASE}/include \
23
	-DHAVE_LIBPNG -DHAVE_LIBJPEG -DHAVE_LIBFREETYPE -DHAVE_LIBZ
24
LDADD=	-L${LOCALBASE}/lib -lpng -lz -ljpeg -lfreetype -lm
25
26
.ifndef WITHOUT_X11
27
CFLAGS+=-I${X11BASE}/include/X11 -I${X11BASE}/include -DHAVE_XPM
28
LDADD+=	-L${X11BASE}/lib -lXpm -lX11
29
.endif
30
31
.ifdef WITH_LZW
32
CFLAGS+=-DLZW_LICENCED
33
.endif
34
35
# The package comes with tests, but without any sort of test-harness,
36
# to run them all automaticly. So building tests is disabled here.	-mi
37
all: lib${LIB}.a ${SHLIB_NAME} ${PROGS} # ${TESTS}
38
39
lib${LIB}.a ${SHLIB_NAME}: ${SRCS}
40
	${MAKE} LIB=${LIB} SRCS="${SRCS}" \
41
		SHLIB_MAJOR=${SHLIB_MAJOR} SHLIB_MINOR=${SHLIB_MINOR} \
42
		CFLAGS="${CFLAGS}" -ECFLAGS LDADD="${LDADD}" \
43
		-f ${MAKEDIR}/bsd.lib.mk ${.TARGET}
44
45
${PROGS} ${TESTS}: ${SHLIB_NAME} lib${LIB}.a
46
	${MAKE} PROG=${.TARGET} NOMAN=1 LDADD="-L. -lgd" \
47
		CFLAGS="${CFLAGS}" -ECFLAGS -f ${MAKEDIR}/bsd.prog.mk
48
49
install:
50
	mkdir -p ${PREFIX}/include
51
	${MAKE} LIB=${LIB} LIBDIR=${PREFIX}/lib NOPROFILE=true \
52
		SHLIB_MAJOR=${SHLIB_MAJOR} SHLIB_MINOR=${SHLIB_MINOR} \
53
		-f ${MAKEDIR}/bsd.lib.mk install
54
	cd ${.CURDIR} && ${BSD_INSTALL_DATA} ${INCS} ${PREFIX}/include
55
	cd ${.CURDIR} && ${BSD_INSTALL_PROGRAM} ${PROGS} ${PREFIX}/bin/
56
	${BSD_INSTALL_SCRIPT} ${.CURDIR}/bdftogd ${PREFIX}/bin/
57
58
.if exists(${.CURDIR}/Makefile.fonts)
59
.include "Makefile.fonts"
60
.endif
(-)graphics/gd/files/gd2togif.c (+40 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include "gd.h"
3
4
/* A short program which converts a .gif file into a .gd file, for
5
	your convenience in creating images on the fly from a
6
	basis image that must be loaded quickly. The .gd format
7
	is not intended to be a general-purpose format. */
8
9
int main(int argc, char **argv)
10
{
11
	gdImagePtr im;
12
	FILE *in, *out;
13
	if (argc != 3) {
14
		fprintf(stderr, "Usage: gd2togif filename.gd2 filename.gif\n");
15
		exit(1);
16
	}
17
	in = fopen(argv[1], "rb");
18
	if (!in) {
19
		fprintf(stderr, "Input file does not exist!\n");
20
		exit(1);
21
	}
22
	im = gdImageCreateFromGd2(in);
23
	fclose(in);
24
	if (!im) {
25
		fprintf(stderr, "Input is not in GIF format!\n");
26
		exit(1);
27
	}
28
	out = fopen(argv[2], "wb");
29
	if (!out) {
30
		fprintf(stderr, "Output file cannot be written to!\n");
31
		gdImageDestroy(im);
32
		exit(1);	
33
	}
34
	gdImageGif(im, out);
35
	fclose(out);
36
	gdImageDestroy(im);
37
38
	return 0;
39
}
40
(-)graphics/gd/files/gd_biggif_out.c (+823 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <math.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include "gd.h"
6
7
/*
8
** Wrapper functions for this module.
9
*/
10
11
void gdImageBigGif(gdImagePtr im, FILE *outFile)
12
{
13
        gdIOCtx   *out = gdNewFileCtx(outFile);
14
        gdImageBigGifCtx(im, out);
15
	out->gd_free(out);
16
}
17
18
void* gdImageBigGifPtr(gdImagePtr im, int *size)
19
{
20
	void	*rv;
21
        gdIOCtx   *out = gdNewDynamicCtx(2048, NULL);
22
        gdImageBigGifCtx(im, out);
23
	rv = gdDPExtractData(out,size);
24
 	out->gd_free(out);
25
	return rv;
26
}
27
28
/* Code drawn from ppmtogif.c, from the pbmplus package
29
**
30
** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
31
** Lempel-Zim compression based on "compress".
32
**
33
** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
34
**
35
** Copyright (C) 1989 by Jef Poskanzer.
36
**
37
** Permission to use, copy, modify, and distribute this software and its
38
** documentation for any purpose and without fee is hereby granted, provided
39
** that the above copyright notice appear in all copies and that both that
40
** copyright notice and this permission notice appear in supporting
41
** documentation.  This software is provided "as is" without express or
42
** implied warranty.
43
**
44
** The Graphics Interchange Format(c) is the Copyright property of
45
** CompuServe Incorporated.  GIF(sm) is a Service Mark property of
46
** CompuServe Incorporated.
47
*
48
*  Heavily modified by Mouse, 1998-02-12.  
49
*  Remove LZW compression.
50
*  Added miGIF run length compression.
51
*
52
*/
53
54
/*
55
 * a code_int must be able to hold 2**GIFBITS values of type int, and also -1
56
 */
57
typedef int code_int;
58
59
static int colorstobpp(int colors);
60
static void BumpPixel (void);
61
static int GIFNextPixel (gdImagePtr im);
62
static void GIFEncode (gdIOCtx *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im);
63
/*static void Putword (int w, gdIOCtx *fp); */
64
static void GIFcompress (int, gdIOCtx *, gdImagePtr, int);
65
static void output (code_int code);
66
67
/* UNUSED
68
* static void char_init (void);
69
* static void char_out (int c);
70
*/
71
72
/* Allows for reuse */
73
static void init_statics(void);
74
75
void gdImageBigGifCtx(gdImagePtr im, gdIOCtx *out)
76
{
77
	int interlace, transparent, BitsPerPixel;
78
79
	interlace = im->interlace;
80
	transparent = im->transparent;
81
82
	BitsPerPixel = colorstobpp(im->colorsTotal);
83
	/* Clear any old values in statics strewn through the GIF code */
84
	init_statics();
85
	/* All set, let's do it. */
86
	GIFEncode(
87
		out, im->sx, im->sy, interlace, 0, transparent, BitsPerPixel,
88
		im->red, im->green, im->blue, im);
89
}
90
91
static int
92
colorstobpp(int colors)
93
{
94
    int bpp = 0;
95
96
    if ( colors <= 2 )
97
        bpp = 1;
98
    else if ( colors <= 4 )
99
        bpp = 2;
100
    else if ( colors <= 8 )
101
        bpp = 3;
102
    else if ( colors <= 16 )
103
        bpp = 4;
104
    else if ( colors <= 32 )
105
        bpp = 5;
106
    else if ( colors <= 64 )
107
        bpp = 6;
108
    else if ( colors <= 128 )
109
        bpp = 7;
110
    else if ( colors <= 256 )
111
        bpp = 8;
112
    return bpp;
113
    }
114
115
/*****************************************************************************
116
 *
117
 * GIFENCODE.C    - GIF Image compression interface
118
 *
119
 * GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
120
 *            BitsPerPixel, Red, Green, Blue, gdImagePtr )
121
 *
122
 *****************************************************************************/
123
124
#define TRUE 1
125
#define FALSE 0
126
127
static int Width, Height;
128
static int curx, cury;
129
static long CountDown;
130
static int Pass = 0;
131
static int Interlace;
132
133
/*
134
 * Bump the 'curx' and 'cury' to point to the next pixel
135
 */
136
static void
137
BumpPixel(void)
138
{
139
        /*
140
         * Bump the current X position
141
         */
142
        ++curx;
143
144
        /*
145
         * If we are at the end of a scan line, set curx back to the beginning
146
         * If we are interlaced, bump the cury to the appropriate spot,
147
         * otherwise, just increment it.
148
         */
149
        if( curx == Width ) {
150
                curx = 0;
151
152
                if( !Interlace )
153
                        ++cury;
154
                else {
155
                     switch( Pass ) {
156
157
                       case 0:
158
                          cury += 8;
159
                          if( cury >= Height ) {
160
                                ++Pass;
161
                                cury = 4;
162
                          }
163
                          break;
164
165
                       case 1:
166
                          cury += 8;
167
                          if( cury >= Height ) {
168
                                ++Pass;
169
                                cury = 2;
170
                          }
171
                          break;
172
173
                       case 2:
174
                          cury += 4;
175
                          if( cury >= Height ) {
176
                             ++Pass;
177
                             cury = 1;
178
                          }
179
                          break;
180
181
                       case 3:
182
                          cury += 2;
183
                          break;
184
                        }
185
                }
186
        }
187
}
188
189
/*
190
 * Return the next pixel from the image
191
 */
192
static int
193
GIFNextPixel(gdImagePtr im)
194
{
195
        int r;
196
197
        if( CountDown == 0 )
198
                return EOF;
199
200
        --CountDown;
201
202
        r = gdImageGetPixel(im, curx, cury);
203
204
        BumpPixel();
205
206
        return r;
207
}
208
209
/* public */
210
211
static void
212
GIFEncode(gdIOCtx *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im)
213
{
214
        int B;
215
        int RWidth, RHeight;
216
        int LeftOfs, TopOfs;
217
        int Resolution;
218
        int ColorMapSize;
219
        int InitCodeSize;
220
        int i;
221
222
        Interlace = GInterlace;
223
224
        ColorMapSize = 1 << BitsPerPixel;
225
226
        RWidth = Width = GWidth;
227
        RHeight = Height = GHeight;
228
        LeftOfs = TopOfs = 0;
229
230
        Resolution = BitsPerPixel;
231
232
        /*
233
         * Calculate number of bits we are expecting
234
         */
235
        CountDown = (long)Width * (long)Height;
236
237
        /*
238
         * Indicate which pass we are on (if interlace)
239
         */
240
        Pass = 0;
241
242
        /*
243
         * The initial code size
244
         */
245
        if( BitsPerPixel <= 1 )
246
                InitCodeSize = 2;
247
        else
248
                InitCodeSize = BitsPerPixel;
249
250
        /*
251
         * Set up the current x and y position
252
         */
253
        curx = cury = 0;
254
255
        /*
256
         * Write the Magic header
257
         */
258
        gdPutBuf( Transparent < 0 ? "GIF87a" : "GIF89a", 6, fp );
259
260
        /*
261
         * Write out the screen width and height
262
         */
263
        Putword( RWidth, fp );
264
        Putword( RHeight, fp );
265
266
        /*
267
         * Indicate that there is a global colour map
268
         */
269
        B = 0x80;       /* Yes, there is a color map */
270
271
        /*
272
         * OR in the resolution
273
         */
274
        B |= (Resolution - 1) << 4;
275
276
        /*
277
         * OR in the Bits per Pixel
278
         */
279
        B |= (BitsPerPixel - 1);
280
281
        /*
282
         * Write it out
283
         */
284
        gdPutC( B, fp );
285
286
        /*
287
         * Write out the Background colour
288
         */
289
        gdPutC( Background, fp );
290
291
        /*
292
         * Byte of 0's (future expansion)
293
         */
294
        gdPutC( 0, fp );
295
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
	/*
306
	 * Write out extension for transparent colour index, if necessary.
307
	 */
308
	if ( Transparent >= 0 ) {
309
	    gdPutC( '!', fp );
310
	    gdPutC( 0xf9, fp );
311
	    gdPutC( 4, fp );
312
	    gdPutC( 1, fp );
313
	    gdPutC( 0, fp );
314
	    gdPutC( 0, fp );
315
	    gdPutC( (unsigned char) Transparent, fp );
316
	    gdPutC( 0, fp );
317
	}
318
319
        /*
320
         * Write an Image separator
321
         */
322
        gdPutC( ',', fp );
323
324
        /*
325
         * Write the Image header
326
         */
327
328
        Putword( LeftOfs, fp );
329
        Putword( TopOfs, fp );
330
        Putword( Width, fp );
331
        Putword( Height, fp );
332
333
        /*
334
         * Write out whether or not the image is interlaced
335
         */
336
        if( Interlace )
337
                gdPutC( 0x40, fp );
338
        else
339
                gdPutC( 0x00, fp );
340
341
        /*
342
         * Write out the initial code size
343
         */
344
        gdPutC( InitCodeSize, fp );
345
346
        /*
347
         * Go and actually compress the data
348
         */
349
        GIFcompress( InitCodeSize+1, fp, im, Background );
350
351
        /*
352
         * Write out a Zero-length packet (to end the series)
353
         */
354
        gdPutC( 0, fp );
355
356
        /*
357
         * Write the GIF file terminator
358
         */
359
        gdPutC( ';', fp );
360
}
361
362
/* Write out a word to the GIF file */
363
/*static void */
364
/*Putword(int w, gdIOCtx *fp) */
365
/*{ */
366
/*        fputc( w & 0xff, fp ); */
367
/*        fputc( (w / 256) & 0xff, fp ); */
368
/*} */
369
370
#define GIFBITS 12
371
372
/*-----------------------------------------------------------------------
373
 *
374
 * miGIF Compression - mouse and ivo's GIF-compatible compression
375
 *
376
 *          -run length encoding compression routines-
377
 *
378
 * Copyright (C) 1998 Hutchison Avenue Software Corporation
379
 *               http://www.hasc.com
380
 *               info@hasc.com
381
 *
382
 * Permission to use, copy, modify, and distribute this software and its
383
 * documentation for any purpose and without fee is hereby granted, provided
384
 * that the above copyright notice appear in all copies and that both that
385
 * copyright notice and this permission notice appear in supporting
386
 * documentation.  This software is provided "AS IS." The Hutchison Avenue 
387
 * Software Corporation disclaims all warranties, either express or implied, 
388
 * including but not limited to implied warranties of merchantability and 
389
 * fitness for a particular purpose, with respect to this code and accompanying
390
 * documentation. 
391
 * 
392
 * The miGIF compression routines do not, strictly speaking, generate files 
393
 * conforming to the GIF spec, since the image data is not LZW-compressed 
394
 * (this is the point: in order to avoid transgression of the Unisys patent 
395
 * on the LZW algorithm.)  However, miGIF generates data streams that any 
396
 * reasonably sane LZW decompresser will decompress to what we want.
397
 *
398
 * miGIF compression uses run length encoding. It compresses horizontal runs 
399
 * of pixels of the same color. This type of compression gives good results
400
 * on images with many runs, for example images with lines, text and solid 
401
 * shapes on a solid-colored background. It gives little or no compression 
402
 * on images with few runs, for example digital or scanned photos.
403
 *
404
 *                               der Mouse
405
 *                      mouse@rodents.montreal.qc.ca
406
 *            7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B
407
 *
408
 *                             ivo@hasc.com
409
 *
410
 * The Graphics Interchange Format(c) is the Copyright property of
411
 * CompuServe Incorporated.  GIF(sm) is a Service Mark property of
412
 * CompuServe Incorporated.
413
 *
414
 */
415
416
static int rl_pixel;
417
static int rl_basecode;
418
static int rl_count;
419
static int rl_table_pixel;
420
static int rl_table_max;
421
static int just_cleared;
422
static int out_bits;
423
static int out_bits_init;
424
static int out_count;
425
static int out_bump;
426
static int out_bump_init;
427
static int out_clear;
428
static int out_clear_init;
429
static int max_ocodes;
430
static int code_clear;
431
static int code_eof;
432
static unsigned int obuf;
433
static int obits;
434
static gdIOCtx *ofile;
435
static unsigned char oblock[256];
436
static int oblen;
437
438
/* Used only when debugging GIF compression code */
439
/* #define DEBUGGING_ENVARS */
440
441
#ifdef DEBUGGING_ENVARS
442
443
static int verbose_set = 0;
444
static int verbose;
445
#define VERBOSE (verbose_set?verbose:set_verbose())
446
447
static int set_verbose(void)
448
{
449
 verbose = !!getenv("GIF_VERBOSE");
450
 verbose_set = 1;
451
 return(verbose);
452
}
453
454
#else
455
456
#define VERBOSE 0
457
458
#endif
459
460
461
static const char *binformat(unsigned int v, int nbits)
462
{
463
 static char bufs[8][64];
464
 static int bhand = 0;
465
 unsigned int bit;
466
 int bno;
467
 char *bp;
468
469
 bhand --;
470
 if (bhand < 0) bhand = (sizeof(bufs)/sizeof(bufs[0]))-1;
471
 bp = &bufs[bhand][0];
472
 for (bno=nbits-1,bit=1U<<bno;bno>=0;bno--,bit>>=1)
473
  { *bp++ = (v & bit) ? '1' : '0';
474
    if (((bno&3) == 0) && (bno != 0)) *bp++ = '.';
475
  }
476
 *bp = '\0';
477
 return(&bufs[bhand][0]);
478
}
479
480
static void write_block(void)
481
{
482
 int i;
483
484
 if (VERBOSE)
485
  { printf("write_block %d:",oblen);
486
    for (i=0;i<oblen;i++) printf(" %02x",oblock[i]);
487
    printf("\n");
488
  }
489
 gdPutC(oblen,ofile);
490
 gdPutBuf(&oblock[0],oblen,ofile);
491
 oblen = 0;
492
}
493
494
static void block_out(unsigned char c)
495
{
496
 if (VERBOSE) printf("block_out %s\n",binformat(c,8));
497
 oblock[oblen++] = c;
498
 if (oblen >= 255) write_block();
499
}
500
501
static void block_flush(void)
502
{
503
 if (VERBOSE) printf("block_flush\n");
504
 if (oblen > 0) write_block();
505
}
506
507
static void output(int val)
508
{
509
 if (VERBOSE) printf("output %s [%s %d %d]\n",binformat(val,out_bits),binformat(obuf,obits),obits,out_bits);
510
 obuf |= val << obits;
511
 obits += out_bits;
512
 while (obits >= 8)
513
  { block_out(obuf&0xff);
514
    obuf >>= 8;
515
    obits -= 8;
516
  }
517
 if (VERBOSE) printf("output leaving [%s %d]\n",binformat(obuf,obits),obits);
518
}
519
520
static void output_flush(void)
521
{
522
 if (VERBOSE) printf("output_flush\n");
523
 if (obits > 0) block_out(obuf);
524
 block_flush();
525
}
526
527
static void did_clear(void)
528
{
529
 if (VERBOSE) printf("did_clear\n");
530
 out_bits = out_bits_init;
531
 out_bump = out_bump_init;
532
 out_clear = out_clear_init;
533
 out_count = 0;
534
 rl_table_max = 0;
535
 just_cleared = 1;
536
}
537
538
static void output_plain(int c)
539
{
540
 if (VERBOSE) printf("output_plain %s\n",binformat(c,out_bits));
541
 just_cleared = 0;
542
 output(c);
543
 out_count ++;
544
 if (out_count >= out_bump)
545
  { out_bits ++;
546
    out_bump += 1 << (out_bits - 1);
547
  }
548
 if (out_count >= out_clear)
549
  { output(code_clear);
550
    did_clear();
551
  }
552
}
553
554
static unsigned int isqrt(unsigned int x)
555
{
556
 unsigned int r;
557
 unsigned int v;
558
559
 if (x < 2) return(x);
560
 for (v=x,r=1;v;v>>=2,r<<=1) ;
561
 while (1)
562
  { v = ((x / r) + r) / 2;
563
    if ((v == r) || (v == r+1)) return(r);
564
    r = v;
565
  }
566
}
567
568
static unsigned int compute_triangle_count(unsigned int count, unsigned int nrepcodes)
569
{
570
 unsigned int perrep;
571
 unsigned int cost;
572
573
 cost = 0;
574
 perrep = (nrepcodes * (nrepcodes+1)) / 2;
575
 while (count >= perrep)
576
  { cost += nrepcodes;
577
    count -= perrep;
578
  }
579
 if (count > 0)
580
  { unsigned int n;
581
    n = isqrt(count);
582
    while ((n*(n+1)) >= 2*count) n --;
583
    while ((n*(n+1)) < 2*count) n ++;
584
    cost += n;
585
  }
586
 return(cost);
587
}
588
589
static void max_out_clear(void)
590
{
591
 out_clear = max_ocodes;
592
}
593
594
static void reset_out_clear(void)
595
{
596
 out_clear = out_clear_init;
597
 if (out_count >= out_clear)
598
  { output(code_clear);
599
    did_clear();
600
  }
601
}
602
603
static void rl_flush_fromclear(int count)
604
{
605
 int n;
606
607
 if (VERBOSE) printf("rl_flush_fromclear %d\n",count);
608
 max_out_clear();
609
 rl_table_pixel = rl_pixel;
610
 n = 1;
611
 while (count > 0)
612
  { if (n == 1)
613
     { rl_table_max = 1;
614
       output_plain(rl_pixel);
615
       count --;
616
     }
617
    else if (count >= n)
618
     { rl_table_max = n;
619
       output_plain(rl_basecode+n-2);
620
       count -= n;
621
     }
622
    else if (count == 1)
623
     { rl_table_max ++;
624
       output_plain(rl_pixel);
625
       count = 0;
626
     }
627
    else
628
     { rl_table_max ++;
629
       output_plain(rl_basecode+count-2);
630
       count = 0;
631
     }
632
    if (out_count == 0) n = 1; else n ++;
633
  }
634
 reset_out_clear();
635
 if (VERBOSE) printf("rl_flush_fromclear leaving table_max=%d\n",rl_table_max);
636
}
637
638
static void rl_flush_clearorrep(int count)
639
{
640
 int withclr;
641
642
 if (VERBOSE) printf("rl_flush_clearorrep %d\n",count);
643
 withclr = 1 + compute_triangle_count(count,max_ocodes);
644
 if (withclr < count)
645
  { output(code_clear);
646
    did_clear();
647
    rl_flush_fromclear(count);
648
  }
649
 else
650
  { for (;count>0;count--) output_plain(rl_pixel);
651
  }
652
}
653
654
static void rl_flush_withtable(int count)
655
{
656
 int repmax;
657
 int repleft;
658
 int leftover;
659
660
 if (VERBOSE) printf("rl_flush_withtable %d\n",count);
661
 repmax = count / rl_table_max;
662
 leftover = count % rl_table_max;
663
 repleft = (leftover ? 1 : 0);
664
 if (out_count+repmax+repleft > max_ocodes)
665
  { repmax = max_ocodes - out_count;
666
    leftover = count - (repmax * rl_table_max);
667
    repleft = 1 + compute_triangle_count(leftover,max_ocodes);
668
  }
669
 if (VERBOSE) printf("rl_flush_withtable repmax=%d leftover=%d repleft=%d\n",repmax,leftover,repleft);
670
 if (1+compute_triangle_count(count,max_ocodes) < repmax+repleft)
671
  { output(code_clear);
672
    did_clear();
673
    rl_flush_fromclear(count);
674
    return;
675
  }
676
 max_out_clear();
677
 for (;repmax>0;repmax--) output_plain(rl_basecode+rl_table_max-2);
678
 if (leftover)
679
  { if (just_cleared)
680
     { rl_flush_fromclear(leftover);
681
     }
682
    else if (leftover == 1)
683
     { output_plain(rl_pixel);
684
     }
685
    else
686
     { output_plain(rl_basecode+leftover-2);
687
     }
688
  }
689
 reset_out_clear();
690
}
691
692
static void rl_flush(void)
693
{
694
 /* UNUSED int table_reps; */
695
 /* UNUSED int table_extra; */
696
697
 if (VERBOSE) printf("rl_flush [ %d %d\n",rl_count,rl_pixel);
698
 if (rl_count == 1)
699
  { output_plain(rl_pixel);
700
    rl_count = 0;
701
    if (VERBOSE) printf("rl_flush ]\n");
702
    return;
703
  }
704
 if (just_cleared)
705
  { rl_flush_fromclear(rl_count);
706
  }
707
 else if ((rl_table_max < 2) || (rl_table_pixel != rl_pixel))
708
  { rl_flush_clearorrep(rl_count);
709
  }
710
 else
711
  { rl_flush_withtable(rl_count);
712
  }
713
 if (VERBOSE) printf("rl_flush ]\n");
714
 rl_count = 0;
715
}
716
717
static void GIFcompress(int init_bits, gdIOCtx *outfile, gdImagePtr im, int background)
718
{
719
 int c;
720
721
 ofile = outfile;
722
 obuf = 0;
723
 obits = 0;
724
 oblen = 0;
725
 code_clear = 1 << (init_bits - 1);
726
 code_eof = code_clear + 1;
727
 rl_basecode = code_eof + 1;
728
 out_bump_init = (1 << (init_bits - 1)) - 1;
729
 /* for images with a lot of runs, making out_clear_init larger will
730
    give better compression. */ 
731
 out_clear_init = (init_bits <= 3) ? 9 : (out_bump_init-1);
732
#ifdef DEBUGGING_ENVARS
733
  { const char *ocienv;
734
    ocienv = getenv("GIF_OUT_CLEAR_INIT");
735
    if (ocienv)
736
     { out_clear_init = atoi(ocienv);
737
       if (VERBOSE) printf("[overriding out_clear_init to %d]\n",out_clear_init);
738
     }
739
  }
740
#endif
741
 out_bits_init = init_bits;
742
 max_ocodes = (1 << GIFBITS) - ((1 << (out_bits_init - 1)) + 3);
743
 did_clear();
744
 output(code_clear);
745
 rl_count = 0;
746
 while (1)
747
  { c = GIFNextPixel(im);
748
    if ((rl_count > 0) && (c != rl_pixel)) rl_flush();
749
    if (c == EOF) break;
750
    if (rl_pixel == c)
751
     { rl_count ++;
752
     }
753
    else
754
     { rl_pixel = c;
755
       rl_count = 1;
756
     }
757
  }
758
 output(code_eof);
759
 output_flush();
760
}
761
762
/*-----------------------------------------------------------------------
763
 *
764
 * End of miGIF section  - See copyright notice at start of section.
765
 *
766
 *-----------------------------------------------------------------------
767
*/
768
769
/******************************************************************************
770
 *
771
 * GIF Specific routines
772
 *
773
 ******************************************************************************/
774
775
/*
776
 * Number of characters so far in this 'packet'
777
 */
778
static int a_count;
779
780
/*
781
 * Set up the 'byte output' routine
782
 */
783
784
/* UNUSED
785
* static void
786
* char_init(void)
787
* {
788
*        a_count = 0;
789
* }
790
*/
791
792
/*
793
 * Define the storage for the packet accumulator
794
 */
795
796
/* UNUSED static char accum[ 256 ]; */
797
798
static void init_statics(void) {
799
	/* Some of these are properly initialized later. What I'm doing
800
		here is making sure code that depends on C's initialization
801
		of statics doesn't break when the code gets called more
802
		than once. */
803
	Width = 0;
804
	Height = 0;
805
	curx = 0;
806
	cury = 0;
807
	CountDown = 0;
808
	Pass = 0;
809
	Interlace = 0;
810
	a_count = 0;
811
}
812
813
814
/* +-------------------------------------------------------------------+ */
815
/* | Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)    | */
816
/* |   Permission to use, copy, modify, and distribute this software   | */
817
/* |   and its documentation for any purpose and without fee is hereby | */
818
/* |   granted, provided that the above copyright notice appear in all | */
819
/* |   copies and that both that copyright notice and this permission  | */
820
/* |   notice appear in supporting documentation.  This software is    | */
821
/* |   provided "as is" without express or implied warranty.           | */
822
/* +-------------------------------------------------------------------+ */
823
(-)graphics/gd/files/gd_gif_in.c (+573 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <math.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include "gd.h"
6
7
/* Used only when debugging GIF compression code */
8
/* #define DEBUGGING_ENVARS */
9
10
#ifdef DEBUGGING_ENVARS
11
12
static int verbose_set = 0;
13
static int verbose;
14
#define VERBOSE (verbose_set?verbose:set_verbose())
15
16
static int set_verbose(void)
17
{
18
 verbose = !!getenv("GIF_VERBOSE");
19
 verbose_set = 1;
20
 return(verbose);
21
}
22
23
#else
24
25
#define VERBOSE 0
26
27
#endif
28
29
30
#define        MAXCOLORMAPSIZE         256
31
32
#define        TRUE    1
33
#define        FALSE   0
34
35
#define CM_RED         0
36
#define CM_GREEN       1
37
#define CM_BLUE                2
38
39
#define        MAX_LWZ_BITS            12
40
41
#define INTERLACE              0x40
42
#define LOCALCOLORMAP  0x80
43
#define BitSet(byte, bit)      (((byte) & (bit)) == (bit))
44
45
#define        ReadOK(file,buffer,len) (gdGetBuf(buffer, len, file) != 0)
46
47
#define LM_to_uint(a,b)                        (((b)<<8)|(a))
48
49
/* We may eventually want to use this information, but def it out for now */
50
#if 0
51
static struct {
52
       unsigned int    Width;
53
       unsigned int    Height;
54
       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
55
       unsigned int    BitPixel;
56
       unsigned int    ColorResolution;
57
       unsigned int    Background;
58
       unsigned int    AspectRatio;
59
} GifScreen;
60
#endif
61
62
static struct {
63
       int     transparent;
64
       int     delayTime;
65
       int     inputFlag;
66
       int     disposal;
67
} Gif89 = { -1, -1, -1, 0 };
68
69
static int ReadColorMap (gdIOCtx *fd, int number, unsigned char (*buffer)[256]);
70
static int DoExtension (gdIOCtx *fd, int label, int *Transparent);
71
static int GetDataBlock (gdIOCtx *fd, unsigned char *buf);
72
static int GetCode (gdIOCtx *fd, int code_size, int flag);
73
static int LWZReadByte (gdIOCtx *fd, int flag, int input_code_size);
74
75
static void ReadImage (gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace); /*1.4//, int ignore); */
76
77
int ZeroDataBlock;
78
79
gdImagePtr gdImageCreateFromGifSource(gdSourcePtr inSource)
80
{
81
        gdIOCtx         *in = gdNewSSCtx(inSource, NULL);
82
        gdImagePtr      im;
83
84
        im = gdImageCreateFromGifCtx(in);
85
86
        in->gd_free(in);
87
88
        return im;
89
}
90
91
gdImagePtr
92
gdImageCreateFromGif(FILE *fdFile)
93
{
94
        gdIOCtx		*fd = gdNewFileCtx(fdFile);
95
        gdImagePtr    	im = 0;
96
97
        im = gdImageCreateFromGifCtx(fd);
98
99
        fd->gd_free(fd);
100
101
        return im;
102
}
103
104
gdImagePtr
105
gdImageCreateFromGifCtx(gdIOCtxPtr fd)
106
{
107
/* 1.4       int imageNumber; */
108
       int BitPixel;
109
       int ColorResolution;
110
       int Background;
111
       int AspectRatio;
112
       int Transparent = (-1);
113
       unsigned char   buf[16];
114
       unsigned char   c;
115
       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
116
       unsigned char   localColorMap[3][MAXCOLORMAPSIZE];
117
       int             imw, imh;
118
       int             useGlobalColormap;
119
       int             bitPixel;
120
       int	       i;
121
       /*1.4//int             imageCount = 0; */
122
       char            version[4];
123
124
       gdImagePtr im = 0;
125
       ZeroDataBlock = FALSE;
126
127
       /*1.4//imageNumber = 1; */
128
       if (! ReadOK(fd,buf,6)) {
129
		return 0;
130
	}
131
       if (strncmp((char *)buf,"GIF",3) != 0) {
132
		return 0;
133
	}
134
       strncpy(version, (char *)buf + 3, 3);
135
       version[3] = '\0';
136
137
       if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
138
		return 0;
139
	}
140
       if (! ReadOK(fd,buf,7)) {
141
		return 0;
142
	}
143
       BitPixel        = 2<<(buf[4]&0x07);
144
       ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
145
       Background      = buf[5];
146
       AspectRatio     = buf[6];
147
148
       if (BitSet(buf[4], LOCALCOLORMAP)) {    /* Global Colormap */
149
               if (ReadColorMap(fd, BitPixel, ColorMap)) {
150
			return 0;
151
		}
152
       }
153
       for (;;) {
154
               if (! ReadOK(fd,&c,1)) {
155
                       return 0;
156
               }
157
               if (c == ';') {         /* GIF terminator */
158
			goto terminated;
159
	       }
160
161
               if (c == '!') {         /* Extension */
162
                       if (! ReadOK(fd,&c,1)) {
163
                               return 0;
164
                       }
165
                       DoExtension(fd, c, &Transparent);
166
                       continue;
167
               }
168
169
               if (c != ',') {         /* Not a valid start character */
170
                       continue;
171
               }
172
173
               /*1.4//++imageCount; */
174
175
               if (! ReadOK(fd,buf,9)) {
176
	               return 0;
177
               }
178
179
               useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
180
181
               bitPixel = 1<<((buf[8]&0x07)+1);
182
183
               imw = LM_to_uint(buf[4],buf[5]);
184
               imh = LM_to_uint(buf[6],buf[7]);
185
	       if (!(im = gdImageCreate(imw, imh))) {
186
			 return 0;
187
	       }
188
               im->interlace = BitSet(buf[8], INTERLACE);
189
               if (! useGlobalColormap) {
190
                       if (ReadColorMap(fd, bitPixel, localColorMap)) { 
191
                                 return 0;
192
                       }
193
                       ReadImage(im, fd, imw, imh, localColorMap, 
194
                                 BitSet(buf[8], INTERLACE)); 
195
                                 /*1.4//imageCount != imageNumber); */
196
               } else {
197
                       ReadImage(im, fd, imw, imh,
198
                                 ColorMap, 
199
                                 BitSet(buf[8], INTERLACE));
200
                                 /*1.4//imageCount != imageNumber); */
201
               }
202
               if (Transparent != (-1)) {
203
                       gdImageColorTransparent(im, Transparent);
204
               }	   
205
	       goto terminated;
206
       }
207
208
terminated:
209
       /* Terminator before any image was declared! */
210
       if (!im) {
211
		return 0;
212
       }
213
       /* Check for open colors at the end, so
214
          we can reduce colorsTotal and ultimately
215
          BitsPerPixel */
216
       for (i=((im->colorsTotal-1)); (i>=0); i--) {
217
		if (im->open[i]) {
218
                	im->colorsTotal--;
219
                } else {
220
                	break;
221
                }
222
       } 
223
       return im;
224
}
225
226
static int
227
ReadColorMap(gdIOCtx *fd, int number, unsigned char (*buffer)[256])
228
{
229
       int             i;
230
       unsigned char   rgb[3];
231
232
233
       for (i = 0; i < number; ++i) {
234
               if (! ReadOK(fd, rgb, sizeof(rgb))) {
235
                       return TRUE;
236
               }
237
               buffer[CM_RED][i] = rgb[0] ;
238
               buffer[CM_GREEN][i] = rgb[1] ;
239
               buffer[CM_BLUE][i] = rgb[2] ;
240
       }
241
242
243
       return FALSE;
244
}
245
246
static int
247
DoExtension(gdIOCtx *fd, int label, int *Transparent)
248
{
249
       static unsigned char     buf[256];
250
251
       switch (label) {
252
       case 0xf9:              /* Graphic Control Extension */
253
               (void) GetDataBlock(fd, (unsigned char*) buf);
254
               Gif89.disposal    = (buf[0] >> 2) & 0x7;
255
               Gif89.inputFlag   = (buf[0] >> 1) & 0x1;
256
               Gif89.delayTime   = LM_to_uint(buf[1],buf[2]);
257
               if ((buf[0] & 0x1) != 0)
258
                       *Transparent = buf[3];
259
260
               while (GetDataBlock(fd, (unsigned char*) buf) != 0)
261
                       ;
262
               return FALSE;
263
       default:
264
               break;
265
       }
266
       while (GetDataBlock(fd, (unsigned char*) buf) != 0)
267
               ;
268
269
       return FALSE;
270
}
271
272
static int
273
GetDataBlock_(gdIOCtx *fd, unsigned char *buf)
274
{
275
       unsigned char   count;
276
277
       if (! ReadOK(fd,&count,1)) {
278
               return -1;
279
       }
280
281
       ZeroDataBlock = count == 0;
282
283
       if ((count != 0) && (! ReadOK(fd, buf, count))) {
284
               return -1;
285
       }
286
287
       return count;
288
}
289
290
static int
291
GetDataBlock(gdIOCtx *fd, unsigned char *buf)
292
{
293
 int rv;
294
 int i;
295
296
 rv = GetDataBlock_(fd,buf);
297
 if (VERBOSE)
298
  { printf("[GetDataBlock returning %d",rv);
299
    if (rv > 0)
300
     { printf(":");
301
       for (i=0;i<rv;i++) printf(" %02x",buf[i]);
302
     }
303
    printf("]\n");
304
  }
305
 return(rv);
306
}
307
308
static int
309
GetCode_(gdIOCtx *fd, int code_size, int flag)
310
{
311
       static unsigned char    buf[280];
312
       static int              curbit, lastbit, done, last_byte;
313
       int                     i, j, ret;
314
       unsigned char           count;
315
316
       if (flag) {
317
               curbit = 0;
318
               lastbit = 0;
319
               done = FALSE;
320
               return 0;
321
       }
322
323
       if ( (curbit+code_size) >= lastbit) {
324
               if (done) {
325
                       if (curbit >= lastbit) {
326
                                /* Oh well */
327
                       }                        
328
                       return -1;
329
               }
330
               buf[0] = buf[last_byte-2];
331
               buf[1] = buf[last_byte-1];
332
333
               if ((count = GetDataBlock(fd, &buf[2])) == 0)
334
                       done = TRUE;
335
336
               last_byte = 2 + count;
337
               curbit = (curbit - lastbit) + 16;
338
               lastbit = (2+count)*8 ;
339
       }
340
341
       ret = 0;
342
       for (i = curbit, j = 0; j < code_size; ++i, ++j)
343
               ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
344
345
       curbit += code_size;
346
       return ret;
347
}
348
349
static int
350
GetCode(gdIOCtx *fd, int code_size, int flag)
351
{
352
 int rv;
353
354
 rv = GetCode_(fd,code_size,flag);
355
 if (VERBOSE) printf("[GetCode(,%d,%d) returning %d]\n",code_size,flag,rv);
356
 return(rv);
357
}
358
359
#define STACK_SIZE ((1<<(MAX_LWZ_BITS))*2)
360
static int
361
LWZReadByte_(gdIOCtx *fd, int flag, int input_code_size)
362
{
363
       static int      fresh = FALSE;
364
       int             code, incode;
365
       static int      code_size, set_code_size;
366
       static int      max_code, max_code_size;
367
       static int      firstcode, oldcode;
368
       static int      clear_code, end_code;
369
       static int      table[2][(1<< MAX_LWZ_BITS)];
370
       static int      stack[STACK_SIZE], *sp;
371
       register int    i;
372
373
       if (flag) {
374
               set_code_size = input_code_size;
375
               code_size = set_code_size+1;
376
               clear_code = 1 << set_code_size ;
377
               end_code = clear_code + 1;
378
               max_code_size = 2*clear_code;
379
               max_code = clear_code+2;
380
381
               GetCode(fd, 0, TRUE);
382
               
383
               fresh = TRUE;
384
385
               for (i = 0; i < clear_code; ++i) {
386
                       table[0][i] = 0;
387
                       table[1][i] = i;
388
               }
389
               for (; i < (1<<MAX_LWZ_BITS); ++i)
390
                       table[0][i] = table[1][0] = 0;
391
392
               sp = stack;
393
394
               return 0;
395
       } else if (fresh) {
396
               fresh = FALSE;
397
               do {
398
                       firstcode = oldcode =
399
                               GetCode(fd, code_size, FALSE);
400
               } while (firstcode == clear_code);
401
               return firstcode;
402
       }
403
404
       if (sp > stack)
405
               return *--sp;
406
407
       while ((code = GetCode(fd, code_size, FALSE)) >= 0) {
408
               if (code == clear_code) {
409
                       for (i = 0; i < clear_code; ++i) {
410
                               table[0][i] = 0;
411
                               table[1][i] = i;
412
                       }
413
                       for (; i < (1<<MAX_LWZ_BITS); ++i)
414
                               table[0][i] = table[1][i] = 0;
415
                       code_size = set_code_size+1;
416
                       max_code_size = 2*clear_code;
417
                       max_code = clear_code+2;
418
                       sp = stack;
419
                       firstcode = oldcode =
420
                                       GetCode(fd, code_size, FALSE);
421
                       return firstcode;
422
               } else if (code == end_code) {
423
                       int             count;
424
                       unsigned char   buf[260];
425
426
                       if (ZeroDataBlock)
427
                               return -2;
428
429
                       while ((count = GetDataBlock(fd, buf)) > 0)
430
                               ;
431
432
                       if (count != 0)
433
                       return -2;
434
               }
435
436
               incode = code;
437
438
	       if (sp == (stack + STACK_SIZE)) {
439
		       /* Bad compressed data stream */
440
		       return -1;
441
	       }
442
443
               if (code >= max_code) {
444
                       *sp++ = firstcode;
445
                       code = oldcode;
446
               }
447
448
               while (code >= clear_code) {
449
		       if (sp == (stack + STACK_SIZE)) {
450
			       /* Bad compressed data stream */
451
			       return -1;
452
		       }
453
                       *sp++ = table[1][code];
454
                       if (code == table[0][code]) {
455
                               /* Oh well */
456
                       }
457
                       code = table[0][code];
458
               }
459
460
               *sp++ = firstcode = table[1][code];
461
462
               if ((code = max_code) <(1<<MAX_LWZ_BITS)) {
463
                       table[0][code] = oldcode;
464
                       table[1][code] = firstcode;
465
                       ++max_code;
466
                       if ((max_code >= max_code_size) &&
467
                               (max_code_size < (1<<MAX_LWZ_BITS))) {
468
                               max_code_size *= 2;
469
                               ++code_size;
470
                       }
471
               }
472
473
               oldcode = incode;
474
475
               if (sp > stack)
476
                       return *--sp;
477
       }
478
       return code;
479
}
480
481
static int
482
LWZReadByte(gdIOCtx *fd, int flag, int input_code_size)
483
{
484
 int rv;
485
486
 rv = LWZReadByte_(fd,flag,input_code_size);
487
 if (VERBOSE) printf("[LWZReadByte(,%d,%d) returning %d]\n",flag,input_code_size,rv);
488
 return(rv);
489
}
490
491
static void
492
ReadImage(gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace) /*1.4//, int ignore) */
493
{
494
       unsigned char   c;      
495
       int             v;
496
       int             xpos = 0, ypos = 0, pass = 0;
497
       int i;
498
       /* Stash the color map into the image */
499
       for (i=0; (i<gdMaxColors); i++) {
500
               im->red[i] = cmap[CM_RED][i];	
501
               im->green[i] = cmap[CM_GREEN][i];	
502
               im->blue[i] = cmap[CM_BLUE][i];	
503
               im->open[i] = 1;
504
       }
505
       /* Many (perhaps most) of these colors will remain marked open. */
506
       im->colorsTotal = gdMaxColors;
507
       /*
508
       **  Initialize the Compression routines
509
       */
510
       if (! ReadOK(fd,&c,1)) {
511
               return; 
512
       }
513
       if (LWZReadByte(fd, TRUE, c) < 0) {
514
               return;
515
       }
516
517
       /*
518
       **  If this is an "uninteresting picture" ignore it.
519
       **  REMOVED For 1.4
520
       */
521
       /*if (ignore) { */
522
       /*        while (LWZReadByte(fd, FALSE, c) >= 0) */
523
       /*                ; */
524
       /*        return; */
525
       /*} */
526
527
       while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) {
528
               /* This how we recognize which colors are actually used. */
529
               if (im->open[v]) {
530
                       im->open[v] = 0;
531
               }
532
               gdImageSetPixel(im, xpos, ypos, v);
533
               ++xpos;
534
               if (xpos == len) {
535
                       xpos = 0;
536
                       if (interlace) {
537
                               switch (pass) {
538
                               case 0:
539
                               case 1:
540
                                       ypos += 8; break;
541
                               case 2:
542
                                       ypos += 4; break;
543
                               case 3:
544
                                       ypos += 2; break;
545
                               }
546
547
                               if (ypos >= height) {
548
                                       ++pass;
549
                                       switch (pass) {
550
                                       case 1:
551
                                               ypos = 4; break;
552
                                       case 2:
553
                                               ypos = 2; break;
554
                                       case 3:
555
                                               ypos = 1; break;
556
                                       default:
557
                                               goto fini;
558
                                       }
559
                               }
560
                       } else {
561
                               ++ypos;
562
                       }
563
               }
564
               if (ypos >= height)
565
                       break;
566
       }
567
568
fini:
569
       if (LWZReadByte(fd,FALSE,c)>=0) {
570
               /* Ignore extra */
571
       }
572
}
573
(-)graphics/gd/files/gd_gif_out.c (+46 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <math.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include "gd.h"
6
7
/*
8
** Wrapper functions for GIF output.
9
*/
10
11
#define LZW_LICENCED
12
13
void gdImageGifToSink(gdImagePtr im, gdSinkPtr outSink)
14
{
15
        gdIOCtx   *out = gdNewSSCtx(NULL,outSink);
16
        gdImageGifCtx(im, out);
17
        out->gd_free(out);
18
}
19
20
void gdImageGifCtx(gdImagePtr im, gdIOCtx *out)
21
{
22
#ifdef LZW_LICENCED
23
	gdImageLzwCtx(im, out);
24
#else
25
        gdImageBigGifCtx(im, out);
26
#endif
27
}
28
29
void gdImageGif(gdImagePtr im, FILE *outFile)
30
{
31
#ifdef LZW_LICENCED
32
        gdImageLzw(im, outFile);
33
#else
34
        gdImageBigGif(im, outFile);
35
#endif
36
}
37
38
void* gdImageGifPtr(gdImagePtr im, int *size)
39
{
40
#ifdef LZW_LICENCED
41
        return gdImageLzwPtr(im, size);
42
#else
43
        return gdImageBigGifPtr(im, size);
44
#endif
45
}
46
(-)graphics/gd/files/gd_lzw_out.c (+795 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <math.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include "gd.h"
6
7
/* Code drawn from ppmtogif.c, from the pbmplus package
8
**
9
** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
10
** Lempel-Zim compression based on "compress".
11
**
12
** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
13
**
14
** Copyright (C) 1989 by Jef Poskanzer.
15
**
16
** Permission to use, copy, modify, and distribute this software and its
17
** documentation for any purpose and without fee is hereby granted, provided
18
** that the above copyright notice appear in all copies and that both that
19
** copyright notice and this permission notice appear in supporting
20
** documentation.  This software is provided "as is" without express or
21
** implied warranty.
22
**
23
** The Graphics Interchange Format(c) is the Copyright property of
24
** CompuServe Incorporated.  GIF(sm) is a Service Mark property of
25
** CompuServe Incorporated.
26
*/
27
28
/*
29
 * a code_int must be able to hold 2**GIFBITS values of type int, and also -1
30
 */
31
typedef int             code_int;
32
33
#ifdef SIGNED_COMPARE_SLOW
34
typedef unsigned long int count_int;
35
typedef unsigned short int count_short;
36
#else /*SIGNED_COMPARE_SLOW*/
37
typedef long int          count_int;
38
#endif /*SIGNED_COMPARE_SLOW*/
39
40
static int colorstobpp(int colors);
41
static void BumpPixel (void);
42
static int GIFNextPixel (gdImagePtr im);
43
static void GIFEncode (gdIOCtx *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im);
44
/*static void Putword (int w, gdIOCtx *fp); */
45
static void compress (int init_bits, gdIOCtx *outfile, gdImagePtr im);
46
static void output (code_int code);
47
static void cl_block (void);
48
static void cl_hash (register count_int hsize);
49
static void char_init (void);
50
static void char_out (int c);
51
static void flush_char (void);
52
/* Allows for reuse */
53
static void init_statics(void);
54
55
void gdImageLzwCtx(gdImagePtr im, gdIOCtx *out)
56
{
57
	int interlace, transparent, BitsPerPixel;
58
59
	interlace = im->interlace;
60
	transparent = im->transparent;
61
62
	BitsPerPixel = colorstobpp(im->colorsTotal);
63
	/* Clear any old values in statics strewn through the GIF code */
64
	init_statics();
65
	/* All set, let's do it. */
66
	GIFEncode(
67
		out, im->sx, im->sy, interlace, 0, transparent, BitsPerPixel,
68
		im->red, im->green, im->blue, im);
69
}
70
71
void gdImageLzw(gdImagePtr im, FILE *outFile)
72
{
73
        gdIOCtx   *out = gdNewFileCtx(outFile);
74
        gdImageLzwCtx(im, out);
75
        out->gd_free(out);
76
}
77
78
void* gdImageLzwPtr(gdImagePtr im, int *size)
79
{
80
	void	*rv;
81
        gdIOCtx   *out = gdNewDynamicCtx(2048, NULL);
82
        gdImageLzwCtx(im, out);
83
        rv = gdDPExtractData(out,size);
84
	out->gd_free(out);
85
	return rv;
86
}
87
88
89
90
static int
91
colorstobpp(int colors)
92
{
93
    int bpp = 0;
94
95
    if ( colors <= 2 )
96
        bpp = 1;
97
    else if ( colors <= 4 )
98
        bpp = 2;
99
    else if ( colors <= 8 )
100
        bpp = 3;
101
    else if ( colors <= 16 )
102
        bpp = 4;
103
    else if ( colors <= 32 )
104
        bpp = 5;
105
    else if ( colors <= 64 )
106
        bpp = 6;
107
    else if ( colors <= 128 )
108
        bpp = 7;
109
    else if ( colors <= 256 )
110
        bpp = 8;
111
    return bpp;
112
    }
113
114
/*****************************************************************************
115
 *
116
 * GIFENCODE.C    - GIF Image compression interface
117
 *
118
 * GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
119
 *            BitsPerPixel, Red, Green, Blue, gdImagePtr )
120
 *
121
 *****************************************************************************/
122
123
#define TRUE 1
124
#define FALSE 0
125
126
static int Width, Height;
127
static int curx, cury;
128
static long CountDown;
129
static int Pass = 0;
130
static int Interlace;
131
132
/*
133
 * Bump the 'curx' and 'cury' to point to the next pixel
134
 */
135
static void
136
BumpPixel(void)
137
{
138
        /*
139
         * Bump the current X position
140
         */
141
        ++curx;
142
143
        /*
144
         * If we are at the end of a scan line, set curx back to the beginning
145
         * If we are interlaced, bump the cury to the appropriate spot,
146
         * otherwise, just increment it.
147
         */
148
        if( curx == Width ) {
149
                curx = 0;
150
151
                if( !Interlace )
152
                        ++cury;
153
                else {
154
                     switch( Pass ) {
155
156
                       case 0:
157
                          cury += 8;
158
                          if( cury >= Height ) {
159
                                ++Pass;
160
                                cury = 4;
161
                          }
162
                          break;
163
164
                       case 1:
165
                          cury += 8;
166
                          if( cury >= Height ) {
167
                                ++Pass;
168
                                cury = 2;
169
                          }
170
                          break;
171
172
                       case 2:
173
                          cury += 4;
174
                          if( cury >= Height ) {
175
                             ++Pass;
176
                             cury = 1;
177
                          }
178
                          break;
179
180
                       case 3:
181
                          cury += 2;
182
                          break;
183
                        }
184
                }
185
        }
186
}
187
188
/*
189
 * Return the next pixel from the image
190
 */
191
static int
192
GIFNextPixel(gdImagePtr im)
193
{
194
        int r;
195
196
        if( CountDown == 0 )
197
                return EOF;
198
199
        --CountDown;
200
201
        r = gdImageGetPixel(im, curx, cury);
202
203
        BumpPixel();
204
205
        return r;
206
}
207
208
/* public */
209
210
static void
211
GIFEncode(gdIOCtx *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im)
212
{
213
        int B;
214
        int RWidth, RHeight;
215
        int LeftOfs, TopOfs;
216
        int Resolution;
217
        int ColorMapSize;
218
        int InitCodeSize;
219
        int i;
220
221
        Interlace = GInterlace;
222
223
        ColorMapSize = 1 << BitsPerPixel;
224
225
        RWidth = Width = GWidth;
226
        RHeight = Height = GHeight;
227
        LeftOfs = TopOfs = 0;
228
229
        Resolution = BitsPerPixel;
230
231
        /*
232
         * Calculate number of bits we are expecting
233
         */
234
        CountDown = (long)Width * (long)Height;
235
236
        /*
237
         * Indicate which pass we are on (if interlace)
238
         */
239
        Pass = 0;
240
241
        /*
242
         * The initial code size
243
         */
244
        if( BitsPerPixel <= 1 )
245
                InitCodeSize = 2;
246
        else
247
                InitCodeSize = BitsPerPixel;
248
249
        /*
250
         * Set up the current x and y position
251
         */
252
        curx = cury = 0;
253
254
        /*
255
         * Write the Magic header
256
         */
257
        gdPutBuf( Transparent < 0 ? "GIF87a" : "GIF89a", 6, fp );
258
259
        /*
260
         * Write out the screen width and height
261
         */
262
        Putword( RWidth, fp );
263
        Putword( RHeight, fp );
264
265
        /*
266
         * Indicate that there is a global colour map
267
         */
268
        B = 0x80;       /* Yes, there is a color map */
269
270
        /*
271
         * OR in the resolution
272
         */
273
        B |= (Resolution - 1) << 5;
274
275
        /*
276
         * OR in the Bits per Pixel
277
         */
278
        B |= (BitsPerPixel - 1);
279
280
        /*
281
         * Write it out
282
         */
283
        gdPutC( B, fp );
284
285
        /*
286
         * Write out the Background colour
287
         */
288
        gdPutC( Background, fp );
289
290
        /*
291
         * Byte of 0's (future expansion)
292
         */
293
        gdPutC( 0, fp );
294
295
        /*
296
         * Write out the Global Colour Map
297
         */
298
        for( i=0; i<ColorMapSize; ++i ) {
299
                gdPutC( Red[i], fp );
300
                gdPutC( Green[i], fp );
301
                gdPutC( Blue[i], fp );
302
        }
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
        /*
319
         * Write an Image separator
320
         */
321
        gdPutC( ',', fp );
322
323
        /*
324
         * Write the Image header
325
         */
326
327
        Putword( LeftOfs, fp );
328
        Putword( TopOfs, fp );
329
        Putword( Width, fp );
330
        Putword( Height, fp );
331
332
        /*
333
         * Write out whether or not the image is interlaced
334
         */
335
        if( Interlace )
336
                gdPutC( 0x40, fp );
337
        else
338
                gdPutC( 0x00, fp );
339
340
        /*
341
         * Write out the initial code size
342
         */
343
        gdPutC( InitCodeSize, fp );
344
345
        /*
346
         * Go and actually compress the data
347
         */
348
        compress( InitCodeSize+1, fp, im );
349
350
        /*
351
         * Write out a Zero-length packet (to end the series)
352
         */
353
        gdPutC( 0, fp );
354
355
        /*
356
         * Write the GIF file terminator
357
         */
358
        gdPutC( ';', fp );
359
}
360
361
/* */
362
/* * Write out a word to the GIF file */
363
/* */
364
/*static void */
365
/*Putword(int w, FILE *fp) */
366
/*{ */
367
/*        fputc( w & 0xff, fp ); */
368
/*        fputc( (w / 256) & 0xff, fp ); */
369
/*} */
370
371
372
/***************************************************************************
373
 *
374
 *  GIFCOMPR.C       - GIF Image compression routines
375
 *
376
 *  Lempel-Ziv compression based on 'compress'.  GIF modifications by
377
 *  David Rowley (mgardi@watdcsu.waterloo.edu)
378
 *
379
 ***************************************************************************/
380
381
/*
382
 * General DEFINEs
383
 */
384
385
#define GIFBITS    12
386
387
#define HSIZE  5003            /* 80% occupancy */
388
389
#ifdef NO_UCHAR
390
 typedef char   char_type;
391
#else /*NO_UCHAR*/
392
 typedef        unsigned char   char_type;
393
#endif /*NO_UCHAR*/
394
395
/*
396
 *
397
 * GIF Image compression - modified 'compress'
398
 *
399
 * Based on: compress.c - File compression ala IEEE Computer, June 1984.
400
 *
401
 * By Authors:  Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
402
 *              Jim McKie               (decvax!mcvax!jim)
403
 *              Steve Davies            (decvax!vax135!petsd!peora!srd)
404
 *              Ken Turkowski           (decvax!decwrl!turtlevax!ken)
405
 *              James A. Woods          (decvax!ihnp4!ames!jaw)
406
 *              Joe Orost               (decvax!vax135!petsd!joe)
407
 *
408
 */
409
#include <ctype.h>
410
411
#define ARGVAL() (*++(*argv) || (--argc && *++argv))
412
413
static int n_bits;                        /* number of bits/code */
414
static int maxbits = GIFBITS;                /* user settable max # bits/code */
415
static code_int maxcode;                  /* maximum code, given n_bits */
416
static code_int maxmaxcode = (code_int)1 << GIFBITS; /* should NEVER generate this code */
417
#ifdef COMPATIBLE               /* But wrong! */
418
# define MAXCODE(n_bits)        ((code_int) 1 << (n_bits) - 1)
419
#else /*COMPATIBLE*/
420
# define MAXCODE(n_bits)        (((code_int) 1 << (n_bits)) - 1)
421
#endif /*COMPATIBLE*/
422
423
static count_int htab [HSIZE];
424
static unsigned short codetab [HSIZE];
425
#define HashTabOf(i)       htab[i]
426
#define CodeTabOf(i)    codetab[i]
427
428
static code_int hsize = HSIZE;                 /* for dynamic table sizing */
429
430
/*
431
 * To save much memory, we overlay the table used by compress() with those
432
 * used by decompress().  The tab_prefix table is the same size and type
433
 * as the codetab.  The tab_suffix table needs 2**GIFBITS characters.  We
434
 * get this from the beginning of htab.  The output stack uses the rest
435
 * of htab, and contains characters.  There is plenty of room for any
436
 * possible stack (stack used to be 8000 characters).
437
 */
438
439
#define tab_prefixof(i) CodeTabOf(i)
440
#define tab_suffixof(i)        ((char_type*)(htab))[i]
441
#define de_stack               ((char_type*)&tab_suffixof((code_int)1<<GIFBITS))
442
443
static code_int free_ent = 0;                  /* first unused entry */
444
445
/*
446
 * block compression parameters -- after all codes are used up,
447
 * and compression rate changes, start over.
448
 */
449
static int clear_flg = 0;
450
451
static int offset;
452
static long int in_count = 1;            /* length of input */
453
static long int out_count = 0;           /* # of codes output (for debugging) */
454
455
/*
456
 * compress stdin to stdout
457
 *
458
 * Algorithm:  use open addressing double hashing (no chaining) on the
459
 * prefix code / next character combination.  We do a variant of Knuth's
460
 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
461
 * secondary probe.  Here, the modular division first probe is gives way
462
 * to a faster exclusive-or manipulation.  Also do block compression with
463
 * an adaptive reset, whereby the code table is cleared when the compression
464
 * ratio decreases, but after the table fills.  The variable-length output
465
 * codes are re-sized at this point, and a special CLEAR code is generated
466
 * for the decompressor.  Late addition:  construct the table according to
467
 * file size for noticeable speed improvement on small files.  Please direct
468
 * questions about this implementation to ames!jaw.
469
 */
470
471
static int g_init_bits;
472
static gdIOCtx* g_outfile;
473
474
static int ClearCode;
475
static int EOFCode;
476
477
static void
478
compress(int init_bits, gdIOCtx *outfile, gdImagePtr im)
479
{
480
    register long fcode;
481
    register code_int i /* = 0 */;
482
    register int c;
483
    register code_int ent;
484
    register code_int disp;
485
    register code_int hsize_reg;
486
    register int hshift;
487
488
    /*
489
     * Set up the globals:  g_init_bits - initial number of bits
490
     *                      g_outfile   - pointer to output file
491
     */
492
    g_init_bits = init_bits;
493
    g_outfile = outfile;
494
495
    /*
496
     * Set up the necessary values
497
     */
498
    offset = 0;
499
    out_count = 0;
500
    clear_flg = 0;
501
    in_count = 1;
502
    maxcode = MAXCODE(n_bits = g_init_bits);
503
504
    ClearCode = (1 << (init_bits - 1));
505
    EOFCode = ClearCode + 1;
506
    free_ent = ClearCode + 2;
507
508
    char_init();
509
510
    ent = GIFNextPixel( im );
511
512
    hshift = 0;
513
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
514
        ++hshift;
515
    hshift = 8 - hshift;                /* set hash code range bound */
516
517
    hsize_reg = hsize;
518
    cl_hash( (count_int) hsize_reg);            /* clear hash table */
519
520
    output( (code_int)ClearCode );
521
522
#ifdef SIGNED_COMPARE_SLOW
523
    while ( (c = GIFNextPixel( im )) != (unsigned) EOF ) {
524
#else /*SIGNED_COMPARE_SLOW*/
525
    while ( (c = GIFNextPixel( im )) != EOF ) {  /* } */
526
#endif /*SIGNED_COMPARE_SLOW*/
527
528
        ++in_count;
529
530
        fcode = (long) (((long) c << maxbits) + ent);
531
        i = (((code_int)c << hshift) ^ ent);    /* xor hashing */
532
533
        if ( HashTabOf (i) == fcode ) {
534
            ent = CodeTabOf (i);
535
            continue;
536
        } else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
537
            goto nomatch;
538
        disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
539
        if ( i == 0 )
540
            disp = 1;
541
probe:
542
        if ( (i -= disp) < 0 )
543
            i += hsize_reg;
544
545
        if ( HashTabOf (i) == fcode ) {
546
            ent = CodeTabOf (i);
547
            continue;
548
        }
549
        if ( (long)HashTabOf (i) > 0 )
550
            goto probe;
551
nomatch:
552
        output ( (code_int) ent );
553
        ++out_count;
554
        ent = c;
555
#ifdef SIGNED_COMPARE_SLOW
556
        if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
557
#else /*SIGNED_COMPARE_SLOW*/
558
        if ( free_ent < maxmaxcode ) {  /* } */
559
#endif /*SIGNED_COMPARE_SLOW*/
560
            CodeTabOf (i) = free_ent++; /* code -> hashtable */
561
            HashTabOf (i) = fcode;
562
        } else
563
                cl_block();
564
    }
565
    /*
566
     * Put out the final code.
567
     */
568
    output( (code_int)ent );
569
    ++out_count;
570
    output( (code_int) EOFCode );
571
}
572
573
/*****************************************************************
574
 * TAG( output )
575
 *
576
 * Output the given code.
577
 * Inputs:
578
 *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
579
 *              that n_bits =< (long)wordsize - 1.
580
 * Outputs:
581
 *      Outputs code to the file.
582
 * Assumptions:
583
 *      Chars are 8 bits long.
584
 * Algorithm:
585
 *      Maintain a GIFBITS character long buffer (so that 8 codes will
586
 * fit in it exactly).  Use the VAX insv instruction to insert each
587
 * code in turn.  When the buffer fills up empty it and start over.
588
 */
589
590
static unsigned long cur_accum = 0;
591
static int cur_bits = 0;
592
593
static unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
594
                                  0x001F, 0x003F, 0x007F, 0x00FF,
595
                                  0x01FF, 0x03FF, 0x07FF, 0x0FFF,
596
                                  0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
597
598
static void
599
output(code_int code)
600
{
601
    cur_accum &= masks[ cur_bits ];
602
603
    if( cur_bits > 0 )
604
        cur_accum |= ((long)code << cur_bits);
605
    else
606
        cur_accum = code;
607
608
    cur_bits += n_bits;
609
610
    while( cur_bits >= 8 ) {
611
        char_out( (unsigned int)(cur_accum & 0xff) );
612
        cur_accum >>= 8;
613
        cur_bits -= 8;
614
    }
615
616
    /*
617
     * If the next entry is going to be too big for the code size,
618
     * then increase it, if possible.
619
     */
620
   if ( free_ent > maxcode || clear_flg ) {
621
622
            if( clear_flg ) {
623
624
                maxcode = MAXCODE (n_bits = g_init_bits);
625
                clear_flg = 0;
626
627
            } else {
628
629
                ++n_bits;
630
                if ( n_bits == maxbits )
631
                    maxcode = maxmaxcode;
632
                else
633
                    maxcode = MAXCODE(n_bits);
634
            }
635
        }
636
637
    if( code == EOFCode ) {
638
        /*
639
         * At EOF, write the rest of the buffer.
640
         */
641
        while( cur_bits > 0 ) {
642
                char_out( (unsigned int)(cur_accum & 0xff) );
643
                cur_accum >>= 8;
644
                cur_bits -= 8;
645
        }
646
647
        flush_char();
648
649
/*        fflush( g_outfile ); */
650
/* */
651
/*        if( ferror( g_outfile ) ) */
652
/*		return; */
653
    }
654
}
655
656
/*
657
 * Clear out the hash table
658
 */
659
static void
660
cl_block (void)             /* table clear for block compress */
661
{
662
663
        cl_hash ( (count_int) hsize );
664
        free_ent = ClearCode + 2;
665
        clear_flg = 1;
666
667
        output( (code_int)ClearCode );
668
}
669
670
static void
671
cl_hash(register count_int hsize)          /* reset code table */
672
                         
673
{
674
675
        register count_int *htab_p = htab+hsize;
676
677
        register long i;
678
        register long m1 = -1;
679
680
        i = hsize - 16;
681
        do {                            /* might use Sys V memset(3) here */
682
                *(htab_p-16) = m1;
683
                *(htab_p-15) = m1;
684
                *(htab_p-14) = m1;
685
                *(htab_p-13) = m1;
686
                *(htab_p-12) = m1;
687
                *(htab_p-11) = m1;
688
                *(htab_p-10) = m1;
689
                *(htab_p-9) = m1;
690
                *(htab_p-8) = m1;
691
                *(htab_p-7) = m1;
692
                *(htab_p-6) = m1;
693
                *(htab_p-5) = m1;
694
                *(htab_p-4) = m1;
695
                *(htab_p-3) = m1;
696
                *(htab_p-2) = m1;
697
                *(htab_p-1) = m1;
698
                htab_p -= 16;
699
        } while ((i -= 16) >= 0);
700
701
        for ( i += 16; i > 0; --i )
702
                *--htab_p = m1;
703
}
704
705
/******************************************************************************
706
 *
707
 * GIF Specific routines
708
 *
709
 ******************************************************************************/
710
711
/*
712
 * Number of characters so far in this 'packet'
713
 */
714
static int a_count;
715
716
/*
717
 * Set up the 'byte output' routine
718
 */
719
static void
720
char_init(void)
721
{
722
        a_count = 0;
723
}
724
725
/*
726
 * Define the storage for the packet accumulator
727
 */
728
static char accum[ 256 ];
729
730
/*
731
 * Add a character to the end of the current packet, and if it is 254
732
 * characters, flush the packet to disk.
733
 */
734
static void
735
char_out(int c)
736
{
737
        accum[ a_count++ ] = c;
738
        if( a_count >= 254 )
739
                flush_char();
740
}
741
742
/*
743
 * Flush the packet to disk, and reset the accumulator
744
 */
745
static void
746
flush_char(void)
747
{
748
        if( a_count > 0 ) {
749
                gdPutC( a_count, g_outfile );
750
                gdPutBuf( accum, a_count, g_outfile );
751
                a_count = 0;
752
        }
753
}
754
755
static void init_statics(void) {
756
	/* Some of these are properly initialized later. What I'm doing
757
		here is making sure code that depends on C's initialization
758
		of statics doesn't break when the code gets called more
759
		than once. */
760
	Width = 0;
761
	Height = 0;
762
	curx = 0;
763
	cury = 0;
764
	CountDown = 0;
765
	Pass = 0;
766
	Interlace = 0;
767
	a_count = 0;
768
	cur_accum = 0;
769
	cur_bits = 0;
770
	g_init_bits = 0;
771
	g_outfile = 0;
772
	ClearCode = 0;
773
	EOFCode = 0;
774
	free_ent = 0;
775
	clear_flg = 0;
776
	offset = 0;
777
	in_count = 1;
778
	out_count = 0;	
779
	hsize = HSIZE;
780
	n_bits = 0;
781
	maxbits = GIFBITS;
782
	maxcode = 0;
783
	maxmaxcode = (code_int)1 << GIFBITS;
784
}
785
786
787
/* +-------------------------------------------------------------------+ */
788
/* | Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)    | */
789
/* |   Permission to use, copy, modify, and distribute this software   | */
790
/* |   and its documentation for any purpose and without fee is hereby | */
791
/* |   granted, provided that the above copyright notice appear in all | */
792
/* |   copies and that both that copyright notice and this permission  | */
793
/* |   notice appear in supporting documentation.  This software is    | */
794
/* |   provided "as is" without express or implied warranty.           | */
795
/* +-------------------------------------------------------------------+ */
(-)graphics/gd/files/gdcmpgif.c (+85 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <unistd.h> /* For unlink function */
3
#include "gd.h"
4
5
/* A short program which converts a .png file into a .gd file, for
6
	your convenience in creating images on the fly from a
7
	basis image that must be loaded quickly. The .gd format
8
	is not intended to be a general-purpose format. */
9
10
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2);
11
12
13
int main(int argc, char **argv)
14
{
15
	gdImagePtr im1, im2;
16
	FILE *in;
17
18
	if (argc != 3) {
19
		fprintf(stderr, "Usage: gdcmpgif filename.gif filename.gif\n");
20
		exit(1);
21
	}
22
	in = fopen(argv[1], "rb");
23
	if (!in) {
24
		fprintf(stderr, "Input file does not exist!\n");
25
		exit(1);
26
	}
27
	im1 = gdImageCreateFromGif(in);
28
	fclose(in);
29
30
	if (!im1) {
31
		fprintf(stderr, "Input is not in GIF format!\n");
32
		exit(1);
33
	}
34
35
	in = fopen(argv[2], "rb");
36
	if (!in) {
37
		fprintf(stderr, "Input file 2 does not exist!\n");
38
		exit(1);
39
	}
40
	im2 = gdImageCreateFromGif(in);
41
	fclose(in);
42
43
	if (!im2) {
44
		fprintf(stderr, "Input 2 is not in GIF format!\n");
45
		exit(1);
46
	}
47
48
	CompareImages("gdcmpgif", im1, im2);
49
50
	gdImageDestroy(im1);
51
        gdImageDestroy(im2);
52
53
	return 0;
54
}
55
56
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2)
57
{
58
	int cmpRes;
59
60
	cmpRes = gdImageCompare(im1, im2);
61
62
	if (cmpRes & GD_CMP_IMAGE) {
63
		printf("%%%s: ERROR images differ: BAD\n",msg);
64
	} else if (cmpRes != 0) {
65
		printf("%%%s: WARNING images differ: WARNING - Probably OK\n",msg);
66
	} else {
67
		printf("%%%s: OK\n",msg);
68
		return;
69
	}
70
71
	if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y)) {
72
		printf("-%s: INFO image sizes differ\n",msg);
73
	}
74
75
	if (cmpRes & GD_CMP_NUM_COLORS) {
76
		printf("-%s: INFO number of pallette entries differ %d Vs. %d\n",msg,
77
			im1->colorsTotal, im2->colorsTotal);
78
	}
79
80
	if (cmpRes & GD_CMP_COLOR) {
81
		printf("-%s: INFO actual colours of pixels differ\n",msg);
82
	}
83
}
84
85
(-)graphics/gd/files/giftogd2.c (+48 lines)
Line 0 Link Here
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
#include "gd.h"
5
6
/* A short program which converts a .gif file into a .gd file, for
7
	your convenience in creating images on the fly from a
8
	basis image that must be loaded quickly. The .gd format
9
	is not intended to be a general-purpose format. */
10
11
int main(int argc, char **argv)
12
{
13
	gdImagePtr im;
14
	FILE *in, *out;
15
	int	cs, fmt;
16
17
	if (argc != 5) {
18
		fprintf(stderr, "Usage: giftogd2 filename.gif filename.gd2 cs fmt\n");
19
                fprintf(stderr, "    where cs is the chunk size\n");
20
		fprintf(stderr, "          fmt is 1 for raw, 2 for compressed\n");
21
		exit(1);
22
	}
23
	in = fopen(argv[1], "rb");
24
	if (!in) {
25
		fprintf(stderr, "Input file does not exist!\n");
26
		exit(1);
27
	}
28
	im = gdImageCreateFromGif(in);
29
	fclose(in);
30
	if (!im) {
31
		fprintf(stderr, "Input is not in GIF format!\n");
32
		exit(1);
33
	}
34
	out = fopen(argv[2], "wb");
35
	if (!out) {
36
		fprintf(stderr, "Output file cannot be written to!\n");
37
		gdImageDestroy(im);
38
		exit(1);	
39
	}
40
	cs = atoi(argv[3]);
41
	fmt = atoi(argv[4]);
42
	gdImageGd2(im, out, cs, fmt);
43
	fclose(out);
44
	gdImageDestroy(im);
45
46
	return 0;
47
}
48
(-)graphics/gd/files/patch-ac (-188 lines)
Lines 1-188 Link Here
1
--- Makefile.orig	Thu Feb 22 19:03:43 2001
2
+++ Makefile	Sun Dec 16 05:57:07 2001
3
@@ -3,22 +3,30 @@
4
 #If you do not have gcc, change the setting for COMPILER, but you must
5
 #use an ANSI standard C compiler (NOT the old SunOS 4.1.3 cc
6
 #compiler; get gcc if you are still using it). 
7
-COMPILER=gcc
8
+COMPILER=${CC}
9
 
10
 #If the ar command fails on your system, consult the ar manpage
11
 #for your system. 
12
-AR=ar
13
+#AR=ar
14
 
15
 #If you don't have FreeType, libjpeg and/or Xpm installed, including the
16
 #header files, uncomment this (default). You really must install
17
 #libpng and zlib to get anywhere if you wish to create PNG images.
18
-CFLAGS=-O -DHAVE_LIBPNG -DHAVE_LIBJPEG
19
+CFLAGS+=-DHAVE_LIBPNG -DHAVE_LIBJPEG -DHAVE_LIBFREETYPE
20
 
21
 #If you do have FreeType, libjpeg and/or Xpm fully installed, uncomment a
22
 #variation of this and comment out the line above. See also LIBS below.
23
 #CFLAGS=-O -DHAVE_LIBXPM -DHAVE_LIBPNG -DHAVE_LIBJPEG \
24
 #	-DHAVE_LIBFREETYPE -DHAVE_LIBTTF 
25
 
26
+.if defined(WITH_X11)
27
+CFLAGS+=-DHAVE_XPM 
28
+.endif
29
+
30
+.if defined(JISX0208)
31
+CFLAGS+=-DJISX0208
32
+.endif
33
+
34
 #To use the old FreeType 1.x library, add this additional #define
35
 #to the line above
36
 #-DHAVE_LIBTTF 
37
@@ -30,7 +38,7 @@
38
 #Some systems are very picky about link order. They don't all agree
39
 #on the right order, either.
40
 
41
-LIBS=-lgd -lpng -lz -lm
42
+LIBS=-lgd -lpng -lz -ljpeg -lm `$(FREETYPE_CONFIG) --libs`
43
 
44
 #If you do have FreeType, JPEG and/or Xpm fully installed, uncomment a 
45
 #variation of this and comment out the line above. Note that
46
@@ -38,6 +46,10 @@
47
 
48
 #LIBS=-lgd -lpng -lz -ljpeg -lfreetype -lm -lttf
49
 
50
+.if defined(WITH_X11)
51
+LIBS+=-lXpm -lX11
52
+.endif
53
+
54
 #Note: for Freetype 1.x, use DHAVE_LIBTTF and -lttf instead.
55
 
56
 #Typical install locations for freetype, zlib, xpm, libjpeg and libpng header 
57
@@ -45,7 +57,11 @@
58
 #ensure that the version of gd you are installing is used, and not an 
59
 #older release in your directory tree somewhere.
60
 
61
-INCLUDEDIRS=-I. -I/usr/include/freetype2 -I/usr/include/X11 -I/usr/X11R6/include/X11 -I/usr/local/include 
62
+INCLUDEDIRS=-I. `$(FREETYPE_CONFIG) --cflags` -I${LOCALBASE}/include
63
+
64
+.if defined(WITH_X11)
65
+INCLUDEDIRS+=-I${X11BASE}/include/X11 -I${X11BASE}/include
66
+.endif
67
 
68
 #Typical install locations for freetype, zlib, xpm and libpng libraries.
69
 #If yours are somewhere else, other than a standard location
70
@@ -55,16 +71,20 @@
71
 #on your system can't cause conflicts while building a new one.
72
 #This line shouldn't hurt if you don't actually have some of the
73
 #optional libraries and directories.
74
-LIBDIRS=-L. -L/usr/local/lib -L/usr/lib/X11 -L/usr/X11R6/lib
75
+LIBDIRS=-L. -L${LOCALBASE}/lib -Wl,--rpath,${LOCALBASE}/lib
76
+
77
+.if defined(WITH_X11)
78
+LIBDIRS+=-L${X11BASE}/lib -Wl,--rpath,${X11BASE}/lib
79
+.endif
80
 
81
 #Location where libgd.a should be installed by "make install".
82
-INSTALL_LIB=/usr/local/lib
83
+INSTALL_LIB=${PREFIX}/lib
84
 
85
 #Location where .h files should be installed by "make install".
86
-INSTALL_INCLUDE=/usr/local/include
87
+INSTALL_INCLUDE=${PREFIX}/include/gd
88
 
89
 #Location where useful non-test programs should be installed by "make install".
90
-INSTALL_BIN=/usr/local/bin
91
+INSTALL_BIN=${PREFIX}/bin
92
 
93
 #
94
 #
95
@@ -74,34 +94,43 @@
96
 
97
 VERSION=1.8.4
98
 
99
-CC=$(COMPILER) $(INCLUDEDIRS)
100
-LINK=$(CC) $(LIBDIRS) $(LIBS)
101
+CFLAGS+=$(INCLUDEDIRS)
102
 
103
 PROGRAMS=$(BIN_PROGRAMS) $(TEST_PROGRAMS)
104
 
105
 BIN_PROGRAMS=pngtogd pngtogd2 gdtopng gd2topng gd2copypal gdparttopng webpng
106
 TEST_PROGRAMS=gdtest gddemo gd2time gdtestft gdtestttf
107
 
108
-all: libgd.a $(PROGRAMS)
109
+SOVER=2
110
+
111
+.SUFFIXES: .c .so .o
112
 
113
-install: libgd.a $(BIN_PROGRAMS)
114
-	sh ./install-item 644 libgd.a $(INSTALL_LIB)/libgd.a
115
-	sh ./install-item 755 pngtogd $(INSTALL_BIN)/pngtogd
116
-	sh ./install-item 755 pngtogd2 $(INSTALL_BIN)/pngtogd2
117
-	sh ./install-item 755 gdtopng $(INSTALL_BIN)/gdtopng
118
-	sh ./install-item 755 gd2topng $(INSTALL_BIN)/gd2topng
119
-	sh ./install-item 755 gd2copypal $(INSTALL_BIN)/gd2copypal
120
-	sh ./install-item 755 gdparttopng $(INSTALL_BIN)/gdparttopng
121
-	sh ./install-item 755 webpng $(INSTALL_BIN)/webpng
122
-	sh ./install-item 755 bdftogd $(INSTALL_BIN)/bdftogd
123
-	sh ./install-item 644 gd.h $(INSTALL_INCLUDE)/gd.h
124
-	sh ./install-item 644 gdcache.h $(INSTALL_INCLUDE)/gdcache.h
125
-	sh ./install-item 644 gd_io.h $(INSTALL_INCLUDE)/gd_io.h
126
-	sh ./install-item 644 gdfontg.h $(INSTALL_INCLUDE)/gdfontg.h
127
-	sh ./install-item 644 gdfontl.h $(INSTALL_INCLUDE)/gdfontl.h
128
-	sh ./install-item 644 gdfontmb.h $(INSTALL_INCLUDE)/gdfontmb.h
129
-	sh ./install-item 644 gdfonts.h $(INSTALL_INCLUDE)/gdfonts.h
130
-	sh ./install-item 644 gdfontt.h $(INSTALL_INCLUDE)/gdfontt.h
131
+.c.so:
132
+	$(CC) -fpic -DPIC $(CFLAGS) -o $@ -c $<
133
+
134
+all: libgd.a libgd.so.$(SOVER) $(PROGRAMS)
135
+
136
+install: libgd.a libgd.so.$(SOVER) $(BIN_PROGRAMS)
137
+	-mkdir -p $(INSTALL_LIB) $(INSTALL_INCLUDE) $(INSTALL_BIN)
138
+	${BSD_INSTALL_DATA} libgd.a $(INSTALL_LIB)/libgd.a
139
+	${BSD_INSTALL_DATA} libgd.so.$(SOVER) $(INSTALL_LIB)/libgd.so.$(SOVER)
140
+	-ln -sf libgd.so.$(SOVER) $(INSTALL_LIB)/libgd.so
141
+	${BSD_INSTALL_PROGRAM} pngtogd $(INSTALL_BIN)/pngtogd
142
+	${BSD_INSTALL_PROGRAM} pngtogd2 $(INSTALL_BIN)/pngtogd2
143
+	${BSD_INSTALL_PROGRAM} gdtopng $(INSTALL_BIN)/gdtopng
144
+	${BSD_INSTALL_PROGRAM} gd2topng $(INSTALL_BIN)/gd2topng
145
+	${BSD_INSTALL_PROGRAM} gd2copypal $(INSTALL_BIN)/gd2copypal
146
+	${BSD_INSTALL_PROGRAM} gdparttopng $(INSTALL_BIN)/gdparttopng
147
+	${BSD_INSTALL_PROGRAM} webpng $(INSTALL_BIN)/webpng
148
+	${BSD_INSTALL_SCRIPT} bdftogd $(INSTALL_BIN)/bdftogd
149
+	${BSD_INSTALL_DATA} gd.h $(INSTALL_INCLUDE)/gd.h
150
+	${BSD_INSTALL_DATA} gdcache.h $(INSTALL_INCLUDE)/gdcache.h
151
+	${BSD_INSTALL_DATA} gd_io.h $(INSTALL_INCLUDE)/gd_io.h
152
+	${BSD_INSTALL_DATA} gdfontg.h $(INSTALL_INCLUDE)/gdfontg.h
153
+	${BSD_INSTALL_DATA} gdfontl.h $(INSTALL_INCLUDE)/gdfontl.h
154
+	${BSD_INSTALL_DATA} gdfontmb.h $(INSTALL_INCLUDE)/gdfontmb.h
155
+	${BSD_INSTALL_DATA} gdfonts.h $(INSTALL_INCLUDE)/gdfonts.h
156
+	${BSD_INSTALL_DATA} gdfontt.h $(INSTALL_INCLUDE)/gdfontt.h
157
 
158
 gddemo: gddemo.o libgd.a
159
 	$(CC) gddemo.o -o gddemo	$(LIBDIRS) $(LIBS)
160
@@ -138,18 +167,21 @@
161
 gdtestttf: gdtestttf.o libgd.a
162
 	$(CC) --verbose gdtestttf.o -o gdtestttf $(LIBDIRS) $(LIBS)
163
 
164
-libgd.a: gd.o gd_gd.o gd_gd2.o gd_io.o gd_io_dp.o gd_io_file.o gd_ss.o \
165
+OBJS= gd.o gd_gd.o gd_gd2.o gd_io.o gd_io_dp.o gd_io_file.o gd_ss.o \
166
 	gd_io_ss.o gd_png.o gd_jpeg.o gdxpm.o gdfontt.o gdfonts.o gdfontmb.o gdfontl.o \
167
 	gdfontg.o gdtables.o gdft.o gdttf.o gdcache.o gdkanji.o wbmp.o \
168
-	gd_wbmp.o gdhelpers.o gd.h gdfontt.h gdfonts.h gdfontmb.h gdfontl.h \
169
+	gd_wbmp.o gdhelpers.o gd_gif_in.o
170
+INCS= gd.h gdfontt.h gdfonts.h gdfontmb.h gdfontl.h \
171
 	gdfontg.h gdhelpers.h
172
+
173
+libgd.a:	$(INCS) $(OBJS)
174
 	rm -f libgd.a
175
-	$(AR) rc libgd.a gd.o gd_gd.o gd_gd2.o gd_io.o gd_io_dp.o \
176
-		gd_io_file.o gd_ss.o gd_io_ss.o gd_png.o gd_jpeg.o gdxpm.o \
177
-		gdfontt.o gdfonts.o gdfontmb.o gdfontl.o gdfontg.o \
178
-		gdtables.o gdft.o gdttf.o gdcache.o gdkanji.o wbmp.o \
179
-		gd_wbmp.o gdhelpers.o
180
+	$(AR) rc libgd.a $(OBJS)
181
 	-ranlib libgd.a
182
+
183
+libgd.so.$(SOVER):	$(INCS) $(OBJS:.o=.so)
184
+	$(CC) -shared -Wl,-x,-soname,$@ -o $@ $(OBJS:.o=.so) $(LIBDIRS) $(LIBS)
185
+	ln -sf libgd.so.$(SOVER) libgd.so
186
 
187
 clean:
188
 	rm -f *.o *.a ${PROGRAMS} test/gdtest.jpg test/gdtest.wbmp
(-)graphics/gd/files/patch-gd.h (+41 lines)
Line 0 Link Here
1
--- gd.h	Thu Jan 16 11:28:09 2003
2
+++ gd.h	Mon Mar 24 16:26:14 2003
3
@@ -209,4 +209,8 @@
4
   gdImagePtr gdImageCreateFromPngSource (gdSourcePtr in);
5
 
6
+  gdImagePtr gdImageCreateFromGif(FILE *fd);
7
+  gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr in);
8
+  gdImagePtr gdImageCreateFromGifSource(gdSourcePtr in);
9
+
10
   gdImagePtr gdImageCreateFromGd (FILE * in);
11
   gdImagePtr gdImageCreateFromGdCtx (gdIOCtxPtr in);
12
@@ -300,4 +304,5 @@
13
 
14
   void gdImagePolygon (gdImagePtr im, gdPointPtr p, int n, int c);
15
+  void gdImageOpenPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
16
   void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c);
17
 
18
@@ -395,4 +400,13 @@
19
   void *gdImageJpegPtr (gdImagePtr im, int *size, int quality);
20
 
21
+  void gdImageLzw(gdImagePtr im, FILE *out);
22
+  void* gdImageLzwPtr(gdImagePtr im, int *size);
23
+  void gdImageLzwCtx(gdImagePtr im, gdIOCtxPtr out);
24
+
25
+  void gdImageBigGif(gdImagePtr im, FILE *out);
26
+  void* gdImageBigGifPtr(gdImagePtr im, int *size);
27
+  void gdImageBigGifCtx(gdImagePtr im, gdIOCtxPtr out);
28
+
29
+
30
 /* A custom data sink. For backwards compatibility. Use
31
 	gdIOCtx instead. */
32
@@ -408,4 +422,9 @@
33
 
34
   void gdImagePngToSink (gdImagePtr im, gdSinkPtr out);
35
+
36
+  void gdImageGif(gdImagePtr im, FILE *out);
37
+  void* gdImageGifPtr(gdImagePtr im, int *size);
38
+  void gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
39
+  void gdImageGifToSink(gdImagePtr im, gdSinkPtr out);
40
 
41
   void gdImageGd (gdImagePtr im, FILE * out);
(-)graphics/gd/files/patch-gdft.c (+92 lines)
Line 0 Link Here
1
--- gdft.c	Thu Jan 16 13:29:32 2003
2
+++ gdft.c	Mon Mar 24 17:09:54 2003
3
@@ -546,8 +546,8 @@
4
 	  a->tweencolor = gdTrueColorAlpha (gdTrueColorGetRed (fg),
5
 					    gdTrueColorGetGreen (fg),
6
 					    gdTrueColorGetBlue (fg),
7
-					    gdAlphaMax -
8
-					    (gdTrueColorGetAlpha (fg) *
9
+					    gdAlphaMax - ((gdAlphaMax -
10
+					    gdTrueColorGetAlpha (fg)) *
11
 					     pixel / NUMCOLORS));
12
 	}
13
       else
14
@@ -680,10 +680,14 @@
15
       if (y >= im->sy || y < 0)
16
 	continue;
17
 
18
-      for (col = 0; col < bitmap.width; col++, pc++)
19
-	{
20
-	  if (bitmap.pixel_mode == ft_pixel_mode_grays)
21
-	    {
22
+      for (col = 0; col < bitmap.width; col++, pc++) {
23
+	x = pen_x + col;
24
+	/* clip if out of bounds */
25
+	if (x >= im->sx || x < 0)
26
+	  continue;
27
+
28
+	switch(bitmap.pixel_mode) {
29
+	case ft_pixel_mode_grays:
30
 	      /*
31
 	       * Round to NUMCOLORS levels of antialiasing for
32
 	       * index color images since only 256 colors are
33
@@ -692,44 +696,38 @@
34
 	      tc_key.pixel = ((bitmap.buffer[pc] * NUMCOLORS)
35
 			      + bitmap.num_grays / 2)
36
 		/ (bitmap.num_grays - 1);
37
-	    }
38
-	  else if (bitmap.pixel_mode == ft_pixel_mode_mono)
39
-	    {
40
-	      tc_key.pixel = ((bitmap.buffer[pc / 8]
41
-			       << (pc % 8)) & 128) ? NUMCOLORS : 0;
42
+	      break;
43
+	case ft_pixel_mode_mono:
44
 	      /* 2.0.5: mode_mono fix from Giuliano Pochini */
45
 	      tc_key.pixel =
46
 		((bitmap.
47
 		  buffer[(col >> 3) +
48
 			 pcr]) & (1 << (~col & 0x07))) ? NUMCOLORS : 0;
49
-	    }
50
-	  else
51
-	    {
52
+	      break;
53
+	default:
54
 	      return "Unsupported ft_pixel_mode";
55
-	    }
56
+	}
57
+
58
 	  if (tc_key.pixel > 0)	/* if not background */
59
 	    {
60
-	      x = pen_x + col;
61
-
62
-	      /* clip if out of bounds */
63
-	      if (x >= im->sx || x < 0)
64
-		continue;
65
-	      /* get pixel location in gd buffer */
66
-	      pixel = &im->pixels[y][x];
67
-	      if (tc_key.pixel == NUMCOLORS)
68
-		{
69
+	      if (im->trueColor) {
70
+		tc_elem = (tweencolor_t *)gdCacheGet(tc_cache, &tc_key);
71
+		gdImageSetPixel(im, x, y, tc_elem->tweencolor);
72
+	      } else {
73
+	        /* get pixel location in gd buffer */
74
+	        pixel = &im->pixels[y][x];
75
+	        if (tc_key.pixel == NUMCOLORS)
76
 		  /* use fg color directly. gd 2.0.2: watch out for
77
 		     negative indexes (thanks to David Marwood). */
78
 		  *pixel = (fg < 0) ? -fg : fg;
79
-		}
80
-	      else
81
-		{
82
+	        else {
83
 		  /* find antialised color */
84
 
85
 		  tc_key.bgcolor = *pixel;
86
 		  tc_elem = (tweencolor_t *) gdCacheGet (tc_cache, &tc_key);
87
 		  *pixel = tc_elem->tweencolor;
88
 		}
89
+	      }
90
 	    }
91
 	}
92
     }
(-)graphics/gd/files/patch-gdkanji.c (-87 lines)
Lines 1-87 Link Here
1
--- gdkanji.c.orig	Fri Feb  2 05:23:56 2001
2
+++ gdkanji.c	Wed Feb  7 20:59:08 2001
3
@@ -103,14 +103,18 @@
4
 unsigned char *str;
5
 #endif
6
 {
7
-	static int whatcode;
8
+	static int whatcode = ASCII;
9
+	int oldcode = ASCII;
10
 	int c, i;
11
 	char *lang = NULL;
12
 
13
 	c = '\1';
14
 	i = 0;
15
 
16
-	if (whatcode == 0) whatcode = ASCII;
17
+	if (whatcode != EUCORSJIS && whatcode != ASCII) {
18
+		oldcode = whatcode;
19
+		whatcode = ASCII;
20
+	}
21
 	
22
 	while ((whatcode == EUCORSJIS || whatcode == ASCII) && c != '\0') {
23
 		if ((c = str[i++]) != '\0') {
24
@@ -167,7 +171,7 @@
25
 				if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160))
26
 					whatcode = SJIS;
27
 				else
28
-				if (c >= 253 && c >= 254) whatcode = EUC;
29
+				if (c >= 253 && c <= 254) whatcode = EUC;
30
 				else
31
 				if (c >= 161 && c <= 252) whatcode = EUCORSJIS;
32
 			}
33
@@ -184,6 +188,8 @@
34
 		debug("Kanji code detected at %d byte.", i);
35
 #endif
36
 
37
+	if (whatcode == EUCORSJIS && oldcode != ASCII) whatcode = oldcode;
38
+
39
 	if (whatcode == EUCORSJIS) {
40
 		if (getenv ("LC_ALL")) lang = getenv ("LC_ALL");
41
 		else
42
@@ -310,7 +316,7 @@
43
 			error("invalid code specification: \"%s\" or \"%s\"",
44
 								EUCSTR, code);
45
 #endif
46
-		strcpy(to, from);
47
+		ustrcpy(to, from);
48
 		return;
49
 	}
50
 
51
@@ -328,7 +334,7 @@
52
 		else
53
 #endif
54
 			error("something happen");
55
-		strcpy(to, from);
56
+		ustrcpy(to, from);
57
 		return;
58
 	}
59
 
60
@@ -526,11 +532,10 @@
61
 
62
 	t = (unsigned char *)gdMalloc(BUFSIZ);
63
 	any2eucjp(t, s, BUFSIZ);
64
-	i = strlen(t);
65
+	i = strlen((const char *)t);
66
 	gdFree(t);
67
 	return i;
68
 }
69
-#endif
70
 
71
 #ifdef DEBUG
72
 int main()
73
@@ -543,7 +548,7 @@
74
 	while ( (c = fgetc(stdin)) != '\n' && i < BUFSIZ ) input[i++] = c;
75
 	input[i] = '\0';
76
 
77
-	printf("input : %d bytes\n", strlen(input));
78
+	printf("input : %d bytes\n", strlen((const char *)input));
79
 	printf("output: %d bytes\n", strwidth(input));
80
 
81
 	output = (unsigned char *)gdMalloc(BUFSIZ);
82
@@ -555,4 +560,5 @@
83
 
84
 	return 0;
85
 }
86
+#endif
87
 #endif
(-)graphics/gd/files/patch-gdtest.c (+81 lines)
Line 0 Link Here
1
--- gdtest.c.orig	Thu Apr 25 14:16:47 2002
2
+++ gdtest.c	Tue Nov  6 15:53:38 2001
3
@@ -84,6 +84,35 @@
4
   gdImageDestroy (im2);
5
   ctx->gd_free (ctx);
6
 
7
+        /* */
8
+        /* Send to GIF File then Ptr */
9
+        /* */
10
+        sprintf(of, "%s.gif", argv[1]);
11
+        out = fopen(of, "wb");
12
+        gdImageGif(im, out);
13
+        fclose(out);
14
+
15
+        in = fopen(of, "rb");
16
+        if (!in) {
17
+                fprintf(stderr, "GIF Output file does not exist!\n");
18
+                exit(1);
19
+        }
20
+        im2 = gdImageCreateFromGif(in);
21
+        fclose(in);
22
+
23
+        CompareImages("GD->GIF File->GD", ref, im2);
24
+
25
+        unlink(of);
26
+        gdImageDestroy(im2);
27
+
28
+	iptr = gdImageGifPtr(im,&sz);
29
+	ctx = gdNewDynamicCtx(sz,iptr);
30
+	im2 = gdImageCreateFromGifCtx(ctx);
31
+
32
+        CompareImages("GD->GIF ptr->GD", ref, im2);
33
+
34
+	gdImageDestroy(im2);
35
+	ctx->gd_free(ctx);
36
 
37
   /* */
38
   /* Send to GD2 File then Ptr */
39
@@ -200,7 +229,30 @@
40
       gdImageDestroy (im2);
41
     };
42
 
43
-  unlink (of);
44
+	/*
45
+	** Test gdImageCreateFromGifSource
46
+	**/
47
+
48
+	in = fopen(of, "rb");
49
+
50
+	if (!in)
51
+	  {
52
+		fprintf (stderr, "GD Source: ERROR - GD Source input file does not exist - Sink may have failed!\n");
53
+	  };
54
+
55
+	imgsrc.source = freadWrapper;
56
+	imgsrc.context = in;
57
+	im2 = gdImageCreateFromGifSource(&imgsrc);
58
+	fclose(in);
59
+
60
+	if (im2 == NULL) {
61
+		printf("GD Source (GIF): ERROR Null returned by gdImageCreateFromGifSource\n");
62
+	} else {
63
+		CompareImages("GD Source (GIF)", ref, im2);
64
+		gdImageDestroy(im2);
65
+	};
66
+
67
+	unlink(of);
68
 
69
   /* */
70
   /*  Test Extraction */
71
@@ -266,6 +318,10 @@
72
 
73
   printf ("[Merged Image has %d colours]\n", im2->colorsTotal);
74
   CompareImages ("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
75
+
76
+  out = fopen ("test/gdtest_merge_out.png", "wb");
77
+  gdImagePng(im2, out);
78
+  close(out);
79
 
80
   gdImageDestroy (im2);
81
   gdImageDestroy (im3);
(-)graphics/gd/files/patch-gdttf.c (-17 lines)
Lines 1-17 Link Here
1
--- gdttf.c.orig	Fri Feb  2 05:23:56 2001
2
+++ gdttf.c	Wed Feb  7 20:57:34 2001
3
@@ -221,11 +221,10 @@
4
     byte = *((unsigned char *) str);
5
 #ifdef JISX0208
6
     if (0xA1 <= byte && byte <= 0xFE) {
7
-	int jiscode, ku, ten;
8
+	int ku, ten;
9
 
10
-	jiscode = 0x100 * (byte & 0x7F) + (str[1] & 0x7F);
11
-	ku = (jiscode >> 8) - 0x20;
12
-	ten = (jiscode % 256) - 0x20;
13
+	ku = (byte & 0x7F) - 0x20;
14
+	ten = (str[1] & 0x7F) - 0x20;
15
 	if ( (ku < 1 || ku > 92) || (ten < 1 || ten > 94) ) {
16
     		*chPtr = (Tcl_UniChar) byte;
17
     		return 1;
(-)graphics/gd/files/patch-gif (-15 lines)
Lines 1-15 Link Here
1
This  patch  together  with  the old  implementation  (from  gd-1.5)  of
2
gd_gif_in.c restore GD's ability to read GIF images. The writing of such
3
images is troubled because of Unisys' zeal in patent enforcement.
4
5
--- gd.h	Tue Feb  6 14:44:01 2001
6
+++ gd.h	Fri Jul 27 16:10:38 2001
7
@@ -95,2 +95,4 @@
8
 gdImagePtr gdImageCreate(int sx, int sy);
9
+gdImagePtr gdImageCreateFromGif(FILE *fd);
10
+gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr in);
11
 gdImagePtr gdImageCreateFromPng(FILE *fd);
12
--- gd_gif_in.c	Fri Jul 27 16:13:45 2001
13
+++ gd_gif_in.c	Fri Jul 27 16:14:46 2001
14
@@ -6,1 +6,0 @@
15
-#include "io.h"
(-)graphics/gd/files/patch-index.html (+71 lines)
Line 0 Link Here
1
--- index.html	Mon Nov 25 11:39:31 2002
2
+++ index.html	Wed Nov 27 12:47:38 2002
3
@@ -35,4 +35,9 @@
4
 modern image formats such as PNG and JPEG as soon as possible.
5
 
6
+<p>Note, that the FreeBSD port of gd2 includes support for GIF files
7
+ported from the earlier releases of gd. If the WITH_LZW was defined at
8
+build time, the software will also use LZW-compression when creating
9
+GIF files.
10
+
11
 <p>
12
 gd 2.0.8 <strong>requires</strong> that the following libraries 
13
@@ -116,4 +121,18 @@
14
 Portions relating to WBMP copyright 2000, 2001, 2002 Maurice Szmurlo and Johan Van 
15
 den Brande.
16
+
17
+<p>
18
+GIF decompression code copyright 1990, 1991, 1993, by David Koblas
19
+(koblas@netcom.com).
20
+<p>
21
+Non-LZW-based GIF compression code copyright 1998, by Hutchison Avenue
22
+Software Corporation (<a href="http://www.hasc.com">http://www.hasc.com/</a>,
23
+info@hasc.com).
24
+<p>
25
+LZW-based GIF compression code David Rowley.
26
+Obtaining a license for the Unisys LZW compression patent is
27
+entirely between the user and Unisys. The authors of gd can provide
28
+NO assistance in this matter.
29
+
30
 <p>
31
 <strong>Permission has been granted to copy, distribute and modify gd in any
32
@@ -192,5 +211,22 @@
33
 <li><a href="http://martin.gleeson.com/fly/">fly</a>, by Martin Gleeson
34
 </ul>
35
-<P>
36
+
37
+<P><A NAME="gifpatch"><H3>What does the FreeBSD port add?</H3></A>
38
+
39
+<p>This version reinstates GIF support. Specifically, the following
40
+functions are added: gdImageGif, gdImageGifPtr, gdImageGifCtx,
41
+gdImageGifToSink, gdImageCreateFromGif, gdImageCreateFromGifCtx,
42
+gdImageCreateFromGifSource.
43
+The can be used just like the corresponding functions for the other
44
+image formats.
45
+
46
+<p>Other functions added, but not documented, are: gdImageLzw,
47
+gdImageLzwPtr, gdImageLzwCtx, gdImageBigGif, gdImageBigGifPtr,
48
+gdImageBigGifCtx.
49
+
50
+<p>The <a href=#gdImageOpenPolygon>gdImageOpenPolygon</a> is added.
51
+This is basically the same as <a href=#gdImagePolygon>gdImagePolygon</A>,
52
+but it does not join the start and end points. It is required by GD.pm.
53
+
54
 <A NAME="whatsnew2.0.8"><H3>What's new in version 2.0.8?</H3></A>
55
 <P>
56
@@ -1058,5 +1094,6 @@
57
 <DD>
58
 Represents a point in the coordinate space of the image; used
59
-by <A HREF="#gdImagePolygon">gdImagePolygon</A> and
60
+by <A HREF="#gdImagePolygon">gdImagePolygon</A>,
61
+<A HREF="#gdImageOpenPolygon">gdImageOpenPolygon</A>, and
62
 <A HREF="#gdImageFilledPolygon">gdImageFilledPolygon</A>.
63
 <PRE>
64
@@ -1068,5 +1105,6 @@
65
 <DD>
66
 A pointer to a <A HREF="#gdPoint">gdPoint</A> structure; passed
67
-as an argument to <A HREF="#gdImagePolygon">gdImagePolygon</A>
68
+as an argument to <A HREF="#gdImagePolygon">gdImagePolygon</A>,
69
+<A HREF="#gdImageOpenPolygon">gdImageOpenPolygon</A>,
70
 and <A HREF="#gdImageFilledPolygon">gdImageFilledPolygon</A>.
71
 </DL>
(-)graphics/gd/pkg-descr (-20 / +13 lines)
Lines 1-23 Link Here
1
DESCRIPTION
1
gd is a graphics library. It allows your code to quickly draw images
2
complete with lines, arcs, text, multiple colors, cut and paste from
3
other images, and flood fills, and write out the result as a PNG, GIF
4
or JPEG file. This is particularly useful in World Wide Web applications,
5
where PNG, GIF and JPEG are three of the formats accepted for inline
6
images by most browsers.
2
7
3
	gd is a graphics library. It allows your code to quickly draw images
8
gd does  not provide for every possible desirable graphics operation,
4
	complete with lines, arcs, text, multiple colors, cut and paste from
9
but version 2.0 does include most frequently requested features,
5
	other images, and flood fills, and write out the result as a .PNG file.
10
including both truecolor and palette images, resampling (smooth
6
	This is particularly useful in World Wide Web applications, where .PNG
11
resizing of truecolor images) and so forth.
7
	is the format used for inline images. 
8
9
	gd is not a paint program. If you are looking for a paint program, you
10
	are looking in the wrong place. If you are not a programmer, you are
11
	looking in the wrong place.
12
13
	gd does not provide for every possible desirable graphics operation.
14
	It is not necessary or desirable for gd to become a kitchen-sink
15
	graphics package, but version 1.7.3 incorporates most of the commonly
16
	requested features for an 8-bit 2D package. Support for truecolor
17
	images, JPEG and truecolor PNG is planned for version 2.0.
18
19
AUTHOR
20
	gd was written by Thomas Boutell and is currently distributed by 
21
	boutell.com, Inc. 
22
12
23
WWW: http://www.boutell.com/gd/
13
WWW: http://www.boutell.com/gd/
14
15
- Alex Dupre
16
sysadmin@alexdupre.com
(-)graphics/gd/pkg-plist (-10 / +13 lines)
Lines 1-22 Link Here
1
bin/annotate
1
bin/bdftogd
2
bin/bdftogd
2
bin/gd2copypal
3
bin/gd2copypal
4
bin/gd2togif
3
bin/gd2topng
5
bin/gd2topng
6
bin/gdcmpgif
4
bin/gdparttopng
7
bin/gdparttopng
5
bin/gdtopng
8
bin/gdtopng
9
bin/giftogd2
6
bin/pngtogd
10
bin/pngtogd
7
bin/pngtogd2
11
bin/pngtogd2
8
bin/webpng
12
bin/webpng
9
include/gd/gd.h
13
include/gd.h
10
include/gd/gd_io.h
14
include/gd_io.h
11
include/gd/gdcache.h
15
include/gdcache.h
12
include/gd/gdfontg.h
16
include/gdfontg.h
13
include/gd/gdfontl.h
17
include/gdfontl.h
14
include/gd/gdfontmb.h
18
include/gdfontmb.h
15
include/gd/gdfonts.h
19
include/gdfonts.h
16
include/gd/gdfontt.h
20
include/gdfontt.h
17
lib/libgd.a
21
lib/libgd.a
18
lib/libgd.so
22
lib/libgd.so
19
lib/libgd.so.2
23
lib/libgd.so.4
20
%%PORTDOCS%%share/doc/gd/index.html
24
%%PORTDOCS%%share/doc/gd/index.html
21
%%PORTDOCS%%@dirrm share/doc/gd
25
%%PORTDOCS%%@dirrm share/doc/gd
22
@dirrm include/gd
(-)graphics/gd/scripts/configure (+44 lines)
Line 0 Link Here
1
#!/bin/sh
2
# $FreeBSD$
3
4
# The GD_FONTS environment variable can be set to specify the gzipped
5
# tar-ball containing the fonts in bdf format and the bdf file names.
6
#
7
# For example:
8
# GD_FONTS="/usr/ports/distfiles/x-koi8u.tgz koi6x10.bdf koi8x13.bdf \
9
#	koi9x15.bdf koi12x24.bdf koi10x20.bdf"
10
#
11
# This can be usefull for slave ports, like ukrainian/gd, which may
12
# now provide alternative fonts easily.
13
14
#	TODO: . handle multiple archiving formats: tgz, tar.bz2, zip
15
#	      . allow for passing already extracted font-files
16
17
if [ -z "$GD_FONTS" ]
18
then
19
	echo "GD_FONTS can be set to specify an alternative list of .bdf files"
20
	echo "See $0 for details..."
21
	exit 0
22
fi
23
24
set $GD_FONTS
25
#
26
# The  tarball is  the  first argument,  the  tiny, small,  medium-bold,
27
# large, and giant fonts follow.
28
#
29
30
tarball=$1
31
shift
32
tar -xvzpf $tarball -C $WRKSRC $@
33
34
rm -f $WRKSRC/Makefile.fonts
35
36
for font in Tiny Small MediumBold Large Giant
37
do
38
	f=`echo $font | tr -d [[:lower:]] | tr [[:upper:]] [[:lower:]]`
39
	rm -f $WRKSRC/gdfont$f.[ch]
40
	printf 'gdfont%s.c gdfont%s.h: %s\
41
		perl ${.CURDIR}/bdftogd gdFont%s font%s < %s\n' \
42
		$f $f $1 $font $f $1 >> $WRKSRC/Makefile.fonts
43
	shift
44
done

Return to bug 53582