Reads and loads a Truevision TGA Format image file.
Inheritance: IDisposable
    public void DoPreview(String path)
    {
        Paloma.TargaImage tga         = new Paloma.TargaImage(Path);
        PictureBox        pictureBox1 = new PictureBox();

        pictureBox1.Dock     = DockStyle.Fill;
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Image    = tga.Image;
        pictureBox1.Width    = this.Width;
        pictureBox1.Height   = this.Width;

        TabControl tb = new TabControl();

        TabPage tp1 = new TabPage();

        tp1.Text       = "Properities ";
        tp1.AutoScroll = true;

        tp1.Controls.Add(new Label
        {
            Text = "Width :     " + tga.Header.Width + " px",
            Dock = DockStyle.Top
        });
        tp1.Controls.Add(new Label
        {
            Text = "Height:    " + tga.Header.Height + " px",
            Dock = DockStyle.Top
        });
        tp1.Controls.Add(new Label
        {
            Text = "ImageType: " + tga.Header.ImageType.ToString() + " px",
            Dock = DockStyle.Top
        });
        tp1.Controls.Add(new Label
        {
            Text = "Format:   " + tga.Format.ToString() + " px",
            Dock = DockStyle.Top
        });


        tp1.Controls.Add(new Label
        {
            Text = "Width:   " + tga.Header.Width + " px",
            Dock = DockStyle.Top
        });

        tb.TabPages.Add(tp1);
        tb.Dock = DockStyle.Bottom;

        Controls.Add(pictureBox1);
        Controls.Add(tb);
    }
示例#2
0
        public static Bitmap load(string path)
        {
            Bitmap bmp = null;

            //TARGA file *.tga
            if (path.ToLower().EndsWith(".tga"))
            {
                using (Paloma.TargaImage tar = new Paloma.TargaImage(path))
                {
                    bmp = new Bitmap(tar.Image);
                }
            }
            //PHOTOSHOP file *.PSD
            else if (path.ToLower().EndsWith(".psd"))
            {
                System.Drawing.PSD.PsdFile psd = (new System.Drawing.PSD.PsdFile()).Load(path);
                bmp = System.Drawing.PSD.ImageDecoder.DecodeImage(psd);
            }
            else
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    bmp = new Bitmap(fs, true);

                    //GIF file *.gif
                    if (bmp.RawFormat.Equals(ImageFormat.Gif))
                    {
                        bmp = new Bitmap(path);
                    }
                    //ICON file *.ico
                    else if (bmp.RawFormat.Equals(ImageFormat.Icon))
                    {
                        bmp = ReadIconFile(path);
                    }
                    else if (bmp.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        //read Exif rotation
                        int rotation = GetRotation(bmp);
                        if (rotation != 0)
                        {
                            bmp = ScaleDownRotateBitmap(bmp, 1.0f, rotation);
                        }
                    }
                }
            }

            return bmp;
        }
示例#3
0
    /*
     * public static Texture2D LoadUITexture(int tgir)
     * {
     *  var file = GetAsset(tgir);
     *  return LoadUITexture(file);
     * }*/
    public static Texture2D LoadTexture(byte[] file, bool mips)
    {
        var pngCheck = Encoding.UTF8.GetString(file, 1, 3);
        var jpgCheck = Encoding.UTF8.GetString(file, 6, 4);

        if (pngCheck != "PNG" && jpgCheck != "JFIF")
        {
            var tex = new Paloma.TargaImage(file).bmpTargaImage;
            tex.filterMode = FilterMode.Point;
            return(tex);
        }
        Texture2D fTex = new Texture2D(1, 1, TextureFormat.ARGB32, mips);

        fTex.filterMode = FilterMode.Point;
        fTex.LoadImage(file);
        return(fTex);
    }
示例#4
0
        /// <summary>
        /// Loads a Targa image file into a Bitmap object.
        /// </summary>
        /// <param name="sFileName">The Targa image filename</param>
        /// <returns>A Bitmap object with the Targa image loaded into it.</returns>
        public static Bitmap LoadTargaImage(string sFileName)
        {
            Bitmap b = null;
            using (TargaImage ti = new TargaImage(sFileName))
            {
                b = new Bitmap(ti.Image);
            }

            return b;
        }
