private static void TestBmpEncoderCore <TPixel>( TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel, bool supportTransparency = true, // if set to true, will write a V4 header, otherwise a V3 header. IQuantizer quantizer = null, ImageComparer customComparer = null) where TPixel : unmanaged, IPixel <TPixel> { using (Image <TPixel> image = provider.GetImage()) { // There is no alpha in bmp with less then 32 bits per pixels, so the reference image will be made opaque. if (bitsPerPixel != BmpBitsPerPixel.Pixel32) { image.Mutate(c => c.MakeOpaque()); } var encoder = new BmpEncoder { BitsPerPixel = bitsPerPixel, SupportTransparency = supportTransparency, Quantizer = quantizer ?? KnownQuantizers.Octree }; // Does DebugSave & load reference CompareToReferenceInput(): image.VerifyEncoder(provider, "bmp", bitsPerPixel, encoder, customComparer); } }
public void Encode_1Bit_WithV4Header_Works <TPixel>( TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : unmanaged, IPixel <TPixel> { // The Magick Reference Decoder can not decode 1-Bit bitmaps, so only execute this on windows. if (TestEnvironment.IsWindows) { TestBmpEncoderCore(provider, bitsPerPixel, supportTransparency: true); } }
public void Encode_4Bit_WithV4Header_Works <TPixel>( TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : unmanaged, IPixel <TPixel> { // Oddly the difference only happens locally but we'll not test for that. // I suspect the issue is with the reference codec. ImageComparer comparer = TestEnvironment.IsFramework ? ImageComparer.TolerantPercentage(0.0161F) : ImageComparer.Exact; TestBmpEncoderCore(provider, bitsPerPixel, supportTransparency: true, customComparer: comparer); }
public void BitmapCanEncodeDifferentBitRates(BmpBitsPerPixel bitsPerPixel) { string path = this.CreateOutputDirectory("Bmp"); foreach (TestFile file in Files) { string filename = file.GetFileNameWithoutExtension(bitsPerPixel); using (Image image = file.CreateImage()) { image.Save($"{path}/{filename}.bmp", new BmpEncoderOptions { BitsPerPixel = bitsPerPixel }); } } }
/// <summary> /// Encodes the image to the specified stream from the <see cref="ImageBase{T,TP}"/>. /// </summary> /// <typeparam name="T">The pixel format.</typeparam> /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam> /// <param name="image">The <see cref="ImageBase{T,TP}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> /// <param name="bitsPerPixel">The <see cref="BmpBitsPerPixel"/></param> public void Encode <T, TP>(ImageBase <T, TP> image, Stream stream, BmpBitsPerPixel bitsPerPixel) where T : IPackedVector <TP> where TP : struct { Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); this.bmpBitsPerPixel = bitsPerPixel; // Cast to int will get the bytes per pixel short bpp = (short)(8 * (int)bitsPerPixel); int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); this.padding = bytesPerLine - (image.Width * (int)bitsPerPixel); // Do not use IDisposable pattern here as we want to preserve the stream. EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream); BmpInfoHeader infoHeader = new BmpInfoHeader { HeaderSize = BmpInfoHeader.Size, Height = image.Height, Width = image.Width, BitsPerPixel = bpp, Planes = 1, ImageSize = image.Height * bytesPerLine, ClrUsed = 0, ClrImportant = 0 }; BmpFileHeader fileHeader = new BmpFileHeader { Type = 19778, // BM Offset = 54, FileSize = 54 + infoHeader.ImageSize }; WriteHeader(writer, fileHeader); this.WriteInfo(writer, infoHeader); this.WriteImage(writer, image); writer.Flush(); }
public void Encode(ImageBase image, Stream stream, BmpBitsPerPixel bitsPerPixel) { if (image == null || stream == null) { throw new ArgumentNullException(); } bmpBitsPerPixel = bitsPerPixel; int rowWidth = image.Width; // TODO: Check this for varying file formats. int amount = (image.Width * (int)bmpBitsPerPixel) % 4; if (amount != 0) { rowWidth += 4 - amount; } // Do not use IDisposable pattern here as we want to preserve the stream. EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream); int bpp = (int)bmpBitsPerPixel; BmpFileHeader fileHeader = new BmpFileHeader { Type = 19778, // BM Offset = 54, FileSize = 54 + image.Height * rowWidth * bpp }; BmpInfoHeader infoHeader = new BmpInfoHeader { HeaderSize = 40, Height = image.Height, Width = image.Width, BitsPerPixel = (short)(8 * bpp), Planes = 1, ImageSize = image.Height * rowWidth * bpp, ClrUsed = 0, ClrImportant = 0 }; WriteHeader(writer, fileHeader); WriteInfo(writer, infoHeader); WriteImage(writer, image); writer.Flush(); }
public void Encode_PreserveBitsPerPixel(string imagePath, BmpBitsPerPixel bmpBitsPerPixel) { var options = new BmpEncoder(); var testFile = TestFile.Create(imagePath); using (Image <Rgba32> input = testFile.CreateRgba32Image()) { using (var memStream = new MemoryStream()) { input.Save(memStream, options); memStream.Position = 0; using (var output = Image.Load <Rgba32>(memStream)) { BmpMetadata meta = output.Metadata.GetBmpMetadata(); Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); } } } }
public void Encode(ImageBase image, Stream stream, BmpBitsPerPixel bitsPerPixel) { if (image == null || stream == null){ throw new ArgumentNullException(); } bmpBitsPerPixel = bitsPerPixel; int rowWidth = image.Width; // TODO: Check this for varying file formats. int amount = (image.Width*(int) bmpBitsPerPixel)%4; if (amount != 0){ rowWidth += 4 - amount; } // Do not use IDisposable pattern here as we want to preserve the stream. EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream); int bpp = (int) bmpBitsPerPixel; BmpFileHeader fileHeader = new BmpFileHeader{ Type = 19778, // BM Offset = 54, FileSize = 54 + image.Height*rowWidth*bpp }; BmpInfoHeader infoHeader = new BmpInfoHeader{ HeaderSize = 40, Height = image.Height, Width = image.Width, BitsPerPixel = (short) (8*bpp), Planes = 1, ImageSize = image.Height*rowWidth*bpp, ClrUsed = 0, ClrImportant = 0 }; WriteHeader(writer, fileHeader); WriteInfo(writer, infoHeader); WriteImage(writer, image); writer.Flush(); }
private static void TestBmpEncoderCore <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : struct, IPixel <TPixel> { using (Image <TPixel> image = provider.GetImage()) { // there is no alpha in bmp! image.Mutate(c => c.MakeOpaque()); var encoder = new BmpEncoder { BitsPerPixel = bitsPerPixel }; // Does DebugSave & load reference CompareToReferenceInput(): image.VerifyEncoder(provider, "bmp", bitsPerPixel, encoder); } }
public void Encode_IsNotBoundToSinglePixelType <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : struct, IPixel <TPixel> { TestBmpEncoderCore(provider, bitsPerPixel); }
public void Encode_WorksWithDifferentSizes <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : struct, IPixel <TPixel> { TestBmpEncoderCore(provider, bitsPerPixel); }
public void Encode_32Bit_WithV3Header_Works <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) // If supportTransparency is false, a v3 bitmap header will be written. where TPixel : unmanaged, IPixel <TPixel> => TestBmpEncoderCore(provider, bitsPerPixel, supportTransparency : false);
/// <summary> /// Initializes a new instance of the <see cref="BmpEncoderCore"/> class. /// </summary> /// <param name="options">The encoder options</param> /// <param name="memoryManager">The memory manager</param> public BmpEncoderCore(IBmpEncoderOptions options, MemoryManager memoryManager) { this.memoryManager = memoryManager; this.bitsPerPixel = options.BitsPerPixel; }
/// <summary> /// Initializes a new instance of the <see cref="BmpEncoderCore"/> class. /// </summary> /// <param name="options">The encoder options</param> /// <param name="memoryAllocator">The memory manager</param> public BmpEncoderCore(IBmpEncoderOptions options, MemoryAllocator memoryAllocator) { this.memoryAllocator = memoryAllocator; this.bitsPerPixel = options.BitsPerPixel; }
/// <summary> /// Initializes a new instance of the <see cref="BmpEncoderCore"/> class. /// </summary> /// <param name="options">The encoder options</param> public BmpEncoderCore(IBmpEncoderOptions options) { this.bitsPerPixel = options.BitsPerPixel; }
public void Encode_8BitGray_WithV4Header_Works <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : unmanaged, IPixel <TPixel> => TestBmpEncoderCore( provider, bitsPerPixel, supportTransparency : true);
public void Encode_PreservesAlpha <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : unmanaged, IPixel <TPixel> => TestBmpEncoderCore(provider, bitsPerPixel, supportTransparency : true);
public void Encode_WorksWithDiscontiguousBuffers <TPixel>(TestImageProvider <TPixel> provider, BmpBitsPerPixel bitsPerPixel) where TPixel : unmanaged, IPixel <TPixel> { provider.LimitAllocatorBufferCapacity().InBytesSqrt(100); TestBmpEncoderCore(provider, bitsPerPixel); }