Author Topic: [Lua] Image Editor  (Read 44522 times)

0 Members and 1 Guest are viewing this topic.

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Image Editor
« Reply #15 on: August 09, 2011, 01:57:52 pm »
New features !
-Creating new images
-Nice sprites for tools
-When a file is modified but not saved, there's a '*' before its name
-The program now shows the image size

Now it can be used as a sprite creator for on-calc Lua, but it still lacks scrolling (if you want very large sprites).

Maybe a mix between MS Paint and Nspire that gives nSpaint ?
I like your idea ! It's now called nSpaint

Did you update the 1st post tns attachment?
Unless I have a cache problem, it is byte to byte identic with the one I downloaded 2 days ago...
Oh, I forgot that ! Sorry. The latest version is attached to this post.

I just have a small suggestion that could possibly be added later. There's a boolean that returns true if the calc is color, but if it's greyscale it returns false, you could make only one slider for the grey level in the color selector.
Even if we can't see the color, I think that it's useful to create RGB pictures on greyscale calcs (because we can send pictures/lua scripts to CX calcs)

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: [Lua] Image Editor
« Reply #16 on: August 09, 2011, 02:23:00 pm »
Once again, nice job!
Updated code please?

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: [Lua] Image Editor
« Reply #17 on: August 09, 2011, 02:32:47 pm »
Updated code please?

Answer :

Oh, I forgot that ! Sorry. The latest version is attached to this post.

=)
« Last Edit: August 09, 2011, 02:33:34 pm by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: [Lua] Image Editor
« Reply #18 on: August 09, 2011, 02:33:49 pm »
Once again, nice job!
Updated code please?

Answer :

Oh, I forgot that ! Sorry. The latest version is attached to this post.

=)
I meant the Lua source code :D

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Image Editor
« Reply #19 on: August 09, 2011, 02:35:41 pm »
Here it is (now 600 lines !) :

Spoiler For Spoiler:
Data.lua :
Code: [Select]

