Skip to content

Commit

Permalink
Improve Imaging,
Browse files Browse the repository at this point in the history
Add more Menu Options.
  • Loading branch information
LoneWandererProductions committed Nov 10, 2024
1 parent 23a46bc commit 3aa346a
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 126 deletions.
16 changes: 16 additions & 0 deletions Imaging/IImageRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ internal interface IImageRender
/// <exception cref="ArgumentNullException"></exception>
Bitmap CutBitmap(Bitmap image, int x, int y, int height, int width);

/// <summary>
/// Cuts the bitmap.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="shape">The shape.</param>
/// <param name="shapeParams">The shape parameters.</param>
/// <param name="startPoint">The start point.</param>
/// <returns>
/// The cut Image, based on the shape
/// </returns>
/// <exception cref="ArgumentNullException"></exception>
Bitmap CutBitmap(Bitmap image, int width, int height, MaskShape shape, object shapeParams = null,
Point? startPoint = null);

/// <summary>
/// Cuts a bitmap.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Imaging/ITextureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Bitmap GenerateTexture(
int width,
int height,
TextureType filter,
TextureShape shape,
MaskShape shape,
Point? startPoint = null,
object shapeParams = null);
}
Expand Down
85 changes: 85 additions & 0 deletions Imaging/ImageMask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: Imaging
* FILE: Imaging/ImageMask.cs
* PURPOSE: Helper class to handle some shape operations on an Image
* PROGRAMER: Peter Geinitz (Wayfarer)
*/

using System.Drawing;

namespace Imaging
{
/// <summary>
/// Handle all the possible selections on an Image
/// </summary>
internal static class ImageMask
{
/// <summary>
/// Applies the rectangle mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
/// <param name="startPoint">The starting point (top-left corner) of the rectangle.</param>
/// <returns>Rectangle Bitmap</returns>
internal static Bitmap ApplyRectangleMask(Image bitmap, int width, int height, Point startPoint)
{
// Create a new bitmap to work on
var rectBitmap = new Bitmap(bitmap.Width, bitmap.Height);

// Use graphics to apply the mask
using var g = Graphics.FromImage(rectBitmap);
// Clear the background to transparent
g.Clear(Color.Transparent);

// Create a texture brush with the original bitmap
using var brush = new TextureBrush(bitmap);
// Fill a rectangle starting from the given start point
g.FillRectangle(brush, new Rectangle(startPoint.X, startPoint.Y, width, height));

return rectBitmap;
}


/// <summary>
/// Applies the circle mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="startPoint">The start point.</param>
/// <returns>
/// Circle Bitmap
/// </returns>
internal static Bitmap ApplyCircleMask(Image bitmap, int width, int height, Point startPoint)
{
var circleBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using var g = Graphics.FromImage(circleBitmap);
g.Clear(Color.Transparent);
using var brush = new TextureBrush(bitmap);

// Fill the ellipse starting at the specified start point
g.FillEllipse(brush, startPoint.X, startPoint.Y, width, height);

return circleBitmap;
}

/// <summary>
/// Applies the polygon mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="points">The points.</param>
/// <returns>Polygon Bitmap</returns>
internal static Bitmap ApplyPolygonMask(Image bitmap, Point[] points)
{
var polyBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using var g = Graphics.FromImage(polyBitmap);
g.Clear(Color.Transparent);
using var brush = new TextureBrush(bitmap);
g.FillPolygon(brush, points);

return polyBitmap;
}
}
}
35 changes: 35 additions & 0 deletions Imaging/ImageRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,41 @@ public Bitmap CutBitmap(Bitmap image, int x, int y, int height, int width)
return ImageStream.CutBitmap(image, x, y, height, width);
}

