annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc @ 127:acb14b11225f

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