Repository navigation
Expand file tree
/
Copy pathMyFileType.cs
More file actions
293 lines (248 loc) · 11.9 KB
/
Copy pathMyFileType.cs
File metadata and controls
293 lines (248 loc) · 11.9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using PaintDotNet;
using PaintDotNet.ComponentModel;
using PaintDotNet.FileTypes;
using PaintDotNet.FileTypes.Samples;
using PaintDotNet.Imaging;
using PaintDotNet.IO;
using PaintDotNet.Rendering;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace PdnFileTypeSamples;
// An implementation of a simple file type with a .mine extension.
// The format is simple:
// * bytes 0 through 3: 'mine' header to identify the file type
// * bytes 4 through 7: integer width
// * bytes 8 through 11: integer height
// * bytes 12 through 27: pixel format GUID
// * bytes 28 through end: pixel values, starting at [x=0, y=0] through [x=width-1, y=0], then [x=0, y=1] through [x=width-1, y=1], etc.
public sealed class MyFileType
: FileType
{
private static ReadOnlySpan<byte> MineFileHeader => "mine"u8;
public MyFileType(IFileTypeHost host)
: base(
host,
"Mine",
FileTypeOptions.Create() with
{
LoadExtensions = [".mine"],
SaveExtensions = [".mine"]
})
{
}
protected override IFileTypeLoader OnCreateLoader()
{
return new Loader(this);
}
protected override IFileTypeSaver OnCreateSaver()
{
return new Saver(this);
}
private sealed class Loader
: FileTypeLoader
{
private readonly MyFileType fileType;
public Loader(MyFileType fileType)
: base(fileType)
{
this.fileType = fileType;
}
protected override IFileTypeDocument OnLoad(IFileTypeLoadContext context)
{
// First, read 4 bytes that identify the file type as 'mine'
Span<byte> header = stackalloc byte[4];
context.Input.ReadExactly(header);
if (!header.SequenceEqual(MineFileHeader))
{
throw new FormatException("Not a valid .mine file");
}
// Read the width and height
int width = context.Input.TryReadInt32() ?? throw new FormatException();
int height = context.Input.TryReadInt32() ?? throw new FormatException();
// Can't be negative or zero, but also place an arbitrary upper limit on the size
if (width <= 0 || width > 100_000 || height <= 0 || height > 100_000)
{
throw new FormatException();
}
// Read the pixel format. We'll use the GUIDs from PixelFormats
PixelFormat format = default;
context.Input.ReadExactly(MemoryMarshal.CreateSpan(ref format, 1).AsBytes());
// We could use one generic method that can handle any pixel format, but I want to show you to deal
// with pixel formats in a few different ways.
// First, if the pixel format is BGRA32, work with that pixel format directly
if (format == PixelFormats.Bgra32)
{
return LoadBgra32(context.Factory, context.Input, width, height);
}
// Second, utilize generics for a few other pixel formats
if (format == PixelFormats.Rgba64)
{
return Load<ColorRgba64>(context.Factory, context.Input, width, height);
}
else if (format == PixelFormats.Bgr24)
{
return Load<ColorBgr24>(context.Factory, context.Input, width, height);
}
// Lastly, handle any other formats in a generic manner but without using C# generics
return Load(context.Factory, context.Input, format, width, height);
}
// This method shows how to work with the IFileType interfaces for a specific pixel format using its
// corresponding color struct. Useful when you only care about working with one particular pixel format.
private IFileTypeDocument<ColorBgra32> LoadBgra32(IFileTypeDocumentFactory factory, Stream input, int width, int height)
{
IFileTypeDocument<ColorBgra32> document = factory.CreateDocumentBgra32(width, height);
IFileTypeBitmapLayer<ColorBgra32> layer = document.CreateBitmapLayer();
using (IFileTypeBitmapSink<ColorBgra32> layerSink = layer.GetBitmap())
using (IFileTypeBitmapLock<ColorBgra32> layerBitmapLock = layerSink.Lock())
{
RegionPtr<ColorBgra32> region = layerBitmapLock.AsRegionPtr();
for (int y = 0; y < height; ++y)
{
input.ReadExactly(region.Rows[y].Span.AsBytes());
}
}
document.Layers.Add(layer);
return document;
}
// This method shows you how to work with the IFileType interfaces in a generic manner for pixel formats
// which have a color struct available. Useful when you want to work with a variety of pixel formats, and
// have a specific list of which ones you'll support.
private IFileTypeDocument<TPixel> Load<TPixel>(IFileTypeDocumentFactory factory, Stream input, int width, int height)
where TPixel : unmanaged, INaturalPixelInfo
{
IFileTypeDocument<TPixel> document = factory.CreateDocument<TPixel>(width, height);
IFileTypeBitmapLayer<TPixel> layer = document.CreateBitmapLayer();
using (IFileTypeBitmapSink<TPixel> layerSink = layer.GetBitmap())
using (IFileTypeBitmapLock<TPixel> layerBitmapLock = layerSink.Lock())
{
RegionPtr<TPixel> region = layerBitmapLock.AsRegionPtr();
for (int y = 0; y < height; ++y)
{
input.ReadExactly(region.Rows[y].Span.AsBytes());
}
}
document.Layers.Add(layer);
return document;
}
// This method shows you how to work with the IFileType interfaces in a generic manner for any pixel format, but without
// using C# generics. Useful when you want to support any pixel format, even if there's no color struct available.
// Note that this method does not work with pixel formats that are less than 1 byte per pixel (like Indexed4), nor would
// it work on any pixel format that is not a whole number of bytes (a hypothetical 36-bits per pixel format would not work).
private unsafe IFileTypeDocument Load(IFileTypeDocumentFactory factory, Stream input, PixelFormat format, int width, int height)
{
IPixelFormatInfo formatInfo = format.GetInfo();
if ((formatInfo.BitsPerPixel % 8) != 0)
{
throw new FormatException("Only pixel formats that are comprised of whole bytes are supported");
}
int bytesPerPixel = formatInfo.BitsPerPixel / 8;
int bytesPerRow = checked(bytesPerPixel * width);
IFileTypeDocument document = factory.CreateDocument(width, height, format);
IFileTypeBitmapLayer layer = document.CreateBitmapLayer();
using (IFileTypeBitmapSink layerSink = layer.GetBitmap())
using (IFileTypeBitmapLock layerBitmapLock = layerSink.Lock())
{
for (int y = 0; y < height; ++y)
{
byte* pRow = (byte*)BitmapUtil.GetRowPointer(layerBitmapLock.Buffer, layerBitmapLock.BufferStride, y);
Span<byte> rowSpan = new Span<byte>(pRow, bytesPerRow);
input.ReadExactly(rowSpan);
}
}
document.Layers.Add(layer);
return document;
}
}
private sealed class Saver
: FileTypeSaver
{
public Saver(MyFileType fileType)
: base(fileType)
{
}
protected override void OnSave(IFileTypeSaveContext context)
{
IReadOnlyFileTypeDocument document = context.Document;
context.Output.Write(MineFileHeader);
context.Output.WriteInt32(document.Size.Width);
context.Output.WriteInt32(document.Size.Height);
context.Output.WriteValue(document.PixelFormat);
// Once again, we'll write out the document in 3 different ways:
// - Specific pixel format, showing how to bring in the color struct via the generics and component model systems
// - Using C# generics for some other pixel formats
// - Without using C# generics for all other pixel formats
if (document.PixelFormat == PixelFormats.Bgra32)
{
// We must use the component model system to "create a ref". We cannot use a standard cast.
IReadOnlyFileTypeDocument<ColorBgra32> docBgra32 = document.CreateRef<IReadOnlyFileTypeDocument<ColorBgra32>>();
WritePixelsToStreamBgra32(context.Output, docBgra32);
}
else if (document.PixelFormat == PixelFormats.Rgba64)
{
IReadOnlyFileTypeDocument<ColorRgba64> docRgba64 = document.CreateRef<IReadOnlyFileTypeDocument<ColorRgba64>>();
WritePixelsToStream(context.Output, docRgba64);
}
else if (document.PixelFormat == PixelFormats.Bgr24)
{
IReadOnlyFileTypeDocument<ColorBgr24> docBgr24 = document.CreateRef<IReadOnlyFileTypeDocument<ColorBgr24>>();
WritePixelsToStream(context.Output, docBgr24);
}
else
{
WritePixelsToStream(context.Output, document);
}
}
private void WritePixelsToStreamBgra32(Stream output, IReadOnlyFileTypeDocument<ColorBgra32> document)
{
SizeInt32 size = document.Size;
IReadOnlyFileTypeLayer<ColorBgra32> layer = document.Layers[0];
using (IFileTypeBitmapSource<ColorBgra32> layerBitmap = layer.GetBitmap())
using (IFileTypeBitmapLock<ColorBgra32> layerBitmapLock = layerBitmap.Lock())
{
RegionPtr<ColorBgra32> region = layerBitmapLock.AsRegionPtr();
for (int y = 0; y < size.Height; ++y)
{
output.Write(region.Rows[y].Span.AsBytes());
}
}
}
private void WritePixelsToStream<TPixel>(Stream output, IReadOnlyFileTypeDocument<TPixel> document)
where TPixel : unmanaged, INaturalPixelInfo
{
SizeInt32 size = document.Size;
IReadOnlyFileTypeLayer<TPixel> layer = document.Layers[0];
using (IFileTypeBitmapSource<TPixel> layerBitmap = layer.GetBitmap())
using (IFileTypeBitmapLock<TPixel> layerBitmapLock = layerBitmap.Lock())
{
RegionPtr<TPixel> region = layerBitmapLock.AsRegionPtr();
for (int y = 0; y < size.Height; ++y)
{
output.Write(region.Rows[y].Span.AsBytes());
}
}
}
private unsafe void WritePixelsToStream(Stream output, IReadOnlyFileTypeDocument document)
{
SizeInt32 size = document.Size;
IPixelFormatInfo formatInfo = document.PixelFormat.GetInfo();
if ((formatInfo.BitsPerPixel % 8) != 0)
{
throw new FormatException("Only pixel formats that are comprised of whole bytes are supported");
}
int bytesPerPixel = formatInfo.BitsPerPixel / 8;
int bytesPerRow = checked(bytesPerPixel * size.Width);
IReadOnlyFileTypeLayer layer = document.Layers[0];
using (IFileTypeBitmapSource layerBitmap = layer.GetBitmap())
using (IFileTypeBitmapLock layerBitmapLock = layerBitmap.Lock())
{
for (int y = 0; y < size.Height; ++y)
{
byte* pRow = (byte*)BitmapUtil.GetRowPointer(layerBitmapLock.Buffer, layerBitmapLock.BufferStride, y);
Span<byte> rowSpan = new Span<byte>(pRow, bytesPerRow);
output.Write(rowSpan);
}
}
}
}
}