Discuss Scratch

ExtraCheeeeese
Scratcher
17 posts

Save Code Compression

I'm working on a vector editor;Vector Editor where you draw and it renders in lines. The problem is, the save codes are in format " ", for the potential tens of thousands of lines. This lags computers out when you copy and paste, and I'd like suggestions for how to compress it. I tried using relative positions between the start and the finish, and I got a cool 2.5D effect, but it didn't work. I don't even know why. I don't plan to build a whole base96 converting engine, so what else is there?
ExtraCheeeeese
Scratcher
17 posts

Save Code Compression

The save codes are in format “(line color) (comma) (line size) (comma) (line starting x) (comma) (line starting y) (comma) (line ending x) (comma) (line ending y)”
Used brackets, didn't know they were escape characters, corrected now
redspacecat
Scratcher
1000+ posts

Save Code Compression

ExtraCheeeeese wrote:

Used brackets, didn't know they were escape characters, corrected now
FYI, you can escape brackets by surrounding them with more brackets.
e.g.
[[]comma[]]
bitmap_caketin
Scratcher
100+ posts

Save Code Compression

What are the ranges of the codes (the minimum and maximum)?
ExtraCheeeeese
Scratcher
17 posts

Save Code Compression

ExtraCheeeeese wrote:

The save codes are in format “(line color) (comma) (line size) (comma) (line starting x) (comma) (line starting y) (comma) (line ending x) (comma) (line ending y)”

bitmap_caketin wrote:

What are the ranges of the codes (the minimum and maximum)?
So, the color and size are actually swapped in order from what I originally said. Also, the range for color is the usual for Scratch; jump by 16s, 16,000,000 or so total, although, I guess I could use that to compress it. Anyways, the size is 1 up to 11 or 12, counting by hundredths. The x's and y's can be up to infinity, but they shouldn't get over a trillion or so.

Last edited by ExtraCheeeeese (Aug. 25, 2026 19:59:49)

MonkeyBean2
Scratcher
500+ posts

Save Code Compression

I would suggest trying delta encoding again, but also trying to make a really compact base-10 only format - eg. you could have something like
<2 digit line segment count><color><3 digit start x><3 digit start y>(<dx><dy><dx><dy>…)
Depending on the average size of your deltas you could use either 1 or 2 digits for dx and dy each. Also if a particular delta is too big to encode with a single <dx><dy> you can just split it into multiple. To represent negative values, you can use a biased representation 0 encodes to the ~midpoint of your unsigned encoded value. Eg. with two digits you could encode as such: 0 -> 49, -5 -> 44, 17 -> 66.
This might get you roughly 40% size savings.

Also I would suggest maybe taking a look at the delta encoding schemes for lines in vector map tile formats, eg. https://github.com/mapbox/vector-tile-spec/blob/21ff2cbd83c241d0ecdce0c782ecc08a565e713b/2.1/README.md#43-geometry-encoding - you could try to implement some of the ideas using base 10/100 instead of binary. Or I guess you could even just do binary using hex, which is pretty easy to encode and decode in scratch.

Also, if you encode to a base which is the square of your un-encoded base, every encoded character will neatly represent two characters from your starting base, which makes encoding and decoding a bit easier as you can just process it in separated blocks that don't need any sort of carry over.

Powered by DjangoBB