Poko
Poko

Reputation: 822

Extracting frames with giflib segfault C++

I'am actually trying to extract images from a gifFile using the giflib with the following code.

t_gif   initGif(const char *filename){
    t_gif gif;
    int *error;
    GifFileType *GifFile = DGifOpenFileName(filename, error);
    assert(error != NULL);

    int ret = DGifSlurp(GifFile);
    assert(ret == GIF_OK);

    gif.h = (int)GifFile->SHeight;
    gif.w = GifFile->SWidth;
    gif.nbFrames = GifFile->ImageCount;
    gif.colorSize = GifFile->SColorResolution;

    GifImageDesc Image = GifFile->Image;
    SavedImage *img = &GifFile->SavedImages[0];

    cout << "width: " << gif.w << endl;
    cout << "height: " << gif.h << endl;
    cout << "Image Count: " << gif.nbFrames << endl;
    cout << "SColor Resolution: " << gif.colorSize << endl;
    Mat color = Mat(Size(gif.w, gif.h), CV_8UC1, img->RasterBits);
    imwrite("./test.png", color);
    return gif;
}

But this causes a segfault. I am using opencv in v2.4.5 and giflib in v5.0.4. I think this is not caused by Opencv because with giflib in v4 I had no problem here.

MoreOver the following test also causes segfault.

printf("%u\n", (unsigned int)img->RasterBits[0]);

Gdb output:

(gdb) run bsd.gif 
Starting program: /home/matt/Code/perso/utils/gif/a.out bsd.gif
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Gif Analyser tool
started by MG in may 2013
compiled with giflib: v5.0.4
width: 1000
height: 907
Image Count: 0
SColor Resolution: 8

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401af3 in initGif (filename=0x7fffffffe2be "bsd.gif") at main.cpp:40
40      Mat color = Mat(Size(gif.w, gif.h), CV_8UC1, img->RasterBits);
(gdb) 

I also don't find how to get color back using the colorGlobal table.

Can someone Help me ? Thanks

Upvotes: 2

Views: 1442

Answers (2)

TurboTurd
TurboTurd

Reputation: 56

To get the color information , you can use the rasterbits of the image as index to the colormap hence getting the RGB information for the corresponding pixel.Then you can simply use CV_8UC3 data type for storing the gif frame as a colored png .

Upvotes: 0

Poko
Poko

Reputation: 822

I just get back to version 4.1.6 and everything is good. The v5 is probably bugged.

Upvotes: 1

Related Questions