Skip to content

Commit

Permalink
Optional image resizing on import / paste. Improved performance while…
Browse files Browse the repository at this point in the history
… selecting all items in the listboxes.
  • Loading branch information
TBXin committed Mar 25, 2016
1 parent 740ea5f commit 0deded1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 72 deletions.
12 changes: 12 additions & 0 deletions src/NFirmwareEditor/Windows/ImportImageWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 34 additions & 12 deletions src/NFirmwareEditor/Windows/ImportImageWindow.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NFirmwareEditor.Core;
using NFirmwareEditor.Managers;
using NFirmwareEditor.UI;

namespace NFirmwareEditor.Windows
{
public partial class ImportImageWindow : Form
internal partial class ImportImageWindow : Form
{
private readonly IDictionary<int, bool[,]> m_originalImportedImages = new Dictionary<int, bool[,]>();
private readonly IDictionary<int, bool[,]> m_croppedImportedImages = new Dictionary<int, bool[,]>();

public ImportImageWindow()
{
InitializeComponent();
Icon = Paths.ApplicationIcon;

ResizeCheckBox.CheckedChanged += ResizeCheckBox_CheckedChanged;
}

public ImportImageWindow
Expand All @@ -33,29 +39,45 @@ int importedImageCount
{
var originalImage = originalImages[i];
var importedImage = importedImages[i];
var croppedImportedImage = FirmwareImageProcessor.PasteImage(originalImage, importedImage);

m_originalImportedImages[i] = importedImage;
m_croppedImportedImages[i] = croppedImportedImage;

LeftLayoutPanel.Controls.Add(CreateGrid(originalImage));
RightLayoutPanel.Controls.Add(CreateGrid(importedImage));
RightLayoutPanel.Controls.Add(CreateGrid(croppedImportedImage));
}

BeforeLabel.Text = string.Format("Before:\nUsing {0} of {1} selected images.", originalImages.Count, originalsImageCount);
AfterLabel.Text = string.Format("After:\nUsing {0} of {1} importing images.", importedImages.Count, importedImageCount);
}

public IEnumerable<bool[,]> GetImportedImages()
{
switch (ResizeCheckBox.Checked)
{
case true: return m_originalImportedImages.Values;
case false: return m_croppedImportedImages.Values;
default: throw new ArgumentOutOfRangeException();
}
}

private void ResizeCheckBox_CheckedChanged(object sender, EventArgs e)
{
var resizeOriginalImages = ResizeCheckBox.Checked;
var pixelGrids = RightLayoutPanel.Controls.OfType<PixelGrid>().ToList();

for (var i = 0; i < pixelGrids.Count; i++)
{
pixelGrids[i].Data = resizeOriginalImages ? m_originalImportedImages[i] : m_croppedImportedImages[i];
}
}

private PixelGrid CreateGrid(bool[,] imageData)
{
return new PixelGrid
{
Width = 205,
Height = 80,
BlockSize = 2,
ShowGrid = false,
Data = imageData,
BackColor = Color.Black,
BlockInnerBorderPen = Pens.Transparent,
BlockOuterBorderPen = Pens.Transparent,
ActiveBlockBrush = Brushes.White,
InactiveBlockBrush = Brushes.Black
Width = 205, Height = 80, BlockSize = 2, ShowGrid = false, Data = imageData, BackColor = Color.Black, BlockInnerBorderPen = Pens.Transparent, BlockOuterBorderPen = Pens.Transparent, ActiveBlockBrush = Brushes.White, InactiveBlockBrush = Brushes.Black
};
}
}
Expand Down
103 changes: 43 additions & 60 deletions src/NFirmwareEditor/Windows/Tabs/ImageEditorTabPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JetBrains.Annotations;
using NFirmware;
Expand Down Expand Up @@ -191,8 +190,10 @@ public bool OnHotkey(Keys keyData)
PasteButton.PerformClick();
return true;
case Keys.A:
ImageListBox.BeginUpdate();
ImageListBox.SelectedIndices.Clear();
ImageListBox.SelectedIndices.AddRange(Enumerable.Range(0, ImageListBox.Items.Count));
ImageListBox.EndUpdate();
return true;
case Keys.Up:
ShiftUpButton.PerformClick();
Expand All @@ -211,6 +212,45 @@ public bool OnHotkey(Keys keyData)
}
#endregion

