Author Topic: Sprite Compression  (Read 5459 times)

0 Members and 1 Guest are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Sprite Compression
« on: October 02, 2010, 08:31:21 pm »
So recently MRide asked for sprites for his Imagination Quest game and I helped with a way of storing them/recalling them. This got me thinking about different ways of doing this. So I thought I would show you guys what I have come up with, but if you guys have any other ways please feel free to share your methods/routines.

Note: My routines are listed according to speed, fastest to slowest (decompression), and by chance size (compressed variable) too, largest to smallest. These also should be as optimized as they can be but I'm sure someone (*cough*nemo*cough* :P) could find something I missed. Also, last thing, all "Es" and "Ls" are the scientific notation E and list token L.

Code: (List Compression 1; 124 bytes with name "LC1") [Select]
1→D
For(A,Y,Y+H-1
For(B,X,X+W-1
If pxl-Test(A,B
Then
BE6+AE4→K
Repeat not(pxl-Test(A,B
B+1→B
End
AnsE2-E2+A
If K=AnsE4
Then
Ans+.1→LP(D
Else
K+Ans→LP(D
End
D+1→D
End
End
End

Code: (List Compression 1 Decompression; 99 bytes with name "LC1D") [Select]
For(A,1,dim(LP
LP(A
If fPart(Ans
Then
.01iPart(Ans
Pxl-On(E2fPart(Ans),iPart(Ans
Else
Line(iPart(E2fPart(Ans/E8)),-iPart(E2fPart(Ans/E6)),iPart(E2fPart(Ans/E4)),-iPart(E2fPart(Ans/E2
End
End

Code: (List Compression 2; 98 bytes with name "LC2") [Select]
1→D
For(A,Y,Y+H-1
For(B,X,X+W-1
If pxl-Test(A,B
Then
BE6+AE4→K
Repeat not(pxl-Test(A,B
B+1→B
End
K+BE2-E2+A→LQ(D
D+1→D
End
End
End

Code: (List Compression 2 Decompression; 73 bytes with name "LC2D") [Select]
For(A,1,dim(LQ
LQ(A
Line(iPart(E2fPart(Ans/E8)),-iPart(E2fPart(Ans/E6)),iPart(E2fPart(Ans/E4)),-iPart(E2fPart(Ans/E2
End

Code: (String Compression 2; 193 bytes with name "SC2") [Select]
"_Str3
{0,1→L1
For(A,Y,Y+H-1
For(B,X,X+W-1
If pxl-Test(A,B
Then
BE6+AE4→K
Repeat not(pxl-Test(A,B
B+1→B
End
{0,K+BE2-E2+A→L
LineReg(ax+b) Y1
Equ►String(Y1,Str4
sub(Str4,1,length(Str4)-3→Str4
If 7=length(Str4
Str3+"0→Str3
If 6=length(Str4
Str3+"00→Str3
Str3+Str4→Str3
End
End
End
sub(Ans,2,length(Ans)-1→Str3

Code: (String Compression 2 Decompression; 86 bytes with name "SC2D") [Select]
For(A,1,length(Str3
expr(sub(Str3,A,8
Line(iPart(E2fPart(Ans/E8)),-iPart(E2fPart(Ans/E6)),iPart(E2fPart(Ans/E4)),-iPart(E2fPart(Ans/E2
A+7→A
End

Code: (String Compression 1; 239 bytes with name "SC1") [Select]
{0,1→L1
"_→Str1
For(A,Y,Y+H-1
For(B,X,X+W-1
If pxl-Test(A,b
Then
BE6+AE4→K
Repeat not(pxl-Test(A,B
B+1→B
End
BE2-E2+A→L
If K=AnsE4
Then
Str1+"+→Str1
Else
Str1+"-→Str1
End
{0,K(K≠LE4)+L→L2
LinReg(ax+b) Y1
Equ►String(Y1,Str2
sub(Str2,1,length(Str2)-3→Str2
If 2=length(Ans
Str1+"00→Str1
If max(length(Str2)={3,7
Str1+"0→Str1
Str1+Str2→Str1
End
End
End
sub(Ans,2,length(Ans)-1→Str1

Code: (String Compression 1; 141 bytes with name "SC1D") [Select]
"expr(sub(Str1,A+1,Ans→u
For(A,1,length(Str1
If sub(Str1,A,1)="+
Then
4
.01u
Pxl-On(E2fPart(Ans),iPart(Ans
A+4→A
Else
8
u
Line(iPart(E2fPart(Ans/E8)),-iPart(E2fPart(Ans/E6)),iPart(E2fPart(Ans/E4)),-iPart(E2fPart(Ans/E2
A+8→A
End
End

Note:
The way to use the compression routines is to do Width→W:Height→H:Upper-Left X-Coordinate→X:Upper-Left Y-Coordinate→Y:prgmRoutine or something like that. The decompression routines can just be run (assuming you've compressed already).

Like I said, this is open for other people to show their own routines for compressing/decompressing and sprite. I hope someone finds these useful :)
« Last Edit: October 02, 2010, 08:41:07 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Sprite Compression
« Reply #1 on: October 02, 2010, 08:38:44 pm »
AnsE2-E2+A

can't that just be Ans+A?

right... oops. for a second i thought that Ans would be less than 10.. don't know why. but hey. i'll find something else.. just give me a second.
i had to break away from my schoolwork *just* so i could find something to optimize  ;D oh, and your last String decompression code block is nested within the compression code block. these look neat, i'll check them out more thoroughly later
« Last Edit: October 02, 2010, 08:40:28 pm by nemo »


Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Sprite Compression
« Reply #2 on: October 02, 2010, 08:42:26 pm »
Looks neat.  I'm sure these are impressive.  Nice job! ;D

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Sprite Compression
« Reply #3 on: October 02, 2010, 08:44:18 pm »
Nice! Lots of compression there :)




Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Sprite Compression
« Reply #4 on: October 02, 2010, 08:46:17 pm »
Thanks guys :) That last decompression routine I think would only be good for smaller sprites. It can take a while to decompress unfortunately, but it is the smallest when compressed. (Darn you size at the risk of speed)
* Meishe91 shakes fist angrily in the air

AnsE2-E2+A

can't that just be Ans+A?

right... oops. for a second i thought that Ans would be less than 10.. don't know why. but hey. i'll find something else.. just give me a second.
i had to break away from my schoolwork *just* so i could find something to optimize  ;D oh, and your last String decompression code block is nested within the compression code block. these look neat, i'll check them out more thoroughly later

Ya, I was about to say it can't be just that. I'm sure you will :P

Also, thanks! I could not figure out why that "[/code][/code]" kept showing up at the bottom. I'm not sure how I missed that...I'm usually good about that.
« Last Edit: October 02, 2010, 08:59:16 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Sprite Compression
« Reply #5 on: October 02, 2010, 08:46:37 pm »
Code: [Select]
Line(iPart(E2fPart(Ans/E8)),-iPart(E2fPart(Ans/E6)),iPart(E2fPart(Ans/E4)),-iPart(E2fPart(Ans/E2
to..
Code: [Select]
seq(iPart(E2fPart(Ans/10^(X))),X,2,8,2  //the token 10^(
Line(Ans(4),-Ans(3),Ans(2),-Ans(1
definitely a size optimization.. you'll probably take a hit on speed.
« Last Edit: October 02, 2010, 08:52:19 pm by nemo »


Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Sprite Compression
« Reply #6 on: October 02, 2010, 08:56:42 pm »
Ah, ya. I didn't think about using seq( at all, mainly because I never really use it >.> But ya, that could work. Only thing though is with decompressions you might need to have them as fast as you can. But ya, that is an option :) Thanks.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Sprite Compression
« Reply #7 on: October 02, 2010, 09:48:15 pm »
I try not to use seq( as it's extremely slow. :)  It's really useful when you don't need speed, though. ;D

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Sprite Compression
« Reply #8 on: October 03, 2010, 04:55:39 am »
Nice stuff meishe!

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Sprite Compression
« Reply #9 on: October 03, 2010, 05:23:41 am »
Thank ya :) Took about a day to create the other three and debug them and all, then maybe an hour going through trying to optimize them as much as I could, which seemed to be good besides nemo's seq( tip (just had to find something, didn't you ;)). Then posted :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Sprite Compression
« Reply #10 on: October 03, 2010, 01:02:07 pm »
Yeah Seq is good for compression sometimes but in some cases it is not very efficient for speed. In ROL3 I had a decompression routine for loading saved files, and it took about 3 seconds to decompress 70-80 lists elements. For Illusiat 13's 100 items, it takes 6 seconds. ROL3 decompressing is hard-coded while Illusiat 13 uses seq(). ROL decompressing program was about 600-700 bytes, if I remember, though.
« Last Edit: October 03, 2010, 01:02:39 pm by DJ Omnimaga »