繁体   English   中英

如何获取DIB并使用libtiff将其转换为tif

[英]How do I take a DIB and convert it to a tif using libtiff

我正在尝试读取扫描的图像并将其从内存中的DIB压缩为TIF文件。 我正在使用libtiff库,并在网上找到了几个示例,但是它们都没有按照我的需要做。 我需要从DIB中获取图像并将其转换为黑白图像。

这是我从在线示例中修改的代码。 它的确将其变成黑白,但它也仅显示扫描的一部分而不是整个内容。 任何帮助,将不胜感激。

编辑*:我注意到如果将其扫描为灰色图像会发生这种情况,如果我将其扫描为黑白图像,则返回的图像完全是黑色的,我不知道这是否有帮助。

// set up the image tags 
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);

unsigned char * psrc = (unsigned char *)lpBits;
unsigned char * pdst = new unsigned char[(w)];

UINT32 src_index;
UINT32 dst_index;

// now go line by line to write out the image data
for (unsigned int row = 0; row < h; row++ )
{

    // initialize the scan line to zero
    memset(pdst,0,(size_t)(w));

    // moving the data from the dib to a row structure that
    // can be used by the tiff library
    for (unsigned int col = 0; col < w; col++){
        src_index = (h - row - 1) * total_width * bytecount
                                  + col * bytecount;
        dst_index = col;
        pdst[dst_index++] = psrc[src_index+2];
        pdst[dst_index++] = psrc[src_index+1];
        pdst[dst_index] = psrc[src_index];
        result++;
    }

    // now actually write the row data
    TIFFWriteScanline(tif, pdst, row, 0);
}

我可能是错的,但我认为对于CCITTFAX4编码,libtiff希望每个字节八个像素,如果图像宽度不能被8整除,则填充到字节的末尾。

例如,如果图像的宽度为150,则扫描线应为19个字节,而不是150个字节。

有使用的libtiff的一个很好的例子在这里 ,虽然它不包括切割所述颜色信息关在一个阈值和包装每个字节中的像素8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM