cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_gammacorrection.cpp
Go to the documentation of this file.
1 
20 #include "../../common/src/common.cpp"
21 
22 using namespace std;
23 
24 //---------------------------------------------------------
25 // HELPERS FOR THE DEBUGGING TRACES
26 // .e.g: DEBUG_LEVEL = (MDL_TRACE | IPS_TRACE)
27 //---------------------------------------------------------
28 #define THIS_NAME "TB"
29 
30 #define TRACE_OFF 0x0000
31 #define TRACE_URIF 1 << 1
32 #define TRACE_UAF 1 << 2
33 #define TRACE_MMIO 1 << 3
34 #define TRACE_ALL 0xFFFF
35 
36 #define DEBUG_LEVEL (TRACE_ALL)
37 
38 
39 //------------------------------------------------------
40 //-- TESTBENCH DEFINES
41 //------------------------------------------------------
42 #define OK true
43 #define KO false
44 #define VALID true
45 #define UNVALID false
46 #define DEBUG_TRACE true
47 
48 #define ENABLED (ap_uint<1>)1
49 #define DISABLED (ap_uint<1>)0
50 
51 
52 //------------------------------------------------------
53 //-- DUT INTERFACES AS GLOBAL VARIABLES
54 //------------------------------------------------------
55 
56 //-- SHELL / Uaf / Mmio / Config Interfaces
57 //ap_uint<2> piSHL_This_MmioEchoCtrl;
60 
61 //-- SHELL / Uaf / Udp Interfaces
62 stream<UdpWord> sSHL_Uaf_Data ("sSHL_Uaf_Data");
63 stream<UdpWord> sUAF_Shl_Data ("sUAF_Shl_Data");
64 stream<UdpWord> image_stream_from_gammacorrection ("image_stream_from_gammacorrection");
65 
66 ap_uint<32> s_udp_rx_ports = 0x0;
67 stream<NetworkMetaStream> siUdp_meta ("siUdp_meta");
68 stream<NetworkMetaStream> soUdp_meta ("soUdp_meta");
69 ap_uint<32> node_rank;
70 ap_uint<32> cluster_size;
71 
72 //------------------------------------------------------
73 //-- TESTBENCH GLOBAL VARIABLES
74 //------------------------------------------------------
75 int simCnt;
76 
77 
78 
82 void stepDut() {
88  simCnt++;
89  printf("[%4.4d] STEP DUT \n", simCnt);
90 }
91 
92 
93 
94 
95 
100 int main(int argc, char** argv) {
101 
102  //------------------------------------------------------
103  //-- TESTBENCH LOCAL VARIABLES
104  //------------------------------------------------------
105  int nrErr = 0;
106 
107  printf("#####################################################\n");
108  printf("## TESTBENCH STARTS HERE ##\n");
109  printf("#####################################################\n");
110 
111  simCnt = 0;
112  nrErr = 0;
113 
114  //------------------------------------------------------
115  //-- TESTBENCH LOCAL VARIABLES FOR GAMMACORRECTION
116  //------------------------------------------------------
117  cv::Mat in_img, img_gray;
118  cv::Mat hls_out_img, ocv_out_img;
119 
120  if (argc != 2) {
121  printf("Usage : %s <input image> \n", argv[0]);
122  return -1;
123  }
124  in_img = cv::imread(argv[1], 0); // reading in the color image
125 
126  if (!in_img.data) {
127  printf("Failed to load the image ... %s\n!", argv[1]);
128  return -1;
129  }
130  else {
131  printf("Succesfully loaded image ... %s\n!", argv[1]);
132  }
133 
134  uint16_t Thresh; // Threshold for HLS
135  float Th;
136  if (FILTER_WIDTH == 3) {
137  Th = 30532960.00;
138  Thresh = 442;
139  } else if (FILTER_WIDTH == 5) {
140  Th = 902753878016.0;
141  Thresh = 3109;
142  } else if (FILTER_WIDTH == 7) {
143  Th = 41151168289701888.000000;
144  Thresh = 566;
145  }
146 
147 
148  //------------------------------------------------------
149  //-- STEP-1.1 : CREATE MEMORY FOR OUTPUT IMAGES
150  //------------------------------------------------------
151  // cvtColor(in_img, img_gray, CV_BGR2GRAY);
152  // Convert rgb into grayscale
153  hls_out_img.create(in_img.rows, in_img.cols, CV_8U); // create memory for hls output image
154  ocv_out_img.create(in_img.rows, in_img.cols, CV_8U); // create memory for opencv output image
155 
156  #if NO
157 
158  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC1> imgInput(in_img.rows, in_img.cols);
159  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC1> imgOutput(in_img.rows, in_img.cols);
160  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC1> imgOutputTb(in_img.rows, in_img.cols);
161 
162  imgInput.copyTo(in_img.data);
163  // imgInput = xf::cv::imread<IN_TYPE, HEIGHT, WIDTH, XF_NPPC1>(argv[1], 0);
164 
165  ap_uint<INPUT_PTR_WIDTH> imgInputArray[in_img.rows * in_img.cols];
166  ap_uint<OUTPUT_PTR_WIDTH> imgOutputArrayTb[in_img.rows * in_img.cols];
167  ap_uint<OUTPUT_PTR_WIDTH> imgOutputArray[in_img.rows * in_img.cols];
168 
169  xf::cv::xfMat2Array<OUTPUT_PTR_WIDTH, IN_TYPE, HEIGHT, WIDTH, NPIX>(imgInput, imgInputArray);
170 
171  if (!dumpImgToFile(imgInput, "ifsSHL_Uaf_Data.dat", simCnt)) {
172  nrErr++;
173  }
174 
175  #endif
176 
177  #if RO
178 
179  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC8> imgInput(in_img.rows, in_img.cols);
180  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC8> imgOutput(in_img.rows, in_img.cols);
181  static xf::cv::Mat<IN_TYPE, HEIGHT, WIDTH, XF_NPPC1> imgOutputTb(in_img.rows, in_img.cols);
182 
183  // imgInput.copyTo(img_gray.data);
184  imgInput = xf::cv::imread<IN_TYPE, HEIGHT, WIDTH, XF_NPPC8>(argv[1], 0);
185 
186  #endif
187 
188 
189 
190  //------------------------------------------------------
191  //-- STEP-1.2 : RUN GAMMACORRECTION DETECTOR FROM OpenCV LIBRARY
192  //------------------------------------------------------
193  ocv_ref(in_img, ocv_out_img, Th);
194 
195 
196  //------------------------------------------------------
197  //-- STEP-2.1 : CREATE TRAFFIC AS INPUT STREAMS
198  //------------------------------------------------------
199  if (nrErr == 0) {
200  if (!setInputDataStream(sSHL_Uaf_Data, "sSHL_Uaf_Data", "ifsSHL_Uaf_Data.dat", simCnt)) {
201  printf("### ERROR : Failed to set input data stream \"sSHL_Uaf_Data\". \n");
202  nrErr++;
203  }
204 
205  //there are TOT_TRANSFERS streams from the the App to the Role
207  for (int i=0; i<TOT_TRANSFERS; i++) {
208  siUdp_meta.write(NetworkMetaStream(tmp_meta));
209  }
210  //set correct node_rank and cluster_size
211  node_rank = 1;
212  cluster_size = 2;
213  }
214 
215  //------------------------------------------------------
216  //-- STEP-2.2 : SET THE PASS-THROUGH MODE
217  //------------------------------------------------------
218  //piSHL_This_MmioEchoCtrl.write(ECHO_PATH_THRU);
219  //[TODO] piSHL_This_MmioPostPktEn.write(DISABLED);
220  //[TODO] piSHL_This_MmioCaptPktEn.write(DISABLED);
221 
222  //------------------------------------------------------
223  //-- STEP-3 : MAIN TRAFFIC LOOP
224  //------------------------------------------------------
225  while (!nrErr) {
226 
227  if (simCnt < IMG_PACKETS*3+10) // Keep enough simulation time for sequntially executing the
228  // FSMs of the main 3 functions (Rx-Proc-Tx)
229  {
230  stepDut();
231 
232  if(simCnt > 2)
233  {
234  assert(s_udp_rx_ports == 0x1);
235  }
236 
237  //if( !soUdp_meta.empty())
238  //{
239  // NetworkMetaStream tmp_meta = soUdp_meta.read();
240  // printf("NRC received NRCmeta stream from node_rank %d.\n", (int) tmp_meta.tdata.src_rank);
241  //}
242 
243 
244  } else {
245  printf("## End of simulation at cycle=%3d. \n", simCnt);
246  break;
247  }
248 
249  } // End: while()
250 
251  //-------------------------------------------------------
252  //-- STEP-4 : DRAIN AND WRITE OUTPUT FILE STREAMS
253  //-------------------------------------------------------
254  //---- UAF-->SHELL Data ----
255  if (!getOutputDataStream(sUAF_Shl_Data, "sUAF_Shl_Data", "ofsUAF_Shl_Data.dat", simCnt))
256  {
257  nrErr++;
258  }
259  //---- UAF-->SHELL META ----
260  if( !soUdp_meta.empty())
261  {
262  int i = 0;
263  while( !soUdp_meta.empty())
264  {
265  i++;
266  NetworkMetaStream tmp_meta = soUdp_meta.read();
267  printf("NRC received NRCmeta stream from rank %d to rank %d.\n", (int) tmp_meta.tdata.src_rank, (int) tmp_meta.tdata.dst_rank);
268  assert(tmp_meta.tdata.src_rank == node_rank);
269  //ensure forwarding behavior
270  assert(tmp_meta.tdata.dst_rank == ((tmp_meta.tdata.src_rank + 1) % cluster_size));
271  }
272  assert(i == TOT_TRANSFERS);
273  }
274  else {
275  printf("Error No metadata received...\n");
276  nrErr++;
277  }
278 
279  //-------------------------------------------------------
280  //-- STEP-5 : FROM THE OUTPUT FILE CREATE AN ARRAY
281  //-------------------------------------------------------
282  if (!setInputFileToArray("ofsUAF_Shl_Data.dat", imgOutputArray, simCnt)) {
283  printf("### ERROR : Failed to set input array from file \"ofsUAF_Shl_Data.dat\". \n");
284  nrErr++;
285  }
286  xf::cv::Array2xfMat<INPUT_PTR_WIDTH, OUT_TYPE, HEIGHT, WIDTH, NPIX>(imgOutputArray, imgOutput);
287 
288 
289  //------------------------------------------------------
290  //-- STEP-6 : COMPARE INPUT AND OUTPUT FILE STREAMS
291  //------------------------------------------------------
292  int rc1 = system("diff --brief -w -i -y ../../../../test/ofsUAF_Shl_Data.dat \
293  ../../../../test/verify_UAF_Shl_Data.dat");
294  if (rc1)
295  {
296  printf("## Error : File \'ofsUAF_Shl_Data.dat\' does not match \'verify_UAF_Shl_Data.dat\'.\n");
297  } else {
298  printf("Output data in file \'ofsUAF_Shl_Data.dat\' verified.\n");
299  }
300 
301  nrErr += rc1;
302 
303  printf("#####################################################\n");
304  if (nrErr)
305  {
306  printf("## ERROR - TESTBENCH FAILED (RC=%d) !!! ##\n", nrErr);
307  } else {
308  printf("## SUCCESSFULL END OF TESTBENCH (RC=0) ##\n");
309  }
310  printf("#####################################################\n");
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325  float K = 0.04;
326  uint16_t k = K * (1 << 16); // Convert to Q0.16 format
327 
328  #if NO
329 
330  // L2 Vitis Gammacorrection
331  GammacorrectionAccelArray(imgInputArray, imgOutputArrayTb, in_img.rows, in_img.cols, Thresh, k);
332  xf::cv::Array2xfMat<INPUT_PTR_WIDTH, OUT_TYPE, HEIGHT, WIDTH, NPIX>(imgOutputArrayTb, imgOutputTb);
333 
334  // L1 Vitis Gammacorrection
335  //gammacorrection_accel(imgInput, imgOutput, Thresh, k);
336 
337  #endif
338 
339  #if RO
340 
341  gammacorrection_accel(imgInput, imgOutputTb, Thresh, k);
342 
343  #endif
344 
346  xf::cv::imwrite("hls_out_tb.jpg", imgOutputTb);
347  xf::cv::imwrite("hls_out.jpg", imgOutput);
348 
349  unsigned int val;
350  unsigned short int row, col;
351 
352  cv::Mat out_img;
353  out_img = in_img.clone();
354 
355  std::vector<cv::Point> hls_points;
356  std::vector<cv::Point> ocv_points;
357  std::vector<cv::Point> common_pts;
358 
359  xf::cv::Mat<OUT_TYPE, HEIGHT, WIDTH, NPIX>* select_imgOutput;
360 
361  // Select which output you want to process for image outputs and corners comparisons:
362  // &imgOutput : The processed image by Gammacorrection IP inside the ROLE (i.e. I/O traffic is passing through SHELL)
363  // &imgOutputTb : The processed image by Gammacorrection IP in this testbench (i.e. I/O traffic is done in testbench)
364  select_imgOutput = &imgOutput;
365 
366  // Mark HLS points on the image
367  markPointsOnImage(*select_imgOutput, in_img, out_img, hls_points);
368 
369  // Write HLS and Opencv corners into a file
370  //nrErr +=
371  writeCornersIntoFile(in_img, ocv_out_img, out_img, hls_points, ocv_points, common_pts);
372 
373  // Gamma Correction TB
374  xf::cv::imwrite("gammacorrection_in_hls.jpg", imgInput);
375  gammacorrection_accel(imgInput, imgOutput, 0.1);
376  xf::cv::imwrite("gammacorrection_out_hls.jpg", imgOutput);
377 
378 
379  return(nrErr);
380 }
381 
382 
383 
384 
#define FILTER_WIDTH
void gammacorrection(ap_uint< 32 > *pi_rank, ap_uint< 32 > *pi_size, stream< NetworkWord > &siSHL_This_Data, stream< NetworkWord > &soTHIS_Shl_Data, stream< NetworkMetaStream > &siNrc_meta, stream< NetworkMetaStream > &soNrc_meta, ap_uint< 32 > *po_rx_ports)
Main process of the Gammacorrection Application directives.
stream< NetworkMetaStream > siUdp_meta("siUdp_meta")
int main(int argc, char **argv)
Main testbench of Hrris.
stream< NetworkMetaStream > soUdp_meta("soUdp_meta")
void ocv_ref(cv::Mat img_gray, cv::Mat &ocv_out_img, float Th)
Definition: xf_ocv_ref.hpp:552
int simCnt
ap_uint< 1 > piSHL_This_MmioCaptPktEn
ap_uint< 1 > piSHL_This_MmioPostPktEn
stream< UdpWord > sUAF_Shl_Data("sUAF_Shl_Data")
void stepDut()
Run a single iteration of the DUT model.
stream< UdpWord > image_stream_from_gammacorrection("image_stream_from_gammacorrection")
stream< UdpWord > sSHL_Uaf_Data("sSHL_Uaf_Data")
ap_uint< 32 > cluster_size
ap_uint< 32 > s_udp_rx_ports
ap_uint< 32 > node_rank
void gammacorrection_accel(xf::cv::Mat< XF_8UC1, 256, 256, XF_NPPC1 > &_src, xf::cv::Mat< XF_8UC1, 256, 256, XF_NPPC1 > &_dst, unsigned short Thresh, unsigned short k)
void markPointsOnImage(Mat &imgOutput, Mat &in_img, Mat &out_img, vector< Point > &hw_points)
Mark the points found by Gammacorrection into the image.
#define IMG_PACKETS
void GammacorrectionAccelArray(ap_uint< 64 > *img_inp, ap_uint< 64 > *img_out, int rows, int cols, int threshold, int k)
Top-level accelerated function of the Gammacorrection Application with array I/F.
#define TOT_TRANSFERS
Definition: config.h:70
bool setInputDataStream(stream< UdpAppData > &sDataStream, const string dataStreamName, const string inpFileName)
Initialize an input data stream from a file.
Definition: tb_nal.cpp:214
#define DEFAULT_RX_PORT
Definition: nal.hpp:139
bool getOutputDataStream(stream< UdpAppData > &sDataStream, const string dataStreamName, const string outFileName)
Fill an output file with data from an output stream.
Definition: tb_nal.cpp:526
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 dumpImgToFile(xf::cv::Mat< OUT_TYPE, HEIGHT, WIDTH, NPIX > &_img, const std::string outFileName, int simCnt)
Fill an output file with data from an image.
bool setInputFileToArray(const std::string inpFileName, ap_uint< OUTPUT_PTR_WIDTH > *imgArray, int simCnt)
Initialize an input array from a file with format "tdata tkeep tlast".
NetworkMeta tdata
Definition: network.hpp:109
NodeId dst_rank
Definition: network.hpp:95
NodeId src_rank
Definition: network.hpp:97