Пример #1
0
        public static unsafe void Copy <T>(Span <T> source, Span <T> destination, int count)
            where T : struct
        {
            DebugGuard.MustBeLessThanOrEqualTo(count, source.Length, nameof(count));
            DebugGuard.MustBeLessThanOrEqualTo(count, destination.Length, nameof(count));

            ref byte srcRef  = ref Unsafe.As <T, byte>(ref source.DangerousGetPinnableReference());
Пример #2
0
 public BasicArrayBuffer(T[] array, int length, TestMemoryAllocator allocator)
 {
     this.allocator = allocator;
     DebugGuard.MustBeLessThanOrEqualTo(length, array.Length, nameof(length));
     this.Array  = array;
     this.Length = length;
 }
Пример #3
0
        public static unsafe void Copy <T>(Span <T> source, Span <T> destination, int count)
            where T : struct
        {
            DebugGuard.MustBeLessThanOrEqualTo(count, source.Length, nameof(count));
            DebugGuard.MustBeLessThanOrEqualTo(count, destination.Length, nameof(count));

            ref byte srcRef  = ref Unsafe.As <T, byte>(ref MemoryMarshal.GetReference(source));
Пример #4
0
        public int ReadSignedValue(int nBits)
        {
            DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
            DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));

            int value = (int)this.ReadValue(nBits);

            return(this.ReadValue(1) != 0 ? -value : value);
        }
Пример #5
0
        private static void WriteRun(ReadOnlySpan <byte> rowSpan, int start, int runLength, Span <byte> compressedRowSpan, int compressedRowPos)
        {
            DebugGuard.MustBeLessThanOrEqualTo(runLength, 127, nameof(runLength));

            sbyte headerByte = (sbyte)(-runLength + 1);

            compressedRowSpan[compressedRowPos]     = (byte)headerByte;
            compressedRowSpan[compressedRowPos + 1] = rowSpan[start];
        }
Пример #6
0
        private static void WriteLiteralRun(ReadOnlySpan <byte> rowSpan, int end, int literalRunLength, Span <byte> compressedRowSpan, int compressedRowPos)
        {
            DebugGuard.MustBeLessThanOrEqualTo(literalRunLength, 127, nameof(literalRunLength));

            int   literalRunStart = end - literalRunLength;
            sbyte runLength       = (sbyte)(literalRunLength - 1);

            compressedRowSpan[compressedRowPos] = (byte)runLength;
            rowSpan.Slice(literalRunStart, literalRunLength).CopyTo(compressedRowSpan.Slice(compressedRowPos + 1));
        }
Пример #7
0
        public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException()
        {
            var exception = Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                DebugGuard.MustBeLessThanOrEqualTo(2, 1, "myParamName");
            });

            Assert.Equal("myParamName", exception.ParamName);
            Assert.True(exception.Message.Contains($"Value must be less than or equal to 1."));
        }
Пример #8
0
        public BufferArea(Buffer2D <T> destinationBuffer, Rectangle rectangle)
        {
            DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.X, 0, nameof(rectangle));
            DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.Y, 0, nameof(rectangle));
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, destinationBuffer.Width, nameof(rectangle));
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, destinationBuffer.Height, nameof(rectangle));

            this.DestinationBuffer = destinationBuffer;
            this.Rectangle         = rectangle;
        }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArraySlice{T}"/> struct.
        /// </summary>
        /// <param name="data">The underlying data buffer.</param>
        /// <param name="start">The offset position in the underlying buffer this slice was created from.</param>
        /// <param name="length">The number of items in the slice.</param>
        public ArraySlice(T[] data, int start, int length)
        {
            DebugGuard.MustBeGreaterThanOrEqualTo(start, 0, nameof(start));
            DebugGuard.MustBeLessThanOrEqualTo(length, data.Length, nameof(length));
            DebugGuard.MustBeLessThanOrEqualTo(start + length, data.Length, nameof(this.data));

            this.data   = data;
            this.Start  = start;
            this.Length = length;
        }
