예제 #1
0
        /// <summary>
        /// Function to read the data from a frame.
        /// </summary>
        /// <param name="wic">WIC interface.</param>
        /// <param name="data">Image data to populate.</param>
        /// <param name="srcFormat">Source image format.</param>
        /// <param name="convertFormat">Conversion format.</param>
        /// <param name="frame">Frame containing the image data.</param>
        private void ReadFrame(GorgonWICImage wic, GorgonImageData data, Guid srcFormat, Guid convertFormat, BitmapFrameDecode frame)
        {
            var buffer = data.Buffers[0];

            // We don't need to convert, so just leave.
            if ((convertFormat == Guid.Empty) || (srcFormat == convertFormat))
            {
                frame.CopyPixels(buffer.PitchInformation.RowPitch, buffer.Data.BasePointer, buffer.PitchInformation.SlicePitch);
                return;
            }

            // Perform conversion.
            using (var converter = new FormatConverter(wic.Factory))
            {
                bool isIndexed = ((frame.PixelFormat == PixelFormat.Format8bppIndexed) ||
                                  (frame.PixelFormat == PixelFormat.Format4bppIndexed) ||
                                  (frame.PixelFormat == PixelFormat.Format2bppIndexed) ||
                                  (frame.PixelFormat == PixelFormat.Format1bppIndexed));
                Tuple <Palette, double, BitmapPaletteType> paletteInfo = null;

                try
                {
                    // If the pixel format is indexed, then retrieve a palette.
                    if (isIndexed)
                    {
                        paletteInfo = GetPaletteInfo(wic, null);
                    }

                    // If we've defined a palette for an indexed image, then copy it to a bitmap and set its palette.
                    if ((paletteInfo != null) && (paletteInfo.Item1 != null))
                    {
                        using (var tempBitmap = new Bitmap(wic.Factory, frame, BitmapCreateCacheOption.CacheOnDemand))
                        {
                            tempBitmap.Palette = paletteInfo.Item1;
                            converter.Initialize(tempBitmap, convertFormat, (BitmapDitherType)Dithering, paletteInfo.Item1, paletteInfo.Item2, paletteInfo.Item3);
                            converter.CopyPixels(buffer.PitchInformation.RowPitch, buffer.Data.BasePointer, buffer.PitchInformation.SlicePitch);
                        }

                        return;
                    }

                    // Only apply palettes to indexed image data.
                    converter.Initialize(frame, convertFormat, (BitmapDitherType)Dithering, null, 0.0, BitmapPaletteType.Custom);
                    converter.CopyPixels(buffer.PitchInformation.RowPitch, buffer.Data.BasePointer, buffer.PitchInformation.SlicePitch);
                }
                finally
                {
                    if ((paletteInfo != null) && (paletteInfo.Item1 != null))
                    {
                        paletteInfo.Item1.Dispose();
                    }
                }
            }
        }
예제 #2
0
            public BitmapFrameDecoder(Func <Device, int, int, int, Format, Usage, Texture> textureFactory, MemoryPool memoryPool, Stream stream)
                : base(textureFactory, memoryPool)
            {
                var dataStream = stream as SharpDX.DataStream;

                using (var wicStream = new WICStream(Factory, new SharpDX.DataPointer(dataStream.DataPointer, (int)dataStream.Length)))
                    using (var decoder = new BitmapDecoder(Factory, wicStream, DecodeOptions.CacheOnLoad))
                    {
                        using (var frame = decoder.GetFrame(0))
                        {
                            var dstPixelFormat = PixelFormat.Format32bppBGRA;

                            Width   = frame.Size.Width;
                            Height  = frame.Size.Height;
                            FStride = PixelFormat.GetStride(dstPixelFormat, Width);
                            FLength = FStride * Height;

                            FBuffer = memoryPool.UnmanagedPool.GetMemory(FLength);

                            if (frame.PixelFormat != dstPixelFormat)
                            {
                                using (var converter = new FormatConverter(Factory))
                                {
                                    converter.Initialize(frame, dstPixelFormat);
                                    converter.CopyPixels(FStride, FBuffer);
                                }
                            }
                            else
                            {
                                frame.CopyPixels(FStride, FBuffer);
                            }
                        }
                    }
                memoryPool.StreamPool.PutStream(stream);
            }
예제 #3
0
            void ColorCameraLoop()
            {
                while (true)
                {
                    var encodedColorData = camera.Client.LatestJPEGImage();

                    // decode JPEG
                    var memoryStream = new MemoryStream(encodedColorData);
                    var stream       = new WICStream(imagingFactory, memoryStream);
                    // decodes to 24 bit BGR
                    var decoder           = new SharpDX.WIC.BitmapDecoder(imagingFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnLoad);
                    var bitmapFrameDecode = decoder.GetFrame(0);

                    // convert to 32 bpp
                    var formatConverter = new FormatConverter(imagingFactory);
                    formatConverter.Initialize(bitmapFrameDecode, SharpDX.WIC.PixelFormat.Format32bppBGR);
                    formatConverter.CopyPixels(nextColorData, Kinect2Calibration.colorImageWidth * 4); // TODO: consider copying directly to texture native memory
                    //lock (colorData)
                    //    Swap<byte[]>(ref colorData, ref nextColorData);
                    lock (renderLock) // necessary?
                    {
                        UpdateColorImage(device.ImmediateContext, nextColorData);
                    }
                    memoryStream.Close();
                    memoryStream.Dispose();
                    stream.Dispose();
                    decoder.Dispose();
                    formatConverter.Dispose();
                    bitmapFrameDecode.Dispose();
                }
            }
예제 #4
0
        static byte[] CreateRaw(IntPtr pixels, int width, int height)
        {
            using (var bitmap = new System.Drawing.Bitmap(width, height, width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, pixels))
            {
                using (var stream = new MemoryStream())
                {
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                    var factory = new ImagingFactory2();
                    using (var decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnDemand))
                    {
                        using (var formatConverter = new FormatConverter(factory))
                        {
                            formatConverter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA);

                            var stride = formatConverter.Size.Width * 4;
                            using (var dataStream = new SharpDX.DataStream(formatConverter.Size.Height * stride, true, true))
                            {
                                formatConverter.CopyPixels(stride, dataStream);

                                byte[] b;

                                using (BinaryReader br = new BinaryReader(dataStream))
                                {
                                    b = br.ReadBytes((int)dataStream.Length);
                                }

                                return(b);
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
파일: WICHelper.cs 프로젝트: alir14/3DModel
        //-------------------------------------------------------------------------------------
        // Encodes a single frame
        //-------------------------------------------------------------------------------------
        private static void EncodeImage(PixelBuffer image, WICFlags flags, BitmapFrameEncode frame)
        {
            Guid pfGuid;

            if (!ToWIC(image.Format, out pfGuid))
            {
                throw new NotSupportedException("Format not supported");
            }

            frame.Initialize();
            frame.SetSize(image.Width, image.Height);
            frame.SetResolution(72, 72);
            Guid targetGuid = pfGuid;

            frame.SetPixelFormat(ref targetGuid);

            if (targetGuid != pfGuid)
            {
                using (var source = new Bitmap(Factory, image.Width, image.Height, pfGuid, new DataRectangle(image.DataPointer, image.RowStride), image.BufferStride))
                {
                    using (var converter = new FormatConverter(Factory))
                    {
                        using (var palette = new Palette(Factory))
                        {
                            palette.Initialize(source, 256, true);
                            converter.Initialize(source, targetGuid, GetWICDither(flags), palette, 0, BitmapPaletteType.Custom);

                            int bpp = GetBitsPerPixel(targetGuid);
                            if (bpp == 0)
                            {
                                throw new NotSupportedException("Unable to determine the Bpp for the target format");
                            }

                            int rowPitch   = (image.Width * bpp + 7) / 8;
                            int slicePitch = rowPitch * image.Height;

                            var temp = Utilities.AllocateMemory(slicePitch);
                            try
                            {
                                converter.CopyPixels(rowPitch, temp, slicePitch);
                                frame.Palette = palette;
                                frame.WritePixels(image.Height, temp, rowPitch, slicePitch);
                            }
                            finally
                            {
                                Utilities.FreeMemory(temp);
                            }
                        }
                    }
                }
            }
            else
            {
                // No conversion required
                frame.WritePixels(image.Height, image.DataPointer, image.RowStride, image.BufferStride);
            }

            frame.Commit();
        }
예제 #6
0
    private static Texture DecodeMultiframe(ImagingFactory imagingFactory, WicFlags flags, TextureDescription description, BitmapDecoder decoder)
    {
      var texture = new Texture(description);
      Guid dstFormat = ToWic(description.Format, false);

      for (int index = 0; index < description.ArraySize; ++index)
      {
        var image = texture.Images[index];
        using (var frame = decoder.GetFrame(index))
        {
          var pfGuid = frame.PixelFormat;
          var size = frame.Size;

          if (size.Width == description.Width && size.Height == description.Height)
          {
            // This frame does not need resized
            if (pfGuid == dstFormat)
            {
              frame.CopyPixels(image.Data, image.RowPitch);
            }
            else
            {
              using (var converter = new FormatConverter(imagingFactory))
              {
                converter.Initialize(frame, dstFormat, GetWicDither(flags), null, 0, BitmapPaletteType.Custom);
                converter.CopyPixels(image.Data, image.RowPitch);
              }
            }
          }
          else
          {
            // This frame needs resizing
            using (var scaler = new BitmapScaler(imagingFactory))
            {
              scaler.Initialize(frame, description.Width, description.Height, GetWicInterp(flags));

              Guid pfScaler = scaler.PixelFormat;
              if (pfScaler == dstFormat)
              {
                scaler.CopyPixels(image.Data, image.RowPitch);
              }
              else
              {
                // The WIC bitmap scaler is free to return a different pixel format than the source image, so here we
                // convert it to our desired format
                using (var converter = new FormatConverter(imagingFactory))
                {
                  converter.Initialize(scaler, dstFormat, GetWicDither(flags), null, 0, BitmapPaletteType.Custom);
                  converter.CopyPixels(image.Data, image.RowPitch);
                }
              }
            }
          }
        }
      }

      return texture;
    }
예제 #7
0
        public void Load(string filename, System.Threading.CancellationToken token)
        {
            int stride;
            var imgF = new ImagingFactory();

            using (var decoder = new SharpDX.WIC.BitmapDecoder(imgF, filename, SharpDX.IO.NativeFileAccess.Read, DecodeOptions.CacheOnLoad))
                using (var frame = decoder.GetFrame(0))
                {
                    var  format      = PixelToTextureFormat(frame.PixelFormat);
                    bool knownFormat = format != Format.Unknown;

                    var w = frame.Size.Width;
                    var h = frame.Size.Height;
                    stride = PixelFormat.GetStride(knownFormat ? frame.PixelFormat:PixelFormat.Format32bppRGBA, w);
                    //stride = PixelFormat.GetStride(PixelFormat.Format32bppBGRA, w);
                    FLength = stride * h;

                    Description = new Texture2DDescription()
                    {
                        ArraySize         = 1,
                        BindFlags         = BindFlags.ShaderResource,
                        CpuAccessFlags    = CpuAccessFlags.None,
                        Format            = knownFormat?format:Format.R8G8B8A8_UNorm,
                        MipLevels         = 1,
                        OptionFlags       = ResourceOptionFlags.None,
                        Usage             = ResourceUsage.Default,
                        Width             = w,
                        Height            = h,
                        SampleDescription = new SampleDescription(1, 0)
                    };
                    token.ThrowIfCancellationRequested();
                    ptr = mp.UnmanagedPool.GetMemory(FLength);
                    //if (frame.PixelFormat != PixelFormat.Format32bppBGRA)
                    if (!knownFormat)
                    {
                        using (var converter = new FormatConverter(imgF))
                        {
                            converter.Initialize(frame, PixelFormat.Format32bppRGBA);
                            converter.CopyPixels(stride, ptr, FLength);
                        }
                    }
                    else
                    {
                        frame.CopyPixels(stride, ptr, FLength);
                    }
                }
            token.ThrowIfCancellationRequested();
            ds = new SlimDX.DataStream(ptr, FLength, true, false);
            var dr = new SlimDX.DataRectangle(stride, ds);

            token.ThrowIfCancellationRequested();
            tex = new Texture2D(Device, Description, dr);
            token.ThrowIfCancellationRequested();
            SRV = new ShaderResourceView(Device, tex);
        }
        public H1ImageWrapper(String filePath)
        {
            // to manipulate WIC objects
            ImagingFactory imagingFactory = new ImagingFactory();

            // to open the file that holds the bitmap data
            NativeFileStream fileStream = new NativeFileStream(
                filePath, NativeFileMode.Open, NativeFileAccess.Read);

            BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream,
                                                            DecodeOptions.CacheOnDemand // for the time being as we won't be needing to take advantage of special cache handling
                                                            );

            // to retrieve the frame index 0 (static image only have one frame)
            const Int32       StaticFrame = 0;
            BitmapFrameDecode frame       = bitmapDecoder.GetFrame(StaticFrame);

            // convert out bitmaps to the same pixel format for the shake of normalization
            FormatConverter converter = new FormatConverter(imagingFactory);

            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

            // set the pixel format
            SetPixelFormat(converter.PixelFormat);

            // having the correct pixel format, we can finally create the desired SharpDX.Direct2D1.Bitmap1
            Int32 width    = converter.Size.Width;
            Int32 height   = converter.Size.Height;
            Int32 stride   = converter.Size.Width * 4;
            Int32 dataSize = height * stride;

            using (var buffer = new SharpDX.DataStream(dataSize, true, true))
            {
                // copy the data to the buffer
                converter.CopyPixels(stride, buffer);

                H1GeneralBuffer generalBuffer = H1Global <H1ManagedRenderer> .Instance.CreateGeneralBuffer(Convert.ToUInt32(dataSize));

                // mapping the data to the resource (buffer)
                generalBuffer.WriteData(buffer.DataPointer, dataSize);

                // first create texture resource
                //H1Texture2D textureObject = H1Global<H1ManagedRenderer>.Instance.CreateTexture2D(PixelFormat, width, height, new SharpDX.Vector4(), null);
                m_tempTextureObject = H1Global <H1ManagedRenderer> .Instance.CreateTexture2D(PixelFormat, width, height, new SharpDX.Vector4(), null);

                // copy texture region
                //H1Global<H1ManagedRenderer>.Instance.CopyTextureRegion(textureObject, generalBuffer);
                H1Global <H1ManagedRenderer> .Instance.CopyTextureRegion(m_tempTextureObject, generalBuffer);
            }

            //https://english.r2d2rigo.es/2014/08/12/loading-and-drawing-bitmaps-with-direct2d-using-sharpdx/
            //http://stackoverflow.com/questions/9602102/loading-textures-with-sharpdx-in-windows-store-app
            //http://sharpdx.org/wiki/class-library-api/wic/
        }
예제 #9
0
        protected override Texture LoadStaging(Stream stream)
        {
            using var wicStream  = new WICStream(_wicFactory, stream);
            using var decoder    = new BitmapDecoder(_wicFactory, wicStream, DecodeOptions.CacheOnDemand);
            using var formatConv = new FormatConverter(_wicFactory);
            // Do NOT dispose the frame as it might lead to a crash.
            // Seems like it's owned by the decoder, so hopefully there should be no leaks.
            BitmapFrameDecode frame = decoder.GetFrame(0);

            formatConv.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGBA);

            uint    width          = (uint)frame.Size.Width;
            uint    height         = (uint)frame.Size.Height;
            Texture stagingTexture = _rf.CreateTexture(TextureDescription.Texture2D(
                                                           width, height, mipLevels: 1, arrayLayers: 1,
                                                           PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Staging
                                                           ));

            MappedResource map      = _gd.Map(stagingTexture, MapMode.Write);
            uint           rowWidth = width * 4;

            if (rowWidth == map.RowPitch)
            {
                formatConv.CopyPixels((int)map.RowPitch, map.Data, (int)map.SizeInBytes);
            }
            else
            {
                for (uint y = 0; y < height; y++)
                {
                    byte *dstStart = (byte *)map.Data + y * map.RowPitch;
                    formatConv.CopyPixels(
                        new RawBox(x: 0, (int)y, (int)width, height: 1),
                        (int)map.RowPitch,
                        new SharpDX.DataPointer(dstStart, (int)map.RowPitch)
                        );
                }
            }

            _gd.Unmap(stagingTexture);
            return(stagingTexture);
        }
예제 #10
0
            protected override Texture Decode(Device device, Format format)
            {
                if (format != FChosenFormat)
                {
                    // Ouch - need to convert once more on render thread
                    var chosenPixelFormat = TextureToPixelFormat(FChosenFormat);
                    // TODO: Remember this for the future
                    using (var frame = new Bitmap(Factory, Width, Height, chosenPixelFormat, new DataRectangle(FBuffer.Pointer, FStride), FBuffer.Size))
                    {
                        var pixelFormat = TextureToPixelFormat(format);
                        FStride = PixelFormat.GetStride(pixelFormat, Width);
                        var length    = FStride * Height;
                        var newBuffer = FMemoryPool.UnmanagedPool.GetMemory(length);
                        using (var converter = new FormatConverter(Factory))
                        {
                            converter.Initialize(frame, pixelFormat);
                            converter.CopyPixels(FStride, newBuffer);
                        }
                        FMemoryPool.UnmanagedPool.PutMemory(FBuffer);
                        FBuffer = newBuffer;
                    }
                    FChosenFormat = format;
                }

                var usage         = Usage.Dynamic & ~Usage.AutoGenerateMipMap;
                var texture       = FTextureFactory(device, Width, Height, 1, format, usage);
                var dataRectangle = texture.LockRectangle(0, LockFlags.Discard);

                try
                {
                    if (dataRectangle.Pitch == FStride)
                    {
                        Utilities.CopyMemory(dataRectangle.DataPointer, FBuffer.Pointer, FBuffer.Size);
                    }
                    else
                    {
                        for (int y = 0; y < Height; y++)
                        {
                            var src = FBuffer.Pointer + y * FStride;
                            var dst = dataRectangle.DataPointer + y * dataRectangle.Pitch;
                            Utilities.CopyMemory(dst, src, FStride);
                        }
                    }
                }
                finally
                {
                    texture.UnlockRectangle(0);
                }
                return(texture);
            }
예제 #11
0
        public static Texture2D LoadTextureFromFile(SharpDX.Direct3D11.Device aDevice, string aFullPath)
        {
            Texture2D      result = null;
            ImagingFactory fac    = new ImagingFactory();

            BitmapDecoder     bc  = new SharpDX.WIC.BitmapDecoder(fac, aFullPath, DecodeOptions.CacheOnLoad);
            BitmapFrameDecode bfc = bc.GetFrame(0);
            FormatConverter   fc  = new FormatConverter(fac);

            System.Guid desiredFormat = PixelFormat.Format32bppBGRA;
            fc.Initialize(bfc, desiredFormat);

            float[] buffer = new float[fc.Size.Width * fc.Size.Height];

            bool canConvert = fc.CanConvert(bfc.PixelFormat, desiredFormat);

            fc.CopyPixels <float>(buffer);
            GCHandle handle      = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            float    sizeOfPixel = PixelFormat.GetBitsPerPixel(desiredFormat) / 8;

            if (sizeOfPixel != 4.0f)
            {
                throw new System.Exception("Unknown error");
            }

            DataBox db = new DataBox(handle.AddrOfPinnedObject(), fc.Size.Width * (int)sizeOfPixel, fc.Size.Width * fc.Size.Height * (int)sizeOfPixel);

            int width  = fc.Size.Width;
            int height = fc.Size.Height;

            Texture2DDescription fTextureDesc = new Texture2DDescription();

            fTextureDesc.CpuAccessFlags    = CpuAccessFlags.None;
            fTextureDesc.Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
            fTextureDesc.Width             = width;
            fTextureDesc.Height            = height;
            fTextureDesc.Usage             = ResourceUsage.Default;
            fTextureDesc.MipLevels         = 1;
            fTextureDesc.ArraySize         = 1;
            fTextureDesc.OptionFlags       = ResourceOptionFlags.None;
            fTextureDesc.BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource;
            fTextureDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

            result = new Texture2D(aDevice, fTextureDesc, new DataBox[] { db });
            handle.Free();

            return(result);
        }
예제 #12
0
            public BitmapFrameDecoder(Func <Device, int, int, int, Format, Usage, Texture> textureFactory, MemoryPool memoryPool, Stream stream, Format preferedFormat)
                : base(textureFactory, memoryPool, preferedFormat)
            {
                var dataStream = stream as SharpDX.DataStream;

                using (var wicStream = new WICStream(Factory, new SharpDX.DataPointer(dataStream.DataPointer, (int)dataStream.Length)))
                    using (var decoder = new BitmapDecoder(Factory, wicStream, DecodeOptions.CacheOnLoad))
                        using (var frame = decoder.GetFrame(0))
                        {
                            // Choose pixel format from file
                            if (preferedFormat == Format.Unknown)
                            {
                                try
                                {
                                    FChosenFormat = PixelToTextureFormat(frame.PixelFormat);
                                }
                                catch (NotSupportedException)
                                {
                                    // The format as given by the file is not supported by DirectX
                                    FChosenFormat = Format.A8R8G8B8;
                                }
                            }

                            FChosenFormat = GetFallbackFormatIfPreferedFormatIsNotSupported(FChosenFormat);
                            var chosenPixelFormat = TextureToPixelFormat(FChosenFormat);
                            Width   = frame.Size.Width;
                            Height  = frame.Size.Height;
                            FStride = PixelFormat.GetStride(chosenPixelFormat, Width);
                            var length = FStride * Height;

                            FBuffer = memoryPool.UnmanagedPool.GetMemory(length);

                            if (frame.PixelFormat != chosenPixelFormat)
                            {
                                using (var converter = new FormatConverter(Factory))
                                {
                                    converter.Initialize(frame, chosenPixelFormat);
                                    converter.CopyPixels(FStride, FBuffer);
                                }
                            }
                            else
                            {
                                frame.CopyPixels(FStride, FBuffer);
                            }
                        }
                memoryPool.StreamPool.PutStream(stream);
            }
예제 #13
0
        public static ShaderResourceView LoadFromFile(GraphicsDevice device, string fileName)
        {
            var factory = new ImagingFactory();

            BitmapDecoder bitmapDecoder = new BitmapDecoder(factory, fileName, DecodeOptions.CacheOnDemand);

            FormatConverter FC = new FormatConverter(factory);

            FC.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom);

            Texture2DDescription desc = new Texture2DDescription();

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

            DataStream DS = new DataStream(FC.Size.Height * FC.Size.Width * 4, true, true);

            FC.CopyPixels(FC.Size.Width * 4, DS);

            DataRectangle rect = new DataRectangle(DS.DataPointer, FC.Size.Width * 4);

            Texture2D t2D = new Texture2D(device.NativeDevice, desc, rect);

            ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription();

            srvDesc.Format    = t2D.Description.Format;
            srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
            srvDesc.Texture2D.MostDetailedMip = 0;
            srvDesc.Texture2D.MipLevels       = -1;

            ShaderResourceView TextureResource = new ShaderResourceView(device.NativeDevice, t2D, srvDesc);

            device.NativeDeviceContext.GenerateMips(TextureResource);


            return(TextureResource);
        }
예제 #14
0
 public static Texture2D FromFile(string file)
 {
     using (var bitmapDecoder = new BitmapDecoder(imagingFactory, file, DecodeOptions.CacheOnDemand))
         using (var formatConverter = new FormatConverter(imagingFactory)) {
             formatConverter.Initialize(
                 bitmapDecoder.GetFrame(0),
                 PixelFormat.Format32bppPRGBA,
                 BitmapDitherType.None,
                 null,
                 0.0,
                 BitmapPaletteType.Custom);
             var tex    = new Texture2D(formatConverter.Size.Width, formatConverter.Size.Height, Format.R8G8B8A8_UNorm);
             int stride = tex.Width * tex.Format.SizeOfInBytes();
             formatConverter.CopyPixels(tex.GetPixels(), stride);
             tex.ApplyChanges();
             return(tex);
         }
 }
예제 #15
0
    public void TestThatUint16ToUint8ConversionDoesNotChangeColorSpace()
    {
        using (var factory = new ImagingFactory()) {
            ushort[] pixelData = new ushort[] { 0, ushort.MaxValue / 2, ushort.MaxValue };
            using (var bitmap = Bitmap.New(factory, 1, 1, PixelFormat.Format48bppRGB, pixelData, sizeof(ushort) * 3)) {
                using (var converter = new FormatConverter(factory)) {
                    converter.Initialize(bitmap, PixelFormat.Format32bppRGB);

                    byte[] pixelDataOut = new byte[4];
                    converter.CopyPixels(pixelDataOut, sizeof(byte) * 4);

                    Assert.AreEqual(0, pixelDataOut[0], "r");
                    Assert.AreEqual(byte.MaxValue / 2, pixelDataOut[1], "b");
                    Assert.AreEqual(byte.MaxValue, pixelDataOut[2], "b");
                }
            }
        }
    }
예제 #16
0
    public void TestThatFloat32ToUint8ConversionDoesChangeColorSpace()
    {
        using (var factory = new ImagingFactory()) {
            float[] pixelData = new float[] { 0, 0.5f, 1 };
            using (var bitmap = Bitmap.New(factory, 1, 1, PixelFormat.Format96bppRGBFloat, pixelData, sizeof(float) * 3)) {
                using (var converter = new FormatConverter(factory)) {
                    converter.Initialize(bitmap, PixelFormat.Format32bppRGB);

                    byte[] pixelDataOut = new byte[4];
                    converter.CopyPixels(pixelDataOut, sizeof(byte) * 4);

                    Assert.AreEqual(0, pixelDataOut[0], "r");
                    Assert.AreEqual(Srgb50Intensity, pixelDataOut[1], "b");
                    Assert.AreEqual(byte.MaxValue, pixelDataOut[2], "b");
                }
            }
        }
    }
예제 #17
0
        internal SharpDX.DataStream CreateBitmap(MemoryStream stream, out SharpDX.Size2 size)
        {
            using (var decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnDemand))
            {
                using (var formatConverter = new FormatConverter(factory))
                {
                    formatConverter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA);

                    var stride     = formatConverter.Size.Width * 4;
                    var dataStream = new SharpDX.DataStream(formatConverter.Size.Height * stride, true, true);
                    formatConverter.CopyPixels(stride, dataStream);

                    size = formatConverter.Size;

                    return(dataStream);
                }
            }
        }
예제 #18
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;
    }
예제 #19
0
    public static UnmanagedRgbaImage Load(FileInfo file)
    {
        using (var factory = new ImagingFactory())
            using (var decoder = new BitmapDecoder(factory, file.FullName, new DecodeOptions {
            }))
                using (var frame = decoder.GetFrame(0)) {
                    if (!SupportedInputPixelFormats.Contains(frame.PixelFormat))
                    {
                        throw new InvalidOperationException($"unsupported pixel format: {frame.PixelFormat}");
                    }

                    using (var converter = new FormatConverter(factory)) {
                        converter.Initialize(frame, PixelFormat.Format32bppBGR);

                        var size  = converter.Size;
                        var image = new UnmanagedRgbaImage(size);
                        converter.CopyPixels(image.Stride, image.PixelData, image.SizeInBytes);
                        return(image);
                    }
                }
    }
 public static byte[] LoadBytesFormFile(SharpDX.Direct3D11.DeviceContext device, string filename)
 {
     using (var Imgfactory = new ImagingFactory2())
         using (var d = new BitmapDecoder(
                    Imgfactory,
                    filename,
                    DecodeOptions.CacheOnDemand
                    ))
             using (var frame = d.GetFrame(0))
                 using (var fconv = new FormatConverter(Imgfactory))
                 {
                     fconv.Initialize(
                         frame,
                         SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                         BitmapDitherType.None, null,
                         0.0, BitmapPaletteType.Custom);
                     var b = new byte[fconv.Size.Width * fconv.Size.Height * 4];
                     fconv.CopyPixels(b, fconv.Size.Width * 4);
                     return(b);
                 }
 }
