//
// MissingEmbeddedArtwork.cs
//
// Author:
// Jacob Johnston (jacobj@inchoatethoughts.com)
//
// Copyright (C) 2009 Jacob Johnston
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
using System.Drawing;
using System.IO;
using ZenLibrary.RuleBase;
namespace ZenLibrary.Rules
{
///
/// This rule scans all MP3s for the presence of an embedded image
/// that is marked as the front cover. If the image is found, it will
/// then check to see if the image meets the required image size.
///
class MissingEmbeddedArtwork : ConfigurableRule
{
private int artHeight = 500;
private int artWidth = 500;
public override string Name
{
get { return "Has Embedded Artwork"; }
}
public override void Configure()
{
char[] splitChars = { ';' };
MissingEmbeddedArtworkConfig configWindow = new MissingEmbeddedArtworkConfig();
configWindow.ArtHeightText.Text = artHeight.ToString();
configWindow.ArtWidthText.Text = artWidth.ToString();
if (configWindow.ShowDialog() == true)
{
artHeight = int.Parse(configWindow.ArtHeightText.Text);
artWidth = int.Parse(configWindow.ArtWidthText.Text);
}
Config.Instance.SaveConfig();
}
public override TestType TestType
{
get { return TestType.FileScan; }
}
protected override void WriteCustomConfigAttributes(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("ArtHeight", artHeight.ToString());
writer.WriteAttributeString("ArtWidth", artWidth.ToString());
base.WriteCustomConfigAttributes(writer);
}
protected override void ReadCustomConfigAttributes(System.Xml.XmlReader reader)
{
artHeight = int.Parse(reader["ArtHeight"]);
artWidth = int.Parse(reader["ArtWidth"]);
base.ReadCustomConfigAttributes(reader);
}
public override RuleTestResult RunTest(DirectoryInfo directoryInfo, FileInfo fileInfo)
{
bool hasPicture = false;
bool pictureMeetsSizeRequirements = false;
int maxImageWidth = 0;
int maxImageHeight = 0;
TagLib.File file = TagLib.File.Create(fileInfo.FullName);
foreach (TagLib.IPicture pic in file.Tag.Pictures)
{
if (pic.Type != TagLib.PictureType.FrontCover)
continue;
hasPicture = true;
using (MemoryStream memStream = new MemoryStream(pic.Data.Data))
{
using (Image image = Bitmap.FromStream(memStream))
{
maxImageHeight = Math.Max(maxImageHeight, image.Height);
maxImageWidth = Math.Max(maxImageWidth, image.Width);
if (image.Width >= artWidth && image.Height >= artHeight)
{
pictureMeetsSizeRequirements = true;
break;
}
}
}
}
if (hasPicture && pictureMeetsSizeRequirements)
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = true;
return result;
}
else if(hasPicture && !pictureMeetsSizeRequirements)
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = false;
result.ResultPath = fileInfo.FullName;
result.RuleTestFailedString = string.Format("File \"{0}\" contains embedded artwork, but it is only {1} x {2} pixels in size.", result.ResultPath, maxImageWidth, maxImageHeight);
return result;
}
else
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = false;
result.ResultPath = fileInfo.FullName;
result.RuleTestFailedString = string.Format("File \"{0}\" does not contain embedded album artwork.", result.ResultPath);
return result;
}
}
}
}