2007.04.11 12:49 "[Tiff] DC to tiff", by
Anybody know how to print a DC to a tiff file? I can't seem to make it
work..
Code:
BITMAPINFOHEADER *pBMh = NULL;
BITMAPINFO *pBM = NULL;
BYTE *bits;
HBITMAP hBmp;
TIFF *tif;
// Allocate bitmap info header
pBMh =
(BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));
if(pBMh == NULL){
return 0;
}
memset(pBMh, 0, sizeof(BITMAPINFOHEADER));
pBMh->biSize = sizeof(BITMAPINFOHEADER);
pBMh->biHeight = GetDeviceCaps(dc, VERTRES);
pBMh->biWidth = ((((int)GetDeviceCaps(dc, HORZRES)*8)+31)&~31)>>3;
pBMh->biBitCount = 24;
pBMh->biPlanes = 1;
pBMh->biCompression = BI_RGB;
// Setup color table for mono DIBs
pBM = (BITMAPINFO*)pBMh;
pBM->bmiColors[1].rgbBlue = 255;
pBM->bmiColors[1].rgbGreen = 255;
pBM->bmiColors[1].rgbRed = 255;
hBmp = CreateDIBSection(dc, (BITMAPINFO*)pBMh, DIB_RGB_COLORS,
(void**)&bits, 0, 0);
if(!hBmp || !bits){
free(pBMh);
return(0);
}
// get the image information from the provided DIB
UINT32 height = pBMh->biHeight;
UINT32 width = pBMh->biWidth;
UINT32 bitcount = (UINT32)pBMh->biBitCount;
UINT32 bytecount = bitcount / 8;
free(pBMh);
if((width > 0) && (height > 0)){
// open the output TIFF image for writing
if((tif = TIFFOpen(achFile, "w")) == NULL)
return 0;
}
// set up the image tags
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
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 *)bits;
unsigned char * pdst = new unsigned char[(width * 3)];
UINT32 src_index=0;
UINT32 dst_index=0;
// now go line by line to write out the image data
for(UINT32 row = 0; row < height; row++ ){
// initialize the scan line to zero
memset(pdst,0,(size_t)(width * 3));
// moving the data from the dib to a row structure that
// can be used by the tiff library
for(UINT32 col = 0; col < (UINT32)width; col++){
src_index=(width-row-1)*width*bytecount+col*bytecount;
dst_index = col*3;
pdst[dst_index++]=psrc[src_index+2];
pdst[dst_index++]=psrc[src_index+1];
pdst[dst_index]=psrc[src_index];
}
// now actually write the row data
TIFFWriteScanline(tif, pdst, row, 0);
}
TIFFClose(tif);
return 1;