This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SquidDev's profile picture

Bitmap Images

Started by SquidDev, 04 June 2014 - 12:45 PM
SquidDev #1
Posted 04 June 2014 - 02:45 PM
I know, I can't see this having any use at all but I still wrote it.

CC-Images allows you to parse any Bitmap (.bmp) file and convert it to the output you get from paint. You can also draw it to the screen or do what ever you want.

Example usage:

if not ImagesAPI then
	os.loadAPI(fs.combine(fs.getDir(shell.getRunningProgram()), "ImagesAPI"))
end

local args = {...}
if #args < 1 then
	error("ExampleUsage.lua <File>")
end

term.clear()
term.setCursorPos(1,1)

local BinFile = ImagesAPI.BinaryFile:new(args[1])
local Parser = ImagesAPI.BitmapParser:new(BinFile)

for Y, Values in pairs(Parser.Pixels) do
	for X, Col in pairs(Values) do
		term.setCursorPos(X, Y)
		term.setBackgroundColour(Col)
		term.write(" ")
	end
end

Parser:Save(args[1]..".image")

print("\n")

Here we just load the API, create a binary file from the first argument and put it though the bitmap parser. This can then be iterated through. Simples.

Download:
pastebin get Y3JeZWzV ImagesAPI
skwerlman #2
Posted 04 June 2014 - 10:54 PM
This is a really cool idea! I love when real life formats are usable in CC.

Will there be a way to convert paint format images to bitmaps?
sEi #3
Posted 04 June 2014 - 11:42 PM
Super!

I have been looking for something like this. So there IS a use for stuff like this.
I do not complain about using .BMP but a .JPG version (or .GIF) would totally save my day :)/>

I'll post again if i get my idea working with your nifty API.

/sEi
Bomb Bloke #4
Posted 05 June 2014 - 03:47 AM
I've been tempted to write a Lua GIF encoder/decoder for ages, simply for the novelty of doing it, but I likewise can't see a use for it. CC's display isn't made up of square pixels, so if you want to draw an image to use in it, odds are you're better off with the paint script.

Well, assuming that doesn't throw a "too long without yielding" error at you when you ask it to save your full-screen picture…
syfygirl #5
Posted 05 June 2014 - 05:20 AM
I've been tempted to write a Lua GIF encoder/decoder for ages, simply for the novelty of doing it, but I likewise can't see a use for it. CC's display isn't made up of square pixels, so if you want to draw an image to use in it, odds are you're better off with the paint script.

Well, assuming that doesn't throw a "too long without yielding" error at you when you ask it to save your full-screen picture…

Just use a sleep.

EX
function draw
Do stuff to picture
Sleep(.05)
End
theoriginalbit #6
Posted 05 June 2014 - 06:42 AM
Just use a sleep.

EX
function draw
Do stuff to picture
Sleep(.05)
End
there can be implications behind yielding, especially when rendering. Bomb Bloke is very aware that you can sleep, but its not always a desirable action to make.
Bomb Bloke #7
Posted 05 June 2014 - 07:16 AM
To be specific, I'm talking about when you ask the ComputerCraft-included "paint" script to save to disk. "Larger" images may crash it. Ditto for such writes in any situation, really - the issue is less in the script and more in the fs (io?) API, and difficult to resolve without butchering its efficiency.

Granted it'd be possible to break the writes down and yield between them, but I wouldn't use "sleep" specifically unless I actually "wanted" a pause.

Anyway, that's somewhat aside from the topic at hand.
SquidDev #8
Posted 05 June 2014 - 07:58 AM
This is a really cool idea! I love when real life formats are usable in CC.

Will there be a way to convert paint format images to bitmaps?
Hmmm. I was thinking about it. It shouldn't be that hard but I can imagine some elements of it being tricky. I'll have a go.

Super!

I have been looking for something like this. So there IS a use for stuff like this.
I do not complain about using .BMP but a .JPG version (or .GIF) would totally save my day :)/>

I'll post again if i get my idea working with your nifty API.

/sEi