예제 #21
0
파일: WICHelper.cs 프로젝트: alir14/3DModel
        //-------------------------------------------------------------------------------------
        // 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);
        }
        private async Task UpdateColorTexture()
        {
            try
            {
                var encodedColorData = await _camera.Client.LatestJPEGImageAsync();

                // decode JPEG
                var memoryStream = new MemoryStream(encodedColorData);

                var stream = new WICStream(_imagingFactory, memoryStream);
                // decodes to 24 bit BGR
                var decoder           = new SharpDX.WIC.BitmapDecoder(_imagingFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnLoad);
                var bitmapFrameDecode = decoder.GetFrame(0);

                // convert to 32 bpp
                var formatConverter = new FormatConverter(_imagingFactory);
                formatConverter.Initialize(bitmapFrameDecode, SharpDX.WIC.PixelFormat.Format32bppBGR);
                formatConverter.CopyPixels(nextColorData, RoomAliveToolkit.Kinect2Calibration.colorImageWidth * 4);

                UpdateColorImage(GraphicsDevice, nextColorData);
                memoryStream.Close();
                memoryStream.Dispose();
                stream.Dispose();
                decoder.Dispose();
                formatConverter.Dispose();
                bitmapFrameDecode.Dispose();
            }
            catch (System.ServiceModel.EndpointNotFoundException ex)
            {
                // TODO Message
                LiveColor = false;
                Console.WriteLine("Could not connect to Kinect for live color. Start Kinect server.");
            }
            catch (System.ServiceModel.CommunicationException)
            {
                Console.WriteLine("Connection to Kinect server for live color was lost. Restart Kinect server and the application.");
                LiveDepth = false;
            }
        }
