--
-- lua5.1 toDPI.lua
--
-- doug rice copyright September 2011
--
-- description  - produces DPI pictures
-- pictures in img_ip
-- pictures output to
--    ./img_op
-- and
--    ./img_op/sm
--
--
require "gd"
--
-- Sec 1.0 Functions
--
-- capture dir /b *.jpg and pasted into string below.
picsStr=""
--
-- paste three images side by side in border
--
function prepareDPI( text, size, ang, filename, offset, w, h )
  im = ""
  im = 1
  im = nil
  im2 = ""
  im2 = 1
  im2 = nil
  im_org = ""
  im_org = 1
  im_org = nil
  print( text, size, ang, bcolor, filename,offset )
  im_org = gd.createFromJpeg( picsA[ offset+1 ] )
  border	= 5
  wantHeight 	= 1050
  wantWidth 	= 1400
  --wantHeight 	= 768
  --wantWidth 	= 1024
--  wantHeight 	= 1024
 -- wantWidth 	= 1280
  wantHeight2 = wantHeight/4
  wantWidth2	= wantWidth/4
  wantHeight2 = 768
  wantWidth2	= 1028
  wantHeight2 = 300
  wantWidth2	= 400
--[==[
  wantHeight 	= 150
  wantWidth 	= 100
  wantHeight2 = 38
  wantWidth2	= 25
]==]
  imgX,imgY = im_org:sizeXY()
  im	= gd.createTrueColor( wantWidth   , wantHeight   )
  im2 	= gd.createTrueColor( wantWidth2   , wantHeight2   )
  color   	= im:colorAllocateAlpha( 255, 255,   32, 64)
  bcolor 	= im:colorAllocateAlpha( 255, 255,   32, 0 )
  color   	= im:colorAllocateAlpha( 0, 0,  0, 64)
  color2   	= im:colorAllocateAlpha( 255, 255,  255, 64)
  bcolor 	= im:colorAllocateAlpha( 55, 55,  55, 0 )
  white  	= im:colorAllocateAlpha( 255, 255, 255, 0 )
  black  	= im:colorAllocateAlpha( 0,0,0,0 )
  bcolor 	= im:colorAllocateAlpha( 255, 255,  255, 0 )
  im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor )
  im2:filledRectangle( 0,0, wantWidth2-1, wantHeight2-1 , bcolor )
  im:rectangle( 0,0, wantWidth-1, wantHeight-1 , black )
  im2:rectangle( 0,0, wantWidth2-1, wantHeight2-1 , black )
  -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH)
  im:copyResampled(im_org, border,border,   0,0,    wantWidth-border*2, wantHeight-border*2,    imgX, imgY)
  im2:copyResampled(im_org, border,border,   0,0,    wantWidth2-border*2, wantHeight2-border*2,    imgX, imgY)
  font = "./Vera.ttf"
  --font = "./Vivaldii.TTF"
  --font = "./TT0756M_.TTF"
  --font = "arch___i.ttf"
  -- font = "Anniebtn.ttf"
  --[==[
  im:stringFT( color, font, size, math.rad(ang), border +2 , border + 2 + size * 1.4 , "Copyright (C) Doug Rice 2011 " )
  im:stringFT( color, font, size, math.rad(ang), border +2 , border + 2 + ( size * 1.4 )*2, picsA[ offset+1 ] )
  im2:stringFT( color, font, size, math.rad(ang), border +2 , border + 2 + size * 1.4 , "Copyright (C) Doug Rice 2011 " )
  im2:stringFT( color, font, size, math.rad(ang), border +2 , border + 2 + ( size * 1.4 )*2, picsA[ offset+1 ] )
  im:stringFT( color2, font, size, math.rad(ang), border +3 , border + 3 + size * 1.4 , "Copyright (C) Doug Rice 2011 " )
  im:stringFT( color2, font, size, math.rad(ang), border +3 , border + 3 + ( size * 1.4 )*2, picsA[ offset+1 ] )
  im2:stringFT( color2, font, size, math.rad(ang), border +3 , border + 3 + size * 1.4 , "Copyright (C) Doug Rice 2011 " )
  im2:stringFT( color2, font, size, math.rad(ang), border +3 , border + 3 + ( size * 1.4 )*2, picsA[ offset+1 ] )
  --[==[
  ]==]
  im:sharpen( 20 )
  im2:sharpen( 20 )
-- xxx01 Felixstowe No1.jpg
  -- im:png( "./img/CT_sep 11_" .. filename .. ".png" )
  im:jpeg(   "./img_op/dhr" .. filename .. ".jpg", 100 )
  im2:jpeg( "./img_op/sm/dhr" .. filename .. "_lt.jpg", 100 )
  im		= nil
  im2     	= nil
  im_org	= nil
  -- force a garbage collect as it ran out of virtual memory without it.
  collectgarbage ( "collect" )
end
function main()
  cnt = 0
  for w in string.gmatch(picsStr, "%C+") do
    print("=="..w.."==")
    if string.len(w) > 0  then
      -- picsA[cnt] = "./APSC/"..w
      picsA[cnt] = "./"..w
      cnt = cnt + 1
    end
  end
  picsA = {}
  fi = io.popen( "dir /b .\\img_ip\\*.jpg ")
  for i in fi:lines() do
    print( "+"..i )
	print( "#picsA ",picsA )
	picsA[ #picsA+1 ] = "./img_ip/" .. i
  end
  cnt = #picsA
  print( "cnt" ,cnt )
  steps = cnt
  while( steps > 0 ) do
    steps = steps - 1
    filename = "./001"..steps ..""
    prepareDPI(    "",  10,  0 ,  1000+steps, steps  )
  end
end
--
-- prepare lines for web page
--
--http://www.dougrice.plus.com/hp/gbbook/simple/codeScraps.htm#T32
function img()
  fi = io.popen( "dir /b .\\img_op\\*.jpg ")
  opStr = ""
  for i in fi:lines() do
    A,B,C = string.match( i ,"(.-)([^\\]*)(\.jpg)")
    -- print( A,"-",B,"-", C )
    -- 
\n" --print( "#picsA ",picsA ) --picsA[ #picsA+1 ] = "./img_ip/" .. i end opStrHead = [==[
]==] opStrTail = [==[ ]==] print( opStrHead .. opStr .. opStrTail ) fo = io.open( "./img_op/index1.htm","w") fo:write( opStrHead .. opStr .. opStrTail ) fo:close() end os.execute("mkdir .\\img_op") os.execute("mkdir .\\img_op\\sm") main() img() -- -- dir /b *.jpg | lua5.1 pipe.lua -- -- -- for line in io.lines() do -- print( "++++++"..line.."===") -- end --[==[ -- -- simple test to open a file line by line -- -- -- test fi = io.open( "txt.lua") for i in fi:lines() do print( ">",i ) end -- -- dir /b | txt.lua -- for i in io.lines() do print( i ) end -- -- test processing lines from dir command -- fi = io.popen( "dir /b") for i in fi:lines() do print( "+"..i ) end ]==]