--DATA
--drawing
status="drawing"
tool=nil
xpos=1
ypos=1
color={225,170,0}
changedMark=""
--image
imgString=nil
imgTable=nil
imgZoom=1
imgName=""
--color select
newColor={200,100,255}
selColor=1
--text request
requested=""
rqstr=""
--error
errtype=""
--tools sprites - 1:move  2:draw  3:erase
toolsSprites={}
toolsSprites[1]=image.new("\020\000\000\000\020\000\000\000\000\000\000\000(\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202_\202_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\000\128\000\128\000\128_\202\000\128\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\000\128\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128\000\128\255\255\000\128_\202\000\128\000\128\000\128\000\128\000\128_\202\000\128\000\128\000\128\000\128\000\128_\202\000\128\255\255\255\255\000\128\000\128\000\128_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202_\202\000\128\255\255\000\128\000\128\255\255\000\128_\202\000\128\000\128\000\128\000\128\000\128_\202\000\128\000\128\000\128\000\128\000\128_\202\000\128\255\255\255\255\000\128\000\128\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\000\128\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\000\128\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\000\128\000\128\000\128_\202\000\128\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202_\202_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128_\202\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
toolsSprites[2]=image.new("\020\000\000\000\020\000\000\000\000\000\000\000(\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\011\202N\218-\214-\210\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\167\193p\230Z\255\148\254h\225e\185\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\234\197\145\222Y\255\148\254\140\253\164\240\001\173\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\167\189L\214\024\227\247\226\173\249\132\252$\209\012\202\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\168\193(\214\206\238h\230\213\218\239\229\226\208\234\197\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\168\193\008\210\238\238g\234#\226*\210\202\209\200\193\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\200\193\231\205\238\242h\234$\226\162\201B\185\168\193\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\200\193\231\205\238\242\136\234#\226\162\201\034\185\201\197\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\168\193\231\205\238\242\136\234#\226\162\201!\181\234\197\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\167\193\007\210\238\242\136\234#\226\162\201!\181\011\202\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\167\189(\214\238\238\136\234$\226\162\201!\181\011\202\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\012\202)\210\204\238g\234$\226\162\201!\181\235\201\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\134\189\240\230u\243\170\234\130\197B\185\202\197\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\232\201\016\235S\243\016\239A\185\168\193\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\012\202\144\214\208\230\008\210#\181\234\197\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\168\193\167\193e\185O\210\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\000\128\255\255\255\255\000\128\255\255\255\255\000\128\255\255\255\255\000\128\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
toolsSprites[3]=image.new("\020\000\000\000\020\000\000\000\000\000\000\000(\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255y\182\215\157\215\157\214\157\214\157\214\157\214\157\214\157\181\157\180\153z\190\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255y\186\215\157\028\215\219\206\219\202\219\202\219\202\219\202\219\202\219\202\219\202/\149Y\182\255\255\000\128\000\128\255\255\255\255\255\255\219\202\215\157<\219\187\198\187\198\187\198\187\198\187\198\187\198\187\198\186\198\028\215\154\190Q\149\255\255\000\128\000\128\255\255\255\255\255\255P\149<\219\219\202\187\198\187\198\187\198\187\198\187\198\187\198\187\198\187\198\028\215\024\170Q\149\255\255\000\128\000\128\255\255\255\255P\149<\219<\219\180\238\180\238\180\242\180\242\180\242\180\242\180\242\147\242\028\215\187\198\024\170Q\149\255\255\000\128\000\128\255\255\255\255\173\245\023\247\180\242\180\242\180\242\180\242\180\242\180\242\180\242\147\242\180\242\023\243r\242\024\170Q\149\255\255\000\128\000\128\255\255\238\245r\246\214\242\180\238\180\242\180\242\180\242\180\242\180\242\180\242\147\242\213\242\147\246r\242Q\149\255\255\255\255\000\128\000\128\255\255Q\246\024\247\180\242\180\242\180\242\148\242\148\242\148\242\147\242\147\242\180\242\246\242r\242\015\242Q\149\255\255\255\255\000\128\000\128\255\255\239\245\246\242\180\242\180\242\180\242\180\242\148\242\147\242\148\246\147\246\181\246\214\246r\246\198\248\255\255\255\255\255\255\000\128\000\128\255\255\238\2459\2478\2518\2518\2518\2518\2518\2518\2518\251\023\251R\250\173\245)\237\255\255\255\255\255\255\000\128\000\128\255\255\140\245\016\254\207\253\239\253\239\253\239\253\239\253\239\253\239\253\239\253\206\253\206\249\231\244\255\255\255\255\255\255\255\255\000\128\000\128\255\255k\245R\2501\2501\2501\2501\2501\2501\2501\2501\2501\250\140\249(\233\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\198\248\166\248\198\248\198\248\198\248\198\248\198\248\198\248\198\248\198\248\198\244\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")

--MENU
function menuNew() on.charIn("N") end
function menuOpen() on.charIn("O") end
function menuSave() on.charIn("S") end
function menuZIn() on.charIn("+") end
function menuZOut() on.charIn("-") end
function menuDraw() on.charIn("d") end
function menuPaint() on.charIn("p") end
function menuErase() on.charIn("e") end
function menuCHex() on.charIn("h") end
function menuCSel() on.charIn("s") end

menu={
{"File",
 {"New (Shift+n)",menuNew},
 {"Open (Shift+o)",menuOpen},
 {"Save (Shift+s)",menuSave}
},
{"Edit",
 {"Zoom in (+)",menuZIn},
 {"Zoom out (-)",menuZOut}
},
{"Tools",
 {"Draw (d)",menuDraw},
 {"Paint (p)",menuPaint},
 {"Erase (e)",menuErase}
},
{"Color",
 {"Hexadecimal (h)",menuCHex},
 {"Select (s)",menuCSel}
}}
toolpalette.register(menu)

Functions.lua
Code: [Select]

--MATHTOOLS
mathTools={}

function mathTools.base10to2(n)
 local str
 str=""
 if n~=0 then
  while n~=1 do
   str=str..tostring(n%2)
   n=math.floor(n/2)
  end
  str=str..tostring(n%2)
  return string.reverse(str)
 else
  return "0"
 end
end

function mathTools.base2to10(n)
 local num = 0
 local ex = string.len(n) - 1
 local l = 0
 l = ex + 1
 for i = 1, l do
  b = string.sub(n, i, i)
  if b == "1" then
   num = num + 2^ex
  end
  ex = ex - 1
 end
 return num
end

function mathTools.int2Hex(int)
 if int<10 then
  return tostring(int)
 else
  if int==10 then return "A"
  elseif int==11 then return "B"
  elseif int==12 then return "C"
  elseif int==13 then return "D"
  elseif int==14 then return "E"
  elseif int==15 then return "F"
  end
 end
end



--MISCELLANEOUS


function width() return platform.window:width() end
function height() return platform.window:height() end
function refresh() platform.window:invalidate() end


function strch(str,ch)
 local test
 test=nil
 for i=1,string.len(str),1 do
  if string.sub(str,i,i)==ch then
   test=1
  end
 end
 return test
end

function saveFile()
 if rqstr=="" then
  status="error"
  errtype="Please type a name"
 elseif strch("0123456789",string.sub(rqstr,1,1)) then
  status="error"
  errtype="Invalid name"
 else
  imgString=imgTools.table2img()
  var.store(rqstr,imgString)
  imgName=rqstr
  changedMark=""
 end
end

function loadFile()
 local test
 test=var.recall(rqstr)
 if not test then
  status="error"
  errtype="File does not exist"
 elseif type(test)~="string" then
  status="error"
  errtype="Invalid file"
 else
  imgZoom=1
  xpos=1
  ypos=1
  tool=nil
  changedMark=""
  imgString=test
  imgTools.img2table(imgString)
  imgName=rqstr
 end
end

function newFile()
 imgWidth=tempVarWidth
 imgHeight=tonumber(rqstr)
 imgZoom=1
 xpos=1
 ypos=1
 tool=nil
 changedMark=""
 imgName="unsaved"
 imgTable={}
 for i=1,imgHeight do
  imgTable[i]={}
  for j=1,imgWidth do
   imgTable[i][j]={0,0,0,0}
  end
 end
end

function loadHexColor()
 local isHex,tmptable
 isHex=1
 tmptable={}
 if string.len(rqstr)==6 then
  for i=1,6,1 do
   currentch=string.sub(rqstr,i,i)
   if strch("0123456789",currentch) then
    tmptable[i]=tonumber(currentch)
   elseif strch("Aa",currentch) then tmptable[i]=10
   elseif strch("Bb",currentch) then tmptable[i]=11
   elseif strch("Cc",currentch) then tmptable[i]=12
   elseif strch("Dd",currentch) then tmptable[i]=13
   elseif strch("Ee",currentch) then tmptable[i]=14
   elseif strch("Ff",currentch) then tmptable[i]=15
   else isHex=nil end
  end
 else isHex=nil
 end
 if isHex then
  color={tmptable[1]*16+tmptable[2],tmptable[3]*16+tmptable[4],tmptable[5]*16+tmptable[6]}
 else
  status="error"
  errtype="Invalid hexadecimal"
 end
end



imgTools.lua
Code: [Select]

imgTools={}

function imgTools.getPixel(byte1,byte2)
 local str2
 str2=imgTools.addZeros(mathTools.base10to2(tonumber(byte2)),8)..imgTools.addZeros(mathTools.base10to2(tonumber(byte1)),8)
 return {tonumber(string.sub(str2,1,1)),mathTools.base2to10(string.sub(str2,2,6)),mathTools.base2to10(string.sub(str2,7,11)),mathTools.base2to10(string.sub(str2,12,16))}
end

function imgTools.getSize(img)
 imgWidth=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,10,12)..string.sub(img,7,9)..string.sub(img,4,6)..string.sub(img,1,3))))
 imgHeight=mathTools.base2to10(mathTools.base10to2(tonumber(string.sub(img,22,24)..string.sub(img,19,21)..string.sub(img,16,18)..string.sub(img,13,15))))
