write() in some specific buffer data arrangement mechanism

Could anyone advise on https://gist.github.com/promach/9d185d35a6e6db0da10992a19c36f754#file-host-cpp-L147 or line 147 of the following coding ?


this is not about solving error. I have not even finished putting in arguments into the write() function. I am not sure how to code the arguments. Please see the comment on line 147 of the code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// g++ -g host.cpp -o host `pkg-config --cflags --libs opencv`

#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>      // std::ifstream, std::ofstream
#include <string>
#include <sys/wait.h>
#include <errno.h>
#include <cmath>

using namespace cv;
using namespace std;

unsigned int image_width;
unsigned int image_height;

const unsigned int CHNL_NUM = 3;
const unsigned int RED_CHNL = 0;
const unsigned int GREEN_CHNL = 1;
const unsigned int BLUE_CHNL = 2;

struct RGB_packet{
  uint8_t R,G,B;
};

struct YUV_packet{
  uint8_t Y,U,V;
};


struct YUV_packet* rgb2yuv(struct RGB_packet rgb_input)  // convert rgb to yuv
{  
	uint8_t red = rgb_input.R;
	uint8_t green = rgb_input.G;
	uint8_t blue = rgb_input.B;

	struct YUV_packet *yuv_result;

	uint8_t Y = yuv_result->Y;
	uint8_t U = yuv_result->U;
	uint8_t V = yuv_result->V;

	// https://www.pcmag.com/encyclopedia/term/55166/yuv-rgb-conversion-formulas
    Y = (uint8_t)(0.299*red + 0.587*green + 0.114*blue);
    U = (uint8_t)(0.492*(blue-Y));
    V = (uint8_t)(0.877*(red-Y));

	return yuv_result;
}

