Commit 2d179ada authored by Peter Billam's avatar Peter Billam

1.1 allows output on - for piping into display

parent 08de10a8
......@@ -5,20 +5,35 @@
-- This script is free software; you can redistribute it and/or --
-- modify it under the same terms as Lua5 itself. --
---------------------------------------------------------------------
local Version = '1.0 for Lua5'
local VersionDate = '11apr2018';
local Version = '1.1 for Lua5'
local VersionDate = '27jun2018';
local Synopsis = [[
fen2img [-w 350] \
'rnb1k1nr/pppp1ppp/8/4p3/3b3q/4PPP1/PPP2K1P/RNBQ1BNR b KQkq - 0 5' \
output.png
# default width is 400 pixels
# output file can be a .jpg or .png or .eps
perldoc fen2img # read the manual :-)
]]
local FEN = require 'chess.fen'
Width = 400;
local function which(s)
local f
for i,d in ipairs(split(os.getenv('PATH'), ':')) do
f=d..'/'..s; if P.access(f, 'x') then return f end
end
end
function warn(...)
local a = {}
for k,v in pairs{...} do table.insert(a, tostring(v)) end
io.stderr:write(table.concat(a),'\n') ; io.stderr:flush()
end
function die(...) warn(...); os.exit(1) end
function round(x) return math.floor(x+0.5) end
local iarg=1; while arg[iarg] ~= nil do
if not string.find(arg[iarg], '^-[a-z]') then break end
local first_letter = string.sub(arg[iarg],2,2)
......@@ -26,6 +41,10 @@ local iarg=1; while arg[iarg] ~= nil do
local n = string.gsub(arg[0],"^.*/","",1)
print(n.." version "..Version.." "..VersionDate)
os.exit(0)
-- elseif first_letter == 'd' then
-- iarg = iarg + 1
-- Display = which('display')
-- if not Display then die("-d given but can't find display in $PATH") end
elseif first_letter == 'w' then
iarg = iarg + 1
Width = tonumber( arg[iarg] )
......@@ -37,14 +56,6 @@ local iarg=1; while arg[iarg] ~= nil do
iarg = iarg+1
end
function round(x) return math.floor(x+0.5) end
function warn(...)
local a = {}
for k,v in pairs{...} do table.insert(a, tostring(v)) end
io.stderr:write(table.concat(a),'\n') ; io.stderr:flush()
end
function die(...) warn(...); os.exit(1) end
Height = round(Width * 0.95)
Dx = round(Width * 0.1)
......@@ -52,11 +63,18 @@ Fenstr = arg[iarg]
if not Fenstr then die('must specify FEN string') end
Output = arg[iarg+1]
if not Output then die('must specify output file') end
-- 20180627 must be able to pipe into display, so if no output file
-- or output file = "-" then use .pgn and write to stdout ?
-- Or: offer a -d option which pipes to display self-contained ?
-- https://www.ghostscript.com/doc/9.21/Devices.htm#PNG
ext2dev = { jpg='jpeg', png='png16m', JPG='jpeg', PNG='png16m', eps='eps' }
extension = string.match(Output,'%.(%a%a%a)$')
if not extension then die('unrecognised extension in file ',Output) end
Device = ext2dev[extension]
if Output == '-' then
Device = 'png16m'
else
ext2dev = { jpg='jpeg', png='png16m', JPG='jpeg', PNG='png16m', eps='eps' }
extension = string.match(Output,'%.(%a%a%a)$')
if not extension then die('unrecognised extension in file ',Output) end
Device = ext2dev[extension]
end
if not Device then die('unrecognised extension ',extension) end
-- print(Fenstr) ; print(Fenstr) ; print(Output)
-- print(extension) ; print(Device) ; os.exit()
......@@ -363,7 +381,7 @@ end
-- where dev is either jpeg or png16m or (if transparent) pgnalpha
local gs_cmd = 'gs -sDEVICE='..Device..' -sOutputFile='..Output..
' -q -g'..tostring(Width)..'x'..tostring(Height)..' -r72 -'
print(gs_cmd)
if Output ~= '-' then print(gs_cmd) end
local GS = assert(io.popen(gs_cmd, 'w'))
GS:write(table.concat(PS_sections, ''))
GS:close()
......@@ -387,6 +405,7 @@ fen2img - Creates a .png or .jpg diagram of a chess position in FEN
fen2img -w 500 "$FEN" /tmp/t.jpg
fen2img -w 500 "$FEN" /tmp/t.png
fen2img -w 500 "$FEN" /tmp/t.eps
fen2img -w 500 "$FEN" - | display - # pipes into display as a .png
=head1 DESCRIPTION
......@@ -416,12 +435,12 @@ Prints the Version
I<fen2img> at is available in
git clone https://github.com/peterbillam/postscriptlib
git clone https://gitlab.com/peterbillam/postscriptlib
or at
https://github.com/peterbillam/postscriptlib
https://github.com/peterbillam/postscriptlib/tree/master/bin
https://gitlab.com/peterbillam/postscriptlib
https://gitlab.com/peterbillam/postscriptlib/tree/master/bin
http://www.pjb.com.au/comp/free/fen2img
It also needs I<gs> to be installed, if it isn't already:
......@@ -437,6 +456,11 @@ It also needs a particular TrueType font:
which unzips to C<CHEQ_TT.TTF>
which you move into C</usr/share/fonts/truetype/>
=head1 CHANGES
20180627 1.1 allows output on stdout, for piping into display
20180411 1.0 first working version
=head1 AUTHOR
Peter J Billam, http://www.pjb.com.au/comp/contact.html
......
......@@ -23,9 +23,13 @@ CONTENT="PostScript, chess, FEN, pgn, jpg">
<B><a href="#OPTIONS">OPTIONS</a></B>
</LI></UL>
</TD><TD ALIGN="left">
<UL><LI>
<UL><LI>
<B><a href="#DOWNLOAD">DOWNLOAD</a></B>
</LI><LI>
<B><a href="#CHANGES">CHANGES</a></B>
</LI></UL>
</TD><TD ALIGN="left">
<UL><LI>
<B><a href="#AUTHOR">AUTHOR</a></B>
</LI><LI>
<B><a href="#SEE-ALSO">SEE ALSO</a></B>
......@@ -45,9 +49,10 @@ CONTENT="PostScript, chess, FEN, pgn, jpg">
display /tmp/hammerschlag.png
FEN=`pgn2fen '1.f3 e5 2. Kf2 Bc5+ 3.d4 Qh4+ 4. g3 Bxd4+ 5.e3'`
fen2img -w 500 &quot;$FEN&quot; /tmp/t.jpg
fen2img -w 500 &quot;$FEN&quot; /tmp/t.png
fen2img -w 500 &quot;$FEN&quot; /tmp/t.eps</code></pre>
fen2img -w 500 "$FEN" /tmp/t.jpg
fen2img -w 500 "$FEN" /tmp/t.png
fen2img -w 500 "$FEN" /tmp/t.eps
fen2img -w 500 "$FEN" - | display - # pipes a .png into display</code></pre>
<H3 id="DESCRIPTION">DESCRIPTION</H3>
......@@ -93,6 +98,11 @@ It also needs a particular TrueType font:</p>
<code>/usr/share/fonts/truetype/</code>
</p>
<H3 id="CHANGES">CHANGES</H3>
<PRE> 20180627 1.1 allows output on stdout, for piping into display
20180411 1.0 first working version
</PRE>
<H3 id="AUTHOR">AUTHOR</H3>
<p>Peter J Billam, http://www.pjb.com.au/comp/contact.html</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment