did it - it's animated ;)

This commit is contained in:
tkarrass 2013-11-13 20:48:15 +01:00
parent 2d874fe9ba
commit b8fb1400b1
8 changed files with 47 additions and 21 deletions

View File

@ -65,10 +65,12 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) {
static char *img = NULL;
static int w;
static int h;
static int f;
if (!img && msg->image) {
img = msg->image;
w = msg->width;
h = msg->height;
f = msg->frames;
}
if (!img) {
printf("awaiting state ... %d\r", msg->timestamp);
@ -97,13 +99,14 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) {
// printf("loop\n");
int rowoffset = (rows-h)/2;
//int coloffset = left-frame;
int imgoff = (frame % f) * w * h;
for (int y=0; y<h; y++) { // y<msg->height; y++) {
for (int x=left-frame; x<cols; x++) {
if (x<0)
continue;
//mvaddch(y + rowoffset, x, ('0' + x-(left-frame)+y));
int p = x-(left-frame);
mvaddch(y + rowoffset, x, p>=w?' ':img[y*w+p]);
mvaddch(y + rowoffset, x, p>=w-1?' ':img[imgoff+(y*w+p)]);
}
}

View File

@ -7,7 +7,7 @@
#define LINELEN 20000
char *readImage(const char* filename, int *cols, int *rows) {
char *readImage(const char* filename, int *cols, int *rows, int *imgz) {
printf("fopen\n");
FILE *f = fopen(filename, "r");
@ -15,28 +15,48 @@ char *readImage(const char* filename, int *cols, int *rows) {
*cols = 0;
*rows = 0;
int maxrows = 0;
*imgz = 1;
char *linebuf = (char *)malloc(LINELEN);
char *ret = NULL;
if (f) {
while (fgets(linebuf, LINELEN, f)) {
int len = strlen(linebuf);
if ( len > 2 && linebuf[0] == '.' && linebuf[1] == '.') {
(*imgz)++;
(*rows)=0;
}
*cols = len>*cols?len:*cols;
(*rows)++;
maxrows = (*rows>maxrows)?*rows:maxrows;
}
(*rows)=maxrows;
(*cols)--;
ret = (char *)malloc(*cols * *rows + 1);
memset(ret, ' ', *cols * *rows);
ret = (char *)malloc(*imgz * *cols * *rows + 1);
memset(ret, ' ', *imgz * *cols * *rows);
fseek(f, 0, SEEK_SET);
for (int fp=0; fp<(*imgz); fp++) {
for (int r=0; r<*rows; r++) {
fgets(&ret[r * *cols], LINELEN, f);
int base = (fp * *rows * *cols) + r * *cols;
fgets(&ret[base], LINELEN, f);
if (ret[base] == '.' && ret[base+1] == '.') {
ret[base] = ' ';
ret[base+1] = ' ';
break;
}
}
}
for (int i=0; i < (*imgz * *cols * *rows); i++)
if (ret[i] == '\n' || ret[i] == '\r')
ret[i] = ' ';
fclose(f);
}
free(linebuf);
printf("image size: %d x %d\n", *cols, *rows);
printf("image size: %d x %d\n%d frames\n", *cols, *rows, *imgz);
for (int y=0; y < *rows; y++) {
for (int x=0; x < *cols; x++) {
@ -49,3 +69,4 @@ char *readImage(const char* filename, int *cols, int *rows) {
return ret;
}

View File

@ -1,6 +1,6 @@
#ifndef __IMAGE
#define __IMAGE
char *readImage(const char* filename, int *cols, int *rows);
char *readImage(const char* filename, int *cols, int *rows, int *imgz);
#endif

View File

@ -123,14 +123,14 @@ int main(int argc, char **argv) {
printf("no image given\n");
return -12;
}
int w, h;
char *image = readImage(prog_info.filename, &w, &h);
int w, h, i;
char *image = readImage(prog_info.filename, &w, &h, &i);
if (!image) {
printf("could not read image!\n");
return -13;
}
prog_info.width = prog_info.width>(w*2)?prog_info.width:(w*2);
ret = run_server(&prog_info, image, w, h);
ret = run_server(&prog_info, image, w, h, i);
} else {
printf("running in CLIENT mode, using client offset %d\n", prog_info.client_offset);
signal(SIGINT,&die);

View File

@ -6,24 +6,26 @@
#include "msg.h"
int getBufferSize(struct message *msg) {
return 3*sizeof(uint32_t) + msg->width * msg->height;
return 4*sizeof(uint32_t) + msg->width * msg->height * msg->frames;
}
void serialize (char *buf, struct message *msg) {
memcpy(&buf[0], &msg->timestamp, 4);
memcpy(&buf[4], &msg->width, 4);
memcpy(&buf[8], &msg->height, 4);
memcpy(&buf[12], &msg->frames, 4);
if (msg->width * msg->height)
memcpy(&buf[12], msg->image, msg->width * msg->height);
memcpy(&buf[16], msg->image, msg->frames * msg->width * msg->height);
}
void deserialize (struct message *msg, const char *buf) {
memcpy(&msg->timestamp, &buf[0], 4);
memcpy(&msg->width, &buf[4], 4);
memcpy(&msg->height, &buf[8], 4);
memcpy(&msg->frames, &buf[12], 4);
if (msg->width * msg->height) {
msg->image = (char*) malloc(msg->width * msg->height);
memcpy(msg->image, &buf[12], msg->width * msg->height);
msg->image = (char*) malloc(msg->width * msg->height * msg->frames);
memcpy(msg->image, &buf[16], msg->width * msg->height * msg->frames);
} else {
msg->image = NULL;
}

View File

@ -12,10 +12,9 @@ struct message {
uint32_t timestamp;
uint32_t width; // normally 80
uint32_t height; // normally 25, may vary
uint32_t frames; // number of frames
//char **image; // dimension is width x height
char *image; // dimension is width x height
//TODO:
// Make image anim capable
/*
* image[row][col] >>> image[row*width+col];
*/

View File

@ -37,7 +37,7 @@ static void *get_in_addr(struct sockaddr *sa) {
// server mode
// pumpt im for(;;) den status ins eth
//
int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
int run_server(const struct prog_info *pinfo, char *img, int w, int h, int frms) {
struct addrinfo hints, *servinfo, *p;
int ret;
int sockfd;
@ -91,6 +91,7 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
bool sendimg = (t % 100) == 0;
outmsg->width = sendimg ? w : 0;
outmsg->height = sendimg ? h : 0;
outmsg->frames = sendimg ? frms: 0;
outmsg->image = sendimg ? img : NULL;
int buflen = getBufferSize(outmsg);

View File

@ -6,7 +6,7 @@
#include "msg.h" // message serialization
#include "display.h" // callback
int run_server(const struct prog_info *pinfo, char *img, int w, int h);
int run_server(const struct prog_info *pinfo, char *img, int w, int h, int frms);
int run_client(const struct prog_info *pinfo,void(*framecallback)(const struct message *, const struct prog_info *));
#endif