~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to gdchart0.94c/gd-1.8.3/gd_gd.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <math.h>
3
 
#include <string.h>
4
 
#include <stdlib.h>
5
 
#include "gd.h"
6
 
 
7
 
#define TRUE 1
8
 
#define FALSE 0
9
 
 
10
 
/* Exported functions: */
11
 
extern void gdImageGd(gdImagePtr im, FILE *out);
12
 
 
13
 
 
14
 
/* Use this for commenting out debug-print statements. */
15
 
/* Just use the first '#define' to allow all the prints... */
16
 
/*#define GD2_DBG(s) (s) */
17
 
#define GD2_DBG(s)
18
 
 
19
 
/* */
20
 
/* Shared code to read color tables from gd file. */
21
 
/* */
22
 
int _gdGetColors(gdIOCtx *in, gdImagePtr im)
23
 
{
24
 
        int i;
25
 
 
26
 
        if (!gdGetByte(&im->colorsTotal, in)) {
27
 
                goto fail1;
28
 
        }
29
 
        if (!gdGetWord(&im->transparent, in)) {
30
 
                goto fail1;
31
 
        }
32
 
        if (im->transparent == 257) {
33
 
                im->transparent = (-1);
34
 
        }
35
 
 
36
 
        GD2_DBG(printf("Pallette had %d colours (T=%d)\n",im->colorsTotal, im->transparent));
37
 
 
38
 
        for (i=0; (i<gdMaxColors); i++) {
39
 
                if (!gdGetByte(&im->red[i], in)) {
40
 
                        goto fail1;
41
 
                }
42
 
                if (!gdGetByte(&im->green[i], in)) {
43
 
                        goto fail1;
44
 
                }
45
 
                if (!gdGetByte(&im->blue[i], in)) {
46
 
                        goto fail1;
47
 
                }
48
 
        }
49
 
 
50
 
        for (i=0; (i < im->colorsTotal); i++) {
51
 
           im->open[i] = 0;
52
 
        };
53
 
 
54
 
        return TRUE;
55
 
fail1:
56
 
        return FALSE;
57
 
}
58
 
 
59
 
/* */
60
 
/* Use the common basic header info to make the image object. */
61
 
/* This is also called from _gd2CreateFromFile */
62
 
/* */
63
 
static
64
 
gdImagePtr _gdCreateFromFile(gdIOCtx *in, int *sx, int *sy)
65
 
{
66
 
        gdImagePtr im;
67
 
 
68
 
        if (!gdGetWord(sx, in)) {
69
 
                goto fail1;
70
 
        }
71
 
        if (!gdGetWord(sy, in)) {
72
 
                goto fail1;
73
 
        }
74
 
 
75
 
        GD2_DBG(printf("Image is %dx%d\n", *sx, *sy));
76
 
 
77
 
        im = gdImageCreate(*sx, *sy);
78
 
 
79
 
        if (!_gdGetColors(in, im)) {
80
 
                goto fail2;
81
 
        }
82
 
 
83
 
        return im;
84
 
fail2:
85
 
        gdImageDestroy(im);
86
 
fail1:
87
 
        return 0;
88
 
}
89
 
 
90
 
gdImagePtr gdImageCreateFromGd(FILE *inFile)
91
 
{
92
 
        gdImagePtr      im;
93
 
        gdIOCtx         *in;
94
 
 
95
 
        in = gdNewFileCtx(inFile);
96
 
        im = gdImageCreateFromGdCtx(in);
97
 
 
98
 
        in->free(in);
99
 
 
100
 
        return im;
101
 
}
102
 
 
103
 
gdImagePtr gdImageCreateFromGdCtx(gdIOCtxPtr in)
104
 
{
105
 
        int sx, sy;
106
 
        int x, y;
107
 
        gdImagePtr im;
108
 
 
109
 
        /* Read the header */
110
 
        im = _gdCreateFromFile(in, &sx, &sy);
111
 
 
112
 
        if (im == NULL) {
113
 
                goto fail1;
114
 
        };
115
 
 
116
 
        /* Then the data... */
117
 
        for (y=0; (y<sy); y++) {
118
 
                for (x=0; (x<sx); x++) {        
119
 
                        int ch;
120
 
                        ch = gdGetC(in);
121
 
                        if (ch == EOF) {
122
 
                                goto fail2;
123
 
                        }
124
 
                        /* ROW-MAJOR IN GD 1.3 */
125
 
                        im->pixels[y][x] = ch;
126
 
                }
127
 
        }
128
 
 
129
 
        return im;
130
 
 
131
 
fail2:
132
 
        gdImageDestroy(im);
133
 
fail1:
134
 
        return 0;
135
 
}
136
 
 
137
 
void _gdPutColors(gdImagePtr im, gdIOCtx *out)
138
 
{
139
 
        int i;
140
 
        int trans;
141
 
 
142
 
        gdPutC((unsigned char)im->colorsTotal, out);
143
 
        trans = im->transparent;
144
 
        if (trans == (-1)) {
145
 
                trans = 257;
146
 
        }
147
 
        gdPutWord(trans, out);
148
 
        for (i=0; (i<gdMaxColors); i++) {
149
 
                gdPutC((unsigned char)im->red[i], out);
150
 
                gdPutC((unsigned char)im->green[i], out);
151
 
                gdPutC((unsigned char)im->blue[i], out);
152
 
        }
153
 
}
154
 
 
155
 
static
156
 
void _gdPutHeader(gdImagePtr im, gdIOCtx *out)
157
 
{
158
 
        gdPutWord(im->sx, out);
159
 
        gdPutWord(im->sy, out);
160
 
 
161
 
        _gdPutColors(im, out);
162
 
 
163
 
}
164
 
 
165
 
static void _gdImageGd(gdImagePtr im, gdIOCtx *out)
166
 
{
167
 
        int x, y;
168
 
 
169
 
        _gdPutHeader(im, out);
170
 
 
171
 
        for (y=0; (y < im->sy); y++) {  
172
 
                for (x=0; (x < im->sx); x++) {  
173
 
                        /* ROW-MAJOR IN GD 1.3 */
174
 
                        gdPutC((unsigned char)im->pixels[y][x], out);
175
 
                }
176
 
        }
177
 
}
178
 
 
179
 
void gdImageGd(gdImagePtr im, FILE *outFile)
180
 
{
181
 
        gdIOCtx   *out = gdNewFileCtx(outFile);
182
 
        _gdImageGd(im, out);
183
 
        out->free(out);
184
 
}
185
 
 
186
 
void* gdImageGdPtr(gdImagePtr im, int *size)
187
 
{
188
 
        void    *rv;
189
 
        gdIOCtx   *out = gdNewDynamicCtx(2048, NULL);
190
 
        _gdImageGd(im, out);
191
 
        rv = gdDPExtractData(out,size);
192
 
        out->free(out);
193
 
        return rv;
194
 
}
195
 
 
196