Mercurial > hg > aimc
comparison trunk/src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc @ 398:3ee03a6b95a0
- All \t to two spaces (style guide compliance)
author | tomwalters |
---|---|
date | Fri, 15 Oct 2010 05:46:53 +0000 |
parents | 7a573750b186 |
children | 7bfed53caacf |
comparison
equal
deleted
inserted
replaced
397:7a573750b186 | 398:3ee03a6b95a0 |
---|---|
34 | 34 |
35 #include "Support/util.h" | 35 #include "Support/util.h" |
36 #include "Output/GraphicsOutputDeviceCairo.h" | 36 #include "Output/GraphicsOutputDeviceCairo.h" |
37 | 37 |
38 GraphicsOutputDeviceCairo::GraphicsOutputDeviceCairo(Parameters *pParam) | 38 GraphicsOutputDeviceCairo::GraphicsOutputDeviceCairo(Parameters *pParam) |
39 : GraphicsOutputDevice(pParam) { | 39 : GraphicsOutputDevice(pParam) { |
40 m_bOutputFile = false; | 40 m_bOutputFile = false; |
41 m_iFileNumber = 0; | 41 m_iFileNumber = 0; |
42 m_iVertexType = VertexTypeNone; | 42 m_iVertexType = VertexTypeNone; |
43 m_bUseMemoryBuffer=false; | 43 m_bUseMemoryBuffer=false; |
44 } | 44 } |
45 | 45 |
46 bool GraphicsOutputDeviceCairo::Initialize(const char *sDir) { | 46 bool GraphicsOutputDeviceCairo::Initialize(const char *sDir) { |
47 Init(); | 47 Init(); |
48 | 48 |
49 //! \todo Output to file if sDir is a file, to directory with | 49 //! \todo Output to file if sDir is a file, to directory with |
50 //! multiple images if it's a directory. | 50 //! multiple images if it's a directory. |
51 strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0])); | 51 strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0])); |
52 | 52 |
53 /* Try to open an image to see if everything is allright. We want to avoid | 53 /* Try to open an image to see if everything is allright. We want to avoid |
54 * errors in the main Process()ing loop. */ | 54 * errors in the main Process()ing loop. */ |
55 if ( !OpenFile(0) ) { | 55 if ( !OpenFile(0) ) { |
56 //! \todo Better error message that is more specific about the cause. | 56 //! \todo Better error message that is more specific about the cause. |
57 AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."), | 57 AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."), |
58 m_sDir, m_pParam->GetString("output.img.format") ); | 58 m_sDir, m_pParam->GetString("output.img.format") ); |
59 return false; | 59 return false; |
60 } | 60 } |
61 CloseFile(); | 61 CloseFile(); |
62 | 62 |
63 return true; | 63 return true; |
64 } | 64 } |
65 | 65 |
66 bool GraphicsOutputDeviceCairo::Initialize() { | 66 bool GraphicsOutputDeviceCairo::Initialize() { |
67 Init(); | 67 Init(); |
68 m_bUseMemoryBuffer = true; | 68 m_bUseMemoryBuffer = true; |
69 return(true); | 69 return(true); |
70 } | 70 } |
71 | 71 |
72 void GraphicsOutputDeviceCairo::Init() { | 72 void GraphicsOutputDeviceCairo::Init() { |
73 AIM_ASSERT(m_pParam); | 73 AIM_ASSERT(m_pParam); |
74 /* | 74 /* |
75 * Set parameters | 75 * Set parameters |
76 */ | 76 */ |
77 m_pParam->GetString("output.img.color.background"); | 77 m_pParam->GetString("output.img.color.background"); |
78 | 78 |
79 m_bInvertColors = m_pParam->GetBool("output.img.color.invert"); | 79 m_bInvertColors = m_pParam->GetBool("output.img.color.invert"); |
80 | 80 |
81 // Output size! | 81 // Output size! |
82 m_iWidth = m_pParam->GetUInt("output.img.width"); | 82 m_iWidth = m_pParam->GetUInt("output.img.width"); |
83 m_iHeight = m_pParam->GetUInt("output.img.height"); | 83 m_iHeight = m_pParam->GetUInt("output.img.height"); |
84 } | 84 } |
85 | 85 |
86 unsigned char* GraphicsOutputDeviceCairo::GetBuffer() { | 86 unsigned char* GraphicsOutputDeviceCairo::GetBuffer() { |
87 if(m_bUseMemoryBuffer) | 87 if(m_bUseMemoryBuffer) |
88 return (cairo_image_surface_get_data (m_cSurface)); | 88 return (cairo_image_surface_get_data (m_cSurface)); |
134 m_cSurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, | 134 m_cSurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, |
135 m_iWidth, | 135 m_iWidth, |
136 m_iHeight); | 136 m_iHeight); |
137 m_cCr = cairo_create (m_cSurface); | 137 m_cCr = cairo_create (m_cSurface); |
138 cairo_scale(m_cCr, (float)m_iWidth, (float)m_iHeight); | 138 cairo_scale(m_cCr, (float)m_iWidth, (float)m_iHeight); |
139 // Now setup things for this plotter. | 139 // Now setup things for this plotter. |
140 cairo_select_font_face(m_cCr, | 140 cairo_select_font_face(m_cCr, |
141 m_pParam->GetString("output.img.fontname"), | 141 m_pParam->GetString("output.img.fontname"), |
142 CAIRO_FONT_SLANT_NORMAL, | 142 CAIRO_FONT_SLANT_NORMAL, |
143 CAIRO_FONT_WEIGHT_BOLD); | 143 CAIRO_FONT_WEIGHT_BOLD); |
144 cairo_set_font_size (m_cCr, 0.015); | 144 cairo_set_font_size (m_cCr, 0.015); |
145 return true; | 145 return true; |
146 } | 146 } |
147 | 147 |
148 void GraphicsOutputDeviceCairo::CloseFile() { | 148 void GraphicsOutputDeviceCairo::CloseFile() { |
149 // Plotting library | 149 // Plotting library |
150 if (m_iPlotHandle>0) { | 150 if (m_iPlotHandle>0) { |
151 cairo_destroy(m_cCr); | 151 cairo_destroy(m_cCr); |
152 m_iPlotHandle = 0; | 152 m_iPlotHandle = 0; |
153 } | 153 } |
154 // And the output file | 154 // And the output file |
155 if (m_bOutputFile) { | 155 if (m_bOutputFile) { |
156 cairo_surface_write_to_png(m_cSurface, m_sFilename); | 156 cairo_surface_write_to_png(m_cSurface, m_sFilename); |
157 m_bOutputFile = false; | 157 m_bOutputFile = false; |
158 } | 158 } |
159 cairo_surface_destroy(m_cSurface); | 159 cairo_surface_destroy(m_cSurface); |
160 } | 160 } |
161 | 161 |
162 GraphicsOutputDeviceCairo::~GraphicsOutputDeviceCairo() { | 162 GraphicsOutputDeviceCairo::~GraphicsOutputDeviceCairo() { |
163 AIM_ASSERT(!m_iPlotHandle); | 163 AIM_ASSERT(!m_iPlotHandle); |
164 CloseFile(); | 164 CloseFile(); |
165 } | 165 } |
166 | 166 |
167 void GraphicsOutputDeviceCairo::gGrab() { | 167 void GraphicsOutputDeviceCairo::gGrab() { |
168 // Open file. | 168 // Open file. |
169 if (!OpenFile(m_iFileNumber)) { | 169 if (!OpenFile(m_iFileNumber)) { |
170 return; | 170 return; |
171 } | 171 } |
172 // Setup plotting area. | 172 // Setup plotting area. |
173 cairo_set_line_width (m_cCr, 0.001f); | 173 cairo_set_line_width (m_cCr, 0.001f); |
174 gColor3f (0.0f, 0.0f, 0.0f); | 174 gColor3f (0.0f, 0.0f, 0.0f); |
175 cairo_paint (m_cCr); | 175 cairo_paint (m_cCr); |
176 gColor3f(1.0f, 1.0f, 0.0f); | 176 gColor3f(1.0f, 1.0f, 0.0f); |
177 } | 177 } |
178 | 178 |
179 int GraphicsOutputDeviceCairo::GetPixelFormat() { | 179 int GraphicsOutputDeviceCairo::GetPixelFormat() { |
180 return AIM_PIX_FMT_RGB24_32; | 180 return AIM_PIX_FMT_RGB24_32; |
181 } | 181 } |
182 | 182 |
183 void GraphicsOutputDeviceCairo::gBeginLineStrip() { | 183 void GraphicsOutputDeviceCairo::gBeginLineStrip() { |
184 m_bIsFirstVertex = true; | 184 m_bIsFirstVertex = true; |
185 m_iVertexType = VertexTypeLine; | 185 m_iVertexType = VertexTypeLine; |
186 //! \todo Make line width user-settable | 186 //! \todo Make line width user-settable |
187 cairo_set_line_width (m_cCr, 0.001f); | 187 cairo_set_line_width (m_cCr, 0.001f); |
188 } | 188 } |
189 | 189 |
190 void GraphicsOutputDeviceCairo::gBeginQuadStrip() { | 190 void GraphicsOutputDeviceCairo::gBeginQuadStrip() { |
191 m_bIsFirstVertex = true; | 191 m_bIsFirstVertex = true; |
192 m_iVertexType = VertexTypeQuad; | 192 m_iVertexType = VertexTypeQuad; |
193 m_iPrevVertexCount = 0; | 193 m_iPrevVertexCount = 0; |
194 cairo_set_line_width (m_cCr, 0.001f); | 194 cairo_set_line_width (m_cCr, 0.001f); |
195 } | 195 } |
196 | 196 |
197 void GraphicsOutputDeviceCairo::gColor3f(float r, float g, float b) { | 197 void GraphicsOutputDeviceCairo::gColor3f(float r, float g, float b) { |
198 if (m_bInvertColors) { | 198 if (m_bInvertColors) { |
199 r = 1-r; | 199 r = 1-r; |
200 g = 1-g; | 200 g = 1-g; |
201 b = 1-b; | 201 b = 1-b; |
202 } | 202 } |
203 cairo_set_source_rgb (m_cCr, r, g, b); | 203 cairo_set_source_rgb (m_cCr, r, g, b); |
204 } | 204 } |
205 | 205 |
206 void GraphicsOutputDeviceCairo::gVertex3f(float x, float y, float z) { | 206 void GraphicsOutputDeviceCairo::gVertex3f(float x, float y, float z) { |
207 switch(m_iVertexType) { | 207 switch(m_iVertexType) { |
208 case VertexTypeLine: | 208 case VertexTypeLine: |
209 if (m_bIsFirstVertex) { | 209 if (m_bIsFirstVertex) { |
210 m_bIsFirstVertex = false; | 210 m_bIsFirstVertex = false; |
211 //pl_fmove(x, y); | 211 //pl_fmove(x, y); |
212 cairo_move_to(m_cCr, x, 1-y); | 212 cairo_move_to(m_cCr, x, 1-y); |
213 } else { | 213 } else { |
214 //pl_fcont(x, y); | 214 //pl_fcont(x, y); |
215 cairo_line_to(m_cCr, x, 1-y); | 215 cairo_line_to(m_cCr, x, 1-y); |
216 } | 216 } |
217 break; | 217 break; |
218 case VertexTypeQuad: | 218 case VertexTypeQuad: |
219 /* Store vertices until we got four in a row. | 219 /* Store vertices until we got four in a row. |
220 * The order of vertices when processing quads is: | 220 * The order of vertices when processing quads is: |
221 * 1-----3-----5 | 221 * 1-----3-----5 |
222 * | | | | 222 * | | | |
223 * 0-----2-----4 | 223 * 0-----2-----4 |
224 */ | 224 */ |
225 if (m_iPrevVertexCount >= 3) { | 225 if (m_iPrevVertexCount >= 3) { |
226 // Plot this quad | 226 // Plot this quad |
227 cairo_move_to(m_cCr, m_aPrevX[0], 1-m_aPrevY[0]); | 227 cairo_move_to(m_cCr, m_aPrevX[0], 1-m_aPrevY[0]); |
228 cairo_line_to(m_cCr, m_aPrevX[1], 1-m_aPrevY[1]); | 228 cairo_line_to(m_cCr, m_aPrevX[1], 1-m_aPrevY[1]); |
229 cairo_line_to(m_cCr, x, y); | 229 cairo_line_to(m_cCr, x, y); |
230 cairo_line_to(m_cCr, m_aPrevX[2], 1-m_aPrevY[2]); | 230 cairo_line_to(m_cCr, m_aPrevX[2], 1-m_aPrevY[2]); |
231 cairo_close_path (m_cCr); | 231 cairo_close_path (m_cCr); |
232 | 232 |
233 // Last vertices of this quad are the first of the next | 233 // Last vertices of this quad are the first of the next |
234 m_aPrevX[0] = m_aPrevX[2]; | 234 m_aPrevX[0] = m_aPrevX[2]; |
235 m_aPrevY[0] = m_aPrevY[2]; | 235 m_aPrevY[0] = m_aPrevY[2]; |
236 m_aPrevX[1] = x; | 236 m_aPrevX[1] = x; |
237 m_aPrevY[1] = y; | 237 m_aPrevY[1] = y; |
238 m_iPrevVertexCount = 2; | 238 m_iPrevVertexCount = 2; |
239 } else { | 239 } else { |
240 // Not at the fourth, keep storing | 240 // Not at the fourth, keep storing |
241 m_aPrevX[m_iPrevVertexCount] = x; | 241 m_aPrevX[m_iPrevVertexCount] = x; |
242 m_aPrevY[m_iPrevVertexCount] = y; | 242 m_aPrevY[m_iPrevVertexCount] = y; |
243 m_iPrevVertexCount++; | 243 m_iPrevVertexCount++; |
244 } | 244 } |
245 break; | 245 break; |
246 default: | 246 default: |
247 // Should not happen | 247 // Should not happen |
248 AIM_ASSERT(0); | 248 AIM_ASSERT(0); |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 void GraphicsOutputDeviceCairo::gEnd() { | 252 void GraphicsOutputDeviceCairo::gEnd() { |
253 if(m_iVertexType==VertexTypeLine) | 253 if(m_iVertexType==VertexTypeLine) |
254 cairo_stroke (m_cCr); | 254 cairo_stroke (m_cCr); |
255 else | 255 else |
256 cairo_fill (m_cCr); | 256 cairo_fill (m_cCr); |
257 m_iVertexType = VertexTypeNone; | 257 m_iVertexType = VertexTypeNone; |
258 } | 258 } |
259 | 259 |
260 void GraphicsOutputDeviceCairo::gText3f(float x, | 260 void GraphicsOutputDeviceCairo::gText3f(float x, |
261 float y, | 261 float y, |
262 float z, | 262 float z, |
263 const char *sStr, | 263 const char *sStr, |
264 bool bRotated) { | 264 bool bRotated) { |
265 cairo_text_extents_t te; | 265 cairo_text_extents_t te; |
266 if (bRotated) { | 266 if (bRotated) { |
267 cairo_rotate(m_cCr, M_PI/2); | 267 cairo_rotate(m_cCr, M_PI/2); |
268 cairo_move_to(m_cCr, x ,1-y); | 268 cairo_move_to(m_cCr, x ,1-y); |
269 cairo_show_text(m_cCr, sStr); | 269 cairo_show_text(m_cCr, sStr); |
270 //cairo_identity_matrix(m_cCr); | 270 //cairo_identity_matrix(m_cCr); |
271 cairo_rotate(m_cCr, -M_PI/2); | 271 cairo_rotate(m_cCr, -M_PI/2); |
272 } else { | 272 } else { |
273 cairo_move_to(m_cCr, x ,1-y); | 273 cairo_move_to(m_cCr, x ,1-y); |
274 cairo_show_text(m_cCr, sStr); | 274 cairo_show_text(m_cCr, sStr); |
275 } | 275 } |
276 } | 276 } |
277 | 277 |
278 void GraphicsOutputDeviceCairo::gRelease() { | 278 void GraphicsOutputDeviceCairo::gRelease() { |
279 AIM_ASSERT(m_iPlotHandle>0); | 279 AIM_ASSERT(m_iPlotHandle>0); |
280 CloseFile(); | 280 CloseFile(); |
281 // Finished this one, up to the next! | 281 // Finished this one, up to the next! |
282 m_iFileNumber++; | 282 m_iFileNumber++; |
283 } | 283 } |