annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDevicePlotutils.cc @ 263:07dc1f7047f5

Debug output for audio_example and added a write function for aimc_data
author hamel.phil
date Thu, 06 Jan 2011 20:11:41 +0000
parents 82e0dc3dfd16
children
rev   line source
tomwalters@116 1 // Copyright 2006, 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 plotutils
tomwalters@116 21 *
tomwalters@116 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@116 23 * \date created 2006/10/13
tomwalters@116 24 * \version \$Id: GraphicsOutputDevicePlotutils.cpp 493 2007-11-27 10:59:20Z tom $
tomwalters@116 25 */
tomwalters@116 26 #include "Support/Common.h"
tomwalters@116 27
tomwalters@116 28 #include <sys/types.h>
tomwalters@116 29 #include <sys/stat.h>
tomwalters@116 30 #include <string.h>
tomwalters@116 31 #include <stdio.h>
tomwalters@116 32 #include <math.h>
tomwalters@116 33
tomwalters@116 34 #include "Support/util.h"
tomwalters@116 35 #include "Modules/Output/Graphics/Devices/GraphicsOutputDevicePlotutils.h"
tomwalters@116 36
tomwalters@116 37 GraphicsOutputDevicePlotutils::GraphicsOutputDevicePlotutils(Parameters *pParam)
tomwalters@116 38 : GraphicsOutputDevice(pParam) {
tomwalters@228 39 m_iPlotHandle = 0;
tomwalters@228 40 m_pOutputFile = NULL;
tomwalters@228 41 m_iFileNumber = 0;
tomwalters@228 42 m_iVertexType = VertexTypeNone;
tomwalters@228 43 m_bUseMemoryBuffer = false;
tomwalters@228 44 m_pMemoryBuffer = NULL;
tomwalters@228 45 m_uWidth=0;
tomwalters@228 46 m_uHeight=0;
tomwalters@116 47 }
tomwalters@116 48
tomwalters@116 49 bool GraphicsOutputDevicePlotutils::Initialize(const char *sDir) {
tomwalters@116 50 Init();
tomwalters@116 51
tomwalters@228 52 //! \todo Output to file if sDir is a file, to directory with multiple
tomwalters@116 53 //! images if it's a directory.
tomwalters@228 54 strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0]));
tomwalters@116 55
tomwalters@228 56 /* Try to open an image to see if everything is allright. We want to avoid
tomwalters@228 57 * errors in the main Process()ing loop. */
tomwalters@228 58 if (!OpenFile(0)) {
tomwalters@228 59 //! \todo Better error message that is more specific about the cause.
tomwalters@228 60 AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."),
tomwalters@228 61 m_sDir, m_pParam->GetString("output.img.format") );
tomwalters@228 62 return false;
tomwalters@228 63 }
tomwalters@228 64 CloseFile();
tomwalters@228 65 return true;
tomwalters@116 66 }
tomwalters@116 67
tomwalters@116 68 void GraphicsOutputDevicePlotutils::Init() {
tomwalters@116 69 AIM_ASSERT(m_pParam);
tomwalters@228 70 /*
tomwalters@228 71 * Set parameters
tomwalters@228 72 */
tomwalters@228 73 pl_parampl("BG_COLOR", (void*)m_pParam->GetString("output.img.color.background"));
tomwalters@228 74 // Handle GIFs as other output formats, don't merge frames into single GIF.
tomwalters@228 75 pl_parampl("GIF_ANIMATION", (void*)"no");
tomwalters@228 76 pl_parampl("INTERLACE", (void*)"no");
tomwalters@116 77
tomwalters@228 78 m_bInvertColors = m_pParam->GetBool("output.img.color.invert");
tomwalters@116 79
tomwalters@228 80 // Output size!
tomwalters@228 81 m_uWidth = m_pParam->GetUInt("output.img.width");
tomwalters@228 82 m_uHeight = m_pParam->GetUInt("output.img.height");
tomwalters@228 83 char strSize[100];
tomwalters@228 84 snprintf(strSize, sizeof(strSize)/sizeof(strSize[0]), "%ux%u", m_uWidth, m_uHeight);
tomwalters@228 85 pl_parampl("BITMAPSIZE", (void*)strSize);
tomwalters@116 86 }
tomwalters@116 87
tomwalters@116 88 bool GraphicsOutputDevicePlotutils::Initialize() {
tomwalters@116 89 Init();
tomwalters@116 90 m_bUseMemoryBuffer = true;
tomwalters@116 91 return(true);
tomwalters@116 92 }
tomwalters@116 93
tomwalters@116 94 bool GraphicsOutputDevicePlotutils::OpenFile(unsigned int index) {
tomwalters@116 95 const char *strPlottype = m_pParam->GetString("output.img.format");
tomwalters@116 96 if (!m_bUseMemoryBuffer) {
tomwalters@116 97 char sFilename[PATH_MAX];
tomwalters@116 98 struct stat fileinfo;
tomwalters@116 99
tomwalters@116 100 // Get filename without trailing slash
tomwalters@116 101 strncpy(sFilename, m_sDir, sizeof(sFilename)/sizeof(sFilename[0]));
tomwalters@116 102 #ifdef _WINDOWS
tomwalters@116 103 if (sFilename[strlen(sFilename)-1]=='\\') {
tomwalters@116 104 sFilename[strlen(sFilename)-1]='\0';
tomwalters@116 105 }
tomwalters@116 106 #else
tomwalters@116 107 if (sFilename[strlen(sFilename)-1]=='/') {
tomwalters@116 108 sFilename[strlen(sFilename)-1]='\0';
tomwalters@116 109 }
tomwalters@116 110 #endif
tomwalters@116 111 // Enumerate files it m_sDir is a directory.
tomwalters@116 112 if ( stat(sFilename, &fileinfo)==0 && (fileinfo.st_mode & S_IFDIR) ) {
tomwalters@116 113 // We have a directory: enumerate with index
tomwalters@116 114 snprintf(sFilename, sizeof(sFilename)/sizeof(sFilename[0]),"%s%06d.%s", m_sDir, index, strPlottype);
tomwalters@116 115 // If type is 'auto', fallback to 'png'
tomwalters@116 116 if (strcmp(strPlottype, "auto")==0)
tomwalters@116 117 strPlottype = "png";
tomwalters@116 118 } else {
tomwalters@116 119 // We have a (probabely non-existant) file. Auto-detect type by extension if requested
tomwalters@116 120 strncpy(sFilename, m_sDir, sizeof(sFilename)/sizeof(sFilename[0]));
tomwalters@116 121 char *pDot = strrchr(sFilename, '.');
tomwalters@116 122 if (!pDot) {
tomwalters@116 123 AIM_ERROR(_T("Please supply extension on filename when using 'auto' format: '%s'"), sFilename);
tomwalters@116 124 return false;
tomwalters@116 125 }
tomwalters@116 126 strPlottype = &pDot[1];
tomwalters@116 127 }
tomwalters@116 128 if ((m_pOutputFile = fopen(sFilename, "wb")) == NULL) {
tomwalters@116 129 return false;
tomwalters@116 130 }
tomwalters@116 131 } else {
tomwalters@116 132 #if !defined(_WINDOWS) && !defined(_MACOSX)
tomwalters@116 133 if ((m_pOutputFile=open_memstream(&m_pMemoryBuffer, &m_sMemoryBufferSize))==NULL)
tomwalters@116 134 #endif
tomwalters@116 135 return false;
tomwalters@116 136 strPlottype="pnm"; // Force pnm format or this doesn't work
tomwalters@116 137 }
tomwalters@116 138
tomwalters@228 139 /*
tomwalters@228 140 * Create a plotter
tomwalters@228 141 *
tomwalters@228 142 * Plotutils knows the following types for file output:
tomwalters@228 143 * pnm gif ai ps gif pcl hpgl tek meta
tomwalters@228 144 */
tomwalters@228 145 if ((m_iPlotHandle = pl_newpl(strPlottype,
tomwalters@116 146 NULL,
tomwalters@116 147 m_pOutputFile,
tomwalters@116 148 stderr)) < 0 ) {
tomwalters@228 149 return false;
tomwalters@228 150 }
tomwalters@228 151 pl_selectpl(m_iPlotHandle);
tomwalters@116 152
tomwalters@228 153 if ( pl_openpl() < 0) {
tomwalters@228 154 return false;
tomwalters@228 155 }
tomwalters@116 156
tomwalters@228 157 // Now setup things for this plotter
tomwalters@228 158 pl_fontname(m_pParam->GetString("output.img.fontname"));
tomwalters@228 159 //! \todo Make fontsize work in Plotutils, currently disabled
tomwalters@228 160 //pl_ffontsize(m_pParam->GetUInt("output.fontsize"));
tomwalters@116 161
tomwalters@228 162 return true;
tomwalters@116 163 }
tomwalters@116 164
tomwalters@116 165 char* GraphicsOutputDevicePlotutils::GetBuffer() {
tomwalters@116 166 if(m_bUseMemoryBuffer && (m_pMemoryBuffer!=NULL))
tomwalters@116 167 return (&m_pMemoryBuffer[m_sMemoryBufferSize-(m_uWidth*m_uHeight*3)]);
tomwalters@116 168 else
tomwalters@116 169 return NULL;
tomwalters@116 170 }
tomwalters@116 171
tomwalters@116 172 void GraphicsOutputDevicePlotutils::CloseFile() {
tomwalters@228 173 // Plotting library
tomwalters@228 174 if (m_iPlotHandle>0) {
tomwalters@228 175 pl_closepl();
tomwalters@116 176
tomwalters@228 177 pl_selectpl(0);
tomwalters@228 178 pl_deletepl(m_iPlotHandle);
tomwalters@228 179 m_iPlotHandle = 0;
tomwalters@228 180 }
tomwalters@116 181
tomwalters@228 182 // And the output file
tomwalters@228 183 if (m_pOutputFile) {
tomwalters@228 184 fclose(m_pOutputFile);
tomwalters@228 185 m_pOutputFile = NULL;
tomwalters@228 186 }
tomwalters@116 187 }
tomwalters@116 188
tomwalters@116 189 GraphicsOutputDevicePlotutils::~GraphicsOutputDevicePlotutils() {
tomwalters@228 190 // Output file should be closed by gRelease()
tomwalters@228 191 AIM_ASSERT(!m_pOutputFile);
tomwalters@228 192 AIM_ASSERT(!m_iPlotHandle);
tomwalters@228 193 CloseFile();
tomwalters@116 194 }
tomwalters@116 195
tomwalters@116 196 PixelFormat GraphicsOutputDevicePlotutils::GetPixelFormat() {
tomwalters@116 197 return AIM_PIX_FMT_RGB24_24;
tomwalters@116 198 }
tomwalters@116 199
tomwalters@116 200 void GraphicsOutputDevicePlotutils::gGrab() {
tomwalters@228 201 // Open file
tomwalters@228 202 if (!OpenFile(m_iFileNumber)) {
tomwalters@228 203 return;
tomwalters@116 204 }
tomwalters@228 205 // Setup plotting area
tomwalters@228 206 pl_fspace(0.0, 0.0, 1.0, 1.0);
tomwalters@228 207 pl_flinewidth(0.0001);
tomwalters@228 208 pl_pencolorname("darkblue");
tomwalters@228 209 pl_erase();
tomwalters@116 210 }
tomwalters@116 211
tomwalters@116 212 void GraphicsOutputDevicePlotutils::gBeginLineStrip() {
tomwalters@228 213 m_bIsFirstVertex = true;
tomwalters@228 214 m_iVertexType = VertexTypeLine;
tomwalters@228 215 pl_filltype(0);
tomwalters@116 216 }
tomwalters@116 217
tomwalters@116 218 void GraphicsOutputDevicePlotutils::gBeginQuadStrip() {
tomwalters@228 219 m_bIsFirstVertex = true;
tomwalters@228 220 m_iVertexType = VertexTypeQuad;
tomwalters@228 221 m_iPrevVertexCount = 0;
tomwalters@228 222 pl_filltype(1);
tomwalters@116 223 }
tomwalters@116 224
tomwalters@116 225 void GraphicsOutputDevicePlotutils::gColor3f(float r, float g, float b) {
tomwalters@228 226 if (m_bInvertColors) {
tomwalters@228 227 r = 1-r;
tomwalters@228 228 g = 1-g;
tomwalters@228 229 b = 1-b;
tomwalters@228 230 }
tomwalters@228 231 int ir = (int)(r*0xffff);
tomwalters@228 232 int ig = (int)(g*0xffff);
tomwalters@228 233 int ib = (int)(b*0xffff);
tomwalters@228 234 ir = MIN(0xffff, MAX(0,ir));
tomwalters@228 235 ig = MIN(0xffff, MAX(0,ig));
tomwalters@228 236 ib = MIN(0xffff, MAX(0,ib));
tomwalters@228 237 pl_color(ir, ig, ib);
tomwalters@116 238 }
tomwalters@116 239
tomwalters@116 240 void GraphicsOutputDevicePlotutils::gVertex3f(float x, float y, float z) {
tomwalters@228 241 switch(m_iVertexType) {
tomwalters@228 242 case VertexTypeLine:
tomwalters@228 243 if (m_bIsFirstVertex) {
tomwalters@228 244 m_bIsFirstVertex = false;
tomwalters@228 245 pl_fmove(x, y);
tomwalters@228 246 } else {
tomwalters@228 247 pl_fcont(x, y);
tomwalters@228 248 }
tomwalters@228 249 break;
tomwalters@228 250 case VertexTypeQuad:
tomwalters@228 251 /* Store vertices until we got four in a row.
tomwalters@228 252 * The order of vertices when processing quads is:
tomwalters@228 253 * 1-----3-----5
tomwalters@228 254 * | | |
tomwalters@228 255 * 0-----2-----4
tomwalters@228 256 */
tomwalters@228 257 if (m_iPrevVertexCount>=3) {
tomwalters@228 258 // Plot this quad
tomwalters@228 259 pl_fmove(m_aPrevX[0],m_aPrevY[0]);
tomwalters@228 260 pl_fcont(m_aPrevX[1],m_aPrevY[1]);
tomwalters@228 261 pl_fcont(x,y);
tomwalters@228 262 pl_fcont(m_aPrevX[2],m_aPrevY[2]);
tomwalters@228 263 pl_endpath();
tomwalters@228 264 // Last vertices of this quad are the first of the next
tomwalters@228 265 m_aPrevX[0] = m_aPrevX[2];
tomwalters@228 266 m_aPrevY[0] = m_aPrevY[2];
tomwalters@228 267 m_aPrevX[1] = x;
tomwalters@228 268 m_aPrevY[1] = y;
tomwalters@228 269 m_iPrevVertexCount = 2;
tomwalters@228 270 } else {
tomwalters@228 271 // Not at the fourth, keep storing
tomwalters@228 272 m_aPrevX[m_iPrevVertexCount] = x;
tomwalters@228 273 m_aPrevY[m_iPrevVertexCount] = y;
tomwalters@228 274 m_iPrevVertexCount++;
tomwalters@228 275 }
tomwalters@228 276 break;
tomwalters@228 277 default:
tomwalters@228 278 // Should not happen
tomwalters@228 279 AIM_ASSERT(0);
tomwalters@228 280 }
tomwalters@116 281 }
tomwalters@116 282
tomwalters@116 283 void GraphicsOutputDevicePlotutils::gEnd() {
tomwalters@228 284 pl_endpath();
tomwalters@228 285 m_iVertexType = VertexTypeNone;
tomwalters@116 286 }
tomwalters@116 287
tomwalters@116 288 void GraphicsOutputDevicePlotutils::gText3f(float x,
tomwalters@116 289 float y,
tomwalters@116 290 float z,
tomwalters@116 291 const char *sStr,
tomwalters@116 292 bool bRotated) {
tomwalters@228 293 if (bRotated) {
tomwalters@228 294 pl_textangle(90);
tomwalters@228 295 pl_fmove(x, y);
tomwalters@228 296 pl_alabel('l', 't', sStr);
tomwalters@228 297 } else {
tomwalters@228 298 pl_textangle(0);
tomwalters@228 299 pl_fmove(x, y);
tomwalters@228 300 pl_alabel('l', 'b', sStr);
tomwalters@228 301 }
tomwalters@116 302 }
tomwalters@116 303
tomwalters@116 304 void GraphicsOutputDevicePlotutils::gRelease() {
tomwalters@228 305 AIM_ASSERT(m_pOutputFile);
tomwalters@228 306 AIM_ASSERT(m_iPlotHandle>0);
tomwalters@228 307 CloseFile();
tomwalters@228 308 m_iFileNumber++;
tomwalters@116 309 }