mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.2 KiB
87 lines
3.2 KiB
// --------------------------------------------------------------------------------------------------------------------
|
|
// <copyright file="BmpDecoder.cs" company="James South">
|
|
// Copyright (c) James South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
// <summary>
|
|
// Image decoder for generating an image out of a Windows bitmap stream.
|
|
// </summary>
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ImageProcessor.Formats
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
|
|
/// <summary>
|
|
/// Image decoder for generating an image out of a Windows bitmap stream.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Does not support the following formats at the moment:
|
|
/// <list type="bullet">
|
|
/// <item>JPG</item>
|
|
/// <item>PNG</item>
|
|
/// <item>RLE4</item>
|
|
/// <item>RLE8</item>
|
|
/// <item>BitFields</item>
|
|
/// </list>
|
|
/// Formats will be supported in a later releases. We advise always
|
|
/// to use only 24 Bit Windows bitmaps.
|
|
/// </remarks>
|
|
public class BmpDecoder : IImageDecoder
|
|
{
|
|
/// <summary>
|
|
/// Gets the size of the header for this image type.
|
|
/// </summary>
|
|
/// <value>The size of the header.</value>
|
|
public int HeaderSize => 2;
|
|
|
|
/// <summary>
|
|
/// Returns a value indicating whether the <see cref="IImageDecoder"/> supports the specified
|
|
/// file header.
|
|
/// </summary>
|
|
/// <param name="extension">The <see cref="string"/> containing the file extension.</param>
|
|
/// <returns>
|
|
/// True if the decoder supports the file extension; otherwise, false.
|
|
/// </returns>
|
|
public bool IsSupportedFileExtension(string extension)
|
|
{
|
|
Guard.NotNullOrEmpty(extension, "extension");
|
|
|
|
extension = extension.StartsWith(".") ? extension.Substring(1) : extension;
|
|
|
|
return extension.Equals("BMP", StringComparison.OrdinalIgnoreCase)
|
|
|| extension.Equals("DIP", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a value indicating whether the <see cref="IImageDecoder"/> supports the specified
|
|
/// file header.
|
|
/// </summary>
|
|
/// <param name="header">The <see cref="T:byte[]"/> containing the file header.</param>
|
|
/// <returns>
|
|
/// True if the decoder supports the file header; otherwise, false.
|
|
/// </returns>
|
|
public bool IsSupportedFileFormat(byte[] header)
|
|
{
|
|
bool isBmp = false;
|
|
if (header.Length >= 2)
|
|
{
|
|
isBmp = header[0] == 0x42 && // B
|
|
header[1] == 0x4D; // M
|
|
}
|
|
|
|
return isBmp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decodes the image from the specified stream to the <see cref="ImageBase"/>.
|
|
/// </summary>
|
|
/// <param name="image">The <see cref="ImageBase"/> to decode to.</param>
|
|
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
|
|
public void Decode(Image image, Stream stream)
|
|
{
|
|
new BmpDecoderCore().Decode(image, stream);
|
|
}
|
|
}
|
|
}
|
|
|