Skip to content

Commit

Permalink
Merge pull request #1 from kneefer1/Threading
Browse files Browse the repository at this point in the history
Finally finished threading processing.
  • Loading branch information
Szymon Bartnik committed Oct 26, 2014
2 parents 138c51c + 5e7d3ab commit 0869f23
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
1 change: 1 addition & 0 deletions Gauss.ASM/Gauss.ASM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
31 changes: 22 additions & 9 deletions Gauss.CPP/Gauss.CPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Pixel** CopyPixels(Pixel** pixels, int height, int width)
return toReturn;
}

int* ComputePascalRow(int n){
int* ComputePascalRow(int n)
{
int* row = new int[n + 1];
row[0] = 1; //First element is always 1
for (int i = 1; i<n / 2 + 1; i++){ //Progress up, until reaching the middle value
Expand All @@ -28,9 +29,18 @@ int* ComputePascalRow(int n){
return row;
}

void DeletePixelsArray(Pixel** array, int height)
{
for (int y = 0; y < height; y++)
{
delete[] array[y];
}
}

void ComputeGaussBlur(ThreadParameters params)
{
int row_padded = (params.ImageWidth * 3 + 3) & (~3);
int gaussHalf = params.GaussMaskSize / 2;

Pixel** pixels = new Pixel*[params.ImageHeight];

Expand Down Expand Up @@ -58,7 +68,7 @@ void ComputeGaussBlur(ThreadParameters params)

double linc_r, linc_g, linc_b;

const int gauss_w = 25; // must be odd
const int gauss_w = params.GaussMaskSize; // must be odd
int gauss_sum = 0;

int* mask = ComputePascalRow(gauss_w - 1);
Expand All @@ -67,17 +77,17 @@ void ComputeGaussBlur(ThreadParameters params)
}

//For every pixel on the temporary bitmap ...
for (int i = gauss_w - 1; i<params.ImageHeight; i++)
for (int i = gaussHalf; i < params.ImageHeight - gaussHalf; i++)
{
for (int j = 0; j<params.ImageWidth; j++)
for (int j = gaussHalf; j<params.ImageWidth - gaussHalf; j++)
{
linc_r = 0;
linc_g = 0;
linc_b = 0;

for (int k = 0; k<gauss_w; k++)
{
color = pixels[i - (gauss_w - 1) + k][j];
color = pixels[i - gaussHalf + k][j];
linc_r += color.R * mask[k];
linc_g += color.G * mask[k];
linc_b += color.B * mask[k];
Expand All @@ -93,17 +103,17 @@ void ComputeGaussBlur(ThreadParameters params)
}

//For every pixel on the output bitmap ...
for (int i = 0; i<params.ImageHeight; i++)
for (int i = gaussHalf; i<params.ImageHeight - gaussHalf; i++)
{
for (int j = gauss_w - 1; j<params.ImageWidth; j++)
for (int j = gaussHalf; j < params.ImageWidth - gaussHalf; j++)
{
linc_r = 0;
linc_g = 0;
linc_b = 0;

for (int k = 0; k<gauss_w; k++)
{
color = temp[i][j - (gauss_w - 1) + k];
color = temp[i][j - gaussHalf + k];
linc_r += color.R * mask[k];
linc_g += color.G * mask[k];
linc_b += color.B * mask[k];
Expand All @@ -118,8 +128,11 @@ void ComputeGaussBlur(ThreadParameters params)
}
}

DeletePixelsArray(temp, params.ImageHeight);
delete[] mask;

for (int y = 0; y < params.ImageHeight; y++)
for (int y = gaussHalf; y < params.ImageHeight - gaussHalf; y++)
memcpy(&params.ImgByteArrayPtr[params.CurrentImgOffset + y * row_padded], pixels[y], sizeof(unsigned char) * 3 * params.ImageWidth);

DeletePixelsArray(pixels, params.ImageHeight);
}
31 changes: 17 additions & 14 deletions Gauss.GUI/Core/GaussImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,26 @@ public async Task<byte[]> GenerateBlurredImageAsync(GeneratorParameters generato
var tasks = new Task[generatorParams.NumberOfThreads];
var imgSizes = GetLoadedImageSizes();

for (int threadNum = 0; threadNum < tasks.Length; threadNum++)
while (generatorParams.BlurLevel-- > 0)
{
int num = threadNum;
tasks[threadNum] = Task.Run(() =>
for (int threadNum = 0; threadNum < tasks.Length; threadNum++)
{
var currentThreadParams = ComputeThreadParams(
threadId: num,
generatorParams: generatorParams,
imageSizes: imgSizes);
int num = threadNum;
tasks[threadNum] = Task.Run(() =>
{
var currentThreadParams = ComputeThreadParams(
threadId: num,
generatorParams: generatorParams,
imageSizes: imgSizes);

Console.WriteLine(currentThreadParams.ToString());

RunUnsafeImageGenerationCode(currentThreadParams, generatorParams.GeneratingLibrary);
});
}

RunUnsafeImageGenerationCode(currentThreadParams, generatorParams.GeneratingLibrary);
});
await Task.WhenAll(tasks);
}

await Task.WhenAll(tasks);
return SourceFile;
}

Expand Down Expand Up @@ -90,11 +95,9 @@ private ThreadParameters ComputeThreadParams(int threadId, GeneratorParameters g
if (i == threadId)
currentThreadImgHeight = numOfLinesOfCurrentThread;
else
sumOfOffsetLines += numOfLinesOfCurrentThread;
sumOfOffsetLines += (numOfLinesOfCurrentThread - (generatorParams.GaussMaskSize - 1));
}

sumOfOffsetLines -= threadId*(generatorParams.GaussMaskSize - 1);

return new ThreadParameters
{
CurrentImgOffset = sumOfOffsetLines * rowPadded,
Expand Down
1 change: 1 addition & 0 deletions Gauss.GUI/Gauss.GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
5 changes: 5 additions & 0 deletions Gauss.GUI/Models/RunParameters/ThreadParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public struct ThreadParameters
public int ImgHeight;
public int IdOfImgPart;
public int NumOfImgParts;

public override string ToString()
{
return string.Format("ThreadID: {0}; Width: {1}; Height: {2}; NumOfParts: {3}; ThreadOffset: {4}", IdOfImgPart, ImgWidth, ImgHeight, NumOfImgParts, CurrentImgOffset);
}
}
}
2 changes: 1 addition & 1 deletion Gauss.GUI/ViewModels/MainWindowViewModel.Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void InitializeGenerateBlurredImageCommand()
MainPanelImage = await ImageManager.GenerateBlurredImageAsync(new GeneratorParameters
{
NumberOfThreads = NumberOfThreads,
BlurLevel = BlurLevel,
BlurLevel = BlurIterations,
GaussMaskSize = GaussMaskSize,
GeneratingLibrary = GeneratingLibrary,
});
Expand Down
14 changes: 7 additions & 7 deletions Gauss.GUI/ViewModels/MainWindowViewModel.Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ public int NumberOfThreads
}
private int _numberOfThreads;

public int BlurLevel
public int BlurIterations
{
get { return _blurLevel; }
get { return _blurIterations; }
set
{
if (value == _blurLevel) return;
_blurLevel = value;
if (value == _blurIterations) return;
_blurIterations = value;
OnPropertyChanged();
}
}
private int _blurLevel;
private int _blurIterations;

public GeneratingLibrary GeneratingLibrary
{
Expand Down Expand Up @@ -160,9 +160,9 @@ public MainWindowViewModel()
private void InitializeProperties()
{
NumberOfThreads = 2;
BlurLevel = 40;
BlurIterations = 5;
GeneratingLibrary = GeneratingLibrary.CPP;
GaussMaskSize = 25;
GaussMaskSize = 13;

SetDropImageZoneState(DropImagesZoneState.Idle);
InformationText = "Computing image...";
Expand Down
4 changes: 2 additions & 2 deletions Gauss.GUI/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
</Border>
<Border BorderBrush="DeepSkyBlue" BorderThickness="3" Margin="0 0 0 10" Padding="5" Background="White">
<StackPanel>
<TextBlock Text="Blur level:" FontSize="12" VerticalAlignment="Center"/>
<TextBlock Text="Blur iterations:" FontSize="12" VerticalAlignment="Center"/>
<TextBlock Text="{Binding ElementName=BlurLevelSlider, Path=Value}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-5" />
<Slider x:Name="BlurLevelSlider" Minimum="1" Maximum="100" VerticalAlignment="Center" TickFrequency="1" IsSnapToTickEnabled="True" Value="{Binding BlurLevel}"/>
<Slider x:Name="BlurLevelSlider" Minimum="1" Maximum="100" VerticalAlignment="Center" TickFrequency="1" IsSnapToTickEnabled="True" Value="{Binding BlurIterations}"/>
</StackPanel>
</Border>
<Border BorderBrush="DeepSkyBlue" BorderThickness="3" Margin="0 0 0 10" Padding="5" Background="White">
Expand Down

0 comments on commit 0869f23

Please sign in to comment.