-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapSaver.cs
More file actions
274 lines (239 loc) · 9.39 KB
/
BitmapSaver.cs
File metadata and controls
274 lines (239 loc) · 9.39 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
namespace ForestPlot
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using Marshal = System.Runtime.InteropServices.Marshal;
using StringBuilder = System.Text.StringBuilder;
public static class BitmapSaver
{
private const int Opaque = unchecked((int)0xff000000);
private static readonly Encoder SaveAsCMYK = new Encoder(new Guid("a219bbc9-0a9d-4005-a3ee-3a421b8bb06c"));
private static readonly Guid TiffFormat = new Guid("557cf405-1a04-11d3-9a73-0000f81ef32e");
private static readonly char[] semicolon = { ';' };
private static readonly char[] asterisk = { '*' };
public static void SaveRgb32(Bitmap rgb32, string filename, bool optimize, bool cmyk)
{
if (rgb32 == null)
{
throw new ArgumentNullException("rgb32");
}
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException("filename");
}
ImageCodecInfo codec = null;
var extensions = new StringBuilder();
foreach (var c in ImageCodecInfo.GetImageEncoders())
{
foreach (var e in c.FilenameExtension.Split(semicolon, StringSplitOptions.RemoveEmptyEntries))
{
if (extensions.Length != 0)
{
extensions.Append(';');
}
extensions.Append(e);
if (filename.EndsWith(e.TrimStart(asterisk), StringComparison.OrdinalIgnoreCase))
{
codec = c;
break;
}
}
}
if (codec == null)
{
throw new InvalidOperationException(
"Unrecognized file extension. Supported file types: " + extensions.ToString());
}
SaveRgb32(rgb32, filename, optimize, cmyk, codec);
}
public static void SaveRgb32(Bitmap rgb32, string filename, bool optimize, bool cmyk, ImageCodecInfo codec)
{
if (rgb32 == null)
{
throw new ArgumentNullException("rgb32");
}
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException("filename");
}
bool saved = false;
if (cmyk && codec.Clsid == TiffFormat)
{
try
{
using (var codecParameters = new EncoderParameters())
{
codecParameters.Param = new EncoderParameter[]
{ new EncoderParameter(SaveAsCMYK, (long)1) };
rgb32.Save(filename, codec, codecParameters);
saved = true;
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
throw new InvalidOperationException("Unable to save CMYK on this computer: " + ex.Message);
}
}
if (!saved && optimize)
{
try
{
if (optimize)
{
try
{
using (var bmpOptimized = Optimize(rgb32))
{
if (bmpOptimized != null)
{
bmpOptimized.Save(filename, codec, null);
saved = true;
}
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
}
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
}
}
if (!saved)
{
rgb32.Save(filename, codec, null);
saved = true;
}
return;
}
private static Bitmap Optimize(Bitmap rgb32)
{
Bitmap bmpOpt = null;
var colorToIndex = new Dictionary<int, int>(256);
var data32 = rgb32.LockBits(
new Rectangle(0, 0, rgb32.Width, rgb32.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);
IntPtr p32;
int[] b32 = new int[data32.Stride / sizeof(int)];
p32 = data32.Scan0;
for (int line = 0; line < data32.Height; line++)
{
Marshal.Copy(p32, b32, 0, b32.Length);
for (int i = 0; i < b32.Length; i++)
{
int rgb = b32[i] & 0xffffff;
colorToIndex[rgb] = 0;
}
p32 = (IntPtr)((long)p32 + data32.Stride);
}
if (colorToIndex.Count == 2 &&
colorToIndex.ContainsKey(0) &&
colorToIndex.ContainsKey(0xffffff))
{
var bmp1 = new Bitmap(rgb32.Width, rgb32.Height, PixelFormat.Format1bppIndexed);
bmp1.SetResolution(rgb32.HorizontalResolution, rgb32.VerticalResolution);
var data1 = bmp1.LockBits(
new Rectangle(0, 0, bmp1.Width, bmp1.Height),
ImageLockMode.WriteOnly,
PixelFormat.Format1bppIndexed);
IntPtr p1 = data1.Scan0;
byte[] b1 = new byte[data1.Stride];
p32 = data32.Scan0;
for (int line = 0; line < data32.Height; line++)
{
Marshal.Copy(p32, b32, 0, b32.Length);
byte b = 0;
for (int i = 0; i < b1.Length * 8; i++)
{
if (i < b32.Length)
{
b = unchecked((byte)((b << 1) | ((b32[i] & 0xffffff) == 0 ? 0 : 1)));
}
else
{
b <<= 1;
}
if (i % 8 == 7)
{
b1[i / 8] = b;
}
}
Marshal.Copy(b1, 0, p1, b1.Length);
p32 = (IntPtr)((long)p32 + data32.Stride);
p1 = (IntPtr)((long)p1 + data1.Stride);
}
bmp1.UnlockBits(data1);
bmpOpt = bmp1;
}
else if (colorToIndex.Count <= 256)
{
var bmp8 = new Bitmap(rgb32.Width, rgb32.Height, PixelFormat.Format8bppIndexed);
PreparePalette(colorToIndex, bmp8);
bmp8.SetResolution(rgb32.HorizontalResolution, rgb32.VerticalResolution);
var data8 = bmp8.LockBits(
new Rectangle(0, 0, bmp8.Width, bmp8.Height),
ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed);
IntPtr p8 = data8.Scan0;
byte[] b8 = new byte[data8.Stride];
p32 = data32.Scan0;
for (int line = 0; line < data32.Height; line++)
{
Marshal.Copy(p32, b32, 0, b32.Length);
for (int i = 0; i < b8.Length; i++)
{
if (i < b32.Length)
{
b8[i] = (byte)colorToIndex[b32[i] & 0xffffff];
}
}
Marshal.Copy(b8, 0, p8, b8.Length);
p32 = (IntPtr)((long)p32 + data32.Stride);
p8 = (IntPtr)((long)p8 + data8.Stride);
}
bmp8.UnlockBits(data8);
bmpOpt = bmp8;
}
return bmpOpt;
}
private static void PreparePalette(Dictionary<int, int> colorToIndex, Bitmap bmp)
{
var palette = bmp.Palette;
var entries = palette.Entries;
var indexToColor = new int[colorToIndex.Count];
var i = 0;
foreach (var kv in colorToIndex)
{
indexToColor[i] = kv.Key;
i++;
}
Array.Sort(indexToColor);
for (i = 0; i < indexToColor.Length; i++)
{
entries[i] = Color.FromArgb(indexToColor[i] | Opaque);
colorToIndex[indexToColor[i]] = i;
}
for (; i < entries.Length; i++)
{
entries[i] = Color.White;
}
bmp.Palette = palette;
}
}
}