Ejemplo n.º 1
0
 /// <summary>
 /// Changes DDS surface format to specified format.
 /// </summary>
 /// <param name="surface">Desired DDS surface format.</param>
 private void ChangeSurface(CompressedDataFormat surface)
 {
     // KFreon: Change surface format of DDS's
     if (surface != CompressedDataFormat.None)
     {
         IL2.Settings.SetDXTcFormat(surface);
     }
 }
Ejemplo n.º 2
0
 public bool ConvertToDxtc(CompressedDataFormat compressedFormat)
 {
     if (!Image.CheckValid(this))
     {
         return(false);
     }
     this.Bind();
     return(IL.ImageToDxtcData(compressedFormat));
 }
Ejemplo n.º 3
0
        public static unsafe bool SetTexImageDxtc(int width, int height, int depth, CompressedDataFormat format, byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return(false);

                fixed(byte *numPtr = data)
                return(IL.ilTexImageDxtc(width, height, depth, (uint)format, new IntPtr((void *)numPtr)));
        }
Ejemplo n.º 4
0
        public static unsafe byte[] GetDxtcData(CompressedDataFormat dxtcFormat)
        {
            uint dxtcData1 = IL.ilGetDXTCData(IntPtr.Zero, 0U, (uint)dxtcFormat);

            if ((int)dxtcData1 == 0)
            {
                return((byte[])null);
            }
            byte[] numArray = new byte[dxtcData1];
            fixed(byte *numPtr = numArray)
            {
                int dxtcData2 = (int)IL.ilGetDXTCData(new IntPtr((void *)numPtr), dxtcData1, (uint)dxtcFormat);
            }

            return(numArray);
        }
Ejemplo n.º 5
0
        private SurfaceFormat GetSurfaceFormat(ImageData image)
        {
            DataType             dataType   = image.DataType;
            DataFormat           dataFormat = image.Format;
            CompressedDataFormat dxtcFormat = image.CompressedFormat;

            if (image.HasCompressedData)
            {
                switch (dxtcFormat)
                {
                case CompressedDataFormat.DXT1:
                    return(SurfaceFormat.DXT1);

                case CompressedDataFormat.DXT3:
                    return(SurfaceFormat.DXT3);

                case CompressedDataFormat.DXT5:
                    return(SurfaceFormat.DXT5);
                }
            }

            return(SurfaceFormat.Color);
        }
