zynq_7010/utilsfunc.cpp

404 lines
13 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.

#include "utilsfunc.h"
#include <unistd.h>
#include <memory>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "filetools.h"
#include "TypeIO.h"
#define VMRSS_LINE 21
#define VMSIZE_LINE 17
#define PROCESS_ITEM 29
#define LUT_COLOR_BLACK_PATH "/usr/local/huago/lut_0.bmp"
#define LUT_COLOR_WHITE_PATH "/usr/local/huago/lut_1.bmp"
#define LUT_GRAY_BLACK_PATH "/usr/local/huago/lut_2.bmp"
#define LUT_GRAY_WHITE_PATH "/usr/local/huago/lut_3.bmp"
#define LUT_COLOR_LUT_PATH "/usr/local/huago/color_lut.bmp"
#define LUT_GRAY_LUT_PATH "/usr/local/huago/gray_lut.bmp"
static std::unique_ptr<TypeIO> g_sTypeio;
//using namespace cv;
// cv::Mat extractRepresentRow(const cv::Mat &src)
// {
// LOG("extractRepresentRow2 enter \n");
// Mat src_temp(src.rows, src.step, CV_8UC1, src.data);
// Mat dst(1, src.cols, CV_8UC(src.channels()));
// Mat dst_temp(1, src.step, CV_8UC1, dst.data);
// for (int i = 0, length = src.step; i < length; i++)
// {
// Mat col = src_temp(Rect(i, 0, 1, src_temp.rows));
// float value = 0;
// for (int j = 0, rows = col.rows; j < rows; j++)
// {
// value += (float)col.at<unsigned char>(Point(0, j)) / rows;
// }
// dst_temp.data[i] = (int) value;
// }
// LOG("extractRepresentRow2 exit \n");
// return dst;
// }
float curve_coffes[2][3][3] = {
{
{-6e-5, 0.9928, 0.289},
{4e-5, 1.0045, -0.0338},
{3e-5, 1.0028, -0.2547}
},
{
{0.00025215105482649316 , 0.9162675232424231 , 2.2064225897716527},
{-6.242394612635725e-05 , 1.0212590446315797 , -0.7470581333124157},
{-0.0002324551311266845 , 1.072239346671703 , -1.7099159032971474}
}
};
// cv::Mat create_lut(const cv::Mat &black, const cv::Mat &white,bool colormode,CISVendor vendor)
// {
// LOG("create_lut enter \n");
// #ifdef G400
// int channel = 4896 / 12; // 1632*3
// #else
// int channel = 5184 / 12; // 1728*3
// #endif
// const int rows = black.cols / channel; //408
// const int cols = 256;
// LOG("LUT White width: %d height :%d channels:%d \n", white.cols, white.rows, white.channels());
// LOG("LUT black width: %d height :%d channels:%d \n", black.cols, black.rows, black.channels());
// Mat lut(rows, cols, CV_8UC(channel));
// float* cisCoffoes;
// float* coffoes;
// if(vendor==CISVendor::DUNNAN_CIS_V0)
// cisCoffoes = &curve_coffes[0][0][0];
// else if(vendor==CISVendor::HUALIN_CIS_V0)
// cisCoffoes = &curve_coffes[1][0][0];
// else
// {
// cisCoffoes = &curve_coffes[1][0][0];
// LOG("warnning!!! unknown cis type,use hualin CIS cisCoffoes params \n");
// }
// for (size_t i = 0; i < rows; i++)
// {
// Mat lut_row = lut(cv::Rect(0, i, cols, 1));
// unsigned char *ptr_buffer = lut_row.data;
// unsigned char *ptr_black = black.data + i * channel;
// unsigned char *ptr_white = white.data + i * channel;
// for (size_t j = 0; j < cols; j++)
// {
// for (size_t k = 0; k < channel; k++)
// {
// if (ptr_black[k] >= ptr_white[k])
// {
// ptr_buffer[j * channel + k] = 0;
// LOG("ptr_black[%d] >= ptr_white[%d]", k, k);
// continue;
// }
// if (j <= ptr_black[k])
// {
// ptr_buffer[j * channel + k] = 0;
// }
// else if (j >= ptr_white[k])
// {
// ptr_buffer[j * channel + k] = 255;
// }
// else
// {
// if(colormode==IMAGE_COLOR)
// {
// float val = 255.0 * (j - ptr_black[k]) / (ptr_white[k] - ptr_black[k])*1.2;
// // if (k % 3 == 2)
// // val = pow(10, -6) * pow(val, 3) - 0.0004 * pow(val, 2) + 1.0065 * val - 1.6495;
// // else if (k % 3 == 1)
// // val = 7 * pow(10, -7) * pow(val, 3) - 0.0002 * pow(val, 2) + 0.9861 * val - 0.4419;
// // else
// // val = -pow(10, -7) * 9 * pow(val, 3) + 0.0001 * pow(val, 2) + 1.0627 * val + 1.1538;
// coffoes = cisCoffoes + 3 * (k % 3);
// val = coffoes[0] * val * val + coffoes[1] * val + coffoes[2];
// if (val > 255)
// val = 255;
// if (val < 0)
// val = 0;
// ptr_buffer[j * channel + k] = (unsigned char)val;
// }
// else
// {
// float val = 255.0 * (j - ptr_black[k]) / (ptr_white[k] - ptr_black[k])*1.2;
// if (val > 255)
// val = 255;
// if (val < 0)
// val = 0;
// ptr_buffer[j * channel + k] = (unsigned char)val;
// }
// }
// }
// }
// }
// LOG("create_lut exit \n");
// return lut;
// }
void initLut()
{
//lutoldGrayMat = cv::imread(LUT_GRAY_LUT_PATH, IMREAD_GRAYSCALE); //灰色校正值
//lutoldColorMat = cv::imread(LUT_COLOR_LUT_PATH, IMREAD_GRAYSCALE); //彩色校正值
}
//void correctColor(cv::Mat src, bool enhance)
//{
// Mat tSrc;
// Mat lutMat;
// int patch = 0;
// int SIZE = src.cols / 12;
// //printf("lutMat.empty() src.width::%d SIZE: %d \n", src.cols, SIZE);
// if (src.type() == CV_8UC3)
// {
// patch = (src.cols * 3) / SIZE;
// if (lutoldColorMat.empty())
// {
// if (access(LUT_COLOR_LUT_PATH, F_OK) != -1)
// {
// lutoldColorMat = imread(LUT_COLOR_LUT_PATH, IMREAD_GRAYSCALE);
// }
// else
// {
// LOG("error error error %s NOT FOUND \n", LUT_COLOR_LUT_PATH);
// return;
// }
// }
// lutMat = lutoldColorMat;
// }
// else
// {
// patch = (src.cols) / SIZE;
// if (lutoldGrayMat.empty())
// {
// if (access(LUT_GRAY_LUT_PATH, F_OK) != -1)
// {
// lutoldGrayMat = imread(LUT_GRAY_LUT_PATH, IMREAD_GRAYSCALE);
// }
// else
// {
// LOG("error error error %s NOT FOUND", LUT_GRAY_LUT_PATH);
// return;
// }
// }
// lutMat = lutoldGrayMat;
// }
// if (lutMat.empty())
// {
// return;
// }
// lutMat = cv::Mat(patch, 256, CV_8UC(SIZE), lutMat.data);
// tSrc = cv::Mat(src.rows, patch, CV_8UC(SIZE), src.data);
// for (int i = 0; i < patch; i++)
// {
// LUT(tSrc(cv::Rect(i, 0, 1, src.rows)), lutMat(cv::Rect(0, i, 256, 1)), tSrc(cv::Rect(i, 0, 1, src.rows)));
// }
//}
// void creatLUTData(int mode,CISVendor vendor)
// {
// LOG("eneter creatLUTData \n");
// auto colormode = mode == IMAGE_COLOR ? IMREAD_COLOR : IMREAD_GRAYSCALE;
// std::string blackPath = mode == IMAGE_COLOR ? LUT_COLOR_BLACK_PATH : LUT_GRAY_BLACK_PATH;
// std::string whitePath = mode == IMAGE_COLOR ? LUT_COLOR_WHITE_PATH : LUT_GRAY_WHITE_PATH;
// std::string lutsavePath = mode == IMAGE_COLOR ? LUT_COLOR_LUT_PATH : LUT_GRAY_LUT_PATH;
// cv::Mat lut;
// cv::Mat twMat = cv::imread(whitePath, colormode);
// cv::Mat tbMat = cv::imread(blackPath, colormode);
// cv::Mat wMat, bMat;
// if (mode == IMAGE_COLOR)
// {
// wMat = cv::Mat(twMat.rows, twMat.cols * 3, CV_8UC1, twMat.data);
// bMat = cv::Mat(twMat.rows, twMat.cols * 3, CV_8UC1, tbMat.data);
// }
// else
// {
// wMat = twMat;
// bMat = tbMat;
// }
// lut = create_lut(extractRepresentRow(bMat), extractRepresentRow(wMat),mode,vendor); // add by liuyong: 刘丁维提供 2019/4/12
// Mat dst(bMat.cols * bMat.channels(), 256, CV_8UC1);
// memcpy(dst.data, lut.data, bMat.cols * bMat.channels() * 256);
// cv::imwrite(lutsavePath, dst);
// LOG("exit creatLUTData \n");
// }
// cv::Mat flipRightHalf(Mat &src, int papertype)
// {
// Mat left = src(cv::Rect(0, 0, src.cols / 2, src.rows));
// Mat right = src(cv::Rect(src.cols / 2, 0, src.cols / 2, src.rows));
// Mat rightFlip;
// // if (papertype == (int)(PaperSize::G400_A4R))
// // {
// flip(right, rightFlip, 0);
// // }
// // else
// // {
// // flip(right, rightFlip, 1);
// // }
// Mat dst;
// hconcat(left, rightFlip, dst);
// return dst;
// }
void SaveScaninfo(const ScannerScanInfo &info)
{
js_config.SaveScanInfo(info);
}
ScannerScanInfo GetScanInfoFromJson()
{
return js_config.GetScanInfo();
}
void writesyslog(int loglevel, std::string loginfo)
{
#ifdef ENLOG_SYSLOG
openlog("scanservice", LOG_CONS | LOG_PID, LOG_USER);
syslog(loglevel, "%s \n", loginfo.c_str());
closelog();
#endif
}
unsigned int get_proc_virtualmem(unsigned int pid)
{
char file_name[64] = {0};
FILE *fd;
char line_buff[512] = {0};
sprintf(file_name, "/proc/%d/status", pid);
fd = fopen(file_name, "r");
if (nullptr == fd)
{
return 0;
}
char name[64];
int vmsize;
for (int i = 0; i < VMSIZE_LINE - 1; i++)
{
fgets(line_buff, sizeof(line_buff), fd);
}
fgets(line_buff, sizeof(line_buff), fd);
sscanf(line_buff, "%s %d", name, &vmsize);
fclose(fd);
return vmsize;
}
unsigned int get_proc_mem(unsigned int pid)
{
char file_name[64] = {0};
FILE *fd;
char line_buff[512] = {0};
sprintf(file_name, "/proc/%d/status", pid);
fd = fopen(file_name, "r");
if (nullptr == fd)
{
return 0;
}
char name[64];
int vmrss;
for (int i = 0; i < VMRSS_LINE - 1; i++)
{
fgets(line_buff, sizeof(line_buff), fd);
}
fgets(line_buff, sizeof(line_buff), fd);
sscanf(line_buff, "%s %d", name, &vmrss);
fclose(fd);
return vmrss;
}
int get_total_mem()
{
const char *file = "/proc/meminfo"; //文件名
FILE *fd; //定义文件指针fd
char line_buff[256] = {0}; //读取行的缓冲区
fd = fopen(file, "r"); //以R读的方式打开文件再赋给指针fd
//获取memtotal:总内存占用大小
int i;
char name[32]; //存放项目名称
int memtotal; //存放内存峰值大小
char *ret = fgets(line_buff, sizeof(line_buff), fd); //读取memtotal这一行的数据,memtotal在第1行
sscanf(line_buff, "%s %d", name, &memtotal);
//fprintf(stderr, "====%s%d====\n", name, memtotal);
fclose(fd); //关闭文件fd
return memtotal;
}
std::string getmeminfo()
{
pid_t pid = getpid();
unsigned int virmem = get_proc_virtualmem(pid);
unsigned int procmem = get_proc_mem(pid);
int totalmem = get_total_mem();
std::string strinfo = "Scanservice current memory info,virtualmem= " + to_string(virmem) + " KB procmem = " + to_string(procmem) + " KB of " + to_string(totalmem) + " KB total";
return strinfo;
}
void start_enter_lowpwoer()
{
#ifdef ENABLE_LOWPOWERMODE
// int seconds=900;
// auto scaninfo= js_config.GetScanInfo();
// if(scaninfo.SleepTime<0||scaninfo.SleepTime<180)
// seconds=180;
// else
// seconds=scaninfo.SleepTime;
// std::string str="sh /etc/countdown.sh start 0 0 "+std::to_string(seconds)+" &";
// LOG("start_enter_lowpwoer %s \n",str.c_str());
// system(str.c_str());
#endif
}
void stop_countdown()
{
#ifdef ENABLE_LOWPOWERMODE
system("sh /etc/hgkilltimer.sh");
#endif
}
void init_get_CISType_GPIO()
{
// system("echo 190 > /sys/class/gpio/export");
// system("echo in > /sys/class/gpio/gpio190/direction");
// system("echo 191 > /sys/class/gpio/export");
// system("echo in > /sys/class/gpio/gpio191/direction");
// system("echo 233 > /sys/class/gpio/export");
// system("echo in > /sys/class/gpio/gpio233/direction");
// system("echo 234 > /sys/class/gpio/export");
// system("echo in > /sys/class/gpio/gpio234/direction");
}
CISVendor GetCisType()
{
if(!g_sTypeio.get())
g_sTypeio = std::make_unique<TypeIO>(CIS_T_S0, CIS_T_S1, CIS_T_S2, CIS_T_S3);
return g_sTypeio->GetCisType();
}
SMBType GetSMBType()
{ if(!g_sTypeio.get())
g_sTypeio = std::make_unique<TypeIO>(CIS_T_S0, CIS_T_S1, CIS_T_S2, CIS_T_S3);
return g_sTypeio->GetMotorType();
}