C++ LibTiff -从内存读取文件并将文件保存到内存

h43kikqp  于 2023-04-01  发布在  其他
关注(0)|答案(4)|浏览(339)

在LibTiff中有没有一种方法可以让我从内存中读取文件并将其保存到内存中?
我不想在用其他库打开光盘之前先将图像保存到光盘...
非常感谢!

7jmck4yq

7jmck4yq1#

您应该创建自己的读/写/等函数,并在创建TIFF时将它们传递给TIFFClientOpen(而不是TIFFOpen)函数。
示例:

TIFF* tif = TIFFClientOpen(
    "Memory", "w", (thandle_t)something_you_will_use_later,
    tiff_Read, tiff_Write, tiff_Seek, tiff_Close, tiff_Size,
    tiff_Map, tiff_Unmap);

您还应该实现以下函数(传递给这些函数的st是传递给TIFFClientOpensomething_you_will_use_later

tsize_t tiff_Read(thandle_t st,tdata_t buffer,tsize_t size)
{
    ...
};

tsize_t tiff_Write(thandle_t st,tdata_t buffer,tsize_t size)
{
    ...
};

int tiff_Close(thandle_t)
{
    return 0;
};

toff_t tiff_Seek(thandle_t st,toff_t pos, int whence)
{
    if (pos == 0xFFFFFFFF)
       return 0xFFFFFFFF;
    ...
};

toff_t tiff_Size(thandle_t st)
{
    ...
};

int tiff_Map(thandle_t, tdata_t*, toff_t*)
{
    return 0;
};

void tiff_Unmap(thandle_t, tdata_t, toff_t)
{
    return;
};
o0lyfsai

o0lyfsai2#

我使用的是…:

#define MALLOC(ptr,type,number,action) {\
if (((ptr) = (type*) malloc ((number)*sizeof(type))) == NULL) {\
    (void) fprintf (stderr, "[%s: #%04d] ERROR : malloc of %lu bytes failed !\n", __FILE__, __LINE__, number*sizeof(type));\
    perror ("Operating system message");\
    action;}}

#define REALLOC(ptr,type,number,action) {\
if (((ptr) = (type*) realloc ((ptr), (number)*sizeof(type))) == NULL) {\
    (void) fprintf (stderr, "[%s: #%04d] ERROR : realloc of %lu bytes failed!\n", __FILE__, __LINE__, number*sizeof(type));\
    perror ("Operating system message");\
    action;}}

#define FREE(ptr) { if (ptr != NULL) free (ptr); ptr = NULL; }

