BOOL ctifimage_48bits::bitmapconvertto48bitstif (CString strimagepath, int nwidth, int nheight, int nbpp, byte* pData) {if (NBPP! = 24) {AfxMessageBox (L "only supports 24-bit true Color maps!) "); return FALSE;} if (PData = = null) {AfxMessageBox (L "Memory segment is empty!) "); return FALSE;} //adjust file path name int nfind = Strimagepath.reversefind ('. '); if (Nfind = =-1) {return FALSE; } cstring strext = Strimagepath.mid (nfind + 1); Strext.makelower (); if (strext! = L "tif" && Strext = L "Tiff") {strImagePath = Strimagepath.left (nfind + 1); strImagePath + = L "TIFF";} //Convert image file path format lpstr ppsza = ' + '; Unicodetoansi (Strimagepath.getbuffer (0), &ppsza); tiff *image; //opens a TIF file if (image = Tiffopen (PpszA , "w")) = = NULL) { afxmessagebox (L) cannot create a TIF file! "); return FALSE;} const int ntifbit = 6; //Allocates memory DWORD dwimgsize = nwidth * nheight * ntifbit; byte* ptifbuf = new Byte[dwimgsize];if (ptifbuf = = NULL) {AfxMessageBox (L "Insufficient memory allocated!"); return FALSE;} memset (ptifbuf, 0, dwimgsize); int NWidthBytes24 = ((nwidth * nbpp +)/+) * 4;int nWidthBytes48 = nwidth * ntifbit; //copy image data byte* PSRC = (pData); byte* pDst = (ptifbuf); int tmpsrc = 0;int TMPDST = 0; for (int j = 0; J < nheight; J + +) {tmpsrc = 0;TMPDST = 0; for (int i = 0; i < nwidth; i++) {// //Rgbrgb,photoshop support, but failed to implement// PDST[TMPDST + 2] = Pdst[tmpdst + 5] = psrc[tmpsrc];// PDST[TMPDST + 1] = pdst[tmpdst + 4] = psrc[tmpsrc + 1];// pdst[tmpdst + 0] = pDst[tmpDst + 3] = psrc[tmpsrc + 2]; //RRGGBBPDST[TMPDST + 4] = pdst[tmpdst + 5] = Psrc[tmpsrc];pD st[tmpdst + 2] = PDST[TMPDST + 3] = psrc[tmpsrc + 1];pD st[tmpdst + 0] = pdst[tmpdst + 1] = psrc[tmpsrc + 2]; tmpsrc + = 3;tmpdst + 6;} &NBSP;PSRC-= nwidthbytes24;pdst + = nWidthBytes48;} //sets the image width. Tiffsetfield (image, Tifftag_imagewidth, nwidth); //sets the image height. Tiffsetfield (image, Tifftag_imagelength, nheight); //sets the size of the memory that a sample occupies. Tiffsetfield (image, tifftag_bitspersample, +); //sets the sample of a pixel.This number. Tiffsetfield (image, Tifftag_samplesperpixel, NTIFBIT/2); //sets the compression method for the image. Tiffsetfield (image, Tifftag_compression, compression_none); //sets the color mode. Tiffsetfield (image, Tifftag_photometric, PHOTOMETRIC_RGB); //settings configuration Tiffsetfield (image, Tifftag_ Planarconfig, Planarconfig_contig) //set planar configuration. Tiffsetfield (image, Tifftag_planarconfig, Planarconfig_ CONTIG); int32 itiffcount = Tiffwriteencodedstrip (image, 0, ptifbuf, nwidth * nheight * ntifbit); Tiffclose (image); delete ptifbuf;ptifbuf = null; //If the return value is greater than 0, indicates the size of the data that has been compressed return (Itiffcount > 0);}
Libtiff generate 48-bit color TIF pictures