zoukankan      html  css  js  c++  java
  • libtiff库使用

    此文章为了记录我在使用libtiff库中的一些问题而写,将不断补充。

    libtiff库是读取和写入tiff文件最主要的一个开源库,但文档写的实在不敢恭维。相对资料也是异常稀少。

    libtiff库的安装

    libtiff库的最新的最新版本可以从http://www.libtiff.org/下载,即可以编译源码也可以采用预先编译好的二进制文件。

    本人推荐使用预编译版本,自己编译容易缺少依赖库,同时也可能出现一些莫名其妙的问题。

    tiff文件的读写函数:

    1 //获取strip大小
    2 tsize_t TIFFStripSize(TIFF *tif);
    3 //读取strip数据,buf缓冲区可由TIFFStripSize计算,size取-1代表读取整个strip
    7 tsize_t TIFFReadEncodedStrip(TIFF *tif, tstrip_t strip, tdata_t buf, tsize_t size);

    将多色tiff文件分割

     1     uint32 imageWidth, imageLength, TileWidth, TileLength, imageRowsPerStrip ;
     2     uint16 imageCompression, imageSamplePerPixel ;
     3     uint16 imagePlanarConfig, imagePhotoMetric, ResolutUnit, Orientation ;
     4     uint16 bps ;
     5     float X_Resolut, Y_Resolut ;
     6 
     7     TIFF *tif_r, *tif_w ;  
     8     unsigned char *buf;
     9     tstrip_t strip ;
    10 
    11     tif_r = TIFFOpen("image_4plane.tif", "r");
    12     if (!tif_r)
    13     {
    14         error_handler("Open Tiff File Error!");
    15         return -1;
    16     }
    17     /* 讀取 TIFF 標籤 */
    18     TIFFGetField(tif_r, TIFFTAG_IMAGEWIDTH, &imageWidth);
    19     TIFFGetField(tif_r, TIFFTAG_IMAGELENGTH, &imageLength);
    20 
    21     TIFFGetField(tif_r, TIFFTAG_BITSPERSAMPLE, &bps);
    22     TIFFGetField(tif_r, TIFFTAG_COMPRESSION, &imageCompression);
    23     TIFFGetField(tif_r, TIFFTAG_PHOTOMETRIC, &imagePhotoMetric);
    24 
    25     TIFFGetField(tif_r, TIFFTAG_SAMPLESPERPIXEL, &imageSamplePerPixel);
    26     TIFFGetField(tif_r, TIFFTAG_ROWSPERSTRIP, &imageRowsPerStrip);
    27     if (imageRowsPerStrip != 1)
    28     {
    29         error_handler("Rows Each Strip Is Not 1!");
    30         return -1;
    31     }
    32 
    33     TIFFGetField(tif_r, TIFFTAG_XRESOLUTION, &X_Resolut);
    34     TIFFGetField(tif_r, TIFFTAG_YRESOLUTION, &Y_Resolut);
    35     TIFFGetField(tif_r, TIFFTAG_RESOLUTIONUNIT, &ResolutUnit);
    36 
    37     TIFFGetField(tif_r, TIFFTAG_PLANARCONFIG, &imagePlanarConfig);
    38     TIFFGetField(tif_r, TIFFTAG_ORIENTATION, &Orientation);
    39 
    40     int stripsize = TIFFStripSize(tif_r);
    41     buf = (unsigned char *) malloc(stripsize) ;
    42     if (!buf)
    43     {
    44         error_handler("Allocate Buffer Failed!");
    45     }
    46     Mat ht_img(Size(imageWidth, imageLength),CV_8UC1,Scalar::all(0));
    47     const int color[7] = {0,1,2,3,4,5,6};
    48     unsigned char * pRow; 
    49             
    50             
    51     for (strip = 0; strip < TIFFNumberOfStrips(tif_r); strip++)
    52     {
    53         TIFFReadEncodedStrip(tif_r, strip, buf, (tsize_t) -1);
    54         pRow = ht_img.ptr(strip);
    55         for (int i_pixel = 0; i_pixel < imageWidth; i_pixel++)
    56         {
    57             pRow[i_pixel] = buf[i_pixel*imageSamplePerPixel + color[6]];
    58         }
    59     }
    60     imwrite("strip_out.tiff", ht_img);
    61     
    62     free(buf);
    63     TIFFClose(tif_r);
    64     printf("Done!
    ");

     

    参考文献:

    1. libtiff库的使用

    http://darkranger.no-ip.org/archives/v5/document/develop/libtiff_tutorial.htm

    2. 关于如何判断一个tiff文件是tile或者是strip的说明

    http://www.asmail.be/msg0054551721.html

  • 相关阅读:
    [学习笔记&教程] 信号, 集合, 多项式, 以及各种卷积性变换 (FFT,NTT,FWT,FMT)
    [学习笔记] CDQ分治&整体二分
    [日常] NOIp 2018 滚粗记
    [学习笔记] 模拟退火 (Simulated Annealing)
    [日常] NOIWC 2018爆零记
    [日常] PKUWC 2018爆零记
    [日常] 最近的一些破事w...
    [BZOJ 1877][SDOI2009]晨跑
    [COGS 2583]南极科考旅行
    [日常] NOIP 2017滚粗记
  • 原文地址:https://www.cnblogs.com/hujianhua/p/3261013.html
Copyright © 2011-2022 走看看