Skip to content

Commit b7b30bc

Browse files
Добавьте файлы проекта.
1 parent 42bcaf0 commit b7b30bc

20 files changed

+1037
-0
lines changed

Pixelate_Core/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

Pixelate_Core/BitmapWrapper.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Drawing;
7+
using System.Drawing.Imaging;
8+
9+
namespace Pixelate_Core
10+
{
11+
public class BitmapWrapper : IDisposable
12+
{
13+
Bitmap bitmap;
14+
BitmapData data;
15+
byte[] RGBs;
16+
17+
public Bitmap Bitmap { get { return bitmap; } }
18+
public byte[] Array { get { return RGBs; } }
19+
public BitmapWrapper(Bitmap bitmap)
20+
{
21+
this.bitmap = bitmap;
22+
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
23+
data = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
24+
25+
IntPtr ptr = data.Scan0;
26+
int bytes = Math.Abs(data.Stride) * bitmap.Height;
27+
28+
RGBs = new byte[bytes];
29+
System.Runtime.InteropServices.Marshal.Copy(ptr, RGBs, 0, bytes);
30+
}
31+
32+
public void SetPixel(int x, int y, Color color)
33+
{
34+
RGBs[(y * data.Width + x) * 4] = color.B;
35+
RGBs[(y * data.Width + x) * 4 + 1] = color.G;
36+
RGBs[(y * data.Width + x) * 4 + 2] = color.R;
37+
RGBs[(y * data.Width + x) * 4 + 3] = color.A;
38+
}
39+
public Color GetPixel(int x, int y)
40+
{
41+
return Color.FromArgb(RGBs[(y * data.Width + x) * 4 + 3], RGBs[(y * data.Width + x) * 4 + 2], RGBs[(y * data.Width + x) * 4 + 1], RGBs[(y * data.Width + x) * 4]);
42+
}
43+
public void Dispose()
44+
{
45+
System.Runtime.InteropServices.Marshal.Copy(RGBs, 0, data.Scan0, Math.Abs(data.Stride) * bitmap.Height);
46+
bitmap.UnlockBits(data);
47+
}
48+
}
49+
}

Pixelate_Core/ImageHelper.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Drawing.Imaging;
4+
using System.Windows;
5+
using System.Windows.Interop;
6+
using System.Windows.Media.Imaging;
7+
8+
namespace Pixelation_
9+
{
10+
public class ImageHelper
11+
{
12+
public static Bitmap GetBitmap(BitmapSource source)
13+
{
14+
Bitmap bmp = new Bitmap
15+
(
16+
source.PixelWidth,
17+
source.PixelHeight,
18+
System.Drawing.Imaging.PixelFormat.Format32bppPArgb
19+
);
20+
21+
BitmapData data = bmp.LockBits
22+
(
23+
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
24+
ImageLockMode.WriteOnly,
25+
System.Drawing.Imaging.PixelFormat.Format32bppPArgb
26+
);
27+
28+
source.CopyPixels
29+
(
30+
Int32Rect.Empty,
31+
data.Scan0,
32+
data.Height * data.Stride,
33+
data.Stride
34+
);
35+
source = null;
36+
bmp.UnlockBits(data);
37+
return bmp;
38+
}
39+
public static BitmapSource GetBitmapSource(Bitmap bitmap)
40+
{
41+
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap
42+
(
43+
bitmap.GetHbitmap(),
44+
IntPtr.Zero,
45+
Int32Rect.Empty,
46+
BitmapSizeOptions.FromEmptyOptions()
47+
);
48+
49+
return bitmapSource;
50+
}
51+
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
52+
public static extern bool DeleteObject(IntPtr hObject);
53+
}
54+
}

