ImageActions.cs (pinta-1.7) | : | ImageActions.cs (pinta-1.7.1) | ||
---|---|---|---|---|
skipping to change at line 200 | skipping to change at line 200 | |||
{ | { | |||
Document doc = PintaCore.Workspace.ActiveDocument; | Document doc = PintaCore.Workspace.ActiveDocument; | |||
PintaCore.Tools.Commit (); | PintaCore.Tools.Commit (); | |||
Gdk.Rectangle rect = doc.GetSelectedBounds (true); | Gdk.Rectangle rect = doc.GetSelectedBounds (true); | |||
CropImageToRectangle (doc, rect, doc.Selection.SelectionP ath); | CropImageToRectangle (doc, rect, doc.Selection.SelectionP ath); | |||
} | } | |||
/// <summary> | ||||
/// Checks if all of the pixels in the row match the specified color. | ||||
/// </summary> | ||||
private static bool IsConstantRow (ImageSurface surf, Cairo.Color color, | ||||
int y) | ||||
{ | ||||
for (int x = 0; x < surf.Width; ++x) { | ||||
if (!color.Equals (surf.GetPixel (x, y))) | ||||
return false; | ||||
} | ||||
return true; | ||||
} | ||||
/// <summary> | ||||
/// Checks if all of the pixels in the column (within the bounds | ||||
of the rectangle) match the specified color. | ||||
/// </summary> | ||||
private static bool IsConstantColumn (ImageSurface surf, Cairo.Co | ||||
lor color, Gdk.Rectangle rect, int x) | ||||
{ | ||||
for (int y = rect.Top; y < rect.Bottom; ++y) { | ||||
if (!color.Equals (surf.GetPixel (x, y))) | ||||
return false; | ||||
} | ||||
return true; | ||||
} | ||||
private void HandlePintaCoreActionsImageAutoCropActivated (object sender, EventArgs e) | private void HandlePintaCoreActionsImageAutoCropActivated (object sender, EventArgs e) | |||
{ | { | |||
Document doc = PintaCore.Workspace.ActiveDocument; | Document doc = PintaCore.Workspace.ActiveDocument; | |||
PintaCore.Tools.Commit (); | PintaCore.Tools.Commit (); | |||
using (var image = doc.GetFlattenedImage ()) | using (var image = doc.GetFlattenedImage ()) | |||
{ | { | |||
Gdk.Rectangle rect = image.GetBounds (); | Gdk.Rectangle rect = image.GetBounds (); | |||
Cairo.Color border_color = image.GetPixel (0, 0); | ||||
Cairo.Color borderColor = image.GetPixel (0, 0); | ||||
bool cropSide = true; | ||||
int depth = -1; | ||||
//From the top down | ||||
while (cropSide) { | ||||
depth++; | ||||
for (int i = 0; i < image.Width; i++) { | ||||
if (!borderColor.Equals(image.GetPixel (i, depth))) { | ||||
cropSide = false; | ||||
break; | ||||
} | ||||
} | ||||
//Check if the image is blank/mono-coloured, only need to do | ||||
it on this scan | ||||
if (depth == image.Height) | ||||
return; | ||||
} | ||||
rect = new Gdk.Rectangle (rect.X, rect.Y + depth, rect.Width, re | // Top down. | |||
ct.Height - depth); | for (int y = 0; y < image.Height; ++y) { | |||
if (!IsConstantRow (image, border_color, y)) | ||||
depth = image.Height; | break; | |||
cropSide = true; | ||||
//From the bottom up | ||||
while (cropSide) { | ||||
depth--; | ||||
for (int i = 0; i < image.Width; i++) { | ||||
if (!borderColor.Equals(image.GetPixel (i, depth))) { | ||||
cropSide = false; | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
rect = new Gdk.Rectangle (rect.X, rect.Y, rect.Width, depth - re | ||||
ct.Y); | ||||
depth = 0; | ||||
cropSide = true; | ||||
//From left to right | ||||
while (cropSide) { | ||||
depth++; | ||||
for (int i = 0; i < image.Height; i++) { | ||||
if (!borderColor.Equals(image.GetPixel (depth, i))) { | ||||
cropSide = false; | ||||
break; | ||||
} | ||||
} | ||||
++rect.Y; | ||||
--rect.Height; | ||||
} | } | |||
rect = new Gdk.Rectangle (rect.X + depth, rect.Y, rect.Width - d | // Bottom up. | |||
epth, rect.Height); | for (int y = rect.Bottom; y >= rect.Top; --y) { | |||
if (!IsConstantRow (image, border_color, | ||||
depth = image.Width; | y)) | |||
cropSide = true; | break; | |||
//From right to left | ||||
while (cropSide) { | --rect.Height; | |||
depth--; | } | |||
for (int i = 0; i < image.Height; i++) { | ||||
if (!borderColor.Equals(image.GetPixel (depth, i))) { | // Left side. | |||
cropSide = false; | for (int x = 0; x < image.Width; ++x) { | |||
break; | if (!IsConstantColumn (image, border_colo | |||
} | r, rect, x)) | |||
} | break; | |||
} | ++rect.X; | |||
--rect.Width; | ||||
} | ||||
// Right side. | ||||
for (int x = rect.Right; x >= rect.Left; --x) { | ||||
if (!IsConstantColumn (image, border_colo | ||||
r, rect, x)) | ||||
break; | ||||
rect = new Gdk.Rectangle (rect.X, rect.Y, depth - rect.X, rect.H | --rect.Width; | |||
eight); | } | |||
// Ignore the current selection when auto-cropping. | // Ignore the current selection when auto-croppin | |||
CropImageToRectangle (doc, rect, /*selection*/ null); | g. | |||
CropImageToRectangle (doc, rect, /*selection*/ nu | ||||
ll); | ||||
} | } | |||
} | } | |||
#endregion | #endregion | |||
static void CropImageToRectangle (Document doc, Gdk.Rectangle rec t, Path selection) | static void CropImageToRectangle (Document doc, Gdk.Rectangle rec t, Path selection) | |||
{ | { | |||
if (rect.Width > 0 && rect.Height > 0) | if (rect.Width > 0 && rect.Height > 0) | |||
{ | { | |||
ResizeHistoryItem hist = new ResizeHistoryItem(do c.ImageSize); | ResizeHistoryItem hist = new ResizeHistoryItem(do c.ImageSize); | |||
hist.Icon = "Menu.Image.Crop.png"; | hist.Icon = "Menu.Image.Crop.png"; | |||
hist.Text = Catalog.GetString("Crop to Selection" ); | hist.Text = Catalog.GetString("Crop to Selection" ); | |||
hist.StartSnapshotOfImage(); | hist.StartSnapshotOfImage(); | |||
End of changes. 8 change blocks. | ||||
72 lines changed or deleted | 68 lines changed or added |