Beispiel #1
0
        private static unsafe void EncodeImage(PixelBuffer image, WICFlags flags, IWICBitmapFrameEncode 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);
            var targetGuid = pfGuid;

            frame.SetPixelFormat(ref targetGuid);

            if (targetGuid != pfGuid)
            {
                using (var source = Factory.CreateBitmapFromMemory(image.Width, image.Height, pfGuid, image.RowStride, image.BufferStride, image.DataPointer.ToPointer()))
                {
                    using (var converter = Factory.CreateFormatConverter())
                    {
                        using (var palette = Factory.CreatePalette())
                        {
                            palette.InitializeFromBitmap(source, 256, true);
                            converter.Initialize(source, targetGuid, GetWICDither(flags), palette, 0, BitmapPaletteType.Custom);

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

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

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

            frame.Commit();
        }
Beispiel #2
0
        public void Dispose()
        {
            if (handle.HasValue)
            {
                handle.Value.Free();
            }

            if (bufferIsDisposable)
            {
                SdxUtilities.FreeMemory(buffer);
            }
        }
Beispiel #3
0
        internal static Image LoadFromWICMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle)
        {
            var flags = WICFlags.AllFrames;

            Image image = null;

            // Create input stream for memory
            using (var stream = Factory.CreateStream(new DataStream(pSource, size, true, false)))
            {
                // If the decoder is unable to decode the image, than return null
                IWICBitmapDecoder decoder = null;
                try
                {
                    decoder = Factory.CreateDecoderFromStream(stream, DecodeOptions.CacheOnDemand);
                    using (var frame = decoder.GetFrame(0))
                    {
                        // Get metadata
                        Guid convertGuid;
                        var  tempDesc = DecodeMetadata(flags, decoder, frame, out convertGuid);

                        // If not supported.
                        if (!tempDesc.HasValue)
                        {
                            return(null);
                        }

                        var mdata = tempDesc.Value;

                        if (mdata.ArraySize > 1 && (flags & WICFlags.AllFrames) != 0)
                        {
                            return(DecodeMultiframe(flags, mdata, decoder));
                        }

                        image = DecodeSingleFrame(flags, mdata, convertGuid, frame);
                    }
                }
                catch
                {
                    image = null;
                }
                finally
                {
                    if (decoder != null)
                    {
                        decoder.Dispose();
                    }
                }
            }

            // For WIC, we are not keeping the original buffer.
            if (image != null && !makeACopy)
            {
                if (handle.HasValue)
                {
                    handle.Value.Free();
                }
                else
                {
                    SdxUtilities.FreeMemory(pSource);
                }
            }
            return(image);
        }