Code:
int prtTiff(char *achFile, HDC dc){
DIBSECTION *hDib=0;
int res(0);
TIFF *tif;
HBITMAP hBmp;
BITMAPINFO *bmi;
//////////
UINT ilc = (GetDeviceCaps(dc,NUMCOLORS) & 0xFE);
if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR){
VOID *bits;
if(GetDeviceCaps(dc,BITSPIXEL) <= ILC_COLOR8){
LPPALETTEENTRY pal;
ULONG i, colors;
BYTE temp;
colors = 1 << GetDeviceCaps(dc,BITSPIXEL);
bmi = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER)+sizeof(PALETTEENTRY)*colors);
pal = (LPPALETTEENTRY)bmi->bmiColors;
GetPaletteEntries((HPALETTE)GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
/* Swap colors returned by GetPaletteEntries so we can use them for
* CreateDIBSection call. */
for (i = 0; i < colors; i++){
temp = pal[i].peBlue;
bmi->bmiColors[i].rgbRed = pal[i].peRed;
bmi->bmiColors[i].rgbBlue = temp;
}
}else{
bmi = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
}
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = GetDeviceCaps(dc,HORZRES);
bmi->bmiHeader.biHeight = GetDeviceCaps(dc,VERTRES);
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biBitCount = GetDeviceCaps(dc,BITSPIXEL);
bmi->bmiHeader.biCompression = BI_RGB;
bmi->bmiHeader.biSizeImage = 0;
bmi->bmiHeader.biXPelsPerMeter = 0;
bmi->bmiHeader.biYPelsPerMeter = 0;
bmi->bmiHeader.biClrUsed = 0;
bmi->bmiHeader.biClrImportant = 0;
hBmp = CreateDIBSection(dc, bmi, DIB_RGB_COLORS, (void**)&hDib, 0, 0);
free(bmi);
}
//////////
// get the image information from the provided DIB
UINT32 w = hDib->dsBm.bmWidth;
UINT32 h = hDib->dsBm.bmHeight;
UINT32 total_width = hDib->dsBmih.biWidth;
UINT32 bitcount = (UINT32)hDib->dsBm.bmBits;
UINT32 bytecount = bitcount / 8;
if((w > 0) && (h > 0)){
// open the output TIFF image for writing
if((tif = TIFFOpen(achFile, "w")) == NULL)
return res;
}
// set up the image tags
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
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 *)hDib->dsBm.bmBits;
unsigned char * pdst = new unsigned char[(w * 3)];
UINT32 src_index;
UINT32 dst_index;
// now go line by line to write out the image data
for(UINT32 row = 0; row < h; row++ ){
// initialize the scan line to zero
memset(pdst,0,(size_t)(w * 3));
// moving the data from the dib to a row structure that
// can be used by the tiff library
for(UINT32 col = 0; col < w; col++){
src_index=(h-row-1)*total_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];
res++;
}
// now actually write the row data
TIFFWriteScanline(tif, pdst, row, 0);
}
TIFFClose(tif);
return res;
}