加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

将内存图像数据保存为png及tif方法

(2014-05-27 10:56:00)
标签:

bmp

png

tif

将内存图像数据保存为png及tif方法

 

需要将内存中的图像数据保存为tif或者png的格式,就需要用到libpng,libtiff及zlib这三个 库.

 

工程中需要有png.h , tiff.h, tiffio.h

这几个库都是可以用在windows及linux下的,我在这两个系统下都用的下面的方法,无非就是这两种系统下的c++中字符串及对象类型的处理

具体的方法如下:

保存TIF

  1. BOOL ImgToTIF(LPCTSTR strFileName, PVOID pImageData, ULONG _ulWidth, ULONG _ulHeight, ULONG nBit)  
  2.  
  3.     UINT nLen _tcsclen(strFileName);  
  4.     TCHAR strFile[255];  
  5.     TCHAR ext3[4],  ext4[5];  
  6.     _tcsnccpy(ext3,strFileName+nLen-4,4);  
  7.     _tcsnccpy(ext4,strFileName+nLen-5,5);  
  8.   
  9.     LPCTSTR tPFileName strFileName;  
  10.   
  11.     if(_tcsnccmp(ext3,_T(".tif"),4)!=0 && _tcsnccmp(ext3,_T(".TIF"),4)!=0  
  12.         &&_tcsnccmp(ext4,_T(".TIFF"),5)!=0 && _tcsnccmp(ext4,_T(".tiff"),5)!=0)  
  13.      
  14.         _tcscpy(strFile,strFileName);  
  15.         _tcscpy(strFile+nLen,_T(".tif"));  
  16.         tPFileName strFile;  
  17.      
  18.   
  19.     BOOL result FALSE;  
  20.   
  21.     BOOL b16Bit nBit/16==0 TRUE:FALSE;  
  22.   
  23.     int nChannels b16Bit nBit/16:nBit/8;  
  24.   
  25.     int depth b16Bit?16:8;      
  26.   
  27.     ifnBit != && nBit%8 !=  
  28.         return FALSE;  
  29.   
  30.     PUCHAR imgData (PUCHAR)pImageData;  
  31.   
  32.     TIFF* tif NULL;  
  33. #ifdef UNICODE    
  34.     tif TIFFOpenW( tPFileName, "w" );   
  35. #else  
  36.     tif TIFFOpen( tPFileName, "w" );  
  37. #endif  
  38.     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, _ulWidth);  // set the width of the image  
  39.     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, _ulHeight);    // set the height of the image  
  40.     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, nChannels);   // set number of channels per pixel  
  41.     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, depth);    // set the size of the channels  
  42.     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);    // set the origin of the image.  
  43.     //   Some other essential fields to set that you do not have to understand for now.  
  44.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);  
  45.     uint16 colorMode nChannels==1 PHOTOMETRIC_MINISBLACK:PHOTOMETRIC_RGB;  
  46.     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, colorMode);  
  47.     uint16 compression COMPRESSION_NONE;//  
  48.     TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);  
  49.   
  50.     tsize_t linebytes nChannels _ulWidth depth 8;     // length in memory of one row of pixel in the image.  
  51.   
  52.     unsigned char *buf NULL;        // buffer used to store the row of pixel information for writing to file  
  53.   
  54.     //    Allocating memory to store the pixels of current row  
  55.     size_t TIFFScanlineSize(tif);  
  56.     if (TIFFScanlineSize(tif)==linebytes)  
  57.         buf =(unsigned char *)_TIFFmalloc(linebytes);  
  58.     else  
  59.         buf (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));  
  60.   
  61.     // We set the strip size of the file to be size of one row of pixels  
  62.     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, nChannels _ulWidth));  
  63.   
  64.     //Now writing image to the file one strip at time  
  65.     for (uint32 row 0; row _ulHeight; row++)  
  66.      
  67.         memcpy(buf, &imgData[(_ulHeight-row-1)*linebytes], linebytes);    // check the index here, and figure out why not using h*linebytes  
  68.         if (TIFFWriteScanline(tif, buf, row, 0) 0)  
  69.             break 
  70.      
  71.   
  72.     TIFFClose(tif);   
  73.     if (buf)  
  74.         _TIFFfree(buf);  
  75.       
  76.     return TRUE;  
  77.  
 


