tx-gxx-linux/device/gxx-linux/testcapimage/main.cpp

330 lines
9.2 KiB
C++

#include <iostream>
#include <memory>
#include "stringex.hpp"
#include <functional>
#include "Capturer.h"
#include "StopWatch.h"
#include "opencv2/opencv.hpp"
#include "config.h"
#include "FpgaComm.h"
#include "usbservice.h"
#include "MonoCapturer.h"
#include "correct_ultis.h"
#define LOOP_DEBUG
static void savebitmap(void *imgdata, int width, int height, bool color, std::string path)
{
cv::Mat mat;
cv::Mat saveMat;
std::vector<cv::Mat> ch_mats;
int blockcnt = 12;
int spitWidth = (width * 3) / blockcnt;
int mergewidth = width / 4;
mat = cv::Mat(height, width * 3, CV_8UC1, imgdata);
cv::imwrite("gray" + path, mat);
saveMat = cv::Mat(height, width, CV_8UC3);
StopWatch sw1;
StopWatch sw;
for (int i = 0; i < 4; i++)
{
sw.reset();
for (int j = 0; j < 3; j++)
ch_mats.push_back(mat(cv::Rect(spitWidth * (i * 3 + j), 0, spitWidth, height)));
std::cout << string_format("split: %d image elapsed:%f ms\n ", i, sw.elapsed_ms());
sw.reset();
// if(i == 0)
// {
// swap(ch_mats[0], ch_mats[2]);
// swap(ch_mats[1], ch_mats[2]);
// }
// if (i == 1)
// {
// swap(ch_mats[0], ch_mats[2]);
// swap(ch_mats[1], ch_mats[0]);
// }
if (i == 2)
{
swap(ch_mats[0], ch_mats[2]);
}
if (i == 3)
{
swap(ch_mats[0], ch_mats[2]);
}
cv::merge(ch_mats, saveMat(cv::Rect(mergewidth * i, 0, mergewidth, height)));
std::cout << string_format("merge: %d image elapsed:%f ms\n ", i, sw.elapsed_ms());
ch_mats.clear();
}
std::cout << string_format("merge image elapsed:%f ms\n ", sw1.elapsed_ms());
cv::imwrite("merged" + path, saveMat);
}
unsigned int delay_done(std::shared_ptr<IRegsAccess> regs, int ms)
{
unsigned int val;
unsigned int reg8;
regs->read(8, reg8);
std::cout << "1 reg[8]:" << string_format("0x%08x", reg8) << std::endl;
regs->write(8, reg8 | 0x8);
std::cout << "sleep:" << ms << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
regs->read(8, reg8);
std::cout << "1 reg[8]:" << string_format("0x%08x", reg8) << std::endl;
regs->read(14, val);
int regv = val;
val &= 0x0000ffff;
std::cout << string_format("ONE height = %d reg[14] = %d \n", val, regv);
regs->write(8, reg8 & 0xfffffff7);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
regs->read(14, val);
regv = val;
val &= 0x0000ffff;
regs->read(8, reg8);
std::cout << "2 reg[8]:" << string_format("0x%08x", reg8) << std::endl;
std::cout << string_format("TWO height = %d reg[14] = %d \n", val, regv);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
regs->write(8, reg8 | 0x8);
regs->read(8, reg8);
std::cout << "2 reg[8]:" << string_format("0x%08x", reg8) << std::endl;
return val;
}
int delaytimes[] = {200, 300, 400, 100, 150};
int ws[] = {7344 / 2, 7344};
void list_regs(std::shared_ptr<IRegsAccess> regs)
{
unsigned int val = 0;
for (int i = 5; i < 8; i++)
{
if (regs->read(i, val))
std::cout << string_format("reg[%d] = 0x%08x\n", i, val);
else
std::cout << "read regs error " << std::endl;
}
}
// int main()
// {
// auto cap = std::shared_ptr<ICapturer>(new MonoCapturer());
// StopWatch sw;
// unsigned int val = 0;
//
// auto regs = cap->regs();
// // UsbService us(regs, nullptr);
// HGScanConfig config;
// config.g200params.color = 0;
// config.g200params.dpi = 3;
// config.g200params.paper = (unsigned int)PaperSize::G400_AUTO;
// void *data;
// // cap->init_autocorrect(0);
// cap->open(config);
// int ch = 0;
// int ixx = 0;
//
// // std::cout << "mod1:" << CV_8UC3 << " " << CV_8UC1 << std::endl;
//
// for (int i = 0; i < 0x10; i++)
// {
// sw.reset();
// if (regs->read(i, val))
// std::cout << string_format("reg[%d] = 0x%08x\n", i, val);
// else
// std::cout << "read regs error " << std::endl;
// }
// std::cout << "type enter to grab" << std::endl;
// int ix = 0;
// int width = cap->width();
// int fixedheight = cap->height();
// int type = cap->color();
// int height = fixedheight;
// if (type == CV_8UC3)
// {
// height = fixedheight / 3;
// width = width * 2 * 3 * 3;
// type = CV_8UC1;
// }
// else
// {
// height = fixedheight;
// width = width * 2 * 3;
// }
//
// while ((ch = getchar()) != 'q')
// {
// #ifdef LOOP_DEBUG
// for (int i = 0; i < 2000; i++)
// #endif
// {
// ix++;
// // sw.reset();
// // for (int j = 0; j < 15; j++)
// // {
// // sw.reset();
// // if (regs->read(j, val))
// // std::cout << string_format("reg[%d] = 0x%08x, %f\n", j, val, sw.elapsed_ms());
// // else
// // std::cout << "read regs error " << std::endl;
// // }
// sw.reset();
// cap->snap();
//
// #ifdef LOOP_DEBUG
// height = delay_done(regs, delaytimes[ix % (sizeof(delaytimes) / sizeof(delaytimes[0]))]);
// std::cout << string_format("%dth grab time height = %d", ix, height) << std::endl;
// height = std::min(fixedheight, height);
// #endif
// if (data = cap->readFrame(5000))
// {
// // for (int i = 0; i < 0x10; i++)
// // {
// // sw.reset();
// // if (regs->read(i, val))
// // std::cout << string_format("readFrame reg[%d] = 0x%08x\n", i, val);
// // else
// // std::cout << "read regs error " << std::endl;
// // }
// // std::cout << string_format("QQQQQQQQ %dth grab time height = %d: %fms", ix, height, sw.elapsed_ms()) << std::endl;
// std::cout << string_format("grab time width = %d, height = %d, type = %d: %fms", width, height, type, sw.elapsed_ms()) << std::endl;
// sw.reset();
// int channel = cap->color() == 16 ? 3 : 1;
// int dstwith = cap->width() * 2 * 3;
// int dstHeight = height;
// cv::Mat mat(dstHeight, dstwith * channel, CV_8UC1, data);
// static int testindex = 0;
// cv::imwrite(std::to_string(++testindex) + std::to_string(dstHeight) + "autosizeorg.jpg", mat);
// #ifndef LOOP_DEBUG
// // cv::Mat mat=GetMergeMat(data,cap->width(),cap->height(),cap->color());
// // mat = cv::Mat(height, width, type, data);
// // cv::imwrite( std::to_string(ix++)+ ".jpg", mat);
// // std::cout << string_format("%dth grab time height = %d: %fms", ix, height, sw.elapsed_ms()) << std::endl;
// // sw.reset();
// // savebitmap(data, width, height, true, std::to_string(ix) + ".jpg");
// // std::cout << string_format("%dth grab save time : %fms", ix, sw.elapsed_ms()) << std::endl;
// // sw.reset();
// // memset(data, 0, width * height * 3);
// // std::cout << string_format("%dth memset time : %fms", ix, sw.elapsed_ms()) << std::endl;
// #endif
// break;
// }
// else
// {
// std::cout << "error :" << ix << std::endl;
// #ifdef LOOP_DEBUG
// break;
// #endif
// }
// }
// std::cout << "type enter to continue" << std::endl;
// }
//
// return 0;
// }
int main()
{
auto cap = std::shared_ptr<ICapturer>(new MonoCapturer());
StopWatch sw;
unsigned int val = 0;
auto regs = cap->regs();
// UsbService us(regs, nullptr)
int looptimes = 0;
// while ((ch = getchar()) != 'q')
{
#ifdef LOOP_DEBUG
for (;;)
#endif
{
HGScanConfig config;
config.g200params.color = 0;
config.g200params.dpi = 2;
config.g200params.paper = (unsigned int)PaperSize::G400_AUTO;
void *data;
// cap->init_autocorrect(0);
FPGAConfigParam fpgaparam = GetFpgaparam(config.g200params.dpi, config.g200params.color);
cap->open(config,fpgaparam);
int ch = 0;
int ixx = 0;
unsigned int fpgaversion;
regs->read(15, fpgaversion);
std::cout << "fpgaversion:" << fpgaversion << std::endl;
// for (int i = 0; i < 0x10; i++)
// {
// sw.reset();
// if (regs->read(i, val))
// std::cout << string_format("reg[%d] = 0x%08x\n", i, val);
// else
// std::cout << "read regs error " << std::endl;
// }
int ix = 0;
int width = cap->width();
int fixedheight = cap->height();
int type = cap->color();
int height = fixedheight;
if (type == CV_8UC3)
{
height = fixedheight / 3;
width = width * 2 * 3 * 3;
type = CV_8UC1;
}
else
{
height = fixedheight;
width = width * 2 * 3;
}
for (int i = 0; i < 100; i++)
{
ix++;
sw.reset();
cap->snap();
#ifdef LOOP_DEBUG
height = delay_done(regs, delaytimes[ix % (sizeof(delaytimes) / sizeof(delaytimes[0]))]);
std::cout << string_format("%dth grab time height = %d", ix, height) << std::endl;
height = std::min(fixedheight, height);
#endif
if (data = cap->readFrame(5000))
{
std::cout << string_format("grab time width = %d, height = %d, type = %d: %fms", width, height, type, sw.elapsed_ms()) << std::endl;
sw.reset();
int channel = cap->color() == 16 ? 3 : 1;
int dstwith = cap->width() * 2 * 3;
int dstHeight = height;
cv::Mat mat(dstHeight, dstwith * channel, CV_8UC1, data);
int dstwidth = type==CV_8UC1?width:width/3;
cv::Mat saveMat =GetMergeMat(dstwidth, height, type,mat,0x00090001);
std::string savepath = std::to_string(++looptimes) + ".bmp";
cv::imwrite(savepath, saveMat);
//auto smat = MergeImage(channel == 3 ? 1 : 0, mat, dstwith, dstHeight);
//auto ok = cv::imwrite(savepath, smat);
// if (!ok)
// std::cout << "error save file :" << savepath << std::endl;
}
else
{
std::cout << "error :" << ix << std::endl;
#ifdef LOOP_DEBUG
break;
#endif
}
}
cap->close();
}
}
return 0;
}