Building LibTIFF with LibJPEG on Windows

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 buildx86
cd buildx86
cmake ..

libtiff default config

Build the library

cmake --build . --config Release
cd 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 executable
add_executable(demo demo.cxx)
target_link_libraries (demo "tiff")

add_custom_command(TARGET demo POST_BUILD 
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${PROJECT_SOURCE_DIR}/lib/tiff.dll"              
        $<TARGET_FILE_DIR:demo>)

Build and run the app.

mkdir build
cd build
cmake ..
cmake --build .

libtiff jpeg compression

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/ ..

libtiff libjpeg config

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.

libtiff libjpeg

References

Source Code

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

Search Blog Posts