-
Notifications
You must be signed in to change notification settings - Fork 0
/
TJ.cs
756 lines (664 loc) · 28.1 KB
/
TJ.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using TurboJpeg.Internal;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
namespace TurboJpeg
{
public enum TJSamp : int
{
Samp444 = 0,
Samp422,
Samp420,
SampGray,
Samp440,
Max,
}
public enum TJPixelFormat : int
{
PixcelFormatRGB = 0,
PixcelFormatBGR,
PixcelFormatRGBX,
PixcelFormatBGRX,
PixcelFormatXBGR,
PixcelFormatXRGB,
PixcelFormatGRAY,
PixcelFormatRGBA,
PixcelFormatBGRA,
PixcelFormatABGR,
PixcelFormatARGB,
Max,
}
[Flags]
public enum TJFlag : int
{
None = 0,
BottomUp = TJ.FLAG_BOTTOMUP,
ForceMMX = TJ.FLAG_FORCEMMX,
ForceSSE = TJ.FLAG_FORCESSE,
ForceSSE2 = TJ.FLAG_FORCESSE2,
ForceSSE3 = TJ.FLAG_FORCESSE3,
ForceFastUpSample = TJ.FLAG_FASTUPSAMPLE,
FastDct = TJ.FLAG_FASTDCT,
AccurateDct = TJ.FLAG_ACCURATEDCT,
}
public static class TJ
{
public static void CheckSubsampling(TJSamp subsamp)
{
if (subsamp < 0 || subsamp >= TJSamp.Max)
throw new ArgumentException("Invalid subsampling type");
}
public static int getMCUWidth(TJSamp subsamp)
{
CheckSubsampling(subsamp);
return mcuWidth[(int)subsamp];
}
private static readonly int[] mcuWidth =
{
8, 16, 16, 8, 8
};
public static int getMCUHeight(TJSamp subsamp)
{
CheckSubsampling(subsamp);
return mcuHeight[(int)subsamp];
}
private static readonly int[] mcuHeight =
{
8, 8, 16, 8, 16
};
internal static void CheckPixelFormat(TJPixelFormat pixelFormat, string message)
{
if (pixelFormat < 0 || pixelFormat >= TJPixelFormat.Max)
throw new ArgumentException(message);
}
public static void CheckPixelFormat(TJPixelFormat pixelFormat)
{
CheckPixelFormat(pixelFormat, "Invalid pixel format");
}
public static int getPixelSize(TJPixelFormat pixelFormat)
{
CheckPixelFormat(pixelFormat);
return pixelSize[(int)pixelFormat];
}
private static readonly int[] pixelSize =
{
3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4
};
public static int getRedOffset(TJPixelFormat pixelFormat)
{
CheckPixelFormat(pixelFormat);
return redOffset[(int)pixelFormat];
}
private static readonly int[] redOffset =
{
0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1
};
public static int getGreenOffset(TJPixelFormat pixelFormat)
{
CheckPixelFormat(pixelFormat);
return greenOffset[(int)pixelFormat];
}
private static readonly int[] greenOffset =
{
1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2
};
public static int getBlueOffset(TJPixelFormat pixelFormat)
{
CheckPixelFormat(pixelFormat);
return blueOffset[(int)pixelFormat];
}
private static readonly int[] blueOffset =
{
2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3
};
public const int FLAG_BOTTOMUP = 2;
public const int FLAG_FORCEMMX = 8;
public const int FLAG_FORCESSE = 16;
public const int FLAG_FORCESSE2 = 32;
public const int FLAG_FORCESSE3 = 128;
public const int FLAG_FASTUPSAMPLE = 256;
public const int FLAG_FASTDCT = 2048;
public const int FLAG_ACCURATEDCT = 4096;
public static int BufSize(int width, int height, TJSamp jpegSubsamp)
{
var ret = (int)tjBufSize(width, height, (int)jpegSubsamp);
if (ret == -1)
{
throw new TJException(tjGetErrorStr());
}
return ret;
}
public static int BufSizeYUV(int width, int height, TJSamp subsamp)
{
var ret = (int)tjBufSizeYUV(width, height, (int)subsamp);
if (ret == -1)
{
throw new TJException(tjGetErrorStr());
}
return ret;
}
public static TJScalingFactor[] GetScalingFactors()
{
int n;
var sf = TJ.tjGetScalingFactors(out n);
var sfArray = new TJScalingFactor[n];
for (int i = 0; i < n; i++)
{
sfArray[i] = (TJScalingFactor)Marshal.PtrToStructure(new IntPtr(sf.ToInt64() + i * Marshal.SizeOf(typeof(TJScalingFactor))), typeof(TJScalingFactor));
}
return sfArray;
}
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern tjhandle tjInitDecompress();
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint tjBufSize(int width, int height, int subsamp);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint tjBufSizeYUV(int width, int height, int subsamp);
[DllImport("turbojpeg.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String tjGetErrorStr();
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tjGetScalingFactors(out int numscalingfactors);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int tjDestroy(IntPtr handle);
}
class TJException : Exception
{
public TJException(string message)
: base(message)
{
}
}
[StructLayout(LayoutKind.Sequential)]
public class TJScalingFactor : IEquatable<TJScalingFactor>
{
TJScalingFactor()
{
}
public TJScalingFactor(int num, int denom)
{
if (num < 1 || denom < 1)
throw new ArgumentException("Numerator and denominator must be >= 1");
this.num = num;
this.denom = denom;
}
public int Num
{
get
{
return num;
}
}
public int Denom
{
get
{
return denom;
}
}
public int GetScaled(int dimension)
{
return (dimension * num + denom - 1) / denom;
}
public bool Equals(TJScalingFactor other)
{
return (this.num == other.num && this.denom == other.denom);
}
public bool IsOne
{
get
{
return (num == 1 && denom == 1);
}
}
public override string ToString()
{
return "num:" + num + ", denom:" + denom;
}
private int num = 1;
private int denom = 1;
};
namespace Internal
{
public sealed class tjhandle : System.Runtime.InteropServices.SafeHandle
{
public tjhandle()
: base(IntPtr.Zero, true)
{
}
public override bool IsInvalid
{
get { return handle == IntPtr.Zero; }
}
protected override bool ReleaseHandle()
{
return TJ.tjDestroy(handle) != -1;
}
}
}
public class TJDecompressor : IDisposable
{
private const String NO_ASSOC_ERROR =
"No JPEG image is associated with this instance";
public TJDecompressor()
{
handle = TJ.tjInitDecompress();
if (handle.IsInvalid)
throw new TJException("executing tjInitDecompress()");
}
public TJDecompressor(byte[] jpegImage)
: this(jpegImage, jpegImage.Length)
{
}
public TJDecompressor(byte[] jpegImage, int imageSize)
: this()
{
SetJPEGImage(jpegImage, imageSize);
}
public TJDecompressor(string filename)
: this(System.IO.File.OpenRead(filename))
{
}
public TJDecompressor(System.IO.Stream stream)
: this()
{
SetJPEGImage(stream);
}
public void SetJPEGImage(System.IO.Stream stream)
{
if(!TrySetJPEGImage(stream))
{
throw new TJException("Error in tjDecompressHeader2()");
}
}
public bool TrySetJPEGImage(System.IO.Stream stream)
{
var memStream = stream as System.IO.MemoryStream;
if (memStream != null)
{
return TrySetJPEGImage(memStream.GetBuffer(), (int)memStream.Length);
}
else
{
var buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length);
return TrySetJPEGImage(buf, buf.Length);
}
}
public bool TrySetJPEGImage(byte[] jpegImage, int imageSize)
{
if (jpegImage == null || imageSize < 1)
throw new ArgumentException("Invalid argument in setJPEGImage()");
if (jpegImage.Length < imageSize)
throw new ArgumentOutOfRangeException();
int width = 0, height = 0, jpegSubsamp = -1;
if (tjDecompressHeader2(handle, jpegImage, (uint)imageSize, out width, out height, out jpegSubsamp) == -1)
{
return false;
}
this.jpegBuf = jpegImage;
this.jpegBufSize = imageSize;
this.jpegSubsamp = (TJSamp)jpegSubsamp;
this.jpegWidth = width;
this.jpegHeight = height;
return true;
}
public void SetJPEGImage(byte[] jpegImage, int imageSize)
{
if (!TrySetJPEGImage(jpegImage, imageSize))
{
throw new TJException("Error in tjDecompressHeader2()");
}
}
public int Width
{
get
{
if (jpegWidth < 1)
throw new InvalidOperationException(NO_ASSOC_ERROR);
return jpegWidth;
}
}
public int Height
{
get
{
if (jpegHeight < 1)
throw new InvalidOperationException(NO_ASSOC_ERROR);
return jpegHeight;
}
}
public TJSamp Subsamp
{
get
{
if (jpegSubsamp < 0)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (jpegSubsamp >= TJSamp.Max)
throw new Exception("JPEG header information is invalid");
return jpegSubsamp;
}
}
public int BufSize
{
get
{
return TJ.BufSize(Width, Height, Subsamp);
}
}
public byte[] JpegBuf
{
get
{
if (jpegBuf == null)
throw new InvalidOperationException(NO_ASSOC_ERROR);
return jpegBuf;
}
}
public int JpegSize
{
get
{
if (jpegBufSize < 1)
throw new InvalidOperationException(NO_ASSOC_ERROR);
return jpegBufSize;
}
}
public int GetScaledWidth(int desiredWidth, int desiredHeight)
{
if (jpegWidth < 1 || jpegHeight < 1)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (desiredWidth < 0 || desiredHeight < 0)
throw new Exception("Invalid argument in getScaledWidth()");
TJScalingFactor[] sf = TJ.GetScalingFactors();
if (desiredWidth == 0)
desiredWidth = jpegWidth;
if (desiredHeight == 0)
desiredHeight = jpegHeight;
int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for (int i = 0; i < sf.Length; i++)
{
scaledWidth = sf[i].GetScaled(jpegWidth);
scaledHeight = sf[i].GetScaled(jpegHeight);
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break;
}
if (scaledWidth > desiredWidth || scaledHeight > desiredHeight)
throw new Exception("Could not scale down to desired image dimensions");
return scaledWidth;
}
public int GetScaledHeight(int desiredWidth, int desiredHeight)
{
if (jpegWidth < 1 || jpegHeight < 1)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (desiredWidth < 0 || desiredHeight < 0)
throw new ArgumentException("Invalid argument in getScaledHeight()");
TJScalingFactor[] sf = TJ.GetScalingFactors();
if (desiredWidth == 0)
desiredWidth = jpegWidth;
if (desiredHeight == 0)
desiredHeight = jpegHeight;
int scaledWidth = jpegWidth, scaledHeight = jpegHeight;
for (int i = 0; i < sf.Length; i++)
{
scaledWidth = sf[i].GetScaled(jpegWidth);
scaledHeight = sf[i].GetScaled(jpegHeight);
if (scaledWidth <= desiredWidth && scaledHeight <= desiredHeight)
break;
}
if (scaledWidth > desiredWidth || scaledHeight > desiredHeight)
throw new Exception("Could not scale down to desired image dimensions");
return scaledHeight;
}
private void CheckDecompressArgument(int x, int y, int desiredWidth, int pitch, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
}
public void Decompress(IntPtr dstBuf, int x, int y, int desiredWidth, int pitch, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
if (jpegBuf == null)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || pitch < 0 ||
desiredHeight < 0 || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
if (x > 0 || y > 0)
DecompressInternal(jpegBuf, jpegBufSize, dstBuf, x, y, desiredWidth, pitch,
desiredHeight, pixelFormat, flags);
else
DecompressInternal(jpegBuf, jpegBufSize, dstBuf, 0, 0, desiredWidth, pitch,
desiredHeight, pixelFormat, flags);
}
public void Decompress(byte[] dstBuf, int x, int y, int desiredWidth, int pitch, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
if (jpegBuf == null)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || pitch < 0 ||
desiredHeight < 0 || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
if (x > 0 || y > 0)
DecompressInternal(jpegBuf, jpegBufSize, dstBuf, x, y, desiredWidth, pitch,
desiredHeight, pixelFormat, flags);
else
DecompressInternal(jpegBuf, jpegBufSize, dstBuf, 0, 0, desiredWidth, pitch,
desiredHeight, pixelFormat, flags);
}
public void Decompress(int[] dstBuf, int x, int y, int desiredWidth, int stride, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
if (jpegBuf == null)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (dstBuf == null || x < 0 || y < 0 || desiredWidth < 0 || stride < 0 ||
desiredHeight < 0 || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
DecompressInternal(jpegBuf, jpegBufSize, dstBuf, x, y, desiredWidth, stride,
desiredHeight, pixelFormat, flags);
}
public byte[] Decompress(int desiredWidth, int pitch, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
if (desiredWidth < 0 || pitch < 0 || desiredHeight < 0 || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
int pixelSize = TJ.getPixelSize(pixelFormat);
int scaledWidth = GetScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = GetScaledHeight(desiredWidth, desiredHeight);
if (pitch == 0)
pitch = scaledWidth * pixelSize;
byte[] buf = new byte[pitch * scaledHeight];
Decompress(buf, 0, 0, desiredWidth, pitch, desiredHeight, pixelFormat, flags);
return buf;
}
public void DecompressToYUV(byte[] dstBuf, TJFlag flags)
{
if (jpegBuf == null)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (dstBuf == null || flags < 0)
throw new ArgumentException("Invalid argument in decompressToYUV()");
DecompressToYUV(jpegBuf, jpegBufSize, dstBuf, flags);
}
public byte[] DecompressToYUV(TJFlag flags)
{
if (flags < 0)
throw new ArgumentException("Invalid argument in decompressToYUV()");
if (jpegWidth < 1 || jpegHeight < 1 || jpegSubsamp < 0)
throw new InvalidOperationException(NO_ASSOC_ERROR);
if (jpegSubsamp >= TJSamp.Max)
throw new Exception("JPEG header information is invalid");
byte[] buf = new byte[TJ.BufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)];
DecompressToYUV(buf, flags);
return buf;
}
public static Bitmap DecompressToBitmap(byte[] srcBuf, TJFlag flags)
{
using (var decompressor = new TJDecompressor(srcBuf))
{
var pf = decompressor.Subsamp == TJSamp.SampGray ? PixelFormat.Indexed : PixelFormat.Format24bppRgb;
var dstImage = new Bitmap(decompressor.Width, decompressor.jpegHeight, pf);
decompressor.Decompress(dstImage, flags);
return dstImage;
}
}
public void Decompress(Bitmap dstImage, TJFlag flags)
{
if (dstImage == null || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
int desiredWidth = dstImage.Width;
int desiredHeight = dstImage.Height;
int scaledWidth = GetScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = GetScaledHeight(desiredWidth, desiredHeight);
if (scaledWidth != desiredWidth || scaledHeight != desiredHeight)
throw new Exception("Bitmap dimensions do not match one of the scaled image sizes that TurboJPEG is capable of generating.");
TJPixelFormat pixelFormat;
bool intPixels = false;
switch (dstImage.PixelFormat)
{
case PixelFormat.Format24bppRgb:
pixelFormat = TJPixelFormat.PixcelFormatBGR; break;
case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppPArgb:
pixelFormat = TJPixelFormat.PixcelFormatBGRA; break;
case PixelFormat.Format8bppIndexed:
pixelFormat = TJPixelFormat.PixcelFormatGRAY; break;
case PixelFormat.Format32bppRgb:
pixelFormat = TJPixelFormat.PixcelFormatBGRX;
intPixels = true; break;
default:
throw new ArgumentException("Unsupported Bitmap format");
}
var bitmapLock = dstImage.LockBits(new Rectangle(Point.Empty, dstImage.Size), System.Drawing.Imaging.ImageLockMode.ReadWrite, dstImage.PixelFormat);
IntPtr buf = bitmapLock.Scan0;
int stride = bitmapLock.Stride;
if (jpegBuf == null)
throw new InvalidOperationException();
DecompressInternal(jpegBuf, jpegBufSize, buf, 0, 0, scaledWidth, stride, scaledHeight,
pixelFormat, flags);
dstImage.UnlockBits(bitmapLock);
}
public Bitmap Decompress(int desiredWidth, int desiredHeight,
PixelFormat bufferedImageType, TJFlag flags)
{
if (desiredWidth < 0 || desiredHeight < 0 || flags < 0)
throw new ArgumentException("Invalid argument in decompress()");
int scaledWidth = GetScaledWidth(desiredWidth, desiredHeight);
int scaledHeight = GetScaledHeight(desiredWidth, desiredHeight);
Bitmap img = new Bitmap(scaledWidth, scaledHeight,
bufferedImageType);
Decompress(img, flags);
return img;
}
public void Close()
{
Dispose();
}
#if USE_UNSAFE
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int tjDecompressToYUV(tjhandle handle, byte* jpegBuf, uint jpegSize, byte* dstBuf,int flags);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int tjDecompress2(tjhandle handle, byte* jpegBuf, uint jpegSize, byte* dstBuf, int width, int pitch, int height, int pixelFormat, int flags);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int tjDecompressHeader2(tjhandle handle, byte* jpegBuff, uint jpegSize, ref int width, ref int height, ref int jpegSubsamp);
#endif
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompressHeader2(tjhandle handle, IntPtr jpegBuff, uint jpegSize, out int width, out int height, out int jpegSubsamp);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompressHeader2(tjhandle handle, byte[] jpegBuff, uint jpegSize, out int width, out int height, out int jpegSubsamp);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompressToYUV(tjhandle handle, IntPtr jpegBuf, uint jpegSize, IntPtr dstBuf, int flags);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompressToYUV(tjhandle handle, byte[] jpegBuf, uint jpegSize, byte[] dstBuf, int flags);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompress2(tjhandle handle, byte[] jpegBuf, uint jpegSize, IntPtr dstBuf, int width, int pitch, int height, int pixelFormat, int flags);
[DllImport("turbojpeg.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int tjDecompress2(tjhandle handle, byte[] jpegBuf, uint jpegSize, byte[] dstBuf, int width, int pitch, int height, int pixelFormat, int flags);
private void DecompressInternal(byte[] srcBuf, int size, byte[] dstBuf, int x, int y, int width, int pitch, int height, TJPixelFormat pixelFormat, TJFlag flags)
{
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
if (srcBuf.Length < size)
throw new ArgumentException("Source buffer is not large enough");
int actualPitch = pitch == 0 ? width * TJ.getPixelSize(pixelFormat) : pitch;
int arraySize = (y + height - 1) * actualPitch + (x + width) * TJ.getPixelSize(pixelFormat);
if (dstBuf.Length < arraySize)
throw new Exception("Destination buffer is not large enough");
var gcHandle = GCHandle.Alloc(dstBuf, GCHandleType.Pinned);
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(dstBuf, y * actualPitch + x * TJ.getPixelSize(pixelFormat));
try
{
if (tjDecompress2(handle, srcBuf, (uint)size, dstBuf, width, pitch, height, (int)pixelFormat, (int)flags) == -1)
throw new TJException(TJ.tjGetErrorStr());
}
catch
{
gcHandle.Free();
}
}
private void DecompressInternal(byte[] srcBuf, int size, IntPtr dstBuf, int x, int y, int desiredWidth, int pitch, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
if (srcBuf.Length < size)
throw new ArgumentException("Source buffer is not large enough");
int actualPitch = pitch == 0 ? desiredWidth * TJ.getPixelSize(pixelFormat) : pitch;
int arraySize = (y + desiredHeight - 1) * actualPitch + (x + desiredWidth) * TJ.getPixelSize(pixelFormat);
var ptr = new IntPtr(dstBuf.ToInt64() + y * actualPitch + x * TJ.getPixelSize(pixelFormat)); ;
if (tjDecompress2(handle, srcBuf, (uint)size, ptr, desiredWidth, pitch, desiredHeight, (int)pixelFormat, (int)flags) == -1)
throw new TJException(TJ.tjGetErrorStr());
}
private void DecompressInternal(byte[] srcBuf, int size, int[] dstBuf, int x, int y, int desiredWidth, int stride, int desiredHeight, TJPixelFormat pixelFormat, TJFlag flags)
{
TJ.CheckPixelFormat(pixelFormat, "Invalid argument in decompress()");
if (srcBuf.Length < size)
throw new ArgumentException("Source buffer is not large enough");
int actualStride = stride == 0 ? desiredWidth : stride;
int arraySize = (y + desiredHeight - 1) * actualStride + x + desiredWidth;
if (dstBuf.Length < arraySize)
throw new Exception("Destination buffer is not large enough");
var gcHandle = GCHandle.Alloc(dstBuf, GCHandleType.Pinned);
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(dstBuf, (y * actualStride + x));
try
{
if (tjDecompress2(handle, srcBuf, (uint)size, ptr, desiredWidth, stride * sizeof(int), desiredHeight, (int)pixelFormat, (int)flags) == -1)
throw new TJException(TJ.tjGetErrorStr());
}
catch
{
gcHandle.Free();
}
}
private void DecompressToYUV(byte[] srcBuf, int size, byte[] dstBuf, TJFlag flags)
{
if (srcBuf.Length < size)
throw new ArgumentException("Source buffer is not large enough");
if (dstBuf.Length < TJ.tjBufSizeYUV(jpegWidth, jpegHeight, (int)jpegSubsamp))
throw new Exception("Destination buffer is not large enough");
if (tjDecompressToYUV(handle, srcBuf, (uint)size, dstBuf, (int)flags) == -1)
throw new TJException(TJ.tjGetErrorStr());
}
protected tjhandle handle;
protected byte[] jpegBuf = null;
protected int jpegBufSize = 0;
protected int jpegWidth = 0;
protected int jpegHeight = 0;
protected TJSamp jpegSubsamp = (TJSamp)(-1);
private bool disposed;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
handle.Dispose();
}
handle = null;
disposed = true;
}
}
#region IDisposable メンバー
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}