예제 #1
0
 /// <summary>
 /// Populates the <seealso cref="AssetData"/> byte array with a JPEG2000
 /// encoded image created from the data in <seealso cref="Image"/>
 /// </summary>
 public override void Encode()
 {
     using (var writer = new OpenJpegDotNet.IO.Writer(Image.ExportBitmap()))
     {
         AssetData = writer.Encode();
     }
 }
예제 #2
0
        void LoadImage(ModelMaterial material)
        {
            var fname = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(FileName), material.Texture);

            try
            {
                string ext = System.IO.Path.GetExtension(material.Texture).ToLower();

                Bitmap bitmap = null;

                switch (ext)
                {
                case ".jp2":
                case ".j2c":
                    material.TextureData = File.ReadAllBytes(fname);
                    return;

                case ".tga":
                    bitmap = LoadTGAClass.LoadTGA(fname);
                    break;

                default:
                    bitmap = (Bitmap)Image.FromFile(fname);
                    break;
                }

                int width  = bitmap.Width;
                int height = bitmap.Height;

                // Handle resizing to prevent excessively large images and irregular dimensions
                if (!IsPowerOfTwo((uint)width) || !IsPowerOfTwo((uint)height) || width > 1024 || height > 1024)
                {
                    var origWidth  = width;
                    var origHieght = height;

                    width  = ClosestPowerOwTwo(width);
                    height = ClosestPowerOwTwo(height);

                    width  = width > 1024 ? 1024 : width;
                    height = height > 1024 ? 1024 : height;

                    Logger.Log("Image has irregular dimensions " + origWidth + "x" + origHieght + ". Resizing to " + width + "x" + height, Helpers.LogLevel.Info);

                    Bitmap   resized  = new Bitmap(width, height, bitmap.PixelFormat);
                    Graphics graphics = Graphics.FromImage(resized);

                    graphics.SmoothingMode     = SmoothingMode.HighQuality;
                    graphics.InterpolationMode =
                        InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(bitmap, 0, 0, width, height);

                    bitmap.Dispose();
                    bitmap = resized;
                }

                using (var writer = new OpenJpegDotNet.IO.Writer(bitmap))
                {
                    material.TextureData = writer.Encode();
                }

                Logger.Log("Successfully encoded " + fname, Helpers.LogLevel.Info);
            }
            catch (Exception ex)
            {
                Logger.Log("Failed loading " + fname + ": " + ex.Message, Helpers.LogLevel.Warning);
            }
        }
예제 #3
0
        public void LoadImage(string fname)
        {
            FileName = fname;

            if (string.IsNullOrEmpty(FileName))
            {
                return;
            }

            txtStatus.AppendText("Loading..." + Environment.NewLine);

            string extension = Path.GetExtension(FileName).ToLower();

            try
            {
                Bitmap bitmap = null;
                switch (extension)
                {
                case ".jp2":
                case ".j2c":
                    // Upload JPEG2000 images untouched
                    UploadData = File.ReadAllBytes(FileName);

                    using (var reader = new OpenJpegDotNet.IO.Reader(UploadData))
                    {
                        if (reader.ReadHeader())
                        {
                            bitmap = reader.DecodeToBitmap();
                        }
                    }

                    txtStatus.AppendText("Loaded raw JPEG2000 data " + FileName + Environment.NewLine);
                    break;

                case ".tga":
                    bitmap = LoadTGAClass.LoadTGA(FileName);
                    break;

                default:
                    bitmap = Image.FromFile(FileName) as Bitmap;
                    break;
                }

                if (bitmap == null)
                {
                    txtStatus.AppendText("Failed to load image " + FileName + Environment.NewLine);
                    return;
                }

                txtStatus.AppendText("Loaded image " + FileName + Environment.NewLine);

                int width  = bitmap.Width;
                int height = bitmap.Height;

                // Handle resizing to prevent excessively large images and irregular dimensions
                if (!IsPowerOfTwo((uint)width) || !IsPowerOfTwo((uint)height) || width > 1024 || height > 1024)
                {
                    txtStatus.AppendText("Image has irregular dimensions " + width + "x" + height + Environment.NewLine);

                    width  = ClosestPowerOwTwo(width);
                    height = ClosestPowerOwTwo(height);

                    width  = width > 1024 ? 1024 : width;
                    height = height > 1024 ? 1024 : height;

                    txtStatus.AppendText("Resizing to " + width + "x" + height + Environment.NewLine);

                    Bitmap   resized  = new Bitmap(width, height, bitmap.PixelFormat);
                    Graphics graphics = Graphics.FromImage(resized);

                    graphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics.InterpolationMode =
                        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(bitmap, 0, 0, width, height);

                    bitmap.Dispose();
                    bitmap = resized;
                }

                txtStatus.AppendText("Encoding image..." + Environment.NewLine);

                using (var writer = new OpenJpegDotNet.IO.Writer(bitmap))
                {
                    var cp = new OpenJpegDotNet.CompressionParameters();
                    OpenJpegDotNet.OpenJpeg.SetDefaultEncoderParameters(cp);
                    cp.CodingParameterDistortionAllocation = 1;
                    if (chkLossless.Checked)
                    {
                        cp.TcpNumLayers = 1;
                        cp.TcpRates[0]  = 0;
                    }
                    else
                    {
                        cp.TcpNumLayers = 5;
                        cp.TcpRates[0]  = 1920;
                        cp.TcpRates[1]  = 480;
                        cp.TcpRates[2]  = 120;
                        cp.TcpRates[3]  = 30;
                        cp.TcpRates[4]  = 10;
                        cp.Irreversible = true;
                        cp.TcpMCT       = 1;
                    }
                    writer.SetupEncoderParameters(cp);
                    UploadData = writer.Encode();
                }

                txtStatus.AppendText("Finished encoding." + Environment.NewLine);
                ImageLoaded = true;
                UpdateButtons();
                txtAssetID.Text = UUID.Zero.ToString();

                pbPreview.Image = bitmap;
                lblSize.Text    = string.Format("{0}x{1} {2} KB", bitmap.Width, bitmap.Height, Math.Round((double)UploadData.Length / 1024.0d, 2));
            }
            catch (Exception ex)
            {
                UploadData        = null;
                btnSave.Enabled   = false;
                btnUpload.Enabled = false;
                txtStatus.AppendText(string.Format("Failed to load the image:" + Environment.NewLine
                                                   + "{0}" + Environment.NewLine, ex.Message));
            }
        }