コード例 #1
0
        public override Rational?ReadJson(JsonReader reader, Type objectType, Rational?existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.Value is null)
            {
                return(null);
            }

            if (reader.Value is double d)
            {
                return(Rational.Approximate(d));
            }

            string text = (string)reader.Value;

            if (text != null)
            {
                if (Rational.TryParseDecimal(text, out Rational result))
                {
                    return(result);
                }

                string[] args = text.Split("/");

                if (args.Length == 1)
                {
                    return(new Rational(BigInteger.Parse(args[0])));
                }
                else
                {
                    return(new Rational(BigInteger.Parse(args[0]), BigInteger.Parse(args[1])));
                }
            }

            return(default);
コード例 #2
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
        public void ApproximateRandom()
        {
            var random = new Generator("ApproximateRandom");

            for (int i = 0; i < 100; i++)
            {
                var dec = (0.5m / random.Decimal() - 1m).ToString();
                dec = dec.Substring(0, dec.IndexOf('.') + 6);
                var x   = decimal.Parse(dec);
                var min = x - 0.000005m;
                var max = x + 0.000005m;
                int d   = 1;
                int n;

                while (true)
                {
                    n = (int)Math.Round(d * x);
                    var y = n / (decimal)d;
                    if (y >= min && y < max)
                    {
                        break;
                    }
                    d++;
                }

                Assert.AreEqual(new Rational(n, d), Rational.Approximate(dec));
            }
        }
コード例 #3
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
        public void ApproximateInteger()
        {
            var n = BigInteger.Pow(3, 100);
            var r = Rational.Approximate(n.ToString());

            Assert.AreEqual(r.P, n);
        }
コード例 #4
0
        public void ApproximatePI(int expectedNumerator, int expectedDenominator, double tolerance)
        {
            // action
            var rational = Rational.Approximate(Math.PI, tolerance);

            // assert
            Assert.Equal((Rational)expectedNumerator / expectedDenominator, rational);
        }
コード例 #5
0
        public void ApproximateDoubleNaN()
        {
            // action
            var rational = Rational.Approximate(double.NaN);

            // assert
            Assert.Equal(Rational.NaN, rational);
        }
コード例 #6
0
        public void ApproximateDouble(double input, double tolerance, int expectedNumerator, int expectedDenominator)
        {
            // action
            var rational = Rational.Approximate(input, tolerance);

            // assert
            Assert.Equal((Rational)expectedNumerator / expectedDenominator, rational);
        }
コード例 #7
0
        public void ApproximateFloatNaN()
        {
            // action
            var rational = Rational.Approximate(float.NaN);

            // assert
            Assert.Equal(Rational.NaN, rational);
        }
コード例 #8
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateString()
 {
     Assert.AreEqual(new Rational(1, 7), Rational.Approximate("0.1"));
     Assert.AreEqual(new Rational(1, 10), Rational.Approximate("0.10"));
     Assert.AreEqual(new Rational(2, 17), Rational.Approximate("0.12"));
     Assert.AreEqual(new Rational(10, 89), Rational.Approximate("0.112"));
     Assert.AreEqual(new Rational(859, 7730), Rational.Approximate("0.111125"));
     Assert.AreEqual(new Rational(8890, 80009), Rational.Approximate("0.111112"));
     Assert.AreEqual(new Rational(91, 272), Rational.Approximate(".33456"));
     Assert.AreEqual(new Rational(22, 7), Rational.Approximate("3.14"));
     Assert.AreEqual(new Rational(355, 113), Rational.Approximate("3.1416"));
 }
コード例 #9
0
        public void ApproximateDecimal(string inputStr, string toleranceStr, int expectedNumerator, int expectedDenominator)
        {
            // arrange
            var input     = decimal.Parse(inputStr, CultureInfo.InvariantCulture);
            var tolerance = decimal.Parse(toleranceStr, CultureInfo.InvariantCulture);

            // action
            var rational = Rational.Approximate(input, tolerance);

            // assert
            Assert.Equal((Rational)expectedNumerator / expectedDenominator, rational);
        }
コード例 #10
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = null;

            if (value != null)
            {
                var doubleValue = System.Convert.ToDouble(value);
                var rational    = Rational.Approximate(doubleValue, tolerance: 0.01);
                var text        = rational.ToString("W");
                text   = text.Replace(" + ", " ");
                result = text;
            }

            return(result);
        }
コード例 #11
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateDecimal()
 {
     Assert.AreEqual(new Rational(1, 7), Rational.Approximate(.1m));
     Assert.AreEqual(new Rational(1, 10), Rational.Approximate(.10m));
     Assert.AreEqual(new Rational(1, 9), Rational.Approximate(.11m));
     Assert.AreEqual(new Rational(8890, 80009), Rational.Approximate(.111112m));
     Assert.AreEqual(new Rational(859, 7730), Rational.Approximate(.111125m));
     Assert.AreEqual(new Rational(10, 89), Rational.Approximate(.112m));
     Assert.AreEqual(new Rational(2, 17), Rational.Approximate(.12m));
     Assert.AreEqual(new Rational(1, 8), Rational.Approximate(.13m));
     Assert.AreEqual(new Rational(1, 7), Rational.Approximate(.14m));
     Assert.AreEqual(new Rational(2, 13), Rational.Approximate(.15m));
     Assert.AreEqual(new Rational(3, 19), Rational.Approximate(.16m));
     Assert.AreEqual(new Rational(1, 6), Rational.Approximate(.17m));
     Assert.AreEqual(new Rational(2, 11), Rational.Approximate(.18m));
     Assert.AreEqual(new Rational(3, 16), Rational.Approximate(.19m));
     Assert.AreEqual(new Rational(91, 272), Rational.Approximate(.33456m));
     Assert.AreEqual(new Rational(22, 7), Rational.Approximate(3.14m));
 }
コード例 #12
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateNull()
 {
     Rational.Approximate(null);
 }
コード例 #13
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="properties">EXIF properties from which to populate</param>
        public ImageMetaData(ExifPropertyCollection properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            // References:
            // http://www.media.mit.edu/pia/Research/deepview/exif.html
            // http://en.wikipedia.org/wiki/APEX_system
            // http://en.wikipedia.org/wiki/Exposure_value

            object rawValue;

            #region Aperture

            rawValue = properties[ExifTag.FNumber].Value;
            if (rawValue is IConvertible)
            {
                // f/x.x
                this.Aperture = Convert.ToDecimal(rawValue);
            }
            else
            {
                rawValue = properties[ExifTag.Aperture].Value;
                if (rawValue is IConvertible)
                {
                    // f/x.x
                    this.Aperture = (decimal)Math.Pow(2.0, Convert.ToDouble(rawValue) / 2.0);
                }
            }

            #endregion Aperture

            this.Artist = Convert.ToString(properties[ExifTag.Artist].Value);

            #region ColorSpace

            rawValue = properties[ExifTag.ColorSpace].Value;
            if (rawValue is Enum)
            {
                this.ColorSpace = (ExifTagColorSpace)rawValue;
            }

            #endregion ColorSpace

            this.Copyright = Convert.ToString(properties[ExifTag.Copyright].Value);

            #region DateTaken

            rawValue = properties[ExifTag.DateTimeOriginal].Value;
            if (rawValue is DateTime)
            {
                this.DateTaken = (DateTime)rawValue;
            }
            else
            {
                rawValue = properties[ExifTag.DateTimeDigitized].Value;
                if (rawValue is DateTime)
                {
                    this.DateTaken = (DateTime)rawValue;
                }
                else
                {
                    rawValue = properties[ExifTag.DateTime].Value;
                    if (rawValue is DateTime)
                    {
                        this.DateTaken = (DateTime)rawValue;
                    }
                }
            }

            #endregion DateTaken

            #region ExposureBias

            rawValue = properties[ExifTag.ExposureBias].Value;
            if (rawValue is Rational <int> )
            {
                this.ExposureBias = (Rational <int>)rawValue;
            }

            #endregion ExposureBias

            #region ExposureMode

            rawValue = properties[ExifTag.ExposureMode].Value;
            if (rawValue is Enum)
            {
                this.ExposureMode = (ExifTagExposureMode)rawValue;
            }

            #endregion ExposureMode

            #region ExposureProgram

            rawValue = properties[ExifTag.ExposureProgram].Value;
            if (rawValue is Enum)
            {
                this.ExposureProgram = (ExifTagExposureProgram)rawValue;
            }

            #endregion ExposureProgram

            #region Flash

            rawValue = properties[ExifTag.Flash].Value;
            if (rawValue is Enum)
            {
                this.Flash = (ExifTagFlash)rawValue;
            }

            #endregion Flash

            #region FocalLength

            rawValue = properties[ExifTag.FocalLength].Value;
            if (rawValue is IConvertible)
            {
                this.FocalLength = Convert.ToDecimal(rawValue);
            }
            else
            {
                rawValue = properties[ExifTag.FocalLengthIn35mmFilm].Value;
                if (rawValue is IConvertible)
                {
                    this.FocalLength = Convert.ToDecimal(rawValue);
                }
            }

            #endregion FocalLength

            #region GpsAltitude

            rawValue = properties[ExifTag.GpsAltitude].Value;
            if (rawValue is IConvertible)
            {
                this.GpsAltitude = Convert.ToDecimal(rawValue);
            }

            #endregion GpsAltitude

            string gpsDir;

            #region GpsLatitude

            gpsDir   = Convert.ToString(properties[ExifTag.GpsLatitudeRef].Value);
            rawValue = properties[ExifTag.GpsLatitude].Value;
            if (!(rawValue is Array))
            {
                gpsDir   = Convert.ToString(properties[ExifTag.GpsDestLatitudeRef].Value);
                rawValue = properties[ExifTag.GpsDestLatitude].Value;
            }
            if (rawValue is Array)
            {
                this.GpsLatitude = this.AsGps((Array)rawValue, gpsDir);
            }

            #endregion GpsLatitude

            #region GpsLongitude

            gpsDir   = Convert.ToString(properties[ExifTag.GpsLongitudeRef].Value);
            rawValue = properties[ExifTag.GpsLongitude].Value;
            if (!(rawValue is Array))
            {
                gpsDir   = Convert.ToString(properties[ExifTag.GpsDestLongitudeRef].Value);
                rawValue = properties[ExifTag.GpsDestLongitude].Value;
            }
            if (rawValue is Array)
            {
                this.GpsLongitude = this.AsGps((Array)rawValue, gpsDir);
            }

            #endregion GpsLongitude

            this.ImageDescription = Convert.ToString(properties[ExifTag.ImageDescription].Value);

            #region ImageHeight

            rawValue = properties[ExifTag.ImageHeight].Value;
            if (rawValue is IConvertible)
            {
                this.ImageHeight = Convert.ToInt32(rawValue);
            }
            else
            {
                rawValue = properties[ExifTag.CompressedImageHeight].Value;
                if (rawValue is IConvertible)
                {
                    this.ImageHeight = Convert.ToInt32(rawValue);
                }
            }

            #endregion ImageHeight

            #region ImageWidth

            rawValue = properties[ExifTag.ImageWidth].Value;
            if (rawValue is IConvertible)
            {
                this.ImageWidth = Convert.ToInt32(rawValue);
            }
            else
            {
                rawValue = properties[ExifTag.CompressedImageWidth].Value;
                if (rawValue is IConvertible)
                {
                    this.ImageWidth = Convert.ToInt32(rawValue);
                }
            }

            #endregion ImageWidth

            this.ImageTitle = Convert.ToString(properties[ExifTag.ImageTitle].Value);

            #region ISOSpeed

            rawValue = properties[ExifTag.ISOSpeed].Value;
            if (rawValue is Array)
            {
                Array array = (Array)rawValue;
                if (array.Length > 0)
                {
                    rawValue = array.GetValue(0);
                }
            }
            if (rawValue is IConvertible)
            {
                this.ISOSpeed = Convert.ToInt32(rawValue);
            }

            #endregion ISOSpeed

            this.Make  = Convert.ToString(properties[ExifTag.Make].Value);
            this.Model = Convert.ToString(properties[ExifTag.Model].Value);

            #region MeteringMode

            rawValue = properties[ExifTag.MeteringMode].Value;
            if (rawValue is Enum)
            {
                this.MeteringMode = (ExifTagMeteringMode)rawValue;
            }

            #endregion MeteringMode

            this.MSAuthor   = Convert.ToString(properties[ExifTag.MSAuthor].Value);
            this.MSComments = Convert.ToString(properties[ExifTag.MSComments].Value);
            this.MSKeywords = Convert.ToString(properties[ExifTag.MSKeywords].Value);
            this.MSSubject  = Convert.ToString(properties[ExifTag.MSSubject].Value);
            this.MSTitle    = Convert.ToString(properties[ExifTag.MSTitle].Value);

            #region Orientation

            rawValue = properties[ExifTag.Orientation].Value;
            if (rawValue is Enum)
            {
                this.Orientation = (ExifTagOrientation)rawValue;
            }

            #endregion Orientation

            #region ShutterSpeed

            rawValue = properties[ExifTag.ExposureTime].Value;
            if (rawValue is Rational <uint> )
            {
                this.ShutterSpeed = (Rational <uint>)rawValue;
            }
            else
            {
                rawValue = properties[ExifTag.ShutterSpeed].Value;
                if (rawValue is Rational <int> )
                {
                    this.ShutterSpeed = Rational <uint> .Approximate((decimal)Math.Pow(2.0, -Convert.ToDouble(rawValue)));
                }
            }

            #endregion ShutterSpeed

            #region WhiteBalance

            rawValue = properties[ExifTag.WhiteBalance].Value;
            if (rawValue is Enum)
            {
                this.WhiteBalance = (ExifTagWhiteBalance)rawValue;
            }

            #endregion WhiteBalance
        }