end

function imgTools.addZeros(str,strSize)
 return string.rep("0",strSize-string.len(str))..str
end

function imgTools.convertChars(img)
 local finished,img2,index
 index=1
 img2=""
 finished=nil
 while not finished do
  if string.sub(img,index,index)~=[[\]] then
   img2=img2..imgTools.addZeros(tostring(string.byte(string.sub(img,index,index))),3)
   index=index+1
  else
   img2=img2..string.sub(img,index+1,index+3)
   index=index+4
  end
  if index>string.len(img) then
   finished=1
  end
 end
 return img2
end

function imgTools.generateHeader()
 local binaryHeader,header
 binaryHeader={}
 binaryHeader[1]=imgTools.addZeros(mathTools.base10to2(imgWidth),32)
 binaryHeader[2]=imgTools.addZeros(mathTools.base10to2(imgHeight),32)
 binaryHeader[3]=imgTools.addZeros(mathTools.base10to2(imgWidth*2),32)
 header={"","",""}
 for i=1,3 do
  for j=3,0,-1 do
   header[i]=header[i]..imgTools.addZeros(tostring(mathTools.base2to10(string.sub(binaryHeader[i],8*j+1,8*(j+1)))),3)
  end
 end
 return header[1]..header[2].."000000000000"..header[3].."016000001000"
