C++ LibTiff - 从内存读取和保存文件

标签 c++ memory libtiff

在 LibTiff 中有没有办法从内存中读取文件并将其保存到内存中?

我不想先将图像保存到光盘,然后再用其他库打开它...

非常感谢!

最佳答案

我知道这是一个老问题,但我将为像我这样需要此信息以获取最新版本的 libtiff 的人发布一个更简单、更最新的答案。在最新版本的 libtiff (4.0.2) 中,甚至我相信的过去几个版本中(检查您的具体版本号),都有一个名为 tiffio.hxx 的包含文件。它有两个外部函数用于读取/写入内存中的流:

extern TIFF* TIFFStreamOpen(const char*, std::ostream *);
extern TIFF* TIFFStreamOpen(const char*, std::istream *);

您可以只包含此文件并读取或写入内存。

编写示例:

#include <tiffio.h>
#include <tiffio.hxx>
#include <sstream>    

std::ostringstream output_TIFF_stream;

//Note: because this is an in memory TIFF, just use whatever you want for the name - we 
//aren't using it to read from a file
TIFF* mem_TIFF = TIFFStreamOpen("MemTIFF", &output_TIFF_stream);

//perform normal operations on mem_TIFF here like setting fields
//...

//Write image data to the TIFF 
//..

TIFFClose(mem_TIFF);   

//Now output_TIFF_stream has all of my image data. I can do whatever I need to with it.

读起来很相似:

#include <tiffio.h>
#include <tiffio.hxx>
#include <sstream>

std::istringstream input_TIFF_stream;
//Populate input_TIFF_stream with TIFF image data
//...

TIFF* mem_TIFF = TIFFStreamOpen("MemTIFF", &input_TIFF_stream);

//perform normal operations on mem_TIFF here reading fields
//...

TIFFClose(mem_TIFF);

这些是非常简单的示例,但您可以看到,通过使用 TIFFStreamOpen,您不必重写这些函数并将它们传递给 TIFFClientOpen。​​

关于C++ LibTiff - 从内存读取和保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4624144/

相关文章:

c++ - 我的 CListCtrl 中的选定项目显示省略号,尽管有足够的空间!

c# - 确定用于操作的足够内存的可用性

javascript - 为什么 Node.js 有 1.76 Gb 的内存限制?

ios - swift ,SpriteKit : Deallocate a Gamescene et reallocate a new one

c - 调试使用 libtiff 的 C 代码

c - 我怎样才能简单地在 libtiff 中加载灰度 tiff 并获得像素强度数组?

c++ - 使用 libtiff 库读写 32 位 tif 图像。 (c++)

c++ - int* 到常量数组

c++ - FLTK GUI 不显示 (Dev-C++)

c++ - cin 上的段错误。尝试了最后的事情。糟糕的 gdb