Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ImageSharp/Formats/Bmp/BmpDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <list type="bullet">
/// <item>JPG</item>
/// <item>PNG</item>
/// <item>RLE4</item>
/// <item>Some OS/2 specific subtypes like: Bitmap Array, Color Icon, Color Pointer, Icon, Pointer.</item>
/// </list>
/// Formats will be supported in a later releases. We advise always
/// to use only 24 Bit Windows bitmaps.
/// </remarks>
public sealed class BmpDecoder : IImageDecoder, IBmpDecoderOptions, IImageInfoDetector
{
/// <summary>
/// Gets or sets a value indicating how to deal with skipped pixels, which can occur during decoding run length encoded bitmaps.
/// </summary>
public RleSkippedPixelHandling RleSkippedPixelHandling { get; set; } = RleSkippedPixelHandling.Black;

/// <inheritdoc/>
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>
Expand Down
240 changes: 191 additions & 49 deletions src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ internal sealed class BmpEncoderCore
/// </summary>
private const int ColorPaletteSize8Bit = 1024;

/// <summary>
/// Used for allocating memory during processing operations.
/// </summary>
private readonly MemoryAllocator memoryAllocator;

/// <summary>
/// The global configuration.
/// </summary>
private Configuration configuration;

/// <summary>
/// The color depth, in number of bits per pixel.
/// </summary>
private BmpBitsPerPixel? bitsPerPixel;

/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions src/ImageSharp/Formats/Bmp/BmpImageFormatDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.Buffers.Binary;

namespace SixLabors.ImageSharp.Formats.Bmp
{
Expand All @@ -21,10 +22,7 @@ public IImageFormat DetectFormat(ReadOnlySpan<byte> header)

private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
{
// TODO: This should be in constants
return header.Length >= this.HeaderSize
&& header[0] == 0x42 // B
&& header[1] == 0x4D; // M
return header.Length >= this.HeaderSize && BinaryPrimitives.ReadInt16LittleEndian(header) == BmpConstants.TypeMarkers.Bitmap;
}
}
}
5 changes: 4 additions & 1 deletion src/ImageSharp/Formats/Bmp/IBmpDecoderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// </summary>
internal interface IBmpDecoderOptions
{
// added this for consistency so we can add stuff as required, no options currently available
/// <summary>
/// Gets the value indicating how to deal with skipped pixels, which can occur during decoding run length encoded bitmaps.
/// </summary>
RleSkippedPixelHandling RleSkippedPixelHandling { get; }
}
}
26 changes: 26 additions & 0 deletions src/ImageSharp/Formats/Bmp/RleSkippedPixelHandling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.ImageSharp.Formats.Bmp
{
/// <summary>
/// Defines possible options, how skipped pixels during decoding of run length encoded bitmaps should be treated.
/// </summary>
public enum RleSkippedPixelHandling : int
{
/// <summary>
/// Undefined pixels should be black. This is the default behavior and equal to how System.Drawing handles undefined pixels.
/// </summary>
Black = 0,

/// <summary>
/// Undefined pixels should be transparent.
/// </summary>
Transparent = 1,

/// <summary>
/// Undefined pixels should have the first color of the palette.
/// </summary>
FirstColorOfPalette = 2
}
}
2 changes: 1 addition & 1 deletion tests/ImageSharp.Tests/FileTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class FileTestBase
/// <summary>
/// A collection of all the bmp test images
/// </summary>
public static IEnumerable<string> AllBmpFiles = TestImages.Bmp.All;
public static IEnumerable<string> AllBmpFiles = TestImages.Bmp.Benchmark;

/// <summary>
/// A collection of all the jpeg test images
Expand Down
Loading