Fix issues with downloaded album art.

This commit is contained in:
Grigory Chudov
2018-11-25 15:12:51 -05:00
parent ededb2112c
commit 3f121a8f9e
8 changed files with 1464 additions and 900 deletions

View File

@@ -240,7 +240,7 @@ namespace CUETools.Processor
Country = metadata.Country;
Label = metadata.Label;
LabelNo = metadata.LabelNo;
AlbumArt = metadata.AlbumArt;
AlbumArt = metadata.AlbumArt.ConvertAll(x => new CTDB.CTDBResponseMetaImage(x));
for (int i = 0; i < Tracks.Count; i++)
{
Tracks[i].Title = metadata.Tracks[i].Title;

View File

@@ -7,6 +7,7 @@ namespace CUETools.Processor
public CUEMetadata metadata { get; set; }
public CDImageLayout TOC { get; set; }
public string ImageKey { get; set; }
public byte[] cover { get; set; }
public CUEMetadataEntry(CUEMetadata metadata, CDImageLayout TOC, string key)
{

View File

@@ -34,6 +34,7 @@ namespace CUETools.Processor
private string _htoaFilename, _singleFilename;
private bool _hasHTOAFilename = false, _hasTrackFilenames = false, _hasSingleFilename = false, _appliedWriteOffset;
private bool _hasEmbeddedCUESheet;
private bool _hasEmbeddedArtwork;
private bool _paddedToFrame, _truncated4608, _usePregapForFirstTrackInSingleFile;
private int _writeOffset;
private CUEAction _action;
@@ -130,9 +131,12 @@ namespace CUETools.Processor
{
get
{
if (AlbumArt == null || AlbumArt.Count == 0)
if (AlbumArt == null)
return null;
var picture = AlbumArt.Find(x => x.Type == TagLib.PictureType.FrontCover) ??
AlbumArt.Find(x => x.Type != TagLib.PictureType.NotAPicture);
if (picture == null)
return null;
TagLib.IPicture picture = AlbumArt[0];
using (MemoryStream imageStream = new MemoryStream(picture.Data.Data, 0, picture.Data.Count))
try { return Image.FromStream(imageStream); }
catch { }
@@ -551,6 +555,7 @@ namespace CUETools.Processor
_minDataTrackLength = null;
hdcdDecoder = null;
_hasEmbeddedCUESheet = false;
_hasEmbeddedArtwork = false;
_isArchive = false;
_isCD = false;
_useLocalDB = false;
@@ -837,8 +842,15 @@ namespace CUETools.Processor
dbmeta = myEntry.Metadata;
}
byte[] frontCover = null;
{
var picture = AlbumArt.Find(x => x.Type == TagLib.PictureType.FrontCover) ??
AlbumArt.Find(x => x.Type != TagLib.PictureType.NotAPicture);
if (picture != null) frontCover = picture.Data.Data;
}
if (dbmeta != null)
Releases.Add(new CUEMetadataEntry(dbmeta, TOC, "local"));
Releases.Add(new CUEMetadataEntry(dbmeta, TOC, "local") { cover = frontCover });
//if (useCache)
//{
@@ -859,12 +871,12 @@ namespace CUETools.Processor
if (dbmeta == null || !dbmeta.Contains(cueMetadata))
{
if (cueMetadata.Contains(taglibMetadata) || !taglibMetadata.Contains(cueMetadata))
Releases.Add(new CUEMetadataEntry(new CUEMetadata(cueMetadata), TOC, "cue"));
Releases.Add(new CUEMetadataEntry(new CUEMetadata(cueMetadata), TOC, "cue") { cover = frontCover });
}
if (dbmeta == null || !dbmeta.Contains(taglibMetadata))
{
if (!cueMetadata.Contains(taglibMetadata))
Releases.Add(new CUEMetadataEntry(new CUEMetadata(taglibMetadata), TOC, "tags"));
Releases.Add(new CUEMetadataEntry(new CUEMetadata(taglibMetadata), TOC, "tags") { cover = frontCover });
}
}
@@ -872,7 +884,7 @@ namespace CUETools.Processor
{
foreach (var entry in _localDB)
if (entry.DiscID == TOC.TOCID && entry.Metadata != null && (dbmeta == null || !dbmeta.Contains(entry.Metadata)))
Releases.Add(new CUEMetadataEntry(entry.Metadata, TOC, "local"));
Releases.Add(new CUEMetadataEntry(entry.Metadata, TOC, "local") { cover = frontCover });
}
bool ctdbFound = false;
@@ -1737,11 +1749,7 @@ namespace CUETools.Processor
}
LoadAlbumArt(_tracks[0]._fileInfo ?? _fileInfo);
#if NET47 || NET20
ResizeAlbumArt();
#endif
if (_config.embedAlbumArt || _config.CopyAlbumArt)
_albumArt.ForEach(t => _padding += t.Data.Count);
if (_config.embedLog && _eacLog != null)
_padding += _eacLog.Length;
@@ -1750,6 +1758,63 @@ namespace CUETools.Processor
// TODO: It should also be set when assigning a DataTrack!!!
}
public void LoadAndResizeAlbumArt()
{
if (Action != CUEAction.Encode)
return;
if (!_hasEmbeddedArtwork)
{
var images = Metadata.AlbumArt;
var primaryImages = images.FindAll(x => x.primary);
if (primaryImages.Count > 0) images = primaryImages;
foreach (var imageMeta in images)
{
// TODO: load secondary album art?
// TODO: load uri150 version first, load full version in background of choice form?
var ms = new MemoryStream();
if (CTDB.FetchFile(imageMeta.uri, ms))
{
TagLib.Picture pic = new TagLib.Picture(new TagLib.ByteVector(ms.ToArray()));
pic.Description = imageMeta.uri;
using (MemoryStream imageStream = new MemoryStream(pic.Data.Data, 0, pic.Data.Count))
try
{
#if NET47 || NET20
var image = Image.FromStream(ms);
pic.Description += $" ({image.Width}x{image.Height})";
//if (image.Height > 0 && image.Width > 0 && (image.Height * 1.0 / image.Width) > 0.9 && (image.Width * 1.0 / image.Height) > 0.9)
// isSquare = true;
// pic.MimeType = f(image.RawFormat);
#endif
}
catch { }
_albumArt.Add(pic);
}
}
CheckStop();
if (_albumArt.Count != 1 || _albumArt[0].Type != TagLib.PictureType.FrontCover)
{
if (_albumArt.Count == 0 || CUEToolsSelection == null)
{
_albumArt.Clear();
}
else
{
CUEToolsSelectionEventArgs e = new CUEToolsSelectionEventArgs();
e.choices = _albumArt.ToArray();
CUEToolsSelection(this, e);
TagLib.IPicture selected = e.selection == -1 ? null : _albumArt[e.selection];
_albumArt.RemoveAll(t => t != selected);
}
}
}
#if NET47 || NET20
ResizeAlbumArt();
#endif
}
public void UseCUEToolsDB(string userAgent, string driveName, bool fuzzy, CTDBMetadataSearch metadataSearch)
{
ShowProgress((string)"Contacting CUETools database...", 0, null, null);
@@ -2920,16 +2985,21 @@ namespace CUETools.Processor
}
}
public void LoadAlbumArt(TagLib.File fileInfo)
private void LoadAlbumArt(TagLib.File fileInfo)
{
if ((_config.extractAlbumArt || _config.CopyAlbumArt) && fileInfo != null)
foreach (TagLib.IPicture picture in fileInfo.Tag.Pictures)
if (picture.Type == TagLib.PictureType.FrontCover)
if (picture.MimeType == "image/jpeg")
{
_albumArt.Add(picture);
return;
}
if (_config.extractAlbumArt || _config.CopyAlbumArt)
{
if (fileInfo != null)
foreach (TagLib.IPicture picture in fileInfo.Tag.Pictures)
{
if (picture.Type == TagLib.PictureType.NotAPicture) continue;
// if (picture.MimeType == "image/jpeg" && picture.Type == TagLib.PictureType.FrontCover)
_albumArt.Add(picture);
_hasEmbeddedArtwork = true;
}
}
if (_hasEmbeddedArtwork) return;
bool isValidName = true;
if ((_config.extractAlbumArt || _config.embedAlbumArt) && !_isCD)
@@ -2969,7 +3039,6 @@ namespace CUETools.Processor
if (files.Count > maxChoice) return;
}
bool isSquare = false;
foreach (string imgPath in files)
{
TagLib.File.IFileAbstraction file = _isArchive
@@ -2980,6 +3049,7 @@ namespace CUETools.Processor
pic.Description = imgPath.Substring(_inputDir.Length).Trim(Path.DirectorySeparatorChar);
else
pic.Description = Path.GetFileName(imgPath);
pic.Type = TagLib.PictureType.Other;
using (MemoryStream imageStream = new MemoryStream(pic.Data.Data, 0, pic.Data.Count))
try
{
@@ -2987,25 +3057,16 @@ namespace CUETools.Processor
var image = Image.FromStream(imageStream);
pic.Description += $" ({image.Width}x{image.Height})";
if (image.Height > 0 && image.Width > 0 && (image.Height * 1.0 / image.Width) > 0.9 && (image.Width * 1.0 / image.Height) > 0.9)
isSquare = true;
{
if (isValidName)
pic.Type = TagLib.PictureType.FrontCover;
}
// pic.MimeType = f(image.RawFormat);
#endif
}
catch { }
_albumArt.Add(pic);
}
if (_albumArt.Count == 0 || CUEToolsSelection == null)
return;
if (_albumArt.Count == 1 && isSquare && isValidName)
return;
CUEToolsSelectionEventArgs e = new CUEToolsSelectionEventArgs();
e.choices = _albumArt.ToArray();
CUEToolsSelection(this, e);
TagLib.IPicture selected = e.selection == -1 ? null : _albumArt[e.selection];
_albumArt.RemoveAll(t => t != selected);
}
}
@@ -3014,13 +3075,16 @@ namespace CUETools.Processor
{
if (_albumArt == null)
return;
int j = 0;
foreach (TagLib.IPicture picture in _albumArt)
using (MemoryStream imageStream = new MemoryStream(picture.Data.Data, 0, picture.Data.Count))
try
{
CheckStop();
using (Image img = Image.FromStream(imageStream))
if (img.Width > _config.maxAlbumArtSize || img.Height > _config.maxAlbumArtSize)
{
ReportProgress("Resizing Album Art", (double)(j++) / _albumArt.Count);
using (Bitmap small = resizeImage(img, new Size(_config.maxAlbumArtSize, _config.maxAlbumArtSize)))
using (MemoryStream encoded = new MemoryStream())
{
@@ -3388,6 +3452,9 @@ namespace CUETools.Processor
destBPS = 16;
}
if (_config.embedAlbumArt || _config.CopyAlbumArt)
_albumArt.ForEach(t => _padding += t.Data.Count);
if (style == CUEStyle.SingleFile || style == CUEStyle.SingleFileWithCUE)
{
iDest++;

View File

@@ -198,6 +198,7 @@ namespace JDP {
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
resources.ApplyResources(this.toolStripStatusLabel1, "toolStripStatusLabel1");
this.toolStripStatusLabel1.Spring = true;
//
@@ -208,6 +209,7 @@ namespace JDP {
| System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom)));
this.toolStripStatusLabelProcessed.BorderStyle = System.Windows.Forms.Border3DStyle.SunkenOuter;
this.toolStripStatusLabelProcessed.Name = "toolStripStatusLabelProcessed";
this.toolStripStatusLabelProcessed.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
resources.ApplyResources(this.toolStripStatusLabelProcessed, "toolStripStatusLabelProcessed");
//
// toolStripStatusLabelCTDB
@@ -219,6 +221,7 @@ namespace JDP {
resources.ApplyResources(this.toolStripStatusLabelCTDB, "toolStripStatusLabelCTDB");
this.toolStripStatusLabelCTDB.Image = global::JDP.Properties.Resources.cdrepair1;
this.toolStripStatusLabelCTDB.Name = "toolStripStatusLabelCTDB";
this.toolStripStatusLabelCTDB.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
//
// toolStripStatusLabelAR
//
@@ -229,12 +232,14 @@ namespace JDP {
resources.ApplyResources(this.toolStripStatusLabelAR, "toolStripStatusLabelAR");
this.toolStripStatusLabelAR.Image = global::JDP.Properties.Resources.AR;
this.toolStripStatusLabelAR.Name = "toolStripStatusLabelAR";
this.toolStripStatusLabelAR.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
this.toolStripStatusLabelAR.Padding = new System.Windows.Forms.Padding(0, 0, 5, 0);
//
// toolStripProgressBar2
//
this.toolStripProgressBar2.AutoToolTip = true;
this.toolStripProgressBar2.Name = "toolStripProgressBar2";
this.toolStripProgressBar2.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
resources.ApplyResources(this.toolStripProgressBar2, "toolStripProgressBar2");
this.toolStripProgressBar2.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
//

View File

@@ -973,19 +973,6 @@ namespace JDP
entry.Metadata.CopyMetadata(dlg.ChosenRelease.metadata);
}
}
if (dlgRes != DialogResult.Cancel)
{
if (cueSheet.AlbumArt.Count == 0 && cueSheet.Metadata.AlbumArt.Count > 0)
{
var ms = new MemoryStream();
var image = cueSheet.Metadata.AlbumArt.Find(x => x.primary) ?? cueSheet.Metadata.AlbumArt[0];
if (cueSheet.CTDB.FetchFile(image.uri, ms))
{
var blob = new TagLib.ByteVector(ms.ToArray());
cueSheet.AlbumArt.Add(new TagLib.Picture(new TagLib.ByteVector(blob)));
}
}
}
dlg.Close();
}
else if (_profile._config.advanced.CacheMetadata)
@@ -1006,6 +993,8 @@ namespace JDP
if (dlgRes == DialogResult.Cancel)
return;
cueSheet.LoadAndResizeAlbumArt();
cueSheet.GenerateFilenames(audioEncoderType, outputFormat, pathOut);
List<string> outputExists = cueSheet.OutputExists();
@@ -1040,6 +1029,10 @@ namespace JDP
}
if (outputExists.Count == 0)
{
this.Invoke((MethodInvoker)delegate ()
{
pictureBoxMotd.Image = cueSheet.Cover ?? motdImage;
});
cueSheet.UsePregapForFirstTrackInSingleFile = _usePregapForFirstTrackInSingleFile && !outputAudio;
if (script == null || script.name == "default")
{
@@ -1212,7 +1205,7 @@ namespace JDP
{
if (e.percent == 0)
{
toolStripStatusLabelProcessed.Visible = false;
toolStripStatusLabelProcessed.Text = "";
toolStripProgressBar2.ToolTipText = "";
}
else if (e.percent > 0.02)
@@ -1227,9 +1220,9 @@ namespace JDP
}
toolStripProgressBar2.ToolTipText = String.Format("{0}:{1:00}/{2}:{3:00}", (int)span.TotalMinutes, span.Seconds, (int)eta.TotalMinutes, eta.Seconds);
toolStripStatusLabelProcessed.Text = String.Format("{0}@{1}", toolStripProgressBar2.ToolTipText, speedStr);
toolStripStatusLabelProcessed.Visible = true;
}
toolStripStatusLabel1.Text = e.status.Replace("&", "&&");
toolStripStatusLabel1.ToolTipText = e.status;
toolStripProgressBar2.Value = Math.Max(0, Math.Min(100, (int)(e.percent * 100)));
toolStripStatusLabelAR.Enabled = e.cueSheet != null && e.cueSheet.ArVerify != null && e.cueSheet.ArVerify.ARStatus == null;
@@ -1290,7 +1283,7 @@ namespace JDP
{
UpdateActions();
pictureBoxMotd.Image = motdImage;
toolStripStatusLabelProcessed.Visible = false;
toolStripStatusLabelProcessed.Text = "";
}
//rbGapsLeftOut.Visible =

File diff suppressed because it is too large Load Diff

View File

@@ -186,9 +186,6 @@ namespace JDP
ri.metadata.Tracks[i].ISRC = CUE.Metadata.Tracks[i].ISRC;
}
CUE.CopyMetadata(ri.metadata);
// TODO: copy album art
}
private void AutoResizeList(ListView list, int mainCol)
@@ -260,11 +257,23 @@ namespace JDP
pictureBox1.Image = null;
}
pictureBox1.ImageLocation = null;
if (r.metadata.AlbumArt.Count > 0)
if (r.metadata.AlbumArt.Count > 0 || r.cover != null)
{
pictureBox1.Show();
var image = r.metadata.AlbumArt.Find(x => x.primary) ?? r.metadata.AlbumArt[0];
pictureBox1.ImageLocation = image.uri150;
if (r.cover != null)
{
using (MemoryStream imageStream = new MemoryStream(r.cover, 0, r.cover.Length))
try
{
pictureBox1.Image = Image.FromStream(imageStream);
}
catch { }
}
else
{
var image = r.metadata.AlbumArt.Find(x => x.primary) ?? r.metadata.AlbumArt[0];
pictureBox1.ImageLocation = image.uri150;
}
} else
{
pictureBox1.Hide();