Files
2026-06-04 11:42:34 +02:00

525 lines
12 KiB
C#

using System;
using System.IO;
using UnityEngine;
namespace Gif.Components
{
// Token: 0x02000003 RID: 3
public class AnimatedGifEncoder
{
// Token: 0x06000007 RID: 7 RVA: 0x000021FA File Offset: 0x000005FA
public void SetPaletteSize(int size)
{
if (size < 2 || size > 256)
{
throw new ArgumentOutOfRangeException("size", "size should be between 2 and 256");
}
this.paletteSize = size;
}
// Token: 0x06000008 RID: 8 RVA: 0x00002226 File Offset: 0x00000626
public void SetMaxSize(int bytes)
{
this.limitFileSize = bytes > 0;
this.maxSize = (long)bytes;
}
// Token: 0x06000009 RID: 9 RVA: 0x0000223B File Offset: 0x0000063B
public void SetDelay(int ms)
{
this.delay = (int)Math.Round((double)((float)ms / 10f));
}
// Token: 0x0600000A RID: 10 RVA: 0x00002253 File Offset: 0x00000653
public void SetDispose(int code)
{
if (code >= 0)
{
this.dispose = code;
}
}
// Token: 0x0600000B RID: 11 RVA: 0x00002266 File Offset: 0x00000666
public void SetRepeat(int iter)
{
if (iter >= 0)
{
this.repeat = iter;
}
}
// Token: 0x0600000C RID: 12 RVA: 0x00002279 File Offset: 0x00000679
public void SetTransparent(Color32 c)
{
this.transparent = c;
}
// Token: 0x0600000D RID: 13 RVA: 0x00002284 File Offset: 0x00000684
public bool AddFrame(Image im)
{
bool flag;
if (im == null || !this.started)
{
flag = false;
}
else
{
try
{
if (!this.sizeSet)
{
this.SetSize(im.width, im.height);
}
this.image = im;
this.GetImagePixels();
this.AnalyzePixels();
if (this.firstFrame)
{
this.WriteLSD();
this.WritePalette();
if (this.repeat >= 0)
{
this.WriteNetscapeExt();
}
}
this.WriteGraphicCtrlExt();
bool flag2 = !this.firstFrame && !this.useStaticColorPalette;
this.WriteImageDesc(flag2);
if (flag2)
{
this.WritePalette();
}
this.WritePixels();
this.firstFrame = false;
}
catch (IOException ex)
{
throw ex;
}
flag = this.WriteTemp();
}
return flag;
}
// Token: 0x0600000E RID: 14 RVA: 0x00002374 File Offset: 0x00000774
private bool WriteTemp()
{
bool flag;
if (!this.limitFileSize || this.tempStream.Length + this.bytesWritten < this.maxSize)
{
this.bytesWritten += this.tempStream.Length;
this.tempStream.WriteTo(this.gifStream);
this.tempStream.Position = 0L;
this.tempStream.SetLength(0L);
flag = true;
}
else
{
flag = false;
}
return flag;
}
// Token: 0x0600000F RID: 15 RVA: 0x00002400 File Offset: 0x00000800
public bool Finish()
{
bool flag;
if (!this.started)
{
flag = false;
}
else
{
bool flag2 = true;
this.started = false;
try
{
this.gifStream.WriteByte(59);
this.gifStream.Flush();
if (this.closeStream)
{
this.gifStream.Close();
}
}
catch (IOException ex)
{
flag2 = false;
}
this.transIndex = 0;
this.bytesWritten = 0L;
this.tempStream = null;
this.image = null;
this.pixels = null;
this.indexedPixels = null;
this.colorTab = null;
this.closeStream = false;
this.firstFrame = true;
this.nq = null;
flag = flag2;
}
return flag;
}
// Token: 0x06000010 RID: 16 RVA: 0x000024CC File Offset: 0x000008CC
public void SetFrameRate(float fps)
{
if (fps != 0f)
{
this.delay = (int)Math.Round((double)(100f / fps));
}
}
// Token: 0x06000011 RID: 17 RVA: 0x000024F0 File Offset: 0x000008F0
public void SetStaticColorPaletteMode(bool useStatic)
{
this.useStaticColorPalette = useStatic;
}
// Token: 0x06000012 RID: 18 RVA: 0x000024FA File Offset: 0x000008FA
public void SetQuality(int quality)
{
if (quality < 1)
{
quality = 1;
}
this.sample = quality;
}
// Token: 0x06000013 RID: 19 RVA: 0x00002510 File Offset: 0x00000910
public void SetSize(int w, int h)
{
if (!this.started || this.firstFrame)
{
this.width = w;
this.height = h;
if (this.width < 1)
{
this.width = 320;
}
if (this.height < 1)
{
this.height = 240;
}
this.sizeSet = true;
}
}
// Token: 0x06000014 RID: 20 RVA: 0x0000257C File Offset: 0x0000097C
public bool Start(Stream os)
{
bool flag;
if (os == null)
{
flag = false;
}
else
{
this.closeStream = false;
this.gifStream = os;
try
{
this.WriteString("GIF89a");
}
catch (IOException ex)
{
throw ex;
}
bool flag2 = this.WriteTemp();
flag = (this.started = flag2);
}
return flag;
}
// Token: 0x06000015 RID: 21 RVA: 0x000025E8 File Offset: 0x000009E8
public bool Start(string file)
{
bool flag = true;
try
{
this.gifStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
flag = this.Start(this.tempStream);
this.closeStream = true;
}
catch (IOException ex)
{
throw ex;
}
return this.started = flag;
}
// Token: 0x06000016 RID: 22 RVA: 0x00002648 File Offset: 0x00000A48
protected void AnalyzePixels()
{
int num = this.pixels.Length;
int num2 = num / 3;
this.indexedPixels = new byte[num2];
if (this.nq == null || !this.useStaticColorPalette)
{
this.nq = new NeuQuant(this.pixels, num, this.sample, this.paletteSize);
this.colorTab = this.nq.Process();
}
int num3 = 0;
for (int i = 0; i < num2; i++)
{
int num4 = this.nq.Map((int)(this.pixels[num3++] & byte.MaxValue), (int)(this.pixels[num3++] & byte.MaxValue), (int)(this.pixels[num3++] & byte.MaxValue));
this.usedEntry[num4] = true;
this.indexedPixels[i] = (byte)num4;
}
this.pixels = null;
this.colorDepth = 8;
this.palSize = 7;
if (this.transparent.Equals(default(Color32)))
{
this.transIndex = this.FindClosest(this.transparent);
}
}
// Token: 0x06000017 RID: 23 RVA: 0x00002774 File Offset: 0x00000B74
protected int FindClosest(Color32 c)
{
int num;
if (this.colorTab == null)
{
num = -1;
}
else
{
int r = (int)c.r;
int g = (int)c.g;
int b = (int)c.b;
int num2 = 0;
int num3 = 16777216;
int num4 = this.colorTab.Length;
for (int i = 0; i < num4; i++)
{
int num5 = r - (int)(this.colorTab[i++] & byte.MaxValue);
int num6 = g - (int)(this.colorTab[i++] & byte.MaxValue);
int num7 = b - (int)(this.colorTab[i] & byte.MaxValue);
int num8 = num5 * num5 + num6 * num6 + num7 * num7;
int num9 = i / 3;
if (this.usedEntry[num9] && num8 < num3)
{
num3 = num8;
num2 = num9;
}
}
num = num2;
}
return num;
}
// Token: 0x06000018 RID: 24 RVA: 0x00002860 File Offset: 0x00000C60
protected void GetImagePixels()
{
int num = this.image.width;
int num2 = this.image.height;
if (num != this.width || num2 != this.height)
{
Image image = new Image(this.width, this.height);
throw new NotImplementedException("meh");
}
this.pixels = new byte[3 * this.image.width * this.image.height];
int num3 = 0;
for (int i = 0; i < this.image.height; i++)
{
for (int j = 0; j < this.image.width; j++)
{
Color32 color = this.image[j, i];
this.pixels[num3] = color.r;
num3++;
this.pixels[num3] = color.g;
num3++;
this.pixels[num3] = color.b;
num3++;
}
}
}
// Token: 0x06000019 RID: 25 RVA: 0x00002970 File Offset: 0x00000D70
protected void WriteGraphicCtrlExt()
{
this.tempStream.WriteByte(33);
this.tempStream.WriteByte(249);
this.tempStream.WriteByte(4);
int num;
int num2;
if (this.transparent.Equals(default(Color32)))
{
num = 0;
num2 = 0;
}
else
{
num = 1;
num2 = 2;
}
if (this.dispose >= 0)
{
num2 = this.dispose & 7;
}
num2 <<= 2;
this.tempStream.WriteByte(Convert.ToByte(0 | num2 | 0 | num));
this.WriteShort(this.delay);
this.tempStream.WriteByte(Convert.ToByte(this.transIndex));
this.tempStream.WriteByte(0);
}
// Token: 0x0600001A RID: 26 RVA: 0x00002A3C File Offset: 0x00000E3C
protected void WriteImageDesc(bool localPalette)
{
this.tempStream.WriteByte(44);
this.WriteShort(0);
this.WriteShort(0);
this.WriteShort(this.width);
this.WriteShort(this.height);
if (this.firstFrame)
{
this.tempStream.WriteByte(0);
}
else
{
this.tempStream.WriteByte(Convert.ToByte(((!localPalette) ? 0 : 128) | 0 | 0 | 0 | this.palSize));
}
}
// Token: 0x0600001B RID: 27 RVA: 0x00002ACC File Offset: 0x00000ECC
protected void WriteLSD()
{
this.WriteShort(this.width);
this.WriteShort(this.height);
this.tempStream.WriteByte(Convert.ToByte(240 | this.palSize));
this.tempStream.WriteByte(0);
this.tempStream.WriteByte(0);
}
// Token: 0x0600001C RID: 28 RVA: 0x00002B28 File Offset: 0x00000F28
protected void WriteNetscapeExt()
{
this.tempStream.WriteByte(33);
this.tempStream.WriteByte(byte.MaxValue);
this.tempStream.WriteByte(11);
this.WriteString("NETSCAPE2.0");
this.tempStream.WriteByte(3);
this.tempStream.WriteByte(1);
this.WriteShort(this.repeat);
this.tempStream.WriteByte(0);
}
// Token: 0x0600001D RID: 29 RVA: 0x00002B9C File Offset: 0x00000F9C
protected void WritePalette()
{
this.tempStream.Write(this.colorTab, 0, this.colorTab.Length);
int num = 768 - this.colorTab.Length;
for (int i = 0; i < num; i++)
{
this.tempStream.WriteByte(0);
}
}
// Token: 0x0600001E RID: 30 RVA: 0x00002BF4 File Offset: 0x00000FF4
protected void WritePixels()
{
LZWEncoder lzwencoder = new LZWEncoder(this.width, this.height, this.indexedPixels, this.colorDepth);
lzwencoder.Encode(this.tempStream);
}
// Token: 0x0600001F RID: 31 RVA: 0x00002C2C File Offset: 0x0000102C
protected void WriteShort(int value)
{
this.tempStream.WriteByte(Convert.ToByte(value & 255));
this.tempStream.WriteByte(Convert.ToByte((value >> 8) & 255));
}
// Token: 0x06000020 RID: 32 RVA: 0x00002C60 File Offset: 0x00001060
protected void WriteString(string s)
{
char[] array = s.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
this.tempStream.WriteByte((byte)array[i]);
}
}
// Token: 0x04000004 RID: 4
protected int width;
// Token: 0x04000005 RID: 5
protected int height;
// Token: 0x04000006 RID: 6
protected Color32 transparent = new Color32(0, 0, 0, 0);
// Token: 0x04000007 RID: 7
protected int transIndex;
// Token: 0x04000008 RID: 8
protected int repeat = -1;
// Token: 0x04000009 RID: 9
protected int delay = 0;
// Token: 0x0400000A RID: 10
protected bool started = false;
// Token: 0x0400000B RID: 11
protected MemoryStream tempStream = new MemoryStream();
// Token: 0x0400000C RID: 12
protected Stream gifStream;
// Token: 0x0400000D RID: 13
protected int paletteSize = 256;
// Token: 0x0400000E RID: 14
protected Image image;
// Token: 0x0400000F RID: 15
protected byte[] pixels;
// Token: 0x04000010 RID: 16
protected byte[] indexedPixels;
// Token: 0x04000011 RID: 17
protected int colorDepth;
// Token: 0x04000012 RID: 18
protected byte[] colorTab;
// Token: 0x04000013 RID: 19
protected bool[] usedEntry = new bool[256];
// Token: 0x04000014 RID: 20
protected int palSize = 7;
// Token: 0x04000015 RID: 21
protected int dispose = -1;
// Token: 0x04000016 RID: 22
protected bool closeStream = false;
// Token: 0x04000017 RID: 23
protected bool firstFrame = true;
// Token: 0x04000018 RID: 24
protected bool sizeSet = false;
// Token: 0x04000019 RID: 25
protected int sample = 10;
// Token: 0x0400001A RID: 26
protected long maxSize = long.MaxValue;
// Token: 0x0400001B RID: 27
protected bool limitFileSize = false;
// Token: 0x0400001C RID: 28
protected long bytesWritten = 0L;
// Token: 0x0400001D RID: 29
protected bool useStaticColorPalette;
// Token: 0x0400001E RID: 30
private NeuQuant nq;
}
}