public bool ReadHeader() { this._Codec?.Dispose(); this._DecompressionParameters?.Dispose(); this._Image?.Dispose(); this._Codec = null; this._DecompressionParameters = null; this._Image = null; // ToDo: Support to change format? this._Codec = OpenJpeg.CreateDecompress(CodecFormat.J2k); //this._Codec = OpenJpeg.CreateDecompress(CodecFormat.Jp2); this._DecompressionParameters = new DecompressionParameters(); OpenJpeg.SetDefaultDecoderParameters(this._DecompressionParameters); if (!OpenJpeg.SetupDecoder(this._Codec, this._DecompressionParameters)) { return(false); } if (!OpenJpeg.ReadHeader(this._Stream, this._Codec, out var image)) { return(false); } this.Width = (int)(image.X1 - image.X0); this.Height = (int)(image.Y1 - image.Y0); this._Image = image; return(true); }
public void Decode() { var targets = new[] { //new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.Unknown, Result = false }, new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.J2k, Result = true }, //new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.Jp2, Result = false }, //new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.Jpp, Result = false }, //new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.Jpt, Result = false }, //new { Name = "Bretagne1_0.j2k", IsReadStream = true, Format = CodecFormat.Jpx, Result = false } }; foreach (var target in targets) { var path = Path.GetFullPath(Path.Combine(TestImageDirectory, target.Name)); var stream = OpenJpeg.StreamCreateDefaultFileStream(path, target.IsReadStream); var codec = OpenJpeg.CreateDecompress(target.Format); var decompressionParameters = new DecompressionParameters(); OpenJpeg.SetDefaultDecoderParameters(decompressionParameters); Assert.True(OpenJpeg.SetupDecoder(codec, decompressionParameters) == target.Result, $"Failed to invoke {nameof(OpenJpeg.SetupDecoder)} for {target.Format} and {target.IsReadStream}"); Assert.True(OpenJpeg.ReadHeader(stream, codec, out var image) == target.Result, $"Failed to invoke {nameof(OpenJpeg.ReadHeader)} for {target.Format} and {target.IsReadStream}"); Assert.True(OpenJpeg.SetDecodeArea(codec, image, 0, 0, 0, 0) == target.Result, $"Failed to invoke {nameof(OpenJpeg.SetDecodeArea)} for {target.Format} and {target.IsReadStream}"); Assert.True(OpenJpeg.Decode(codec, stream, image) == target.Result, $"Failed to invoke {nameof(OpenJpeg.Decode)} for {target.Format} and {target.IsReadStream}"); Assert.True(OpenJpeg.EndDecompress(codec, stream) == target.Result, $"Failed to invoke {nameof(OpenJpeg.EndDecompress)} for {target.Format} and {target.IsReadStream}"); this.DisposeAndCheckDisposedState(image); this.DisposeAndCheckDisposedState(stream); this.DisposeAndCheckDisposedState(decompressionParameters); this.DisposeAndCheckDisposedState(codec); } }
private static Image DecodeImageFromFile(string path, CodecFormat format) { using var stream = OpenJpeg.StreamCreateDefaultFileStream(path, true); using var codec = OpenJpeg.CreateDecompress(format); using var decompressionParameters = new DecompressionParameters(); OpenJpeg.SetDefaultDecoderParameters(decompressionParameters); OpenJpeg.SetupDecoder(codec, decompressionParameters); OpenJpeg.ReadHeader(stream, codec, out var image); OpenJpeg.SetDecodeArea(codec, image, 0, 0, 0, 0); OpenJpeg.Decode(codec, stream, image); OpenJpeg.EndDecompress(codec, stream); return(image); }