Skip to content

Commit 1a54321

Browse files
committed
Submission from 21.12.2019, 00:00 UTC+02:00
0 parents  commit 1a54321

File tree

69 files changed

+9224
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+9224
-0
lines changed

.gitignore

Lines changed: 477 additions & 0 deletions
Large diffs are not rendered by default.

CUDA/ImageProcessingKernels.cu

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//Kernel code:
2+
extern "C"
3+
{
4+
#include <math.h>
5+
6+
// Device code
7+
__global__ void ConvertRgb24ToGray8(char* source, char* result, double redPortion, double greenPortion, double bluePortion, int N)
8+
{
9+
int indexResult = blockDim.x * blockIdx.x + threadIdx.x;
10+
11+
if (indexResult < N)
12+
{
13+
int indexSource = indexResult * 3;
14+
15+
double value = redPortion * source[indexSource] + greenPortion * source[indexSource + 1] + bluePortion * source[indexSource + 2];
16+
17+
if (value >= 0xFF)
18+
result[indexResult] = 0xFF;
19+
else if (value <= 0x00)
20+
result[indexResult] = 0x00;
21+
else
22+
result[indexResult] = (char)value;
23+
}
24+
}
25+
26+
__global__ void ConvertGray8ToRgb24(char* source, char* result, int N)
27+
{
28+
int indexSource = blockDim.x * blockIdx.x + threadIdx.x;
29+
30+
if (indexSource < N)
31+
{
32+
int indexResult = indexSource * 3;
33+
34+
result[indexResult++] = source[indexSource];
35+
result[indexResult++] = source[indexSource];
36+
result[indexResult] = source[indexSource];
37+
}
38+
}
39+
40+
__global__ void Invert(char* source, char* result, int N)
41+
{
42+
int i = blockDim.x * blockIdx.x + threadIdx.x;
43+
44+
if (i < N)
45+
result[i] = ~source[i];
46+
}
47+
48+
__global__ void ApplyThreshold(char* source, char* result, int threshold, int N)
49+
{
50+
int i = blockDim.x * blockIdx.x + threadIdx.x;
51+
52+
if (i < N)
53+
{
54+
if (source[i] < threshold)
55+
{
56+
result[i] = 0x00;
57+
}
58+
else
59+
{
60+
result[i] = 0xFF;
61+
}
62+
}
63+
}
64+
65+
__global__ void AdjustBrightness(char* source, char* result, int value, int N)
66+
{
67+
int i = blockDim.x * blockIdx.x + threadIdx.x;
68+
69+
if (i < N)
70+
{
71+
int newValue = source[i] + value;
72+
73+
if (newValue < 0x00)
74+
result[i] = 0x00;
75+
else if (newValue > 0xFF)
76+
result[i] = 0xFF;
77+
else
78+
result[i] = (char)newValue;
79+
}
80+
}
81+
82+
__global__ void AdjustContrast(char* source, char* result, int factor, int N)
83+
{
84+
int i = blockDim.x * blockIdx.x + threadIdx.x;
85+
86+
if (i < N)
87+
{
88+
float newValue = factor * (source[i] - 128.0) + 128.0;
89+
90+
if (newValue > 0xFF)
91+
result[i] = 0xFF;
92+
else if (newValue < 0x00)
93+
result[i] = 0x00;
94+
else
95+
result[i] = (char)round(newValue);
96+
}
97+
}
98+
99+
__global__ void ApplyU8LookupTablePerChannel(char* source, char* result, char* lookupTable, int N)
100+
{
101+
__shared__ char lut[256];
102+
103+
if (threadIdx.x < 256)
104+
lut[threadIdx.x] = lookupTable[threadIdx.x];
105+
106+
__syncthreads();
107+
108+
int i = blockDim.x * blockIdx.x + threadIdx.x;
109+
110+
if (i < N)
111+
{
112+
result[i] = lut[source[i]];
113+
}
114+
}
115+
}

CoreImageProcessor.WPF/App.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="CoreImageProcessor.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:CoreImageProcessor"
5+
StartupUri="Windows/MainWindow.xaml">
6+
<Application.Resources>
7+
</Application.Resources>
8+
</Application>

