Skip to content

Commit

Permalink
fix: add image-io cxx sources
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst authored and thewtex committed Oct 3, 2023
1 parent 376e70f commit f4ff5eb
Show file tree
Hide file tree
Showing 11 changed files with 970 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"build:emscripten:compare-images": "node ./src/itk-wasm-cli.js -s packages/compare-images -b emscripten-build build",
"build:bindgen:typescript:compare-images": "./src/itk-wasm-cli.js -s packages/compare-images -b emscripten-build bindgen --package-version 1.0.1 --package-name @itk-wasm/compare-images --package-description \"Compare images with a tolerance for regression testing.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:compare-images": "./src/itk-wasm-cli.js -s packages/compare-images -b wasi-build bindgen --package-version 1.0.1 --interface python --package-name itkwasm-compare-images --package-description \"Compare images with a tolerance for regression testing.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:packages": "npm run build:emscripten:compress-stringify && npm run build:bindgen:typescript:compress-stringify && npm run build:emscripten:dicom && npm run build:bindgen:typescript:dicom && npm run build:emscripten:compare-images && npm run build:bindgen:typescript:compare-images",
"build:emscripten:image-io": "node ./src/itk-wasm-cli.js -s packages/image-io -b emscripten-build build",
"build:bindgen:typescript:image-io": "./src/itk-wasm-cli.js -s packages/image-io -b emscripten-build bindgen --package-version 1.0.0 --package-name @itk-wasm/image-io --package-description \"Input and output for scientific and medical image file formats.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:packages": "npm run build:emscripten:compress-stringify && npm run build:bindgen:typescript:compress-stringify && npm run build:emscripten:dicom && npm run build:bindgen:typescript:dicom && npm run build:emscripten:compare-images && npm run build:bindgen:typescript:compare-images && npm run build:emscripten:image-io && npm run build:bindgen:typescript:image-io",
"build:wasi": "node ./src/build-wasi.js && npm run build:wasi:packages",
"build:wasi:compress-stringify": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/compress-stringify -b wasi-build build",
"build:wasi:dicom": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/dicom -b wasi-build build",
Expand Down
3 changes: 3 additions & 0 deletions packages/image-io/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ImageIOIndex.ts.in
dist
emscripten-build
25 changes: 25 additions & 0 deletions packages/image-io/BuildZstd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include(FetchContent)
option(ZSTD_BUILD_CONTRIB "BUILD_CONTRIB" OFF)
option(ZSTD_BUILD_PROGRAMS "BUILD_PROGRAMS" OFF)
option(ZSTD_BUILD_SHARED "BUILD_SHARED" OFF)
option(ZSTD_BUILD_STATIC "BUILD_STATIC" ON)
option(ZSTD_BUILD_TESTS "BUILD_TESTS" OFF)
option(ZSTD_BUILD_LEGACY_SUPPORT "BUILD_LEGACY_SUPPORT" OFF)
option(ZSTD_MULTITHREAD_SUPPORT "BUILD_MULTITHREAD_SUPPORT" OFF)
option(ZSTD_BUILD_PROGRAMS_LINK_SHARED "BUILD_PROGRAMS_LINK_SHARED" OFF)
option(ZSTD_BUILD_LZ4 "BUILD_LZ4" OFF)
option(ZSTD_BUILD_LZMA "BUILD_LZMA" OFF)
option(ZSTD_BUILD_ZLIB "BUILD_ZLIB" OFF)
set(zstd_GIT_REPOSITORY "https://github.com/facebook/zstd.git")
# v1.5.2
set(zstd_GIT_TAG c9c7be85f49f45a581ec00c309afda5c62ba9ef2)
FetchContent_Declare(
zstd_lib
GIT_REPOSITORY ${zstd_GIT_REPOSITORY}
GIT_TAG ${zstd_GIT_TAG}
)

FetchContent_MakeAvailable(zstd_lib)
set(zstd_lib_INCLUDE_DIR "${zstd_lib_SOURCE_DIR}/lib")
include_directories(${zstd_lib_INCLUDE_DIR})
add_subdirectory("${zstd_lib_SOURCE_DIR}/build/cmake" "${zstd_lib_BINARY_DIR}")
193 changes: 193 additions & 0 deletions packages/image-io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
cmake_minimum_required(VERSION 3.16)
project(convert-image)

