Skip to content

Commit

Permalink
Improved handling of picture frames. Passes all tests now.
Browse files Browse the repository at this point in the history
  • Loading branch information
sahands committed Feb 26, 2014
1 parent 14c1670 commit a19d53b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 21 deletions.
4 changes: 4 additions & 0 deletions ID3/ID3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<Compile Include="Source\Tag\TagBase.cs" />
<Compile Include="Source\Tag\TagHeader.cs" />
<Compile Include="Source\V1\ID3v1Tag.cs" />
<Compile Include="Source\Utils\ImagingHelpers.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Documentation\ExtensibilityGuide.txt" />
Expand All @@ -203,4 +204,7 @@
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup>
<Folder Include="Source\Utils\" />
</ItemGroup>
</Project>
20 changes: 8 additions & 12 deletions ID3/Source/Frame Implementations/Writers/PictureFrameWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public override void WriteToStream(System.IO.Stream stream)
PictureFrame frame=(PictureFrame)this.FrameToWrite;
List<Field> fields=new List<Field>();

String imageFormat = Utils.ImagingHelpers.ImageFormatToExtension(frame.Picture.RawFormat);

// Declare the fields to write.
fields.Add(new SingleByteField((byte)this.Encoding));
fields.Add(new FixedLengthAsciiTextField("JPG"));
fields.Add(new FixedLengthAsciiTextField(imageFormat));
fields.Add(new SingleByteField((byte)frame.PictureType));
fields.Add(TextField.CreateTextField(frame.Description, this.Encoding));

System.IO.MemoryStream memoryBuffer=new System.IO.MemoryStream();
frame.Picture.Save(memoryBuffer, System.Drawing.Imaging.ImageFormat.Jpeg);
fields.Add(new BinaryField(memoryBuffer.GetBuffer(), 0, (int)memoryBuffer.Length));
memoryBuffer.Close();
fields.Add(new BinaryField(frame.RawData, 0, (int)frame.RawData.Length));

// Write the header
int length=0;
Expand Down Expand Up @@ -56,16 +54,14 @@ public override void WriteToStream(System.IO.Stream stream)
PictureFrame frame=(PictureFrame)this.FrameToWrite;
List<Field> fields=new List<Field>();

string imageMimeType = Utils.ImagingHelpers.ImageFormatToMimeType (frame.Picture.RawFormat);

// Declare the fields to write.
fields.Add(new SingleByteField((byte)this.Encoding));
fields.Add(TextField.CreateTextField("image/jpeg", EncodingScheme.Ascii));
fields.Add(TextField.CreateTextField(imageMimeType, EncodingScheme.Ascii));
fields.Add(new SingleByteField((byte)frame.PictureType));
fields.Add(TextField.CreateTextField(frame.Description, this.Encoding));

System.IO.MemoryStream memoryBuffer=new System.IO.MemoryStream();
frame.Picture.Save(memoryBuffer, System.Drawing.Imaging.ImageFormat.Jpeg);
fields.Add(new BinaryField(memoryBuffer.GetBuffer(), 0, (int)memoryBuffer.Length));
memoryBuffer.Close();
fields.Add(new BinaryField(frame.RawData, 0, (int)frame.RawData.Length));

// Write the header
int length=0;
Expand Down
30 changes: 21 additions & 9 deletions ID3/Source/Frames/Other/PictureFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ public string Description
}
}

private byte[] _raw_data = null;
private System.Drawing.Image _image;
public System.Drawing.Image Picture
private byte[] _rawData = null;
public byte[] RawData
{
get
get
{
return _image;
if (this._rawData == null)
{
this.LoadRawDataFromImage();
}
return this._rawData;
}
}

public byte[] RawData
private System.Drawing.Image _image;
public System.Drawing.Image Picture
{
get
get
{
return this._raw_data;
return _image;
}
}

Expand All @@ -51,6 +55,14 @@ public PictureType PictureType
}
}

protected void LoadRawDataFromImage()
{
System.IO.MemoryStream memoryBuffer=new System.IO.MemoryStream();
this._image.Save(memoryBuffer, this._image.RawFormat);
this._rawData = memoryBuffer.GetBuffer();
memoryBuffer.Close();
}

protected PictureFrame(string description, PictureType pictureType)
: base()
{
Expand Down Expand Up @@ -81,7 +93,7 @@ public PictureFrame(byte[] raw_data, System.Drawing.Image image, string descript
{
throw new ArgumentNullException("The passed image raw data can not be null.");
}
this._raw_data = raw_data;
this._rawData = raw_data;
}


Expand Down
68 changes: 68 additions & 0 deletions ID3/Source/Utils/ImagingHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Drawing.Imaging;

namespace Achamenes.ID3.Utils
{
public class ImagingHelpers
{
public static string ImageFormatToMimeType(ImageFormat imageFormat)
{
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())
{
if (codec.FormatID == imageFormat.Guid)
{
return codec.MimeType;
}
}
return "application/octet-stream";
}

// Taken from http://www.java2s.com/Code/CSharp/File-Stream/ImageFormattoExtension.htm
// And modified.
public static string ImageFormatToExtension(ImageFormat imageFormat)
{
if (imageFormat.Equals(ImageFormat.Bmp))
{
return "BMP";
}
else if (imageFormat.Equals(ImageFormat.MemoryBmp))
{
return "BMP";
}
else if (imageFormat.Equals(ImageFormat.Wmf))
{
return "EMF";
}
else if (imageFormat.Equals(ImageFormat.Wmf))
{
return "WMF";
}
else if (imageFormat.Equals(ImageFormat.Gif))
{
return "GIF";
}
else if (imageFormat.Equals(ImageFormat.Jpeg))
{
return "JPG";
}
else if (imageFormat.Equals(ImageFormat.Png))
{
return "PNG";
}
else if (imageFormat.Equals(ImageFormat.Tiff))
{
return "TIF";
}
else if (imageFormat.Equals(ImageFormat.Exif))
{
return "EXF";
}
else if (imageFormat.Equals(ImageFormat.Icon))
{
return "ICO";
}
return "";
}
}
}

0 comments on commit a19d53b

Please sign in to comment.