@@ -15,92 +15,98 @@ namespace CrossStitchProject
1515{
1616 public class PatternWriter
1717 {
18- private readonly List < Bitmap > _imageChunks = new List < Bitmap > ( ) ;
18+ public List < List < Bitmap > > _imageChunks = new List < List < Bitmap > > ( ) ;
19+ public List < string > PatternChunks ;
20+ public string PatternLegend ;
1921 private readonly Dictionary < Color , Floss > _color2Floss ;
20- private readonly bool _isColored ;
22+ private readonly bool _isColorPattern ;
2123 private const decimal DefaultChunkWidth = 50.0M ;
2224 private const decimal DefaultChunkHeight = 60.0M ;
2325 private static string _outputPath ;
24- public List < string > PatternChunks ;
25- public string PatternLegend ;
26+ const string CrossStitchFolderName = "Cross Stitch Patterns" ;
2627
2728 public PatternWriter ( Bitmap b , Dictionary < Color , Floss > c2F , bool colored , string outDir )
2829 {
2930 PatternChunks = new List < string > ( ) ;
3031 ChunkifyImage ( b ) ;
3132
32- _isColored = colored ;
33+ _isColorPattern = colored ;
3334 _color2Floss = c2F ;
3435
3536 var pictureFolder = Environment . GetFolderPath ( Environment . SpecialFolder . MyPictures ) ;
37+ var crossStitchFolder = Path . Combine ( pictureFolder , CrossStitchFolderName ) ;
3638 if ( string . IsNullOrEmpty ( outDir ) ) { outDir = "MyCrossStitchPattern" ; }
37- _outputPath = Path . Combine ( pictureFolder , outDir ) ;
39+ _outputPath = Path . Combine ( crossStitchFolder , outDir ) ;
3840
41+ if ( ! Directory . Exists ( crossStitchFolder ) ) { Directory . CreateDirectory ( crossStitchFolder ) ; }
3942 if ( ! Directory . Exists ( _outputPath ) ) { Directory . CreateDirectory ( _outputPath ) ; }
4043 }
41- public void BuildAndSavePattern ( )
44+ public void Build ( )
4245 {
43- var task = Task . Factory . StartNew ( ( ) => GetDistinctFlossesInImage ( ) ) ;
4446 BuildCrossStitchPattern ( ) ;
45- GenerateHtmlLegend ( task . Result ) ;
47+ BuildLegend ( ) ;
48+ }
49+ public void Save ( )
50+ {
4651 for ( var i = 0 ; i < PatternChunks . Count ; i ++ )
4752 {
48- Save ( PatternChunks [ i ] , $ "chunk{ i + 1 } .html") ;
53+ Save ( PatternChunks [ i ] , $ "chunk{ i + 1 } .html") ;
4954 }
50- Save ( PatternLegend , "legend.html" ) ;
55+ Save ( PatternLegend , "legend.html" ) ;
5156 }
5257
5358 private void ChunkifyImage ( Bitmap b )
5459 {
5560 var numHorizontalChunks = ( int ) Math . Ceiling ( b . Width / DefaultChunkWidth ) ;
5661 var numVerticalChunks = ( int ) Math . Ceiling ( b . Height / DefaultChunkHeight ) ;
57- for ( var y = 0 ; y < numVerticalChunks ; y ++ )
62+ for ( var y = 0 ; y < numVerticalChunks ; y ++ )
63+ {
64+ _imageChunks . Add ( new List < Bitmap > ( ) ) ;
5865 for ( var x = 0 ; x < numHorizontalChunks ; x ++ )
5966 {
6067 var widthLeft = b . Width - ( x + 1 ) * DefaultChunkWidth ;
6168 var heightLeft = b . Height - ( y + 1 ) * DefaultChunkHeight ;
62- var curChunkWidth = ( int ) ( widthLeft < 0 ? widthLeft + DefaultChunkWidth : DefaultChunkWidth ) ;
63- var curChunkHeight = ( int ) ( heightLeft < 0 ? heightLeft + DefaultChunkHeight : DefaultChunkHeight ) ;
69+ var curChunkWidth = ( int ) ( widthLeft < 0 ? widthLeft + DefaultChunkWidth : DefaultChunkWidth ) ;
70+ var curChunkHeight = ( int ) ( heightLeft < 0 ? heightLeft + DefaultChunkHeight : DefaultChunkHeight ) ;
6471 var chunk = b . Clone (
65- new Rectangle ( x * ( int ) DefaultChunkWidth , y * ( int ) DefaultChunkHeight , curChunkWidth ,
72+ new Rectangle ( x * ( int ) DefaultChunkWidth , y * ( int ) DefaultChunkHeight , curChunkWidth ,
6673 curChunkHeight ) , PixelFormat . Format32bppArgb ) ;
67- _imageChunks . Add ( chunk ) ;
74+ _imageChunks [ y ] . Add ( chunk ) ;
6875 }
76+ }
6977 }
7078 private void Save ( string outString , string filename )
7179 {
7280 File . WriteAllText ( Path . Combine ( _outputPath , filename ) , outString ) ;
7381 Process . Start ( _outputPath ) ;
7482 }
7583
76- private void GenerateHtmlLegend ( HashSet < Floss > flosses )
84+ private void BuildLegend ( )
7785 {
78- var model = flosses . ToList ( ) ;
86+ var model = _color2Floss . Values . OrderBy ( x => x . DMC ) . ToList ( ) ;
7987 var template = File . ReadAllText ( "Templates/legend_razor.html" ) ;
80- var result = Engine . Razor . RunCompile ( template , "legendKey" , null , model ) ;
88+ var result = Engine . Razor . RunCompile ( template , "legendKey" , null , model ) ;
8189 PatternLegend = result ;
8290 }
8391 private void BuildCrossStitchPattern ( )
8492 {
85- foreach ( var chunk in _imageChunks )
86- {
87- var template = File . ReadAllText ( "Templates/pattern_razor.html" ) ;
88- var model = new PatternChunk { Chunk = chunk , ColorToFlossDict = _color2Floss , IsColorPattern = _isColored } ;
89- //Engine.Razor = RazorEngineService.Create(new TemplateServiceConfiguration { Debug = true });
90- var result = Engine . Razor . RunCompile ( template , "patternKey" , null , model ) ;
91- PatternChunks . Add ( result ) ;
92- }
93- }
94- private HashSet < Floss > GetDistinctFlossesInImage ( )
95- {
96- var flossesUsed = new HashSet < Floss > ( ) ;
97- foreach ( var chunk in _imageChunks )
98- for ( var y = 0 ; y < chunk . Height ; y ++ )
99- for ( var x = 0 ; x < chunk . Width ; x ++ )
93+ for ( var y = 0 ; y < _imageChunks . Count ; y ++ )
94+ for ( var x = 0 ; x < _imageChunks [ y ] . Count ; x ++ )
95+ {
96+ var startY = y * ( int ) DefaultChunkHeight ;
97+ var startX = x * ( int ) DefaultChunkWidth ;
98+ var template = File . ReadAllText ( "Templates/pattern_razor.html" ) ;
99+ var model = new PatternChunk
100100 {
101- flossesUsed . Add ( _color2Floss [ chunk . GetPixel ( x , y ) ] ) ;
102- }
103- return flossesUsed ;
101+ Chunk = _imageChunks [ y ] [ x ] ,
102+ ColorToFlossDict = _color2Floss ,
103+ IsColorPattern = _isColorPattern ,
104+ StartX = startX ,
105+ StartY = startY
106+ } ;
107+ var result = Engine . Razor . RunCompile ( template , "patternKey" , null , model ) ;
108+ PatternChunks . Add ( result ) ;
109+ }
104110 }
105111 }
106112}
0 commit comments