Publish verified live restoration source snapshot and known limits
This commit is contained in:
@@ -14,6 +14,7 @@ namespace uGIF
|
||||
public static CaptureToGIFCustom Instance;
|
||||
public List<Image> Frames = new List<Image>();
|
||||
public bool stop = false;
|
||||
public bool transparentBackground = false;
|
||||
|
||||
[System.NonSerialized]
|
||||
public byte[] bytes = null;
|
||||
@@ -55,32 +56,56 @@ namespace uGIF
|
||||
UmaViewerUI.Instance.ScreenshotSettings.GifButton.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = "Record GIF";
|
||||
}
|
||||
|
||||
public IEnumerator _Encode (float fps, int quality)
|
||||
public IEnumerator _Encode(float fps, int quality)
|
||||
{
|
||||
var ge = new GIFEncoder ();
|
||||
ge.useGlobalColorTable = true;
|
||||
ge.repeat = 0;
|
||||
ge.FPS = fps;
|
||||
ge.quality = quality;
|
||||
ge.transparent = new Color32 (0, 0, 0, 0);
|
||||
ge.dispose = 2;
|
||||
var ge = new GIFEncoder();
|
||||
|
||||
var stream = new MemoryStream ();
|
||||
ge.Start (stream);
|
||||
while (!stop || Frames.Count > 0)
|
||||
{
|
||||
if(Frames.Count>0 && Frames[0] != null)
|
||||
// 每帧使用自己的调色板,避免后续彩色帧变灰或偏色。
|
||||
ge.useGlobalColorTable = false;
|
||||
ge.repeat = 0;
|
||||
ge.FPS = Mathf.Max(1f, fps);
|
||||
ge.quality = Mathf.Clamp(quality, 1, 30);
|
||||
|
||||
if (transparentBackground)
|
||||
{
|
||||
// 这个颜色值只是通知编码器启用透明。
|
||||
// 实际透明判断由像素 Alpha 完成。
|
||||
ge.transparent = new Color32(0, 0, 0, 0);
|
||||
|
||||
// 恢复到透明背景,适合完整帧 GIF。
|
||||
ge.dispose = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
ge.transparent = null;
|
||||
ge.dispose = 0;
|
||||
}
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
ge.Start(stream);
|
||||
|
||||
while (!stop || Frames.Count > 0)
|
||||
{
|
||||
Frames[0].Flip();
|
||||
ge.AddFrame(Frames[0]);
|
||||
Frames.RemoveAt(0);
|
||||
if (Frames.Count > 0)
|
||||
{
|
||||
Image frame = Frames[0];
|
||||
Frames.RemoveAt(0);
|
||||
|
||||
if (frame != null)
|
||||
{
|
||||
// Flip is fused into the Burst quantization/mapping jobs, avoiding
|
||||
// an additional full-frame copy before every encoded frame.
|
||||
ge.AddFrame(frame, true);
|
||||
}
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
yield return 0;
|
||||
}
|
||||
ge.Finish ();
|
||||
bytes = stream.GetBuffer ();
|
||||
stream.Close ();
|
||||
yield break;
|
||||
|
||||
ge.Finish();
|
||||
bytes = stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,16 @@ namespace uGIF
|
||||
|
||||
public float FPS {
|
||||
set {
|
||||
delay = Mathf.RoundToInt (100f / value);
|
||||
delay = Mathf.Max(1, Mathf.RoundToInt(100f / Mathf.Max(1f, value)));
|
||||
}
|
||||
}
|
||||
|
||||
public void AddFrame (Image im)
|
||||
{
|
||||
AddFrame(im, false);
|
||||
}
|
||||
|
||||
public void AddFrame (Image im, bool flipVertical)
|
||||
{
|
||||
if (im == null)
|
||||
throw new ArgumentNullException ("im");
|
||||
@@ -33,7 +38,7 @@ namespace uGIF
|
||||
}
|
||||
|
||||
pixels = im.pixels;
|
||||
RemapPixels (); // build color table & map pixels
|
||||
RemapPixels (im.width, im.height, flipVertical); // build color table & map pixels
|
||||
pixels = null;
|
||||
if (firstFrame) {
|
||||
WriteLSD (); // logical screen descriptior
|
||||
@@ -82,66 +87,59 @@ namespace uGIF
|
||||
WriteString ("GIF89a"); // header
|
||||
}
|
||||
|
||||
void RemapPixels ()
|
||||
void RemapPixels(int imageWidth, int imageHeight, bool flipVertical)
|
||||
{
|
||||
int len = pixels.Length;
|
||||
indexedPixels = new byte[len];
|
||||
bool mappedDuringTraining = false;
|
||||
|
||||
if (firstFrame || !useGlobalColorTable) {
|
||||
// initialize quantizer
|
||||
nq = new NeuQuant (pixels, len, quality);
|
||||
colorTab = nq.Process (); // create reduced palette
|
||||
if (firstFrame || !useGlobalColorTable)
|
||||
{
|
||||
nq = new NeuQuant(pixels, len, quality, imageWidth, imageHeight, flipVertical);
|
||||
nq.ProcessAndMap(
|
||||
transparent.HasValue,
|
||||
255,
|
||||
out colorTab,
|
||||
out indexedPixels);
|
||||
mappedDuringTraining = true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
int index = nq.Map (pixels [i].r & 0xff, pixels [i].g & 0xff, pixels [i].b & 0xff);
|
||||
usedEntry [index] = true;
|
||||
indexedPixels [i] = (byte)index;
|
||||
|
||||
if (dispose == 1 && prevIndexedPixels != null) {
|
||||
if (indexedPixels [i] == prevIndexedPixels [i]) {
|
||||
indexedPixels [i] = (byte)transIndex;
|
||||
} else {
|
||||
prevIndexedPixels [i] = (byte)index;
|
||||
}
|
||||
}
|
||||
}
|
||||
colorDepth = 8;
|
||||
palSize = 7;
|
||||
// get closest match to transparent color if specified
|
||||
if (transparent.HasValue) {
|
||||
var c = transparent.Value;
|
||||
//transIndex = FindClosest(transparent);
|
||||
transIndex = nq.Map (c.b, c.g, c.r);
|
||||
|
||||
bool useTransparency = transparent.HasValue;
|
||||
|
||||
// 固定保留调色板最后一个索引作为透明色。
|
||||
transIndex = 255;
|
||||
|
||||
if (!mappedDuringTraining)
|
||||
{
|
||||
indexedPixels = nq.MapPixels(
|
||||
pixels,
|
||||
colorTab,
|
||||
useTransparency,
|
||||
transIndex,
|
||||
imageWidth,
|
||||
imageHeight,
|
||||
flipVertical);
|
||||
}
|
||||
|
||||
if (useTransparency && colorTab != null)
|
||||
{
|
||||
int paletteOffset = transIndex * 3;
|
||||
|
||||
if (paletteOffset + 2 < colorTab.Length)
|
||||
{
|
||||
// 透明索引对应的 RGB 实际不会显示。
|
||||
colorTab[paletteOffset] = 0;
|
||||
colorTab[paletteOffset + 1] = 0;
|
||||
colorTab[paletteOffset + 2] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (dispose == 1 && prevIndexedPixels == null)
|
||||
prevIndexedPixels = indexedPixels.Clone () as byte[];
|
||||
}
|
||||
|
||||
int FindClosest (Color32 c)
|
||||
{
|
||||
if (colorTab == null)
|
||||
return -1;
|
||||
int r = c.r;
|
||||
int g = c.g;
|
||||
int b = c.b;
|
||||
int minpos = 0;
|
||||
int dmin = 256 * 256 * 256;
|
||||
int len = colorTab.Length;
|
||||
for (int i = 0; i < len;) {
|
||||
int dr = r - (colorTab [i++] & 0xff);
|
||||
int dg = g - (colorTab [i++] & 0xff);
|
||||
int db = b - (colorTab [i] & 0xff);
|
||||
int d = dr * dr + dg * dg + db * db;
|
||||
int index = i / 3;
|
||||
if (usedEntry [index] && (d < dmin)) {
|
||||
dmin = d;
|
||||
minpos = index;
|
||||
}
|
||||
i++;
|
||||
{
|
||||
prevIndexedPixels = indexedPixels.Clone() as byte[];
|
||||
}
|
||||
return minpos;
|
||||
}
|
||||
|
||||
void WriteGraphicCtrlExt ()
|
||||
@@ -180,9 +178,14 @@ namespace uGIF
|
||||
WriteShort (0);
|
||||
WriteShort (width); // image size
|
||||
WriteShort (height);
|
||||
// no LCT - GCT is used for first (or only) frame
|
||||
ms.WriteByte (0);
|
||||
|
||||
if (!firstFrame && !useGlobalColorTable) {
|
||||
// bit 7 = Local Color Table 存在;palSize=7 表示 256 色。
|
||||
ms.WriteByte(Convert.ToByte(0x80 | palSize));
|
||||
} else {
|
||||
// 第一帧使用 Logical Screen Descriptor 中的 Global Color Table。
|
||||
ms.WriteByte(0);
|
||||
}
|
||||
}
|
||||
|
||||
void WriteLSD ()
|
||||
@@ -252,10 +255,9 @@ namespace uGIF
|
||||
byte[] prevIndexedPixels;
|
||||
int colorDepth;
|
||||
byte[] colorTab;
|
||||
bool[] usedEntry = new bool[256]; // active palette entries
|
||||
int palSize = 7; // color table size (bits-1)
|
||||
bool firstFrame = true;
|
||||
NeuQuant nq;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+183
-33
@@ -1,5 +1,7 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
|
||||
namespace uGIF
|
||||
{
|
||||
@@ -43,16 +45,22 @@ namespace uGIF
|
||||
|
||||
public void Flip ()
|
||||
{
|
||||
for (var y = 0; y < height/2; y++) {
|
||||
for (var x = 0; x < width; x++) {
|
||||
var top = y * width + x;
|
||||
var bottom = (height - y - 1) * width + x;
|
||||
var temp = pixels [top];
|
||||
pixels [top] = pixels [bottom];
|
||||
pixels [bottom] = temp;
|
||||
}
|
||||
if (pixels == null || pixels.Length == 0 || width <= 0 || height <= 1)
|
||||
return;
|
||||
|
||||
using (var source = new NativeArray<Color32>(pixels, Allocator.TempJob))
|
||||
using (var destination = new NativeArray<Color32>(pixels.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory))
|
||||
{
|
||||
new FlipVerticalJob
|
||||
{
|
||||
Source = source,
|
||||
Destination = destination,
|
||||
Width = width,
|
||||
Height = height
|
||||
}.Schedule(pixels.Length, 256).Complete();
|
||||
|
||||
destination.CopyTo(pixels);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Resize (int scale)
|
||||
@@ -61,12 +69,25 @@ namespace uGIF
|
||||
return;
|
||||
var newWidth = width / scale;
|
||||
var newHeight = height / scale;
|
||||
if (newWidth <= 0 || newHeight <= 0)
|
||||
return;
|
||||
|
||||
var newColors = new Color32[newWidth * newHeight];
|
||||
for (var y=0; y<newHeight; y++) {
|
||||
for (var x=0; x<newWidth; x++) {
|
||||
newColors [(y * newWidth) + x] = pixels [(y * scale) * width + (x * scale)];
|
||||
}
|
||||
using (var source = new NativeArray<Color32>(pixels, Allocator.TempJob))
|
||||
using (var destination = new NativeArray<Color32>(newColors.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory))
|
||||
{
|
||||
new PointResizeJob
|
||||
{
|
||||
Source = source,
|
||||
Destination = destination,
|
||||
SourceWidth = width,
|
||||
DestinationWidth = newWidth,
|
||||
Scale = scale
|
||||
}.Schedule(newColors.Length, 256).Complete();
|
||||
|
||||
destination.CopyTo(newColors);
|
||||
}
|
||||
|
||||
pixels = newColors;
|
||||
height = newHeight;
|
||||
width = newWidth;
|
||||
@@ -76,35 +97,164 @@ namespace uGIF
|
||||
{
|
||||
if (newWidth == width && newHeight == height)
|
||||
return;
|
||||
if (newWidth <= 0 || newHeight <= 0 || width <= 0 || height <= 0)
|
||||
return;
|
||||
if (width == 1 || height == 1)
|
||||
{
|
||||
ResizeNearest(newWidth, newHeight);
|
||||
return;
|
||||
}
|
||||
|
||||
var texColors = pixels;
|
||||
var newColors = new Color32[newWidth * newHeight];
|
||||
var ratioX = 1.0f / ((float)newWidth / (width - 1));
|
||||
var ratioY = 1.0f / ((float)newHeight / (height - 1));
|
||||
var w = width;
|
||||
var w2 = newWidth;
|
||||
|
||||
for (var y = 0; y < newHeight; y++) {
|
||||
var yFloor = Mathf.FloorToInt (y * ratioY);
|
||||
var y1 = yFloor * w;
|
||||
var y2 = (yFloor + 1) * w;
|
||||
var yw = y * w2;
|
||||
|
||||
for (var x = 0; x < w2; x++) {
|
||||
int xFloor = (int)Mathf.Floor (x * ratioX);
|
||||
var xLerp = x * ratioX - xFloor;
|
||||
newColors [yw + x] = ColorLerpUnclamped (ColorLerpUnclamped (texColors [y1 + xFloor], texColors [y1 + xFloor + 1], xLerp), ColorLerpUnclamped (texColors [y2 + xFloor], texColors [y2 + xFloor + 1], xLerp), y * ratioY - yFloor);
|
||||
}
|
||||
using (var source = new NativeArray<Color32>(texColors, Allocator.TempJob))
|
||||
using (var destination = new NativeArray<Color32>(newColors.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory))
|
||||
{
|
||||
new BilinearResizeJob
|
||||
{
|
||||
Source = source,
|
||||
Destination = destination,
|
||||
SourceWidth = width,
|
||||
SourceHeight = height,
|
||||
DestinationWidth = newWidth,
|
||||
RatioX = ratioX,
|
||||
RatioY = ratioY
|
||||
}.Schedule(newColors.Length, 256).Complete();
|
||||
|
||||
destination.CopyTo(newColors);
|
||||
}
|
||||
|
||||
pixels = newColors;
|
||||
height = newHeight;
|
||||
width = newWidth;
|
||||
|
||||
}
|
||||
|
||||
Color32 ColorLerpUnclamped (Color A, Color B, float P)
|
||||
|
||||
private void ResizeNearest(int newWidth, int newHeight)
|
||||
{
|
||||
var newColors = new Color32[newWidth * newHeight];
|
||||
using (var source = new NativeArray<Color32>(pixels, Allocator.TempJob))
|
||||
using (var destination = new NativeArray<Color32>(newColors.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory))
|
||||
{
|
||||
new NearestResizeJob
|
||||
{
|
||||
Source = source,
|
||||
Destination = destination,
|
||||
SourceWidth = width,
|
||||
SourceHeight = height,
|
||||
DestinationWidth = newWidth,
|
||||
DestinationHeight = newHeight
|
||||
}.Schedule(newColors.Length, 256).Complete();
|
||||
|
||||
return new Color (A.r + (B.r - A.r) * P, A.g + (B.g - A.g) * P, A.b + (B.b - A.b) * P, A.a + (B.a - A.a) * P);
|
||||
destination.CopyTo(newColors);
|
||||
}
|
||||
|
||||
pixels = newColors;
|
||||
width = newWidth;
|
||||
height = newHeight;
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
private struct FlipVerticalJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeArray<Color32> Source;
|
||||
[WriteOnly] public NativeArray<Color32> Destination;
|
||||
public int Width;
|
||||
public int Height;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
int y = index / Width;
|
||||
int x = index - y * Width;
|
||||
Destination[index] = Source[(Height - 1 - y) * Width + x];
|
||||
}
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
private struct PointResizeJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeArray<Color32> Source;
|
||||
[WriteOnly] public NativeArray<Color32> Destination;
|
||||
public int SourceWidth;
|
||||
public int DestinationWidth;
|
||||
public int Scale;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
int y = index / DestinationWidth;
|
||||
int x = index - y * DestinationWidth;
|
||||
Destination[index] = Source[(y * Scale) * SourceWidth + x * Scale];
|
||||
}
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
private struct NearestResizeJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeArray<Color32> Source;
|
||||
[WriteOnly] public NativeArray<Color32> Destination;
|
||||
public int SourceWidth;
|
||||
public int SourceHeight;
|
||||
public int DestinationWidth;
|
||||
public int DestinationHeight;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
int y = index / DestinationWidth;
|
||||
int x = index - y * DestinationWidth;
|
||||
int sourceX = x * SourceWidth / DestinationWidth;
|
||||
int sourceY = y * SourceHeight / DestinationHeight;
|
||||
Destination[index] = Source[sourceY * SourceWidth + sourceX];
|
||||
}
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
private struct BilinearResizeJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeArray<Color32> Source;
|
||||
[WriteOnly] public NativeArray<Color32> Destination;
|
||||
public int SourceWidth;
|
||||
public int SourceHeight;
|
||||
public int DestinationWidth;
|
||||
public float RatioX;
|
||||
public float RatioY;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
int y = index / DestinationWidth;
|
||||
int x = index - y * DestinationWidth;
|
||||
float sourceX = x * RatioX;
|
||||
float sourceY = y * RatioY;
|
||||
int xFloor = (int)sourceX;
|
||||
int yFloor = (int)sourceY;
|
||||
|
||||
if (xFloor >= SourceWidth - 1)
|
||||
xFloor = SourceWidth - 2;
|
||||
if (yFloor >= SourceHeight - 1)
|
||||
yFloor = SourceHeight - 2;
|
||||
|
||||
float xLerp = sourceX - xFloor;
|
||||
float yLerp = sourceY - yFloor;
|
||||
int row0 = yFloor * SourceWidth;
|
||||
int row1 = (yFloor + 1) * SourceWidth;
|
||||
|
||||
Color32 top = LerpColor32(Source[row0 + xFloor], Source[row0 + xFloor + 1], xLerp);
|
||||
Color32 bottom = LerpColor32(Source[row1 + xFloor], Source[row1 + xFloor + 1], xLerp);
|
||||
Destination[index] = LerpColor32(top, bottom, yLerp);
|
||||
}
|
||||
|
||||
private static Color32 LerpColor32(Color32 a, Color32 b, float t)
|
||||
{
|
||||
Color colorA = a;
|
||||
Color colorB = b;
|
||||
return new Color(
|
||||
colorA.r + (colorB.r - colorA.r) * t,
|
||||
colorA.g + (colorB.g - colorA.g) * t,
|
||||
colorA.b + (colorB.b - colorA.b) * t,
|
||||
colorA.a + (colorB.a - colorA.a) * t);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+645
-407
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user