@@ -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