保存PNG:

  1. BOOL ImgToPNG(LPCTSTR strFileName, PVOID pImageData, ULONG _ulWidth, ULONG _ulHeight, ULONG nBit)  
  2.  
  3.     UINT nLen _tcsclen(strFileName);  
  4.     TCHAR strFile[255];  
  5.     TCHAR ext3[4],  ext4[5];  
  6.     _tcsnccpy(ext3,strFileName+nLen-4,4);  
  7.     _tcsnccpy(ext4,strFileName+nLen-5,5);  
  8.   
  9.     LPCTSTR tPFileName strFileName;  
  10.   
  11.     if(_tcsnccmp(ext3,_T(".PNG"),4)!=0 && _tcsnccmp(ext3,_T(".png"),4)!=0)  
  12.      
  13.         _tcscpy(strFile,strFileName);  
  14.         _tcscpy(strFile+nLen,_T(".png"));  
  15.         tPFileName strFile;  
  16.      
  17.   
  18.     char filename[255];  
  19. #ifdef UNICODE    
  20.     WideCharToMultiByte(CP_ACP, NULL,tPFileName,-1,filename, 255, NULL, NULL);    
  21. #else  
  22.     _tcscpy(filename,tPFileName);  
  23. #endif  
  24.   
  25.     png_structp png_ptr png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, );  
  26.     png_infop info_ptr 0;  
  27.     FILE0;  
  28.     uchar** buffer 0;  
  29.     PUCHAR imgData (PUCHAR)pImageData;  
  30.     int y;  
  31.     BOOL result FALSE;  
  32.   
  33.     BOOL b16Bit nBit/16==0 TRUE:FALSE;  
  34.   
  35.     int nChannels b16Bit nBit/16:nBit/8;  
  36.   
  37.     int depth b16Bit?16:8;      
  38.   
  39.     int  step _ulWidth*nChannels*depth/8;    
  40.   
  41.     ifnBit != && nBit%8 !=  
  42.         return FALSE;  
  43.   
  44.     ifpng_ptr  
  45.      
  46.         info_ptr png_create_info_struct( png_ptr );  
  47.   
  48.         ifinfo_ptr  
  49.          
  50.             ifsetjmp( png_ptr->jmpbuf ==  
  51.              
  52.                 fopen( filename, "wb" );  
  53.   
  54.                 if 
  55.                  
  56.                     png_init_io( png_ptr, );  
  57.   
  58.                     png_set_compression_mem_level( png_ptr, MAX_MEM_LEVEL );  
  59.   
  60.                     png_set_IHDR( png_ptr, info_ptr, _ulWidth, _ulHeight, depth,  
  61.                         nChannels == PNG_COLOR_TYPE_GRAY  
  62.                         nChannels == PNG_COLOR_TYPE_RGB PNG_COLOR_TYPE_RGBA,  
  63.                         PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,  
  64.                         PNG_FILTER_TYPE_DEFAULT );  
  65.   
  66.                     png_write_info( png_ptr, info_ptr );  
  67.   
  68.                     png_set_bgr( png_ptr );  
  69.                     if!isBigEndian()  
  70.                         png_set_swap( png_ptr );  
  71.   
  72.                     ///////////////////////////////////////  
  73.                     //png图像内存的上下位置跟具体图像的数据颠倒  
  74.                     buffer new uchar*[_ulHeight];  
  75.                     for0; _ulHeight; y++  
  76.                         buffer[_ulHeight-y-1] (uchar*)(imgData y*step);  
  77.   
  78.                     png_write_image( png_ptr, buffer );  
  79.                     png_write_end( png_ptr, info_ptr );  
  80.   
  81.                     delete[] buffer;  
  82.   
  83.                     result TRUE;  
  84.                  
  85.              
  86.          
  87.      
  88.     png_destroy_write_struct( &png_ptr, &info_ptr );  
  89.   
  90.     if(f) fclose( );  
  91.       
  92.     return result;  
  93.  

 

摘自http://blog.csdn.net/tiantangxingkong/article/details/6715328#

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有