<commit_before>using System;
using System.IO;
using Axiom.Core;
using Axiom.Exceptions;

namespace Axiom.Media {
	/// <summary>
	///    Class representing an image file.
	/// </summary>
	/// <remarks>
    ///    The Image class usually holds uncompressed image data and is the
    ///    only object that can be loaded in a texture. Image objects handle 
    ///    image data decoding themselves by the means of locating the correct 
    ///    ICodec implementation for each data type.
	/// </remarks>
	public class Image {
        #region Fields

        /// <summary>
        ///    Byte array containing the image data.
        /// </summary>
        protected byte[] buffer;
        /// <summary>
        ///    Width of the image (in pixels).
        /// </summary>
        protected int width;
        /// <summary>
        ///    Width of the image (in pixels).
        /// </summary>
        protected int height;
        /// <summary>
        ///    Depth of the image
        /// </summary>
        protected int depth;
        /// <summary>
        ///    Size of the image buffer.
        /// </summary>
        protected int size;
        /// <summary>
        ///    Number of mip maps in this image.
        /// </summary>
        protected int numMipMaps;
        /// <summary>
        ///    Additional features on this image.
        /// </summary>
        protected ImageFlags flags;
        /// <summary>
        ///    Image format.
        /// </summary>
        protected PixelFormat format;

        #endregion Fields

        #region Constructors

		public Image() {    
		}

        #endregion Constructors

        #region Methods

        /// <summary>
        ///    Performs gamma adjusment on this image.
        /// </summary>
        /// <remarks>
        ///    Basic algo taken from Titan Engine, copyright (c) 2000 Ignacio 
        ///    <NAME>.
        /// </remarks>
        /// <param name="buffer"></param>
        /// <param name="gamma"></param>
        /// <param name="size"></param>
        /// <param name="bpp"></param>
        public static void ApplyGamma(byte[] buffer, float gamma, int size, int bpp) {
            // TODO: Implement ApplyGamma
        }

        /// <summary>
        ///    Checks the specified image format to determine if it contains an alpha
        ///    component.
        /// </summary>
        /// <param name="format">Pixel format to check.</param>
        /// <returns>True if the pixel format contains an alpha component.</returns>
        public static bool FormatHasAlpha(PixelFormat format) {
            switch(format) {
                case PixelFormat.A8:
                case PixelFormat.A4L4:
                case PixelFormat.L4A4:
                case PixelFormat.A4R4G4B4:
                case PixelFormat.B4G4R4A4:
                case PixelFormat.A8R8G8B8:
                case PixelFormat.B8G8R8A8:
                case PixelFormat.A2R10G10B10:
                case PixelFormat.B10G10R10A2:
                    return true;
            }

            // no alpha
            return false;
        }

        /// <summary>
        ///    Loads an image file from the file system.
        /// </summary>
        /// <param name="fileName">Full path to the image file on disk.</param>
        public static Image FromFile(string fileName) {
            int pos = fileName.LastIndexOf(".");

            if(pos == -1) {
                throw new AxiomException("Unable to load image file {0} - invalid extension.", fileName);
            }

            // grab the extension from the filename
            string ext = fileName.Substring(pos + 1, fileName.Length - pos - 1);

            // find a registered codec for this type
            ICodec codec = CodecManager.Instance.GetCodec(ext);

            // TODO: Need ArchiveManager
            Stream encoded = ResourceManager.FindCommonResourceData(fileName);
            MemoryStream decoded = new MemoryStream();

            // decode the image data
            ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode(encoded, decoded);

            Image image = new Image();

            // copy the image data
            image.height = data.height;
            image.width = data.width;
            image.depth = data.depth;
            image.format = data.format;
            image.flags = data.flags;
            image.numMipMaps = data.numMipMaps;

            // grab the buffer data as an array
            image.buffer = decoded.ToArray();

            return image;
        }

        /// <summary>
        ///    Loads raw image data from memory.
        /// </summary>
        /// <param name="stream">Stream containing the raw image data.</param>
        /// <param name="width">Width of this image data (in pixels).</param>
        /// <param name="height">Height of this image data (in pixels).</param>
        /// <param name="format">Pixel format used in this texture.</param>
        /// <returns>A new instance of Image containing the raw data supplied.</returns>
        public static Image FromRawStream(Stream stream, int width, int height, PixelFormat format) {
            Image image = new Image();

            image.width = width;
            image.height = height;
            image.format = format;
            image.size = width * height * GetNumElemBytes(format);

            // create a new buffer and write the image data directly to it
            image.buffer = new byte[image.size];
            stream.Write(image.buffer, 0, image.size);

            return image;
        }

