#include "imgprc_mgr.h" #include #include #include #include #include "./algs/rebuild.h" static std::string device_opt_json[] = { "{\"is-multiout\":{\"cat\":\"base\",\"group\":\"base\",\"title\":\"\\u591a\\u6d41\\u8f93\\u51fa\",\"desc\":\"\\u540c\\u65f6\\u8f93\\u51fa\\u591a\\u79cd\\u989c\\u8272\\u6a21\\u5f0f\\u7684\\u56fe\\u50cf\",\"type\":\"bool\",\"fix-id\":34817,\"ui-pos\":10,\"auth\":0,\"size\":4,\"cur\":false,\"default\":false},\"multiout-type\":{\"cat\":\"base\",\"group\":\"base\",\"title\":\"\\u591a\\u6d41\\u8f93\\u51fa\\u7c7b\\u578b\",\"desc\":\"\\u9009\\u62e9\\u591a\\u6d41\\u8f93\\u51fa\\u7684\\u7c7b\\u578b\",\"type\":\"string\",\"fix-id\":34818,\"ui-pos\":11,\"auth\":0,\"enabled\":false,\"size\":66,\"cur\":\"\\u5f69\\u8272+\\u7070\\u5ea6+\\u9ed1\\u767d\",\"default\":\"\\u5f69\\u8272+\\u7070\\u5ea6+\\u9ed1\\u767d\",\"range\":[\"\\u5f69\\u8272+\\u7070\\u5ea6+\\u9ed1\\u767d\",\"\\u5f69\\u8272+\\u7070\\u5ea6\",\"\\u5f69\\u8272+\\u9ed1\\u767d\",\"\\u7070\\u5ea6+\\u9ed1\\u767d\"],\"depend\":\"is-multiout==true\"},\"mode\":{\"cat\":\"base\",\"group\":\"base\",\"title\":\"\\u989c\\u8272\\u6a21\\u5f0f\",\"desc\":\"\\u9009\\u62e9\\u8272\\u5f69\\u6a21\\u5f0f\",\"type\":\"string\",\"fix-id\":34819,\"ui-pos\":15,\"auth\":0,\"size\":24,\"cur\":\"24\\u4f4d\\u5f69\\u8272\",\"default\":\"24\\u4f4d\\u5f69\\u8272\",\"range\":[\"24\\u4f4d\\u5f69\\u8272\",\"256\\u7ea7\\u7070\\u5ea6\",\"\\u9ed1\\u767d\",\"\\u989c\\u8272\\u81ea\\u52a8\\u8bc6\\u522b\"],\"depend\":\"is-multiout!=true\"},\"resolution\":{\"cat\":\"base\",\"group\":\"base\",\"title\":\"\\u5206\\u8fa8\\u7387\",\"desc\":\"\\u8bbe\\u7f6e\\u626b\\u63cf\\u56fe\\u50cf\\u7684\\u5206\\u8fa8\\u7387\",\"type\":\"int\",\"fix-id\":34840,\"ui-pos\":20,\"auth\":0,\"size\":4,\"cur\":200,\"default\":200,\"range\":{\"min\":100,\"max\":{\"default\":600,\"paper==\\u6700\\u5927\\u626b\\u63cf\\u5c3a\\u5bf8\\u81ea\\u52a8\\u88c1\\u5207 || paper==\\u6700\\u5927\\u626b\\u63cf\\u5c3a\\u5bf8 || paper==\\u4e09\\u8054\\u8bd5\\u5377\":500},\"step\":1}},\"dump-img\":{\"cat\":\"base\",\"group\":\"\\u9ad8\\u7ea7\\u8bbe\\u7f6e\",\"title\":\"\\u8f93\\u51fa\\u4e2d\\u95f4\\u56fe\\u50cf\",\"desc\":\"\\u8f93\\u51fa\\u5404\\u7b97\\u6cd5\\u4e2d\\u95f4\\u7ed3\\u679c\\u56fe\\u50cf\",\"type\":\"bool\",\"ui-pos\":10,\"auth\":0,\"affect\":2,\"size\":4,\"cur\":false,\"default\":false}}" }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // imgproc_mgr imgproc_mgr::imgproc_mgr(std::function sender , device_option* devopts ) : img_sender_(sender), opts_(devopts), prc_que_("prcimg") { std::string text(""); gb_json *jsn = new gb_json(); for (auto& v : device_opt_json) text += v; if (jsn->attach_text(&text[0])) { text = jsn->to_string(); } jsn->release(); set_opt_json_text(&text[0]); if (opts_) opts_->add_ref(); else opts_ = new device_option(true); load_processor(nullptr); rebuild_.reset(new rebuild()); auto thrd = [&](void) -> void { thread_worker(); }; workers_.start(thrd, "imgproc_mgr::thread_worker", (void*)&imgproc_mgr::thread_worker); } imgproc_mgr::~imgproc_mgr() { clear(); opts_->release(); } bool imgproc_mgr::sort_processor_by_pos(image_processor* l, image_processor* r) { return l->get_position() < r->get_position(); } bool imgproc_mgr::sort_image_packet(image_packet_ptr l, image_packet_ptr r) { return l->get_paper_index() < r->get_paper_index(); } data_source_ptr imgproc_mgr::scan_finished_packet(uint32_t scanid, uint32_t err) { dyn_mem_ptr reply = dyn_mem::memory(sizeof(PACK_BASE)); BASE_PACKET_REPLY(*((LPPACK_BASE)reply->ptr()), PACK_CMD_SCAN_FINISHED_ROGER, scanid, err); reply->set_len(sizeof(PACK_BASE)); return reply; } image_packet_ptr imgproc_mgr::image_sent_packet(LPPACKIMAGE head, dyn_mem_ptr img, uint32_t scanid, void* info, size_t info_l) { image_packet_ptr pimg = new image_packet(head, img, scanid, info, info_l); return pimg; } uint32_t imgproc_mgr::add_busy_worker(int inc) { SIMPLE_LOCK(working_cnt_lock_); working_cnt_ += inc; return working_cnt_; } void imgproc_mgr::thread_worker(void) { RAWIMG img; while(run_) { if(prc_que_.take(img, true)) { add_busy_worker(); // try // { process(&img); // } // catch(const std::exception& e) // { // printf("exception occurs when process paper %d: %s!\n", img.info.pos.paper_ind, e.what()); // } add_busy_worker(-1); } } } void imgproc_mgr::process(RAWIMG* img) { if(img->img) { std::vector in, out; chronograph watch; rebuild_->do_rebuild(&img->info, img->data->ptr(), in); utils::to_log(LOG_LEVEL_ALL, "Rebuild paper %d spend %llu milliseconds.\n", img->info.pos.paper_ind, watch.elapse_ms()); if(dump_img_) { send_image(&img->info, img->data->ptr(), img->info.width * img->info.height); img->data->release(); for(auto& v: processors_) { send_image(in, false); if(v->is_enable()) { v->process(in, out); in = std::move(out); } } } else { img->data->release(); for(auto& v: processors_) { if(v->is_enable()) { v->process(in, out); in = std::move(out); } } } send_image(in, true); } else { data_source_ptr ptr = imgproc_mgr::scan_finished_packet(scan_id_, img->info.data_size); uint32_t wait = 0, que = 0; ptr->set_session_id(session_id_); while((que = add_busy_worker(0)) > 1) { if(wait++ == 0) utils::to_log(LOG_LEVEL_DEBUG, "Received scan completed event while processing %u paper(s), wait ...\n", que - 1); std::this_thread::sleep_for(std::chrono::milliseconds(5)); } img_sender_(ptr); ptr->release(); } } void imgproc_mgr::send_image(LPPACKIMAGE head, uint8_t* data, size_t size, void* info, size_t info_l) { dyn_mem_ptr mem(dyn_mem::memory(size)); image_packet_ptr ptr = nullptr; printf("send image %d(%d-0x%04x) with size %u:\n", head->pos.paper_ind, head->pos.paper_side, head->prc_stage, size); mem->put(data, size); ptr = imgproc_mgr::image_sent_packet(head, mem, scan_id_, info, info_l); mem->release(); ptr->set_session_id(session_id_); img_sender_(ptr); ptr->release(); } void imgproc_mgr::send_image(std::vector& imgs, bool clear_after_send) { for(auto& v: imgs) { send_image(&v.info, v.img.ptr(), v.img.total() * v.img.channels() , v.ext_info.empty() ? nullptr : &v.ext_info[0], v.ext_info.length()); if(clear_after_send) v.img.release(); } } int imgproc_mgr::set_value(const char* name, void* val) { int ret = SCANNER_ERR_OK; if(strcmp(name, SANE_FULL_NAME(DUMP_IMG)) == 0) dump_img_ = *(bool*)val; else ret = SCANNER_ERR_DEVICE_NOT_SUPPORT; return ret; } int imgproc_mgr::load_processor(const char* path) { int ret = SCANNER_ERR_OK; #define ADD_IMG_PROCESSOR(cls) \ { \ cls *obj = new cls(); \ opts_->add(obj); \ processors_.push_back(obj); \ } // ADD_IMG_PROCESSOR(rebuild); std::sort(processors_.begin(), processors_.end(), &imgproc_mgr::sort_processor_by_pos); return ret; } int imgproc_mgr::clear(void) { for (auto& v : processors_) v->release(); processors_.clear(); return 0; } int imgproc_mgr::process(LPPACKIMAGE info, dyn_mem_ptr data, bool img) { RAWIMG ri; int ret = SCANNER_ERR_OK; ri.data = data; if(img) data->add_ref(); if(img) ri.info = *info; else ri.info.data_size = (uint32_t)(long)info; ri.img = img; prc_que_.save(ri, true); return ret; } void imgproc_mgr::stop(void) { run_ = false; prc_que_.trigger(); workers_.stop(nullptr); } bool imgproc_mgr::is_busy(void) { SIMPLE_LOCK(working_cnt_lock_); return working_cnt_; } void imgproc_mgr::start_new_turn(uint32_t scanid, uint32_t sessionid) { scan_id_ = scanid; sent_ind_ = 0; session_id_ = sessionid; }