예제 #23
0
        //
        // https://docs.microsoft.com/en-us/windows/desktop/direct3d11/overviews-direct3d-11-resources-textures-how-to
        //
        public static void LoadImage(ImageBytes image, Action <DataStream, Format, int, int> callback)
        {
            var bytes = image.Bytes;

            using (var s = new MemoryStream(bytes.Array, bytes.Offset, bytes.Count, false))
                using (var factory = new ImagingFactory())
                    using (var stream = new WICStream(factory, s))
                        using (var decoder = GetDecoder(factory, image.Format))
                        {
                            decoder.Initialize(stream, DecodeOptions.CacheOnDemand);
                            using (var frame = decoder.GetFrame(0))
                            {
                                var format = default(Format);
                                var stride = frame.Size.Width * 4;

                                using (var buffer = new DataStream(frame.Size.Height * stride, true, true))
                                {
                                    if (frame.PixelFormat == PixelFormat.Format32bppBGRA)
                                    {
                                        // OK
                                        format = Format.B8G8R8A8_UNorm;
                                        frame.CopyPixels(stride, buffer);
                                    }
                                    else
                                    {
                                        // Convert
                                        var fc = new FormatConverter(factory);
                                        fc.Initialize(frame, PixelFormat.Format32bppBGR);
                                        fc.CopyPixels(stride, buffer);
                                        format = Format.B8G8R8A8_UNorm;
                                    }

                                    callback(buffer, format, frame.Size.Width, frame.Size.Height);
                                }
                            };
                        }
        }
