Re: [dev] Looking for simple, alpha supporting image format

From: Evan Gates <>
Date: Thu, 17 Jul 2014 16:20:10 -0700

> Anyway, here's a viewer script in case anybody wants it. :-)

A few notes on the script.


I recommend against using the .sh extension. This is a new command you
are going to run. You don't type ls.elf why would you type
What if at some point in the future you want to replace it with
something that isn't written in sh? Then anything that calls it is
screwed or you have to keep an inaccurate extension.

> hdr=`head -c 30`

Will not work if the "any byte" is a null byte. Shell variables are C
strings and would end at a null byte. (and stylistically I highly
recommend $() over `` as it's easier to nest and IMO read). I also
think it's worth noting that head -c is not portable (well, not
POSIX). I recommend dd.

hdr=$(dd bs=1 count=30 2>/dev/null)

Moving on, I'll just assume that the "any byte" isn't null.

> sig=`echo "$hdr" | cut -b 1-9`

I recommend using printf instead of echo when the contents of a
variable are unknown. And then you can just use a format modifier and
a parameter expansion instead of cut.

sig=$(printf %.9s "$hdr"); hdr=${hdr#"$sig"?}
w=$(printf %.9s "$hdr"); hdr=${hdr#"$w"?}
h=$(printf %.9s "$hdr");

> if [ "x$sig = "ximageRGBA" ]; then

If you can assume at least a POSIX compliant test, then you don't need
the extra x [0]. It is however necessary for some legacy shells.

if [ "$sig" = imageRGBA ]; then

> echo "bad image signature" > /dev/stdderr

I think that >&2 is more portable.

> if [ "x$1" = "x" ]; then

Once again if you don't have to support legacy systems can be written as

if ! [ "$1" ]; then

or to invert the case

if [ "$1" ]; then view < "$1"; else view; fi

I've attached a version that works with the waterfall.image from
earlier in the thread. (imgRGBA signature and 7 bytes for width and
height). It also:
1) is POSIX compliant
2) works with null bytes separating the sig, width, and height
3) will run display serially on all non null arguments provided



Received on Fri Jul 18 2014 - 01:20:10 CEST

This archive was generated by hypermail 2.3.0 : Fri Jul 18 2014 - 01:24:07 CEST