public void ImageCreate() { const uint numComps = 3; const int numCompsMax = 4; const int codeBlockWidthInitial = 64; const int codeBlockHeightInitial = 64; const int imageWidth = 2000; const int imageHeight = 2000; const int tileWidth = 1000; const int tileHeight = 1000; const uint compPrec = 8; const bool irreversible = false; const uint offsetX = 0; const uint offsetY = 0; using var codec = OpenJpeg.CreateCompress(CodecFormat.Jp2); using var compressionParameters = new CompressionParameters(); OpenJpeg.SetDefaultEncoderParameters(compressionParameters); compressionParameters.TcpNumLayers = 1; compressionParameters.CodingParameterFixedQuality = 1; compressionParameters.TcpDistoratio[0] = 20; compressionParameters.CodingParameterTx0 = 0; compressionParameters.CodingParameterTy0 = 0; compressionParameters.TileSizeOn = true; compressionParameters.CodingParameterTdx = tileWidth; compressionParameters.CodingParameterTdy = tileHeight; compressionParameters.CodeBlockWidthInitial = codeBlockWidthInitial; compressionParameters.CodeBlockHeightInitial = codeBlockHeightInitial; compressionParameters.Irreversible = irreversible; var parameters = new ImageComponentParameters[numCompsMax]; for (var index = 0; index < parameters.Length; index++) { parameters[index] = new ImageComponentParameters { Dx = 1, Dy = 1, Height = imageHeight, Width = imageWidth, Signed = false, Precision = compPrec, X0 = offsetX, Y0 = offsetY }; } var data = new byte[imageWidth * imageHeight]; for (var index = 0; index < data.Length; index++) { data[index] = (byte)(index % byte.MaxValue); } var image = OpenJpeg.ImageCreate(numComps, parameters, ColorSpace.Srgb); foreach (var parameter in parameters) { this.DisposeAndCheckDisposedState(parameter); } this.DisposeAndCheckDisposedState(image); }
public byte[] Write(Bitmap bitmap) { if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } this._Codec?.Dispose(); this._CompressionParameters?.Dispose(); this._Image?.Dispose(); var channels = 0; var outPrecision = 0u; var colorSpace = ColorSpace.Gray; var format = bitmap.PixelFormat; var width = bitmap.Width; var height = bitmap.Height; switch (format) { case PixelFormat.Format24bppRgb: channels = 3; outPrecision = 24u / (uint)channels; colorSpace = ColorSpace.Srgb; break; } var componentParametersArray = new ImageComponentParameters[channels]; for (var i = 0; i < channels; i++) { componentParametersArray[i].Precision = outPrecision; componentParametersArray[i].Bpp = outPrecision; componentParametersArray[i].Signed = false; componentParametersArray[i].Dx = (uint)this._CompressionParameters.SubsamplingDx; componentParametersArray[i].Dy = (uint)this._CompressionParameters.SubsamplingDy; componentParametersArray[i].Width = (uint)width; componentParametersArray[i].Height = (uint)height; } Image image = null; try { // ToDo: throw proper exception image = OpenJpeg.ImageCreate((uint)channels, componentParametersArray, colorSpace); if (image == null) { throw new ArgumentException(); } // ToDo: support alpha components //switch (channels) //{ // case 2: // case 4: // image.Components[(int)(channels - 1)].Alpha = 1; // break; //} image.X0 = 0; image.Y0 = 0; image.X1 = componentParametersArray[0].Dx * componentParametersArray[0].Width; image.Y1 = componentParametersArray[0].Dy * componentParametersArray[0].Height; //std::vector<OPJ_INT32*> outcomps(channels, nullptr); //switch (channels) //{ // case 1: // outcomps.assign({ image.Components[0].data }); // break; // // Reversed order for BGR -> RGB conversion // case 2: // outcomps.assign({ image.Components[0].data, image.Components[1].data }); // break; // case 3: // outcomps.assign({ image.Components[2].data, image.Components[1].data, image.Components[0].data }); // break; // case 4: // outcomps.assign({ // image.Components[2].data, image.Components[1].data, image.Components[0].data, // image.Components[3].data }); // break; //} } finally { image?.Dispose(); } return(null); }