Skip to content

Commit

Permalink
Add support for HTJ2K, returning bytearray (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion authored Jan 4, 2024
1 parent 16dfb13 commit e3c8c16
Show file tree
Hide file tree
Showing 13 changed files with 1,226 additions and 708 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
python -m pip install -U pip
python -m pip install -U pytest coverage pytest-cov
python -m pip install git+https://github.com/pydicom/pylibjpeg-data
python -m pip install . -vv
python -m pip install .
- name: Run pytest
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __pycache__/

# Distribution / packaging
.Python
build/
#build/
develop-eggs/
dist/
downloads/
Expand Down
67 changes: 63 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


PACKAGE_DIR = Path(__file__).parent / "openjpeg"
BUILD_TOOLS = PACKAGE_DIR.parent / "build_tools"
OPENJPEG_SRC = PACKAGE_DIR / "src" / "openjpeg" / "src" / "lib" / "openjp2"
INTERFACE_SRC = PACKAGE_DIR / "src" / "interface"

Expand Down Expand Up @@ -50,6 +51,8 @@ def build(setup_kwargs: Any) -> Any:
relative_ext = output.relative_to(cmd.build_lib)
shutil.copyfile(output, relative_ext)

reset_oj()

return setup_kwargs


Expand Down Expand Up @@ -77,14 +80,42 @@ def get_source_files() -> List[Path]:

def setup_oj() -> None:
"""Run custom cmake."""
base_dir = os.path.join("openjpeg", "src", "openjpeg")
base_dir = PACKAGE_DIR / "src" / "openjpeg"
p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"

# Backup original CMakeLists.txt and openjpeg.c files
backup_dir = BUILD_TOOLS / "backup"
if os.path.exists(backup_dir):
shutil.rmtree(backup_dir)

backup_dir.mkdir(exist_ok=True, parents=True)

shutil.copy(
base_dir / "CMakeLists.txt",
backup_dir / "CMakeLists.txt.backup",
)
shutil.copy(
p_openjpeg,
backup_dir / "openjpeg.c.backup",
)

# Copy custom CMakeLists.txt file to openjpeg base dir
shutil.copy(
os.path.join("build_tools", "cmake", "CMakeLists.txt"),
base_dir
BUILD_TOOLS / "cmake" / "CMakeLists.txt",
base_dir,
)
build_dir = os.path.join(base_dir, "build")
# Edit openjpeg.c to remove the OPJ_API declaration
with p_openjpeg.open("r") as f:
data = f.readlines()

data = [
line.replace("OPJ_API ", "")
if line.startswith("OPJ_API ") else line for line in data
]
with p_openjpeg.open("w") as f:
f.write("".join(data))

build_dir = base_dir / "build"
if os.path.exists(build_dir):
shutil.rmtree(build_dir)

Expand All @@ -106,3 +137,31 @@ def setup_oj() -> None:
with open(INTERFACE_SRC / "opj_config.h", "a") as f:
f.write("\n")
f.write("#define USE_JPIP 0")


def reset_oj() -> None:
# Restore submodule to original state
# Restore CMakeLists.txt and openjpeg.c files
base_dir = PACKAGE_DIR / "src" / "openjpeg"
build_dir = base_dir / "build"
backup_dir = BUILD_TOOLS / "backup"
p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"

if (backup_dir / "CMakeLists.txt.backup").exists():
shutil.copy(
backup_dir / "CMakeLists.txt.backup",
base_dir / "CMakeLists.txt",
)

if (backup_dir / "openjpeg.c.backup").exists():
shutil.copy(
BUILD_TOOLS / "backup" / "openjpeg.c.backup",
p_openjpeg,
)

# Cleanup added directories
if os.path.exists(build_dir):
shutil.rmtree(build_dir)

if os.path.exists(backup_dir):
shutil.rmtree(backup_dir)
29 changes: 29 additions & 0 deletions build_tools/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@ include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
endif()

#-----------------------------------------------------------------------------
# OpenJPEG build configuration options.
option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
option(BUILD_STATIC_LIBS "Build OpenJPEG static library." ON)
set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)

set(BUILD_SHARED_LIBS ON)

#-----------------------------------------------------------------------------
# configure name mangling to allow multiple libraries to coexist
# peacefully
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
@ONLY)
endif()

#-----------------------------------------------------------------------------
# Compiler specific flags:
if(CMAKE_COMPILER_IS_GNUCC)
# For all builds, make sure openjpeg is std99 compliant:
# set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
# Do not use ffast-math for all build, it would produce incorrect results, only set for release:
set(OPENJPEG_LIBRARY_COMPILE_OPTIONS ${OPENJPEG_LIBRARY_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>")
set(OPENJP2_COMPILE_OPTIONS ${OPENJP2_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>" -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement)
endif()

#-----------------------------------------------------------------------------
# opj_config.h generation (1/2)
Expand Down
Loading

0 comments on commit e3c8c16

Please sign in to comment.