clean ffread and rename to ffload

- also remove unneeded memmove in load()
This commit is contained in:
Markus Teich 2016-06-30 16:42:34 +02:00
parent fd303ee9c1
commit 827f3de2e7

82
sent.c
View File

@ -32,9 +32,8 @@ char *argv0;
typedef enum { typedef enum {
NONE = 0, NONE = 0,
LOADED = 1, SCALED = 1,
SCALED = 2, DRAWN = 2
DRAWN = 4
} imgstate; } imgstate;
typedef struct { typedef struct {
@ -42,7 +41,6 @@ typedef struct {
unsigned int bufwidth, bufheight; unsigned int bufwidth, bufheight;
imgstate state; imgstate state;
XImage *ximg; XImage *ximg;
int fd;
int numpasses; int numpasses;
} Image; } Image;
@ -90,7 +88,7 @@ typedef struct {
} Shortcut; } Shortcut;
static void fffree(Image *img); static void fffree(Image *img);
static Image *ffread(char *filename); static void ffload(Slide *s);
static void ffprepare(Image *img); static void ffprepare(Image *img);
static void ffscale(Image *img); static void ffscale(Image *img);
static void ffdraw(Image *img); static void ffdraw(Image *img);
@ -167,8 +165,8 @@ fffree(Image *img)
free(img); free(img);
} }
Image * void
ffread(char *filename) ffload(Slide *s)
{ {
uint32_t y, x; uint32_t y, x;
uint16_t *row; uint16_t *row;
@ -177,9 +175,12 @@ ffread(char *filename)
ssize_t count; ssize_t count;
unsigned char hdr[16]; unsigned char hdr[16];
char *bin = NULL; char *bin = NULL;
char *filename;
regex_t regex; regex_t regex;
Image *img; int fdin, fdout;
int tmpfd, fd;
if (s->img || !(filename = s->embed) || !s->embed[0])
return; /* already done */
for (i = 0; i < LEN(filters); i++) { for (i = 0; i < LEN(filters); i++) {
if (regcomp(&regex, filters[i].regex, if (regcomp(&regex, filters[i].regex,
@ -191,33 +192,30 @@ ffread(char *filename)
} }
} }
if (!bin) if (!bin)
return NULL; die("sent: Unable to find matching filter for file %s", filename);
if ((fd = open(filename, O_RDONLY)) < 0) if ((fdin = open(filename, O_RDONLY)) < 0)
die("sent: Unable to open file %s:", filename); die("sent: Unable to open file %s:", filename);
tmpfd = fd; if ((fdout = filter(fdin, bin)) < 0)
fd = filter(fd, bin);
if (fd < 0)
die("sent: Unable to filter %s:", filename); die("sent: Unable to filter %s:", filename);
close(tmpfd); close(fdin);
if (read(fd, hdr, 16) != 16 || memcmp("farbfeld", hdr, 8)) if (read(fdout, hdr, 16) != 16 || memcmp("farbfeld", hdr, 8))
return NULL; die("sent: Unable to filter %s into a valid farbfeld file", filename);
img = calloc(1, sizeof(Image)); s->img = calloc(1, sizeof(Image));
img->fd = fd; s->img->bufwidth = ntohl(*(uint32_t *)&hdr[8]);
img->bufwidth = ntohl(*(uint32_t *)&hdr[8]); s->img->bufheight = ntohl(*(uint32_t *)&hdr[12]);
img->bufheight = ntohl(*(uint32_t *)&hdr[12]);
if (img->buf) if (s->img->buf)
free(img->buf); free(s->img->buf);
/* internally the image is stored in 888 format */ /* internally the image is stored in 888 format */
if (!(img->buf = malloc(3 * img->bufwidth * img->bufheight))) if (!(s->img->buf = malloc(3 * s->img->bufwidth * s->img->bufheight)))
die("sent: Unable to malloc buffer for image.\n"); die("sent: Unable to malloc buffer for image.\n");
/* scratch buffer to read row by row */ /* scratch buffer to read row by row */
rowlen = img->bufwidth * 2 * strlen("RGBA"); rowlen = s->img->bufwidth * 2 * strlen("RGBA");
row = malloc(rowlen); row = malloc(rowlen);
if (!row) if (!row)
die("sent: Unable to malloc buffer for image row.\n"); die("sent: Unable to malloc buffer for image row.\n");
@ -227,10 +225,10 @@ ffread(char *filename)
bg_g = (sc[ColBg].pixel >> 8) % 256; bg_g = (sc[ColBg].pixel >> 8) % 256;
bg_b = (sc[ColBg].pixel >> 0) % 256; bg_b = (sc[ColBg].pixel >> 0) % 256;
for (off = 0, y = 0; y < img->bufheight; y++) { for (off = 0, y = 0; y < s->img->bufheight; y++) {
nbytes = 0; nbytes = 0;
while (nbytes < rowlen) { while (nbytes < rowlen) {
count = read(img->fd, (char *)row + nbytes, rowlen - nbytes); count = read(fdout, (char *)row + nbytes, rowlen - nbytes);
if (count < 0) if (count < 0)
die("sent: Unable to read from pipe:"); die("sent: Unable to read from pipe:");
nbytes += count; nbytes += count;
@ -242,17 +240,14 @@ ffread(char *filename)
opac = ntohs(row[x + 3]) / 257; opac = ntohs(row[x + 3]) / 257;
/* blend opaque part of image data with window background color to /* blend opaque part of image data with window background color to
* emulate transparency */ * emulate transparency */
img->buf[off++] = (fg_r * opac + bg_r * (255 - opac)) / 255; s->img->buf[off++] = (fg_r * opac + bg_r * (255 - opac)) / 255;
img->buf[off++] = (fg_g * opac + bg_g * (255 - opac)) / 255; s->img->buf[off++] = (fg_g * opac + bg_g * (255 - opac)) / 255;
img->buf[off++] = (fg_b * opac + bg_b * (255 - opac)) / 255; s->img->buf[off++] = (fg_b * opac + bg_b * (255 - opac)) / 255;
} }
} }
free(row); free(row);
close(img->fd); close(fdout);
img->state |= LOADED;
return img;
} }
void void
@ -417,11 +412,9 @@ load(FILE *fp)
if (s->lines[s->linecount][blen-1] == '\n') if (s->lines[s->linecount][blen-1] == '\n')
s->lines[s->linecount][blen-1] = '\0'; s->lines[s->linecount][blen-1] = '\0';
/* only make image slide if first line of a slide starts with @ */ /* mark as image slide if first line of a slide starts with @ */
if (s->linecount == 0 && s->lines[0][0] == '@') { if (s->linecount == 0 && s->lines[0][0] == '@')
memmove(s->lines[0], &s->lines[0][1], blen); s->embed = &s->lines[0][1];
s->embed = s->lines[0];
}
if (s->lines[s->linecount][0] == '\\') if (s->lines[s->linecount][0] == '\\')
memmove(s->lines[s->linecount], &s->lines[s->linecount][1], blen); memmove(s->lines[s->linecount], &s->lines[s->linecount][1], blen);
@ -444,6 +437,10 @@ advance(const Arg *arg)
slides[idx].img->state &= ~(DRAWN | SCALED); slides[idx].img->state &= ~(DRAWN | SCALED);
idx = new_idx; idx = new_idx;
xdraw(); xdraw();
if (slidecount > idx + 1)
ffload(&slides[idx + 1]);
if (0 < idx)
ffload(&slides[idx - 1]);
} }
} }
@ -489,11 +486,7 @@ void
xdraw() xdraw()
{ {
unsigned int height, width, i; unsigned int height, width, i;
Image *im; Image *im = slides[idx].img;
if (!slides[idx].img && slides[idx].embed && slides[idx].embed[0])
slides[idx].img = ffread(slides[idx].embed);
im = slides[idx].img;
getfontsize(&slides[idx], &width, &height); getfontsize(&slides[idx], &width, &height);
XClearWindow(xw.dpy, xw.win); XClearWindow(xw.dpy, xw.win);
@ -567,6 +560,7 @@ xinit()
XSetWindowBackground(xw.dpy, xw.win, sc[ColBg].pixel); XSetWindowBackground(xw.dpy, xw.win, sc[ColBg].pixel);
xloadfonts(); xloadfonts();
ffload(&slides[0]);
XStringListToTextProperty(&argv0, 1, &prop); XStringListToTextProperty(&argv0, 1, &prop);
XSetWMName(xw.dpy, xw.win, &prop); XSetWMName(xw.dpy, xw.win, &prop);