Converts an image to ascii text. You can find the complete JTMR Img2Txt
software at the following address
http://services.worldnet.fr/elian-c/?part=Resources
-- properties
property modelMember, pasteMatrixWidth, pasteMatrixHeight
property grayTonesCharList, status
-- scripts
on beginSprite me
-- checks whether sprite(me.spriteNum).member is a valid member
if not(getOne([#Text, #Field], sprite(me.spriteNum).member.type)) then
alert "JTMR Img2Txt #1" & Return & "Use this behavior with a text or a field member."
me.status = #done
return 0
end if
-- checks whether modelMember is a valid member
if me.modelMember.type <> #Bitmap then
alert "JTMR Img2Txt #1" & Return & "Member Ref must be a valid 8bits depth bitmap"
me.status = #done
return 0
end if
if me.modelMember.depth > 8 then
alert "JTMR Img2Txt #1" & Return & "Member Ref must be a valid 8bits depth bitmap"
me.status = #done
return 0
end if
-- Initializes...
me.grayTonesCharList = [" ", ".", ",", "!", ":", ";", "@", "#"]
sprite(me.spriteNum).member.wordWrap = false
sprite(me.spriteNum).member.text = "JTMR Img2Txt #1" & Return & "wait a few seconds ..."
end beginSprite
on exitFrame me
if me.status = #done then return 0
me.modelMember.paletteRef = #grayscale
sprite(me.spriteNum).member.text = JTMRCompute(me)
me.status = #done
end exitFrame
on JTMRCompute me
myComputedStr = ""
myXPos = 1
myYPos = 1
repeat while TRUE
myPValue = []
repeat with y = myYPos to myYPos + me.pasteMatrixHeight - 1
myPValue[y-myYPos+1] = []
repeat with x = myXPos to myXPos + me.pasteMatrixWidth - 1
myPValue[y-myYPos+1][x-myXPos+1] = getPixel(me.modelMember, x, y)
end repeat
end repeat
myGrayValue = JTMRgetSumFromList(myPValue)/(myPValue[1].count * myPValue.count)
myChar = me.grayTonesCharList[getOne(myGraylist, myGrayValue)]
myComputedStr = myComputedStr & myChar
myXPos = myXPos + me.pasteMatrixWidth + 1
if myXPos > me.modelMember.width - me.pasteMatrixWidth then
myXPos = 1
myYPos = myYPos + me.pasteMatrixHeight + 1
myComputedStr = myComputedStr & return
if myYPos > me.modelMember.height - me.pasteMatrixHeight then
exit repeat
end if
end if
end repeat
return myComputedStr
end JTMRCompute
on JTMRgetSumFromList pList
if not(listP(pList)) then
return -13
end if
myListLength = pList.count
myRetValue = 0
-- 1D List
if not(listP(pList[1])) then
repeat with x = 1 to myListLength
myRetValue = myRetValue + value(pList[x])
end repeat
return myRetValue
-- 2D List
else
repeat with y = 1 to myListLength
myRetValue = myRetValue + JTMRgetSumFromList(pList[y])
end repeat
return myRetValue
end if
end JTMRgetSumFromList