Murga-Projects Forums
Convert bitmap image to ascii - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: Convert bitmap image to ascii (/showthread.php?tid=359)


Convert bitmap image to ascii - iGame3D - 01-09-2009 11:04 AM

Heya, I need to convert 256 images to a individual strings of 8x8 ones and zeroes.
Is this doable in murgaLua.

I found a pc program that does the job, but its going to be about 2100
mouseclicks so I'd rather try to program it and just rip a folder
of images and clean up after my mess.

Any ideas?


RE: Convert bitmap image to ascii - Juergen - 01-09-2009 01:07 PM

You may want to use convert from ImageMagick.

For example "convert -resize 8x8 -extent 8x8 -monochrome -compress none ifile.png ofile.pbm" converts any ifile.png to an ASCII ofile.pbm. If you cut the first two lines you have what you want.

If you want to do it with 256 images you can use a simple shell script like:

Code:
for image in $(ls *.png); do convert -resize 8x8 -extent 8x8 -monochrome -compress none $image $(basename $image .png).pbm ; done


Should convert all png pictures in the directory.

Juergen


RE: Convert bitmap image to ascii - iGame3D - 01-10-2009 10:08 PM

Hey thanks.
Couldn't do ImageMagick as I need a new OS so I purchased Graphic Converter for $35 and it did the magick I needed to do.

So I got this 1983 Custom Ascii Character set batch converted to
256 individual 3D objects, dropped into a game level and blinking
different colors and shades.
Yay! 11,000 + polys of wonder.
Flash Movie of that here!

And then I parsed a screenshot of the old computer running in emulation.
Here's the console output at like a 2 point font size.


Having trouble mapping that back to the old computer though.
I'll have to clean up my code and post it.

So now, FLTK has some kind of embedded graphic format yes?
Where does the strange data for those images come from?
I've seen it in some scripts and it just confuses me.

So last year I was working on a little graphics creator for the old computer
in murgaLua and had planned to do more with it, but things happened.
Using pngs I was not able to deal with colors and the mouse is
drawing in the window even when you are choosing characters.

You can get the binaries and scripts in a zipped file, as long as see sample images in this directory

I'm pretty much aiming at a universal grid based mapping tool.
Of course I'm straying form universal all the time.
Right now its a world of zombies mapper. muahaha.

Anyway thanks in advance for any ideas or infos about such things.


RE: Convert bitmap image to ascii - mikshaw - 01-13-2009 12:01 AM

Quote:
So now, FLTK has some kind of embedded graphic format yes?
Where does the strange data for those images come from?
I've seen it in some scripts and it just confuses me.

It can use embedded pixmap (xpm) and bitmap (xbm) data, stored as Fl_Pixmap and Fl_Bitmap objects. I don't understand the bitmap data structure, probably less than you do. The xpm format is simple text, and it translates to FLTK pretty easily.
See here for the XPM image format:
http://www.fileformat.info/format/xpm/
An example of its use in FLTK:
http://seriss.com/people/erco/fltk/#DraggableBoxes
In MurgaLua I've noticed that the comments, including /* XPM */, must be removed from xpm data.

The 2-color thing would probably be better served with Fl_Bitmap, but sorry I just haven't figured out what format the data must be.

I'm pretty sure there are other ways of creating and manipulating embedded image data in FLTK, but that's all I know.


RE: Convert bitmap image to ascii - iGame3D - 01-13-2009 09:31 AM

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




RE: Convert bitmap image to ascii - Juergen - 01-13-2009 11:03 AM

iGame3D Wrote:
Hey thanks.
Couldn't do ImageMagick as I need a new OS so I purchased Graphic Converter for $35 and it did the magick I needed to do.

So I got this 1983 Custom Ascii Character set batch converted to
256 individual 3D objects, dropped into a game level and blinking
different colors and shades.
Yay! 11,000 + polys of wonder.
Flash Movie of that here!

I don't get that. Do you want to say that you can't run the binary release, because you have an older OSX release? Then you can still install it via macports (macports) or you can run linux in a virtual machine.

With imagemagick it definitely works:

Code:
for image in $(ls *.png); do convert -resize 8x8 -extent 8x8 -monochrome -compress none $image ${image%.png}.pbm ; done

This should also work on OSX. The only problem is that the font uses shadows and I'm not 100% sure that you can scale it down to 8x8 that easy. A little bit of tuning is still necessary and maybe it may be necessary to optimize a few chars by hand.

Quote:
So now, FLTK has some kind of embedded graphic format yes?
Where does the strange data for those images come from?
I've seen it in some scripts and it just confuses me.

So last year I was working on a little graphics creator for the old computer
in murgaLua and had planned to do more with it, but things happened.
Using pngs I was not able to deal with colors and the mouse is
drawing in the window even when you are choosing characters.


Of course this should also work with murgaLua. I can get it working with gifs and xpms, but not with pngs. When I try the data() method on the fltk image buffer I get a string of 1024 nil. While I did get a 1024 character string (16x16=256*4=1024 for rgba), there was no data in there. Maybe there is a bug somewhere or data() doesn't currently support RGB buffers.

You can test it with gifs for examle:
convert a png to gif like

Code:
convert aquarius_chr_006.png aquarius_chr_006.gif


The start murgaLua

Code:
> fltk.fl_register_images()
> my_image=Fl_Shared_Image.get(./"aquarius_chr_006.gif")
> my_image_data=my_image:data()
> table.foreach(my_image_data,print)
1       16 16 -2 1
2        
3                      
4                 !!    
5                 !!    
6                 !!    
7                   !!  
8                   !!  
9       !!!!!!!!!!!!!!!!
10      !!!!!!!!!!!!!!!!
11      !!!!!!!!!!!!!!!!
12      !!!!!!!!!!!!!!!!
13                  !!  
14                  !!  
15                !!    
16                !!    
17                !!    
18                      
d       1
h       16
w       16
ld      0


