FeynmanTech

LuaGD Element-based Image Plotting

Sep 10th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1. require 'gd'
  2.  
  3. --[[
  4.  
  5.  USAGE: ePlot(filename of source image, element init. resolution)
  6.  
  7.  Init. resolution should always be 255, but it might do well with lower ones.
  8.  
  9.  (Init. resolution: how close in color an element should be to be considered
  10.  'close'. It may seem better to have a low value, but that just means pixels
  11.  with no close-colored elements will simply be ignored.)
  12.  
  13. -]]
  14.  
  15. local fileWriteStr = [[
  16. DUST,255,224,160
  17. WATR,32,48,208
  18. OIL,64,64,16
  19. STNE,160,160,160
  20. LAVA,224,80,16
  21. GUN,192,192,208
  22. NITR,32,224,16
  23. CLNE,255,208,16
  24. GAS,224,255,32
  25. C-4,208,128,224
  26. GOO,128,64,0
  27. ICE,160,192,255
  28. METL,64,64,96
  29. SPRK,255,255,128
  30. SNOW,192,224,255
  31. WOOD,192,160,64
  32. NEUT,32,224,255
  33. PLUT,64,112,32
  34. PLNT,12,172,0
  35. ACID,237,85,255
  36. VOID,121,11,11
  37. WTRV,160,160,255
  38. CNCT,192,192,192
  39. DSTW,16,32,192
  40. SALT,255,255,255
  41. SLTW,64,80,240
  42. DMND,204,255,255
  43. BMTL,80,80,112
  44. BRMT,112,80,96
  45. PHOT,255,255,255
  46. URAN,112,112,32
  47. WAX,240,240,187
  48. MWAX,224,224,170
  49. PSCN,128,80,80
  50. NSCN,80,80,128
  51. LN2,128,160,223
  52. INSL,158,163,182
  53. VACU,48,48,48
  54. VENT,239,239,239
  55. RBDM,204,204,204
  56. LRBD,170,170,170
  57. NTCT,80,80,64
  58. SAND,255,208,144
  59. GLAS,64,64,64
  60. PTCT,64,80,80
  61. BGLA,96,96,96
  62. THDR,255,255,160
  63. ETRD,64,64,64
  64. NICE,192,224,255
  65. NBLE,235,73,23
  66. BTRY,133,133,5
  67. LCRY,80,80,80
  68. SWCH,16,59,17
  69. DESL,68,0,0
  70. COAL,34,34,34
  71. LOXY,128,160,239
  72. OXYG,128,160,255
  73. INWR,84,65,65
  74. YEST,238,224,192
  75. DYST,187,176,160
  76. THRM,160,128,144
  77. BRCK,128,128,128
  78. FIRW,255,160,64
  79. FUSE,10,87,6
  80. FSEP,99,173,95
  81. AMTR,128,128,128
  82. BCOL,51,51,51
  83. PCLN,59,59,10
  84. HSWC,59,10,10
  85. IRON,112,112,112
  86. MORT,224,224,224
  87. LIFE,12,172,0
  88. DLAY,117,53,144
  89. CO2,102,102,102
  90. DRIC,224,224,224
  91. BUBW,32,48,208
  92. STOR,80,223,223
  93. PVOD,121,32,32
  94. CONV,10,171,10
  95. CAUS,128,255,160
  96. LIGH,255,255,192
  97. TESC,112,112,64
  98. DEST,255,51,17
  99. SPNG,255,190,48
  100. RIME,204,204,204
  101. FOG,170,170,170
  102. BCLN,255,208,64
  103. LOVE,255,48,255
  104. DEUT,0,21,63
  105. WARP,16,16,16
  106. PUMP,10,10,59
  107. FWRK,102,102,102
  108. PIPE,68,68,68
  109. FRZZ,192,224,255
  110. FRZW,16,32,192
  111. GRAV,255,224,160
  112. BIZR,0,255,119
  113. BIZG,0,255,187
  114. BIZS,0,228,85
  115. INST,64,64,57
  116. ISOZ,170,48,208
  117. ISZS,102,32,137
  118. PRTI,235,89,23
  119. PRTO,0,32,235
  120. PSTE,170,153,170
  121. PSTS,119,102,119
  122. ANAR,255,255,238
  123. VINE,7,154,0
  124. INVS,0,204,204
  125. EQVE,255,224,160
  126. SHLD,170,170,170
  127. SHD2,119,119,119
  128. SHD3,68,68,68
  129. SHD4,33,33,33
  130. LOLZ,86,146,18
  131. WIFI,64,160,96
  132. FILT,0,0,86
  133. ARAY,255,187,0
  134. BOMB,255,242,136
  135. C-5,32,80,224
  136. SING,36,36,36
  137. QRTZ,170,221,221
  138. PQRT,136,187,187
  139. EMP,102,170,255
  140. BREL,112,112,96
  141. ELEC,223,239,255
  142. ACEL,0,153,204
  143. DCEL,153,204,0
  144. TNT,192,80,80
  145. IGNC,192,176,80
  146. BOYL,10,50,0
  147. GEL,255,153,0
  148. TTAN,144,144,144
  149. EMBR,255,242,136
  150. HYGN,80,112,255
  151. SOAP,245,245,220
  152. BHOL,32,32,32
  153. WHOL,255,255,255
  154. MERC,115,107,109
  155. PBCN,59,29,10
  156. GPMP,10,59,59
  157. CLST,228,164,164
  158. WWLD,255,204,0
  159. GBMB,17,68,187
  160. FRAY,0,187,255
  161. RPEL,153,204,0
  162. PPIP,68,68,102
  163. DTEC,253,157,24
  164. DMG,136,255,136
  165. TSNS,253,0,213
  166. VIBR,0,80,0
  167. BVBR,0,80,0
  168. CRAY,187,255,0
  169. PSTN,170,153,153
  170. FRME,153,153,136
  171. GOLD,220,173,44
  172. TUNG,80,80,80
  173. PSNS,219,32,32
  174. ]]
  175.  
  176. local f = io.open('element colors.txt', 'w')
  177. f:write(fileWriteStr)
  178. f:close()
  179.  
  180. function getNums(str) -- Get the key values out of the string (used later)
  181.  local numMat = {}
  182.  local numStr = ''
  183.  local count = 1
  184.  for k = 1, #str do
  185.   cstr = string.sub(str, k, k)
  186.   if string.gsub(cstr, '%a', '') ~= '' and string.gsub(cstr, '%d', ' ') == ' ' then
  187.    isNum = true
  188.    numStr = numStr .. cstr
  189.    if k == #str then
  190.     numMat[count] = tonumber(numStr)
  191.    end
  192.   elseif cstr == ',' and isNum == true then
  193.    numMat[count] = tonumber(numStr)
  194.    numStr = ''
  195.    count = count + 1
  196.   else
  197.    isNum = false
  198.   end
  199.  end
  200.  return numMat
  201. end
  202.  
  203. function lines(file) -- Used for reading lines of a file and returning them as a matrix
  204.  local rLines = {}
  205.  local lines = io.lines(file)
  206.  local bLines = io.lines(file)
  207.  local k = 1
  208.  while lines() do
  209.   rLines[k] = bLines()
  210.   k = k +1
  211.  end
  212.  return rLines
  213. end
  214.  
  215. function generateFile(f1, f2) -- used to set up colors file
  216.  local clines = lines(f2)
  217.  local f = io.open(f1, 'w')
  218.  local r = {}
  219.  local g = {}
  220.  local b = {}
  221.  local e = {}
  222.  for k = 1, #clines do
  223.   local cline = clines[k]
  224.   local nums = getNums(cline)
  225.   local celem = string.gsub(string.sub(cline, 1, 4), ',', '')
  226.   r[k] = nums[1]
  227.   g[k] = nums[2]
  228.   b[k] = nums[3]
  229.   e[k] = celem
  230.  end
  231.  f:write('r = {')
  232.  for k = 1, #clines do
  233.   if k == #clines then
  234.    f:write(r[k], '}\ng = {')
  235.   else
  236.    f:write(r[k], ', ')
  237.   end
  238.  end
  239.  for k = 1, #clines do
  240.   if k == #clines then
  241.    f:write(g[k], '}\nb = {')
  242.   else
  243.    f:write(g[k], ', ')
  244.   end
  245.  end
  246.  for k = 1, #clines do
  247.   if k == #clines then
  248.    f:write(b[k], '}\ne = {')
  249.   else
  250.    f:write(b[k], ', ')
  251.   end
  252.  end
  253.  for k = 1, #clines do
  254.   if k == #clines then
  255.    f:write('\'', string.gsub(e[k], '-', ''), '\'', '}')
  256.   else
  257.    f:write('\'', string.gsub(e[k], '-', ''), '\'', ', ')
  258.   end
  259.  end
  260.  f:close()
  261. end
  262.  
  263. generateFile('plottingElems.txt', 'element colors.txt')
  264.  
  265. dofile('plottingelems.txt')
  266.  
  267. function hextonum(str)
  268.  local num = 0
  269.  for k = 1, #str do
  270.   local cstr = string.lower(string.sub(str, k, k))
  271.   if cstr == 'a' then n = 10
  272.   elseif cstr == 'b' then n = 11
  273.   elseif cstr == 'c' then n = 12
  274.   elseif cstr == 'd' then n = 13
  275.   elseif cstr == 'e' then n = 14
  276.   elseif cstr == 'f' then n = 15
  277.   else n = tonumber(cstr)
  278.   end
  279.   num = num + (n * 16 ^ (#str - k))
  280.  end
  281.  return num
  282. end
  283. function getElem(res)
  284.  local rec = res
  285.  for k = 1, #r do
  286.   if r[k] >= cr - rec and r[k] <= cr + rec then
  287.    if g[k] >= cg - rec and g[k] <= cg + rec then
  288.     if b[k] >= cb - rec and b[k] <= cb + rec then
  289.      elemDrawType = e[k]
  290.      rec = math.floor((math.abs(r[k] - cr) + math.abs(g[k] - cg) + math.abs(b[k] - cb)) / 3)
  291.     end
  292.    end
  293.   end
  294.  end
  295. end
  296.  
  297. function ePlot(file, res)
  298.  --Get image type
  299.  itype = string.sub(file, -4, -1)
  300.  itype = string.gsub(itype, '%.', '')
  301.  --Open image
  302.  if itype == 'jpeg' or itype == 'jpg' then
  303.   im = gd.createFromJpeg(file)
  304.  elseif itype == 'png' then
  305.   im = gd.createFromPng(file)
  306.  elseif itype == 'gif' then
  307.   im = gd.createFromGif(file)
  308.  end
  309.  --Height, width
  310.  local w, h = im:sizeXY()
  311.  --is it bigger than the screen?
  312.  if w >= 610 then
  313.   w = 610
  314.  end
  315.  if h > 380 then
  316.   h = 380
  317.  end
  318.  --Run through the image
  319.  for x = 0, w do
  320.   for y = 0, h do
  321.    --Get color as hex string...
  322.    color = tostring(bit.tohex(im:getPixel(x, y)))
  323.    --and take the r, g, b individual values.
  324.    cr = hextonum(string.sub(color, 3, 4))
  325.    cg = hextonum(string.sub(color, 5, 6))
  326.    cb = hextonum(string.sub(color, 7, 8))
  327.    if cr <= 20 and cg <= 20 and cb <= 20 then --Black - Improving speed by not looking through all the elements
  328.     elemDrawType = 'NONE'
  329.    elseif cr >= 240 and cg >= 240 and cb >= 240 then -- White
  330.     elemDrawType = 'WHOL'
  331.    else
  332.     getElem(res)
  333.    end
  334.    --Draw a pixel
  335.    tpt.create(x, y, elemDrawType)
  336.    if elemDrawType == 'PUMP' or elemDrawType == 'GPMP' or elemDrawType == 'LAVA' then
  337.     tpt.set_property('life', 0, x, y)
  338.    elseif elemDrawType == 'QRTZ' or elemDrawType == 'PQRT' or elemDrawType == 'STOR' then
  339.     tpt.set_property('tmp', 5, x, y)
  340.    elseif elemDrawType == 'SPRK' then
  341.     tpt.create(x, y, 'METL')
  342.     tpt.create(x, y, 'SPRK')
  343.    end
  344.    tpt.set_property('dcolor', '0xFF' .. color, x, y)
  345.   end
  346.  end
  347. end
Advertisement
Add Comment
Please, Sign In to add comment