summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2020-07-16 01:28:34 +0100
committerAlastair Poole <netstar@gmail.com>2020-07-16 01:28:34 +0100
commitfcde89b02b938b719ff973a55cab295653576e7a (patch)
tree1a8ecd74bdee4d6052deecbbe77d8f24c91ec0c7
parente002da570ebde6c79da4c8fd1387b537f0e35749 (diff)
make this work reliablyish...
signage!!! Also work with whatever res the camera supports, try 1080p....
-rw-r--r--cam.c98
-rw-r--r--makefile2
2 files changed, 63 insertions, 37 deletions
diff --git a/cam.c b/cam.c
index d09eb8d..d67efe1 100644
--- a/cam.c
+++ b/cam.c
@@ -17,7 +17,7 @@
#include <Evas.h>
#include <Ecore_Evas.h>
-#define RGB_VALID(x) ((x) < 0)? 0 :(((x)>255)? 255: (x))
+#define RGB_VALID(x) ((x) < 0) ? 0 :(((x) > 255) ? 255: (x))
static void
borked(const char *why)
@@ -27,33 +27,62 @@ borked(const char *why)
}
static void
-yuvtorgba(int Y, int U, int V, char *rgb)
+yuvtorgb(int Y, int U, int V, unsigned char *rgb)
{
- int r, g, b;
+ int r, g, b;
+ static short L1[256], L2[256], L3[256], L4[256], L5[256];
+ static int initialised;
+
+ if (!initialised) {
+ initialised=1;
+ for (int i = 0; i < 256 ; i++) {
+ L1[i] = 1.164*(i-16);
+ L2[i] = 1.596*(i-128);
+ L3[i] = -0.813*(i-128);
+ L4[i] = 2.018*(i-128);
+ L5[i] = -0.391*(i-128);
+ }
+ }
+
+ r = L1[Y] + L2[V];
+ g = L1[Y] + L3[U] + L5[V];
+ b = L1[Y] + L4[U];
+
+ rgb[0] = RGB_VALID(b);
+ rgb[1] = RGB_VALID(g);
+ rgb[2] = RGB_VALID(r);
+ rgb[3] = 255;
+}
+
+static void
+YUV444toBGRA(uint8_t Y, uint8_t U, uint8_t V, uint8_t *rgb)
+{
+ int r, g, b;
+
+ r = Y + 1.402 * (V - 128);
+ g = Y - 0.344 * (U - 128) - (0.714 * (V - 128));
+ b = Y + 1.772 * (U - 128);
- r = 1.164*(Y-16) + 1.596*(V-128);
- g = 1.164*(Y-16) + (-0.813*(U-128)) + (-0.391*(V-128));
- b = 1.164*(Y-16) + 2.018*(U-128);
rgb[0] = RGB_VALID(b);
- rgb[1] = RGB_VALID(g);
- rgb[2] = RGB_VALID(r);
- rgb[3] = 0;
+ rgb[1] = RGB_VALID(g);
+ rgb[2] = RGB_VALID(r);
+ rgb[3] = 255;
}
void
-yuv_convert(char *buf, char *rgb, int x, int y)
+yuv_convert(unsigned char *buf, char *rgb, unsigned int w, unsigned int h)
{
- for (int i = 0; i < x * y; i += 2) {
- int Y1, Y2, U, V;
+ for (int i = 0; i < w * h; i += 2) {
+ uint8_t Y1, Y2, U, V;
- Y1 = buf[2*i+0];
- Y2 = buf[2*i+2];
- U = buf[2*i+1];
- V = buf[2*i+3];
+ Y1 = buf[2*i+0];
+ Y2 = buf[2*i+2];
+ U = buf[2*i+1];
+ V = buf[2*i+3];
- yuvtorgba(Y1, U, V, &rgb[4*i]);
- yuvtorgba(Y2, U, V, &rgb[4*(i+1)]);
- }
+ YUV444toBGRA(Y1, U, V, &rgb[4*i]);
+ YUV444toBGRA(Y2, U, V, &rgb[4*(i+1)]);
+ }
}
static void
@@ -93,7 +122,7 @@ typedef struct {
} buffer_t;
static int
-mmap_mjpeg(int fd, int w, int h)
+mmap_camera(int fd, int w, int h)
{
fd_set fds;
struct timeval tv;
@@ -102,7 +131,7 @@ mmap_mjpeg(int fd, int w, int h)
int i, type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
memset(&req, 0, sizeof(req));
- req.count = 10;
+ req.count = 16;
req.type= V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
@@ -158,7 +187,7 @@ mmap_mjpeg(int fd, int w, int h)
int status = select(fd + 1, &fds, NULL, NULL, NULL);
if (status == -1 && errno == EINTR) continue;
- else if (status == 0)
+ else if (status == 0)
borked("timeout");
memset(&buf, 0, sizeof(buf));
@@ -204,7 +233,7 @@ mmap_mjpeg(int fd, int w, int h)
}
static int
-read_mjpeg(int fd, int w, int h)
+read_camera(int fd, int w, int h)
{
char *out, *data;
fd_set fds;
@@ -229,7 +258,7 @@ read_mjpeg(int fd, int w, int h)
status = select(fd + 1, &fds, NULL, NULL, &tv);
if (status == -1 && errno == EINTR) continue;
- else if (status == 0)
+ else if (status == 0)
borked("timeout");
if ((read(fd, data, (w * h) * 2)) == -1) {
@@ -249,14 +278,13 @@ read_mjpeg(int fd, int w, int h)
int main(void)
{
struct v4l2_capability cap;
- struct v4l2_cropcap cropcap;
struct v4l2_format format;
int fd, ret = 1;
uint32_t w, h;
ecore_evas_init();
- fd = open("/dev/video0", O_RDWR | O_NONBLOCK);
+ fd = open("/dev/video1", O_RDWR | O_NONBLOCK);
if (fd == -1) return 1;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
@@ -269,29 +297,27 @@ int main(void)
memset(&format, 0, sizeof(format));
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (ioctl(fd, VIDIOC_G_FMT, &format) == -1) {
- printf("%s\n", strerror(errno));
- goto cleanup;
- }
-
- w = format.fmt.pix.width = 1920;
- h = format.fmt.pix.height = 1080;
-
+ format.fmt.pix.width = 1920;
+ format.fmt.pix.height = 1080;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
if (ioctl(fd, VIDIOC_S_FMT, &format) == -1) {
printf("%s\n", strerror(errno));
goto cleanup;
}
+
if (ioctl(fd, VIDIOC_G_FMT, &format) == -1) {
printf("%s\n", strerror(errno));
goto cleanup;
}
+ w = format.fmt.pix.width;
+ h = format.fmt.pix.height;
+
switch (format.fmt.pix.pixelformat) {
case V4L2_PIX_FMT_YUYV:
- read_mjpeg(fd, w, h);
- //mmap_mjpeg(fd, w, h);
+ //read_camera(fd, w, h);
+ mmap_camera(fd, w, h);
break;
default:
break;
diff --git a/makefile b/makefile
index 0de8e8b..79328ab 100644
--- a/makefile
+++ b/makefile
@@ -3,6 +3,6 @@ FLAGS=`pkg-config --libs --cflags evas ecore-evas`
OUT=cam
default:
- $(CC) $(FLAGS) -O2 $(OUT).c -o $(OUT)
+ $(CC) $(FLAGS) -O0 -g -ggdb3 $(OUT).c -o $(OUT)
clean:
-rm $(OUT)