end

function imgTools.convertPixel(pixel)
 local str
 str=tostring(pixel[1])..imgTools.addZeros(mathTools.base10to2(pixel[2]),5)..imgTools.addZeros(mathTools.base10to2(pixel[3]),5)..imgTools.addZeros(mathTools.base10to2(pixel[4]),5)
 return imgTools.addZeros(tostring(mathTools.base2to10(string.sub(str,9,16))),3)..imgTools.addZeros(tostring(mathTools.base2to10(string.sub(str,1,8))),3)
end

function imgTools.addSlashes(str)
 local finished,str2,index
 finished=nil
 str2=[[\]]..str
 index=1
 while not finished do
  index=index+4
  if index>string.len(str2) then
   finished=1
  else
   str2=string.sub(str2,1,index-1)..[[\]]..string.sub(str2,index)
  end
 end
 return str2
end

function imgTools.img2table(str)
 local index
 str2=imgTools.convertChars(str)
 imgTable={}
 imgTools.getSize(str2)
 for raw=1,imgHeight do
  imgTable[raw]={}
  for column=1,imgWidth do
   index=(column-1)*6+61+(raw-1)*6*imgWidth
   imgTable[raw][column]=imgTools.getPixel(string.sub(str2,index,index+2),string.sub(str2,index+3,index+5))
  end
 end
end

function imgTools.drawTable(gc,x,y)
 gc:setPen("thin","dashed")
 gc:drawRect(x-1,y-1,imgWidth*imgZoom+1,imgHeight*imgZoom+1)
 gc:setPen("thin","smooth")
 for raw=1,imgHeight do
  for column=1,imgWidth do
   if imgTable[raw][column][1]==1 then
    gc:setColorRGB(imgTable[raw][column][2]*8,imgTable[raw][column][3]*8,imgTable[raw][column][4]*8)
    gc:fillRect((column-1)*imgZoom+x,(raw-1)*imgZoom+y,imgZoom,imgZoom)
    gc:setColorRGB(0,0,0)
   end
  end
 end
end

function imgTools.table2img()
 local imgStr
 imgStr=imgTools.generateHeader()
 for i=1,imgHeight do
  for j=1,imgWidth do
   imgStr=imgStr..imgTools.convertPixel(imgTable[i][j])
  end
 end
 return imgTools.addSlashes(imgStr)
end





Events.lua
Code: [Select]

