Publish verified live restoration source snapshot and known limits

This commit is contained in:
0xcjy
2026-10-01 19:42:22 +08:00
parent 0dd0d3bdaa
commit d70a65c7ce
896 changed files with 105420 additions and 12745 deletions
+183 -33
View File
@@ -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);
}
}
}
}
}