コード例 #14
0
        /// <summary>
        /// Factory method
        /// </summary>
        /// <param name="properties">EXIF properties from which to populate</param>
        public static ImageXmp Create(XmpPropertyCollection properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            // References:
            // http://www.media.mit.edu/pia/Research/deepview/exif.html
            // http://en.wikipedia.org/wiki/APEX_system
            // http://en.wikipedia.org/wiki/Exposure_value

            decimal       decimalValue;
            DateTime      dateValue;
            string        stringValue;
            GpsCoordinate gpsValue;
            IDictionary <string, object> dictionaryValue;

            ImageXmp xmp = new ImageXmp();

            #region Aperture

            if (properties.TryGetValue(ExifSchema.FNumber, out decimalValue))
            {
                // f/x.x
                xmp.Aperture = decimalValue;
            }
            else if (properties.TryGetValue(ExifSchema.ApertureValue, out decimalValue))
            {
                // f/x.x
                xmp.Aperture = Decimal.Round((decimal)Math.Pow(2.0, Convert.ToDouble(decimalValue) / 2.0), 1);
            }

            #endregion Aperture

            #region Creator

            if (properties.TryGetValue(DublinCoreSchema.Creator, out stringValue) ||
                properties.TryGetValue(ExifTiffSchema.Artist, out stringValue) ||
                properties.TryGetValue(ExifSchema.MSAuthor, out stringValue))
            {
                xmp.Creator = stringValue;
            }

            #endregion Creator

            #region Copyright

            if (properties.TryGetValue(DublinCoreSchema.Rights, out stringValue) ||
                properties.TryGetValue(ExifTiffSchema.Copyright, out stringValue))
            {
                xmp.Copyright = stringValue;
            }

            #endregion Copyright

            #region DateTaken

            if (properties.TryGetValue(ExifSchema.DateTimeOriginal, out dateValue) ||
                properties.TryGetValue(ExifSchema.DateTimeDigitized, out dateValue) ||
                properties.TryGetValue(ExifTiffSchema.DateTime, out dateValue))
            {
                xmp.DateTaken = dateValue;
            }

            #endregion DateTaken

            #region Description

            if (properties.TryGetValue(DublinCoreSchema.Description, out stringValue) ||
                properties.TryGetValue(ExifTiffSchema.ImageDescription, out stringValue) ||
                properties.TryGetValue(ExifSchema.MSSubject, out stringValue))
            {
                xmp.Description = stringValue;
            }

            #endregion Description

            #region ExposureBias

            xmp.ExposureBias = properties.GetValue(ExifSchema.ExposureBiasValue, Rational <int> .Empty);

            #endregion ExposureBias

            #region ExposureMode

            xmp.ExposureMode = properties.GetValue(ExifSchema.ExposureMode, default(ExifTagExposureMode));

            #endregion ExposureMode

            #region ExposureProgram

            xmp.ExposureProgram = properties.GetValue(ExifSchema.ExposureProgram, default(ExifTagExposureProgram));

            #endregion ExposureProgram

            #region Flash

            if (properties.TryGetValue(ExifSchema.Flash, out dictionaryValue))
            {
                // decode object into EXIF enum
                ExifTagFlash flashValue = default(ExifTagFlash);

                if (dictionaryValue.ContainsKey("Fired") &&
                    StringComparer.OrdinalIgnoreCase.Equals(Convert.ToString(dictionaryValue["Fired"]), Boolean.TrueString))
                {
                    flashValue |= ExifTagFlash.FlashFired;
                }

                if (dictionaryValue.ContainsKey("Function") &&
                    StringComparer.OrdinalIgnoreCase.Equals(Convert.ToString(dictionaryValue["Function"]), Boolean.TrueString))
                {
                    flashValue |= ExifTagFlash.NoFlashFunction;
                }

                if (dictionaryValue.ContainsKey("Mode"))
                {
                    switch (Convert.ToString(dictionaryValue["Mode"]))
                    {
                    case "1":
                    {
                        flashValue |= ExifTagFlash.ModeOn;
                        break;
                    }

                    case "2":
                    {
                        flashValue |= ExifTagFlash.ModeOff;
                        break;
                    }

                    case "3":
                    {
                        flashValue |= ExifTagFlash.ModeAuto;
                        break;
                    }
                    }
                }

                if (dictionaryValue.ContainsKey("RedEyeMode") &&
                    StringComparer.OrdinalIgnoreCase.Equals(Convert.ToString(dictionaryValue["RedEyeMode"]), Boolean.TrueString))
                {
                    flashValue |= ExifTagFlash.RedEyeReduction;
                }

                if (dictionaryValue.ContainsKey("Return"))
                {
                    switch (Convert.ToString(dictionaryValue["Return"]))
                    {
                    case "2":
                    {
                        flashValue |= ExifTagFlash.ReturnNotDetected;
                        break;
                    }

                    case "3":
                    {
                        flashValue |= ExifTagFlash.ReturnDetected;
                        break;
                    }
                    }
                }

                xmp.Flash = flashValue;
            }

            #endregion Flash

            #region FocalLength

            if (properties.TryGetValue(ExifSchema.FocalLength, out decimalValue) ||
                properties.TryGetValue(ExifSchema.FocalLengthIn35mmFilm, out decimalValue))
            {
                xmp.FocalLength = Decimal.Round(decimalValue, 1);
            }

            #endregion FocalLength

            #region GpsAltitude

            if (properties.TryGetValue(ExifSchema.GPSAltitude, out decimalValue))
            {
                xmp.GpsAltitude = Decimal.Round(decimalValue, 5);
            }

            #endregion GpsAltitude

            #region GpsLatitude

            if (properties.TryGetValue(ExifSchema.GPSLatitude, out gpsValue) ||
                properties.TryGetValue(ExifSchema.GPSDestLatitude, out gpsValue))
            {
                xmp.GpsLatitude = gpsValue;
            }

            #endregion GpsLatitude

            #region GpsLongitude

            if (properties.TryGetValue(ExifSchema.GPSLongitude, out gpsValue) ||
                properties.TryGetValue(ExifSchema.GPSDestLongitude, out gpsValue))
            {
                xmp.GpsLongitude = gpsValue;
            }

            #endregion GpsLongitude

            #region ImageHeight

            if (properties.TryGetValue(ExifTiffSchema.ImageLength, out decimalValue))
            {
                xmp.ImageHeight = Convert.ToInt32(decimalValue);
            }

            #endregion ImageHeight

            #region ImageWidth

            if (properties.TryGetValue(ExifTiffSchema.ImageWidth, out decimalValue))
            {
                xmp.ImageWidth = Convert.ToInt32(decimalValue);
            }

            #endregion ImageWidth

            #region ISOSpeed

            if (properties.TryGetValue(ExifSchema.ISOSpeedRatings, out decimalValue))
            {
                xmp.ISOSpeed = Convert.ToInt32(decimalValue);
            }

            #endregion ISOSpeed

            #region Make

            if (properties.TryGetValue(ExifTiffSchema.Make, out stringValue))
            {
                xmp.Make = stringValue;
            }

            #endregion Make

            #region Make

            if (properties.TryGetValue(ExifTiffSchema.Model, out stringValue))
            {
                xmp.Model = stringValue;
            }

            #endregion Model

            #region MeteringMode

            xmp.MeteringMode = properties.GetValue(ExifSchema.MeteringMode, default(ExifTagMeteringMode));

            #endregion MeteringMode

            #region Orientation

            xmp.Orientation = properties.GetValue(ExifTiffSchema.Orientation, default(ExifTagOrientation));

            #endregion Orientation

            #region ShutterSpeed

            xmp.ShutterSpeed = properties.GetValue(ExifSchema.ExposureTime, Rational <uint> .Empty);
            if (xmp.ShutterSpeed.IsEmpty)
            {
                Rational <int> shutterSpeed = properties.GetValue(ExifSchema.ShutterSpeedValue, Rational <int> .Empty);
                if (!shutterSpeed.IsEmpty)
                {
                    xmp.ShutterSpeed = Rational <uint> .Approximate((decimal)Math.Pow(2.0, -Convert.ToDouble(shutterSpeed)));
                }
            }

            #endregion ShutterSpeed

            #region Tags

            xmp.Tags = properties.GetValue(DublinCoreSchema.Subject, default(IEnumerable <string>));
            // TODO: ExifSchema.MSKeywords

            #endregion Tags

            #region Title

            if (properties.TryGetValue(DublinCoreSchema.Title, out stringValue) ||
                properties.TryGetValue(ExifSchema.ImageTitle, out stringValue) ||
                properties.TryGetValue(ExifSchema.MSTitle, out stringValue))
            {
                xmp.Title = stringValue;
            }

            #endregion Title

            #region WhiteBalance

            xmp.WhiteBalance = properties.GetValue(ExifSchema.WhiteBalance, default(ExifTagWhiteBalance));

            #endregion WhiteBalance

            return(xmp);
        }
コード例 #15
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateNonNumberWithoutDecimal()
 {
     var r = Rational.Approximate("ab");
 }
コード例 #16
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateEmpty()
 {
     Rational.Approximate("");
 }
コード例 #17
0
ファイル: Rational.cs プロジェクト: plutoscarab/Foundations
 public void ApproximateWhitespace()
 {
     Rational.Approximate(" ");
 }