int main(int argc, char *argv[]) {

  int fdr, fdw, rc, donebytes;
  char *buf;
  pid_t pid;
  struct RGB_packet *tologic;
  struct YUV_packet *fromlogic;

  fdr = open("/dev/stdin", O_RDONLY);  // will change to /dev/xillybus_read_128
  fdw = open("/dev/stdout", O_WRONLY); // will change to /dev/xillybus_write_128

  if ((fdr < 0) || (fdw < 0)) {
    perror("Failed to open Xillybus device file(s)");
    exit(1);
  }

  // READ in an image file

  String imageName( "lena512color.tiff" ); // by default

  if( argc > 1)
  {
	imageName = argv[1];
  }

  Mat image;

  image = imread( imageName, IMREAD_COLOR ); // Read the file

  if( image.empty() )                      // Check for invalid input
  {
	cout <<  "Could not open or find the image" << std::endl ;
	return -1;
  }

  else
  {
	image_width = image.size().width;
	image_height = image.size().height;
  }

  namedWindow( "Original Image", CV_WINDOW_AUTOSIZE );
  imshow( "Original Image", image );
	 
  Mat rgbchannel[CHNL_NUM];
  // The actual splitting.
  split(image, rgbchannel);
	 
  namedWindow("Blue",CV_WINDOW_AUTOSIZE);
  imshow("Red", rgbchannel[RED_CHNL]);
	 
  namedWindow("Green",CV_WINDOW_AUTOSIZE);
  imshow("Green", rgbchannel[GREEN_CHNL]);
	 
  namedWindow("Red",CV_WINDOW_AUTOSIZE);
  imshow("Blue", rgbchannel[BLUE_CHNL]);

  waitKey(0);  // see all three split channels before feeding in the channel data to xillybus/RIFFA for hardware computation


  pid = fork();

  if (pid < 0) {
    perror("Failed to fork()");
    exit(1);
  }

  if (pid) {
	close(fdr);

	vector<RGB_packet> vTo(sizeof(struct RGB_packet) * image_width * image_height);  // lena.tiff is sized as 512*512*3

    tologic = vTo.data();

    if (!tologic) {
      fprintf(stderr, "Failed to allocate memory\n");
      exit(1);
    }

	tologic->R = *(rgbchannel[RED_CHNL].data);
	tologic->G = *(rgbchannel[GREEN_CHNL].data);
	tologic->B = *(rgbchannel[BLUE_CHNL].data);

    buf = (char *) tologic;
    donebytes = 0;

	const unsigned int PIXEL_NUM_THAT_FITS_STREAM_WIDTH = 5;  // 128-bit stream can at most fits 5 pixels (5*24 bits = 120 bits), each pixels contains R, G, B which are encoded in 8 bits for each of the three color components

	while (donebytes < sizeof(struct RGB_packet) * image_width * image_height) 
	{
	  if(((sizeof(struct RGB_packet) * image_width * image_height)-donebytes) >= PIXEL_NUM_THAT_FITS_STREAM_WIDTH)
	  {
	  	rc = write();  // arrange the five pixels in the format as in https://i.imgur.com/mdJwk7J.png
	  }
	  else  // the remaining pixels do not fill all five pixel slots for a 128-bit stream
	  {
		rc = write(fdw, buf + donebytes, sizeof(struct RGB_packet) * image_width * image_height - donebytes);
	  }

	  if ((rc < 0) && (errno == EINTR))
		continue;

	  if (rc <= 0) {
		perror("write() failed");
		exit(1);
	  }

	  donebytes += rc;
	}

	sleep(1); // Let debug output drain (if used)

	close(fdw);

	return 0;
  } 

  else {
    close(fdw);

	vector<YUV_packet> vFrom(sizeof(struct YUV_packet) * image_width * image_height);

    fromlogic = vFrom.data();

    if (!fromlogic) {
      fprintf(stderr, "Failed to allocate memory\n");
      exit(1);
    }

    buf = (char *) fromlogic;
    donebytes = 0;

    while (donebytes < sizeof(struct YUV_packet) * image_width * image_height) {
      rc = read(fdr, buf + donebytes, sizeof(struct YUV_packet) * image_width * image_height - donebytes);

      if ((rc < 0) && (errno == EINTR))
		continue;

      if (rc < 0) {
		perror("read() failed");
		exit(1);
      }

      if (rc == 0) {
		fprintf(stderr, "Reached read EOF!? Should never happen.\n");
		exit(0);
      }

      donebytes += rc;
    }

    for (int i = 0; i < (image_width * image_height); i++)  // check the perfomance of hardware with respect to software computation
	{
		if((abs(rgb2yuv(tologic[i])->Y - fromlogic[i].Y) > 1) ||
		   (abs(rgb2yuv(tologic[i])->U - fromlogic[i].U) > 1) ||
		   (abs(rgb2yuv(tologic[i])->V - fromlogic[i].V) > 1)) // rgb2yuv conversion hardware tolerance fails by more than 1 compared to software computation
		{
			printf("R:%d G:%d B:%d \n", tologic[i].R, tologic[i].G, tologic[i].B);
      		printf("Y:%d U:%d V:%d \n", fromlogic[i].Y, fromlogic[i].U, fromlogic[i].V);
		}
	}

    sleep(1); // Let debug output drain (if used)

    close(fdr);

    return 0;
  }
}
Last edited on
I have finished coding the R, G, B arrangement formatting mechanism, yet I am getting many zeroes for the data.

I have updated the code accordingly and what I worry the most is the following code segment at https://gist.github.com/promach/9d185d35a6e6db0da10992a19c36f754#file-host-cpp-L136-L147

1
2
3
4
5
6
7
8
9
10
11
12
vector<RGB_packet> vTo(sizeof(struct RGB_packet) * image_width * image_height);  // lena.tiff is sized as 512*512*3

tologic = vTo.data();

if (!tologic) {
  fprintf(stderr, "Failed to allocate memory\n");
  exit(1);
}

tologic->R = *(rgbchannel[RED_CHNL].data);
tologic->G = *(rgbchannel[GREEN_CHNL].data);
tologic->B = *(rgbchannel[BLUE_CHNL].data);


how do I trace the structure tologic in gdb ? I am getting the following gdb output though