set(CMAKE_CXX_STANDARD 17)

if (NOT TARGET libzstd_static)
include(${CMAKE_CURRENT_SOURCE_DIR}/BuildZstd.cmake)
endif()

if(WASI)
set(WebAssemblyInterface_ImageIOModules
"ITKIOPNG"
"ITKIOMeta"
"ITKIOTIFF"
"ITKIONIFTI"
"ITKIOJPEG"
"ITKIONRRD"
"ITKIOVTK"
"ITKIOBMP"
"ITKIOMRC"
"ITKIOLSM"
"MGHIO"
"ITKIOBioRad"
"ITKIOGIPL"
"ITKIOGE"
"ITKIOGDCM"
"IOScanco"
"IOFDF"
CACHE STRING
"String delimited list of ITK image IO modules to support.")
else()
# This list should be ordered from approximately most commonly to least
# commonly used modules.
set(WebAssemblyInterface_ImageIOModules
"ITKIOPNG"
"ITKIOMeta"
"ITKIOTIFF"
"ITKIONIFTI"
"ITKIOJPEG"
"ITKIONRRD"
"ITKIOVTK"
"ITKIOBMP"
"ITKIOHDF5"
"ITKIOMINC"
"ITKIOMRC"
"ITKIOLSM"
"MGHIO"
"ITKIOBioRad"
"ITKIOGIPL"
"ITKIOGE"
"ITKIOGDCM"
"IOScanco"
"IOFDF"
CACHE STRING
"String delimited list of ITK image IO modules to support.")
endif()
set(imageios_ITKIOPNG itkPNGImageIO)
set(imageio_id_itkPNGImageIO 0)

set(imageios_ITKIOMeta itkMetaImageIO)
set(imageio_id_itkMetaImageIO 1)

set(imageios_ITKIOTIFF itkTIFFImageIO)
set(imageio_id_itkTIFFImageIO 2)

set(imageios_ITKIONIFTI itkNiftiImageIO)
set(imageio_id_itkNiftiImageIO 3)

set(imageios_ITKIOJPEG itkJPEGImageIO)
set(imageio_id_itkJPEGImageIO 4)

set(imageios_ITKIONRRD itkNrrdImageIO)
set(imageio_id_itkNrrdImageIO 5)

set(imageios_ITKIOVTK itkVTKImageIO)
set(imageio_id_itkVTKImageIO 6)

set(imageios_ITKIOBMP itkBMPImageIO)
set(imageio_id_itkBMPImageIO 7)

set(imageios_ITKIOHDF5 itkHDF5ImageIO)
set(imageio_id_itkHDF5ImageIO 8)

set(imageios_ITKIOMINC itkMINCImageIO)
set(imageio_id_itkMINCImageIO 9)

set(imageios_ITKIOMRC itkMRCImageIO)
set(imageio_id_itkMRCImageIO 10)

set(imageios_ITKIOLSM itkLSMImageIO)
set(imageio_id_itkLSMImageIO 11)

set(imageios_MGHIO itkMGHImageIO)
set(imageio_id_itkMGHImageIO 12)

set(imageios_ITKIOBioRad itkBioRadImageIO)
set(imageio_id_itkBioRadImageIO 13)

set(imageios_ITKIOGIPL itkGiplImageIO)
set(imageio_id_itkGiplImageIO 14)

set(imageios_ITKIOGE itkGEAdwImageIO itkGE4ImageIO itkGE5ImageIO)
set(imageio_id_itkGE4ImageIO 15)
set(imageio_id_itkGE5ImageIO 16)
set(imageio_id_itkGEAdwImageIO 17)

set(imageios_ITKIOGDCM itkGDCMImageIO)
set(imageio_id_itkGDCMImageIO 18)

set(imageios_IOScanco itkScancoImageIO)
set(imageio_id_itkScancoImageIO 19)

