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
|
// 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>
using namespace cv;
using namespace std;
#define N 512
struct packet{
uint8_t r,g,b;
};
int main(int argc, char *argv[]) {
int fdr, fdw, rc, donebytes;
char *buf;
pid_t pid;
struct packet *tologic, *fromlogic;
int i;
float a, da;
fdr = open("/dev/stdin", O_RDONLY);
fdw = open("/dev/stdout", O_WRONLY);
if ((fdr < 0) || (fdw < 0)) {
perror("Failed to open Xillybus device file(s)");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("Failed to fork()");
exit(1);
}
if (pid) {
close(fdr);
tologic = vector<uint8_t>(sizeof(struct packet) * N * N, 0); // lena.tiff is sized as 512*512*3
if (!tologic) {
fprintf(stderr, "Failed to allocate memory\n");
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;
}
namedWindow( "Original Image", CV_WINDOW_AUTOSIZE );
imshow( "Original Image", image );
Mat rgbchannel[3];
// The actual splitting.
split(image, rgbchannel);
namedWindow("Blue",CV_WINDOW_AUTOSIZE);
imshow("Red", rgbchannel[0]);
namedWindow("Green",CV_WINDOW_AUTOSIZE);
imshow("Green", rgbchannel[1]);
namedWindow("Red",CV_WINDOW_AUTOSIZE);
imshow("Blue", rgbchannel[2]);
tologic->r = *(rgbchannel[0].data);
tologic->g = *(rgbchannel[1].data);
tologic->b = *(rgbchannel[2].data);
buf = (char *) tologic;
donebytes = 0;
while (donebytes < sizeof(struct packet) * N) {
rc = write(fdw, buf + donebytes, sizeof(struct packet) * N - 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);
fromlogic = vector<packet>(sizeof(struct packet) * N * N);
if (!fromlogic) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
buf = (char *) fromlogic;
donebytes = 0;
while (donebytes < sizeof(struct packet) * N) {
rc = read(fdr, buf + donebytes, sizeof(struct packet) * N - 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 (i=0; i<N; i++)
printf("R:%d G:%d B:%d \n", fromlogic[0].r, fromlogic[0].g, fromlogic[0].b);
printf("R:%d G:%d B:%d \n", fromlogic[68499].r, fromlogic[68499].g, fromlogic[68499].b);
sleep(1); // Let debug output drain (if used)
close(fdr);
return 0;
}
}
|