C LibTiff – 从和内存中读取并保存文件

前端之家收集整理的这篇文章主要介绍了C LibTiff – 从和内存中读取并保存文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在LibTiff中有没有办法如何从内存中读取文件并将其保存到内存?

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

非常感谢!

解决方法

我知道这是一个古老的问题,但是我将为需要这些信息的人们提供一个更简单,更新的最新的答案,这些信息需要更多最新版本的libtiff.在最新版本的libtiff(4.0.2)中,甚至过去几个版本我相信(检查您的具体版本号),还有一个名为tiffio.hxx的包含文件.它有两个外部函数用于读/写内存中的流:
  1. extern TIFF* TIFFStreamOpen(const char*,std::ostream *);
  2. extern TIFF* TIFFStreamOpen(const char*,std::istream *);

您可以只包含这个文件并读取或写入内存.

写作范例:

  1. #include <tiffio.h>
  2. #include <tiffio.hxx>
  3. #include <sstream>
  4.  
  5. std::ostringstream output_TIFF_stream;
  6.  
  7. //Note: because this is an in memory TIFF,just use whatever you want for the name - we
  8. //aren't using it to read from a file
  9. TIFF* mem_TIFF = TIFFStreamOpen("MemTIFF",&output_TIFF_stream);
  10.  
  11. //perform normal operations on mem_TIFF here like setting fields
  12. //...
  13.  
  14. //Write image data to the TIFF
  15. //..
  16.  
  17. TIFFClose(created_TIFF);
  18.  
  19. //Now output_TIFF_stream has all of my image data. I can do whatever I need to with it.

阅读非常相似:

  1. #include <tiffio.h>
  2. #include <tiffio.hxx>
  3. #include <sstream>
  4.  
  5. std::istringstream input_TIFF_stream;
  6. //Populate input_TIFF_stream with TIFF image data
  7. //...
  8.  
  9. TIFF* mem_TIFF = TIFFStreamOpen("MemTIFF",&input_TIFF_stream);
  10.  
  11. //perform normal operations on mem_TIFF here reading fields
  12. //...
  13.  
  14. TIFFClose(created_TIFF);

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

猜你在找的C&C++相关文章