aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2016-05-27 20:07:47 +0200
committerjaseg <code@jaseg.net>2016-05-27 20:07:47 +0200
commit4e5d910e5b7e51cd01447e909480f23b4fe0be75 (patch)
treea978f4581deff0086d4d9ddab557fb33ff3c8722
parent16f41a5298b70faaca92b190f67ffacbb1f2736c (diff)
downloadclippy-4e5d910e5b7e51cd01447e909480f23b4fe0be75.tar.gz
clippy-4e5d910e5b7e51cd01447e909480f23b4fe0be75.tar.bz2
clippy-4e5d910e5b7e51cd01447e909480f23b4fe0be75.zip
Added pixelflut client code
-rwxr-xr-xclippy.py12
-rw-r--r--pixelflut.c15
2 files changed, 20 insertions, 7 deletions
diff --git a/clippy.py b/clippy.py
index 37981f9..5d81252 100755
--- a/clippy.py
+++ b/clippy.py
@@ -68,18 +68,20 @@ class Pixelflut:
self.so = ctypes.CDLL('./pixelflut.so')
self.sock = None
- def sendframe(self, frame):
- np.copyto(self.dbuf, frame)
- cptr = self.dbuf.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
+ def sendframe(self, idx):
if self.sock is None:
while self.sock is None or self.sock < 0:
time.sleep(1)
self.sock = self.so.cct(self.host, self.port)
- if self.so.sendframe(self.sock, cptr, self.w, self.h, self.x, self.y):
+ if self.so.sendframe(self.sock, idx, self.w, self.h, self.x, self.y):
self.so.discct(self.sock)
+ self.sock = None
def encode_image(self, img):
- return np.array(resize_image(img, (self.w, self.h), blackbg=False)).reshape(self.w*self.h*4)
+ frame = np.array(resize_image(img, (self.w, self.h), blackbg=False)).reshape(self.w*self.h*4)
+ np.copyto(self.dbuf, frame)
+ cptr = self.dbuf.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
+ return self.so.store_image(cptr, self.w, self.h)
def weightedChoice(choices, default=None):
acc = 0
diff --git a/pixelflut.c b/pixelflut.c
index 15dcc55..60d76ef 100644
--- a/pixelflut.c
+++ b/pixelflut.c
@@ -9,6 +9,17 @@
#include <unistd.h>
#include <errno.h>
+static uint8_t *images[2048] = { 0 };
+static int image_count = 0;
+
+int store_image(const uint8_t *img, int w, int h) {
+ if (image_count >= sizeof(images)/sizeof(images[0]))
+ return -1;
+ images[image_count] = malloc(w*h*4);
+ memcpy(images[image_count], img, w*h*4);
+ return image_count++;
+}
+
#define PIXEL_FORMAT "PX %zd %zd %02x%02x%02x\n"
int cct(const char *target, int port) {
printf("Reconnecting %s:%d\n", target, port);
@@ -35,7 +46,7 @@ int cct(const char *target, int port) {
return sockfd;
}
-int sendframe(int fd, uint8_t *img, int w, int h, int ox, int oy) {
+int sendframe(int fd, int idx, int w, int h, int ox, int oy) {
static unsigned long fcnt=0;
printf("frame %lu %dx%d @pos %dx%d\n", fcnt++, w, h, ox, oy);
int fmtlen = snprintf(NULL, 0, PIXEL_FORMAT, (size_t)1000, (size_t)1000, 0xff, 0xff, 0xff);
@@ -47,7 +58,7 @@ int sendframe(int fd, uint8_t *img, int w, int h, int ox, int oy) {
char *p = out;
for (size_t x=0; x<w; x++) {
for (size_t y=0; y<h; y++) {
- uint8_t *px = img + (y*w + x)*4;
+ uint8_t *px = images[idx] + (y*w + x)*4;
uint8_t r = px[0], g = px[1], b = px[2], a = px[3];
if (a != 255)
continue;