示例#5
0
 private static Bitmap CopyToBitmap(TargaImage ti)
 {
     Bitmap b = null;
     if (ti.Image.PixelFormat == PixelFormat.Format8bppIndexed)
     {
         b = (Bitmap)ti.Image.Clone();
     }
     else
     {
         b = new Bitmap(ti.Image.Width, ti.Image.Height, ti.Image.PixelFormat);
         using (Graphics g = Graphics.FromImage(b))
         {
             g.DrawImage(ti.Image, 0, 0, new Rectangle(0, 0, b.Width, b.Height), GraphicsUnit.Pixel);
         }
     }
     return b;
 }
示例#6
0
 /// <summary>
 /// Loads a stream with Targa image data into a Bitmap object.
 /// </summary>
 /// <param name="sFileName">The Targa image stream</param>
 /// <returns>A Bitmap object with the Targa image loaded into it.</returns>
 public static Bitmap LoadTargaImage(Stream ImageStream)
 {
     using (TargaImage ti = new TargaImage(ImageStream))
     {
         return CopyToBitmap(ti);
     }
 }
示例#7
0
 /// <summary>
 /// Loads a Targa image file into a Bitmap object.
 /// </summary>
 /// <param name="sFileName">The Targa image filename</param>
 /// <returns>A Bitmap object with the Targa image loaded into it.</returns>
 public static Bitmap LoadTargaImage(string sFileName)
 {
     using (TargaImage ti = new TargaImage(sFileName))
     {
         return CopyToBitmap(ti);
     }
 }
