Skip to content

Commit

Permalink
fiddling...
Browse files Browse the repository at this point in the history
  • Loading branch information
ptahmose committed Oct 5, 2024
1 parent 9cdca8d commit 95a1a43
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 187 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,5 @@ FodyWeavers.xsd

out/
build/
/oct2
/oct
54 changes: 36 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/modules" ${CMAKE_MODULE_PATH})

project ("MEXlibCZI")

option(MEXLIBCZI_BUILD_MATLABMEX "Build mex-file for MATLAB" ON)
option(MEXLIBCZI_BUILD_OCTAVEOCT "Build oct-file for Octave" OFF)


# include RapidJSON -> https://www.jibbow.com/posts/rapidjson-cmake/
include("${CMAKE_SOURCE_DIR}/modules/rapidJSON.cmake")

Expand All @@ -28,26 +32,40 @@ FetchContent_GetProperties(libCZI)
set(LIBCZI_INCLUDE_DIR "${libczi_SOURCE_DIR}/Src/libCZI")
message(STATUS ${LIBCZI_INCLUDE_DIR})

if(MEXLIBCZI_HEADERS)
set(Matlab_INCLUDE_DIRS ${MEXLIBCZI_HEADERS})
if(UNIX)
set(Matlab_MEX_LIBRARY ${MEXLIBCZI_LIBS}/libmex.so)
set(Matlab_MX_LIBRARY ${MEXLIBCZI_LIBS}/libmx.so)
else()
set(Matlab_MEX_LIBRARY ${MEXLIBCZI_LIBS}/libmex.lib)
set(Matlab_MX_LIBRARY ${MEXLIBCZI_LIBS}/libmx.lib)
endif()
else()
find_package(Matlab REQUIRED)
if(MEXLIBCZI_BUILD_MATLABMEX)
if(MEXLIBCZI_HEADERS)
set(Matlab_INCLUDE_DIRS ${MEXLIBCZI_HEADERS})
if(UNIX)
set(Matlab_MEX_LIBRARY ${MEXLIBCZI_LIBS}/libmex.so)
set(Matlab_MX_LIBRARY ${MEXLIBCZI_LIBS}/libmx.so)
else()
set(Matlab_MEX_LIBRARY ${MEXLIBCZI_LIBS}/libmex.lib)
set(Matlab_MX_LIBRARY ${MEXLIBCZI_LIBS}/libmx.lib)
endif()
else()
find_package(Matlab REQUIRED)
endif()

message("########################################")
message(STATUS "Matlab_ROOT_DIR: " ${Matlab_ROOT_DIR})
message(STATUS "Matlab_INCLUDE_DIR: " ${Matlab_INCLUDE_DIRS})
message(STATUS "Matlab_MEX_LIBRARY: " ${Matlab_MEX_LIBRARY})
message(STATUS "Matlab_MX_LIBRARY: " ${Matlab_MX_LIBRARY})
message(STATUS "Matlab_LIBRARIES: " ${Matlab_LIBRARIES})
message("########################################")
endif()

message("########################################")
message(STATUS "Matlab_ROOT_DIR: " ${Matlab_ROOT_DIR})
message(STATUS "Matlab_INCLUDE_DIR: " ${Matlab_INCLUDE_DIRS})
message(STATUS "Matlab_MEX_LIBRARY: " ${Matlab_MEX_LIBRARY})
message(STATUS "Matlab_MX_LIBRARY: " ${Matlab_MX_LIBRARY})
message(STATUS "Matlab_LIBRARIES: " ${Matlab_LIBRARIES})
message("########################################")
if(MEXLIBCZI_BUILD_OCTAVEOCT)
set(OCTAVE_ROOT_DIR "D:/SW/octave-9.2.0-w64")
set(OCTAVE_CONFIGEXECUTABLE_PATHHINT "D:/SW/octave-9.2.0-w64/mingw64/bin/")
find_package(Octave REQUIRED)
message("########################################")
message(STATUS "Octave_INCLUDE_DIRS: " ${OCTAVE_INCLUDE_DIRS})
message(STATUS "Octave_LIBRARIES: " ${OCTAVE_LIBRARIES})
message("########################################")
endif()




