在特定的应用场景下,有时候我们只是想获取图片的宽高,
但不想通过解码图片才取得这个信息。
预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验。
在*有一篇相关讨论。
Get Image size WITHOUT loading image into memory
http://*.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/
不加图片到内存,而取得图像的大小。
这个技巧具有一定的实用价值,博主据此进行了相应的编码。
实现了 常用图片格式(png,jpeg,ico,bmp,gif) 不采用第三方解码库,解析得到图像宽高的函数get_image_size_without_decode_image。
bool get_image_size_without_decode_image(const char* file_path, int*width, int*height);
完整代码:
#include <stdio.h> #include <sys/stat.h> unsigned long byteswap_ulong(unsigned long i) { unsigned int j; j = (i << ); j += (i << ) & 0x00FF0000; j += (i >> ) & 0x0000FF00; j += (i >> ); return j; } inline int Abs(int x) { )) - (x >> ); } unsigned short byteswap_ushort(unsigned short i) { unsigned short j; j = (i << ); j += (i >> ); return j; } // Get Image size WITHOUT loading image into memory // ref: http://*.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/ // 博客: http://tntmonks.cnblogs.com/ // 邮箱: gaozhihan@vip.qq.com bool get_image_size_without_decode_image(const char* file_path, int*width, int*height) { bool has_image_size = false; *height = -; *width = -; ; FILE * fp = fopen(file_path, "rb"); if (fp == NULL) return has_image_size; struct stat st; ]; ) { fclose(fp); return has_image_size; } else { file_size = st.st_size; } , , fp) < ) { fclose(fp); return has_image_size; } char* png_signature = "\211PNG\r\n\032\n"; unsigned ] = { 'I', 'H', 'D', 'R' }; char* gif87_signature = "GIF87a"; char* gif89_signature = "GIF89a"; char* jpeg_signature = "\377\330"; char* bmp_signature = "BM"; ) && (memcmp(sigBuf, gif87_signature, strlen(gif87_signature)) == || memcmp(sigBuf, gif89_signature, strlen(gif89_signature)) == )) { // image type: gif unsigned ); *width = size_info[]; *height = size_info[]; has_image_size = true; } ) && (memcmp(sigBuf, png_signature, strlen(png_signature)) == && memcmp(sigBuf + , ihdr_signature, strlen(ihdr_signature)) == )) { // image type: png unsigned ); *width = byteswap_ulong(size_info[]); *height = byteswap_ulong(size_info[]); has_image_size = true; } ) && (memcmp(sigBuf, png_signature, strlen(png_signature)) == )) { // image type: old png unsigned ); *width = byteswap_ulong(size_info[]); *height = byteswap_ulong(size_info[]); has_image_size = true; } ) && (memcmp(sigBuf, jpeg_signature, strlen(jpeg_signature)) == )) { // image type: jpeg fseek(fp, , SEEK_SET); ; fread(&sigBuf, , , fp); fread(&b, , , fp); ; ; while (b && ((unsigned char)b & 0xff) != 0xDA) { while (((unsigned char)b & 0xff) != 0xFF) { fread(&b, , , fp); } while (((unsigned char)b & 0xff) == 0xFF) { fread(&b, , , fp); } if (((unsigned char)b & 0xff) >= 0xC0 && ((unsigned char)b & 0xff) <= 0xC3) { fread(&sigBuf, , , fp); fread(&sigBuf, , , fp); unsigned short* size_info = (unsigned short*)(sigBuf); h = byteswap_ushort(size_info[]); w = byteswap_ushort(size_info[]); } else { unsigned ; fread(&chunk_size, , , fp); , SEEK_CUR) != ) break; } fread(&b, , , fp); } && h != -) { *width = w; *height = h; } has_image_size = true; } ) && (memcmp(sigBuf, bmp_signature, strlen(bmp_signature)) == )) { // image type: bmp unsigned )); ) { unsigned ); *width = size_info[]; *height = size_info[]; } ) { unsigned ); *width = size_info[]; *height = Abs((size_info[])); } has_image_size = true; } ) { // image type: ico fseek(fp, , SEEK_SET); unsigned ; unsigned ; fread(&reserved, , , fp); fread(&format, , , fp); && format == ) { unsigned ; fread(&num, , , fp); ) { printf("ico 包含多个图片"); } else { , h = ; fread(&w, , , fp); fread(&h, , , fp); *width = int((unsigned char)w & 0xff); *height = int((unsigned char)h & 0xff); } } has_image_size = true; } if (fp != NULL) fclose(fp); return has_image_size; }
调用方法:
const char* file_path = "d:\\test.png";
int h, w;
get_image_size_without_decode_image(file_path , &w, &h);
传入图片的位置,输出对应的宽高,高和宽 为-1时,就是解析失败了。
代码比较简单,不多注释了。
若有其他相关问题或者需求可以邮件联系俺探讨。
邮箱地址是:
gaozhihan@vip.qq.com