VoilĂ  tout!

Juergen


RE: Convert bitmap image to ascii - iGame3D - 01-14-2009 04:08 AM

No go on the ImageMagick install.
After endless time spent trying its a dead end.
I followed directions here

After frustration of using that install attempt I read the posts, nobody had success.
Finally a post from three days ago says install Xcode 3.1.2 which is OS X10.5 only.

I've got Graphic Converter, it does the job.
That final bit of code you posted is great.
Access to the data so fast!
Doesn't do much on black and white PBM but its great on the XPM.
Thanks.


RE: Convert bitmap image to ascii - jyf1987 - 01-16-2009 11:47 PM

i think the lua-gd lib will be more helpful than imageMagick


RE: Convert bitmap image to ascii - iGame3D - 01-17-2009 11:29 AM

Well here's the progress. I was able to convert the binary bits of an 1982 bitmap custom font into 3D objects with no effort, once the code was done of course.
Looks like this:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquamovie.swf

Here are some downloads.
MAC OS X version:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquaMac.zip

Windows Version:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquaWin.zip

You'll need to move your mouse down a bit to orient yourself to the 'floor'
of Aquarius Characters.

Keyboard works like a video game:
W = forward
S = back
A = slide left
D = slide right
R = rise
F = fall

Command F on the Mac will toggle fullscreen.
Maximize the window on Windows witht he usual control.

Command or Control L will exit into the editor, the mouse may be locked
to the camera, a second press of those keys should unlock it.

The editor is an adventure in undocumented discovery.
A ton of lua scripts and fluid files in there for exploration
if you are into reading code and stuff.

I've also been able to do some screenshot conversion that translates back into ye olde BASIC statements that POKE the characters back into the ancient computer.
That needs some work and will have to wait until I'm at a stand still in zombie game making to continue.

In all its been a cool experiment that murgaLua has made possible.
Eventually I'll pull together some kind of BASIC emulation in Lua,
so that you can play some really old school games in your console!
There is nothing like driving a pound sign down the dangerous curving
roads of asterisk alley.

Is there a wait to get input in console in Lua?
Like "you are in a dungeon there are exits north and west, what will you do?
And then the program waits for input like "N" or "W" before continuing.
I could and probably should do in a gui I suppose.


RE: Convert bitmap image to ascii - mikshaw - 01-17-2009 02:22 PM

Quote:
Is there a wait to get input in console in Lua?
Like "you are in a dungeon there are exits north and west, what will you do?
And then the program waits for input like "N" or "W" before continuing.

my_variable=io.read()
if string.lower(my_variable)=="n" then print("north") end


RE: Convert bitmap image to ascii - jyf1987 - 01-17-2009 04:27 PM

iGame3D Wrote:
Well here's the progress. I was able to convert the binary bits of an 1982 bitmap custom font into 3D objects with no effort, once the code was done of course.
Looks like this:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquamovie.swf

Here are some downloads.
MAC OS X version:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquaMac.zip

Windows Version:
http://aquarius.residenteasel.com/aquarius2009/ig3dAquaWin.zip

You'll need to move your mouse down a bit to orient yourself to the 'floor'
of Aquarius Characters.

Keyboard works like a video game:
W = forward
S = back
A = slide left
D = slide right
R = rise
F = fall

Command F on the Mac will toggle fullscreen.
Maximize the window on Windows witht he usual control.

Command or Control L will exit into the editor, the mouse may be locked
to the camera, a second press of those keys should unlock it.

The editor is an adventure in undocumented discovery.
A ton of lua scripts and fluid files in there for exploration
if you are into reading code and stuff.

I've also been able to do some screenshot conversion that translates back into ye olde BASIC statements that POKE the characters back into the ancient computer.
That needs some work and will have to wait until I'm at a stand still in zombie game making to continue.

In all its been a cool experiment that murgaLua has made possible.
Eventually I'll pull together some kind of BASIC emulation in Lua,
so that you can play some really old school games in your console!
There is nothing like driving a pound sign down the dangerous curving
roads of asterisk alley.

Is there a wait to get input in console in Lua?
Like "you are in a dungeon there are exits north and west, what will you do?
And then the program waits for input like "N" or "W" before continuing.
I could and probably should do in a gui I suppose.


unforturnally,the link was banned by the GFW at my location,:[


RE: Convert bitmap image to ascii - jpjacobs - 01-17-2009 08:30 PM

iGame3D Wrote:
Is there a wait to get input in console in Lua?
Like "you are in a dungeon there are exits north and west, what will you do?
And then the program waits for input like "N" or "W" before continuing.
I could and probably should do in a gui I suppose.


I've been searching for this stuff too, but apparently it isn't possible in console, as io.read always waits for a newline, and you can't change the input buffering behaviour.
So this would mean you would need to write some small routine in C to accomplish the job. I've been trying too, but i did not succeed. Let me know if you do get it right Wink.
In gui it's easy to just put some zerospace hidden buttons with hotkeys binded to them...


Anyhow, for the old adventure game lovers, there still is http://www.nethack.org (for all common platforms) Wink.

Greetz,
Jan-Pieter


RE: Convert bitmap image to ascii - iGame3D - 01-18-2009 04:20 PM

As an experiment I created particle emitters of the
3D from 8-bit characters that that I posted yesterday.
Then I started a little BASIC interpreter.

Here's a flash movie

oh and here's a youtube video of the real deal


I'm thinking I have to take a step way back and
figure out how to build a solid interpreter,
No clue really how to do that, I'm just winging it.

Having too much fun. :-)

Mr Bill