#[[
Expand Down
56 changes: 53 additions & 3 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": []
"variables": [
{
"name": "MEXLIBCZI_BUILD_MATLABMEX",
"value": "False",
"type": "BOOL"
},
{
"name": "MEXLIBCZI_BUILD_OCTAVEOCT",
"value": "True",
"type": "BOOL"
}
]
},
{
"name": "x64-Release",
Expand All @@ -21,8 +32,47 @@
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
"inheritEnvironments": [ "msvc_x64_x64" ]
},
{
"name": "Mingw64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"inheritEnvironments": [ "mingw_64" ],
"environments": [
{
"MINGW64_ROOT": "D:\\SW\\msys64\\ucrt64",
"BIN_ROOT": "${env.MINGW64_ROOT}/bin",
"FLAVOR": "x86_64-w64-ucrt64",
"TOOLSET_VERSION": "9.1.0",
"PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
"INCLUDE": "${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
"environment": "mingw_64"
}
],
"variables": [
{
"name": "CMAKE_C_COMPILER",
"value": "${env.BIN_ROOT}/gcc.exe",
"type": "STRING"
},
{
"name": "CMAKE_CXX_COMPILER",
"value": "${env.BIN_ROOT}/g++.exe",
"type": "STRING"
},
{
"name": "MEXLIBCZI_BUILD_MATLABMEX",
"value": "False",
"type": "BOOL"
}
],
"intelliSenseMode": "linux-gcc-x64",
"cmakeCommandArgs": "-DMEXLIBCZI_BUILD_MATLABMEX=OFF -DMEXLIBCZI_BUILD_OCTAVEOCT=ON"
}
]
}
71 changes: 50 additions & 21 deletions MEXlibCZI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,65 @@ set(mexoctaveSourceFiles
"src/include_rapidjson.h"
)

add_library (MEXlibCZI SHARED
${mexoctaveSourceFiles})
if(MEXLIBCZI_BUILD_MATLABMEX)
add_library (MEXlibCZI SHARED
${mexoctaveSourceFiles})

set_target_properties(MEXlibCZI PROPERTIES CXX_STANDARD 17)
set_target_properties(MEXlibCZI PROPERTIES CXX_STANDARD 17)

target_include_directories(MEXlibCZI PRIVATE ${Matlab_INCLUDE_DIRS} ${RAPIDJSON_INCLUDE_DIR})
target_include_directories(MEXlibCZI PRIVATE ${Matlab_INCLUDE_DIRS} ${RAPIDJSON_INCLUDE_DIR})

target_include_directories(MEXlibCZI PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # this is necessary so that we can find "wllczi_Config.h" which we created above
target_include_directories(MEXlibCZI PRIVATE ${LIBCZI_INCLUDE_DIR})
target_link_libraries(MEXlibCZI PRIVATE ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY})
target_link_libraries(MEXlibCZI PRIVATE libCZIStatic JxrDecodeStatic)
target_include_directories(MEXlibCZI PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # this is necessary so that we can find "wllczi_Config.h" which we created above
target_include_directories(MEXlibCZI PRIVATE ${LIBCZI_INCLUDE_DIR})
target_link_libraries(MEXlibCZI PRIVATE ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY})
target_link_libraries(MEXlibCZI PRIVATE libCZIStatic JxrDecodeStatic)

if(WIN32)
set_target_properties(MEXlibCZI PROPERTIES SUFFIX ".mexw64")
elseif(UNIX)
set_target_properties(MEXlibCZI PROPERTIES SUFFIX ".mexa64")
target_link_options(MEXlibCZI PRIVATE -static-libgcc -static-libstdc++ )
set_target_properties(MEXlibCZI PROPERTIES PREFIX "") # otherwise "lib" is prepended
endif()
if(WIN32)
set_target_properties(MEXlibCZI PROPERTIES SUFFIX ".mexw64")
elseif(UNIX)
set_target_properties(MEXlibCZI PROPERTIES SUFFIX ".mexa64")
target_link_options(MEXlibCZI PRIVATE -static-libgcc -static-libstdc++ )
set_target_properties(MEXlibCZI PROPERTIES PREFIX "") # otherwise "lib" is prepended
endif()

SET_TARGET_PROPERTIES (MEXlibCZI PROPERTIES DEFINE_SYMBOL "MATLABMEXBUILD")
SET_TARGET_PROPERTIES (MEXlibCZI PROPERTIES DEFINE_SYMBOL "MATLABMEXBUILD")

