FeynmanTech

Javascript BMP-To-Script Converter for Khan Academy

Apr 24th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. function khan(filename,scriptname,squaresize) -- Type in your luasocket or whatever "khan(Name of image,name of
  2.                           -- created script,squaresize)" Without the quotes. It works well if pic
  3.                           -- is less than 200x200 pix. It only works with 24-bit BMPs though.
  4.                           -- Squaresize: Size of the pixels.
  5. local file=io.open(filename,'rb')
  6. local f=io.open(scriptname,'w')
  7. file:seek('set',18)
  8. local y=string.byte(file:read(1))+string.byte(file:read(1))*256+string.byte(file:read(1))*65536+string.byte(file:read(1))*16777216
  9. local x=string.byte(file:read(1))+string.byte(file:read(1))*256+string.byte(file:read(1))*65536+string.byte(file:read(1))*16777216
  10. file:seek('set',54)
  11. local r,g,b=0,0,0
  12. for j=0,x-1 do
  13.  for i=0,y-1 do
  14.   b,g,r=string.byte(file:read(1)),string.byte(file:read(1)),string.byte(file:read(1))
  15.   if i*squaresize<401 and (x-j)*squaresize<401 then
  16.    f:write("stroke(",r,",",g,",",b,");\n")
  17.    f:write("rect(",i*squaresize,",",(x-j)*squaresize,",",squaresize,",",squaresize,");\n")
  18. end
  19.   end
  20.  end
  21.  for i=1,y%4 do file:read(1) end
  22. file:close()
  23. end
Advertisement
Add Comment
Please, Sign In to add comment