phung@UbuntuHW15:~/Documents/fpga_overlay/xillybus-eval-xl-virtex7-2.0b_DATE/verilog/vivado/xillydemo.srcs/sources_1/imports/taylor$ g++ -g host.cpp -o host `pkg-config --cflags --libs opencv`
phung@UbuntuHW15:~/Documents/fpga_overlay/xillybus-eval-xl-virtex7-2.0b_DATE/verilog/vivado/xillydemo.srcs/sources_1/imports/taylor$ gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file host
Reading symbols from host...done.
(gdb) break 146
Breakpoint 1 at 0x401b9e: file host.cpp, line 146.
(gdb) run
Starting program: /home/phung/Documents/fpga_overlay/xillybus-eval-xl-virtex7-2.0b_DATE/verilog/vivado/xillydemo.srcs/sources_1/imports/taylor/host 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffd7c5b700 (LWP 20787)]
[New Thread 0x7fffd545a700 (LWP 20788)]
[New Thread 0x7fffd2c59700 (LWP 20789)]
[New Thread 0x7fffd0458700 (LWP 20790)]
[New Thread 0x7fffcdc57700 (LWP 20791)]
[New Thread 0x7fffcb456700 (LWP 20792)]
[New Thread 0x7fffc8c55700 (LWP 20793)]
[New Thread 0x7fffc6454700 (LWP 20794)]
[New Thread 0x7fffc3c53700 (LWP 20795)]
[New Thread 0x7fffc1452700 (LWP 20796)]
[New Thread 0x7fffc0c51700 (LWP 20797)]
[New Thread 0x7fffb984b700 (LWP 20798)]
[New Thread 0x7fffb904a700 (LWP 20799)]
[New Thread 0x7fffb8849700 (LWP 20800)]
[Thread 0x7fffc0c51700 (LWP 20797) exited]
[Thread 0x7fffc1452700 (LWP 20796) exited]
[Thread 0x7fffc3c53700 (LWP 20795) exited]
[Thread 0x7fffc6454700 (LWP 20794) exited]
[Thread 0x7fffc8c55700 (LWP 20793) exited]
[Thread 0x7fffcb456700 (LWP 20792) exited]
[Thread 0x7fffcdc57700 (LWP 20791) exited]
[Thread 0x7fffd0458700 (LWP 20790) exited]
[Thread 0x7fffd2c59700 (LWP 20789) exited]
[Thread 0x7fffd545a700 (LWP 20788) exited]
[Thread 0x7fffd7c5b700 (LWP 20787) exited]

Thread 1 "host" hit Breakpoint 1, main (argc=1, argv=0x7fffffffd638) at host.cpp:146
146		tologic->G = *(rgbchannel[GREEN_CHNL].data);
(gdb) print tologic->R
$1 = 125 '}'
(gdb) print tologic[4]->R
$2 = 0 '\000'
(gdb) print tologic[3]->R
$3 = 0 '\000'
(gdb) print tologic[6]->R
$4 = 0 '\000'
(gdb) print tologic[0]->R
$5 = 125 '}'
(gdb) print tologic[1]->R
$6 = 0 '\000'
(gdb) print tologic[8]->R
$7 = 0 '\000'
(gdb) print tologic[8].R
$8 = 0 '\000'
(gdb) print tologic[7].R
$9 = 0 '\000'
(gdb) print tologic[0].R
$10 = 125 '}'
(gdb) print rgbchannel[0]
$11 = {flags = 1124024320, dims = 2, rows = 512, cols = 512, 
  data = 0x7ffff7e65040 "}}\205\200xt{|\177w~ylwxrlqyrmifmnqlbyoymgmjzp|h{tpuyy{yywtvztypybi\177mg[b\\TUY`Pb]RYW[Z\\XZ]XVXSSY\\YWYYVZWTX]V]WRTQVSY\\VX[\\XS]ZX_UaV[^_ZX]`^`]e_]hf]obef^c_[gZa[b\\]_Vj]_g[ne[a]f_ceh^f[fddd`faia\\ea\\ffehfdcaeg_d_h\\i"..., 
  datastart = 0x7ffff7e65040 "}}\205\200xt{|\177w~ylwxrlqyrmifmnqlbyoymgmjzp|h{tpuyy{yywtvztypybi\177mg[b\\TUY`Pb]RYW[Z\\XZ]XVXSSY\\YWYYVZWTX]V]WRTQVSY\\VX[\\XS]ZX_UaV[^_ZX]`^`]e_]hf]obef^c_[gZa[b\\]_Vj]_g[ne[a]f_ceh^f[fddd`faia\\ea\\ffehfdcaeg_d_h\\i"..., dataend = 0x7ffff7ea5040 "", datalimit = 0x7ffff7ea5040 "", allocator = 0x0, u = 0x73dba0, size = {p = 0x7fffffffd408}, step = {p = 0x7fffffffd450, buf = {512, 1}}}
