TIFF(Tag Image File Format)图像文件是图形图像处理中常用的格式之一,其图像格式很复杂,但由于它对图像信息的存放灵活多变,可以支持很多色彩系统,而且独立于操作系统,因此得到了广泛应用。在各种地理信息系统、摄影测量与遥感等应用中,要求图像具有地理编码信息,例如图像所在的坐标系、比例尺、图像上点的坐标、经纬度、长度单位及角度单位等等。

该库的发行版中包括一个用于读写 TIFF 的库 libtiff,一小部分用于在 UNIX 系统上对 TIFF 映像进行简单操作的工具以及该库和工具上的文档。 还包括由他人提供的一小部分与 TIFF 相关的 UNIX 软件。

该库以及相关的工具程序应可以满足您在32位和64位计算机上读写TIFF图像的大部分需求。 该软件也可以在较旧的16位系统上使用,尽管可能需要一些努力,并且您可能需要省略一些压缩支持。

本篇文章主要是介绍在 Windows 平台下,如何编译 libpng 的静态库。在开始之前,确认你的机器已经安装了 CMake 和 Visual Studio,如果你不知道怎样安装,可以查看 CMake 安装教程Visual Studio 安装教程

首先去官网,下载最新的 libtiff 源码包,最新的版本为 4.1.0。

下载列表

下载完成后,解压,找到 CMakeLists.txt 文件,用已经安装好的 CMake 软件打开,或者直接将该文件拖拽到 CMake 的 UI 界面上。在 Where to build the binaries 目录后面增加 _build 目录。这样可以确保生成的文件在 _build 目录中,而不会污染原有的 libtiff 目录。

cmake gui

点击 Configure 按钮,生成配置信息,如果弹出新建 _build 目录确认的对话框,选择 Yes。

提示框

选择你想要编译的平台版本,这里使用 vs2019 编译 x64 位的静态库。

平台选择

点击 Finish,CMake 会自动识别当前系统的配置信息,并生成相关的配置文件。

Building tiff version 4.1.0
libtiff library version 5.5.0
Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
The C compiler identification is MSVC 19.24.28315.0
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Performing Test C_FLAG_W3
Performing Test C_FLAG_W3 - Success
Performing Test HAVE_LD_VERSION_SCRIPT
Performing Test HAVE_LD_VERSION_SCRIPT - Failed
Looking for assert.h
Looking for assert.h - found
Looking for dlfcn.h
Looking for dlfcn.h - not found
Looking for fcntl.h
Looking for fcntl.h - found
Looking for inttypes.h
Looking for inttypes.h - found
Looking for io.h
Looking for io.h - found
Looking for search.h
Looking for search.h - found
Looking for stdint.h
Looking for stdint.h - found
Looking for string.h
Looking for string.h - found
Looking for strings.h
Looking for strings.h - not found
Looking for sys/time.h
Looking for sys/time.h - not found
Looking for sys/types.h
Looking for sys/types.h - found
Looking for unistd.h
Looking for unistd.h - not found
Performing Test C_HAS_inline
Performing Test C_HAS_inline - Success
Performing Test TIME_WITH_SYS_TIME
Performing Test TIME_WITH_SYS_TIME - Failed
Performing Test TM_IN_SYS_TIME
Performing Test TM_IN_SYS_TIME - Success
Looking for stddef.h
Looking for stddef.h - found
Check size of signed int
Check size of signed int - done
Check size of unsigned int
Check size of unsigned int - done
Check size of signed long
Check size of signed long - done
Check size of unsigned long
Check size of unsigned long - done
Check size of signed long long
Check size of signed long long - done
Check size of unsigned long long
Check size of unsigned long long - done
Check size of unsigned char *
Check size of unsigned char * - done
Check size of size_t
Check size of size_t - done
Check size of ptrdiff_t
Check size of ptrdiff_t - done
Looking for mmap
Looking for mmap - not found
Looking for setmode
Looking for setmode - not found
Looking for snprintf
Looking for snprintf - found
Looking for strcasecmp
Looking for strcasecmp - not found
Looking for strtol
Looking for strtol - found
Looking for strtoll
Looking for strtoll - found
Looking for strtoul
Looking for strtoul - found
Looking for strtoull
Looking for strtoull - found
Looking for getopt
Looking for getopt - not found
Looking for lfind
Looking for lfind - found
Check if the system is big endian
Searching 16 bit integer
Check size of unsigned short
Check size of unsigned short - done
Using unsigned short
Check if the system is big endian - little endian
CMAKE_HOST_SYSTEM_PROCESSOR set to AMD64
HOST_FILLORDER set to FILLORDER_LSB2MSB
HOST_BIG_ENDIAN set to 0
HAVE_IEEEFP set to 1
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
Could NOT find JPEG (missing: JPEG_LIBRARY JPEG_INCLUDE_DIR) 
Looking for jbg_newlen
Looking for jbg_newlen - not found
Could NOT find LibLZMA (missing: LIBLZMA_LIBRARY LIBLZMA_INCLUDE_DIR LIBLZMA_HAS_AUTO_DECODER LIBLZMA_HAS_EASY_ENCODER LIBLZMA_HAS_LZMA_PRESET) 
The CXX compiler identification is MSVC 19.24.28315.0
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Found OpenGL: opengl32   
Could NOT find GLUT (missing: GLUT_glut_LIBRARY GLUT_INCLUDE_DIR) 
Looking for GL/gl.h
Looking for GL/gl.h - not found
Looking for GL/glu.h
Looking for GL/glu.h - not found
Looking for GL/glut.h
Looking for GL/glut.h - not found
Looking for GLUT/glut.h
Looking for GLUT/glut.h - not found
Looking for OpenGL/gl.h
Looking for OpenGL/gl.h - not found
Looking for OpenGL/glu.h
Looking for OpenGL/glu.h - not found

