├── _config.yml ├── docs ├── .gitignore ├── OpenJPH_docs.css ├── docker.md ├── web_demos.md ├── status.md ├── doxygen_style.md ├── usage_examples.md └── compiling.md ├── subprojects ├── .gitignore └── js │ ├── build │ └── .gitignore │ ├── html │ ├── .gitignore │ ├── test.j2c │ ├── libopenjph.wasm │ └── libopenjph_simd.wasm │ ├── .gitignore │ ├── emscripten-docker.sh │ ├── build.sh │ ├── CMakeLists.txt │ └── standalone │ ├── com_decom.sh │ ├── com_decom_yuv.sh │ ├── README.md │ └── build.sh ├── .gitignore ├── src ├── openjph-config.cmake.in ├── openjph.pc.in ├── apps │ ├── CMakeLists.txt │ ├── ojph_stream_expand │ │ ├── CMakeLists.txt │ │ ├── threaded_frame_processors.cpp │ │ └── threaded_frame_processors.h │ ├── ojph_expand │ │ └── CMakeLists.txt │ ├── ojph_compress │ │ └── CMakeLists.txt │ ├── others │ │ ├── ojph_threads.cpp │ │ └── ojph_sockets.cpp │ └── common │ │ └── ojph_threads.h └── core │ ├── common │ ├── ojph_version.h │ ├── ojph_base.h │ └── ojph_defs.h │ ├── coding │ ├── ojph_block_common.h │ ├── ojph_block_decoder.h │ └── ojph_block_encoder.h │ ├── codestream │ ├── ojph_codestream_sse.cpp │ ├── ojph_codestream_avx.cpp │ ├── ojph_precinct.h │ ├── ojph_tile_comp.h │ ├── ojph_tile.h │ ├── ojph_subband.h │ ├── ojph_codeblock.h │ ├── ojph_codeblock_fun.h │ ├── ojph_resolution.h │ ├── ojph_bitbuffer_write.h │ ├── ojph_codestream_gen.cpp │ ├── ojph_codestream_local.h │ ├── ojph_codestream.cpp │ └── ojph_tile_comp.cpp │ ├── transform │ ├── ojph_colour_sse.cpp │ ├── ojph_transform.h │ ├── ojph_colour_avx.cpp │ └── ojph_colour.h │ └── others │ ├── ojph_mem.c │ ├── ojph_mem.cpp │ └── ojph_message.cpp ├── tests ├── test_helpers │ └── Readme.md ├── mse_pae.cmake ├── compare_files.cpp └── CMakeLists.txt ├── .github ├── FUNDING.yml └── workflows │ ├── emcc.yml │ ├── codeql.yml │ └── ccp-workflow.yml ├── Dockerfile ├── .devcontainer ├── devcontainer.json └── Dockerfile ├── ojph_version.cmake ├── LICENSE ├── README.md └── target_arch.cmake /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | html/* 3 | -------------------------------------------------------------------------------- /subprojects/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /subprojects/js/build/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | * 3 | -------------------------------------------------------------------------------- /subprojects/js/html/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /subprojects/js/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | build 4 | bin -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | mytest/* 3 | others/* 4 | lib/* 5 | 6 | .vscode 7 | build.sh -------------------------------------------------------------------------------- /subprojects/js/html/test.j2c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aous72/OpenJPH/HEAD/subprojects/js/html/test.j2c -------------------------------------------------------------------------------- /docs/OpenJPH_docs.css: -------------------------------------------------------------------------------- 1 | .memTemplParams { 2 | font-size: 100%; 3 | } 4 | .memtemplate { 5 | font-size: 100%; 6 | } -------------------------------------------------------------------------------- /subprojects/js/html/libopenjph.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aous72/OpenJPH/HEAD/subprojects/js/html/libopenjph.wasm -------------------------------------------------------------------------------- /subprojects/js/html/libopenjph_simd.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aous72/OpenJPH/HEAD/subprojects/js/html/libopenjph_simd.wasm -------------------------------------------------------------------------------- /subprojects/js/emscripten-docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | docker run -it --user $(id -u):$(id -g) -v "$(cd ../.. && pwd)":/src emscripten/emsdk /bin/bash 3 | -------------------------------------------------------------------------------- /src/openjph-config.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include("${CMAKE_CURRENT_LIST_DIR}/openjph-targets.cmake") 4 | 5 | check_required_components(openjph) 6 | -------------------------------------------------------------------------------- /src/openjph.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | libdir=@PKG_CONFIG_LIBDIR@ 3 | includedir=@PKG_CONFIG_INCLUDEDIR@ 4 | 5 | Name: @PROJECT_NAME@ 6 | Description: @PROJECT_DESCRIPTION@ 7 | Version: @PROJECT_VERSION@ 8 | Requires: @PKG_CONFIG_REQUIRES@ 9 | Libs: -L${libdir} -lopenjph 10 | Cflags: -I${includedir} -D_FILE_OFFSET_BITS=64 11 | -------------------------------------------------------------------------------- /subprojects/js/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir -p build 4 | cd build 5 | emcmake cmake .. -DCMAKE_BUILD_TYPE=Release -DOJPH_DISABLE_SIMD=ON && emmake make -j8 && mv libopenjph.* ../html/ 6 | emcmake cmake .. -DCMAKE_BUILD_TYPE=Release -DOJPH_DISABLE_SIMD=OFF && emmake make -j8 && mv libopenjph.wasm ../html/libopenjph_simd.wasm 7 | cd .. 8 | sed 's/libopenjph.wasm/libopenjph_simd.wasm/g' build/libopenjph.js > html/libopenjph_simd.js 9 | rm build/libopenjph.js 10 | -------------------------------------------------------------------------------- /docs/docker.md: -------------------------------------------------------------------------------- 1 | # Compiling and Running in Docker # 2 | 3 | ## Step 1 - clone repository 4 | `https://github.com/aous72/OpenJPH.git` 5 | 6 | ## Step 2 - build docker image 7 | `cd OpenJPH` 8 | `docker build --rm -f Dockerfile -t openjph:latest .` 9 | 10 | ## Step 3 - run docker image 11 | 12 | ### in isolated container 13 | `docker run -it --rm openjph:latest` 14 | 15 | ### mapping /usr/src/openjph/build directory in the container to local windows c:\temp 16 | `docker run -it --rm -v C:\\temp:/usr/src/openjph/build openjph:latest` 17 | -------------------------------------------------------------------------------- /docs/web_demos.md: -------------------------------------------------------------------------------- 1 | # Web-based Demos # 2 | 3 | The associate site [openjph.org](https://openjph.org) serves as a blog. It currently host the [javascript](https://openjph.org/javascript/demo.html) demo of the decoder; the webpage demonstrates that the library can be compiled to javascript, and can run inside a web-browser. Any browser supporting webassembly can be used to view this webpage; examples include Firefox, Chrome, Safari, and Edge, on a desktop, mobile, or tablet. 4 | 5 | Another project of interest is the [openjphjs](https://github.com/chafey/openjphjs) project, developed by [Chris](https://github.com/chafey). You can see [there](https://chafey.github.io/openjphjs/test/browser/index.html) a nice online demonstration of javascript-based HTJ2K encoding/decoding, with a wealth of features and user-selectable options. 6 | -------------------------------------------------------------------------------- /tests/test_helpers/Readme.md: -------------------------------------------------------------------------------- 1 | # Test Helpers 2 | 3 | This folder has some code that helps with generating tests. 4 | 5 | Currently it has 6 | 7 | # convert_mse_pae_to_test.cpp 8 | This file can be compiled using 9 | 10 | ``` g++ -g3 convert_mse_pae_to_tests.cpp -o convert_mse_pae_to_tests ``` 11 | 12 | The generated executable uses ```mse_pae.txt``` in the 13 | ```jp2k_test_streams/openjph``` folder and ```ht_cmdlines.txt``` in this folder 14 | to generate the ```openjph_tests.cpp``` file. The ```openjph_tests.cpp``` file 15 | contains all the googletest tests from those original files, together with some 16 | comments. The content of ```openjph_tests.cpp``` needs to be copied and pasted 17 | in ```../test_executables.cpp```, because googletest does not support 18 | included files. 19 | 20 | # Other files can added in the future. -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: aous72 # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | # patreon: # Replace with a single Patreon username 5 | # open_collective: # Replace with a single Open Collective username 6 | # ko_fi: # Replace with a single Ko-fi username 7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | # liberapay: # Replace with a single Liberapay username 10 | # issuehunt: # Replace with a single IssueHunt username 11 | # otechie: # Replace with a single Otechie username 12 | # lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | # custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:focal 2 | 3 | RUN apt-get update 4 | 5 | # disable interactive install 6 | ENV DEBIAN_FRONTEND noninteractive 7 | 8 | # install developement tools 9 | RUN apt-get -y install cmake 10 | RUN apt-get -y install g++ 11 | RUN apt-get -y install libtiff-dev 12 | 13 | # install developement debugging tools 14 | RUN apt-get -y install valgrind 15 | 16 | # OpenJPH 17 | WORKDIR /usr/src/openjph/ 18 | COPY . . 19 | WORKDIR /usr/src/openjph/build 20 | RUN cmake -DCMAKE_BUILD_TYPE=Release ../ 21 | RUN make 22 | ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/src/openjph/bin 23 | ENV PATH=$PATH:/usr/src/openjph/bin 24 | 25 | # finalize docker environment 26 | WORKDIR /usr/src/openjph 27 | 28 | # step 1 - build docker image 29 | # docker build --rm -f Dockerfile -t openjph:latest . 30 | # step 2 - run docker image 31 | # docker run -it --rm openjph:latest 32 | # docker run -it --rm -v C:\\temp:/tmp openjph:latest 33 | -------------------------------------------------------------------------------- /subprojects/js/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10.0) 2 | 3 | set(CMAKE_SYSTEM_NAME Generic) 4 | 5 | project (OpenJPH_WASM DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX) 6 | 7 | add_subdirectory("../.." openjph EXCLUDE_FROM_ALL) 8 | add_executable(libopenjph "src/ojph_wrapper.cpp") 9 | if (OJPH_DISABLE_SIMD) 10 | else() 11 | target_compile_options(libopenjph PRIVATE -DOJPH_ENABLE_WASM_SIMD -msimd128) 12 | endif() 13 | set_target_properties(libopenjph PROPERTIES SUFFIX ".js") 14 | target_link_options(libopenjph PRIVATE 15 | -fexceptions 16 | -sWASM=1 17 | -sEXPORT_ES6=1 18 | -sMODULARIZE=1 19 | -sENVIRONMENT=web 20 | -sEXPORTED_FUNCTIONS=[_free,_malloc] 21 | -sEXPORTED_RUNTIME_METHODS=[ccall,cwrap,writeArrayToMemory] 22 | -sNO_EXIT_RUNTIME=1 23 | -sALLOW_MEMORY_GROWTH=1 24 | -sINITIAL_MEMORY=134217728 25 | ) 26 | target_link_libraries(libopenjph PRIVATE openjph) 27 | 28 | -------------------------------------------------------------------------------- /src/apps/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # Add tiff library 3 | ############################################################ 4 | if( OJPH_ENABLE_TIFF_SUPPORT AND (NOT EMSCRIPTEN)) 5 | 6 | FIND_PACKAGE( TIFF ) 7 | 8 | if( TIFF_FOUND ) 9 | set(USE_TIFF TRUE CACHE BOOL "Add TIFF support") 10 | add_definitions(-DOJPH_ENABLE_TIFF_SUPPORT) 11 | else() 12 | message(WARNING "TIFF support has been requested but no path to the TIFF library " 13 | "has been specified; please configure with -DCMAKE_PREFIX_PATH=, " 14 | "or disable TIFF support using -DOJPH_ENABLE_TIFF_SUPPORT=OFF.") 15 | endif( TIFF_FOUND ) 16 | 17 | endif() 18 | ############################################################ 19 | 20 | if (EMSCRIPTEN) 21 | add_link_options(-sWASM=1 -sASSERTIONS=1 -sALLOW_MEMORY_GROWTH=1 -sNODERAWFS=1 -sENVIRONMENT=node -sEXIT_RUNTIME=1 -sEXCEPTION_CATCHING_ALLOWED=['fake']) 22 | endif() 23 | 24 | ## Build executables 25 | add_subdirectory(ojph_expand) 26 | add_subdirectory(ojph_compress) 27 | if (OJPH_BUILD_STREAM_EXPAND) 28 | add_subdirectory(ojph_stream_expand) 29 | endif() -------------------------------------------------------------------------------- /src/apps/ojph_stream_expand/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ## building ojph_stream_expand 2 | ############################## 3 | 4 | find_package(Threads) 5 | 6 | file(GLOB OJPH_STREAM_EXPAND "*.cpp" "*.h") 7 | file(GLOB OJPH_SOCKETS "../others/ojph_sockets.cpp") 8 | file(GLOB OJPH_SOCKETS_H "../common/ojph_sockets.h") 9 | file(GLOB OJPH_THREADS "../others/ojph_threads.cpp") 10 | file(GLOB OJPH_THREADS_H "../common/ojph_threads.h") 11 | 12 | list(APPEND SOURCES ${OJPH_STREAM_EXPAND} ${OJPH_SOCKETS} ${OJPH_SOCKETS_H} ${OJPH_THREADS} ${OJPH_THREADS_H}) 13 | 14 | source_group("main" FILES ${OJPH_STREAM_EXPAND}) 15 | source_group("others" FILES ${OJPH_SOCKETS} ${OJPH_THREADS}) 16 | source_group("common" FILES ${OJPH_SOCKETS_H} ${OJPH_THREADS_H}) 17 | 18 | add_executable(ojph_stream_expand ${SOURCES}) 19 | target_include_directories(ojph_stream_expand PRIVATE ../common) 20 | target_link_libraries(ojph_stream_expand PRIVATE openjph Threads::Threads) 21 | if(WIN32) 22 | target_link_libraries(ojph_stream_expand PRIVATE ws2_32) 23 | endif() 24 | 25 | install(TARGETS ojph_stream_expand) 26 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/cpp 3 | { 4 | "name": "C++", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined"], 7 | 8 | // Set *default* container specific settings.json values on container create. 9 | "settings": { 10 | "terminal.integrated.shell.linux": "/bin/bash" 11 | }, 12 | 13 | // Add the IDs of extensions you want installed when the container is created. 14 | "extensions": [ 15 | "ms-vscode.cpptools", 16 | "twxs.cmake", 17 | "flixs.vs-code-http-server-and-html-preview" 18 | ], 19 | 20 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 21 | // "forwardPorts": [], 22 | 23 | // Use 'postCreateCommand' to run commands after the container is created. 24 | // "postCreateCommand": "gcc -v", 25 | 26 | // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. 27 | "remoteUser": "emscripten" 28 | 29 | } -------------------------------------------------------------------------------- /ojph_version.cmake: -------------------------------------------------------------------------------- 1 | ################################################################################################ 2 | # Generating ojph library version number 3 | ################################################################################################ 4 | 5 | ############################################################ 6 | # Parse version file 7 | # credit: https://stackoverflow.com/a/47084079 8 | 9 | file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/core/common/ojph_version.h" VERFILE) 10 | if (NOT VERFILE) 11 | message(FATAL_ERROR "Failed to parse ojph_version.h!") 12 | endif() 13 | 14 | string(REGEX MATCH "OPENJPH_VERSION_MAJOR ([0-9]*)" _ ${VERFILE}) 15 | set(OPENJPH_VERSION_MAJOR ${CMAKE_MATCH_1}) 16 | string(REGEX MATCH "OPENJPH_VERSION_MINOR ([0-9]*)" _ ${VERFILE}) 17 | set(OPENJPH_VERSION_MINOR ${CMAKE_MATCH_1}) 18 | string(REGEX MATCH "OPENJPH_VERSION_PATCH ([0-9]*)" _ ${VERFILE}) 19 | set(OPENJPH_VERSION_PATCH ${CMAKE_MATCH_1}) 20 | 21 | set(OPENJPH_VERSION "${OPENJPH_VERSION_MAJOR}.${OPENJPH_VERSION_MINOR}.${OPENJPH_VERSION_PATCH}") 22 | ############################################################ 23 | 24 | message(STATUS "OpenJPH library version: ${OPENJPH_VERSION}") 25 | 26 | if (OPENJPH_VERSION) 27 | else() 28 | message(FATAL_ERROR "OPENJPH_VERSION is not set") 29 | endif() 30 | -------------------------------------------------------------------------------- /docs/status.md: -------------------------------------------------------------------------------- 1 | # Status # 2 | 3 | The code is written in C++; the color and wavelet transform steps can employ SIMD instructions on Intel platforms. SIMD instructions are also available for the block decoder (SSE3) and for the block encoder (AVX512). Other parts of the library may include SIMD in the future, for Intel and ARM; existing implementations can also be improved as there is still decent performance improvements on the table. SIMD instructions are also employed for WebAssembly (Emscripten-based), which is now widely supported in most browsers. 4 | 5 | The encoder supports lossless and quantization-based lossy encoding. There is currently no implementation for rate-control-based encoding. 6 | 7 | As it stands, the OpenJPH library needs documentation. The provided encoder ojph\_compress only generates HTJ2K codestreams, with the extension j2c; the generated files lack the .jph header. Adding the .jph header is of little urgency, as the codestream contains all needed information to properly decode an image. The .jph header will be added at a future point in time. The provided decoder ojph\_expand decodes .jph files, by ignoring the .jph header if it is present. 8 | 9 | The provided command line tools ojph\_compress and ojph\_expand accepts and generates .pgm, .ppm, .yuv, .raw, and .dpx. See the usage examples below. -------------------------------------------------------------------------------- /.github/workflows/emcc.yml: -------------------------------------------------------------------------------- 1 | name: Build with EMCC 2 | 3 | on: 4 | push: 5 | pull_request: 6 | types: [opened, reopened] 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v4 15 | 16 | - name: Configure emcc 17 | uses: mymindstorm/setup-emsdk@v14 18 | with: 19 | actions-cache-folder: 'emsdk-cache' 20 | 21 | - name: Build non-SIMD and Debug 22 | run: | 23 | cd build 24 | emcmake cmake .. --fresh -DOJPH_DISABLE_SIMD=ON -DCMAKE_BUILD_TYPE=Debug 25 | cmake --build . --config Debug --clean-first 26 | cd .. 27 | 28 | - name: Build non-SIMD and Release 29 | run: | 30 | cd build 31 | emcmake cmake .. --fresh -DOJPH_DISABLE_SIMD=ON -DCMAKE_BUILD_TYPE=Release 32 | cmake --build . --config Release --clean-first 33 | cd .. 34 | 35 | - name: Build SIMD and Debug 36 | run: | 37 | cd build 38 | emcmake cmake .. --fresh -DOJPH_DISABLE_SIMD=OFF -DCMAKE_BUILD_TYPE=Debug 39 | cmake --build . --config Debug --clean-first 40 | cd .. 41 | 42 | - name: Build SIMD and Release 43 | run: | 44 | cd build 45 | emcmake cmake .. --fresh -DOJPH_DISABLE_SIMD=OFF -DCMAKE_BUILD_TYPE=Release 46 | cmake --build . --config Release --clean-first 47 | cd .. 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2019, Aous Naman 4 | Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 5 | Copyright (c) 2019, The University of New South Wales, Sydney, Australia 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain the above copyright notice, this 12 | list of conditions and the following disclaimer. 13 | 14 | 2. Redistributions in binary form must reproduce the above copyright notice, 15 | this list of conditions and the following disclaimer in the documentation 16 | and/or other materials provided with the distribution. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /docs/doxygen_style.md: -------------------------------------------------------------------------------- 1 | # Doxygen Documentation Style 2 | 3 | Documentation is still experimental for me, and I might change things down the line. 4 | 5 | Here, we describe how to document the source code. This represent so sort of minimal set of markers that need to be used. Other markers can be used to enhance the documentation of the code. This serves as a live document that can be updated when needed. 6 | I am preferencing `@` over `\`. 7 | 8 | ## Comment block 9 | 10 | The comment block should use 11 | 12 | ``` 13 | /** 14 | * ... comment ... 15 | */ 16 | ``` 17 | 18 | ## File Documentation 19 | 20 | Each file should have a file description. The marker `@file` can be used 21 | 22 | ``` 23 | /** @file file.h 24 | * @brief A brief file description. 25 | * 26 | * A more elaborated file description. 27 | */ 28 | ``` 29 | 30 | The file description should be inserted after the license statement. 31 | 32 | ## C++ Objects Documentation 33 | 34 | 1. The current plan is to put interface documentation in the .h file, 35 | and put detailed descriptions in the .cpp files. 36 | 2. There is no need to use `@class` marker. 37 | 3. There is no need to use a function comment block marker `@fn'`. 38 | * A function can also have `@param`, `@return`, and optionally `@sa`. 39 | It is useful to use `[in], [out], [in,out]` to specify the direction of 40 | a parameter. 41 | 4. For template functions, one can use '@tparam'. 42 | 5. Member variables can be documented as follows 43 | * short comment 44 | 45 | ``` 46 | int var; //!" kdu_compress error 12 | exit 1 13 | fi 14 | elif [ "$1" = "-enc" -o "$1" = "-renc" ]; then 15 | if ! ~/.wasmer/bin/wasmer run --llvm --dir .. --mapdir ./:./ --enable-all ../../bin/ojph_compress_simd.wasm -- $2; then 16 | echo "===========>" ojph_compress error 17 | exit 1 18 | fi 19 | else 20 | exit 1 21 | fi 22 | if ! kdu_expand $3; then 23 | echo "===========>" kdu_expand error 24 | exit 1 25 | fi 26 | if ! ~/.wasmer/bin/wasmer run --llvm --dir . --enable-all ../../bin/ojph_expand_simd.wasm -- $4; then 27 | echo "===========>" ojph_expand error 28 | exit 1 29 | fi 30 | 31 | # test PSNR and maximum error 32 | out1=$(../psnr_pae $5 $6) 33 | if [ $? -ne 0 ]; then 34 | echo "===========>" psnr_pae error at location 1 35 | exit 1 36 | fi 37 | out2=$(../psnr_pae $5 $7) 38 | if [ $? -ne 0 ]; then 39 | echo "===========>" psnr_pae error at location 2 40 | exit 1 41 | fi 42 | 43 | rm test.j2c test.jph $6 $7 44 | 45 | psnr1=$(echo $out1 | cut -f1 -d' ') 46 | psnr2=$(echo $out2 | cut -f1 -d' ') 47 | d1=$(bc -l <<< "$psnr1 - $psnr2") 48 | d1=${d1##*[+-]} 49 | 50 | pae1=$(echo $out1 | cut -f2 -d' ') 51 | pae2=$(echo $out2 | cut -f2 -d' ') 52 | d2=$(($pae1 - $pae2)) 53 | d2=${d2##*[+-]} 54 | 55 | if [ "$1" = "-renc" -o "$1" = "-rdec" ]; then 56 | if [ "$psnr1" = "inf" -a "$psnr2" = "inf" -a "$pae1" = "0" -a "$pae2" = "0" ]; then 57 | exit 0 58 | else 59 | echo "===========>" PSNR or PAE error 60 | echo psnr1 = $psnr1, psnr2 = $psnr2, pae1 = $pae1, pae2 = $pae2 61 | exit 1 62 | fi 63 | else 64 | t=$(bc -l <<< "$d1 > 0.01 || $d2 > 1") 65 | if [ $t -eq 0 ]; then 66 | exit 0 67 | else 68 | echo "===========>" PSNR or PAE error 69 | echo psnr1 = $psnr1, psnr2 = $psnr2, pae1 = $pae1, pae2 = $pae2 70 | exit 1 71 | fi 72 | fi 73 | -------------------------------------------------------------------------------- /src/core/common/ojph_version.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_base.h 34 | //***************************************************************************/ 35 | 36 | #define OPENJPH_VERSION_MAJOR 0 37 | #define OPENJPH_VERSION_MINOR 25 38 | #define OPENJPH_VERSION_PATCH 3 39 | -------------------------------------------------------------------------------- /src/apps/ojph_expand/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ## building ojph_expand 2 | ####################### 3 | 4 | file(GLOB OJPH_EXPAND "ojph_expand.cpp") 5 | file(GLOB OJPH_IMG_IO "../others/ojph_img_io.cpp") 6 | file(GLOB OJPH_IMG_IO_SSE4 "../others/ojph_img_io_sse41.cpp") 7 | file(GLOB OJPH_IMG_IO_AVX2 "../others/ojph_img_io_avx2.cpp") 8 | file(GLOB OJPH_IMG_IO_H "../common/ojph_img_io.h") 9 | 10 | list(APPEND SOURCES ${OJPH_EXPAND} ${OJPH_IMG_IO} ${OJPH_IMG_IO_H}) 11 | 12 | source_group("main" FILES ${OJPH_EXPAND}) 13 | source_group("others" FILES ${OJPH_IMG_IO}) 14 | source_group("common" FILES ${OJPH_IMG_IO_H}) 15 | 16 | if(EMSCRIPTEN) 17 | if (OJPH_ENABLE_WASM_SIMD) 18 | list(APPEND SOURCES ${OJPH_IMG_IO_SSE4}) 19 | source_group("others" FILES ${OJPH_IMG_IO_SSE4}) 20 | set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1) 21 | endif() 22 | else() 23 | if (NOT OJPH_DISABLE_SIMD) 24 | if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64") 25 | OR ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_I386") 26 | OR MULTI_GEN_X86_64) 27 | 28 | if (NOT OJPH_DISABLE_SSE4) 29 | list(APPEND SOURCES ${OJPH_IMG_IO_SSE4}) 30 | source_group("others" FILES ${OJPH_IMG_IO_SSE4}) 31 | endif() 32 | if (NOT OJPH_DISABLE_AVX2) 33 | list(APPEND SOURCES ${OJPH_IMG_IO_AVX2}) 34 | source_group("others" FILES ${OJPH_IMG_IO_AVX2}) 35 | endif() 36 | 37 | # Set compilation flags 38 | if (MSVC) 39 | set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2") 40 | else() 41 | set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1) 42 | set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2) 43 | endif() 44 | endif() 45 | 46 | if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_ARM") OR MULTI_GEN_ARM64) 47 | 48 | endif() 49 | 50 | endif() 51 | 52 | endif() 53 | 54 | add_executable(ojph_expand ${SOURCES}) 55 | target_include_directories(ojph_expand PRIVATE ../common) 56 | target_link_libraries(ojph_expand PRIVATE openjph $) 57 | 58 | install(TARGETS ojph_expand) 59 | -------------------------------------------------------------------------------- /src/apps/ojph_compress/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ## building ojph_compress 2 | ######################### 3 | 4 | file(GLOB OJPH_COMPRESS "ojph_compress.cpp") 5 | file(GLOB OJPH_IMG_IO "../others/ojph_img_io.cpp") 6 | file(GLOB OJPH_IMG_IO_SSE4 "../others/ojph_img_io_sse41.cpp") 7 | file(GLOB OJPH_IMG_IO_AVX2 "../others/ojph_img_io_avx2.cpp") 8 | file(GLOB OJPH_IMG_IO_H "../common/ojph_img_io.h") 9 | 10 | list(APPEND SOURCES ${OJPH_COMPRESS} ${OJPH_IMG_IO} ${OJPH_IMG_IO_H}) 11 | 12 | source_group("main" FILES ${OJPH_COMPRESS}) 13 | source_group("others" FILES ${OJPH_IMG_IO}) 14 | source_group("common" FILES ${OJPH_IMG_IO_H}) 15 | 16 | if(EMSCRIPTEN) 17 | if (OJPH_ENABLE_WASM_SIMD) 18 | list(APPEND SOURCES ${OJPH_IMG_IO_SSE4}) 19 | source_group("others" FILES ${OJPH_IMG_IO_SSE4}) 20 | set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1) 21 | endif() 22 | else() 23 | if (NOT OJPH_DISABLE_SIMD) 24 | if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64") 25 | OR ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_I386") 26 | OR MULTI_GEN_X86_64) 27 | 28 | if (NOT OJPH_DISABLE_SSE4) 29 | list(APPEND SOURCES ${OJPH_IMG_IO_SSE4}) 30 | source_group("others" FILES ${OJPH_IMG_IO_SSE4}) 31 | endif() 32 | if (NOT OJPH_DISABLE_AVX2) 33 | list(APPEND SOURCES ${OJPH_IMG_IO_AVX2}) 34 | source_group("others" FILES ${OJPH_IMG_IO_AVX2}) 35 | endif() 36 | 37 | # Set compilation flags 38 | if (MSVC) 39 | set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2") 40 | else() 41 | set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1) 42 | set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2) 43 | endif() 44 | endif() 45 | 46 | if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_ARM") OR MULTI_GEN_ARM64) 47 | 48 | endif() 49 | 50 | endif() 51 | 52 | endif() 53 | 54 | add_executable(ojph_compress ${SOURCES}) 55 | target_include_directories(ojph_compress PRIVATE ../common) 56 | target_link_libraries(ojph_compress PRIVATE openjph $) 57 | 58 | install(TARGETS ojph_compress) 59 | -------------------------------------------------------------------------------- /subprojects/js/standalone/com_decom_yuv.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export LD_LIBRARY_PATH=`pwd`/../:`\pwd`/../../bin 4 | export PATH=$PATH:`pwd`/../:`\pwd`/../../bin 5 | 6 | pwd 7 | 8 | # compress and decompress the images 9 | if [ "$1" = "-dec" -o "$1" = "-rdec" ]; then 10 | if ! kdu_compress $2; then 11 | echo "===========>" kdu_compress error 12 | exit 1 13 | fi 14 | elif [ "$1" = "-enc" -o "$1" = "-renc" ]; then 15 | if ! ~/.wasmer/bin/wasmer run --llvm --dir=.. --mapdir ./:./ --enable-all ../../bin/ojph_compress_simd.wasm -- $2; then 16 | echo "===========>" ojph_compress error 17 | exit 1 18 | fi 19 | else 20 | exit 1 21 | fi 22 | 23 | if ! kdu_expand $3; then 24 | echo "===========>" kdu_expand error 25 | exit 1 26 | fi 27 | cat test1y.rawl test1u.rawl test1v.rawl > test1.yuv 28 | if ! ~/.wasmer/bin/wasmer run --llvm --dir=. --enable-all ../../bin/ojph_expand_simd.wasm -- $4; then 29 | echo "===========>" ojph_expand error 30 | exit 1 31 | fi 32 | exit 0 33 | # test PSNR and maximum error 34 | out1=$(../psnr_pae $5 $6) 35 | if [ $? -ne 0 ]; then 36 | echo "===========>" psnr_pae error at location 1 37 | exit 1 38 | fi 39 | out2=$(../psnr_pae $5 $7) 40 | if [ $? -ne 0 ]; then 41 | echo "===========>" psnr_pae error at location 2 42 | exit 1 43 | fi 44 | 45 | rm test1y.rawl test1u.rawl test1v.rawl 46 | rm test.j2c test.jph 47 | rm $(echo $6 | cut -f1 -d':') 48 | rm $(echo $7 | cut -f1 -d':') 49 | 50 | psnr1=$(echo $out1 | cut -f1 -d' ') 51 | psnr2=$(echo $out2 | cut -f1 -d' ') 52 | d1=$(bc -l <<< "$psnr1 - $psnr2") 53 | d1=${d1##*[+-]} 54 | 55 | pae1=$(echo $out1 | cut -f2 -d' ') 56 | pae2=$(echo $out2 | cut -f2 -d' ') 57 | d2=$(($pae1 - $pae2)) 58 | d2=${d2##*[+-]} 59 | 60 | if [ "$1" = "-renc" -o "$1" = "-rdec" ]; then 61 | if [ "$psnr1" = "inf" -a "$psnr2" = "inf" -a "$pae1" = "0" -a "$pae2" = "0" ]; then 62 | exit 0 63 | else 64 | echo "===========>" PSNR or PAE error 65 | echo psnr1 = $psnr1, psnr2 = $psnr2, pae1 = $pae1, pae2 = $pae2 66 | exit 1 67 | fi 68 | else 69 | t=$(bc -l <<< "$d1 > 0.01 || $d2 > 1") 70 | if [ $t -eq 0 ]; then 71 | exit 0 72 | else 73 | echo "===========>" PSNR or PAE error 74 | echo psnr1 = $psnr1, psnr2 = $psnr2, pae1 = $pae1, pae2 = $pae2 75 | exit 1 76 | fi 77 | fi -------------------------------------------------------------------------------- /src/core/coding/ojph_block_common.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2022, Aous Naman 6 | // Copyright (c) 2022, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2022, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_block_common.h 34 | // Author: Aous Naman 35 | // Date: 13 May 2022 36 | //***************************************************************************/ 37 | 38 | #include "ojph_defs.h" 39 | 40 | namespace ojph{ 41 | namespace local { 42 | 43 | extern ui16 vlc_tbl0[1024]; 44 | extern ui16 vlc_tbl1[1024]; 45 | extern ui16 uvlc_tbl0[256+64]; 46 | extern ui16 uvlc_tbl1[256]; 47 | extern ui8 uvlc_bias[256+64]; 48 | } // !namespace local 49 | } // !namespace ojph 50 | -------------------------------------------------------------------------------- /tests/compare_files.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | //////////////////////////////////////////////////////////////////////////////// 6 | static inline 7 | int getc_and_check(FILE *f, const char* filename) 8 | { 9 | int c; 10 | c = getc(f); 11 | if (feof(f)) { 12 | fprintf(stderr, "Error while reading a comment from %s\n", filename); 13 | exit(-1); 14 | } 15 | return c; 16 | } 17 | 18 | //////////////////////////////////////////////////////////////////////////////// 19 | static inline 20 | void eat_comments(FILE *f, const char* filename, int& c) noexcept 21 | { 22 | int length = 0; 23 | length = getc_and_check(f, filename) << 8; 24 | length |= getc_and_check(f, filename); 25 | length -= 2; 26 | 27 | for (int i = 0; i < length; ++i) 28 | c = getc_and_check(f, filename); 29 | } 30 | 31 | 32 | //////////////////////////////////////////////////////////////////////////////// 33 | int main(int argc, char* argv[]) 34 | { 35 | if (argc < 3) 36 | { 37 | fprintf(stderr, 38 | "compare_files expects two arguments \n"); 39 | exit(-1); 40 | } 41 | 42 | FILE *f1 = fopen(argv[1], "rb"); 43 | if (f1 == NULL) 44 | { 45 | fprintf(stderr, "Unable to open file %s.\n", argv[1]); 46 | return -1; 47 | } 48 | 49 | FILE *f2 = fopen(argv[2], "rb"); 50 | if (f2 == NULL) 51 | { 52 | fprintf(stderr, "Unable to open file %s.\n", argv[2]); 53 | return -1; 54 | } 55 | 56 | bool tile_started = false; 57 | int old_c1 = ' '; 58 | while (1) // both files must end at the same time 59 | { 60 | int c1 = getc(f1); 61 | int c2 = getc(f2); 62 | 63 | bool eof1 = (feof(f1) != 0), eof2 = (feof(f2) != 0); 64 | 65 | if (eof1 && eof2) // both reached end of file 66 | { 67 | fprintf(stdout, "Matching files.\n"); 68 | return 0; 69 | } 70 | else if (!eof1 && !eof2) 71 | { 72 | if (c1 != c2) 73 | return -1; 74 | if (!tile_started && old_c1 == 0xFF && c1 == 0x64) { 75 | eat_comments(f1, argv[1], c1); 76 | eat_comments(f2, argv[2], c2); 77 | } 78 | if (!tile_started && old_c1 == 0xFF && c1 == 0x90) { 79 | // stop checking comments when a tile starts; we ignoring 80 | // the case where tile can also have comments 81 | tile_started = true; 82 | } 83 | old_c1 = c1; 84 | } 85 | else // only one of them reached end of file 86 | { 87 | fprintf(stderr, "One file finished before the other one.\n"); 88 | return -1; 89 | } 90 | } 91 | 92 | fclose(f1); 93 | fclose(f2); 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /src/core/codestream/ojph_codestream_sse.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2022, Aous Naman 6 | // Copyright (c) 2022, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2022, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codestream_sse.cpp 34 | // Author: Aous Naman 35 | // Date: 15 May 2022 36 | //***************************************************************************/ 37 | 38 | #include "ojph_arch.h" 39 | #if defined(OJPH_ARCH_I386) || defined(OJPH_ARCH_X86_64) 40 | 41 | #include 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | namespace local { 46 | 47 | ////////////////////////////////////////////////////////////////////////// 48 | void sse_mem_clear(void* addr, size_t count) 49 | { 50 | float* p = (float*)addr; 51 | __m128 zero = _mm_setzero_ps(); 52 | for (size_t i = 0; i < count; i += 16, p += 4) 53 | _mm_storeu_ps(p, zero); 54 | } 55 | } 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_codestream_avx.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2022, Aous Naman 6 | // Copyright (c) 2022, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2022, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codestream_avx.cpp 34 | // Author: Aous Naman 35 | // Date: 15 May 2022 36 | //***************************************************************************/ 37 | 38 | #include "ojph_arch.h" 39 | #if defined(OJPH_ARCH_I386) || defined(OJPH_ARCH_X86_64) 40 | #include 41 | #include "ojph_defs.h" 42 | 43 | namespace ojph { 44 | namespace local { 45 | 46 | ////////////////////////////////////////////////////////////////////////// 47 | void avx_mem_clear(void* addr, size_t count) 48 | { 49 | float* p = (float*)addr; 50 | __m256 zero = _mm256_setzero_ps(); 51 | for (size_t i = 0; i < count; i += 32, p += 8) 52 | _mm256_storeu_ps(p, zero); 53 | } 54 | 55 | } 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/apps/ojph_stream_expand/threaded_frame_processors.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2024, Aous Naman 6 | // Copyright (c) 2024, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2024, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: threaded_frame_processors.cpp 34 | // Author: Aous Naman 35 | // Date: 23 April 2024 36 | //***************************************************************************/ 37 | 38 | #include "threaded_frame_processors.h" 39 | 40 | namespace ojph 41 | { 42 | namespace stex 43 | { 44 | 45 | /////////////////////////////////////////////////////////////////////////////// 46 | // 47 | // 48 | // 49 | // 50 | // 51 | /////////////////////////////////////////////////////////////////////////////// 52 | 53 | void j2k_frame_storer::execute() 54 | { 55 | //printf("saving file with index %d\n", file->frame_idx); 56 | char buf[128], name[128]; 57 | snprintf(buf, 128, "%s.j2c", file->name_template); 58 | snprintf(name, 128, buf, file->frame_idx); 59 | file->f.write_to_file(name); 60 | file->notify_file_completion(); 61 | } 62 | 63 | } // !stex namespace 64 | } // !ojph namespace -------------------------------------------------------------------------------- /src/core/common/ojph_base.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_base.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_BASE_H 40 | #define OJPH_BASE_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | 46 | ///////////////////////////////////////////////////////////////////////////// 47 | struct size 48 | { 49 | explicit size(ui32 w = 0, ui32 h = 0) : w(w), h(h) {} 50 | ui32 w; //width 51 | ui32 h; //height 52 | 53 | ui64 area() const { return (ui64)w * (ui64)h; } 54 | }; 55 | 56 | ///////////////////////////////////////////////////////////////////////////// 57 | struct point 58 | { 59 | explicit point(ui32 x = 0, ui32 y = 0) : x(x), y(y) {} 60 | ui32 x, y; 61 | }; 62 | 63 | ///////////////////////////////////////////////////////////////////////////// 64 | struct rect 65 | { 66 | point org; 67 | size siz; 68 | }; 69 | 70 | } 71 | 72 | #endif // !OJPH_BASE_H 73 | -------------------------------------------------------------------------------- /target_arch.cmake: -------------------------------------------------------------------------------- 1 | # This is to detect the target architecture. 2 | # The detection relies on the compiler's "#error" preprocessor directive to emit the architecture. 3 | 4 | # This is inspired by https://github.com/axr/solar-cmake/blob/master/TargetArch.cmake 5 | # which is inspired by 6 | # https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h 7 | 8 | set(archdetect_c_code " 9 | #if defined(__arm__) || defined(__TARGET_ARCH_ARM) \ 10 | || defined(__aarch64__) || defined(_M_ARM64) 11 | #error cmake_ARCH OJPH_ARCH_ARM 12 | #elif defined(__i386) || defined(__i386__) || defined(_M_IX86) 13 | #error cmake_ARCH OJPH_ARCH_I386 14 | #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) 15 | #error cmake_ARCH OJPH_ARCH_X86_64 16 | #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) 17 | #error cmake_ARCH OJPH_ARCH_IA64 18 | #elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\ 19 | || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\ 20 | || defined(_M_MPPC) || defined(_M_PPC) 21 | #if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__) 22 | #error cmake_ARCH OJPH_ARCH_PPC64 23 | #else 24 | #error cmake_ARCH OJPH_ARCH_PPC 25 | #endif 26 | #endif 27 | 28 | #error cmake_ARCH OJPH_ARCH_UNKNOWN 29 | ") 30 | 31 | function(target_architecture output_var) 32 | 33 | file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}") 34 | 35 | enable_language(C) 36 | 37 | # Detect the architecture in a rather creative way... 38 | # This compiles a small C program which is a series of ifdefs that selects a 39 | # particular #error preprocessor directive whose message string contains the 40 | # target architecture. The program will always fail to compile (both because 41 | # file is not a valid C program, and obviously because of the presence of the 42 | # #error preprocessor directives... but by exploiting the preprocessor in this 43 | # way, we can detect the correct target architecture even when cross-compiling, 44 | # since the program itself never needs to be run (only the compiler/preprocessor) 45 | try_run( 46 | run_result_unused 47 | compile_result_unused 48 | "${CMAKE_BINARY_DIR}" 49 | "${CMAKE_BINARY_DIR}/arch.c" 50 | COMPILE_OUTPUT_VARIABLE ARCH 51 | ) 52 | 53 | # Parse the architecture name from the compiler output 54 | string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}") 55 | 56 | # Get rid of the value marker leaving just the architecture name 57 | string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}") 58 | 59 | # If we are compiling with an unknown architecture this variable should 60 | # already be set to "unknown" but in the case that it's empty (i.e. due 61 | # to a typo in the code), then set it to unknown 62 | if (NOT ARCH) 63 | set(ARCH OJPH_ARCH_UNKNOWN) 64 | endif() 65 | 66 | set(${output_var} "${ARCH}" PARENT_SCOPE) 67 | 68 | endfunction() -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # Fetch googletest 3 | include(FetchContent) 4 | FetchContent_Declare( 5 | googletest 6 | URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz 7 | EXCLUDE_FROM_ALL 8 | ) 9 | # For Windows: Prevent overriding the parent project's compiler/linker settings 10 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 11 | set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) 12 | option(INSTALL_GTEST "Enable installation of googletest." OFF) 13 | FetchContent_MakeAvailable(googletest) 14 | 15 | # Fetch test files 16 | FetchContent_Declare( 17 | jp2k_test_codestreams 18 | URL https://github.com/aous72/jp2k_test_codestreams/archive/refs/heads/main.zip 19 | SOURCE_DIR jp2k_test_codestreams/ 20 | ) 21 | FetchContent_MakeAvailable(jp2k_test_codestreams) 22 | 23 | # create the mse_pae executable 24 | include(mse_pae.cmake) 25 | 26 | # create the compare_files executable 27 | add_executable( 28 | compare_files 29 | compare_files.cpp 30 | ) 31 | 32 | # configure test executables 33 | add_executable( 34 | test_executables 35 | test_executables.cpp 36 | ) 37 | 38 | target_link_libraries( 39 | test_executables 40 | GTest::gtest_main 41 | ) 42 | 43 | include(GoogleTest) 44 | gtest_add_tests(TARGET test_executables) 45 | 46 | if (MSVC) 47 | add_custom_command(TARGET test_executables POST_BUILD 48 | COMMAND ${CMAKE_COMMAND} -E copy "../bin/\$(Configuration)/gtest.dll" "./" 49 | COMMAND ${CMAKE_COMMAND} -E copy "../bin/\$(Configuration)/gtest_main.dll" "./" 50 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 51 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 52 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 53 | ) 54 | add_custom_command(TARGET compare_files POST_BUILD 55 | COMMAND ${CMAKE_COMMAND} -E copy "./\$(Configuration)/compare_files.exe" "./" 56 | ) 57 | add_custom_command(TARGET mse_pae POST_BUILD 58 | COMMAND ${CMAKE_COMMAND} -E copy "./\$(Configuration)/mse_pae.exe" "./" 59 | ) 60 | if (OJPH_ENABLE_TIFF_SUPPORT) 61 | file(COPY "${TIFF_INCLUDE_DIR}\\..\\bin\\tiff.dll" DESTINATION "./") 62 | file(COPY "${TIFF_INCLUDE_DIR}\\..\\bin\\tiffxx.dll" DESTINATION "./") 63 | file(COPY "${TIFF_INCLUDE_DIR}\\..\\bin\\tiffd.dll" DESTINATION "./") 64 | file(COPY "${TIFF_INCLUDE_DIR}\\..\\bin\\tiffxxd.dll" DESTINATION "./") 65 | endif() 66 | else() 67 | add_custom_command(TARGET test_executables POST_BUILD 68 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 69 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 70 | ) 71 | if(EMSCRIPTEN) 72 | add_custom_command(TARGET test_executables POST_BUILD 73 | COMMAND ${CMAKE_COMMAND} -E copy "$/ojph_expand.wasm" "./" 74 | COMMAND ${CMAKE_COMMAND} -E copy "$/ojph_compress.wasm" "./" 75 | ) 76 | endif(EMSCRIPTEN) 77 | if(CYGWIN OR MINGW) 78 | add_custom_command(TARGET test_executables POST_BUILD 79 | COMMAND ${CMAKE_COMMAND} -E copy "../bin/${CMAKE_SHARED_LIBRARY_PREFIX}gtest.dll" "./" 80 | COMMAND ${CMAKE_COMMAND} -E copy "../bin/${CMAKE_SHARED_LIBRARY_PREFIX}gtest_main.dll" "./" 81 | COMMAND ${CMAKE_COMMAND} -E copy "$" "./" 82 | ) 83 | endif() 84 | endif(MSVC) 85 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_precinct.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_precinct.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_PRECINCT_H 40 | #define OJPH_PRECINCT_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | 46 | //////////////////////////////////////////////////////////////////////////// 47 | //defined elsewhere 48 | class mem_elastic_allocator; 49 | struct coded_lists; 50 | 51 | namespace local { 52 | 53 | ////////////////////////////////////////////////////////////////////////// 54 | //defined here 55 | class subband; 56 | 57 | ////////////////////////////////////////////////////////////////////////// 58 | struct precinct 59 | { 60 | precinct() { 61 | scratch = NULL; bands = NULL; coded = NULL; 62 | may_use_sop = uses_eph = false; 63 | } 64 | ui32 prepare_precinct(int tag_tree_size, ui32* lev_idx, 65 | mem_elastic_allocator *elastic); 66 | void write(outfile_base *file); 67 | void parse(int tag_tree_size, ui32* lev_idx, 68 | mem_elastic_allocator *elastic, 69 | ui32& data_left, infile_base *file, bool skipped); 70 | 71 | ui8 *scratch; 72 | point img_point; //the precinct projected to full resolution 73 | rect cb_idxs[4]; //indices of codeblocks 74 | subband *bands; //the subbands 75 | coded_lists* coded; 76 | bool may_use_sop, uses_eph; 77 | }; 78 | 79 | } 80 | } 81 | 82 | #endif // !OJPH_PRECINCT_H -------------------------------------------------------------------------------- /src/core/common/ojph_defs.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_defs.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_TYPES_H 40 | #define OJPH_TYPES_H 41 | 42 | #include 43 | #include "ojph_version.h" 44 | 45 | namespace ojph { 46 | 47 | ///////////////////////////////////////////////////////////////////////////// 48 | // types 49 | ///////////////////////////////////////////////////////////////////////////// 50 | typedef uint8_t ui8; 51 | typedef int8_t si8; 52 | typedef uint16_t ui16; 53 | typedef int16_t si16; 54 | typedef uint32_t ui32; 55 | typedef int32_t si32; 56 | typedef uint64_t ui64; 57 | typedef int64_t si64; 58 | 59 | ///////////////////////////////////////////////////////////////////////////// 60 | #define OJPH_INT_STRINGIFY(I) #I 61 | #define OJPH_INT_TO_STRING(I) OJPH_INT_STRINGIFY(I) 62 | 63 | ///////////////////////////////////////////////////////////////////////////// 64 | // number of fractional bits for 16 bit representation 65 | // for 32 bits, it is NUM_FRAC_BITS + 16 66 | // All numbers are in the range of [-0.5, 0.5) 67 | const int NUM_FRAC_BITS = 13; 68 | 69 | ///////////////////////////////////////////////////////////////////////////// 70 | #define ojph_div_ceil(a, b) (((a) + (b) - 1) / (b)) 71 | 72 | ///////////////////////////////////////////////////////////////////////////// 73 | #define ojph_max(a, b) (((a) > (b)) ? (a) : (b)) 74 | 75 | ///////////////////////////////////////////////////////////////////////////// 76 | #define ojph_min(a, b) (((a) < (b)) ? (a) : (b)) 77 | 78 | #define ojph_unused(x) (void)(x) 79 | 80 | 81 | } 82 | 83 | #endif // !OJPH_TYPES_H 84 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ "master" ] 20 | schedule: 21 | - cron: '39 20 * * 2' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | # Runner size impacts CodeQL analysis time. To learn more, please see: 27 | # - https://gh.io/recommended-hardware-resources-for-running-codeql 28 | # - https://gh.io/supported-runners-and-hardware-resources 29 | # - https://gh.io/using-larger-runners 30 | # Consider using larger runners for possible analysis time improvements. 31 | runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} 32 | timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} 33 | permissions: 34 | actions: read 35 | contents: read 36 | security-events: write 37 | 38 | strategy: 39 | fail-fast: false 40 | matrix: 41 | language: [ 'cpp' ] 42 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ] 43 | # Use only 'java' to analyze code written in Java, Kotlin or both 44 | # Use only 'javascript' to analyze code written in JavaScript, TypeScript or both 45 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 46 | 47 | steps: 48 | - name: Checkout repository 49 | uses: actions/checkout@v4 50 | 51 | # Initializes the CodeQL tools for scanning. 52 | - name: Initialize CodeQL 53 | uses: github/codeql-action/init@v4 54 | with: 55 | languages: ${{ matrix.language }} 56 | # If you wish to specify custom queries, you can do so here or in a config file. 57 | # By default, queries listed here will override any specified in a config file. 58 | # Prefix the list here with "+" to use these queries and those in the config file. 59 | 60 | # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 61 | # queries: security-extended,security-and-quality 62 | 63 | 64 | # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). 65 | # If this step fails, then you should remove it and run the build manually (see below) 66 | - name: Autobuild 67 | uses: github/codeql-action/autobuild@v4 68 | 69 | # ℹ️ Command-line programs to run using the OS shell. 70 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 71 | 72 | # If the Autobuild fails above, remove it and uncomment the following three lines. 73 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 74 | 75 | # - run: | 76 | # echo "Run, Build Application using script" 77 | # ./location_of_script_within_repo/buildscript.sh 78 | 79 | - name: Perform CodeQL Analysis 80 | uses: github/codeql-action/analyze@v4 81 | with: 82 | category: "/language:${{matrix.language}}" 83 | -------------------------------------------------------------------------------- /src/core/coding/ojph_block_decoder.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_block_decoder.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_BLOCK_DECODER_H 40 | #define OJPH_BLOCK_DECODER_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | namespace local { 46 | 47 | ////////////////////////////////////////////////////////////////////////// 48 | //decodes the cleanup pass, significance propagation pass, 49 | // and magnitude refinement pass 50 | 51 | // generic decoder 52 | bool 53 | ojph_decode_codeblock32(ui8* coded_data, ui32* decoded_data, 54 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 55 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 56 | 57 | bool 58 | ojph_decode_codeblock64(ui8* coded_data, ui64* decoded_data, 59 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 60 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 61 | 62 | // SSSE3-accelerated decoder 63 | bool 64 | ojph_decode_codeblock_ssse3(ui8* coded_data, ui32* decoded_data, 65 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 66 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 67 | 68 | // AVX2-accelerated decoder 69 | bool 70 | ojph_decode_codeblock_avx2(ui8* coded_data, ui32* decoded_data, 71 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 72 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 73 | 74 | // WASM SIMD-accelerated decoder 75 | bool 76 | ojph_decode_codeblock_wasm(ui8* coded_data, ui32* decoded_data, 77 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 78 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 79 | 80 | } 81 | } 82 | 83 | #endif // !OJPH_BLOCK_DECODER_H 84 | -------------------------------------------------------------------------------- /subprojects/js/standalone/README.md: -------------------------------------------------------------------------------- 1 | # How to compile to standalone WASM file 2 | 3 | Similar to how java code is compiled and run using the java executable (java.exe) which creates the runtime environment (RTE), C++ can be compiled to WASM and run from a command line that creates an RTE for WASM. 4 | 5 | To achieve this, we need to have the tools to compile the C++ code to wasm, and we need a runtime environment. Here, we will focus on the wasi-sdk and wasmer. 6 | 7 | ## Compilation 8 | 9 | The [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) provides the tools to compile the code to WASM; in particular, it provides the clang compiler, necessary C++ header files, sysroot directory, etc. You can download the latest release from Releases (as of this writing, it is [here](https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-15)). A separate package, [wasienv](https://github.com/wasienv/wasienv), is supposed to make it easier to use wasi-sdk with cmake; as of this writing wasienv does not include the latest release of wasi-sdk, and it feels like the project is left is disrepair -- last update is more than a year ago, but it might worth giving it a crack, adapting some of its code. 10 | 11 | We hope that wasienv is update in the coming months. 12 | 13 | To compile, I am using 14 | 15 | /home/aous/wasi-sdk-15.0/bin/clang --sysroot=/home/aous/wasi-sdk-15.0/share/wasi-sysroot/ ... 16 | 17 | 18 | It is worth nothing that as of this writing wasi-sdk and wasmer do not support C++ exceptions. This is a complication, because it requires the user to remove all try, throw, and catch statements. It also requires modifying ```new``` to ```new (std::nothrow)```, which requires the addition of ```#include ```. This situation is expected be resolved in the coming months because there is a standard for WASM exceptions; these can be activated by passing the clang commandline flag ```-fwasm-exceptions```. 19 | 20 | This folder has the ```build.sh``` script; put this script in the ```OpenJPH/bin``` folder, and use it to build ojph_expand.wasm, ojph_expand_simd.wasm, ojph_compress.wasm, and ojph_compress_simd.wasm, after removing the offending exception generating code (hopefully this is a temporary solution). Perhaps, it is useful to have a separate tree for this modified code. 21 | 22 | ## Runtime Environment (RTE) 23 | 24 | For RTE, There are two executables that I am aware of, wasmtime and wasmer. The former is from a nonprofit organization while the latter from a startup. The authors of wasmer claim that their implementation is faster -- I will go with this. The wasmer RTE, which installs by default to ```.wasmer```, can obtained from https://github.com/wasmerio/wasmer. To install, use 25 | 26 | curl https://get.wasmer.io -sSfL | sh 27 | 28 | 29 | To run I am using 30 | 31 | ~/.wasmer/bin/wasmer run --dir=. --enable-all helloworld.wasm -- arguments 32 | 33 | Of course, you can include this in the execution path. 34 | 35 | ## Testing the WASM code 36 | 37 | To test the WASM subroutines, I would like to use the same test subroutines for the library. To do so, move the com_decom.sh and com_decom_yuv.sh files in this folder to ```OpenJPH/tests``` folder, and build the library as usual using 38 | 39 | cd build 40 | cmake -DBUILD_TESTING=yes .. 41 | make 42 | make test 43 | 44 | This is a standard build without any modifications. The modified com_decom.sh and com_decom_yuv.sh files should invoke the WASM versions of the code. You can modify these files to test with or without WASM SIMD. 45 | 46 | There is a small complication with the test. The test reads uncompressed images from ```..\```, and writes compressed images to ```.\```, while wasmer has access to ```..``` only with the command ```--dir ..```. It would be convenient if I can specify two folders, but I do not know how to do that. To overcome this, I also use ```--mapdir ./:./``` that maps the ```.\``` folder to the same location in wasmer, thus giving wasmer access to this folder. 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/apps/ojph_stream_expand/threaded_frame_processors.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2024, Aous Naman 6 | // Copyright (c) 2024, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2024, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: threaded_frame_processors.h 34 | // Author: Aous Naman 35 | // Date: 23 April 2024 36 | //***************************************************************************/ 37 | 38 | #ifndef THREADED_FRAME_PROCESSOR_H 39 | #define THREADED_FRAME_PROCESSOR_H 40 | 41 | #include "ojph_threads.h" 42 | #include "stream_expand_support.h" 43 | 44 | namespace ojph 45 | { 46 | namespace thds 47 | { class thread_pool; } 48 | 49 | namespace stex 50 | { 51 | 52 | /////////////////////////////////////////////////////////////////////////////// 53 | // 54 | // 55 | // 56 | // 57 | // 58 | /////////////////////////////////////////////////////////////////////////////// 59 | 60 | /*****************************************************************************/ 61 | /** @brief Saves a j2k frame to disk without decoding. 62 | * 63 | */ 64 | struct j2k_frame_storer : public thds::worker_thread_base 65 | { 66 | public: 67 | /** 68 | * @brief default construction 69 | */ 70 | j2k_frame_storer() { 71 | file = NULL; 72 | name_template = NULL; 73 | } 74 | /** 75 | * @brief default destructor doing nothing 76 | */ 77 | ~j2k_frame_storer() override {} 78 | 79 | public: 80 | /** 81 | * @brief call this function to initialize its members 82 | * 83 | * @param file is a stex_file holding the j2k codestream with other 84 | * variables. 85 | * @param name_template holds the a filename template 86 | */ 87 | void init(stex_file* file, const char* name_template) 88 | { 89 | this->file = file; 90 | this->name_template = name_template; 91 | } 92 | 93 | /** 94 | * @brief A thread from the thread_pool call this function to execute 95 | * the task 96 | */ 97 | void execute() override; 98 | 99 | private: 100 | stex_file* file; //! lock(tp->mutex); 88 | // wait releases the mutex, blocks until notified (or spuriously), 89 | // and acquire the mutex 90 | tp->condition.wait(lock); 91 | 92 | if(tp->stop.load(std::memory_order_acquire)) 93 | return; 94 | 95 | worker_thread_base* task = NULL; 96 | if (!tp->tasks.empty()) 97 | { 98 | task = tp->tasks.front(); 99 | tp->tasks.pop_front(); 100 | } 101 | lock.unlock(); 102 | if (task) 103 | task->execute(); 104 | } 105 | } 106 | 107 | } // !thds namespace 108 | } // !ojph namespace -------------------------------------------------------------------------------- /src/core/codestream/ojph_tile_comp.h: -------------------------------------------------------------------------------- 1 | 2 | //***************************************************************************/ 3 | // This software is released under the 2-Clause BSD license, included 4 | // below. 5 | // 6 | // Copyright (c) 2019, Aous Naman 7 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 8 | // Copyright (c) 2019, The University of New South Wales, Australia 9 | // 10 | // Redistribution and use in source and binary forms, with or without 11 | // modification, are permitted provided that the following conditions are 12 | // met: 13 | // 14 | // 1. Redistributions of source code must retain the above copyright 15 | // notice, this list of conditions and the following disclaimer. 16 | // 17 | // 2. Redistributions in binary form must reproduce the above copyright 18 | // notice, this list of conditions and the following disclaimer in the 19 | // documentation and/or other materials provided with the distribution. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | //***************************************************************************/ 33 | // This file is part of the OpenJPH software implementation. 34 | // File: ojph_tile_comp.h 35 | // Author: Aous Naman 36 | // Date: 28 August 2019 37 | //***************************************************************************/ 38 | 39 | 40 | #ifndef OJPH_TILE_COMP_H 41 | #define OJPH_TILE_COMP_H 42 | 43 | #include "ojph_defs.h" 44 | #include "ojph_base.h" 45 | #include "ojph_file.h" 46 | 47 | namespace ojph { 48 | 49 | //////////////////////////////////////////////////////////////////////////// 50 | //defined elsewhere 51 | class line_buf; 52 | class codestream; 53 | 54 | namespace local { 55 | 56 | ////////////////////////////////////////////////////////////////////////// 57 | //defined here 58 | class tile; 59 | class resolution; 60 | 61 | ////////////////////////////////////////////////////////////////////////// 62 | class tile_comp 63 | { 64 | public: 65 | static void pre_alloc(codestream *codestream, ui32 comp_num, 66 | const rect& comp_rect, 67 | const rect& recon_comp_rect); 68 | void finalize_alloc(codestream *codestream, tile *parent, 69 | ui32 comp_num, const rect& comp_rect, 70 | const rect& recon_comp_rect); 71 | 72 | ui32 get_num_resolutions() { return num_decomps + 1; } 73 | ui32 get_num_decompositions() { return num_decomps; } 74 | tile* get_tile() { return parent_tile; } 75 | line_buf* get_line(); 76 | void push_line(); 77 | line_buf* pull_line(); 78 | 79 | ui32 prepare_precincts(); 80 | void write_precincts(ui32 res_num, outfile_base *file); 81 | bool get_top_left_precinct(ui32 res_num, point &top_left); 82 | void write_one_precinct(ui32 res_num, outfile_base *file); 83 | void parse_precincts(ui32 res_num, ui32& data_left, infile_base *file); 84 | void parse_one_precinct(ui32 res_num, ui32& data_left, 85 | infile_base *file); 86 | 87 | ui32 get_num_bytes() const { return num_bytes; } 88 | ui32 get_num_bytes(ui32 resolution_num) const; 89 | 90 | private: 91 | tile *parent_tile; 92 | resolution *res; 93 | rect comp_rect; 94 | ojph::point comp_downsamp; 95 | ui32 num_decomps; 96 | ui32 comp_num; 97 | ui32 num_bytes; // number of bytes in this tile component 98 | // used for tilepart length 99 | }; 100 | 101 | } 102 | } 103 | 104 | #endif // !OJPH_TILE_COMP_H -------------------------------------------------------------------------------- /src/core/codestream/ojph_tile.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_tile.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_TILE_H 40 | #define OJPH_TILE_H 41 | 42 | #include "ojph_defs.h" 43 | #include "ojph_file.h" 44 | #include "ojph_params_local.h" 45 | 46 | namespace ojph { 47 | 48 | //////////////////////////////////////////////////////////////////////////// 49 | //defined elsewhere 50 | class line_buf; 51 | class codestream; 52 | 53 | namespace local { 54 | 55 | ////////////////////////////////////////////////////////////////////////// 56 | //defined here 57 | class tile_comp; 58 | 59 | ////////////////////////////////////////////////////////////////////////// 60 | class tile 61 | { 62 | public: 63 | static void pre_alloc(codestream *codestream, const rect& tile_rect, 64 | const rect& recon_tile_rect, ui32 &num_tileparts); 65 | void finalize_alloc(codestream *codestream, const rect& tile_rect, 66 | ui32 tile_idx, ui32& offset, ui32 &num_tileparts); 67 | 68 | bool push(line_buf *line, ui32 comp_num); 69 | void prepare_for_flush(); 70 | void fill_tlm(param_tlm* tlm); 71 | void flush(outfile_base *file); 72 | void parse_tile_header(const param_sot& sot, infile_base *file, 73 | const ui64& tile_start_location); 74 | bool pull(line_buf *, ui32 comp_num); 75 | rect get_tile_rect() { return tile_rect; } 76 | 77 | private: 78 | //codestream *parent; 79 | rect tile_rect; 80 | ui32 num_comps; 81 | tile_comp *comps; 82 | ui32 num_lines; 83 | line_buf* lines; 84 | bool employ_color_transform, resilient; 85 | bool *reversible; 86 | rect *comp_rects, *recon_comp_rects; 87 | ui32 *line_offsets; 88 | ui32 skipped_res_for_read; 89 | 90 | ui32 *num_bits; 91 | bool *is_signed; 92 | ui32 *cur_line; 93 | ui8 *nlt_type3; 94 | int prog_order; 95 | 96 | private: 97 | param_sot sot; 98 | int next_tile_part; 99 | 100 | private: 101 | int profile; 102 | ui32 tilepart_div; // tilepart division value 103 | bool need_tlm; // true if tlm markers are needed 104 | 105 | ui32 num_bytes; // number of bytes in this tile 106 | // used for tile length 107 | }; 108 | 109 | } 110 | } 111 | 112 | #endif // !OJPH_TILE_H -------------------------------------------------------------------------------- /src/core/transform/ojph_colour_sse.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_colour_sse.cpp 34 | // Author: Aous Naman 35 | // Date: 11 October 2019 36 | //***************************************************************************/ 37 | 38 | #include "ojph_arch.h" 39 | #if defined(OJPH_ARCH_I386) || defined(OJPH_ARCH_X86_64) 40 | 41 | #include 42 | 43 | #include "ojph_defs.h" 44 | #include "ojph_colour.h" 45 | #include "ojph_colour_local.h" 46 | 47 | #include 48 | 49 | namespace ojph { 50 | namespace local { 51 | 52 | ////////////////////////////////////////////////////////////////////////// 53 | void sse_ict_forward(const float *r, const float *g, const float *b, 54 | float *y, float *cb, float *cr, ui32 repeat) 55 | { 56 | __m128 alpha_rf = _mm_set1_ps(CT_CNST::ALPHA_RF); 57 | __m128 alpha_gf = _mm_set1_ps(CT_CNST::ALPHA_GF); 58 | __m128 alpha_bf = _mm_set1_ps(CT_CNST::ALPHA_BF); 59 | __m128 beta_cbf = _mm_set1_ps(CT_CNST::BETA_CbF); 60 | __m128 beta_crf = _mm_set1_ps(CT_CNST::BETA_CrF); 61 | for (ui32 i = (repeat + 3) >> 2; i > 0; --i) 62 | { 63 | __m128 mr = _mm_load_ps(r); 64 | __m128 mb = _mm_load_ps(b); 65 | __m128 my = _mm_mul_ps(alpha_rf, mr); 66 | my = _mm_add_ps(my, _mm_mul_ps(alpha_gf, _mm_load_ps(g))); 67 | my = _mm_add_ps(my, _mm_mul_ps(alpha_bf, mb)); 68 | _mm_store_ps(y, my); 69 | _mm_store_ps(cb, _mm_mul_ps(beta_cbf, _mm_sub_ps(mb, my))); 70 | _mm_store_ps(cr, _mm_mul_ps(beta_crf, _mm_sub_ps(mr, my))); 71 | 72 | r += 4; g += 4; b += 4; 73 | y += 4; cb += 4; cr += 4; 74 | } 75 | } 76 | 77 | ////////////////////////////////////////////////////////////////////////// 78 | void sse_ict_backward(const float *y, const float *cb, const float *cr, 79 | float *r, float *g, float *b, ui32 repeat) 80 | { 81 | __m128 gamma_cr2g = _mm_set1_ps(CT_CNST::GAMMA_CR2G); 82 | __m128 gamma_cb2g = _mm_set1_ps(CT_CNST::GAMMA_CB2G); 83 | __m128 gamma_cr2r = _mm_set1_ps(CT_CNST::GAMMA_CR2R); 84 | __m128 gamma_cb2b = _mm_set1_ps(CT_CNST::GAMMA_CB2B); 85 | for (ui32 i = (repeat + 3) >> 2; i > 0; --i) 86 | { 87 | __m128 my = _mm_load_ps(y); 88 | __m128 mcr = _mm_load_ps(cr); 89 | __m128 mcb = _mm_load_ps(cb); 90 | __m128 mg = _mm_sub_ps(my, _mm_mul_ps(gamma_cr2g, mcr)); 91 | _mm_store_ps(g, _mm_sub_ps(mg, _mm_mul_ps(gamma_cb2g, mcb))); 92 | _mm_store_ps(r, _mm_add_ps(my, _mm_mul_ps(gamma_cr2r, mcr))); 93 | _mm_store_ps(b, _mm_add_ps(my, _mm_mul_ps(gamma_cb2b, mcb))); 94 | 95 | y += 4; cb += 4; cr += 4; 96 | r += 4; g += 4; b += 4; 97 | } 98 | } 99 | } 100 | } 101 | 102 | #endif 103 | -------------------------------------------------------------------------------- /src/core/transform/ojph_transform.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_transform.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_TRANSFORM_H 40 | #define OJPH_TRANSFORM_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | 46 | // defined elsewhere 47 | class line_buf; 48 | 49 | namespace local { 50 | union lifting_step; 51 | struct param_atk; 52 | 53 | ////////////////////////////////////////////////////////////////////////// 54 | void init_wavelet_transform_functions(); 55 | 56 | ///////////////////////////////////////////////////////////////////////// 57 | // Reversible functions 58 | ///////////////////////////////////////////////////////////////////////// 59 | 60 | ///////////////////////////////////////////////////////////////////////// 61 | extern void (*rev_vert_step) 62 | (const lifting_step* s, const line_buf* sig, const line_buf* other, 63 | const line_buf* aug, ui32 repeat, bool synthesis); 64 | 65 | ///////////////////////////////////////////////////////////////////////// 66 | extern void (*rev_horz_ana) 67 | (const param_atk* atk, const line_buf* ldst, const line_buf* hdst, 68 | const line_buf* src, ui32 width, bool even); 69 | 70 | ///////////////////////////////////////////////////////////////////////// 71 | extern void (*rev_horz_syn) 72 | (const param_atk* atk, const line_buf* dst, const line_buf* lsrc, 73 | const line_buf* hsrc, ui32 width, bool even); 74 | 75 | ///////////////////////////////////////////////////////////////////////// 76 | // Irreversible functions 77 | ///////////////////////////////////////////////////////////////////////// 78 | 79 | ///////////////////////////////////////////////////////////////////////// 80 | extern void (*irv_vert_step) 81 | (const lifting_step* s, const line_buf* sig, const line_buf* other, 82 | const line_buf* aug, ui32 repeat, bool synthesis); 83 | 84 | ///////////////////////////////////////////////////////////////////////// 85 | extern void (*irv_vert_times_K) 86 | (float K, const line_buf* aug, ui32 repeat); 87 | 88 | ///////////////////////////////////////////////////////////////////////// 89 | extern void (*irv_horz_ana) 90 | (const param_atk* atk, const line_buf* ldst, const line_buf* hdst, 91 | const line_buf* src, ui32 width, bool even); 92 | 93 | ///////////////////////////////////////////////////////////////////////// 94 | extern void (*irv_horz_syn) 95 | (const param_atk* atk, const line_buf* dst, const line_buf* lsrc, 96 | const line_buf* hsrc, ui32 width, bool even); 97 | 98 | } 99 | } 100 | 101 | #endif // !OJPH_TRANSFORM_H 102 | -------------------------------------------------------------------------------- /src/core/transform/ojph_colour_avx.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_colour_avx.cpp 34 | // Author: Aous Naman 35 | // Date: 11 October 2019 36 | //***************************************************************************/ 37 | 38 | #include "ojph_arch.h" 39 | #if defined(OJPH_ARCH_I386) || defined(OJPH_ARCH_X86_64) 40 | 41 | #include 42 | 43 | #include "ojph_defs.h" 44 | #include "ojph_colour.h" 45 | #include "ojph_colour_local.h" 46 | 47 | #include 48 | 49 | namespace ojph { 50 | namespace local { 51 | 52 | ////////////////////////////////////////////////////////////////////////// 53 | void avx_ict_forward(const float *r, const float *g, const float *b, 54 | float *y, float *cb, float *cr, ui32 repeat) 55 | { 56 | __m256 alpha_rf = _mm256_set1_ps(CT_CNST::ALPHA_RF); 57 | __m256 alpha_gf = _mm256_set1_ps(CT_CNST::ALPHA_GF); 58 | __m256 alpha_bf = _mm256_set1_ps(CT_CNST::ALPHA_BF); 59 | __m256 beta_cbf = _mm256_set1_ps(CT_CNST::BETA_CbF); 60 | __m256 beta_crf = _mm256_set1_ps(CT_CNST::BETA_CrF); 61 | for (int i = (repeat + 7) >> 3; i > 0; --i) 62 | { 63 | __m256 mr = _mm256_load_ps(r); 64 | __m256 mb = _mm256_load_ps(b); 65 | __m256 my = _mm256_mul_ps(alpha_rf, mr); 66 | my = _mm256_add_ps(my, _mm256_mul_ps(alpha_gf, _mm256_load_ps(g))); 67 | my = _mm256_add_ps(my, _mm256_mul_ps(alpha_bf, mb)); 68 | _mm256_store_ps(y, my); 69 | _mm256_store_ps(cb, _mm256_mul_ps(beta_cbf, _mm256_sub_ps(mb, my))); 70 | _mm256_store_ps(cr, _mm256_mul_ps(beta_crf, _mm256_sub_ps(mr, my))); 71 | 72 | r += 8; g += 8; b += 8; 73 | y += 8; cb += 8; cr += 8; 74 | } 75 | } 76 | 77 | ////////////////////////////////////////////////////////////////////////// 78 | void avx_ict_backward(const float *y, const float *cb, const float *cr, 79 | float *r, float *g, float *b, ui32 repeat) 80 | { 81 | __m256 gamma_cr2g = _mm256_set1_ps(CT_CNST::GAMMA_CR2G); 82 | __m256 gamma_cb2g = _mm256_set1_ps(CT_CNST::GAMMA_CB2G); 83 | __m256 gamma_cr2r = _mm256_set1_ps(CT_CNST::GAMMA_CR2R); 84 | __m256 gamma_cb2b = _mm256_set1_ps(CT_CNST::GAMMA_CB2B); 85 | for (int i = (repeat + 7) >> 3; i > 0; --i) 86 | { 87 | __m256 my = _mm256_load_ps(y); 88 | __m256 mcr = _mm256_load_ps(cr); 89 | __m256 mcb = _mm256_load_ps(cb); 90 | __m256 mg = _mm256_sub_ps(my, _mm256_mul_ps(gamma_cr2g, mcr)); 91 | _mm256_store_ps(g, _mm256_sub_ps(mg, _mm256_mul_ps(gamma_cb2g, mcb))); 92 | _mm256_store_ps(r, _mm256_add_ps(my, _mm256_mul_ps(gamma_cr2r, mcr))); 93 | _mm256_store_ps(b, _mm256_add_ps(my, _mm256_mul_ps(gamma_cb2b, mcb))); 94 | 95 | y += 8; cb += 8; cr += 8; 96 | r += 8; g += 8; b += 8; 97 | } 98 | } 99 | 100 | } 101 | } 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_subband.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_subband.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_SUBBAND_H 40 | #define OJPH_SUBBAND_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | 46 | //////////////////////////////////////////////////////////////////////////// 47 | //defined elsewhere 48 | class line_buf; 49 | class mem_elastic_allocator; 50 | class codestream; 51 | 52 | namespace local { 53 | 54 | ////////////////////////////////////////////////////////////////////////// 55 | //defined here 56 | class resolution; 57 | struct precinct; 58 | class codeblock; 59 | struct coded_cb_header; 60 | 61 | ////////////////////////////////////////////////////////////////////////// 62 | class subband 63 | { 64 | friend struct precinct; 65 | public: 66 | subband() { 67 | res_num = band_num = 0; 68 | reversible = false; 69 | empty = true; // <---- true 70 | lines = NULL; 71 | parent = NULL; 72 | blocks = NULL; 73 | xcb_prime = ycb_prime = 0; 74 | cur_cb_row = 0; 75 | cur_line = 0; 76 | cur_cb_height = 0; 77 | delta = delta_inv = 0.0f; 78 | K_max = 0; 79 | coded_cbs = NULL; 80 | elastic = NULL; 81 | } 82 | 83 | static void pre_alloc(codestream *codestream, const rect& band_rect, 84 | ui32 comp_num, ui32 res_num, ui32 transform_flags); 85 | void finalize_alloc(codestream *codestream, const rect& band_rect, 86 | resolution* res, ui32 res_num, ui32 subband_num); 87 | 88 | void exchange_buf(line_buf* l); 89 | line_buf* get_line() { return lines; } 90 | void push_line(); 91 | 92 | void get_cb_indices(const size& num_precincts, precinct *precincts); 93 | float get_delta() { return delta; } 94 | bool exists() { return !empty; } 95 | 96 | line_buf* pull_line(); 97 | resolution* get_parent() { return parent; } 98 | const resolution* get_parent() const { return parent; } 99 | 100 | private: 101 | bool empty; // true if the subband has no pixels or 102 | // the subband is NOT USED 103 | ui32 res_num, band_num; 104 | bool reversible; 105 | rect band_rect; 106 | line_buf *lines; 107 | resolution* parent; 108 | codeblock* blocks; 109 | size num_blocks; 110 | size log_PP; 111 | ui32 xcb_prime, ycb_prime; 112 | ui32 cur_cb_row; 113 | int cur_line; 114 | int cur_cb_height; 115 | float delta, delta_inv; 116 | ui32 K_max; 117 | coded_cb_header *coded_cbs; 118 | mem_elastic_allocator *elastic; 119 | }; 120 | 121 | } 122 | } 123 | 124 | #endif // !OJPH_SUBBAND_H -------------------------------------------------------------------------------- /src/core/codestream/ojph_codeblock.h: -------------------------------------------------------------------------------- 1 | 2 | //***************************************************************************/ 3 | // This software is released under the 2-Clause BSD license, included 4 | // below. 5 | // 6 | // Copyright (c) 2019, Aous Naman 7 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 8 | // Copyright (c) 2019, The University of New South Wales, Australia 9 | // 10 | // Redistribution and use in source and binary forms, with or without 11 | // modification, are permitted provided that the following conditions are 12 | // met: 13 | // 14 | // 1. Redistributions of source code must retain the above copyright 15 | // notice, this list of conditions and the following disclaimer. 16 | // 17 | // 2. Redistributions in binary form must reproduce the above copyright 18 | // notice, this list of conditions and the following disclaimer in the 19 | // documentation and/or other materials provided with the distribution. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | //***************************************************************************/ 33 | // This file is part of the OpenJPH software implementation. 34 | // File: ojph_codeblock.h 35 | // Author: Aous Naman 36 | // Date: 28 August 2019 37 | //***************************************************************************/ 38 | 39 | 40 | #ifndef OJPH_CODEBLOCK_H 41 | #define OJPH_CODEBLOCK_H 42 | 43 | #include "ojph_defs.h" 44 | #include "ojph_file.h" 45 | #include "ojph_codeblock_fun.h" 46 | 47 | namespace ojph { 48 | 49 | //////////////////////////////////////////////////////////////////////////// 50 | //defined elsewhere 51 | class line_buf; 52 | class mem_elastic_allocator; 53 | class codestream; 54 | struct coded_lists; 55 | 56 | namespace local { 57 | 58 | ////////////////////////////////////////////////////////////////////////// 59 | //defined here 60 | struct precinct; 61 | class subband; 62 | struct coded_cb_header; 63 | 64 | ////////////////////////////////////////////////////////////////////////// 65 | class codeblock 66 | { 67 | friend struct precinct; 68 | enum : ui32 { 69 | BUF32 = 4, 70 | BUF64 = 8, 71 | }; 72 | 73 | public: 74 | static void pre_alloc(codestream *codestream, const size& nominal, 75 | ui32 precision); 76 | void finalize_alloc(codestream *codestream, subband* parent, 77 | const size& nominal, const size& cb_size, 78 | coded_cb_header* coded_cb, ui32 K_max, 79 | int tbx0, ui32 precision, ui32 comp_idx); 80 | void push(line_buf *line); 81 | void encode(mem_elastic_allocator *elastic); 82 | void recreate(const size& cb_size, coded_cb_header* coded_cb); 83 | 84 | void decode(); 85 | void pull_line(line_buf *line); 86 | 87 | private: 88 | ui32 precision; 89 | union { 90 | ui32* buf32; 91 | ui64* buf64; 92 | }; 93 | size nominal_size; 94 | size cb_size; 95 | ui32 stride; 96 | ui32 buf_size; 97 | subband* parent; 98 | int line_offset; 99 | ui32 cur_line; 100 | float delta, delta_inv; 101 | ui32 K_max; 102 | bool reversible; 103 | bool resilient; 104 | bool stripe_causal; 105 | bool zero_block; // true when the decoded block is all zero 106 | union { 107 | ui32 max_val32[8]; // supports up to 256 bits 108 | ui64 max_val64[4]; // supports up to 256 bits 109 | }; 110 | coded_cb_header* coded_cb; 111 | codeblock_fun codeblock_functions; 112 | }; 113 | 114 | ////////////////////////////////////////////////////////////////////////// 115 | struct coded_cb_header 116 | { 117 | ui32 pass_length[2]; 118 | ui32 num_passes; // number of passes to be decoded 119 | ui32 Kmax; 120 | ui32 missing_msbs; 121 | coded_lists *next_coded; 122 | 123 | static const int prefix_buf_size = 8; 124 | static const int suffix_buf_size = 16; 125 | }; 126 | 127 | } 128 | } 129 | 130 | #endif // !OJPH_CODEBLOCK_H -------------------------------------------------------------------------------- /src/core/transform/ojph_colour.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_colour.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_COLOR_H 40 | #define OJPH_COLOR_H 41 | 42 | namespace ojph { 43 | 44 | // defined elsewhere 45 | class line_buf; 46 | 47 | namespace local { 48 | 49 | //////////////////////////////////////////////////////////////////////////// 50 | void init_colour_transform_functions(); 51 | 52 | //////////////////////////////////////////////////////////////////////////// 53 | extern void (*rev_convert) 54 | (const line_buf *src_line, const ui32 src_line_offset, 55 | line_buf *dst_line, const ui32 dst_line_offset, 56 | si64 shift, ui32 width); 57 | 58 | //////////////////////////////////////////////////////////////////////////// 59 | extern void (*rev_convert_nlt_type3) 60 | (const line_buf *src_line, const ui32 src_line_offset, 61 | line_buf *dst_line, const ui32 dst_line_offset, 62 | si64 shift, ui32 width); 63 | 64 | 65 | //////////////////////////////////////////////////////////////////////////// 66 | extern void (*irv_convert_to_integer) ( 67 | const line_buf *src_line, line_buf *dst_line, ui32 dst_line_offset, 68 | ui32 bit_depth, bool is_signed, ui32 width); 69 | 70 | //////////////////////////////////////////////////////////////////////////// 71 | extern void (*irv_convert_to_float) ( 72 | const line_buf *src_line, ui32 src_line_offset, 73 | line_buf *dst_line, ui32 bit_depth, bool is_signed, ui32 width); 74 | 75 | //////////////////////////////////////////////////////////////////////////// 76 | extern void (*irv_convert_to_integer_nlt_type3) ( 77 | const line_buf *src_line, line_buf *dst_line, ui32 dst_line_offset, 78 | ui32 bit_depth, bool is_signed, ui32 width); 79 | 80 | //////////////////////////////////////////////////////////////////////////// 81 | extern void (*irv_convert_to_float_nlt_type3) ( 82 | const line_buf *src_line, ui32 src_line_offset, 83 | line_buf *dst_line, ui32 bit_depth, bool is_signed, ui32 width); 84 | 85 | //////////////////////////////////////////////////////////////////////////// 86 | extern void (*rct_forward) 87 | (const line_buf *r, const line_buf *g, const line_buf *b, 88 | line_buf *y, line_buf *cb, line_buf *cr, ui32 repeat); 89 | 90 | //////////////////////////////////////////////////////////////////////////// 91 | extern void (*rct_backward) 92 | (const line_buf *y, const line_buf *cb, const line_buf *cr, 93 | line_buf *r, line_buf *g, line_buf *b, ui32 repeat); 94 | 95 | //////////////////////////////////////////////////////////////////////////// 96 | extern void (*ict_forward) 97 | (const float *r, const float *g, const float *b, 98 | float *y, float *cb, float *cr, ui32 repeat); 99 | 100 | //////////////////////////////////////////////////////////////////////////// 101 | extern void (*ict_backward) 102 | (const float *y, const float *cb, const float *cr, 103 | float *r, float *g, float *b, ui32 repeat); 104 | } 105 | } 106 | 107 | 108 | 109 | #endif // !OJPH_COLOR_H 110 | -------------------------------------------------------------------------------- /src/core/others/ojph_mem.c: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2025, Aous Naman 6 | // Copyright (c) 2025, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2025, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_mem.c 34 | // Author: Aous Naman 35 | // Date: 17 October 2025 36 | //***************************************************************************/ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | //////////////////////////////////////////////////////////////////////////////// 43 | // OS detection definitions for C only 44 | //////////////////////////////////////////////////////////////////////////////// 45 | #if (defined WIN32) || (defined _WIN32) || (defined _WIN64) 46 | #define OJPH_OS_WINDOWS 47 | #elif (defined __APPLE__) 48 | #define OJPH_OS_APPLE 49 | #elif (defined __ANDROID__) 50 | #define OJPH_OS_ANDROID 51 | #elif (defined __linux) 52 | #define OJPH_OS_LINUX 53 | #endif 54 | 55 | //////////////////////////////////////////////////////////////////////////////// 56 | // Defines for dll in C only 57 | //////////////////////////////////////////////////////////////////////////////// 58 | #if defined(OJPH_OS_WINDOWS) && defined(OJPH_BUILD_SHARED_LIBRARY) 59 | #define OJPH_EXPORT __declspec(dllexport) 60 | #else 61 | #define OJPH_EXPORT 62 | #endif 63 | 64 | //////////////////////////////////////////////////////////////////////////////// 65 | #ifdef OJPH_OS_WINDOWS 66 | OJPH_EXPORT void* ojph_aligned_malloc(size_t alignment, size_t size) 67 | { 68 | assert(alignment != 0 && (alignment & (alignment - 1)) == 0); 69 | return _aligned_malloc(size, alignment); 70 | } 71 | 72 | OJPH_EXPORT void ojph_aligned_free(void* pointer) 73 | { 74 | _aligned_free(pointer); 75 | } 76 | #elif (defined OJPH_ALIGNED_ALLOC_EXISTS) 77 | void* ojph_aligned_malloc(size_t alignment, size_t size) 78 | { 79 | assert(alignment != 0 && (alignment & (alignment - 1)) == 0); 80 | return aligned_alloc(alignment, size); 81 | } 82 | 83 | void ojph_aligned_free(void* pointer) 84 | { 85 | free(pointer); 86 | } 87 | #elif (defined OJPH_POSIX_MEMALIGN_EXISTS) 88 | void* ojph_aligned_malloc(size_t alignment, size_t size) 89 | { 90 | assert(alignment != 0 && (alignment & (alignment - 1)) == 0); 91 | void *p = NULL; 92 | int e = posix_memalign(&p, alignment, size); 93 | return (e ? NULL : p); 94 | } 95 | 96 | void ojph_aligned_free(void* pointer) 97 | { 98 | free(pointer); 99 | } 100 | #else 101 | void* ojph_aligned_malloc(size_t alignment, size_t size) 102 | { 103 | assert(alignment != 0 && (alignment & (alignment - 1)) == 0); 104 | 105 | // emulate aligned_alloc 106 | void* orig_ptr = malloc(size + alignment + sizeof(void*)); 107 | if (orig_ptr == NULL) 108 | return NULL; // Allocation failed 109 | 110 | uintptr_t start_of_mem = (uintptr_t)orig_ptr + sizeof(void*); 111 | uintptr_t aligned_addr = (start_of_mem + alignment - 1) & ~(alignment - 1); 112 | 113 | void** ptr_to_orig_ptr = (void**)aligned_addr; 114 | ptr_to_orig_ptr[-1] = orig_ptr; 115 | 116 | return (void*)aligned_addr; 117 | } 118 | 119 | void ojph_aligned_free(void* pointer) 120 | { 121 | if (pointer) { 122 | // Retrieve the original pointer stored just before aligned pointer 123 | void** ptr_to_orig_ptr = (void**)pointer; 124 | void* orig_ptr = ptr_to_orig_ptr[-1]; 125 | 126 | free(orig_ptr); 127 | } 128 | } 129 | #endif 130 | -------------------------------------------------------------------------------- /src/apps/common/ojph_threads.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2024, Aous Naman 6 | // Copyright (c) 2024, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2024, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_threads.h 34 | // Author: Aous Naman 35 | // Date: 22 April 2024 36 | //***************************************************************************/ 37 | 38 | #ifndef OJPH_THREADS_H 39 | #define OJPH_THREADS_H 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | namespace ojph 49 | { 50 | namespace thds 51 | { 52 | 53 | /////////////////////////////////////////////////////////////////////////////// 54 | // 55 | // 56 | // 57 | // 58 | // 59 | /////////////////////////////////////////////////////////////////////////////// 60 | 61 | /*****************************************************************************/ 62 | /** @brief A base object for queuing tasks in the thread_pool 63 | * 64 | * Tasks run in the thread_pool must derive from this function and define 65 | * \"execute\". Derived objects can include their own member variables. 66 | * 67 | */ 68 | class worker_thread_base 69 | { 70 | public: 71 | /** 72 | * @brief virtual construction is a necessity to deconstruct derived 73 | * objects. 74 | */ 75 | virtual ~worker_thread_base() { } 76 | 77 | /** 78 | * @brief Derived functions must define this function to execute its work 79 | */ 80 | virtual void execute() = 0; 81 | }; 82 | 83 | 84 | /////////////////////////////////////////////////////////////////////////////// 85 | // 86 | // 87 | // 88 | // 89 | // 90 | /////////////////////////////////////////////////////////////////////////////// 91 | 92 | /*****************************************************************************/ 93 | /** 94 | * @brief Implements a pool of threads, and can queue tasks. 95 | * 96 | */ 97 | class thread_pool 98 | { 99 | public: 100 | /** 101 | * @brief default constructor 102 | */ 103 | thread_pool() { stop.store(false, std::memory_order_relaxed); } 104 | /** 105 | * @brief default destructor 106 | */ 107 | ~thread_pool(); 108 | 109 | public: 110 | /** 111 | * @brief Initializes the thread pool 112 | * 113 | * @param num_threads the number of threads the thread pool holds 114 | */ 115 | void init(size_t num_threads); 116 | 117 | /** 118 | * @brief Adds a task to the thread pool 119 | * 120 | * @param task the task to added, must be derived from worker_thread_base 121 | */ 122 | void add_task(worker_thread_base* task); 123 | 124 | /** 125 | * @brief Returns the number of threads in the thread pool 126 | * 127 | * @retuen number of threads in the thread pool 128 | */ 129 | size_t get_num_threads() { return threads.size(); } 130 | 131 | private: 132 | /** 133 | * @brief A static function to start a thread 134 | * 135 | * @param tp a pointer to the thread pool 136 | */ 137 | static void start_thread(thread_pool* tp); 138 | 139 | private: 140 | std::vector threads; 141 | std::deque tasks; 142 | std::mutex mutex; 143 | std::condition_variable condition; 144 | std::atomic_bool stop; 145 | }; 146 | 147 | } // !thds namespace 148 | } // !ojph namespace 149 | 150 | 151 | 152 | 153 | 154 | 155 | #endif // !OJPH_THREADS_H -------------------------------------------------------------------------------- /src/core/others/ojph_mem.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_mem.cpp 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #include 40 | #include "ojph_mem.h" 41 | 42 | namespace ojph { 43 | 44 | //////////////////////////////////////////////////////////////////////////// 45 | // 46 | // 47 | // 48 | // 49 | // 50 | //////////////////////////////////////////////////////////////////////////// 51 | 52 | //////////////////////////////////////////////////////////////////////////// 53 | template<> 54 | void line_buf::wrap(si32 *buffer, size_t num_ele, ui32 pre_size) 55 | { 56 | this->i32 = buffer; 57 | this->size = num_ele; 58 | this->pre_size = pre_size; 59 | this->flags = LFT_32BIT | LFT_INTEGER; 60 | } 61 | 62 | //////////////////////////////////////////////////////////////////////////// 63 | template<> 64 | void line_buf::wrap(float *buffer, size_t num_ele, ui32 pre_size) 65 | { 66 | this->f32 = buffer; 67 | this->size = num_ele; 68 | this->pre_size = pre_size; 69 | this->flags = LFT_32BIT; 70 | } 71 | 72 | //////////////////////////////////////////////////////////////////////////// 73 | template<> 74 | void line_buf::wrap(si64 *buffer, size_t num_ele, ui32 pre_size) 75 | { 76 | this->i64 = buffer; 77 | this->size = num_ele; 78 | this->pre_size = pre_size; 79 | this->flags = LFT_64BIT | LFT_INTEGER; 80 | } 81 | 82 | //////////////////////////////////////////////////////////////////////////// 83 | // 84 | // 85 | // 86 | // 87 | // 88 | //////////////////////////////////////////////////////////////////////////// 89 | 90 | //////////////////////////////////////////////////////////////////////////// 91 | mem_elastic_allocator::stores_list* 92 | mem_elastic_allocator::allocate(mem_elastic_allocator::stores_list** list, 93 | ui32 extended_bytes) 94 | { 95 | ui32 bytes = ojph_max(extended_bytes, chunk_size); 96 | if (avail != NULL && avail->orig_size >= bytes) 97 | { 98 | *list = avail; 99 | avail = avail->next_store; 100 | (*list)->restart(); 101 | return *list; 102 | } 103 | else 104 | { 105 | ui32 store_bytes = stores_list::eval_store_bytes(bytes); 106 | *list = (stores_list*) malloc(store_bytes); 107 | total_allocated += store_bytes; 108 | return new (*list) stores_list(bytes); 109 | } 110 | } 111 | 112 | //////////////////////////////////////////////////////////////////////////// 113 | void mem_elastic_allocator::get_buffer(ui32 needed_bytes, coded_lists* &p) 114 | { 115 | ui32 extended_bytes = needed_bytes + (ui32)sizeof(coded_lists); 116 | 117 | if (store == NULL) 118 | cur_store = store = allocate(&store, extended_bytes); 119 | else if (cur_store->available < extended_bytes) 120 | cur_store = allocate(&cur_store->next_store, extended_bytes); 121 | 122 | p = new (cur_store->data) coded_lists(needed_bytes); 123 | 124 | assert(cur_store->available >= extended_bytes); 125 | cur_store->available -= extended_bytes; 126 | cur_store->data += extended_bytes; 127 | } 128 | 129 | //////////////////////////////////////////////////////////////////////////// 130 | void mem_elastic_allocator::restart() 131 | { 132 | // move to the end of avail 133 | stores_list** p = &avail; 134 | while (*p != NULL) 135 | p = &((*p)->next_store); 136 | *p = store; 137 | cur_store = store = NULL; 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_codeblock_fun.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codeblock_fun.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_CODEBLOCK_FUN_H 40 | #define OJPH_CODEBLOCK_FUN_H 41 | 42 | #include "ojph_defs.h" 43 | #include "ojph_file.h" 44 | #include "ojph_params_local.h" 45 | 46 | namespace ojph { 47 | 48 | namespace local { 49 | 50 | // define function signature simple memory clearing 51 | typedef void (*mem_clear_fun)(void* addr, size_t count); 52 | 53 | // define function signature for max value finding 54 | typedef ui32 (*find_max_val_fun32)(ui32* addr); 55 | 56 | typedef ui64 (*find_max_val_fun64)(ui64* addr); 57 | 58 | // define line transfer function signature from subbands to codeblocks 59 | typedef void (*tx_to_cb_fun32)(const void *sp, ui32 *dp, ui32 K_max, 60 | float delta_inv, ui32 count, ui32* max_val); 61 | 62 | typedef void (*tx_to_cb_fun64)(const void *sp, ui64 *dp, ui32 K_max, 63 | float delta_inv, ui32 count, ui64* max_val); 64 | 65 | // define line transfer function signature from codeblock to subband 66 | typedef void (*tx_from_cb_fun32)(const ui32 *sp, void *dp, ui32 K_max, 67 | float delta, ui32 count); 68 | 69 | typedef void (*tx_from_cb_fun64)(const ui64 *sp, void *dp, ui32 K_max, 70 | float delta, ui32 count); 71 | 72 | // define the block decoder function signature 73 | typedef bool (*cb_decoder_fun32)(ui8* coded_data, ui32* decoded_data, 74 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 75 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 76 | 77 | typedef bool (*cb_decoder_fun64)(ui8* coded_data, ui64* decoded_data, 78 | ui32 missing_msbs, ui32 num_passes, ui32 lengths1, ui32 lengths2, 79 | ui32 width, ui32 height, ui32 stride, bool stripe_causal); 80 | 81 | // define the block encoder function signature 82 | typedef void (*cb_encoder_fun32)(ui32* buf, ui32 missing_msbs, 83 | ui32 num_passes, ui32 width, ui32 height, ui32 stride, 84 | ui32* lengths, ojph::mem_elastic_allocator* elastic, 85 | ojph::coded_lists*& coded); 86 | 87 | typedef void (*cb_encoder_fun64)(ui64* buf, ui32 missing_msbs, 88 | ui32 num_passes, ui32 width, ui32 height, ui32 stride, 89 | ui32* lengths, ojph::mem_elastic_allocator* elastic, 90 | ojph::coded_lists*& coded); 91 | 92 | ////////////////////////////////////////////////////////////////////////// 93 | struct codeblock_fun { 94 | 95 | void init(bool reversible); 96 | 97 | // a pointer to the max value finding function 98 | mem_clear_fun mem_clear; 99 | 100 | // a pointer to the max value finding function 101 | find_max_val_fun32 find_max_val32; 102 | find_max_val_fun64 find_max_val64; 103 | 104 | // a pointer to function transferring samples from subbands to codeblocks 105 | tx_to_cb_fun32 tx_to_cb32; 106 | tx_to_cb_fun64 tx_to_cb64; 107 | 108 | // a pointer to function transferring samples from codeblocks to subbands 109 | tx_from_cb_fun32 tx_from_cb32; 110 | tx_from_cb_fun64 tx_from_cb64; 111 | 112 | // a pointer to the decoder function 113 | cb_decoder_fun32 decode_cb32; 114 | cb_decoder_fun64 decode_cb64; 115 | 116 | // a pointer to the encoder function 117 | cb_encoder_fun32 encode_cb32; 118 | cb_encoder_fun64 encode_cb64; 119 | }; 120 | 121 | } 122 | } 123 | #endif // !OJPH_CODEBLOCK_FUN_H -------------------------------------------------------------------------------- /src/core/codestream/ojph_resolution.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_resolution.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_RESOLUTION_H 40 | #define OJPH_RESOLUTION_H 41 | 42 | #include "ojph_defs.h" 43 | 44 | namespace ojph { 45 | 46 | //////////////////////////////////////////////////////////////////////////// 47 | //defined elsewhere 48 | class line_buf; 49 | class mem_elastic_allocator; 50 | class codestream; 51 | 52 | namespace local { 53 | 54 | ////////////////////////////////////////////////////////////////////////// 55 | //defined here 56 | class tile_comp; 57 | struct precinct; 58 | class subband; 59 | 60 | ////////////////////////////////////////////////////////////////////////// 61 | class resolution 62 | { 63 | public: 64 | enum : ui32 { 65 | HORZ_TRX = 0x01, // horizontal transform 66 | VERT_TRX = 0x02, // vertical transform 67 | }; 68 | 69 | public: 70 | static void pre_alloc(codestream *codestream, const rect& res_rect, 71 | const rect& recon_res_rect, 72 | ui32 comp_num, ui32 res_num); 73 | void finalize_alloc(codestream *codestream, const rect& res_rect, 74 | const rect& recon_res_rect, ui32 comp_num, 75 | ui32 res_num, point comp_downsamp, 76 | point res_downsamp, tile_comp *parent_tile_comp, 77 | resolution *parent_res); 78 | 79 | line_buf* get_line(); 80 | void push_line(); 81 | line_buf* pull_line(); 82 | rect get_rect() { return res_rect; } 83 | ui32 get_comp_num() { return comp_num; } 84 | bool has_horz_transform() { return (transform_flags & HORZ_TRX) != 0; } 85 | bool has_vert_transform() { return (transform_flags & VERT_TRX) != 0; } 86 | 87 | ui32 prepare_precinct(); 88 | void write_precincts(outfile_base *file); 89 | bool get_top_left_precinct(point &top_left); 90 | void write_one_precinct(outfile_base *file); 91 | resolution *next_resolution() { return child_res; } 92 | void parse_all_precincts(ui32& data_left, infile_base *file); 93 | void parse_one_precinct(ui32& data_left, infile_base *file); 94 | 95 | ui32 get_num_bytes() const { return num_bytes; } 96 | ui32 get_num_bytes(ui32 resolution_num) const; 97 | 98 | private: 99 | bool reversible, skipped_res_for_read, skipped_res_for_recon; 100 | ui32 num_steps; 101 | ui32 res_num; 102 | ui32 comp_num; 103 | ui32 num_bytes; // number of bytes in this resolution 104 | // used for tilepart length 105 | point comp_downsamp; 106 | rect res_rect; // resolution rectangle 107 | line_buf* lines; // used to store lines 108 | lifting_buf *ssp; // step state pointer 109 | lifting_buf *aug, *sig; 110 | subband *bands; 111 | tile_comp *parent_comp; 112 | resolution *parent_res, *child_res; 113 | //precincts stuff 114 | precinct *precincts; 115 | size num_precincts; 116 | size log_PP; 117 | ui32 max_num_levels; 118 | int tag_tree_size; 119 | ui32 level_index[20]; //more than enough 120 | point cur_precinct_loc; //used for progressing spatial modes (2, 3, 4) 121 | const param_atk* atk; 122 | ui32 transform_flags; 123 | //wavelet machinery 124 | ui32 cur_line; 125 | ui32 rows_to_produce; 126 | bool vert_even, horz_even; 127 | mem_elastic_allocator *elastic; 128 | }; 129 | 130 | } 131 | } 132 | 133 | #endif // !OJPH_RESOLUTION_H -------------------------------------------------------------------------------- /docs/compiling.md: -------------------------------------------------------------------------------- 1 | # Compiling # 2 | 3 | The code employs the *cmake* tool to generate a variety of build environments. A visual studio code container is included for building using 4 | the visual studio code remote containers add in (highly recommended) 5 | 6 | ## For Linux ## 7 | 8 | You may need to install libtiff; then, 9 | 10 | cd build 11 | cmake -DCMAKE_BUILD_TYPE=Release ../ 12 | make 13 | sudo make install 14 | 15 | ## For Windows ## 16 | 17 | Compilation depends on libtiff. A pre-compiled library with all the library features for Windows is not available; I am using [this](https://github.com/aous72/OpenJPH/files/14060335/tiff.zip), but I think I have only the basic library. 18 | 19 | cd build 20 | cmake .. -G "Visual Studio 17 2022 Win64" -DCMAKE_PREFIX_PATH= 21 | 22 | `cmake` supports other visual studio versions. This command generates a solution in the build folder, which can be build using visual studio. 23 | 24 | To compile from the command line, use 25 | 26 | cmake --build . --config Release 27 | 28 | To install either use 29 | 30 | cmake --install . --prefix 31 | 32 | to install the library to your desired folder, or, if you want to install to C:\Program Files, you need a PowerShell/CMD running as administrator, and 33 | 34 | cmake --install . 35 | 36 | 37 | ## For macOS ## 38 | 39 | You can use the "For Linux" approach above. Alternatively, you can use the Xcode project in src/apps/apps.xcodeproj, which I use. Another approach is to use cmake to generate an xcode project, in the build folder, using 40 | 41 | cd build 42 | cmake ../ -G Xcode 43 | make 44 | sudo make install 45 | 46 | I have not tested this in a long time, but you get the picture. 47 | 48 | ## Building Tests ## 49 | 50 | When you invoke `cmake` add `-DOJPH_BUILD_TESTS=ON`, then, for Windows 51 | 52 | cd tests 53 | ctest -C Release 54 | 55 | For other platforms 56 | 57 | cd tests 58 | ctest 59 | 60 | The test setup is a bit finicky, and may sometimes fail for silly reasons. 61 | 62 | # Compiling to Node.js # 63 | 64 | The library can be compiled to run with Node.js. Compilation needs the [emscripten](https://emscripten.org/) tools. One way of using these tools is to install them on your machine, and activate them using, assuming running on platform other than Windows, 65 | 66 | source emsdk_env.sh 67 | 68 | before compilation. Then, 69 | emcmake cmake .. 70 | emmake make 71 | 72 | Compilation will generate two version of the library and executables, one with WebAssembly SIMD instructions and one without. 73 | 74 | 75 | # Compiling to javascript/wasm # 76 | 77 | The library can now be compiled to javascript/wasm. For this purpose, a small wrapper file (ojph_wrapper.cpp) has been written to interface between javascript and C++; the wrapper currently supports decoding only. A small demo page demonstrating the script can be accessed [here](https://openjph.org/javascript/demo.html). 78 | 79 | Compilation needs the [emscripten](https://emscripten.org/) tools. One way of using these tools is to install them on your machine, and activate them using 80 | 81 | source emsdk_env.sh 82 | 83 | before compilation. Alternatively, if you are a docker user, the you can launch a docker session using script provided at ```subprojects/js/emscripten-docker.sh```; this script will download a third-party docker image that has the emscripten tools integrated in it -- Thanks to [Chris](https://github.com/chafey) for the suggesting and providing these tools. 84 | 85 | The javascript decoder can be compiled using 86 | 87 | cd subprojects/js/build 88 | emcmake cmake .. 89 | emmake make 90 | 91 | The compilation creates libopenjph.js and libopenjph.wasm in subprojects/js/html folder; it also creates libopenjphsimd.js and libopenjphsimd.wasm. That html folder also has the demo webpage index.html and a compressed image test.j2c which the script in index.html decodes. The index.html detects if the browser supports WebAssembly SIMD instructions, and loads the correct library accordingly. 92 | 93 | To run the demo webpage on your machine, you need a webserver running on the machine -- Due to security reasons, javascript engines running in a browser cannot access local files on the machine. You can use the ```emrun``` command, provided with the emscripten 94 | tools, by issuing the command 95 | 96 | emrun index.html 97 | 98 | from inside the html folder; the default port is 6931. 99 | Alternatively, a simple python webserver can be run using 100 | 101 | python -m http.server 8000 102 | 103 | also from inside the html folder. Here, 8000 is the port number at which the webserver will be listening. The webpage can then be accessed by open localhost:8000 in you browser. Any browser supporting webassembly can be used to view this webpage; examples include Firefox, Chrome, Safari, and Edge, on a desktop, mobile, or tablet. 104 | 105 | # Visual Studio Code Remote Containers # 106 | 107 | Visual Studio Code Remote Containers are now available with OpenJPH. These scripts/configuration files are provided by [Chris](https://github.com/chafey) -- Thank you Chris, and I must say I am not familiar with them. 108 | The scripts, in the ```.devcontainer``` folder, will build a docker image that can be used with visual studio code as a development environment. 109 | 110 | # Compiling for ARM and other platforms # 111 | 112 | Compilation should simply work now. The simple test code I have passes when run on MacOS ARM on GitHub. 113 | 114 | # Disabling SIMD instructions # 115 | 116 | The code now employs the architecture-agnostic option `OJPH_DISABLE_SIMD`, which should include SIMD instructions wherever they are supported. This can be achieved with `-DOJPH_DISABLE_SIMD=ON` option during CMake configuration. Individual instruction sets can be disabled; see the options in the main CMakeLists.txt file. 117 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_bitbuffer_write.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_bitbuffer_write.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_BITBUFFER_WRITE_H 40 | #define OJPH_BITBUFFER_WRITE_H 41 | 42 | #include "ojph_defs.h" 43 | #include "ojph_file.h" 44 | 45 | namespace ojph { 46 | 47 | //////////////////////////////////////////////////////////////////////////// 48 | //defined elsewhere 49 | class mem_elastic_allocator; 50 | struct coded_lists; 51 | 52 | namespace local { 53 | 54 | ////////////////////////////////////////////////////////////////////////// 55 | struct bit_write_buf 56 | { 57 | static const int needed; 58 | 59 | bit_write_buf() { ccl = NULL; avail_bits = 0; tmp = 0; } 60 | coded_lists* ccl; 61 | int avail_bits; 62 | ui64 tmp; 63 | }; 64 | 65 | ////////////////////////////////////////////////////////////////////////// 66 | const int bit_write_buf::needed = 512; 67 | 68 | ////////////////////////////////////////////////////////////////////////// 69 | static inline 70 | void bb_expand_buf(bit_write_buf *bbp, mem_elastic_allocator *elastic, 71 | coded_lists*& cur_coded_list) 72 | { 73 | assert(cur_coded_list == NULL); 74 | elastic->get_buffer(bit_write_buf::needed, cur_coded_list); 75 | bbp->ccl = cur_coded_list; 76 | bbp->tmp = 0; 77 | } 78 | 79 | ////////////////////////////////////////////////////////////////////////// 80 | static inline 81 | void bb_init(bit_write_buf *bbp, mem_elastic_allocator *elastic, 82 | coded_lists*& cur_coded_list) 83 | { 84 | bb_expand_buf(bbp, elastic, cur_coded_list); 85 | bbp->avail_bits = 8; 86 | } 87 | 88 | ////////////////////////////////////////////////////////////////////////// 89 | static inline 90 | void bb_put_bit(bit_write_buf *bbp, ui32 bit, 91 | mem_elastic_allocator *elastic, 92 | coded_lists*& cur_coded_list, ui32& ph_bytes) 93 | { 94 | --bbp->avail_bits; 95 | bbp->tmp |= (bit & 1) << bbp->avail_bits; 96 | if (bbp->avail_bits <= 0) 97 | { 98 | bbp->avail_bits = 8 - (bbp->tmp != 0xFF ? 0 : 1); 99 | bbp->ccl->buf[bbp->ccl->buf_size - bbp->ccl->avail_size] = 100 | (ui8)(bbp->tmp & 0xFF); 101 | bbp->tmp = 0; 102 | --bbp->ccl->avail_size; 103 | if (bbp->ccl->avail_size == 0) 104 | { 105 | bb_expand_buf(bbp, elastic, cur_coded_list->next_list); 106 | cur_coded_list = cur_coded_list->next_list; 107 | ph_bytes += bit_write_buf::needed; 108 | } 109 | } 110 | } 111 | 112 | ////////////////////////////////////////////////////////////////////////// 113 | static inline 114 | void bb_put_zeros(bit_write_buf *bbp, int num_zeros, 115 | mem_elastic_allocator *elastic, 116 | coded_lists*& cur_coded_list, ui32& ph_bytes) 117 | { 118 | for (int i = num_zeros; i > 0; --i) 119 | bb_put_bit(bbp, 0, elastic, cur_coded_list, ph_bytes); 120 | } 121 | 122 | ////////////////////////////////////////////////////////////////////////// 123 | static inline 124 | void bb_put_bits(bit_write_buf *bbp, ui32 data, int num_bits, 125 | mem_elastic_allocator *elastic, 126 | coded_lists*& cur_coded_list, ui32& ph_bytes) 127 | { 128 | assert(num_bits <= 32); 129 | for (int i = num_bits - 1; i >= 0; --i) 130 | bb_put_bit(bbp, data >> i, elastic, cur_coded_list, ph_bytes); 131 | } 132 | 133 | ////////////////////////////////////////////////////////////////////////// 134 | static inline 135 | void bb_terminate(bit_write_buf *bbp) 136 | { 137 | if (bbp->avail_bits < 8) //bits have been written 138 | { 139 | ui8 val = (ui8)(bbp->tmp & 0xFF); 140 | bbp->ccl->buf[bbp->ccl->buf_size - bbp->ccl->avail_size] = val; 141 | --bbp->ccl->avail_size; 142 | } 143 | } 144 | 145 | } 146 | } 147 | #endif // !OJPH_BITBUFFER_WRITE_H -------------------------------------------------------------------------------- /.github/workflows/ccp-workflow.yml: -------------------------------------------------------------------------------- 1 | # taken from https://github.com/onqtam/doctest/blob/master/.github/workflows/main.yml 2 | 3 | name: C/C++ CI 4 | on: 5 | push: 6 | pull_request: 7 | types: [opened, reopened] 8 | 9 | 10 | jobs: 11 | build: 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | include: [ 16 | { system: MacOS, runner: macos-latest }, 17 | { system: Ubuntu-22, runner: ubuntu-22.04 }, 18 | { system: Ubuntu-latest, runner: ubuntu-latest }, 19 | ] 20 | name: ${{ matrix.system }} Build 21 | runs-on: ${{ matrix.runner }} 22 | steps: 23 | - uses: actions/checkout@v5 24 | - name: cmake 25 | run: cmake -DOJPH_BUILD_STREAM_EXPAND=ON .. 26 | working-directory: build 27 | - name: build 28 | run: make 29 | working-directory: build 30 | 31 | build_mac: 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | include: [ 36 | { system: MacOS Dual Build, runner: macos-latest }, 37 | ] 38 | name: ${{ matrix.system }} Build 39 | runs-on: ${{ matrix.runner }} 40 | steps: 41 | - uses: actions/checkout@v5 42 | - name: cmake 43 | run: cmake -DOJPH_BUILD_STREAM_EXPAND=ON -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DOJPH_ENABLE_TIFF_SUPPORT=OFF .. 44 | working-directory: build 45 | - name: build 46 | run: make 47 | working-directory: build 48 | 49 | build_windows: 50 | strategy: 51 | fail-fast: false 52 | matrix: 53 | include: [ 54 | { system: Windows, runner: windows-latest }, 55 | ] 56 | name: ${{ matrix.system }} Build 57 | runs-on: ${{ matrix.runner }} 58 | steps: 59 | - uses: actions/checkout@v5 60 | - name: cmake 61 | run: cmake -G "Visual Studio 17 2022" -A x64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_STREAM_EXPAND=ON .. 62 | working-directory: build 63 | - name: build 64 | run: cmake --build . --config Release 65 | working-directory: build 66 | 67 | build_msys2: 68 | strategy: 69 | fail-fast: false 70 | matrix: 71 | include: [ 72 | { system: Windows-MSYS2, runner: windows-latest }, 73 | ] 74 | name: ${{ matrix.system }} Build 75 | runs-on: ${{ matrix.runner }} 76 | defaults: 77 | run: 78 | shell: msys2 {0} 79 | steps: 80 | - uses: actions/checkout@v5 81 | - uses: msys2/setup-msys2@v2 82 | with: 83 | msystem: UCRT64 84 | update: false 85 | pacboy: cc:p cmake:p libtiff:p 86 | - name: cmake 87 | run: cmake -DOJPH_BUILD_STREAM_EXPAND=ON .. 88 | working-directory: build 89 | - name: build 90 | run: cmake --build . --config Release 91 | working-directory: build 92 | 93 | build_windows_on_arm: 94 | strategy: 95 | fail-fast: false 96 | matrix: 97 | include: [ 98 | { system: WindowsOnARM, runner: windows-11-arm }, 99 | ] 100 | name: ${{ matrix.system }} Build 101 | runs-on: ${{ matrix.runner }} 102 | steps: 103 | - uses: actions/checkout@v5 104 | - name: cmake 105 | run: cmake -G "Visual Studio 17 2022" -A ARM64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_STREAM_EXPAND=ON .. 106 | working-directory: build 107 | - name: build 108 | run: cmake --build . --config Release 109 | working-directory: build 110 | 111 | test: 112 | strategy: 113 | fail-fast: false 114 | matrix: 115 | include: [ 116 | { system: MacOS-Intel, runner: macos-15-intel }, 117 | { system: MacOS-latest, runner: macos-latest }, 118 | { system: Ubuntu-latest, runner: ubuntu-latest }, 119 | ] 120 | name: ${{ matrix.system }} Test 121 | runs-on: ${{ matrix.runner }} 122 | steps: 123 | - uses: actions/checkout@v5 124 | - name: cmake 125 | run: cmake -DOJPH_BUILD_TESTS=yes .. 126 | working-directory: build 127 | - name: build 128 | run: make 129 | working-directory: build 130 | - name: test 131 | run: ctest --output-on-failure 132 | working-directory: build 133 | 134 | test_windows: 135 | strategy: 136 | fail-fast: false 137 | matrix: 138 | include: [ 139 | { system: Windows, runner: windows-latest }, 140 | ] 141 | name: ${{ matrix.system }} Test 142 | runs-on: ${{ matrix.runner }} 143 | steps: 144 | - uses: actions/checkout@v5 145 | - name: cmake 146 | run: cmake -G "Visual Studio 17 2022" -A x64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_TESTS=ON .. 147 | working-directory: build 148 | - name: build 149 | run: cmake --build . --config Release 150 | working-directory: build 151 | - name: test 152 | run: ctest --output-on-failure -C Release 153 | working-directory: build 154 | 155 | test_msys2: 156 | strategy: 157 | fail-fast: false 158 | matrix: 159 | include: [ 160 | { system: Windows-MSYS2, runner: windows-latest }, 161 | ] 162 | name: ${{ matrix.system }} Test 163 | runs-on: ${{ matrix.runner }} 164 | defaults: 165 | run: 166 | shell: msys2 {0} 167 | steps: 168 | - uses: actions/checkout@v5 169 | - uses: msys2/setup-msys2@v2 170 | with: 171 | msystem: UCRT64 172 | update: false 173 | pacboy: cc:p cmake:p python:p 174 | - name: cmake 175 | run: cmake -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_TESTS=ON -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe .. 176 | working-directory: build 177 | - name: build 178 | run: cmake --build . --config Release 179 | working-directory: build 180 | - name: test 181 | run: ctest --output-on-failure -C Release 182 | working-directory: build 183 | 184 | test_windows_on_arm: 185 | strategy: 186 | fail-fast: false 187 | matrix: 188 | include: [ 189 | { system: WindowsOnARM, runner: windows-11-arm }, 190 | ] 191 | name: ${{ matrix.system }} Test 192 | runs-on: ${{ matrix.runner }} 193 | steps: 194 | - uses: actions/checkout@v5 195 | - name: cmake 196 | run: cmake -G "Visual Studio 17 2022" -A ARM64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_TESTS=ON .. 197 | working-directory: build 198 | - name: build 199 | run: cmake --build . --config Release 200 | working-directory: build 201 | - name: test 202 | run: ctest --output-on-failure -C Release 203 | working-directory: build 204 | 205 | -------------------------------------------------------------------------------- /src/core/others/ojph_message.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_message.cpp 34 | // Author: Aous Naman 35 | // Date: 29 August 2019 36 | //***************************************************************************/ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #include "ojph_message.h" 44 | 45 | namespace ojph { 46 | 47 | //////////////////////////////////////////////////////////////////////////// 48 | FILE* info_stream = stdout; 49 | 50 | //////////////////////////////////////////////////////////////////////////// 51 | message_info info; 52 | message_info* local_info = &info; 53 | OJPH_MSG_LEVEL message_level = OJPH_MSG_LEVEL::ALL_MSG; 54 | 55 | //////////////////////////////////////////////////////////////////////////// 56 | void configure_info(message_info* info) 57 | { 58 | local_info = info; 59 | } 60 | 61 | //////////////////////////////////////////////////////////////////////////// 62 | message_info* get_info() 63 | { 64 | return local_info; 65 | } 66 | 67 | //////////////////////////////////////////////////////////////////////////// 68 | void set_info_stream(FILE* s) 69 | { 70 | info_stream = s; 71 | } 72 | 73 | //////////////////////////////////////////////////////////////////////////// 74 | void message_info::operator()(int info_code, const char* file_name, 75 | int line_num, const char* fmt, ...) 76 | { 77 | if (info_stream == NULL || message_level > OJPH_MSG_LEVEL::INFO) 78 | return; 79 | 80 | fprintf(info_stream, "ojph info 0x%08X at %s:%d: ", 81 | info_code, file_name, line_num); 82 | va_list args; 83 | va_start(args, fmt); 84 | vfprintf(info_stream, fmt, args); 85 | fprintf(info_stream, "\n"); 86 | va_end(args); 87 | } 88 | 89 | //////////////////////////////////////////////////////////////////////////// 90 | FILE *warning_stream = stdout; 91 | 92 | //////////////////////////////////////////////////////////////////////////// 93 | message_warning warn; 94 | message_warning* local_warn = &warn; 95 | 96 | //////////////////////////////////////////////////////////////////////////// 97 | void configure_warning(message_warning* warn) 98 | { 99 | local_warn = warn; 100 | } 101 | 102 | //////////////////////////////////////////////////////////////////////////// 103 | message_warning* get_warning() 104 | { 105 | return local_warn; 106 | } 107 | 108 | //////////////////////////////////////////////////////////////////////////// 109 | void set_warning_stream(FILE *s) 110 | { 111 | warning_stream = s; 112 | } 113 | 114 | //////////////////////////////////////////////////////////////////////////// 115 | void message_warning::operator()(int warn_code, const char* file_name, 116 | int line_num, const char *fmt, ...) 117 | { 118 | if (warning_stream == NULL || message_level > OJPH_MSG_LEVEL::WARN) 119 | return; 120 | 121 | fprintf(warning_stream, "ojph warning 0x%08X at %s:%d: ", 122 | warn_code, file_name, line_num); 123 | va_list args; 124 | va_start(args, fmt); 125 | vfprintf(warning_stream, fmt, args); 126 | fprintf(warning_stream, "\n"); 127 | va_end(args); 128 | } 129 | 130 | //////////////////////////////////////////////////////////////////////////// 131 | FILE *error_stream = stderr; 132 | 133 | //////////////////////////////////////////////////////////////////////////// 134 | message_error error; 135 | message_error* local_error = &error; 136 | 137 | //////////////////////////////////////////////////////////////////////////// 138 | void configure_error(message_error* error) 139 | { 140 | local_error = error; 141 | } 142 | 143 | //////////////////////////////////////////////////////////////////////////// 144 | message_error* get_error() 145 | { 146 | return local_error; 147 | } 148 | 149 | //////////////////////////////////////////////////////////////////////////// 150 | void set_error_stream(FILE *s) 151 | { 152 | error_stream = s; 153 | } 154 | 155 | //////////////////////////////////////////////////////////////////////////// 156 | void message_error::operator()(int error_code, const char* file_name, 157 | int line_num, const char *fmt, ...) 158 | { 159 | if (error_stream != NULL && message_level <= OJPH_MSG_LEVEL::ERROR) 160 | { 161 | fprintf(error_stream, "ojph error 0x%08X at %s:%d: ", 162 | error_code, file_name, line_num); 163 | va_list args; 164 | va_start(args, fmt); 165 | vfprintf(error_stream, fmt, args); 166 | fprintf(error_stream, "\n"); 167 | va_end(args); 168 | } 169 | 170 | throw std::runtime_error("ojph error"); 171 | } 172 | 173 | //////////////////////////////////////////////////////////////////////////// 174 | void set_message_level(OJPH_MSG_LEVEL level) 175 | { 176 | assert(level >= OJPH_MSG_LEVEL::ALL_MSG && 177 | level <= OJPH_MSG_LEVEL::NO_MSG); 178 | message_level = level; 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_codestream_gen.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2022, Aous Naman 6 | // Copyright (c) 2022, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2022, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codestream_gen.cpp 34 | // Author: Aous Naman 35 | // Date: 15 May 2022 36 | //***************************************************************************/ 37 | 38 | #include "ojph_defs.h" 39 | #include "ojph_arch.h" 40 | 41 | namespace ojph { 42 | namespace local { 43 | 44 | ////////////////////////////////////////////////////////////////////////// 45 | void gen_mem_clear(void* addr, size_t count) 46 | { 47 | si64* p = (si64*)addr; 48 | for (size_t i = 0; i < count; i += 8) 49 | *p++ = 0; 50 | } 51 | 52 | ////////////////////////////////////////////////////////////////////////// 53 | ui32 gen_find_max_val32(ui32* addr) { return addr[0]; } 54 | 55 | ////////////////////////////////////////////////////////////////////////// 56 | ui64 gen_find_max_val64(ui64* addr) { return addr[0]; } 57 | 58 | ////////////////////////////////////////////////////////////////////////// 59 | void gen_rev_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max, 60 | float delta_inv, ui32 count, 61 | ui32* max_val) 62 | { 63 | ojph_unused(delta_inv); 64 | ui32 shift = 31 - K_max; 65 | // convert to sign and magnitude and keep max_val 66 | ui32 tmax = *max_val; 67 | si32 *p = (si32*)sp; 68 | for (ui32 i = count; i > 0; --i) 69 | { 70 | si32 v = *p++; 71 | ui32 sign = v >= 0 ? 0U : 0x80000000U; 72 | ui32 val = (ui32)(v >= 0 ? v : -v); 73 | val <<= shift; 74 | *dp++ = sign | val; 75 | tmax |= val; // it is more efficient to use or than max 76 | } 77 | *max_val = tmax; 78 | } 79 | 80 | ////////////////////////////////////////////////////////////////////////// 81 | void gen_rev_tx_to_cb64(const void *sp, ui64 *dp, ui32 K_max, 82 | float delta_inv, ui32 count, 83 | ui64* max_val) 84 | { 85 | ojph_unused(delta_inv); 86 | ui32 shift = 63 - K_max; 87 | // convert to sign and magnitude and keep max_val 88 | ui64 tmax = *max_val; 89 | si64 *p = (si64*)sp; 90 | for (ui32 i = count; i > 0; --i) 91 | { 92 | si64 v = *p++; 93 | ui64 sign = v >= 0 ? 0ULL : 0x8000000000000000ULL; 94 | ui64 val = (ui64)(v >= 0 ? v : -v); 95 | val <<= shift; 96 | *dp++ = sign | val; 97 | tmax |= val; // it is more efficient to use or than max 98 | } 99 | *max_val = tmax; 100 | } 101 | 102 | ////////////////////////////////////////////////////////////////////////// 103 | void gen_irv_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max, 104 | float delta_inv, ui32 count, 105 | ui32* max_val) 106 | { 107 | ojph_unused(K_max); 108 | //quantize and convert to sign and magnitude and keep max_val 109 | ui32 tmax = *max_val; 110 | float *p = (float*)sp; 111 | for (ui32 i = count; i > 0; --i) 112 | { 113 | float v = *p++; 114 | si32 t = ojph_trunc(v * delta_inv); 115 | ui32 sign = t >= 0 ? 0U : 0x80000000U; 116 | ui32 val = (ui32)(t >= 0 ? t : -t); 117 | *dp++ = sign | val; 118 | tmax |= val; // it is more efficient to use or than max 119 | } 120 | *max_val = tmax; 121 | } 122 | 123 | ////////////////////////////////////////////////////////////////////////// 124 | void gen_rev_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max, 125 | float delta, ui32 count) 126 | { 127 | ojph_unused(delta); 128 | ui32 shift = 31 - K_max; 129 | //convert to sign and magnitude 130 | si32 *p = (si32*)dp; 131 | for (ui32 i = count; i > 0; --i) 132 | { 133 | ui32 v = *sp++; 134 | si32 val = (v & 0x7FFFFFFFU) >> shift; 135 | *p++ = (v & 0x80000000U) ? -val : val; 136 | } 137 | } 138 | 139 | ////////////////////////////////////////////////////////////////////////// 140 | void gen_rev_tx_from_cb64(const ui64 *sp, void *dp, ui32 K_max, 141 | float delta, ui32 count) 142 | { 143 | ojph_unused(delta); 144 | ui32 shift = 63 - K_max; 145 | //convert to sign and magnitude 146 | si64 *p = (si64*)dp; 147 | for (ui32 i = count; i > 0; --i) 148 | { 149 | ui64 v = *sp++; 150 | si64 val = (v & 0x7FFFFFFFFFFFFFFFULL) >> shift; 151 | *p++ = (v & 0x8000000000000000ULL) ? -val : val; 152 | } 153 | } 154 | 155 | ////////////////////////////////////////////////////////////////////////// 156 | void gen_irv_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max, 157 | float delta, ui32 count) 158 | { 159 | ojph_unused(K_max); 160 | //convert to sign and magnitude 161 | float *p = (float*)dp; 162 | for (ui32 i = count; i > 0; --i) 163 | { 164 | ui32 v = *sp++; 165 | float val = (float)(v & 0x7FFFFFFFU) * delta; 166 | *p++ = (v & 0x80000000U) ? -val : val; 167 | } 168 | } 169 | 170 | } 171 | } -------------------------------------------------------------------------------- /src/core/codestream/ojph_codestream_local.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codestream_local.h 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #ifndef OJPH_CODESTREAM_LOCAL_H 40 | #define OJPH_CODESTREAM_LOCAL_H 41 | 42 | #include "ojph_defs.h" 43 | #include "ojph_params_local.h" 44 | 45 | namespace ojph { 46 | 47 | //////////////////////////////////////////////////////////////////////////// 48 | //defined elsewhere 49 | class line_buf; 50 | class mem_fixed_allocator; 51 | class mem_elastic_allocator; 52 | class codestream; 53 | 54 | namespace local { 55 | 56 | ///////////////////////////////////////////////////////////////////////// 57 | static inline 58 | ui16 swap_byte(ui16 t) 59 | { 60 | return (ui16)((t << 8) | (t >> 8)); 61 | } 62 | 63 | ////////////////////////////////////////////////////////////////////////// 64 | //defined elsewhere 65 | class tile; 66 | 67 | ////////////////////////////////////////////////////////////////////////// 68 | class codestream 69 | { 70 | friend ::ojph::codestream; 71 | 72 | public: 73 | codestream(); 74 | ~codestream(); 75 | 76 | void restart(); 77 | 78 | void pre_alloc(); 79 | void finalize_alloc(); 80 | 81 | ojph::param_siz access_siz() // returns externally wrapped siz 82 | { return ojph::param_siz(&siz); } 83 | const param_siz* get_siz() // returns internal siz 84 | { return &siz; } 85 | ojph::param_cod access_cod() // returns externally wrapped cod 86 | { return ojph::param_cod(&cod); } 87 | const param_cod* get_cod() // returns internal cod 88 | { return &cod; } 89 | const param_cod* get_coc(ui32 comp_num) // returns internal cod 90 | { return cod.get_coc(comp_num); } 91 | const param_qcd* access_qcd() 92 | { return &qcd; } 93 | const param_dfs* access_dfs() 94 | { if (dfs.exists()) return &dfs; else return NULL; } 95 | const param_nlt* get_nlt() 96 | { return ≮ } 97 | mem_fixed_allocator* get_allocator() { return allocator; } 98 | mem_elastic_allocator* get_elastic_alloc() { return elastic_alloc; } 99 | outfile_base* get_file() { return outfile; } 100 | 101 | line_buf* exchange(line_buf* line, ui32& next_component); 102 | void write_headers(outfile_base *file, const comment_exchange* comments, 103 | ui32 num_comments); 104 | void enable_resilience(); 105 | bool is_resilient() { return resilient; } 106 | void read_headers(infile_base *file); 107 | void restrict_input_resolution(ui32 skipped_res_for_data, 108 | ui32 skipped_res_for_recon); 109 | void read(); 110 | void set_planar(int planar); 111 | void set_profile(const char *s); 112 | void set_tilepart_divisions(ui32 value); 113 | void request_tlm_marker(bool needed); 114 | line_buf* pull(ui32 &comp_num); 115 | void flush(); 116 | void close(); 117 | 118 | bool is_planar() const { return planar != 0; } 119 | si32 get_profile() const { return profile; }; 120 | ui32 get_tilepart_div() const { return tilepart_div; }; 121 | bool is_tlm_needed() const { return need_tlm; }; 122 | 123 | void check_imf_validity(); 124 | void check_broadcast_validity(); 125 | 126 | ui8* get_precinct_scratch() { return precinct_scratch; } 127 | ui32 get_skipped_res_for_recon() 128 | { return skipped_res_for_recon; } 129 | ui32 get_skipped_res_for_read() 130 | { return skipped_res_for_read; } 131 | 132 | private: 133 | ui32 precinct_scratch_needed_bytes; 134 | ui8* precinct_scratch; 135 | 136 | private: 137 | ui32 cur_line; 138 | ui32 cur_comp; 139 | ui32 cur_tile_row; 140 | bool resilient; 141 | ui32 skipped_res_for_read, skipped_res_for_recon; 142 | 143 | private: 144 | size num_tiles; 145 | tile *tiles; 146 | line_buf* lines; 147 | ui32 num_comps; 148 | size *comp_size; //stores full resolution no. of lines and width 149 | size *recon_comp_size; //stores number of lines and width of each comp 150 | bool employ_color_transform; 151 | int planar; 152 | int profile; 153 | ui32 tilepart_div; // tilepart division value 154 | bool need_tlm; // true if tlm markers are needed 155 | 156 | private: 157 | param_siz siz; // image and tile size 158 | param_cod cod; // coding style default 159 | param_cap cap; // extended capabilities 160 | param_qcd qcd; // quantization default 161 | param_tlm tlm; // tile-part lengths 162 | param_nlt nlt; // non-linearity point transformation 163 | 164 | private: // these are from Part 2 of the standard 165 | param_dfs dfs; // downsmapling factor styles 166 | param_atk atk; // wavelet structure and coefficients 167 | 168 | private: 169 | mem_fixed_allocator *allocator; 170 | mem_elastic_allocator *elastic_alloc; 171 | outfile_base *outfile; 172 | infile_base *infile; 173 | }; 174 | 175 | } 176 | } 177 | 178 | #endif // !OJPH_CODESTREAM_LOCAL_H 179 | -------------------------------------------------------------------------------- /src/apps/others/ojph_sockets.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2024, Aous Naman 6 | // Copyright (c) 2024, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2024, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_socket.cpp 34 | // Author: Aous Naman 35 | // Date: 17 April 2024 36 | //***************************************************************************/ 37 | 38 | #include 39 | #include 40 | #include "ojph_message.h" 41 | #include "ojph_sockets.h" 42 | 43 | namespace ojph 44 | { 45 | namespace net 46 | { 47 | 48 | /////////////////////////////////////////////////////////////////////////// 49 | // 50 | // 51 | // 52 | // 53 | // 54 | /////////////////////////////////////////////////////////////////////////// 55 | 56 | /////////////////////////////////////////////////////////////////////////// 57 | socket::socket(const ojph_socket& s) 58 | { 59 | this->s = s; 60 | } 61 | 62 | /////////////////////////////////////////////////////////////////////////// 63 | void socket::close() 64 | { 65 | 66 | if (s != OJPH_INVALID_SOCKET) 67 | { 68 | #ifdef OJPH_OS_WINDOWS 69 | ::closesocket(s); 70 | #else 71 | ::close(s); 72 | #endif 73 | s = OJPH_INVALID_SOCKET; 74 | } 75 | } 76 | 77 | /////////////////////////////////////////////////////////////////////////// 78 | bool socket::set_blocking_mode(bool block) 79 | { 80 | #ifdef OJPH_OS_WINDOWS 81 | u_long mode = block ? 0 : 1; 82 | return ioctlsocket(s, FIONBIO, &mode) == 0; 83 | #else 84 | int flags = fcntl(s, F_GETFL); 85 | if (flags == -1) // error 86 | return false; 87 | if (block) 88 | flags &= ~O_NONBLOCK; 89 | else 90 | flags |= O_NONBLOCK; 91 | return fcntl(s, F_SETFL, flags) != -1; 92 | #endif 93 | } 94 | 95 | /////////////////////////////////////////////////////////////////////////// 96 | // 97 | // 98 | // 99 | // 100 | // 101 | /////////////////////////////////////////////////////////////////////////// 102 | 103 | /////////////////////////////////////////////////////////////////////////// 104 | int socket_manager::ojph_socket_manager_counter = 0; 105 | 106 | /////////////////////////////////////////////////////////////////////////// 107 | socket_manager::socket_manager() 108 | { 109 | if (ojph_socket_manager_counter == 0) 110 | { 111 | #ifdef OJPH_OS_WINDOWS 112 | WSADATA wsa; 113 | if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) 114 | { 115 | std::string err = get_last_error_message(); 116 | OJPH_ERROR(0x00080001, "Could not create socket : %s\n", err.data()); 117 | } 118 | #endif 119 | } 120 | ++ojph_socket_manager_counter; 121 | } 122 | 123 | /////////////////////////////////////////////////////////////////////////// 124 | socket_manager::~socket_manager() 125 | { 126 | assert(ojph_socket_manager_counter >= 1); 127 | --ojph_socket_manager_counter; 128 | if (ojph_socket_manager_counter == 0) 129 | { 130 | #ifdef OJPH_OS_WINDOWS 131 | WSACleanup(); 132 | #endif 133 | } 134 | } 135 | 136 | /////////////////////////////////////////////////////////////////////////// 137 | socket socket_manager::create_socket(int domain, int type, int protocol) 138 | { 139 | socket s(::socket(domain, type, protocol)); 140 | return s; 141 | } 142 | 143 | /////////////////////////////////////////////////////////////////////////// 144 | int socket_manager::get_last_error() 145 | { 146 | #ifdef OJPH_OS_WINDOWS 147 | return WSAGetLastError(); 148 | #else 149 | return errno; 150 | #endif 151 | } 152 | 153 | /////////////////////////////////////////////////////////////////////////// 154 | std::string socket_manager::get_error_message(int errnum) 155 | { 156 | if( errnum == 0 ) 157 | return std::string(""); 158 | const int max_buf_size = 1024; 159 | char buf[max_buf_size]; 160 | char *v = buf; 161 | #ifdef OJPH_OS_WINDOWS 162 | size_t size = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM 163 | | FORMAT_MESSAGE_IGNORE_INSERTS, 164 | NULL, errnum, 165 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 166 | buf, max_buf_size, NULL); 167 | buf[max_buf_size - 1] = 0; 168 | #elif (defined __GLIBC__) && \ 169 | ((defined _GNU_SOURCE) || (_POSIX_C_SOURCE < 200112L)) 170 | v = strerror_r(errnum, (char*)buf, max_buf_size); 171 | #else 172 | // it is not clear if the returned value is in buf or in v 173 | int t = strerror_r(errnum, (char*)buf, max_buf_size); 174 | if (t != 0) 175 | OJPH_ERROR(0x00080002, "Error retrieving a text message for " 176 | "socket error number %d\n", errnum); 177 | buf[max_buf_size - 1] = 0; 178 | #endif 179 | std::string str; 180 | str = v; 181 | return str; 182 | } 183 | 184 | /////////////////////////////////////////////////////////////////////////// 185 | std::string socket_manager::get_last_error_message() 186 | { 187 | int errnum = get_last_error(); 188 | return get_error_message(errnum); 189 | } 190 | 191 | /////////////////////////////////////////////////////////////////////////// 192 | ui32 socket_manager::get_addr(const sockaddr_in& addr) 193 | { 194 | #ifdef OJPH_OS_WINDOWS 195 | return addr.sin_addr.S_un.S_addr; 196 | #else 197 | return addr.sin_addr.s_addr; 198 | #endif 199 | } 200 | 201 | } // !net namespace 202 | } // !ojph namespace 203 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_codestream.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_codestream.cpp 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #include 40 | #include 41 | 42 | #include "ojph_mem.h" 43 | #include "ojph_params.h" 44 | #include "ojph_codestream.h" 45 | #include "ojph_codestream_local.h" 46 | 47 | namespace ojph { 48 | 49 | //////////////////////////////////////////////////////////////////////////// 50 | // 51 | // 52 | // 53 | // 54 | // 55 | //////////////////////////////////////////////////////////////////////////// 56 | 57 | //////////////////////////////////////////////////////////////////////////// 58 | codestream::~codestream() 59 | { 60 | if (state) 61 | delete state; 62 | state = NULL; 63 | } 64 | 65 | //////////////////////////////////////////////////////////////////////////// 66 | codestream::codestream() 67 | { 68 | state = new local::codestream; 69 | } 70 | 71 | //////////////////////////////////////////////////////////////////////////// 72 | void codestream::restart() 73 | { 74 | assert(state != NULL); 75 | state->restart(); 76 | } 77 | 78 | //////////////////////////////////////////////////////////////////////////// 79 | param_siz codestream::access_siz() 80 | { 81 | return param_siz(&state->siz); 82 | } 83 | 84 | //////////////////////////////////////////////////////////////////////////// 85 | param_cod codestream::access_cod() 86 | { 87 | return param_cod(&state->cod); 88 | } 89 | 90 | //////////////////////////////////////////////////////////////////////////// 91 | param_qcd codestream::access_qcd() 92 | { 93 | return param_qcd(&state->qcd); 94 | } 95 | 96 | //////////////////////////////////////////////////////////////////////////// 97 | param_nlt codestream::access_nlt() 98 | { 99 | return param_nlt(&state->nlt); 100 | } 101 | 102 | //////////////////////////////////////////////////////////////////////////// 103 | void codestream::set_planar(bool planar) 104 | { 105 | state->set_planar(planar ? 1 : 0); 106 | } 107 | 108 | //////////////////////////////////////////////////////////////////////////// 109 | void codestream::set_profile(const char *s) 110 | { 111 | state->set_profile(s); 112 | } 113 | 114 | //////////////////////////////////////////////////////////////////////////// 115 | void codestream::set_tilepart_divisions(bool at_resolutions, 116 | bool at_components) 117 | { 118 | ui32 value = 0; 119 | if (at_resolutions) 120 | value |= OJPH_TILEPART_RESOLUTIONS; 121 | if (at_components) 122 | value |= OJPH_TILEPART_COMPONENTS; 123 | state->set_tilepart_divisions(value); 124 | } 125 | 126 | //////////////////////////////////////////////////////////////////////////// 127 | bool codestream::is_tilepart_division_at_resolutions() 128 | { 129 | ui32 res = state->get_tilepart_div() & OJPH_TILEPART_RESOLUTIONS; 130 | return res ? true : false; 131 | } 132 | 133 | //////////////////////////////////////////////////////////////////////////// 134 | bool codestream::is_tilepart_division_at_components() 135 | { 136 | ui32 comp = state->get_tilepart_div() & OJPH_TILEPART_COMPONENTS; 137 | return comp ? true : false; 138 | } 139 | 140 | //////////////////////////////////////////////////////////////////////////// 141 | void codestream::request_tlm_marker(bool needed) 142 | { 143 | state->request_tlm_marker(needed); 144 | } 145 | 146 | //////////////////////////////////////////////////////////////////////////// 147 | bool codestream::is_tlm_requested() 148 | { 149 | return state->is_tlm_needed(); 150 | } 151 | 152 | //////////////////////////////////////////////////////////////////////////// 153 | bool codestream::is_planar() const 154 | { 155 | return state->is_planar(); 156 | } 157 | 158 | //////////////////////////////////////////////////////////////////////////// 159 | void codestream::write_headers(outfile_base *file, 160 | const comment_exchange* comments, 161 | ui32 num_comments) 162 | { 163 | state->write_headers(file, comments, num_comments); 164 | } 165 | 166 | //////////////////////////////////////////////////////////////////////////// 167 | void codestream::enable_resilience() 168 | { 169 | state->enable_resilience(); 170 | } 171 | 172 | //////////////////////////////////////////////////////////////////////////// 173 | void codestream::read_headers(infile_base *file) 174 | { 175 | state->read_headers(file); 176 | } 177 | 178 | //////////////////////////////////////////////////////////////////////////// 179 | void codestream::restrict_input_resolution(ui32 skipped_res_for_read, 180 | ui32 skipped_res_for_recon) 181 | { 182 | state->restrict_input_resolution(skipped_res_for_read, 183 | skipped_res_for_recon); 184 | } 185 | 186 | //////////////////////////////////////////////////////////////////////////// 187 | void codestream::create() 188 | { 189 | state->read(); 190 | } 191 | 192 | //////////////////////////////////////////////////////////////////////////// 193 | line_buf* codestream::pull(ui32 &comp_num) 194 | { 195 | return state->pull(comp_num); 196 | } 197 | 198 | 199 | //////////////////////////////////////////////////////////////////////////// 200 | void codestream::flush() 201 | { 202 | state->flush(); 203 | } 204 | 205 | //////////////////////////////////////////////////////////////////////////// 206 | void codestream::close() 207 | { 208 | state->close(); 209 | } 210 | 211 | //////////////////////////////////////////////////////////////////////////// 212 | line_buf* codestream::exchange(line_buf* line, ui32& next_component) 213 | { 214 | return state->exchange(line, next_component); 215 | } 216 | 217 | } 218 | -------------------------------------------------------------------------------- /src/core/codestream/ojph_tile_comp.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************/ 2 | // This software is released under the 2-Clause BSD license, included 3 | // below. 4 | // 5 | // Copyright (c) 2019, Aous Naman 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia 7 | // Copyright (c) 2019, The University of New South Wales, Australia 8 | // 9 | // Redistribution and use in source and binary forms, with or without 10 | // modification, are permitted provided that the following conditions are 11 | // met: 12 | // 13 | // 1. Redistributions of source code must retain the above copyright 14 | // notice, this list of conditions and the following disclaimer. 15 | // 16 | // 2. Redistributions in binary form must reproduce the above copyright 17 | // notice, this list of conditions and the following disclaimer in the 18 | // documentation and/or other materials provided with the distribution. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | //***************************************************************************/ 32 | // This file is part of the OpenJPH software implementation. 33 | // File: ojph_tile_comp.cpp 34 | // Author: Aous Naman 35 | // Date: 28 August 2019 36 | //***************************************************************************/ 37 | 38 | 39 | #include 40 | #include 41 | 42 | #include "ojph_mem.h" 43 | #include "ojph_params.h" 44 | #include "ojph_codestream_local.h" 45 | #include "ojph_tile_comp.h" 46 | #include "ojph_resolution.h" 47 | 48 | namespace ojph { 49 | 50 | namespace local 51 | { 52 | 53 | ////////////////////////////////////////////////////////////////////////// 54 | void tile_comp::pre_alloc(codestream *codestream, ui32 comp_num, 55 | const rect& comp_rect, 56 | const rect& recon_comp_rect) 57 | { 58 | mem_fixed_allocator* allocator = codestream->get_allocator(); 59 | 60 | //allocate a resolution 61 | ui32 num_decomps; 62 | num_decomps = codestream->get_coc(comp_num)->get_num_decompositions(); 63 | allocator->pre_alloc_obj(1); 64 | 65 | resolution::pre_alloc(codestream, comp_rect, recon_comp_rect, comp_num, 66 | num_decomps); 67 | } 68 | 69 | ////////////////////////////////////////////////////////////////////////// 70 | void tile_comp::finalize_alloc(codestream *codestream, tile *parent, 71 | ui32 comp_num, const rect& comp_rect, 72 | const rect& recon_comp_rect) 73 | { 74 | mem_fixed_allocator* allocator = codestream->get_allocator(); 75 | 76 | //allocate a resolution 77 | num_decomps = codestream->get_coc(comp_num)->get_num_decompositions(); 78 | 79 | comp_downsamp = codestream->get_siz()->get_downsampling(comp_num); 80 | this->comp_rect = comp_rect; 81 | this->parent_tile = parent; 82 | 83 | this->comp_num = comp_num; 84 | this->num_bytes = 0; 85 | res = allocator->post_alloc_obj(1); 86 | res->finalize_alloc(codestream, comp_rect, recon_comp_rect, comp_num, 87 | num_decomps, comp_downsamp, comp_downsamp, this, 88 | NULL); 89 | } 90 | 91 | ////////////////////////////////////////////////////////////////////////// 92 | line_buf* tile_comp::get_line() 93 | { 94 | return res->get_line(); 95 | } 96 | 97 | ////////////////////////////////////////////////////////////////////////// 98 | void tile_comp::push_line() 99 | { 100 | res->push_line(); 101 | } 102 | 103 | ////////////////////////////////////////////////////////////////////////// 104 | line_buf* tile_comp::pull_line() 105 | { 106 | return res->pull_line(); 107 | } 108 | 109 | ////////////////////////////////////////////////////////////////////////// 110 | ui32 tile_comp::prepare_precincts() 111 | { 112 | this->num_bytes = res->prepare_precinct(); 113 | return this->num_bytes; 114 | } 115 | 116 | ////////////////////////////////////////////////////////////////////////// 117 | void tile_comp::write_precincts(ui32 res_num, outfile_base *file) 118 | { 119 | assert(res_num <= num_decomps); 120 | res_num = num_decomps - res_num; //how many levels to go down 121 | resolution *r = res; 122 | while (res_num > 0 && r != NULL) 123 | { 124 | r = r->next_resolution(); 125 | --res_num; 126 | } 127 | if (r) //resolution does not exist if r is NULL 128 | r->write_precincts(file); 129 | } 130 | 131 | ////////////////////////////////////////////////////////////////////////// 132 | bool tile_comp::get_top_left_precinct(ui32 res_num, point &top_left) 133 | { 134 | int resolution_num = (int)num_decomps - (int)res_num; 135 | resolution *r = res; 136 | while (resolution_num > 0 && r != NULL) 137 | { 138 | r = r->next_resolution(); 139 | --resolution_num; 140 | } 141 | if (r) //resolution does not exist if r is NULL 142 | return r->get_top_left_precinct(top_left); 143 | else 144 | return false; 145 | } 146 | 147 | ////////////////////////////////////////////////////////////////////////// 148 | void tile_comp::write_one_precinct(ui32 res_num, outfile_base *file) 149 | { 150 | int resolution_num = (int)num_decomps - (int)res_num; 151 | resolution *r = res; 152 | while (resolution_num > 0 && r != NULL) 153 | { 154 | r = r->next_resolution(); 155 | --resolution_num; 156 | } 157 | if (r) //resolution does not exist if r is NULL 158 | r->write_one_precinct(file); 159 | } 160 | 161 | ////////////////////////////////////////////////////////////////////////// 162 | void tile_comp::parse_precincts(ui32 res_num, ui32& data_left, 163 | infile_base *file) 164 | { 165 | assert(res_num <= num_decomps); 166 | res_num = num_decomps - res_num; //how many levels to go down 167 | resolution *r = res; 168 | while (res_num > 0 && r != NULL) 169 | { 170 | r = r->next_resolution(); 171 | --res_num; 172 | } 173 | if (r) //resolution does not exist if r is NULL 174 | r->parse_all_precincts(data_left, file); 175 | } 176 | 177 | 178 | ////////////////////////////////////////////////////////////////////////// 179 | void tile_comp::parse_one_precinct(ui32 res_num, ui32& data_left, 180 | infile_base *file) 181 | { 182 | assert(res_num <= num_decomps); 183 | res_num = num_decomps - res_num; 184 | resolution *r = res; 185 | while (res_num > 0 && r != NULL) 186 | { 187 | r = r->next_resolution(); 188 | --res_num; 189 | } 190 | if (r) //resolution does not exist if r is NULL 191 | r->parse_one_precinct(data_left, file); 192 | } 193 | 194 | ////////////////////////////////////////////////////////////////////////// 195 | ui32 tile_comp::get_num_bytes(ui32 resolution_num) const 196 | { 197 | return res->get_num_bytes(resolution_num); 198 | } 199 | } 200 | } 201 | --------------------------------------------------------------------------------