annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc @ 237:af02b6addf7a

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