(gdb) print rgbchannel[1]
$12 = {flags = 1124024320, dims = 2, rows = 512, cols = 512, 
  data = 0x7ffff7e24040 "\211\211\211\210\212\201\212\206\214\210\207\206\202\213\207\201\206\203\212\213\177\204\201\206\203\205\202}\202\201\177\203\177\206\200\207\205\220\207\216\217\216\217\226\224\232\231\225\225\225\217\232\221\216\207\205vx\177nbRVJ?A?=7CHBIEIJIIFNPLKIHPPNMKKLNNKNNONNIKIKOOYMQVZVNYY\\_\\Z\\[ea\\Y^hde`hb`bd_jlfgdd`ch[b_iebeafaaihig_faiffggcg`jfcfejdhgefdddhlghchcijeiiiem"..., 
  datastart = 0x7ffff7e24040 "\211\211\211\210\212\201\212\206\214\210\207\206\202\213\207\201\206\203\212\213\177\204\201\206\203\205\202}\202\201\177\203\177\206\200\207\205\220\207\216\217\216\217\226\224\232\231\225\225\225\217\232\221\216\207\205vx\177nbRVJ?A?=7CHBIEIJIIFNPLKIHPPNMKKLNNKNNONNIKIKOOYMQVZVNYY\\_\\Z\\[ea\\Y^hde`hb`bd_jlfgdd`ch[b_iebeafaaihig_faiffggcg`jfcfejdhgefdddhlghchcijeiiiem"..., dataend = 0x7ffff7e64040 "", datalimit = 0x7ffff7e64040 "", 
  allocator = 0x0, u = 0x73d790, size = {p = 0x7fffffffd468}, step = {p = 0x7fffffffd4b0, buf = {512, 1}}}
(gdb) print rgbchannel[1][30]
No symbol "operator[]" in current context.
(gdb) print image
$13 = {flags = 1124024336, dims = 2, rows = 512, cols = 512, 
  data = 0x7fffbc390040 "}\211\342}\211ⅉ߀\210\337x\212\342t\201\342{\212\344|\206\343\177\214\343w\210\341~\207\344y\206\341l\202\337w\213\342x\207\337r\201\335l\206\335q\203\335y\212\336r\213\336m\177\337i\204\337f\201\340m\206\335n\203\332q\205\335l\202\337b}\341y\202\335o\201\335y\177\334m\203\337g\177\341m\206\337j\200\342z\207\337p\205\341|\220\343h\207\345{\216\347t\217\347p\216\350u\217\346y\226\351y\224\352{\232\355y\231\351y\225\347w\225\355t\225\356v\217\352z\232\353t\221\352y\216\350p\207\351y\205\346bv\343ix\335\177\177\333mn\325gb\313[R\312bV\273\\J\256T?\251UA\246Y?"..., 
  datastart = 0x7fffbc390040 "}\211\342}\211ⅉ߀\210\337x\212\342t\201\342{\212\344|\206\343\177\214\343w\210\341~\207\344y\206\341l\202\337w\213\342x\207\337r\201\335l\206\335q\203\335y\212\336r\213\336m\177\337i\204\337f\201\340m\206\335n\203\332q\205\335l\202\337b}\341y\202\335o\201\335y\177\334m\203\337g\177\341m\206\337j\200\342z\207\337p\205\341|\220\343h\207\345{\216\347t\217\347p\216\350u\217\346y\226\351y\224\352{\232\355y\231\351y\225\347w\225\355t\225\356v\217\352z\232\353t\221\352y\216\350p\207\351y\205\346bv\343ix\335\177\177\333mn\325gb\313[R\312bV\273\\J\256T?\251UA\246Y?"..., dataend = 0x7fffbc450040 "", datalimit = 0x7fffbc450040 "", allocator = 0x0, u = 0x66fd10, size = {
    p = 0x7fffffffd348}, step = {p = 0x7fffffffd390, buf = {1536, 3}}}
(gdb) print tologic[8].R
$14 = 0 '\000'
(gdb) print tologic[0].R
$15 = 125 '}'
(gdb) print tologic[0].G
$16 = 0 '\000'
(gdb) print tologic[0].B
$17 = 0 '\000'
(gdb) print tologic[5].B
$18 = 0 '\000'
(gdb) print tologic[5].G
$19 = 0 '\000'
(gdb)
Last edited on
Topic archived. No new replies allowed.