添加BMP文件头函数;restore不判断auto属性

This commit is contained in:
gb 2024-01-05 17:39:16 +08:00
parent b30660fc49
commit 1d76b40512
11 changed files with 182 additions and 121 deletions

View File

@ -2,7 +2,7 @@
#include "async_usb_host.h"
#include <huagao/hgscanner_error.h>
@ -132,7 +132,7 @@ int cmd_result::clean(void)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// scanner_handler
scanner_handler::scanner_handler(void) : usb_(nullptr), status_(SCANNER_STATUS_NOT_OPEN)
scanner_handler::scanner_handler(void) : usb_(nullptr), status_(SCANNER_ERR_NOT_OPEN)
, img_receiver_(std::function<data_holder_ptr(LPPACKIMAGE, uint64_t)>())
, status_notify_(std::function<void(uint32_t)>())
{
@ -149,20 +149,20 @@ scanner_handler::scanner_handler(void) : usb_(nullptr), status_(SCANNER_STATUS_N
ret = cmd->roger(reply, used, more);
if (cmd->is_over())
cmd->release();
else if (status_ != SCANNER_STATUS_RESET_BULK)
else // if (status_ != SCANNER_STATUS_RESET_BULK)
{
SIMPLE_LOCK(lock_reply_);
reply_.push_back(cmd);
}
}
else if (status_ == SCANNER_STATUS_RESET_BULK)
{
if (pack->size == sizeof(PACK_BASE) &&
pack->cmd == PACK_CMD_SYNC_ROGER &&
pack->payload_len == 0)
status_ = SCANNER_STATUS_READY;
}
//else if (status_ == SCANNER_STATUS_RESET_BULK)
//{
// if (pack->size == sizeof(PACK_BASE) &&
// pack->cmd == PACK_CMD_SYNC_ROGER &&
// pack->payload_len == 0)
// status_ = SCANNER_STATUS_READY;
//}
else if (pack->size == sizeof(PACK_BASE))
{
if (pack->cmd == PACK_CMD_SCAN_IMG_ROGER)
@ -489,7 +489,7 @@ int scanner_handler::scan_start(void)
*used = sizeof(PACK_BASE);
*more = nullptr;
status_ = pack->data == 0 ? SCANNER_STATUS_START_SCANNING : SCANNER_STATUS_READY;
status_ = pack->data == 0 ? SCANNER_ERR_DEVICE_BUSY : SCANNER_ERR_OK;
cmd->trigger();
return nullptr;
@ -590,7 +590,7 @@ int scanner_handler::file_transfer(const char* local_path, const char* remote_pa
else
{
*more = dynamic_cast<packet_data_base_ptr>(freader);
status_ = SCANNER_STATUS_BUSY;
status_ = SCANNER_ERR_DEVICE_BUSY;
}
}
}
@ -651,7 +651,7 @@ int scanner_handler::file_transfer(const char* local_path, const char* remote_pa
fsaver->set_progress_notify(progress);
*more = dynamic_cast<packet_data_base_ptr>(fsaver);
(*(LPPACK_BASE)reply->ptr()).data = 0;
status_ = SCANNER_STATUS_BUSY;
status_ = SCANNER_ERR_DEVICE_BUSY;
}
}
}
@ -781,7 +781,7 @@ int scanner_handler::open_usb_scanner(libusb_device* dev)
{
ret = usb_->start(dev);
if(ret == 0)
status_ = SCANNER_STATUS_READY;
status_ = SCANNER_ERR_OK;
//usb_->set_gadget_encrypting_method(ENCRYPT_CMD_XOR_PID);
}
@ -798,7 +798,7 @@ int scanner_handler::close(void)
reply_.clear();
}
if(ret == 0)
status_ = SCANNER_STATUS_NOT_OPEN;
status_ = SCANNER_ERR_NOT_OPEN;
return ret;
}
@ -806,37 +806,36 @@ int scanner_handler::reset_message_que(void)
{
return 0;
int err = usb_->cancel_write();
status_ = SCANNER_STATUS_RESET_BULK;
utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send reset command ...\r\n");
err = usb_->reset_peer();
utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send reset command = %d\r\n", err);
if (err == 0)
{
EP0REPLYSTATUS s;
dyn_mem_ptr raw(dyn_mem::memory(sizeof(PACK_BASE)));
LPPACK_BASE pack = (LPPACK_BASE)raw->ptr();
BASE_PACKET_REPLY(*pack, PACK_CMD_SYNC, 0, 0);
raw->set_len(sizeof(PACK_BASE));
utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - wait 100ms ...\r\n");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send SYNC packet ...\r\n");
usb_->send_bulk_raw_data(raw);
raw->release();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (status_ == SCANNER_STATUS_RESET_BULK)
err = EFAULT;
utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - final status = %d\r\n", status_);
}
return err;
//int err = usb_->cancel_write();
//
//status_ = SCANNER_STATUS_RESET_BULK;
//utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send reset command ...\r\n");
//err = usb_->reset_peer();
//utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send reset command = %d\r\n", err);
//if (err == 0)
//{
// EP0REPLYSTATUS s;
// dyn_mem_ptr raw(dyn_mem::memory(sizeof(PACK_BASE)));
// LPPACK_BASE pack = (LPPACK_BASE)raw->ptr();
//
// BASE_PACKET_REPLY(*pack, PACK_CMD_SYNC, 0, 0);
// raw->set_len(sizeof(PACK_BASE));
// utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - wait 100ms ...\r\n");
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - send SYNC packet ...\r\n");
// usb_->send_bulk_raw_data(raw);
// raw->release();
//
// std::this_thread::sleep_for(std::chrono::milliseconds(50));
// if (status_ == SCANNER_STATUS_RESET_BULK)
// err = EFAULT;
// utils::to_log(LOG_LEVEL_DEBUG, "reset_message_que - final status = %d\r\n", status_);
//}
//
//return err;
}
bool scanner_handler::is_scanner_available(void)
{
return status_ != SCANNER_STATUS_NOT_OPEN
&& status_ != SCANNER_STATUS_LOST_CONNECT
&& status_ != SCANNER_STATUS_RESET_BULK;
return status_ == SCANNER_ERR_OK ||
status_ == SCANNER_ERR_DEVICE_BUSY;
}

