Skip to content

Commit abf201b

Browse files
committed
Removed some verbose logging
Added inputinformation to remove duplicated code Apparently parallel for over y is faster then over x Added stopwatches
1 parent fabd35a commit abf201b

File tree

5 files changed

+83
-74
lines changed

5 files changed

+83
-74
lines changed

Devedse.DeveImagePyramid/Devedse.DeveImagePyramid.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
</ItemGroup>
7070
<ItemGroup>
7171
<Compile Include="FileExtensionHelper.cs" />
72+
<Compile Include="InputInformation.cs" />
7273
<Compile Include="Logging\Appenders\DateTimeLoggerAppender.cs" />
7374
<Compile Include="Logging\Appenders\LoggingLevelLoggerAppender.cs" />
7475
<Compile Include="Logging\ConsoleLogger.cs" />

Devedse.DeveImagePyramid/ImageWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void WriteImageTiff(string path, PretzelImage pretzelImage)
106106

107107
private void WriteImagePng(string path, PretzelImage pretzelImage)
108108
{
109-
_logger.Write($"{System.Threading.Thread.CurrentThread.ManagedThreadId} - Writing image Png: {path}, PretzelImage.Width: {pretzelImage.Width} PretzelImage.Height: {pretzelImage.Height} PretzelImage.Data.Length: {pretzelImage.Data.Length}", LogLevel.Verbose);
109+
//_logger.Write($"{System.Threading.Thread.CurrentThread.ManagedThreadId} - Writing image Png: {path}, PretzelImage.Width: {pretzelImage.Width} PretzelImage.Height: {pretzelImage.Height} PretzelImage.Data.Length: {pretzelImage.Data.Length}", LogLevel.Verbose);
110110

111111
using (var image = new Bitmap(pretzelImage.Width, pretzelImage.Height, PixelFormat.Format24bppRgb))
112112
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Devedse.DeveImagePyramid
8+
{
9+
class InputInformation
10+
{
11+
public int AmountOfFiles { get; set; }
12+
public string FoundExtension { get; set; }
13+
}
14+
}

Devedse.DeveImagePyramid/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@ private static void ExecuteImagePyramidGeneration(bool useDifferentFileNamesForL
2929
{
3030
using (var logger = CreateLogger(useDifferentFileNamesForLogs))
3131
{
32+
var w = Stopwatch.StartNew();
33+
3234
var retryHandler = new RetryHandler(logger);
3335
var imageReader = new ImageReader(logger);
3436
var imageWriter = new ImageWriter(retryHandler, logger);
3537
var pyramidCreator = new PyramidCreator(imageWriter, imageReader, logger);
3638

3739

3840
logger.Write($"Starting generation of lowest level folder (+ conversion to {desiredExtension})", color: ConsoleColor.Yellow);
41+
var wMoveInput = Stopwatch.StartNew();
3942
int deepestFolderNumber = pyramidCreator.MoveInputToOutputAndConvert(inputFolder, outputFolder, desiredExtension, useParallel);
43+
wMoveInput.Stop();
4044
logger.Write("Done with generation of lowest level.", color: ConsoleColor.Green);
4145
logger.EmptyLine();
4246

4347
logger.Write("Starting the scaling process...");
48+
var wPyramid = Stopwatch.StartNew();
4449
for (int i = deepestFolderNumber - 1; i >= 1; i--)
4550
{
4651
var destFolder = Path.Combine(outputFolder, i.ToString());
@@ -51,7 +56,12 @@ private static void ExecuteImagePyramidGeneration(bool useDifferentFileNamesForL
5156
logger.Write($"Done with scale {i}", color: ConsoleColor.Green);
5257
logger.EmptyLine();
5358
}
59+
wPyramid.Stop();
5460

61+
logger.Write("Time taken for moving and converting input: " + wMoveInput.Elapsed);
62+
logger.Write("Time taken for creating pyramid: " + wPyramid.Elapsed);
63+
logger.Write("Total time taken: " + w.Elapsed);
64+
logger.EmptyLine();
5565
logger.Write("Completed, press any key to continue...");
5666
Console.ReadKey();
5767
}

Devedse.DeveImagePyramid/PyramidCreator.cs

Lines changed: 57 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,13 @@ public int MoveInputToOutputAndConvert(string inputFolder, string outputFolder,
2525
{
2626
_logger.Write("Counting all files from input directory...");
2727

28-
var filesInDirectoryEnumerable = Directory.EnumerateFiles(inputFolder).Where(t => FileExtensionHelper.IsValidImageFileExtension(Path.GetExtension(t)));
29-
30-
var filesInInputCount = 0;
31-
var foundExtensions = new HashSet<string>();
32-
33-
foreach (var foundFile in filesInDirectoryEnumerable)
34-
{
35-
filesInInputCount++;
36-
37-
var extension = Path.GetExtension(foundFile);
38-
foundExtensions.Add(extension);
39-
}
28+
var inputInformation = GetInputInformation(inputFolder);
4029

41-
_logger.Write($"Found {filesInInputCount} files in {inputFolder}");
42-
43-
if (foundExtensions.Count != 1)
44-
{
45-
var exceptionString = $"Did not find exactly 1 file extension. Found '{foundExtensions.Count}' extensions instead. Extensions: {string.Join(",", foundExtensions)}.";
46-
_logger.WriteError(exceptionString, LogLevel.Exception);
47-
throw new InvalidOperationException(exceptionString);
48-
}
49-
50-
var foundExtension = foundExtensions.Single();
51-
52-
_logger.Write($"Found extension: '{foundExtension}'");
53-
54-
var firstImageFileName = $"0_0{foundExtension}";
30+
var firstImageFileName = $"0_0{inputInformation.FoundExtension}";
5531
var firstImagePath = Path.Combine(inputFolder, firstImageFileName);
5632
var firstImage = _imageReader.ReadImage(firstImagePath);
5733

58-
int filesInWidthAndHeight = (int)Math.Sqrt(filesInInputCount);
34+
int filesInWidthAndHeight = (int)Math.Sqrt(inputInformation.AmountOfFiles);
5935
int sizeInWidthAndHeight = filesInWidthAndHeight * firstImage.Width;
6036

6137
int deepestFolderNumber = (int)Math.Log(sizeInWidthAndHeight, 2);
@@ -65,7 +41,7 @@ public int MoveInputToOutputAndConvert(string inputFolder, string outputFolder,
6541
Directory.CreateDirectory(outputForThis);
6642
var fileConversionAction = new Action<int, int>((x, y) =>
6743
{
68-
var justFileName = $"{x}_{y}{foundExtension}";
44+
var justFileName = $"{x}_{y}{inputInformation.FoundExtension}";
6945
var filePath = Path.Combine(inputFolder, justFileName);
7046

7147
var readImage = _imageReader.ReadImage(filePath);
@@ -80,17 +56,18 @@ public int MoveInputToOutputAndConvert(string inputFolder, string outputFolder,
8056

8157
_logger.Write("Starting conversion/copy of images...", color: ConsoleColor.Yellow);
8258

83-
var expectedFilesInOutput = filesInInputCount;
59+
var expectedFilesInOutput = inputInformation.AmountOfFiles;
8460
var filesInWidth = (int)Math.Sqrt(expectedFilesInOutput);
8561
var filesInHeight = (int)Math.Sqrt(expectedFilesInOutput);
8662

8763
if (useParallel)
8864
{
89-
90-
for (int y = 0; y < filesInHeight; y++)
65+
//In some quick tests it seemed to be faster to do the parallel loop on the y then on the x.
66+
//Either way the inner loop should be parallelized, not the outer loop. This is to work around strange threading errors.
67+
for (int x = 0; x < filesInWidth; x++)
9168
{
92-
int localY = y;
93-
Parallel.For(0, filesInWidth, (x) => fileConversionAction(x, localY));
69+
int localX = x;
70+
Parallel.For(0, filesInHeight, (y) => fileConversionAction(localX, y));
9471
}
9572
}
9673
else
@@ -113,38 +90,7 @@ public void CreatePyramid(string inputFolder, string outputFolder, string desire
11390
{
11491
_logger.Write("Counting all files from input directory...");
11592

116-
var allFilesInInputEnumerable = Directory.EnumerateFiles(inputFolder).Where(t => FileExtensionHelper.IsValidImageFileExtension(Path.GetExtension(t)));
117-
118-
var filesInInputCount = 0;
119-
var foundExtensions = new HashSet<string>();
120-
121-
foreach (var foundFile in allFilesInInputEnumerable)
122-
{
123-
filesInInputCount++;
124-
125-
var extension = Path.GetExtension(foundFile);
126-
foundExtensions.Add(extension);
127-
}
128-
129-
_logger.Write($"Found {filesInInputCount} files in {inputFolder}");
130-
131-
if (foundExtensions.Count != 1)
132-
{
133-
var exceptionString = $"Did not find exactly 1 file extension. Found '{foundExtensions.Count}' extensions instead. Extensions: {string.Join(",", foundExtensions)}.";
134-
_logger.WriteError(exceptionString, LogLevel.Exception);
135-
throw new InvalidOperationException(exceptionString);
136-
}
137-
138-
var foundExtension = foundExtensions.Single();
139-
140-
_logger.Write($"Found extension: '{foundExtension}'");
141-
142-
if (!MathHelper.IsPowerOfTwo(filesInInputCount))
143-
{
144-
var exceptionString = "Amount of files in input directory is not a power of 2";
145-
_logger.WriteError(exceptionString, LogLevel.Exception);
146-
throw new InvalidOperationException(exceptionString);
147-
}
93+
var inputInformation = GetInputInformation(inputFolder);
14894

14995
_logger.Write($"Creating output directory: '{outputFolder}'");
15096
Directory.CreateDirectory(outputFolder);
@@ -153,24 +99,24 @@ public void CreatePyramid(string inputFolder, string outputFolder, string desire
15399

154100
_logger.Write("Starting scaling of images...", color: ConsoleColor.Yellow);
155101

156-
if (filesInInputCount == 1)
102+
if (inputInformation.AmountOfFiles == 1)
157103
{
158-
var inputFileName = $"0_0{foundExtension}";
104+
var inputFileName = $"0_0{inputInformation.FoundExtension}";
159105
var inputTotalPath = Path.Combine(inputFolder, inputFileName);
160106
var singleImage = _imageReader.ReadImage(inputTotalPath);
161107

162108
var combinedImage = new PretzelImageCombined(singleImage);
163109
var scaledCombinedImage = ImageZoomOuter.ScaleV2(combinedImage);
164110

165-
var outputFileName = $"0_0{foundExtension}";
111+
var outputFileName = $"0_0{inputInformation.FoundExtension}";
166112
var outputTotalPath = Path.Combine(outputFolder, outputFileName);
167113

168114
_logger.Write($"Writing scaled: {Path.Combine(folderName, outputFileName)}", LogLevel.Verbose);
169115
_imageWriter.WriteImage(outputTotalPath, scaledCombinedImage);
170116
}
171117
else
172118
{
173-
var expectedFilesInOutput = filesInInputCount / 4;
119+
var expectedFilesInOutput = inputInformation.AmountOfFiles / 4;
174120
//var filesInWidth = (int)Math.Sqrt(expectedFilesInOutput);
175121
var filesInHeight = (int)Math.Sqrt(expectedFilesInOutput);
176122

@@ -179,10 +125,10 @@ public void CreatePyramid(string inputFolder, string outputFolder, string desire
179125
int xStart = (i % filesInHeight) * 2;
180126
int yStart = (i / filesInHeight) * 2;
181127

182-
var topLeftFileName = $"{xStart}_{yStart}{foundExtension}";
183-
var bottomLeftFileName = $"{xStart}_{yStart + 1}{foundExtension}";
184-
var topRightFileName = $"{xStart + 1}_{yStart}{foundExtension}";
185-
var bottomRightFileName = $"{xStart + 1}_{yStart + 1}{foundExtension}";
128+
var topLeftFileName = $"{xStart}_{yStart}{inputInformation.FoundExtension}";
129+
var bottomLeftFileName = $"{xStart}_{yStart + 1}{inputInformation.FoundExtension}";
130+
var topRightFileName = $"{xStart + 1}_{yStart}{inputInformation.FoundExtension}";
131+
var bottomRightFileName = $"{xStart + 1}_{yStart + 1}{inputInformation.FoundExtension}";
186132

187133
var topLeftTotalPath = Path.Combine(inputFolder, topLeftFileName);
188134
var bottomLeftTotalPath = Path.Combine(inputFolder, bottomLeftFileName);
@@ -219,5 +165,43 @@ public void CreatePyramid(string inputFolder, string outputFolder, string desire
219165

220166
_logger.Write("Completed scaling of images.", color: ConsoleColor.Green);
221167
}
168+
169+
private InputInformation GetInputInformation(string inputFolder)
170+
{
171+
var filesInDirectoryEnumerable = Directory.EnumerateFiles(inputFolder).Where(t => FileExtensionHelper.IsValidImageFileExtension(Path.GetExtension(t)));
172+
173+
var filesInInputCount = 0;
174+
var foundExtensions = new HashSet<string>();
175+
176+
foreach (var foundFile in filesInDirectoryEnumerable)
177+
{
178+
filesInInputCount++;
179+
180+
var extension = Path.GetExtension(foundFile);
181+
foundExtensions.Add(extension);
182+
}
183+
184+
_logger.Write($"Found {filesInInputCount} files in {inputFolder}");
185+
186+
if (foundExtensions.Count != 1)
187+
{
188+
var exceptionString = $"Did not find exactly 1 file extension. Found '{foundExtensions.Count}' extensions instead. Extensions: {string.Join(",", foundExtensions)}.";
189+
_logger.WriteError(exceptionString, LogLevel.Exception);
190+
throw new InvalidOperationException(exceptionString);
191+
}
192+
193+
var foundExtension = foundExtensions.Single();
194+
195+
_logger.Write($"Found extension: '{foundExtension}'");
196+
197+
if (!MathHelper.IsPowerOfTwo(filesInInputCount))
198+
{
199+
var exceptionString = "Amount of files in input directory is not a power of 2";
200+
_logger.WriteError(exceptionString, LogLevel.Exception);
201+
throw new InvalidOperationException(exceptionString);
202+
}
203+
204+
return new InputInformation { AmountOfFiles = filesInInputCount, FoundExtension = foundExtension };
205+
}
222206
}
223207
}

0 commit comments

Comments
 (0)