code_device/hgdriver/hgdev/image_process.h

238 lines
8.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
// API for image process
//
// created on 2022-03-01
//
#include "huagao/hgscanner_error.h"
#include "common_setting.h"
#include <memory>
#include <vector>
#include <sane/sane_ex.h>
#include "../ImageProcess/ImageApplyHeaders.h"
//#include <vld.h>
class tiny_buffer;
namespace hg_imgproc
{
typedef struct _img_data
{
int img_fmt; // 图片格式BMP、PNG……暂时未用
int width; // 行宽度(像素数)
int height; // 高度(像素数)
int line_bytes; // 每行的字节数
int bits; // 每个像素的位深
bool is_bw; // 是否为黑白图片,由图像处理模块设置
unsigned bytes; // 图像数据字节数(buf中的字节数)
void* ip_data; // 图像处理模块的自定义数据,调用者不要随意更改此值!!!
void* buf; // 图像数据信息
}IMGDATA, *LPIMGDATA;
// 用于释放图像处理模块返回的各种动态内存, mem[i] == NULL 时终止,下列相同参数均照此规则
void imgproc_free_memory(LPIMGDATA* mem);
// 所有函数均返回“int”值代表错误代码 0为成功其它错误码由图像处理模块开发者定义
// Function: 用于将原始数据如图像文件内容解码为target_fmt指定的图像格式
//
// Parameters: raw - 原始的图像数据
//
// bytes - raw中数据字节数
//
// bits - 位深
//
// out - 用于接收解码后的图像数据。由图像处理模块分配内存调用者调用imgproc_free_memory释放
//
// Return: 错误码
//
// NOTE: 该函数返回的out结构体中buf为图像处理的中间数据类型如cv::mat*),要想获取最终的图片数据,
// 需要再调用 imgproc_final 函数进行转换
int imgproc_decode(void* buf, unsigned bytes, int bits, LPIMGDATA** out);
// Function: 将倾斜的文本校正
//
// Parameters: raw - [in]: 原始的图像数据,*raw == NULL时终止
//
// [out]: 最终的图像数据
//
// out - 用于接收解码后的图像数据。由图像处理模块分配内存调用者调用imgproc_free_memory释放
//
// dpi - 图片DPI
//
// double_face - 是否双面
//
// Return: 错误码
int imgproc_correct_text(LPIMGDATA** raw, int dpi, bool double_face); // 适用于raw从其它图像处理函数返回的情况
int imgproc_correct_text(LPIMGDATA* raw, LPIMGDATA** out, int dpi, bool double_face); // 适用于raw从文件或者内存中转换而来
// Function: 自动匹配颜色
//
// Parameters: raw - [in]: 原始的图像数据,*raw == NULL时终止
//
// [out]: 最终的图像数据
//
// out - 用于接收解码后的图像数据。由图像处理模块分配内存调用者调用imgproc_free_memory释放
//
// double_face - 是否双面
//
// Return: 错误码
int imgproc_auto_adjust_color(LPIMGDATA** raw, bool double_face); // 适用于raw从其它图像处理函数返回的情况
int imgproc_auto_adjust_color(LPIMGDATA* raw, LPIMGDATA** out, bool double_face); // 适用于raw从文件或者内存中转换而来
// Function: 图像拆分
//
// Parameters: raw - [in]: 原始的图像数据,*raw == NULL时终止
//
// [out]: 最终的图像数据
//
// out - 用于接收解码后的图像数据。由图像处理模块分配内存调用者调用imgproc_free_memory释放
//
// Return: 错误码
int imgproc_split(LPIMGDATA** raw); // 适用于raw从其它图像处理函数返回的情况
int imgproc_split(LPIMGDATA* raw, LPIMGDATA** out); // 适用于raw从文件或者内存中转换而来
// Function: 多流输出
//
// Parameters: raw - [in]: 原始的图像数据,*raw == NULL时终止
//
// out - 用于接收解码后的图像数据。由图像处理模块分配内存调用者调用imgproc_free_memory释放
//
// dpi - 图片DPI
//
// double_face - 是否双面
//
// Return: 错误码
int imgproc_multi_out(LPIMGDATA* raw, LPIMGDATA** out, int dpi, bool double_face);
enum
{
TARGET_FORMAT_GRAY = 1 << 0,
TARGET_FORMAT_BLACK_WHITE = 1 << 1,
TARGET_FORMAT_RGB = 1 << 2,
};
// Function: 用于将图像处理过程的中间态数据,转换为最终的图片数据
//
// Parameters: raw - [in]: 原始的图像数据,*raw == NULL时终止通常该变量是由其它图像处理函数返回的
//
// [out]: 最终的图像数据
//
// target_fmt - 预期解码到的格式TARGET_FORMAT_xxx
//
// Return: 错误码
int imgproc_final(LPIMGDATA** raw, int target_fmt, int dpi);
////////////////////////////////////////////////////////////////////////////////
// NEWflow ...
//
// 1 - call init to initialize an image processing object handle HIMGPRC
//
// 2 - call load_buffer or load_file to load raw data
//
// 3 - call image-processing methods
//
// 4 - repeat step-3
//
// 5 - call final to got outputing data
//
// 6 - call get_final_data to get outputing data, while return SCANNER_ERR_NO_DATA
//
// 7 - call release to free the HIMGPRC handle
//
////////////////////////////////////////////////////////////////////////////////
typedef void* HIMGPRC;
typedef struct _img_proc_param
{
int dpi;
int bits; // 单分量位深
int channels;
int color_mode;
bool double_side;
bool black_white;
bool cis_image; //设置原图
int width; // in pixel
int height; // in pixel
unsigned total_bytes;// total bytes
bool is_sup_real_300dpi_;
bool is_sup_real_600dpi_;
bool device_7010;
}IMGPRCPARAM, *LPIMGPRCPARAM;
typedef struct _img_header
{
int width; // in pixel
int height; // in pixel
int bits; // per channel
int channels; // RGB - 3; GRAY - 1; ...
int line_bytes; // bytes of ONE line
unsigned total_bytes;// total bytes
SANE_Image_Statu statu; // added on 2022-07-23
}IMGHEAD, *LPIMGHEAD;
HIMGPRC init(int pid, bool isx86Advan_);
int load_buffer(HIMGPRC himg,std::shared_ptr<tiny_buffer> buff);
int load_file(HIMGPRC himg, const char* path_file);
//图像数据转换
int decode(HIMGPRC himg,int pid, LPSCANCONF img_param, LPIMGPRCPARAM param, std::map<int, setting3288dsp::FLAT_INFO_IMAGE>& correction_image_map_);
int correct_text(HIMGPRC himg);
int init_auto_txt_hanld(HIMGPRC himg);
int free_auto_txt_hanld(HIMGPRC himg);
//拆分
int split(HIMGPRC himg,int split3399);
int fadeback(HIMGPRC himg);
int multi_out(HIMGPRC himg,int out_type, int bw_threshold);
int multi_out_red(HIMGPRC himg);
int auto_matic_color(HIMGPRC himg,int color_type);
int auto_crop(HIMGPRC himg, float dpi);
int dispersion(HIMGPRC himg);
int fillhole(HIMGPRC himg, float top, float low, float r, float l);
int resolution_change(HIMGPRC himg,float dpi3288);
int croprect(HIMGPRC himg);
int channel(HIMGPRC himg);
int adjust_color(HIMGPRC himg, unsigned char* table = nullptr, int tableLength = 0/*default value is to adjust color, or apply custom gamma*/);
int antiInflow(HIMGPRC himg,int permeate_lv);
int colorCorrection(HIMGPRC himg);
int orentation(HIMGPRC himg);
int textureRemove(HIMGPRC himg);
int remove_morr(HIMGPRC himg);
int sharpenType(HIMGPRC himg);
int nosieDetach(HIMGPRC himg);
int errorextention(HIMGPRC himg, int bw_threshold);
int discardBlank(HIMGPRC himg);
int answerSheetFilterRed(HIMGPRC himg);
int imgtypechange(HIMGPRC himg,std::string img_type_,void *buf,std::vector<unsigned char> &bmpdata);
int fold(HIMGPRC himg);
int quality(HIMGPRC himg,int dpi);
int ocr_auto_txtdirect(HIMGPRC himg);
int tesseract_auto_txtdirect(HIMGPRC himg);
int size_detection(HIMGPRC himg);
int cis_test_image(HIMGPRC himg, CISTestImageProcess::CISTestResult& res);
int color_cast_correction(HIMGPRC himg);
int lost_frame_test(HIMGPRC himg);
int final(HIMGPRC himg);
int correction_image(HIMGPRC himg, cv::Mat& flat_lut, cv::Mat black_mat, cv::Mat white_mat);
// pimh must not to be NULL, and pimh->total_bytes indicates the length of 'buf'
//
// if 'buf' was NULL, then return SCANNER_ERR_INSUFFICIENT_MEMORY
//
// index is ZERO-base, if index is valid, fill the 'pimh' first, and copy data to buf if it was not NULL
// return SCANNER_ERR_NO_DATA if it was out of range
//
// return SCANNER_ERR_OK if index has valid image data and buf is large enough
int get_final_data(HIMGPRC himg, LPIMGHEAD pimh, void** buf, int index = 0);
int get_final_data(HIMGPRC himg, LPIMGHEAD pimh, std::vector<unsigned char>* buf, int index);
void dump_2_file(HIMGPRC himg, const char* local_file);
void release(HIMGPRC himg);
// seperate utilites ...
int convert_image_file(SANE_ImageFormatConvert* conv);
int save_2_bmp_file(const char* bmp_file, LPIMGHEAD head, void* buf, int resolution);
std::string bmp8_2_1bit(const unsigned char* data, int w, int h, int line_len, int threshold, bool reverse, bool align);
}