# this definition is necessary so that "mexFunction" is exported
target_compile_definitions(MEXlibCZI PRIVATE MW_NEEDS_VERSION_H=1)
# this definition is necessary so that "mexFunction" is exported
target_compile_definitions(MEXlibCZI PRIVATE MW_NEEDS_VERSION_H=1)

if(WIN32)
target_compile_definitions(MEXlibCZI PRIVATE _WIN32API=1)
if(WIN32)
target_compile_definitions(MEXlibCZI PRIVATE _WIN32API=1)
endif()
endif()

#------------------------------------------------------------------------------------------
if(MEXLIBCZI_BUILD_OCTAVEOCT)
add_library (OctlibCZI SHARED
${mexoctaveSourceFiles})

set_target_properties(OctlibCZI PROPERTIES CXX_STANDARD 17)
target_include_directories(OctlibCZI PRIVATE ${OCTAVE_INCLUDE_DIRS} ${RAPIDJSON_INCLUDE_DIR})

target_include_directories(OctlibCZI PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # this is necessary so that we can find "wllczi_Config.h" which we created above
target_include_directories(OctlibCZI PRIVATE ${LIBCZI_INCLUDE_DIR})
target_include_directories(OctlibCZI PRIVATE "D:/SW/octave-9.2.0-w64/mingw64/include/octave-9.2.0/octave")
target_include_directories(OctlibCZI PRIVATE "/d/SW/octave-9.2.0-w64/mingw64/include/octave-9.2.0/octave")
target_link_libraries(OctlibCZI PRIVATE libCZIStatic JxrDecodeStatic)
SET_TARGET_PROPERTIES(OctlibCZI PROPERTIES DEFINE_SYMBOL "OCTAVEBUILD")
#target_link_libraries(OctlibCZI PRIVATE ${OCTAVE_OCTINTERP_LIBRARY} )
target_link_libraries(OctlibCZI PRIVATE "/d/SW/octave-9.2.0-w64/mingw64/lib/octave/9.2.0/liboctinterp.dll.a")
message(STATUS "OCTAVE_OCTINTERP_LIBRARY: " ${OCTAVE_OCTINTERP_LIBRARY})
set_target_properties(OctlibCZI PROPERTIES PREFIX "") # otherwise "lib" is prepended

if(WIN32)
target_compile_definitions(OctlibCZI PRIVATE _WIN32API=1)
endif()

target_link_libraries(OctlibCZI PUBLIC pthread)

set_target_properties(OctlibCZI PROPERTIES SUFFIX ".mex")
endif()



#[[
add_library (OctlibCZI SHARED
Expand Down
2 changes: 1 addition & 1 deletion MEXlibCZI/mexFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void Initialize()
therefore no cleanup happens here).
- here it is stated that the same happens if pressing "Ctrl-C" -> https://www.mathworks.com/help/matlab/matlab_external/automatic-cleanup-of-temporary-arrays.html
*/
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
extern "C" void __declspec(dllexport) mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
if (!gIsInitialized)
{
Expand Down
6 changes: 3 additions & 3 deletions MEXlibCZI/src/CziReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ MexArray* CziReader::GetScalingAsMatlabStruct()
{
static const char* fieldNames[] = { "scaleX", "scaleY", "scaleZ" };

static const mwSize dims[2] = { 1, 1 };
static const size_t dims[2] = { 1, 1 };
auto mexApi = MexApi::GetInstance();
auto* s = mexApi.MxCreateStructArray(
2,
Expand Down Expand Up @@ -201,7 +201,7 @@ MexArray* CziReader::GetMultiChannelScalingTileComposite(const libCZI::IntRect&
vecBm.data(),
dsplHlp.GetChannelInfosArray());

mwSize dims[3] = { bitmap->GetHeight(), bitmap->GetWidth(), 3 };
size_t dims[3] = { bitmap->GetHeight(), bitmap->GetWidth(), 3 };
auto mexApi = MexApi::GetInstance();
//auto* arr = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
auto* arr = mexApi.MxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
Expand All @@ -219,7 +219,7 @@ MexArray* CziReader::GetMultiChannelScalingTileCompositeAllChannelsDisabled(cons
const auto sizeResult = accessor->CalcSize(roi, zoom);
RgbFloatColor color{ 0,0,0 };

mwSize dims[3] = { sizeResult.h, sizeResult.w, 3 };
size_t dims[3] = { sizeResult.h, sizeResult.w, 3 };
//auto* arr = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
auto* arr = MexApi::GetInstance().MxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
return arr;
Expand Down
60 changes: 3 additions & 57 deletions MEXlibCZI/src/mexapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ using namespace std;
return MexApi::instance;
}

#if defined(MATLABMEXBUILD)

double* MexApi::MxGetDoubles(const MexArray* pArr)
{
Expand Down Expand Up @@ -50,7 +49,7 @@ std::int32_t* MexApi::MxGetInt32s(const MexArray* pArr)

bool* MexApi::MxGetLogicals(const MexArray* pArr)
{
return mxGetLogicals((const mxArray*)pArr);
return (bool*)mxGetLogicals((const mxArray*)pArr);
}

std::uint32_t* MexApi::MxGetUint32s(const MexArray* pArr)
Expand Down Expand Up @@ -134,7 +133,7 @@ MexArray* MexApi::MxCreateNumericMatrix(size_t m, size_t n, mxClassID classid, m

MexArray* MexApi::MxCreateNumericArray(size_t ndim, const size_t* dims, mxClassID classid, mxComplexity flag)
{
return (MexArray*)mxCreateNumericArray(ndim, dims, classid, flag);
return (MexArray*)mxCreateNumericArray(ndim, (const mwSize*)dims, classid, flag);
}

void MexApi::MxSetFieldByNumber(MexArray* pa, size_t i, int fieldnum, MexArray* value)
Expand All @@ -144,7 +143,7 @@ void MexApi::MxSetFieldByNumber(MexArray* pa, size_t i, int fieldnum, MexArray*

MexArray* MexApi::MxCreateStructArray(size_t ndim, const size_t* dims, int nfields, const char** fieldnames)
{
return (MexArray*)mxCreateStructArray(ndim, dims, nfields, fieldnames);
return (MexArray*)mxCreateStructArray(ndim, (const mwSize*)dims, nfields, fieldnames);
}


Expand Down Expand Up @@ -214,56 +213,3 @@ const MexArray* MexApi::MxGetField(const MexArray* pArr, const char* fieldname)
return (const MexArray*)mxGetField((const mxArray*)pArr, 0, fieldname);
}

#elif defined(OCTAVEMEXBUILD)

double* MexApi::MxGetDoubles(const mxArray* pArr)
{
return (double*)mxGetData(pArr);
}

float* MexApi::MxGetSingles(const mxArray* pArr)
{
return (float*)mxGetData(pArr);
}

std::int8_t* MexApi::MxGetInt8s(const mxArray* pArr)
{
return (int8_T*)mxGetData(pArr);
}

std::uint8_t* MexApi::MxGetUint8s(const mxArray* pArr)
{
return (uint8_T*)mxGetData(pArr);
}

std::int16_t* MexApi::MxGetInt16s(const mxArray* pArr)
{
return (int16_T*)mxGetData(pArr);
}

std::uint16_t* MexApi::MxGetUint16s(const mxArray* pArr)
{
return (uint16_T*)mxGetData(pArr);
}

std::int32_t* MexApi::MxGetInt32s(const mxArray* pArr)
{
return (int32_T*)mxGetData(pArr);
}

std::uint32_t* MexApi::MxGetUint32s(const mxArray* pArr)
{
return (uint32_T*)mxGetData(pArr);
}

std::int64_t* MexApi::MxGetInt64s(const mxArray* pArr)
{
return (int64_T*)mxGetData(pArr);
}

std::uint64_t* MexApi::MxGetUint64s(const mxArray* pArr)
{
return (uint64_T*)mxGetData(pArr);
}

#endif
9 changes: 7 additions & 2 deletions MEXlibCZI/src/mexapi.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "../include_matlabheaders.h"
#include <cstdint>
#include <memory>
#include <string>

#include "../include_matlabheaders.h"

class MexArray;

class MexApi
Expand Down Expand Up @@ -90,7 +91,10 @@ class MexApi
);
};

#if defined(HAVE_OCTAVE)



#if defined(OCTAVEBUILD)

inline double* mxGetDoubles(const mxArray* pArr)
{
Expand Down Expand Up @@ -146,4 +150,5 @@ inline char* mxArrayToUTF8String(const mxArray* pArr)
return mxArrayToString(pArr);
}


#endif
Loading

0 comments on commit 95a1a43

Please sign in to comment.