Browse Source

c-style gif internals

v1.x
Tj Holowaychuk 14 years ago
parent
commit
b153b6785d
  1. 41
      src/Image.cc

41
src/Image.cc

@ -17,16 +17,12 @@
#endif #endif
#ifdef HAVE_GIF #ifdef HAVE_GIF
#include <gif_lib.h> #include <gif_lib.h>
typedef struct {
struct GIFInputFuncData {
uint8_t *buf; uint8_t *buf;
unsigned int length; unsigned len;
unsigned int cpos; unsigned pos;
}; } gif_data_t;
int readGIFFromMemory(GifFileType *gft, GifByteType *buf, int length);
int getGIFTransparentColor(GifFileType *gft, int framenum);
#endif #endif
Persistent<FunctionTemplate> Image::constructor; Persistent<FunctionTemplate> Image::constructor;
@ -352,10 +348,12 @@ Image::loadPNG() {
return cairo_surface_status(_surface); return cairo_surface_status(_surface);
} }
// GIF support
#ifdef HAVE_GIF #ifdef HAVE_GIF
int int
getGIFTransparentColor(GifFileType * gft, int framenum) { get_gif_transparent_color(GifFileType * gft, int framenum) {
ExtensionBlock *ext = gft->SavedImages[framenum].ExtensionBlocks; ExtensionBlock *ext = gft->SavedImages[framenum].ExtensionBlocks;
for (int ix = 0; ix < gft->SavedImages[framenum].ExtensionBlockCount; ix++, ext++) { for (int ix = 0; ix < gft->SavedImages[framenum].ExtensionBlockCount; ix++, ext++) {
@ -368,12 +366,12 @@ getGIFTransparentColor(GifFileType * gft, int framenum) {
} }
int int
readGIFFromMemory(GifFileType *gft, GifByteType *buf, int length) { read_gif_from_memory(GifFileType *gft, GifByteType *buf, int length) {
struct GIFInputFuncData *gifd = (struct GIFInputFuncData*)gft->UserData; gif_data_t *gifd = (gif_data_t *) gft->UserData;
// Make sure we don't read past our buffer // Make sure we don't read past our buffer
if((gifd->cpos + length) > gifd->length) length = gifd->length - gifd->cpos; if((gifd->pos + length) > gifd->len) length = gifd->len - gifd->pos;
memcpy(buf, gifd->cpos + gifd->buf, length); memcpy(buf, gifd->pos + gifd->buf, length);
gifd->cpos += length; gifd->pos += length;
return length; return length;
} }
@ -413,13 +411,13 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
int imageIdx = 0; int imageIdx = 0;
GifFileType* gft; GifFileType* gft;
struct GIFInputFuncData gifd = { gif_data_t gifd = {
buf: buf buf: buf
, length: len , len: len
, cpos: 0 , pos: 0
}; };
if((gft = DGifOpen((void*) &gifd, readGIFFromMemory)) == NULL) if((gft = DGifOpen((void*) &gifd, read_gif_from_memory)) == NULL)
return CAIRO_STATUS_READ_ERROR; return CAIRO_STATUS_READ_ERROR;
if(DGifSlurp(gft) != GIF_OK) { if(DGifSlurp(gft) != GIF_OK) {
@ -441,8 +439,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
ColorMapObject *colormap = img->ColorMap ? img->ColorMap : gft->SColorMap; ColorMapObject *colormap = img->ColorMap ? img->ColorMap : gft->SColorMap;
int bgColor = 0; int bgColor = 0;
int alphaColor = getGIFTransparentColor(gft, imageIdx); int alphaColor = get_gif_transparent_color(gft, imageIdx);
if(gft->SColorMap) if(gft->SColorMap)
bgColor = (uint8_t) gft->SBackGroundColor; bgColor = (uint8_t) gft->SBackGroundColor;
else if(alphaColor >= 0) else if(alphaColor >= 0)
@ -535,6 +532,8 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
} }
#endif /* HAVE_GIF */ #endif /* HAVE_GIF */
// JPEG support
#ifdef HAVE_JPEG #ifdef HAVE_JPEG
/* /*

Loading…
Cancel
Save