- Tensorflow C++ 内部提供PNG图片解码的操作,其定义的头文件为
#include "tensorflow/core/lib/png/png_io.h"
std::string pngString;
std::fstream png_in("xxxxx.png");
png_in.seekg(0, std::ios::end);
pngString.resize(png_in.tellg());
png_in.seekg(0, std::ios::beg);
png_in.read(&pngString[0], pngString.size());
png_in.close();
tensorflow::png::DecodeContext context;
CommonInitDecode(pngString, 3 , 8 , &context);
char* image_buffer = new char[3*context.width*context.height];
CommonFinishDecode(tensorflow::bit_cast<png_byte*>(image_buffer),
3*context.width , &context);
delete[] image_buffer;
- 注意:CommonInitDecode函数后,context对象的data成员,context.data不是PNG解码后的数据。而是,去掉了PNG头、标志位等等的原始数据,不要使用该数据进行处理,否则可能会导致内存泄露(非法访问)。