Initial commit

This commit is contained in:
AbstractConcept 2022-09-13 00:36:34 -05:00
commit 3c7cc0c973
8391 changed files with 704313 additions and 0 deletions

View file

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using PDNWrapper;
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
namespace UnityEditor.U2D.PSD
{
class ExtractLayerTask
{
struct ConvertBufferJob : IJobParallelFor
{
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<IntPtr> original;
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<int> width;
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<int> height;
[DeallocateOnJobCompletion]
public NativeArray<IntPtr> output;
public unsafe void Execute(int index)
{
Color32* originalColor = (Color32*)original[index];
Color32* otuputColor = (Color32*)output[index];
for (int i = 0; i < height[index]; ++i)
{
int originalYOffset = i * width[index];
int outputYOffset = (height[index] - i - 1) * width[index];
for (int j = 0; j < width[index]; ++j)
{
otuputColor[j + outputYOffset] = originalColor[j + originalYOffset];
}
}
}
}
public static unsafe void Execute(List<PSDLayer> extractedLayer, List<BitmapLayer> layers, bool importHiddenLayer)
{
UnityEngine.Profiling.Profiler.BeginSample("ExtractLayer_PrepareJob");
var tempExtractLayer = new List<PSDLayer>();
int layerWithBuffer = ExtractLayer(tempExtractLayer, layers, importHiddenLayer);
if (layerWithBuffer == 0)
return;
var job = new ConvertBufferJob();
job.original = new NativeArray<IntPtr>(layerWithBuffer, Allocator.TempJob);
job.output = new NativeArray<IntPtr>(layerWithBuffer, Allocator.TempJob);
job.width = new NativeArray<int>(layerWithBuffer, Allocator.TempJob);
job.height = new NativeArray<int>(layerWithBuffer, Allocator.TempJob);
for (int i = 0, jobIndex = 0; i < tempExtractLayer.Count; ++i)
{
var el = tempExtractLayer[i];
if (el.texture.Length == 0 || el.width == 0 || el.height == 0)
{
extractedLayer.Add(el);
continue;
}
job.original[jobIndex] = new IntPtr(el.texture.GetUnsafeReadOnlyPtr());
el.texture = new NativeArray<Color32>(el.texture.Length, Allocator.Persistent);
extractedLayer.Add(el);
job.output[jobIndex] = new IntPtr(el.texture.GetUnsafePtr());
job.width[jobIndex] = el.width;
job.height[jobIndex] = el.height;
++jobIndex;
}
var jobsPerThread = layerWithBuffer / (SystemInfo.processorCount == 0 ? 8 : SystemInfo.processorCount);
jobsPerThread = Mathf.Max(jobsPerThread, 1);
var handle = job.Schedule(layerWithBuffer, jobsPerThread);
UnityEngine.Profiling.Profiler.EndSample();
handle.Complete();
}
static int ExtractLayer(List<PSDLayer> extractedLayer, List<BitmapLayer> layers, bool importHiddenLayer)
{
// parent is the previous element in extracedLayer
int parentGroupIndex = extractedLayer.Count - 1;
int actualLayerWithBuffer = 0;
foreach (var l in layers)
{
if (!importHiddenLayer && !l.Visible)
continue;
if (l.IsGroup)
{
extractedLayer.Add(new PSDLayer(new NativeArray<Color32>(0, Allocator.Persistent), parentGroupIndex, l.IsGroup, l.Name, 0, 0, l.LayerID));
actualLayerWithBuffer += ExtractLayer(extractedLayer, l.ChildLayer, importHiddenLayer);
}
else
{
extractedLayer.Add(new PSDLayer(l.Surface.color, parentGroupIndex, l.IsGroup, l.Name, l.Surface.width, l.Surface.height, l.LayerID));
++actualLayerWithBuffer;
}
}
return actualLayerWithBuffer;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 16af8882f4d64f641925549e4262327c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using PDNWrapper;
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
namespace UnityEditor.U2D.PSD
{
static class FlattenImageTask
{
static unsafe public void Execute(List<BitmapLayer> layer, bool importHiddenLayer, int width, int height, NativeArray<Color32> output)
{
UnityEngine.Profiling.Profiler.BeginSample("FlattenImage");
List<IntPtr> buffers = new List<IntPtr>();
for (int i = layer.Count - 1; i >= 0; --i)
{
GetBuffersToMergeFromLayer(layer[i], importHiddenLayer, buffers);
}
if (buffers.Count == 0)
return;
var layersPerJob = buffers.Count / (SystemInfo.processorCount == 0 ? 8 : SystemInfo.processorCount);
layersPerJob = Mathf.Max(layersPerJob, 1);
var job = new FlattenImageInternalJob();
var combineJob = new FlattenImageInternalJob();
job.buffers = new NativeArray<IntPtr>(buffers.ToArray(), Allocator.TempJob);
for (int i = 0; i < buffers.Count; ++i)
job.buffers[i] = buffers[i];
combineJob.width = job.width = width;
combineJob.height = job.height = height;
job.layersPerJob = layersPerJob;
job.flipY = false;
combineJob.flipY = true;
int jobCount = buffers.Count / layersPerJob + (buffers.Count % layersPerJob > 0 ? 1 : 0);
combineJob.layersPerJob = jobCount;
NativeArray<byte>[] premergedBuffer = new NativeArray<byte>[jobCount];
job.output = new NativeArray<IntPtr>(jobCount, Allocator.TempJob);
combineJob.buffers = new NativeArray<IntPtr>(jobCount, Allocator.TempJob);
for (int i = 0; i < jobCount; ++i)
{
premergedBuffer[i] = new NativeArray<byte>(width * height * 4, Allocator.TempJob);
job.output[i] = new IntPtr(premergedBuffer[i].GetUnsafePtr());
combineJob.buffers[i] = new IntPtr(premergedBuffer[i].GetUnsafeReadOnlyPtr());
}
combineJob.output = new NativeArray<IntPtr>(new[] {new IntPtr(output.GetUnsafePtr()), }, Allocator.TempJob);
var handle = job.Schedule(jobCount, 1);
combineJob.Schedule(1, 1, handle).Complete();
foreach (var b in premergedBuffer)
{
if (b.IsCreated)
b.Dispose();
}
UnityEngine.Profiling.Profiler.EndSample();
}
static unsafe void GetBuffersToMergeFromLayer(BitmapLayer layer, bool importHiddenLayer, List<IntPtr> buffers)
{
if (!layer.Visible && importHiddenLayer == false)
return;
if (layer.IsGroup)
{
for (int i = layer.ChildLayer.Count - 1; i >= 0; --i)
GetBuffersToMergeFromLayer(layer.ChildLayer[i], importHiddenLayer, buffers);
}
if (layer.Surface != null)
buffers.Add(new IntPtr(layer.Surface.color.GetUnsafeReadOnlyPtr()));
else
Debug.LogWarning(string.Format("Layer {0} has no color buffer", layer.Name));
}
struct FlattenImageInternalJob : IJobParallelFor
{
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<IntPtr> buffers;
[ReadOnly]
public int layersPerJob;
[ReadOnly]
public int width;
[ReadOnly]
public int height;
[ReadOnly]
public bool flipY;
[DeallocateOnJobCompletion]
public NativeArray<IntPtr> output;
public unsafe void Execute(int index)
{
var premerge = (Color32*)output[index].ToPointer();
for (int layerIndex = index * layersPerJob; layerIndex < (index * layersPerJob) + layersPerJob; ++layerIndex)
{
if (buffers.Length <= layerIndex)
break;
var buffer = (Color32*)buffers[layerIndex].ToPointer();
for (int i = 0; i < height; ++i)
{
int sourceYIndex = i * width;
int destYIndex = flipY ? (height - 1 - i) * width : sourceYIndex;
for (int j = 0; j < width; ++j)
{
int sourceIndex = sourceYIndex + j;
int destIndex = destYIndex + j;
float alpha = buffer[sourceIndex].a / 255.0f;
premerge[destIndex].r = (byte)(alpha * (float)(buffer[sourceIndex].r) + (float)((1.0f - alpha) * (float)premerge[destIndex].r));
premerge[destIndex].g = (byte)(alpha * (float)(buffer[sourceIndex].g) + (float)((1.0f - alpha) * (float)premerge[destIndex].g));
premerge[destIndex].b = (byte)(alpha * (float)(buffer[sourceIndex].b) + (float)((1.0f - alpha) * (float)premerge[destIndex].b));
premerge[destIndex].a = (byte)(alpha * (float)(buffer[sourceIndex].a) + (float)((1.0f - alpha) * (float)premerge[destIndex].a));
}
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 91006214ab79ae444b7ae4f0e018beb8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: