Exemplo n.º 1
0
        public CieLchuv Convert(CieLuv input)
        {
            DebugGuard.NotNull(input, nameof(input));

            // Conversion algorithm described here:
            // https://en.wikipedia.org/wiki/CIELUV#Cylindrical_representation_.28CIELCH.29
            float l = input.L, a = input.U, b = input.V;
            float c        = MathF.Sqrt((a * a) + (b * b));
            float hRadians = MathF.Atan2(b, a);
            float hDegrees = MathF.RadianToDegree(hRadians);

            // Wrap the angle round at 360.
            hDegrees = hDegrees % 360;

            // Make sure it's not negative.
            while (hDegrees < 0)
            {
                hDegrees += 360;
            }

            return(new CieLchuv(l, c, hDegrees, input.WhitePoint));
        }
        public CieXyz Convert(CieLuv input)
        {
            DebugGuard.NotNull(input, nameof(input));

            // Conversion algorithm described here: http://www.brucelindbloom.com/index.html?Eqn_Luv_to_XYZ.html
            float l = input.L, u = input.U, v = input.V;

            float u0 = ComputeU0(input.WhitePoint);
            float v0 = ComputeV0(input.WhitePoint);

            float y = l > CieConstants.Kappa * CieConstants.Epsilon
                        ? MathF.Pow((l + 16) / 116, 3)
                        : l / CieConstants.Kappa;

            float a = ((52 * l / (u + (13 * l * u0))) - 1) / 3;
            float b = -5 * y;
            float c = -0.3333333F;
            float d = y * ((39 * l / (v + (13 * l * v0))) - 5);

            float x = (d - b) / (a - c);
            float z = (x * a) + b;

            if (float.IsNaN(x) || x < 0)
            {
                x = 0;
            }

            if (float.IsNaN(y) || y < 0)
            {
                y = 0;
            }

            if (float.IsNaN(z) || z < 0)
            {
                z = 0;
            }

            return(new CieXyz(x, y, z));
        }
Exemplo n.º 3
0
        public CieLch Convert(CieLab input)
        {
            DebugGuard.NotNull(input, nameof(input));

            // Conversion algorithm described here:
            // https://en.wikipedia.org/wiki/Lab_color_space#Cylindrical_representation:_CIELCh_or_CIEHLC
            float l = input.L, a = input.A, b = input.B;
            float c        = (float)Math.Sqrt((a * a) + (b * b));
            float hRadians = (float)Math.Atan2(b, a);
            float hDegrees = MathFExtensions.RadianToDegree(hRadians);

            // Wrap the angle round at 360.
            hDegrees = hDegrees % 360;

            // Make sure it's not negative.
            while (hDegrees < 0)
            {
                hDegrees += 360;
            }

            return(new CieLch(l, c, hDegrees, input.WhitePoint));
        }
