Exemplo n.º 1
0
        private void LoadImage(bool throwOnError = true)
        {
            if (!_fileLoaded || _openedFile == null)
            {
                return;
            }

            if (!int.TryParse(tbOffset.Text, out var offset) &&
                !int.TryParse(tbOffset.Text.Replace("0x", ""), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out offset) ||
                !int.TryParse(tbWidth.Text, out var width) ||
                !int.TryParse(tbHeight.Text, out var height))
            {
                return;
            }

            if (offset < 0 || offset >= _openedFile.Length || width <= 0 || height <= 0)
            {
                return;
            }

            if (!TryParseParameters(gbEncParameters, SelectedColorEncodingExtension.Parameters.Values.ToArray()) ||
                !TryParseParameters(gbSwizzleParameters, SelectedSwizzleExtension.Parameters.Values.ToArray()))
            {
                return;
            }

            if (SelectedSwizzleExtension.Name == "Custom" && CheckCustomSwizzle())
            {
                return;
            }

            var activeControl = this.GetActiveControl();

            ToggleParameters(false);
            ToggleForm(false);

            //var progress = new Progress<ProgressReport>();
            try
            {
                var encoding = CreateEncoding();

                var totalColors = width * height;
                if (totalColors % encoding.ColorsPerValue > 0)
                {
                    return;
                }

                var bitsPerColor = encoding.BitsPerValue / encoding.ColorsPerValue;
                var dataLength   = totalColors * bitsPerColor / 8;

                var minDataLength = Math.Min(dataLength, _openedFile.Length - offset);
                var imgData       = new byte[minDataLength];

                _openedFile.Position = offset;
                _openedFile.Read(imgData, 0, imgData.Length);

                var imageConfiguration = new ImageConfiguration();
                if (SelectedSwizzleExtension.Name != "None")
                {
                    imageConfiguration.RemapPixelsWith(size => CreateSwizzle(size, encoding));
                }

                var transcoder = imageConfiguration.TranscodeWith(size => encoding).Build();

                pbMain.Image = transcoder.Decode(imgData, new Size(width, height));
            }
            catch (Exception e)
            {
                if (throwOnError)
                {
                    MessageBox.Show(e.ToString(), "Exception catched.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            tslWidth.Text  = pbMain.Width.ToString();
            tslHeight.Text = pbMain.Height.ToString();

            ToggleParameters(true);
            ToggleForm(true);

            if (activeControl != null && Contains(activeControl))
            {
                activeControl.Focus();
            }
        }
Exemplo n.º 2
0
        private void UpdateImage()
        {
            SetStatus(string.Empty);

            if (_openedFile == null)
            {
                return;
            }

            if (!int.TryParse(offsetText.Text, out var offset) &&
                !int.TryParse(offsetText.Text.Replace("0x", ""), NumberStyles.HexNumber, CultureInfo.CurrentCulture,
                              out offset) ||
                !int.TryParse(widthText.Text, out var width) ||
                !int.TryParse(heightText.Text, out var height))
            {
                SetStatus("Input values are invalid.");
                return;
            }

            if (offset < 0 || offset >= _openedFile.Length || width <= 0 || height <= 0)
            {
                SetStatus("Input values are invalid.");
                return;
            }

            if (SelectedSwizzleExtension.Name == "Custom" && CheckCustomSwizzle())
            {
                SetStatus("Custom swizzle is invalid.");
                return;
            }

            UpdateParameters(false);
            UpdateForm(false);

            try
            {
                var encoding = CreateEncoding();

                var totalColors = width * height;
                if (totalColors % encoding.ColorsPerValue > 0)
                {
                    throw new InvalidOperationException($"Total pixels does not match with the encoding specification.");
                }

                var bitsPerColor = encoding.BitsPerValue / encoding.ColorsPerValue;
                var dataLength   = totalColors * bitsPerColor / 8;

                var minDataLength = Math.Min(dataLength, _openedFile.Length - offset);
                var imgData       = new byte[minDataLength];

                _openedFile.Position = offset;
                _openedFile.Read(imgData, 0, imgData.Length);

                var imageConfiguration = new ImageConfiguration();
                if (SelectedSwizzleExtension.Name != "None")
                {
                    imageConfiguration.RemapPixelsWith(size => CreateSwizzle(size, encoding));
                }

                var transcoder = imageConfiguration.TranscodeWith(() => encoding).Build();
                imageView.Image = transcoder.Decode(imgData, new System.Drawing.Size(width, height)).ToEto();
                imageView.Invalidate();
            }
            catch (Exception e)
            {
                SetStatus(e.Message);
            }

            UpdateParameters(true);
            UpdateForm(true);
        }