newtx/imgproc/algs/rebuild.cpp

123 lines
3.8 KiB
C++

#include "rebuild.h"
#include <huagao/hgscanner_error.h>
#include <sane/sane_ex.h>
#include <cis/cis_param.h>
static std::string device_opt_json[] = {
"{\"rebuild\":{\"cat\":\"imgp\",\"group\":\"imgp\",\"title\":\"CIS\\u56fe\\u50cf\\u8fd8\\u539f\",\"desc\":\"\\u5c06\\u4eceCIS\\u8f93\\u51fa\\u7684\\u539f\\u59cb\\u6570\\u636e\\u6d41\\uff0c\\u8fd8\\u539f\\u4e3aBMP\\u56fe\\u7247\\uff0c\\u5e76\\u62c6\\u5206\\u6b63\\u53cd\\u9762\",\"type\":\"bool\",\"pos\":10,\"ui-pos\":1,\"auth\":0,\"visible\":0,\"size\":4,\"auto\":false,\"cur\":true,\"default\":true}}"
};
rebuild::rebuild() : image_processor("rebuild")
{
ADD_THIS_JSON();
}
rebuild::~rebuild()
{}
int rebuild::set_value(const char* name/*nullptr for all options*/, void* val/*nullptr for restore*/)
{
int ret = SCANNER_ERR_OK;
if(strcmp(name, SANE_OPT_NAME(CIS_REBUILD)) == 0)
rebuild_ = *(bool*)val;
else
ret = SCANNER_ERR_DEVICE_NOT_SUPPORT;
return ret;
}
void rebuild::enable(const char* name, bool able)
{
enabled_ = able;
}
int rebuild::process(std::vector<PROCIMGINFO>& in, std::vector<PROCIMGINFO>& out)
{
int ret = SCANNER_ERR_OK;
if(is_enable() && rebuild_)
{
for(auto& v: in)
{
do_rebuild(&v.info, v.img.ptr(), out);
}
}
else
{
out = std::move(in);
for(auto& v: out)
v.info.prc_stage = get_position();
}
return ret;
}
void rebuild::do_rebuild(LPPACKIMAGE info, uint8_t* stream, std::vector<PROCIMGINFO>& out)
{
int secl = cis::get_sector_pixels(0, info->resolution_x, true),
linel = cis::get_line_stream_length(info->resolution_x, false),
size = info->height * info->width / 2;
PROCIMGINFO o;
chronograph watch;
o.info = *info;
if(info->pos.paper_side != PAPER_SIDE_BACK &&
info->pos.paper_side != PAPER_SIDE_FRONT)
linel *= 2;
o.info.prc_stage = get_position();
o.info.pos.paper_side = PAPER_SIDE_FRONT;
out.push_back(o);
o.info.pos.paper_side = PAPER_SIDE_BACK;
out.push_back(o);
if(info->width == linel) // gray
{
}
else // color
{
uint8_t *srcd = stream + info->width * (info->height - 1), // line reverse
*dstf = nullptr, // (uint8_t*)malloc(size),
*dstb = nullptr, // (uint8_t*)malloc(size),
*df = dstf, *db = dstb,
soff[] = {11, 10, 9, 2, 1, 0};
out[1].info.channels = out[0].info.channels = 3;
out[0].info.width /= out[0].info.channels * 2;
out[1].info.width /= out[1].info.channels * 2;
out[0].img.create(out[0].info.height, out[0].info.width, CV_8UC3);
out[1].img.create(out[1].info.height, out[1].info.width, CV_8UC3);
df = dstf = out[0].img.ptr();
db = dstb = out[1].img.ptr();
o.info.width = out[0].info.width;
o.info.channels = out[0].info.channels;
printf("rebuild (%d * %d * 8) to (%d * %d * 24), front = %p, back = %p, size = %u ...\n", info->width, info->height
, o.info.width, o.info.height, dstf, dstb, size);
for(int h = 0; h < info->height; ++h)
{
for(int w = 0; w < o.info.width; ++w)
{
int s = w / secl;
*df++ = srcd[(soff[s] + 0) * secl + (w % secl)];
*df++ = srcd[(soff[s] + 6) * secl + (w % secl)];
*df++ = srcd[(soff[s] + 3) * secl + (w % secl)];
*db++ = srcd[info->width / 2 + (soff[s] + 6) * secl + (w % secl)];
*db++ = srcd[info->width / 2 + (soff[s] + 0) * secl + (w % secl)];
*db++ = srcd[info->width / 2 + (soff[s] + 3) * secl + (w % secl)];
}
srcd -= info->width;
}
}
size = watch.elapse_ms();
out[0].info.prc_time = size;
out[1].info.prc_time = size;
}