From f98e3c97f811a5feb56f32d7a4b59a4103013e34 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Wed, 21 Mar 2018 18:37:43 -0400 Subject: [PATCH] Tiff options GUI + auto compression --- .../ImportExport/Images/ImageSettings.cs | 1 + NAPS2.Core/ImportExport/Images/TiffHelper.cs | 47 ++-- .../WinForms/FImageSettings.Designer.cs | 65 +++++- NAPS2.Core/WinForms/FImageSettings.cs | 9 +- NAPS2.Core/WinForms/FImageSettings.resx | 212 +++++++++++++----- NAPS2.Core/WinForms/FPdfSettings.cs | 2 +- 6 files changed, 243 insertions(+), 93 deletions(-) diff --git a/NAPS2.Core/ImportExport/Images/ImageSettings.cs b/NAPS2.Core/ImportExport/Images/ImageSettings.cs index 86badf078..413d057af 100644 --- a/NAPS2.Core/ImportExport/Images/ImageSettings.cs +++ b/NAPS2.Core/ImportExport/Images/ImageSettings.cs @@ -26,6 +26,7 @@ namespace NAPS2.ImportExport.Images public enum TiffCompression { + Auto, Lzw, Ccitt4, None diff --git a/NAPS2.Core/ImportExport/Images/TiffHelper.cs b/NAPS2.Core/ImportExport/Images/TiffHelper.cs index 077fed3a1..8c2069b72 100644 --- a/NAPS2.Core/ImportExport/Images/TiffHelper.cs +++ b/NAPS2.Core/ImportExport/Images/TiffHelper.cs @@ -36,35 +36,26 @@ namespace NAPS2.ImportExport.Images { var iparams = new EncoderParameters(1); Encoder iparam = Encoder.Compression; - var iparamPara = new EncoderParameter(iparam, (long)GetEncoderValue(compression)); - iparams.Param[0] = iparamPara; using (var bitmap = scannedImageRenderer.Render(images[0])) { ValidateBitmap(bitmap); + var iparamPara = new EncoderParameter(iparam, (long)GetEncoderValue(compression, bitmap)); + iparams.Param[0] = iparamPara; bitmap.Save(location, codecInfo, iparams); } } else if (images.Count > 1) { - Encoder saveEncoder; - Encoder compressionEncoder; - EncoderParameter SaveEncodeParam; - EncoderParameter CompressionEncodeParam; var encoderParams = new EncoderParameters(2); - - saveEncoder = Encoder.SaveFlag; - compressionEncoder = Encoder.Compression; - - // Save the first page (frame). - SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame); - CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionLZW); - encoderParams.Param[0] = CompressionEncodeParam; - encoderParams.Param[1] = SaveEncodeParam; + var saveEncoder = Encoder.SaveFlag; + var compressionEncoder = Encoder.Compression; File.Delete(location); using (var bitmap0 = scannedImageRenderer.Render(images[0])) { ValidateBitmap(bitmap0); + encoderParams.Param[0] = new EncoderParameter(compressionEncoder, (long)GetEncoderValue(compression, bitmap0)); + encoderParams.Param[1] = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame); ; bitmap0.Save(location, codecInfo, encoderParams); for (int i = 1; i < images.Count; i++) @@ -79,20 +70,16 @@ namespace NAPS2.ImportExport.Images return false; } - SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage); - CompressionEncodeParam = new EncoderParameter(compressionEncoder, - (long)EncoderValue.CompressionLZW); - encoderParams.Param[0] = CompressionEncodeParam; - encoderParams.Param[1] = SaveEncodeParam; using (var bitmap = scannedImageRenderer.Render(images[i])) { ValidateBitmap(bitmap); + encoderParams.Param[0] = new EncoderParameter(compressionEncoder, (long)GetEncoderValue(compression, bitmap)); + encoderParams.Param[1] = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage); bitmap0.SaveAdd(bitmap, encoderParams); } } - SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush); - encoderParams.Param[0] = SaveEncodeParam; + encoderParams.Param[0] = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush); bitmap0.SaveAdd(encoderParams); } } @@ -107,7 +94,7 @@ namespace NAPS2.ImportExport.Images } - private EncoderValue GetEncoderValue(TiffCompression compression) + private EncoderValue GetEncoderValue(TiffCompression compression, Bitmap bitmap) { switch (compression) { @@ -115,8 +102,20 @@ namespace NAPS2.ImportExport.Images return EncoderValue.CompressionNone; case TiffCompression.Ccitt4: return EncoderValue.CompressionCCITT4; - default: + case TiffCompression.Lzw: return EncoderValue.CompressionLZW; + default: + if (bitmap.PixelFormat == PixelFormat.Format1bppIndexed + && bitmap.Palette.Entries.Length == 2 + && bitmap.Palette.Entries[0].ToArgb() == Color.Black.ToArgb() + && bitmap.Palette.Entries[1].ToArgb() == Color.White.ToArgb()) + { + return EncoderValue.CompressionCCITT4; + } + else + { + return EncoderValue.CompressionLZW; + } } } diff --git a/NAPS2.Core/WinForms/FImageSettings.Designer.cs b/NAPS2.Core/WinForms/FImageSettings.Designer.cs index b98998264..23385d697 100644 --- a/NAPS2.Core/WinForms/FImageSettings.Designer.cs +++ b/NAPS2.Core/WinForms/FImageSettings.Designer.cs @@ -41,14 +41,20 @@ namespace NAPS2.WinForms this.btnRestoreDefaults = new System.Windows.Forms.Button(); this.txtDefaultFilePath = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); - this.label2 = new System.Windows.Forms.Label(); this.linkPlaceholders = new System.Windows.Forms.LinkLabel(); this.txtJpegQuality = new System.Windows.Forms.TextBox(); this.tbJpegQuality = new System.Windows.Forms.TrackBar(); this.lblWarning = new System.Windows.Forms.Label(); this.btnChooseFolder = new System.Windows.Forms.Button(); this.cbSkipSavePrompt = new System.Windows.Forms.CheckBox(); + this.label3 = new System.Windows.Forms.Label(); + this.cmbTiffCompr = new System.Windows.Forms.ComboBox(); + this.cbSinglePageTiff = new System.Windows.Forms.CheckBox(); + this.groupJpeg = new System.Windows.Forms.GroupBox(); + this.groupTiff = new System.Windows.Forms.GroupBox(); ((System.ComponentModel.ISupportInitialize)(this.tbJpegQuality)).BeginInit(); + this.groupJpeg.SuspendLayout(); + this.groupTiff.SuspendLayout(); this.SuspendLayout(); // // btnOK @@ -89,11 +95,6 @@ namespace NAPS2.WinForms resources.ApplyResources(this.label1, "label1"); this.label1.Name = "label1"; // - // label2 - // - resources.ApplyResources(this.label2, "label2"); - this.label2.Name = "label2"; - // // linkPlaceholders // resources.ApplyResources(this.linkPlaceholders, "linkPlaceholders"); @@ -133,27 +134,65 @@ namespace NAPS2.WinForms this.cbSkipSavePrompt.Name = "cbSkipSavePrompt"; this.cbSkipSavePrompt.UseVisualStyleBackColor = true; // + // label3 + // + resources.ApplyResources(this.label3, "label3"); + this.label3.Name = "label3"; + // + // cmbTiffCompr + // + this.cmbTiffCompr.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.cmbTiffCompr.FormattingEnabled = true; + resources.ApplyResources(this.cmbTiffCompr, "cmbTiffCompr"); + this.cmbTiffCompr.Name = "cmbTiffCompr"; + // + // cbSinglePageTiff + // + resources.ApplyResources(this.cbSinglePageTiff, "cbSinglePageTiff"); + this.cbSinglePageTiff.Name = "cbSinglePageTiff"; + this.cbSinglePageTiff.UseVisualStyleBackColor = true; + // + // groupJpeg + // + this.groupJpeg.Controls.Add(this.lblWarning); + this.groupJpeg.Controls.Add(this.tbJpegQuality); + this.groupJpeg.Controls.Add(this.txtJpegQuality); + resources.ApplyResources(this.groupJpeg, "groupJpeg"); + this.groupJpeg.Name = "groupJpeg"; + this.groupJpeg.TabStop = false; + // + // groupTiff + // + this.groupTiff.Controls.Add(this.label3); + this.groupTiff.Controls.Add(this.cmbTiffCompr); + this.groupTiff.Controls.Add(this.cbSinglePageTiff); + resources.ApplyResources(this.groupTiff, "groupTiff"); + this.groupTiff.Name = "groupTiff"; + this.groupTiff.TabStop = false; + // // FImageSettings // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.groupTiff); + this.Controls.Add(this.groupJpeg); this.Controls.Add(this.cbSkipSavePrompt); this.Controls.Add(this.btnChooseFolder); - this.Controls.Add(this.lblWarning); - this.Controls.Add(this.txtJpegQuality); this.Controls.Add(this.linkPlaceholders); - this.Controls.Add(this.label2); this.Controls.Add(this.txtDefaultFilePath); this.Controls.Add(this.label1); this.Controls.Add(this.btnRestoreDefaults); this.Controls.Add(this.cbRememberSettings); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnOK); - this.Controls.Add(this.tbJpegQuality); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FImageSettings"; ((System.ComponentModel.ISupportInitialize)(this.tbJpegQuality)).EndInit(); + this.groupJpeg.ResumeLayout(false); + this.groupJpeg.PerformLayout(); + this.groupTiff.ResumeLayout(false); + this.groupTiff.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -168,12 +207,16 @@ namespace NAPS2.WinForms private System.Windows.Forms.Button btnRestoreDefaults; private System.Windows.Forms.TextBox txtDefaultFilePath; private System.Windows.Forms.Label label1; - private System.Windows.Forms.Label label2; private System.Windows.Forms.LinkLabel linkPlaceholders; private System.Windows.Forms.TextBox txtJpegQuality; private System.Windows.Forms.TrackBar tbJpegQuality; private System.Windows.Forms.Label lblWarning; private System.Windows.Forms.Button btnChooseFolder; private System.Windows.Forms.CheckBox cbSkipSavePrompt; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.ComboBox cmbTiffCompr; + private System.Windows.Forms.CheckBox cbSinglePageTiff; + private System.Windows.Forms.GroupBox groupJpeg; + private System.Windows.Forms.GroupBox groupTiff; } } diff --git a/NAPS2.Core/WinForms/FImageSettings.cs b/NAPS2.Core/WinForms/FImageSettings.cs index 31958bd3d..20c8ae067 100644 --- a/NAPS2.Core/WinForms/FImageSettings.cs +++ b/NAPS2.Core/WinForms/FImageSettings.cs @@ -22,6 +22,7 @@ namespace NAPS2.WinForms this.userConfigManager = userConfigManager; this.dialogHelper = dialogHelper; InitializeComponent(); + AddEnumItems(cmbTiffCompr); } protected override void OnLoad(object sender, EventArgs e) @@ -31,7 +32,7 @@ namespace NAPS2.WinForms .BottomToForm() .Bind(txtJpegQuality, btnOK, btnCancel, btnChooseFolder) .RightToForm() - .Bind(txtDefaultFilePath, tbJpegQuality, lblWarning) + .Bind(txtDefaultFilePath, tbJpegQuality, lblWarning, groupTiff, groupJpeg) .WidthToForm() .Activate(); @@ -45,6 +46,8 @@ namespace NAPS2.WinForms txtDefaultFilePath.Text = imageSettings.DefaultFileName; cbSkipSavePrompt.Checked = imageSettings.SkipSavePrompt; txtJpegQuality.Text = imageSettings.JpegQuality.ToString(CultureInfo.InvariantCulture); + cmbTiffCompr.SelectedIndex = (int) imageSettings.TiffCompression; + cbSinglePageTiff.Checked = imageSettings.SinglePageTiff; } private void UpdateEnabled() @@ -63,7 +66,9 @@ namespace NAPS2.WinForms { DefaultFileName = txtDefaultFilePath.Text, SkipSavePrompt = cbSkipSavePrompt.Checked, - JpegQuality = tbJpegQuality.Value + JpegQuality = tbJpegQuality.Value, + TiffCompression = (TiffCompression)cmbTiffCompr.SelectedIndex, + SinglePageTiff = cbSinglePageTiff.Checked }; imageSettingsContainer.ImageSettings = imageSettings; diff --git a/NAPS2.Core/WinForms/FImageSettings.resx b/NAPS2.Core/WinForms/FImageSettings.resx index 7fc14a8bf..aaa65bfc0 100644 --- a/NAPS2.Core/WinForms/FImageSettings.resx +++ b/NAPS2.Core/WinForms/FImageSettings.resx @@ -123,7 +123,7 @@ - 264, 204 + 276, 299 75, 23 @@ -145,13 +145,13 @@ $this - 11 + 10 NoControl - 345, 204 + 357, 299 75, 23 @@ -172,7 +172,7 @@ $this - 10 + 9 17, 17 @@ -181,7 +181,7 @@ True - 12, 183 + 13, 278 145, 17 @@ -202,13 +202,13 @@ $this - 9 + 8 True - 12, 204 + 12, 299 145, 23 @@ -229,13 +229,13 @@ $this - 8 + 7 12, 25 - 368, 20 + 380, 20 27 @@ -250,7 +250,7 @@ $this - 6 + 5 True @@ -280,34 +280,7 @@ $this - 7 - - - True - - - 12, 97 - - - 68, 13 - - - 29 - - - Jpeg Quality: - - - label2 - - - System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 5 + 6 True @@ -337,7 +310,7 @@ 4 - 382, 113 + 376, 19 38, 20 @@ -355,16 +328,16 @@ System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - $this + groupJpeg - 3 + 2 NoControl - 12, 113 + 6, 19 364, 45 @@ -379,16 +352,16 @@ System.Windows.Forms.TrackBar, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - $this + groupJpeg - 12 + 1 NoControl - 12, 145 + 6, 51 408, 30 @@ -397,7 +370,7 @@ 33 - For high JPEG qualities (80+), use the Maximum Quality profile option for best results. + For high JPEG qualities (80+), also increase Image Quality in your profile for best results. lblWarning @@ -406,16 +379,16 @@ System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - $this + groupJpeg - 2 + 0 NoControl - 386, 24 + 398, 23 34, 22 @@ -436,7 +409,7 @@ $this - 1 + 3 True @@ -466,6 +439,135 @@ $this + 2 + + + True + + + NoControl + + + 6, 39 + + + 70, 13 + + + 37 + + + Compression: + + + label3 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + groupTiff + + + 0 + + + 6, 55 + + + 185, 21 + + + 38 + + + cmbTiffCompr + + + System.Windows.Forms.ComboBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + groupTiff + + + 1 + + + True + + + NoControl + + + 9, 19 + + + 103, 17 + + + 39 + + + Single page files + + + cbSinglePageTiff + + + System.Windows.Forms.CheckBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + groupTiff + + + 2 + + + 12, 91 + + + 420, 87 + + + 40 + + + Jpeg Quality + + + groupJpeg + + + System.Windows.Forms.GroupBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + 12, 184 + + + 420, 87 + + + 41 + + + Tiff Options + + + groupTiff + + + System.Windows.Forms.GroupBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + 0 @@ -475,7 +577,7 @@ 6, 13 - 432, 239 + 444, 334 @@ -502,10 +604,10 @@ - 800, 278 + 800, 373 - 400, 278 + 400, 373 Image Settings @@ -514,12 +616,12 @@ ilProfileIcons - NAPS2.WinForms.ILProfileIcons, NAPS2.Core, Version=5.0.2.26149, Culture=neutral, PublicKeyToken=null + NAPS2.WinForms.ILProfileIcons, NAPS2.Core, Version=5.7.1.32493, Culture=neutral, PublicKeyToken=null FImageSettings - NAPS2.WinForms.FormBase, NAPS2.Core, Version=5.0.2.26149, Culture=neutral, PublicKeyToken=null + NAPS2.WinForms.FormBase, NAPS2.Core, Version=5.7.1.32493, Culture=neutral, PublicKeyToken=null \ No newline at end of file diff --git a/NAPS2.Core/WinForms/FPdfSettings.cs b/NAPS2.Core/WinForms/FPdfSettings.cs index ac328e8e8..738652630 100644 --- a/NAPS2.Core/WinForms/FPdfSettings.cs +++ b/NAPS2.Core/WinForms/FPdfSettings.cs @@ -28,7 +28,7 @@ namespace NAPS2.WinForms new LayoutManager(this) .Bind(btnOK, btnCancel, cbShowOwnerPassword, cbShowUserPassword, btnChooseFolder) .RightToForm() - .Bind(groupMetadata, groupProtection) + .Bind(groupMetadata, groupProtection, groupCompat, clbPerms) .WidthToForm() .Bind(txtDefaultFilePath, txtTitle, txtAuthor, txtSubject, txtKeywords, txtOwnerPassword, txtUserPassword) .WidthToForm()