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