Skip to content

Commit e3c8c16

Browse files
authored
Add support for HTJ2K, returning bytearray (#70)
1 parent 16dfb13 commit e3c8c16

File tree

13 files changed

+1226
-708
lines changed

13 files changed

+1226
-708
lines changed

.github/workflows/pytest-builds.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
python -m pip install -U pip
3232
python -m pip install -U pytest coverage pytest-cov
3333
python -m pip install git+https://github.com/pydicom/pylibjpeg-data
34-
python -m pip install . -vv
34+
python -m pip install .
3535
3636
- name: Run pytest
3737
run: |

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __pycache__/
88

99
# Distribution / packaging
1010
.Python
11-
build/
11+
#build/
1212
develop-eggs/
1313
dist/
1414
downloads/

build.py

+63-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
PACKAGE_DIR = Path(__file__).parent / "openjpeg"
11+
BUILD_TOOLS = PACKAGE_DIR.parent / "build_tools"
1112
OPENJPEG_SRC = PACKAGE_DIR / "src" / "openjpeg" / "src" / "lib" / "openjp2"
1213
INTERFACE_SRC = PACKAGE_DIR / "src" / "interface"
1314

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

54+
reset_oj()
55+
5356
return setup_kwargs
5457

5558

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

7881
def setup_oj() -> None:
7982
"""Run custom cmake."""
80-
base_dir = os.path.join("openjpeg", "src", "openjpeg")
83+
base_dir = PACKAGE_DIR / "src" / "openjpeg"
84+
p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"
85+
86+
# Backup original CMakeLists.txt and openjpeg.c files
87+
backup_dir = BUILD_TOOLS / "backup"
88+
if os.path.exists(backup_dir):
89+
shutil.rmtree(backup_dir)
90+
91+
backup_dir.mkdir(exist_ok=True, parents=True)
92+
93+
shutil.copy(
94+
base_dir / "CMakeLists.txt",
95+
backup_dir / "CMakeLists.txt.backup",
96+
)
97+
shutil.copy(
98+
p_openjpeg,
99+
backup_dir / "openjpeg.c.backup",
100+
)
81101

82102
# Copy custom CMakeLists.txt file to openjpeg base dir
83103
shutil.copy(
84-
os.path.join("build_tools", "cmake", "CMakeLists.txt"),
85-
base_dir
104+
BUILD_TOOLS / "cmake" / "CMakeLists.txt",
105+
base_dir,
86106
)
87-
build_dir = os.path.join(base_dir, "build")
107+
# Edit openjpeg.c to remove the OPJ_API declaration
108+
with p_openjpeg.open("r") as f:
109+
data = f.readlines()
110+
111+
data = [
112+
line.replace("OPJ_API ", "")
113+
if line.startswith("OPJ_API ") else line for line in data
114+
]
115+
with p_openjpeg.open("w") as f:
116+
f.write("".join(data))
117+
118+
build_dir = base_dir / "build"
88119
if os.path.exists(build_dir):
89120
shutil.rmtree(build_dir)
90121

@@ -106,3 +137,31 @@ def setup_oj() -> None:
106137
with open(INTERFACE_SRC / "opj_config.h", "a") as f:
107138
f.write("\n")
108139
f.write("#define USE_JPIP 0")
140+
141+
142+
def reset_oj() -> None:
143+
# Restore submodule to original state
144+
# Restore CMakeLists.txt and openjpeg.c files
145+
base_dir = PACKAGE_DIR / "src" / "openjpeg"
146+
build_dir = base_dir / "build"
147+
backup_dir = BUILD_TOOLS / "backup"
148+
p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"
149+
150+
if (backup_dir / "CMakeLists.txt.backup").exists():
151+
shutil.copy(
152+
backup_dir / "CMakeLists.txt.backup",
153+
base_dir / "CMakeLists.txt",
154+
)
155+
156+
if (backup_dir / "openjpeg.c.backup").exists():
157+
shutil.copy(
158+
BUILD_TOOLS / "backup" / "openjpeg.c.backup",
159+
p_openjpeg,
160+
)
161+
162+
# Cleanup added directories
163+
if os.path.exists(build_dir):
164+
shutil.rmtree(build_dir)
165+
166+
if os.path.exists(backup_dir):
167+
shutil.rmtree(backup_dir)

build_tools/cmake/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
9595
TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
9696
endif()
9797

98+
#-----------------------------------------------------------------------------
99+
# OpenJPEG build configuration options.
100+
option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
101+
option(BUILD_STATIC_LIBS "Build OpenJPEG static library." ON)
102+
set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
103+
set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
104+
mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
105+
106+
set(BUILD_SHARED_LIBS ON)
107+
108+
#-----------------------------------------------------------------------------
109+
# configure name mangling to allow multiple libraries to coexist
110+
# peacefully
111+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
112+
set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
113+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
114+
${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
115+
@ONLY)
116+
endif()
117+
118+
#-----------------------------------------------------------------------------
119+
# Compiler specific flags:
120+
if(CMAKE_COMPILER_IS_GNUCC)
121+
# For all builds, make sure openjpeg is std99 compliant:
122+
# set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
123+
# Do not use ffast-math for all build, it would produce incorrect results, only set for release:
124+
set(OPENJPEG_LIBRARY_COMPILE_OPTIONS ${OPENJPEG_LIBRARY_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>")
125+
set(OPENJP2_COMPILE_OPTIONS ${OPENJP2_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>" -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement)
126+
endif()
98127

99128
#-----------------------------------------------------------------------------
100129
# opj_config.h generation (1/2)

0 commit comments

Comments
 (0)