예제 #24
0
        public static Texture2D LoadImage(Device device, RenderTarget rtv2d, string filename)
        {
            var imgFactory = new ImagingFactory();

            BitmapDecoder   bmpDecoder   = new BitmapDecoder(imgFactory, filename, DecodeOptions.CacheOnDemand);
            FormatConverter bitmapSource = new FormatConverter(imgFactory);

            bitmapSource.Initialize(bmpDecoder.GetFrame(0), SharpDX.WIC.PixelFormat.Format32bppPRGBA);

            SharpDX.Direct2D1.Bitmap bitmap = Bitmap1.FromWicBitmap(rtv2d, bitmapSource);

            int stride = bitmapSource.Size.Width * 4;

            using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true))
            {
                bitmapSource.CopyPixels(stride, buffer);
                Texture2D texture = 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            = Format.R8G8B8A8_UNorm,
                    MipLevels         = 1,
                    OptionFlags       = ResourceOptionFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                }, new DataRectangle(buffer.DataPointer, stride));

                Utilities.Dispose(ref bmpDecoder);
                Utilities.Dispose(ref bitmapSource);
                Utilities.Dispose(ref bitmap);

                return(texture);
            }
        }
예제 #25
0
        /// <summary>
        /// Import texture for DirectX11 renderer
        /// </summary>
        /// <param name="device"> DirectX11 target rendering device </param>
        /// <param name="TexturePath"> Source path for the texture </param>
        internal static Texture2D BitmapToDX11Texture(SharpDX.Direct3D11.Device device, string TexturePath)
        {
            ImagingFactory2 Fac = new ImagingFactory2();

            BitmapDecoder bitmapDecoder = new BitmapDecoder(Fac, TexturePath, DecodeOptions.CacheOnDemand
                                                            );

            FormatConverter formatConverter = new FormatConverter(Fac);

            formatConverter.Initialize(
                bitmapDecoder.GetFrame(0),
                PixelFormat.Format32bppPRGBA,
                BitmapDitherType.None,
                null,
                0.0,
                BitmapPaletteType.Custom);

            using (var buffer = new DataStream(formatConverter.Size.Height * formatConverter.Size.Width * 4, true, true))
            {
                formatConverter.CopyPixels(formatConverter.Size.Width * 4, buffer);

                return(new Texture2D(device, new Texture2DDescription()
                {
                    Width = formatConverter.Size.Width,
                    Height = formatConverter.Size.Height,
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource,
                    Usage = ResourceUsage.Immutable,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Format = Format.R8G8B8A8_UNorm,
                    MipLevels = 1,
                    OptionFlags = ResourceOptionFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                }, new DataRectangle(buffer.DataPointer, formatConverter.Size.Width * 4)));
            }
        }
예제 #26
0
        public MeshDeviceResources(Device device, SharpDX.WIC.ImagingFactory2 imagingFactory, Mesh mesh)
        {
            this.mesh = mesh;

            // create single vertex buffer
            var stream = new DataStream(mesh.vertices.Count * Mesh.VertexPositionNormalTexture.sizeInBytes, true, true);

            stream.WriteRange(mesh.vertices.ToArray());
            stream.Position = 0;

            var vertexBufferDesc = new BufferDescription()
            {
                BindFlags      = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                Usage          = ResourceUsage.Default,
                SizeInBytes    = mesh.vertices.Count * Mesh.VertexPositionNormalTexture.sizeInBytes,
            };

            vertexBuffer = new SharpDX.Direct3D11.Buffer(device, stream, vertexBufferDesc);

            stream.Dispose();

            vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Mesh.VertexPositionNormalTexture.sizeInBytes, 0);

            foreach (var subset in mesh.subsets)
            {
                if (subset.material.textureFilename != null)
                {
                    var decoder           = new SharpDX.WIC.BitmapDecoder(imagingFactory, subset.material.textureFilename, SharpDX.WIC.DecodeOptions.CacheOnLoad);
                    var bitmapFrameDecode = decoder.GetFrame(0);

                    var stagingTextureDesc = new Texture2DDescription()
                    {
                        Width             = bitmapFrameDecode.Size.Width,
                        Height            = bitmapFrameDecode.Size.Height,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                        Usage             = ResourceUsage.Dynamic,
                        BindFlags         = BindFlags.ShaderResource,
                        CpuAccessFlags    = CpuAccessFlags.Write
                    };
                    var stagingTexture = new Texture2D(device, stagingTextureDesc);

                    var textureDesc = new Texture2DDescription()
                    {
                        Width             = bitmapFrameDecode.Size.Width,
                        Height            = bitmapFrameDecode.Size.Height,
                        MipLevels         = 0,
                        ArraySize         = 1,
                        Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                        Usage             = ResourceUsage.Default,
                        BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                        CpuAccessFlags    = CpuAccessFlags.None,
                        OptionFlags       = ResourceOptionFlags.GenerateMipMaps
                    };
                    var texture = new Texture2D(device, textureDesc);

                    // convert to 32 bpp
                    var formatConverter = new FormatConverter(imagingFactory);
                    formatConverter.Initialize(bitmapFrameDecode, SharpDX.WIC.PixelFormat.Format32bppBGR);
                    var dataBox = device.ImmediateContext.MapSubresource(stagingTexture, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
                    formatConverter.CopyPixels(dataBox.RowPitch, dataBox.DataPointer, dataBox.RowPitch * bitmapFrameDecode.Size.Height);
                    device.ImmediateContext.UnmapSubresource(stagingTexture, 0);

                    var resourceRegion = new ResourceRegion()
                    {
                        Left   = 0,
                        Top    = 0,
                        Right  = bitmapFrameDecode.Size.Width,
                        Bottom = bitmapFrameDecode.Size.Height,
                        Front  = 0,
                        Back   = 1,
                    };
                    device.ImmediateContext.CopySubresourceRegion(stagingTexture, 0, resourceRegion, texture, 0);
                    var textureRV = new ShaderResourceView(device, texture);
                    device.ImmediateContext.GenerateMips(textureRV);

                    decoder.Dispose();
                    formatConverter.Dispose();
                    bitmapFrameDecode.Dispose();

                    textureRVs[subset] = textureRV;
                }
            }
        }
