简体   繁体   中英

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

I am trying to read in a scanned image and compress it from a DIB in memory into a TIF file. I am using the libtiff library and have found a couple examples online but none of them really do as I need them to. I need to take the image from the DIB and turn it into a B&W image.

Here is the code I have modified from an online example. It does turn it black and white but it also only shows one section of the scan rather than the whole thing. Any help would be appreciated.

EDIT*: I've noticed this happens if I scan it in as a gray image, if I scan it in as black and white then the image that is returned is entirely black, I don't know if this helps at all.

// 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);
}

I may be wrong, but I think that for CCITTFAX4 encoding libtiff expects eight pixels per byte, padded to the end of the byte if the image width is not divisible by 8.

If the width of the image is 150, for example, the scanline should be 19 bytes, not 150.

There is a good example of using libtiff here , though it does not include cutting the color information off at a threshold and packing the pixels 8 per byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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