C++获取GIF图片的长和宽

c++

bool GetGifSize(const char* file_path, int* width, int* height)
{
	bool has_image_size = false;
	int file_size;
	*height = -1;
	*width = -1;
	file_size = -1;
	FILE * fp = fopen(file_path, "rb");
	if (fp == NULL)
		return has_image_size;
	struct stat st;
	char sigBuf[26];
	if (fstat(fileno(fp), &st) < 0)
	{
		fclose(fp);
		return has_image_size;
	}
	else
	{
		file_size = st.st_size;
	}
	if (fread(&sigBuf, 26, 1, fp) < 1)
	{
		fclose(fp);
		return has_image_size;
	}

	char* gif87_signature = "GIF87a";
	char* gif89_signature = "GIF89a";

	if ((file_size >= 10) && (memcmp(sigBuf, gif87_signature, strlen(gif87_signature)) == 0 || memcmp(sigBuf, gif89_signature, strlen(gif89_signature)) == 0))
	{
		// image type: gif
		unsigned short* size_info = (unsigned short*)(sigBuf + 6);
		*width = size_info[0];
		*height = size_info[1];
		has_image_size = true;
	}
	if (fp != NULL)
		fclose(fp);
	return has_image_size;
}

int main()
{
	char[MAX_PATH] file_path="d://123.gif";
	int nImgWidth;
	int nImgHeight;
	GetGifSize(file_path,&nImgWidth, &nImgHeight);
}