예제 #27
0
        /// <summary>
        /// Function to read multiple frames from a decoder that supports multiple frames.
        /// </summary>
        /// <param name="wic">WIC interface.</param>
        /// <param name="data">Image data to populate.</param>
        /// <param name="decoder">Decoder for the image.</param>
        private void ReadFrames(GorgonWICImage wic, GorgonImageData data, BitmapDecoder decoder)
        {
            Guid bestPixelFormat = wic.GetGUID(data.Settings.Format);

            // Find the best fit pixel format.
            if (bestPixelFormat == Guid.Empty)
            {
                throw new IOException(string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, data.Settings.Format));
            }

            // Use the image array as the frames.
            int arrayCount = _actualArrayCount.Min(data.Settings.ArrayCount);

            for (int array = 0; array < arrayCount; array++)
            {
                var buffer = data.Buffers[0, array];

                // Get the frame data.
                using (var frame = decoder.GetFrame(array))
                {
                    IntPtr bufferPointer = buffer.Data.BasePointer;
                    Guid   frameFormat   = frame.PixelFormat;
                    int    frameWidth    = frame.Size.Width;
                    int    frameHeight   = frame.Size.Height;
                    var    frameOffset   = GetFrameOffset(frame);

                    // Calculate the pointer offset if we have an offset from the frame.  Only offset if we're clipping the image though.
                    if (((frameOffset.Y != 0) || (frameOffset.X != 0)) && (Clip))
                    {
                        bufferPointer = buffer.Data.BasePointer + (frameOffset.Y * buffer.PitchInformation.RowPitch) + (frameOffset.X * (buffer.PitchInformation.RowPitch / buffer.Width));
                    }

                    // Confirm that we actually need to perform clipping.
                    bool needsSizeAdjust = (frameWidth + frameOffset.X > data.Settings.Width) || (frameHeight + frameOffset.Y > data.Settings.Height);

                    // If the formats match, then we don't need to do conversion.
                    if (bestPixelFormat == frameFormat)
                    {
                        // If the width and height are the same then we can just do a straight copy into the buffer.
                        if (((frameWidth == data.Settings.Width) && (frameHeight == data.Settings.Height)) || ((!needsSizeAdjust) && (Clip)))
                        {
                            frame.CopyPixels(buffer.PitchInformation.RowPitch, bufferPointer, buffer.PitchInformation.SlicePitch);

                            continue;
                        }

                        // We need to scale the image up/down to the size of our image data.
                        if (!Clip)
                        {
                            using (var scaler = new BitmapScaler(wic.Factory))
                            {
                                scaler.Initialize(frame, data.Settings.Width, data.Settings.Height, (BitmapInterpolationMode)Filter);
                                scaler.CopyPixels(buffer.PitchInformation.RowPitch, bufferPointer, buffer.PitchInformation.SlicePitch);
                            }
                            continue;
                        }

                        using (var clipper = new BitmapClipper(wic.Factory))
                        {
                            clipper.Initialize(frame, new Rectangle(0, 0, data.Settings.Width, data.Settings.Height));
                            clipper.CopyPixels(buffer.PitchInformation.RowPitch, bufferPointer, buffer.PitchInformation.SlicePitch);
                        }

                        continue;
                    }

                    // Poop.  We need to convert this image.
                    using (var converter = new FormatConverter(wic.Factory))
                    {
                        converter.Initialize(frame,
                                             bestPixelFormat,
                                             (BitmapDitherType)Dithering,
                                             null,
                                             0.0,
                                             BitmapPaletteType.Custom);

                        if (((frameWidth == data.Settings.Width) && (frameHeight == data.Settings.Height)) ||
                            ((!needsSizeAdjust) && (Clip)))
                        {
                            converter.CopyPixels(buffer.PitchInformation.RowPitch,
                                                 bufferPointer,
                                                 buffer.PitchInformation.SlicePitch);
                            continue;
                        }

                        // And we need to scale the image.
                        if (!Clip)
                        {
                            using (var scaler = new BitmapScaler(wic.Factory))
                            {
                                scaler.Initialize(converter,
                                                  data.Settings.Width,
                                                  data.Settings.Height,
                                                  (BitmapInterpolationMode)Filter);
                                scaler.CopyPixels(buffer.PitchInformation.RowPitch,
                                                  bufferPointer,
                                                  buffer.PitchInformation.SlicePitch);
                            }

                            continue;
                        }

                        using (var clipper = new BitmapClipper(wic.Factory))
                        {
                            clipper.Initialize(frame,
                                               new Rectangle(0, 0, data.Settings.Width, data.Settings.Height));
                            clipper.CopyPixels(buffer.PitchInformation.RowPitch,
                                               bufferPointer,
                                               buffer.PitchInformation.SlicePitch);
                        }
                    }
                }
            }
        }
        private Texture LoadTextureFromFile(string fileName, bool generateMips, int mipLevels = -1)
        {
            BitmapDecoder decoder = new BitmapDecoder(_imagingFactory,
                                                      fileName, DecodeOptions.CacheOnDemand);
            BitmapFrameDecode bitmapFirstFrame = decoder.GetFrame(0);

            Utilities.Dispose(ref decoder);

            FormatConverter formatConverter = new FormatConverter(_imagingFactory);

            formatConverter.Initialize(bitmapFirstFrame, PixelFormat.Format32bppRGBA,
                                       BitmapDitherType.None, null, 0.0f, BitmapPaletteType.Custom);

            int        stride = formatConverter.Size.Width * 4;
            DataStream buffer = new DataStream(
                formatConverter.Size.Height * stride, true, true);

            formatConverter.CopyPixels(stride, buffer);

            int width  = formatConverter.Size.Width;
            int height = formatConverter.Size.Height;
            Texture2DDescription texture2DDescription = new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = (generateMips ? 0 : 1),
                ArraySize         = 1,
                Format            = Format.R8G8B8A8_UNorm,
                SampleDescription = _sampleDescription,
                Usage             = ResourceUsage.Default,
                BindFlags         = (
                    generateMips ?
                    BindFlags.ShaderResource | BindFlags.RenderTarget : BindFlags.ShaderResource
                    ),
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags    = (
                    generateMips ?
                    ResourceOptionFlags.GenerateMipMaps :
                    ResourceOptionFlags.None
                    )
            };

            Texture2D textureObject;

            if (generateMips)
            {
                textureObject = new Texture2D(_directX3DGraphics.Device, texture2DDescription);
            }
            else
            {
                DataRectangle dataRectangle = new DataRectangle(buffer.DataPointer, stride);
                textureObject = new Texture2D(_directX3DGraphics.Device, texture2DDescription, dataRectangle);
            }

            ShaderResourceViewDescription shaderResourceViewDescription =
                new ShaderResourceViewDescription()
            {
                Dimension = ShaderResourceViewDimension.Texture2D,
                Format    = Format.R8G8B8A8_UNorm,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = (generateMips ? mipLevels : 1)
                }
            };
            ShaderResourceView shaderResourceView =
                new ShaderResourceView(_directX3DGraphics.Device, textureObject, shaderResourceViewDescription);

            if (generateMips)
            {
                DataBox dataBox = new DataBox(buffer.DataPointer, stride, 1);
                _directX3DGraphics.DeviceContext.UpdateSubresource(dataBox, textureObject, 0);
                _directX3DGraphics.DeviceContext.GenerateMips(shaderResourceView);
            }

            Utilities.Dispose(ref formatConverter);

            return(new Texture(textureObject, shaderResourceView, width, height, samplerState));
        }