示例#8
0
        /*
         * public static Texture2D FromStreamSoft(GraphicsDevice gd, Stream str)
         * {
         *  //TODO: does not compile on xamarin platforms, so we use the slower method since it seems to load TGAs fine.
         *  Bitmap bmp = null;
         *  var magic = (str.ReadByte() | (str.ReadByte() << 8));
         *  str.Seek(0, SeekOrigin.Begin);
         *  magic += 0;
         *  if (magic == 0x4D42)
         *  {
         *      try
         *      {
         *          bmp = (Bitmap)Image.FromStream(str); //try as bmp
         *      }
         *      catch (Exception)
         *      {
         *          return null; //bad bitmap
         *      }
         *  }
         *  else
         *  {
         *      //test for targa
         *      str.Seek(-18, SeekOrigin.End);
         *      byte[] sig = new byte[16];
         *      str.Read(sig, 0, 16);
         *      str.Seek(0, SeekOrigin.Begin);
         *      if (ASCIIEncoding.Default.GetString(sig) == "TRUEVISION-XFILE")
         *      {
         *          try
         *          {
         *              bmp = new Paloma.TargaImage(str).Image; //try as tga
         *          }
         *          catch (Exception)
         *          {
         *              return null; //bad tga
         *          }
         *      }
         *  }
         *
         *  if (bmp != null)
         *  {
         *      //image loaded into bitmap
         *      bool premultiplied = false;
         *
         *      var data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
         *      var bytes = new byte[data.Height * data.Stride];
         *
         *      // copy the bytes from bitmap to array
         *      Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
         *
         *      for (int i = 0; i < bytes.Length; i += 4)
         *      { //flip red and blue and premultiply alpha
         *          if (bytes[i] > 0xFD && bytes[i + 1] < 3 && bytes[i + 2] > 0xFD)
         *              bytes[i + 3] = 0;
         *          byte temp = bytes[i + 2];
         *          float a = (premultiplied) ? 1 : (bytes[i + 3] / 255f);
         *          bytes[i + 2] = (byte)(bytes[i] * a);
         *          bytes[i + 1] = (byte)(bytes[i + 1] * a);
         *          bytes[i] = (byte)(temp * a);
         *      }
         *
         *      var tex = new Texture2D(gd, data.Width, data.Height);
         *      tex.SetData<byte>(bytes);
         *      return tex;
         *  } else
         *  {
         *      try
         *      {
         *          var tex = Texture2D.FromStream(gd, str);
         *          //ManualTextureMaskSingleThreaded(ref tex, MASK_COLORS.ToArray());
         *          return tex;
         *      }
         *      catch (Exception e)
         *      {
         *          Console.WriteLine("image load error: " + e.ToString());
         *          return new Texture2D(gd, 1, 1);
         *      }
         *  }
         * }
         */

        public static Texture2D FromStream(GraphicsDevice gd, Stream str)
        {
            //if (!UseSoftLoad)
            //{
            //attempt monogame load of image

            var magic = (str.ReadByte() | (str.ReadByte() << 8));

            str.Seek(0, SeekOrigin.Begin);
            magic += 0;
            if (magic == 0x4D42)
            {
                try
                {
                    //it's a bitmap.
                    var tex = Texture2D.FromStream(gd, str);
                    ManualTextureMaskSingleThreaded(ref tex, MASK_COLORS.ToArray());
                    return(tex);
                }
                catch (Exception)
                {
                    return(null); //bad bitmap :(
                }
            }
            else
            {
                //test for targa
                str.Seek(-18, SeekOrigin.End);
                byte[] sig = new byte[16];
                str.Read(sig, 0, 16);
                str.Seek(0, SeekOrigin.Begin);
                if (ASCIIEncoding.Default.GetString(sig) == "TRUEVISION-XFILE")
                {
                    try
                    {
                        var tga = new Paloma.TargaImage(str);
                        var tex = new Texture2D(gd, tga.Image.Width, tga.Image.Height);

                        //image loaded into bitmap
                        bool premultiplied = false;

                        var data  = tga.Image.LockBits(new System.Drawing.Rectangle(0, 0, tga.Image.Width, tga.Image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        var bytes = new byte[data.Height * data.Stride];

                        // copy the bytes from bitmap to array
                        Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

                        for (int i = 0; i < bytes.Length; i += 4)
                        { //flip red and blue and premultiply alpha
                            if (bytes[i] > 0xFD && bytes[i + 1] < 3 && bytes[i + 2] > 0xFD)
                            {
                                bytes[i + 3] = 0;
                            }
                            byte  temp = bytes[i + 2];
                            float a    = (premultiplied) ? 1 : (bytes[i + 3] / 255f);
                            bytes[i + 2] = (byte)(bytes[i] * a);
                            bytes[i + 1] = (byte)(bytes[i + 1] * a);
                            bytes[i]     = (byte)(temp * a);
                        }

                        tex.SetData <byte>(bytes);
                        return(tex);
                    }
                    catch (Exception)
                    {
                        return(null); //bad tga
                    }
                }
                else
                {
                    //anything else
                    try
                    {
                        var tex = Texture2D.FromStream(gd, str);
                        return(tex);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("error: " + e.ToString());
                        return(new Texture2D(gd, 1, 1));
                    }
                }
            }
        }
示例#9
0
        public static Bitmap load(string path)
        {
            Bitmap bmp = null;

            //file *.hdr
            if (path.ToLower().EndsWith(".hdr"))
            {
                FIBITMAP hdr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_HDR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY);
                bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(hdr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0));
                FreeImage.Unload(hdr);
            }
            //file *.exr
            else if (path.ToLower().EndsWith(".exr"))
            {
                FIBITMAP exr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_EXR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY);
                bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(exr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0));
                FreeImage.Unload(exr);
            }
            //file *.svg
            else if (path.ToLower().EndsWith(".svg"))
            {
                SvgDocument svg = SvgDocument.Open(path);
                bmp = svg.Draw();
            }
            //TARGA file *.tga
            else if (path.ToLower().EndsWith(".tga"))
            {
                using (Paloma.TargaImage tar = new Paloma.TargaImage(path))
                {
                    bmp = new Bitmap(tar.Image);
                }
            }
            //PHOTOSHOP file *.PSD
            else if (path.ToLower().EndsWith(".psd"))
            {
                System.Drawing.PSD.PsdFile psd = (new System.Drawing.PSD.PsdFile()).Load(path);
                bmp = System.Drawing.PSD.ImageDecoder.DecodeImage(psd);
            }
            else
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    bmp = new Bitmap(fs, true);

                    //GIF file *.gif
                    if (bmp.RawFormat.Equals(ImageFormat.Gif))
                    {
                        bmp = new Bitmap(path);
                    }
                    //ICON file *.ico
                    else if (bmp.RawFormat.Equals(ImageFormat.Icon))
                    {
                        bmp = ReadIconFile(path);
                    }
                    else if (bmp.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        //read Exif rotation
                        int rotation = GetRotation(bmp);
                        if (rotation != 0)
                        {
                            bmp = ScaleDownRotateBitmap(bmp, 1.0f, rotation);
                        }
                    }
                }
            }



            return(bmp);
        }
