public void ImageComponentParametersBpp()
 {
     using var parameter = new ImageComponentParameters();
     foreach (var value in new[] { 0u, 1u, 10u })
     {
         parameter.Bpp = value;
         Assert.Equal(value, parameter.Bpp);
     }
 }
 public void ImageComponentParametersSigned()
 {
     using var parameter = new ImageComponentParameters();
     foreach (var value in new[] { true, false })
     {
         parameter.Signed = value;
         Assert.Equal(value, parameter.Signed);
     }
 }
 public void ImageComponentParametersPrecision()
 {
     using var parameter = new ImageComponentParameters();
     foreach (var value in new[] { 0u, 1u, 10u })
     {
         parameter.Precision = value;
         Assert.Equal(value, parameter.Precision);
     }
 }
        private static Image CreateImage(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.ImageTileCreate(numComps, parameters, ColorSpace.Srgb);

            foreach (var parameter in parameters)
            {
                parameter.Dispose();
            }

            return(image);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        public void Compress()
        {
            var targets = new[]
            {
                new { Format = CodecFormat.Unknown, FileName = $"{nameof(this.Compress)}.ukn", Result = false },
                new { Format = CodecFormat.J2k, FileName = $"{nameof(this.Compress)}.j2k", Result = true },
                new { Format = CodecFormat.Jp2, FileName = $"{nameof(this.Compress)}.jp2", Result = true },
                new { Format = CodecFormat.Jpp, FileName = $"{nameof(this.Compress)}.jpp", Result = false },
                new { Format = CodecFormat.Jpt, FileName = $"{nameof(this.Compress)}.jpt", Result = false },
                new { Format = CodecFormat.Jpx, FileName = $"{nameof(this.Compress)}.jpx", Result = false },
            };

            const int  numCompsMax            = 4;
            const int  codeBlockWidthInitial  = 64;
            const int  codeBlockHeightInitial = 64;
            const int  numComps     = 3;
            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;

            var tilesWidth  = (offsetX + imageWidth + tileWidth - 1) / tileWidth;
            var tilesHeight = (offsetY + imageHeight + tileHeight - 1) / tileHeight;
            var tiles       = tilesWidth * tilesHeight;
            var dataSize    = tileWidth * tileHeight * numComps * (compPrec / 8);

            var data = new byte[dataSize];

            for (var index = 0; index < data.Length; index++)
            {
                data[index] = (byte)(index % byte.MaxValue);
            }

            foreach (var target in targets)
            {
                var codec = OpenJpeg.CreateCompress(target.Format);
                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 image = OpenJpeg.ImageTileCreate(numComps, parameters, ColorSpace.Srgb);
                image.X0         = offsetX;
                image.Y0         = offsetY;
                image.X1         = offsetX + imageWidth;
                image.Y1         = offsetY + imageHeight;
                image.ColorSpace = ColorSpace.Srgb;

                Directory.CreateDirectory(ResultDirectory);
                Directory.CreateDirectory(Path.Combine(ResultDirectory, nameof(this.Compress)));
                var path = Path.Combine(ResultDirectory, nameof(this.Compress), target.FileName);

                Assert.True(OpenJpeg.SetupEncoder(codec, compressionParameters, image) == target.Result, $"Failed to invoke {nameof(OpenJpeg.SetupDecoder)} for {target.Format}");
                if (!target.Result)
                {
                    this.DisposeAndCheckDisposedState(image);
                    this.DisposeAndCheckDisposedState(compressionParameters);
                    this.DisposeAndCheckDisposedState(codec);
                    continue;
                }

                var stream = OpenJpeg.StreamCreateDefaultFileStream(path, false);

                OpenJpeg.StartCompress(codec, image, stream);

                for (var i = 0; i < tiles; ++i)
                {
                    var tileY    = (uint)(i / tilesWidth);
                    var tileX    = (uint)(i % tilesHeight);
                    var tileX0   = Math.Max(image.X0, tileX * tileWidth);
                    var tileY0   = Math.Max(image.Y0, tileY * tileHeight);
                    var tileX1   = Math.Min(image.X1, (tileX + 1) * tileWidth);
                    var tileY1   = Math.Min(image.Y1, (tileY + 1) * tileHeight);
                    var tilesize = (tileX1 - tileX0) * (tileY1 - tileY0) * numComps * (compPrec / 8);
                    Assert.True(OpenJpeg.WriteTile(codec, i, data, tilesize, stream), $"Failed to invoke {nameof(OpenJpeg.WriteTile)}");
                }

                OpenJpeg.EndCompress(codec, stream);

                this.DisposeAndCheckDisposedState(stream);
                this.DisposeAndCheckDisposedState(image);
                this.DisposeAndCheckDisposedState(compressionParameters);
                this.DisposeAndCheckDisposedState(codec);
            }
        }