#pragma once #include #include "imgproc.h" #include "mutex" #include "itransmit.h" #include "inotify.h" #include "ireceive.h" #include "BlockingQueue.h" #include #include "./src/async_model/common/packet.h" class UsbImageProcQueue { void (*push_image_)(int data_type, void* data, size_t w, size_t h, int dpi_x, int dpi_y, size_t paper_ind, paper_side side, clr_channel clr, img_status status, bool img_new, bool img_over, float ratio_h, float ratio_v, void* param) = nullptr; void(*img_keeper_)(MemoryPtr, bool, void*); void* kp_param_; int scan_status_; int dpi_x_; int dpi_y_; float ratio_h_; float ratio_v_; bool imgp_over_; int sensor_status_2_scanner_status(int ss) { switch(ss) { case 2: ss = SCANNER_STATUS_NO_PAPER; break; case 4: ss = SCANNER_STATUS_COVER_OPENNED; break; case 5: ss = SCANNER_STATUS_COVER_CLOSED; break; case 8: ss = SCANNER_STATUS_FEED_FAILED; break; case 0x10: ss = SCANNER_STATUS_PAPER_JAMMED; break; case 0x20: ss = SCANNER_STATUS_DOUBLE_FEEDED; break; case 0x40: ss = SCANNER_STATUS_STAPLE_ON; break; case 0x80: ss = SCANNER_STATUS_PAPER_ASKEW; break; case 0x20000: break; } return ss; } public: UsbImageProcQueue(NotifyPtr notify) : img_keeper_(nullptr), kp_param_(nullptr), scan_status_(0), dpi_x_(200), dpi_y_(200), ratio_h_(1.0f), ratio_v_(1.0f) { this->notify = notify; } void push(MemoryPtr image,bool containsimg) { #ifdef ASYNC_EP if(!containsimg) { HGIntInfo* info = (HGIntInfo*)image->data(); if(info->From == HGType::STOPSCAN) { if(push_image_) push_image_(IMG_CB_STOPPED, nullptr, sensor_status_2_scanner_status(info->Code), 0, dpi_x_, dpi_y_, 0, PAPER_SIDE_LEFT, (clr_channel)0, (img_status)0, true, true, ratio_h_, ratio_v_, kp_param_); } else { scan_status_ = sensor_status_2_scanner_status(info->Code); if(push_image_) push_image_(IMG_CB_STATUS, nullptr, sensor_status_2_scanner_status(info->Code), 0, dpi_x_, dpi_y_, 0, PAPER_SIDE_LEFT, (clr_channel)0, (img_status)0, true, true, ratio_h_, ratio_v_, kp_param_); } } return; #endif if(img_keeper_) img_keeper_(image, containsimg, kp_param_); else { std::lock_guard lck(mx); HGIntInfo info; if(containsimg) { images.push(image); //images.Put(image); info.From = IMG; info.Code = image->size(); } else { info = *((HGIntInfo*)image->data()); } if(notify) notify->notify(&info, sizeof(info)); } } bool push_raw(void *data, int width, int height = 0, int type = 0, int scannnum = 0, unsigned int fpgaversion = 0, int status = 0) { if(scannnum == 1) { scan_status_ = 0; imgp_over_ = false; } if(push_image_) { push_image_(IMG_CB_IMAGE, data, width, height, dpi_x_, dpi_y_, scannnum, PAPER_SIDE_LEFT, (clr_channel)type, (img_status)status, true, true, ratio_h_, ratio_v_, kp_param_); return true; } return false; } void set_image_keeper(void(*img_keeper)(MemoryPtr, bool, void*), void* param) { printf("UsbImageProcQueue::set_image_keeper(%p, %p)\n", img_keeper, param); img_keeper_ = img_keeper; kp_param_ = param; *(uint64_t*)&push_image_ = (uint64_t)img_keeper_; } void set_dpi(int x, int y) { dpi_x_ = x; dpi_y_ = y; } void set_image_processing(bool over) { imgp_over_ = over; } bool is_image_processing_over(void) { return imgp_over_; } MemoryPtr front() { std::lock_guard lck(mx); auto front = images.front(); return front; //return images.Front(); } MemoryPtr pop() { std::lock_guard lck(mx); if(images.size()>0) { auto front = images.front(); images.pop(); //auto front=images.Take(); return front; } return MemoryPtr(); } bool empty() { std::lock_guard lck(mx); return images.empty(); //return images.Size()>0; } int size() { std::lock_guard lck(mx); return images.size(); //return images.Size(); } int front_datasize() { std::lock_guard lck(mx); if(images.size()<1) return 0; auto img=images.front(); return images.empty() ? 0 : images.front()->size(); //return (images.Size()>0) ? 0 : images.Front()->size(); } void set_ratio(float h, float v) { ratio_h_ = h; ratio_v_ = v; } void clear() { std::lock_guard lck(mx); while(images.size()>0) images.pop(); } private: std::queue images; //BlockingQueue images; NotifyPtr notify; std::mutex mx; };