#pragma once
#ifndef USIZE
#define USIZE sizeof(uchar)
#endif
#ifndef FSIZE
#define FSIZE sizeof(float)
#endif
#include <stdio.h>
#include <stdarg.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
//
//#define __cplusplus__
//#define __CUDACC__
//#include <crt/device_functions.h>
//#undef __cplusplus__
//#undef __CUDACC__
#define SHOWDATA
#define SHOWTIME
#ifdef SHOWTIME
static void TIME(const char *format, ...)
{
va_list argPtr;
int count;
va_start(argPtr, format);
fflush(stdout);
count = vfprintf(stderr, format, argPtr);
va_end(argPtr);
}
#else
static inline void TIME(const char *format, ...)
{
}
#endif
#ifdef SHOWDATA
static void PRINTF(const char *format, ...)
{
va_list argPtr;
int count;
va_start(argPtr, format);
fflush(stdout);
count = vfprintf(stderr, format, argPtr);
va_end(argPtr);
}
#else
static inline void PRINTF(const char *format, ...)
{
}
#endif
struct IMGINFO
{
int row;
int col;
int ch;
long int imgSize;
long int _3ImgSizes;
};
static int Mat2Array(cv::Mat& sMat, float* fMem, uchar* uMem)
{
if (sMat.type() == CV_32FC1 || sMat.type() == CV_32FC2 ||
sMat.type() == CV_32FC3 || sMat.type() == CV_32FC4)
{
if (fMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
float* lineHead = sMat.ptr<float>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
fMem[c + j * sMat.channels() + i * sMat.channels()* sMat.cols] = lineHead[j * sMat.channels() + c];
}
}
}
}
else if (sMat.type() == CV_8UC1 || sMat.type() == CV_8UC2 ||
sMat.type() == CV_8UC3 || sMat.type() == CV_8UC4)
{
if (uMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
uchar* lineHead = sMat.ptr<uchar>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
uMem[c + j * sMat.channels() + i * sMat.channels() * sMat.cols] = lineHead[j * sMat.channels() + c];
}
}
}
}
return true;
}
static int Mat2ArrayM(cv::Mat& sMat, float* fMem, uchar* uMem)
{
if (sMat.type() == CV_32FC1 || sMat.type() == CV_32FC2 ||
sMat.type() == CV_32FC3 || sMat.type() == CV_32FC4)
{
if (fMem == nullptr)
{
printf("Host array pointer is nullptr!!! please malloc\n");
return false;
}
for (int i = 0; i < sMat.rows; i++)
{
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
fMem[c + j * sMat.channels() + i * sMat.channels()* sMat.cols] = sMat.at<cv::Vec3b>(j, i)[c];
}
}
}
}
else if (sMat.type() == CV_8UC1 || sMat.type() == CV_8UC2 ||
sMat.type() == CV_8UC3 || sMat.type() == CV_8UC4)
{
if (uMem == nullptr)
{
printf("Host array pointer is nullptr!!! please malloc\n");
return false;
}
for (int y = 0; y < sMat.rows; y++)
{
for (int x = 0; x < sMat.cols; x++)
{
for (int c = 0; c < sMat.channels(); c++)
{
/*if (y == 120 && x == 267)
{
printf("&&&&&&&&&&&&&&%d\n", sMat.at<cv::Vec3b>(y, x)[c]);
}*/
uMem[c + x * sMat.channels() + y * sMat.channels()* sMat.cols] = sMat.at<cv::Vec3b>(y, x)[c];
}
}
}
}
return true;
}
static int Array2Mat(cv::Mat& sMat, float* fMem, uchar* uMem)
{
if (sMat.type() == CV_32FC1 || sMat.type() == CV_32FC2 ||
sMat.type() == CV_32FC3 || sMat.type() == CV_32FC4)
{
if (fMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
float* lineHead = sMat.ptr<float>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = fMem[c + j * sMat.channels() + i * sMat.channels()* sMat.cols];
}
}
}
}
else if (sMat.type() == CV_8UC1 || sMat.type() == CV_8UC2 ||
sMat.type() == CV_8UC3 || sMat.type() == CV_8UC4)
{
if (uMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
uchar* lineHead = sMat.ptr<uchar>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = uMem[c + j * sMat.channels() + i * sMat.channels() * sMat.cols];
}
}
}
}
return true;
}
template<class T>
void printfArray(T* arrMem, int len)
{
bool isUchar = std::is_same<T, uchar>::value;
bool isFloat = std::is_same<T, float>::value;
if (isFloat)
{
for (int i = 0; i < len; i++)
{
PRINTF("printf %f \n", arrMem[i]);
}
}
if (isUchar)
{
for (int i = 0; i < len; i++)
{
PRINTF("printf %d \n", arrMem[i]);
}
}
PRINTF("===========================================\n");
}
template<class T>
void printfArrayLong(T* arrMem, int time, int step)
{
bool isUchar = std::is_same<T, uchar>::value;
bool isFloat = std::is_same<T, float>::value;
if (isFloat)
{
for (int i = 0; i < time; i++)
{
PRINTF("printf %f \n", arrMem[i * step]);
}
}
if (isUchar)
{
for (int i = 0; i < time; i++)
{
PRINTF("printf %d \n", arrMem[i * step]);
}
}
PRINTF("===========================================\n");
}
static int divUp(int total, int grain)
{
return (total + grain - 1) / grain;
}
static std::vector<cv::Mat>& img2List(std::string dir, int nPic)
{
std::vector<cv::Mat> imgList;
cv::Mat h_Mat;
for (int i = 0; i < 10; i++)
{
h_Mat = cv::imread(dir.c_str() + std::to_string(i) + ".jpg");
if (!h_Mat.empty())
{
//return nullptr;
}
imgList.push_back(h_Mat);
}
return imgList;
}
/* read binary data */
static int readBinaryData(const char* filename, float* data, int w, int h, int ch)
{
FILE* subfile = fopen(filename, "r+");
fread(data, 4, w * h * ch, subfile);
return true;
}
static int readBinaryData(const char* filename, uchar* data, int w, int h, int ch)
{
FILE* subfile = fopen(filename, "r+");
fread(data, 1, w * h * ch, subfile);
return true;
}
static bool compareMat(cv::Mat A, cv::Mat B)
{
CV_Assert(A.type() == B.type());
CV_Assert(A.size() == B.size());
if (A.type() == CV_32FC3 )
{
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.cols; j++)
{
for (int k = 0; k < A.channels(); k++)
{
if (A.at<cv::Vec3f>(i, j)[k] != B.at<cv::Vec3f>(i, j)[k])
{
float a = A.at<cv::Vec3f>(i, j)[k];
float b = B.at<cv::Vec3f>(i, j)[k];
printf("i = %d, j = %d k = %d\n", i, j, k);
printf("value frist = %f, value second = %f\n",
A.at<cv::Vec3f>(i, j)[k], B.at<cv::Vec3f>(i, j)[k]);
//return false;
//break;
}
}
}
}
return true;
}
else if (A.type() == CV_8UC3)
{
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.cols; j++)
{
for (int k = 0; k < A.channels(); k++)
{
if (A.at<cv::Vec3b>(i, j)[k] != B.at<cv::Vec3b>(i, j)[k])
{
printf("i = %d, j = %d k = %d\n", i, j, k);
printf("value frist = %d, value second = %d\n",
A.at<cv::Vec3b>(i, j)[k], B.at<cv::Vec3b>(i, j)[k]);
return false;
break;
}
}
}
}
return true;
}
else
{
printf("Unsupport MAT type!!!\n");
return false;
}
}
static bool compareArray(float* A, float* B, int w, int h, int c)
{
//uchar* a = (uchar*)A;
//uchar* b = (uchar*)B;
//for (int i = 0; i < w * h * c * sizeof(float); i++)
//{
// if (a[i] != b[i])
// {
// int ofs = i / 4;
// int n = ofs / (w * h);
// int outside = ofs % (w * h);
// int x = outside % w;
// int y = outside / w;
// printf("i=%d , x = %d, y = %d n = %d\n",i, x, y, n);
// printf("value frist = %d, value second = %d\n", a[i], b[i]);
//
// if (i >1000) break;
// // return false;
// }
//}
for (int i = 0; i < w * h * c ; i++)
{
if ((A[i] - B[i]) != 0.f)
{
int n = i / (w * h);
int outside = i % (w * h);
int x = outside % w;
int y = outside / w;
printf("x = %d, y = %d n = %d\n", x, y, n);
printf("value frist = %f, value second = %f\n", A[i], B[i]);
// return false;
}
}
return true;
}
static int bbggrrArray2Mat(cv::Mat& sMat, float* fMem, uchar* uMem)
{
if (sMat.type() == CV_32FC1 || sMat.type() == CV_32FC2 ||
sMat.type() == CV_32FC3 || sMat.type() == CV_32FC4)
{
if (fMem == nullptr) return false;
/*int imgSize = sMat.cols * sMat.rows * sMat.channels();
float* h_transe = (float*)malloc(sizeof(float) * sMat.cols * sMat.rows * sMat.channels());
for (int c = 0; c < 3; c++)
{
for (int i = 0; i < sMat.rows; i++)
{
for (int j = 0; j < sMat.cols; j++)
{
h_transe[i * sMat.cols + j + c * sMat.rows * sMat.cols] = fMem[c + j * sMat.channels() + i * sMat.channels()* sMat.cols];
}
}
}*/
for (int i = 0; i < sMat.rows; i++)
{
float* lineHead = sMat.ptr<float>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = fMem[i * sMat.cols + j + c * sMat.rows * sMat.cols];
}
}
}
}
else if (sMat.type() == CV_8UC1 || sMat.type() == CV_8UC2 ||
sMat.type() == CV_8UC3 || sMat.type() == CV_8UC4)
{
if (uMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
uchar* lineHead = sMat.ptr<uchar>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = uMem[i * sMat.cols + j + c * sMat.rows * sMat.cols];
}
}
}
}
return true;
}
static int rrggbbArray2Mat(cv::Mat& sMat, float* fMem, uchar* uMem)
{
if (sMat.type() == CV_32FC1 || sMat.type() == CV_32FC2 ||
sMat.type() == CV_32FC3 || sMat.type() == CV_32FC4)
{
if (fMem == nullptr) return false;
/*int imgSize = sMat.cols * sMat.rows * sMat.channels();
float* h_transe = (float*)malloc(sizeof(float) * sMat.cols * sMat.rows * sMat.channels());
for (int c = 0; c < 3; c++)
{
for (int i = 0; i < sMat.rows; i++)
{
for (int j = 0; j < sMat.cols; j++)
{
h_transe[i * sMat.cols + j + c * sMat.rows * sMat.cols] = fMem[c + j * sMat.channels() + i * sMat.channels()* sMat.cols];
}
}
}*/
for (int i = 0; i < sMat.rows; i++)
{
float* lineHead = sMat.ptr<float>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = fMem[i * sMat.cols + j + abs(c - 2) * sMat.rows * sMat.cols];
}
}
}
}
else if (sMat.type() == CV_8UC1 || sMat.type() == CV_8UC2 ||
sMat.type() == CV_8UC3 || sMat.type() == CV_8UC4)
{
if (uMem == nullptr) return false;
for (int i = 0; i < sMat.rows; i++)
{
uchar* lineHead = sMat.ptr<uchar>(i);
for (int j = 0; j < sMat.cols; j++)
{
for (int c = 0; c < sMat.channels(); c++)
{
lineHead[j * sMat.channels() + c] = uMem[i * sMat.cols + j + abs(c -2) * sMat.rows * sMat.cols];
}
}
}
}
return true;
}
/* simulate opencv cutdown */
static uchar cvRounding(float value)
{
uchar res;
float sub = value - (int)value;
if (sub == 0.5)
res = ((int)value % 2 == 0) ? (((uchar)(value))) : (((uchar)(value)+1));
else
res = ((uchar)(value + .5f));
return res;
}