Ejemplo n.º 1
0
        public static D2D.Bitmap LoadBitmap(D2D.RenderTarget renderTarget, string imagePath)
        {
            FileInfo fi = new FileInfo(imagePath);

            if (!fi.Exists)
            {
                var    ext    = fi.Extension;
                string newExt = "";
                if (ext == ".jpg")
                {
                    newExt = ".png";
                }
                else if (ext == ".png")
                {
                    newExt = ".jpg";
                }
                string newName = fi.FullName.Remove(fi.FullName.Length - 4, 4);
                imagePath = newName + newExt;
            }

            D2D.Bitmap bmp;
            if (Cached.ContainsKey(imagePath))
            {
                bmp = Cached[imagePath];
                if (bmp.IsDisposed)
                {
                    Cached.TryRemove(imagePath, out _);
                }
                else
                {
                    return(bmp);
                }
            }

            WIC.ImagingFactory    imagingFactory = new WIC.ImagingFactory();
            DXIO.NativeFileStream fileStream     = new DXIO.NativeFileStream(imagePath,
                                                                             DXIO.NativeFileMode.Open, DXIO.NativeFileAccess.Read);

            WIC.BitmapDecoder bitmapDecoder =
                new WIC.BitmapDecoder(imagingFactory, fileStream, WIC.DecodeOptions.CacheOnDemand);
            WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

            WIC.FormatConverter converter = new WIC.FormatConverter(imagingFactory);
            converter.Initialize(frame, WIC.PixelFormat.Format32bppPRGBA);

            var bitmapProperties =
                new D2D.BitmapProperties(new D2D.PixelFormat(Format.R8G8B8A8_UNorm, D2D.AlphaMode.Premultiplied));

            //Size2 size = new Size2(frame.Size.Width, frame.Size.Height);

            bmp = D2D.Bitmap.FromWicBitmap(renderTarget, converter, bitmapProperties);

            if (!Cached.ContainsKey(imagePath))
            {
                Cached.TryAdd(imagePath, bmp);
                //LogUtil.LogInfo("Created cache.");
            }

            return(bmp);
        }
