图像库和 gif 图像
我需要在 C 程序中检查 gif 图像。 我查看了 FreeImage 库,但无法弄清楚。 有人可以举一个使用 FreeImage 来了解有关 gif 图像的所有小细节的示例,例如帧、背景、透明度等...以及如何递增帧并获取动画的当前图像/像素。 我遇到的问题是您将图像加载到 FIBITMAP 类型中,而大多数函数都采用 FIBITMAP 类型。 我不明白你应该如何使用带有 gif 的库。
另外,有没有更简单的库?
Ferruccio 说使用 libGD。 这是我在这个库中发现的内容。
/* These read the first frame only */
BGD_DECLARE(gdImagePtr) gdImageCreateFromGif (FILE * fd);
我需要所有的框架。
下面是一种工作方式。 问题是图像的宽度/高度变化小于实际图像。 我认为它只是显示了更改的内容,但我不确定如何将其映射回原始图像以充分利用它。 这是 Gif 文件。
实际上这个库并没有像我想象的那样工作。 例如,我无法获取图像的调色板。 我得到的全是零。 对此有何建议?
#include <stdio.h>
#include <string.h>
#include <FreeImage.h>
int main(int argc, char *argv) {
FIMULTIBITMAP *src;
src = FreeImage_OpenMultiBitmap(FIF_GIF, "clock_logo_ani.gif", FALSE, TRUE, FALSE, GIF_DEFAULT);
int count = FreeImage_GetPageCount(src);
int page, i, row, col, background, transparent;
RGBQUAD bkcolor;
BYTE pixel;
char buffer[25*16 + 16];
RGBQUAD *palette;
background = 0;
for(page = 0; page < count; page++) {
FIBITMAP *dib = FreeImage_LockPage(src, page);
if(dib) {
printf("%dx%d\n", FreeImage_GetHeight(dib), FreeImage_GetWidth(dib));
FreeImage_GetBackgroundColor(dib, &bkcolor);
background = (&bkcolor)->rgbReserved;
transparent = FreeImage_GetTransparentIndex(dib);
memset(buffer, ' ', sizeof(char) * 24 * 16 + 17 * sizeof(char));
for( row = 0; row < 16; row++ ) {
for( col = 0; col < 24; col++ ) {
if ( FreeImage_GetPixelIndex(dib, col, row, &pixel) ) {
if((int)pixel != 0)
buffer[row * 25 + col] = '.';
}
}
}
for (row = 0; row < 16; row++)
buffer[row * 25 + 25] = '\n';
buffer[row * 25] = '\0';
printf("-----\n%s\n-----\n", buffer);
FreeImage_UnlockPage(src, dib, FALSE);
}
}
FreeImage_CloseMultiBitmap(src, GIF_DEFAULT);
}
这是输出。 注意图像大小如何变化。
16x24
-----
...
.. ..
. .
. .
. .
. .
. .
. ... .
. . .
. . .
. . .
. . .
. .
.. ..
...
-----
4x4
-----
.
.
.
-----
4x3
-----
...
-----
4x3
-----
.
.
.
-----
4x4
-----
.
-----
4x4
-----
.
.
-----
4x4
-----
...
-----
4x4
-----
.
.
.
-----
I need to examine a gif image in a C program. I looked into the FreeImage library but I can't figure it out. Could someone give an example of using FreeImage to get into all the little details about a gif image such as frames, background, transparency, etc... And how to increment through the frames and get the animation's current image/pixels. The problem I'm having is that you load the image into a FIMULTIBITMAP type, while most of the functions take a FIBITMAP type. I'm not seeing how you're supposed to use the library with gifs.
Also, is there a simpler library?
Ferruccio said to use libGD. Here's what I found with this library.
/* These read the first frame only */
BGD_DECLARE(gdImagePtr) gdImageCreateFromGif (FILE * fd);
I need all of the frames.
Below is kind of working. The problem is the image's width/height change to be smaller than the actual image. I think it's just showing what changed, but I'm not sure how to map that back onto the original image to make any use of it. Here's the Gif File.
Actually this library's not working like I assume it's supposed to work. For instance, I can't get the palette for the image. What I get is all zeros. Any suggestions on that?
#include <stdio.h>
#include <string.h>
#include <FreeImage.h>
int main(int argc, char *argv) {
FIMULTIBITMAP *src;
src = FreeImage_OpenMultiBitmap(FIF_GIF, "clock_logo_ani.gif", FALSE, TRUE, FALSE, GIF_DEFAULT);
int count = FreeImage_GetPageCount(src);
int page, i, row, col, background, transparent;
RGBQUAD bkcolor;
BYTE pixel;
char buffer[25*16 + 16];
RGBQUAD *palette;
background = 0;
for(page = 0; page < count; page++) {
FIBITMAP *dib = FreeImage_LockPage(src, page);
if(dib) {
printf("%dx%d\n", FreeImage_GetHeight(dib), FreeImage_GetWidth(dib));
FreeImage_GetBackgroundColor(dib, &bkcolor);
background = (&bkcolor)->rgbReserved;
transparent = FreeImage_GetTransparentIndex(dib);
memset(buffer, ' ', sizeof(char) * 24 * 16 + 17 * sizeof(char));
for( row = 0; row < 16; row++ ) {
for( col = 0; col < 24; col++ ) {
if ( FreeImage_GetPixelIndex(dib, col, row, &pixel) ) {
if((int)pixel != 0)
buffer[row * 25 + col] = '.';
}
}
}
for (row = 0; row < 16; row++)
buffer[row * 25 + 25] = '\n';
buffer[row * 25] = '\0';
printf("-----\n%s\n-----\n", buffer);
FreeImage_UnlockPage(src, dib, FALSE);
}
}
FreeImage_CloseMultiBitmap(src, GIF_DEFAULT);
}
Here's the output. Notice how the image's size changes.
16x24
-----
...
.. ..
. .
. .
. .
. .
. .
. ... .
. . .
. . .
. . .
. . .
. .
.. ..
...
-----
4x4
-----
.
.
.
-----
4x3
-----
...
-----
4x3
-----
.
.
.
-----
4x4
-----
.
-----
4x4
-----
.
.
-----
4x4
-----
...
-----
4x4
-----
.
.
.
-----
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在问题中包含的代码中,锁定页面后,您可以执行以下操作:
FreeImage_LockPage 为您提供的指针是该框架的图像。 您应该能够在任何采用 FIBITMAP 类型的其他函数中使用它。 (但请记住,在上面的代码中,您以只读方式打开它)
您已经知道动画中有多少帧,“count”可以告诉您这一点。 (当然是从零开始的)这非常简单。
In the code included in your question, once you lock the page, you can do:
The pointer that FreeImage_LockPage gives you is that frame's image. You should be able to use it in any of the other functions that take the FIBITMAP type. (but remember, in the code above, you opened it read-only)
You already know how many frames are in the animation, "count" gives you that. (zero based, of course) It is pretty simple.
如果您愿意使用 C++,ImageMagick 具有执行此操作的绑定。 它有 ac api,但我没有看到任何函数可以做到这一点。
If you are willing to use c++, ImageMagick has bindings to do this. It has a c api, but I don't see any functions to do that.
我在 LibGD 方面运气不错。
I've had good luck with LibGD.