public TiffIfdEntryInfo(ushort tag, TiffType type, int count, byte[] data) { Tag = tag; Type = type; Count = count; Data = data; }
public byte[] ConvertToBytes(TiffType type, uint value) { switch (type) { case TiffType.Byte: return(new byte[] { (byte)value }); case TiffType.Short: return(BitConverter.GetBytes((ushort)value).WithByteOrder(_byteOrder)); case TiffType.Long: return(BitConverter.GetBytes((uint)value).WithByteOrder(_byteOrder)); case TiffType.SByte: return(BitConverter.GetBytes((sbyte)value)); case TiffType.SShort: return(BitConverter.GetBytes((short)value).WithByteOrder(_byteOrder)); case TiffType.SLong: return(BitConverter.GetBytes((int)value).WithByteOrder(_byteOrder)); default: throw new InvalidOperationException("TiffIfdBuilder cannot convert this data type."); } }
public static int SizeOfDataType(TiffType type) { switch (type) { case TiffType.Byte: case TiffType.Ascii: case TiffType.SByte: case TiffType.Undefined: return(1); case TiffType.Short: case TiffType.SShort: return(2); case TiffType.Long: case TiffType.SLong: case TiffType.Float: case TiffType.Ifd: return(4); case TiffType.Rational: case TiffType.SRational: case TiffType.Double: return(8); default: return(0); } }
public void GetFillOrder_ReturnsValue(ByteOrder byteOrder, TiffType type, int?data, TiffFillOrder expectedResult) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.FillOrder, type, data, byteOrder).Ifd; var result = ifd.GetFillOrder(byteOrder); Assert.Equal(expectedResult, result); }
public void GetGrayResponseUnit_ReturnsValue(ByteOrder byteOrder, TiffType type, int?data, double expectedResult) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.GrayResponseUnit, type, data, byteOrder).Ifd; var result = ifd.GetGrayResponseUnit(byteOrder); Assert.Equal(expectedResult, result); }
public void GetPhotometricInterpretation_ReturnsValue(ByteOrder byteOrder, TiffType type, int?data, TiffPhotometricInterpretation?expectedResult) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.PhotometricInterpretation, type, data, byteOrder).Ifd; var result = ifd.GetPhotometricInterpretation(byteOrder); Assert.Equal(expectedResult, result); }
public void GetPlanarConfiguraion_ReturnsValue(ByteOrder byteOrder, TiffType type, int?data, TiffPlanarConfiguration expectedResult) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.PlanarConfiguration, type, data, byteOrder).Ifd; var result = ifd.GetPlanarConfiguration(byteOrder); Assert.Equal(expectedResult, result); }
public void GetResolutionUnit_ReturnsValue(ByteOrder byteOrder, TiffType type, int?data, TiffResolutionUnit expectedResult) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.ResolutionUnit, type, data, byteOrder).Ifd; var result = ifd.GetResolutionUnit(byteOrder); Assert.Equal(expectedResult, result); }
private uint insertData(TiffType type, int v) { int t = (int)type; if (m_header.tiff_magic == TIFF_BIGENDIAN) return (((uint)v & m_typemask[t]) << m_typeshift[t]); return ((uint)v & m_typemask[t]); }
public PRVW(byte[] content, uint offset) { Size = TiffType.getInt(offset, content, true);; Name = Encoding.ASCII.GetString(content, (int)offset + 4, 4).ToString(); JPGSize = TiffType.getInt(offset + 0x14, content, true); JPG = new byte[JPGSize]; Array.Copy(content, offset + 0x18, JPG, 0, JPGSize); }
/// <summary> /// Writes the specified TIFF tag data to the stream. /// </summary> /// <param name="stream">The stream to write to.</param> /// <param name="tag">The <see cref="TiffTag"/> to write.</param> /// <param name="type">The <see cref="TiffType"/> being written.</param> /// <param name="count">The number of values being written.</param> /// <param name="value">The actual value to be written.</param> private static void WriteTiffTag(Stream stream, TiffTag tag, TiffType type, uint count, uint value) { if (stream == null) return; stream.Write(BitConverter.GetBytes((uint)tag), 0, 2); stream.Write(BitConverter.GetBytes((uint)type), 0, 2); stream.Write(BitConverter.GetBytes(count), 0, 4); stream.Write(BitConverter.GetBytes(value), 0, 4); }
private bool writeRationalPair(TiffDirEntry[] entries, int dirOffset, TiffType type, TiffTag tag1, float v1, TiffTag tag2, float v2) { if (!writeRational(type, tag1, ref entries[dirOffset], v1)) return false; if (!writeRational(type, tag2, ref entries[dirOffset + 1], v2)) return false; return true; }
public TiffIfdBuilder WithIfdEntry(ushort tag, TiffType type, uint?value) { if (value != null) { var data = ConvertToBytes(type, value.Value); _entries.Add(new TiffIfdEntryInfo(tag, type, 1, data)); } return(this); }
public TiffIfdBuilder WithIfdEntry(ushort tag, TiffType type, uint[] value) { if (value != null) { var data = value.SelectMany(v => ConvertToBytes(type, v)).ToArray(); _entries.Add(new TiffIfdEntryInfo(tag, type, value.Length, data)); } return(this); }
public TiffIfdBuilder WithIfdEntry(ushort tag, TiffType type, string value) { if (value != null) { var data = Encoding.ASCII.GetBytes((string)value); _entries.Add(new TiffIfdEntryInfo(tag, type, data.Length, data)); } return(this); }
public async Task ReadExtraSamplesAsync_ReturnsValue(ByteOrder byteOrder, TiffType type, uint[] data, TiffExtraSamples[] expectedResult) { var ifdStreamTuple = TiffIfdBuilder.GenerateIfd(TiffTags.ExtraSamples, type, data, byteOrder); var ifd = ifdStreamTuple.Ifd; var stream = ifdStreamTuple.Stream; var result = await ifd.ReadExtraSamplesAsync(stream, byteOrder); Assert.Equal(expectedResult, result); }
public TiffIfdBuilder WithIfdEntry(ushort tag, TiffType type, Rational?value) { if (value != null) { var rational = value.Value; var data = new[] { rational.Numerator, rational.Denominator }.SelectMany(v => ConvertToBytes(TiffType.Long, v)).ToArray(); _entries.Add(new TiffIfdEntryInfo(tag, type, 1, data)); } return(this); }
public void GetRowsPerStrip_ReturnsValue(ByteOrder byteOrder, TiffType type, uint?value) { var ifd = TiffIfdBuilder.GenerateIfd(TiffTags.RowsPerStrip, type, value, byteOrder).Ifd; var result = ifd.GetRowsPerStrip(byteOrder); if (value == null) { value = UInt32.MaxValue; // Default value } Assert.Equal(value, result); }
public static TiffIfdEntry ParseIfdEntry(byte[] bytes, int offset, ByteOrder byteOrder) { ushort tag = DataConverter.ToUInt16(bytes, offset + 0, byteOrder); TiffType type = (TiffType)DataConverter.ToUInt16(bytes, offset + 2, byteOrder); int count = DataConverter.ToInt32(bytes, offset + 4, byteOrder); byte[] value = DataConverter.ToBytes(bytes, offset + 8, 4); return(new TiffIfdEntry { Tag = tag, Type = type, Count = count, Value = value }); }
/// <summary> /// Initializes a new instance of the <see cref="TiffFieldInfo"/> class. /// </summary> /// <param name="tag">The tag to describe.</param> /// <param name="readCount">The number of values to read when reading field information or /// one of <see cref="Variable"/>, <see cref="Spp"/> and <see cref="Variable2"/>.</param> /// <param name="writeCount">The number of values to write when writing field information /// or one of <see cref="Variable"/>, <see cref="Spp"/> and <see cref="Variable2"/>.</param> /// <param name="type">The type of the field value.</param> /// <param name="bit">Index of the bit to use in "Set Fields Vector" when this instance /// is merged into field info collection. Take a look at <see cref="FieldBit"/> class.</param> /// <param name="okToChange">If true, then it is permissible to set the tag's value even /// after writing has commenced.</param> /// <param name="passCount">If true, then number of value elements should be passed to /// <see cref="Tiff.SetField"/> method as second parameter (right after tag type AND /// before value itself).</param> /// <param name="name">The name (description) of the tag this instance describes.</param> public TiffFieldInfo(TiffTag tag, short readCount, short writeCount, TiffType type, short bit, bool okToChange, bool passCount, string name) { m_tag = tag; m_readCount = readCount; m_writeCount = writeCount; m_type = type; m_bit = bit; m_okToChange = okToChange; m_passCount = passCount; m_name = name; }
private bool writeRational(TiffType type, TiffTag tag, ref TiffDirEntry dir, float v) { dir.tdir_tag = tag; dir.tdir_type = type; dir.tdir_count = 1; float[] a = new float[1]; a[0] = v; if (!writeRationalArray(ref dir, a)) return false; return true; }
/// <summary> /// Retrieves field information for the tag with specified name. /// </summary> /// <param name="name">The name of the tag to retrieve field information for.</param> /// <param name="type">The tiff data type to use us additional filter.</param> /// <returns>The field information for specified tag with specified type or <c>null</c> if /// the field information wasn't found.</returns> public TiffFieldInfo FindFieldInfoByName(string name, TiffType type) { if (m_foundfield != null && m_foundfield.Name == name && (type == TiffType.Any || type == m_foundfield.Type)) { return m_foundfield; } // If we are invoked with no field information, then just return. if (m_fieldinfo == null) return null; m_foundfield = null; foreach (TiffFieldInfo info in m_fieldinfo) { if (info != null && info.Name == name && (type == TiffType.Any || type == info.Type)) { m_foundfield = info; break; } } return m_foundfield; }
/* * Return size of TiffDataType in bytes. * * XXX: We need a separate function to determine the space needed * to store the value. For TiffType.Rational values DataWidth() * returns 8, but we use 4-byte float to represent rationals. */ internal static int dataSize(TiffType type) { switch (type) { case TiffType.Byte: case TiffType.SByte: case TiffType.ASCII: case TiffType.Undefined: return 1; case TiffType.Short: case TiffType.SShort: return 2; case TiffType.Long: case TiffType.SLong: case TiffType.Float: case TiffType.IFD: case TiffType.Rational: case TiffType.SRational: return 4; case TiffType.Double: return 8; default: return 0; } }
private static TiffFieldInfo createAnonFieldInfo(TiffTag tag, TiffType field_type) { TiffFieldInfo fld = new TiffFieldInfo(tag, TiffFieldInfo.Variable2, TiffFieldInfo.Variable2, field_type, FieldBit.Custom, true, true, null); // note that this name is a special sign to Close() and // setupFieldInfo() to free the field fld.Name = string.Format(CultureInfo.InvariantCulture, "Tag {0}", tag); return fld; }
/// <summary> /// Writes an array of "type" values for a specified tag (i.e. this is /// a tag which is allowed to have different types, e.g. SMaxSampleType). /// Internally the data values are represented as double since a double /// can hold any of the TIFF tag types (yes, this should really be an abstract /// type tany_t for portability). The data is converted into the specified /// type in a temporary buffer and then handed off to the appropriate array /// writer. /// </summary> private bool writeAnyArray(TiffType type, TiffTag tag, ref TiffDirEntry dir, int n, double[] v) { dir.tdir_tag = tag; dir.tdir_type = type; dir.tdir_count = n; bool failed = false; switch (type) { case TiffType.Byte: case TiffType.SByte: { byte[] bp = new byte[n]; for (int i = 0; i < n; i++) bp[i] = (byte)v[i]; if (!writeByteArray(ref dir, bp)) failed = true; } break; case TiffType.Short: case TiffType.SShort: { short[] bp = new short[n]; for (int i = 0; i < n; i++) bp[i] = (short)v[i]; if (!writeShortArray(ref dir, bp)) failed = true; } break; case TiffType.Long: case TiffType.SLong: { int[] bp = new int[n]; for (int i = 0; i < n; i++) bp[i] = (int)v[i]; if (!writeLongArray(ref dir, bp)) failed = true; } break; case TiffType.Float: { float[] bp = new float[n]; for (int i = 0; i < n; i++) bp[i] = (float)v[i]; if (!writeFloatArray(ref dir, bp)) failed = true; } break; case TiffType.Double: if (!writeDoubleArray(ref dir, v)) failed = true; break; default: // NoType // ASCII // Undefined // Rational // SRational failed = true; break; } return !failed; }
/* * Setup a directory entry that references a samples/pixel array of ``type'' * values and (potentially) write the associated indirect values. The source * data from GetField() for the specified tag must be returned as double. */ private bool writePerSampleAnys(TiffType type, TiffTag tag, ref TiffDirEntry dir) { double[] w = new double[m_dir.td_samplesperpixel]; FieldValue[] result = GetField(tag); double v = result[0].ToDouble(); for (short i = 0; i < m_dir.td_samplesperpixel; i++) w[i] = v; bool status = writeAnyArray(type, tag, ref dir, m_dir.td_samplesperpixel, w); return status; }
/// <summary> /// Gets the number of bytes occupied by the item of given type. /// </summary> /// <param name="type">The type.</param> /// <returns>The number of bytes occupied by the <paramref name="type"/> or 0 if unknown /// data type is supplied.</returns> public static int DataWidth(TiffType type) { switch (type) { case TiffType.NoType: case TiffType.Byte: case TiffType.ASCII: case TiffType.SByte: case TiffType.Undefined: return 1; case TiffType.Short: case TiffType.SShort: return 2; case TiffType.Long: case TiffType.SLong: case TiffType.Float: case TiffType.IFD: return 4; case TiffType.Rational: case TiffType.SRational: case TiffType.Double: return 8; default: // will return 0 for unknown types return 0; } }
public tagToCopy(TiffTag _tag, short _count, TiffType _type) { tag = _tag; count = _count; type = _type; }
private static void copyTag(Tiff inImage, Tiff outImage, TiffTag tag, short count, TiffType type) { FieldValue[] result = null; switch (type) { case TiffType.SHORT: result = inImage.GetField(tag); if (result != null) { if (count == 1) outImage.SetField(tag, result[0]); else if (count == 2) outImage.SetField(tag, result[0], result[1]); else if (count == 4) outImage.SetField(tag, result[0], result[1], result[2]); else if (count == -1) outImage.SetField(tag, result[0], result[1]); } break; case TiffType.LONG: result = inImage.GetField(tag); if (result != null) outImage.SetField(tag, result[0]); break; case TiffType.RATIONAL: result = inImage.GetField(tag); if (result != null) outImage.SetField(tag, result[0]); break; case TiffType.ASCII: result = inImage.GetField(tag); if (result != null) outImage.SetField(tag, result[0]); break; case TiffType.DOUBLE: result = inImage.GetField(tag); if (result != null) outImage.SetField(tag, result[0]); break; default: Tiff.Error(inImage.FileName(), "Data type {0} is not supported, tag {1} skipped.", tag, type); break; } }
/// <summary> /// Writes an array of "type" values for a specified tag (i.e. this is /// a tag which is allowed to have different types, e.g. SMaxSampleType). /// Internally the data values are represented as double since a double /// can hold any of the TIFF tag types (yes, this should really be an abstract /// type tany_t for portability). The data is converted into the specified /// type in a temporary buffer and then handed off to the appropriate array /// writer. /// </summary> private bool writeAnyArray(TiffType type, TiffTag tag, ref TiffDirEntry dir, int n, double[] v) { dir.tdir_tag = tag; dir.tdir_type = type; dir.tdir_count = n; bool failed = false; switch (type) { case TiffType.BYTE: case TiffType.SBYTE: { byte[] bp = new byte [n]; for (int i = 0; i < n; i++) bp[i] = (byte)v[i]; if (!writeByteArray(ref dir, bp)) failed = true; } break; case TiffType.SHORT: case TiffType.SSHORT: { short[] bp = new short [n]; for (int i = 0; i < n; i++) bp[i] = (short)v[i]; if (!writeShortArray(ref dir, bp)) failed = true; } break; case TiffType.LONG: case TiffType.SLONG: { int[] bp = new int [n]; for (int i = 0; i < n; i++) bp[i] = (int)v[i]; if (!writeLongArray(ref dir, bp)) failed = true; } break; case TiffType.FLOAT: { float[] bp = new float [n]; for (int i = 0; i < n; i++) bp[i] = (float)v[i]; if (!writeFloatArray(ref dir, bp)) failed = true; } break; case TiffType.DOUBLE: if (!writeDoubleArray(ref dir, v)) failed = true; break; default: // NOTYPE // ASCII // UNDEFINED // RATIONAL // SRATIONAL failed = true; break; } return !failed; }
/* * Return size of TiffDataType in bytes. * * XXX: We need a separate function to determine the space needed * to store the value. For TiffType.RATIONAL values DataWidth() * returns 8, but we use 4-byte float to represent rationals. */ internal static int dataSize(TiffType type) { switch (type) { case TiffType.BYTE: case TiffType.SBYTE: case TiffType.ASCII: case TiffType.UNDEFINED: return 1; case TiffType.SHORT: case TiffType.SSHORT: return 2; case TiffType.LONG: case TiffType.SLONG: case TiffType.FLOAT: case TiffType.IFD: case TiffType.RATIONAL: case TiffType.SRATIONAL: return 4; case TiffType.DOUBLE: return 8; default: return 0; } }
/// <summary> /// Gets the number of bytes occupied by the item of given type. /// </summary> /// <param name="type">The type.</param> /// <returns>The number of bytes occupied by the <paramref name="type"/> or 0 if unknown /// data type is supplied.</returns> public static int DataWidth(TiffType type) { switch (type) { case TiffType.NOTYPE: case TiffType.BYTE: case TiffType.ASCII: case TiffType.SBYTE: case TiffType.UNDEFINED: return 1; case TiffType.SHORT: case TiffType.SSHORT: return 2; case TiffType.LONG: case TiffType.SLONG: case TiffType.FLOAT: case TiffType.IFD: return 4; case TiffType.RATIONAL: case TiffType.SRATIONAL: case TiffType.DOUBLE: return 8; default: // will return 0 for unknown types return 0; } }
/// <summary> /// Retrieves field information for the specified tag. /// </summary> /// <param name="tag">The tag to retrieve field information for.</param> /// <param name="type">The tiff data type to use us additional filter.</param> /// <returns>The field information for specified tag with specified type or <c>null</c> if /// the field information wasn't found.</returns> public TiffFieldInfo FindFieldInfo(TiffTag tag, TiffType type) { if (m_foundfield != null && m_foundfield.Tag == tag && (type == TiffType.ANY || type == m_foundfield.Type)) { return m_foundfield; } // If we are invoked with no field information, then just return. if (m_fieldinfo == null) return null; m_foundfield = null; foreach (TiffFieldInfo info in m_fieldinfo) { if (info != null && info.Tag == tag && (type == TiffType.ANY || type == info.Type)) { m_foundfield = info; break; } } return m_foundfield; }