set(imageios_IOFDF itkFDFImageIO)
set(imageio_id_itkFDFImageIO 20)

set(imageios_WebAssemblyInterface itkWasmImageIO itkWasmZstdImageIO)
set(imageio_id_itkWasmImageIO 21)
set(imageio_id_itkWasmZstdImageIO 22)

set(ITK_NO_IMAGEIO_FACTORY_REGISTER_MANAGER 1)
set(ImageIOIndex_ARRAY "")
foreach(io_module ${WebAssemblyInterface_ImageIOModules} WebAssemblyInterface)
if (DEFINED WebAssemblyInterface_INCLUDE_DIRS)
if(${io_module} STREQUAL "WebAssemblyInterface")
find_package(ITK REQUIRED COMPONENTS ITKIOImageBase)
include(${ITK_USE_FILE})
include_directories(${WebAssemblyInterface_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND ITK_LIBRARIES ${WebAssemblyInterface_LIBRARIES})
else()
find_package(ITK REQUIRED COMPONENTS ${io_module} WebAssemblyInterface)
include(${ITK_USE_FILE})
endif()
else()
find_package(ITK REQUIRED COMPONENTS ${io_module} WebAssemblyInterface)
include(${ITK_USE_FILE})
endif()

if(NOT DEFINED imageios_${io_module})
message(FATAL_ERROR "Unknown ImageIOBase classes for module ${io_module}")
endif()
foreach(imageio ${imageios_${io_module}})
string(SUBSTRING "${imageio}" 3 -1 ioname)
set(read_binary "${ioname}-read-image")
set(write_binary "${ioname}-write-image")
set(ImageIOIndex_ARRAY "${ImageIOIndex_ARRAY}'${ioname}', ")
set(extra_srcs)
if(${imageio} STREQUAL "itkWasmZstdImageIO")
list(APPEND ITK_LIBRARIES libzstd_static)
list(APPEND extra_srcs itkWasmZstdImageIO.cxx)
endif()

add_executable(${read_binary} read-image.cxx ${extra_srcs})
target_link_libraries(${read_binary} PUBLIC ${ITK_LIBRARIES})
target_compile_definitions(${read_binary} PUBLIC -DIMAGE_IO_CLASS=${imageio_id_${imageio}})
add_executable(${write_binary} write-image.cxx ${extra_srcs})
target_link_libraries(${write_binary} PUBLIC ${ITK_LIBRARIES})
target_compile_definitions(${write_binary} PUBLIC -DIMAGE_IO_CLASS=${imageio_id_${imageio}})
if (EMSCRIPTEN)
set(target_esm_read "${read_binary}")
set(target_umd_read "${read_binary}.umd")
set(target_esm_write "${write_binary}")
set(target_umd_write "${write_binary}.umd")
target_compile_definitions(${target_umd_read} PUBLIC -DIMAGE_IO_CLASS=${imageio_id_${imageio}})
target_compile_definitions(${target_umd_write} PUBLIC -DIMAGE_IO_CLASS=${imageio_id_${imageio}})
foreach(target ${target_esm_read} ${target_umd_read} ${target_esm_write} ${target_umd_write})
set(exception_catching )
if(${io_module} STREQUAL "ITKIOGE")
set(exception_catching " -s DISABLE_EXCEPTION_CATCHING=0")
endif()
set(imageio_common_link_flags " -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s SUPPORT_LONGJMP=1")
get_property(link_flags TARGET ${target} PROPERTY LINK_FLAGS)
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${imageio_common_link_flags} ${link_flags}")
set_property(TARGET ${target}
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/typescript/dist/pipelines
)
endforeach()
elseif (WASI)
foreach(target ${read_binary} ${write_binary})
set(exception_catching )
set_property(TARGET ${target}
PROPERTY RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/typescript/wasi/pipelines
)
endforeach()
endif()
endforeach()
endforeach()
if(EMSCRIPTEN)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ImageIOIndex.ts.in
${CMAKE_CURRENT_SOURCE_DIR}/typescript/src/ImageIOIndex.ts @ONLY)
endif()
Loading

0 comments on commit f4ff5eb

Please sign in to comment.