-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathEncodeBitmap.cs
329 lines (266 loc) · 12 KB
/
EncodeBitmap.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
#if !NETSTANDARD
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using BitMiracle.LibTiff.Classic;
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
class EncodeBitmap
{
private static string[] CCITT_Files
{
get
{
return new string[]
{
"bitonal.tif",
"CCITT_1.tif"
};
}
}
private static string[] RGB_Files
{
get
{
return new string[]
{
"FLAG_T24.TIF",
};
}
}
private static byte[] getImageRasterBytes(Bitmap bmp, PixelFormat format)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
byte[] bits = null;
try
{
// Lock the managed memory
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
// Declare an array to hold the bytes of the bitmap.
bits = new byte[bmpdata.Stride * bmpdata.Height];
// Copy the values into the array.
Marshal.Copy(bmpdata.Scan0, bits, 0, bits.Length);
// Release managed memory
bmp.UnlockBits(bmpdata);
}
catch
{
return null;
}
return bits;
}
private static byte[] getImageRasterBytes(Bitmap bmp)
{
return getImageRasterBytes(bmp, bmp.PixelFormat);
}
private string OutputFolder
{
get
{
return Path.Combine(TestCase.Folder, "Output.Tiff");
}
}
private string ExpectedFolder
{
get
{
return Path.Combine(TestCase.Folder, "Expected.Tiff");
}
}
[Test, TestCaseSource("CCITT_Files")]
public void EncodeCCITTByScanlines(string file)
{
string fullPath = Path.Combine(TestCase.Folder, file);
string outputPath = Path.Combine(OutputFolder, file);
string expectedPath = Path.Combine(ExpectedFolder, file);
string suffix = "_ccitt_scanlines.tif";
string fullOutputPath = outputPath + suffix;
string fullExpectedPath = expectedPath + suffix;
using (Bitmap bmp = new Bitmap(fullPath))
{
byte[] raster = getImageRasterBytes(bmp);
using (Tiff tif = Tiff.Open(fullOutputPath, "w"))
{
Assert.IsNotNull(tif);
tif.SetField(TiffTag.IMAGEWIDTH, bmp.Width);
tif.SetField(TiffTag.IMAGELENGTH, bmp.Height);
tif.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tif.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
tif.SetField(TiffTag.ROWSPERSTRIP, bmp.Height);
tif.SetField(TiffTag.XRESOLUTION, bmp.HorizontalResolution);
tif.SetField(TiffTag.YRESOLUTION, bmp.VerticalResolution);
tif.SetField(TiffTag.SUBFILETYPE, 0);
tif.SetField(TiffTag.BITSPERSAMPLE, 1);
tif.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
tif.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
tif.SetField(TiffTag.SAMPLESPERPIXEL, 1);
tif.SetField(TiffTag.T6OPTIONS, 0);
tif.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
tif.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int tiffStride = tif.ScanlineSize();
int stride = raster.Length / bmp.Height;
// raster stride MAY be bigger than TIFF stride (due to padding in raster bits)
Assert.False(tiffStride > stride);
for (int i = 0, offset = 0; i < bmp.Height; i++)
{
bool res = tif.WriteScanline(raster, offset, i, 0);
Assert.IsTrue(res);
offset += stride;
}
}
}
FileAssert.AreEqual(fullExpectedPath, fullOutputPath);
}
[Test, TestCaseSource("CCITT_Files")]
public void EncodeCCITT(string file)
{
string fullPath = Path.Combine(TestCase.Folder, file);
string outputPath = Path.Combine(OutputFolder, file);
string expectedPath = Path.Combine(ExpectedFolder, file);
string suffix = "_ccitt.tif";
string fullOutputPath = outputPath + suffix;
string fullExpectedPath = expectedPath + suffix;
using (Bitmap bmp = new Bitmap(fullPath))
{
byte[] raster = getImageRasterBytes(bmp);
using (Tiff tif = Tiff.Open(fullOutputPath, "w"))
{
Assert.IsNotNull(tif);
tif.SetField(TiffTag.IMAGEWIDTH, bmp.Width);
tif.SetField(TiffTag.IMAGELENGTH, bmp.Height);
tif.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tif.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
tif.SetField(TiffTag.ROWSPERSTRIP, bmp.Height);
tif.SetField(TiffTag.XRESOLUTION, bmp.HorizontalResolution);
tif.SetField(TiffTag.YRESOLUTION, bmp.VerticalResolution);
tif.SetField(TiffTag.SUBFILETYPE, 0);
tif.SetField(TiffTag.BITSPERSAMPLE, 1);
tif.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
tif.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
tif.SetField(TiffTag.SAMPLESPERPIXEL, 1);
tif.SetField(TiffTag.T6OPTIONS, 0);
tif.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
tif.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int tiffStride = tif.ScanlineSize();
int rasterStride = raster.Length / bmp.Height;
Assert.False(tiffStride > rasterStride);
if (tiffStride < rasterStride)
{
// raster stride is bigger than TIFF stride
// this is due to padding in raster bits
// we need to create correct TIFF strip and write it into TIFF
byte[] stripBits = new byte[tiffStride * bmp.Height];
for (int i = 0, rasterPos = 0, stripPos = 0; i < bmp.Height; i++)
{
System.Buffer.BlockCopy(raster, rasterPos, stripBits, stripPos, tiffStride);
rasterPos += rasterStride;
stripPos += tiffStride;
}
// Write the information to the file
int n = tif.WriteEncodedStrip(0, stripBits, stripBits.Length);
Assert.AreNotEqual(0, n);
}
else
{
// Write the information to the file
int n = tif.WriteEncodedStrip(0, raster, raster.Length);
Assert.AreNotEqual(0, n);
}
}
}
FileAssert.AreEqual(fullExpectedPath, fullOutputPath);
}
[Test, TestCaseSource("RGB_Files")]
public void EncodeRGBByScanlines(string file)
{
string fullPath = Path.Combine(TestCase.Folder, file);
string outputPath = Path.Combine(OutputFolder, file);
string expectedPath = Path.Combine(ExpectedFolder, file);
string suffix = "_rgb_scanlines.tif";
string fullOutputPath = outputPath + suffix;
string fullExpectedPath = expectedPath + suffix;
using (Bitmap bmp = new Bitmap(fullPath))
{
using (Tiff tif = Tiff.Open(fullOutputPath, "w"))
{
Assert.IsNotNull(tif);
convertToTiff(bmp, tif, PixelFormat.Format24bppRgb);
}
}
FileAssert.AreEqual(fullExpectedPath, fullOutputPath);
}
[Test, TestCaseSource("RGB_Files")]
public void EncodeRGBAByScanlines(string file)
{
string fullPath = Path.Combine(TestCase.Folder, file);
string outputPath = Path.Combine(OutputFolder, file);
string expectedPath = Path.Combine(ExpectedFolder, file);
string suffix = "_rgba_scanlines.tif";
string fullOutputPath = outputPath + suffix;
string fullExpectedPath = expectedPath + suffix;
using (Bitmap bmp = new Bitmap(fullPath))
{
using (Tiff tif = Tiff.Open(fullOutputPath, "w"))
{
Assert.IsNotNull(tif);
convertToTiff(bmp, tif, PixelFormat.Format32bppArgb);
}
}
FileAssert.AreEqual(fullExpectedPath, fullOutputPath);
}
private static void convertToTiff(Bitmap bmp, Tiff tif, PixelFormat outputFormat)
{
if (outputFormat != PixelFormat.Format24bppRgb && outputFormat != PixelFormat.Format32bppArgb)
throw new System.ArgumentOutOfRangeException();
byte[] raster = getImageRasterBytes(bmp, outputFormat);
tif.SetField(TiffTag.IMAGEWIDTH, bmp.Width);
tif.SetField(TiffTag.IMAGELENGTH, bmp.Height);
tif.SetField(TiffTag.COMPRESSION, Compression.LZW);
tif.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
tif.SetField(TiffTag.ROWSPERSTRIP, bmp.Height);
tif.SetField(TiffTag.XRESOLUTION, bmp.HorizontalResolution);
tif.SetField(TiffTag.YRESOLUTION, bmp.VerticalResolution);
tif.SetField(TiffTag.BITSPERSAMPLE, 8);
if (outputFormat == PixelFormat.Format32bppArgb)
tif.SetField(TiffTag.SAMPLESPERPIXEL, 4);
else
tif.SetField(TiffTag.SAMPLESPERPIXEL, 3);
tif.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int stride = raster.Length / bmp.Height;
convertRGBSamples(raster, bmp.Width, bmp.Height, outputFormat);
for (int i = 0, offset = 0; i < bmp.Height; i++)
{
bool res = tif.WriteScanline(raster, offset, i, 0);
Assert.IsTrue(res);
offset += stride;
}
}
/// <summary>
/// Converts BGRA or BGR samples into RGBA or RGB samples
/// </summary>
private static void convertRGBSamples(byte[] data, int width, int height, PixelFormat format)
{
if (format != PixelFormat.Format24bppRgb && format != PixelFormat.Format32bppArgb)
throw new InvalidDataException();
int stride = data.Length / height;
int samplesPerPixel = 4;
if (format == PixelFormat.Format24bppRgb)
samplesPerPixel = 3;
for (int y = 0; y < height; y++)
{
int offset = stride * y;
int strideEnd = offset + width * samplesPerPixel;
for (int i = offset; i < strideEnd; i += samplesPerPixel)
{
byte temp = data[i + 2];
data[i + 2] = data[i];
data[i] = temp;
}
}
}
}
}
#endif