Re: [hackers] [sent] Support farbfeld as an intermediate format || sin

2015-12-08 Thread Markus Teich
Dimitris Papastamos wrote:
> > +   for (bin = NULL, i = 0; i < LEN(filters); i++) {
> > +   if (regcomp(®ex, filters[i].regex,
> > +   REG_NOSUB | REG_EXTENDED | REG_ICASE))
> > +   continue;
> > +   if (!regexec(®ex, filename, 0, NULL, 0)) {
> > +   bin = filters[i].bin;
> > +   break;
> > +   }
> > +   }
> 
> I forgot an if (!bin) return NULL sort of thing here.

Heyho Dimitris,

thanks, fixed.

--Markus



Re: [hackers] [sent] Support farbfeld as an intermediate format || sin

2015-12-08 Thread Dimitris Papastamos
> +Image *ffopen(char *filename)
>  {
> - FILE *f;
> - unsigned char buf[8];
> + unsigned char hdr[16];
> + char *bin;
> + regex_t regex;
>   Image *img;
> + size_t i;
> + int tmpfd, fd;
> +
> + for (bin = NULL, i = 0; i < LEN(filters); i++) {
> + if (regcomp(®ex, filters[i].regex,
> + REG_NOSUB | REG_EXTENDED | REG_ICASE))
> + continue;
> + if (!regexec(®ex, filename, 0, NULL, 0)) {
> + bin = filters[i].bin;
> + break;
> + }
> + }

I forgot an if (!bin) return NULL sort of thing here.



[hackers] [sent] Support farbfeld as an intermediate format || sin

2015-12-08 Thread git
commit b516f468faa3a80c31932cbcb1ea8ccd1468ffc1
Author: sin 
AuthorDate: Wed Nov 18 10:41:02 2015 +
Commit: Markus Teich 
CommitDate: Tue Dec 8 20:01:36 2015 +0100

Support farbfeld as an intermediate format

Sent now uses farbfeld[0] as an intermediate format.  A series of
filters is specified in config.h that matches file extensions to
filter programs.  The programs will convert between formats such as
png to farbfeld.  Internally in sent we do not need to worry on how
to parse png or any other format.

This also works with jpg and gif and others.  The 2ff wrapper will
use imagemagick conversion tools.  This is temporary as jpg2ff and
gif2ff will also be implemented.

To make this work, you will have to clone[0] and put png2ff and 2ff
in your PATH.

[0] http://git.2f30.org/farbfeld/

diff --git a/LICENSE b/LICENSE
index d1da8fc..8d5665e 100644
--- a/LICENSE
+++ b/LICENSE
@@ -2,8 +2,6 @@ The MIT License (MIT)
 
 Copyright (c) 2014-2015 Markus Teich
 
-png handling stuff adapted from meh by John Hawthorn
-
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
diff --git a/README.md b/README.md
index 007564b..e213efb 100644
--- a/README.md
+++ b/README.md
@@ -33,7 +33,6 @@ presentation file could look like this:

depends on
- Xlib
-   - libpng

sent FILENAME
one slide per paragraph
diff --git a/config.def.h b/config.def.h
index 2dae620..94ed09e 100644
--- a/config.def.h
+++ b/config.def.h
@@ -45,3 +45,8 @@ static Shortcut shortcuts[] = {
{ XK_n,   advance,{.i = +1} },
{ XK_p,   advance,{.i = -1} },
 };
+
+static Filter filters[] = {
+   { "\\.png$",   "png2ff" },
+   { "\\.(jpg|gif)$", "2ff" },
+};
diff --git a/config.mk b/config.mk
index ed08199..52d5fb1 100644
--- a/config.mk
+++ b/config.mk
@@ -12,7 +12,7 @@ X11LIB = /usr/X11R6/lib
 
 # includes and libs
 INCS = -I. -I/usr/include -I/usr/include/freetype2 -I${X11INC}
-LIBS = -L/usr/lib -lc -lm -L${X11LIB} -lXft -lfontconfig -lX11 -lpng
+LIBS = -L/usr/lib -lc -lm -L${X11LIB} -lXft -lfontconfig -lX11
 
 # flags
 CPPFLAGS = -DVERSION=\"${VERSION}\" -D_XOPEN_SOURCE=600
diff --git a/example b/example
index 39e0206..d4b62d2 100644
--- a/example
+++ b/example
@@ -20,7 +20,6 @@ easy to use
 
 depends on
 ♽ Xlib
-☢ libpng
 
 ~1000 lines of code
 
diff --git a/sent.c b/sent.c
index e717a69..eb3aadc 100644
--- a/sent.c
+++ b/sent.c
@@ -1,12 +1,17 @@
 /* See LICENSE for licence details. */
+#include 
+#include 
+
 #include 
+#include 
 #include 
-#include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -36,13 +41,16 @@ typedef struct {
unsigned int bufwidth, bufheight;
imgstate state;
XImage *ximg;
-   FILE *f;
-   png_structp png_ptr;
-   png_infop info_ptr;
+   int fd;
int numpasses;
 } Image;
 
 typedef struct {
+   char *regex;
+   char *bin;
+} Filter;
+
+typedef struct {
unsigned int linecount;
char **lines;
Image *img;
@@ -79,12 +87,12 @@ typedef struct {
const Arg arg;
 } Shortcut;
 
-static Image *pngopen(char *filename);
-static void pngfree(Image *img);
-static int pngread(Image *img);
-static int pngprepare(Image *img);
-static void pngscale(Image *img);
-static void pngdraw(Image *img);
+static Image *ffopen(char *filename);
+static void fffree(Image *img);
+static int ffread(Image *img);
+static int ffprepare(Image *img);
+static void ffscale(Image *img);
+static void ffdraw(Image *img);
 
 static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
 static void cleanup();
@@ -128,56 +136,87 @@ static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
 };
 
-Image *pngopen(char *filename)
+int
+filter(int fd, const char *cmd)
+{
+   int fds[2];
+
+   if (pipe(fds) < 0)
+   eprintf("pipe:");
+
+   switch (fork()) {
+   case -1:
+   eprintf("fork:");
+   case 0:
+   dup2(fd, 0);
+   dup2(fds[1], 1);
+   close(fds[0]);
+   close(fds[1]);
+   execlp(cmd, cmd, (char *)0);
+   eprintf("execlp %s:", cmd);
+   }
+   close(fds[1]);
+   return fds[0];
+}
+
+Image *ffopen(char *filename)
 {
-   FILE *f;
-   unsigned char buf[8];
+   unsigned char hdr[16];
+   char *bin;
+   regex_t regex;
Image *img;
+   size_t i;
+   int tmpfd, fd;
+
+   for (bin = NULL, i = 0; i < LEN(filters); i++) {
+   if (regcomp(®ex, filters[i].regex,
+   REG_NOSUB | REG_EXTENDED | REG_ICASE))
+   continue;
+