View File

@ -7,6 +7,16 @@
#include <string>
// name password time-limit
//
// developer calc(dev-sn + login-times) 6min
//
// owner calc(dev-sn + login-times) 30min
//
// admin dev-sn 1hour
//
// user none, default level always
//
class hguser
{
std::string name_;

View File

@ -10,28 +10,6 @@
#include <sys/mman.h>
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// sys_info
uint32_t sys_info::page_size = 0;
uint32_t sys_info::page_map_size = 0;
uint32_t sys_info::cluster_size = 0;
sys_info::sys_info()
{
sys_info::page_size = utils::get_page_size(&sys_info::page_map_size);
std::string path(utils::get_local_data_path());
unsigned long long cluster = 0;
utils::get_disk_space(path.c_str(), nullptr, nullptr, &cluster);
sys_info::cluster_size = cluster;
printf("Page size: %u\nMap size: %u\nCluster : %u\n", sys_info::page_size, sys_info::page_map_size, sys_info::cluster_size);
}
sys_info::~sys_info()
{}
static sys_info g_si;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//

View File

@ -76,17 +76,6 @@ public:
uint8_t* buffer(void);
};
class sys_info
{
public:
sys_info();
~sys_info();
public:
static uint32_t page_size;
static uint32_t page_map_size;
static uint32_t cluster_size;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//

View File

@ -126,33 +126,6 @@ enum img_cb_type
IMG_CB_STOPPED,
};
enum scanner_status
{
SCANNER_STATUS_READY = 0x10000, // status beginning, avoiding conficts with standards/system error code
SCANNER_STATUS_NOT_OPEN,
SCANNER_STATUS_LOST_CONNECT,
SCANNER_STATUS_RESET_BULK,
SCANNER_STATUS_START_SCANNING, // start ok, but scanning-thread not working
SCANNER_STATUS_SCANNING, // start ok, and scanning-thread is working
SCANNER_STATUS_SCAN_FINISHED, // not a persistance status
SCANNER_STATUS_BUSY, // doing task exclude scanning
SCANNER_STATUS_COVER_OPENNED,
SCANNER_STATUS_COVER_CLOSED,
SCANNER_STATUS_SLEEPING,
SCANNER_STATUS_WAKED_UP,
SCANNER_STATUS_COUNT_MODE,
SCANNER_STATUS_DOUBLE_FEEDED,
SCANNER_STATUS_PAPER_JAMMED,
SCANNER_STATUS_PAPER_ASKEW,
SCANNER_STATUS_FEED_FAILED,
SCANNER_STATUS_NO_PAPER,
SCANNER_STATUS_PAPER_ON,
SCANNER_STATUS_STAPLE_ON,
SCANNER_STATUS_SIZE_ERR,
SCANNER_STATUS_DOGEAR,
SCANNER_STATUS_CFG_CHANGED, // PACK_BASE::payload - LPCFGVAL
};
// option affection if value changed, see SANE_INFO_xxx
enum opt_affect
{

View File

@ -17,6 +17,7 @@
#define ALIGN_TO(v, align) (((v) + (align) - 1) / (align) * (align))
#define ALIGN_INT(v) ALIGN_TO(v, sizeof(int))
#define CLEAN_ARRAY(a) memset(a, 0, sizeof(a))
#define BMP_LINE_BYTES(bits) (((bits) + 31) / 32 * 4)
#define RETURN_ENUM_STR(v, e) \
if(v == e) \

View File

@ -606,7 +606,11 @@ namespace utils
ldp += PATH_SEPARATOR;
}
#else
#ifdef BUILD_AS_DEVICE
const char* path = "/var/log";
#else
const char* path(getenv("HOME"));
#endif
if (path)
{
@ -926,11 +930,11 @@ namespace utils
HMODULE load_dll(const char* path_file, int flag)
{
#if OS_WIN
HMODULE h = LoadLibraryA(path_file);
int ret = GetLastError();
HMODULE h = NULL; // LoadLibraryA(path_file);
int ret = 0; // GetLastError();
utils::to_log(1, "[TWAIN]Load: LoadLibraryA(%s) = %d\r\n", path_file, ret);
if (!h && (ret == ERROR_MOD_NOT_FOUND || ret == ERROR_BAD_EXE_FORMAT))
// utils::to_log(1, "[TWAIN]Load: LoadLibraryA(%s) = %d\r\n", path_file, ret);
// if (!h && (ret == ERROR_MOD_NOT_FOUND || ret == ERROR_BAD_EXE_FORMAT))
{
std::string dir(path_file);
size_t pos = dir.rfind('\\');
@ -1239,6 +1243,53 @@ namespace utils
return 0;
}
std::string bitmap_info_header(int pixel_w, int pixel_h, int bpp, int dpix, int dpiy)
{
BITMAPINFODEADER bih = {0};
std::string stream("");
if(dpiy == 0)
dpiy = dpix;
bih.biSize = sizeof(bih);
bih.biWidth = pixel_w;
bih.biHeight = pixel_h;
bih.biBitCount = bpp;
bih.biSizeImage = BMP_LINE_BYTES(pixel_w * bpp) * pixel_h;
bih.biPlanes = 1;
bih.biCompression = BI_RGB;
bih.biXPelsPerMeter = (LONG)(dpix * 39.37f + .5f);
bih.biYPelsPerMeter = (LONG)(dpiy * 39.37f + .5f);
stream = std::string((char*)&bih, sizeof(bih));
if(bpp == 1)
{
int pal[] = { 0, 0x0ffffff };
stream += std::string((char*)pal, sizeof(pal));
bih.biClrUsed = _countof(pal);
}
else if(bpp == 8)
{
stream += std::string((char*)global_info::gray_pallete, sizeof(global_info::gray_pallete));
bih.biClrUsed = _countof(global_info::gray_pallete);
}
return std::move(stream);
}
std::string bitmap_file_header(BITMAPINFOHEADER *lpbi) // return BITMAPFILEHEADER
{
BITMAPFILEHEADER bfh = {0};
int pal_size = 0;
bfh.bfType = MAKEWORD('B', 'M');
if(lpbi->biBitCount == 1)
pal_size = 2 * sizeof(int);
else if(lpbi->biBitCount == 8)
pal_size = 256 * sizeof(int);
bfh.bfOffBits = sizeof(bfh) + sizeof(BITMAPINFOHEADER) + pal_size;
bfh.bfSize = bfh.bfOffBits + lpbi->biSizeImage;
return std::move(std::string((char*)&bfh, sizeof(bfh)));
}
#if OS_WIN
bool run_get_message(HWND hwnd, UINT filter_min, UINT filter_max, std::function<bool(MSG*, bool*)> msghandler)
{
@ -1598,6 +1649,32 @@ void chronograph::reset()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// global_info
uint32_t global_info::page_size = 0;
uint32_t global_info::page_map_size = 0;
uint32_t global_info::cluster_size = 0;
uint32_t global_info::gray_pallete[] = {0};
global_info::global_info()
{
global_info::page_size = utils::get_page_size(&global_info::page_map_size);
std::string path(utils::get_local_data_path());
unsigned long long cluster = 0;
utils::get_disk_space(path.c_str(), nullptr, nullptr, &cluster);
global_info::cluster_size = cluster;
for(int i = 0; i < _countof(global_info::gray_pallete); ++i)
global_info::gray_pallete[i] = MAKELONG(MAKEWORD(i, i), MAKEWORD(i, 0));
printf("Page size: %u\nMap size: %u\nCluster : %u\n", global_info::page_size, global_info::page_map_size, global_info::cluster_size);
}
global_info::~global_info()
{}
static global_info g_si;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -77,6 +77,10 @@ namespace utils
int copy_log_file_to(const char* dst);
int clear_log_file(void);
// bitmap header ...
std::string bitmap_info_header(int pixel_w, int pixel_h, int bpp, int dpix, int dpiy = 0); // return BITMPINFOHEADER + pallete if need. dpiy same as dpix if was ZERO
std::string bitmap_file_header(BITMAPINFOHEADER *lpbi); // return BITMAPFILEHEADER
#if OS_WIN
// Function: pump message recycle (GetMessageW)
//
@ -232,6 +236,20 @@ public:
void reset(void);
};
// global info
class global_info
{
public:
global_info();
~global_info();
public:
static uint32_t page_size;
static uint32_t page_map_size;
static uint32_t cluster_size;
static uint32_t gray_pallete[256];
};
// event
class platform_event : public refer
{

View File

@ -1785,6 +1785,20 @@ int device_option::update_data(const char* name, void* value, bool reorder_if_ne
|| (child->get_value("auth", err) && user_ && !user_(err)))
{
err = SCANNER_ERR_ACCESS_DENIED;
// following ...
if (src_.count(name))
{
sane_opt_provider* opt = src_[name]->get_following(name);
while(opt)
{
opt->set_value(name, value);
sane_opt_provider* next = opt->get_following(name);
opt->release();
opt = next;
}
}
}
else
{
@ -1921,7 +1935,8 @@ int device_option::restore(sane_opt_provider* holder)
while (child)
{
if ((!holder || src_.count(child->key()) && src_[child->key()] == holder)
&& is_auto_restore_default(child))
// && is_auto_restore_default(child)
)
{
std::string val(device_option::option_value(child, true));
update_data(child->key().c_str(), &val[0], false);

View File

@ -375,8 +375,8 @@ public:
// "resolution": {
// "cat": "base",
// "group" : "base",
// "title" : "分辨率",
// "desc" : "设置扫描图像的分辨率",
// "title" : "<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD>",
// "desc" : "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ķֱ<EFBFBD><EFBFBD><EFBFBD>",
// "type" : "int",
// "fix-id" : 34840,
// "size" : 4,
@ -386,7 +386,7 @@ public:
// "min": 100,
// "max" : {
// "default": 600,
// "paper==最大扫描尺寸自动裁切 || paper==最大扫描尺寸 || paper==三联试卷" : 500
// "paper==<EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><EFBFBD>ߴ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> || paper==<3D><><EFBFBD>ɨ<EFBFBD><C9A8>ߴ<EFBFBD> || paper==<3D><><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD>" : 500
// },
// "step" : 1
// }
@ -395,19 +395,19 @@ public:
// "paper": {
// "cat": "base",
// "group" : "base",
// "title" : "纸张尺寸",
// "desc" : "设置出图大小",
// "title" : "ֽ<EFBFBD>ųߴ<EFBFBD>",
// "desc" : "<EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ͼ<EFBFBD><EFBFBD>С",
// "type" : "string",
// "fix-id" : 34831,
// "size" : 44,
// "cur" : "匹配原始尺寸",
// "default" : "匹配原始尺寸",
// "range" : ["A3", "8开", "A4", "16开", "A5", "A6", "B4", "B5", "B6", "Letter", "Double Letter", "LEGAL", "匹配原始尺寸", {
// "resolution<500": "最大扫描尺寸自动裁切"
// "cur" : "ƥ<EFBFBD><EFBFBD>ԭʼ<EFBFBD>ߴ<EFBFBD>",
// "default" : "ƥ<EFBFBD><EFBFBD>ԭʼ<EFBFBD>ߴ<EFBFBD>",
// "range" : ["A3", "8<EFBFBD><EFBFBD>", "A4", "16<31><36>", "A5", "A6", "B4", "B5", "B6", "Letter", "Double Letter", "LEGAL", "ƥ<EFBFBD><EFBFBD>ԭʼ<EFBFBD>ߴ<EFBFBD>", {
// "resolution<500": "<EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><EFBFBD>ߴ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
// }, {
// "resolution<500": "最大扫描尺寸"
// "resolution<500": "<EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><EFBFBD>ߴ<EFBFBD>"
// }, {
// "resolution<500": "三联试卷"
// "resolution<500": "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD>"
// }]
// }
//}

View File

@ -275,6 +275,7 @@ std::string scanned_img::file_header(SANE_ImageType type, float resolution)
pal_size = 2 * sizeof(int);
else if (bih.biBitCount == 8)
pal_size = 256 * sizeof(int);
bih.biClrUsed = pal_size / sizeof(int); // some APP (PJScanner.exe 光大……) use this field to calculate pallete
{
BITMAPFILEHEADER fh = { 0 };