public virtual void TestGetReciprocal()
		{
			Rational rational = new Rational(1, 3);
			Rational reciprocal = rational.GetReciprocal();
			Sharpen.Tests.AreEqual("new rational should be reciprocal", new Rational(3, 1), reciprocal);
			Sharpen.Tests.AreEqual("original reciprocal should remain unchanged", new Rational(1, 3), rational);
		}
		public virtual void TestCreateRational()
		{
			Rational rational = new Rational(1, 3);
			Sharpen.Tests.AreEqual(1, rational.GetNumerator());
			Sharpen.Tests.AreEqual(3, rational.GetDenominator());
			Sharpen.Tests.AreEqual(1d / 3d, rational.DoubleValue(), 0.0001);
		}
		public virtual void TestToSimpleString()
		{
			Rational third1 = new Rational(1, 3);
			Rational third2 = new Rational(2, 6);
			Sharpen.Tests.AreEqual("1/3", third1.ToSimpleString(true));
			Sharpen.Tests.AreEqual("1/3", third2.ToSimpleString(true));
			Sharpen.Tests.AreEqual(third1, third2);
			Rational twoThirds = new Rational(10, 15);
			Sharpen.Tests.AreEqual("2/3", twoThirds.ToSimpleString(true));
			Rational two = new Rational(10, 5);
			Sharpen.Tests.IsTrue(two.IsInteger());
			Sharpen.Tests.AreEqual("2", two.ToSimpleString(true));
			Sharpen.Tests.AreEqual("2", two.ToSimpleString(false));
			Rational twoFifths = new Rational(4, 10);
			Sharpen.Tests.AreEqual("0.4", twoFifths.ToSimpleString(true));
			Sharpen.Tests.AreEqual("2/5", twoFifths.ToSimpleString(false));
			Rational threeEigths = new Rational(3, 8);
			Sharpen.Tests.AreEqual("3/8", threeEigths.ToSimpleString(true));
			Rational zero = new Rational(0, 8);
			Sharpen.Tests.IsTrue(zero.IsInteger());
			Sharpen.Tests.AreEqual("0", zero.ToSimpleString(true));
			Sharpen.Tests.AreEqual("0", zero.ToSimpleString(false));
			zero = new Rational(0, 0);
			Sharpen.Tests.IsTrue(zero.IsInteger());
			Sharpen.Tests.AreEqual("0", zero.ToSimpleString(true));
			Sharpen.Tests.AreEqual("0", zero.ToSimpleString(false));
		}
		private static void ProcessTag(Com.Drew.Metadata.Directory directory, int tagType, int tagValueOffset, int componentCount, int formatCode, RandomAccessReader reader)
		{
			switch (formatCode)
			{
				case FmtUndefined:
				{
					// Directory simply stores raw values
					// The display side uses a Descriptor class per directory to turn the raw values into 'pretty' descriptions
					// this includes exif user comments
					directory.SetByteArray(tagType, reader.GetBytes(tagValueOffset, componentCount));
					break;
				}

				case FmtString:
				{
					string @string = reader.GetNullTerminatedString(tagValueOffset, componentCount);
					directory.SetString(tagType, @string);
					break;
				}

				case FmtSrational:
				{
					if (componentCount == 1)
					{
						directory.SetRational(tagType, new Rational(reader.GetInt32(tagValueOffset), reader.GetInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] rationals = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								rationals[i] = new Rational(reader.GetInt32(tagValueOffset + (8 * i)), reader.GetInt32(tagValueOffset + 4 + (8 * i)));
							}
							directory.SetRationalArray(tagType, rationals);
						}
					}
					break;
				}

				case FmtUrational:
				{
					if (componentCount == 1)
					{
						directory.SetRational(tagType, new Rational(reader.GetUInt32(tagValueOffset), reader.GetUInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] rationals = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								rationals[i] = new Rational(reader.GetUInt32(tagValueOffset + (8 * i)), reader.GetUInt32(tagValueOffset + 4 + (8 * i)));
							}
							directory.SetRationalArray(tagType, rationals);
						}
					}
					break;
				}

				case FmtSingle:
				{
					if (componentCount == 1)
					{
						directory.SetFloat(tagType, reader.GetFloat32(tagValueOffset));
					}
					else
					{
						float[] floats = new float[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							floats[i] = reader.GetFloat32(tagValueOffset + (i * 4));
						}
						directory.SetFloatArray(tagType, floats);
					}
					break;
				}

				case FmtDouble:
				{
					if (componentCount == 1)
					{
						directory.SetDouble(tagType, reader.GetDouble64(tagValueOffset));
					}
					else
					{
						double[] doubles = new double[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							doubles[i] = reader.GetDouble64(tagValueOffset + (i * 4));
						}
						directory.SetDoubleArray(tagType, doubles);
					}
					break;
				}

				case FmtSbyte:
				{
					//
					// Note that all integral types are stored as int32 internally (the largest supported by TIFF)
					//
					if (componentCount == 1)
					{
						directory.SetInt(tagType, reader.GetInt8(tagValueOffset));
					}
					else
					{
						int[] bytes = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							bytes[i] = reader.GetInt8(tagValueOffset + i);
						}
						directory.SetIntArray(tagType, bytes);
					}
					break;
				}

				case FmtByte:
				{
					if (componentCount == 1)
					{
						directory.SetInt(tagType, reader.GetUInt8(tagValueOffset));
					}
					else
					{
						int[] bytes = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							bytes[i] = reader.GetUInt8(tagValueOffset + i);
						}
						directory.SetIntArray(tagType, bytes);
					}
					break;
				}

				case FmtUshort:
				{
					if (componentCount == 1)
					{
						int i = reader.GetUInt16(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetUInt16(tagValueOffset + (i * 2));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				case FmtSshort:
				{
					if (componentCount == 1)
					{
						int i = reader.GetInt16(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetInt16(tagValueOffset + (i * 2));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				case FmtSlong:
				case FmtUlong:
				{
					// NOTE 'long' in this case means 32 bit, not 64
					if (componentCount == 1)
					{
						int i = reader.GetInt32(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetInt32(tagValueOffset + (i * 4));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				default:
				{
					directory.AddError("Unknown format code " + formatCode + " for tag " + tagType);
					break;
				}
			}
		}
		public virtual void TestToString()
		{
			Rational rational = new Rational(1, 3);
			Sharpen.Tests.AreEqual("1/3", rational.ToString());
		}
示例#6
0
        /// <exception cref="System.IO.IOException"/>
        private static void ProcessTag([NotNull] TiffHandler handler, int tagId, int tagValueOffset, int componentCount, int formatCode, [NotNull] RandomAccessReader reader)
        {
            switch (formatCode)
            {
                case TiffDataFormat.CodeUndefined:
                {
                    // this includes exif user comments
                    handler.SetByteArray(tagId, reader.GetBytes(tagValueOffset, componentCount));
                    break;
                }

                case TiffDataFormat.CodeString:
                {
                    handler.SetString(tagId, reader.GetNullTerminatedString(tagValueOffset, componentCount));
                    break;
                }

                case TiffDataFormat.CodeRationalS:
                {
                    if (componentCount == 1)
                    {
                        handler.SetRational(tagId, new Rational(reader.GetInt32(tagValueOffset), reader.GetInt32(tagValueOffset + 4)));
                    }
                    else
                    {
                        if (componentCount > 1)
                        {
                            Rational[] array = new Rational[componentCount];
                            for (int i = 0; i < componentCount; i++)
                            {
                                array[i] = new Rational(reader.GetInt32(tagValueOffset + (8 * i)), reader.GetInt32(tagValueOffset + 4 + (8 * i)));
                            }
                            handler.SetRationalArray(tagId, array);
                        }
                    }
                    break;
                }

                case TiffDataFormat.CodeRationalU:
                {
                    if (componentCount == 1)
                    {
                        handler.SetRational(tagId, new Rational(reader.GetUInt32(tagValueOffset), reader.GetUInt32(tagValueOffset + 4)));
                    }
                    else
                    {
                        if (componentCount > 1)
                        {
                            Rational[] array = new Rational[componentCount];
                            for (int i = 0; i < componentCount; i++)
                            {
                                array[i] = new Rational(reader.GetUInt32(tagValueOffset + (8 * i)), reader.GetUInt32(tagValueOffset + 4 + (8 * i)));
                            }
                            handler.SetRationalArray(tagId, array);
                        }
                    }
                    break;
                }

                case TiffDataFormat.CodeSingle:
                {
                    if (componentCount == 1)
                    {
                        handler.SetFloat(tagId, reader.GetFloat32(tagValueOffset));
                    }
                    else
                    {
                        float[] array = new float[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetFloat32(tagValueOffset + (i * 4));
                        }
                        handler.SetFloatArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeDouble:
                {
                    if (componentCount == 1)
                    {
                        handler.SetDouble(tagId, reader.GetDouble64(tagValueOffset));
                    }
                    else
                    {
                        double[] array = new double[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetDouble64(tagValueOffset + (i * 4));
                        }
                        handler.SetDoubleArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt8S:
                {
                    if (componentCount == 1)
                    {
                        handler.SetInt8s(tagId, reader.GetInt8(tagValueOffset));
                    }
                    else
                    {
                        sbyte[] array = new sbyte[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetInt8(tagValueOffset + i);
                        }
                        handler.SetInt8sArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt8U:
                {
                    if (componentCount == 1)
                    {
                        handler.SetInt8u(tagId, reader.GetUInt8(tagValueOffset));
                    }
                    else
                    {
                        short[] array = new short[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetUInt8(tagValueOffset + i);
                        }
                        handler.SetInt8uArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt16S:
                {
                    if (componentCount == 1)
                    {
                        handler.SetInt16s(tagId, (int)reader.GetInt16(tagValueOffset));
                    }
                    else
                    {
                        short[] array = new short[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetInt16(tagValueOffset + (i * 2));
                        }
                        handler.SetInt16sArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt16U:
                {
                    if (componentCount == 1)
                    {
                        handler.SetInt16u(tagId, reader.GetUInt16(tagValueOffset));
                    }
                    else
                    {
                        int[] array = new int[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetUInt16(tagValueOffset + (i * 2));
                        }
                        handler.SetInt16uArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt32S:
                {
                    // NOTE 'long' in this case means 32 bit, not 64
                    if (componentCount == 1)
                    {
                        handler.SetInt32s(tagId, reader.GetInt32(tagValueOffset));
                    }
                    else
                    {
                        int[] array = new int[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetInt32(tagValueOffset + (i * 4));
                        }
                        handler.SetInt32sArray(tagId, array);
                    }
                    break;
                }

                case TiffDataFormat.CodeInt32U:
                {
                    // NOTE 'long' in this case means 32 bit, not 64
                    if (componentCount == 1)
                    {
                        handler.SetInt32u(tagId, reader.GetUInt32(tagValueOffset));
                    }
                    else
                    {
                        long[] array = new long[componentCount];
                        for (int i = 0; i < componentCount; i++)
                        {
                            array[i] = reader.GetUInt32(tagValueOffset + (i * 4));
                        }
                        handler.SetInt32uArray(tagId, array);
                    }
                    break;
                }

                default:
                {
                    handler.Error(Sharpen.Extensions.StringFormat("Unknown format code %d for tag %d", formatCode, tagId));
                    break;
                }
            }
        }
		/// <summary>Sets a <code>Rational</code> value for the specified tag.</summary>
		/// <param name="tagType">the tag's value as an int</param>
		/// <param name="rational">rational number</param>
		public virtual void SetRational(int tagType, Rational rational)
		{
			SetObject(tagType, rational);
		}
		/// <summary>Sets a <code>Rational[]</code> (array) for the specified tag.</summary>
		/// <param name="tagType">the tag identifier</param>
		/// <param name="rationals">the Rational array to store</param>
		public virtual void SetRationalArray(int tagType, Rational[] rationals)
		{
			SetObjectArray(tagType, rationals);
		}
 public virtual void TestToString()
 {
     Rational rational = new Rational(1, 3);
     Sharpen.Tests.AreEqual("1/3", Sharpen.Extensions.ConvertToString(rational));
 }
示例#10
0
        /// <summary>Reads an property value with given namespace URI and property name.</summary>
        /// <remarks>Reads an property value with given namespace URI and property name. Add property value to directory if exists</remarks>
        /// <exception cref="Com.Adobe.Xmp.XMPException"/>
        private static void ProcessXmpTag([NotNull] XMPMeta meta, [NotNull] XmpDirectory directory, int tagType, int formatCode)
        {
            string schemaNS = XmpDirectory._tagSchemaMap.Get(tagType);
            string propName = XmpDirectory._tagPropNameMap.Get(tagType);
            string property = meta.GetPropertyString(schemaNS, propName);
            if (property == null)
            {
                return;
            }
            switch (formatCode)
            {
                case FmtRational:
                {
                    string[] rationalParts = property.Split("/", 2);
                    if (rationalParts.Length == 2)
                    {
                        try
                        {
                            Rational rational = new Rational((long)float.Parse(rationalParts[0]), (long)float.Parse(rationalParts[1]));
                            directory.SetRational(tagType, rational);
                        }
                        catch (FormatException)
                        {
                            directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as a Rational.", propName));
                        }
                    }
                    else
                    {
                        directory.AddError("Error in rational format for tag " + tagType);
                    }
                    break;
                }

                case FmtInt:
                {
                    try
                    {
                        directory.SetInt(tagType, (int)Sharpen.Extensions.ValueOf(property));
                    }
                    catch (FormatException)
                    {
                        directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as an int.", propName));
                    }
                    break;
                }

                case FmtDouble:
                {
                    try
                    {
                        directory.SetDouble(tagType, (double)double.Parse(property));
                    }
                    catch (FormatException)
                    {
                        directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as an double.", propName));
                    }
                    break;
                }

                case FmtString:
                {
                    directory.SetString(tagType, property);
                    break;
                }

                case FmtStringArray:
                {
                    //XMP iterators are 1-based
                    int count = meta.CountArrayItems(schemaNS, propName);
                    string[] array = new string[count];
                    for (int i = 1; i <= count; ++i)
                    {
                        array[i - 1] = meta.GetArrayItem(schemaNS, propName, i).GetValue();
                    }
                    directory.SetStringArray(tagType, array);
                    break;
                }

                default:
                {
                    directory.AddError(Sharpen.Extensions.StringFormat("Unknown format code %d for tag %d", formatCode, tagType));
                    break;
                }
            }
        }
		public static double? DegreesMinutesSecondsToDecimal(Rational degs, Rational mins, Rational secs, bool isNegative)
		{
			double @decimal = Math.Abs(degs.DoubleValue()) + mins.DoubleValue() / 60.0d + secs.DoubleValue() / 3600.0d;
			if (double.IsNaN(@decimal))
			{
				return null;
			}
			if (isNegative)
			{
				@decimal *= -1;
			}
			return @decimal;
		}
示例#12
0
		/// <summary>Reads an property value with given namespace URI and property name.</summary>
		/// <remarks>Reads an property value with given namespace URI and property name. Add property value to directory if exists</remarks>
		/// <exception cref="Com.Adobe.Xmp.XMPException"/>
		private void ProcessXmpTag(XMPMeta meta, XmpDirectory directory, string schemaNS, string propName, int tagType, int formatCode)
		{
			string property = meta.GetPropertyString(schemaNS, propName);
			if (property == null)
			{
				return;
			}
			switch (formatCode)
			{
				case FmtRational:
				{
					string[] rationalParts = property.Split("/", 2);
					if (rationalParts.Length == 2)
					{
						try
						{
							Rational rational = new Rational((long)float.Parse(rationalParts[0]), (long)float.Parse(rationalParts[1]));
							directory.SetRational(tagType, rational);
						}
						catch (FormatException)
						{
							directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as a Rational.", propName));
						}
					}
					else
					{
						directory.AddError("Error in rational format for tag " + tagType);
					}
					break;
				}

				case FmtInt:
				{
					try
					{
						directory.SetInt(tagType, Sharpen.Extensions.ValueOf(property));
					}
					catch (FormatException)
					{
						directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as an int.", propName));
					}
					break;
				}

				case FmtDouble:
				{
					try
					{
						directory.SetDouble(tagType, double.Parse(property));
					}
					catch (FormatException)
					{
						directory.AddError(Sharpen.Extensions.StringFormat("Unable to parse XMP property %s as an double.", propName));
					}
					break;
				}

				case FmtString:
				{
					directory.SetString(tagType, property);
					break;
				}

				default:
				{
					directory.AddError(Sharpen.Extensions.StringFormat("Unknown format code %d for tag %d", formatCode, tagType));
					break;
				}
			}
		}
        public virtual void TestToString()
        {
            Rational rational = new Rational(1, 3);

            Sharpen.Tests.AreEqual("1/3", rational.ToString());
        }