cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
common.cpp
Go to the documentation of this file.
1 
17 #include "../include/common.hpp"
18 
19 using namespace std;
20 using namespace cv;
21 
22 
23 
24 
25 
33 bool setInputDataStream(stream<UdpWord> &sDataStream, const string dataStreamName,
34  const string inpFileName, int simCnt) {
35  string strLine;
36  ifstream inpFileStream;
37  string datFile = "../../../../test/" + inpFileName;
38  UdpWord udpWord;
39 
40  //-- STEP-1 : OPEN FILE
41  inpFileStream.open(datFile.c_str());
42  if ( !inpFileStream ) {
43  cout << "### ERROR : Could not open the input data file " << datFile << endl;
44  return(KO);
45  }
46 
47  //-- STEP-2 : SET DATA STREAM
48  while (inpFileStream) {
49 
50  if (!inpFileStream.eof()) {
51 
52  getline(inpFileStream, strLine);
53  if (strLine.empty()) continue;
54  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
55 
56  // Write to sDataStream
57  if (sDataStream.full()) {
58  printf("### ERROR : Stream is full. Cannot write stream with data from file \"%s\".\n", inpFileName.c_str());
59  return(KO);
60  } else {
61  sDataStream.write(udpWord);
62  // Print Data to console
63  printf("[%4.4d] TB is filling input stream [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
64  simCnt, dataStreamName.c_str(),
65  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
66  }
67  }
68  }
69 
70  //-- STEP-3: CLOSE FILE
71  inpFileStream.close();
72 
73  return(OK);
74 }
75 
76 
77 
78 
85 bool setInputFileToArray(const string inpFileName, ap_uint<OUTPUT_PTR_WIDTH>* imgArray, int simCnt) {
86  string strLine;
87  ifstream inpFileStream;
88  string datFile = "../../../../test/" + inpFileName;
89  UdpWord udpWord;
90  unsigned int index = 0;
91  ap_uint<OUTPUT_PTR_WIDTH> current_tdata;
92 
93  //-- STEP-1 : OPEN FILE
94  inpFileStream.open(datFile.c_str());
95  if ( !inpFileStream ) {
96  cout << "### ERROR : Could not open the input data file " << datFile << endl;
97  return(KO);
98  }
99 
100  //-- STEP-2 : SET DATA STREAM
101  while (inpFileStream) {
102 
103  if (!inpFileStream.eof()) {
104 
105  getline(inpFileStream, strLine);
106  if (strLine.empty()) continue;
107  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
108  for (unsigned int i=0; i<(BITS_PER_10GBITETHRNET_AXI_PACKET/OUTPUT_PTR_WIDTH); i++) {
109  current_tdata = (ap_uint<OUTPUT_PTR_WIDTH>)(udpWord.tdata >> i*8);//FIXME: check
110  // possible seg.fault
111  imgArray[index++] = current_tdata;
112  // Print Data to console
113  printf("[%4.4d] TB is filling input array from [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d, current_tdata=0x%16.16llX} \n",
114  simCnt, inpFileName.c_str(),
115  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int(),
116  current_tdata.to_long());
117  }
118  }
119  }
120 
121  //-- STEP-3: CLOSE FILE
122  inpFileStream.close();
123 
124  return(OK);
125 }
126 
127 
128 
137 bool readDataStream(stream <UdpWord> &sDataStream, UdpWord *udpWord) {
138  // Get the DUT/Data results
139  sDataStream.read(*udpWord);
140  return(VALID);
141 }
142 
143 
144 
150 ap_uint<64> pack_ap_uint_64_ (ap_uint<8> *buffer) {
151 
152  ap_uint<64> value ;
153 
154  value = buffer[7] ;
155  value = (value << 8 ) + buffer[6] ;
156  value = (value << 8 ) + buffer[5] ;
157  value = (value << 8 ) + buffer[4] ;
158  value = (value << 8 ) + buffer[3] ;
159  value = (value << 8 ) + buffer[2] ;
160  value = (value << 8 ) + buffer[1] ;
161  value = (value << 8 ) + buffer[0] ;
162 
163  return value ;
164 
165 }
166 
167 
174 bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream) {
175  if (!outFileStream.is_open()) {
176  printf("### ERROR : Output file stream is not open. \n");
177  return(KO);
178  }
179  outFileStream << hex << noshowbase << setfill('0') << setw(16) << udpWord->tdata.to_uint64();
180  outFileStream << " ";
181  outFileStream << hex << noshowbase << setfill('0') << setw(2) << udpWord->tkeep.to_int();
182  outFileStream << " ";
183  outFileStream << setw(1) << udpWord->tlast.to_int() << "\n";
184  return(OK);
185 }
186 
187 
188 
196 bool getOutputDataStream(stream<UdpWord> &sDataStream,
197  const string dataStreamName, const string outFileName, int simCnt)
198 {
199  string strLine;
200  ofstream outFileStream;
201  string datFile = "../../../../test/" + outFileName;
202  UdpWord udpWord;
203  bool rc = OK;
204 
205  //-- STEP-1 : OPEN FILE
206  outFileStream.open(datFile.c_str());
207  if ( !outFileStream ) {
208  cout << "### ERROR : Could not open the output data file " << datFile << endl;
209  return(KO);
210  }
211 
212  //-- STEP-2 : EMPTY STREAM AND DUMP DATA TO FILE
213  while (!sDataStream.empty()) {
214  if (readDataStream(sDataStream, &udpWord) == VALID) {
215  // Print DUT/Data to console
216  printf("[%4.4d] TB is draining output stream [%s] - Data read = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
217  simCnt, dataStreamName.c_str(),
218  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
219  if (!dumpDataToFile(&udpWord, outFileStream)) {
220  rc = KO;
221  break;
222  }
223  }
224  }
225 
226  //-- STEP-3: CLOSE FILE
227  outFileStream.close();
228 
229  return(rc);
230 }
231 
232 
233 
240 bool dumpImgToFile(xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPIX>& _img,
241  const string outFileName, int simCnt)
242 {
243  string strLine;
244  ofstream outFileStream;
245  string datFile = "../../../../test/" + outFileName;
246  UdpWord udpWord;
247  bool rc = OK;
248  unsigned int bytes_per_line = 8;
249 
250  //-- STEP-1 : OPEN FILE
251  outFileStream.open(datFile.c_str());
252  if ( !outFileStream ) {
253  cout << "### ERROR : Could not open the output data file " << datFile << endl;
254  return(KO);
255  }
256  printf("came to dumpImgToFile: _img.rows=%u, img.cols=%u\n", _img.rows, _img.cols);
257 
258  ap_uint<8> value[bytes_per_line];
259 
260  //-- STEP-2 : DUMP IMAGE DATA TO FILE
261  for (unsigned int total_bytes = 0, chan =0 ; chan < _img.channels(); chan++) {
262  for (unsigned int j = 0; j < _img.rows; j++) {
263  int l = 0;
264  for (unsigned int i = 0; i < (_img.cols >> XF_BITSHIFT(NPIX)); i+=bytes_per_line, total_bytes+=bytes_per_line) {
265  //if (NPIX == XF_NPPC8) {
266  for (unsigned int k = 0; k < bytes_per_line; k++) {
267  value[k] = _img.read(j * (_img.cols >> XF_BITSHIFT(NPIX)) + i + k);
268  }
269  udpWord.tdata = pack_ap_uint_64_(value);
270  udpWord.tkeep = 255;
271  // We are signaling a packet termination either at the end of the image or the end of MTU
272  if ((total_bytes >= (_img.rows * _img.cols * _img.channels() - bytes_per_line)) ||
273  ((total_bytes + bytes_per_line) % PACK_SIZE == 0)) {
274  udpWord.tlast = 1;
275  }
276  else {
277  udpWord.tlast = 0;
278  }
279  printf("[%4.4d] IMG TB is dumping image to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
280  simCnt, datFile.c_str(), total_bytes, value,
281  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
282  if (!dumpDataToFile(&udpWord, outFileStream)) {
283  rc = KO;
284  break;
285  }
286  //}
287  }
288  }
289  }
290  //-- STEP-3: CLOSE FILE
291  outFileStream.close();
292 
293  return(rc);
294 }
295 
296 
297 
304 bool dumpImgToFileWarpTransform(xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPIX>& _img,
305  const string outFileName, int simCnt, float * transform_matrix)
306 {
307  string strLine;
308  ofstream outFileStream;
309  string datFile = "../../../../test/" + outFileName;
310  UdpWord udpWord;
311  bool rc = OK;
312  unsigned int bytes_per_line = 8;
313 
314  //-- STEP-1 : OPEN FILE
315  outFileStream.open(datFile.c_str());
316  if ( !outFileStream ) {
317  cout << "### ERROR : Could not open the output data file " << datFile << endl;
318  return(KO);
319  }
320  printf("came to dumpImgToFile: _img.rows=%u, img.cols=%u\n", _img.rows, _img.cols);
321 
322  ap_uint<8> value[bytes_per_line];
323  ap_uint<8> tx_cmd[bytes_per_line];
324  ap_uint<8> img_cmd[bytes_per_line];
325  //init tx and img cmd
326  for (unsigned int k = 0; k < bytes_per_line; k++) {
327  value[k] = (char)0;
328  if (k != 0) {
329  tx_cmd[k] = (char)0;
330  img_cmd[k] = (char)0;
331  }
332  else {
333  tx_cmd[k] = (char)1;
334  img_cmd[k] = (char)2;
335  }
336  }
337 
338  //dump tx cmd
339  udpWord.tdata = pack_ap_uint_64_(tx_cmd);
340  udpWord.tkeep = 255;
341  udpWord.tlast = 0;
342  if (!dumpDataToFile(&udpWord, outFileStream)) {
343  rc = KO;
344  outFileStream.close();
345  return(rc);
346  }
347  int off = 4;
348  for (int i = 0; i < 8; i++)
349  {
350  memcpy(value+off, (float*)transform_matrix+i, 4);
351  off += 4;
352  off = off % bytes_per_line;
353  std::cout << "[DEBUG] off=" << off << " tx mat=" << transform_matrix[i] << " idx=" << i << " flt val=" << value[off] << " " << value[off+1] << " " << value[off+2] << " " << value[off+3]<< std::endl;
354  if (i%2 && i!=0)
355  {
356  std::cout << "[DEBUG] packing the valua :D" << std::endl;
357  udpWord.tdata = pack_ap_uint_64_(value);
358  udpWord.tkeep = 255;
359  udpWord.tlast = 0;
360  if (!dumpDataToFile(&udpWord, outFileStream)) {
361  rc = KO;
362  outFileStream.close();
363  return(rc);
364  }
365  }
366 
367  }
368 
369  //dump last value
370  unsigned int zero_constant = 0;
371  memcpy(value, (char*)transform_matrix+8, 4);
372  memcpy(value, (char*)&zero_constant, 4);
373  udpWord.tdata = pack_ap_uint_64_(value);
374  udpWord.tkeep = 255;
375  udpWord.tlast = 0;
376  if (!dumpDataToFile(&udpWord, outFileStream)) {
377  rc = KO;
378  outFileStream.close();
379  return(rc);
380  }
381 
382  //creating img mat cmd
383  memcpy(img_cmd+6, (char*)&_img.rows, 2);
384  memcpy(img_cmd+4, (char*)&_img.cols, 2);
385  img_cmd[1]=_img.channels();
386 
387  udpWord.tdata = pack_ap_uint_64_(img_cmd);
388  udpWord.tkeep = 255;
389  udpWord.tlast = 0;
390  if (!dumpDataToFile(&udpWord, outFileStream)) {
391  rc = KO;
392  outFileStream.close();
393  return(rc);
394  }
395  //-- STEP-2 : DUMP IMAGE DATA TO FILE
396  for (unsigned int total_bytes = 0, chan =0 ; chan < _img.channels(); chan++) {
397  for (unsigned int j = 0; j < _img.rows; j++) {
398  int l = 0;
399  for (unsigned int i = 0; i < (_img.cols >> XF_BITSHIFT(NPIX)); i+=bytes_per_line, total_bytes+=bytes_per_line) {
400  //if (NPIX == XF_NPPC8) {
401  for (unsigned int k = 0; k < bytes_per_line; k++) {
402  value[k] = _img.read(j * (_img.cols >> XF_BITSHIFT(NPIX)) + i + k);
403  }
404  udpWord.tdata = pack_ap_uint_64_(value);
405  udpWord.tkeep = 255;
406  // We are signaling a packet termination either at the end of the image or the end of MTU
407  if ((total_bytes >= (_img.rows * _img.cols * _img.channels() - bytes_per_line)) ||
408  ((total_bytes + bytes_per_line) % PACK_SIZE == 0)) {
409  udpWord.tlast = 1;
410  }
411  else {
412  udpWord.tlast = 0;
413  }
414  printf("[%4.4d] IMG TB is dumping image to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
415  simCnt, datFile.c_str(), total_bytes, value,
416  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
417  if (!dumpDataToFile(&udpWord, outFileStream)) {
418  rc = KO;
419  break;
420  }
421  //}
422  }
423  }
424  }
425  //-- STEP-3: CLOSE FILE
426  outFileStream.close();
427 
428  return(rc);
429 }
430 
431 
432 
437 unsigned int writeCornersIntoFile(cv::Mat& in_img, cv::Mat& ocv_out_img, cv::Mat& out_img,
438  std::vector<cv::Point>& hls_points,
439  std::vector<cv::Point>& ocv_points,
440  std::vector<cv::Point>& common_pts) {
441 
442  cv::Mat ocvpnts, hlspnts;
443 
444  ocvpnts = in_img.clone();
445 
446  int nhls = hls_points.size();
447 
449  for (int j = 1; j < ocv_out_img.rows - 1; j++) {
450  for (int i = 1; i < ocv_out_img.cols - 1; i++) {
451  if ((int)ocv_out_img.at<unsigned char>(j, i)) {
452  cv::circle(ocvpnts, cv::Point(i, j), 5, cv::Scalar(0, 0, 255), 2, 8, 0);
453  ocv_points.push_back(cv::Point(i, j));
454  }
455  }
456  }
457 
458  printf("ocv corner count = %d, Hls corner count = %d\n", ocv_points.size(), hls_points.size());
459  int nocv = ocv_points.size();
460 
461  /* End
462  */
463  /* Find common points in among opencv and HLS
464  */
465  int ocv_x, ocv_y, hls_x, hls_y;
466  for (int j = 0; j < nocv; j++) {
467  for (int k = 0; k < nhls; k++) {
468  ocv_x = ocv_points[j].x;
469  ocv_y = ocv_points[j].y;
470  hls_x = hls_points[k].x;
471  hls_y = hls_points[k].y;
472 
473  if ((ocv_x == hls_x) && (ocv_y == hls_y)) {
474  common_pts.push_back(ocv_points[j]);
475  break;
476  }
477  }
478  }
479  /* End */
480  imwrite("output_hls.png", out_img); // HLS Image
481  imwrite("output_ocv.png", ocvpnts); // Opencv Image
482  /* Success, Loss and Gain Percentages */
483  float persuccess, perloss, pergain;
484 
485  int totalocv = ocv_points.size();
486  int ncommon = common_pts.size();
487  int totalhls = hls_points.size();
488  persuccess = (((float)ncommon / totalhls) * 100);
489  perloss = (((float)(totalocv - ncommon) / totalocv) * 100);
490  pergain = (((float)(totalhls - ncommon) / totalhls) * 100);
491 
492  printf("Commmon = %d\t Success = %f\t Loss = %f\t Gain = %f\n", ncommon, persuccess, perloss, pergain);
493 
494  if (persuccess < 60 || totalhls == 0)
495  return 1;
496  else
497  return 0;
498 
499 }
500 
501 
506 void markPointsOnImage(xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPIX>& imgOutput, cv::Mat& in_img,
507  cv::Mat& out_img, std::vector<cv::Point>& hls_points) {
508 
509  for (int j = 0; j < imgOutput.rows; j++) {
510  int l = 0;
511  for (int i = 0; i < (imgOutput.cols >> XF_BITSHIFT(NPIX)); i++) {
512  if (NPIX == XF_NPPC8) {
513  ap_uint<64> value =
514  imgOutput.read(j * (imgOutput.cols >> XF_BITSHIFT(NPIX)) + i); //.at<unsigned char>(j,i);
515  for (int k = 0; k < 64; k += 8, l++) {
516  uchar pix = value.range(k + 7, k);
517  if (pix != 0) {
518  cv::Point tmp;
519  tmp.x = l;
520  tmp.y = j;
521  if ((tmp.x < in_img.cols) && (tmp.y < in_img.rows) && (j > 0)) {
522  hls_points.push_back(tmp);
523  }
524  short int y, x;
525  y = j;
526  x = l;
527  if (j > 0) cv::circle(out_img, cv::Point(x, y), 5, cv::Scalar(0, 0, 255, 255), 2, 8, 0);
528  }
529  }
530  }
531  if (NPIX == XF_NPPC1) {
532  unsigned char pix = imgOutput.read(j * (imgOutput.cols >> XF_BITSHIFT(NPIX)) + i);
533  if (pix != 0) {
534  cv::Point tmp;
535  tmp.x = i;
536  tmp.y = j;
537  if ((tmp.x < in_img.cols) && (tmp.y < in_img.rows) && (j > 0)) {
538  hls_points.push_back(tmp);
539  }
540  short int y, x;
541  y = j;
542  x = i;
543  if (j > 0) cv::circle(out_img, cv::Point(x, y), 5, cv::Scalar(0, 0, 255, 255), 2, 8, 0);
544  }
545  }
546  }
547  }
548 }
549 
550 
551 
552 
int simCnt
Definition: tb_fmc.cpp:113
void markPointsOnImage(Mat &imgOutput, Mat &in_img, Mat &out_img, vector< Point > &hw_points)
Mark the points found by Gammacorrection into the image.
#define NPIX
ap_uint< 64 > pack_ap_uint_64_(ap_uint< 8 > *buffer)
Pack an array of 8 x ap_uint<8> into a ap_uint<64> word.
Definition: common.cpp:116
bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream)
Dump a data word to a file.
Definition: common.cpp:153
bool setInputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string inpFileName, int simCnt)
Initialize an input data stream from a file.
Definition: common.cpp:45
bool getOutputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string outFileName, int simCnt)
Fill an output file with data from an output stream.
Definition: common.cpp:176
#define BITS_PER_10GBITETHRNET_AXI_PACKET
#define OUTPUT_PTR_WIDTH
#define PACK_SIZE
Definition: config.h:51
bool readDataStream(stream< UdpAppData > &sDataStream, UdpAppData *udpWord)
Read data from a stream.
Definition: tb_nal.cpp:395
#define VALID
Definition: tb_nal.cpp:43
#define OK
Definition: nts_types.hpp:57
#define KO
Definition: nts_types.hpp:58
bool dumpImgToFile(xf::cv::Mat< OUT_TYPE, HEIGHT, WIDTH, NPIX > &_img, const string outFileName, int simCnt)
Fill an output file with data from an image.
Definition: common.cpp:240
bool dumpImgToFileWarpTransform(xf::cv::Mat< OUT_TYPE, HEIGHT, WIDTH, NPIX > &_img, const string outFileName, int simCnt, float *transform_matrix)
Fill an output file with data from an image.
Definition: common.cpp:304
unsigned int writeCornersIntoFile(cv::Mat &in_img, cv::Mat &ocv_out_img, cv::Mat &out_img, std::vector< cv::Point > &hls_points, std::vector< cv::Point > &ocv_points, std::vector< cv::Point > &common_pts)
Write the corners found by Harris into a file.
Definition: common.cpp:437
bool setInputFileToArray(const string inpFileName, ap_uint< OUTPUT_PTR_WIDTH > *imgArray, int simCnt)
Initialize an input array from a file with format "tdata tkeep tlast".
Definition: common.cpp:85
ap_uint< 64 > tdata
ap_uint< 1 > tlast
ap_uint< 8 > tkeep