From 4df74982b149eb0614ca6a3d7789b2eb868ca699 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Mon, 25 Feb 2013 20:36:44 +0330 Subject: [PATCH] support ppm files --- Makefile | 2 +- fbvis.c | 32 +++++++++++++------------------- ppm.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 ppm.c diff --git a/Makefile b/Makefile index d32321d..474f902 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = -lm all: fbvis %.o: %.c $(CC) -c $(CFLAGS) $< -fbvis: fbvis.o draw.o stb_image.o lodepng.o +fbvis: fbvis.o draw.o ppm.o stb_image.o lodepng.o $(CC) -o $@ $^ $(LDFLAGS) clean: rm -f *.o fbvis diff --git a/fbvis.c b/fbvis.c index 07083cb..5131326 100644 --- a/fbvis.c +++ b/fbvis.c @@ -24,8 +24,9 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define REGION(a, b, x) (MIN(b, MAX(x, a))) -static int cols, rows, ch; -static char *buf; +static int cols, rows; +static int ch; +static unsigned char *buf; static int head, left; static int count; static struct termios termios; @@ -46,7 +47,7 @@ static void draw(void) int i, j; for (i = rs; i < re; i++) { for (j = cs; j < ce; j++) { - unsigned char *src = (void *) (buf + (i * cols + j) * ch); + unsigned char *src = buf + (i * cols + j) * ch; unsigned int *dst = (void *) (row + (j - cs) * bpp); *dst = FB_VAL(src[0], src[1], src[2]); } @@ -67,7 +68,7 @@ static void drawfs(void) memset(row, 0, fb_cols() * bpp); for (j = 0; j < fb_cols() && r < rows; j++) { int c = j * cols / fb_cols(); - unsigned char *src = (void *) (buf + (r * cols + c) * ch); + unsigned char *src = buf + (r * cols + c) * ch; unsigned int *dst = (void *) (row + j * bpp); *dst = FB_VAL(src[0], src[1], src[2]); } @@ -76,25 +77,18 @@ static void drawfs(void) } unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp); - -static char *loadstbi(char *path, int *h, int *w, int *ch) -{ - return (void *) stbi_load(path, w, h, ch, 0); -} - -static char *loadlode(char *path, int *h, int *w, int *ch) -{ - char *s = NULL; - *ch = 4; - lodepng_decode32_file((void *) &s, (void *) w, (void *) h, path); - return s; -} +char *ppm_load(char *path, int *h, int *w); static int loadfile(char *path) { - buf = loadlode(path, &rows, &cols, &ch); + ch = 4; + lodepng_decode32_file(&buf, (void *) &cols, (void *) &rows, path); if (!buf) - buf = loadstbi(path, &rows, &cols, &ch); + buf = stbi_load(path, &cols, &rows, &ch, 0); + if (!buf) { + ch = 3; + buf = (void *) ppm_load(path, &rows, &cols); + } return !buf; } diff --git a/ppm.c b/ppm.c new file mode 100644 index 0000000..c9d8840 --- /dev/null +++ b/ppm.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static int nextchar(int fd) +{ + unsigned char b[4] = {0}; + read(fd, b, 1); + return b[0]; +} + +static int cutint(int fd) +{ + int c; + int n = 0; + do { + c = nextchar(fd); + } while (isspace(c)); + while (isdigit(c)) { + n = n * 10 + c - '0'; + c = nextchar(fd); + } + return n; +} + +char *ppm_load(char *path, int *h, int *w) +{ + char *d; + int fd = open(path, O_RDONLY); + if (fd < 0 || nextchar(fd) != 'P' || nextchar(fd) != '6') + return NULL; + *w = cutint(fd); /* image width */ + *h = cutint(fd); /* image height */ + cutint(fd); /* max color val */ + + d = malloc(*h * *w * 3); + read(fd, d, *h * *w * 3); + close(fd); + return d; +} + +void ppm_save(char *path, char *s, int h, int w) +{ + char sig[128]; + int fd; + sprintf(sig, "P6\n%d %d\n255\n", w, h); + fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600); + write(fd, sig, strlen(sig)); + write(fd, s, h * w * 3); + close(fd); +} -- 2.11.4.GIT