Created
July 16, 2013 22:30
-
-
Save briarfox/6015774 to your computer and use it in GitHub Desktop.
ImageToProject Release v0.0.2 -Read/Write Projects from Images!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ImageToProject Tab Order Version: 0.0.2 | |
------------------------------ | |
This file should not be included in the Codea project. | |
#Main | |
#ByteImage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ByteImage = {} | |
ByteImage.createImage = function(p,n) | |
local project = p | |
local name = n | |
--local loading = Loading() | |
local projectstr = "" | |
local projectTabs = listProjectTabs(project) | |
for _,j in pairs(projectTabs) do | |
projectstr = projectstr .. readProjectTab(project..":"..j) | |
end | |
print("Dumping Project, please wait") | |
coroutine.yield() | |
local byteStr = ByteImage.dump(loadstring(projectstr)) | |
local col = 100 | |
local byteCount = #byteStr/3 | |
local row = math.ceil(byteCount / col) | |
local img = image(col,row) | |
local ist = 1 | |
local ien = 3 | |
local percent = row*col | |
local currentCount = 0 | |
for i=1,row do | |
output.clear() | |
print("Writing Image "..math.ceil(100*currentCount/percent).."%") | |
for j=1,col do | |
--Loading(math.ceil(100*currentCount/percent)) | |
local r = tonumber(byteStr:sub(ist,ien)) | |
ist = ien +1 | |
ien = ien + 3 | |
if r == nil then r = 000 end | |
local g = tonumber(byteStr:sub(ist,ien)) | |
ist = ien +1 | |
ien = ien + 3 | |
if g == nil then g = 000 end | |
local b = tonumber(byteStr:sub(ist,ien)) | |
ist = ien +1 | |
ien = ien + 3 | |
if b == nil then b = 000 end | |
img:set(j,i,r,g,b,255) | |
currentCount = currentCount + 1 | |
end | |
coroutine.yield() | |
end | |
crCreateResume = false | |
saveImage("Documents:"..name,img) | |
print("Image Created!") | |
crCreate = coroutine.create(ByteImage.createImage) | |
end | |
ByteImage.readImage = function(img) | |
-- local tststr = "" | |
local im = readImage(img) | |
local byteCount = 880 | |
local byteItr = 1 | |
local str = "tststr='" | |
local w,h = im.width, im.height | |
local percent = w*h | |
local currentCount = 0 | |
for i =1,h do | |
output.clear() | |
print("Reading Image "..math.ceil(100*currentCount/percent).."%") | |
for j=1,w do | |
--[[ | |
local r,g,b = im:get(j,i) | |
if byteItr <= byteCount then r = string.format("%03d",r) byteItr = byteItr + 1 end | |
if byteItr <= byteCount then g = string.format("%03d",g) byteItr = byteItr + 1 end | |
if byteItr <= byteCount then b = string.format("%03d",b) byteItr = byteItr + 1 end | |
if byteItr <= byteCount then str = str.."\\" .. r.."\\"..g.."\\"..b end | |
--]] | |
local r,g,b = im:get(j,i) | |
r = string.format("%03d",r) | |
g = string.format("%03d",g) | |
b = string.format("%03d",b) | |
str = str.."\\" .. r.."\\"..g.."\\"..b | |
currentCount = currentCount + 1 | |
end | |
coroutine.yield() | |
end | |
str = str .. "'" | |
loadstring(str)() | |
--print("test: "..tststr) | |
local f = loadstring(tststr) | |
print("Project Loaded") | |
crReadResume = false | |
crRead = coroutine.create(ByteImage.readImage) | |
f() --assert(loadstring(str)) | |
setup() | |
end | |
ByteImage.dump = function(f) | |
local binary = string.dump(f) | |
local formatted_binary = "" | |
for i = 1, string.len(binary) do | |
local dec, _ = ("%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0') | |
formatted_binary = formatted_binary .. dec | |
end | |
return formatted_binary | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ImageToProject | |
VERSION = "0.0.2" | |
PROJECTNAME = "ImageToProject" | |
-- Use this function to perform your initial setup | |
function setup() | |
VersionUpdateChecker.check() | |
menu() | |
crCreate = coroutine.create(ByteImage.createImage) | |
crCreateResume = false | |
crRead = coroutine.create(ByteImage.readImage) | |
crReadResume = false | |
img = nil | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(40, 40, 50) | |
text("Loading may take a few seconds",WIDTH/2,HEIGHT/2) | |
if crCreateResume then coroutine.resume(crCreate)end | |
if crReadResume then coroutine.resume(crRead) end | |
if img then sprite(img,WIDTH/2,HEIGHT/2) end | |
end | |
function menu() | |
parameter.clear() | |
parameter.action("Create_Image",createImage1) | |
parameter.action("Load_Image",loadImage) | |
parameter.action("Display_Image",display) | |
end | |
function createImage1() | |
parameter.clear() | |
parameter.action("back",function() menu() end) | |
parameter.text("Project_Name") | |
parameter.text("Save_Image_As") | |
parameter.action("Create",function() | |
print("Creating Image, This may take some time.") | |
--crCreate = coroutine.create(createImage) | |
coroutine.resume(crCreate,Project_Name,Save_Image_As) | |
crCreateResume = true | |
menu() | |
--ByteImage:createImage(Project_Name,Save_As) | |
end) | |
end | |
function loadImage() | |
parameter.clear() | |
parameter.action("back",function() menu() end) | |
parameter.text("Image_Name") | |
parameter.action("Load_Image",function() | |
print("Loading Image, this may take some time.") | |
coroutine.resume(crRead,"Documents:"..Image_Name) | |
crReadResume = true | |
menu() end) | |
end | |
function display() | |
parameter.clear() | |
parameter.action("back",function() menu() end) | |
parameter.text("Image_Name",displayIm) | |
end | |
function displayIm() | |
local tmp = readImage("Documents:"..Image_Name) | |
if tmp then img = tmp end | |
end | |
----------------------------- | |
--Update Checker Code added by AutoGist | |
----------------------------- | |
VersionUpdateChecker = {} | |
VersionUpdateChecker.gistURL = "https://api.github.com/gists/6015774" | |
VersionUpdateChecker.check = function() | |
local jsonURL = "https://dl.dropboxusercontent.com/s/9e4nvqeu4hsux2q/Json.lua?token_hash=AAFyMB98j4bnt_1gawf9wSke52hsoC7hsIvARcTuZNeOEw&dl=1" | |
local jsonSuccess = function(data) | |
local jsonCode = data | |
if jsonCode then local l = loadstring(jsonCode) l() end | |
local handleSuccess = function(data) | |
local gist = json.decode(data) | |
local version = string.match(gist.description,"%d+%.%d+%.%d+") | |
if VERSION ~= version then | |
displayMode(STANDARD) | |
print("Click Update_Project.") | |
alert("New Update Availiable! Click Update.","Update") | |
parameter.action("Update_Project",function() VersionUpdateChecker.GetProject() end) | |
end | |
end | |
http.request(VersionUpdateChecker.gistURL,handleSuccess,function() print("Update Failed") end) | |
end | |
http.request(jsonURL,jsonSuccess,function() print("Check Internet Connection") end) | |
end | |
function VersionUpdateChecker.GetProject() | |
local handleSuccess = function(data,i,j) | |
if listProjectTabs(PROJECTNAME) == nil then | |
error("Check Project Name") | |
end | |
local gist = json.decode(data) | |
local projName = PROJECTNAME | |
if gist.files["1aTabOrder"] then | |
print("***Tab Order Found***") | |
local taborder = gist.files["1aTabOrder"].content | |
local strStart =1 | |
local strEnd =0 | |
strStart = string.find(taborder,"#",strEnd) | |
strEnd = string.find(taborder,"\n",strStart) | |
while strStart do | |
local tmp = string.sub(taborder,strStart+1,strEnd-1) | |
local name = PROJECTNAME..":"..tmp | |
tmp = tmp..".lua" | |
saveProjectTab(name,gist.files[tmp].content) | |
strStart = string.find(taborder,"#",strEnd) | |
strEnd = string.find(taborder,"\n",strStart) | |
end | |
else | |
for k,v in pairs(gist.files) do | |
local name = PROJECTNAME .. ":" .. string.gsub(k,".lua","") | |
saveProjectTab(name, v.content) | |
end | |
end | |
if gist.files["ChangeLog.lua"] then | |
local str = gist.files["ChangeLog.lua"].content | |
alert(str,"Version Notes") | |
end | |
sound(SOUND_PICKUP, 24058) | |
print("Reload Project to finish update!") | |
end | |
http.request(VersionUpdateChecker.gistURL,handleSuccess,function(data) print("Update Failed") end) | |
end | |
--End of Update Checker | |
-------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment