Skip to content

Commit

Permalink
It now scaled multiple folders
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Jun 18, 2016
1 parent f609717 commit 3b777bb
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Devedse.DeveImagePyramid/Devedse.DeveImagePyramid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@
<Compile Include="ImageZoomOuter.cs" />
<Compile Include="ImageWriter.cs" />
<Compile Include="ImageReader.cs" />
<Compile Include="MathHelper.cs" />
<Compile Include="Pixel.cs" />
<Compile Include="PretzelImage.cs" />
<Compile Include="PretzelImageCombined.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PyramidCreator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
16 changes: 16 additions & 0 deletions Devedse.DeveImagePyramid/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Devedse.DeveImagePyramid
{
public static class MathHelper
{
public static bool IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
}
}
39 changes: 14 additions & 25 deletions Devedse.DeveImagePyramid/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,29 @@ static void Main(string[] args)
string outputFolder = @"C:\#ImageScalerOutput";
string desiredExtension = ".tiff";
int deepestFolderNumber = 15;
bool useRealitvePixelScale = true;

Console.WriteLine($"Starting generation of lowest level folder (+ conversion to {desiredExtension})");
//PyramidCreator.MoveInputToOutputAndConvert(inputFolder, outputFolder, desiredExtension, deepestFolderNumber);
Console.WriteLine();

MoveInputToOutputAndConvert(inputFolder, outputFolder, desiredExtension, deepestFolderNumber);
Console.WriteLine("Starting the scaling process...");
for (int i = deepestFolderNumber - 1; i >= 1; i--)
{
var destFolder = Path.Combine(outputFolder, i.ToString());
var srcFolder = Path.Combine(outputFolder, (i + 1).ToString());

Console.WriteLine($"Starting with scale {i}");
PyramidCreator.CreatePyramid(srcFolder, destFolder, desiredExtension, useRealitvePixelScale);
Console.WriteLine();
}

Console.WriteLine("Press any key to continue...");
Console.WriteLine("Completed, press any key to continue...");
Console.ReadKey();
}

private static void MoveInputToOutputAndConvert(string inputFolder, string outputFolder, string desiredExtension, int deepestFolderNumber)
{
var outputForThis = Path.Combine(outputFolder, deepestFolderNumber.ToString());
Directory.CreateDirectory(outputForThis);

var filesInDirectory = Directory.GetFiles(inputFolder);


Parallel.ForEach(filesInDirectory, filePath =>
{
var extension = Path.GetExtension(filePath);
if (FileExtensionHelper.IsValidImageFileExtension(extension))
{
var readImage = ImageReader.ReadImage(filePath);

var outputFileName = Path.GetFileNameWithoutExtension(filePath);
var outputFileNameWithExtension = outputFileName + desiredExtension;
var totalOutputPath = Path.Combine(outputForThis, outputFileNameWithExtension);

Console.WriteLine($"Writing: {outputFileNameWithExtension}");
ImageWriter.WriteImage(totalOutputPath, readImage);
}
});
}

private static void ScaleTest()
{
var topLeft = ImageReader.ReadImage("0_0.tiff");
Expand Down
86 changes: 86 additions & 0 deletions Devedse.DeveImagePyramid/PyramidCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Devedse.DeveImagePyramid
{
public static class PyramidCreator
{
public static void MoveInputToOutputAndConvert(string inputFolder, string outputFolder, string desiredExtension, int deepestFolderNumber)
{
var outputForThis = Path.Combine(outputFolder, deepestFolderNumber.ToString());
Directory.CreateDirectory(outputForThis);

var filesInDirectory = Directory.GetFiles(inputFolder);


Parallel.ForEach(filesInDirectory, filePath =>
{
var extension = Path.GetExtension(filePath);
if (FileExtensionHelper.IsValidImageFileExtension(extension))
{
var readImage = ImageReader.ReadImage(filePath);

var outputFileName = Path.GetFileNameWithoutExtension(filePath);
var outputFileNameWithExtension = outputFileName + desiredExtension;
var totalOutputPath = Path.Combine(outputForThis, outputFileNameWithExtension);

Console.WriteLine($"Writing: {outputFileNameWithExtension}");
ImageWriter.WriteImage(totalOutputPath, readImage);
}
});
}

public static void CreatePyramid(string inputFolder, string outputFolder, string desiredExtension, bool useRealitvePixelScale)
{
var allFilesInInput = Directory.GetFiles(inputFolder).Where(t => FileExtensionHelper.IsValidImageFileExtension(Path.GetExtension(t))).ToList();
if (!MathHelper.IsPowerOfTwo(allFilesInInput.Count))
{
throw new InvalidOperationException("Amount of files in input directory is not a power of 2");
}
var foundExtension = allFilesInInput.Select(t => Path.GetExtension(t)).Distinct().SingleOrDefault();

Directory.CreateDirectory(outputFolder);

var folderName = Path.GetFileName(outputFolder);

var expectedFilesInOutput = allFilesInInput.Count / 4;
int filesInWidth = (int)Math.Sqrt(expectedFilesInOutput);
int filesInHeight = (int)Math.Sqrt(expectedFilesInOutput);

//Parallel.For(0, expectedFilesInOutput, i =>
for (int i = 0; i < expectedFilesInOutput; i++)
{
int xStart = (i % filesInHeight) * 2;
int yStart = (i / filesInHeight) * 2;

var topLeftFileName = $"{xStart}_{yStart}{foundExtension}";
var bottomLeftFileName = $"{xStart}_{yStart + 1}{foundExtension}";
var topRightFileName = $"{xStart + 1}_{yStart}{foundExtension}";
var bottomRightFileName = $"{xStart + 1}_{yStart + 1}{foundExtension}";

var topLeftTotalPath = Path.Combine(inputFolder, topLeftFileName);
var bottomLeftTotalPath = Path.Combine(inputFolder, bottomLeftFileName);
var topRightTotalPath = Path.Combine(inputFolder, topRightFileName);
var bottomRightTotalPath = Path.Combine(inputFolder, bottomRightFileName);

var topLeft = ImageReader.ReadImage(topLeftTotalPath);
var bottomLeft = ImageReader.ReadImage(bottomLeftTotalPath);
var topRight = ImageReader.ReadImage(topRightTotalPath);
var bottomRight = ImageReader.ReadImage(bottomRightTotalPath);

var combinedImage = new PretzelImageCombined(topLeft, bottomLeft, topRight, bottomRight);
var scaledCombinedImage = ImageZoomOuter.Scale(combinedImage, useRealitvePixelScale);

var outputFileName = $"{xStart / 2}_{yStart / 2}{desiredExtension}";
var outputTotalPath = Path.Combine(outputFolder, outputFileName);

Console.WriteLine($"{folderName}: {outputFileName}");
ImageWriter.WriteImage(outputTotalPath, scaledCombinedImage);
}
}
}
}

0 comments on commit 3b777bb

Please sign in to comment.