📷 A modern, cross-platform, 2D Graphics library for .NET
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.
 
 

179 lines
6.4 KiB

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormatUtilities.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Utility methods for working with supported image formats.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging.Formats
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ImageProcessor.Configuration;
/// <summary>
/// Utility methods for working with supported image formats.
/// </summary>
public static class FormatUtilities
{
/// <summary>
/// Gets the correct <see cref="ISupportedImageFormat"/> from the given stream.
/// <see href="http://stackoverflow.com/questions/55869/determine-file-type-of-an-image"/>
/// </summary>
/// <param name="stream">
/// The <see cref="System.IO.Stream"/> to read from.
/// </param>
/// <returns>
/// The <see cref="ISupportedImageFormat"/>.
/// </returns>
public static ISupportedImageFormat GetFormat(Stream stream)
{
// Reset the position of the stream to ensure we're reading the correct part.
stream.Position = 0;
IEnumerable<ISupportedImageFormat> supportedImageFormats =
ImageProcessorBootstrapper.Instance.SupportedImageFormats;
byte[] buffer = new byte[4];
stream.Read(buffer, 0, buffer.Length);
foreach (ISupportedImageFormat supportedImageFormat in supportedImageFormats)
{
byte[][] headers = supportedImageFormat.FileHeaders;
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (byte[] header in headers)
{
if (header.SequenceEqual(buffer.Take(header.Length)))
{
stream.Position = 0;
// Return a new instance as we want to use instance properties.
return Activator.CreateInstance(supportedImageFormat.GetType()) as ISupportedImageFormat;
}
}
}
stream.Position = 0;
return null;
}
/// <summary>
/// Returns a value indicating whether the given image is indexed.
/// </summary>
/// <param name="image">
/// The <see cref="System.Drawing.Image"/> to test.
/// </param>
/// <returns>
/// The true if the image is indexed; otherwise, false.
/// </returns>
public static bool IsIndexed(Image image)
{
// Test value of flags using bitwise AND.
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
return (image.PixelFormat & PixelFormat.Indexed) != 0;
}
/// <summary>
/// Returns the color count from the palette of the given image.
/// </summary>
/// <param name="image">
/// The <see cref="System.Drawing.Image"/> to get the colors from.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the color count.
/// </returns>
public static int GetColorCount(Image image)
{
ConcurrentDictionary<Color, Color> colors = new ConcurrentDictionary<Color, Color>();
int width = image.Width;
int height = image.Height;
using (FastBitmap fastBitmap = new FastBitmap(image))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable once AccessToDisposedClosure
Color color = fastBitmap.GetPixel(x, y);
colors.TryAdd(color, color);
}
});
}
int count = colors.Count;
colors.Clear();
return count;
}
/// <summary>
/// Returns a value indicating whether the given image is indexed.
/// </summary>
/// <param name="image">
/// The <see cref="System.Drawing.Image"/> to test.
/// </param>
/// <returns>
/// The true if the image is animated; otherwise, false.
/// </returns>
public static bool IsAnimated(Image image)
{
return ImageAnimator.CanAnimate(image);
}
/// <summary>
/// Returns an instance of EncodingParameters for jpeg compression.
/// </summary>
/// <param name="quality">The quality to return the image at.</param>
/// <returns>The encodingParameters for jpeg compression. </returns>
public static EncoderParameters GetEncodingParameters(int quality)
{
EncoderParameters encoderParameters = null;
try
{
// Create a series of encoder parameters.
encoderParameters = new EncoderParameters(1);
// Set the quality.
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
}
catch
{
if (encoderParameters != null)
{
encoderParameters.Dispose();
}
}
return encoderParameters;
}
/// <summary>
/// Uses reflection to allow the creation of an instance of <see cref="PropertyItem"/>.
/// </summary>
/// <returns>
/// The <see cref="PropertyItem"/>.
/// </returns>
public static PropertyItem CreatePropertyItem()
{
Type type = typeof(PropertyItem);
ConstructorInfo constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, new Type[] { }, null);
return (PropertyItem)constructor.Invoke(null);
}
}
}