diff --git a/examples/convert_gif_png.js b/examples/convert_gif_png.js new file mode 100644 index 00000000..0366b952 --- /dev/null +++ b/examples/convert_gif_png.js @@ -0,0 +1,14 @@ +/** + * Example for using LWIP to convert an image from png to jpg. + */ + +var path = require('path'), + lwip = require('../'); + +lwip.open('lena.gif', function(err, image) { + if (err) return console.log(err); + image.writeFile('lena_from_gif.png', function(err) { + if (err) return console.log(err); + console.log('done') + }); +}); diff --git a/examples/convert_png_gif.js b/examples/convert_png_gif.js new file mode 100644 index 00000000..2446b1a5 --- /dev/null +++ b/examples/convert_png_gif.js @@ -0,0 +1,14 @@ +/** + * Example for using LWIP to convert an image from png to jpg. + */ + +var path = require('path'), + lwip = require('../'); + +lwip.open('lena.png', function(err, image) { + if (err) return console.log(err); + image.writeFile('lena_from_png.gif', function(err) { + if (err) return console.log(err); + console.log('done') + }); +}); diff --git a/examples/lena_from_gif.png b/examples/lena_from_gif.png new file mode 100644 index 00000000..c87d48c7 Binary files /dev/null and b/examples/lena_from_gif.png differ diff --git a/src/decoder/gif_decoder.cpp b/src/decoder/gif_decoder.cpp index 5bf1f006..e6a14ff8 100644 --- a/src/decoder/gif_decoder.cpp +++ b/src/decoder/gif_decoder.cpp @@ -1,10 +1,14 @@ #include "decoder.h" +#define ALPHA_TRANS 0 +#define ALPHA_OPAQUE 255 +#define C_TRANS 0 + string decode_gif_buffer(char * buffer, size_t size, CImg ** cimg) { gifReadCbData buffinf = {(unsigned char *) buffer, size, 0}; - GifFileType * gif; - int errcode; + GifFileType * gif = NULL; + int errcode = 0; // buffinf will be available in gifReadCB as gif->userData gif = DGifOpen((void *) &buffinf, gifReadCB, &errcode); @@ -18,7 +22,7 @@ string decode_gif_buffer(char * buffer, size_t size, CImg ** cimg GraphicsControlBlock gcb; // only for the first image - DGifSavedExtensionToGCB(gif, 0, &gcb); + bool hasGCB = DGifSavedExtensionToGCB(gif, 0, &gcb) != GIF_ERROR; // if may return GIF_ERROR, which means this image has no gcb. // that's fine, as gcb's are optional @@ -42,20 +46,23 @@ string decode_gif_buffer(char * buffer, size_t size, CImg ** cimg size_t i = 0, len = width * height; GifByteType ci; - GifColorType *c; - for (; i < len; ci = ipxls[i++]){ - if (gcb.TransparentColor != ci){ - c = &cmap->Colors[ci]; - *(ptr_r++) = c->Red; - *(ptr_g++) = c->Green; - *(ptr_b++) = c->Blue; - *(ptr_a++) = 255; + GifColorType c; + GifColorType c_trans = {C_TRANS, C_TRANS, C_TRANS}; + unsigned char alpha; + + for (; i < len; i++){ + ci = ipxls[i]; + if (hasGCB && ci == gcb.TransparentColor){ + c = c_trans; + alpha = ALPHA_TRANS; } else { - *(ptr_r++) = 0; - *(ptr_g++) = 0; - *(ptr_b++) = 0; - *(ptr_a++) = 0; + c = cmap->Colors[ci]; + alpha = ALPHA_OPAQUE; } + *(ptr_r++) = c.Red; + *(ptr_g++) = c.Green; + *(ptr_b++) = c.Blue; + *(ptr_a++) = alpha; } if (GIF_ERROR == DGifCloseFile(gif, &errcode)){