Guillermo Rodriguez Garcia
<guille@iies.es>
- Version: 2.0
+ Version: 2.1
*************************************************************************/
typedef struct
{
int w; /* width */
int h; /* height */
+ int transparent; /* transparent color (-1 = none) */
unsigned char *p; /* bitmap */
unsigned char *pal; /* palette */
} IMAGEN;
Guillermo Rodriguez Garcia
<guille@iies.es>
- Version: 2.0
+ Version: 2.1
*************************************************************************/
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int restbyte; /* remaining bytes in this block */
unsigned int lastbyte; /* last byte read */
- unsigned char* file; /* input file in memory */
- unsigned int file_pos, file_size; /* position & size in it */
-
- int fgetc();
- void fread(void *ptr, size_t size, size_t nmemb);
- void fseekcur(int rel_pos);
+ wxInputStream *f; /* input file */
public:
- gifDecoder(unsigned char* mem, int sz) {file = mem; file_pos = 0; file_size = sz;}
+ gifDecoder(wxInputStream *s) {f = s;}
int getcode(int bits);
int dgif(IMAGEN *img, int interl, int bits);
int readgif(IMAGEN *img);
+
+ private:
+ unsigned char mygetc();
+ // This is NEEDED! GetC is char (signed) why we need unsigned value
+ // from here
};
-int gifDecoder::fgetc()
+unsigned char gifDecoder::mygetc()
{
- if (file_pos < file_size) return file[file_pos++];
- else return EOF;
+ unsigned char c;
+ f -> Read(&c, 1);
+ return c;
}
-void gifDecoder::fread(void *ptr, size_t size, size_t nmemb)
-{
- int todo = size * nmemb;
- if (todo + file_pos > file_size) todo = file_size - file_pos;
- memcpy(ptr, file + file_pos, todo);
- file_pos += todo;
-}
-
-void gifDecoder::fseekcur(int rel_pos)
-{
- file_pos += rel_pos;
-}
/* getcode:
* Reads the next code from the file, with size 'bits'
{
/* if no bytes left in this block, read the next block */
if (restbyte == 0)
- restbyte = fgetc();
+ restbyte = mygetc();
/* read next byte and isolate the bits we need */
- lastbyte = fgetc();
+ lastbyte = mygetc();
mask = (1 << (bits - restbits)) - 1;
code = code + ((lastbyte & mask) << restbits);
restbyte--;
return code;
}
+
/* dgif:
* GIF decoding function. The initial code size (aka root size)
* is 'bits'. Supports interlaced images (interl == 1).
unsigned char pal[768];
unsigned char buf[16];
-
/* read header and logical screen descriptor block (LSDB) */
- fread(buf, 1, 13);
+ f -> Read(buf, 13);
/* check GIF signature */
if (memcmp(buf, "GIF", 3) != 0) return E_FORMATO;
if ((buf[10] & 0x80) == 0x80)
{
ncolors = 2 << (buf[10] & 0x07);
- fread(pal, 1, 3 * ncolors);
+ f -> Read(pal, 3 * ncolors);
}
- /* skip extensions */
- while (fgetc() == 0x21) /* separator */
- {
- fgetc(); /* function code */
+ /* assume no transparent color */
+ img->transparent = -1;
- while ((i = fgetc()) != 0) /* byte count */
- fseekcur(i);
+ /* skip most extensions */
+ while (mygetc() == 0x21) /* separator */
+ {
+ wxLogDebug("ugh");
+ if (mygetc() == 0xF9) /* graphic control ext. */
+ {
+ wxLogDebug("...");
+ f->Read(buf, 6);
+ wxLogDebug("buf[1] is %i (%i)", buf[1], buf[1] & 0x01);
+ if (buf[1] & 0x01) {
+ wxLogDebug("setting transparen %i", buf[4]);
+ img->transparent = buf[4];
+ }
+ }
+ else
+ while ((i = mygetc()) != 0) /* byte count */
+ f->SeekI(i, wxFromCurrent);
}
/* read image descriptor block (IDB) */
- fread(buf, 1, 9);
+ f -> Read(buf, 9);
img->w = buf[4] + 256 * buf[5];
img->h = buf[6] + 256 * buf[7];
if ((buf[8] & 0x80) == 0x80)
{
ncolors = 2 << (buf[8] & 0x07);
- fread(pal, 1, 3 * ncolors);
+ f -> Read(pal, 3 * ncolors);
}
/* get initial code size from first byte in raster data */
- bits = fgetc();
+ bits = mygetc();
/* allocate memory for image and palette */
if ((img->p = (unsigned char*) malloc(size)) == NULL) return E_MEMORIA;
bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
unsigned char *ptr, *src, *pal;
- int file_size;
- unsigned char* file_con;
IMAGEN igif;
int i;
gifDecoder *decod;
image->Destroy();
- file_size = stream.StreamSize();
- file_con = (unsigned char*) malloc(file_size);
- stream.Read(file_con, file_size);
- decod = new gifDecoder(file_con, file_size);
+ decod = new gifDecoder(&stream);
if (decod -> readgif(&igif) != E_OK) {
wxLogDebug("Error reading GIF");
- free(file_con);
delete decod;
return FALSE;
}
- free(file_con);
delete decod;
image->Create(igif.w, igif.h);
*(ptr++) = pal[3 * (*src) + 2];
}
+ if (igif.transparent != -1) {
+ wxLogDebug("oko");
+ image->SetMaskColour(pal[3 * (igif.transparent) + 0], pal[3 * (igif.transparent) + 0], pal[3 * (igif.transparent) + 0]);
+ image->SetMask(TRUE);
+ }
+
+ wxLogDebug("(unsigned int)%i", (unsigned int)-1);
free(igif.pal);
free(igif.p);
return TRUE;
return FALSE;
}
-//////////////////// Module:
-/* We haven't yet decided to go for the wxModule approach or
- * explicit handler-adding: assuming the latter for now -- JACS
-class wxGIFModule : public wxModule
-{
- DECLARE_DYNAMIC_CLASS(wxGIFModule)
- public:
- virtual bool OnInit()
- {
- wxImage::AddHandler(new wxGIFHandler);
- return TRUE;
- }
- virtual void OnExit() {}
-};
-IMPLEMENT_DYNAMIC_CLASS(wxGIFModule, wxModule)
-
-*/