//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Formats
{
using System.Collections.Generic;
///
/// Encapsulates the means to encode and decode jpeg images.
///
public class JpegFormat : IImageFormat
{
///
public string MimeType => "image/jpeg";
///
public string Extension => "jpg";
///
public IEnumerable SupportedExtensions => new string[] { "jpg", "jpeg", "jfif" };
///
public IImageDecoder Decoder => new JpegDecoder();
///
public IImageEncoder Encoder => new JpegEncoder();
///
public int HeaderSize => 11;
///
public bool IsSupportedFileFormat(byte[] header)
{
return header.Length >= this.HeaderSize &&
(IsJfif(header) || IsExif(header) || IsJpeg(header));
}
///
/// Returns a value indicating whether the given bytes identify Jfif data.
///
/// The bytes representing the file header.
/// The
private static bool IsJfif(byte[] header)
{
bool isJfif =
header[6] == 0x4A && // J
header[7] == 0x46 && // F
header[8] == 0x49 && // I
header[9] == 0x46 && // F
header[10] == 0x00;
return isJfif;
}
///
/// Returns a value indicating whether the given bytes identify EXIF data.
///
/// The bytes representing the file header.
/// The
private static bool IsExif(byte[] header)
{
bool isExif =
header[6] == 0x45 && // E
header[7] == 0x78 && // X
header[8] == 0x69 && // I
header[9] == 0x66 && // F
header[10] == 0x00;
return isExif;
}
///
/// Returns a value indicating whether the given bytes identify Jpeg data.
/// This is a last chance resort for jpegs that contain ICC information.
///
/// The bytes representing the file header.
/// The
private static bool IsJpeg(byte[] header)
{
bool isJpg =
header[0] == 0xFF && // 255
header[1] == 0xD8; // 216
return isJpg;
}
}
}