#pragma once // image processor // // created on 2023-11-07 // // ver: 1.0 // // NOTE: the interface is for all algorithms are in ONE module #include #include // for refer #include #include #define DUMP_PROTO union _page_thrd_data_4k* obj, std::vector* arr, LPPACKIMAGE info, cv::Mat* mat, char* infoex, size_t infexl, bool last, void* param class device_option; class img_encoder; class rebuild; class imgproc_mgr : public sane_opt_provider { typedef struct _raw_img { #ifdef REBUILD_IN_CIS_THREAD std::vector imgs; #else PACKIMAGE info; dyn_mem_ptr data; // nullptr and img is 'true' to exit worker #endif bool img; }RAWIMG; // allocate a page to store the vars for every worker thread typedef union _page_thrd_data_4k { struct { bool run; bool dumpi; int ind; int session; int scan_id; rebuild* rebld; img_encoder* encoder; safe_fifo *que; image_processor* processors[20]; bool(*res)(RES_CHK_PROTO); void *res_param; void(*dump)(DUMP_PROTO); void *dump_param; void(*sender)(SENDER_PROTO); void *sender_param; }mean; uint8_t page[SIZE_KB(4)]; _page_thrd_data_4k() { mean.rebld = nullptr; mean.encoder = nullptr; mean.que = nullptr; memset(mean.processors, 0, sizeof(mean.processors)); mean.res = nullptr; mean.dump = nullptr; mean.sender = nullptr; mean.res_param = mean.dump_param = mean.sender_param = nullptr; } ~_page_thrd_data_4k() { this->free(); } void free(void); }PAGEDPARAM, *LPPAGEDPARAM; #ifdef USE_THREAD_PAGED_DATA std::vector params_; #else safe_fifo raw_; #endif refer_guard first_; refer_guard last_; volatile bool run_ = true; volatile uint32_t sent_ind_ = 1; bool dump_img_ = false; uint32_t scan_id_ = 0; uint32_t session_id_ = 0; MUTEX working_cnt_lock_; uint32_t working_cnt_ = 0; uint32_t put_ind_ = 0; std::vector processors_; device_option* opts_; safe_thread workers_; bool(*res_)(RES_CHK_PROTO) = nullptr; void* res_param_ = nullptr; void(*img_sender_)(SENDER_PROTO) = nullptr; void* sender_param_ = nullptr; static bool sort_processor_by_pos(image_processor* l, image_processor* r); static bool sort_image_packet(image_packet_ptr l, image_packet_ptr r); static data_source_ptr scan_finished_packet(uint32_t scanid, uint32_t err = 0); static void process(image_processor* prc, std::vector* in, std::vector* out); #ifdef USE_THREAD_PAGED_DATA static #endif void send_image(LPPAGEDPARAM obj, LPPACKIMAGE head, cv::Mat& mat, void* info = nullptr, size_t info_l = 0, bool last = true); static void real_dump_image(DUMP_PROTO); static void empty_dump_image(DUMP_PROTO); void start_workers(int cnt); uint32_t add_busy_worker(int inc = 1); void thread_worker(void* param); void process(RAWIMG* img, LPPAGEDPARAM param, int thrd_sn); void send_image(LPPAGEDPARAM obj, std::vector& imgs, bool last); public: imgproc_mgr(device_option* devopts, void(*sender)(SENDER_PROTO), void* sp, bool(*res)(RES_CHK_PROTO) = nullptr, void* rp = nullptr); protected: virtual ~imgproc_mgr(); public: virtual int set_value(const char* name, void* val) override; // process public: int load_processor(const char* path); // outer-modules path int clear(void); int process(CIS_IMAGE_PROTO); // life public: void stop(void); bool is_busy(void); void start_new_turn(uint32_t scanid, uint32_t sessionid); };