FeynmanTech

Lua/LuaGD Image Plotting for Powder Toy

Sep 2nd, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.90 KB | None | 0 0
  1. require "gd"
  2. function gdplot(file, itype)
  3.     --Get image type
  4.     itype = string.sub(file, -4, -1)
  5.     itype = string.gsub(itype, '%.', '')
  6.     --Open image
  7.     if itype == 'jpeg' or itype == 'jpg' then
  8.         im = gd.createFromJpeg(file)
  9.     elseif itype == 'png' then
  10.         im = gd.createFromPng(file)
  11.     elseif itype == 'gif' then
  12.         im = gd.createFromGif(file)
  13.     end
  14.     --Height, width
  15.     local w, h = im:sizeXY()
  16.     --is it bigger than the screen?
  17.     if w >= 610 then
  18.         w = 610
  19.     end
  20.     if h > 380 then
  21.         h = 380
  22.     end
  23.     --Run through the image
  24.     for x = 0, w do
  25.         for y = 0, h do
  26.             --Get color as hex string...
  27.             color = tostring(bit.tohex(im:getPixel(x, y)))
  28.             --and take the r, g, b individual values.
  29.             r = string.sub(color, 3, 4)
  30.             g = string.sub(color, 5, 6)
  31.             b = string.sub(color, 7, 8)
  32.             --Draw a pixel
  33.             tpt.create(x, y, "dmnd")
  34.             tpt.set_property("dcolor", '0xFF' .. r .. g .. b, x, y)
  35.         end
  36.     end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment