首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用libtiff获取DIB并将其转换为tif

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

Stack Overflow用户
提问于 2009-06-15 18:35:19
回答 1查看 1.2K关注 0票数 1

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

以下是我从一个在线示例中修改的代码。它确实把它变成了黑白的,但它也只显示了扫描的一部分,而不是整个扫描。任何帮助都将不胜感激。

编辑*:我注意到,如果我把它作为灰色图像扫描,如果我把它作为黑白扫描,那么返回的图像就完全是黑色的,我不知道这是否有帮助。

代码语言:javascript
复制
// 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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-06-15 19:05:07

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

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

使用libtiff here有一个很好的例子,尽管它不包括在阈值上剪切颜色信息并将像素打包为每字节8个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/997620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档