public void EncodeFromRaw() { const string testImage = "obama-240p.raw"; var path = Path.GetFullPath(Path.Combine(TestImageDirectory, testImage)); var imageByte = File.ReadAllBytes(path); using var image = ImageHelper.FromRaw(imageByte, 427, 240, 427 * 3, 3, true); using var compressionParameters = new CompressionParameters(); OpenJpeg.SetDefaultEncoderParameters(compressionParameters); compressionParameters.TcpNumLayers = 1; compressionParameters.CodingParameterDistortionAllocation = 1; using var codec = OpenJpeg.CreateCompress(CodecFormat.J2k); OpenJpeg.SetInfoHandler(codec, new DelegateHandler <MsgCallback>(MsgInfoCallback), IntPtr.Zero); OpenJpeg.SetWarnHandler(codec, new DelegateHandler <MsgCallback>(MsgWarnCallback), IntPtr.Zero); OpenJpeg.SetErrorHandler(codec, new DelegateHandler <MsgCallback>(MsgErrorCallback), IntPtr.Zero); Assert.True(OpenJpeg.SetupEncoder(codec, compressionParameters, image)); var bufferLength = imageByte.Length + 1024; var outputBuffer = Marshal.AllocHGlobal(bufferLength); var buffer = new Buffer { Data = outputBuffer, Length = bufferLength, Position = 0 }; var size = Marshal.SizeOf(buffer); var userData = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(buffer, userData, false); using var stream = OpenJpeg.StreamCreate((ulong)buffer.Length, false); OpenJpeg.StreamSetUserData(stream, userData); OpenJpeg.StreamSetUserDataLength(stream, buffer.Length); OpenJpeg.StreamSetWriteFunction(stream, new DelegateHandler <StreamWrite>(StreamWriteCallback)); OpenJpeg.StreamSetReadFunction(stream, new DelegateHandler <StreamRead>(StreamReadCallback)); OpenJpeg.StreamSetSeekFunction(stream, new DelegateHandler <StreamSeek>(StreamSeekCallback)); OpenJpeg.StreamSetSkipFunction(stream, new DelegateHandler <StreamSkip>(StreamSkipCallback)); Assert.True(OpenJpeg.StartCompress(codec, image, stream)); Assert.True(OpenJpeg.Encode(codec, stream)); Assert.True(OpenJpeg.EndCompress(codec, stream)); var outputPath = Path.Combine(ResultDirectory, nameof(this.EncodeFromRaw), $"{Path.GetFileNameWithoutExtension(testImage)}.j2k"); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); var tmp = Marshal.PtrToStructure <Buffer>(userData); var output = new byte[tmp.Position]; Marshal.Copy(buffer.Data, output, 0, output.Length); File.WriteAllBytes(outputPath, output); Marshal.FreeHGlobal(outputBuffer); Marshal.FreeHGlobal(userData); }
public void StreamCreate() { var targets = new[] { new { IsReadStream = true }, new { IsReadStream = false } }; foreach (var target in targets) { var stream = OpenJpeg.StreamCreate(1024, target.IsReadStream); this.DisposeAndCheckDisposedState(stream); } }