--EVENTS
function on.backspaceKey()
 if status=="requesting" then
  rqstr=string.sub(rqstr,1,string.len(rqstr)-1)
  platform.window:invalidate()
 end
end

function on.charIn(ch)
 if status=="drawing" then
  if ch=="+" and imgZoom<8 then
   imgZoom=imgZoom*2
  elseif ch=="-" and imgZoom>1 then
   imgZoom=imgZoom/2
  elseif ch=="d" then
   tool="draw"
  elseif ch=="e" then
   tool="erase"
  elseif ch=="N" then
   status="requesting"
   rqstr=""
   requested="Image width"
  elseif ch=="O" then
   status="requesting"
   rqstr=""
   requested="File name"
  elseif ch=="S" then
   if not imgTable then
    status="error"
    errtype="No opened file"
   else
    status="requesting"
    rqstr=imgName
    requested="Save as"
   end
  elseif ch=="s" then
   status="selectingColor"
   selColor=1
   newColor={math.floor(color[1]/5)*5,math.floor(color[2]/5)*5,math.floor(color[3]/5)*5}
  elseif ch=="h" then
   status="requesting"
   rqstr=""
   requested="Hex code"
  end
 elseif status=="requesting" and string.len(rqstr)<10 and strch("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",ch) then
  rqstr=rqstr..ch
 end
 refresh()
end

function on.escapeKey()
 if status=="drawing" then
  tool=nil
 else
  status="drawing"
 end
 refresh()
end

function on.enterKey()
 if status=="drawing" then
  if tool=="draw" then
   imgTable[ypos][xpos]={1,math.floor(color[1]/8),math.floor(color[2]/8),math.floor(color[3]/8)}
   changedMark="*"
  elseif tool=="erase" then
   imgTable[ypos][xpos][1]=0
   changedMark="*"
  end
 elseif status=="selectingColor" then
  color=newColor
  status="drawing"
 elseif status=="requesting" then
  status="drawing"
  if requested=="Hex code" then
   loadHexColor()
  elseif requested=="Image width" then
   if tonumber(rqstr) and tonumber(rqstr)~=0 then
    tempVarWidth=tonumber(rqstr)
    status="requesting"
    requested="Image height"
    rqstr=""
   else
    status="error"
    errtype="Invalid number"
   end
  elseif requested=="Image height" then
   if tonumber(rqstr) and tonumber(rqstr)~=0 then
    if tonumber(rqstr)*tempVarWidth>3000 then
     status="error"
     errtype="Overflow (3000 px max)"
    else
     newFile()
    end
   else
    status="error"
    errtype="Invalid number"
   end
  elseif requested=="File name" then
   rqstr=string.lower(rqstr)
   loadFile()
  elseif requested=="Save as" then
   rqstr=string.lower(rqstr)
   saveFile()
  end
 elseif status=="error" then
  status="drawing"
 end
 refresh()
end

function on.arrowKey(ar)
 if status=="drawing" and tool and imgTable then
  if ar=="up" and ypos>1 then ypos=ypos-1 end
  if ar=="down" and ypos<imgHeight then ypos=ypos+1 end
  if ar=="left" and xpos>1 then xpos=xpos-1 end
  if ar=="right" and xpos<imgWidth then xpos=xpos+1 end
 elseif status=="selectingColor" then
  if ar=="up" then
   selColor=selColor-1
  elseif ar=="down" then
   selColor=selColor+1
  elseif ar=="right" and newColor[selColor]<251 then
   newColor[selColor]=newColor[selColor]+5
  elseif ar=="left" and newColor[selColor]>4 then
   newColor[selColor]=newColor[selColor]-5
  end
  if selColor>3 then
   selColor=1
  elseif selColor<1 then
   selColor=3
  end
 end
 refresh()
end

Drawing.lua
Code: [Select]


