-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapLuminanceSource.cs
518 lines (477 loc) · 21.8 KB
/
BitmapLuminanceSource.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using ZXing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace TheTalosCompanion
{
/// <summary>
/// class which represents the luminance values for a bitmap object
/// </summary>
public partial class BitmapLuminanceSource : BaseLuminanceSource
{
/// <summary>
/// Initializes a new instance of the <see cref="BitmapLuminanceSource"/> class.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
protected BitmapLuminanceSource(int width, int height)
: base(width, height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BitmapLuminanceSource"/> class
/// with the image of a Bitmap instance
/// </summary>
/// <param name="bitmap">The bitmap.</param>
public BitmapLuminanceSource(Bitmap bitmap)
: base(bitmap.Width, bitmap.Height)
{
CalculateLuminanceValues(bitmap, luminances);
}
/// <summary>
/// calculates the luminance values for bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="luminances"></param>
protected static void CalculateLuminanceValues(Bitmap bitmap, byte[] luminances)
{
var height = bitmap.Height;
var width = bitmap.Width;
// In order to measure pure decoding speed, we convert the entire image to a greyscale array
// The underlying raster of image consists of bytes with the luminance values
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
try
{
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
if (pixelWidth > 4)
{
// old slow way for unsupported bit depth
CalculateLuminanceValuesSlow(bitmap, luminances);
}
else
{
if (bitmap.PixelFormat == PixelFormat.Format32bppArgb ||
bitmap.PixelFormat == PixelFormat.Format32bppPArgb)
{
pixelWidth = 40;
}
if ((int)bitmap.PixelFormat == 8207 ||
(bitmap.Flags & (int)ImageFlags.ColorSpaceCmyk) == (int)ImageFlags.ColorSpaceCmyk)
{
pixelWidth = 41;
}
switch (pixelWidth)
{
case 0:
if (bitmap.PixelFormat == PixelFormat.Format4bppIndexed)
CalculateLuminanceValuesForIndexed4Bit(bitmap, data, luminances);
else
CalculateLuminanceValuesForIndexed1Bit(bitmap, data, luminances);
break;
case 1:
CalculateLuminanceValuesForIndexed8Bit(bitmap, data, luminances);
break;
case 2:
// should be RGB565 or RGB555, assume RGB565
CalculateLuminanceValues565(bitmap, data, luminances);
break;
case 3:
CalculateLuminanceValues24Bit(bitmap, data, luminances);
break;
case 4:
CalculateLuminanceValues32BitWithoutAlpha(bitmap, data, luminances);
break;
case 40:
CalculateLuminanceValues32BitWithAlpha(bitmap, data, luminances);
break;
case 41:
CalculateLuminanceValues32BitCMYK(bitmap, data, luminances);
break;
default:
throw new NotSupportedException();
}
}
}
finally
{
bitmap.UnlockBits(data);
}
}
/// <summary>
/// old slow way for unsupported bit depth
/// </summary>
/// <param name="bitmap"></param>
/// <param name="luminances"></param>
protected static void CalculateLuminanceValuesSlow(Bitmap bitmap, byte[] luminances)
{
var height = bitmap.Height;
var width = bitmap.Width;
for (int y = 0; y < height; y++)
{
int offset = y * width;
for (int x = 0; x < width; x++)
{
var c = bitmap.GetPixel(x, y);
luminances[offset + x] = (byte)((RChannelWeight * c.R + GChannelWeight * c.G + BChannelWeight * c.B) >> ChannelWeight);
}
}
}
/// <summary>
/// calculates the luminance values for 1-bit indexed bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
protected static void CalculateLuminanceValuesForIndexed1Bit(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
if (pixelWidth != 0)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
// prepare palette for 1, 4 and 8 bit indexed bitmaps
var luminancePalette = new byte[256];
var luminancePaletteLength = Math.Min(bitmap.Palette.Entries.Length, luminancePalette.Length);
for (var index = 0; index < luminancePaletteLength; index++)
{
var color = bitmap.Palette.Entries[index];
luminancePalette[index] = (byte)((RChannelWeight * color.R +
GChannelWeight * color.G +
BChannelWeight * color.B) >> ChannelWeight);
}
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
for (int x = 0; x * 8 < width; x++)
{
var x8 = 8 * x;
var offset8 = offset + x8;
for (int subX = 0; subX < 8 && x8 + subX < width; subX++)
{
var index = (buffer[x] >> (7 - subX)) & 1;
luminances[offset8 + subX] = luminancePalette[index];
}
}
}
}
/// <summary>
/// calculates the luminance values for 4-bit indexed bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
protected static void CalculateLuminanceValuesForIndexed4Bit(Bitmap bitmap, BitmapData data, byte[] luminances)
{
if (bitmap.PixelFormat != PixelFormat.Format4bppIndexed)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
var evenWidth = (width / 2) * 2;
if (pixelWidth != 0)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
// prepare palette for 1, 4 and 8 bit indexed bitmaps
var luminancePalette = new byte[256];
var luminancePaletteLength = Math.Min(bitmap.Palette.Entries.Length, luminancePalette.Length);
for (var index = 0; index < luminancePaletteLength; index++)
{
var color = bitmap.Palette.Entries[index];
luminancePalette[index] = (byte)((RChannelWeight * color.R +
GChannelWeight * color.G +
BChannelWeight * color.B) >> ChannelWeight);
}
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
int sourceX = 0;
int destX = 0;
byte sourceValue = 0;
for (; destX < evenWidth; sourceX++, destX += 2)
{
sourceValue = buffer[sourceX];
var index = sourceValue & 15;
luminances[offset + destX + 1] = luminancePalette[index];
index = (sourceValue >> 4) & 15;
luminances[offset + destX] = luminancePalette[index];
}
if (width > evenWidth)
{
var index = (sourceValue >> 4) & 15;
luminances[offset + destX] = luminancePalette[index];
}
}
}
/// <summary>
/// calculates the luminance values for 8-bit indexed bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
protected static void CalculateLuminanceValuesForIndexed8Bit(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
if (pixelWidth != 1)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
// prepare palette for 1, 4 and 8 bit indexed bitmaps
var luminancePalette = new byte[256];
var luminancePaletteLength = Math.Min(bitmap.Palette.Entries.Length, luminancePalette.Length);
for (var index = 0; index < luminancePaletteLength; index++)
{
var color = bitmap.Palette.Entries[index];
luminancePalette[index] = (byte)((RChannelWeight * color.R +
GChannelWeight * color.G +
BChannelWeight * color.B) >> ChannelWeight);
}
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
for (int x = 0; x < width; x++)
{
luminances[offset + x] = luminancePalette[buffer[x]];
}
}
}
/// <summary>
/// calculates the luminance values for 565 encoded bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
private static void CalculateLuminanceValues565(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
if (pixelWidth != 2)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
var maxIndex = 2 * width;
for (int index = 0; index < maxIndex; index += 2)
{
var byte1 = buffer[index];
var byte2 = buffer[index + 1];
var b5 = byte1 & 0x1F;
var g5 = (((byte1 & 0xE0) >> 5) | ((byte2 & 0x03) << 3)) & 0x1F;
var r5 = (byte2 >> 2) & 0x1F;
var r8 = (r5 * 527 + 23) >> 6;
var g8 = (g5 * 527 + 23) >> 6;
var b8 = (b5 * 527 + 23) >> 6;
luminances[offset] = (byte)((RChannelWeight * r8 + GChannelWeight * g8 + BChannelWeight * b8) >> ChannelWeight);
offset++;
}
}
}
/// <summary>
/// calculates the luminance values for 24-bit encoded bitmaps
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
private static void CalculateLuminanceValues24Bit(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
if (pixelWidth != 3)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
var maxIndex = width * 3;
for (int x = 0; x < maxIndex; x += 3)
{
var luminance = (byte)((BChannelWeight * buffer[x] +
GChannelWeight * buffer[x + 1] +
RChannelWeight * buffer[x + 2]) >> ChannelWeight);
luminances[offset] = luminance;
offset++;
}
}
}
/// <summary>
/// calculates the luminance values for 32-bit encoded bitmaps without respecting the alpha channel
/// </summary>
/// <param name="bitmap"></param>
/// <param name="data"></param>
/// <param name="luminances"></param>
private static void CalculateLuminanceValues32BitWithoutAlpha(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
var maxIndex = 4 * width;
if (pixelWidth != 4)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
// 4 bytes without alpha channel value
for (int x = 0; x < maxIndex; x += 4)
{
var luminance = (byte)((BChannelWeight * buffer[x] +
GChannelWeight * buffer[x + 1] +
RChannelWeight * buffer[x + 2]) >> ChannelWeight);
luminances[offset] = luminance;
offset++;
}
}
}
/// calculates the luminance values for 32-bit encoded bitmaps with alpha channel
private static void CalculateLuminanceValues32BitWithAlpha(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
var maxIndex = 4 * width;
if (pixelWidth != 4)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
// with alpha channel; some barcodes are completely black if you
// only look at the r, g and b channel but the alpha channel controls
// the view
for (int x = 0; x < maxIndex; x += 4)
{
var luminance = (byte)((BChannelWeight * buffer[x] +
GChannelWeight * buffer[x + 1] +
RChannelWeight * buffer[x + 2]) >> ChannelWeight);
// calculating the resulting luminance based upon a white background
// var alpha = buffer[x * pixelWidth + 3] / 255.0;
// luminance = (byte)(luminance * alpha + 255 * (1 - alpha));
var alpha = buffer[x + 3];
luminance = (byte)(((luminance * alpha) >> 8) + (255 * (255 - alpha) >> 8) + 1);
luminances[offset] = luminance;
offset++;
}
}
}
/// calculates the luminance values for 32-bit CMYK encoded bitmaps (k is ignored at the momen)
private static void CalculateLuminanceValues32BitCMYK(Bitmap bitmap, BitmapData data, byte[] luminances)
{
var height = data.Height;
var width = data.Width;
var stride = Math.Abs(data.Stride);
var pixelWidth = stride / width;
var strideStep = data.Stride;
var buffer = new byte[stride];
var ptrInBitmap = data.Scan0;
var maxIndex = 4 * width;
if (pixelWidth != 4)
throw new InvalidOperationException("Unsupported pixel format: " + bitmap.PixelFormat);
for (int y = 0; y < height; y++)
{
// copy a scanline not the whole bitmap because of memory usage
Marshal.Copy(ptrInBitmap, buffer, 0, stride);
#if NET40 || NET45 || NET46 || NET47 || NET48
ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep);
#else
ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep);
#endif
var offset = y * width;
for (int x = 0; x < maxIndex; x += 4)
{
var luminance = (byte)(255 - ((BChannelWeight * buffer[x] +
GChannelWeight * buffer[x + 1] +
RChannelWeight * buffer[x + 2]) >> ChannelWeight));
// Ignore value of k at the moment
luminances[offset] = luminance;
offset++;
}
}
}
/// <summary>
/// Should create a new luminance source with the right class type.
/// The method is used in methods crop and rotate.
/// </summary>
/// <param name="newLuminances">The new luminances.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <returns></returns>
protected override LuminanceSource CreateLuminanceSource(byte[] newLuminances, int width, int height)
{
return new BitmapLuminanceSource(width, height) { luminances = newLuminances };
}
}
}