Added imaggif.h, imaggif.cpp (wxImage GIF-reading support); candidate
[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 <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70
71
72 /* error codes */
73 #define E_OK 0 /* everything was OK */
74 #define E_ARCHIVO -1 /* error opening file */
75 #define E_FORMATO -2 /* error in gif header */
76 #define E_MEMORIA -3 /* error allocating memory */
77
78
79 /* This class binding is by VS, so all bugs in it are mine ;-) */
80
81 class gifDecoder
82 {
83 private:
84 /* globals */
85 int restbits; /* remaining valid bits */
86 unsigned int restbyte; /* remaining bytes in this block */
87 unsigned int lastbyte; /* last byte read */
88
89 unsigned char* file; /* input file in memory */
90 unsigned int file_pos, file_size; /* position & size in it */
91
92 int fgetc();
93 void fread(void *ptr, size_t size, size_t nmemb);
94 void fseekcur(int rel_pos);
95
96 public:
97 gifDecoder(unsigned char* mem, int sz) {file = mem; file_pos = 0; file_size = sz;}
98 int getcode(int bits);
99 int dgif(IMAGEN *img, int interl, int bits);
100 int readgif(IMAGEN *img);
101 };
102
103
104 int gifDecoder::fgetc()
105 {
106 if (file_pos < file_size) return file[file_pos++];
107 else return EOF;
108 }
109
110 void gifDecoder::fread(void *ptr, size_t size, size_t nmemb)
111 {
112 int todo = size * nmemb;
113 if (todo + file_pos > file_size) todo = file_size - file_pos;
114 memcpy(ptr, file + file_pos, todo);
115 file_pos += todo;
116 }
117
118 void gifDecoder::fseekcur(int rel_pos)
119 {
120 file_pos += rel_pos;
121 }
122
123 /* getcode:
124 * Reads the next code from the file, with size 'bits'
125 * v2.0 - changed to support 'bits' values < 8
126 */
127 int gifDecoder::getcode(int bits)
128 {
129 unsigned int mask; /* bit mask */
130 unsigned int code; /* code (result) */
131
132
133 /* get remaining bits from last byte read */
134 mask = (1 << bits) - 1;
135 code = (lastbyte >> (8 - restbits)) & mask;
136
137 /* keep reading new bytes until needed */
138 while (bits > restbits)
139 {
140 /* if no bytes left in this block, read the next block */
141 if (restbyte == 0)
142 restbyte = fgetc();
143
144 /* read next byte and isolate the bits we need */
145 lastbyte = fgetc();
146 mask = (1 << (bits - restbits)) - 1;
147 code = code + ((lastbyte & mask) << restbits);
148 restbyte--;
149
150 /* adjust total number of bits extracted from the buffer */
151 restbits = restbits + 8;
152 }
153
154 /* find number of bits reamining for next code */
155 restbits = (restbits - bits);
156
157 return code;
158 }
159
160 /* dgif:
161 * GIF decoding function. The initial code size (aka root size)
162 * is 'bits'. Supports interlaced images (interl == 1).
163 */
164 int gifDecoder::dgif(IMAGEN *img, int interl, int bits)
165 {
166 int ab_prefix[4096]; /* alphabet (prefixes) */
167 int ab_tail[4096]; /* alphabet (tails) */
168 int stack[4096]; /* decompression stack */
169
170 int ab_clr; /* clear code */
171 int ab_fin; /* end of info code */
172 int ab_bits; /* actual symbol width, in bits */
173 int ab_free; /* first free position in alphabet */
174 int ab_max; /* last possible character in alphabet */
175 int pass; /* pass number in interlaced images */
176 int pos; /* index into decompresion stack */
177 int x, y; /* position in image buffer */
178
179 int code, readcode, lastcode, abcabca;
180
181 /* these won't change */
182 ab_clr = (1 << bits);
183 ab_fin = (1 << bits) + 1;
184
185 /* these will change through the decompression proccess */
186 ab_bits = bits + 1;
187 ab_free = (1 << bits) + 2;
188 ab_max = (1 << ab_bits) - 1;
189 lastcode = -1;
190 abcabca = -1;
191 pass = 1;
192 pos = x = y = 0;
193
194 /* reset static globals */
195 restbits = 0;
196 restbyte = 0;
197 lastbyte = 0;
198
199 do
200 {
201 /* get next code */
202 readcode = code = getcode(ab_bits);
203
204 /* end of image? */
205 if (code == ab_fin) break;
206
207 /* reset alphabet? */
208 if (code == ab_clr)
209 {
210 /* reset main variables */
211 ab_bits = bits + 1;
212 ab_free = (1 << bits) + 2;
213 ab_max = (1 << ab_bits) - 1;
214 lastcode = -1;
215 abcabca = -1;
216
217 /* skip to next code */
218 continue;
219 }
220
221 /* unknown code: special case (like in ABCABCA) */
222 if (code >= ab_free)
223 {
224 code = lastcode; /* take last string */
225 stack[pos++] = abcabca; /* add first character */
226 }
227
228 /* build the string for this code in the stack */
229 while (code > ab_clr)
230 {
231 stack[pos++] = ab_tail[code];
232 code = ab_prefix[code];
233 }
234 stack[pos] = code; /* push last code into the stack */
235 abcabca = code; /* save for special case */
236
237 /* make new entry in alphabet (only if NOT just cleared) */
238 if (lastcode != -1)
239 {
240 ab_prefix[ab_free] = lastcode;
241 ab_tail[ab_free] = code;
242 ab_free++;
243
244 if ((ab_free > ab_max) && (ab_bits < 12))
245 {
246 ab_bits++;
247 ab_max = (1 << ab_bits) - 1;
248 }
249 }
250
251 /* dump stack data to the buffer */
252 while (pos >= 0)
253 {
254 (img->p)[x + (y * (img->w))] = (char)stack[pos--];
255
256 if (++x >= (img->w))
257 {
258 x = 0;
259
260 if (interl)
261 {
262 /* support for interlaced images */
263 switch (pass)
264 {
265 case 1: y += 8; break;
266 case 2: y += 8; break;
267 case 3: y += 4; break;
268 case 4: y += 2; break;
269 }
270 if (y >= (img->h))
271 {
272 switch (++pass)
273 {
274 case 2: y = 4; break;
275 case 3: y = 2; break;
276 case 4: y = 1; break;
277 }
278 }
279 }
280 else
281 {
282 /* non-interlaced */
283 y++;
284 }
285 }
286 }
287
288 pos = 0;
289 lastcode = readcode;
290 }
291 while (code != ab_fin);
292
293 return 0;
294 }
295
296 /* readgif:
297 * Reads a GIF image from the file with filename 'nombre' in the
298 * IMAGEN structure pointed by 'img'. Can read GIFs with any bit
299 * size (color depth), but the output image is always expanded
300 * to 8 bits per pixel. Also, the image palette always contains
301 * 256 colors, although some of them may be unused. Returns E_OK
302 * (== 0) on success, or an error code if something fails. Error
303 * codes are E_ARCHIVO, E_FORMATO, E_MEMORIA (see above).
304 */
305 int gifDecoder::readgif(IMAGEN *img)
306 {
307 int size, ncolors, bits, interl, i;
308 unsigned char pal[768];
309 unsigned char buf[16];
310
311
312 /* read header and logical screen descriptor block (LSDB) */
313 fread(buf, 1, 13);
314
315 /* check GIF signature */
316 if (memcmp(buf, "GIF", 3) != 0) return E_FORMATO;
317
318 /* load global color map if available */
319 if ((buf[10] & 0x80) == 0x80)
320 {
321 ncolors = 2 << (buf[10] & 0x07);
322 fread(pal, 1, 3 * ncolors);
323 }
324
325 /* skip extensions */
326 while (fgetc() == 0x21) /* separator */
327 {
328 fgetc(); /* function code */
329
330 while ((i = fgetc()) != 0) /* byte count */
331 fseekcur(i);
332 }
333
334 /* read image descriptor block (IDB) */
335 fread(buf, 1, 9);
336 img->w = buf[4] + 256 * buf[5];
337
338 img->h = buf[6] + 256 * buf[7];
339 size = img->w * img->h;
340 interl = ((buf[8] & 0x40)? 1 : 0);
341
342 /* load local color map if available */
343 if ((buf[8] & 0x80) == 0x80)
344 {
345 ncolors = 2 << (buf[8] & 0x07);
346 fread(pal, 1, 3 * ncolors);
347 }
348
349 /* get initial code size from first byte in raster data */
350 bits = fgetc();
351
352 /* allocate memory for image and palette */
353 if ((img->p = (unsigned char*) malloc(size)) == NULL) return E_MEMORIA;
354 if ((img->pal = (unsigned char*) malloc(768)) == NULL) return E_MEMORIA;
355
356 /* shift palette to fit VGA 6-bit format */
357 for (i = 0; i < 768; i++)
358 (img->pal)[i] = (unsigned char)pal[i] /* >> 2 not needed under wxWin */;
359
360 /* decode GIF */
361 dgif(img, interl, bits);
362
363 /* finish successfully :-) */
364 return E_OK;
365 }
366
367 /*
368
369 FOLLOWING CODE IS BY V.S. :
370
371 */
372
373 //-----------------------------------------------------------------------------
374 // wxGIFHandler
375 //-----------------------------------------------------------------------------
376
377 IMPLEMENT_DYNAMIC_CLASS(wxGIFHandler,wxImageHandler)
378
379 bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream )
380 {
381 unsigned char *ptr, *src, *pal;
382 int file_size;
383 unsigned char* file_con;
384 IMAGEN igif;
385 int i;
386 gifDecoder *decod;
387
388 image->Destroy();
389
390 file_size = stream.StreamSize();
391 file_con = (unsigned char*) malloc(file_size);
392 stream.Read(file_con, file_size);
393 decod = new gifDecoder(file_con, file_size);
394
395 if (decod -> readgif(&igif) != E_OK) {
396 wxLogDebug("Error reading GIF");
397 free(file_con);
398 delete decod;
399 return FALSE;
400 }
401 free(file_con);
402 delete decod;
403
404 image->Create(igif.w, igif.h);
405 if (!image->Ok()) {
406 free(igif.pal);
407 free(igif.p);
408 return FALSE;
409 }
410 image->SetMask(FALSE);
411
412 ptr = image->GetData();
413 src = igif.p;
414 pal = igif.pal;
415 for (i = 0; i < igif.w * igif.h; i++, src++) {
416 *(ptr++) = pal[3 * (*src) + 0];
417 *(ptr++) = pal[3 * (*src) + 1];
418 *(ptr++) = pal[3 * (*src) + 2];
419 }
420
421 free(igif.pal);
422 free(igif.p);
423 return TRUE;
424 }
425
426 bool wxGIFHandler::SaveFile( wxImage *image, wxOutputStream& stream )
427 {
428 wxLogDebug("wxGIFHandler is read-only!!");
429 return FALSE;
430 }
431
432 //////////////////// Module:
433
434 /* We haven't yet decided to go for the wxModule approach or
435 * explicit handler-adding: assuming the latter for now -- JACS
436
437 class wxGIFModule : public wxModule
438 {
439 DECLARE_DYNAMIC_CLASS(wxGIFModule)
440
441 public:
442 virtual bool OnInit()
443 {
444 wxImage::AddHandler(new wxGIFHandler);
445 return TRUE;
446 }
447 virtual void OnExit() {}
448 };
449
450 IMPLEMENT_DYNAMIC_CLASS(wxGIFModule, wxModule)
451
452 */
453
454