annotate trunk/src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc @ 411:a908972d234e

- Added support for movies!
author tomwalters
date Thu, 21 Oct 2010 01:46:39 +0000
parents 7af493eb1563
children 9c26f6532ea3
rev   line source
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@397 34
tomwalters@411 35 #include "cairo-quartz.h"
tomwalters@411 36
tom@399 37 #include "Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.h"
tom@399 38
tom@399 39 namespace aimc {
tomwalters@397 40
tomwalters@397 41 GraphicsOutputDeviceCairo::GraphicsOutputDeviceCairo(Parameters *pParam)
tom@399 42 : GraphicsOutputDevice(pParam) {
tomwalters@398 43 m_bOutputFile = false;
tomwalters@398 44 m_iFileNumber = 0;
tomwalters@398 45 m_iVertexType = VertexTypeNone;
tomwalters@398 46 m_bUseMemoryBuffer=false;
tomwalters@411 47 parameters_->DefaultString("output.img.format", ".png");
tomwalters@397 48 }
tomwalters@397 49
tomwalters@411 50 bool GraphicsOutputDeviceCairo::Initialize(string directory) {
tomwalters@411 51 directory_ = directory;
tomwalters@411 52 InititalzeInternal();
tomwalters@397 53
tomwalters@398 54 /* Try to open an image to see if everything is allright. We want to avoid
tomwalters@398 55 * errors in the main Process()ing loop. */
tomwalters@411 56 /*if (!OpenFile(0)) {
tomwalters@398 57 //! \todo Better error message that is more specific about the cause.
tom@400 58 LOG_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."),
tomwalters@411 59 directory_.c_str(), parameters_->DefaultString("output.img.format", "png"));
tomwalters@398 60 return false;
tomwalters@398 61 }
tomwalters@411 62 CloseFile();*/
tomwalters@397 63
tomwalters@398 64 return true;
tomwalters@397 65 }
tomwalters@397 66
tomwalters@411 67 /*bool GraphicsOutputDeviceCairo::Initialize() {
tomwalters@397 68 Init();
tomwalters@397 69 m_bUseMemoryBuffer = true;
tomwalters@397 70 return(true);
tomwalters@411 71 }*/
tomwalters@397 72
tomwalters@411 73 void GraphicsOutputDeviceCairo::InititalzeInternal() {
tomwalters@411 74 AIM_ASSERT(parameters_);
tomwalters@397 75
tomwalters@411 76 parameters_->DefaultString("output.img.color.background", "black");
tomwalters@397 77
tomwalters@411 78 m_bInvertColors = parameters_->DefaultBool("output.img.color.invert", "false");
tomwalters@411 79
tomwalters@411 80 // Output size.
tomwalters@411 81 m_iWidth = parameters_->DefaultInt("output.img.width", 800);
tomwalters@411 82 m_iHeight = parameters_->DefaultInt("output.img.height", 600);
tomwalters@411 83
tomwalters@411 84 // Cairo's RGB24 format has 32-bit pixels with the upper 8 bits unused.
tomwalters@411 85 // This is not the same as the plotutils PNG format. This information is transferred by the
tomwalters@411 86 // function GetPixelFormat. The pixel format is dealt with by the reciever.
tomwalters@411 87 m_cSurface = cairo_quartz_surface_create(CAIRO_FORMAT_RGB24,
tomwalters@411 88 m_iWidth,
tomwalters@411 89 m_iHeight);
tomwalters@411 90 m_cCr = cairo_create(m_cSurface);
tomwalters@411 91 cairo_scale(m_cCr, (float)m_iWidth, (float)m_iHeight);
tomwalters@411 92 // Now setup things for this plotter.
tomwalters@411 93 cairo_select_font_face(m_cCr,
tomwalters@411 94 parameters_->DefaultString("output.img.fontname",
tomwalters@411 95 "HersheySans"),
tomwalters@411 96 CAIRO_FONT_SLANT_NORMAL,
tomwalters@411 97 CAIRO_FONT_WEIGHT_BOLD);
tomwalters@411 98 cairo_set_font_size (m_cCr, 0.02);
tomwalters@397 99 }
tomwalters@397 100
tomwalters@397 101 unsigned char* GraphicsOutputDeviceCairo::GetBuffer() {
tomwalters@397 102 if(m_bUseMemoryBuffer)
tomwalters@397 103 return (cairo_image_surface_get_data (m_cSurface));
tomwalters@397 104 else
tomwalters@397 105 return NULL;
tomwalters@397 106 }
tomwalters@397 107
tomwalters@397 108 bool GraphicsOutputDeviceCairo::OpenFile(unsigned int index) {
tomwalters@411 109 const char *strPlottype = parameters_->GetString("output.img.format");
tomwalters@397 110 if (!m_bUseMemoryBuffer) {
tomwalters@397 111 struct stat fileinfo;
tomwalters@397 112 // Get filename without trailing slash
tomwalters@411 113 char filename[PATH_MAX];
tomwalters@411 114 strncpy(filename, directory_.c_str(), sizeof(filename)/sizeof(filename[0]));
tomwalters@397 115 #ifdef _WINDOWS
tomwalters@411 116 if (filename[strlen(filename)-1]=='\\') {
tomwalters@411 117 filename[strlen(filename)-1]='\0';
tomwalters@397 118 }
tomwalters@397 119 #else
tomwalters@411 120 if (filename[strlen(filename)-1]=='/') {
tomwalters@411 121 filename[strlen(filename)-1]='\0';
tomwalters@397 122 }
tomwalters@397 123 #endif
tomwalters@397 124 // Enumerate files it m_sDir is a directory.
tomwalters@411 125 if (stat(filename, &fileinfo) == 0 && (fileinfo.st_mode & S_IFDIR)) {
tomwalters@397 126 // We have a directory: enumerate with index
tomwalters@411 127 snprintf(filename, sizeof(filename) / sizeof(filename[0]),
tomwalters@411 128 "%s%06d.%s",
tomwalters@411 129 directory_.c_str(),
tomwalters@397 130 index,
tomwalters@397 131 strPlottype);
tomwalters@397 132 // If type is 'auto', fallback to 'png'
tomwalters@397 133 if (strcmp(strPlottype, "auto")==0)
tomwalters@397 134 strPlottype = "png";
tomwalters@397 135 } else {
tomwalters@397 136 // We have a (probably non-existant) file. Auto-detect type by extension if requested
tomwalters@411 137 strncpy(filename,
tomwalters@411 138 directory_.c_str(),
tomwalters@411 139 sizeof(filename)/sizeof(filename[0]));
tomwalters@411 140 char *pDot = strrchr(filename, '.');
tomwalters@397 141 if (!pDot) {
tom@400 142 LOG_ERROR(_T("Please supply extension on filename when using 'auto' format: '%s'"),
tomwalters@411 143 filename);
tomwalters@397 144 return false;
tomwalters@397 145 }
tomwalters@397 146 strPlottype = &pDot[1];
tomwalters@397 147 }
tomwalters@411 148 image_filename_ = filename;
tomwalters@397 149 m_bOutputFile= true; //! \todo Should check that it's possible to write to the file
tomwalters@397 150 }
tomwalters@411 151
tomwalters@398 152 return true;
tomwalters@397 153 }
tomwalters@397 154
tomwalters@397 155 void GraphicsOutputDeviceCairo::CloseFile() {
tomwalters@398 156 // And the output file
tomwalters@398 157 if (m_bOutputFile) {
tomwalters@411 158 cairo_surface_write_to_png(m_cSurface, image_filename_.c_str());
tomwalters@398 159 m_bOutputFile = false;
tomwalters@398 160 }
tomwalters@411 161 cairo_set_source_rgb (m_cCr, 0.0, 0.0, 0.0);
tomwalters@411 162 cairo_paint (m_cCr);
tomwalters@411 163 //cairo_destroy(m_cCr);
tomwalters@411 164 //cairo_surface_destroy(m_cSurface);
tomwalters@397 165 }
tomwalters@397 166
tomwalters@397 167 GraphicsOutputDeviceCairo::~GraphicsOutputDeviceCairo() {
tomwalters@398 168 AIM_ASSERT(!m_iPlotHandle);
tomwalters@398 169 CloseFile();
tomwalters@397 170 }
tomwalters@397 171
tomwalters@397 172 void GraphicsOutputDeviceCairo::gGrab() {
tomwalters@397 173 // Open file.
tomwalters@398 174 if (!OpenFile(m_iFileNumber)) {
tomwalters@398 175 return;
tomwalters@397 176 }
tomwalters@398 177 // Setup plotting area.
tomwalters@398 178 cairo_set_line_width (m_cCr, 0.001f);
tomwalters@398 179 gColor3f (0.0f, 0.0f, 0.0f);
tomwalters@397 180 cairo_paint (m_cCr);
tomwalters@397 181 gColor3f(1.0f, 1.0f, 0.0f);
tomwalters@397 182 }
tomwalters@397 183
tomwalters@397 184 int GraphicsOutputDeviceCairo::GetPixelFormat() {
tomwalters@397 185 return AIM_PIX_FMT_RGB24_32;
tomwalters@397 186 }
tomwalters@397 187
tomwalters@397 188 void GraphicsOutputDeviceCairo::gBeginLineStrip() {
tomwalters@398 189 m_bIsFirstVertex = true;
tomwalters@398 190 m_iVertexType = VertexTypeLine;
tomwalters@398 191 //! \todo Make line width user-settable
tomwalters@398 192 cairo_set_line_width (m_cCr, 0.001f);
tomwalters@397 193 }
tomwalters@397 194
tomwalters@397 195 void GraphicsOutputDeviceCairo::gBeginQuadStrip() {
tomwalters@398 196 m_bIsFirstVertex = true;
tomwalters@398 197 m_iVertexType = VertexTypeQuad;
tomwalters@398 198 m_iPrevVertexCount = 0;
tomwalters@397 199 cairo_set_line_width (m_cCr, 0.001f);
tomwalters@397 200 }
tomwalters@397 201
tomwalters@397 202 void GraphicsOutputDeviceCairo::gColor3f(float r, float g, float b) {
tomwalters@397 203 if (m_bInvertColors) {
tomwalters@411 204 r = 1.0 - r;
tomwalters@411 205 g = 1.0 - g;
tomwalters@411 206 b = 1.0 - b;
tomwalters@398 207 }
tomwalters@397 208 cairo_set_source_rgb (m_cCr, r, g, b);
tomwalters@397 209 }
tomwalters@397 210
tomwalters@397 211 void GraphicsOutputDeviceCairo::gVertex3f(float x, float y, float z) {
tomwalters@398 212 switch(m_iVertexType) {
tomwalters@398 213 case VertexTypeLine:
tomwalters@398 214 if (m_bIsFirstVertex) {
tomwalters@398 215 m_bIsFirstVertex = false;
tomwalters@398 216 //pl_fmove(x, y);
tomwalters@411 217 cairo_move_to(m_cCr, x, 1.0 - y);
tomwalters@398 218 } else {
tomwalters@398 219 //pl_fcont(x, y);
tomwalters@411 220 cairo_line_to(m_cCr, x, 1.0 - y);
tomwalters@398 221 }
tomwalters@398 222 break;
tomwalters@398 223 case VertexTypeQuad:
tomwalters@411 224 /* Store vertices until we have four in a row.
tomwalters@398 225 * The order of vertices when processing quads is:
tomwalters@398 226 * 1-----3-----5
tomwalters@398 227 * | | |
tomwalters@398 228 * 0-----2-----4
tomwalters@398 229 */
tomwalters@398 230 if (m_iPrevVertexCount >= 3) {
tomwalters@398 231 // Plot this quad
tomwalters@411 232 //cairo_set_source_rgb(m_cCr, 0.2, 1 - m_aPrevY[0], m_aPrevX[0]);
tomwalters@411 233 cairo_rectangle (m_cCr, m_aPrevX[2],
tomwalters@411 234 1 - m_aPrevY[2], m_aPrevX[2] - m_aPrevX[0],
tomwalters@411 235 y - m_aPrevY[2]);
tomwalters@411 236 cairo_fill (m_cCr);
tomwalters@411 237
tomwalters@411 238 /*cairo_move_to(m_cCr, , );
tomwalters@411 239 cairo_line_to(m_cCr, , 1 - m_aPrevY[1]);
tomwalters@398 240 cairo_line_to(m_cCr, x, y);
tomwalters@411 241 cairo_line_to(m_cCr, m_aPrevX[2], 1 - m_aPrevY[2]);*/
tomwalters@411 242
tomwalters@397 243
tomwalters@398 244 // Last vertices of this quad are the first of the next
tomwalters@398 245 m_aPrevX[0] = m_aPrevX[2];
tomwalters@398 246 m_aPrevY[0] = m_aPrevY[2];
tomwalters@398 247 m_aPrevX[1] = x;
tomwalters@398 248 m_aPrevY[1] = y;
tomwalters@398 249 m_iPrevVertexCount = 2;
tomwalters@398 250 } else {
tomwalters@398 251 // Not at the fourth, keep storing
tomwalters@398 252 m_aPrevX[m_iPrevVertexCount] = x;
tomwalters@398 253 m_aPrevY[m_iPrevVertexCount] = y;
tomwalters@398 254 m_iPrevVertexCount++;
tomwalters@398 255 }
tomwalters@398 256 break;
tomwalters@398 257 default:
tomwalters@398 258 // Should not happen
tomwalters@398 259 AIM_ASSERT(0);
tomwalters@398 260 }
tomwalters@397 261 }
tomwalters@397 262
tomwalters@397 263 void GraphicsOutputDeviceCairo::gEnd() {
tomwalters@398 264 if(m_iVertexType==VertexTypeLine)
tomwalters@397 265 cairo_stroke (m_cCr);
tomwalters@397 266 else
tomwalters@397 267 cairo_fill (m_cCr);
tomwalters@398 268 m_iVertexType = VertexTypeNone;
tomwalters@397 269 }
tomwalters@397 270
tomwalters@397 271 void GraphicsOutputDeviceCairo::gText3f(float x,
tomwalters@397 272 float y,
tomwalters@397 273 float z,
tomwalters@397 274 const char *sStr,
tomwalters@397 275 bool bRotated) {
tomwalters@402 276 //cairo_text_extents_t te;
tomwalters@398 277 if (bRotated) {
tomwalters@398 278 cairo_rotate(m_cCr, M_PI/2);
tomwalters@411 279 //cairo_move_to(m_cCr, x ,1-y);
tomwalters@398 280 cairo_show_text(m_cCr, sStr);
tomwalters@398 281 //cairo_identity_matrix(m_cCr);
tomwalters@398 282 cairo_rotate(m_cCr, -M_PI/2);
tomwalters@398 283 } else {
tomwalters@398 284 cairo_move_to(m_cCr, x ,1-y);
tomwalters@398 285 cairo_show_text(m_cCr, sStr);
tomwalters@398 286 }
tomwalters@397 287 }
tomwalters@397 288
tomwalters@397 289 void GraphicsOutputDeviceCairo::gRelease() {
tomwalters@398 290 AIM_ASSERT(m_iPlotHandle>0);
tomwalters@398 291 CloseFile();
tomwalters@398 292 // Finished this one, up to the next!
tomwalters@398 293 m_iFileNumber++;
tomwalters@397 294 }
tom@399 295 } // namespace aimc