News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Convert bitmap image to ascii
Author Message
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #1
RE: Convert bitmap image to ascii

Thanks for the link to the format.
I was able to at least parse some colors from a screenshot and create
a little random line pattern generator and write a file out. Woohoo!

Can't remember the last time I 'wrote' an image to disk by code.
1993?

Anyhow here's my first xpm code before I go much it up.

Code:
cCommentsStart="/".."*"
cCommentsEnd="*".."/"
Q='"'
SPC=" "
XPMEndOfFile="};"
-- These values is temporary from Aquarius ScreenShot
XPMimageName="XPMFile.xpm"
XPMImageWidth,XPMImageHeight,XPMNumberOfImageColors,XPMCharsPerPixel = 352,232,16,1

XPMLines={}
XPMCodesHexOffset = 4 ;-- thats where the xpm hex color strings are in the XPMColorCodes table

-- XPMColorCodes HEX and color names sourced from an Mattel Aquarius 16 color Screenshot
XPMColorCodes={
{"A","Black","0","A c #101010101010"},
{"B","Red","1","B c #F0F010101010"},
{"C","Green","2","C c #1010F0F01010"},
{"D","Yellow","3","D c #F0F0E8E81010"},
{"E","Blue","4", "E c #20202020D8D8"},
{"F","Violet","5", "F c #F0F01010F0F0"},
{"G","Light Blue-Green","6", "G c #3030C0C0C0C0"},
{"H","White","7", "H c #F0F0F0F0F0F0"},
{"I","Light Gray","8", "I c #C0C0C0C0C0C0"},
{"J","Blue Green","9", "J c #2828A8A8A8A8"},
{"K","Magenta","10", "K c #C0C02020C0C0"},
{"L","Dark Blue","11", "L c #404010108888"},
{"M","Light Yellow","12", "M c #F0F0F0F07070"},
{"N","Light Green","13", "N c #2020C8C84040"},
{"O","Orange","14", "O c #A8A820202020"},
{"P","Dark Grey","15", "P c #303030303030"}
}


--
function init_XPMHeaderLines()
local XPMHexColorsHeader=""
XPMLines[1]= cCommentsStart.." XPM ".. cCommentsEnd.."\n";
XPMLines[2]="static char *"..XPMimageName.."[] = {\n";
XPMLines[3]=cCommentsStart.." width height ncolors chars_per_pixel ".. cCommentsEnd.."\n";
XPMLines[4]=Q..XPMImageWidth..SPC..XPMImageHeight..SPC..#XPMColorCodes..SPC..XPMCharsPerPixel..Q..",\n";
XPMLines[5]=cCommentsStart..SPC.."colors"..SPC..cCommentsEnd.."\n";

for i =1,#XPMColorCodes,1 do
XPMHexColorsHeader=XPMHexColorsHeader..Q..XPMColorCodes[i][XPMCodesHexOffset]..Q..",\n"
end
XPMLines[6]=XPMHexColorsHeader
XPMLines[7]= cCommentsStart..SPC.."pixels" ..SPC .. cCommentsEnd.. "\n" ;
-- OK now you are ready to write hundreds of XPMStrings that look like this
--  "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP"
end



function init_RandomXPM(w,h)
init_XPMHeaderLines()
local s=""
local sb=""
for i = 1,w,1 do
s=s..string.char(math.random(65,64+#XPMColorCodes))
end
for i =1,h,1 do
sb=sb..Q..s..Q..",\n"
end
XPMLines[#XPMLines+1]=string.sub(sb,1,-3)..XPMEndOfFile
end

function save_XPM(name)
local n = 0
local shortname=""
for i=string.len(name),1,-1 do
    if string.sub(name,i,i) == "/" then
    if string.sub(name,-4,-4) == "." then
    shortname = string.sub(name,i+1,-5)
    else
    shortname = string.sub(name,i+1,-1)
    end
    end
    if shortname~="" then break end

end
XPMimageName=shortname
init_RandomXPM(XPMImageWidth,XPMImageHeight)
local XPMDataOut=""
for i = 1,#XPMLines,1 do
        XPMDataOut= XPMDataOut..XPMLines[i]
    end

    f=io.open(name,"w")
    f:write(XPMDataOut)
    io.close(f)
end

This post was last modified: 01-13-2009 09:37 AM by iGame3D.

01-13-2009 09:31 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
Convert bitmap image to ascii - iGame3D - 01-09-2009, 11:04 AM
RE: Convert bitmap image to ascii - iGame3D - 01-13-2009 09:31 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: