示例#1
0
        // Reads textures from jpg, png, and tga files
        public static Texture2D LoadTexture(BasicEffect effect, string texturePath)
        {
            Texture2D texture;

            if (texturePath.ToLower().EndsWith(".tga"))
            {
                TargaImage image = new TargaImage(texturePath);
                texture = new Texture2D(effect.GraphicsDevice, image.Header.Width, image.Header.Height);
                Color[] data = new Color[image.Header.Height * image.Header.Width];
                for (int y = 0; y < image.Header.Height; y++)
                {
                    for (int x = 0; x < image.Header.Width; x++)
                    {
                        System.Drawing.Color color = image.Image.GetPixel(x, y);
                        data[y * image.Header.Width + x] = new Color(color.R, color.G, color.B, color.A);
                    }
                }
                image.Dispose();
                texture.SetData(data);
            }
            else
            {
                FileStream stream = new FileStream(texturePath, FileMode.Open);
                texture = Texture2D.FromStream(effect.GraphicsDevice, stream);
                stream.Close();
            }

            return(texture);
        }
示例#2
0
 protected virtual void Dispose(bool dispose)
 {
     if (dispose)
     {
         if (TarImage != null)
         {
             TarImage.Dispose();
         }
     }
 }
示例#3
0
        /// <summary>
        /// Loads image from stream.
        /// </summary>
        /// <param name="stream">Full image stream.</param>
        /// <param name="Format">Detected Format.</param>
        /// <param name="extension">File Extension. Used to determine format more easily.</param>
        /// <param name="maxWidth">Maximum width to allow when loading. Resized if enforceResize = true.</param>
        /// <param name="maxHeight">Maximum height to allow when loading. Resized if enforceResize = true.</param>
        /// <param name="enforceResize">True = Resizes image to match either maxWidth or maxHeight.</param>
        /// <param name="header">DDS header of image.</param>
        /// <param name="mergeAlpha">ONLY valid when enforceResize is true. True = Flattens alpha down, directly affecting RGB.</param>
        /// <returns>List of Mipmaps.</returns>
        internal static List <MipMap> LoadImage(Stream stream, out Format Format, string extension, int maxWidth, int maxHeight, bool enforceResize, out DDSGeneral.DDS_HEADER header, bool mergeAlpha)
        {
            // KFreon: See if image is built-in codec agnostic.
            header = null;
            Format = ImageFormats.ParseFormat(stream, extension, ref header);
            List <MipMap> MipMaps = null;

            switch (Format.SurfaceFormat)
            {
            case ImageEngineFormat.BMP:
            case ImageEngineFormat.JPG:
            case ImageEngineFormat.PNG:
                MipMaps = WIC_Codecs.LoadWithCodecs(stream, maxWidth, maxHeight, false);
                break;

            case ImageEngineFormat.DDS_DXT1:
            case ImageEngineFormat.DDS_DXT2:
            case ImageEngineFormat.DDS_DXT3:
            case ImageEngineFormat.DDS_DXT4:
            case ImageEngineFormat.DDS_DXT5:
                if (WindowsWICCodecsAvailable)
                {
                    MipMaps = WIC_Codecs.LoadWithCodecs(stream, maxWidth, maxHeight, true);
                }
                else
                {
                    MipMaps = DDSGeneral.LoadDDS(stream, header, Format, maxHeight > maxWidth ? maxHeight : maxWidth);
                }
                break;

            case ImageEngineFormat.DDS_ARGB:
            case ImageEngineFormat.DDS_A8L8:
            case ImageEngineFormat.DDS_RGB:
            case ImageEngineFormat.DDS_ATI1:
            case ImageEngineFormat.DDS_ATI2_3Dc:
            case ImageEngineFormat.DDS_G8_L8:
            case ImageEngineFormat.DDS_V8U8:
                MipMaps = DDSGeneral.LoadDDS(stream, header, Format, maxHeight > maxWidth ? maxHeight : maxWidth);
                break;

            case ImageEngineFormat.TGA:
                var             img    = new TargaImage(stream);
                byte[]          pixels = UsefulThings.WinForms.Imaging.GetPixelDataFromBitmap(img.Image);
                WriteableBitmap wbmp   = UsefulThings.WPF.Images.CreateWriteableBitmap(pixels, img.Image.Width, img.Image.Height);
                var             mip1   = new MipMap(wbmp);
                MipMaps = new List <MipMap>()
                {
                    mip1
                };
                img.Dispose();
                break;

            default:
                throw new InvalidDataException("Image format is unknown.");
            }

            if (MipMaps == null || MipMaps.Count == 0)
            {
                throw new InvalidDataException("No mipmaps loaded.");
            }


            // KFreon: No resizing requested
            if (maxHeight == 0 && maxWidth == 0)
            {
                return(MipMaps);
            }

            // KFreon: Test if we need to resize
            var top = MipMaps.First();

            if (top.Width == maxWidth || top.Height == maxHeight)
            {
                return(MipMaps);
            }

            int max = maxWidth > maxHeight ? maxWidth : maxHeight;

            // KFreon: Attempt to resize
            var sizedMips = MipMaps.Where(m => m.Width > m.Height ? m.Width <= max : m.Height <= max);

            if (sizedMips != null && sizedMips.Any())  // KFreon: If there's already a mip, return that.
            {
                MipMaps = sizedMips.ToList();
            }
            else if (enforceResize)
            {
                // Get top mip and clear others.
                var mip = MipMaps[0];
                MipMaps.Clear();
                MipMap output = null;

                int divisor = mip.Width > mip.Height ? mip.Width / max : mip.Height / max;

                output = Resize(mip, 1f / divisor, mergeAlpha);

                MipMaps.Add(output);
            }
            return(MipMaps);
        }
