Esempio n. 1
0
        public void IsValid_FalseAfterDisposal()
        {
            using var g = MemoryGroup <byte> .Allocate(this.MemoryAllocator, 10, 100);

            g.Dispose();
            Assert.False(g.IsValid);
        }
            public void MemoryAllocatorIsUtilizedCorrectly(AllocationOptions allocationOptions)
            {
                this.MemoryAllocator.BufferCapacityInBytes = 200;

                HashSet <int> bufferHashes;

                int expectedBlockCount = 5;

                using (var g = MemoryGroup <short> .Allocate(this.MemoryAllocator, 500, 100, allocationOptions))
                {
                    IReadOnlyList <TestMemoryAllocator.AllocationRequest> allocationLog = this.MemoryAllocator.AllocationLog;
                    Assert.Equal(expectedBlockCount, allocationLog.Count);
                    bufferHashes = allocationLog.Select(l => l.HashCodeOfBuffer).ToHashSet();
                    Assert.Equal(expectedBlockCount, bufferHashes.Count);
                    Assert.Equal(0, this.MemoryAllocator.ReturnLog.Count);

                    for (int i = 0; i < expectedBlockCount; i++)
                    {
                        Assert.Equal(allocationOptions, allocationLog[i].AllocationOptions);
                        Assert.Equal(100, allocationLog[i].Length);
                        Assert.Equal(200, allocationLog[i].LengthInBytes);
                    }
                }

                Assert.Equal(expectedBlockCount, this.MemoryAllocator.ReturnLog.Count);
                Assert.True(bufferHashes.SetEquals(this.MemoryAllocator.ReturnLog.Select(l => l.HashCodeOfBuffer)));
            }
            public void BufferSizesAreCorrect <T>(
                T dummy,
                int bufferCapacity,
                int bufferAlignment,
                long totalLength,
                int expectedNumberOfBuffers,
                int expectedBufferSize,
                int expectedSizeOfLastBuffer)
                where T : struct
            {
                this.MemoryAllocator.BufferCapacityInBytes = bufferCapacity;

                // Act:
                using var g = MemoryGroup <T> .Allocate(this.MemoryAllocator, totalLength, bufferAlignment);

                // Assert:
                Assert.Equal(expectedNumberOfBuffers, g.Count);

                if (expectedBufferSize >= 0)
                {
                    Assert.Equal(expectedBufferSize, g.BufferLength);
                }

                if (g.Count == 0)
                {
                    return;
                }

                for (int i = 0; i < g.Count - 1; i++)
                {
                    Assert.Equal(g[i].Length, expectedBufferSize);
                }

                Assert.Equal(g.Last().Length, expectedSizeOfLastBuffer);
            }
Esempio n. 4
0
        public void Init()
        {
            this.WidthInBlocks = (int)MathF.Ceiling(
                MathF.Ceiling(this.Frame.SamplesPerLine / 8F) * this.HorizontalSamplingFactor / this.Frame.MaxHorizontalFactor);

            this.HeightInBlocks = (int)MathF.Ceiling(
                MathF.Ceiling(this.Frame.Scanlines / 8F) * this.VerticalSamplingFactor / this.Frame.MaxVerticalFactor);

            int blocksPerLineForMcu   = this.Frame.McusPerLine * this.HorizontalSamplingFactor;
            int blocksPerColumnForMcu = this.Frame.McusPerColumn * this.VerticalSamplingFactor;

            this.SizeInBlocks = new Size(blocksPerLineForMcu, blocksPerColumnForMcu);

            JpegComponent c0 = this.Frame.Components[0];

            this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors);

            if (this.SubSamplingDivisors.Width == 0 || this.SubSamplingDivisors.Height == 0)
            {
                JpegThrowHelper.ThrowBadSampling();
            }

            int totalNumberOfBlocks = blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
            int width  = this.WidthInBlocks + 1;
            int height = totalNumberOfBlocks / width;

            var buf = MemoryGroup <Block8x8> .Allocate(width *height, height);

            this.SpectralBlocks = new Buffer2D <Block8x8>(buf, width, height);
        }
