protected int OnExecute(CommandLineApplication app) { var inputFile = PngFile; var outputFile = OutputImd ?? Path.GetFullPath(Path.GetFileName(Path.ChangeExtension(inputFile, ".imd"))); Directory.CreateDirectory(Path.GetDirectoryName(outputFile)); // Alpha enabled png → always 32 bpp using (var bitmap = new Bitmap(inputFile)) { var imgd = ImgdBitmapUtil.ToImgd(bitmap, BitsPerPixel, QuantizerFactory.MakeFrom(this)); var buffer = new MemoryStream(); imgd.Write(buffer); File.WriteAllBytes(outputFile, buffer.ToArray()); } return(0); }
protected int OnExecute(CommandLineApplication app) { OutputImz = OutputImz ?? Path.GetFullPath(Path.GetFileName(Path.ChangeExtension(InputFile.First(), ".imz"))); Directory.CreateDirectory(Path.GetDirectoryName(OutputImz)); var prependImgdList = (Append && File.Exists(OutputImz)) ? File.OpenRead(OutputImz).Using(stream => Imgz.Read(stream).ToArray()) : new Imgd[0]; var buffer = new MemoryStream(); Imgz.Write( buffer, prependImgdList .Concat( InputFile .SelectMany(imdFile => ImgdBitmapUtil.FromFileToImgdList(imdFile, BitsPerPixel, QuantizerFactory.MakeFrom(this))) ) .ToArray() ); File.WriteAllBytes(OutputImz, buffer.ToArray()); return(0); }
public ImageViewerViewModel() { OpenCommand = new RelayCommand(x => { FileDialog.OnOpen(fileName => { LoadImage(fileName); }, OpenFilters); }, x => !IsTool); SaveCommand = new RelayCommand(x => { if (IsTool) { Save(_toolInvokeDesc.SelectedEntry.Stream); } else if (!string.IsNullOrEmpty(FileName)) { using (var stream = File.Open(FileName, FileMode.Create)) { Save(stream); } } else { SaveAsCommand.Execute(x); } }, x => ImageFormat != null); SaveAsCommand = new RelayCommand(x => { var filter = new List <FileDialogFilter>().AddExtensions($"{ImageFormat.Name} format", ImageFormat.Extension); FileDialog.OnSave(fileName => { using (var stream = File.Open(fileName, FileMode.Create)) { Save(stream); } }, filter); }, x => ImageFormat != null && !IsTool); ExitCommand = new RelayCommand(x => { Window.Close(); }, x => true); AboutCommand = new RelayCommand(x => { new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog(); }, x => true); ExportCommand = new RelayCommand(x => { FileDialog.OnSave(fileName => { var imageFormat = _imageFormatService.GetFormatByFileName(fileName); if (imageFormat == null) { var extension = Path.GetExtension(fileName); MessageBox.Show($"The format with extension {extension} is not supported for export.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } File.OpenWrite(fileName).Using(stream => Save(stream, imageFormat)); }, ExportFilters); }, x => true); ImportCommand = new RelayCommand( parameter => { AddImage(Enum.Parse <AddPosition>($"{parameter}")); }, x => IsMultipleImageFormat ); CreateNewImgzCommand = new RelayCommand( x => { if (IsSingleImageFormat || IsMultipleImageFormat) { if (MessageBoxResult.OK != MessageBox.Show("This will discard all of existing images.", null, MessageBoxButton.OKCancel, MessageBoxImage.Exclamation)) { return; } } FileName = null; EditImageList( currentImageList => { var newImage = CreateDummyImage(); return(new EditResult { imageList = new IImageRead[] { newImage }, selection = newImage, }); } ); } ); ConvertToImgzCommand = new RelayCommand( x => { FileName = null; EditImageList( currentImageList => { return(new EditResult { imageList = new IImageRead[] { Image.Source }, selection = Image.Source }); } ); }, x => IsSingleImageFormat ); InsertEmptyImageCommand = new RelayCommand( x => { AddEmptyImage(AddPosition.BeforeCurrent); }, x => IsMultipleImageFormat ); RemoveImageCommand = new RelayCommand( x => { EditImageList( currentImageList => { return(new EditResult { imageList = currentImageList.Except(new IImageRead[] { Image?.Source }) }); } ); }, x => IsMultipleImageFormat && ImageContainer.Count >= 1 ); ConvertBitsPerPixelCommand = new RelayCommand( parameter => { try { EditImageList( currentImageList => { var sourceImage = Image.Source; var bpp = Convert.ToInt32(parameter); var newImage = ImgdBitmapUtil.ToImgd( sourceImage.CreateBitmap(), bpp, QuantizerFactory.MakeFrom( bpp, UsePngquant ?? false ) ); return(new EditResult { imageList = currentImageList .Select(it => ReferenceEquals(it, sourceImage) ? newImage : it), selection = newImage, }); } ); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }, x => IsMultipleImageFormat && ImageContainer.Count >= 1 ); ZoomLevel = ZoomLevelFit; }
public ImageViewerViewModel() { OpenCommand = new RelayCommand(x => { FileDialog.OnOpen(fileName => { LoadImage(fileName); }, OpenFilters); }, x => !IsTool); SaveCommand = new RelayCommand(x => { if (IsTool) { // Clear current bar entry content before saving. _toolInvokeDesc.SelectedEntry.Stream.SetLength(0); Save(_toolInvokeDesc.SelectedEntry.Stream); } else if (!string.IsNullOrEmpty(FileName)) { using (var stream = File.Open(FileName, FileMode.Create)) { Save(stream); } } else { SaveAsCommand.Execute(x); } }, x => ImageFormat != null); SaveAsCommand = new RelayCommand(x => { var filter = new List <FileDialogFilter>().AddExtensions($"{ImageFormat.Name} format", ImageFormat.Extension); FileDialog.OnSave(fileName => { using (var stream = File.Open(fileName, FileMode.Create)) { Save(stream); } }, filter); }, x => ImageFormat != null && !IsTool); ExitCommand = new RelayCommand(x => { Window.Close(); }, x => true); AboutCommand = new RelayCommand(x => { new AboutDialog(Assembly.GetExecutingAssembly()).ShowDialog(); }, x => true); ExportCurrentCommand = new RelayCommand(x => { var singleImage = Image?.Source; if (singleImage != null) { FileDialog.OnSave(fileName => { var imageFormat = _imageFormatService.GetFormatByFileName(fileName); if (imageFormat == null) { var extension = Path.GetExtension(fileName); MessageBox.Show($"The format with extension {extension} is not supported for export.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } File.OpenWrite(fileName).Using( stream => { if (imageFormat.IsContainer) { imageFormat.As <IImageMultiple>().Write( stream, new ImageFormatService.ImageContainer(new IImageRead[] { singleImage }) ); } else { imageFormat.As <IImageSingle>().Write( stream, singleImage ); } } ); }, ExportToSingleImageFilters); } }, x => true); ExportAllCommand = new RelayCommand(x => { var multiImages = GetImagesForExport(); if (multiImages.Any()) { FileDialog.OnSave(fileName => { var imageFormat = _imageFormatService.GetFormatByFileName(fileName); if (imageFormat == null) { var extension = Path.GetExtension(fileName); MessageBox.Show($"The format with extension {extension} is not supported for export.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } var imageContainer = new ImageFormatService.ImageContainer(multiImages); File.OpenWrite(fileName).Using(stream => imageFormat.As <IImageMultiple>().Write(stream, imageContainer)); }, ExportToContainerFilters); } }, x => true); ImportCommand = new RelayCommand( parameter => { AddImage(Enum.Parse <AddPosition>($"{parameter}")); }, x => IsMultipleImageFormat ); CreateNewImgzCommand = new RelayCommand( x => { if (IsSingleImageFormat || IsMultipleImageFormat) { if (MessageBoxResult.OK != MessageBox.Show("This will discard all of existing images.", null, MessageBoxButton.OKCancel, MessageBoxImage.Exclamation)) { return; } } FileName = null; EditImageList( currentImageList => { var newImage = CreateDummyImage(); return(new EditResult { imageList = new IImageRead[] { newImage }, selection = newImage, }); } ); } ); ConvertToImgzCommand = new RelayCommand( x => { FileName = null; EditImageList( currentImageList => { return(new EditResult { imageList = new IImageRead[] { Image.Source }, selection = Image.Source }); } ); }, x => IsSingleImageFormat ); InsertEmptyImageCommand = new RelayCommand( x => { AddEmptyImage(AddPosition.BeforeCurrent); }, x => IsMultipleImageFormat ); RemoveImageCommand = new RelayCommand( x => { EditImageList( currentImageList => { return(new EditResult { imageList = currentImageList.Except(new IImageRead[] { Image?.Source }) }); } ); }, x => IsMultipleImageFormat && ImageContainer.Count >= 1 ); ConvertBitsPerPixelCommand = new RelayCommand( parameter => { try { EditImageList( currentImageList => { var sourceImage = Image.Source; var bpp = Convert.ToInt32(parameter); var newImage = ImgdBitmapUtil.ToImgd( sourceImage.CreateBitmap(), bpp, QuantizerFactory.MakeFrom( bpp, UsePngquant ?? false ) ); return(new EditResult { imageList = currentImageList .Select(it => ReferenceEquals(it, sourceImage) ? newImage : it), selection = newImage, }); } ); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }, x => IsMultipleImageFormat && ImageContainer.Count >= 1 ); CheckQuant = new RelayCommand(x => { if (UsePngquant == true) { if (!File.Exists("pngquant.exe")) { UsePngquant = false; string _msg = "PNGQuant was not located in OpenKH's root folder.\n" + "Please acquire PNGQuant from the link below and\n" + "place it in the same folder as this viewer."; new MessageDialog(_msg, "https://pngquant.org/pngquant-windows.zip").ShowDialog(); OnPropertyChanged(nameof(UsePngquant)); } } }); ZoomLevel = ZoomLevelFit; }