Writing to a binary file format. ACO

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Writing to a binary file format. ACO

Post by Muzz »

Hi guys!

I need a bit of help.

I have a simple binary file type i need to write out from love2d, http://www.adobe.com/devnet-apps/photos ... 7411_20528 ctrl+f and do a search for aco, and you will get to the fomat spec.

It's a super simple spec, but i'm having a few issues. On the irc i've been told to get the correct data types i can use ffi, which i've done.

Code: Select all

  	local ffi = require("ffi")

	ffi.cdef[[
	typedef struct {
	    double version;
	    double colcount;
	} header;

	typedef struct {
		double colourid;
		unsigned short int r;
		unsigned short int g;
		unsigned short int b;
		unsigned short int undefined;

	} color;
	]]

	colour = ffi.typeof("color")
Oh here is a simple bin of a 2 colour aco for reference.

Code: Select all

0001 0002 0000 0202 0302 2222 0000 0000
3030 4746 3d3c 0000 0002 0002 0000 0202
0302 2222 0000 0000 0009 006d 0069 0064
006e 0069 0067 0068 0074 0000 0000 3030
4746 3d3c 0000 0000 000b 0067 0072 0065
0065 006e 0020 0066 0061 0064 0065 0000
The two colours are called "midnight" (2,3,34) and "green fade"(48,71,61)

That's not everything i need but it's a start. Now my issues are, i have no idea how to convert the ffi data types to their binary counterpart and save it out as a file. It's proving to be a very difficult thing to search for.

And also of course, i can't just save a txt file with the binary data in it, (tried that) it needs to be a proper binary file which i have no idea how to save.

Any help you can give would be greatly appreciated!
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Writing to a binary file format. ACO

Post by airstruck »

Try converting the structs to strings and then writing the strings to the file (open the file in binary mode, see the manual).

The code to get a string from a struct should look something like this:

Code: Select all

local data = ffi.string(colour, ffi.sizeof(colour))
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Writing to a binary file format. ACO

Post by Muzz »

Almost there!

http://hastebin.com/ulevukemij.lua

This is the code. You can see that im concatenating the strings, and data together and that works fine. Now the last bit of the puzzle is working out why the bits seem to be exporting numbers incorrectly. At first i thought it was an endian issue, but it seems like i was wrong there.

https://drive.google.com/file/d/0B-qJDC ... sp=sharing

Here is the aco file i'm trying to replicate, and there is my attempt so far.

So for some reason it is outputting a short with a value of 2 as 0200 instead of 0002, or a value of 3 as 0300 instead of 0302
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Writing to a binary file format. ACO

Post by Nixola »

It's probably because the pc uses little endian internally.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Writing to a binary file format. ACO

Post by Muzz »

So i worked it out.

The colour format didn't care about endianess. The version and colour count did, but swapping bits with a short didn't work with bit.swap, but that was just a matter of i*256. The long ints however i could use bit.swap.

I'll post up my code when i clean it up in case it's handy for anyone else.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests