-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQoiFileType.cs
143 lines (116 loc) · 5.24 KB
/
QoiFileType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using PaintDotNet;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
using PaintDotNet.Rendering;
using System;
using System.Collections.Generic;
using System.IO;
namespace QoiFileTypeNet {
public enum BitDepth {
AutoDetect,
Depth32Bit,
Depth24Bit
}
[PluginSupportInfo(typeof(PluginSupportInfo))]
public sealed class QoiFileType : PropertyBasedFileType {
public QoiFileType() :
base("Quite OK Image (QOI)", new FileTypeOptions() {
LoadExtensions = new string[] { ".qoi" },
SaveExtensions = new string[] { ".qoi" },
SupportsCancellation = true,
SupportsLayers = false
})
{
}
public override PropertyCollection OnCreateSavePropertyCollection() {
List<Property> props = new List<Property>
{
StaticListChoiceProperty.CreateForEnum(ConstantStrings.BitDepthString, BitDepth.AutoDetect),
new UriProperty(ConstantStrings.GitHubLinkString, new Uri(ConstantStrings.GitHubLinkValue))
};
return new PropertyCollection(props);
}
public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props) {
ControlInfo info = CreateDefaultSaveConfigUI(props);
PropertyControlInfo bitDepthPCI = info.FindControlForPropertyName(ConstantStrings.BitDepthString);
bitDepthPCI.SetValueDisplayName(BitDepth.AutoDetect, "Auto Detect");
bitDepthPCI.SetValueDisplayName(BitDepth.Depth32Bit, "32 Bits");
bitDepthPCI.SetValueDisplayName(BitDepth.Depth24Bit, "24 Bits");
PropertyControlInfo githubLinkPCI = info.FindControlForPropertyName(ConstantStrings.GitHubLinkString);
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = "Author info";
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = "GitHub";
return info;
}
protected override Document OnLoad(Stream input) {
Document document = null;
QoiDesc desc = new QoiDesc();
QoiRgba[] pixels = QoiFile.Load(input, ref desc);
if (pixels.Length > 0) {
document = new Document(desc.width, desc.height);
BitmapLayer layer = Layer.CreateBackgroundLayer(desc.width, desc.height);
Surface surface = layer.Surface;
unsafe {
int offset = 0;
for (int y = 0; y < surface.Height; ++y) {
ColorBgra* dst = surface.GetRowPointerUnchecked(y);
for (int x = 0; x < surface.Width; ++x, ++offset) {
dst->R = pixels[offset].r;
dst->G = pixels[offset].g;
dst->B = pixels[offset].b;
dst->A = pixels[offset].a;
++dst;
}
}
}
document.Layers.Add(layer);
}
return document;
}
protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback) {
scratchSurface.Clear();
input.CreateRenderer().Render(scratchSurface);
int srcWidth = scratchSurface.Width;
int srcHeight = scratchSurface.Height;
int numPixels = srcWidth * srcHeight;
BitDepth bitDepth = (BitDepth)token.GetProperty(ConstantStrings.BitDepthString).Value;
bool writeAlpha = (bitDepth == BitDepth.AutoDetect) ?
this.DetectIfSurfaceHasAlpha(scratchSurface) :
(bitDepth == BitDepth.Depth32Bit);
QoiRgba[] pixels = new QoiRgba[numPixels];
unsafe {
int offset = 0;
for (int y = 0; y < srcHeight; ++y) {
ColorBgra* src = scratchSurface.GetRowPointerUnchecked(y);
for (int x = 0; x < srcWidth; ++x, ++offset) {
pixels[offset].r = src->R;
pixels[offset].g = src->G;
pixels[offset].b = src->B;
pixels[offset].a = writeAlpha ? src->A : (byte)255;
++src;
}
}
}
QoiDesc desc = new QoiDesc {
width = srcWidth,
height = srcHeight,
channels = writeAlpha ? 4 : 3,
colorspace = QoiDesc.QOI_SRGB
};
QoiFile.Save(output, ref desc, pixels);
}
private bool DetectIfSurfaceHasAlpha(Surface scratchSurface) {
unsafe {
for (int y = 0; y < scratchSurface.Height; ++y) {
ColorBgra* src = scratchSurface.GetRowPointerUnchecked(y);
for (int x = 0; x < scratchSurface.Width; ++x) {
if (src->A != 255) {
return true;
}
++src;
}
}
}
return false;
}
}
}