Re: [hackers] [sent] [PATCH 2/2] replace farbfeld with libnetpbm

2015-12-10 Thread Grant Mathews
On Fri, Dec 11, 2015 at 01:30:52AM +0100, Markus Teich wrote:
> sent with farbfeld uses a separate process instead of linking to a
> library and is therefore more UNIXy and sucks less.

This patch still does all the file conversion in a separate process, and
sent still uses an external library regardless of whether this patch is
merged.

I'm a big fan of process-level code reuse, but no tool is right for
every job, and sometimes a library is the correct way to go.

Aside from those points: would rewriting the patch to not use libnetpbm,
but to use the Netpbm helpers instead, be acceptable?

- Grant



Re: [hackers] [sent] [PATCH 2/2] replace farbfeld with libnetpbm

2015-12-10 Thread Grant Mathews
On Thu, Dec 10, 2015 at 04:39:49PM +0100, FRIGN wrote:
> Netpbm is arguably almost more complex than BMP and not easy to handle.

It's literally the top result when I search for the phrase "simplest
image format". I've written PPM loaders by hand in projects too small
for a makefile.

> We could discuss it if it was widely used,

Again, it's the top result for simplest image format: people use it.
dcraw uses it, it's supported by every image viewer I've ever installed,
and it shows up in enough random places that I'd consider it pretty
standard.

farbfeld is used nowhere, and sets the bar for "widely used" quite low.

> but the main point is: __You need a library to handle it__ and it's
> not that much of a popular format to justify installing a library for
> it.

You don't actually need a library for it, I'm just really lazy and the
library handles all the hard parts for me.

If the library "requirement" is the only sticking point, it'd be pretty
easy to fix that.

> I don't know about you guys, but I don't have libpbm installed on my
> computer and even though ffmpeg for instance offers support, I might
> be having a hard time finding a format ffmpeg _doesn't_ support.

I don't have farbfeld installed on my computer, and ffmpeg doesn't even
support it.

> You've replaced the entire farbfeld parsing code in sent with the
> boilerplate "offered" by netpbm. Good job!

Yes, that is not a bad thing.

> A question for the diligent reader: Can you read in a netpbm file
> without first looking into the docs?

I, uh, what? Are you proposing a format that doesn't require
explanation? Without any sort of docs (or reverse engineering), *no*
format is readable. Though, to be fair, PAM comes close: the header is
pure ASCII, and fairly self-explanatory.


Really, I just want this tool to be usable. Requiring some ridiculously
obscure image format for no reason takes this from "something I might
want to use" to "something nobody is going to use".

- Grant



[hackers] [sent] [PATCH 1/2] filter via shell pipeline

2015-12-09 Thread Grant Mathews
Instead of requiring an executable, allow building arbitrary shell
pipelines to filter filetypes through.
---
 sent.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sent.c b/sent.c
index fc5e389..99361e8 100644
--- a/sent.c
+++ b/sent.c
@@ -152,8 +152,8 @@ filter(int fd, const char *cmd)
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
-   execlp(cmd, cmd, (char *)0);
-   eprintf("execlp %s:", cmd);
+   execlp("sh", "sh", "-c", cmd, (char *)0);
+   eprintf("execlp sh -c '%s':", cmd);
}
close(fds[1]);
return fds[0];
-- 
2.4.10




[hackers] [sent] [PATCH 2/2] replace farbfeld with libnetpbm

2015-12-09 Thread Grant Mathews
Since the PNM/PAM format already exist as a minimal intermediate
representation with a rich set of commandline tools to manipulate them,
use Netpbm to handle images.
---
 README.md|   8 ++---
 config.def.h |   5 +--
 config.mk|   2 +-
 example  |   2 +-
 sent.c   | 105 ++-
 5 files changed, 62 insertions(+), 60 deletions(-)

diff --git a/README.md b/README.md
index 3fda5ee..43ba423 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
 sent is a simple plaintext presentation tool.
 
 sent does not need latex, libreoffice or any other fancy file format, it uses
-plaintext files to describe the slides and can include images via farbfeld.
+plaintext files to describe the slides and can include images via libnetpbm.
 Every paragraph represents a slide in the presentation.
 
 The presentation is displayed in a simple X11 window. The content of each slide
