文章目录
- 如何使用`boost.gil`解析`tiff`图片并返回像素
<2022-06-11 周六>
如何使用boost.gil
解析tiff
图片并返回像素
这是我的测试代码,能工作,我觉得比较有用,代码片断贴在这里:
#include <boost/gil.hpp>
#include <boost/gil/extension/io/tiff.hpp>
#include <boost/gil/extension/numeric/sampler.hpp>
#include <boost/gil/extension/numeric/resample.hpp>const int PIXELCNT = 4;if (lpArgs[3] == 3) {namespace bg = boost::gil;bg::rgba8_image_t img;bg::read_image(lpimgPath, img, bg::tiff_tag{});bg::rgba8_image_t out(*pDestW, *pDestH);bg::resize_view(bg::const_view(img), bg::view(out),bg::bilinear_sampler{});size_t size = *pDestW * *pDestH * PIXELCNT;unsigned char* ret = new unsigned char[size];int i = 0;for (int y = 1; y < *pDestH; ++y) {bg::rgba8_view_t::x_iterator it = out._view.row_begin(y);for (int x = 0; x < *pDestW; ++x, ++i, ++it) {ret[PIXELCNT * i + 0] = *((unsigned char*)it + 3);ret[PIXELCNT * i + 1] = *((unsigned char*)it + 2);ret[PIXELCNT * i + 2] = *((unsigned char*)it + 1);ret[PIXELCNT * i + 3] = *((unsigned char*)it + 0);}}return ret;
}
另外,在debian
虚拟机上我用的是预装的boost-1.67
,版本有点低,原以为低版本boost.gil
不应该更稳定嘛,况且还是debian
精挑细选的稳定版本,其实这个版本的boost.gil
根本是不能用的,因为它没有boost/gil/extension/numeric/sampler.hpp
等相关头文件,可以看boost-1.67
的文档,会提示你去下载缺失的扩展部分,但是好像是adobe
网站吧,已经不提供这些扩展部分的下载了,这是我遇到的第一个坑。
而且即使你下载了这些扩展部分,由于boost.gil
的更新较大,代码不能通用,所以我最后选择了最新的boost-1.79
版本,因为boost.gil
只需要头文件,不需要链接库,所以只要debian
的编译器支持即可。