Use Resize function in CoverLoader
Add possible multithreading
This commit is contained in:
1 parent
879176a87f
commit
bf7eec4233
2 files changed
+97
-70
No files matched your search
@@ -82,7 +82,7 @@ namespace Vocaluxe.Lib.Database
|
|||||||
using (var command = new SQLiteCommand(_Connection))
|
using (var command = new SQLiteCommand(_Connection))
|
||||||
{
|
{
|
||||||
command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path";
|
command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path";
|
||||||
command.Parameters.Add("@path", DbType.String, 0).Value = coverPath;
|
command.Parameters.Add("@path", DbType.String).Value = coverPath;
|
||||||
|
|
||||||
SQLiteDataReader reader = command.ExecuteReader();
|
SQLiteDataReader reader = command.ExecuteReader();
|
||||||
|
|
||||||
@@ -101,81 +101,81 @@ namespace Vocaluxe.Lib.Database
|
|||||||
if (reader.HasRows)
|
if (reader.HasRows)
|
||||||
{
|
{
|
||||||
reader.Read();
|
reader.Read();
|
||||||
byte[] data = _GetBytes(reader);
|
byte[] data2 = _GetBytes(reader);
|
||||||
reader.Dispose();
|
reader.Dispose();
|
||||||
tex = CDraw.EnqueueTexture(w, h, data);
|
tex = CDraw.EnqueueTexture(w, h, data2);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
command.CommandText = "DELETE FROM Cover WHERE id = @id";
|
||||||
else
|
command.Parameters.Add("@id", DbType.Int32).Value = id;
|
||||||
{
|
|
||||||
if (reader != null)
|
|
||||||
reader.Close();
|
|
||||||
|
|
||||||
if (_TransactionCover == null)
|
|
||||||
_TransactionCover = _Connection.BeginTransaction();
|
|
||||||
|
|
||||||
Bitmap origin;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
origin = new Bitmap(coverPath);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
CLog.LogError("Error loading Texture: " + coverPath);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int w = maxSize;
|
|
||||||
int h = maxSize;
|
|
||||||
byte[] data;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (origin.Width >= origin.Height && origin.Width > w)
|
|
||||||
h = (int)Math.Round((float)w / origin.Width * origin.Height);
|
|
||||||
else if (origin.Height > origin.Width && origin.Height > h)
|
|
||||||
w = (int)Math.Round((float)h / origin.Height * origin.Width);
|
|
||||||
|
|
||||||
using (var bmp = new Bitmap(w, h))
|
|
||||||
{
|
|
||||||
using (Graphics g = Graphics.FromImage(bmp))
|
|
||||||
g.DrawImage(origin, new Rectangle(0, 0, w, h));
|
|
||||||
|
|
||||||
data = new byte[w * h * 4];
|
|
||||||
BitmapData bmpData = bmp.LockBits(bmp.GetRect(), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
||||||
Marshal.Copy(bmpData.Scan0, data, 0, w * h * 4);
|
|
||||||
bmp.UnlockBits(bmpData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
origin.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
tex = CDraw.EnqueueTexture(w, h, data);
|
|
||||||
|
|
||||||
command.CommandText = "INSERT INTO Cover (Path, width, height) VALUES (@path, @w, @h)";
|
|
||||||
command.Parameters.Add("@w", DbType.Int32).Value = w;
|
|
||||||
command.Parameters.Add("@h", DbType.Int32).Value = h;
|
|
||||||
command.Parameters.Add("@path", DbType.String, 0).Value = coverPath;
|
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
if (reader != null)
|
||||||
|
reader.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
command.CommandText = "SELECT id FROM Cover WHERE [Path] = @path";
|
// At this point we do not have a mathing entry in the CoverDB (either no Data found and deleted or nothing at all)
|
||||||
command.Parameters.Add("@path", DbType.String, 0).Value = coverPath;
|
// We break out of the lock to do the bitmap loading and resizing here to allow multithreaded loading
|
||||||
reader = command.ExecuteReader();
|
|
||||||
|
|
||||||
if (reader != null)
|
Bitmap origin = CHelper.LoadBitmap(coverPath);
|
||||||
{
|
if (origin == null)
|
||||||
reader.Read();
|
return false;
|
||||||
int id = reader.GetInt32(0);
|
|
||||||
reader.Dispose();
|
Size size = origin.GetSize();
|
||||||
command.CommandText = "INSERT INTO CoverData (CoverID, Data) VALUES (@id, @data)";
|
if (size.Width > maxSize || size.Height > maxSize)
|
||||||
command.Parameters.Add("@id", DbType.Int32).Value = id;
|
{
|
||||||
command.Parameters.Add("@data", DbType.Binary, 20).Value = data;
|
size = CHelper.FitInBounds(new SRectF(0, 0, maxSize, maxSize, 0), (float)size.Width / size.Height, EAspect.LetterBox).SizeI;
|
||||||
command.ExecuteReader();
|
Bitmap tmp = origin.Resize(size);
|
||||||
return true;
|
origin.Dispose();
|
||||||
}
|
origin = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] data;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
data = new byte[size.Width * size.Height * 4];
|
||||||
|
BitmapData bmpData = origin.LockBits(origin.GetRect(), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
||||||
|
Marshal.Copy(bmpData.Scan0, data, 0, data.Length);
|
||||||
|
origin.UnlockBits(bmpData);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
origin.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
tex = CDraw.EnqueueTexture(size.Width, size.Height, data);
|
||||||
|
|
||||||
|
lock (_CoverMutex)
|
||||||
|
{
|
||||||
|
//Double check here because we may have just closed our connection
|
||||||
|
if (_Connection == null)
|
||||||
|
return false;
|
||||||
|
if (_TransactionCover == null)
|
||||||
|
_TransactionCover = _Connection.BeginTransaction();
|
||||||
|
using (var command = new SQLiteCommand(_Connection))
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO Cover (Path, width, height) VALUES (@path, @w, @h)";
|
||||||
|
command.Parameters.Add("@w", DbType.Int32).Value = size.Width;
|
||||||
|
command.Parameters.Add("@h", DbType.Int32).Value = size.Height;
|
||||||
|
command.Parameters.Add("@path", DbType.String).Value = coverPath;
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
command.CommandText = "SELECT id FROM Cover WHERE [Path] = @path";
|
||||||
|
command.Parameters.Add("@path", DbType.String).Value = coverPath;
|
||||||
|
SQLiteDataReader reader = command.ExecuteReader();
|
||||||
|
|
||||||
|
if (reader != null)
|
||||||
|
{
|
||||||
|
reader.Read();
|
||||||
|
int id = reader.GetInt32(0);
|
||||||
|
reader.Dispose();
|
||||||
|
command.CommandText = "INSERT INTO CoverData (CoverID, Data) VALUES (@id, @data)";
|
||||||
|
command.Parameters.Add("@id", DbType.Int32).Value = id;
|
||||||
|
command.Parameters.Add("@data", DbType.Binary).Value = data;
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,23 @@ namespace VocaluxeLib
|
|||||||
public float Z;
|
public float Z;
|
||||||
public float Rotation; //0..360°
|
public float Rotation; //0..360°
|
||||||
|
|
||||||
|
public float Right
|
||||||
|
{
|
||||||
|
get { return X + W; }
|
||||||
|
}
|
||||||
|
public float Bottom
|
||||||
|
{
|
||||||
|
get { return Y + H; }
|
||||||
|
}
|
||||||
|
public Size SizeI
|
||||||
|
{
|
||||||
|
get { return new Size((int)W, (int)H); }
|
||||||
|
}
|
||||||
|
public SizeF Size
|
||||||
|
{
|
||||||
|
get { return new SizeF(W, H); }
|
||||||
|
}
|
||||||
|
|
||||||
public SRectF(float x, float y, float w, float h, float z)
|
public SRectF(float x, float y, float w, float h, float z)
|
||||||
{
|
{
|
||||||
X = x;
|
X = x;
|
||||||
@@ -85,6 +102,16 @@ namespace VocaluxeLib
|
|||||||
Z = rect.Z;
|
Z = rect.Z;
|
||||||
Rotation = 0f;
|
Rotation = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SRectF(Rectangle rect)
|
||||||
|
{
|
||||||
|
X = rect.X;
|
||||||
|
Y = rect.Y;
|
||||||
|
W = rect.Width;
|
||||||
|
H = rect.Height;
|
||||||
|
Z = 0;
|
||||||
|
Rotation = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct SPoint3F
|
public struct SPoint3F
|
||||||
|
|||||||
Reference in new issue
Block a user