private void ImportImages([NotNull] List<bool[,]> importedImages, [NotNull] IList<FirmwareImageMetadata> imageMetadata)
{
if (importedImages == null) throw new ArgumentNullException("importedImages");
if (imageMetadata == null) throw new ArgumentNullException("imageMetadata");
if (importedImages.Count == 0) return;

var originalImages = m_firmware.ReadImages(imageMetadata).ToList();
if (importedImages.Count == 1 && FirmwareImageProcessor.GetImageSize(importedImages[0]) == FirmwareImageProcessor.GetImageSize(originalImages[0]))
{
ImagePixelGrid.Data = ImagePreviewPixelGrid.Data = ProcessImage(data => importedImages[0], imageMetadata[0], true);
}
else
{
var originalImagesCount = originalImages.Count;
var copiedImagesCount = importedImages.Count;
var minimumImagesCount = Math.Min(originalImagesCount, copiedImagesCount);

originalImages = originalImages.Take(minimumImagesCount).ToList();
importedImages = importedImages.Take(minimumImagesCount).ToList();

using (var importWindow = new ImportImageWindow(originalImages, importedImages, originalImagesCount, copiedImagesCount))
{
if (importWindow.ShowDialog() != DialogResult.OK) return;
importedImages = importWindow.GetImportedImages().ToList();
}

var updatedImage = new bool[5, 5];
for (var i = 0; i < minimumImagesCount; i++)
{
var index = i;
updatedImage = ProcessImage(x => importedImages[index], SelectedImageMetadata[index]);
}

ImageCacheManager.RebuildImageCache(m_firmware);
ImageListBox.Invalidate();
ImagePixelGrid.Data = ImagePreviewPixelGrid.Data = updatedImage;
}
}

private bool[,] ProcessImage(Func<bool[,], bool[,]> imageDataProcessor, FirmwareImageMetadata imageMetadata, bool rebuildCache = false)
{
var processedData = imageDataProcessor(ImagePixelGrid.Data);
Expand Down Expand Up @@ -381,38 +421,7 @@ private void PasteButton_Click(object sender, EventArgs e)
if (SelectedImageMetadata.Count == 0) return;

var copiedImages = m_clipboardManager.GetData();
if (copiedImages.Count == 0) return;
if (copiedImages.Count == 1)
{
ImagePixelGrid.Data = ImagePreviewPixelGrid.Data = ProcessImage(data => copiedImages[0], LastSelectedImageMetadata, true);
}
else
{
var originalImages = m_firmware.ReadImages(SelectedImageMetadata).ToList();

var originalImagesCount = originalImages.Count;
var copiedImagesCount = copiedImages.Count;
var minimumImagesCount = Math.Min(originalImagesCount, copiedImagesCount);

originalImages = originalImages.Take(minimumImagesCount).ToList();
copiedImages = copiedImages.Take(minimumImagesCount).ToList();

using (var importWindow = new ImportImageWindow(originalImages, copiedImages, originalImagesCount, copiedImagesCount))
{
if (importWindow.ShowDialog() != DialogResult.OK) return;
}

var updatedImage = new bool[5, 5];
for (var i = 0; i < minimumImagesCount; i++)
{
var index = i;
updatedImage = ProcessImage(x => copiedImages[index], SelectedImageMetadata[index]);
}

ImageCacheManager.RebuildImageCache(m_firmware);
ImageListBox.Invalidate();
ImagePixelGrid.Data = ImagePreviewPixelGrid.Data = updatedImage;
}
ImportImages(copiedImages, SelectedImageMetadata);
}

private void BitmapImportButton_Click(object sender, EventArgs eventArgs)
Expand Down Expand Up @@ -481,34 +490,8 @@ private void ImportContextMenuItem_Click(object sender, EventArgs e)
}

var exportedImages = ImageExportManager.Import(fileName);
if (exportedImages.Count == 0) return;

var originalImages = m_firmware.ReadImages(SelectedImageMetadata).ToList();
var importedImages = exportedImages.Select(x => x.Data).ToList();

var originalImagesCount = originalImages.Count;
var importedImagesCount = importedImages.Count;
var minimumImagesCount = Math.Min(originalImagesCount, importedImagesCount);

originalImages = originalImages.Take(minimumImagesCount).ToList();
importedImages = importedImages.Take(minimumImagesCount).ToList();

using (var importWindow = new ImportImageWindow(originalImages, importedImages, originalImagesCount, importedImagesCount))
{
if (importWindow.ShowDialog() != DialogResult.OK) return;
}

for (var i = 0; i < minimumImagesCount; i++)
{
var index = i;
ProcessImage(x => importedImages[index], SelectedImageMetadata[index]);
}

ImageCacheManager.RebuildImageCache(m_firmware);

var lastSelectedItem = ImageListBox.SelectedIndices[ImageListBox.SelectedIndices.Count - 1];
ImageListBox.SelectedIndices.Clear();
ImageListBox.SelectedIndex = lastSelectedItem;
ImportImages(importedImages, SelectedImageMetadata);
}

private void ImageListBox_DrawItem(object sender, DrawItemEventArgs e)
Expand Down

0 comments on commit 0deded1

Please sign in to comment.