示例#1
0
        /// <summary>
        /// WIC <see cref="SharpDX.WIC.BitmapSource"/> から <see cref="SharpDX.Direct3D11.Texture2D"/> を作成する
        /// </summary>
        /// <param name="app"></param>
        /// <param name="bitmapSource">The WIC bitmap source</param>
        /// <param name="name">任意の名前</param>
        /// <returns>A Texture2D</returns>
        protected static MCTexture CreateTexture2DFromBitmap(Application app, SharpDX.WIC.BitmapSource bitmapSource, string name)
        {
            // WICイメージピクセルを受け取るためにDataStreamを割り当てます。
            int       stride = bitmapSource.Size.Width * 4;
            MCTexture tx     = new MCTexture(app, name);

            using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true))
            {
                // WICの内容をバッファにコピーする
                bitmapSource.CopyPixels(stride, buffer);
                Texture123DDesc d = new Texture123DDesc();
                d.D2 = new Texture2DDescription()
                {
                    Width             = bitmapSource.Size.Width,
                    Height            = bitmapSource.Size.Height,
                    ArraySize         = 1,
                    BindFlags         = SharpDX.Direct3D11.BindFlags.ShaderResource,
                    Usage             = SharpDX.Direct3D11.ResourceUsage.Immutable,
                    CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None,
                    Format            = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    MipLevels         = 1,
                    OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.None,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                };
                if (tx.CreateTexture2D(0, d, new DataRectangle(buffer.DataPointer, stride)) == 1)
                {
                    return(null);
                }
            }
            return(tx);
        }
        public static Texture2D CreateTex2DFromBitmap(SharpDX.WIC.BitmapSource bsource, SharpDX.Direct3D11.Device device)
        {
            Texture2DDescription desc;

            desc.Width                     = bsource.Size.Width;
            desc.Height                    = bsource.Size.Height;
            desc.ArraySize                 = 1;
            desc.BindFlags                 = BindFlags.ShaderResource;
            desc.Usage                     = ResourceUsage.Default;
            desc.CpuAccessFlags            = CpuAccessFlags.None;
            desc.Format                    = SharpDX.DXGI.Format.R8G8B8A8_UNorm;
            desc.MipLevels                 = 1;
            desc.OptionFlags               = ResourceOptionFlags.None;
            desc.SampleDescription.Count   = 1;
            desc.SampleDescription.Quality = 0;

            using (DataStream s = new DataStream(bsource.Size.Height * bsource.Size.Width * 4, true, true))
            {
                bsource.CopyPixels(bsource.Size.Width * 4, s);
                DataRectangle rect = new DataRectangle(s.DataPointer, bsource.Size.Width * 4);
                return(new Texture2D(device, desc, rect));
            }
        }
示例#3
0
        /// <summary>
        /// Creates a <see cref="SharpDX.Direct3D11.Texture2D"/> from a WIC <see cref="SharpDX.WIC.BitmapSource"/>
        /// </summary>
        /// <param name="device">The Direct3D11 device</param>
        /// <param name="bitmapSource">The WIC bitmap source</param>
        /// <returns>A Texture2D</returns>
        public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(Device device, SharpDX.WIC.BitmapSource bitmapSource)
        {
            // Allocate DataStream to receive the WIC image pixels
            int stride = bitmapSource.Size.Width * 4;

            using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true))
            {
                // Copy the content of the WIC to the buffer
                bitmapSource.CopyPixels(stride, buffer);
                return(new Texture2D(device, new Texture2DDescription()
                {
                    Width = bitmapSource.Size.Width,
                    Height = bitmapSource.Size.Height,
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource,
                    Usage = ResourceUsage.Immutable,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    MipLevels = 1,
                    OptionFlags = ResourceOptionFlags.None,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                }, new SharpDX.DataRectangle(buffer.DataPointer, stride)));
            }
        }
