direct stream use
[wxWidgets.git] / src / common / imaggif.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imaggif.cpp
3 // Purpose: wxGIFHandler
4 // Author: Guillermo Rodriguez Garcia
5 // wxWindows adaptation by Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifdef __GNUG__
10 #pragma implementation "imaggif.h"
11 #endif
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include <wx/wxprec.h>
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include <wx/wx.h>
21
22 #include <wx/image.h>
23 #include <wx/wfstream.h>
24 #include <wx/module.h>
25
26 #include <wx/imaggif.h>
27
28 /*
29
30 FOLLOWING CODE IS BY G.R.G. :
31 (except wxInputStream stuff)
32
33 */
34
35 /************************************************************************
36 READGIF.H - Leer un archivo GIF de 8 bits
37 ------------------------------------------------------------------------
38 Tratamiento Digital de la Imagen
39 ------------------------------------------------------------------------
40 Guillermo Rodriguez Garcia
41 <guille@iies.es>
42
43 Version: 2.0
44 *************************************************************************/
45
46 typedef struct
47 {
48 int w; /* width */
49 int h; /* height */
50 unsigned char *p; /* bitmap */
51 unsigned char *pal; /* palette */
52 } IMAGEN;
53
54
55 /************************************************************************
56 READGIF.C - Lee un archivo GIF de 256 colores
57 ------------------------------------------------------------------------
58 Tratamiento Digital de la Imagen
59 ------------------------------------------------------------------------
60 Guillermo Rodriguez Garcia
61 <guille@iies.es>
62
63 Version: 2.0
64 *************************************************************************/
65
66
67 #include <stdlib.h>
68 #include <string.h>
69
70
71 /* error codes */
72 #define E_OK 0 /* everything was OK */
73 #define E_ARCHIVO -1 /* error opening file */
74 #define E_FORMATO -2 /* error in gif header */
75 #define E_MEMORIA -3 /* error allocating memory */
76
77
78 /* This class binding is by VS, so all bugs in it are mine ;-) */
79
80 class gifDecoder
81 {
82 private:
83 /* globals */
84 int restbits; /* remaining valid bits */
85 unsigned int restbyte; /* remaining bytes in this block */
86 unsigned int lastbyte; /* last byte read */
87
88 wxInputStream *f; /* input file */
89
90 public:
91 gifDecoder(wxInputStream *s) {f = s;}
92 int getcode(int bits);
93 int dgif(IMAGEN *img, int interl, int bits);
94 int readgif(IMAGEN *img);
95
96 private:
97 int mygetc() {return (unsigned char)f -> GetC();}
98 // This is NEEDED! GetC is char (signed) why we need unsigned value
99 // from here
100 };
101
102
103
104 /* getcode:
105 * Reads the next code from the file, with size 'bits'
106 * v2.0 - changed to support 'bits' values < 8
107 */
108 int gifDecoder::getcode(int bits)
109 {
110 unsigned int mask; /* bit mask */
111 unsigned int code; /* code (result) */
112
113
114 /* get remaining bits from last byte read */
115 mask = (1 << bits) - 1;
116 code = (lastbyte >> (8 - restbits)) & mask;
117
118 /* keep reading new bytes until needed */
119 while (bits > restbits)
120 {
121 /* if no bytes left in this block, read the next block */
122 if (restbyte == 0)
123 restbyte = mygetc();
124
125 /* read next byte and isolate the bits we need */
126 lastbyte = mygetc();
127 mask = (1 << (bits - restbits)) - 1;
128 code = code + ((lastbyte & mask) << restbits);
129 restbyte--;
130
131 /* adjust total number of bits extracted from the buffer */
132 restbits = restbits + 8;
133 }
134
135 /* find number of bits reamining for next code */
136 restbits = (restbits - bits);
137
138 return code;
139 }
140
141
142 /* dgif:
143 * GIF decoding function. The initial code size (aka root size)
144 * is 'bits'. Supports interlaced images (interl == 1).
145 */
146 int gifDecoder::dgif(IMAGEN *img, int interl, int bits)
147 {
148 int ab_prefix[4096]; /* alphabet (prefixes) */
149 int ab_tail[4096]; /* alphabet (tails) */
150 int stack[4096]; /* decompression stack */
151
152 int ab_clr; /* clear code */
153 int ab_fin; /* end of info code */
154 int ab_bits; /* actual symbol width, in bits */
155 int ab_free; /* first free position in alphabet */
156 int ab_max; /* last possible character in alphabet */
157 int pass; /* pass number in interlaced images */
158 int pos; /* index into decompresion stack */
159 int x, y; /* position in image buffer */
160
161 int code, readcode, lastcode, abcabca;
162
163 /* these won't change */
164 ab_clr = (1 << bits);
165 ab_fin = (1 << bits) + 1;
166
167 /* these will change through the decompression proccess */
168 ab_bits = bits + 1;
169 ab_free = (1 << bits) + 2;
170 ab_max = (1 << ab_bits) - 1;
171 lastcode = -1;
172 abcabca = -1;
173 pass = 1;
174 pos = x = y = 0;
175
176 /* reset static globals */
177 restbits = 0;
178 restbyte = 0;
179 lastbyte = 0;
180
181 do
182 {
183 /* get next code */
184 readcode = code = getcode(ab_bits);
185
186 /* end of image? */
187 if (code == ab_fin) break;
188
189 /* reset alphabet? */
190 if (code == ab_clr)
191 {
192 /* reset main variables */
193 ab_bits = bits + 1;
194 ab_free = (1 << bits) + 2;
195 ab_max = (1 << ab_bits) - 1;
196 lastcode = -1;
197 abcabca = -1;
198
199 /* skip to next code */
200 continue;
201 }
202
203 /* unknown code: special case (like in ABCABCA) */
204 if (code >= ab_free)
205 {
206 code = lastcode; /* take last string */
207 stack[pos++] = abcabca; /* add first character */
208 }
209
210 /* build the string for this code in the stack */
211 while (code > ab_clr)
212 {
213 stack[pos++] = ab_tail[code];
214 code = ab_prefix[code];
215 }
216 stack[pos] = code; /* push last code into the stack */
217 abcabca = code; /* save for special case */
218
219 /* make new entry in alphabet (only if NOT just cleared) */
220 if (lastcode != -1)
221 {
222 ab_prefix[ab_free] = lastcode;
223 ab_tail[ab_free] = code;
224 ab_free++;
225
226 if ((ab_free > ab_max) && (ab_bits < 12))
227 {
228 ab_bits++;
229 ab_max = (1 << ab_bits) - 1;
230 }
231 }
232
233 /* dump stack data to the buffer */
234 while (pos >= 0)
235 {
236 (img->p)[x + (y * (img->w))] = (char)stack[pos--];
237
238 if (++x >= (img->w))
239 {
240 x = 0;
241
242 if (interl)
243 {
244 /* support for interlaced images */
245 switch (pass)
246 {
247 case 1: y += 8; break;
248 case 2: y += 8; break;
249 case 3: y += 4; break;
250 case 4: y += 2; break;
251 }
252 if (y >= (img->h))
253 {
254 switch (++pass)
255 {
256 case 2: y = 4; break;
257 case 3: y = 2; break;
258 case 4: y = 1; break;
259 }
260 }
261 }
262 else
263 {
264 /* non-interlaced */
265 y++;
266 }
267 }
268 }
269
270 pos = 0;
271 lastcode = readcode;
272 }
273 while (code != ab_fin);
274
275 return 0;
276 }
277
278 /* readgif:
279 * Reads a GIF image from the file with filename 'nombre' in the
280 * IMAGEN structure pointed by 'img'. Can read GIFs with any bit
281 * size (color depth), but the output image is always expanded
282 * to 8 bits per pixel. Also, the image palette always contains
283 * 256 colors, although some of them may be unused. Returns E_OK
284 * (== 0) on success, or an error code if something fails. Error
285 * codes are E_ARCHIVO, E_FORMATO, E_MEMORIA (see above).
286 */
287 int gifDecoder::readgif(IMAGEN *img)
288 {
289 int size, ncolors, bits, interl, i;
290 unsigned char pal[768];
291 unsigned char buf[16];
292
293 /* read header and logical screen descriptor block (LSDB) */
294 f -> Read(buf, 13);
295
296 /* check GIF signature */
297 if (memcmp(buf, "GIF", 3) != 0) return E_FORMATO;
298
299 /* load global color map if available */
300 if ((buf[10] & 0x80) == 0x80)
301 {
302 ncolors = 2 << (buf[10] & 0x07);
303 f -> Read(pal, 3 * ncolors);
304 }
305
306 /* skip extensions */
307 while (mygetc() == 0x21) /* separator */
308 {
309 mygetc(); /* function code */
310
311 while ((i = mygetc()) != 0) /* byte count */
312 f -> SeekI(i, wxFromCurrent);
313 }
314
315 /* read image descriptor block (IDB) */
316 f -> Read(buf, 9);
317 img->w = buf[4] + 256 * buf[5];
318
319 img->h = buf[6] + 256 * buf[7];
320 size = img->w * img->h;
321 interl = ((buf[8] & 0x40)? 1 : 0);
322
323 /* load local color map if available */
324 if ((buf[8] & 0x80) == 0x80)
325 {
326 ncolors = 2 << (buf[8] & 0x07);
327 f -> Read(pal, 3 * ncolors);
328 }
329
330 /* get initial code size from first byte in raster data */
331 bits = mygetc();
332
333 /* allocate memory for image and palette */
334 if ((img->p = (unsigned char*) malloc(size)) == NULL) return E_MEMORIA;
335 if ((img->pal = (unsigned char*) malloc(768)) == NULL) return E_MEMORIA;
336
337 /* shift palette to fit VGA 6-bit format */
338 for (i = 0; i < 768; i++)
339 (img->pal)[i] = (unsigned char)pal[i] /* >> 2 not needed under wxWin */;
340
341 /* decode GIF */
342 dgif(img, interl, bits);
343
344 /* finish successfully :-) */
345 return E_OK;
346 }
347
348 /*
349
350 FOLLOWING CODE IS BY V.S. :
351
352 */
353
354 //-----------------------------------------------------------------------------
355 // wxGIFHandler
356 //-----------------------------------------------------------------------------
357
358 IMPLEMENT_DYNAMIC_CLASS(wxGIFHandler,wxImageHandler)
359
360 bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream )
361 {
362 unsigned char *ptr, *src, *pal;
363 IMAGEN igif;
364 int i;
365 gifDecoder *decod;
366
367 image->Destroy();
368
369 decod = new gifDecoder(&stream);
370
371 if (decod -> readgif(&igif) != E_OK) {
372 wxLogDebug("Error reading GIF");
373 delete decod;
374 return FALSE;
375 }
376 delete decod;
377
378 image->Create(igif.w, igif.h);
379 if (!image->Ok()) {
380 free(igif.pal);
381 free(igif.p);
382 return FALSE;
383 }
384 image->SetMask(FALSE);
385
386 ptr = image->GetData();
387 src = igif.p;
388 pal = igif.pal;
389 for (i = 0; i < igif.w * igif.h; i++, src++) {
390 *(ptr++) = pal[3 * (*src) + 0];
391 *(ptr++) = pal[3 * (*src) + 1];
392 *(ptr++) = pal[3 * (*src) + 2];
393 }
394
395 free(igif.pal);
396 free(igif.p);
397 return TRUE;
398 }
399
400 bool wxGIFHandler::SaveFile( wxImage *image, wxOutputStream& stream )
401 {
402 wxLogDebug("wxGIFHandler is read-only!!");
403 return FALSE;
404 }
405
406