本系列文章以koral实现的GIF文件native渲染为根据,解读实现的大致过程以及关键代码分析,github地址:https://github.com/koral–/android-gif-drawable。
由前文中纹理贴图native实现可知,若想实现图像的OpenGL渲染(主要glTexImage2D函数),首先需要解析出该图像文件(当时PNG为例)的图像数据以及其它相关信息,然后利用native OpenGL API实现,而GIF文件的渲染过程稍微麻烦一些,因为涉及到多帧图像的渲染,具体过程可以大致分为以下几个步骤。
(1)创建解析GIF文件的句柄
利用GIFLIB解析GIF文件,把相关信息保存在GifInfo结构体中,并将该结构体的指针(long)返回给JAVA层,作为访问GIF文件这些信息的句柄。从JAVA层到GIFLIB的大致实现流程如下:
GifInfo结构体:
struct GifInfo {
void (*destructor)(GifInfo *, JNIEnv *);
GifFileType *gifFilePtr;
GifWord originalWidth, originalHeight;
uint_fast16_t sampleSize;
long long lastFrameRemainder;
long long nextStartTime;
uint_fast32_t currentIndex;
GraphicsControlBlock *controlBlock;
argb *backupPtr;
long long startPos;
unsigned char *rasterBits;
uint_fast32_t rasterSize;
char *comment;
uint_fast16_t loopCount;
uint_fast16_t currentLoop;
RewindFunc rewindFunction;
jfloat speedFactor;
int32_t stride;
jlong sourceLength;
bool isOpaque;
void *frameBufferDescriptor;
};
解析GIF文件相关信息的关键代码:
/******************************************************************************
This routine should be called before any other DGif calls. Note that
this routine is called automatically from DGif file open routines.
******************************************************************************/
int
DGifGetScreenDesc(GifFileType *GifFile) {
// bool SortFlag;
GifByteType Buf[3];
// GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
// if (!IS_READABLE(Private)) {
// /* This file was NOT o