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 char *img = NULL;
static int w; static int w;
static int h; static int h;
static int f;
if (!img && msg->image) { if (!img && msg->image) {
img = msg->image; img = msg->image;
w = msg->width; w = msg->width;
h = msg->height; h = msg->height;
f = msg->frames;
} }
if (!img) { if (!img) {
printf("awaiting state ... %d\r", msg->timestamp); 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"); // printf("loop\n");
int rowoffset = (rows-h)/2; int rowoffset = (rows-h)/2;
//int coloffset = left-frame; //int coloffset = left-frame;
int imgoff = (frame % f) * w * h;
for (int y=0; y<h; y++) { // y<msg->height; y++) { for (int y=0; y<h; y++) { // y<msg->height; y++) {
for (int x=left-frame; x<cols; x++) { for (int x=left-frame; x<cols; x++) {
if (x<0) if (x<0)
continue; continue;
//mvaddch(y + rowoffset, x, ('0' + x-(left-frame)+y)); //mvaddch(y + rowoffset, x, ('0' + x-(left-frame)+y));
int p = x-(left-frame); 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 #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"); printf("fopen\n");
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
@ -15,30 +15,50 @@ char *readImage(const char* filename, int *cols, int *rows) {
*cols = 0; *cols = 0;
*rows = 0; *rows = 0;
int maxrows = 0;
*imgz = 1;
char *linebuf = (char *)malloc(LINELEN); char *linebuf = (char *)malloc(LINELEN);
char *ret = NULL; char *ret = NULL;
if (f) { if (f) {
while (fgets(linebuf, LINELEN, f)) { while (fgets(linebuf, LINELEN, f)) {
int len = strlen(linebuf); int len = strlen(linebuf);
if ( len > 2 && linebuf[0] == '.' && linebuf[1] == '.') {
(*imgz)++;
(*rows)=0;
}
*cols = len>*cols?len:*cols; *cols = len>*cols?len:*cols;
(*rows)++; (*rows)++;
maxrows = (*rows>maxrows)?*rows:maxrows;
} }
(*rows)=maxrows;
(*cols)--; (*cols)--;
ret = (char *)malloc(*cols * *rows + 1); ret = (char *)malloc(*imgz * *cols * *rows + 1);
memset(ret, ' ', *cols * *rows); memset(ret, ' ', *imgz * *cols * *rows);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
for (int r=0; r<*rows; r++) {
fgets(&ret[r * *cols], LINELEN, f); for (int fp=0; fp<(*imgz); fp++) {
for (int r=0; r<*rows; r++) {
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); fclose(f);
} }
free(linebuf); 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 y=0; y < *rows; y++) {
for (int x=0; x < *cols; x++) { for (int x=0; x < *cols; x++) {
//mvaddch(y,x,img[y * *cols +x]); //mvaddch(y,x,img[y * *cols +x]);
printf("%c", ret[y * *cols +x]); printf("%c", ret[y * *cols +x]);
@ -49,3 +69,4 @@ char *readImage(const char* filename, int *cols, int *rows) {
return ret; return ret;
} }

View File

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

View File

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

View File

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

View File

@ -12,10 +12,9 @@ struct message {
uint32_t timestamp; uint32_t timestamp;
uint32_t width; // normally 80 uint32_t width; // normally 80
uint32_t height; // normally 25, may vary 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
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]; * image[row][col] >>> image[row*width+col];
*/ */

View File

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

View File

@ -6,7 +6,7 @@
#include "msg.h" // message serialization #include "msg.h" // message serialization
#include "display.h" // callback #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 *)); int run_client(const struct prog_info *pinfo,void(*framecallback)(const struct message *, const struct prog_info *));
#endif #endif