mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
Code quality changes, and added the possibility to set the amount of colors for the Quantizer.
This commit is contained in:
parent
3b1560390b
commit
77a92d98c3
92 changed files with 690 additions and 653 deletions
|
@ -18,29 +18,32 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using Greenshot.IniFile;
|
||||
using Greenshot.Plugin;
|
||||
using GreenshotPlugin.Controls;
|
||||
using log4net;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
using Greenshot.IniFile;
|
||||
using Greenshot.Plugin;
|
||||
using GreenshotPlugin.Controls;
|
||||
using Greenshot.Core;
|
||||
using System.Diagnostics;
|
||||
using System.Security.AccessControl;
|
||||
using Encoder = System.Drawing.Imaging.Encoder;
|
||||
|
||||
namespace GreenshotPlugin.Core {
|
||||
/// <summary>
|
||||
/// Description of ImageOutput.
|
||||
/// </summary>
|
||||
public static class ImageOutput {
|
||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImageOutput));
|
||||
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(ImageOutput));
|
||||
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
||||
private static readonly int PROPERTY_TAG_SOFTWARE_USED = 0x0131;
|
||||
private static Cache<string, string> tmpFileCache = new Cache<string, string>(10 * 60 * 60, new Cache<string, string>.CacheObjectExpired(RemoveExpiredTmpFile));
|
||||
private static Cache<string, string> tmpFileCache = new Cache<string, string>(10 * 60 * 60, RemoveExpiredTmpFile);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a PropertyItem (Metadata) to store with the image.
|
||||
|
@ -60,7 +63,7 @@ namespace GreenshotPlugin.Core {
|
|||
// Set the ID
|
||||
propertyItem.Id = id;
|
||||
// Set the text
|
||||
byte[] byteString = System.Text.ASCIIEncoding.ASCII.GetBytes(text + " ");
|
||||
byte[] byteString = Encoding.ASCII.GetBytes(text + " ");
|
||||
// Set Zero byte for String end.
|
||||
byteString[byteString.Length - 1] = 0;
|
||||
propertyItem.Value = byteString;
|
||||
|
@ -78,7 +81,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="stream">Stream to save to</param>
|
||||
/// <param name="outputSettings">SurfaceOutputSettings</param>
|
||||
public static void SaveToStream(ISurface surface, Stream stream, SurfaceOutputSettings outputSettings) {
|
||||
Image imageToSave = null;
|
||||
Image imageToSave;
|
||||
bool disposeImage = CreateImageFromSurface(surface, outputSettings, out imageToSave);
|
||||
SaveToStream(imageToSave, surface, stream, outputSettings);
|
||||
// cleanup if needed
|
||||
|
@ -97,7 +100,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="stream">Stream to save to</param>
|
||||
/// <param name="outputSettings">SurfaceOutputSettings</param>
|
||||
public static void SaveToStream(Image imageToSave, ISurface surface, Stream stream, SurfaceOutputSettings outputSettings) {
|
||||
ImageFormat imageFormat = null;
|
||||
ImageFormat imageFormat;
|
||||
bool useMemoryStream = false;
|
||||
MemoryStream memoryStream = null;
|
||||
if (outputSettings.Format == OutputFormat.greenshot && surface == null) {
|
||||
|
@ -118,8 +121,6 @@ namespace GreenshotPlugin.Core {
|
|||
case OutputFormat.tiff:
|
||||
imageFormat = ImageFormat.Tiff;
|
||||
break;
|
||||
case OutputFormat.greenshot:
|
||||
case OutputFormat.png:
|
||||
default:
|
||||
// Problem with non-seekable streams most likely doesn't happen with Windows 7 (OS Version 6.1 and later)
|
||||
// http://stackoverflow.com/questions/8349260/generic-gdi-error-on-one-machine-but-not-the-other
|
||||
|
@ -144,7 +145,7 @@ namespace GreenshotPlugin.Core {
|
|||
targetStream = memoryStream;
|
||||
}
|
||||
|
||||
if (imageFormat == ImageFormat.Jpeg) {
|
||||
if (Equals(imageFormat, ImageFormat.Jpeg)) {
|
||||
bool foundEncoder = false;
|
||||
foreach (ImageCodecInfo imageCodec in ImageCodecInfo.GetImageEncoders()) {
|
||||
if (imageCodec.FormatID == imageFormat.Guid) {
|
||||
|
@ -171,14 +172,14 @@ namespace GreenshotPlugin.Core {
|
|||
} else {
|
||||
bool needsDispose = false;
|
||||
// Removing transparency if it's not supported in the output
|
||||
if (imageFormat != ImageFormat.Png && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) {
|
||||
if (!Equals(imageFormat, ImageFormat.Png) && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) {
|
||||
imageToSave = ImageHelper.Clone(imageToSave, PixelFormat.Format24bppRgb);
|
||||
needsDispose = true;
|
||||
}
|
||||
AddTag(imageToSave);
|
||||
// Added for OptiPNG
|
||||
bool processed = false;
|
||||
if (imageFormat == ImageFormat.Png && !string.IsNullOrEmpty(conf.OptimizePNGCommand)) {
|
||||
if (Equals(imageFormat, ImageFormat.Png) && !string.IsNullOrEmpty(conf.OptimizePNGCommand)) {
|
||||
processed = ProcessPNGImageExternally(imageToSave, targetStream);
|
||||
}
|
||||
if (!processed) {
|
||||
|
@ -202,7 +203,7 @@ namespace GreenshotPlugin.Core {
|
|||
using (BinaryWriter writer = new BinaryWriter(tmpStream)) {
|
||||
writer.Write(bytesWritten);
|
||||
Version v = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
byte[] marker = System.Text.Encoding.ASCII.GetBytes(String.Format("Greenshot{0:00}.{1:00}", v.Major, v.Minor));
|
||||
byte[] marker = Encoding.ASCII.GetBytes(String.Format("Greenshot{0:00}.{1:00}", v.Major, v.Minor));
|
||||
writer.Write(marker);
|
||||
tmpStream.WriteTo(stream);
|
||||
}
|
||||
|
@ -249,19 +250,21 @@ namespace GreenshotPlugin.Core {
|
|||
processStartInfo.RedirectStandardError = true;
|
||||
processStartInfo.UseShellExecute = false;
|
||||
Process process = Process.Start(processStartInfo);
|
||||
process.WaitForExit();
|
||||
if (process.ExitCode == 0) {
|
||||
if (LOG.IsDebugEnabled) {
|
||||
LOG.DebugFormat("File size after processing {0}", new FileInfo(tmpFileName).Length);
|
||||
LOG.DebugFormat("Reading back tmp file: {0}", tmpFileName);
|
||||
if (process != null) {
|
||||
process.WaitForExit();
|
||||
if (process.ExitCode == 0) {
|
||||
if (LOG.IsDebugEnabled) {
|
||||
LOG.DebugFormat("File size after processing {0}", new FileInfo(tmpFileName).Length);
|
||||
LOG.DebugFormat("Reading back tmp file: {0}", tmpFileName);
|
||||
}
|
||||
byte[] processedImage = File.ReadAllBytes(tmpFileName);
|
||||
targetStream.Write(processedImage, 0, processedImage.Length);
|
||||
return true;
|
||||
}
|
||||
byte[] processedImage = File.ReadAllBytes(tmpFileName);
|
||||
targetStream.Write(processedImage, 0, processedImage.Length);
|
||||
return true;
|
||||
LOG.ErrorFormat("Error while processing PNG image: {0}", process.ExitCode);
|
||||
LOG.ErrorFormat("Output: {0}", process.StandardOutput.ReadToEnd());
|
||||
LOG.ErrorFormat("Error: {0}", process.StandardError.ReadToEnd());
|
||||
}
|
||||
LOG.ErrorFormat("Error while processing PNG image: {0}", process.ExitCode);
|
||||
LOG.ErrorFormat("Output: {0}", process.StandardOutput.ReadToEnd());
|
||||
LOG.ErrorFormat("Error: {0}", process.StandardError.ReadToEnd());
|
||||
} catch (Exception e) {
|
||||
LOG.Error("Error while processing PNG image: ", e);
|
||||
} finally {
|
||||
|
@ -282,26 +285,6 @@ namespace GreenshotPlugin.Core {
|
|||
/// <returns>true if the image must be disposed</returns>
|
||||
public static bool CreateImageFromSurface(ISurface surface, SurfaceOutputSettings outputSettings, out Image imageToSave) {
|
||||
bool disposeImage = false;
|
||||
ImageFormat imageFormat = null;
|
||||
switch (outputSettings.Format) {
|
||||
case OutputFormat.bmp:
|
||||
imageFormat = ImageFormat.Bmp;
|
||||
break;
|
||||
case OutputFormat.gif:
|
||||
imageFormat = ImageFormat.Gif;
|
||||
break;
|
||||
case OutputFormat.jpg:
|
||||
imageFormat = ImageFormat.Jpeg;
|
||||
break;
|
||||
case OutputFormat.tiff:
|
||||
imageFormat = ImageFormat.Tiff;
|
||||
break;
|
||||
case OutputFormat.greenshot:
|
||||
case OutputFormat.png:
|
||||
default:
|
||||
imageFormat = ImageFormat.Png;
|
||||
break;
|
||||
}
|
||||
|
||||
if (outputSettings.Format == OutputFormat.greenshot || outputSettings.SaveBackgroundOnly) {
|
||||
// We save the image of the surface, this should not be disposed
|
||||
|
@ -313,47 +296,50 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
// The following block of modifications should be skipped when saving the greenshot format, no effects or otherwise!
|
||||
if (outputSettings.Format != OutputFormat.greenshot) {
|
||||
Image tmpImage;
|
||||
if (outputSettings.Effects != null && outputSettings.Effects.Count > 0) {
|
||||
// apply effects, if there are any
|
||||
Point ignoreOffset;
|
||||
tmpImage = ImageHelper.ApplyEffects((Bitmap)imageToSave, outputSettings.Effects, out ignoreOffset);
|
||||
if (tmpImage != null) {
|
||||
if (outputSettings.Format == OutputFormat.greenshot) {
|
||||
return disposeImage;
|
||||
}
|
||||
Image tmpImage;
|
||||
if (outputSettings.Effects != null && outputSettings.Effects.Count > 0) {
|
||||
// apply effects, if there are any
|
||||
Point ignoreOffset;
|
||||
tmpImage = ImageHelper.ApplyEffects(imageToSave, outputSettings.Effects, out ignoreOffset);
|
||||
if (tmpImage != null) {
|
||||
if (disposeImage) {
|
||||
imageToSave.Dispose();
|
||||
}
|
||||
imageToSave = tmpImage;
|
||||
disposeImage = true;
|
||||
}
|
||||
}
|
||||
|
||||
// check for color reduction, forced or automatically, only when the DisableReduceColors is false
|
||||
if (outputSettings.DisableReduceColors || (!conf.OutputFileAutoReduceColors && !outputSettings.ReduceColors)) {
|
||||
return disposeImage;
|
||||
}
|
||||
bool isAlpha = Image.IsAlphaPixelFormat(imageToSave.PixelFormat);
|
||||
if (outputSettings.ReduceColors || (!isAlpha && conf.OutputFileAutoReduceColors)) {
|
||||
using (var quantizer = new WuQuantizer((Bitmap)imageToSave)) {
|
||||
int colorCount = quantizer.GetColorCount();
|
||||
LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
|
||||
if (!outputSettings.ReduceColors && colorCount >= 256) {
|
||||
return disposeImage;
|
||||
}
|
||||
try {
|
||||
LOG.Info("Reducing colors on bitmap to 256.");
|
||||
tmpImage = quantizer.GetQuantizedImage(conf.OutputFileReduceColorsTo);
|
||||
if (disposeImage) {
|
||||
imageToSave.Dispose();
|
||||
}
|
||||
imageToSave = tmpImage;
|
||||
// Make sure the "new" image is disposed
|
||||
disposeImage = true;
|
||||
} catch (Exception e) {
|
||||
LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
// check for color reduction, forced or automatically, only when the DisableReduceColors is false
|
||||
if (!outputSettings.DisableReduceColors && (conf.OutputFileAutoReduceColors || outputSettings.ReduceColors)) {
|
||||
bool isAlpha = Image.IsAlphaPixelFormat(imageToSave.PixelFormat);
|
||||
if (outputSettings.ReduceColors || (!isAlpha && conf.OutputFileAutoReduceColors)) {
|
||||
using (WuQuantizer quantizer = new WuQuantizer((Bitmap)imageToSave)) {
|
||||
int colorCount = quantizer.GetColorCount();
|
||||
LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
|
||||
if (outputSettings.ReduceColors || colorCount < 256) {
|
||||
try {
|
||||
LOG.Info("Reducing colors on bitmap to 256.");
|
||||
tmpImage = quantizer.GetQuantizedImage(256);
|
||||
if (disposeImage) {
|
||||
imageToSave.Dispose();
|
||||
}
|
||||
imageToSave = tmpImage;
|
||||
// Make sure the "new" image is disposed
|
||||
disposeImage = true;
|
||||
} catch (Exception e) {
|
||||
LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isAlpha && !outputSettings.ReduceColors) {
|
||||
LOG.Info("Skipping 'optional' color reduction as the image has alpha");
|
||||
}
|
||||
}
|
||||
} else if (isAlpha && !outputSettings.ReduceColors) {
|
||||
LOG.Info("Skipping 'optional' color reduction as the image has alpha");
|
||||
}
|
||||
return disposeImage;
|
||||
}
|
||||
|
@ -378,12 +364,13 @@ namespace GreenshotPlugin.Core {
|
|||
/// Load a Greenshot surface
|
||||
/// </summary>
|
||||
/// <param name="fullPath"></param>
|
||||
/// <param name="returnSurface"></param>
|
||||
/// <returns></returns>
|
||||
public static ISurface LoadGreenshotSurface(string fullPath, ISurface returnSurface) {
|
||||
if (string.IsNullOrEmpty(fullPath)) {
|
||||
return null;
|
||||
}
|
||||
Image fileImage = null;
|
||||
Image fileImage;
|
||||
LOG.InfoFormat("Loading image from file {0}", fullPath);
|
||||
// Fixed lock problem Bug #3431881
|
||||
using (Stream surfaceFileStream = File.OpenRead(fullPath)) {
|
||||
|
@ -402,15 +389,14 @@ namespace GreenshotPlugin.Core {
|
|||
string greenshotMarker;
|
||||
using (StreamReader streamReader = new StreamReader(surfaceFileStream)) {
|
||||
greenshotMarker = streamReader.ReadToEnd();
|
||||
if (greenshotMarker == null || !greenshotMarker.StartsWith("Greenshot")) {
|
||||
if (!greenshotMarker.StartsWith("Greenshot")) {
|
||||
throw new ArgumentException(string.Format("{0} is not a Greenshot file!", fullPath));
|
||||
}
|
||||
LOG.InfoFormat("Greenshot file format: {0}", greenshotMarker);
|
||||
const int filesizeLocation = 8 + markerSize;
|
||||
surfaceFileStream.Seek(-filesizeLocation, SeekOrigin.End);
|
||||
long bytesWritten = 0;
|
||||
using (BinaryReader reader = new BinaryReader(surfaceFileStream)) {
|
||||
bytesWritten = reader.ReadInt64();
|
||||
long bytesWritten = reader.ReadInt64();
|
||||
surfaceFileStream.Seek(-(bytesWritten + filesizeLocation), SeekOrigin.End);
|
||||
returnSurface.LoadElementsFromStream(surfaceFileStream);
|
||||
}
|
||||
|
@ -431,9 +417,11 @@ namespace GreenshotPlugin.Core {
|
|||
string path = Path.GetDirectoryName(fullPath);
|
||||
|
||||
// check whether path exists - if not create it
|
||||
DirectoryInfo di = new DirectoryInfo(path);
|
||||
if (!di.Exists) {
|
||||
Directory.CreateDirectory(di.FullName);
|
||||
if (path != null) {
|
||||
DirectoryInfo di = new DirectoryInfo(path);
|
||||
if (!di.Exists) {
|
||||
Directory.CreateDirectory(di.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
if (!allowOverwrite && File.Exists(fullPath)) {
|
||||
|
@ -462,9 +450,7 @@ namespace GreenshotPlugin.Core {
|
|||
string extension = fullPath.Substring(fullPath.LastIndexOf(".") + 1);
|
||||
OutputFormat format = OutputFormat.png;
|
||||
try {
|
||||
if (extension != null) {
|
||||
format = (OutputFormat)Enum.Parse(typeof(OutputFormat), extension.ToLower());
|
||||
}
|
||||
format = (OutputFormat)Enum.Parse(typeof(OutputFormat), extension.ToLower());
|
||||
} catch (ArgumentException ae) {
|
||||
LOG.Warn("Couldn't parse extension: " + extension, ae);
|
||||
}
|
||||
|
@ -493,10 +479,10 @@ namespace GreenshotPlugin.Core {
|
|||
qualityDialog.ShowDialog();
|
||||
}
|
||||
// TODO: For now we always overwrite, should be changed
|
||||
ImageOutput.Save(surface, fileNameWithExtension, true, outputSettings, conf.OutputFileCopyPathToClipboard);
|
||||
Save(surface, fileNameWithExtension, true, outputSettings, conf.OutputFileCopyPathToClipboard);
|
||||
returnValue = fileNameWithExtension;
|
||||
IniConfig.Save();
|
||||
} catch (System.Runtime.InteropServices.ExternalException) {
|
||||
} catch (ExternalException) {
|
||||
MessageBox.Show(Language.GetFormattedString("error_nowriteaccess", saveImageFileDialog.FileName).Replace(@"\\", @"\"), Language.GetString("error"));
|
||||
}
|
||||
}
|
||||
|
@ -530,13 +516,13 @@ namespace GreenshotPlugin.Core {
|
|||
// Catching any exception to prevent that the user can't write in the directory.
|
||||
// This is done for e.g. bugs #2974608, #2963943, #2816163, #2795317, #2789218
|
||||
try {
|
||||
ImageOutput.Save(surface, tmpFile, true, outputSettings, false);
|
||||
Save(surface, tmpFile, true, outputSettings, false);
|
||||
tmpFileCache.Add(tmpFile, tmpFile);
|
||||
} catch (Exception e) {
|
||||
// Show the problem
|
||||
MessageBox.Show(e.Message, "Error");
|
||||
// when save failed we present a SaveWithDialog
|
||||
tmpFile = ImageOutput.SaveWithDialog(surface, captureDetails);
|
||||
tmpFile = SaveWithDialog(surface, captureDetails);
|
||||
}
|
||||
return tmpFile;
|
||||
}
|
||||
|
@ -555,7 +541,8 @@ namespace GreenshotPlugin.Core {
|
|||
tmpFileCache.Remove(tmpfile);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception) {
|
||||
} catch (Exception ex) {
|
||||
LOG.Warn("Error deleting tmp file: ", ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -563,7 +550,9 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Helper method to create a temp image file
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
/// <param name="surface"></param>
|
||||
/// <param name="outputSettings"></param>
|
||||
/// <param name="destinationPath"></param>
|
||||
/// <returns></returns>
|
||||
public static string SaveToTmpFile(ISurface surface, SurfaceOutputSettings outputSettings, string destinationPath) {
|
||||
string tmpFile = Path.GetRandomFileName() + "." + outputSettings.Format.ToString();
|
||||
|
@ -576,7 +565,7 @@ namespace GreenshotPlugin.Core {
|
|||
LOG.Debug("Creating TMP File : " + tmpPath);
|
||||
|
||||
try {
|
||||
ImageOutput.Save(surface, tmpPath, true, outputSettings, false);
|
||||
Save(surface, tmpPath, true, outputSettings, false);
|
||||
tmpFileCache.Add(tmpPath, tmpPath);
|
||||
} catch (Exception) {
|
||||
return null;
|
||||
|
@ -600,8 +589,8 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Cleanup handler for expired tempfiles
|
||||
/// </summary>
|
||||
/// <param name="filekey"></param>
|
||||
/// <param name="filename"></param>
|
||||
/// <param name="alsoTheFilename"></param>
|
||||
private static void RemoveExpiredTmpFile(string filekey, object filename) {
|
||||
string path = filename as string;
|
||||
if (path != null && File.Exists(path)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue