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

RIF - Rahph's Image Format - Yet another image format

Started by rahph, 14 January 2018 - 11:11 AM
rahph #1
Posted 14 January 2018 - 12:15 PM
Who said image storage has to be inefficient?
While it used to, I am here to change it!
RIF can store it all! Background color, text color and the character to draw!
RIF comes with 2 storage modes:
* compressed : Extremely efficient! 2 bytes per pixel + 2 bytes for metadata separators and enough bytes to write the x and y size as ASCII numbers! Use compress and uncompress methods to use this format
* table : Exported and loaded by the API. This one has the following size signature: 39 bytes for metadata, 8 bytes line overhead and 2 bytes per pixel also 6 byte trailer.

Now that is efficient!
But how to use the API, you may ask?
Its actually very easy!

pastebin get tyrbiqRh rif.lua
And in your code,

os.loadAPI('rif.lua')

The API provides 4 methods:

image(x, y) : Returns Image object
load(uncompressedData) : Returns Image object
uncompress(compressedData) : Returns UncompressedData
compress(uncompressedData) : Returns CompressedData

Methods in the Image object:

place(x, y, character) : Returns Image object to allow call chaining
setTextColor(x, y, color) : Returns Image object to allow call chaining, note that the color has to be one from the colors API!
setBackgroundColor(x, y, color) : Returns Image object to allow call chaining, note that the color has to be one from the colors API!
export() : Returns uncompressedData
get(x, y) : Returns backgroundColor, textColor, character at the position. Colors in colors API format
getSize() : Returns x,y canvas size

For storing in files, it is recommended to compress the exported data!
More example code as well as an image viewer coming soon.

COMPRESSION BUG FIXED!
Edited on 14 January 2018 - 01:18 PM
ardera #2
Posted 14 January 2018 - 06:50 PM
Not bad, of course you're not doing any entropy compression like UCG but still :P/>

Seems like you're affected by the CC bug where writing a string to a file will corrupt it, when the string contains bytes greater than 127. At least for me the file size of a 320x213 pixels image varies between 133 and 170kb; depending on what background colors I set; that shouldn't happen I think. Maybe you should add a function that writes the data to a file in the correct way, so the user doesn't have to.

And also, it'd be really useful if you could do something like loadPaintutils(paintutilsImage) to convert a paintutils image to an RIF handle.
rahph #3
Posted 14 January 2018 - 10:39 PM
Not bad, of course you're not doing any entropy compression like UCG but still :P/>/>/> Seems like you're affected by the CC bug where writing a string to a file will corrupt it, when the string contains bytes greater than 127. At least for me the file size of a 320x213 pixels image varies between 133 and 170kb; depending on what background colors I set; that shouldn't happen I think. Maybe you should add a function that writes the data to a file in the correct way, so the user doesn't have to. And also, it'd be really useful if you could do something like loadPaintutils(paintutilsImage) to convert a paintutils image to an RIF handle.
I have only ever tested it on computercraft 1.8.0pre and higher.
I am working on a paintutils convertor, in fact a whole RIFUtils API is coming as well as RIF2 with transparency handling
Edited on 14 January 2018 - 09:40 PM