From cc317d0d24b648e7bead64825c64caa3b2217088 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 13 Mar 2024 09:02:11 -0700 Subject: [PATCH] refactored code and start work on streaming. --- scripts/watch_compilelib.sh | 2 +- src/CMakeLists.txt | 6 +- src/base.hpp | 15 +++++ src/frame.cxx | 38 ++++++++++++ src/frame.h | 20 +++++++ src/image.cxx | 114 ++++++++++++++++++------------------ src/image.h | 41 ++++++------- src/streamingview.cxx | 15 +++++ src/streamingview.hpp | 25 ++++++++ 9 files changed, 191 insertions(+), 85 deletions(-) create mode 100644 src/base.hpp create mode 100644 src/frame.cxx create mode 100644 src/frame.h create mode 100644 src/streamingview.cxx create mode 100644 src/streamingview.hpp diff --git a/scripts/watch_compilelib.sh b/scripts/watch_compilelib.sh index ee35bd6..93bb406 100755 --- a/scripts/watch_compilelib.sh +++ b/scripts/watch_compilelib.sh @@ -10,5 +10,5 @@ DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd) SRC_DIR=$(realpath ${DIR}/../src) -find ${SRC_DIR} -name "*.c*" -o -name "*.h" -o -name "CMakeLists.txt" | \ +find ${SRC_DIR} -name "*.c*" -o -name "*.h*" -o -name "CMakeLists.txt" | \ entr -r ${DIR}/compilelib.sh \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f904144..521f36b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -59,6 +59,8 @@ set(SKIP_INSTALL_ALL OFF CACHE BOOL "" FORCE) # we only want lib add_definitions(${PNG_DEFINITIONS}) add_library(${LIBDART_OPENMVG} SHARED + "frame.cxx" + "streamingview.cxx" "image.cxx" ) @@ -68,11 +70,9 @@ target_include_directories( ${FFMPEG_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS} ${ARCHIMEDES_INCLUDE_DIRS} - ${OPENMVG_INCLUDE_DIRS}/src + ${OpenMVG_DIR}/../../../include/openMVG_dependencies/cereal/include/cereal/ ) -# message("LIBRARIES:" ${OpenMVG_LIBRARIES}) - target_link_libraries( ${LIBDART_OPENMVG} cwalk diff --git a/src/base.hpp b/src/base.hpp new file mode 100644 index 0000000..f42156f --- /dev/null +++ b/src/base.hpp @@ -0,0 +1,15 @@ +#ifdef __cplusplus +#define EXTC extern "C" +#define START_EXTC_GRP extern "C" { +#define END_EXTC_GRP } +#else +#define EXTC +#define START_EXTC_GRP +#define END_EXTC_GRP +#endif // _CPLUSPLUS + +#ifdef __cplusplus +# define _FFI_PLUGIN extern "C" __attribute__((visibility("default"))) __attribute__((used)) +#else +# define _FFI_PLUGIN __attribute__((visibility("default"))) __attribute__((used)) +#endif // __cplusplus diff --git a/src/frame.cxx b/src/frame.cxx new file mode 100644 index 0000000..b80b6ee --- /dev/null +++ b/src/frame.cxx @@ -0,0 +1,38 @@ +#include "frame.h" + +const Frame *new_frame_from_handle(FILE *stream, int w, int h, int depth) +{ + Frame *f = (Frame *)malloc(sizeof(Frame)); + f->stream = stream; + f->w = w; + f->h = h; + f->depth = depth; + return f; +} + +FILE *make_buffer(const uint8_t *data, const size_t data_len) +{ + FILE *file = fmemopen((void *)data, data_len, "r+"); + + if (file == NULL) + { + perror("Error opening file"); + return NULL; + } + + // Seek to the beginning of the file + rewind(file); + + // Write the data to the file + fwrite(data, sizeof(uint8_t), data_len, file); + + return file; +} + +_FFI_PLUGIN +const Frame * +new_frame_from_data(const uint8_t *data, const size_t data_len, int w, int h, int depth) +{ + FILE *buf = make_buffer(data, data_len); + return new_frame_from_handle(buf, w, h, depth); +} diff --git a/src/frame.h b/src/frame.h new file mode 100644 index 0000000..c3bd1e7 --- /dev/null +++ b/src/frame.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include "./base.hpp" + +typedef struct _Frame +{ + FILE *stream; + int w; + int h; + int depth; +} Frame; + +FILE *make_buffer(const uint8_t *, const size_t); + +_FFI_PLUGIN +const Frame *new_frame_from_handle(FILE *, int, int, int); +_FFI_PLUGIN +const Frame * +new_frame_from_data(const uint8_t *, const size_t, int, int, int); \ No newline at end of file diff --git a/src/image.cxx b/src/image.cxx index db34130..def4332 100644 --- a/src/image.cxx +++ b/src/image.cxx @@ -1,86 +1,67 @@ #include "./image.h" #ifdef __cplusplus +#include #include #include +#include +#include +#include #endif // __ cplusplus _FFI_PLUGIN -ImageResult *image_result_new(const u_char *data, const size_t data_len) { - ImageResult *r = (ImageResult *) malloc(sizeof(ImageResult)); - r->data = (u_char *) calloc(sizeof(u_char), data_len); - memcpy(r->data, data, data_len * sizeof(u_char)); - r->data_len = data_len; - r->error = 0; - return r; +ImageResult *image_result_new(const u_char *data, const size_t data_len) +{ + ImageResult *r = (ImageResult *)malloc(sizeof(ImageResult)); + r->data = (u_char *)calloc(sizeof(u_char), data_len); + memcpy(r->data, data, data_len * sizeof(u_char)); + r->data_len = data_len; + r->error = 0; + return r; } _FFI_PLUGIN -ImageResult *image_result_new_error(const error_t err) { - ImageResult *r = (ImageResult *) malloc(sizeof(ImageResult)); - r->data = NULL; - r->data_len = 0; - r->error = err; - return r; +ImageResult *image_result_new_error(const error_t err) +{ + ImageResult *r = (ImageResult *)malloc(sizeof(ImageResult)); + r->data = NULL; + r->data_len = 0; + r->error = err; + return r; } _FFI_PLUGIN -void free_image_result(ImageResult *image_result) { - free(image_result->data); - free(image_result); +void free_image_result(ImageResult *image_result) +{ + free(image_result->data); + free(image_result); } -const Frame *new_frame_from_handle(FILE *stream, int w, int h, int depth) -{ - Frame *f = (Frame *)malloc(sizeof(Frame)); - f->stream = stream; - f->w = w; - f->h = h; - f->depth = depth; - return f; -} -FILE *make_buffer(const uint8_t *data, const size_t data_len) +std::vector Archimedes::get_image_data_as_vector(const Frame *frame) { - FILE *file = fmemopen((void *)data, data_len, "r+"); - - if (file == NULL) + rewind(frame->stream); + std::vector imageData; + if (!openMVG::image::ReadPngStream(frame->stream, &imageData, (int *)&(frame->w), (int *)&(frame->h), (int *)&(frame->depth))) { - perror("Error opening file"); - return NULL; + throw std::runtime_error("Could not read stream"); } - - // Seek to the beginning of the file - rewind(file); - - // Write the data to the file - fwrite(data, sizeof(uint8_t), data_len, file); - - return file; -} - -_FFI_PLUGIN -const Frame * -new_frame_from_data(const uint8_t *data, const size_t data_len, int w, int h, int depth) -{ - FILE *buf = make_buffer(data, data_len); - return new_frame_from_handle(buf, w, h, depth); + return imageData; } #ifdef __cplusplus -extern "C" ImageResult *Archimedes::get_image_data(const Frame *frame) { - std::vector imageData; - rewind(frame->stream); - if (!openMVG::image::ReadPngStream(frame->stream, &imageData, (int *)&(frame->w), (int *)&(frame->h), (int *)&(frame->depth))) { - printf("ERROR: Could not read stream!\n"); + try { + const std::vector imageData = get_image_data_as_vector(frame); + printf("Read %lu bytes.\n", imageData.size()); + const unsigned char *data = imageData.data(); + return image_result_new(data, imageData.size()); + } catch (const std::runtime_error& e) { + std::cerr << e.what() << std::endl; return image_result_new_error(1); } - printf("Read %lu bytes.\n", imageData.size()); - const unsigned char *data = imageData.data(); - return image_result_new(data, imageData.size()); } #endif @@ -91,10 +72,29 @@ ImageResult *archimedes_get_image_data(const Frame *frame) return Archimedes::get_image_data(frame); } -_FFI_PLUGIN int Archimedes::images_to_sfm(const Frame **frames, size_t n_frames) { - + using namespace openMVG::sfm; + + const Frame *frame = frames[0]; + const std::vector imageData = get_image_data_as_vector(frame); + + const size_t w = frame->w; + const size_t h = frame->h; + + // Create the Eigen matrix and the image + + Eigen::Matrix eigenMatrix(w, h); + for (int i = 0; i < w * h; ++i) { + eigenMatrix(i / h, i % h) = imageData[i]; + } + + openMVG::image::Image bvgImage(eigenMatrix); + + + SfM_Data sfmData {}; + sfmData.views = Views(); + return 0; } diff --git a/src/image.h b/src/image.h index a5d101b..cd03581 100644 --- a/src/image.h +++ b/src/image.h @@ -29,17 +29,13 @@ #include #endif // __cplusplus +#include "./base.hpp" +#include "frame.h" #include #include #include #include -#ifdef __cplusplus -# define _FFI_PLUGIN extern "C" __attribute__((visibility("default"))) __attribute__((used)) -#else -# define _FFI_PLUGIN __attribute__((visibility("default"))) __attribute__((used)) -#endif // __cplusplus - #if defined(__cplusplus) extern "C" { @@ -48,15 +44,21 @@ extern "C" { #endif // __cplusplus +#ifdef __cplusplus -typedef struct _Frame -{ - FILE *stream; - int w; - int h; - int depth; -} Frame; +class StreamedView : openMVG::sfm::View { + private: + + const Frame *mFrame; + + public: + + StreamedView(const Frame *); + +}; + +#endif typedef struct _ImageResult { @@ -72,21 +74,12 @@ ImageResult *image_result_new_error(const error_t); _FFI_PLUGIN int image_result_free(ImageResult *); -_FFI_PLUGIN -const Frame *new_frame_from_handle(FILE *, int, int, int); -_FFI_PLUGIN -const Frame * -new_frame_from_data(const uint8_t *, const size_t, int, int, int); #ifdef __cplusplus -typedef openMVG::image::Image RgbImage; -typedef std::vector FeatureVector; -typedef openMVG::image::Image UImage; -typedef std::vector RegionVector; -typedef openMVG::sfm::SfM_Data Sfm_Data; -extern "C" namespace Archimedes +namespace Archimedes { + std::vector get_image_data_as_vector(const Frame *frame); ImageResult *get_image_data(const Frame *); int images_to_sfm(const Frame **, size_t); } diff --git a/src/streamingview.cxx b/src/streamingview.cxx new file mode 100644 index 0000000..130426a --- /dev/null +++ b/src/streamingview.cxx @@ -0,0 +1,15 @@ +#include "streamingview.hpp" + +template +void libdart_openmvg::StreamingView::load(cereal::BinaryOutputArchive &ar) const { + int i = 0; + while (i = fgetc(mFrame->stream) != EOF) { + ar(i); + } +} + +template +void libdart_openmvg::StreamingView::save(cereal::BinaryInputArchive &ar) const { + // int i = 0; + // todo +} \ No newline at end of file diff --git a/src/streamingview.hpp b/src/streamingview.hpp new file mode 100644 index 0000000..9f35012 --- /dev/null +++ b/src/streamingview.hpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include "frame.h" + +typedef openMVG::sfm::View View; + +namespace libdart_openmvg +{ + + struct StreamingView : View + { + + private: + const Frame *mFrame; + + public: + StreamingView(const Frame *frame) : mFrame(frame), View () {} + template + void save(cereal::BinaryInputArchive &) const; + template + void load(cereal::BinaryOutputArchive &) const; + }; + +} \ No newline at end of file