-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPMRenderer.cs
executable file
·133 lines (118 loc) · 5.09 KB
/
PPMRenderer.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
//INSTANT C# NOTE: Formerly VB project-level imports:
using System;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace PPMLib
{
public static class PPMRenderer
{
public static readonly Color[] ThumbnailPalette =
{
Color.FromRgba(0xFF, 0xFF, 0xFF, 0xFF),
Color.FromRgba(0x52, 0x52, 0x52, 0xFF),
Color.FromRgba(0xFF, 0xFF, 0xFF, 0xFF),
Color.FromRgba(0x9C, 0x9C, 0x9C, 0xFF),
Color.FromRgba(0x44, 0x48, 0xFF, 0xFF),
Color.FromRgba(0x4F, 0x51, 0xC8, 0xFF),
Color.FromRgba(0xAC, 0xAD, 0xFF, 0xFF),
Color.FromRgba(0x00, 0xFF, 0x00, 0xFF),
Color.FromRgba(0xFF, 0x40, 0x48, 0xFF),
Color.FromRgba(0xB8, 0x4F, 0x51, 0xFF),
Color.FromRgba(0xFF, 0xAB, 0xAD, 0xFF),
Color.FromRgba(0x00, 0xFF, 0x00, 0xFF),
Color.FromRgba(0xB7, 0x57, 0xB6, 0xFF),
Color.FromRgba(0x00, 0xFF, 0x00, 0xFF),
Color.FromRgba(0x00, 0xFF, 0x00, 0xFF),
Color.FromRgba(0x00, 0xFF, 0x00, 0xFF)
};
// public static Image GetThumbnailImage(byte[] buffer)
// {
// if (buffer.Length != 1536)
// {
// throw new ArgumentException("Wrong thumbnail buffer size");
// }
// // Directly set bitmap's 4-bit palette instead of using 32-bit colors
// var bmp = new Image<Byte4>(64, 48)
// var palette = bmp.Palette;
// var entries = palette.Entries;
// for (var i = 0; i <= 15; i++)
// {
// entries[i] = ThumbnailPalette[i];
// }
// bmp.Palette = palette;
// var rect = new Rectangle(0, 0, 64, 48);
// var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
// byte[] bytes = new byte[(32 * 48) + 1];
// var IPtr = bmpData.Scan0;
// Marshal.Copy(IPtr, bytes, 0, 32 * 48);
// int offset = 0;
// for (int ty = 0; ty <= 47; ty += 8)
// {
// for (int tx = 0; tx <= 31; tx += 4)
// {
// for (int l = 0; l <= 7; l++)
// {
// int line = (ty + l) << 5;
// for (int px = 0; px <= 3; px++)
// {
// // Need to reverse nibbles :
// bytes[line + tx + px] = (byte)(((buffer[offset] & 0xF) << 4) | ((buffer[offset] & 0xF0) >> 4));
// offset += 1;
// }
// }
// }
// }
// Marshal.Copy(bytes, 0, IPtr, 32 * 48);
// bmp.UnlockBits(bmpData);
// return bmp;
// }
public static readonly Color[] FramePalette =
{
Color.FromRgba(0x00, 0x00, 0x00, 0xFF),
Color.FromRgba(0xFF, 0xFF, 0xFF, 0xFF),
Color.FromRgba(0xFF, 0x00, 0x00, 0xFF), //0xFFFF0000
Color.FromRgba(0x00, 0x00, 0xFF, 0xFF) //0xFF0000FF
};
public static Image GetFrameBitmap(PPMFrame frame)
{
var bmp = new Image<Rgba32>(256, 192);
for (var y = 0; y < 192; y++)
{
for (var x = 0; x < 256; x++)
{
bmp[x, y] = frame.PaperColor.ToColor();
if (frame.Layer1[x, y])
{
bmp[x, y] = frame.Layer1.PenColor == PenColor.Inverted ? frame.PaperColor.ToInvertedColor() : frame.Layer1.PenColor.ToColor();
}
if (frame.Layer2[x, y])
bmp[x, y] = frame.Layer2.PenColor == PenColor.Inverted ? frame.PaperColor.ToInvertedColor() : frame.Layer2.PenColor.ToColor();
// if (frame.Layer1[x, y])
// {
// if (frame.Layer1.PenColor != PenColor.Inverted)
// {
// bmp[x, y] = frame.Layer1.PenColor == PenColor.Red ? Color.Red : Color.Blue;
// }
// else
// {
// bmp[x, y] = frame.PaperColor == PaperColor.Black ? Color.Black : Color.White;
// }
// }
// else if (frame.Layer2[x, y])
// {
// if (frame.Layer2.PenColor != PenColor.Inverted)
// {
// bmp[x, y] = frame.Layer2.PenColor == PenColor.Red ? Color.Red : Color.Blue;
// }
// else
// {
// bmp[x, y] = frame.PaperColor == PaperColor.Black ? Color.Black : Color.White;
// }
// }
}
}
return bmp;
}
}
}