Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gifencode.cxx
Go to the documentation of this file.
1/* @(#)root/x11:$Id$ */
2/* Author: E.Chernyaev 19/01/94
3 * C++ interface: S.Linev 20/04/2026 */
4
5#include "gifencode.h"
6
7#include <vector>
8
9
10#define BITS 12 /* largest code size */
11#define THELIMIT 4096 /* NEVER generate this */
12#define SHIFT 4 /* shift for hashing */
13
14
15void TGifEncode::put_byte(unsigned char b)
16{
17 if (fOut && (ferror(fOut) == 0)) {
18 fputc(b, fOut);
19 fNbyte++;
20 }
21}
22
23bool TGifEncode::OpenFile(const char *fname, const char *opt)
24{
25 fOut = fopen(fname, opt);
26 return fOut != nullptr;
27}
28
30{
31 if (fOut)
32 fclose(fOut);
33 fOut = nullptr;
34}
35
36
38{
39 a_count = 0;
40 cur_accum = 0;
41 cur_bits = 0;
42}
43
44void TGifEncode::char_out(unsigned char c)
45{
46 accum[a_count++] = c;
47 if (a_count >= 254)
48 char_flush();
49}
50
52{
53 if (a_count == 0) return;
55 for (int i=0; i<a_count; i++) {
56 put_byte(accum[i]);
57 }
58 a_count = 0;
59}
60
62{
63 put_byte(word & 0xFF);
64 put_byte((word>>8) & 0xFF);
65}
66
67/***************************************************************
68 * *
69 * Name: output Date: 02.10.92 *
70 * *
71 * Function: output GIF code *
72 * *
73 * Input: code - GIF code *
74 * *
75 ***************************************************************/
76void TGifEncode::output(int code)
77{
78 /* O U T P U T C O D E */
79
80 static unsigned long masks[] = { 0x0000,
81 0x0001, 0x0003, 0x0007, 0x000F,
82 0x001F, 0x003F, 0x007F, 0x00FF,
83 0x01FF, 0x03FF, 0x07FF, 0x0FFF,
84 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
85
86 cur_accum &= masks[cur_bits];
87 if (cur_bits > 0)
88 cur_accum |= ((long)code << cur_bits);
89 else
90 cur_accum = code;
92 while( cur_bits >= 8 ) {
93 char_out(cur_accum & 0xFF);
94 cur_accum >>= 8;
95 cur_bits -= 8;
96 }
97
98 /* R E S E T */
99
100 if (code == ClearCode ) {
101 memset((char *) HashTab, -1, sizeof(HashTab));
102 FreeCode = ClearCode + 2;
104 CurMaxCode = (1 << (IniCodeSize)) - 1;
105 }
106
107 /* I N C R E A S E C O D E S I Z E */
108
109 if (FreeCode > CurMaxCode ) {
110 CurCodeSize++;
111 if ( CurCodeSize == BITS )
113 else
114 CurMaxCode = (1 << (CurCodeSize)) - 1;
115 }
116
117 /* E N D O F F I L E : write the rest of the buffer */
118
119 if( code == EOFCode ) {
120 while( cur_bits > 0 ) {
121 char_out(cur_accum & 0xff);
122 cur_accum >>= 8;
123 cur_bits -= 8;
124 }
125 char_flush();
126 }
127}
128
129
130
131/***********************************************************************
132 * *
133 * Name: GIFencode Date: 02.10.92 *
134 * Author: E.Chernyaev (IHEP/Protvino) Revised: *
135 * *
136 * Function: GIF compression of the image *
137 * *
138 * Input: Width - image width (must be >= 8) *
139 * Height - image height (must be >= 8) *
140 * Ncol - number of colors *
141 * R[] - red components *
142 * G[] - green components *
143 * B[] - blue components *
144 * ScLine[] - array for scan line (byte per pixel) *
145 * get_scline - user routine to read scan line: *
146 * get_scline(y, Width, ScLine) *
147 * pb - user routine for "put_byte": pb(b) *
148 * *
149 * Return: size of GIF *
150 * *
151 ***********************************************************************/
152long TGifEncode::GIFencode(int Width, int Height, int Ncol, unsigned char *R, unsigned char *G, unsigned char *B)
153// void(*get_scline) ARGS((int, int, byte *)), void(*pb) ARGS((byte)))
154{
155 long CodeK;
156 int ncol, i, x, y, disp, Code, K;
157
158 std::vector<unsigned char> ScLine(Width);
159
160 /* C H E C K P A R A M E T E R S */
161
162 Code = 0;
163 if (Width <= 0 || Width > 4096 || Height <= 0 || Height > 4096) {
164 fprintf(stderr,
165 "\nGIFencode: incorrect image size: %d x %d\n", Width, Height);
166 return 0;
167 }
168
169 if (Ncol <= 0 || Ncol > 256) {
170 fprintf(stderr,"\nGIFencode: wrong number of colors: %d\n", Ncol);
171 return 0;
172 }
173
174 /* I N I T I A L I S A T I O N */
175
176 fNbyte = 0;
177 char_init(); /* initialise "char_..." routines */
178
179 /* F I N D # O F B I T S P E R P I X E L */
180
181 BitsPixel = 1;
182 if (Ncol > 2) BitsPixel = 2;
183 if (Ncol > 4) BitsPixel = 3;
184 if (Ncol > 8) BitsPixel = 4;
185 if (Ncol > 16) BitsPixel = 5;
186 if (Ncol > 32) BitsPixel = 6;
187 if (Ncol > 64) BitsPixel = 7;
188 if (Ncol > 128) BitsPixel = 8;
189
190 ncol = 1 << BitsPixel;
192 if (BitsPixel <= 1) IniCodeSize = 2;
193
194 /* W R I T E H E A D E R */
195
196 put_byte('G'); /* magic number: GIF87a */
197 put_byte('I');
198 put_byte('F');
199 put_byte('8');
200 put_byte('7');
201 put_byte('a');
202
203 put_short(Width); /* screen size */
204 put_short(Height);
205
206 K = 0x80; /* yes, there is a color map */
207 K |= (8-1)<<4; /* OR in the color resolution */
208 K |= (BitsPixel - 1); /* OR in the # of bits per pixel */
209 put_byte(K);
210
211 put_byte(0); /* background color */
212 put_byte(0); /* future expansion byte */
213
214 for (i=0; i<Ncol; i++) { /* global colormap */
215 put_byte(R[i]);
216 put_byte(G[i]);
217 put_byte(B[i]);
218 }
219 for (; i<ncol; i++) {
220 put_byte(0);
221 put_byte(0);
222 put_byte(0);
223 }
224
225 put_byte(','); /* image separator */
226 put_short(0); /* left offset of image */
227 put_short(0); /* top offset of image */
228 put_short(Width); /* image size */
229 put_short(Height);
230 put_byte(0); /* no local colors, no interlace */
231 put_byte(IniCodeSize); /* initial code size */
232
233 /* L W Z C O M P R E S S I O N */
234
236 CurMaxCode = (1 << (IniCodeSize)) - 1;
237 ClearCode = (1 << (IniCodeSize - 1));
238 EOFCode = ClearCode + 1;
239 FreeCode = ClearCode + 2;
241 for (y=0; y<Height; y++) {
242 get_scline(y, Width, ScLine.data());
243 x = 0;
244 if (y == 0)
245 Code = ScLine[x++];
246 while(x < Width) {
247 K = ScLine[x++]; /* next symbol */
248 CodeK = ((long) K << BITS) + Code; /* set full code */
249 i = (K << SHIFT) ^ Code; /* xor hashing */
250
251 if (HashTab[i] == CodeK) { /* full code found */
252 Code = CodeTab[i];
253 continue;
254 }
255 else if (HashTab[i] < 0 ) /* empty slot */
256 goto NOMATCH;
257
258 disp = HSIZE - i; /* secondary hash */
259 if (i == 0) disp = 1;
260
261PROBE:
262 if ((i -= disp) < 0)
263 i += HSIZE;
264
265 if (HashTab[i] == CodeK) { /* full code found */
266 Code = CodeTab[i];
267 continue;
268 }
269
270 if (HashTab[i] > 0) /* try again */
271 goto PROBE;
272
273NOMATCH:
274 output(Code); /* full code not found */
275 Code = K;
276
277 if (FreeCode < THELIMIT) {
278 CodeTab[i] = FreeCode++; /* code -> hashtable */
279 HashTab[i] = CodeK;
280 }
281 else
283 }
284 }
285 /* O U T P U T T H E R E S T */
286
287 output(Code);
289 put_byte(0); /* zero-length packet (EOF) */
290 put_byte(';'); /* GIF file terminator */
291
292 return fNbyte;
293}
294
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
#define Code
Definition ZTrees.c:172
virtual void get_scline(int y, int width, unsigned char *buf)=0
void char_init()
Definition gifencode.cxx:37
int BitsPixel
Definition gifencode.h:24
unsigned char accum[256]
Definition gifencode.h:34
unsigned long cur_accum
Definition gifencode.h:20
int EOFCode
Definition gifencode.h:29
int IniCodeSize
Definition gifencode.h:25
int CodeTab[HSIZE]
Definition gifencode.h:33
int FreeCode
Definition gifencode.h:30
int cur_bits
Definition gifencode.h:21
bool OpenFile(const char *fname, const char *opt="w+")
Definition gifencode.cxx:23
int ClearCode
Definition gifencode.h:28
void CloseFile()
Definition gifencode.cxx:29
void put_byte(unsigned char b)
Definition gifencode.cxx:15
long GIFencode(int Width, int Height, int Ncol, unsigned char *R, unsigned char *G, unsigned char *B)
void put_short(int word)
Definition gifencode.cxx:61
int a_count
Definition gifencode.h:22
int CurCodeSize
Definition gifencode.h:26
long HashTab[HSIZE]
Definition gifencode.h:32
long fNbyte
Definition gifencode.h:36
void char_out(unsigned char c)
Definition gifencode.cxx:44
void output(int code)
Definition gifencode.cxx:76
int CurMaxCode
Definition gifencode.h:27
void char_flush()
Definition gifencode.cxx:51
FILE * fOut
Definition gifencode.h:37
#define BITS
Definition gifdecode.cxx:12
#define THELIMIT
Definition gifencode.cxx:11
#define SHIFT
Definition gifencode.cxx:12
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
#define G(x, y, z)