CoreImageProcessor.WPF/App.xaml.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 CoreImageProcessor
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
public const string ApplicationName = "Core Image Processor";
17+
18+
private const string EnvironmentVariableKey_UseCUDA = "CoreImageProcessor_UseCUDA";
19+
private const string EnvironmentVariableKey_ThreadLimit = "CoreImageProcessor_ThreadLimit";
20+
21+
private static AppSettings? _settings;
22+
23+
public static event EventHandler? SettingsChanged;
24+
25+
internal static AppSettings Settings
26+
{
27+
get => _settings ?? AppSettings.Default;
28+
set
29+
{
30+
_settings = value;
31+
SettingsChanged?.Invoke(_settings, EventArgs.Empty);
32+
}
33+
}
34+
35+
private static void LoadSettingsFromEnvironmentvariables()
36+
{
37+
bool useCUDA = AppSettings.Default.UseCUDA;
38+
int threadLimit = AppSettings.Default.ThreadLimit;
39+
40+
try
41+
{
42+
if (bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariableKey_UseCUDA), out bool value))
43+
useCUDA = value;
44+
}
45+
catch
46+
{
47+
}
48+
49+
try
50+
{
51+
if (int.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariableKey_UseCUDA), out int value))
52+
threadLimit = value;
53+
}
54+
catch
55+
{
56+
}
57+
58+
_settings = new AppSettings(useCUDA, threadLimit);
59+
}
60+
61+
protected override void OnStartup(StartupEventArgs e)
62+
{
63+
LoadSettingsFromEnvironmentvariables();
64+
SettingsChanged += OnSettingsChanged;
65+
66+
base.OnStartup(e);
67+
}
68+
69+
//Save Settings
70+
private void OnSettingsChanged(object? sender, EventArgs e)
71+
{
72+
try
73+
{
74+
Environment.SetEnvironmentVariable(EnvironmentVariableKey_UseCUDA, Settings.UseCUDA.ToString());
75+
}
76+
catch { }
77+
78+
try
79+
{
80+
Environment.SetEnvironmentVariable(EnvironmentVariableKey_ThreadLimit, Settings.ThreadLimit.ToString());
81+
}
82+
catch { }
83+
}
84+
}
85+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CoreImageProcessor
6+
{
7+
internal class AppSettings
8+
{
9+
public static readonly int MaxThreadLimit = Environment.ProcessorCount;
10+
public static readonly AppSettings Default = new AppSettings(false, Environment.ProcessorCount);
11+
12+
public AppSettings(bool useCuda, int threadLimit)
13+
{
14+
if (threadLimit < 1)
15+
throw new ArgumentOutOfRangeException(nameof(threadLimit), threadLimit, "Thread limit can't be smaller than one!");
16+
17+
UseCUDA = useCuda;
18+
ThreadLimit = threadLimit > MaxThreadLimit ? MaxThreadLimit : threadLimit;
19+
}
20+
21+
public bool UseCUDA
22+
{
23+
get;
24+
}
25+
26+
public int ThreadLimit
27+
{
28+
get;
29+
}
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Text;
5+
using System.Windows;
6+
using System.Windows.Data;
7+
8+
namespace CoreImageProcessor.Converters
9+
{
10+
public class BoolToVisibilityConverter : IValueConverter
11+
{
12+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
13+
{
14+
if (targetType != typeof(Visibility))
15+
throw new NotSupportedException();
16+
17+
if (value is bool booleanValue)
18+
{
19+
return booleanValue ? Visibility.Visible : Visibility.Collapsed;
20+
}
21+
else
22+
throw new NotSupportedException();
23+
}
24+
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
26+
{
27+
if (targetType != typeof(bool))
28+
throw new NotSupportedException();
29+
30+
if (value is Visibility visibility)
31+
return visibility == Visibility.Visible;
32+
else
33+
throw new NotSupportedException();
34+
}
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Text;
5+
using System.Windows.Data;
6+
7+
namespace CoreImageProcessor.Converters
8+
{
9+
public class BooleanInverter : IValueConverter
10+
{
11+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
if (targetType != typeof(bool))
14+
throw new NotSupportedException();
15+
16+
if (value is bool booleanValue)
17+
{
18+
return !booleanValue;
19+
}
20+
else
21+
throw new NotSupportedException();
22+
}
23+
24+
//The Inverse of the Boolean Inverse Function is the Boolean Inverse Function itself.
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Convert(value, targetType, parameter, culture);
26+
}
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Globalization;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Windows.Data;
8+
9+
namespace CoreImageProcessor.Converters
10+
{
11+
12+
public class EnumDescriptionConverter : IValueConverter
13+
{
14+
private string GetEnumDescription(Enum enumObj)
15+
{
16+
string enumToString = enumObj.ToString();
17+
18+
FieldInfo? fieldInfo = enumObj.GetType().GetField(enumToString);
19+
20+
if (fieldInfo != null)
21+
{
22+
object[] attributeArray = fieldInfo.GetCustomAttributes(false);
23+
24+
if (attributeArray.Length > 0)
25+
{
26+
foreach (var attribute in attributeArray)
27+
{
28+
if (attribute is DescriptionAttribute descriptionAttribute)
29+
return descriptionAttribute.Description;
30+
}
31+
}
32+
}
33+
34+
return enumToString;
35+
}
36+
37+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
38+
{
39+
try
40+
{
41+
return GetEnumDescription((Enum)value);
42+
}
43+
catch (Exception)
44+
{
45+
return "N/A";
46+
}
47+
}
48+
49+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
50+
{
51+
return string.Empty;
52+
}
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace CoreImageProcessor.Converters
6+
{
7+
public class ToStringConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
return value?.ToString() ?? "N/A";
12+
}
13+
14+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
15+
{
16+
return string.Empty;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)