From 2f80cb3e5590d42584ee5c38f881a0f28054a897 Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 15 Mar 2024 08:22:44 -0700 Subject: [PATCH] rename project in pubspect. testing streamingview. --- pubspec.yaml | 2 +- src/CMakeLists.txt | 5 ++++- src/frame.cxx | 33 +++++++++++++++++++++++++++++++-- src/frame.h | 14 +++++++++++--- src/image.cxx | 1 + src/image.h | 15 --------------- src/streamingview.cxx | 26 ++++++++++++++++---------- src/streamingview.hpp | 10 ++++------ src/tests/test_image.cxx | 30 +++++++++++++++++++++++++++++- 9 files changed, 97 insertions(+), 39 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index d505c4f..0fc9eb9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,4 +1,4 @@ -name: dart_openmvg +name: libdart_openmvg description: "Dart Bindings for OpenMVG" version: 0.0.1 homepage: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 638c1be..b39c846 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,13 +64,15 @@ add_library(${LIBDART_OPENMVG} SHARED "image.cxx" ) +set(CEREAL_INCLUDE_DIR ${OpenMVG_DIR}/../../../include/openMVG_dependencies/cereal/include/cereal/) + target_include_directories( ${LIBDART_OPENMVG} PRIVATE ${FFMPEG_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS} ${ARCHIMEDES_INCLUDE_DIRS} - ${OpenMVG_DIR}/../../../include/openMVG_dependencies/cereal/include/cereal/ + ${CEREAL_INCLUDE_DIR} ) target_link_libraries( @@ -119,6 +121,7 @@ target_include_directories( PRIVATE ${LIBDART_OPENMVG_INCLUDE_DIRS} ${OpenMVG_INCLUDE_DIRS}/src + ${CEREAL_INCLUDE_DIR} ) set_target_properties(test_image diff --git a/src/frame.cxx b/src/frame.cxx index a02c276..0bb2cf3 100644 --- a/src/frame.cxx +++ b/src/frame.cxx @@ -20,15 +20,44 @@ FILE *make_buffer(const uint8_t *data, const size_t data_len) return file; } +int read_buffer(FILE *file, uint8_t **buffer, size_t *length) { + if (file != NULL) { + // Get the file size + fseek(file, 0, SEEK_END); + (*length) = ftell(file); + fseek(file, 0, SEEK_SET); -const Frame *new_frame_from_data(const uint8_t *data, const size_t data_len, int w, int h, int depth) + // Allocate memory + *buffer = (uint8_t *) malloc((*length) * sizeof(char)); + if (buffer == NULL) { + printf("Error: Unable to allocate memory.\n"); + return 1; + } + + // Read file + size_t newLen = fread(*buffer, sizeof(char), *length, file); + if (newLen == 0) { + if (feof(file)) { + printf("Error: End of file reached.\n"); + return 2; + } + printf("Error: Reading file.\n"); + return 3; + } + return 4; + } + return 5; +} + + +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); } -const Frame *new_frame_from_handle(FILE *stream, int w, int h, int depth) +Frame *new_frame_from_handle(FILE *stream, int w, int h, int depth) { Frame *f = (Frame *)malloc(sizeof(Frame)); f->stream = stream; diff --git a/src/frame.h b/src/frame.h index de218a8..1481360 100644 --- a/src/frame.h +++ b/src/frame.h @@ -3,6 +3,10 @@ #include #include "./base.hpp" +#ifndef __FRAME_H +#define __FRAME_H + + typedef struct _Frame { FILE *stream; @@ -13,8 +17,12 @@ typedef struct _Frame FILE *make_buffer(const uint8_t *, const size_t); -_FFI_PLUGIN -const Frame *new_frame_from_handle(FILE *, int, int, int); +int read_buffer(FILE *, uint8_t **, size_t *); _FFI_PLUGIN -const Frame *new_frame_from_data(const uint8_t *, const size_t, int, int, int); \ No newline at end of file +Frame *new_frame_from_handle(FILE *, int, int, int); + +_FFI_PLUGIN +Frame *new_frame_from_data(const uint8_t *, const size_t, int, int, int); + +#endif // __FRAME_H \ No newline at end of file diff --git a/src/image.cxx b/src/image.cxx index def4332..d5f1b77 100644 --- a/src/image.cxx +++ b/src/image.cxx @@ -94,6 +94,7 @@ int Archimedes::images_to_sfm(const Frame **frames, size_t n_frames) SfM_Data sfmData {}; sfmData.views = Views(); + return 0; } diff --git a/src/image.h b/src/image.h index 1099589..da264df 100644 --- a/src/image.h +++ b/src/image.h @@ -48,21 +48,6 @@ extern "C" { #define error_t int -#ifdef __cplusplus - -class StreamedView : openMVG::sfm::View { - - private: - - const Frame *mFrame; - - public: - - StreamedView(const Frame *); - -}; - -#endif typedef struct _ImageResult { diff --git a/src/streamingview.cxx b/src/streamingview.cxx index 130426a..101f719 100644 --- a/src/streamingview.cxx +++ b/src/streamingview.cxx @@ -1,15 +1,21 @@ #include "streamingview.hpp" +#include -template -void libdart_openmvg::StreamingView::load(cereal::BinaryOutputArchive &ar) const { - int i = 0; - while (i = fgetc(mFrame->stream) != EOF) { - ar(i); - } +void libdart_openmvg::StreamingView::load(cereal::BinaryInputArchive &ar) const { + uint8_t *data = NULL; + const size_t dLen1 = (mFrame->w * mFrame->h * mFrame->depth * sizeof(uint8_t)); + ar.loadBinary(data, sizeof(uint8_t) * dLen1); + const size_t dlen2 = sizeof(data) * sizeof(uint8_t); + mFrame->stream = make_buffer(data, dlen2); + rewind(mFrame->stream); } -template -void libdart_openmvg::StreamingView::save(cereal::BinaryInputArchive &ar) const { - // int i = 0; - // todo +void libdart_openmvg::StreamingView::save(cereal::BinaryOutputArchive &ar) const { + uint8_t * data = NULL; + size_t data_len = 0; + int ret = 0; + if(ret = read_buffer(mFrame->stream, &data, &data_len)) { + std::cerr << "ERROR: " << ret << std::endl; + } + ar.saveBinary(data, data_len); } \ No newline at end of file diff --git a/src/streamingview.hpp b/src/streamingview.hpp index 9f35012..ddb50c6 100644 --- a/src/streamingview.hpp +++ b/src/streamingview.hpp @@ -12,14 +12,12 @@ namespace libdart_openmvg { private: - const Frame *mFrame; + Frame *mFrame; public: - StreamingView(const Frame *frame) : mFrame(frame), View () {} - template - void save(cereal::BinaryInputArchive &) const; - template - void load(cereal::BinaryOutputArchive &) const; + StreamingView(Frame *frame) : mFrame(frame), View () {} + void save(cereal::BinaryOutputArchive &) const; + void load(cereal::BinaryInputArchive &) const; }; } \ No newline at end of file diff --git a/src/tests/test_image.cxx b/src/tests/test_image.cxx index 79bc9f2..d38fe5c 100644 --- a/src/tests/test_image.cxx +++ b/src/tests/test_image.cxx @@ -1,6 +1,11 @@ +#include #include #include "../image.h" +#include "../streamingview.hpp" #include +#ifdef __cplusplus +#include +#endif // __cplusplus #define STRLEN 2048 @@ -100,6 +105,28 @@ void test_archimedes_get_data(void) CU_ASSERT_TRUE(result->data_len > 0); } +void test_archimedes_streamed_view(void) { + imageHandle = fopen(frame1Path, "rw+"); + Frame *f = new_frame_from_handle( + imageHandle, + 768, + 768, + 64 + ); + + const libdart_openmvg::StreamingView view(f); + + std::cerr << "OPENING " << frame1Path << std::endl; + std::ifstream inStream(std::string(frame1Path), std::ios::in | std::ios::binary); + std::ofstream outStream; + + cereal::BinaryInputArchive inArchive(inStream); + cereal::BinaryOutputArchive outArchive(outStream); + + // view.save(outArchive); + view.load(inArchive); +} + int main() { CU_pSuite pSuite = NULL; @@ -117,7 +144,8 @@ int main() { /* add the tests to the suite */ if ((NULL == CU_add_test(pSuite, "frame can be created", test_create_frame)) || (NULL == CU_add_test(pSuite, "frame can be created from data", test_frame_from_data)) || - (NULL == CU_add_test(pSuite, "data can be retrieved from frame", test_archimedes_get_data))) + (NULL == CU_add_test(pSuite, "data can be retrieved from frame", test_archimedes_get_data)) || + (NULL == CU_add_test(pSuite, "Streaming view can read & write data", test_archimedes_streamed_view))) { CU_cleanup_registry(); return CU_get_error();