tomwalters@411
|
1 // Copyright 2007-2010, Thomas Walters, Willem van Engen
|
tomwalters@397
|
2 //
|
tomwalters@397
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@397
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@397
|
5 //
|
tomwalters@397
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@397
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@397
|
8 // You may obtain a copy of the License at
|
tomwalters@397
|
9 //
|
tomwalters@397
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@397
|
11 //
|
tomwalters@397
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@397
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@397
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@397
|
15 // See the License for the specific language governing permissions and
|
tomwalters@397
|
16 // limitations under the License.
|
tomwalters@397
|
17
|
tomwalters@397
|
18 /*!
|
tomwalters@397
|
19 * \file
|
tomwalters@397
|
20 * \brief Output device for output to a graphics file using cairo (LGPL)
|
tomwalters@397
|
21 *
|
tomwalters@397
|
22 * \author Tom Walters <tom@acousticscale.org> and Willem van Engen <cnbh@willem.engen.nl>
|
tomwalters@397
|
23 * \date created 2007/09/17
|
tomwalters@397
|
24 * \version \$Header: $
|
tomwalters@397
|
25 */
|
tomwalters@397
|
26
|
tomwalters@397
|
27 #include "Support/Common.h"
|
tomwalters@397
|
28
|
tomwalters@397
|
29 #include <sys/types.h>
|
tomwalters@397
|
30 #include <sys/stat.h>
|
tomwalters@397
|
31 #include <string.h>
|
tomwalters@397
|
32 #include <stdio.h>
|
tomwalters@397
|
33 #include <math.h>
|
tomwalters@412
|
34 #include <limits.h>
|
tomwalters@397
|
35
|
tomwalters@444
|
36 #ifdef _WINDOWS
|
tomwalters@444
|
37 # include <direct.h> // for _mkdir&_rmdir
|
tomwalters@444
|
38 #else
|
tomwalters@444
|
39
|
tomwalters@412
|
40 //#include "cairo-quartz.h"
|
tomwalters@411
|
41
|
tom@399
|
42 #include "Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.h"
|
tom@399
|
43
|
tom@399
|
44 namespace aimc {
|
tomwalters@397
|
45
|
tomwalters@397
|
46 GraphicsOutputDeviceCairo::GraphicsOutputDeviceCairo(Parameters *pParam)
|
tom@399
|
47 : GraphicsOutputDevice(pParam) {
|
tomwalters@398
|
48 m_bOutputFile = false;
|
tomwalters@398
|
49 m_iFileNumber = 0;
|
tomwalters@398
|
50 m_iVertexType = VertexTypeNone;
|
tomwalters@398
|
51 m_bUseMemoryBuffer=false;
|
tomwalters@443
|
52 parameters_->DefaultString("output.img.format", "png");
|
tomwalters@443
|
53 }
|
tomwalters@443
|
54
|
tomwalters@443
|
55 void GraphicsOutputDeviceCairo::Reset(Parameters* global_parameters) {
|
tomwalters@443
|
56 Initialize(global_parameters);
|
tomwalters@443
|
57 }
|
tomwalters@443
|
58
|
tomwalters@443
|
59 bool GraphicsOutputDeviceCairo::Initialize(Parameters *global_parameters) {
|
tomwalters@443
|
60 global_parameters_ = global_parameters;
|
tomwalters@443
|
61 #ifdef _WINDOWS
|
tomwalters@443
|
62 string pathsep("\\");
|
tomwalters@443
|
63 #else
|
tomwalters@443
|
64 string pathsep("/");
|
tomwalters@443
|
65 #endif
|
tomwalters@443
|
66 directory_ = global_parameters->GetString("output_filename_base") + pathsep;
|
tomwalters@443
|
67 //! \todo Make build system check for mkdtemp() to use it when available. See TODO.txt.
|
tomwalters@443
|
68 #ifdef _WINDOWS
|
tomwalters@443
|
69 if (_mkdir(directory_.c_str()) < 0) {
|
tomwalters@443
|
70 LOG_ERROR(_T("Couldn't create directory for image output."));
|
tomwalters@443
|
71 return false;
|
tomwalters@443
|
72 }
|
tomwalters@443
|
73 #else
|
tomwalters@443
|
74 mkdir(directory_.c_str(), S_IRWXU);
|
tomwalters@443
|
75 #endif
|
tomwalters@443
|
76 InitialzeInternal();
|
tomwalters@443
|
77 return true;
|
tomwalters@397
|
78 }
|
tomwalters@397
|
79
|
tomwalters@411
|
80 bool GraphicsOutputDeviceCairo::Initialize(string directory) {
|
tomwalters@411
|
81 directory_ = directory;
|
tomwalters@443
|
82 InitialzeInternal();
|
tomwalters@397
|
83
|
tomwalters@398
|
84 /* Try to open an image to see if everything is allright. We want to avoid
|
tomwalters@398
|
85 * errors in the main Process()ing loop. */
|
tomwalters@411
|
86 /*if (!OpenFile(0)) {
|
tomwalters@398
|
87 //! \todo Better error message that is more specific about the cause.
|
tom@400
|
88 LOG_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."),
|
tomwalters@411
|
89 directory_.c_str(), parameters_->DefaultString("output.img.format", "png"));
|
tomwalters@398
|
90 return false;
|
tomwalters@398
|
91 }
|
tomwalters@411
|
92 CloseFile();*/
|
tomwalters@397
|
93
|
tomwalters@398
|
94 return true;
|
tomwalters@397
|
95 }
|
tomwalters@397
|
96
|
tomwalters@411
|
97 /*bool GraphicsOutputDeviceCairo::Initialize() {
|
tomwalters@397
|
98 Init();
|
tomwalters@397
|
99 m_bUseMemoryBuffer = true;
|
tomwalters@397
|
100 return(true);
|
tomwalters@411
|
101 }*/
|
tomwalters@397
|
102
|
tomwalters@443
|
103 void GraphicsOutputDeviceCairo::InitialzeInternal() {
|
tomwalters@411
|
104 AIM_ASSERT(parameters_);
|
tomwalters@397
|
105
|
tomwalters@411
|
106 parameters_->DefaultString("output.img.color.background", "black");
|
tomwalters@397
|
107
|
tomwalters@411
|
108 m_bInvertColors = parameters_->DefaultBool("output.img.color.invert", "false");
|
tomwalters@411
|
109
|
tomwalters@411
|
110 // Output size.
|
tomwalters@411
|
111 m_iWidth = parameters_->DefaultInt("output.img.width", 800);
|
tomwalters@411
|
112 m_iHeight = parameters_->DefaultInt("output.img.height", 600);
|
tomwalters@411
|
113
|
tomwalters@411
|
114 // Cairo's RGB24 format has 32-bit pixels with the upper 8 bits unused.
|
tomwalters@411
|
115 // This is not the same as the plotutils PNG format. This information is transferred by the
|
tomwalters@411
|
116 // function GetPixelFormat. The pixel format is dealt with by the reciever.
|
tomwalters@412
|
117 m_cSurface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
|
tomwalters@411
|
118 m_iWidth,
|
tomwalters@411
|
119 m_iHeight);
|
tomwalters@411
|
120 m_cCr = cairo_create(m_cSurface);
|
tomwalters@411
|
121 cairo_scale(m_cCr, (float)m_iWidth, (float)m_iHeight);
|
tomwalters@411
|
122 // Now setup things for this plotter.
|
tomwalters@411
|
123 cairo_select_font_face(m_cCr,
|
tomwalters@411
|
124 parameters_->DefaultString("output.img.fontname",
|
tomwalters@411
|
125 "HersheySans"),
|
tomwalters@411
|
126 CAIRO_FONT_SLANT_NORMAL,
|
tomwalters@411
|
127 CAIRO_FONT_WEIGHT_BOLD);
|
tomwalters@411
|
128 cairo_set_font_size (m_cCr, 0.02);
|
tomwalters@397
|
129 }
|
tomwalters@397
|
130
|
tomwalters@397
|
131 unsigned char* GraphicsOutputDeviceCairo::GetBuffer() {
|
tomwalters@397
|
132 if(m_bUseMemoryBuffer)
|
tomwalters@397
|
133 return (cairo_image_surface_get_data (m_cSurface));
|
tomwalters@397
|
134 else
|
tomwalters@397
|
135 return NULL;
|
tomwalters@397
|
136 }
|
tomwalters@397
|
137
|
tomwalters@397
|
138 bool GraphicsOutputDeviceCairo::OpenFile(unsigned int index) {
|
tomwalters@411
|
139 const char *strPlottype = parameters_->GetString("output.img.format");
|
tomwalters@397
|
140 if (!m_bUseMemoryBuffer) {
|
tomwalters@397
|
141 struct stat fileinfo;
|
tomwalters@397
|
142 // Get filename without trailing slash
|
tomwalters@411
|
143 char filename[PATH_MAX];
|
tomwalters@411
|
144 strncpy(filename, directory_.c_str(), sizeof(filename)/sizeof(filename[0]));
|
tomwalters@397
|
145 #ifdef _WINDOWS
|
tomwalters@411
|
146 if (filename[strlen(filename)-1]=='\\') {
|
tomwalters@411
|
147 filename[strlen(filename)-1]='\0';
|
tomwalters@397
|
148 }
|
tomwalters@397
|
149 #else
|
tomwalters@411
|
150 if (filename[strlen(filename)-1]=='/') {
|
tomwalters@411
|
151 filename[strlen(filename)-1]='\0';
|
tomwalters@397
|
152 }
|
tomwalters@397
|
153 #endif
|
tomwalters@397
|
154 // Enumerate files it m_sDir is a directory.
|
tomwalters@411
|
155 if (stat(filename, &fileinfo) == 0 && (fileinfo.st_mode & S_IFDIR)) {
|
tomwalters@397
|
156 // We have a directory: enumerate with index
|
tomwalters@411
|
157 snprintf(filename, sizeof(filename) / sizeof(filename[0]),
|
tomwalters@411
|
158 "%s%06d.%s",
|
tomwalters@411
|
159 directory_.c_str(),
|
tomwalters@397
|
160 index,
|
tomwalters@397
|
161 strPlottype);
|
tomwalters@397
|
162 // If type is 'auto', fallback to 'png'
|
tomwalters@397
|
163 if (strcmp(strPlottype, "auto")==0)
|
tomwalters@397
|
164 strPlottype = "png";
|
tomwalters@397
|
165 } else {
|
tomwalters@397
|
166 // We have a (probably non-existant) file. Auto-detect type by extension if requested
|
tomwalters@411
|
167 strncpy(filename,
|
tomwalters@411
|
168 directory_.c_str(),
|
tomwalters@411
|
169 sizeof(filename)/sizeof(filename[0]));
|
tomwalters@411
|
170 char *pDot = strrchr(filename, '.');
|
tomwalters@397
|
171 if (!pDot) {
|
tom@400
|
172 LOG_ERROR(_T("Please supply extension on filename when using 'auto' format: '%s'"),
|
tomwalters@411
|
173 filename);
|
tomwalters@397
|
174 return false;
|
tomwalters@397
|
175 }
|
tomwalters@397
|
176 strPlottype = &pDot[1];
|
tomwalters@397
|
177 }
|
tomwalters@411
|
178 image_filename_ = filename;
|
tomwalters@397
|
179 m_bOutputFile= true; //! \todo Should check that it's possible to write to the file
|
tomwalters@397
|
180 }
|
tomwalters@411
|
181
|
tomwalters@398
|
182 return true;
|
tomwalters@397
|
183 }
|
tomwalters@397
|
184
|
tomwalters@397
|
185 void GraphicsOutputDeviceCairo::CloseFile() {
|
tomwalters@398
|
186 // And the output file
|
tomwalters@398
|
187 if (m_bOutputFile) {
|
tomwalters@411
|
188 cairo_surface_write_to_png(m_cSurface, image_filename_.c_str());
|
tomwalters@398
|
189 m_bOutputFile = false;
|
tomwalters@398
|
190 }
|
tomwalters@411
|
191 cairo_set_source_rgb (m_cCr, 0.0, 0.0, 0.0);
|
tomwalters@411
|
192 cairo_paint (m_cCr);
|
tomwalters@411
|
193 //cairo_destroy(m_cCr);
|
tomwalters@411
|
194 //cairo_surface_destroy(m_cSurface);
|
tomwalters@397
|
195 }
|
tomwalters@397
|
196
|
tomwalters@397
|
197 GraphicsOutputDeviceCairo::~GraphicsOutputDeviceCairo() {
|
tomwalters@398
|
198 AIM_ASSERT(!m_iPlotHandle);
|
tomwalters@398
|
199 CloseFile();
|
tomwalters@397
|
200 }
|
tomwalters@397
|
201
|
tomwalters@397
|
202 void GraphicsOutputDeviceCairo::gGrab() {
|
tomwalters@397
|
203 // Open file.
|
tomwalters@398
|
204 if (!OpenFile(m_iFileNumber)) {
|
tomwalters@398
|
205 return;
|
tomwalters@397
|
206 }
|
tomwalters@398
|
207 // Setup plotting area.
|
tomwalters@398
|
208 cairo_set_line_width (m_cCr, 0.001f);
|
tomwalters@398
|
209 gColor3f (0.0f, 0.0f, 0.0f);
|
tomwalters@397
|
210 cairo_paint (m_cCr);
|
tomwalters@397
|
211 gColor3f(1.0f, 1.0f, 0.0f);
|
tomwalters@397
|
212 }
|
tomwalters@397
|
213
|
tomwalters@397
|
214 int GraphicsOutputDeviceCairo::GetPixelFormat() {
|
tomwalters@397
|
215 return AIM_PIX_FMT_RGB24_32;
|
tomwalters@397
|
216 }
|
tomwalters@397
|
217
|
tomwalters@397
|
218 void GraphicsOutputDeviceCairo::gBeginLineStrip() {
|
tomwalters@398
|
219 m_bIsFirstVertex = true;
|
tomwalters@398
|
220 m_iVertexType = VertexTypeLine;
|
tomwalters@398
|
221 //! \todo Make line width user-settable
|
tomwalters@398
|
222 cairo_set_line_width (m_cCr, 0.001f);
|
tomwalters@397
|
223 }
|
tomwalters@397
|
224
|
tomwalters@397
|
225 void GraphicsOutputDeviceCairo::gBeginQuadStrip() {
|
tomwalters@398
|
226 m_bIsFirstVertex = true;
|
tomwalters@398
|
227 m_iVertexType = VertexTypeQuad;
|
tomwalters@398
|
228 m_iPrevVertexCount = 0;
|
tomwalters@397
|
229 cairo_set_line_width (m_cCr, 0.001f);
|
tomwalters@397
|
230 }
|
tomwalters@397
|
231
|
tomwalters@397
|
232 void GraphicsOutputDeviceCairo::gColor3f(float r, float g, float b) {
|
tomwalters@397
|
233 if (m_bInvertColors) {
|
tomwalters@411
|
234 r = 1.0 - r;
|
tomwalters@411
|
235 g = 1.0 - g;
|
tomwalters@411
|
236 b = 1.0 - b;
|
tomwalters@398
|
237 }
|
tomwalters@397
|
238 cairo_set_source_rgb (m_cCr, r, g, b);
|
tomwalters@397
|
239 }
|
tomwalters@397
|
240
|
tomwalters@397
|
241 void GraphicsOutputDeviceCairo::gVertex3f(float x, float y, float z) {
|
tomwalters@398
|
242 switch(m_iVertexType) {
|
tomwalters@398
|
243 case VertexTypeLine:
|
tomwalters@398
|
244 if (m_bIsFirstVertex) {
|
tomwalters@398
|
245 m_bIsFirstVertex = false;
|
tomwalters@398
|
246 //pl_fmove(x, y);
|
tomwalters@411
|
247 cairo_move_to(m_cCr, x, 1.0 - y);
|
tomwalters@398
|
248 } else {
|
tomwalters@398
|
249 //pl_fcont(x, y);
|
tomwalters@411
|
250 cairo_line_to(m_cCr, x, 1.0 - y);
|
tomwalters@398
|
251 }
|
tomwalters@398
|
252 break;
|
tomwalters@398
|
253 case VertexTypeQuad:
|
tomwalters@411
|
254 /* Store vertices until we have four in a row.
|
tomwalters@398
|
255 * The order of vertices when processing quads is:
|
tomwalters@398
|
256 * 1-----3-----5
|
tomwalters@398
|
257 * | | |
|
tomwalters@398
|
258 * 0-----2-----4
|
tomwalters@398
|
259 */
|
tomwalters@398
|
260 if (m_iPrevVertexCount >= 3) {
|
tomwalters@398
|
261 // Plot this quad
|
tomwalters@411
|
262 //cairo_set_source_rgb(m_cCr, 0.2, 1 - m_aPrevY[0], m_aPrevX[0]);
|
tomwalters@411
|
263 cairo_rectangle (m_cCr, m_aPrevX[2],
|
tomwalters@411
|
264 1 - m_aPrevY[2], m_aPrevX[2] - m_aPrevX[0],
|
tomwalters@411
|
265 y - m_aPrevY[2]);
|
tomwalters@411
|
266 cairo_fill (m_cCr);
|
tomwalters@443
|
267
|
tomwalters@411
|
268 /*cairo_move_to(m_cCr, , );
|
tomwalters@411
|
269 cairo_line_to(m_cCr, , 1 - m_aPrevY[1]);
|
tomwalters@398
|
270 cairo_line_to(m_cCr, x, y);
|
tomwalters@411
|
271 cairo_line_to(m_cCr, m_aPrevX[2], 1 - m_aPrevY[2]);*/
|
tomwalters@397
|
272
|
tomwalters@398
|
273 // Last vertices of this quad are the first of the next
|
tomwalters@398
|
274 m_aPrevX[0] = m_aPrevX[2];
|
tomwalters@398
|
275 m_aPrevY[0] = m_aPrevY[2];
|
tomwalters@398
|
276 m_aPrevX[1] = x;
|
tomwalters@398
|
277 m_aPrevY[1] = y;
|
tomwalters@398
|
278 m_iPrevVertexCount = 2;
|
tomwalters@398
|
279 } else {
|
tomwalters@398
|
280 // Not at the fourth, keep storing
|
tomwalters@398
|
281 m_aPrevX[m_iPrevVertexCount] = x;
|
tomwalters@398
|
282 m_aPrevY[m_iPrevVertexCount] = y;
|
tomwalters@398
|
283 m_iPrevVertexCount++;
|
tomwalters@398
|
284 }
|
tomwalters@398
|
285 break;
|
tomwalters@398
|
286 default:
|
tomwalters@398
|
287 // Should not happen
|
tomwalters@398
|
288 AIM_ASSERT(0);
|
tomwalters@398
|
289 }
|
tomwalters@397
|
290 }
|
tomwalters@397
|
291
|
tomwalters@397
|
292 void GraphicsOutputDeviceCairo::gEnd() {
|
tomwalters@398
|
293 if(m_iVertexType==VertexTypeLine)
|
tomwalters@397
|
294 cairo_stroke (m_cCr);
|
tomwalters@397
|
295 else
|
tomwalters@397
|
296 cairo_fill (m_cCr);
|
tomwalters@398
|
297 m_iVertexType = VertexTypeNone;
|
tomwalters@397
|
298 }
|
tomwalters@397
|
299
|
tomwalters@397
|
300 void GraphicsOutputDeviceCairo::gText3f(float x,
|
tomwalters@397
|
301 float y,
|
tomwalters@397
|
302 float z,
|
tomwalters@397
|
303 const char *sStr,
|
tomwalters@397
|
304 bool bRotated) {
|
tomwalters@402
|
305 //cairo_text_extents_t te;
|
tomwalters@398
|
306 if (bRotated) {
|
tomwalters@398
|
307 cairo_rotate(m_cCr, M_PI/2);
|
tomwalters@411
|
308 //cairo_move_to(m_cCr, x ,1-y);
|
tomwalters@398
|
309 cairo_show_text(m_cCr, sStr);
|
tomwalters@398
|
310 //cairo_identity_matrix(m_cCr);
|
tomwalters@398
|
311 cairo_rotate(m_cCr, -M_PI/2);
|
tomwalters@398
|
312 } else {
|
tomwalters@398
|
313 cairo_move_to(m_cCr, x ,1-y);
|
tomwalters@398
|
314 cairo_show_text(m_cCr, sStr);
|
tomwalters@398
|
315 }
|
tomwalters@397
|
316 }
|
tomwalters@397
|
317
|
tomwalters@397
|
318 void GraphicsOutputDeviceCairo::gRelease() {
|
tomwalters@398
|
319 AIM_ASSERT(m_iPlotHandle>0);
|
tomwalters@398
|
320 CloseFile();
|
tomwalters@398
|
321 // Finished this one, up to the next!
|
tomwalters@398
|
322 m_iFileNumber++;
|
tomwalters@397
|
323 }
|
tom@399
|
324 } // namespace aimc
|