Ejemplo n.º 1
0
        private void Dispose(bool finalising)
        {
            if (handle == IntPtr.Zero)
            {
                Debug.WriteLine("Image already deleted.");
            }
            else
            {
                try
                {
                    // KFreon: Delete image from ResIL
                    if (!IL2.DeleteImage(handle))
                    {
                        Debug.WriteLine("Image already deleted.");
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Failed to delete image from ResIL: " + e.Message);
                }

                // KFreon: Reset file handle
                handle = IntPtr.Zero;

                if (!finalising)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load image from byte[]. Returns true if successful.
        /// </summary>
        /// <param name="data">Data of image file, NOT raw pixel data.</param>
        /// <param name="type">Type of data (format etc jpg, dds, etc)</param>
        private bool LoadImage(byte[] data, ImageType type = ImageType.Bmp)
        {
            IL2.Settings.KeepDXTC(true);
            if (!IL2.LoadImageFromArray(ref handle, data, type))
            {
                Debug.WriteLine("Loading image failed for some reason");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load image from file. Returns true if successful.
        /// </summary>
        /// <param name="FilePath">Path to image file.</param>
        private bool LoadImage(string FilePath)
        {
            IL2.Settings.KeepDXTC(true);
            if (!IL2.LoadImage(ref handle, FilePath))
            {
                Debug.WriteLine("Loading from file failed for some reason.");
                //Debug.WriteLine(GET ERROR FUNCTION);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts instance to array of image data of a given image type.
        /// </summary>
        public override byte[] ToArray()
        {
            byte[] data = null;

            ChangeSurface(SurfaceFormat);
            if (IL2.SaveToArray(handle, ImageType, out data) != 0)
            {
                return(data);
            }
            else
            {
                Debug.WriteLine("To Array failed for some reason.");
            }
            return(null);
        }
Ejemplo n.º 5
0
        private ImageType DetermineType(ImageType type, byte[] imgData)
        {
            // KFreon: Attempt to determine type unless provided
            ImageType GivenType = type;

            if (type == ImageType.Bmp || type == ImageType.Unknown)
            {
                GivenType = IL2.DetermineImageType(imgData);
            }

            if (GivenType == ImageType.Unknown)
            {
                GivenType = type;
            }

            return(GivenType);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts this image to WPF bitmap. Returns null on failure.
        /// </summary>
        /// <param name="type">Type of image to create.</param>
        /// <param name="quality">Quality of JPG image. Valid only if type is JPG. Range 0-100.</param>
        public override BitmapImage ToImage(ImageType type = ImageType.Jpg, int quality = 80, int width = 0, int height = 0)
        {
            byte[] data = null;

            // KFreon: Set JPG quality if necessary
            if (type == ImageType.Jpg)
            {
                ResIL.Settings.SetJPGQuality(quality);
            }

            // KFreon: Save to array and build WPF bitmap.
            if (IL2.SaveToArray(handle, type, out data) != 0)
            {
                return(UsefulThings.WPF.Images.CreateWPFBitmap(data, width, height));
            }
            else
            {
                Debug.WriteLine("Saving to array failed for some reason.");
                Debug.WriteLine(Enum.GetName(typeof(ErrorType), IL2.GetError()));
                return(null);
            }
        }
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
 /// <summary>
 /// Load image from MemoryStream. Returns true if successful.
 /// </summary>
 /// <param name="data">Data of image file, NOT raw pixel data.</param>asf
 private bool LoadImage(Stream data)
 {
     IL2.Settings.KeepDXTC(true);
     return(IL2.LoadImageFromStream(ref handle, data));
 }