--DRAWING
function drawColorSelect(gc)
 gc:setColorRGB(200,200,255)
 gc:fillRect(width()/2-75,height()/2-50,150,100)
 gc:setColorRGB(0,0,0)
 gc:fillRect(width()/2-75,height()/2-50,150,15)
 gc:setPen("thin","smooth")
 gc:drawRect(width()/2-75,height()/2-50,150,100)
 gc:setFont("sansserif","r",10)
 gc:setColorRGB(255,255,255)
 gc:drawString("Select your color",width()/2-73,height()/2-53,"top")
 gc:setColorRGB(0,0,0)
 gc:drawString("Red :",width()/2-73,height()/2-35,"top")
 gc:drawString("Green :",width()/2-73,height()/2-20,"top")
 gc:drawString("Blue :",width()/2-73,height()/2-5,"top")
 for i=0,63 do
  gc:setColorRGB(i*4,newColor[2],newColor[3])
  gc:fillRect(width()/2-23+i,height()/2-30,1,10)
  gc:setColorRGB(newColor[1],i*4,newColor[3])
  gc:fillRect(width()/2-23+i,height()/2-15,1,10)
  gc:setColorRGB(newColor[1],newColor[2],i*4)
  gc:fillRect(width()/2-23+i,height()/2,1,10)
 end
 gc:setColorRGB(0,0,0)
 gc:drawRect(width()/2-23,height()/2-46+selColor*15,64,11)
 for i=1,3 do
  gc:drawString(tostring(newColor[i]),width()/2+52,height()/2-50+i*15,"top")
  gc:fillRect(width()/2-24+newColor[i]/4,height()/2-47+i*15,3,14)
 end
 gc:setColorRGB(color[1],color[2],color[3])
 gc:fillRect(width()/2-70,height()/2+20,30,20)
 gc:setColorRGB(newColor[1],newColor[2],newColor[3])
 gc:fillRect(width()/2-28,height()/2+20,30,20)
 gc:setColorRGB(0,0,0)
 gc:drawRect(width()/2-70,height()/2+20,30,20)
 gc:drawRect(width()/2-28,height()/2+20,30,20)
 gc:drawString(mathTools.int2Hex(math.floor(newColor[1]/16))..mathTools.int2Hex(newColor[1]%16)..mathTools.int2Hex(math.floor(newColor[2]/16))..mathTools.int2Hex(newColor[2]%16)..mathTools.int2Hex(math.floor(newColor[3]/16))..mathTools.int2Hex(newColor[3]%16),width()/2+20,height()/2+20,"top")
 gc:drawString("Old     New",width()/2-65,height()/2+21,"top")
end

function drawRequest(gc,msg)
 gc:setColorRGB(200,200,255)
 gc:fillRect(width()/2-75,height()/2-25,150,50)
 gc:setColorRGB(0,0,0)
 gc:setPen("thin","smooth")
 gc:drawRect(width()/2-75,height()/2-25,150,50)
 gc:fillRect(width()/2-75,height()/2-25,150,15)
 gc:setFont("sansserif","r",10)
 gc:setColorRGB(255,255,255)
 gc:drawString(msg,width()/2-70,height()/2-28,"top")
 gc:setColorRGB(0,0,0)
 gc:drawRect(width()/2-70,height()/2,140,20)
 gc:drawString(rqstr,width()/2-65,height()/2,"top")
end

function drawError(gc)
 gc:setColorRGB(200,200,255)
 gc:fillRect(width()/2-75,height()/2-20,150,40)
 gc:setColorRGB(0,0,0)
 gc:setPen("thin","smooth")
 gc:drawRect(width()/2-75,height()/2-20,150,40)
 gc:fillRect(width()/2-75,height()/2-20,150,15)
 gc:setFont("sansserif","r",10)
 gc:setColorRGB(255,255,255)
 gc:drawString("Error",width()/2-73,height()/2-23,"top")
 gc:setColorRGB(0,0,0)
 gc:drawString(errtype,width()/2-70,height()/2-5,"top")
end

