// g++ gif_wrapper.cpp -o testgif -L./lib -lgif -I./lib -I./
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include "gif_wrapper.h"
CGifImage::CGifImage():mpGifFile(NULL),mpScreenBuffer(NULL)
{
}
CGifImage::~CGifImage()
{
close();
}
void CGifImage::close()
{
if (NULL != mpScreenBuffer)
{
(void)free(mpScreenBuffer);
mpScreenBuffer=NULL;
}
if (NULL != mpGifFile)
{
int Error=0;
DGifCloseFile(mpGifFile, &Error);
mpGifFile=NULL;
}
}
bool CGifImage::open( const char* gif_path )
{
GifFileType *GifFile=NULL;
GifRowType * ScreenBuffer=NULL;
int i, j, Size, Row, Col, Width, Height, ExtCode, Count,Error;
GifRecordType RecordType;
GifByteType *Extension;
int ImageNum = 0;
int InterlacedOffset[] = { 0, 4, 2, 1 }; /* The way Interlaced image should. */
int InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */
if ((GifFile = DGifOpenFileName(gif_path, &Error)) == NULL)
{
close();
return false;
}
/*
* * Allocate the screen as vector of column of rows. Note this
* * screen is device independent - it's the screen defined by the
* * GIF file parameters.
* */
if ((ScreenBuffer = (GifRowType *)malloc(GifFile->SHeight * sizeof(GifRowType))) == NULL)
{
//GIF_EXIT("Failed to allocate memory required, aborted.");
close();
return false;
}
Size = GifFile->SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/
if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */
{
//GIF_EXIT("Failed to allocate memory required, aborted.");
close();
return false;
}
for (i = 0; i < GifFile->SWidth; i++) /* Set its color to BackGround. */
ScreenBuffer[0][i] = GifFile->SBackGroundColor;
for (i = 1; i < GifFile->SHeight; i++)
{
/* Allocate the other rows, and set their color to background too: */
if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL)
{
//GIF_EXIT("Failed to allocate memory required, aborted.");
close();
return false;
}
memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
}
/* Scan the content of the GIF file and load the image(s) in: */
do
{
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
switch (RecordType)
{
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(GifFile) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
Row = GifFile->Image.Top; /* Image Position relative to Screen. */
Col = GifFile->Image.Left;
Width = GifFile->Image.Width;
Height = GifFile->Image.Height;
//GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ", PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight)
{
//fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n",ImageNum);
//exit(EXIT_FAILURE);
close();
return false;
}
if (GifFile->Image.Interlace)
{
/* Need to perform 4 passes on the images: */
for (Count = i = 0; i < 4; i++)
for (j = Row + InterlacedOffset[i]; j < Row + Height;j += InterlacedJumps[i])
{
//GifQprintf("\b\b\b\b%-4d", Count++);
if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],Width) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
}
}
else
{
for (i = 0; i < Height; i++)
{
//GifQprintf("\b\b\b\b%-4d", i);
if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],Width) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
}
}
break;
case EXTENSION_RECORD_TYPE:
/* Skip any extension blocks in file: */
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
while (Extension != NULL)
{
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR)
{
//PrintGifError(GifFile->Error);
//exit(EXIT_FAILURE);
close();
return false;
}
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be trapped by DGifGetRecordType. */
break;
}
} while (RecordType != TERMINATE_RECORD_TYPE);
// printf("open finish:GifFile=%x width=%d,height=%d ScreenBuffer=%x\n",GifFile,GifFile->SWidth,GifFile->SHeight,ScreenBuffer);
mpGifFile=GifFile;
mpScreenBuffer=ScreenBuffer;
return true;
}
int main()
{
CGifImage gif;
if (false == gif.open("0.gif"))
{
printf("Error: gif.open\n");
return 1;
}
ColorMapObject *ColorMap = (gif.mpGifFile->Image.ColorMap? gif.mpGifFile->Image.ColorMap: gif.mpGifFile->SColorMap);
if (ColorMap == NULL)
{
printf("Error: ColorMap NULL\n");
return 2;
}
printf("info:gif=%x width=%d,height=%d\n",(void*)&gif,gif.mpGifFile->SWidth,gif.mpGifFile->SHeight);
GifRowType GifRow;
GifColorType *ColorMapEntry;
for (int i = 0; i < gif.mpGifFile->SHeight; i++)
{
GifRow = gif.mpScreenBuffer[i];
for (int j = 0; j < gif.mpGifFile->SWidth; j++)
{
ColorMapEntry = &ColorMap->Colors[GifRow[j]];
printf("row-col=(%d,%d) color=%d rgb=(%d %d %d)\n",i,j,GifRow[j],ColorMapEntry->Red,ColorMapEntry->Green,ColorMapEntry->Blue);
}
}
printf("ok\n");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
gif处理.zip (8个子文件)
giflib-5.1.0.tar.bz2 607KB
gif处理
gif_wrapper.cpp 5KB
0.gif 2KB
gif_wrapper.h 479B
lib
gif_lib.h 13KB
libgif.a 156KB
gif_hash.h 1KB
gif_lib_private.h 2KB
共 8 条
- 1
长寿梦
- 粉丝: 49
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页