tomwalters@397: // Copyright 2007, Thomas Walters tomwalters@397: // tomwalters@397: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@397: // http://www.acousticscale.org/AIMC tomwalters@397: // tomwalters@397: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@397: // you may not use this file except in compliance with the License. tomwalters@397: // You may obtain a copy of the License at tomwalters@397: // tomwalters@397: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@397: // tomwalters@397: // Unless required by applicable law or agreed to in writing, software tomwalters@397: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@397: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@397: // See the License for the specific language governing permissions and tomwalters@397: // limitations under the License. tomwalters@397: tomwalters@397: /*! tomwalters@397: * \file tomwalters@397: * \brief Output device for output to a graphics file using cairo (LGPL) tomwalters@397: * tomwalters@397: * \author Tom Walters and Willem van Engen tomwalters@397: * \date created 2007/09/17 tomwalters@397: * \version \$Header: $ tomwalters@397: */ tomwalters@397: tomwalters@397: #include "Support/Common.h" tomwalters@397: tomwalters@397: #include tomwalters@397: #include tomwalters@397: #include tomwalters@397: #include tomwalters@397: #include tomwalters@397: tomwalters@397: #include "Support/util.h" tomwalters@397: #include "Output/GraphicsOutputDeviceCairo.h" tomwalters@397: tomwalters@397: GraphicsOutputDeviceCairo::GraphicsOutputDeviceCairo(Parameters *pParam) tomwalters@397: : GraphicsOutputDevice(pParam) { tomwalters@397: m_bOutputFile = false; tomwalters@397: m_iFileNumber = 0; tomwalters@397: m_iVertexType = VertexTypeNone; tomwalters@397: m_bUseMemoryBuffer=false; tomwalters@397: } tomwalters@397: tomwalters@397: bool GraphicsOutputDeviceCairo::Initialize(const char *sDir) { tomwalters@397: Init(); tomwalters@397: tomwalters@397: //! \todo Output to file if sDir is a file, to directory with tomwalters@397: //! multiple images if it's a directory. tomwalters@397: strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0])); tomwalters@397: tomwalters@397: /* Try to open an image to see if everything is allright. We want to avoid tomwalters@397: * errors in the main Process()ing loop. */ tomwalters@397: if ( !OpenFile(0) ) { tomwalters@397: //! \todo Better error message that is more specific about the cause. tomwalters@397: AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."), tomwalters@397: m_sDir, m_pParam->GetString("output.img.format") ); tomwalters@397: return false; tomwalters@397: } tomwalters@397: CloseFile(); tomwalters@397: tomwalters@397: return true; tomwalters@397: } tomwalters@397: tomwalters@397: bool GraphicsOutputDeviceCairo::Initialize() { tomwalters@397: Init(); tomwalters@397: m_bUseMemoryBuffer = true; tomwalters@397: return(true); tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::Init() { tomwalters@397: AIM_ASSERT(m_pParam); tomwalters@397: /* tomwalters@397: * Set parameters tomwalters@397: */ tomwalters@397: m_pParam->GetString("output.img.color.background"); tomwalters@397: tomwalters@397: m_bInvertColors = m_pParam->GetBool("output.img.color.invert"); tomwalters@397: tomwalters@397: // Output size! tomwalters@397: m_iWidth = m_pParam->GetUInt("output.img.width"); tomwalters@397: m_iHeight = m_pParam->GetUInt("output.img.height"); tomwalters@397: } tomwalters@397: tomwalters@397: unsigned char* GraphicsOutputDeviceCairo::GetBuffer() { tomwalters@397: if(m_bUseMemoryBuffer) tomwalters@397: return (cairo_image_surface_get_data (m_cSurface)); tomwalters@397: else tomwalters@397: return NULL; tomwalters@397: } tomwalters@397: tomwalters@397: bool GraphicsOutputDeviceCairo::OpenFile(unsigned int index) { tomwalters@397: const char *strPlottype = m_pParam->GetString("output.img.format"); tomwalters@397: if (!m_bUseMemoryBuffer) { tomwalters@397: struct stat fileinfo; tomwalters@397: // Get filename without trailing slash tomwalters@397: strncpy(m_sFilename, m_sDir, sizeof(m_sFilename)/sizeof(m_sFilename[0])); tomwalters@397: #ifdef _WINDOWS tomwalters@397: if (m_sFilename[strlen(m_sFilename)-1]=='\\') { tomwalters@397: m_sFilename[strlen(m_sFilename)-1]='\0'; tomwalters@397: } tomwalters@397: #else tomwalters@397: if (m_sFilename[strlen(m_sFilename)-1]=='/') { tomwalters@397: m_sFilename[strlen(m_sFilename)-1]='\0'; tomwalters@397: } tomwalters@397: #endif tomwalters@397: // Enumerate files it m_sDir is a directory. tomwalters@397: if (stat(m_sFilename, &fileinfo) == 0 && (fileinfo.st_mode & S_IFDIR)) { tomwalters@397: // We have a directory: enumerate with index tomwalters@397: snprintf(m_sFilename, sizeof(m_sFilename)/sizeof(m_sFilename[0]),"%s%06d.%s", tomwalters@397: m_sDir, tomwalters@397: index, tomwalters@397: strPlottype); tomwalters@397: // If type is 'auto', fallback to 'png' tomwalters@397: if (strcmp(strPlottype, "auto")==0) tomwalters@397: strPlottype = "png"; tomwalters@397: } else { tomwalters@397: // We have a (probably non-existant) file. Auto-detect type by extension if requested tomwalters@397: strncpy(m_sFilename, m_sDir, sizeof(m_sFilename)/sizeof(m_sFilename[0])); tomwalters@397: char *pDot = strrchr(m_sFilename, '.'); tomwalters@397: if (!pDot) { tomwalters@397: AIM_ERROR(_T("Please supply extension on filename when using 'auto' format: '%s'"), tomwalters@397: m_sFilename); tomwalters@397: return false; tomwalters@397: } tomwalters@397: strPlottype = &pDot[1]; tomwalters@397: } tomwalters@397: m_bOutputFile= true; //! \todo Should check that it's possible to write to the file tomwalters@397: } tomwalters@397: // Cairo's RGB24 format has 32-bit pixels with the upper 8 bits unused. tomwalters@397: // This is not the same as the plotutils PNG format. This information is transferred by the tomwalters@397: // function GetPixelFormat. The pixel format is dealt with by the reciever. tomwalters@397: m_cSurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, tomwalters@397: m_iWidth, tomwalters@397: m_iHeight); tomwalters@397: m_cCr = cairo_create (m_cSurface); tomwalters@397: cairo_scale(m_cCr, (float)m_iWidth, (float)m_iHeight); tomwalters@397: // Now setup things for this plotter. tomwalters@397: cairo_select_font_face(m_cCr, tomwalters@397: m_pParam->GetString("output.img.fontname"), tomwalters@397: CAIRO_FONT_SLANT_NORMAL, tomwalters@397: CAIRO_FONT_WEIGHT_BOLD); tomwalters@397: cairo_set_font_size (m_cCr, 0.015); tomwalters@397: return true; tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::CloseFile() { tomwalters@397: // Plotting library tomwalters@397: if (m_iPlotHandle>0) { tomwalters@397: cairo_destroy(m_cCr); tomwalters@397: m_iPlotHandle = 0; tomwalters@397: } tomwalters@397: // And the output file tomwalters@397: if (m_bOutputFile) { tomwalters@397: cairo_surface_write_to_png(m_cSurface, m_sFilename); tomwalters@397: m_bOutputFile = false; tomwalters@397: } tomwalters@397: cairo_surface_destroy(m_cSurface); tomwalters@397: } tomwalters@397: tomwalters@397: GraphicsOutputDeviceCairo::~GraphicsOutputDeviceCairo() { tomwalters@397: AIM_ASSERT(!m_iPlotHandle); tomwalters@397: CloseFile(); tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gGrab() { tomwalters@397: // Open file. tomwalters@397: if (!OpenFile(m_iFileNumber)) { tomwalters@397: return; tomwalters@397: } tomwalters@397: // Setup plotting area. tomwalters@397: cairo_set_line_width (m_cCr, 0.001f); tomwalters@397: gColor3f (0.0f, 0.0f, 0.0f); tomwalters@397: cairo_paint (m_cCr); tomwalters@397: gColor3f(1.0f, 1.0f, 0.0f); tomwalters@397: } tomwalters@397: tomwalters@397: int GraphicsOutputDeviceCairo::GetPixelFormat() { tomwalters@397: return AIM_PIX_FMT_RGB24_32; tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gBeginLineStrip() { tomwalters@397: m_bIsFirstVertex = true; tomwalters@397: m_iVertexType = VertexTypeLine; tomwalters@397: //! \todo Make line width user-settable tomwalters@397: cairo_set_line_width (m_cCr, 0.001f); tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gBeginQuadStrip() { tomwalters@397: m_bIsFirstVertex = true; tomwalters@397: m_iVertexType = VertexTypeQuad; tomwalters@397: m_iPrevVertexCount = 0; tomwalters@397: cairo_set_line_width (m_cCr, 0.001f); tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gColor3f(float r, float g, float b) { tomwalters@397: if (m_bInvertColors) { tomwalters@397: r = 1-r; tomwalters@397: g = 1-g; tomwalters@397: b = 1-b; tomwalters@397: } tomwalters@397: cairo_set_source_rgb (m_cCr, r, g, b); tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gVertex3f(float x, float y, float z) { tomwalters@397: switch(m_iVertexType) { tomwalters@397: case VertexTypeLine: tomwalters@397: if (m_bIsFirstVertex) { tomwalters@397: m_bIsFirstVertex = false; tomwalters@397: //pl_fmove(x, y); tomwalters@397: cairo_move_to(m_cCr, x, 1-y); tomwalters@397: } else { tomwalters@397: //pl_fcont(x, y); tomwalters@397: cairo_line_to(m_cCr, x, 1-y); tomwalters@397: } tomwalters@397: break; tomwalters@397: case VertexTypeQuad: tomwalters@397: /* Store vertices until we got four in a row. tomwalters@397: * The order of vertices when processing quads is: tomwalters@397: * 1-----3-----5 tomwalters@397: * | | | tomwalters@397: * 0-----2-----4 tomwalters@397: */ tomwalters@397: if (m_iPrevVertexCount >= 3) { tomwalters@397: // Plot this quad tomwalters@397: cairo_move_to(m_cCr, m_aPrevX[0], 1-m_aPrevY[0]); tomwalters@397: cairo_line_to(m_cCr, m_aPrevX[1], 1-m_aPrevY[1]); tomwalters@397: cairo_line_to(m_cCr, x, y); tomwalters@397: cairo_line_to(m_cCr, m_aPrevX[2], 1-m_aPrevY[2]); tomwalters@397: cairo_close_path (m_cCr); tomwalters@397: tomwalters@397: // Last vertices of this quad are the first of the next tomwalters@397: m_aPrevX[0] = m_aPrevX[2]; tomwalters@397: m_aPrevY[0] = m_aPrevY[2]; tomwalters@397: m_aPrevX[1] = x; tomwalters@397: m_aPrevY[1] = y; tomwalters@397: m_iPrevVertexCount = 2; tomwalters@397: } else { tomwalters@397: // Not at the fourth, keep storing tomwalters@397: m_aPrevX[m_iPrevVertexCount] = x; tomwalters@397: m_aPrevY[m_iPrevVertexCount] = y; tomwalters@397: m_iPrevVertexCount++; tomwalters@397: } tomwalters@397: break; tomwalters@397: default: tomwalters@397: // Should not happen tomwalters@397: AIM_ASSERT(0); tomwalters@397: } tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gEnd() { tomwalters@397: if(m_iVertexType==VertexTypeLine) tomwalters@397: cairo_stroke (m_cCr); tomwalters@397: else tomwalters@397: cairo_fill (m_cCr); tomwalters@397: m_iVertexType = VertexTypeNone; tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gText3f(float x, tomwalters@397: float y, tomwalters@397: float z, tomwalters@397: const char *sStr, tomwalters@397: bool bRotated) { tomwalters@397: cairo_text_extents_t te; tomwalters@397: if (bRotated) { tomwalters@397: cairo_rotate(m_cCr, M_PI/2); tomwalters@397: cairo_move_to(m_cCr, x ,1-y); tomwalters@397: cairo_show_text(m_cCr, sStr); tomwalters@397: //cairo_identity_matrix(m_cCr); tomwalters@397: cairo_rotate(m_cCr, -M_PI/2); tomwalters@397: } else { tomwalters@397: cairo_move_to(m_cCr, x ,1-y); tomwalters@397: cairo_show_text(m_cCr, sStr); tomwalters@397: } tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceCairo::gRelease() { tomwalters@397: AIM_ASSERT(m_iPlotHandle>0); tomwalters@397: CloseFile(); tomwalters@397: // Finished this one, up to the next! tomwalters@397: m_iFileNumber++; tomwalters@397: }