示例#10
0
        public Image GetImage()
        {
            if (this._IsImageChecked)
            {
                return this._Image;
            }

            this.ImageSubformat = "";
            this.ImageFormat = "";
            this._AlphaBits = -1;

            if (!Util.IsImage(this.Filename))
            {
                this._IsImageChecked = true;
                this._Image = null;

                return null;
            }

            byte[] _bytes = this.GetContents();

            if (_bytes == null)
            {
                this._IsImageChecked = true;

                this.Status = Result.OutOfMemory;

                return null;
            }

            Bitmap _loading = null;

            MemoryStream _stream = new MemoryStream(_bytes); // MemoryStream should remain open for the lifetime of the Bitmap.

            // Natively Supported Image File?
            try
            {
                _loading = new Bitmap(_stream);

                this.ImageFormat = System.IO.Path.GetExtension(this.Filename).ToLower();
            }
            catch
            {
                _loading = null;

                _stream.Seek(0, SeekOrigin.Begin);
            }

            if (_loading == null)
            {
                // TGA image?

                try
                {
                    TargaImage _tga = new TargaImage(_stream);

                    _loading = _tga.Image;

                    this.ImageFormat = ".tga";
                }
                catch
                {
                    _loading = null;
                }
            }

            if (_loading == null)
            {
                // DDS Texture?

                try
                {
                    DDSImage _dds = DDSImage.Load(_bytes);

                    _loading = _dds.Images[0];

                    this.ImageFormat = ".dds";
                    this.ImageSubformat = _dds.FormatName;
                }
                catch
                {
                    _loading = null;
                }
            }

            this._IsImageChecked = true;

            this._Image = _loading;

            return this._Image;
        }
示例#11
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;
        }
示例#12
0
        /// <summary>
        /// Loads a Targa image file into a Bitmap object.
        /// </summary>
        /// <param name="sFileName">The Targa image filename</param>
        /// <returns>A Bitmap object with the Targa image loaded into it.</returns>
        public static Bitmap LoadTargaImage(string sFileName)
        {
            Bitmap b = null;
            using (TargaImage ti = new TargaImage(sFileName))
            {
                // b = new Bitmap(ti.Image);

                // Patch from here: http://www.codeproject.com/Articles/31702/NET-Targa-Image-Reader?msg=4209395#xx4209395xx

                // Copy the bitmap manually
                b = new Bitmap(ti.Image.Width, ti.Image.Height, ti.Image.PixelFormat);
                Graphics g = Graphics.FromImage(b);
                g.DrawImage(ti.Image, 0, 0, new Rectangle(0, 0, b.Width, b.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }

            return b;
        }
示例#13
0
        public static Bitmap load(string path)
        {
            Bitmap bmp = null;

            //file *.hdr
            if (path.ToLower().EndsWith(".hdr"))
            {
                FIBITMAP hdr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_HDR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY);
                bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(hdr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0));
                FreeImage.Unload(hdr);
            }
            //file *.exr
            else if (path.ToLower().EndsWith(".exr"))
            {
                FIBITMAP exr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_EXR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY);
                bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(exr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0));
                FreeImage.Unload(exr);
            }
            //file *.svg
            else if (path.ToLower().EndsWith(".svg"))
            {
                SvgDocument svg = SvgDocument.Open(path);
                bmp = svg.Draw();
            }
            //TARGA file *.tga
            else if (path.ToLower().EndsWith(".tga"))
            {
                using (Paloma.TargaImage tar = new Paloma.TargaImage(path))
                {
                    bmp = new Bitmap(tar.Image);
                }
            }
            //PHOTOSHOP file *.PSD
            else if (path.ToLower().EndsWith(".psd"))
            {
                System.Drawing.PSD.PsdFile psd = (new System.Drawing.PSD.PsdFile()).Load(path);
                bmp = System.Drawing.PSD.ImageDecoder.DecodeImage(psd);
            }
            else
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    bmp = new Bitmap(fs, true);

                    //GIF file *.gif
                    if (bmp.RawFormat.Equals(ImageFormat.Gif))
                    {
                        bmp = new Bitmap(path);
                    }
                    //ICON file *.ico
                    else if (bmp.RawFormat.Equals(ImageFormat.Icon))
                    {
                        bmp = ReadIconFile(path);
                    }
                    else if (bmp.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        //read Exif rotation
                        int rotation = GetRotation(bmp);
                        if (rotation != 0)
                        {
                            bmp = ScaleDownRotateBitmap(bmp, 1.0f, rotation);
                        }
                    }
                }
            }

            return bmp;
        }