|
Title: Variable length file headers? Post by: Jack Gleeson on July 01, 2010, 04:10:05 PM So, as a sort of experiment, I'm going to try and write a new image file format but given how I'm thinking of implementing it, I was wondering is there a way to have the file header at different lengths.
For example, the design I'm going for now, would be structured like so: Bytes Description 0-4 Height in pixels of file 5-8 Width in pixels of file 9-12 First byte is a number, going up with each one of these sectors. The other 3 would have an 8-bit int for RGB values. 13-16 Some sort of data that shows that this is the end of the header. Would it be possible for me to have a number of the 9-12 byte sections? Title: Re: Variable length file headers? Post by: LemonScented on July 01, 2010, 04:45:01 PM I'm not sure I understand the question... Any file format is possible so long as it can be expressed as binary data. All you have to do is make sure that the program that reads the data can interpret it correctly.
File headers are typically of a standard size because they tend to contain a known finite amount of information, but that doesn't have to be the case. You could make the file header (and any other sections of the file you want) look something like the chunks in a RIFF file format (http://en.wikipedia.org/wiki/Resource_Interchange_File_Format)), where the size of the chunk (in this case, the file header itself) is specified as some known offset from the start of the chunk. Specifying the size of a chunk at the start is useful to code that has to read file formats, so it can know how much memory to allocate for certain things up front, and it saves the need for "footers" at the end of the chunk (bytes 13-16 in your example). Incidentally, is it intentional that you use 5 bytes to store the height of your image? The best way I've found when creating binary data formats is just to jump in and start defining one or more structs (I'm assuming you're working in C/C++ here - if not, whatever is the equivalent of a struct in your language of choice) which lays out the data for your header and whatever other chunks you might want to use. It's pretty easy then to write something that can fill out the information in the structs and save them to file, and to write something else that can read the data back out of the file into the structs so they can be used. Title: Re: Variable length file headers? Post by: Jack Gleeson on July 01, 2010, 05:04:53 PM Yeah, it was just the question about the header size being able to be of arbitrary length. Your post and that link explained it. Thanks very much, that response was much quicker than I expected.
Incidentally, is it intentional that you use 5 bytes to store the height of your image? Yes actually, that extra byte will allow me to take over the world >:D |