Пример #1
0
 public Bitmap LoadImage(string filename)
 {
     var f = File.ReadAllBytes(filename);
     {
         return(d.DecodeFromBytes(f, f.Length));
     }
 }
Пример #2
0
        private void ConvertWebpToPng(string name, bool isDeleteInputFile = true)
        {
            var stream  = File.Open($"{DownloadLocation}{name}.webp", FileMode.Open);
            var decoder = new SimpleDecoder();
            var bytes   = new byte[stream.Length];

            stream.Read(bytes, 0, bytes.Length);
            if (bytes.Length == 0)
            {
                stream.Dispose();
                File.Delete($"{DownloadLocation}{name}.webp");
                OnFailedDonwload?.Invoke();
                return;
            }
            var bmp = decoder.DecodeFromBytes(bytes, bytes.Length);

            bmp.Save($"{DownloadLocation}{name}.png", ImageFormat.Png);
            bmp.Dispose();
            stream.Dispose();

            if (isDeleteInputFile)
            {
                File.Delete($"{DownloadLocation}{name}.webp");
            }
        }
        private void TestLosslessRoundtrip(Bitmap gdiBitmap)
        {
            var encoder = new SimpleEncoder();
            var decoder = new SimpleDecoder();

            using (var outStream = new System.IO.MemoryStream())
            {
                encoder.Encode(gdiBitmap, outStream, -1);
                outStream.Close();

                var webpBytes = outStream.ToArray();
                var reloaded  = decoder.DecodeFromBytes(webpBytes, webpBytes.LongLength);

                Assert.Equal(gdiBitmap.Height, reloaded.Height);
                Assert.Equal(gdiBitmap.Width, reloaded.Width);

                for (var y = 0; y < reloaded.Height; y++)
                {
                    for (var x = 0; x < reloaded.Width; x++)
                    {
                        var expectedColor = gdiBitmap.GetPixel(x, y);
                        var actualColor   = reloaded.GetPixel(x, y);
                        Assert.Equal(expectedColor, actualColor);
                    }
                }
            }
        }
        public async Task <Texture2D> GetCoverImageTexture2DAsync(CancellationToken cancellationToken)
        {
            // Logger.Debug($"GetCoverImageTextre2DAsync() : {Source.Title}");
            try
            {
                var cachePath = Path.Combine(CoverFolder, $"{Source.Hash}.jpg");
                // キャッシュ利用
                if (File.Exists(cachePath))
                {
                    return(await Utils.GetTextureFromFileAsync(cachePath, cancellationToken));
                }

                if (!IsDownloadingCover)
                {
                    IsDownloadingCover = true;

                    var context = new DownloadContext($"{CoverUrlBase}{Source.Hash}.webp")
                    {
                        OnSuccess = (status, data) =>
                        {
                            // WebPはUnityで使えないので変換する
                            var decoder = new SimpleDecoder();
                            var bitmap  = decoder.DecodeFromBytes(data, data.Length);

                            // キャッシュ作成
                            bitmap.Save(cachePath, ImageFormat.Jpeg);

                            /*
                             * // Texture2Dを作成
                             * using (var stream = new MemoryStream())
                             * {
                             *  bitmap.Save(stream, bitmap.RawFormat);
                             *  var texture = new Texture2D(2, 2);
                             *  texture.LoadImage(stream.ToArray());
                             * }
                             */
                        },
                        OnError      = (_, __) => IsDownloadingCover = false,
                        OnCancelling = () => IsDownloadingCover = false,
                    };

                    WebRequestClient.Instance.EnqueueRequest(context);
                }

                return(await Utils.GetTextureFromResourceAsync(DefaultCoverResourceName, cancellationToken));
            }
            catch (Exception ex)
            {
                Logger.Debug("in GetCoverImageTexture2DAsync");
                Logger.Debug(ex);

                return(null);
            }
        }
Пример #5
0
        /// <summary>
        /// Декодируем WebP изображение
        /// </summary>
        /// <param name="fi">Информация о декодируемом файле</param>
        private void Decode(FileInfo fi)
        {
            //Инициализируем декодер
            SimpleDecoder decoder = new SimpleDecoder();

            //Считываем байты файла
            byte[] bytes = File.ReadAllBytes(fi.FullName);
            //Декодируем изображение
            using (Bitmap image = decoder.DecodeFromBytes(bytes, bytes.Length))
                //Сохраняем файл
                image.Save($"{savePath}{GenerateFileName(fi)}");
        }
Пример #6
0
        public void TestDecSimple()
        {
            var decoder  = new SimpleDecoder();
            var fileName = "testimage.webp";
            var outFile  = "testimageout.jpg";

            File.Delete(outFile);
            FileStream outStream = new FileStream(outFile, FileMode.Create);

            using (Stream inputStream = System.IO.File.Open(fileName, System.IO.FileMode.Open))
            {
                var bytes     = ReadFully(inputStream);
                var outBitmap = decoder.DecodeFromBytes(bytes, bytes.LongLength);
                outBitmap.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                outStream.Close();
            }

            FileInfo finfo = new FileInfo(outFile);

            Assert.True(finfo.Exists);
        }
Пример #7
0
        /// <summary>
        /// Read non-animated WEBP format
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Bitmap ReadWebpFile(string path)
        {
            var webpDecoder = new SimpleDecoder();

            using (Stream inputStream = File.Open(path, FileMode.Open))
            {
                byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read;
                    while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }

                    var    bytes     = ms.ToArray();
                    Bitmap outBitmap = webpDecoder.DecodeFromBytes(bytes, bytes.LongLength);

                    return(outBitmap);
                }
            }
        }