Ejemplo n.º 2
0
        public static byte[] LoadImage(string fileName, bool premultiplied, out int width, out int height)
        {
            WIC.ImagingFactory factory = new WIC.ImagingFactory();

            SharpDX.IO.NativeFileStream stream  = new SharpDX.IO.NativeFileStream(fileName, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
            WIC.BitmapDecoder           decoder = new WIC.BitmapDecoder(factory, stream, WIC.DecodeOptions.CacheOnDemand);
            WIC.BitmapFrameDecode       frame   = decoder.GetFrame(0);
            width  = frame.Size.Width;
            height = frame.Size.Height;
            byte[] data = new byte[width * height * 4];
            frame.CopyPixels(data, width * 4);
            return(data);
        }
Ejemplo n.º 3
0
        public static D2D.Bitmap LoadBitmap(this D2D.RenderTarget renderTarget, string imagePath)
        {
            lock (LockObj)
            {
                Sw.Start();
                D2D.Bitmap bmp;
                if (Cached.ContainsKey(imagePath))
                {
                    Cached[imagePath].Time = DateTime.Now;
                    bmp = Cached[imagePath].Bitmap;

                    Sw.Stop();
                    LogUtil.LogInfo($"Cache: {Sw.ElapsedMilliseconds}ms.");
                }
                else
                {
                    WIC.ImagingFactory    imagingFactory = new WIC.ImagingFactory();
                    DXIO.NativeFileStream fileStream     = new DXIO.NativeFileStream(imagePath,
                                                                                     DXIO.NativeFileMode.Open, DXIO.NativeFileAccess.Read);

                    WIC.BitmapDecoder     bitmapDecoder = new WIC.BitmapDecoder(imagingFactory, fileStream, WIC.DecodeOptions.CacheOnDemand);
                    WIC.BitmapFrameDecode frame         = bitmapDecoder.GetFrame(0);

                    WIC.FormatConverter converter = new WIC.FormatConverter(imagingFactory);
                    converter.Initialize(frame, WIC.PixelFormat.Format32bppPRGBA);

                    var bitmapProperties =
                        new D2D.BitmapProperties(new D2D.PixelFormat(Format.R8G8B8A8_UNorm, D2D.AlphaMode.Premultiplied));
                    //Size2 size = new Size2(frame.Size.Width, frame.Size.Height);

                    bmp = D2D.Bitmap.FromWicBitmap(renderTarget, converter, bitmapProperties);

                    Sw.Stop();
                    LogUtil.LogInfo($"Load: {Sw.ElapsedMilliseconds}ms.");
                }

                if (!Cached.ContainsKey(imagePath) && Sw.ElapsedMilliseconds > 50)
                {
                    Cached.TryAdd(imagePath, new CacheInfo(DateTime.Now, bmp));
                    LogUtil.LogInfo("Created cache.");
                    if (Cached.Count > 50)
                    {
                        Cached.TryRemove(Cached.OrderByDescending(c => c.Value.Time).First().Key, out _);
                        LogUtil.LogInfo("Removed unused cache.");
                    }
                }
                Sw.Reset();

                return(bmp);
            }
        }
Ejemplo n.º 4
0
        public static D2D.Bitmap LoadFromFile(D2D.RenderTarget renderTarget, string filePath)
        {
            WIC.ImagingFactory    imagingFactory = new WIC.ImagingFactory();
            DXIO.NativeFileStream fileStream     = new DXIO.NativeFileStream(filePath,
                                                                             DXIO.NativeFileMode.Open, DXIO.NativeFileAccess.Read);

            WIC.BitmapDecoder     bitmapDecoder = new WIC.BitmapDecoder(imagingFactory, fileStream, WIC.DecodeOptions.CacheOnDemand);
            WIC.BitmapFrameDecode frame         = bitmapDecoder.GetFrame(0);

            WIC.FormatConverter converter = new WIC.FormatConverter(imagingFactory);
            converter.Initialize(frame, WIC.PixelFormat.Format32bppPRGBA);

            return(D2D.Bitmap.FromWicBitmap(RenderForm.RenderTarget, converter));
        }
Ejemplo n.º 5
0
        private static Direct2D1.Bitmap1 CreateD2dBitmap(
            WIC.ImagingFactory imagingFactory,
            string filename,
            Direct2D1.DeviceContext renderTarget)
        {
            var decoder = new WIC.BitmapDecoder(imagingFactory, filename, WIC.DecodeOptions.CacheOnLoad);

            WIC.BitmapFrameDecode frame = decoder.GetFrame(0);

            var image = new WIC.FormatConverter(imagingFactory);

            image.Initialize(frame, WIC.PixelFormat.Format32bppPBGRA);
            return(Direct2D1.Bitmap1.FromWicBitmap(renderTarget, image));
        }
Ejemplo n.º 6
0
        private WIC.BitmapTransformOptions GetExifRotation(WIC.BitmapFrameDecode frame)
        {
            try
            {
                using (var mdr = frame.MetadataQueryReader)
                {
                    if (mdr == null || !mdr.TryGetMetadataByName("/app1/{ushort=0}/{ushort=274}", out var vv).Success)
                    {
                        return(0);
                    }
                    switch ((ushort)vv)
                    {
                    case 0: return(WIC.BitmapTransformOptions.Rotate0);

                    case 1: return(WIC.BitmapTransformOptions.Rotate0);

                    case 2: return(WIC.BitmapTransformOptions.Rotate0 | WIC.BitmapTransformOptions.FlipHorizontal);

                    case 3: return(WIC.BitmapTransformOptions.Rotate180);

                    case 4: return(WIC.BitmapTransformOptions.Rotate180 | WIC.BitmapTransformOptions.FlipVertical);

                    case 5: return(WIC.BitmapTransformOptions.Rotate270 | WIC.BitmapTransformOptions.FlipHorizontal);

                    case 6: return(WIC.BitmapTransformOptions.Rotate90);

                    case 7: return(WIC.BitmapTransformOptions.Rotate90 | WIC.BitmapTransformOptions.FlipHorizontal);

                    case 8: return(WIC.BitmapTransformOptions.Rotate270);

                    default: return(0);
                    }
                }
            }
            catch

            {
                return(0);
            }
        }
        public static d2.Bitmap LoadBitmap(this d2.RenderTarget renderTarget, Stream stream)
        {
            var bitmapDecoder = new wic.BitmapDecoder(DXGraphicsService.FactoryImaging, stream,
                                                      wic.DecodeOptions.CacheOnDemand);

            wic.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
            var bitmapSource = new wic.BitmapSource(bitmapFrameDecode.NativePointer);

            var formatConverter = new wic.FormatConverter(DXGraphicsService.FactoryImaging);

            formatConverter.Initialize(bitmapSource, wic.PixelFormat.Format32bppPBGRA);

            d2.Bitmap bitmap = d2.Bitmap.FromWicBitmap(renderTarget, formatConverter);

            formatConverter.Dispose();
            /* todo: check to see if I need to register to dispose of this later...  Can't comment this out because server side rendering will crash */
            //bitmapSource.Dispose();
            bitmapFrameDecode.Dispose();
            bitmapDecoder.Dispose();

            return(bitmap);
        }
Ejemplo n.º 8
0
        //-------------------------------------------------------------------------------------
        // Decodes a single frame
        //-------------------------------------------------------------------------------------
        private static Image DecodeSingleFrame(WICFlags flags, ImageDescription metadata, Guid convertGUID, WIC.BitmapFrameDecode frame)
        {
            var image = Image.New(metadata);

            var pixelBuffer = image.PixelBuffer[0];

            if (convertGUID == Guid.Empty)
            {
                frame.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
            }
            else
            {
                using (var converter = new WIC.FormatConverter(Factory))
                {
                    converter.Initialize(frame, convertGUID, GetWICDither(flags), null, 0, WIC.BitmapPaletteType.Custom);
                    converter.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                }
            }

            return(image);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Determines metadata for image
        /// </summary>
        /// <param name="flags">The flags.</param>
        /// <param name="decoder">The decoder.</param>
        /// <param name="frame">The frame.</param>
        /// <param name="pixelFormat">The pixel format.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">If pixel format is not supported.</exception>
        private static ImageDescription?DecodeMetadata(WICFlags flags, WIC.BitmapDecoder decoder, WIC.BitmapFrameDecode frame, out Guid pixelFormat)
        {
            var size = frame.Size;

            var metadata = new ImageDescription
            {
                Dimension = TextureDimension.Texture2D,
                Width     = size.Width,
                Height    = size.Height,
                Depth     = 1,
                MipLevels = 1,
                ArraySize = (flags & WICFlags.AllFrames) != 0 ? decoder.FrameCount : 1,
                Format    = DetermineFormat(frame.PixelFormat, flags, out pixelFormat)
            };

            if (metadata.Format == DXGI.Format.Unknown)
            {
                return(null);
            }

            return(metadata);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FastMetadataEncoder"/> class from a <see cref="BitmapFrameDecode"/>
 /// </summary>
 /// <param name="factory">The factory.</param>
 /// <param name="frameDecoder">The frame decoder.</param>
 public FastMetadataEncoder(ImagingFactory factory, BitmapFrameDecode frameDecoder)
     : base(IntPtr.Zero)
 {
     factory.CreateFastMetadataEncoderFromFrameDecode(frameDecoder, this);
 }
Ejemplo n.º 11
0
        //-------------------------------------------------------------------------------------
        // Decodes a single frame
        //-------------------------------------------------------------------------------------
        private static Image DecodeSingleFrame(WICFlags flags, ImageDescription metadata, Guid convertGUID, BitmapFrameDecode frame)
        {
            var image = Image.New(metadata);

            var pixelBuffer = image.PixelBuffer[0];

            if (convertGUID == Guid.Empty)
            {
                frame.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
            }
            else
            {
                using (var converter = new FormatConverter(Factory))
                {
                    converter.Initialize(frame, convertGUID, GetWICDither(flags), null, 0, BitmapPaletteType.Custom);
                    converter.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                }
            }

            return image;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Determines metadata for image
        /// </summary>
        /// <param name="flags">The flags.</param>
        /// <param name="decoder">The decoder.</param>
        /// <param name="frame">The frame.</param>
        /// <param name="pixelFormat">The pixel format.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">If pixel format is not supported.</exception>
        private static ImageDescription? DecodeMetadata(WICFlags flags, BitmapDecoder decoder, BitmapFrameDecode frame, out Guid pixelFormat)
        {
            var size = frame.Size;

            var metadata = new ImageDescription
                               {
                                   Dimension = TextureDimension.Texture2D,
                                   Width = size.Width,
                                   Height = size.Height,
                                   Depth = 1,
                                   MipLevels = 1,
                                   ArraySize = (flags & WICFlags.AllFrames) != 0 ? decoder.FrameCount : 1,
                                   Format = DetermineFormat(frame.PixelFormat, flags, out pixelFormat)
                               };

            if (metadata.Format == DXGI.Format.Unknown)
                return null;

            return metadata;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FastMetadataEncoder"/> class from a <see cref="BitmapFrameDecode"/>
 /// </summary>
 /// <param name="factory">The factory.</param>
 /// <param name="frameDecoder">The frame decoder.</param>
 public FastMetadataEncoder(ImagingFactory factory, BitmapFrameDecode frameDecoder)
     : base(IntPtr.Zero)
 {
     factory.CreateFastMetadataEncoderFromFrameDecode(frameDecoder, this);
 }
Ejemplo n.º 14
0
        private static Texture DecodeSingleframe(ImagingFactory imagingFactory, WicFlags flags, TextureDescription description, Guid convertGuid, BitmapFrameDecode frame)
        {
            var texture = new Texture(description);
              var image = texture.Images[0];

              if (convertGuid == Guid.Empty)
              {
            frame.CopyPixels(image.Data, image.RowPitch);
              }
              else
              {
            using (var converter = new FormatConverter(imagingFactory))
            {
              converter.Initialize(frame, convertGuid, GetWicDither(flags), null, 0, BitmapPaletteType.Custom);
              converter.CopyPixels(image.Data, image.RowPitch);
            }
              }

              return texture;
        }
Ejemplo n.º 15
0
        private static TextureDescription DecodeMetadata(ImagingFactory imagingFactory, WicFlags flags, BitmapDecoder decoder, BitmapFrameDecode frame, out Guid pixelFormat)
        {
            var size = frame.Size;

              var description = new TextureDescription
              {
            Dimension = TextureDimension.Texture2D,
            Width = size.Width,
            Height = size.Height,
            Depth = 1,
            MipLevels = 1,
            ArraySize = (flags & WicFlags.AllFrames) != 0 ? decoder.FrameCount : 1,
            Format = DetermineFormat(imagingFactory, frame.PixelFormat, flags, out pixelFormat)
              };

              if (description.Format == DataFormat.Unknown)
            throw new NotSupportedException("The pixel format is not supported.");

              if ((flags & WicFlags.IgnoreSrgb) == 0)
              {
            // Handle sRGB.
            #pragma warning disable 168
            try
            {
              Guid containerFormat = decoder.ContainerFormat;
              var metareader = frame.MetadataQueryReader;
              if (metareader != null)
              {
            // Check for sRGB color space metadata.
            bool sRgb = false;

            if (containerFormat == ContainerFormatGuids.Png)
            {
              // Check for sRGB chunk.
              if (metareader.GetMetadataByName("/sRGB/RenderingIntent") != null)
                sRgb = true;
            }
            else if (containerFormat == ContainerFormatGuids.Jpeg)
            {
              if (Equals(metareader.GetMetadataByName("/app1/ifd/exif/{ushort=40961}"), 1))
                sRgb = true;
            }
            else if (containerFormat == ContainerFormatGuids.Tiff)
            {
              if (Equals(metareader.GetMetadataByName("/ifd/exif/{ushort=40961}"), 1))
                sRgb = true;
            }
            else
            {
              if (Equals(metareader.GetMetadataByName("System.Image.ColorSpace"), 1))
                sRgb = true;
            }

            if (sRgb)
              description.Format = TextureHelper.MakeSRgb(description.Format);
              }
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception exception)
            {
              // Some formats just don't support metadata (BMP, ICO, etc.).
            }
              }
            #pragma warning restore 168

              return description;
        }