From 2fc79b2b9331c89c418c7d00755991f76739a138 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Thu, 21 Nov 2024 20:52:02 +0000 Subject: [PATCH] ENH: Add custom CMakeLists.txt to build libjpeg-turbo --- .../JPEG/src/itkjpeg-turbo/CMakeLists.txt | 139 ++++++++++++++++++ .../JPEG/src/itkjpeg-turbo/jconfig.h.in | 8 +- .../JPEG/src/itkjpeg-turbo/jconfigint.h.in | 12 +- .../JPEG/src/itkjpeg-turbo/jmorecfg.h | 12 ++ 4 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 Modules/ThirdParty/JPEG/src/itkjpeg-turbo/CMakeLists.txt diff --git a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/CMakeLists.txt b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/CMakeLists.txt new file mode 100644 index 00000000000..0f316e4bda0 --- /dev/null +++ b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/CMakeLists.txt @@ -0,0 +1,139 @@ +set(JPEG16_SOURCES jcapistd.c jccolor.c jcdiffct.c jclossls.c jcmainct.c + jcprepct.c jcsample.c jdapistd.c jdcolor.c jddiffct.c jdlossls.c jdmainct.c + jdpostct.c jdsample.c jutils.c) +set(JPEG12_SOURCES ${JPEG16_SOURCES} jccoefct.c jcdctmgr.c jdcoefct.c + jddctmgr.c jdmerge.c jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c + jidctred.c jquant1.c jquant2.c) +set(JPEG_SOURCES ${JPEG12_SOURCES} jcapimin.c jchuff.c jcicc.c jcinit.c + jclhuff.c jcmarker.c jcmaster.c jcomapi.c jcparam.c jcphuff.c jctrans.c + jdapimin.c jdatadst.c jdatasrc.c jdhuff.c jdicc.c jdinput.c jdlhuff.c + jdmarker.c jdmaster.c jdphuff.c jdtrans.c jerror.c jfdctflt.c jmemmgr.c + jmemnobs.c jpeg_nbits.c) + +set(JPEG_SOURCES ${JPEG_SOURCES} jaricom.c jcarith.c jdarith.c) + + +include(CheckSymbolExists) +check_symbol_exists("memset" "string.h" HAVE_MEMSET) +check_symbol_exists("memcpy" "string.h" HAVE_MEMCPY) +if (NOT HAVE_MEMSET OR NOT HAVE_MEMCPY) + set(NEED_BSD_STRINGS 1) +endif () + +include(CheckIncludeFile) +if(MSVC) + check_include_file("intrin.h" HAVE_INTRIN_H) +endif() + +set(BITS_IN_JSAMPLE 8) + +set(JPEG_LIB_VERSION 80) +set(LIBJPEG_TURBO_VERSION "3.0.4") +set(LIBJPEG_TURBO_VERSION_NUMBER "003000004") +set(MEM_SRCDST_SUPPORTED 1) +set(C_ARITH_CODING_SUPPORTED 1) +set(D_ARITH_CODING_SUPPORTED 1) +set(NEED_SYS_TYPES_H 1) +#set(WITH_SIMD 1) + +set(THREAD_LOCAL "thread_local") + + +if(UNIX) + if(CMAKE_CROSSCOMPILING) + set(RIGHT_SHIFT_IS_UNSIGNED 0) + else() + include(CheckCSourceRuns) + check_c_source_runs(" + #include + #include + static int is_shifting_signed (long arg) { + long res = arg >> 4; + if (res == -0x7F7E80CL) + return 1; /* right shift is signed */ + /* see if unsigned-shift hack will fix it. */ + /* we can't just test exact value since it depends on width of long... */ + res |= 0xFFFFFFFFL << (32-4); + if (res == -0x7F7E80CL) + return 0; /* right shift is unsigned */ + printf(\"Right shift isn't acting as I expect it to.\\\\n\"); + printf(\"I fear the JPEG software will not work at all.\\\\n\\\\n\"); + return 0; /* try it with unsigned anyway */ + } + int main (void) { + exit(is_shifting_signed(-0x7F7E80B1L)); + }" RIGHT_SHIFT_IS_UNSIGNED) + endif() +endif() + + +if (BUILD_SHARED_LIBS) + set(JPEG_SHARED_LIBS 1) +else () + set(JPEG_SHARED_LIBS 0) +endif () + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}${header_input_dir}/jconfig.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/jconfig.h") + +set(headers + jerror.h + jmorecfg.h + jpeglib.h + "${CMAKE_CURRENT_BINARY_DIR}/jconfig.h") + +include(CheckTypeSize) +check_type_size("size_t" SIZE_T) + +if(SIZE_T EQUAL UNSIGNED_LONG) + check_c_source_compiles("int main(int argc, char **argv) { unsigned long a = argc; return __builtin_ctzl(a); }" + HAVE_BUILTIN_CTZL) +endif() + +set(SIZEOF_SIZE_T "${SIZE_T}") +set(VERSION "${LIBJPEG_TURBO_VERSION}") +set(PACKAGE_NAME "itkjpeg") +string(TIMESTAMP BUILD "%Y%m%d") + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/jconfigint.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/jconfigint.h") +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/jversion.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/jversion.h") + + +# Compile a separate version of these source files with 12-bit and 16-bit +# data precision. +add_library(itkjpeg12 OBJECT ${JPEG12_SOURCES}) +set_property(TARGET itkjpeg12 PROPERTY COMPILE_FLAGS "-DBITS_IN_JSAMPLE=12") +target_include_directories(itkjpeg12 + PUBLIC + "$") +add_library(itkjpeg16 OBJECT ${JPEG16_SOURCES}) +set_property(TARGET itkjpeg16 PROPERTY COMPILE_FLAGS "-DBITS_IN_JSAMPLE=16") +target_include_directories(itkjpeg16 + PUBLIC + "$") + + +add_library(itkjpeg ${JPEG_SOURCES}) +target_link_libraries(itkjpeg itkjpeg12 itkjpeg16) +target_include_directories(itkjpeg + PUBLIC + "$") + +# Apply user-defined properties to the library target. +if(ITK_LIBRARY_PROPERTIES) + set_target_properties(itkjpeg PROPERTIES ${ITK_LIBRARY_PROPERTIES}) + #set_target_properties(itkjpeg12 PROPERTIES ${ITK_LIBRARY_PROPERTIES}) + #set_target_properties(itkjpeg16 PROPERTIES ${ITK_LIBRARY_PROPERTIES}) +endif(ITK_LIBRARY_PROPERTIES) + +install(TARGETS itkjpeg itkjpeg12 itkjpeg16 + EXPORT ${ITK3P_INSTALL_EXPORT_NAME} + RUNTIME DESTINATION ${ITK3P_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries + LIBRARY DESTINATION ${ITK3P_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries + ARCHIVE DESTINATION ${ITK3P_INSTALL_ARCHIVE_DIR} COMPONENT Development +) \ No newline at end of file diff --git a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfig.h.in b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfig.h.in index 6cb82962ffe..76f4d72e616 100644 --- a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfig.h.in +++ b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfig.h.in @@ -1,13 +1,13 @@ /* Version ID for the JPEG library. * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60". */ -#define JPEG_LIB_VERSION @JPEG_LIB_VERSION@ +#cmakedefine JPEG_LIB_VERSION @JPEG_LIB_VERSION@ /* libjpeg-turbo version */ -#define LIBJPEG_TURBO_VERSION @VERSION@ +#cmakedefine LIBJPEG_TURBO_VERSION @VERSION@ /* libjpeg-turbo version in integer form */ -#define LIBJPEG_TURBO_VERSION_NUMBER @LIBJPEG_TURBO_VERSION_NUMBER@ +#cmakedefine LIBJPEG_TURBO_VERSION_NUMBER @LIBJPEG_TURBO_VERSION_NUMBER@ /* Support arithmetic encoding when using 8-bit samples */ #cmakedefine C_ARITH_CODING_SUPPORTED 1 @@ -57,4 +57,6 @@ typedef signed int INT32; unsigned. */ #cmakedefine RIGHT_SHIFT_IS_UNSIGNED 1 +#cmakedefine JPEG_SHARED_LIBS + #endif diff --git a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfigint.h.in b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfigint.h.in index 5c14e32a1d1..1834fe7ff6b 100644 --- a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfigint.h.in +++ b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jconfigint.h.in @@ -1,5 +1,5 @@ /* libjpeg-turbo build number */ -#define BUILD "@BUILD@" +#cmakedefine BUILD "@BUILD@" /* How to hide global symbols. */ #define HIDDEN @HIDDEN@ @@ -8,19 +8,19 @@ #undef inline /* How to obtain function inlining. */ -#define INLINE @INLINE@ +#define INLINE inline /* How to obtain thread-local storage */ -#define THREAD_LOCAL @THREAD_LOCAL@ +#cmakedefine THREAD_LOCAL @THREAD_LOCAL@ /* Define to the full name of this package. */ -#define PACKAGE_NAME "@CMAKE_PROJECT_NAME@" +#cmakedefine PACKAGE_NAME "@CMAKE_PROJECT_NAME@" /* Version number of package */ -#define VERSION "@VERSION@" +#cmakedefine VERSION "@VERSION@" /* The size of `size_t', as computed by sizeof. */ -#define SIZEOF_SIZE_T @SIZE_T@ +#cmakedefine SIZEOF_SIZE_T @SIZE_T@ /* Define if your compiler has __builtin_ctzl() and sizeof(unsigned long) == sizeof(size_t). */ #cmakedefine HAVE_BUILTIN_CTZL diff --git a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jmorecfg.h b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jmorecfg.h index 89c7842c871..7cb38a966e2 100644 --- a/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jmorecfg.h +++ b/Modules/ThirdParty/JPEG/src/itkjpeg-turbo/jmorecfg.h @@ -171,7 +171,19 @@ typedef unsigned int JDIMENSION; /* a function referenced thru EXTERNs: */ #define GLOBAL(type) type /* a reference to a GLOBAL function: */ +#ifdef _WIN32 +#if JPEG_SHARED_LIBS +#ifdef itkjpeg_EXPORTS +#define EXTERN(type) __declspec(dllexport) type +#else +#define EXTERN(type) __declspec(dllimport) type +#endif +#else +#define EXTERN(type) type +#endif +#else #define EXTERN(type) extern type +#endif /* Originally, this macro was used as a way of defining function prototypes