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