示例#4
0
        private void threadSpellIcons_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] _files;

            e.Result = DialogResult.Abort;

            try
            {
                _files = Directory.GetFiles(_InputFolder, "spells??.tga");
                string _outPath = _OutputFolder;

                if (!Directory.Exists(_outPath))
                {
                    Directory.CreateDirectory(_outPath);
                }

                _SpellIcons = _files.Length * _SpellIconsPerFile;

                int _count = 0;

                string _spellFileName;

                for (int _spellIconSheet = 0; _spellIconSheet < 100; _spellIconSheet++)
                {
                    if (File.Exists(_spellFileName = _InputFolder + @"\spells" + (_spellIconSheet + 1).ToString("00") + ".tga"))
                    {
                        int _x         = _SpellIconStart.X;
                        int _y         = _SpellIconStart.Y;
                        int _iconIndex = 0;

                        TargaImage _iconSheet = new TargaImage(_spellFileName);

                        while ((_y + _SpellIconSize.Y) < _iconSheet.Image.Height)
                        {
                            string _outFile = _outPath + @"\" + ((_spellIconSheet * _SpellIconsPerFile) + _iconIndex).ToString() + ".png";

                            Bitmap   _icon    = new Bitmap(_SpellIconSize.X, _SpellIconSize.Y, _iconSheet.Image.PixelFormat);
                            Graphics _blitter = Graphics.FromImage(_icon);
                            _blitter.DrawImage(_iconSheet.Image, 0, 0, new Rectangle(_x, _y, _SpellIconSize.X, _SpellIconSize.Y), GraphicsUnit.Pixel);
                            _blitter.Dispose();
                            _icon.Save(_outFile);
                            _icon.Dispose();

                            _iconIndex++;
                            _x += _SpellIconSize.X + _SpellIconPadding.X;

                            if ((_x + _SpellIconSize.X) >= _iconSheet.Image.Width)
                            {
                                _x  = _SpellIconStart.X;
                                _y += _SpellIconSize.Y + _SpellIconPadding.Y;
                            }

                            _TotalSpellIconCount++;
                            threadSpellIcons.ReportProgress(_count++);

                            if (threadSpellIcons.CancellationPending)
                            {
                                e.Result = DialogResult.Cancel;

                                return;
                            }
                        }

                        _iconSheet.Dispose();
                    }
                }
            }
            catch
            {
                return;
            }

            e.Result = DialogResult.OK;

            return;
        }
示例#5
0
 public void Unload()
 {
     TargaImage.Dispose();
 }