annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc @ 146:fea6ecdb16b7

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