Пример #10
0
        public Buffer2DRegion(Buffer2D <T> buffer, Rectangle rectangle)
        {
            DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.X, 0, nameof(rectangle));
            DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.Y, 0, nameof(rectangle));
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, buffer.Width, nameof(rectangle));
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, buffer.Height, nameof(rectangle));

            this.Buffer    = buffer;
            this.Rectangle = rectangle;
        }
Пример #11
0
        public BufferArea <T> GetSubArea(Rectangle rectangle)
        {
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, this.Rectangle.Width, nameof(rectangle));
            DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, this.Rectangle.Height, nameof(rectangle));

            int x = this.Rectangle.X + rectangle.X;
            int y = this.Rectangle.Y + rectangle.Y;

            rectangle = new Rectangle(x, y, rectangle.Width, rectangle.Height);
            return(new BufferArea <T>(this.DestinationBuffer, rectangle));
        }
Пример #12
0
        /// <summary>
        /// Decompresses image data into the supplied buffer.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> to read image data from.</param>
        /// <param name="stripOffset">The strip offset of stream.</param>
        /// <param name="stripByteCount">The number of bytes to read from the input stream.</param>
        /// <param name="stripHeight">The height of the strip.</param>
        /// <param name="buffer">The output buffer for uncompressed data.</param>
        public void Decompress(BufferedReadStream stream, ulong stripOffset, ulong stripByteCount, int stripHeight, Span <byte> buffer)
        {
            DebugGuard.MustBeLessThanOrEqualTo(stripOffset, (ulong)long.MaxValue, nameof(stripOffset));
            DebugGuard.MustBeLessThanOrEqualTo(stripByteCount, (ulong)long.MaxValue, nameof(stripByteCount));

            stream.Seek((long)stripOffset, SeekOrigin.Begin);
            this.Decompress(stream, (int)stripByteCount, stripHeight, buffer);

            if ((long)stripOffset + (long)stripByteCount < stream.Position)
            {
                TiffThrowHelper.ThrowImageFormatException("Out of range when reading a strip.");
            }
        }
Пример #13
0
        /// <summary>
        /// Gets the width of the image frame.
        /// </summary>
        /// <param name="exifProfile">The image frame exif profile.</param>
        /// <returns>The image width.</returns>
        private static int GetImageWidth(ExifProfile exifProfile)
        {
            IExifValue <Number> width = exifProfile.GetValue(ExifTag.ImageWidth);

            if (width == null)
            {
                TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageWidth");
            }

            DebugGuard.MustBeLessThanOrEqualTo((ulong)width.Value, (ulong)int.MaxValue, nameof(ExifTag.ImageWidth));

            return((int)width.Value);
        }
Пример #14
0
        public uint ReadValue(int nBits)
        {
            DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
            DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));

            uint v = 0;

            while (nBits-- > 0)
            {
                v |= (uint)this.GetBit(0x80) << nBits;
            }

            return(v);
        }
Пример #15
0
            internal Enumerator(ArraySlice <T> slice)
            {
                DebugGuard.NotNull(slice.data, nameof(slice.data));
                DebugGuard.MustBeGreaterThanOrEqualTo(slice.Start, 0, nameof(slice.Start));
                DebugGuard.MustBeGreaterThanOrEqualTo(slice.Length, 0, nameof(slice.Length));

                DebugGuard.MustBeLessThanOrEqualTo(
                    slice.Start + slice.Length,
                    slice.data.Length,
                    nameof(slice.data.Length));

                this.array   = slice.data;
                this.start   = slice.Start;
                this.end     = slice.Start + slice.Length;
                this.current = slice.Start - 1;
            }
Пример #16
0
        /// <summary>
        /// Calculates the size (in bytes) for a pixel buffer using the determined color format.
        /// </summary>
        /// <param name="width">The width for the desired pixel buffer.</param>
        /// <param name="height">The height for the desired pixel buffer.</param>
        /// <param name="plane">The index of the plane for planar image configuration (or zero for chunky).</param>
        /// <returns>The size (in bytes) of the required pixel buffer.</returns>
        private int CalculateStripBufferSize(int width, int height, int plane = -1)
        {
            DebugGuard.MustBeLessThanOrEqualTo(plane, 3, nameof(plane));

            int bitsPerPixel = 0;

            if (this.PlanarConfiguration == TiffPlanarConfiguration.Chunky)
            {
                DebugGuard.IsTrue(plane == -1, "Expected Chunky planar.");
                bitsPerPixel = this.BitsPerPixel;
            }
            else
            {
                switch (plane)
                {
                case 0:
                    bitsPerPixel = this.BitsPerSample.Channel0;
                    break;

                case 1:
                    bitsPerPixel = this.BitsPerSample.Channel1;
                    break;

                case 2:
                    bitsPerPixel = this.BitsPerSample.Channel2;
                    break;

                default:
                    TiffThrowHelper.ThrowNotSupported("More then 3 color channels are not supported");
                    break;
                }
            }

            int bytesPerRow = ((width * bitsPerPixel) + 7) / 8;

            return(bytesPerRow * height);
        }
        public Span <float> ScanOddEven(float y, Span <ScanEdge> edges, Span <float> intersections)
        {
            DebugGuard.MustBeLessThanOrEqualTo(edges.Length, MaxEdges, "edges.Length");

            int intersectionCounter = 0;
            int offset = 0;

            Span <int> active = this.ActiveEdges;

            for (int i = 0; i < active.Length; i++)
            {
                int          flaggedIdx = active[i];
                int          edgeIdx    = Strip(flaggedIdx);
                ref ScanEdge edge       = ref edges[edgeIdx];
                float        x          = edge.GetX(y);
                if (IsEntering(flaggedIdx))
                {
                    Emit(x, edge.EmitV0, intersections, ref intersectionCounter);
                }
                else if (IsLeaving(flaggedIdx))
                {
                    Emit(x, edge.EmitV1, intersections, ref intersectionCounter);

                    offset++;

                    // Do not offset:
                    continue;
                }
                else
                {
                    // Emit once:
                    intersections[intersectionCounter++] = x;
                }

                // Unmask and offset:
                active[i - offset] = edgeIdx;
            }
Пример #18
0
        public void Update(byte[] buffer, int offset, int count)
        {
            DebugGuard.NotNull(buffer, nameof(buffer));
            DebugGuard.MustBeGreaterThanOrEqualTo(offset, 0, nameof(offset));
            DebugGuard.MustBeGreaterThanOrEqualTo(count, 0, nameof(count));
            DebugGuard.MustBeLessThan(offset, buffer.Length, nameof(offset));
            DebugGuard.MustBeLessThanOrEqualTo(offset + count, buffer.Length, nameof(count));

            // (By Per Bothner)
            uint s1 = this.checksum & 0xFFFF;
            uint s2 = this.checksum >> 16;

            while (count > 0)
            {
                // We can defer the modulo operation:
                // s1 maximally grows from 65521 to 65521 + 255 * 3800
                // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
                int n = 3800;
                if (n > count)
                {
                    n = count;
                }

                count -= n;
                while (--n >= 0)
                {
                    s1 = s1 + (uint)(buffer[offset++] & 0xff);
                    s2 = s2 + s1;
                }

                s1 %= Base;
                s2 %= Base;
            }

            this.checksum = (s2 << 16) | s1;
        }
Пример #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BasicArrayBuffer{T}"/> class
 /// </summary>
 /// <param name="array">The array</param>
 /// <param name="length">The length of the buffer</param>
 public BasicArrayBuffer(T[] array, int length)
 {
     DebugGuard.MustBeLessThanOrEqualTo(length, array.Length, nameof(length));
     this.Array  = array;
     this.Length = length;
 }
Пример #20
0
 public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max)
 {
     DebugGuard.MustBeLessThanOrEqualTo(value, max, "myParamName");
 }