Libtiff is now configured for 

  Installation directory:             C:/Program Files (x86)/tiff
  Documentation directory:            C:/Program Files (x86)/tiff/share/doc/tiff
  C compiler:                         C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
  C++ compiler:                       C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
  Build shared libraries:             ON
  Enable linker symbol versioning:    FALSE
  Support Microsoft Document Imaging: ON
  Use win32 IO:                       TRUE

 Support for internal codecs:
  CCITT Group 3 & 4 algorithms:       ON
  Macintosh PackBits algorithm:       ON
  LZW algorithm:                      ON
  ThunderScan 4-bit RLE algorithm:    ON
  NeXT 2-bit RLE algorithm:           ON
  LogLuv high dynamic range encoding: ON

 Support for external codecs:
  ZLIB support:                       ON (requested) FALSE (availability)
  Pixar log-format algorithm:         ON (requested) FALSE (availability)
  JPEG support:                       ON (requested) FALSE (availability)
  Old JPEG support:                   ON (requested) FALSE (availability)
  JPEG 8/12 bit dual mode:            ON (requested) FALSE (availability)
  ISO JBIG support:                   ON (requested) FALSE (availability)
  LZMA2 support:                      ON (requested) FALSE (availability)
  ZSTD support:                       ON (requested)  (availability)
  WEBP support:                       ON (requested) FALSE (availability)

  C++ support:                        ON (requested) TRUE (availability)

  OpenGL support:                     FALSE

Configuring done

TIFF 库包含很多可选项,大多数配置都不是必须的,这里我们可以添加 zlibjpegwebp 的支持。因为之前的文章中我们编译过它们的静态文件,其余不支持的功能,需要一一剔除,否则编译可能会报错。

剔除 jbig、jpeg12、lzma、zstd 的支持:

cmake 选项

然后点击 Advanced 复选框,配置相关的目录信息。

首先配置 JPEG 相关信息:

JPEG 相关配置

接着,WEBP 和 ZLIB 相关信息:

webp 和 zlib 配置

再次点击 Configure 按钮,配置 OK 后,点击 Generate 按钮生成解决方案。完毕后,点击 OpenProject 打开工程。

libtiff 相关的项目非常多,这里主要关注的是 tiff 工程,该工程是用来编译 libtiff 静态库的源码工程。按照 Debug/Release 和 /MT 以及 /MD 之分,至少可以组合四种配置,所以我们分别选择不同的配置属性,生成最后的四种库文件,并将它们放到不同的目录下。具体的配置方法可以查看 运行时库详解 这篇文章,每次修改配置都编译一次工程,编译成功后,将生成的结果放到一开始设定的目录下,最后的目录结构如下:

---
 |--- include //放头文件
 |--- lib
       |--- Debug
       |      |--- MDd
       |      |     |-- tiffd.lib
       |      |
       |      |--- MTd
       |            |-- tiffd.lib
       |
       |--- Release
              |--- MD
              |     |-- tiff.lib
              |
              |--- MT
                    |-- tiff.lib

为了方便这里我打包了最后的结果,方便大家直接使用,不过这里只有 x64 版本,如果你的机器还是 32 位系统则无法使用。libtiff 静态库下载

这里有一点注意,在 include 头文件的目录里面需要加入 CMake 生成的 tif_config.h、tiffconf.h 文件,这两个文件默认生成在 _build/libtiff 里面,如果忘记这两个头文件,在使用 libtiff 库的时候,可能会无法找到 tif_config.h、tiffconf.h 而导致编译无法通过。