Pixelate_Core/Pixelate.cs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Drawing;
7+
using System.Drawing.Drawing2D;
8+
9+
namespace Pixelate_Core
10+
{
11+
public class Pixelator
12+
{
13+
static List<Color> ColorPallete;
14+
/// <summary>
15+
/// Pixalates given Bitmap
16+
/// </summary>
17+
/// <param name="bitmap"></param>
18+
/// <param name="blockSize">Sample Factor, how many pixels colors transforms into one color</param>
19+
/// <param name="colors">Amount of color in pallete, -1 for using all colors in Bitmap</param>
20+
/// <param name="randomColors">Color selection mode, random or most used</param>
21+
/// <returns>Pixelated Bitmap</returns>
22+
public static Bitmap Pixelate(Bitmap bitmap, IEnumerable<Color> Pallete, int blockSize = 4, int colors = -1, bool randomColors = true)
23+
{
24+
if (Pallete != null)
25+
ColorPallete = new List<Color>(Pallete);
26+
else
27+
ColorPallete = new List<Color>();
28+
29+
if (colors != -1)
30+
GeneratePallete(new BitmapWrapper(bitmap), colors, randomColors);
31+
else
32+
ColorPallete = null;
33+
34+
35+
using (Graphics graphics = Graphics.FromImage(bitmap))
36+
{
37+
for (int i = 0; i < bitmap.Width; i += blockSize)
38+
{
39+
for (int j = 0; j < bitmap.Height; j += blockSize)
40+
{
41+
Color avrg = AverageColor(bitmap, i, j, blockSize);
42+
graphics.FillRectangle(new SolidBrush(avrg), new Rectangle(i, j, blockSize, blockSize));
43+
}
44+
}
45+
}
46+
return bitmap;
47+
}
48+
49+
public static Bitmap Pixelate(Bitmap bitmap, int blockSize = 4, int colors = -1, bool randomColors = true)
50+
{
51+
return Pixelate(bitmap, null, blockSize, colors);
52+
}
53+
static Color AverageColor(Bitmap bitmap, int x, int y, int size)
54+
{
55+
Bitmap bmp = new Bitmap(1, 1);
56+
using (Graphics g = Graphics.FromImage(bmp))
57+
{
58+
g.InterpolationMode = InterpolationMode.NearestNeighbor;
59+
g.DrawImage(bitmap, new Rectangle(0, 0, 1, 1), new Rectangle(x, y, size, size), GraphicsUnit.Pixel);
60+
}
61+
Color pixel = bmp.GetPixel(0, 0);
62+
Pixelation_.ImageHelper.DeleteObject(bmp.GetHbitmap());
63+
// if ColorPallete == null, then we use all color mode
64+
if (ColorPallete != null)
65+
pixel = FindNearest(pixel);
66+
return pixel;
67+
}
68+
69+
/// <summary>
70+
/// Generates color pallete
71+
/// </summary>
72+
/// <param name="bitmap"></param>
73+
/// <param name="size"></param>
74+
/// <param name="randomColors">Mode: random or most used</param>
75+
static void GeneratePallete(BitmapWrapper bitmap, int size, bool randomColors)
76+
{
77+
if (randomColors)
78+
{
79+
Random random = new Random((int)DateTime.Now.Ticks);
80+
ColorPallete.Add(Color.Black);
81+
ColorPallete.Add(Color.White);
82+
while(ColorPallete.Count < size + 2)
83+
{
84+
ColorPallete.Add
85+
(
86+
bitmap.GetPixel(random.Next(0, bitmap.Bitmap.Width), random.Next(0, bitmap.Bitmap.Height))
87+
);
88+
}
89+
}
90+
else
91+
{
92+
Dictionary<Color, int> colors = new Dictionary<Color, int>();
93+
Color color;
94+
for (int i = 0; i < bitmap.Bitmap.Width; i++)
95+
{
96+
for (int j = 0; j < bitmap.Bitmap.Height; j++)
97+
{
98+
color = bitmap.GetPixel(i, j);
99+
if (colors.ContainsKey(color))
100+
{
101+
colors[color]++;
102+
}
103+
else
104+
colors.Add(color, 1);
105+
}
106+
}
107+
List<Color> avrgColors = colors.OrderByDescending(item => item.Value).Select(item => item.Key).ToList();
108+
ColorPallete.Add(avrgColors[0]);
109+
const int step = 30;
110+
for (int i = 1; i < colors.Count; i++)
111+
{
112+
if (Math.Abs(RGBSum(avrgColors[i]) - RGBSum(ColorPallete.Last())) > step)
113+
ColorPallete.Add(avrgColors[i]);
114+
}
115+
ColorPallete = ColorPallete.Take(size).ToList();
116+
colors = null;
117+
118+
}
119+
bitmap.Dispose();
120+
}
121+
static Color FindNearest(Color color)
122+
{
123+
int difference = 1000;
124+
int rgb_color = color.R + color.G + color.B;
125+
126+
Color nearest = color;
127+
128+
int temp;
129+
foreach(var item in ColorPallete)
130+
{
131+
temp = item.R + item.G + item.B;
132+
if (Math.Abs(temp - rgb_color) < difference)
133+
{
134+
difference = Math.Abs(temp - rgb_color);
135+
nearest = item;
136+
}
137+
}
138+
return nearest;
139+
}
140+
public static int RGBSum(Color color)
141+
{
142+
return color.R + color.G + color.B;
143+
}
144+
}
145+
}