@@ -11,8 +11,8 @@ worry about alignment. Instead you can really concentrate on 
the content.
 
 Dependencies
 
-You need Xlib to build sent and the farbfeld[0] tools installed to use images 
in
-your presentations.
+You need Xlib and libnetpbm to build sent and the Netpbm tools installed to use
+images in your presentations.
 
 Demo
 
@@ -53,5 +53,3 @@ Development
 
 sent is developed at http://tools.suckless.org/sent
 
-
-0: http://git.2f30.org/farbfeld/about/
diff --git a/config.def.h b/config.def.h
index 94ed09e..3a4f1d8 100644
--- a/config.def.h
+++ b/config.def.h
@@ -47,6 +47,7 @@ static Shortcut shortcuts[] = {
 };
 
 static Filter filters[] = {
-   { "\\.png$",   "png2ff" },
-   { "\\.(jpg|gif)$", "2ff" },
+   { "\\.png$",   "pngtopam -alphapam" },
+   { "\\.(jpg|gif)$", "anytopnm" },
+   { "\\.p[bgp]m$",   "cat" },
 };
diff --git a/config.mk b/config.mk
index 52d5fb1..78055bd 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
+LIBS = -L/usr/lib -lc -lm -L${X11LIB} -lXft -lfontconfig -lX11 -lnetpbm
 
 # flags
 CPPFLAGS = -DVERSION=\"${VERSION}\" -D_XOPEN_SOURCE=600
diff --git a/example b/example
index 8b385f0..f7f6f61 100644
--- a/example
+++ b/example
@@ -20,7 +20,7 @@ easy to use
 
 depends on
 ♽ Xlib
-☃ farbfeld
+☃ libnetpbm
 
 ~1000 lines of code
 
diff --git a/sent.c b/sent.c
index 99361e8..978e3ef 100644
--- a/sent.c
+++ b/sent.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -41,7 +42,7 @@ typedef struct {
unsigned int bufwidth, bufheight;
imgstate state;
XImage *ximg;
-   int fd;
+   struct pam pamimg;
int numpasses;
 } Image;
 
@@ -87,12 +88,12 @@ typedef struct {
const Arg arg;
 } Shortcut;
 
-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 Image *pamopen(char *filename);
+static void pamfree(Image *img);
+static int pamread(Image *img);
+static int pamprepare(Image *img);
+static void pamscale(Image *img);
+static void pamdraw(Image *img);
 
 static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
 static void cleanup();
@@ -159,9 +160,9 @@ filter(int fd, const char *cmd)
return fds[0];
 }
 
-Image *ffopen(char *filename)
+Image *pamopen(char *filename)
 {
-   unsigned char hdr[16];
+   struct pam inpam;
char *bin = NULL;
regex_t regex;
Image *img;
@@ -192,21 +193,17 @@ Image *ffopen(char *filename)
eprintf("Unable to filter %s:", filename);
close(tmpfd);
 
-   if (read(fd, hdr, 16) != 16)
-   return NULL;
-
-   if (memcmp("farbfeld", hdr, 8))
-   return NULL;
+   pnm_readpaminit(fdopen(fd, "r"), , PAM_STRUCT_SIZE(tuple_type));
 
img = calloc(1, sizeof(Image));
-   img->fd = fd;
-   img->bufwidth = ntohl(*(uint32_t *)[8]);
-   img->bufheight = ntohl(*(uint32_t *)[12]);
+   img->pamimg = inpam;
+   img->bufwidth = inpam.width;
+   img->bufheight = inpam.height;
 
return img;
 }
 
-void fffree(Image *img)
+void pamfree(Image *img)
 {
free(img->buf);
if (img->ximg)
@@ -214,14 +211,13 @@ void fffree(Image *img)
free(img);
 }
 
-int ffread(Image *img)
+int pamread(Image *img)
 {
-   uint32_t y, x;
-   uint16_t *row;
+   uint32_t y, x, depth;
uint8_t opac;
uint8_t fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
-   size_t rowlen, off, nbytes;
-   ssize_t count;
+   tuple *pamrow;
+   size_t off;
 
if (!img)
return 0;
@@ -236,9 +232,8 @@ int ffread(Image *img)
return 0;
 
/* scratch buffer to read row by row */
-   rowlen = img->bufwidth * 2 *