        /// <summary>
        ///    Loads an image from a stream.
        /// </summary>
        /// <remarks>
        ///    This method allows loading an image from a stream, which is helpful for when
        ///    images are being decompressed from an archive into a stream, which needs to be
        ///    loaded as is.
        /// </remarks><commit_msg>- Fixed data loading bug in FromRawStream.<commit_after>using System;
using System.IO;
using Axiom.Core;
using Axiom.Exceptions;

namespace Axiom.Media {
	/// <summary>
	///    Class representing an image file.
	/// </summary>
	/// <remarks>
    ///    The Image class usually holds uncompressed image data and is the
    ///    only object that can be loaded in a texture. Image objects handle 
    ///    image data decoding themselves by the means of locating the correct 
    ///    ICodec implementation for each data type.
	/// </remarks>
	public class Image {
        #region Fields

        /// <summary>
        ///    Byte array containing the image data.
        /// </summary>
        protected byte[] buffer;
        /// <summary>
        ///    Width of the image (in pixels).
        /// </summary>
        protected int width;
        /// <summary>
        ///    Width of the image (in pixels).
        /// </summary>
        protected int height;
        /// <summary>
        ///    Depth of the image
        /// </summary>
        protected int depth;
        /// <summary>
        ///    Size of the image buffer.
        /// </summary>
        protected int size;
        /// <summary>
        ///    Number of mip maps in this image.
        /// </summary>
        protected int numMipMaps;
        /// <summary>
        ///    Additional features on this image.
        /// </summary>
        protected ImageFlags flags;
        /// <summary>
        ///    Image format.
        /// </summary>
        protected PixelFormat format;

        #endregion Fields

        #region Constructors

		public Image() {    
		}

        #endregion Constructors

        #region Methods

        /// <summary>
        ///    Performs gamma adjusment on this image.
        /// </summary>
        /// <remarks>
        ///    Basic algo taken from Titan Engine, copyright (c) 2000 Ignacio 
        ///    <NAME>.
        /// </remarks>
        /// <param name="buffer"></param>
        /// <param name="gamma"></param>
        /// <param name="size"></param>
        /// <param name="bpp"></param>
        public static void ApplyGamma(byte[] buffer, float gamma, int size, int bpp) {
            // TODO: Implement ApplyGamma
        }

        /// <summary>
        ///    Checks the specified image format to determine if it contains an alpha
        ///    component.
        /// </summary>
        /// <param name="format">Pixel format to check.</param>
        /// <returns>True if the pixel format contains an alpha component.</returns>
        public static bool FormatHasAlpha(PixelFormat format) {
            switch(format) {
                case PixelFormat.A8:
                case PixelFormat.A4L4:
                case PixelFormat.L4A4:
                case PixelFormat.A4R4G4B4:
                case PixelFormat.B4G4R4A4:
                case PixelFormat.A8R8G8B8:
                case PixelFormat.B8G8R8A8:
                case PixelFormat.A2R10G10B10:
                case PixelFormat.B10G10R10A2:
                    return true;
            }

            // no alpha
            return false;
        }

        /// <summary>
        ///    Loads an image file from the file system.
        /// </summary>
        /// <param name="fileName">Full path to the image file on disk.</param>
        public static Image FromFile(string fileName) {
            int pos = fileName.LastIndexOf(".");

            if(pos == -1) {
                throw new AxiomException("Unable to load image file {0} - invalid extension.", fileName);
            }

            // grab the extension from the filename
            string ext = fileName.Substring(pos + 1, fileName.Length - pos - 1);

            // find a registered codec for this type
            ICodec codec = CodecManager.Instance.GetCodec(ext);

            // TODO: Need ArchiveManager
            Stream encoded = ResourceManager.FindCommonResourceData(fileName);
            MemoryStream decoded = new MemoryStream();

            // decode the image data
            ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode(encoded, decoded);

            Image image = new Image();

            // copy the image data
            image.height = data.height;
            image.width = data.width;
            image.depth = data.depth;
            image.format = data.format;
            image.flags = data.flags;
            image.numMipMaps = data.numMipMaps;

            // grab the buffer data as an array
            image.buffer = decoded.ToArray();

            return image;
        }

        /// <summary>
        ///    Loads raw image data from memory.
        /// </summary>
        /// <param name="stream">Stream containing the raw image data.</param>
        /// <param name="width">Width of this image data (in pixels).</param>
        /// <param name="height">Height of this image data (in pixels).</param>
        /// <param name="format">Pixel format used in this texture.</param>
        /// <returns>A new instance of Image containing the raw data supplied.</returns>
        public static Image FromRawStream(Stream stream, int width, int height, PixelFormat format) {
            Image image = new Image();

            image.width = width;
            image.height = height;
            image.format = format;
            image.size = width * height * GetNumElemBytes(format);

            // create a new buffer and write the image data directly to it
            image.buffer = new byte[image.size];
            stream.Read(image.buffer, 0, image.size);

            return image;
        }

        /// <summary>
        ///    Loads an image from a stream.
        /// </summary>
        /// <remarks>
        ///    This method allows loading an image from a stream, which is helpful for when
        ///    images are being decompressed from an archive into a stream, which needs to be
        ///    loaded as is.
        /// </remarks>
{'fraction_non_alphanumeric': 0.1103448275862069, 'fraction_numerical': 0.007401177460050462, 'mean_word_length': 1.5070630402698715, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2331076', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2960, 'n_words': 1011}