Image storage questions

how would you guys handle image importation for roblox? I basically need a better way to locally store some image data and read from it, as in being able to get a pixels color from it (bitmap format is the image). currently what i do is just not gonna scale up. i basically save it as a json string in a module script and read from that. issue here is with no compression i can only store 100x100 per texture. i am looking for something more along the lines of atleast 4x that. now what is the main issue here? well it is the string limit of 200 thousand characters. i have thought of like having multiple strings but then im not really sure how to go about it

current method -

1 Like

I would store them as bytes rather than raw strings. Using string.char, the numbers 1-127 can be stored as a character in a string. 0 is out because it’s a null terminator, but string.char(string.byte(128)) returns 239. As inconvenient as that is, 239 is a unique value so it can be used still. Thus, by adding 1 to each half byte of data (and of course the extra step to turn 239 back into a 128 when retrieving the data) you can actually store 1 byte of input data as theoretically 1 byte of string data. If you can stomach 16 bit color instead of 32 bit, then each color can be stored in three letters. If not, then four letters and a lot more processing power.

I suggest you group it like this.
2 bytes x resolution
data scanned line-by-line based on x resolution

If you need any more help with this, let me know. I’m happy to whip up a converter for all of this.

Edit: sorry not 16 bits, but one less bit per byte. So if RGB is 24 bits, then this will be 21 bits. That’s almost not even noticeable.

1 Like

is there any other way to get to 255 integers? I really need it to be FF if possible, but i understand if not.

1 Like

It is possible but it complicates everything. Sorry for my mess of a reply last night, I was tired.
So for some reason I can only get 7 bits out of every byte of string. I’m not sure why other than that their character sets don’t seem to go over 127.

So let me make a case for 128 first. If you haven’t already figured it out, you can store images up to about 256x256 in a single string so long as each R/G/B number skips every other value i.e. 1,3,5,7, …, 253, 255. This results in a total of over two million possible colors, and they are almost indistinguishable to the human eye. Here they are right next to each other in red:

It’s a little more noticeable in a grey value where all three channels have been rounded:

If you still want the full 8-bits to a color, I’ll make you a system that can handle it. But you’ll only get 238x238 images max per string and it will be more resource intensive to convert them.

Edit: Oh this is cool. I have some good news. So it looks like StringValues (as opposed to script sources) can hold the full 256 but I haven’t finished testing those. I’m worried of course about null terminators still. I need to disappear for work for a while, I’d love to keep working on this when I get back though.

You could store arbitrary binary data as base64.

You can take your whole image, compress it with normal bitmap RLE or as a PNG, encode it in base64, then save that string. Save it as an array of substrings if you’re running into length limits.

To read it, decode the base64 (either into a byte string or an array of bytes), then decompress it.

There are definitely base64 libraries for lua you could use, and I’ve seen at least one PNG library as well that could work with minor tweaks. There are also probably bitmap/RLE libs but you could probably implement that yourself slightly easier.

Edit: I would also just check with the TOS/community rules. I’m not sure you’re allowed to show arbitrary images like this, but as long as you’re not letting users display them from URLs it’s probably fine.

so after some thinking i realized i am over thinking this, i wrote some python script which converts some numpy array to a lua array like the syntax

1 Like