//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace BansheeEngine { /** @addtogroup Utility * @{ */ /// /// Utility methods for converting and managing pixel data and formats. /// public static class PixelUtility { /// /// Returns the size of the memory region required to hold pixels of the provided size ana format. /// /// Number of pixels in each row. /// Number of pixels in each column. /// Number of 2D slices. /// Format of individual pixels. /// Size of the memory region in bytes. public static int GetMemorySize(int width, int height, int depth, PixelFormat format) { int value; Internal_GetMemorySize(width, height, depth, format, out value); return value; } /// /// Checks if the provided pixel format has an alpha channel. /// /// Format to check. /// True if the format contains an alpha channel. public static bool HasAlpha(PixelFormat format) { bool value; Internal_HasAlpha(format, out value); return value; } /// /// Checks is the provided pixel format a floating point format. /// /// Format to check. /// True if the format contains floating point values. public static bool IsFloatingPoint(PixelFormat format) { bool value; Internal_IsFloatingPoint(format, out value); return value; } /// /// Checks is the provided pixel format contains compressed data. /// /// Format to check. /// True if the format contains compressed data. public static bool IsCompressed(PixelFormat format) { bool value; Internal_IsCompressed(format, out value); return value; } /// /// Checks is the provided pixel format a depth/stencil buffer format. /// /// Format to check. /// True if the format is a depth/stencil buffer format. public static bool IsDepth(PixelFormat format) { bool value; Internal_IsDepth(format, out value); return value; } /// /// Returns the maximum number of mip maps that can be generated until we reachthe minimum size possible. This does /// not count the base level. /// /// Number of pixels in each row. /// Number of pixels in each column. /// Number of 2D slices. /// Format of individual pixels. /// Possible number of mip-maps not counting the base level. public static int GetMaxMipmaps(int width, int height, int depth, PixelFormat format) { int value; Internal_GetMaxMipmaps(width, height, depth, format, out value); return value; } /// /// Converts a set of pixels from one format to another. /// /// Pixels to convert. /// New pixel format. /// New pixel data object containing the converted pixels. public static PixelData ConvertFormat(PixelData source, PixelFormat newFormat) { return Internal_ConvertFormat(source, newFormat); } /// /// Compresses the provided pixels using the specified compression options. /// /// Pixels to compress. /// Options to control the compression. Make sure the format contained within is a /// compressed format. /// New pixel data object containing the compressed pixels. public static PixelData Compress(PixelData source, CompressionOptions options) { return Internal_Compress(source, ref options); } /// /// Generates mip-maps from the provided source data using the specified compression options. Returned list includes /// the base level. /// /// Pixels to generate mip-maps for. /// Options controlling mip-map generation. /// A list of calculated mip-map data. First entry is the largest mip and other follow in order from /// largest to smallest. public static PixelData[] GenerateMipmaps(PixelData source, MipMapGenOptions options) { return Internal_GenerateMipmaps(source, ref options); } /// /// Scales pixel data in the source buffer and stores the scaled data in the destination buffer. /// /// Source pixels to scale. /// New dimensions to scale to. /// Filter to use when scaling. /// New pixel data object containing the scaled pixels. public static PixelData Scale(PixelData source, PixelVolume newSize, ScaleFilter filter = ScaleFilter.Linear) { return Internal_Scale(source, ref newSize, filter); } /// /// Applies gamma correction to the pixels in the provided buffer. /// /// Source pixels to gamma correct. /// Gamma value to apply. public static void ApplyGamma(PixelData source, float gamma) { Internal_ApplyGamma(source, gamma); } [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_GetMemorySize(int width, int height, int depth, PixelFormat format, out int value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_HasAlpha(PixelFormat format, out bool value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_IsFloatingPoint(PixelFormat format, out bool value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_IsCompressed(PixelFormat format, out bool value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_IsDepth(PixelFormat format, out bool value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_GetMaxMipmaps(int width, int height, int depth, PixelFormat format, out int value); [MethodImpl(MethodImplOptions.InternalCall)] private static extern PixelData Internal_ConvertFormat(PixelData source, PixelFormat newFormat); [MethodImpl(MethodImplOptions.InternalCall)] private static extern PixelData Internal_Compress(PixelData source, ref CompressionOptions options); [MethodImpl(MethodImplOptions.InternalCall)] private static extern PixelData[] Internal_GenerateMipmaps(PixelData source, ref MipMapGenOptions options); [MethodImpl(MethodImplOptions.InternalCall)] private static extern PixelData Internal_Scale(PixelData source, ref PixelVolume newSize, ScaleFilter filter); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_ApplyGamma(PixelData source, float gamma); } /// /// Filtering types to use when scaling images. /// public enum ScaleFilter // Note: Must match the C++ enum PixelUtil::Filter { /// /// No filtering is performed and nearest existing value is used. /// Nearest, /// /// Box filter is applied, averaging nearby pixels. /// Linear }; /// /// Types of texture compression quality. /// public enum CompressionQuality // Note: Must match the C++ enum CompressionQuality { Fastest, Normal, Production, Highest }; /// /// Mode of the alpha channel in a texture. /// public enum AlphaMode // Note: Must match the C++ enum AlphaMode { /// /// Texture has no alpha values. /// None, /// /// Alpha is in the separate transparency channel. /// Transparency, /// /// Alpha values have been pre-multiplied with the color values. /// Premultiplied }; /// /// Wrap mode to use when generating mip maps. /// public enum MipMapWrapMode // Note: Must match the C++ enum MipMapWrapMode { Mirror, Repeat, Clamp }; /// /// Filter to use when generating mip maps. /// public enum MipMapFilter // Note: Must match the C++ enum MipMapFilter { Box, Triangle, Kaiser }; /// /// Options used to control texture compression. /// [StructLayout(LayoutKind.Sequential)] public struct CompressionOptions // Note: Must match the C++ struct CompressionOptions { /// /// Format to compress to. Must be a format containing compressed data. /// public PixelFormat format; /// /// Controls how to (and if) to compress the alpha channel. /// public AlphaMode alphaMode; /// /// Determines does the input data represent a normal map. /// public bool isNormalMap; /// /// Determines has the input data been gamma corrected. /// public bool isSRGB; /// /// Compressed image quality. Better compression might take longer to execute but will generate better results. /// public CompressionQuality quality; }; /// /// Options used to control texture mip map generation. /// [StructLayout(LayoutKind.Sequential)] public struct MipMapGenOptions // Note: Must match the C++ struct MipMapGenOptions { /// /// Filter to use when downsamping input data. /// public MipMapFilter filter; /// /// Determines how to downsample pixels on borders. /// public MipMapWrapMode wrapMode; /// /// Determines does the input data represent a normal map. /// public bool isNormalMap; /// /// Should the downsampled values be re-normalized. Only relevant for mip-maps representing normal maps. /// public bool normalizeMipmaps; /// /// Determines has the input data been gamma corrected. /// bool isSRGB; }; /** @} */ }