Ejemplo n.º 6
0
 public override bool ConvertAndSave(ImageType type, string savePath, ResILImageBase.MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
 {
     using (FileStream fs = new FileStream(savePath, FileMode.CreateNew))
         return(ConvertAndSave(type, fs, MipsMode, surface, quality, SetJPGQuality));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Converts image to different types and saves to stream. Returns true if successful.
        /// </summary>
        /// <param name="type">Desired image type.</param>
        /// <param name="stream">Stream to save to. Contains data of image file, NOT raw pixel data.</param>
        /// <param name="surface">Surface format. ONLY valid when type is DDS.</param>
        /// <param name="quality">JPG quality. ONLY valid when tpye is JPG.</param>
        /// <param name="SetJPGQuality">Sets JPG output quality if true.</param>
        public override bool ConvertAndSave(ImageType type, Stream stream, MipMapMode MipsMode = MipMapMode.None, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
        {
            if (SetJPGQuality && type == ImageType.Jpg)
            {
                ResIL.Settings.SetJPGQuality(quality);
            }

            if (surface == CompressedDataFormat.V8U8)
            {
                byte[] imgdata = ToArray();

                if (imgdata == null)
                {
                    return(false);
                }

                byte[] rawdata = null;
                using (MemoryTributary test = new MemoryTributary(imgdata))
                {
                    var frame  = BitmapFrame.Create(test);
                    int stride = (Width * 32 + 7) / 8;
                    rawdata = new byte[stride * 1024];
                    frame.CopyPixels(rawdata, stride, 0);
                }

                using (V8U8Image img = new V8U8Image(rawdata, Width, Height, BitsPerPixel))
                {
                    return(img.ConvertAndSave(type, stream, MipsMode, surface, quality, SetJPGQuality));
                }
            }
            else
            {
                bool mipsOperationSuccess = true;
                switch (MipsMode)
                {
                case MipMapMode.BuildAll:
                    mipsOperationSuccess = BuildMipMaps();
                    break;

                case MipMapMode.Rebuild:
                    mipsOperationSuccess = BuildMipMaps(true);
                    break;

                case MipMapMode.RemoveAllButOne:
                    mipsOperationSuccess = RemoveMipMaps();
                    break;

                case MipMapMode.ForceRemove:
                    mipsOperationSuccess = RemoveMipMaps(true);
                    break;
                }

                if (!mipsOperationSuccess)
                {
                    Console.WriteLine("Failed to build mips for image.");
                }


                ChangeSurface(surface);
                return(IL2.SaveImageAsStream(handle, type, stream));
            }
        }
Ejemplo n.º 8
0
        public override bool ConvertAndSave(ImageType type, Stream stream, ResILImageBase.MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
        {
            // KFreon: If converting to something other than V8U8...
            if (surface != SurfaceFormat)
            {
                byte[] RawImageData = GetImageDataAs3Channel(); // KFreon: Get image data as raw rgb pixels
                int stride = (Width * 32 + 7) / 8;
                BitmapSource test = BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Bgr32, BitmapPalettes.Halftone125, RawImageData, stride);

                MemoryTributary stream2 = new MemoryTributary();
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(test));
                encoder.Save(stream2);

                using (ResILImage img = new ResILImage(stream2))
                    return img.ConvertAndSave(type, stream, MipsMode, surface, quality, SetJPGQuality);
            }
            else
            {
                // KFreon: Deal with mips first
                int expectedMips = EstimateNumMips(Width, Height);
                bool success = true;
                switch (MipsMode)
                {
                    case MipMapMode.BuildAll:
                        if (expectedMips != Mips)
                            success = BuildMipMaps();
                        break;
                    case MipMapMode.Rebuild:
                        // KFreon: Remove existing mips before building them again
                        if (!RemoveMipMaps())
                            success = false;
                        else
                            success = BuildMipMaps();
                        break;
                    case MipMapMode.ForceRemove:
                    case MipMapMode.RemoveAllButOne:
                        success = RemoveMipMaps();
                        break;
                }

                if (!success)
                {
                    Debug.WriteLine("Failed to fix mipmaps.");
                    return false;
                }

                // KFreon: Build formatting and write out to file
                return WriteV8U8ToStream(MipMaps, stream, Height, Width, Mips, false);
            }
        }
Ejemplo n.º 9
0
 public abstract bool ConvertAndSave(ImageType type, Stream stream, MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true);
Ejemplo n.º 10
0
 /// <summary>
 /// Sets DXT format in ResIL. Note this is NOT threadsafe yet, so saving MUST be single threaded.
 /// </summary>
 /// <param name="surface">Surface format to save in.</param>
 public static void SetDXTcFormat(CompressedDataFormat surface)
 {
     il2SetInteger((uint)ILDefines.IL_DXTC_FORMAT, (uint)surface);
 }
Ejemplo n.º 11
0
Archivo: IL.cs Proyecto: Kolky/open3mod
 public static void SetDxtcFormat(CompressedDataFormat format) {
     ilSetInteger((uint) ILDefines.IL_DXTC_FORMAT, (int) format);
 }
Ejemplo n.º 12
0
Archivo: IL.cs Proyecto: Kolky/open3mod
        public static byte[] GetDxtcData(CompressedDataFormat dxtcFormat) {
            uint bufferSize = ilGetDXTCData(IntPtr.Zero, 0, (uint) dxtcFormat);
            if(bufferSize == 0) {
                return null;
            }
            byte[] buffer = new byte[bufferSize];

            unsafe {
                fixed(byte* ptr = buffer) {
                    ilGetDXTCData(new IntPtr(ptr), bufferSize, (uint) dxtcFormat);
                }
            }
            return buffer;
        }
Ejemplo n.º 13
0
 public static bool SurfaceToDxtcData(CompressedDataFormat format)
 {
     return(IL.ilSurfaceToDxtcData((uint)format));
 }
Ejemplo n.º 14
0
 public static void SetDxtcFormat(CompressedDataFormat format)
 {
     IL.ilSetInteger(1797U, (int)format);
 }
Ejemplo n.º 15
0
        public override bool ConvertAndSave(ImageType type, Stream stream, ResILImageBase.MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
        {
            // KFreon: If converting to something other than V8U8...
            if (surface != SurfaceFormat)
            {
                byte[]       RawImageData = GetImageDataAs3Channel(); // KFreon: Get image data as raw rgb pixels
                int          stride       = (Width * 32 + 7) / 8;
                BitmapSource test         = BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Bgr32, BitmapPalettes.Halftone125, RawImageData, stride);

                MemoryTributary   stream2 = new MemoryTributary();
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(test));
                encoder.Save(stream2);

                using (ResILImage img = new ResILImage(stream2))
                    return(img.ConvertAndSave(type, stream, MipsMode, surface, quality, SetJPGQuality));
            }
            else
            {
                // KFreon: Deal with mips first
                int  expectedMips = EstimateNumMips(Width, Height);
                bool success      = true;
                switch (MipsMode)
                {
                case MipMapMode.BuildAll:
                    if (expectedMips != Mips)
                    {
                        success = BuildMipMaps();
                    }
                    break;

                case MipMapMode.Rebuild:
                    // KFreon: Remove existing mips before building them again
                    if (!RemoveMipMaps())
                    {
                        success = false;
                    }
                    else
                    {
                        success = BuildMipMaps();
                    }
                    break;

                case MipMapMode.ForceRemove:
                case MipMapMode.RemoveAllButOne:
                    success = RemoveMipMaps();
                    break;
                }

                if (!success)
                {
                    Debug.WriteLine("Failed to fix mipmaps.");
                    return(false);
                }

                // KFreon: Build formatting and write out to file
                return(WriteV8U8ToStream(MipMaps, stream, Height, Width, Mips, false));
            }
        }
Ejemplo n.º 16
0
Archivo: IL.cs Proyecto: Kolky/open3mod
 /// <summary>
 /// Converts the currently bound surface (image, mipmap, etc) to the specified compressed format.
 /// </summary>
 /// <param name="format">Comrpessed format</param>
 /// <returns>True if the operation was successful or not.</returns>
 public static bool SurfaceToDxtcData(CompressedDataFormat format) {
     return ilSurfaceToDxtcData((uint) format);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Sets DXT format in ResIL. Note this is NOT threadsafe yet, so saving MUST be single threaded.
 /// </summary>
 /// <param name="surface">Surface format to save in.</param>
 public static void SetDXTcFormat(CompressedDataFormat surface)
 {
     il2SetInteger((uint)ILDefines.IL_DXTC_FORMAT, (uint)surface);
 }
Ejemplo n.º 18
0
Archivo: IL.cs Proyecto: Kolky/open3mod
 /// <summary>
 /// Resets the currently bounded image with the new parameters. This destroys all existing data.
 /// </summary>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="depth"></param>
 /// <param name="format"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static bool SetTexImageDxtc(int width, int height, int depth, CompressedDataFormat format, byte[] data) {
     if(data == null || data.Length == 0)
         return false;
     unsafe {
         fixed(byte* ptr = data) {
             return ilTexImageDxtc(width, height, depth, (uint) format, new IntPtr(ptr));
         }
     }
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Sets DXTC surface format globally in ResIL.
 /// </summary>
 /// <param name="format">Surface format to set.</param>
 public static void SetDXTCFormat(CompressedDataFormat format)
 {
     IL2.Settings.SetDXTcFormat(format);
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Reads ILImage struct information and populates relevent fields.
        /// Recursively (ew...) counts mips. Returns number of mips.
        /// </summary>
        /// <param name="BasePointer">Pointer to ILImage struct.</param>
        /// <param name="toplevel">True only when first called. All recursive stuff is False.</param>
        /// <returns>Number of mipmaps.</returns>
        private unsafe int CountMipsAndGetDetails(uint *BasePointer, bool toplevel = true)
        {
            int mipcount = 1;

            // KFreon: Read image dimensions
            int W     = (int)*BasePointer++;
            int H     = (int)*BasePointer++;
            int Depth = (int)*BasePointer++;


            // KFreon: Go to byte* land.
            byte *bytePtr        = (byte *)BasePointer;
            uint  BPP            = *bytePtr++;
            int   BitsPerChannel = *bytePtr++;

            bytePtr += 2;  // KFreon: Fix alignment

            // KFreon: Go back to uint* land.
            BasePointer = (uint *)bytePtr;
            uint BitsPerPScanLine = (uint)*BasePointer++;

            if (IntPtr.Size == 8)  // KFreon: x64 alignment?
            {
                BasePointer++;
            }

            // KFreon: Round and round the mulberry bush...
            uint *dataPtr = (uint *)(BasePointer += (IntPtr.Size / 4));

            int            datasize  = (int)*BasePointer++;
            int            PlaneSize = (int)*BasePointer++;
            DataFormat     memform   = (DataFormat)(*BasePointer++);
            DataType       datatyp   = (DataType)(*BasePointer++);
            OriginLocation origin    = (OriginLocation)(*BasePointer++);

            if (IntPtr.Size == 8)  // KFreon: x64 alignment?
            {
                BasePointer++;
            }

            // KFreon: Skip palette
            BasePointer += 7;     // x86 = 1C, x64 = 28

            if (IntPtr.Size == 8) // KFreon: Alignment or no idea...
            {
                BasePointer += 3;
            }

            int         duration  = (int)*(BasePointer++);
            CubeMapFace cubeflags = (CubeMapFace)(*BasePointer++);


            #region CHECK MIPS
            long *th = (long *)BasePointer;
            th = (long *)*th;

            // KFreon: If there's a mip left, read it and increment mip count recursivly (Ugh...)
            if (th != (uint *)0)
            {
                mipcount += CountMipsAndGetDetails((uint *)th, false);
            }
            #endregion

            BasePointer++;        // KFreon: Alignment?

            if (IntPtr.Size == 8) // KFreon: x64 alignment?
            {
                BasePointer++;
            }

            int Next        = (int)*BasePointer++;
            int Faces       = (int)*BasePointer++;
            int Layers      = (int)*BasePointer++;
            int animlist    = (int)*BasePointer++;
            int animsize    = (int)*BasePointer++;
            int profile     = (int)*BasePointer++;
            int profilesize = (int)*BasePointer++;
            int offx        = (int)*BasePointer++;
            int offy        = (int)*BasePointer++;


            // KFreon: Imma byte this pointer
            bytePtr = (byte *)BasePointer;
            int dxtcdataPtr = (int)*bytePtr++;
            bytePtr += 3; // KFreon: Alignment


            // KFreon: Uint gonna byte me!
            BasePointer = (uint *)bytePtr;

            if (IntPtr.Size == 8)  // KFreon: uhh....
            {
                BasePointer += 8;
            }

            CompressedDataFormat surface = (CompressedDataFormat)(*BasePointer++);
            int dxtcsize = (int)*BasePointer++;

            // KFreon: Set original image properties
            if (toplevel)
            {
                Width         = W;
                Height        = H;
                BitsPerPixel  = (int)BPP;
                Channels      = BitsPerChannel;
                DataSize      = datasize;
                MemoryFormat  = memform;
                DataType      = datatyp;
                SurfaceFormat = surface;
            }

            return(mipcount);
        }
Ejemplo n.º 21
0
 public override bool ConvertAndSave(ImageType type, string savePath, ResILImageBase.MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
 {
     using (FileStream fs = new FileStream(savePath, FileMode.CreateNew))
         return ConvertAndSave(type, fs, MipsMode, surface, quality, SetJPGQuality);
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Convert image to different types and save to path. Returns true if successful.
        /// </summary>
        /// <param name="type">Type of image to save as.</param>
        /// <param name="savePath">Path to save to.</param>
        /// <param name="surface">DDS Surface format to change to. Valid only if type is DDS.</param>
        /// <returns>True if success.</returns>
        public override bool ConvertAndSave(ImageType type, string savePath, MipMapMode MipsMode = MipMapMode.None, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
        {
            /*if (SetJPGQuality && type == ImageType.Jpg)
             *  ResIL.Settings.SetJPGQuality(quality);
             *
             * bool mipsOperationSuccess = true;
             * switch (MipsMode)
             * {
             *  case MipMapMode.BuildAll:
             *      mipsOperationSuccess = BuildMipMaps();
             *      break;
             *  case MipMapMode.Rebuild:
             *      mipsOperationSuccess = BuildMipMaps(true);
             *      break;
             *  case MipMapMode.RemoveAllButOne:
             *      mipsOperationSuccess = RemoveMipMaps();
             *      break;
             *  case MipMapMode.ForceRemove:
             *      mipsOperationSuccess = RemoveMipMaps(true);
             *      break;
             * }
             *
             * if (!mipsOperationSuccess)
             *  Console.WriteLine("Failed to build mips for {0}", savePath);
             *
             * ChangeSurface(surface);
             * //IL2.SaveImage(handle, savePath + ".dds", type);
             * return IL2.SaveImage(handle, savePath, type);*/

            using (FileStream fs = new FileStream(savePath, FileMode.CreateNew))
                return(ConvertAndSave(type, fs, MipsMode, surface, quality, SetJPGQuality));
        }
Ejemplo n.º 23
0
 public abstract bool ConvertAndSave(ImageType type, Stream stream, MipMapMode MipsMode   = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true);
Ejemplo n.º 24
0
 /// <summary>
 /// Sets DXTC surface format globally in ResIL.
 /// </summary>
 /// <param name="format">Surface format to set.</param>
 public static void SetDXTCFormat(CompressedDataFormat format)
 {
     IL2.Settings.SetDXTcFormat(format);
 }