/// <inheritdoc />
/// <summary>
/// Cuts the bitmap.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="shape">The shape.</param>
/// <param name="shapeParams">The shape parameters.</param>
/// <param name="startPoint">The start point.</param>
/// <returns>The selected Image area.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">shape - null</exception>
public Bitmap CutBitmap(Bitmap image, int width, int height, MaskShape shape, object shapeParams = null, Point? startPoint = null)
{
var btm= ImageStream.CutBitmap(image, 0, 0, image.Height, image.Width);

// If no start point is provided, default to (0, 0)
var actualStartPoint = startPoint ?? new Point(0, 0);

switch (shape)
{
case MaskShape.Rectangle:
return ImageMask.ApplyRectangleMask(btm, width, height, actualStartPoint);

case MaskShape.Circle:
return ImageMask.ApplyCircleMask(btm, width, height, actualStartPoint);

case MaskShape.Polygon:
return ImageMask.ApplyPolygonMask(btm, (Point[])shapeParams);

default:
throw new ArgumentOutOfRangeException(nameof(shape), shape, null);
}
}

/// <inheritdoc />
/// <summary>
/// Cuts a bitmap.
Expand Down
4 changes: 2 additions & 2 deletions Imaging/TextureShape.cs → Imaging/MaskShape.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: Imaging
* FILE: Imaging/TextureShape.cs
* FILE: Imaging/MaskShape.cs
* PURPOSE: Enum that shows all allowed shapes
* PROGRAMER: Peter Geinitz (Wayfarer)
*/
Expand All @@ -13,7 +13,7 @@ namespace Imaging
/// <summary>
/// Texture Shapes
/// </summary>
public enum TextureShape
public enum MaskShape
{
/// <summary>
/// The rectangle
Expand Down
81 changes: 7 additions & 74 deletions Imaging/TextureAreas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static class TextureAreas
internal static Bitmap GenerateTexture(int width,
int height,
TextureType texture,
TextureShape shape,
MaskShape shape,
ImageRegister imageSettings,
object shapeParams = null,
Point? startPoint = null)
Expand Down Expand Up @@ -107,85 +107,18 @@ internal static Bitmap GenerateTexture(int width,
// Apply the texture to the specified area shape
switch (shape)
{
case TextureShape.Rectangle:
return ApplyRectangleMask(textureBitmap, width, height, actualStartPoint);
case MaskShape.Rectangle:
return ImageMask.ApplyRectangleMask(textureBitmap, width, height, actualStartPoint);

case TextureShape.Circle:
return ApplyCircleMask(textureBitmap, width, height, actualStartPoint);
case MaskShape.Circle:
return ImageMask.ApplyCircleMask(textureBitmap, width, height, actualStartPoint);

case TextureShape.Polygon:
return ApplyPolygonMask(textureBitmap, (Point[])shapeParams);
case MaskShape.Polygon:
return ImageMask.ApplyPolygonMask(textureBitmap, (Point[])shapeParams);

default:
throw new ArgumentOutOfRangeException(nameof(shape), shape, null);
}
}

/// <summary>
/// Applies the rectangle mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
/// <param name="startPoint">The starting point (top-left corner) of the rectangle.</param>
/// <returns>Rectangle Bitmap</returns>
private static Bitmap ApplyRectangleMask(Image bitmap, int width, int height, Point startPoint)
{
// Create a new bitmap to work on
var rectBitmap = new Bitmap(bitmap.Width, bitmap.Height);

// Use graphics to apply the mask
using var g = Graphics.FromImage(rectBitmap);
// Clear the background to transparent
g.Clear(Color.Transparent);

// Create a texture brush with the original bitmap
using var brush = new TextureBrush(bitmap);
// Fill a rectangle starting from the given start point
g.FillRectangle(brush, new Rectangle(startPoint.X, startPoint.Y, width, height));

return rectBitmap;
}


/// <summary>
/// Applies the circle mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="startPoint">The start point.</param>
/// <returns>
/// Circle Bitmap
/// </returns>
private static Bitmap ApplyCircleMask(Image bitmap, int width, int height, Point startPoint)
{
var circleBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using var g = Graphics.FromImage(circleBitmap);
g.Clear(Color.Transparent);
using var brush = new TextureBrush(bitmap);

// Fill the ellipse starting at the specified start point
g.FillEllipse(brush, startPoint.X, startPoint.Y, width, height);

return circleBitmap;
}

/// <summary>
/// Applies the polygon mask.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="points">The points.</param>
/// <returns>Polygon Bitmap</returns>
private static Bitmap ApplyPolygonMask(Image bitmap, Point[] points)
{
var polyBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using var g = Graphics.FromImage(polyBitmap);
g.Clear(Color.Transparent);
using var brush = new TextureBrush(bitmap);
g.FillPolygon(brush, points);

return polyBitmap;
}
}
}
2 changes: 1 addition & 1 deletion Imaging/TextureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public Bitmap GenerateWoodBitmap(int width, int height)
/// <param name="startPoint">The Start point.</param>
/// <param name="shapeParams">The shape parameters.</param>
/// <returns>Texture Bitmap</returns>
public Bitmap GenerateTexture(int width, int height, TextureType filter, TextureShape shape,
public Bitmap GenerateTexture(int width, int height, TextureType filter, MaskShape shape,
Point? startPoint = null,
object shapeParams = null)
{
Expand Down
13 changes: 6 additions & 7 deletions SlimViewer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@
<ColumnDefinition Width="5" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="25" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="5" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
Expand Down Expand Up @@ -243,11 +242,11 @@
ItemsSource="{Binding Selections}" SelectedItem="{Binding SelectedForm}" />
<ComboBox Grid.Row="2" Grid.Column="6" ItemsSource="{Binding Tooling}"
SelectedItem="{Binding SelectedTool}" />
<commonControls:ColorPickerMenu x:Name="ColorPick" Grid.Row="0" Grid.Column="8" HorizontalAlignment="Left"
<commonControls:ColorPickerMenu x:Name="ColorPick" Grid.Row="0" Grid.Column="9" HorizontalAlignment="Left"
ColorChangedCommand="{Binding ColorChangedCommand}" />
<ContentControl Grid.Row="0" Grid.Column="10" Content="{Binding SelectedTool}"
ContentTemplateSelector="{DynamicResource ToolOptionsTemplateSelector}" Grid.RowSpan="4"
Grid.ColumnSpan="2" />
<ContentControl Grid.Row="0" Grid.Column="8" Content="{Binding SelectedTool}"
ContentTemplateSelector="{DynamicResource ToolOptionsTemplateSelector}"
Grid.RowSpan="4" />
<Slider Minimum="0.1" Maximum="5.0" Value="{Binding ZoomScale, ElementName=ImageZoom, Mode=TwoWay}"
Grid.Row="2" Grid.Column="2" IsEnabled="{Binding Path =IsActive}" />

Expand Down
33 changes: 33 additions & 0 deletions SlimViews/FillTypeVisibilityConverter .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace SlimViews
{
public class FillTypeVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int selectedIndex && parameter is string targetFillType)
{
switch (targetFillType)
{
case "SolidColor":
return selectedIndex == 0 ? Visibility.Visible : Visibility.Collapsed;
case "Texture":
return selectedIndex == 1 ? Visibility.Visible : Visibility.Collapsed;
case "Filter":
return selectedIndex == 2 ? Visibility.Visible : Visibility.Collapsed;
}
}

return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion SlimViews/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ internal static Bitmap Texture(Bitmap bitmap, TextureType texture)
try
{
//just overlay the whole image with the texture, will be changed later
var btm = Generator.GenerateTexture(bitmap.Width, bitmap.Height, texture, TextureShape.Rectangle);
var btm = Generator.GenerateTexture(bitmap.Width, bitmap.Height, texture, MaskShape.Rectangle);
//overlay both images
bitmap = Render.CombineBitmap(bitmap, btm, 0, 0);
}
Expand Down
14 changes: 2 additions & 12 deletions SlimViews/ImageTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ public enum ImageTools
/// </summary>
Erase = 1,

/// <summary>
/// The texture
/// </summary>
Texture = 2,

/// <summary>
/// The filter
/// </summary>
Filter = 3,

/// <summary>
/// The cut
/// </summary>
Expand All @@ -41,11 +31,11 @@ public enum ImageTools
/// <summary>
/// The color select
/// </summary>
ColorSelect = 5,
ColorSelect = 2,

/// <summary>
/// The area
/// </summary>
Area = 6
Area = 3
}
}
Loading

0 comments on commit 3aa346a

Please sign in to comment.