Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gifdecode.cxx
Go to the documentation of this file.
1/* @(#)root/x11:$Id$ */
2/* Author: Rene Brun 11/06/97 */
3/* C++ interface Sergey Linev 21/04/2026 */
4
5#include "gifdecode.h"
6
7#include <cstdio>
8#include <cstring>
9
10
11
12#define BITS 12 /* largest code size */
13#define TSIZE 4096 /* tables size */
14
15/***************************************************************
16 * *
17 ***************************************************************/
19{
20 if (CurBit == -1) {
21 lblk = 0;
22 CurByte = -1;
23 }
24
26 long OldByte = CurByte;
27 CurByte = CurBit/8;
28 int nbyte = CurByte - OldByte;
29 int shift = 17 + (CurBit%8) - CurCodeSize;
30 while (nbyte-- > 0) {
31 if (lblk == 0) {
32 lblk = *ptr1++;
33 if (lblk == 0) return -1;
34 }
35 b3[0] = b3[1];
36 b3[1] = b3[2];
37 b3[2] = *ptr1++;
38 lblk--;
39 }
40 return (((b3[0]+0x100*b3[1]+0x10000*b3[2])>>shift) & (CurMaxCode-1));
41}
42
43/***************************************************************
44 * *
45 ***************************************************************/
46void TGifDecode::OutPixel(unsigned char pix)
47{
48 *ptr2++ = pix;
49}
50
51/***************************************************************
52 * *
53 * Name: GIFinfo Date: 03.10.94 *
54 * *
55 * Function: Get information on GIF image *
56 * *
57 * Input: GIFarr[] - compressed image in GIF format *
58 * *
59 * Output: Width - image width *
60 * Height - image height *
61 * Ncols - number of colors *
62 * return - 0 - if O.K. *
63 * 1 - if error *
64 * *
65 ***************************************************************/
66int TGifDecode::GIFinfo(unsigned char *GIFarr, int *Width, int *Height, int *Ncols)
67{
68 unsigned char b;
69
70 unsigned char *ptr1 = GIFarr;
71
72 /* R E A D H E A D E R */
73
74 if (strncmp((const char *)GIFarr,"GIF87a",6) && strncmp((const char *)GIFarr,"GIF89a",6))
75 {
76 fprintf(stderr,"\nGIFinfo: not a GIF\n");
77 return 1;
78 }
79
80 ptr1 += 6;
81
82 ptr1 += 2; /* screen width ... ignore */
83 ptr1 += 2; /* screen height ... ignore */
84
85 b = *ptr1++;
86 *Ncols = 1 << ((b & 7) + 1);
87 if ((b & 0x80) == 0) { /* is there color map? */
88 fprintf(stderr,"\nGIFinfo: warning! no color map\n");
89 *Ncols = 0;
90 }
91
92 ++ptr1; /* background color ... ignore */
93 b = *ptr1++; /* supposed to be NULL */
94 if (b) {
95 fprintf(stderr,"\nGIFdecode: bad screen descriptor\n");
96 return 1;
97 }
98
99 ptr1 += (*Ncols) * 3; /* skip color map */
100
101 b = *ptr1++; /* image separator */
102 if (b != ',') {
103 fprintf(stderr,"\nGIFinfo: no image separator\n");
104 return 1;
105 }
106
107 ptr1 += 2; /* left offset ... ignore */
108 ptr1 += 2; /* top offset ... ignore */
109 b = *ptr1++; /* image width */
110 *Width = b + 0x100*(*ptr1++);
111 b = *ptr1++; /* image height */
112 *Height = b + 0x100*(*ptr1++);
113 return 0;
114}
115
116/***************************************************************
117 * *
118 * Name: GIFdecode Date: 06.10.92 *
119 * *
120 * Function: Decode image from GIF array *
121 * *
122 * Input: GIFarr[] - compressed image in GIF format *
123 * *
124 * Output: PIXarr[] - image (byte per pixel) *
125 * Width - image width *
126 * Height - image height *
127 * Ncols - number of colors *
128 * R[] - red components *
129 * G[] - green components *
130 * B[] - blue components *
131 * return - 0 - if O.K. *
132 * 1 - if error *
133 * *
134 ***************************************************************/
135int TGifDecode::GIFdecode(unsigned char *GIFarr, unsigned char *PIXarr, int *Width, int *Height, int *Ncols, unsigned char *R, unsigned char *G, unsigned char *B)
136{
137 unsigned char b, /* working variable */
138 FinChar; /* final character */
139
140 int i, /* working variable for loops */
141 BitsPixel, /* number of bits per pixel */
142 IniCodeSize, /* initial number of bits per code */
143 ClearCode, /* reset code */
144 EOFCode, /* end of file code */
145 FreeCode, /* first unused entry */
146 CurCode, /* current code */
147 InCode, /* input code */
148 OldCode, /* previous code */
149 PixMask, /* mask for pixel */
150 OutCount; /* output stack counter */
151
152 long Npix; /* number of pixels */
153
154 int Prefix[TSIZE]; /* prefix table */
155 unsigned char Suffix[TSIZE]; /* suffix table */
156 unsigned char OutCode[TSIZE]; /* output stack */
157
158 ptr1 = GIFarr;
159 ptr2 = PIXarr;
160 OldCode = 0;
161 FinChar = 0;
162
163 /* R E A D H E A D E R */
164 if (strncmp((char *)GIFarr,"GIF87a",6) && strncmp((char *)GIFarr,"GIF89a",6))
165 {
166 fprintf(stderr,"\nGIFinfo: not a GIF\n");
167 return 1;
168 }
169
170 ptr1 += 6;
171
172 ptr1 += 2; /* screen width ... ignore */
173 ptr1 += 2; /* screen height ... ignore */
174
175 b = *ptr1++;
176 BitsPixel = (b & 7) + 1; /* # of bits per pixel */
177 *Ncols = 1 << BitsPixel;
178 PixMask = (*Ncols) - 1; /* mask for pixel code */
179 if ((b & 0x80) == 0) { /* is there color map? */
180 fprintf(stderr,"\nGIFdecode: warning! no color map\n");
181 *Ncols = 0;
182 }
183
184 ++ptr1; /* background color ... ignore */
185 b = *ptr1++; /* supposed to be NULL */
186 if (b) {
187 fprintf(stderr,"\nGIFdecode: bad screen descriptor\n");
188 return 1;
189 }
190
191 for (i=0; i<(*Ncols); i++) { /* global color map */
192 R[i] = *ptr1++;
193 G[i] = *ptr1++;
194 B[i] = *ptr1++;
195 }
196
197 b = *ptr1++; /* image separator */
198 if (b != ',') {
199 fprintf(stderr,"\nGIFdecode: no image separator\n");
200 return 1;
201 }
202
203 ptr1 += 2; /* left offset ... ignore */
204 ptr1 += 2; /* top offset ... ignore */
205 b = *ptr1++; /* image width */
206 *Width = b + 0x100*(*ptr1++);
207 b = *ptr1++; /* image height */
208 *Height = b + 0x100*(*ptr1++);
209
210 b = *ptr1++; /* local colors, interlace */
211 if ((b & 0xc0) != 0) {
212 fprintf(stderr,
213 "\nGIFdecode: unexpected item (local colors or interlace)\n");
214 return 1;
215 }
216
217 IniCodeSize = *ptr1++;
218 CurCodeSize = ++IniCodeSize;
219 CurMaxCode = (1 << IniCodeSize);
220 ClearCode = (1 << (IniCodeSize - 1));
221 EOFCode = ClearCode + 1;
222 FreeCode = ClearCode + 2;
223
224 /* D E C O D E I M A G E */
225
226 Npix =(long) (*Width) * (*Height);
227 OutCount = 0;
228 CurBit = -1;
229 CurCode = ReadCode();
230 while (Npix > 0) {
231
232 if (CurCode < 0) {
233 fprintf(stderr,"\nGIFdecode: corrupted GIF (zero block length)\n");
234 return 1;
235 }
236
237 if (CurCode == EOFCode) {
238 fprintf(stderr,"\nGIFdecode: corrupted GIF (unexpected EOF)\n");
239 return 1;
240 }
241
242 if (CurCode == ClearCode) { /* clear code ... reset */
243
244 CurCodeSize = IniCodeSize;
245 CurMaxCode = (1 << IniCodeSize);
246 FreeCode = ClearCode + 2;
247 OldCode = CurCode = ReadCode();
248 FinChar = CurCode;
249 OutPixel(FinChar);
250 Npix--;
251
252 } else { /* image code */
253
254 InCode = CurCode;
255 if (CurCode >= FreeCode) {
256 CurCode = OldCode;
257 OutCode[OutCount++] = FinChar;
258 }
259 while (CurCode > PixMask) { /* build output pixel chain */
260 if (OutCount >= TSIZE) {
261 fprintf(stderr,"\nGIFdecode: corrupted GIF (big output count)\n");
262 return 1;
263 }
264 OutCode[OutCount++] = Suffix[CurCode];
265 CurCode = Prefix[CurCode];
266 }
267 FinChar = CurCode;
268 OutCode[OutCount++] = FinChar;
269
270 for (i=OutCount-1; i>=0; i--) { /* put out pixel chain */
271 OutPixel(OutCode[i]);
272 Npix--;
273 }
274 OutCount = 0;
275
276 Prefix[FreeCode] = OldCode; /* build the tables */
277 Suffix[FreeCode] = FinChar;
278 OldCode = InCode;
279
280 FreeCode++; /* move pointer */
281 if (FreeCode >= CurMaxCode) {
282 if (CurCodeSize < BITS) {
283 CurCodeSize++;
284 CurMaxCode *= 2;
285 }
286 }
287 }
288 CurCode = ReadCode();
289 }
290 return 0;
291}
#define b(i)
Definition RSha256.hxx:100
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
unsigned char lblk
Definition gifdecode.h:19
long b3[3]
Definition gifdecode.h:17
long CurByte
Definition gifdecode.h:18
void OutPixel(unsigned char pix)
Definition gifdecode.cxx:46
static int GIFinfo(unsigned char *GIFarr, int *Width, int *Height, int *Ncols)
Definition gifdecode.cxx:66
int ReadCode()
Definition gifdecode.cxx:18
int CurMaxCode
Definition gifdecode.h:14
unsigned char * ptr1
Definition gifdecode.h:10
int CurCodeSize
Definition gifdecode.h:13
long CurBit
Definition gifdecode.h:15
unsigned char * ptr2
Definition gifdecode.h:11
int GIFdecode(unsigned char *GIFarr, unsigned char *PIXarr, int *Width, int *Height, int *Ncols, unsigned char *R, unsigned char *G, unsigned char *B)
#define TSIZE
Definition gifdecode.cxx:13
#define BITS
Definition gifdecode.cxx:12
#define G(x, y, z)