-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOPJEncode.cs
174 lines (142 loc) · 6.87 KB
/
OPJEncode.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
using System;
using System.IO;
using System.Runtime.InteropServices;
using OpenJPEG;
namespace testopj
{
public class OPJEncode
{
private readonly OpenJPEGFunctions.opj_msg_callback _cbInfo;
private readonly OpenJPEGFunctions.opj_msg_callback _cbWarning;
private readonly OpenJPEGFunctions.opj_msg_callback _cbError;
private readonly OpenJPEGFunctions.opj_stream_write_fn _writeFn;
private readonly OpenJPEGFunctions.opj_stream_skip_fn _skipFn;
private readonly OpenJPEGFunctions.opj_stream_seek_fn _seekFn;
private MemoryStream _s;
public OPJEncode()
{
_cbInfo = new OpenJPEGFunctions.opj_msg_callback(MsgInfo);
_cbWarning = new OpenJPEGFunctions.opj_msg_callback(MsgWarning);
_cbError = new OpenJPEGFunctions.opj_msg_callback(MsgError);
_writeFn = new OpenJPEGFunctions.opj_stream_write_fn(Write);
_skipFn = new OpenJPEGFunctions.opj_stream_skip_fn(Skip);
_seekFn = new OpenJPEGFunctions.opj_stream_seek_fn(Seek);
}
/// <summary>
///
/// </summary>
/// <param name="bmp"></param>
/// <param name="r">quality 0-100</param>
/// <returns></returns>
public byte[] Encode(BMPGray16 bmp, float r)
{
_s = new MemoryStream();
var opjImage = IntPtr.Zero;
var opjCodec = IntPtr.Zero;
var opjStream = IntPtr.Zero;
var encoderParametersPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OpenJPEGEncoderParameters)));
var imageCreateParamPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OpenJPEGImageCompParam)));
try
{
var createImageParam = new OpenJPEGImageCompParam
{
w = (uint)bmp.W,
h = (uint)bmp.H,
prec = (uint)bmp.ActualBPP,
dx = 1,
dy = 1
};
Marshal.StructureToPtr(createImageParam, imageCreateParamPtr, false);
opjImage = OpenJPEGFunctions.opj_image_create(1, imageCreateParamPtr, (int)OpenJPEGColorSpace.Gray);
if (opjImage == IntPtr.Zero)
throw new OPJException();
var img = Marshal.PtrToStructure<OpenJPEGImage>(opjImage);
var imgComp = Marshal.PtrToStructure<OpenJPEGImageComp>(img.comps);
img.x1 = createImageParam.w;
img.y1 = createImageParam.h;
Marshal.StructureToPtr(img, opjImage, false);
unsafe
{
var p = (byte*)imgComp.data;
for (var n = 0; n < bmp.Pixels.Length; ++n)
{
var v = bmp.Pixels[n] >> (16 - bmp.ActualBPP);
*p = (byte)(v & 0x00ff);
++p;
*p = (byte)(v >> 8);
p += 3;
}
}
opjCodec = OpenJPEGFunctions.opj_create_compress((int)OpenJPEGCodecFormat.JP2);
OpenJPEGFunctions.opj_set_info_handler(opjCodec, _cbInfo, IntPtr.Zero);
OpenJPEGFunctions.opj_set_warning_handler(opjCodec, _cbWarning, IntPtr.Zero);
OpenJPEGFunctions.opj_set_error_handler(opjCodec, _cbError, IntPtr.Zero);
OpenJPEGFunctions.opj_set_default_encoder_parameters(encoderParametersPtr);
var ep = Marshal.PtrToStructure<OpenJPEGEncoderParameters>(encoderParametersPtr);
ep.cp_disto_alloc = 1;
ep.tcp_numlayers = 1;
ep.tcp_rates[0] = r;
Marshal.StructureToPtr(ep, encoderParametersPtr, false);
if (OpenJPEGFunctions.opj_setup_encoder(opjCodec, encoderParametersPtr, opjImage) != (int)OpenJPEGBool.True)
throw new OPJException();
opjStream = OpenJPEGFunctions.opj_stream_default_create((int)OpenJPEGBool.False);
//opjStream = OpenJPEGFunctions.opj_stream_create_default_file_stream("111.j2k", (int)OpenJPEGBool.False);
if (opjStream == IntPtr.Zero)
throw new OPJException();
OpenJPEGFunctions.opj_stream_set_write_function(opjStream, _writeFn);
OpenJPEGFunctions.opj_stream_set_skip_function(opjStream, _skipFn);
OpenJPEGFunctions.opj_stream_set_seek_function(opjStream, _seekFn);
if (OpenJPEGFunctions.opj_start_compress(opjCodec, opjImage, opjStream) != (int)OpenJPEGBool.True)
throw new OPJException();
if (OpenJPEGFunctions.opj_encode(opjCodec, opjStream) != (int)OpenJPEGBool.True)
throw new OPJException();
if (OpenJPEGFunctions.opj_end_compress(opjCodec, opjStream) != (int)OpenJPEGBool.True)
throw new OPJException();
return _s.ToArray();
}
finally
{
_s = null;
Marshal.FreeHGlobal(imageCreateParamPtr);
Marshal.FreeHGlobal(encoderParametersPtr);
if (opjImage != IntPtr.Zero)
OpenJPEGFunctions.opj_image_destroy(opjImage);
if (opjCodec != IntPtr.Zero)
OpenJPEGFunctions.opj_destroy_codec(opjCodec);
if (opjStream != IntPtr.Zero)
OpenJPEGFunctions.opj_stream_destroy(opjStream);
}
}
private void MsgInfo(string msg, IntPtr clientData)
{
Console.Write("info {0}", msg);
}
private void MsgWarning(string msg, IntPtr clientData)
{
Console.Write("warning {0}", msg);
}
private void MsgError(string msg, IntPtr clientData)
{
Console.Write("error {0}", msg);
}
private int Write(IntPtr buffer, int bytes, IntPtr userData)
{
unsafe
{
var src = new UnmanagedMemoryStream((byte*)buffer.ToPointer(), bytes, bytes, FileAccess.Read);
src.CopyTo(_s);
}
return bytes;
}
private long Skip(long bytes, IntPtr userData)
{
_s.Position += bytes;
return bytes;
}
private int Seek(long bytes, IntPtr userData)
{
_s.Position = bytes;
return 1;
}
}
}