Jump to content




Bitmap Images

api

23 replies to this topic

#1 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 04 June 2014 - 01: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

#2 skwerlman

  • Members
  • 163 posts
  • LocationPennsylvania

Posted 04 June 2014 - 09: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?

#3 sEi

  • Members
  • 41 posts

Posted 04 June 2014 - 10: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

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 June 2014 - 02: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...

#5 syfygirl

  • Members
  • 94 posts
  • LocationSomewhere in my mind, to which only makes sense to me.

Posted 05 June 2014 - 04:20 AM

View PostBomb Bloke, on 05 June 2014 - 02:47 AM, said:

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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 June 2014 - 05:42 AM

View Postsyfyguy13, on 05 June 2014 - 04:20 AM, said:

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.

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 June 2014 - 06: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.

#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 05 June 2014 - 06:58 AM

View Postskwerlman, on 04 June 2014 - 09:54 PM, said:

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.

View PostsEi, on 04 June 2014 - 10:42 PM, said:

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.

View PostBomb Bloke, on 05 June 2014 - 02:47 AM, said:

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:
Spoiler

Hmmm. Might need some work.

Edited by SquidDev, 05 June 2014 - 07:06 AM.


#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 June 2014 - 07:13 AM

View PostSquidDev, on 05 June 2014 - 06:58 AM, said:

I chose to do .BMP first because I thought it would be easiest
that and bmp files are uncompressed

#10 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 05 June 2014 - 07:21 AM

View Posttheoriginalbit, on 05 June 2014 - 07:13 AM, said:

View PostSquidDev, on 05 June 2014 - 06:58 AM, said:

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 by SquidDev, 05 June 2014 - 07:42 AM.


#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 June 2014 - 07:28 AM

View PostSquidDev, on 05 June 2014 - 07:21 AM, said:

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

#12 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 05 June 2014 - 07:41 AM

View Posttheoriginalbit, on 05 June 2014 - 07:28 AM, said:

View PostSquidDev, on 05 June 2014 - 07:21 AM, said:

-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. :)

#13 sEi

  • Members
  • 41 posts

Posted 17 June 2014 - 07: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
  • You go to an online QR generator - download the image.
  • Convert the image to .BMP (photohop, gimp...)
  • Upload image to net (Dropbox or....)
IN CC
  • 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

#14 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 June 2014 - 08: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?

#15 sEi

  • Members
  • 41 posts

Posted 17 June 2014 - 01:01 PM

View PostBomb Bloke, on 17 June 2014 - 08:38 AM, said:

(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

#16 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 June 2014 - 02:06 PM

View Postsyfyguy13, on 05 June 2014 - 04:20 AM, said:

View PostBomb Bloke, on 05 June 2014 - 02:47 AM, said:

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


#17 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2014 - 02:12 PM

View PostTheOddByte, on 17 June 2014 - 02:06 PM, said:

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")

#18 sEi

  • Members
  • 41 posts

Posted 17 June 2014 - 03:08 PM

QRcode thingy using SquidDev's ImagesAPI

Proof of concept.

What we wanna do:

Quote

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:
Posted Image
Save the image to your computer
I got this image:
Posted Image

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

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 by sEi, 17 June 2014 - 03:10 PM.


#19 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 June 2014 - 03:45 PM

View PostsEi, on 17 June 2014 - 03:08 PM, said:

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.

#20 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 June 2014 - 02:24 AM

Gave it a test under 1.63, but sorry, nothing's changed.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users