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(); }
internal unsafe void Initialize(ImageDescription description, IntPtr dataPointer, int offset, GCHandle?handle, bool bufferIsDisposable, PitchFlags pitchFlags = PitchFlags.None) { if (!FormatHelper.IsValid(description.Format) || FormatHelper.IsVideo(description.Format)) { throw new InvalidOperationException("Unsupported DXGI Format"); } this.handle = handle; switch (description.Dimension) { case TextureDimension.Texture1D: if (description.Width <= 0 || description.Height != 1 || description.Depth != 1 || description.ArraySize == 0) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 1D"); } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, 1, description.MipLevels); break; case TextureDimension.Texture2D: case TextureDimension.TextureCube: if (description.Width <= 0 || description.Height <= 0 || description.Depth != 1 || description.ArraySize == 0) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 2D"); } if (description.Dimension == TextureDimension.TextureCube) { if (description.ArraySize % 6 != 0) { throw new InvalidOperationException("TextureCube must have an arraysize = 6"); } } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, description.Height, description.MipLevels); break; case TextureDimension.Texture3D: if (description.Width <= 0 || description.Height <= 0 || description.Depth <= 0 || description.ArraySize != 1) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 3D"); } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, description.Height, description.Depth, description.MipLevels); break; } // Calculate mipmaps int pixelBufferCount; mipMapToZIndex = CalculateImageArray(description, pitchFlags, out pixelBufferCount, out totalSizeInBytes); mipmapDescriptions = CalculateMipMapDescription(description, pitchFlags); zBufferCountPerArraySlice = mipMapToZIndex[mipMapToZIndex.Count - 1]; // Allocate all pixel buffers pixelBuffers = new PixelBuffer[pixelBufferCount]; pixelBufferArray = new PixelBufferArray(this); // Setup all pointers // only release buffer that is not pinned and is asked to be disposed. this.bufferIsDisposable = !handle.HasValue && bufferIsDisposable; buffer = dataPointer; if (dataPointer == IntPtr.Zero) { buffer = SdxUtilities.AllocateMemory(totalSizeInBytes); offset = 0; this.bufferIsDisposable = true; } SetupImageArray((IntPtr)((byte *)buffer + offset), totalSizeInBytes, description, pitchFlags, pixelBuffers); Description = description; // PreCompute databoxes dataBoxArray = this.ComputeDataBox(); }