本文介绍了从SVS文件中提取完整比例的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Libtiff提取SVS文件的第一页.SVS文件对此图像使用JPEG2000压缩.

I am trying to extract the first page of a SVS file using Libtiff.The SVS file uses JPEG2000 compression for this image.

我的计划是:

  1. 从SVS的第一页中提取原始图块,并且
  2. 使用OpenJPEG对其进行解码.

下面是我试图从SVS中提取原始图块的内容.我从152MB的SVS文件中只有一个43KB的输出文件(无法提取原始图块).

Below is what I tried to extract raw tiles from the SVS. I only got a 43KB output file from 152MB svs file (it failed to extract the raw tiles).

我希望有人能让我知道如何从SVS文件中提取图块.

I hope someone could let me know how to extract tiles from a SVS file.

int main()
{
    const char* filename = "input.svs";
    TIFF* tiff_in = TIFFOpen(filename, "r");
    TIFF* tiff_out = TIFFOpen("output.raw", "w");
    if (tiff_in) {
        uint32 imageWidth, imageLength;
        uint32 tileWidth, tileLength;
        uint32 tileCount, tileByteCounts;
        uint32 samplePerPixel, bitsPerSample;
        uint32 orientation, planarConfig, photoMetric;
        uint32 xResolution, yResolution;
        uint32 x, y;
        ttile_t tile;
        // get fileds from the input file
        TIFFGetField(tiff_in, TIFFTAG_IMAGEWIDTH, &imageWidth);
        TIFFGetField(tiff_in, TIFFTAG_IMAGELENGTH, &imageLength);
        TIFFGetField(tiff_in, TIFFTAG_TILEWIDTH, &tileWidth);
        TIFFGetField(tiff_in, TIFFTAG_TILELENGTH, &tileLength);
        TIFFGetField(tiff_in, TIFFTAG_SAMPLESPERPIXEL, &samplePerPixel);
        TIFFGetField(tiff_in, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
        TIFFGetField(tiff_in, TIFFTAG_TILEBYTECOUNTS, &tileByteCounts);
        TIFFGetField(tiff_in, TIFFTAG_ORIENTATION, &orientation);
        TIFFGetField(tiff_in, TIFFTAG_PLANARCONFIG, &planarConfig);
        TIFFGetField(tiff_in, TIFFTAG_PHOTOMETRIC, &photoMetric);
        TIFFGetField(tiff_in, TIFFTAG_XRESOLUTION, &xResolution);
        TIFFGetField(tiff_in, TIFFTAG_YRESOLUTION, &yResolution);

        // set files to the output file
        TIFFSetField(tiff_out, TIFFTAG_IMAGEWIDTH, imageWidth);
        TIFFSetField(tiff_out, TIFFTAG_IMAGELENGTH, imageLength);
        TIFFSetField(tiff_out, TIFFTAG_TILEWIDTH, tileWidth);
        TIFFSetField(tiff_out, TIFFTAG_TILELENGTH, tileLength);
        TIFFSetField(tiff_out, TIFFTAG_SAMPLESPERPIXEL, samplePerPixel);
        TIFFSetField(tiff_out, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
        TIFFSetField(tiff_out, TIFFTAG_PLANARCONFIG, planarConfig);
        TIFFSetField(tiff_out, TIFFTAG_PHOTOMETRIC, photoMetric);
        TIFFSetField(tiff_out, TIFFTAG_XRESOLUTION, xResolution);
        TIFFSetField(tiff_out, TIFFTAG_YRESOLUTION, yResolution);
        //TIFFSetField(tiff_out, TIFFTAG_ROWSPERSTRIP, 1);

        tdata_t buf = _TIFFmalloc(TIFFTileSize(tiff_in));
        for (int tile = 0; tile < TIFFNumberOfTiles(tiff_in); tile++)
        {
            TIFFReadRawTile(tiff_in, tile, buf, (tsize_t)-1);
            TIFFWriteRawTile(tiff_out, tile, buf, (tsize_t)sizeof(buf));
        }
        _TIFFfree(buf);
        TIFFClose(tiff_in);
        TIFFClose(tiff_out);
    }
    return 0;
}

推荐答案

您正在编写每个8字节的图块. sizeof(buf)是指针buf的字节大小.您想使用由TIFFReadRawTile返回的实际读取的字节数:

You are writing tiles with 8 bytes each. sizeof(buf) is the size in bytes of the pointer buf. You want to use the number of bytes actually read, which is returned by TIFFReadRawTile:

tsize_t size = TIFFReadRawTile(tiff_in, tile, buf, (tsize_t)-1);
TIFFWriteRawTile(tiff_out, tile, buf, size);

请注意,这里是将JPEG2000压缩的数据复制到输出文件.您必须使用OpenJPEG解压缩此数据,然后写入未压缩图块大小的数据,在连续的平面配置中为tileWidth * tileLength * samplePerPixel * bitsPerSample / 8,在单独的平面配置中为tileWidth * tileLength * bitsPerSample / 8.

Note that here you are copying the JPEG2000-compressed data to the output file. You'll have to decompress this data using OpenJPEG, then write data of the size of the uncompressed tile, which is tileWidth * tileLength * samplePerPixel * bitsPerSample / 8 in the contiguous planar configuration, or tileWidth * tileLength * bitsPerSample / 8 in the separate planar configuration.

使用TIFFGetFieldTIFFSetField时,需要检查返回值.如果发生错误,则返回0.您不能依赖未正确读取的值.

When using TIFFGetField and TIFFSetField, you need to check the return values. A 0 is returned if an error occurred. You cannot depend on values that were not read properly.

其他错误和警告通常会打印到stderr.如果您在Windows下进行开发,则通常不会显示这些内容.我建议您查看 TIFFSetErrorHandler TIFFSetWarningHandler ,在此处设置合适的消息显示功能将在开发过程中极大地帮助您.

Other errors and warnings are typically printed to stderr. If you are developing under Windows, these will typically not be shown. I recommend that you look into TIFFSetErrorHandler and TIFFSetWarningHandler, setting suitable message display functions here will help you immensely during development.

这篇关于从SVS文件中提取完整比例的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:14