Skip to content

Commit

Permalink
fix seg fault when decoding gif on my ubuntu machine
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Dec 20, 2014
1 parent acc56e2 commit 0bcfd76
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
14 changes: 14 additions & 0 deletions examples/convert_gif_png.js
Original file line number Diff line number Diff line change
@@ -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')
});
});
14 changes: 14 additions & 0 deletions examples/convert_png_gif.js
Original file line number Diff line number Diff line change
@@ -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')
});
});
Binary file added examples/lena_from_gif.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 22 additions & 15 deletions src/decoder/gif_decoder.cpp
Original file line number Diff line number Diff line change
@@ -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<unsigned char> ** 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);
Expand All @@ -18,7 +22,7 @@ string decode_gif_buffer(char * buffer, size_t size, CImg<unsigned char> ** 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

Expand All @@ -42,20 +46,23 @@ string decode_gif_buffer(char * buffer, size_t size, CImg<unsigned char> ** 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)){
Expand Down

0 comments on commit 0bcfd76

Please sign in to comment.