Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to enable PDF image compression with a customizable quality #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions NAPS2.Core/ImportExport/Pdf/PdfImageSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
NAPS2 (Not Another PDF Scanner 2)
http://sourceforge.net/projects/naps2/

Copyright (C) 2015 Luca De Petrillo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

using NAPS2.Lang.Resources;

namespace NAPS2.ImportExport.Pdf
{
public class PdfImageSettings
{
public const int DEFAULT_JPEG_QUALITY = 75;
public const bool DEFAULT_COMPRESS_IMAGES = false;

public PdfImageSettings()
{
JpegQuality = DEFAULT_JPEG_QUALITY;
CompressImages = DEFAULT_COMPRESS_IMAGES;
}

public int JpegQuality { get; set; }

public bool CompressImages { get; set; }
}
}
16 changes: 16 additions & 0 deletions NAPS2.Core/ImportExport/Pdf/PdfSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Copyright (C) 2009 Pavel Sorejs
Copyright (C) 2012 Michael Adams
Copyright (C) 2013 Peter De Leeuw
Copyright (C) 2015 Luca De Petrillo
Copyright (C) 2012-2015 Ben Olden-Cooligan

This program is free software; you can redistribute it and/or
Expand All @@ -27,11 +28,13 @@ namespace NAPS2.ImportExport.Pdf
public class PdfSettings
{
private PdfMetadata metadata;
private PdfImageSettings imageSettings;
private PdfEncryption encryption;

public PdfSettings()
{
metadata = new PdfMetadata();
imageSettings = new PdfImageSettings();
encryption = new PdfEncryption();
}

Expand All @@ -50,6 +53,19 @@ public PdfMetadata Metadata
}
}

public PdfImageSettings ImageSettings
{
get { return imageSettings; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
imageSettings = value;
}
}

public PdfEncryption Encryption
{
get { return encryption; }
Expand Down
34 changes: 33 additions & 1 deletion NAPS2.Core/ImportExport/Pdf/PdfSharpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Copyright (C) 2009 Pavel Sorejs
Copyright (C) 2012 Michael Adams
Copyright (C) 2013 Peter De Leeuw
Copyright (C) 2015 Luca De Petrillo
Copyright (C) 2012-2015 Ben Olden-Cooligan

This program is free software; you can redistribute it and/or
Expand All @@ -30,6 +31,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;
using System.Drawing.Imaging;

namespace NAPS2.ImportExport.Pdf
{
Expand Down Expand Up @@ -107,7 +109,37 @@ public bool Export(string path, IEnumerable<IScannedImage> images, PdfSettings s
tf.DrawString(element.Text, new XFont("Times New Roman", adjustedFontSize, XFontStyle.Regular), XBrushes.Transparent, adjustedBounds);
}
}
gfx.DrawImage(img, 0, 0, (int)realWidth, (int)realHeight);

if (scannedImage.IsHighQuality() && settings.ImageSettings.CompressImages)
{
// Compress the image to JPEG and use it if smaller than the original one.
var quality = Math.Max(Math.Min(settings.ImageSettings.JpegQuality, 100), 0);
var encoder = ImageCodecInfo.GetImageEncoders().First(x => x.FormatID == ImageFormat.Jpeg.Guid);
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);

using (var streamJpg = new MemoryStream())
{
img.Save(streamJpg, encoder, encoderParams);
if (streamJpg.Length < stream.Length)
{
using (var imgJpg = Bitmap.FromStream(streamJpg))
{
gfx.DrawImage(imgJpg, 0, 0, (int)realWidth, (int)realHeight);
}
}
else
{
gfx.DrawImage(img, 0, 0, (int)realWidth, (int)realHeight);
}

}
}
else
{
gfx.DrawImage(img, 0, 0, (int)realWidth, (int)realHeight);
}

