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

CBM - ComputerCraft BitMap file format

Started by faubiguy, 16 October 2012 - 10:12 PM
faubiguy #1
Posted 17 October 2012 - 12:12 AM
This is an api for reading and writing image files from my CBM (ComputerCraft Bit Map) file format. It is designed to be a universal image format for ComputerCraft, and is smaller than any uncompressed format (I may add a compressed format in the future if it isn't too difficult). The functions now support animation.

It saves a binary bitmap of the file, given to it in the format of a table of rows, each of which is a table of pixels in that row, each pixel being a number from 0 to 15 ,or -1. 0-15 represents the color with that hex value, and -1 indicates transparency. It can also load files in this same table format. The table can also contain color tags (like &7 or &c) for compatibility with Marumaru's color tags API.

If you find any bugs or errors, please report them here.

Functions:
saveCBM(filename, imageData, hasTransparency)
filename is the path to where you want to save the file, imageData is the table containing the image as described above, and hasTransparency marks whether to save transparency data. If it is set to false or not given, it won't save the optional bit mask to decrease file size. If the table contains a field "repetitions", it will set the number of repetitions to this number (1-255, 0 for infinite). If there is a field called interval, it will set the interval in ticks to this value (1-255, 0 is reserved).

loadCBM(filename, useTags, noAnimation)
filename is the path where you would like to load the file from. If useTags is true, it will load as a table of colortags, for compatibility with the color tags API. If the file cannot be successfully loaded, it will return nil and an error message. Unless the noAnimation flag is true, it will return the image as a table of frames, even if the is only one frame. If it is true, it will only return the first frame. The table will also contain repetitions, and interval. This is the number of repetitions for the image (1-255 or math.huge), and the interval in ticks between each frame.

The file format is:
0x0-0x2: The bytes corresponding to the ASCII characters "CBM".
0x3-0x6: The height in pixels, big-endian.
0x7-0xA: The width in pixels, big-endian.
0xB: Flags about the image. The first is a flag stating whether the image contains a bit mask for transparency information. If the flag is incorrect, the image will fail to load. The second states whether the image has animation or a single image. Flags 3-8 are reserved for futute versions of CBM.
0xC: If the animation flag is set to true, store the number of repetitions. 0 means to loop forever.
0xD: If the animation flag is set to true, stores the interval (in ticks) between frames.

Following this is a sequential list of pixels, 2 per byte, as the color value, 0-15, plus an extra half-byte padding if the number is odd.
If the transparency flag is true, following this is a bit mask for transparency, 8 pixels per byte, with enough padding at the end to bring it to a whole number of bytes. 1's represent transparency

If an animation has multiple frams, they will follow this immediately in the same format. (Only the image data and optional bitmask, not header information such as width, height, and flags).

Link: http://pastebin.com/69W2E4Qd

You are free to use the code directly in a program (so image editors can use it without needing an external API) as long as you give me credit.
faubiguy #2
Posted 20 October 2012 - 07:49 PM
Animation support added.
faubiguy #3
Posted 21 October 2012 - 05:44 AM
Converter from NFP to CBM: http://pastebin.com/d7hypaMW

Requires CBM api to be saved at /CBM
wilcomega #4
Posted 24 October 2012 - 05:34 PM
suggestion: add pics. everyone wants pics
faubiguy #5
Posted 24 October 2012 - 05:48 PM
Pics of what? Its a file format made of binary data. Nothing to take a picture of. I could take a screenshot of an image, but it could just as well be saved in any format.