Skip to content

Commit a773743

Browse files
committed
Refactor image importer
1 parent aa1c44f commit a773743

File tree

7 files changed

+335
-200
lines changed

7 files changed

+335
-200
lines changed

Runtime/AsyncImageLoader/FreeImage.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,76 @@ internal enum ColorType {
7070
FIC_CMYK = 5
7171
}
7272

73+
[System.Flags]
74+
internal enum LoadFlags {
75+
/// <summary>
76+
/// Default option for all types.
77+
/// </summary>
78+
DEFAULT = 0,
79+
/// <summary>
80+
/// Load the image as a 256 color image with ununsed palette entries, if it's 16 or 2 color.
81+
/// </summary>
82+
GIF_LOAD256 = 1,
83+
/// <summary>
84+
/// 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading.
85+
/// </summary>
86+
GIF_PLAYBACK = 2,
87+
/// <summary>
88+
/// Convert to 32bpp and create an alpha channel from the AND-mask when loading.
89+
/// </summary>
90+
ICO_MAKEALPHA = 1,
91+
/// <summary>
92+
/// Load the file as fast as possible, sacrificing some quality.
93+
/// </summary>
94+
JPEG_FAST = 0x0001,
95+
/// <summary>
96+
/// Load the file with the best quality, sacrificing some speed.
97+
/// </summary>
98+
JPEG_ACCURATE = 0x0002,
99+
/// <summary>
100+
/// Load separated CMYK "as is" (use | to combine with other load flags).
101+
/// </summary>
102+
JPEG_CMYK = 0x0004,
103+
/// <summary>
104+
/// Load and rotate according to Exif 'Orientation' tag if available.
105+
/// </summary>
106+
JPEG_EXIFROTATE = 0x0008,
107+
/// <summary>
108+
/// Load the bitmap sized 768 x 512.
109+
/// </summary>
110+
PCD_BASE = 1,
111+
/// <summary>
112+
/// Load the bitmap sized 384 x 256.
113+
/// </summary>
114+
PCD_BASEDIV4 = 2,
115+
/// <summary>
116+
/// Load the bitmap sized 192 x 128.
117+
/// </summary>
118+
PCD_BASEDIV16 = 3,
119+
/// <summary>
120+
/// Avoid gamma correction.
121+
/// </summary>
122+
PNG_IGNOREGAMMA = 1,
123+
/// <summary>
124+
/// If set the loader converts RGB555 and ARGB8888 -> RGB888.
125+
/// </summary>
126+
TARGA_LOAD_RGB888 = 1,
127+
/// <summary>
128+
/// Reads tags for separated CMYK.
129+
/// </summary>
130+
TIFF_CMYK = 0x0001,
131+
/// <summary>
132+
/// Tries to load the JPEG preview image, embedded in
133+
/// Exif Metadata or load the image as RGB 24-bit if no
134+
/// preview image is available.
135+
/// </summary>
136+
RAW_PREVIEW = 0x1,
137+
/// <summary>
138+
/// Loads the image as RGB 24-bit.
139+
/// </summary>
140+
RAW_DISPLAY = 0x2,
141+
}
142+
73143
const string FreeImageLibrary = "FreeImage";
74144

75145
[DllImport(FreeImageLibrary, EntryPoint = "FreeImage_GetFileTypeFromMemory")]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Unity.Burst;
2+
using Unity.Collections;
3+
using Unity.Jobs;
4+
using Unity.Mathematics;
5+
using static Unity.Mathematics.math;
6+
7+
public partial class AsyncImageLoader {
8+
interface IMipmapJob<TChannel> : IJobParallelFor where TChannel : struct {
9+
int2 InputSize { get; set; }
10+
int2 OutputSize { get; set; }
11+
NativeSlice<TChannel> InputChannel { get; set; }
12+
NativeSlice<TChannel> OutputChannel { get; set; }
13+
}
14+
15+
[BurstCompile(CompileSynchronously = true)]
16+
struct ByteChannelMipmapJob : IMipmapJob<byte> {
17+
public int2 InputSize { get; set; }
18+
public int2 OutputSize { get; set; }
19+
20+
public NativeSlice<byte> InputChannel { get => _inputChannel; set => _inputChannel = value; }
21+
public NativeSlice<byte> OutputChannel { get => _outputChannel; set => _outputChannel = value; }
22+
23+
[ReadOnly] NativeSlice<byte> _inputChannel;
24+
[WriteOnly] NativeSlice<byte> _outputChannel;
25+
26+
public void Execute(int outputIndex) {
27+
var outputPosition = int2(
28+
outputIndex % OutputSize.x,
29+
outputIndex / OutputSize.x
30+
);
31+
var offset = int2(0);
32+
var total = 0u;
33+
34+
for (offset.y = 0; offset.y < 2; offset.y++) {
35+
for (offset.x = 0; offset.x < 2; offset.x++) {
36+
var inputPosition = min(mad(2, outputPosition, offset), InputSize - 1);
37+
var inputIndex = mad(InputSize.x, inputPosition.y, inputPosition.x);
38+
total += _inputChannel[inputIndex];
39+
}
40+
}
41+
42+
_outputChannel[outputIndex] = (byte)(total >> 2);
43+
}
44+
}
45+
}

Runtime/AsyncImageLoader/MipmapJobs.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/AsyncImageLoader/Pixels.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public partial class AsyncImageLoader {
2+
interface IPixel<TChannel> where TChannel : struct {
3+
static int ChannelCount() => throw new System.NotImplementedException();
4+
static int ChannelByteCount() => throw new System.NotImplementedException();
5+
}
6+
7+
struct RGB24Pixel : IPixel<byte> {
8+
public static int ChannelCount() => 3;
9+
public static int ChannelByteCount() => sizeof(byte);
10+
11+
public byte r, g, b;
12+
}
13+
14+
struct RGBA32Pixel : IPixel<byte> {
15+
public static int ChannelCount() => 4;
16+
public static int ChannelByteCount() => sizeof(byte);
17+
18+
public byte r, g, b, a;
19+
}
20+
}

Runtime/AsyncImageLoader/Pixels.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)