I chose to do .BMP first because I thought it would be easiest (but so many different standards (They're not all supported so this may not always work)). I could do .GIF but animation?? I'm not sure I could cope with that. I'll probably have a look at them but as I've said, there isn't much use for this script.
I've been tempted to write a Lua GIF encoder/decoder for ages, simply for the novelty of doing it, but I likewise can't see a use for it. CC's display isn't made up of square pixels, so if you want to draw an image to use in it, odds are you're better off with the paint script.

Quite, this and the total lack of colours means images won't look great. This is the CC logo rendered using my parser:
SpoilerOn a normal computer:

On a full sized monitor:

Hmmm. Might need some work.
Edited on 05 June 2014 - 06:06 AM
theoriginalbit #9
Posted 05 June 2014 - 08:13 AM
I chose to do .BMP first because I thought it would be easiest
that and bmp files are uncompressed
SquidDev #10
Posted 05 June 2014 - 08:21 AM
I chose to do .BMP first because I thought it would be easiest
that and bmp files are uncompressed

That might also have something to do with it. :)/> Reading through the JPEG docs, this is looking less likely to happen. I might do what I normally do and find some code in Python/C#/etc… and convert it to Lua.

Oh and the entire EXIF metadata thing. *sigh*
Edited on 05 June 2014 - 06:42 AM
theoriginalbit #11
Posted 05 June 2014 - 08:28 AM
That might also have something to do with it. :)/> Reading through the JPEG docs, this is looking less likely to happen. I might do what I normally do and find some code in Python/C#/etc… and convert it to Lua.
yeah a little while ago I was looking into making a script that rendered a BMP/JPG/PNG to the OpenPeripheral Terminal Glasses. After looking at the JPG and PNG specification I decided to only do BMP, but then after seeing all the variations of BMP files I decided that I couldn't even be bothered doing that :P/>
SquidDev #12
Posted 05 June 2014 - 08:41 AM
-snip-
yeah a little while ago I was looking into making a script that rendered a BMP/JPG/PNG to the OpenPeripheral Terminal Glasses. After looking at the JPG and PNG specification I decided to only do BMP, but then after seeing all the variations of BMP files I decided that I couldn't even be bothered doing that :P/>

Quite. I've only supported BITMAPINFOHEADER because it is described as the 'most common'. It is what MS Paint produces, so its good enough for me. :)/>
sEi #13
Posted 17 June 2014 - 08:21 AM
I have made a QRcode program using this script. (Of cause i will put it here when i have cleaned up the code!!!)

OFF CC
  1. You go to an online QR generator - download the image.
  2. Convert the image to .BMP (photohop, gimp…)
  3. Upload image to net (Dropbox or….)
IN CC
  1. You run the script supplying the dropbox URL
As result you get a CC image that you can view on a computer and/or get a turtle to build the QRcode.

It works nicely but i want to clean up the code before i post it here.

So bottom line: I can see use for this script: Bitmap Images

/sEi
Bomb Bloke #14
Posted 17 June 2014 - 09:38 AM
:huh:/> I was of the impression that the http API still mangled downloaded data that wasn't in the form of basic ASCII - is that no longer the case, or have you managed to rig some workaround through DropBox?
sEi #15
Posted 17 June 2014 - 02:01 PM
(TRUNCATED)…or have you managed to rig some workaround through DropBox?

I uploaded the image to my own website and testing in SP, but i strongly assume that it works with dropbox too.

I am going to test uploading to dropbox and then test it from a multiplayer MC server.

I will post here with the result, AND paste the HOW-TO and the script here!

/sEi
TheOddByte #16
Posted 17 June 2014 - 03:06 PM
I've been tempted to write a Lua GIF encoder/decoder for ages, simply for the novelty of doing it, but I likewise can't see a use for it. CC's display isn't made up of square pixels, so if you want to draw an image to use in it, odds are you're better off with the paint script.

Well, assuming that doesn't throw a "too long without yielding" error at you when you ask it to save your full-screen picture…

Just use a sleep.

EX
function draw
Do stuff to picture
Sleep(.05)
End
And it would still be better to use os.queueEvent to yield, it's much faster

local function yield()
    os.queueEvent( "sleep" )
    os.pullEvent( "sleep" )
end
theoriginalbit #17
Posted 17 June 2014 - 03:12 PM
And it would still be better to use os.queueEvent to yield, it's much faster

local function yield()
	os.queueEvent( "sleep" )
	os.pullEvent( "sleep" )
end
if speed is what you're after then why not use coroutine.yield("sleep")
sEi #18
Posted 17 June 2014 - 04:08 PM
QRcode thingy using SquidDev's ImagesAPI

Proof of concept.

What we wanna do:
Generate a QR-code image
upload it as BMP to the web
Ingame generate a CC image using the BMP from the web

I decided to strip my develop code to show a working example.

NOTE: My script is not intended as a release but ONLY as proof of concept!.

BTW: I would be happy if someone else take it from here. Because i hate QR images and do not even have a QR scanner (smartphone). So i am not going to develop the script further! :)/>

If you either display the generated qr-image ingame as and image or build it from black and white blocks you can take a picture with your very smart smartphone and it can read the code!
——————————————————————-
How to test :
Navigate to: https://www.the-qrcode-generator.com/
Generate your QRcode
Make shure it is generated with no margin and one 'dot' equals one pixel as shown in this image:

Save the image to your computer
I got this image:


Open the image in photoshop
Crop it removing 'whitespace' so you end up with a picture like this:


Convert to greyscale
Save as .BMP (windows/8 bit)
Here is my bmp: http://dl.dropboxuse...d/qr/qrcode.bmp

Upload the image to the web

Enter Minecraft and:
Fetch a copy of SquidDev's ImagesAPI
pastebin get Y3JeZWzV ImagesAPI

Fetch my script:
pastebin get bGWZ5uvF qr

Run qr with 2 arguments:
1 = the URL to the BMP image
2 = What savename you want to use (maybe use xxxx.bmp to avoid confusion later on)
NOTE: If not supplying any arguments to qr it will use default data (see inside script).

Result:
A text file named [savename].image
——————————————————————-
Voila!

Tested in CCLite and SP. (I use ComputerCraft1.64pr2 on MC 1.7.2)

I only have access to a multiplayer server with an old CC (ComputerCraft1.58) version and the image did not download correct. But i am kind of shure it works if using latest CC build!

Please post if it works on a multiplayer server with latest CC build. I will be very happy to get feedback.

Finally use the resulting image to display or get a turtle to 'build' the picture

/sEi
Edited on 17 June 2014 - 02:10 PM
Bomb Bloke #19
Posted 17 June 2014 - 04:45 PM
I only have access to a multiplayer server with an old CC (ComputerCraft1.58) version and the image did not download correct. But i am kind of shure it works if using latest CC build!

I won't be testing it just yet, but I kinda doubt that it will… This is what I was hinting at before: the downloaded data likely has any non-ASCII characters (that is to say, any bytes with the high bit set) converted to a question mark. 0x3F or somesuch.

Thus far the only real work arounds have been to use a go-between server to present your files in base64 or somesuch.

You might be better off rigging things to simply use local image files. That way users can provide their own solutions for getting the BMPs onto their computers.
Bomb Bloke #20
Posted 18 June 2014 - 03:24 AM
Gave it a test under 1.63, but sorry, nothing's changed.
theoriginalbit #21
Posted 18 June 2014 - 03:26 AM
Gave it a test under 1.63, but sorry, nothing's changed.
of course, its a LuaJ bug. you could run it through a 3rd party service that converts the response into base64 (that's how nevercast and I got around the problem previously) but I wouldn't overly trust a 3rd party with my credentials for services like Dropbox and the like.
Bomb Bloke #22
Posted 18 June 2014 - 03:33 AM
I live in hope that one day Dan will patch what he won't update. That's all. ;)/>
sEi #23
Posted 18 June 2014 - 10:24 AM
Hmm - Ok then users have to use a CC emulator or SP to generate the image-textfile and then through pastebin get it to their server. But then its not so automagical any more. Thank you for testing.

/sEi
Bomb Bloke #24
Posted 18 June 2014 - 12:05 PM
You could get them to run their images through something like this and pastebin the result.

There may even be a PHP server somewhere which can accept URLs from a script and provide base64 versions of the results. Who knows.