i++;
}
}
Expand Down
1 change: 1 addition & 0 deletions NAPS2.Core/NAPS2.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Icons.resx</DependentUpon>
</Compile>
<Compile Include="ImportExport\Pdf\PdfImageSettings.cs" />
<Compile Include="ImportExport\Pdf\PdfSaver.cs" />
<Compile Include="Util\ChangeTracker.cs" />
<Compile Include="Util\CollectionExtensions.cs" />
Expand Down
9 changes: 9 additions & 0 deletions NAPS2.Core/Scan/Images/FileBasedScannedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Copyright (C) 2009 Pavel Sorejs
Copyright (C) 2012 Michael Adams
Copyright (C) 2013 Peter De Leeuw
Copyright (C) 2015 Luca De Petrillo
Copyright (C) 2012-2015 Ben Olden-Cooligan

This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -69,9 +70,12 @@ private static DirectoryInfo RecoveryFolder
// Store a base image and transform pair (rather than doing the actual transform on the base image)
// so that JPEG degradation is minimized when multiple rotations/flips are performed
private readonly List<Transform> transformList = new List<Transform>();
private readonly bool highQuality;

public FileBasedScannedImage(Bitmap img, ScanBitDepth bitDepth, bool highQuality)
{
this.highQuality = highQuality;

Bitmap baseImage;
MemoryStream baseImageEncoded;
ScannedImageHelper.GetSmallestBitmap(img, bitDepth, highQuality, out baseImage, out baseImageEncoded, out baseImageFileFormat);
Expand Down Expand Up @@ -214,5 +218,10 @@ public void MovedTo(int index)
_recoveryIndexManager.Index.Images.Insert(index, indexImage);
_recoveryIndexManager.Save();
}

public bool IsHighQuality()
{
return highQuality;
}
}
}
7 changes: 7 additions & 0 deletions NAPS2.Core/Scan/Images/IScannedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Copyright (C) 2009 Pavel Sorejs
Copyright (C) 2012 Michael Adams
Copyright (C) 2013 Peter De Leeuw
Copyright (C) 2015 Luca De Petrillo
Copyright (C) 2012-2015 Ben Olden-Cooligan

This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -77,5 +78,11 @@ public interface IScannedImage : IDisposable
/// </summary>
/// <param name="index">The index at which the image was inserted after being removed.</param>
void MovedTo(int index);


/// <summary>
/// Indicates if the scanned image has been acquired using "High Quality" configuration, or if it has been imported using a lossless format.
/// </summary>
bool IsHighQuality();
}
}
10 changes: 10 additions & 0 deletions NAPS2.Core/Scan/Images/ScannedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Copyright (C) 2009 Pavel Sorejs
Copyright (C) 2012 Michael Adams
Copyright (C) 2013 Peter De Leeuw
Copyright (C) 2015 Luca De Petrillo
Copyright (C) 2012-2015 Ben Olden-Cooligan

This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -34,6 +35,7 @@ public class ScannedImage : IScannedImage
private Bitmap thumbnail;
// The image's bit depth (or C24Bit if unknown)
private readonly ScanBitDepth bitDepth;
private readonly bool highQuality;
// Only one of the following (baseImage/baseImageEncoded) should have a value for any particular ScannedImage
private readonly Bitmap baseImage;
private readonly MemoryStream baseImageEncoded;
Expand All @@ -42,9 +44,12 @@ public class ScannedImage : IScannedImage
// so that JPEG degradation is minimized when multiple rotations/flips are performed
private readonly List<Transform> transformList = new List<Transform>();


public ScannedImage(Bitmap img, ScanBitDepth bitDepth, bool highQuality)
{
this.bitDepth = bitDepth;
this.highQuality = highQuality;

ScannedImageHelper.GetSmallestBitmap(img, bitDepth, highQuality, out baseImage, out baseImageEncoded, out baseImageFileFormat);
}

Expand Down Expand Up @@ -126,5 +131,10 @@ public void MovedTo(int index)
{
// Do nothing, this is only important for FileBasedScannedImage
}

public bool IsHighQuality()
{
return highQuality;
}
}
}
60 changes: 60 additions & 0 deletions NAPS2.Core/WinForms/FPdfSettings.Designer.cs

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

Loading