extern "C" {

    typedef struct _memtiff {
        unsigned char *data;
        tsize_t size;
        tsize_t incsiz;
        tsize_t flen;
        toff_t fptr;
    } MEMTIFF;

    static MEMTIFF *memTiffOpen(tsize_t incsiz = 10240, tsize_t initsiz = 10240)
    {
        MEMTIFF *memtif;
        MALLOC(memtif, MEMTIFF, 1, exit(-1));
        memtif->incsiz = incsiz;
        if (initsiz == 0) initsiz = incsiz;
        MALLOC(memtif->data, unsigned char, initsiz, exit(-1));
        memtif->size = initsiz;
        memtif->flen = 0;
        memtif->fptr = 0;
        return memtif;
    }
    /*===========================================================================*/

    static tsize_t memTiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        tsize_t n;
        if (((tsize_t) memtif->fptr + size) <= memtif->flen) {
            n = size;
        }
        else {
            n = memtif->flen - memtif->fptr;
        }
        memcpy(buf, memtif->data + memtif->fptr, n);
        memtif->fptr += n;

        return n;
    }
    /*===========================================================================*/

    static tsize_t memTiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        if (((tsize_t) memtif->fptr + size) > memtif->size) {
            memtif->data = (unsigned char *) realloc(memtif->data, memtif->fptr + memtif->incsiz + size);
            memtif->size = memtif->fptr + memtif->incsiz + size;
        }
        memcpy (memtif->data + memtif->fptr, buf, size);
        memtif->fptr += size;
        if (memtif->fptr > memtif->flen) memtif->flen = memtif->fptr;

        return size;
    }
    /*===========================================================================*/

    static toff_t memTiffSeekProc(thandle_t handle, toff_t off, int whence)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        switch (whence) {
            case SEEK_SET: {
                if ((tsize_t) off > memtif->size) {
                    memtif->data = (unsigned char *) realloc(memtif->data, memtif->size + memtif->incsiz + off);
                    memtif->size = memtif->size + memtif->incsiz + off;
                }
                memtif->fptr = off;
                break;
            }
            case SEEK_CUR: {
                if ((tsize_t)(memtif->fptr + off) > memtif->size) {
                    memtif->data = (unsigned char *) realloc(memtif->data, memtif->fptr + memtif->incsiz + off);
                    memtif->size = memtif->fptr + memtif->incsiz + off;
                }
                memtif->fptr += off;
                break;
            }
            case SEEK_END: {
                if ((tsize_t) (memtif->size + off) > memtif->size) {
                    memtif->data = (unsigned char *) realloc(memtif->data, memtif->size + memtif->incsiz + off);
                    memtif->size = memtif->size + memtif->incsiz + off;
                }
                memtif->fptr = memtif->size + off;
                break;
            }
        }
        if (memtif->fptr > memtif->flen) memtif->flen = memtif->fptr;
        return memtif->fptr;
    }
    /*===========================================================================*/

    static int memTiffCloseProc(thandle_t handle)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        memtif->fptr = 0;
        return 0;
    }
    /*===========================================================================*/

    static toff_t memTiffSizeProc(thandle_t handle)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        return memtif->flen;
    }
    /*===========================================================================*/

    static int memTiffMapProc(thandle_t handle, tdata_t* base, toff_t* psize)
    {
        MEMTIFF *memtif = (MEMTIFF *) handle;
        *base = memtif->data;
        *psize = memtif->flen;
        return (1);
    }
    /*===========================================================================*/

    static void memTiffUnmapProc(thandle_t handle, tdata_t base, toff_t size)
    {
        return;
    }
    /*===========================================================================*/

    static void memTiffFree(MEMTIFF *memtif)
    {
        FREE(memtif->data);
        FREE(memtif);
        return;
    }
    /*===========================================================================*/

}

然后:

if ((filepath == "-") || (filepath == "HTTP")) {
    memtif = memTiffOpen();
    tif = TIFFClientOpen("MEMTIFF", "wb", (thandle_t) memtif,
        memTiffReadProc,
        memTiffWriteProc,
        memTiffSeekProc,
        memTiffCloseProc,
        memTiffSizeProc,
        memTiffMapProc,
        memTiffUnmapProc
    );
}
else {
    if ((tif = TIFFOpen (filepath.c_str(), "wb")) == NULL) {
        if (memtif != NULL) memTiffFree(memtif);
        string msg = "TIFFopen of \"" + filepath + "\" failed!";
        throw SipiError(__file__, __LINE__, msg);
    }
}

要使用内存缓冲区:

if (filepath == "-") {
        size_t n = 0;
        while (n < memtif->flen) {
            n += fwrite (&(memtif->data[n]), 1, memtif->flen - n > 10240 ? 10240 : memtif->flen - n, stdout);
        }
        fflush(stdout);
        memTiffFree(memtif);
    }
1rhkuytd

1rhkuytd3#

我遇到了同样的问题:将TIFF保存为临时文件并重新读取它可以工作,而在内存中读取同一文件并尝试保存或创建cv::Mat失败。原因是读取文件通常会通过_tiffReadProc,它消耗8个字节(在我的情况下是“II*\0\b\0\0\0”)并将它们作为有效的头返回,而从内存中读取则会通过_tiffisReadProc,它只读取3个字节并因无效的头而失败。不知道如何使其工作。填充std::istringstream很容易:std::isstringstream input_TIFF_stream(mem);其中“mem”是char数组。

6ovsh4lw

6ovsh4lw4#

我知道这是一个老问题了,但是对于那些像我一样需要了解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。

相关问题