Esempio n. 5
0
        public Bitmap Load()
        {
            if (this.ImageWidth == 0 || this.ImageHeight == 0)
            {
                JpegThrowHelper.ThrowInvalidImageDimensions(this.ImageWidth, this.ImageHeight);
            }

            var buffer = new Buffer2D <Vector4>(MemoryGroup <Vector4> .Allocate(this.ImageWidth * this.ImageHeight, this.ImageWidth), this.ImageWidth, this.ImageHeight);

            using (var postProcessor = new JpegImagePostProcessor(this.RawData))
            {
                postProcessor.PostProcess(buffer, new System.Threading.CancellationToken());
            }

            var bitmap = new Bitmap(this.ImageWidth, this.ImageHeight);

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    ref var p = ref buffer[x, y];
                    bitmap.PixelBufferView[x, y] = new Pixel()
                    {
                        A = p.W,
                        B = p.Z,
                        G = p.Y,
                        R = p.X
                    };
                }
            }
            public void WhenBlockAlignmentIsOverCapacity_Throws_InvalidMemoryOperationException()
            {
                this.MemoryAllocator.BufferCapacityInBytes = 84; // 42 * Int16

                Assert.Throws <InvalidMemoryOperationException>(() =>
                {
                    MemoryGroup <short> .Allocate(this.MemoryAllocator, 50, 43);
                });
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="JpegImagePostProcessor"/> class.
        /// </summary>
        /// <param name="configuration">The <see cref="Configuration"/> to configure internal operations.</param>
        /// <param name="rawJpeg">The <see cref="IRawJpegData"/> representing the uncompressed spectral Jpeg data</param>
        public JpegImagePostProcessor(IRawJpegData rawJpeg)
        {
            this.RawJpeg = rawJpeg;
            IJpegComponent c0 = rawJpeg.Components[0];

            this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / BlockRowsPerStep;
            this.PostProcessorBufferSize    = new Size(c0.SizeInBlocks.Width * 8, PixelRowsPerStep);

            this.ComponentProcessors = new JpegComponentPostProcessor[rawJpeg.Components.Length];
            for (int i = 0; i < rawJpeg.Components.Length; i++)
            {
                this.ComponentProcessors[i] = new JpegComponentPostProcessor(this, rawJpeg.Components[i]);
            }

            this.rgbaBuffer = MemoryGroup <Vector4> .Allocate(rawJpeg.ImageSizeInPixels.Width);

            this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace, rawJpeg.Precision);
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JpegComponentPostProcessor"/> class.
        /// </summary>
        public JpegComponentPostProcessor(JpegImagePostProcessor imagePostProcessor, IJpegComponent component)
        {
            Buffer2D <float> Allocate2DOveraligned()
            {
                long groupLength = (long)imagePostProcessor.PostProcessorBufferSize.Width * imagePostProcessor.PostProcessorBufferSize.Height;
                MemoryGroup <float> memoryGroup = MemoryGroup <float> .Allocate(
                    groupLength,
                    imagePostProcessor.PostProcessorBufferSize.Width *this.blockAreaSize.Height);

                return(new Buffer2D <float>(memoryGroup, imagePostProcessor.PostProcessorBufferSize.Width, imagePostProcessor.PostProcessorBufferSize.Height));
            }

            this.Component          = component;
            this.ImagePostProcessor = imagePostProcessor;
            this.blockAreaSize      = this.Component.SubSamplingDivisors * 8;
            this.ColorBuffer        = Allocate2DOveraligned();

            this.BlockRowsPerStep = JpegImagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height;
        }
        /// <summary>
        /// Create a group, either uninitialized or filled with incrementing numbers starting with 1.
        /// </summary>
        internal MemoryGroup <int> CreateTestGroup(long totalLength, int bufferLength, bool fillSequence = false)
        {
            this.MemoryAllocator.BufferCapacityInBytes = bufferLength * sizeof(int);
            var g = MemoryGroup <int> .Allocate(this.MemoryAllocator, totalLength, bufferLength);

            if (!fillSequence)
            {
                return(g);
            }

            int j = 1;

            for (MemoryGroupIndex i = g.MinIndex(); i < g.MaxIndex(); i += 1)
            {
                g.SetElementAt(i, j);
                j++;
            }

            return(g);
        }
Esempio n. 10
0
        /// <summary>
        /// Encode writes the image to the jpeg baseline format with the given options.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The image to write from.</param>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="cancellationToken">The token to request cancellation.</param>
        public void Encode(
            Bitmap bitmap,
            Stream stream,
            ImageMetadata?metadata = null,
            CancellationToken cancellationToken = new CancellationToken())
        {
            using var buffer2d = new Buffer2D <Pixel>(MemoryGroup <Pixel> .Allocate(bitmap.Width * bitmap.Height, bitmap.Width), bitmap.Width, bitmap.Height);

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    ref var p = ref bitmap.PixelBufferView[x, y];
                    buffer2d[x, y] = new Pixel
                    {
                        R = p.R,
                        G = p.G,
                        B = p.B,
                        A = p.A
                    };
                }
            }
Esempio n. 11
0
        public void IsValid_TrueAfterCreation()
        {
            using var g = MemoryGroup <byte> .Allocate(this.MemoryAllocator, 10, 100);

            Assert.True(g.IsValid);
        }