function drawCursor(gc)
 gc:setColorRGB(0,0,0)
 gc:setPen("thin","smooth")
 gc:drawRect((xpos-1)*imgZoom+29,(ypos-1)*imgZoom+29,imgZoom+1,imgZoom+1)
 gc:setColorRGB(255,255,255)
 gc:drawRect((xpos-1)*imgZoom+30,(ypos-1)*imgZoom+30,imgZoom-1,imgZoom-1)
end

function drawTool(gc)
 local id
 if not tool then
  id=1
 elseif tool=="draw" then
  id=2
 elseif tool=="erase" then
  id=3
 end
 if id then
  gc:drawImage(toolsSprites[id],0,15)
 end
end

function on.paint(gc)
 gc:setColorRGB(color[1],color[2],color[3])
 gc:fillRect(0,0,20,15)
 gc:setColorRGB(0,0,0)
 gc:setPen("thin","smooth")
 gc:drawRect(0,0,19,15)
 drawTool(gc)
 gc:setFont("sansserif","r",10)
 if imgTable then
  gc:drawString(changedMark..imgName.." - "..tostring(imgWidth).."x"..tostring(imgHeight).." ("..tostring(imgZoom*100).."%)",25,0,"top")
 else
  gc:drawString("Open or create a file",25,0,"top")
 end
 if imgTable then
  imgTools.drawTable(gc,30,30)
  if tool then
   drawCursor(gc)
  end
 end
 if status=="selectingColor" then
  drawColorSelect(gc)
 elseif status=="requesting" then
  drawRequest(gc,requested)
 elseif status=="error" then
  drawError(gc)
 end
 gc:setColorRGB(0,0,0)
 gc:setFont("sansserif","r",8)
 gc:drawString("Lua nSpaint - Par Loic Pujet",10,height()-12,"top")
end


Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: [Lua] Image Editor
« Reply #20 on: August 09, 2011, 02:38:58 pm »
I meant the Lua source code :D

Oh, sorry !
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: [Lua] Image Editor
« Reply #21 on: August 09, 2011, 02:41:14 pm »
Not a problem, Levak :D
And thanks, Chockosta!

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Image Editor
« Reply #22 on: August 10, 2011, 01:43:02 pm »
New version !

New :
-Fill tool
-Color pick tool
-Scrolling when no tool selected

And some bugs fixed...


Offline p2

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 849
  • Rating: +51/-11
  • I'm back :)
    • View Profile
Re: [Lua] Image Editor
« Reply #23 on: August 10, 2011, 01:52:14 pm »
what's about transparencity of colours?

or colours which are like small GIF-pictures (blinking like them: )  :hyper: *.*
*insert supercool signature*

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Image Editor
« Reply #24 on: August 10, 2011, 02:03:10 pm »
Transparency is impossible... A pixel can only be 0% transparent or 100%transparent (with ti.images).
By the way, when you use the erase tool, the pixel become transparent, but as the background is white, it looks like the pixel is white.

And for animations, ti.images don't support that...

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] Image Editor
« Reply #25 on: August 10, 2011, 02:04:40 pm »
Well, you can show many after each other :)
Although I fear that would be to slow (might work good for small sprites).

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] Image Editor
« Reply #26 on: August 10, 2011, 02:13:27 pm »
The editor could make an array of ti.images for an animated sprite, and have a timeline for scrolling between frames.

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Image Editor
« Reply #27 on: August 10, 2011, 02:15:20 pm »
That's a good idea !

But, I will first add basic tools (line, circle, etc)

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: [Lua] Image Editor
« Reply #28 on: August 10, 2011, 04:54:35 pm »
Would anyone be able to upload a compiled version? I can't compile since I can't find a free and working compiler other than the weird trial of Student Software.

EDIT: I mean a new compiled one, not the old one in the OP.
« Last Edit: August 10, 2011, 05:01:44 pm by BrownyTCat »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: [Lua] Image Editor
« Reply #29 on: August 10, 2011, 05:10:11 pm »
Did you try Luna?
Ndless.me with the finest TI-Nspire programs