不用第三方解码库取得图片宽高 附完整C++算法实现代码
在特定的应用场景下,有时候我们只是想获取图片的宽高,
但不想通过解码图片才取得这个信息。
预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验。
在stackoverflow有一篇相关讨论。
Get Image size WITHOUT loading image into memory
http://stackoverflow.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/
不加图片到内存,而取得图像的大小。
这个技巧具有一定的实用价值,博主据此进行了相应的编码。
项目地址:https://github.com/cpuimage/image_size
实现了 常用图片格式(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,int *filesize);
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdint.h> #if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
#pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
#endif /*_MSC_VER */ 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) {
return (x ^ (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://stackoverflow.com/questions/15800704/python-get-image-size-without-loading-image-into-memory/ bool get_image_size_without_decode_image(const char* file_path, int*width, int*height, int * file_size)
{
bool has_image_size = false;
*height = -;
*width = -;
*file_size = -;
FILE * fp = fopen(file_path, "rb");
if (fp == NULL)
return has_image_size;
struct stat st;
char sigBuf[];
if (fstat(fileno(fp), &st) < )
{
fclose(fp);
return has_image_size;
}
else
{
*file_size = st.st_size;
}
if (fread(&sigBuf, , , fp) < )
{
fclose(fp);
return has_image_size;
}
char* png_signature = "\211PNG\r\n\032\n";
char* ihdr_signature = "IHDR";
char* gif87_signature = "GIF87a";
char* gif89_signature = "GIF89a";
char* jpeg_signature = "\377\330";
char* bmp_signature = "BM";
if ((*file_size >= ) && (memcmp(sigBuf, gif87_signature, strlen(gif87_signature)) == || memcmp(sigBuf, gif89_signature, strlen(gif89_signature)) == ))
{
// image type: gif
unsigned short* size_info = (unsigned short*)(sigBuf + );
*width = size_info[];
*height = size_info[];
has_image_size = true;
}
else if ((*file_size >= ) && (memcmp(sigBuf, png_signature, strlen(png_signature)) == && memcmp(sigBuf + , ihdr_signature, strlen(ihdr_signature)) == ))
{
// image type: png
unsigned long* size_info = (unsigned long*)(sigBuf + );
*width = byteswap_ulong(size_info[]);
*height = byteswap_ulong(size_info[]);
has_image_size = true;
}
else if ((*file_size >= ) && (memcmp(sigBuf, png_signature, strlen(png_signature)) == ))
{
// image type: old png
unsigned long* size_info = (unsigned long*)(sigBuf + );
*width = byteswap_ulong(size_info[]);
*height = byteswap_ulong(size_info[]);
has_image_size = true;
}
else if ((*file_size >= ) && (memcmp(sigBuf, jpeg_signature, strlen(jpeg_signature)) == ))
{
// image type: jpeg
fseek(fp, , SEEK_SET);
char b = ;
fread(&sigBuf, , , fp);
fread(&b, , , fp);
int w = -;
int h = -;
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 short chunk_size = ;
fread(&chunk_size, , , fp);
if (fseek(fp, byteswap_ushort(chunk_size) - , SEEK_CUR) != )
break;
}
fread(&b, , , fp);
}
if (w != - && h != -)
{
*width = w;
*height = h;
}
has_image_size = true;
}
else if ((*file_size >= ) && (memcmp(sigBuf, bmp_signature, strlen(bmp_signature)) == ))
{
// image type: bmp
unsigned int header_size = (*(sigBuf + ));
if (header_size == )
{
unsigned short* size_info = (unsigned short*)(sigBuf + );
*width = size_info[];
*height = size_info[];
}
else if (header_size >= )
{
unsigned int* size_info = (unsigned int*)(sigBuf + );
*width = size_info[];
*height = Abs((size_info[]));
}
has_image_size = true;
}
else if (*file_size >= )
{
// image type: ico
fseek(fp, , SEEK_SET);
unsigned short format = -;
unsigned short reserved = -;
fread(&reserved, , , fp);
fread(&format, , , fp);
if (reserved == && format == )
{
unsigned short num = -;
fread(&num, , , fp);
if (num > )
{
printf("this is a muti-ico file.");
}
else
{
char w = , 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;
} int main(int argc, char ** argv) { const char * pic_file = "lookmanodeps.png";
int w = ;
int h = ;
int filesize = ;
get_image_size_without_decode_image(pic_file, &w, &h, &filesize);
printf("file:%s\nfilesize:%d\nwidth:%d\nheight%d\n", pic_file, filesize, w, h);
getchar();
}
传入图片的位置,输出对应的宽高,高和宽 为-1时,就是解析失败了。
代码比较简单,不多注释了。
若有其他相关问题或者需求可以邮件联系俺探讨。
邮箱地址是:
gaozhihan@vip.qq.com
不用第三方解码库取得图片宽高 附完整C++算法实现代码的更多相关文章
- 不用第三方解码码取得图片宽高 附完整C++算法实现代码
在特定的应用场景下,有时候我们只是想获取图片的宽高, 但不想通过解码图片才取得这个信息. 预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验. 在stackoverflow有一篇相关 ...
- JS快速获取图片宽高的方法
快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...
- 转载:JS快速获取图片宽高的方法
快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...
- php 图片上传的公共方法(按图片宽高缩放或原图)
写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...
- css3圆形头像(当图片宽高不相等时)
1.图片宽高相等,width:300px: height:300px; 把他变成宽高100px的圆形头像 img{width:100px; height:100px; border-radius:50 ...
- JS实现图片宽高的等比缩放
关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形. 例如,需要实现如下的效果: 要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高. ...
- js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)
js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...
- css+background实现 图片宽高自适应,拉伸裁剪不变形
图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性. 下面咱们在网上找两张宽高不一样的照片: No.1 ...
- 写个js动态调整图片宽高 (原创)
<body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...
随机推荐
- Python中的PYTHONPATH环境变量
PYTHONPATH是Python中一个重要的环境变量,用于在导入模块的时候搜索路径.可以通过如下方式访问: >>> import sys >>> sys.path ...
- shell随机生成身份证,姓名,电话,日期,分数,等级和insert语句
#!/bin/bash#生成随机身份证号,性别,年龄,电话,姓名,日期,分数和对应等级,并生成insert语句#作者AiYS,2018-02-06,转载请注明http://www.cnblogs.co ...
- 四则运算程序(java基于控制台)
四则运算题目生成程序(基于控制台) 一.题目描述: 1. 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 -o Exercise.txt 将生成10个题目. 2. 使用 -r ...
- Django--入门篇:下载与项目生成
django作为python web应用开发最火的框架,没有之一,今天就给大家介绍django的一些入门知识. 我们选择pycharm工具,首先得要有django. 1.下载django --pip ...
- Java8学习(4)-Stream流
Stream和Collection的区别是什么 流和集合的区别是什么? 粗略地说, 集合和流之间的差异就在于什么时候进行计算.集合是一个内存中的数据结构,它包含数据结构中目前所有的值--集合中的每个元 ...
- alpha-咸鱼冲刺day4
一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 QAQ具体工作量没啥进展.但是前后端终于可以数据交互了!.. 四,问题困难 日常啥都不会,百度真心玩一年. 还得自学nodejs ...
- 大神都在看的RxSwift 的完全入坑手册
大神都在看的RxSwift 的完全入坑手册 2015-09-24 18:25 CallMeWhy callmewhy 字号:T | T 我主要是通过项目里的 Rx.playground 进行学习和了解 ...
- AngularJS1.X学习笔记13-路由
ThinkPHP框架有路由的概念,看起来路由更多的是后端的事情,Angular怎么也会跑出个路由呢?事实上,Angular是着眼于单页应用的,他的一个应用一般来说是一个页面,你所看到的页面内容的改变, ...
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- emqtt 试用(一)安装和测试
一.安装 http://emqtt.io/docs/v2/getstarted.html http://emqtt.io/docs/v2/advanced.html http://emqtt.io/d ...