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