-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTiler.cs
152 lines (119 loc) · 5.43 KB
/
Tiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
namespace LeafletPano
{
public class Tiler
{
public string ImageFile { get; private set; }
public int OrgLevel { get; private set; }
public int MaxLevel { get; set; }
public int MinLevel { get; set; }
public Image SourceImage { get; private set; }
public Color BackgroundColor { get; set; }
public Tiler(string imageFile)
{
this.ImageFile = imageFile;
SourceImage = Image.FromFile(imageFile);
int width = SourceImage.Width;
int height = SourceImage.Height;
int size = Math.Max(width, height);
double numTiles = size / 256.0;
OrgLevel = (int)Math.Ceiling(Math.Log(numTiles, 2)); // the level where the image is 100%
MinLevel = 0; // the minimum tile level
MaxLevel = OrgLevel; // the maximum tile level
}
public event EventHandler<SendFeedbackEventArgs> SendFeedback;
public event EventHandler<CompletedEventArgs> Completed;
BackgroundWorker bw;
public void CreateTiles()
{
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.WorkerReportsProgress = true;
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Completed != null)
Completed(this, new CompletedEventArgs { HtmlFile = e.Result as string });
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (SendFeedback != null)
SendFeedback(this, e.UserState as SendFeedbackEventArgs);
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
string directoryName = Path.GetDirectoryName(ImageFile);
string imageName = Path.GetFileNameWithoutExtension(ImageFile);
Directory.CreateDirectory(directoryName + "/" + imageName);
int width = SourceImage.Width;
int height = SourceImage.Height;
int size = Math.Max(width, height);
double numTiles = size / 256.0;
for (int level = MinLevel; level <= MaxLevel; level++)
{
int logTileSize = (OrgLevel > level) ? 256 << (OrgLevel - level) : 256 >> (level - OrgLevel);
int numTilesX = (width % logTileSize == 0) ? width / logTileSize : width / logTileSize + 1;
int numTilesY = (height % logTileSize == 0) ? height / logTileSize : height / logTileSize + 1;
for (int tx = 0; tx < numTilesX; tx++)
for (int ty = 0; ty < numTilesY; ty++)
{
using(var tileImg = new Bitmap(256, 256))
using(var graphcis = Graphics.FromImage(tileImg))
{
graphcis.FillRectangle(new SolidBrush(BackgroundColor), 0, 0, 256, 256);
graphcis.DrawImage(SourceImage, new Rectangle(0, 0, 256, 256),
new Rectangle(tx * logTileSize, ty * logTileSize, logTileSize, logTileSize),
GraphicsUnit.Pixel);
tileImg.Save(string.Format("{0}/{1}/{2}-{3}-{4}.jpg", directoryName, imageName, level, tx, ty),
System.Drawing.Imaging.ImageFormat.Jpeg);
bw.ReportProgress(0, new SendFeedbackEventArgs { Level = level, X = tx, Y = ty, Image = tileImg.Clone() as Bitmap});
}
}
}
string template;
using (var reader = new StreamReader(Application.StartupPath + "\\Template.html"))
{
template = reader.ReadToEnd();
reader.Close();
}
template = template.Replace("//image//", imageName);
template = template.Replace("//width//", width.ToString());
template = template.Replace("//height//", height.ToString());
template = template.Replace("//maxLevel//", MaxLevel.ToString());
template = template.Replace("//minLevel//", MinLevel.ToString());
template = template.Replace("//orgLevel//", OrgLevel.ToString());
template = template.Replace("//bgColor//", ColorTranslator.ToHtml(BackgroundColor));
string htmlFile = directoryName + "/" + imageName + ".html";
using (var writer = new StreamWriter(htmlFile))
{
writer.Write(template);
writer.Close();
}
e.Result = htmlFile;
}
public void Dispose()
{
SourceImage.Dispose();
}
}
public class SendFeedbackEventArgs : EventArgs
{
public int Level { get; set; }
public int X { get; set; }
public int Y { get; set; }
public Bitmap Image { get; set; }
}
public class CompletedEventArgs : EventArgs
{
public string HtmlFile { get; set; }
}
}