Building LibTIFF with LibJPEG on Windows

Xiao Ling
3 min readApr 23, 2018

TIFF (Tagged Image File Format) is a file format for storing raster graphics images. TIFF supports many different compression schemes. This post demonstrates how to build libtiff to make it work for TIFF files that hold JPEG compressed images.

Prerequisites

Building LibTIFF with CMake

Get and unzip the latest libtiff source code.

LibTIFF

Generate project configuration files with CMake.

mkdir buildx86cd buildx86cmake ..

Build the library

cmake --build . --config Releasecd buildx86\libtiff\Release

By default, libtiff does not support JPEG compression.We can write a simple program to see what will happen when reading TIFF files that support different compression scheme.

Reading TIFF Files in C

Create demo.cxx.

#include <stdio.h>#include "tiffio.h"int main(int argc, const char* argv[]){if (argc < 2) {printf("Usage: demo [TIFF file]\n");return 0;}const char* pszImageFile = argv[1];TIFF* tif = TIFFOpen(pszImageFile, "r");if (tif) {uint32 imageWidth, imageLength;uint16 compression;TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);printf("imageWidth %d, imageLength %d \n\n", imageWidth, imageLength);switch(compression) {case COMPRESSION_LZW:printf("COMPRESSION_LZW \n\n");break;case COMPRESSION_OJPEG:printf("COMPRESSION_OJPEG \n\n");break;case COMPRESSION_JPEG:printf("COMPRESSION_JPEG \n\n");break;}TIFFClose(tif);}return 0;}

This console app will display image width, image length, and compression scheme.

Create CMakeLists.txt. Set the directories of library and header files.

cmake_minimum_required (VERSION 2.6)project (demo)MESSAGE( STATUS "PROJECT_NAME: " ${PROJECT_NAME} )link_directories("${PROJECT_SOURCE_DIR}/lib")include_directories("${PROJECT_SOURCE_DIR}/include/")# Add the executableadd_executable(demo demo.cxx)target_link_libraries (demo "tiff")add_custom_command(TARGET demo POST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_if_different"${PROJECT_SOURCE_DIR}/lib/tiff.dll"$<TARGET_FILE_DIR:demo>)

Build and run the app.

mkdir buildcd buildcmake ..cmake --build .

I got warnings for JPEG and old-style JPEG compression support. To fix it, turn on the option of JPEG support and link JPEG library when building the TIFF library.

Build LibTIFF with LibJPEG

To support JPEG, make a few changes to the configuration command.

cmake -DJPEG_LIBRARY:PATH=G:/libtiff/tiff-4.0.9/libjpeg/lib/jpeg.lib -DJPEG_INCLUDE_DIR:PATH=G:/libtiff/tiff-4.0.9/libjpeg/include/ ..

After running the command, the availability of JPEG support and Old JPEG support changed to True.

Rebuild the library and rebuild the simple program. There is no warning message now.

References

Source Code

https://github.com/yushulx/libtiff-libjpeg

Originally published at www.codepool.biz on April 23, 2018.

--

--

Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover