C ++ LibTiff-从内存读取和保存文件 [英] C++ LibTiff - Read and Save file from and to Memory
问题描述
在LibTiff中有一种方法可以从内存中读取文件并将其保存到内存中吗?
Is there a way in LibTiff how I can read a file from Memory and save it to Memory?
在使用其他库打开图像之前,我不想先将图像保存到光盘上.
I don't want to save the image to the disc first, before opening it with an other library...
非常感谢!
推荐答案
我知道这是一个老问题,但是对于像我这样需要这些信息的人,我将发布一个更简单,最新的答案. libtiff的最新版本.在libtiff的最新版本(4.0.2)中,甚至我相信过去的几个版本(请检查您的特定版本号)中,都有一个名为tiffio.hxx的包含文件.它具有两个用于读取/写入内存中流的外部函数:
I know this is an old question, but I am going to post an easier, more up-to-date answer for those like myself who need this information for more recent versions of libtiff. In the newest version of libtiff (4.0.2), and even the past few versions I believe (check for your specific version number), there is an include file called tiffio.hxx. It has two extern functions for reading/writing to streams in memory:
extern TIFF* TIFFStreamOpen(const char*, std::ostream *);
extern TIFF* TIFFStreamOpen(const char*, std::istream *);
您可以仅包含此文件并读取或写入内存.
You can just include this file and read or write to memory.
写作示例:
#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.
These are very simple examples, but you can see that by using TIFFStreamOpen you don't have to override those functions and pass them to TIFFClientOpen.
这篇关于C ++ LibTiff-从内存读取和保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!