#pragma once // // For: interface definition for SANE to TWAIN // // Date: 2022-06-08 // #include #include #include #include #define COM_API_DECLARE(ret, decl) virtual ret __stdcall decl = 0 #define COM_API_DECLARE_NON_PURE(ret, decl) virtual ret __stdcall decl #define COM_API_OVERRIDE(ret, decl) virtual ret __stdcall decl override #define COM_API_IMPLEMENT(cls, ret, decl) ret __stdcall cls##::decl #define SANE_OPTION_ID_API(opt) COM_API_DECLARE(int, sane_opt_id_##opt(void)) // -1 is none #define SANE_OPTION_ID_OVERRIDE(opt) COM_API_OVERRIDE(int, sane_opt_id_##opt(void)) #define SANE_OPTION_IMPLEMENT(cls, opt) int __stdcall cls##::sane_opt_id_##opt(void) #define SANE_OPTION_ID_API_EX(opt) COM_API_DECLARE(int, sane_opt_id_ex_##opt(void)) // -1 is none #define SANE_OPTION_ID_OVERRIDE_EX(opt) COM_API_OVERRIDE(int, sane_opt_id_ex_##opt(void)) #define SANE_OPTION_IMPLEMENT_EX(cls, opt) int __stdcall cls##::sane_opt_id_ex_##opt(void) #define ALIGN_MEMORY(n, align) ((n + align - 1) / (align) * (align)) #define _TO_UNICODE_(str) L##str #define UNICODE_STR(str) _TO_UNICODE_(str) #define TWPT_AUTOMATIC_COLOR 0x0a0c #define AUTO_MATIC_ROTATE 123.456f #define IS_DOUBLE_EQUAL(a, b) fabs((a) - (b)) < .000001 enum value_limit { VAL_LIMIT_NONE = 0, // VAL_LIMIT_ENUM, // VAL_LIMIT_RANGE, // }; enum value_role { VAL_ROLE_NONE = 0, // this value is no role but an item of the option only VAL_ROLE_DEFAULT = 0x01, // this value is the default value of the option VAL_ROLE_CURRENT = 0x02, // this value is the current value of the option VAL_ROLE_LOWER = 0x04, // the lower value of a VAL_LIMIT_RANGE VAL_ROLE_UPPER = 0x08, // the upper value of a VAL_LIMIT_RANGE VAL_ROLE_STEP = 0x10, // the step value of a VAL_LIMIT_RANGE }; enum value_type { VAL_TYPE_NONE = 0, VAL_TYPE_BOOL, // bool VAL_TYPE_INT, // int VAL_TYPE_FLOAT, // float VAL_TYPE_STR, // char* VAL_TYPE_BUTTON, // a button }; enum color_value // 除最后一项外,其余与Twpp::PixelType值保持一致 { COLOR_BW = 0, COLOR_GRAY, COLOR_RGB, COLOR_AUTO_MATCH = TWPT_AUTOMATIC_COLOR, }; enum paper_value // 与Twpp::PaperSize保持一致 { PAPER_A4 = 1, PAPER_16K = 1, PAPER_LETTER = 3, PAPER_LEGAL = 4, PAPER_A5 = 5, PAPER_B4 = 6, PAPER_B6 = 7, PAPER_A3 = 11, PAPER_8K = 11, PAPER_A6 = 13, PAPER_B5 = 29, PAPER_STATEMENT = 52, // 匹配原始尺寸 PAPER_MAXSIZE = 54, // 最大扫描尺寸 // 以下为未匹配选项 PAPER_DOUBLE_LETTER = 103, PAPER_MAXSIZE_CROP = 154, // 最大扫描尺寸自动裁切 PAPER_TRIPPLE = 111, // 三联试卷 }; enum filter_value // 除色选项 { FILTER_NONE = 0, FILTER_RED, FILTER_GREEN, FILTER_BLUE, }; enum enhance_value // 颜色增强选项 { ENHANCE_NONE = 0, ENHANCE_RED, ENHANCE_GREEN, ENHANCE_BLUE, }; enum sharpen_value { SHARPEN_NONE = 0, SHARPEN_SHARPEN, SHARPEN_SHARPEN_MORE, SHARPEN_BLUR, SHARPEN_BLUR_MORE, }; enum multiout_value { MULTI_OUT_NONE = -1, MULTI_OUT_ALL, MULTI_OUT_COLOR_GRAY, MULTI_OUT_COLOR_BW, MULTI_OUT_GRAY_BW, }; typedef bool(__stdcall* set_opt_value)(void* val, value_role role, void* param); // return false to stop the callback __declspec(novtable) struct IRef { COM_API_DECLARE(long, add_ref(void)); COM_API_DECLARE(long, release(void)); }; __declspec(novtable) struct IScanImg : public IRef { COM_API_DECLARE(int, width(void)); COM_API_DECLARE(int, line_bytes(void)); COM_API_DECLARE(int, height(void)); COM_API_DECLARE(int, depth(void)); COM_API_DECLARE(int, channel(void)); COM_API_DECLARE(SANE_Frame, type(void)); COM_API_DECLARE(unsigned int, bytes(void)); COM_API_DECLARE(unsigned char*, bits(void)); COM_API_DECLARE(void, copy_header(SANE_Parameters* head)); }; __declspec(novtable) struct ISaneInvoker : public IRef { COM_API_DECLARE(int, start(void)); COM_API_DECLARE(int, stop(void)); COM_API_DECLARE(void, set_event_callback(void(*cb)(int ev_type, void* data, unsigned int* len, void* param), void* param)); COM_API_DECLARE(bool, wait_image(DWORD milliseconds = -1)); COM_API_DECLARE(int, get_scanned_images(DWORD milliseconds = 0)); COM_API_DECLARE(IScanImg*, take_first_image(void)); // call 'release' on returned value, plz COM_API_DECLARE(bool, get_first_image_header(SANE_Parameters* header)); COM_API_DECLARE(bool, is_online(void)); COM_API_DECLARE(bool, is_paper_on(void)); // Function: 获取配置项信息 // // Parameter: sn - 配置项索引 // // type - 配置项数据类型 // // limit - 配置项限制类型 // // bytes - *type 为 VAL_TYPE_STR时,需要的最小空间字节数 COM_API_DECLARE(bool, get_option_info(int sn, value_type* type, value_limit* limit, int *bytes)); COM_API_DECLARE(bool, get_value(int sn, set_opt_value, void* param)); COM_API_DECLARE(int, set_value(int sn, void* val)); // SANE options ID ... SANE_OPTION_ID_API(is_multiout); SANE_OPTION_ID_API(multiout_type); SANE_OPTION_ID_API(color_mode); SANE_OPTION_ID_API(erase_color); SANE_OPTION_ID_API(erase_multiout_red); SANE_OPTION_ID_API(erase_paper_red); SANE_OPTION_ID_API(is_erase_background); SANE_OPTION_ID_API(background_color_range); SANE_OPTION_ID_API(sharpen); SANE_OPTION_ID_API(erase_morr); SANE_OPTION_ID_API(erase_grids); // 除网纹 SANE_OPTION_ID_API(error_extend); SANE_OPTION_ID_API(is_noise_modify); SANE_OPTION_ID_API(noise_threshold); SANE_OPTION_ID_API(paper); SANE_OPTION_ID_API(is_custom_area); SANE_OPTION_ID_API(curstom_area_l); SANE_OPTION_ID_API(curstom_area_r); SANE_OPTION_ID_API(curstom_area_t); SANE_OPTION_ID_API(curstom_area_b); SANE_OPTION_ID_API(is_size_check); SANE_OPTION_ID_API(page); SANE_OPTION_ID_API(blank_page_threshold); // 跳过空白页灵敏度 SANE_OPTION_ID_API(resolution); SANE_OPTION_ID_API(image_quality); SANE_OPTION_ID_API(is_swap); // 交换正反面 SANE_OPTION_ID_API(is_split); // 图像拆分 SANE_OPTION_ID_API(is_auto_deskew); // 自动纠偏 SANE_OPTION_ID_API(is_custom_gamma); SANE_OPTION_ID_API(bright); SANE_OPTION_ID_API(contrast); SANE_OPTION_ID_API(gamma); SANE_OPTION_ID_API(is_erase_black_frame); // bool SANE_OPTION_ID_API(deep_sample); SANE_OPTION_ID_API(threshold); SANE_OPTION_ID_API(anti_noise); // 抗噪等级 SANE_OPTION_ID_API(margin); SANE_OPTION_ID_API(fill_background); SANE_OPTION_ID_API(is_anti_permeate); SANE_OPTION_ID_API(anti_permeate_level); SANE_OPTION_ID_API(is_erase_hole); SANE_OPTION_ID_API(search_hole_range); SANE_OPTION_ID_API(is_filling_color); // 色彩填充 SANE_OPTION_ID_API(is_ultrasonic_check); SANE_OPTION_ID_API(is_check_staple); SANE_OPTION_ID_API(scan_mode); // 扫描张数 SANE_OPTION_ID_API(scan_count); // 扫描数量 SANE_OPTION_ID_API(text_direction); SANE_OPTION_ID_API(is_rotate_bkg180); SANE_OPTION_ID_API(is_check_dogear); SANE_OPTION_ID_API(dogear_size); SANE_OPTION_ID_API(is_check_skew); SANE_OPTION_ID_API(skew_range); // SANE-ex option ID: SANE_OPTION_ID_API_EX(multiout_type); // int SANE_OPTION_ID_API_EX(auto_color_type); // int SANE_OPTION_ID_API_EX(color_mode); // int SANE_OPTION_ID_API_EX(sharpen); // int SANE_OPTION_ID_API_EX(paper); // paper_value SANE_OPTION_ID_API_EX(paper_lateral); // bool SANE_OPTION_ID_API_EX(auto_paper_size); // bool SANE_OPTION_ID_API_EX(is_paper_auto_crop); // bool SANE_OPTION_ID_API_EX(text_direction); // float 90, 180, ..., -1 is auto-text-direction SANE_OPTION_ID_API_EX(duplex); // bool SANE_OPTION_ID_API_EX(fill_background); // bool true - 凸多边形 SANE_OPTION_ID_API_EX(discard_blank_page); // bool SANE_OPTION_ID_API_EX(discard_blank_receipt); // bool SANE_OPTION_ID_API_EX(is_page_fold); // bool SANE_OPTION_ID_API_EX(color_filter); // int (filter_value) SANE_OPTION_ID_API_EX(color_enhance); // int (enhance_value) SANE_OPTION_ID_API_EX(final_compression); // int SANE_OPTION_ID_API_EX(final_format); // SANE_FinalImgFormat SANE_OPTION_ID_API_EX(serial); // std::string SANE_OPTION_ID_API_EX(to_be_scan); // bool SANE_OPTION_ID_API_EX(scan_with_hole); // bool SANE_OPTION_ID_API_EX(device_code); // std::string SANE_OPTION_ID_API_EX(power); // int SANE_OPTION_ID_API_EX(hardware_version); // std::string SANE_OPTION_ID_API_EX(ip); // std::string // ui ... COM_API_DECLARE(void, ui_show_main(void)); COM_API_DECLARE(void, ui_show_setting(bool with_scan)); COM_API_DECLARE(void, ui_show_progress(void)); COM_API_DECLARE(void, ui_hide(void)); COM_API_DECLARE(void, ui_handle_sane_event(int sane_ev, void* data, unsigned int* len)); COM_API_DECLARE(bool, ui_is_ok(void)); COM_API_DECLARE(bool, ui_is_progress_ui_showing(void)); }; struct delete_scanner { void operator()(ISaneInvoker* p) { p->release(); } }; #include #include namespace sane_opts { template class get_opts { public: T* init_; T* cur_; T* lower_; T* upper_; T* step_; std::vector* vvs_; std::list* lvs_; public: get_opts(T* cur = NULL, T* init = NULL, std::vector* vs = NULL, std::list* ls = NULL, T* lower = NULL, T* upper = NULL, T* step = NULL) : init_(init), cur_(cur), vvs_(vs), lvs_(ls) , lower_(lower), upper_(upper), step_(step) {} ~get_opts() {} }; template bool __stdcall set_opt_value(void* val, value_role role, void* param) { get_opts* v = (get_opts*)param; bool go = true; if (role & VAL_ROLE_CURRENT) { if (v->cur_) { *v->cur_ = *(T*)val; } } if (role & VAL_ROLE_DEFAULT) { if (v->init_) { *v->init_ = *(T*)val; } } if (role & VAL_ROLE_LOWER) { if (v->lower_) *v->lower_ = *(T*)val; } if (role & VAL_ROLE_UPPER) { if (v->upper_) *v->upper_ = *(T*)val; } if (role & VAL_ROLE_STEP) { if (v->step_) *v->step_ = *(T*)val; return go; } if (v->vvs_) v->vvs_->push_back(*(T*)val); else if (v->lvs_) v->lvs_->push_back(*(T*)val); return go; } } #define GET_SANE_OPT(type, object, id_name, cur, init, vec, lst) \ { \ int ind = object->sane_opt_id_##id_name##(); \ if(ind > 0) \ { \ sane_opts::get_opts op(cur, init, vec, lst); \ object->get_value(ind, sane_opts::set_opt_value, &op);\ } \ } #define GET_SANE_OPT_RANGE(type, object, id_name, cur, init, low, up, step) \ { \ int ind = object->sane_opt_id_##id_name##(); \ if(ind > 0) \ { \ sane_opts::get_opts op(cur, init, NULL, NULL, low, up, step); \ object->get_value(ind, sane_opts::set_opt_value, &op);\ } \ } #define SET_SANE_OPT(ret, object, id_name, val) \ { \ int ind = object->sane_opt_id_##id_name##(); \ if(ind > 0) \ ret = object->set_value(ind, val); \ else \ ret = SCANNER_ERR_INVALID_PARAMETER; \ } typedef unsigned int SCANNERID; #define MAKE_SCANNER_ID(pid, vid) MAKELONG(pid, vid) #define GET_SCANNER_PID(sid) LOWORD(sid) #define GET_SCANNER_VID(sid) HIWORD(sid) #ifdef EXPORT_SANE_API __declspec(dllexport) #else __declspec(dllimport) #endif int __stdcall initialize(void* reserve); #ifdef EXPORT_SANE_API __declspec(dllexport) #else __declspec(dllimport) #endif int __stdcall open_scanner(SCANNERID scanner_id, ISaneInvoker** invoker); #ifdef EXPORT_SANE_API __declspec(dllexport) #else __declspec(dllimport) #endif bool __stdcall is_scanner_online(SCANNERID scanner_id); #ifdef EXPORT_SANE_API __declspec(dllexport) #else __declspec(dllimport) #endif int __stdcall uninitialize(void* reserve);