<commit_before>﻿using System.Drawing;
using System;
using TeximpNet;
using TeximpNet.DDS;

namespace RelhaxModpack.AtlasesCreator
{
    public class DssImageHandler : IImageHandler
    {
        public string ImageExtension
        {
            get { return "dds"; }
        }

        public Bitmap Load(string filename)
        {
            return DDSReader.DDS.LoadImage(filename);
        }

        public void Save(string filename, Bitmap image)
        {
            /*
            libaries needed to make this code work:
            FreeImage32.dll
            FreeImage64.dll
            nvtt32.dll
            nvtt64.dll
            TeximpNet.dll
            TeximpNet.xml
            */
            System.Drawing.Imaging.BitmapData bmpData = null;

            try
            {
                // Lock the bitmap's bits. 
                Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                bmpData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);

                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;

                Surface surfaceFromRawData = null;
                try
                {
                    surfaceFromRawData = Surface.LoadFromRawData(ptr, image.Width, image.Height, bmpData.Stride, true);
                }
                catch (Exception ex)
                {
                    Utils.ExceptionLog("DssImageExporter", "LoadFromRawData", ex);
                    return;
                }

                if (surfaceFromRawData == null)
                {
                    Logging.Manager("Failed to get surfaceFromRawData");
                    return;
                }
                try
                {
                    DDSFile.Write(filename, surfaceFromRawData, TextureDimension.Two, DDSFlags.None);
                }
                catch (Exception ex)
                {
                    Utils.ExceptionLog("DssImageExporter", "Write", ex);
                }
            }
            finally
            {
                image.UnlockBits(bmpData);
                image.Dispose();
            }
        }

        private static readonly int CHUNK_SIZE = 0x20;
        public Size GetImageSize(string filename)
        {
            byte[] chunk = null;
            Size size = new Size<commit_msg>added DDS export with DXT5 image compressor<commit_after>﻿using System.Drawing;
using System;
using TeximpNet;

namespace RelhaxModpack.AtlasesCreator
{
    public class DssImageHandler : IImageHandler
    {
        public string ImageExtension
        {
            get { return "dds"; }
        }

        public Bitmap Load(string filename)
        {
            return DDSReader.DDS.LoadImage(filename);
        }

        public void Save(string filename, Bitmap image)
        {
            /*
            libaries needed to make this code work:
            FreeImage32.dll
            FreeImage64.dll
            nvtt32.dll
            nvtt64.dll
            TeximpNet.dll
            TeximpNet.xml
            */
            System.Drawing.Imaging.BitmapData bmpData = null;
            Compressor compressor = null;
            try
            {
                // Lock the bitmap's bits. 
                Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                bmpData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);

                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;

                Surface surfaceFromRawData = null;
                try
                {
                    surfaceFromRawData = Surface.LoadFromRawData(ptr, image.Width, image.Height, bmpData.Stride, true);
                }
                catch (Exception ex)
                {
                    Utils.ExceptionLog("DssImageExporter", "LoadFromRawData", ex);
                    return;
                }

                if (surfaceFromRawData == null)
                {
                    Logging.Manager("Failed to get surfaceFromRawData");
                    return;
                }

                try
                {
                    // DDSFile.Write(filename, surfaceFromRawData, TextureDimension.Two, DDSFlags.None);
                    compressor = new Compressor();
                    compressor.Compression.Format = CompressionFormat.DXT5;
                    compressor.Input.AlphaMode = AlphaMode.None;
                    compressor.Input.GenerateMipmaps = false;
                    compressor.Input.ConvertToNormalMap = false;
                    compressor.Input.SetData(surfaceFromRawData);
                }
                catch (Exception ex)
                {
                    Utils.ExceptionLog("DssImageExporter", "Write", ex);
                }
            }
            finally
            {
                image.UnlockBits(bmpData);
                image.Dispose();
            }
        }

        private static readonly int CHUNK_SIZE = 0x20;
        public Size GetImageSize(string filename)
        {
            byte[] chunk = null;
            Size size = new Size
{'fraction_non_alphanumeric': 0.07229612492770388, 'fraction_numerical': 0.00578368999421631, 'mean_word_length': 1.2381363244176014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4263264', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1238, 'n_words': 278}