Example #1
0
        /// <summary>
        ///     Helper function for loading a bitmap in a task
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="textureRef"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        private Size _LoadAndEnqueueBitmap(string filePath, CTextureRef textureRef, TTextureType texture)
        {
            Bitmap bmp = CHelper.LoadBitmap(filePath);

            if (bmp == null)
            {
                RemoveTexture(ref textureRef); // Done asynchonously in the function
                lock (_Textures)
                {
                    _TextureCache.Remove(filePath);
                    lock (_BitmapsLoading)
                    {
                        _BitmapsLoading.Remove(filePath);
                    }
                }
                return(new Size(-1, -1));
            }
            Size origSize = bmp.GetSize();

            textureRef.OrigSize = origSize;
            // Update cache, use the same lock as in add/get cache methods
            lock (_Textures)
            {
                STextureCacheEntry cacheEntry;
                if (_TextureCache.TryGetValue(filePath, out cacheEntry))
                {
                    cacheEntry.OrigSize     = origSize;
                    _TextureCache[filePath] = cacheEntry;
                }
                lock (_BitmapsLoading)
                {
                    _BitmapsLoading.Remove(filePath);
                }
            }
            _EnqueueTextureAddOrUpdate(texture, bmp, EQueueAction.Add, false);
            return(origSize);
        }
Example #2
0
        /// <summary>
        ///     Adds a texture and stores it in the VRam
        /// </summary>
        /// <param name="texturePath">The texture's filepath</param>
        /// <returns>A STexture object containing the added texture</returns>
        public CTextureRef AddTexture(string texturePath)
        {
            _EnsureMainThread();
            CTextureRef textureRef;
            Task <Size> loader;

            lock (_TextureCache)
            {
                textureRef = _GetFromCache(texturePath, out loader);
            }
            if (textureRef != null)
            {
                if (loader != null)
                {
                    textureRef.OrigSize = loader.Result;
                }
                Debug.Assert(textureRef.OrigSize.Width > 0);
                return(textureRef);
            }

            Bitmap bmp = CHelper.LoadBitmap(texturePath);

            if (bmp == null)
            {
                return(null);
            }
            try
            {
                textureRef = AddTexture(bmp, texturePath);
            }
            finally
            {
                bmp.Dispose();
            }
            return(textureRef);
        }
Example #3
0
        public bool GetCover(string coverPath, ref CTextureRef tex, int maxSize)
        {
            if (_Connection == null)
            {
                return(false);
            }
            if (!File.Exists(coverPath))
            {
                CLog.LogError("Can't find File: " + coverPath);
                return(false);
            }

            lock (_Mutex)
            {
                //Double check here because we may have just closed our connection
                if (_Connection == null)
                {
                    return(false);
                }
                using (var command = new SQLiteCommand(_Connection))
                {
                    command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path";
                    command.Parameters.Add("@path", DbType.String).Value = coverPath;

                    SQLiteDataReader reader = command.ExecuteReader();

                    if (reader != null && reader.HasRows)
                    {
                        reader.Read();
                        int id = reader.GetInt32(0);
                        int w  = reader.GetInt32(1);
                        int h  = reader.GetInt32(2);
                        reader.Close();

                        command.CommandText = "SELECT Data FROM CoverData WHERE CoverID = @id";
                        command.Parameters.Add("@id", DbType.Int32).Value = id;
                        reader = command.ExecuteReader();

                        if (reader.HasRows)
                        {
                            reader.Read();
                            byte[] data2 = _GetBytes(reader);
                            reader.Dispose();
                            tex = CDraw.EnqueueTexture(w, h, data2);
                            return(true);
                        }
                        command.CommandText = "DELETE FROM Cover WHERE id = @id";
                        command.Parameters.Add("@id", DbType.Int32).Value = id;
                        command.ExecuteNonQuery();
                    }
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }

            // At this point we do not have a mathing entry in the CoverDB (either no Data found and deleted or nothing at all)
            // We break out of the lock to do the bitmap loading and resizing here to allow multithreaded loading

            Bitmap origin = CHelper.LoadBitmap(coverPath);

            if (origin == null)
            {
                return(false);
            }

            Size size = origin.GetSize();

            if (size.Width > maxSize || size.Height > maxSize)
            {
                size = CHelper.FitInBounds(new SRectF(0, 0, maxSize, maxSize, 0), (float)size.Width / size.Height, EAspect.LetterBox).SizeI;
                Bitmap tmp = origin.Resize(size);
                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 (_Mutex)
            {
                //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);
                    }
                }
            }
            return(false);
        }