FeynmanTech

TPT Lua Element-based GD Image Plotting v.3

Sep 11th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 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. PUMP,10,10,59
  106. FWRK,102,102,102
  107. PIPE,68,68,68
  108. FRZZ,192,224,255
  109. FRZW,16,32,192
  110. GRAV,255,224,160
  111. BIZR,0,255,119
  112. BIZG,0,255,187
  113. BIZS,0,228,85
  114. INST,64,64,57
  115. ISOZ,170,48,208
  116. ISZS,102,32,137
  117. PRTI,235,89,23
  118. PRTO,0,32,235
  119. PSTE,170,153,170
  120. PSTS,119,102,119
  121. ANAR,255,255,238
  122. VINE,7,154,0
  123. INVS,0,204,204
  124. EQVE,255,224,160
  125. SHLD,170,170,170
  126. SHD2,119,119,119
  127. SHD3,68,68,68
  128. SHD4,33,33,33
  129. LOLZ,86,146,18
  130. WIFI,64,160,96
  131. FILT,0,0,86
  132. ARAY,255,187,0
  133. BOMB,255,242,136
  134. C-5,32,80,224
  135. SING,36,36,36
  136. QRTZ,170,221,221
  137. PQRT,136,187,187
  138. EMP,102,170,255
  139. BREL,112,112,96
  140. ELEC,223,239,255
  141. ACEL,0,153,204
  142. DCEL,153,204,0
  143. TNT,192,80,80
  144. IGNC,192,176,80
  145. BOYL,10,50,0
  146. GEL,255,153,0
  147. TTAN,144,144,144
  148. EMBR,255,242,136
  149. HYGN,80,112,255
  150. SOAP,245,245,220
  151. BHOL,32,32,32
  152. WHOL,255,255,255
  153. MERC,115,107,109
  154. PBCN,59,29,10
  155. GPMP,10,59,59
  156. CLST,228,164,164
  157. WWLD,255,204,0
  158. GBMB,17,68,187
  159. FRAY,0,187,255
  160. RPEL,153,204,0
  161. PPIP,68,68,102
  162. DTEC,253,157,24
  163. DMG,136,255,136
  164. TSNS,253,0,213
  165. VIBR,0,80,0
  166. BVBR,0,80,0
  167. CRAY,187,255,0
  168. PSTN,170,153,153
  169. FRME,153,153,136
  170. GOLD,220,173,44
  171. TUNG,80,80,80
  172. PSNS,219,32,32
  173. ]]
  174.  
  175. local f = io.open('element colors.txt', 'w')
  176. f:write(fileWriteStr)
  177. f:close()
  178.  
  179. function getNums(str) -- Get the key values out of the string (used later)
  180.  local numMat = {}
  181.  local numStr = ''
  182.  local count = 1
  183.  for k = 1, #str do
  184.   cstr = string.sub(str, k, k)
  185.   if string.gsub(cstr, '%a', '') ~= '' and string.gsub(cstr, '%d', ' ') == ' ' then
  186.    isNum = true
  187.    numStr = numStr .. cstr
  188.    if k == #str then
  189.     numMat[count] = tonumber(numStr)
  190.    end
  191.   elseif cstr == ',' and isNum == true then
  192.    numMat[count] = tonumber(numStr)
  193.    numStr = ''
  194.    count = count + 1
  195.   else
  196.    isNum = false
  197.   end
  198.  end
  199.  return numMat
  200. end
  201.  
  202. function lines(file) -- Used for reading lines of a file and returning them as a matrix
  203.  local rLines = {}
  204.  local lines = io.lines(file)
  205.  local bLines = io.lines(file)
  206.  local k = 1
  207.  while lines() do
  208.   rLines[k] = bLines()
  209.   k = k +1
  210.  end
  211.  return rLines
  212. end
  213.  
  214. function generateFile(f1, f2) -- used to set up colors file
  215.  local clines = lines(f2)
  216.  local f = io.open(f1, 'w')
  217.  local r = {}
  218.  local g = {}
  219.  local b = {}
  220.  local e = {}
  221.  for k = 1, #clines do
  222.   local cline = clines[k]
  223.   local nums = getNums(cline)
  224.   local celem = string.gsub(string.sub(cline, 1, 4), ',', '')
  225.   r[k] = nums[1]
  226.   g[k] = nums[2]
  227.   b[k] = nums[3]
  228.   e[k] = celem
  229.  end
  230.  f:write('r = {')
  231.  for k = 1, #clines do
  232.   if k == #clines then
  233.    f:write(r[k], '}\ng = {')
  234.   else
  235.    f:write(r[k], ', ')
  236.   end
  237.  end
  238.  for k = 1, #clines do
  239.   if k == #clines then
  240.    f:write(g[k], '}\nb = {')
  241.   else
  242.    f:write(g[k], ', ')
  243.   end
  244.  end
  245.  for k = 1, #clines do
  246.   if k == #clines then
  247.    f:write(b[k], '}\ne = {')
  248.   else
  249.    f:write(b[k], ', ')
  250.   end
  251.  end
  252.  for k = 1, #clines do
  253.   if k == #clines then
  254.    f:write('\'', string.gsub(e[k], '-', ''), '\'', '}')
  255.   else
  256.    f:write('\'', string.gsub(e[k], '-', ''), '\'', ', ')
  257.   end
  258.  end
  259.  f:close()
  260. end
  261.  
  262. generateFile('plottingElems.txt', 'element colors.txt')
  263.  
  264. dofile('plottingelems.txt')
  265.  
  266. function hextonum(str)
  267.  local num = 0
  268.  for k = 1, #str do
  269.   local cstr = string.lower(string.sub(str, k, k))
  270.   if cstr == 'a' then n = 10
  271.   elseif cstr == 'b' then n = 11
  272.   elseif cstr == 'c' then n = 12
  273.   elseif cstr == 'd' then n = 13
  274.   elseif cstr == 'e' then n = 14
  275.   elseif cstr == 'f' then n = 15
  276.   else n = tonumber(cstr)
  277.   end
  278.   num = num + (n * 16 ^ (#str - k))
  279.  end
  280.  return num
  281. end
  282. function range(num1, num2, range)
  283.  if type(num1) == 'number' and type(num2) == 'number' and type(range) == 'number' then
  284.   return math.abs(num1 - num2) <= range
  285.  else
  286.   return nil
  287.  end
  288. end
  289. function getElem(res, pTest, nTest)
  290.  if nTest == nil or nTest == false then
  291.   nTestBool = true
  292.  else
  293.   nTestBool = false
  294.  end
  295.  local rec = res
  296.  for k = 1, #r do
  297.   local tr = r[k]
  298.   local tg = g[k]
  299.   local tb = b[k]
  300.   if tr >= cr - rec and tr <= cr + rec then
  301.    if tg >= cg - rec and tg <= cg + rec then
  302.     if tb >= cb - rec and tb <= cb + rec then
  303.      if isPositive(tr - cr) == isPositive(tg - cg) and isPositive(tr - cr) == isPositive(tb - cb) or pTest then
  304.       --if range(tr, cr, nTest) and range(tg, cg, nTest) and range(tb, cb, nTest) or nTestBool then -- WIP
  305.        elemDrawType = e[k]
  306.        rec = math.floor((math.abs(r[k] - cr) + math.abs(g[k] - cg) + math.abs(b[k] - cb)) / 3)
  307.       --end
  308.      end
  309.     end
  310.    end
  311.   end
  312.  end
  313. end
  314.  
  315. function isPositive(num) --For testing divergence direction of colors
  316.  return num > 0
  317. end
  318.  
  319. function ePlot(file, res, ptest, ltest)
  320.  --Get image type
  321.  itype = string.sub(file, -4, -1)
  322.  itype = string.gsub(itype, '%.', '')
  323.  --Open image
  324.  if itype == 'jpeg' or itype == 'jpg' then
  325.   im = gd.createFromJpeg(file)
  326.  elseif itype == 'png' then
  327.   im = gd.createFromPng(file)
  328.  elseif itype == 'gif' then
  329.   im = gd.createFromGif(file)
  330.  end
  331.  --Height, width
  332.  local w, h = im:sizeXY()
  333.  --is it bigger than the screen?
  334.  if w >= 610 then
  335.   w = 610
  336.  end
  337.  if h > 380 then
  338.   h = 380
  339.  end
  340.  --Run through the image
  341.  for x = 0, w do
  342.   for y = 0, h do
  343.    --Get color as hex string...
  344.    color = tostring(bit.tohex(im:getPixel(x, y)))
  345.    --and take the r, g, b individual values.
  346.    cr = hextonum(string.sub(color, 3, 4))
  347.    cg = hextonum(string.sub(color, 5, 6))
  348.    cb = hextonum(string.sub(color, 7, 8))
  349.    if cr <= 32 and cg <= 32 and cb <= 32 then --Black - Improving speed by not looking through all the elements
  350.     elemDrawType = 'BRAY'
  351.    elseif cr == 255 and cg == 255 and cb == 255 then -- White
  352.     elemDrawType = 'WHOL'
  353.    else
  354.     getElem(res, ptest, ltest)
  355.    end
  356.    --Draw a pixel
  357.    tpt.create(x, y, elemDrawType)
  358.    if elemDrawType == 'PUMP' or elemDrawType == 'GPMP' or elemDrawType == 'LAVA' then
  359.     tpt.set_property('life', 0, x, y)
  360.    elseif elemDrawType == 'QRTZ' or elemDrawType == 'PQRT' or elemDrawType == 'STOR' or elemDrawType == 'CLST' then
  361.     tpt.set_property('tmp', 5, x, y)
  362.    elseif elemDrawType == 'SPRK' then
  363.     tpt.create(x, y, 'METL')
  364.     tpt.create(x, y, 'SPRK')
  365.    elseif elemDrawType == 'BRAY' then
  366.     tpt.set_property('life', (cr + cg + cb) * 1.3, x, y)
  367.     tpt.set_property('tmp', 1, x, y)
  368.    end
  369.    if debugging == true then print(color) end
  370.    tpt.set_property('dcolor', '0xFF' .. bit.tohex(cr, 2) .. bit.tohex(cg, 2) .. bit.tohex(cb, 2), x, y)
  371.   end
  372.  end
  373. end
Advertisement
Add Comment
Please, Sign In to add comment