Pixelate_Core/Pixelate_Core.csproj

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{8C1A1E78-1B54-48E0-8046-B2789A2232C1}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<RootNamespace>Pixelate_Core</RootNamespace>
10+
<AssemblyName>Pixelate_Core</AssemblyName>
11+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<Deterministic>true</Deterministic>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<PropertyGroup>
35+
<StartupObject />
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<None Include="App.config" />
39+
</ItemGroup>
40+
<ItemGroup>
41+
<Compile Include="BitmapWrapper.cs" />
42+
<Compile Include="ImageHelper.cs" />
43+
<Compile Include="Pixelate.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Reference Include="PresentationCore" />
47+
<Reference Include="System.Drawing" />
48+
<Reference Include="System.Drawing.Design" />
49+
<Reference Include="System.Windows" />
50+
<Reference Include="System.Windows.Forms" />
51+
<Reference Include="WindowsBase" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
</Project>

Pixelate_GUI/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

Pixelate_GUI/App.xaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="Pixelation_.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Pixelation_"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

Pixelate_GUI/App.xaml.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace Pixelation_
10+
{
11+
/// <summary>
12+
/// Логика взаимодействия для App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

Pixelate_GUI/ColorConfiguration.xaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Window x:Class="Pixelation_.ColorConfiguration"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Pixelation_"
7+
mc:Ignorable="d"
8+
Title="ColorConfiguration" Height="450" Width="300" ShowInTaskbar="False" WindowStyle="ToolWindow">
9+
<Grid Margin="10,10,0,0">
10+
<Grid.ColumnDefinitions>
11+
<ColumnDefinition Width="26*"/>
12+
<ColumnDefinition Width="21*"/>
13+
</Grid.ColumnDefinitions>
14+
<ListBox x:Name="Colors" HorizontalAlignment="Left" Height="412" Margin="10,10,0,0" VerticalAlignment="Top" Width="171" Grid.ColumnSpan="2" KeyDown="Colors_KeyDown">
15+
<ListBox.Effect>
16+
<DropShadowEffect/>
17+
</ListBox.Effect>
18+
</ListBox>
19+
<Button x:Name="AddColorButton" Content="Add Color" HorizontalAlignment="Left" Margin="41,33,0,0" VerticalAlignment="Top" Width="75" Click="AddColorButton_Click" Grid.Column="1"/>
20+
<TextBox Text="#ff98Aa" x:Name="ColorName" HorizontalAlignment="Left" Height="18" Margin="41,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="75" Grid.Column="1" KeyDown="ColorName_KeyDown"/>
21+
<Button x:Name="SelectColorButton" Content="Select Color" HorizontalAlignment="Left" Margin="41,58,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="SelectColorButton_Click"/>
22+
<Button x:Name="DeleteColor" Content="Delete Color" HorizontalAlignment="Left" Margin="41,83,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="DeleteColor_Click"/>
23+
24+
</Grid>
25+
</Window>

0 commit comments

Comments
 (0)