예제 #29
0
파일: WICHelper.cs 프로젝트: alir14/3DModel
        //-------------------------------------------------------------------------------------
        // Decodes an image array, resizing/format converting as needed
        //-------------------------------------------------------------------------------------
        private static Image DecodeMultiframe(WICFlags flags, ImageDescription metadata, BitmapDecoder decoder)
        {
            var image = Image.New(metadata);

            Guid sourceGuid;

            if (!ToWIC(metadata.Format, out sourceGuid))
            {
                return(null);
            }

            for (int index = 0; index < metadata.ArraySize; ++index)
            {
                var pixelBuffer = image.PixelBuffer[index, 0];

                using (var frame = decoder.GetFrame(index))
                {
                    var pfGuid = frame.PixelFormat;
                    var size   = frame.Size;

                    if (pfGuid == sourceGuid)
                    {
                        if (size.Width == metadata.Width && size.Height == metadata.Height)
                        {
                            // This frame does not need resized or format converted, just copy...
                            frame.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                        }
                        else
                        {
                            // This frame needs resizing, but not format converted
                            using (var scaler = new BitmapScaler(Factory))
                            {
                                scaler.Initialize(frame, metadata.Width, metadata.Height, GetWICInterp(flags));
                                scaler.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                            }
                        }
                    }
                    else
                    {
                        // This frame required format conversion
                        using (var converter = new FormatConverter(Factory))
                        {
                            converter.Initialize(frame, pfGuid, GetWICDither(flags), null, 0, BitmapPaletteType.Custom);

                            if (size.Width == metadata.Width && size.Height == metadata.Height)
                            {
                                converter.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                            }
                            else
                            {
                                // This frame needs resizing, but not format converted
                                using (var scaler = new BitmapScaler(Factory))
                                {
                                    scaler.Initialize(frame, metadata.Width, metadata.Height, GetWICInterp(flags));
                                    scaler.CopyPixels(pixelBuffer.RowStride, pixelBuffer.DataPointer, pixelBuffer.BufferStride);
                                }
                            }
                        }
                    }
                }
            }
            return(image);
        }
예제 #30
0
        public Texture LoadTextureFromFile(string fileName, bool generateMip, SamplerState samplerState)
        {
            BitmapDecoder     decoder          = new BitmapDecoder(_imagingFactory, fileName, DecodeOptions.CacheOnDemand);
            BitmapFrameDecode bitmapFirstFrame = decoder.GetFrame(0);

            Utilities.Dispose(ref decoder);

            FormatConverter imageFormatConverter = new FormatConverter(_imagingFactory);

            imageFormatConverter.Initialize(
                bitmapFirstFrame,
                PixelFormat.Format32bppRGBA,
                BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom);

            int        stride = imageFormatConverter.Size.Width * 4;
            DataStream buffer = new DataStream(imageFormatConverter.Size.Height * stride, true, true);

            imageFormatConverter.CopyPixels(stride, buffer);

            int width  = imageFormatConverter.Size.Width;
            int height = imageFormatConverter.Size.Height;

            Texture2DDescription textureDescription = new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8_UNorm,
                SampleDescription = _directX3DGraphics.SampleDescription, // new SampleDescription(1,0)
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = (generateMip ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None)
            };
            Texture2D textureObject = new Texture2D(_directX3DGraphics.Device, textureDescription, new DataRectangle(buffer.DataPointer, stride));

            int mipLevels = (int)Math.Log(width, 2) + 1;
            ShaderResourceViewDescription shaderResourceViewDescription = new ShaderResourceViewDescription();

            shaderResourceViewDescription.Dimension = ShaderResourceViewDimension.Texture2D;
            shaderResourceViewDescription.Format    = Format.R8G8B8A8_UNorm;
            shaderResourceViewDescription.Texture2D = new ShaderResourceViewDescription.Texture2DResource
            {
                MostDetailedMip = 0,
                MipLevels       = -1 // (generateMip ? mipLevels : -1)
            };
            ShaderResourceView shaderResourceView = new ShaderResourceView(_directX3DGraphics.Device, textureObject, shaderResourceViewDescription);

            if (generateMip)
            {
                _directX3DGraphics.DeviceContext.GenerateMips(shaderResourceView);
            }

            Utilities.Dispose(ref imageFormatConverter);
            string[] path = fileName.Split('\\');
            fileName = path[path.Length - 1];

            Texture texture = new Texture(textureObject, shaderResourceView, width, height, fileName, samplerState);

            textures.Add(texture.Name, texture);

            return(texture);
        }