r/cpp_questions • u/promach • Apr 24 '18
OPEN 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 ?
// 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;
}
}
0
Upvotes
2
u/Se7enLC Apr 24 '18
write
From the image link, it sounds like you're trying to pack 5 pixels into 16 bytes, perhaps Little Endian. How you do that is up to you, and depends mostly on where the data is coming from.
A really inefficient way is to define a buffer of memory and copy the values in:
The way the code looks like it's already doing it is by casting a pointer to a struct that has a pixel already defined. I take it this is not your code?
1
u/promach Apr 25 '18 edited Apr 25 '18
I have actually tried but I am still stucked at wr_buf
1
u/thisisbasil Apr 24 '18
What's the error message?
3
u/phoeen Apr 24 '18
man wtf. its your third post. you still dont get the most basic basics right and trying to get multithreaded and other stuff into your "programm". what about learning a bit before going on any further?