Applied my "patch" which refactors the complete codebase to have IEffect support, this is an interface which can be used to supply effects to a bitmap. These effects can now be passed to the OutputSettings (which now is called SurfaceOutputSettings) to control how the output is modified. This is very useful for e.g. OCR which needs a grayscale & resized image.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2377 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-12-12 09:51:41 +00:00
commit 0c7e16a771
33 changed files with 212 additions and 148 deletions

View file

@ -29,6 +29,7 @@ using Greenshot.Helpers;
using Greenshot.Plugin.Drawing;
using GreenshotPlugin.Core;
using System.Drawing.Drawing2D;
using Greenshot.Core;
namespace Greenshot.Drawing {
/// <summary>
@ -91,13 +92,8 @@ namespace Greenshot.Drawing {
public Bitmap Bitmap {
set {
if (bitmap != null) {
bitmap.Dispose();
}
if (shadowBitmap != null) {
shadowBitmap.Dispose();
shadowBitmap = null;
}
// Remove all current bitmaps
Dispose(true);
bitmap = ImageHelper.Clone(value);
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
CheckShadow(shadow);
@ -114,29 +110,29 @@ namespace Greenshot.Drawing {
get { return bitmap; }
}
/**
* Destructor
*/
/// <summary>
/// Destructor
/// </summary>
~BitmapContainer() {
Dispose(false);
}
/**
* The public accessible Dispose
* Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
*/
/// <summary>
/// The public accessible Dispose
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary>
public new void Dispose() {
Dispose(true);
base.Dispose();
GC.SuppressFinalize(this);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
/**
* This Dispose is called from the Dispose and the Destructor.
* When disposing==true all non-managed resources should be freed too!
*/
/// <summary>
/// The bulk of the clean-up code is implemented in Dispose(bool)
/// This Dispose is called from the Dispose and the Destructor.
/// When disposing==true all non-managed resources should be freed too!
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (bitmap != null) {
@ -150,6 +146,10 @@ namespace Greenshot.Drawing {
shadowBitmap = null;
}
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
public void Load(string filename) {
if (File.Exists(filename)) {
// Always make sure ImageHelper.LoadBitmap results are disposed some time,
@ -161,21 +161,35 @@ namespace Greenshot.Drawing {
}
}
/// <summary>
/// Rotate the bitmap
/// </summary>
/// <param name="rotateFlipType"></param>
public override void Rotate(RotateFlipType rotateFlipType) {
Bitmap newBitmap = ImageHelper.RotateFlip((Bitmap)bitmap, rotateFlipType);
if (bitmap != null) {
bitmap.Dispose();
if (newBitmap != null) {
// Remove all current bitmaps, also the shadow (will be recreated)
Dispose(true);
bitmap = newBitmap;
}
bitmap = newBitmap;
base.Rotate(rotateFlipType);
}
/// <summary>
/// This checks if a shadow is already generated
/// </summary>
/// <param name="shadow"></param>
private void CheckShadow(bool shadow) {
if (shadow && shadowBitmap == null) {
shadowBitmap = ImageHelper.CreateShadow(bitmap, 1f, 6, ref shadowOffset, PixelFormat.Format32bppArgb);
shadowBitmap = ImageHelper.ApplyEffect(bitmap, new DropShadowEffect(), out shadowOffset);
}
}
/// <summary>
/// Draw the actual container to the graphics object
/// </summary>
/// <param name="graphics"></param>
/// <param name="rm"></param>
public override void Draw(Graphics graphics, RenderMode rm) {
if (bitmap != null) {
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);

View file

@ -37,6 +37,7 @@ using Greenshot.IniFile;
using Greenshot.Drawing.Filters;
using System.Drawing.Drawing2D;
using GreenshotPlugin.Controls;
using Greenshot.Core;
namespace Greenshot.Drawing {
@ -896,61 +897,14 @@ namespace Greenshot.Drawing {
/// Apply a bitmap effect to the surface
/// </summary>
/// <param name="effect"></param>
public void ApplyBitmapEffect(Effects effect) {
public void ApplyBitmapEffect(IEffect effect) {
BackgroundForm backgroundForm = new BackgroundForm("Effect", "Please wait");
backgroundForm.Show();
Application.DoEvents();
try {
Rectangle imageRectangle = new Rectangle(Point.Empty, Image.Size);
Bitmap newImage = null;
Point offset = new Point(-1,-1);
switch (effect) {
case Effects.Shadow:
newImage = ImageHelper.CreateShadow((Bitmap)Image, 1f, 9, ref offset, PixelFormat.Format32bppArgb); //Image.PixelFormat);
break;
case Effects.TornEdge:
using (Bitmap tmpImage = ImageHelper.CreateTornEdge((Bitmap)Image)) {
newImage = ImageHelper.CreateShadow(tmpImage, 1f, 7, ref offset, PixelFormat.Format32bppArgb); //Image.PixelFormat);
}
break;
case Effects.Border:
newImage = ImageHelper.CreateBorder((Bitmap)Image, 2, Color.Black, Image.PixelFormat, out offset);
break;
case Effects.Grayscale:
newImage = ImageHelper.CreateGrayscale((Bitmap)Image);
break;
case Effects.Invert:
newImage = ImageHelper.CreateNegative((Bitmap)Image);
break;
case Effects.RotateClockwise:
case Effects.RotateCounterClockwise:
RotateFlipType rotateFlipType = RotateFlipType.Rotate270FlipNone;
if (effect == Effects.RotateClockwise) {
rotateFlipType = RotateFlipType.Rotate90FlipNone;
}
// Do not rotate the drawable containers until this works!
//MakeUndoable(new DrawableContainerBoundsChangeMemento(elements.AsIDrawableContainerList()), false);
//foreach (DrawableContainer drawableContainer in elements) {
// if (drawableContainer.CanRotate) {
// drawableContainer.Rotate(rotateFlipType);
// }
//}
newImage = ImageHelper.RotateFlip((Bitmap)Image, rotateFlipType);
break;
}
// The following was added to correct any unneeded pixels, had the bad effect that sometimes everything was cropped... :(
//Rectangle autoCropRectangle = ImageHelper.FindAutoCropRectangle(newImage, 0);
//if (!Size.Empty.Equals(autoCropRectangle.Size) && !autoCropRectangle.Size.Equals(newImage.Size)) {
// LOG.InfoFormat("Crop to {0}", autoCropRectangle);
// using (Bitmap tmpImage = newImage) {
// newImage = ImageHelper.CloneArea(newImage, autoCropRectangle, PixelFormat.DontCare);
// }
// // Fix offset
// offset = new Point(offset.X - autoCropRectangle.X, offset.Y - autoCropRectangle.Y);
//} else {
// LOG.DebugFormat("No cropping needed!");
//}
Point offset;
Bitmap newImage = ImageHelper.ApplyEffect((Bitmap)Image, effect, out offset);
if (newImage != null) {
// Make sure the elements move according to the offset the effect made the bitmap move
elements.MoveBy(offset.X, offset.Y);