Beispiel #1
0
        private static void WarnIfClipped(JP2Codestream src, CodMarker cod)
        {
            bool tileSizePow2       = true;
            bool isClippedCodeblock = false;
            int  diffTileCblk;

            tileSizePow2       &= BitHacks.IsPowerOf2((uint)src.TileSize.Width);
            tileSizePow2       &= BitHacks.IsPowerOf2((uint)src.TileSize.Height);
            diffTileCblk        = BitHacks.LogFloor2((uint)src.TileSize.Width);
            diffTileCblk       -= cod.CodeblockWidth;
            isClippedCodeblock |= diffTileCblk < cod.DecompositionLevels;
            diffTileCblk        = BitHacks.LogFloor2((uint)src.TileSize.Height);
            diffTileCblk       -= cod.CodeblockHeight;
            isClippedCodeblock |= diffTileCblk < cod.DecompositionLevels;
            if (isClippedCodeblock && !tileSizePow2)
            {
                var sb = new StringBuilder();
                Console.WriteLine(sb
                                  .AppendLine(
                                      "Codeblock in one of the subbands is clipped by the ")
                                  .AppendLine(
                                      "actual subband dimensions, to perform merging well ")
                                  .AppendLine(
                                      "input origin must be aligned at the same position ")
                                  .AppendLine(
                                      "as their output coordinates")
                                  .ToString());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Calculates the length in bytes of all the TLM markers
        /// that are required to
        /// </summary>
        /// <param name="tileparts"></param>
        /// <returns></returns>
        private static int GetTotalTlmLength(int tileparts)
        {
            int tlmCount = BitHacks.DivCeil(tileparts, TlmMarker.MaxEntries);

            if (tlmCount > TlmMarker.MaxMarkers)
            {
                throw new InvalidOperationException(
                          String.Concat(string.Format(
                                            "too many tile-parts {0}, cannot create more ",
                                            "than {1} TLM markers, each marker containing up to ",
                                            "{2} entries",
                                            tileparts, TlmMarker.MaxMarkers, TlmMarker.MaxEntries)));
            }
            int tilepartsRemainder =
                tileparts - ((tlmCount - 1) * TlmMarker.MaxEntries);
            // calculate length for all the full TLM markers
            int tlmLength =
                (tlmCount - 1) * TlmMarker.LengthOf(TlmMarker.MaxEntries);

            // add the length of all TLM markers which are left.
            tlmLength += TlmMarker.LengthOf(tilepartsRemainder);
            // TlmMarker.LengthOf returns the Length of the TLM
            // field as signaled in the marker segment, without
            // without the 2 bytes of the marker itself.
            tlmLength += tlmCount * MarkerSegment.MarkerLength;
            return(tlmLength);
        }
Beispiel #3
0
        public void IsSet_Smoke_Test()
        {
            var binaryString = "1100";

            Assert.IsTrue(BitHacks.IsSet(Convert.ToInt32(binaryString, 2), 2));
            Assert.IsFalse(BitHacks.IsSet(Convert.ToInt32(binaryString, 2), 1));
        }
Beispiel #4
0
        public void HasOppositeSigns_Smoke_Test()
        {
            Assert.IsTrue(BitHacks.HasOppositeSigns(11, -22));
            Assert.IsTrue(BitHacks.HasOppositeSigns(-11, 22));

            Assert.IsFalse(BitHacks.HasOppositeSigns(21, 22));
            Assert.IsFalse(BitHacks.HasOppositeSigns(-21, -22));
        }
Beispiel #5
0
        public void CountSetBits_Smoke_Test()
        {
            var binaryString = "1011";

            Assert.AreEqual(3, BitHacks.CountSetBits(Convert.ToInt32(binaryString, 2)));

            binaryString = "1111";
            Assert.AreEqual(4, BitHacks.CountSetBits(Convert.ToInt32(binaryString, 2)));
        }
Beispiel #6
0
        public void UpdateBitToValue_Smoke_Test()
        {
            var binaryString = "1011";

            Assert.AreEqual("1111", Convert.ToString(BitHacks.UpdateBitToValue(Convert.ToInt32(binaryString, 2), 2, true), 2));

            binaryString = "1111";
            Assert.AreEqual("1011", Convert.ToString(BitHacks.UpdateBitToValue(Convert.ToInt32(binaryString, 2), 2, false), 2));
        }
Beispiel #7
0
        public void TurnOnBitAfterRightmostSetBit_Smoke_Test()
        {
            var binaryString = "1100";

            Assert.AreEqual("1110", Convert.ToString(BitHacks.TurnOnBitAfterRightmostSetBit(Convert.ToInt32(binaryString, 2)), 2));

            binaryString = "1101";
            Assert.AreEqual("1101", Convert.ToString(BitHacks.TurnOnBitAfterRightmostSetBit(Convert.ToInt32(binaryString, 2)), 2));
        }
Beispiel #8
0
        private Size CalculatePrecinctCount(ushort tileIdx, int resolution)
        {
            Rectangle tile      = Tile[resolution];
            Point     partition = Log2Partitions[resolution];
            int       minHorz   = tile.Left >> partition.X;
            int       minVert   = tile.Top >> partition.Y;
            int       maxHorz   = BitHacks.DivShiftCeil(tile.Right, partition.X);
            int       maxVert   = BitHacks.DivShiftCeil(tile.Bottom, partition.Y);

            return(new Size(maxHorz - minHorz, maxVert - minVert));
        }
Beispiel #9
0
        public void CountTrailingZerosUsingBinarySearch_Smoke_Test()
        {
            var binaryString = "1000";

            Assert.AreEqual(3, BitHacks.CountTrailingZerosByBinarySearch(Convert.ToInt32(binaryString, 2)));

            binaryString = "1111";
            Assert.AreEqual(0, BitHacks.CountTrailingZerosByBinarySearch(Convert.ToInt32(binaryString, 2)));

            binaryString = "11110110000000000000000000000000";
            Assert.AreEqual(25, BitHacks.CountTrailingZerosByBinarySearch(Convert.ToInt32(binaryString, 2)));
        }
Beispiel #10
0
        public void RoundToPower()
        {
            uint a = 2;

            Assert.AreEqual(2, BitHacks.RoundToPower(a));
            a = 3;
            Assert.AreEqual(4, BitHacks.RoundToPower(a));
            a = 128;
            Assert.AreEqual(128, BitHacks.RoundToPower(a));
            a = 65;
            Assert.AreEqual(128, BitHacks.RoundToPower(a));
            a = 999;
            Assert.AreEqual(1024, BitHacks.RoundToPower(a));
            a = 1;
            Assert.AreEqual(1, BitHacks.RoundToPower(a));
        }
Beispiel #11
0
        private static int createEvenMask()
        {
            var mask   = 1;
            var result = 0;

            for (int i = 0; i < 32; i++)
            {
                if (!BitHacks.IsEven(i))
                {
                    result = result | mask;
                }

                mask = mask << 1;
            }

            return(result);
        }
Beispiel #12
0
        public void LogCeiling()
        {
            uint a = 2;

            Assert.AreEqual(1, BitHacks.Power2MSB(a));
            a = 4;
            Assert.AreEqual(2, BitHacks.Power2MSB(a));
            a = 8;
            Assert.AreEqual(3, BitHacks.Power2MSB(a));
            a = 16;
            Assert.AreEqual(4, BitHacks.Power2MSB(a));
            a = 32;
            Assert.AreEqual(5, BitHacks.Power2MSB(a));
            a = 64;
            Assert.AreEqual(6, BitHacks.Power2MSB(a));
            a = 128;
            Assert.AreEqual(7, BitHacks.Power2MSB(a));
        }
Beispiel #13
0
        private static int CalculateBitCount()
        {
            var values = ((T[])System.Enum.GetValues(typeof(T))).Select(Cast32BitEnum <T> .ToUInt).ToArray();

            Assertion.Require(values.Length > 0, "Bit slice for enum {0} with no values is meaningless", typeof(T));

            var max = values.Max();

            Assertion.Require(max <= int.MaxValue, "Values in enum must {0} must be in range [0..int.MaxValue]", typeof(T));

            var count = max + 1L;
            var res   = BitHacks.Log2Ceil(count);

            if (res == 0)
            {
                res++;     //for enums with one value
            }
            return(res);
        }
Beispiel #14
0
        private static int CalculateBitCount()
        {
            var type = typeof(T);

#if !NETSTANDARD
            Assertion.Require(type.IsEnum, "Type must be enum, actual type: {0}", type);
#endif

            var values = ((T[])System.Enum.GetValues(typeof(T))).Select(CastTo <long> .From).ToList();

            Assertion.Require(values.Count > 0, "Bit slice for enum with no value is meaningless");
            Assertion.Require(values.All(v => v >= 0), "Enums with negative values or values greater than long.MaxValue are unsupported");

            var count = values.Max() + 1;
            var res   = BitHacks.Log2Ceil(count);
            if (res == 0)
            {
                res++;     //for enums with one value
            }
            return(res);
        }
Beispiel #15
0
 private void ConstructCommon()
 {
     _tileparts = new List <JP2TilePart>();
     _imageSize = new Lazy <Size>(() =>
     {
         var siz = Markers[MarkerType.SIZ] as SizMarker;
         return(new Size(
                    (int)(siz.RefGridSize.Width - siz.ImageOffset.X),
                    (int)(siz.RefGridSize.Height - siz.ImageOffset.Y)));
     });
     _tileCount = new Lazy <Size>(() =>
     {
         var siz = Markers[MarkerType.SIZ] as SizMarker;
         return(new Size(
                    BitHacks.DivCeil(
                        siz.RefGridSize.Width - siz.TileOffset.X,
                        TileSize.Width),
                    BitHacks.DivCeil(
                        siz.RefGridSize.Height - siz.TileOffset.Y,
                        siz.TileSize.Height)));
     });
 }
Beispiel #16
0
        private Rectangle CalculateTileRect(ushort tileIdx, int res)
        {
            // TODO - fast lane computation for images
            // without tile or image offset, and 2^x tile sizes
            int   resShiftFactor = Codestream.DecompositionLevels - res;
            int   tileX          = tileIdx % Codestream.TileCount.Width;
            int   tileY          = tileIdx / Codestream.TileCount.Width;
            Size  sz             = Codestream.TileSize;
            Point ul             = new Point(
                Math.Max(tileX * sz.Width, Codestream.TileOffset.X),
                Math.Max(tileY * sz.Height, Codestream.TileOffset.Y));
            Rectangle tile = new Rectangle(ul, sz);

            // for image offset and border effects
            tile.Intersect(Image);
            // reduce to resolution
            tile.X      = BitHacks.DivShiftCeil(tile.X, resShiftFactor);
            tile.Y      = BitHacks.DivShiftCeil(tile.Y, resShiftFactor);
            tile.Width  = BitHacks.DivShiftCeil(tile.Width, resShiftFactor);
            tile.Height = BitHacks.DivShiftCeil(tile.Height, resShiftFactor);

            return(tile);
        }
Beispiel #17
0
        public void UnsetBit_Smoke_Test()
        {
            var binaryString = "1100";

            Assert.AreEqual("100", Convert.ToString(BitHacks.UnsetBit(Convert.ToInt32(binaryString, 2), 3), 2));
        }
Beispiel #18
0
        public CodMarker(
            bool usePrecincts,
            bool useEph,
            bool useSop,
            Size codeblockSize,
            Size[] precinctSizes,
            ProgressionOrder progression,
            ushort qualityLayers,
            bool useMultiComponentTransform,
            WaveletTransform waveletFilter,
            byte decompositionLevels,
            CodeblockStyle cblkStyle)
            : base(MarkerType.COD)
        {
            _scod  = CodingStyle.None;
            _scod |=
                usePrecincts ? CodingStyle.UsePrecincts : CodingStyle.None;
            _scod |=
                useSop ? CodingStyle.UseSopMarker : CodingStyle.None;
            _scod |=
                useEph ? CodingStyle.UseEphMarker: CodingStyle.None;

            Progression                   = progression;
            QualityLayers                 = qualityLayers;
            DecompositionLevels           = decompositionLevels;
            UseMultipleComponentTransform = useMultiComponentTransform;
            CBlkStyle     = cblkStyle;
            WaveletFilter = waveletFilter;

            // width > max || height > max
            if ((new List <int> {
                codeblockSize.Width,
                codeblockSize.Height,
                MAX_CBLK_SIZE
            }).Max() != MAX_CBLK_SIZE)
            {
                throw new ArgumentOutOfRangeException(
                          "Codeblock dimensions must be up to "
                          + MAX_CBLK_SIZE + " samples on each side");
            }

            bool isCodeblockPow2 =
                BitHacks.IsPowerOf2((uint)codeblockSize.Width) &&
                BitHacks.IsPowerOf2((uint)codeblockSize.Height);

            if (!isCodeblockPow2)
            {
                throw new ArgumentException(
                          "Codeblock size must be power of 2");
            }

            // codeblock size range is [4,64], and is a power of 2.
            // to save space and fill it inside a byte,
            // codestream specifies them using the 2-exponent in the range
            // [0, 16]. The additional 2 is implicit.
            _cblkExpnX  = (byte)(BitHacks.LogFloor2((uint)codeblockSize.Width));
            _cblkExpnX -= 2;

            _cblkExpnY  = (byte)(BitHacks.LogFloor2((uint)codeblockSize.Height));
            _cblkExpnY -= 2;

            if (UsePrecincts)
            {
                bool valid = precinctSizes.Any();
                valid &= precinctSizes.Length <= (decompositionLevels + 1);
                valid &= precinctSizes
                         .All(prc => BitHacks.IsPowerOf2((uint)prc.Width));
                valid &= precinctSizes
                         .All(prc => BitHacks.IsPowerOf2((uint)prc.Height));
                if (!valid)
                {
                    throw new ArgumentException(
                              "precincts unspecified or not power of two");
                }

                _ppx = new byte[decompositionLevels + 1];
                _ppx = new byte[decompositionLevels + 1];

                int idx = 0;
                foreach (var prc in precinctSizes)
                {
                    _ppx[idx] = (byte)BitHacks.LogFloor2((uint)prc.Width);
                    _ppy[idx] = (byte)BitHacks.LogFloor2((uint)prc.Height);
                    idx++;
                }

                for (; idx <= decompositionLevels; idx++)
                {
                    // there is at least one element in ppx/ppy
                    // because we checked that precSizes.Any()
                    _ppx[idx] = _ppx[idx - 1];
                    _ppy[idx] = _ppy[idx - 1];
                }
            }
            else
            {
                // maximal precincts: size 2^15
                _ppx = new byte[] { 0xF };
                _ppy = new byte[] { 0xF };
            }

            _markerBody   = GenerateMarkerBody();
            _markerLength = (ushort)(_markerBody.Length + 2);
        }
Beispiel #19
0
 public void MaxValue()
 {
     Assert.AreEqual(1, BitHacks.MaxValue(1));
     Assert.AreEqual(3, BitHacks.MaxValue(2));
     Assert.AreEqual(uint.MaxValue, BitHacks.MaxValue(32));
 }
Beispiel #20
0
 public void IsPowerOf2_Smoke_Test()
 {
     Assert.IsTrue(BitHacks.IsPowerOf2(32));
     Assert.IsFalse(BitHacks.IsPowerOf2(22));
 }
Beispiel #21
0
        public void ToggleBit_Smoke_Test()
        {
            var binaryString = "1110";

            Assert.AreEqual(Convert.ToString(BitHacks.ToggleBit(Convert.ToInt32(binaryString, 2), 1), 2), "1100");
        }
Beispiel #22
0
        public void GetRightmostSubBitsStartingWithAnUnsetBit_Smoke_Test()
        {
            var binaryString = "1011";

            Assert.AreEqual("11", Convert.ToString(BitHacks.GetRightmostSubBitsStartingWithAnUnsetBit(Convert.ToInt32(binaryString, 2)), 2));
        }
Beispiel #23
0
        public void RightPropogateRightmostUnsetBit_Smoke_Test()
        {
            var binaryString = "1011";

            Assert.AreEqual("1000", Convert.ToString(BitHacks.RightPropogateRightmostUnsetBit(Convert.ToInt32(binaryString, 2)), 2));
        }
Beispiel #24
0
 public void IsEven_Smoke_Test()
 {
     Assert.IsTrue(BitHacks.IsEven(22));
     Assert.IsFalse(BitHacks.IsEven(101));
 }