我已经阅读了giflib用法说明并编写了以下代码:
GifFileType *gifFile = DGifOpenFileName("D:\\my.gif00.gif");
DGifSlurp(gifFile);
int h = gifFile->SHeight;
int w = gifFile->SWidth;
int count = gifFile->ImageCount;
GifPixelType *myPixels = new GifPixelType[w];
int errcode = DGifGetLine(gifFile, myPixels, 1);
if (errcode == GIF_OK) {
} else {
PrintGifError();
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我在DGIFOpenFileName函数中设置的任何文件都会导致错误,PrintGifError()会打印出以下消息:
GIF-LIB错误:#Pixels大于宽度*高度.
我无法理解我的代码中出了什么问题.我想要的是读取gif文件的像素,编辑它们然后设置回gif文件.你能帮忙解决这个问题吗?
据我所知,这似乎表明你的gif文件已损坏.
问题是你不应该使用DGifGetLine().该函数DGifSlurp()将整个GIF文件读入gifFile结构.在内部,它调用DGifGetLine()(以及其他一些东西),并且当它返回时,整个文件已被处理.DGifGetLine()在那之后尝试打电话没有意义.
这就是dgif_lib.c所说的DGifSlurp():
/******************************************************************************
This routine reads an entire GIF into core, hanging all its state info off
the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
first to initialize I/O. Its inverse is EGifSpew().
*******************************************************************************/
Run Code Online (Sandbox Code Playgroud)