示例#4
0
        /// <summary>
        /// Function to perform conversion/transformation of a bitmap.
        /// </summary>
        /// <param name="sourceData">WIC bitmap to transform.</param>
        /// <param name="destData">Destination data buffer.</param>
        /// <param name="rowPitch">Number of bytes per row in the image.</param>
        /// <param name="slicePitch">Number of bytes in total for the image.</param>
        /// <param name="destFormat">Destination format for transformation.</param>
        /// <param name="isSourcesRGB">TRUE if the source format is sRGB.</param>
        /// <param name="isDestsRGB">TRUE if the destination format is sRGB.</param>
        /// <param name="dither">Dithering to apply to images that get converted to a lower bit depth.</param>
        /// <param name="destRect">Rectangle containing the area to scale or clip</param>
        /// <param name="clip">TRUE to perform clipping instead of scaling.</param>
        /// <param name="scaleFilter">Filter to apply to scaled data.</param>
        public void TransformImageData(WIC.BitmapSource sourceData, IntPtr destData, int rowPitch, int slicePitch, Guid destFormat, bool isSourcesRGB, bool isDestsRGB, ImageDithering dither, Rectangle destRect, bool clip, ImageFilter scaleFilter)
        {
            WIC.BitmapSource    source        = sourceData;
            WIC.FormatConverter converter     = null;
            WIC.BitmapClipper   clipper       = null;
            WIC.BitmapScaler    scaler        = null;
            WIC.ColorContext    sourceContext = null;
            WIC.ColorContext    destContext   = null;
            WIC.ColorTransform  sRGBTransform = null;

            try
            {
                if (destFormat != Guid.Empty)
                {
                    if (sourceData.PixelFormat != destFormat)
                    {
                        converter = new WIC.FormatConverter(Factory);

                        if (!converter.CanConvert(sourceData.PixelFormat, destFormat))
                        {
                            throw new GorgonException(GorgonResult.FormatNotSupported,
                                                      string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, destFormat));
                        }

                        converter.Initialize(source, destFormat, (WIC.BitmapDitherType)dither, null, 0, WIC.BitmapPaletteType.Custom);
                        source = converter;
                    }

                    if ((isDestsRGB) ||
                        (isSourcesRGB))
                    {
                        sRGBTransform = new WIC.ColorTransform(Factory);
                        sourceContext = new WIC.ColorContext(Factory);
                        destContext   = new WIC.ColorContext(Factory);

                        sourceContext.InitializeFromExifColorSpace(isSourcesRGB ? 1 : 2);
                        destContext.InitializeFromExifColorSpace(isDestsRGB ? 1 : 2);

                        sRGBTransform.Initialize(source, sourceContext, destContext, destFormat);
                        source = sRGBTransform;
                    }
                }

                if (destRect.IsEmpty)
                {
                    source.CopyPixels(rowPitch, destData, slicePitch);
                    return;
                }

                Guid pixelFormat = source.PixelFormat;

                if (!clip)
                {
                    scaler = new WIC.BitmapScaler(Factory);
                    scaler.Initialize(source,
                                      destRect.Width,
                                      destRect.Height,
                                      (WIC.BitmapInterpolationMode)scaleFilter);
                    source = scaler;
                }
                else
                {
                    destRect.Width  = destRect.Width.Min(source.Size.Width);
                    destRect.Height = destRect.Height.Min(source.Size.Height);

                    if ((destRect.Width < source.Size.Width) ||
                        (destRect.Height < source.Size.Height))
                    {
                        clipper = new WIC.BitmapClipper(Factory);
                        clipper.Initialize(source,
                                           new DX.Rectangle(destRect.X, destRect.Y, destRect.Width, destRect.Height));
                        source     = clipper;
                        destRect.X = 0;
                        destRect.Y = 0;
                    }
                }

                // We have a change of format (probably due to the filter when scaling)... so we need to convert.
                if (source.PixelFormat != pixelFormat)
                {
                    converter = new WIC.FormatConverter(Factory);

                    if (!converter.CanConvert(source.PixelFormat, pixelFormat))
                    {
                        throw new GorgonException(GorgonResult.FormatNotSupported,
                                                  string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, pixelFormat));
                    }

                    converter.Initialize(source,
                                         pixelFormat,
                                         WIC.BitmapDitherType.None,
                                         null,
                                         0,
                                         WIC.BitmapPaletteType.Custom);
                    source = converter;
                }

                source.CopyPixels(rowPitch, destData, slicePitch);
            }
            finally
            {
                if (converter != null)
                {
                    converter.Dispose();
                }

                if (sourceContext != null)
                {
                    sourceContext.Dispose();
                }

                if (destContext != null)
                {
                    destContext.Dispose();
                }

                if (sRGBTransform != null)
                {
                    sRGBTransform.Dispose();
                }

                if (scaler != null)
                {
                    scaler.Dispose();
                }

                if (clipper != null)
                {
                    clipper.Dispose();
                }
            }
        }