Exemplo n.º 4
0
        private static void LogFoundAssemblies([NotNull] IEnumerable <Assembly> assemblies)
        {
            DebugGuard.NotNull(assemblies, nameof(assemblies));

            if (!Logger.IsDebugEnabled)
            {
                return;
            }

            var assembliesArray = assemblies.ToArray();

            if (assembliesArray.Any())
            {
                Logger.Debug("Found plugins:");
                foreach (var assembly in assembliesArray)
                {
                    Logger.Debug($"- {assembly.FullName}");
                }
            }
            else
            {
                Logger.Debug("No plugins found");
            }
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
        public ExifReader(byte[] exifData)
        {
            DebugGuard.NotNull(exifData, nameof(exifData));

            this.exifData = exifData;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ImageFrameMetaData"/> class
        /// by making a copy from other metadata.
        /// </summary>
        /// <param name="other">
        /// The other <see cref="ImageFrameMetaData"/> to create this instance from.
        /// </param>
        internal ImageFrameMetaData(ImageFrameMetaData other)
        {
            DebugGuard.NotNull(other, nameof(other));

            this.FrameDelay = other.FrameDelay;
        }
Exemplo n.º 8
0
        public Rgb Convert(Hsv input)
        {
            DebugGuard.NotNull(input, nameof(input));

            float s = input.S;
            float v = input.V;

            if (MathF.Abs(s) < Constants.Epsilon)
            {
                return(new Rgb(v, v, v));
            }

            float h = (MathF.Abs(input.H - 360) < Constants.Epsilon) ? 0 : input.H / 60;
            int   i = (int)Math.Truncate(h);
            float f = h - i;

            float p = v * (1F - s);
            float q = v * (1F - (s * f));
            float t = v * (1F - (s * (1F - f)));

            float r, g, b;

            switch (i)
            {
            case 0:
                r = v;
                g = t;
                b = p;
                break;

            case 1:
                r = q;
                g = v;
                b = p;
                break;

            case 2:
                r = p;
                g = v;
                b = t;
                break;

            case 3:
                r = p;
                g = q;
                b = v;
                break;

            case 4:
                r = t;
                g = p;
                b = v;
                break;

            default:
                r = v;
                g = p;
                b = q;
                break;
            }

            return(new Rgb(r, g, b));
        }
Exemplo n.º 9
0
 public ErrorCapturedArgs([NotNull] string data)
 {
     DebugGuard.NotNull(data, nameof(data)); // can be empty
     Data = data;
 }
Exemplo n.º 10
0
 private static void RegisterUserInterfaceDependencies([NotNull] Container container)
 {
     DebugGuard.NotNull(container, nameof(container));
     container.RegisterSingleton <ISearchProviderNameOption, AppConfigConfiguration>();
 }
Exemplo n.º 11
0
 private static void RegisterDelay([NotNull] Container container)
 {
     DebugGuard.NotNull(container, nameof(container));
     DelayCommandExecution.Register(container);
 }
Exemplo n.º 12
0
 public void NotNull_TargetNotNull_ThrowsNoException()
 {
     DebugGuard.NotNull("test", "myParamName");
 }
Exemplo n.º 13
0
        public static int BuildHuffmanTable(Span <HuffmanCode> table, int rootBits, int[] codeLengths, int codeLengthsSize)
        {
            DebugGuard.MustBeGreaterThan(rootBits, 0, nameof(rootBits));
            DebugGuard.NotNull(codeLengths, nameof(codeLengths));
            DebugGuard.MustBeGreaterThan(codeLengthsSize, 0, nameof(codeLengthsSize));

            // sorted[codeLengthsSize] is a pre-allocated array for sorting symbols by code length.
            int[] sorted    = new int[codeLengthsSize];
            int   totalSize = 1 << rootBits;                                 // total size root table + 2nd level table.
            int   len;                                                       // current code length.
            int   symbol;                                                    // symbol index in original or sorted table.

            int[] counts  = new int[WebpConstants.MaxAllowedCodeLength + 1]; // number of codes of each length.
            int[] offsets = new int[WebpConstants.MaxAllowedCodeLength + 1]; // offsets in sorted table for each length.

            // Build histogram of code lengths.
            for (symbol = 0; symbol < codeLengthsSize; ++symbol)
            {
                int codeLengthOfSymbol = codeLengths[symbol];
                if (codeLengthOfSymbol > WebpConstants.MaxAllowedCodeLength)
                {
                    return(0);
                }

                counts[codeLengthOfSymbol]++;
            }

            // Error, all code lengths are zeros.
            if (counts[0] == codeLengthsSize)
            {
                return(0);
            }

            // Generate offsets into sorted symbol table by code length.
            offsets[1] = 0;
            for (len = 1; len < WebpConstants.MaxAllowedCodeLength; ++len)
            {
                int codesOfLength = counts[len];
                if (codesOfLength > 1 << len)
                {
                    return(0);
                }

                offsets[len + 1] = offsets[len] + codesOfLength;
            }

            // Sort symbols by length, by symbol order within each length.
            for (symbol = 0; symbol < codeLengthsSize; ++symbol)
            {
                int symbolCodeLength = codeLengths[symbol];
                if (symbolCodeLength > 0)
                {
                    sorted[offsets[symbolCodeLength]++] = symbol;
                }
            }

            // Special case code with only one value.
            if (offsets[WebpConstants.MaxAllowedCodeLength] == 1)
            {
                var huffmanCode = new HuffmanCode()
                {
                    BitsUsed = 0,
                    Value    = (uint)sorted[0]
                };
                ReplicateValue(table, 1, totalSize, huffmanCode);
                return(totalSize);
            }

            int step;                       // step size to replicate values in current table
            int low       = -1;             // low bits for current root entry
            int mask      = totalSize - 1;  // mask for low bits
            int key       = 0;              // reversed prefix code
            int numNodes  = 1;              // number of Huffman tree nodes
            int numOpen   = 1;              // number of open branches in current tree level
            int tableBits = rootBits;       // key length of current table
            int tableSize = 1 << tableBits; // size of current table

            symbol = 0;

            // Fill in root table.
            for (len = 1, step = 2; len <= rootBits; ++len, step <<= 1)
            {
                int countsLen = counts[len];
                numOpen <<= 1;
                numNodes += numOpen;
                numOpen  -= counts[len];
                if (numOpen < 0)
                {
                    return(0);
                }

                for (; countsLen > 0; countsLen--)
                {
                    var huffmanCode = new HuffmanCode()
                    {
                        BitsUsed = len,
                        Value    = (uint)sorted[symbol++]
                    };
                    ReplicateValue(table.Slice(key), step, tableSize, huffmanCode);
                    key = GetNextKey(key, len);
                }

                counts[len] = countsLen;
            }

            // Fill in 2nd level tables and add pointers to root table.
            Span <HuffmanCode> tableSpan = table;
            int tablePos = 0;

            for (len = rootBits + 1, step = 2; len <= WebpConstants.MaxAllowedCodeLength; ++len, step <<= 1)
            {
                numOpen <<= 1;
                numNodes += numOpen;
                numOpen  -= counts[len];
                if (numOpen < 0)
                {
                    return(0);
                }

                for (; counts[len] > 0; --counts[len])
                {
                    if ((key & mask) != low)
                    {
                        tableSpan  = tableSpan.Slice(tableSize);
                        tablePos  += tableSize;
                        tableBits  = NextTableBitSize(counts, len, rootBits);
                        tableSize  = 1 << tableBits;
                        totalSize += tableSize;
                        low        = key & mask;
                        table[low] = new HuffmanCode
                        {
                            BitsUsed = tableBits + rootBits,
                            Value    = (uint)(tablePos - low)
                        };
                    }

                    var huffmanCode = new HuffmanCode
                    {
                        BitsUsed = len - rootBits,
                        Value    = (uint)sorted[symbol++]
                    };
                    ReplicateValue(tableSpan.Slice(key >> rootBits), step, tableSize, huffmanCode);
                    key = GetNextKey(key, len);
                }
            }

            return(totalSize);
        }
        public static TiffBaseColorDecoder <TPixel> Create(
            Configuration configuration,
            MemoryAllocator memoryAllocator,
            TiffColorType colorType,
            TiffBitsPerSample bitsPerSample,
            ushort[] colorMap,
            Rational[] referenceBlackAndWhite,
            Rational[] ycbcrCoefficients,
            ushort[] ycbcrSubSampling,
            ByteOrder byteOrder)
        {
            switch (colorType)
            {
            case TiffColorType.WhiteIsZero:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZeroTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.WhiteIsZero1:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 1, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero1TiffColor <TPixel>());

            case TiffColorType.WhiteIsZero4:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 4, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero4TiffColor <TPixel>());

            case TiffColorType.WhiteIsZero8:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero8TiffColor <TPixel>());

            case TiffColorType.WhiteIsZero16:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero16TiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.WhiteIsZero24:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 24, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero24TiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.WhiteIsZero32:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 32, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero32TiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.WhiteIsZero32Float:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 32, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new WhiteIsZero32FloatTiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.BlackIsZero:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZeroTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.BlackIsZero1:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 1, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero1TiffColor <TPixel>());

            case TiffColorType.BlackIsZero4:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 4, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero4TiffColor <TPixel>());

            case TiffColorType.BlackIsZero8:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero8TiffColor <TPixel>(configuration));

            case TiffColorType.BlackIsZero16:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero16TiffColor <TPixel>(configuration, byteOrder == ByteOrder.BigEndian));

            case TiffColorType.BlackIsZero24:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 24, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero24TiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.BlackIsZero32:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 32, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero32TiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.BlackIsZero32Float:
                DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 32, "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new BlackIsZero32FloatTiffColor <TPixel>(byteOrder == ByteOrder.BigEndian));

            case TiffColorType.Rgb:
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.Rgb222:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 2 &&
                    bitsPerSample.Channel1 == 2 &&
                    bitsPerSample.Channel0 == 2,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.Rgb444:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 4 &&
                    bitsPerSample.Channel1 == 4 &&
                    bitsPerSample.Channel0 == 4,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new Rgb444TiffColor <TPixel>());

            case TiffColorType.Rgb888:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 8 &&
                    bitsPerSample.Channel1 == 8 &&
                    bitsPerSample.Channel0 == 8,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new Rgb888TiffColor <TPixel>(configuration));

            case TiffColorType.Rgb101010:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 10 &&
                    bitsPerSample.Channel1 == 10 &&
                    bitsPerSample.Channel0 == 10,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.Rgb121212:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 12 &&
                    bitsPerSample.Channel1 == 12 &&
                    bitsPerSample.Channel0 == 12,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.Rgb141414:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 14 &&
                    bitsPerSample.Channel1 == 14 &&
                    bitsPerSample.Channel0 == 14,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbTiffColor <TPixel>(bitsPerSample));

            case TiffColorType.Rgb161616:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 16 &&
                    bitsPerSample.Channel1 == 16 &&
                    bitsPerSample.Channel0 == 16,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new Rgb161616TiffColor <TPixel>(configuration, isBigEndian: byteOrder == ByteOrder.BigEndian));

            case TiffColorType.Rgb242424:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 24 &&
                    bitsPerSample.Channel1 == 24 &&
                    bitsPerSample.Channel0 == 24,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new Rgb242424TiffColor <TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian));

            case TiffColorType.Rgb323232:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 32 &&
                    bitsPerSample.Channel1 == 32 &&
                    bitsPerSample.Channel0 == 32,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new Rgb323232TiffColor <TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian));

            case TiffColorType.RgbFloat323232:
                DebugGuard.IsTrue(
                    bitsPerSample.Channels == 3 &&
                    bitsPerSample.Channel2 == 32 &&
                    bitsPerSample.Channel1 == 32 &&
                    bitsPerSample.Channel0 == 32,
                    "bitsPerSample");
                DebugGuard.IsTrue(colorMap == null, "colorMap");
                return(new RgbFloat323232TiffColor <TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian));

            case TiffColorType.PaletteColor:
                DebugGuard.NotNull(colorMap, "colorMap");
                return(new PaletteTiffColor <TPixel>(bitsPerSample, colorMap));

            case TiffColorType.YCbCr:
                return(new YCbCrTiffColor <TPixel>(memoryAllocator, referenceBlackAndWhite, ycbcrCoefficients, ycbcrSubSampling));

            default:
                throw TiffThrowHelper.InvalidColorType(colorType.ToString());
            }
        }