/// <summary> /// Creates a <see cref="ByteVector"/> for the comment segment of this file /// </summary> /// <returns> /// A <see cref="ByteVector"/> with the whole comment segment, if a comment tag /// exists, otherwise null. /// </returns> private ByteVector RenderCOMSegment() { // check, if Comment is contained JpegCommentTag com_tag = GetTag(TagTypes.JpegComment) as JpegCommentTag; if (com_tag == null) { return(null); } // create comment data ByteVector com_data = ByteVector.FromString(com_tag.Value + "\0", StringType.Latin1); uint segment_size = (uint)(2 + com_data.Count); // do not render data segments, which cannot fit into the possible segment size if (segment_size > ushort.MaxValue) { throw new Exception("Comment Segment is too big to render"); } // create segment ByteVector data = new ByteVector(new byte [] { 0xFF, (byte)Marker.COM }); data.Add(ByteVector.FromUShort((ushort)segment_size)); data.Add(com_data); return(data); }
/// <summary> /// Reads a COM segment to find the JPEG comment. /// </summary> /// <param name="length"> /// The length of the segment that will be read. /// </param> private void ReadCOMSegment(int length) { if ((ImageTag.TagTypes & TagLib.TagTypes.JpegComment) != 0x00) { return; } long position = Tell; JpegCommentTag com_tag; if (length == 0) { com_tag = new JpegCommentTag(); } else { ByteVector data = ReadBlock(length); int terminator = data.Find("\0", 0); if (terminator < 0) { com_tag = new JpegCommentTag(data.ToString()); } else { com_tag = new JpegCommentTag(data.Mid(0, terminator).ToString()); } } ImageTag.AddTag(com_tag); AddMetadataBlock(position - 4, length + 4); }
private ByteVector RenderCOMSegment() { JpegCommentTag com_tag = GetTag(TagTypes.JpegComment) as JpegCommentTag; if (com_tag == null) { return(null); } ByteVector com_data = ByteVector.FromString(com_tag.Value + "\0", StringType.Latin1); uint segment_size = (uint)(2 + com_data.Count); if (segment_size > ushort.MaxValue) { throw new Exception("Comment Segment is too big to render"); } ByteVector data = new ByteVector(new byte[] { 0xFF, (byte)Marker.COM }); data.Add(ByteVector.FromUShort((ushort)segment_size)); data.Add(com_data); return(data); }
/// <summary> /// Gets a tag of a specified type from the current instance, /// optionally creating a new tag if possible. /// </summary> /// <param name="type"> /// A <see cref="TagLib.TagTypes" /> value indicating the /// type of tag to read. /// </param> /// <param name="create"> /// A <see cref="bool" /> value specifying whether or not to /// try and create the tag if one is not found. /// </param> /// <returns> /// A <see cref="Tag" /> object containing the tag that was /// found in or added to the current instance. If no /// matching tag was found and none was created, <see /// langword="null" /> is returned. /// </returns> public override TagLib.Tag GetTag (TagLib.TagTypes type, bool create) { foreach (Tag tag in ImageTag.AllTags) { if ((tag.TagTypes & type) == type) return tag; } if (!create || (type & ImageTag.AllowedTypes) == 0) return null; ImageTag new_tag = null; switch (type) { case TagTypes.JpegComment: new_tag = new JpegCommentTag (); break; case TagTypes.GifComment: new_tag = new GifCommentTag (); break; case TagTypes.Png: new_tag = new PngTag (); break; case TagTypes.TiffIFD: new_tag = new IFDTag (); break; case TagTypes.XMP: new_tag = new XmpTag (); break; } if (new_tag != null) { ImageTag.AddTag (new_tag); return new_tag; } throw new NotImplementedException (String.Format ("Adding tag of type {0} not supported!", type)); }
/// <summary> /// Reads a COM segment to find the JPEG comment. /// </summary> /// <param name="length"> /// The length of the segment that will be read. /// </param> private void ReadCOMSegment (int length) { if ((ImageTag.TagTypes & TagLib.TagTypes.JpegComment) != 0x00) return; long position = Tell; JpegCommentTag com_tag; if (length == 0) { com_tag = new JpegCommentTag (); } else { ByteVector data = ReadBlock (length); int terminator = data.Find ("\0", 0); if (terminator < 0) com_tag = new JpegCommentTag (data.ToString ()); else com_tag = new JpegCommentTag (data.Mid (0, terminator).ToString ()); } ImageTag.AddTag (com_tag); AddMetadataBlock (position - 4, length + 4); }
/// <summary> /// Reads a COM segment to find the JPEG comment. /// </summary> /// <param name="length"> /// The length of the segment that will be read. /// </param> private bool ReadCOMSegment (int length) { if (comment_position != 0) return false; comment_position = Tell; JpegCommentTag com_tag; if (length == 0) com_tag = new JpegCommentTag (); else com_tag = new JpegCommentTag (ReadBlock (length - 1).ToString ()); ImageTag.AddTag (com_tag); return true; }