1 | #include <QImage> |
---|
2 | #include <QGLWidget> |
---|
3 | #include <QPainter> |
---|
4 | #include "ProjectionModel.h" |
---|
5 | #include "Channel.h" |
---|
6 | #include "RSync.h" |
---|
7 | #include "GUIControler.h" |
---|
8 | |
---|
9 | #include "Exporter.h" |
---|
10 | |
---|
11 | using namespace projection; |
---|
12 | |
---|
13 | /** |
---|
14 | * Constructor. |
---|
15 | * |
---|
16 | * @param pModel Projection model. |
---|
17 | */ |
---|
18 | Exporter::Exporter(ProjectionModel* pModel) |
---|
19 | { |
---|
20 | m_pModel = pModel; |
---|
21 | |
---|
22 | m_bSoftEdge = true; |
---|
23 | m_blendEdgeWidth = 16; |
---|
24 | m_blendEdgeExponent = 1.0f; |
---|
25 | m_exportWidth = 512; |
---|
26 | m_exportHeight = 512; |
---|
27 | m_distortMapFileNamePattern = "distort_%CHANNELNAME%.bmp"; |
---|
28 | m_blendMapFileNamePattern = "blend_%CHANNELNAME%.bmp"; |
---|
29 | m_viewMatrixFileNamePattern = "view_%CHANNELNAME%.cfg"; |
---|
30 | |
---|
31 | m_texCoordColorTexIndex = 0; |
---|
32 | m_texCoordColorRTexIndex = 0; |
---|
33 | m_blendAreaTexIndex = 0; |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Destructor. |
---|
38 | */ |
---|
39 | Exporter::~Exporter() |
---|
40 | { |
---|
41 | if (glIsTexture(m_texCoordColorTexIndex)) |
---|
42 | glDeleteTextures(1, &m_texCoordColorTexIndex); |
---|
43 | if (glIsTexture(m_blendAreaTexIndex)) |
---|
44 | glDeleteTextures(1, &m_blendAreaTexIndex); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Enable or disable soft edge blending. |
---|
49 | * |
---|
50 | * @param bSoftEdge True if soft edge blending is enabled. |
---|
51 | */ |
---|
52 | void Exporter::setSoftEdge(bool bSoftEdge) |
---|
53 | { |
---|
54 | if (m_bSoftEdge != bSoftEdge) { |
---|
55 | m_bSoftEdge = bSoftEdge; |
---|
56 | if (glIsTexture(m_blendAreaTexIndex)) |
---|
57 | glDeleteTextures(1, &m_blendAreaTexIndex); |
---|
58 | m_blendAreaTexIndex = 0; |
---|
59 | if (m_pModel->getRSync()->isServer()) |
---|
60 | { |
---|
61 | QDomDocument doc; |
---|
62 | doc.appendChild(domElement("Exporter", doc)); |
---|
63 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORT, doc.toString(0)); |
---|
64 | } |
---|
65 | if (m_pModel->getDesignMode() == DESIGN_MODE_BLENDMAP) |
---|
66 | m_pModel->updateViews(); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Set width of the soft edge blending. |
---|
72 | * |
---|
73 | * @param blendWidth Width of the soft edge blending. |
---|
74 | */ |
---|
75 | void Exporter::setBlendEdgeWidth(int blendEdgeWidth) |
---|
76 | { |
---|
77 | if (m_blendEdgeWidth != blendEdgeWidth) { |
---|
78 | m_blendEdgeWidth = blendEdgeWidth; |
---|
79 | if (glIsTexture(m_blendAreaTexIndex)) |
---|
80 | glDeleteTextures(1, &m_blendAreaTexIndex); |
---|
81 | m_blendAreaTexIndex = 0; |
---|
82 | if (m_pModel->getRSync()->isServer()) |
---|
83 | { |
---|
84 | QDomDocument doc; |
---|
85 | doc.appendChild(domElement("Exporter", doc)); |
---|
86 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORT, doc.toString(0)); |
---|
87 | } |
---|
88 | if (m_pModel->getDesignMode() == DESIGN_MODE_BLENDMAP) |
---|
89 | m_pModel->updateViews(); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Set gamma curve of blend map edge. |
---|
95 | * |
---|
96 | * @param gamma Curve exponent of blend map edge. |
---|
97 | */ |
---|
98 | void Exporter::setBlendEdgeExponent(float blendEdgeExponent) |
---|
99 | { |
---|
100 | if (m_blendEdgeExponent != blendEdgeExponent) { |
---|
101 | m_blendEdgeExponent = blendEdgeExponent; |
---|
102 | if (glIsTexture(m_blendAreaTexIndex)) |
---|
103 | glDeleteTextures(1, &m_blendAreaTexIndex); |
---|
104 | m_blendAreaTexIndex = 0; |
---|
105 | if (m_pModel->getRSync()->isServer()) |
---|
106 | { |
---|
107 | QDomDocument doc; |
---|
108 | doc.appendChild(domElement("Exporter", doc)); |
---|
109 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORT, doc.toString(0)); |
---|
110 | } |
---|
111 | if (m_pModel->getDesignMode() == DESIGN_MODE_BLENDMAP) |
---|
112 | m_pModel->updateViews(); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Set width of distortion map to export. |
---|
118 | * |
---|
119 | * @param width Width of distortion map to export. |
---|
120 | */ |
---|
121 | void Exporter::setExportWidth(int exportWidth) |
---|
122 | { |
---|
123 | if (m_exportWidth != exportWidth) { |
---|
124 | m_exportWidth = exportWidth; |
---|
125 | if (m_pModel->getRSync()->isServer()) |
---|
126 | { |
---|
127 | QDomDocument doc; |
---|
128 | doc.appendChild(domElement("Exporter", doc)); |
---|
129 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORT, doc.toString(0)); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Set height of distortion map to export. |
---|
136 | * |
---|
137 | * @param height Height of distortion map to export. |
---|
138 | */ |
---|
139 | void Exporter::setExportHeight(int exportHeight) |
---|
140 | { |
---|
141 | if (m_exportHeight != exportHeight) { |
---|
142 | m_exportHeight = exportHeight; |
---|
143 | if (m_pModel->getRSync()->isServer()) |
---|
144 | { |
---|
145 | QDomDocument doc; |
---|
146 | doc.appendChild(domElement("Exporter", doc)); |
---|
147 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORT, doc.toString(0)); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Create a color encoded coordinate texture and retrieve its texture object index. |
---|
154 | * |
---|
155 | * @return Texture object index of the encoded coordinate texture. |
---|
156 | */ |
---|
157 | unsigned int Exporter::getTexCoordColorTexture() |
---|
158 | { |
---|
159 | if (m_texCoordColorTexIndex == 0) |
---|
160 | { |
---|
161 | unsigned char *data; |
---|
162 | data = new unsigned char[256*256*4]; |
---|
163 | int count = 0; |
---|
164 | for (unsigned int y=0; y<256; ++y) { |
---|
165 | for (unsigned int x=0; x<256; ++x) { |
---|
166 | data[x*4+y*256*4+0] = x; |
---|
167 | data[x*4+y*256*4+1] = y; |
---|
168 | data[x*4+y*256*4+2] = 0; |
---|
169 | data[x*4+y*256*4+3] = 255; |
---|
170 | count++; |
---|
171 | } |
---|
172 | } |
---|
173 | QImage image((uchar *)data, 256, 256, QImage::Format_RGB32); |
---|
174 | m_texCoordColorTexIndex = m_pModel->getGUI()->getGLWidget()->bindTexture(image); |
---|
175 | delete [] data; |
---|
176 | } |
---|
177 | return m_texCoordColorTexIndex; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Create a color encoded coordinate texture (red channel) and retrieve its texture object index. |
---|
182 | * |
---|
183 | * @return Texture object index of the encoded coordinate texture (red channel). |
---|
184 | */ |
---|
185 | unsigned int Exporter::getTexCoordColorRTexture() |
---|
186 | { |
---|
187 | if (m_texCoordColorRTexIndex == 0) |
---|
188 | { |
---|
189 | unsigned char* data; |
---|
190 | data = new unsigned char[16*16*4]; |
---|
191 | int count = 0; |
---|
192 | for (unsigned int y=0; y<16; ++y) { |
---|
193 | for (unsigned int x=0; x<16; ++x) { |
---|
194 | data[x*4+y*16*4+0] = 0; |
---|
195 | data[x*4+y*16*4+1] = 0; |
---|
196 | data[x*4+y*16*4+2] = x+y*16; |
---|
197 | data[x*4+y*16*4+3] = 255; |
---|
198 | count++; |
---|
199 | } |
---|
200 | } |
---|
201 | QImage image((uchar *)data, 16, 16, QImage::Format_RGB32); |
---|
202 | m_texCoordColorRTexIndex = m_pModel->getGUI()->getGLWidget()->bindTexture(image); |
---|
203 | delete [] data; |
---|
204 | } |
---|
205 | return m_texCoordColorRTexIndex; |
---|
206 | } |
---|
207 | /** |
---|
208 | * Create an edge blending texture and retrieve its texture object index. |
---|
209 | * |
---|
210 | * @return Texture object index of the edge blending texture. |
---|
211 | */ |
---|
212 | unsigned int Exporter::getBlendingAreaTexture() |
---|
213 | { |
---|
214 | if (m_blendAreaTexIndex == 0) |
---|
215 | { |
---|
216 | int blendBaseValue = 255; |
---|
217 | QPixmap pixmap(128, 128); |
---|
218 | pixmap.fill(QColor(blendBaseValue, blendBaseValue, blendBaseValue)); |
---|
219 | if (m_bSoftEdge) |
---|
220 | { |
---|
221 | QPainter painter(&pixmap); |
---|
222 | QPen pen; |
---|
223 | for (int w=0; w<m_blendEdgeWidth; w++) { |
---|
224 | int value = int(pow(1.0f/m_blendEdgeWidth*(m_blendEdgeWidth-w-1), m_blendEdgeExponent) * blendBaseValue); |
---|
225 | pen.setColor(QColor(value, value, value)); |
---|
226 | pen.setWidth(m_blendEdgeWidth-w); |
---|
227 | painter.setPen(pen); |
---|
228 | painter.drawRect(0, 0, 127, 127); |
---|
229 | } |
---|
230 | } |
---|
231 | m_blendAreaTexIndex = m_pModel->getGUI()->getGLWidget()->bindTexture(pixmap); |
---|
232 | } |
---|
233 | return m_blendAreaTexIndex; |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * Export projection settings to file. |
---|
238 | * |
---|
239 | * @param fileName File name of the projection settings. |
---|
240 | * @return True if successfully exported. |
---|
241 | */ |
---|
242 | bool Exporter::exportDataset(const QString& fileName) |
---|
243 | { |
---|
244 | for (unsigned int i=0; i<m_pModel->getNumChannels(); ++i) |
---|
245 | m_pModel->getChannel(i)->exportDataset(fileName); |
---|
246 | if (m_pModel->getRSync()->isServer()) |
---|
247 | m_pModel->getRSync()->sendChanges(RSYNC_COMMAND_EXPORTFILE, fileName); |
---|
248 | |
---|
249 | return true; |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Restore the export settings from XML data. |
---|
254 | * |
---|
255 | * @param element Parent XML element of the export settings data. |
---|
256 | */ |
---|
257 | bool Exporter::initFromDOMElement(const QDomElement& element) |
---|
258 | { |
---|
259 | if (!element.isNull()) |
---|
260 | { |
---|
261 | setSoftEdge(element.attribute("softEdge")=="true"); |
---|
262 | setBlendEdgeWidth(element.attribute("blendEdgeWidth").toInt()); |
---|
263 | setBlendEdgeExponent(element.attribute("blendEdgeExponent").toFloat()); |
---|
264 | m_exportWidth = element.attribute("exportWidth").toInt(); |
---|
265 | m_exportHeight = element.attribute("exportHeight").toInt(); |
---|
266 | m_distortMapFileNamePattern = element.attribute("distortMapFileNamePattern"); |
---|
267 | m_blendMapFileNamePattern = element.attribute("blendMapFileNamePattern"); |
---|
268 | m_viewMatrixFileNamePattern = element.attribute("viewMatrixFileNamePattern"); |
---|
269 | if (m_distortMapFileNamePattern.isEmpty()) |
---|
270 | m_distortMapFileNamePattern = "distort_%CHANNELNAME%.bmp"; |
---|
271 | if (m_blendMapFileNamePattern.isEmpty()) |
---|
272 | m_blendMapFileNamePattern = "blend_%CHANNELNAME%.bmp"; |
---|
273 | if (m_viewMatrixFileNamePattern.isEmpty()) |
---|
274 | m_viewMatrixFileNamePattern = "view_%CHANNELNAME%.cfg"; |
---|
275 | } |
---|
276 | |
---|
277 | return true; // todo: secure this function and return false on any critical error |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Store the current export settings as XML data. |
---|
282 | * |
---|
283 | * @param name XML node name of the data. |
---|
284 | * @param doc XML document to store the data. |
---|
285 | * @return Current export settings data as XML data. |
---|
286 | */ |
---|
287 | QDomElement Exporter::domElement(const QString& name, QDomDocument& doc) const |
---|
288 | { |
---|
289 | QDomElement de = doc.createElement(name); |
---|
290 | de.setAttribute("softEdge", m_bSoftEdge?"true":"false"); |
---|
291 | de.setAttribute("blendEdgeWidth", QString::number(m_blendEdgeWidth)); |
---|
292 | de.setAttribute("blendEdgeExponent", QString::number(m_blendEdgeExponent)); |
---|
293 | de.setAttribute("exportWidth", QString::number(m_exportWidth)); |
---|
294 | de.setAttribute("exportHeight", QString::number(m_exportHeight)); |
---|
295 | de.setAttribute("distortMapFileNamePattern", m_distortMapFileNamePattern); |
---|
296 | de.setAttribute("blendMapFileNamePattern", m_blendMapFileNamePattern); |
---|
297 | de.setAttribute("viewMatrixFileNamePattern", m_viewMatrixFileNamePattern); |
---|
298 | |
---|
299 | return de; |
---|
300 | } |
---|