-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageWriter.cs
377 lines (341 loc) · 14.6 KB
/
ImageWriter.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System.Text;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Bmp;
using System.IO;
using System.Collections.Generic;
using System;
namespace fcfh
{
public static class ImageWriter
{
public const string MAGIC = "BMPENC";
/// <summary>
/// BMPENC file
/// </summary>
public struct ImageFile
{
/// <summary>
/// Checks if this Instance is Empty or Valid
/// </summary>
public bool IsEmpty
{
get
{
return string.IsNullOrEmpty(FileName) && (Data == null || Data.Length == 0);
}
}
/// <summary>
/// File Name
/// </summary>
public string FileName;
/// <summary>
/// File Data
/// </summary>
public byte[] Data;
}
/// <summary>
/// Provides encoding and decoding from Image Headers
/// </summary>
public static class HeaderMode
{
/// <summary>
/// Header name
/// </summary>
/// <remarks>Upper-and lowercase follow a format: http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html#Chunk-naming-conventions</remarks>
public const string DEFAULT_HEADER = "fcFh";
/// <summary>
/// Reads a byte array as PNG
/// </summary>
/// <param name="Data">Image Bytes</param>
/// <returns>Header list</returns>
public static PNGHeader[] ReadPNG(byte[] Data)
{
using (var MS = new MemoryStream(Data, false))
{
return ReadPNG(MS);
}
}
/// <summary>
/// Reads a stream as PNG
/// </summary>
/// <remarks>Stream must start with a PNG header at the current position, Stream is left open</remarks>
/// <param name="S">Stream</param>
/// <returns>Header list</returns>
public static PNGHeader[] ReadPNG(Stream S)
{
var Headers = new List<PNGHeader>();
byte[] HeaderData = new byte[8];
S.Read(HeaderData, 0, 8);
if (BitConverter.ToUInt64(HeaderData, 0) == PNGHeader.PNG_MAGIC)
{
do
{
Headers.Add(new PNGHeader(S));
} while (Headers.Last().HeaderName != "IEND");
}
return Headers.ToArray();
}
/// <summary>
/// Reads a file as PNG
/// </summary>
/// <param name="FileName">File name</param>
/// <returns>Header list</returns>
public static PNGHeader[] ReadPNG(string FileName)
{
using (var FS = File.OpenRead(FileName))
{
return ReadPNG(FS);
}
}
/// <summary>
/// Writes a collection of PNG Headers to a byte array
/// </summary>
/// <remarks>This will check for IHDR and IEND positions</remarks>
/// <param name="Headers">PNG Headers</param>
/// <returns>PNG bytes</returns>
public static byte[] WritePNG(IEnumerable<PNGHeader> Headers)
{
var Arr = Headers.ToArray();
if (Arr.Length > 1 && Arr.First().HeaderName == "IHDR" && Arr.Last().HeaderName == "IEND")
{
using (var MS = new MemoryStream())
{
MS.Write(BitConverter.GetBytes(PNGHeader.PNG_MAGIC), 0, 8);
foreach (var H in Headers)
{
H.WriteHeader(MS);
}
return MS.ToArray();
}
}
return null;
}
/// <summary>
/// Checks if the given File is a PNG
/// </summary>
/// <remarks>Only checks the 8 byte header</remarks>
/// <param name="FileName">File name</param>
/// <returns>True if PNG, false otherwise or on I/O error</returns>
public static bool IsPNG(string FileName)
{
try
{
using (var FS = File.OpenRead(FileName))
{
using (var BR = new BinaryReader(FS))
{
return BR.ReadUInt64() == PNGHeader.PNG_MAGIC;
}
}
}
catch
{
}
return false;
}
/// <summary>
/// Stores data in a PNG header
/// </summary>
/// <param name="FullFileName">Source file</param>
/// <param name="ImageFile">Existing Image file</param>
/// <param name="HeaderName">Name of Header</param>
/// <returns>PNG with custom header</returns>
/// <remarks>This process is repeatable</remarks>
public static byte[] CreateImageFromFile(string FullFileName, string ImageFile, string HeaderName = DEFAULT_HEADER)
{
using (var FS = File.OpenRead(FullFileName))
{
using (var IMG = File.OpenRead(ImageFile))
{
return CreateImageFromFile(FS, Path.GetFileName(FullFileName), IMG, HeaderName);
}
}
}
/// <summary>
/// Stores data in a PNG header
/// </summary>
/// <param name="InputFile">Source File Stream</param>
/// <param name="FileName">Source File Name (no path)</param>
/// <param name="InputImage">Source Image Stream</param>
/// <param name="HeaderName">Name of Header</param>
/// <returns>PNG with custom header</returns>
/// <remarks>This process is repeatable</remarks>
public static byte[] CreateImageFromFile(Stream InputFile, string FileName, Stream InputImage, string HeaderName = DEFAULT_HEADER)
{
var Headers = ReadPNG(InputImage).ToList();
if (Headers.Count > 0)
{
var Data = Tools.ReadAll(InputFile);
Headers.Insert(1, new PNGHeader(HeaderName,
Encoding.UTF8.GetBytes(MAGIC)
.Concat(BitConverter.GetBytes(Tools.IntToNetwork(Encoding.UTF8.GetByteCount(FileName))))
.Concat(Encoding.UTF8.GetBytes(FileName))
.Concat(BitConverter.GetBytes(Tools.IntToNetwork(Data.Length)))
.Concat(Data)
.ToArray()));
return WritePNG(Headers);
}
return null;
}
}
/// <summary>
/// Provides encoding and decoding from Pixel Data
/// </summary>
public static class PixelMode
{
/// <summary>
/// Bytes for each Pixel. In 24bpp Mode this is 24/8=3
/// </summary>
private const int BYTES_PER_PIXEL = 24 / 8;
/// <summary>
/// Saves binary Data as an Image
/// </summary>
/// <param name="FullFileName">Source File Name to process</param>
/// <param name="PNG">Use PNG instead of BMP</param>
/// <param name="AllowDirectDecode">if true, Data is stored so it appears in Order when viewed as BMP</param>
/// <returns>Image Data</returns>
public static byte[] CreateImageFromFile(string FullFileName, bool PNG = false, bool AllowDirectDecode = false)
{
using (var FS = File.OpenRead(FullFileName))
{
return CreateImageFromFile(FS, Path.GetFileName(FullFileName), PNG, AllowDirectDecode);
}
}
/// <summary>
/// Saves binary Data as an Image
/// </summary>
/// <param name="Input">Source Content</param>
/// <param name="FileName">File Name to store</param>
/// <param name="PNG">Use PNG instead of BMP</param>
/// <param name="AllowDirectDecode">if true, Data is stored so it appears in Order when viewed as BMP</param>
/// <returns>Image Data</returns>
public static byte[] CreateImageFromFile(Stream Input, string FileName, bool PNG = false, bool AllowDirectDecode = false)
{
if (Input == null)
{
throw new ArgumentNullException(nameof(Input));
}
byte[] AllData = Tools.ReadAll(Input);
byte[] Data =
// Header
Encoding.UTF8.GetBytes(MAGIC)
// File name length (big-endian)
.Concat(BitConverter.GetBytes(Tools.IntToNetwork(Encoding.UTF8.GetByteCount(FileName))))
// File name
.Concat(Encoding.UTF8.GetBytes(FileName))
// Data length (big-endian)
.Concat(BitConverter.GetBytes(Tools.IntToNetwork(AllData.Length)))
// Data
.Concat(AllData)
// Make array
.ToArray();
var W = (int)Math.Sqrt(Data.Length / BYTES_PER_PIXEL);
// Width must be a multiple of 4
W -= W % 4;
var H = (int)Math.Ceiling(Data.Length / (double)BYTES_PER_PIXEL / W);
using (var image = new SixLabors.ImageSharp.Image<Rgb24>(W, H))
{
byte[] DataArray = Data; // Ensure consistent naming
int dataLength = DataArray.Length;
int maxDataLength = W * H * BYTES_PER_PIXEL;
// Ensure we don't exceed the image capacity
if (dataLength > maxDataLength)
{
throw new ArgumentException("Data is too large to fit in the generated image.");
}
// Write pixel data using ProcessPixelRows
image.ProcessPixelRows(accessor =>
{
int dataIndex = 0;
for (int y = 0; y < accessor.Height; y++)
{
Span<Rgb24> pixelRow = accessor.GetRowSpan(y);
for (int x = 0; x < accessor.Width; x++)
{
if (dataIndex + 2 >= DataArray.Length)
break; // Prevent overflow if Data.Length exceeds pixel capacity
byte r = DataArray[dataIndex++];
byte g = (dataIndex < DataArray.Length) ? DataArray[dataIndex++] : (byte)0;
byte b = (dataIndex < DataArray.Length) ? DataArray[dataIndex++] : (byte)0;
pixelRow[x] = new Rgb24(r, g, b);
}
if (dataIndex >= DataArray.Length)
break;
}
});
using (var ms = new MemoryStream())
{
if (PNG)
{
image.Save(ms, new PngEncoder());
}
else
{
image.Save(ms, new BmpEncoder());
}
return ms.ToArray();
}
}
}
/// <summary>
/// Extracts a File From an Image
/// </summary>
/// <param name="Input"></param>
/// <returns></returns>
public static ImageFile CreateFileFromImage(Stream Input, bool AllowDirectDecode = false)
{
using (var image = Image.Load<Rgb24>(Input))
{
// Calculate the total number of bytes to copy
int totalBytes = image.Width * image.Height * BYTES_PER_PIXEL; // 3 bytes per pixel
byte[] Data = new byte[totalBytes];
// Copy pixel data into the byte array
image.CopyPixelDataTo(Data);
// Check MAGIC
string magic = Encoding.UTF8.GetString(Data, 0, MAGIC.Length);
if (magic != MAGIC)
{
if (!AllowDirectDecode)
{
// Data might be in reverse order if BMP was used
Array.Reverse(Data);
magic = Encoding.UTF8.GetString(Data, 0, MAGIC.Length);
if (magic != MAGIC)
{
return new ImageFile(); // Returns default with nulls
}
}
else
{
return new ImageFile(); // Returns default with nulls
}
}
// Parse the encoded data
try
{
int offset = MAGIC.Length;
int fileNameLength = Tools.IntToHost(BitConverter.ToInt32(Data, offset));
offset += 4;
string FileName = Encoding.UTF8.GetString(Data, offset, fileNameLength);
offset += fileNameLength;
int dataLength = Tools.IntToHost(BitConverter.ToInt32(Data, offset));
offset += 4;
byte[] fileData = new byte[dataLength];
Array.Copy(Data, offset, fileData, 0, dataLength);
return new ImageFile
{
FileName = FileName,
Data = fileData
};
}
catch
{
return new ImageFile(); // Returns default with nulls
}
}
}
}
}
}