/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLQt/src/ICLQt/GLPaintEngine.cpp ** ** Module : ICLQt ** ** Authors: Christof Elbrechter, Robert Haschke ** ** ** ** ** ** GNU LESSER GENERAL PUBLIC LICENSE ** ** This file may be used under the terms of the GNU Lesser General ** ** Public License version 3.0 as published by the ** ** ** ** Free Software Foundation and appearing in the file LICENSE.LGPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the license requirements will ** ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** ********************************************************************/ #include #include #include #include #include #include #include using std::string; using std::min; using namespace icl::utils; using namespace icl::core; namespace icl{ namespace qt{ namespace{ Rect32f computeRect(const Rect32f &rect, const Size &imageSize, PaintEngine::AlignMode mode){ // {{{ open switch(mode){ case PaintEngine::NoAlign: return Rect32f(rect.x, rect.y, imageSize.width, imageSize.height); case PaintEngine::Centered: { float cx = rect.x+rect.width/2; float cy = rect.y+rect.height/2; return Rect32f(cx-imageSize.width/2,cy-imageSize.height/2,imageSize.width,imageSize.height); } default: return rect; } } // }}} // inline float winToDraw(float x, float w) { return (2/w) * x -1; } // inline float drawToWin(float x, float w) { return (w/2) * x + (w/2); } } GLPaintEngine::GLPaintEngine(QGLWidget *widget): // {{{ open m_widget(widget),m_bciauto(false), m_font(QFont("Arial",30)), m_incompDepthBuf(0){ m_linewidth = 1; m_pointsize = 1; // widget->makeCurrent(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, widget->width(), widget->height(), 0, -999999, 999999); glPixelTransferf(GL_ALPHA_SCALE,1); glPixelTransferf(GL_RED_SCALE,1); glPixelTransferf(GL_GREEN_SCALE,1); glPixelTransferf(GL_BLUE_SCALE,1); glPixelTransferf(GL_ALPHA_BIAS,0); glPixelTransferf(GL_RED_BIAS,0); glPixelTransferf(GL_GREEN_BIAS,0); glPixelTransferf(GL_BLUE_BIAS,0); std::fill(m_fillcolor,m_fillcolor+4,0); std::fill(m_linecolor,m_linecolor+4,255); std::fill(m_bci,m_bci+3,0); } // }}} GLPaintEngine::~GLPaintEngine(){ // {{{ open ICL_DELETE(m_incompDepthBuf); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); } // }}} void GLPaintEngine::fontsize(float size){ // {{{ open m_font.setPointSize(size); } // }}} void GLPaintEngine::font(string name, float size, PaintEngine::TextWeight weight, PaintEngine::TextStyle style){ // {{{ open m_font.setFamily(name.c_str()); m_font.setPointSize(size); m_font.setStyle(style == PaintEngine::StyleNormal ? QFont::StyleNormal : style == PaintEngine::StyleItalic ? QFont::StyleItalic : QFont::StyleOblique); m_font.setWeight(weight == PaintEngine::Light ? QFont::Light : weight == PaintEngine::Normal ? QFont::Normal : weight == PaintEngine::DemiBold ? QFont::DemiBold : weight == PaintEngine::Bold ? QFont::Bold : QFont::Black); } // }}} void GLPaintEngine::color(float r, float g, float b, float a){ // {{{ open m_linecolor[0] = (float)r/255.0; m_linecolor[1] = (float)g/255.0; m_linecolor[2] = (float)b/255.0; m_linecolor[3] = (float)a/255.0; } // }}} void GLPaintEngine::fill(float r, float g, float b, float a){ // {{{ open m_fillcolor[0] = (float)r/255.0; m_fillcolor[1] = (float)g/255.0; m_fillcolor[2] = (float)b/255.0; m_fillcolor[3] = (float)a/255.0; } void GLPaintEngine::linewidth(float w){ m_linewidth = w; } void GLPaintEngine::pointsize(float s){ m_pointsize = s; } // }}} void GLPaintEngine::line(const Point32f &a, const Point32f &b){ // {{{ open glEnable(GL_LINE_SMOOTH); glLineWidth(m_linewidth); glColor4fv(m_linecolor); glBegin(GL_LINES); glVertex2f(a.x,a.y); glVertex2f(b.x,b.y); glEnd(); glDisable(GL_LINE_SMOOTH); } // }}} void GLPaintEngine::point(const Point32f &p){ // {{{ open glColor4fv(m_linecolor); glPointSize(m_pointsize); glBegin(GL_POINTS); glVertex2f((GLfloat)p.x,(GLfloat)p.y); glEnd(); } // }}} void GLPaintEngine::image(const Rect32f &r,const QImage &image, PaintEngine::AlignMode mode, scalemode sm){ // {{{ open Img8u buf; if(image.format()==QImage::Format_Indexed8){ buf = Img8u(Size(image.width(),image.height()),formatGray); }else{ buf = Img8u(Size(image.width(),image.height()),4); } interleavedToPlanar(image.bits(),&buf); this->image(r,&buf,mode,sm); } // }}} void GLPaintEngine::image(const Rect32f &r,ImgBase *image, PaintEngine::AlignMode mode, scalemode sm){ // {{{ open ICLASSERT_RETURN(image); glColor4f(1,1,1,1); GLImg gli(image,sm); gli.setBCI(m_bci[0],m_bci[1],m_bci[2]); gli.draw2D(computeRect(r,image->getSize(),mode), Size(m_widget->width(),m_widget->height())); } // }}} void GLPaintEngine::rect(const Rect32f &r){ // {{{ open glLineWidth(m_linewidth); glColor4fv(m_fillcolor); glBegin(GL_QUADS); glVertex2f((GLfloat)r.x,(GLfloat)r.y); glVertex2f((GLfloat)r.right(),(GLfloat)r.y); glVertex2f((GLfloat)r.right(),(GLfloat)r.bottom()); glVertex2f((GLfloat)r.x,(GLfloat)r.bottom()); glEnd(); glColor4fv(m_linecolor); glBegin(GL_LINE_STRIP); glVertex2f((GLfloat)r.x,(GLfloat)r.y); glVertex2f((GLfloat)r.right(),(GLfloat)r.y); glVertex2f((GLfloat)r.right(),(GLfloat)r.bottom()); glVertex2f((GLfloat)r.x,(GLfloat)r.bottom()); glVertex2f((GLfloat)r.x,(GLfloat)r.y); glEnd(); } void GLPaintEngine::triangle(const Point32f &a, const Point32f &b, const Point32f &c){ glColor4fv(m_fillcolor); glBegin(GL_TRIANGLES); glVertex2f((GLfloat)a.x,(GLfloat)a.y); glVertex2f((GLfloat)b.x,(GLfloat)b.y); glVertex2f((GLfloat)c.x,(GLfloat)c.y); glEnd(); glLineWidth(m_linewidth); glEnable(GL_LINE_SMOOTH); glColor4fv(m_linecolor); glBegin(GL_LINE_LOOP); glVertex2f((GLfloat)a.x,(GLfloat)a.y); glVertex2f((GLfloat)b.x,(GLfloat)b.y); glVertex2f((GLfloat)c.x,(GLfloat)c.y); glEnd(); glDisable(GL_LINE_SMOOTH); } // }}} void GLPaintEngine::quad(const Point32f &a, const Point32f &b, const Point32f &c, const Point32f &d){ glEnable(GL_LINE_SMOOTH); glColor4fv(m_fillcolor); glBegin(GL_QUADS); glVertex2f((GLfloat)a.x,(GLfloat)a.y); glVertex2f((GLfloat)b.x,(GLfloat)b.y); glVertex2f((GLfloat)c.x,(GLfloat)c.y); glVertex2f((GLfloat)d.x,(GLfloat)d.y); glEnd(); glColor4fv(m_linecolor); glBegin(GL_LINE_LOOP); glVertex2f((GLfloat)a.x,(GLfloat)a.y); glVertex2f((GLfloat)b.x,(GLfloat)b.y); glVertex2f((GLfloat)c.x,(GLfloat)c.y); glVertex2f((GLfloat)d.x,(GLfloat)d.y); glEnd(); glDisable(GL_LINE_SMOOTH); } // }}} void GLPaintEngine::ellipse(const Rect32f &r){ // {{{ open glLineWidth(m_linewidth); glEnable(GL_LINE_SMOOTH); glColor4fv(m_fillcolor); GLfloat w2 = 0.5*(r.width); GLfloat h2= 0.5*(r.height); GLfloat cx = r.x+w2; GLfloat cy = r.y+h2; GLint NSTEPS = 32; float circumference = 0; if(w2 == h2){ circumference = 2* M_PI * w2; }else{ if(!(w2+h2)){ circumference = 0; }else{ float l = 3 * (w2-h2) / (w2+h2), l3s = 3*l*l; circumference = (w2+h2) * M_PI * ( 1 + l3s / 10 + ::sqrt(4-l3s)); } // compute circumference of ellipse using Ramanujan's approximation } if(circumference > 50000) NSTEPS = 4000; else if(circumference > 10000) NSTEPS = 2000; else if(circumference > 5000) NSTEPS = 1000; else if(circumference > 1000) NSTEPS = 256; else if(circumference > 100) NSTEPS = 64; //DEBUG_LOG("circumference: " << circumference << " using nSteps:" << NSTEPS << " Rect was: " << r); const GLfloat D_ARC = (2*M_PI)/(double)NSTEPS; glBegin(GL_POLYGON); for(int i=0;i0) c*=10; fScaleRGB*=(1.0+c); fBiasRGB-=c/2; glPixelTransferf(GL_RED_SCALE,fScaleRGB); glPixelTransferf(GL_GREEN_SCALE,fScaleRGB); glPixelTransferf(GL_BLUE_SCALE,fScaleRGB); glPixelTransferf(GL_RED_BIAS,fBiasRGB); glPixelTransferf(GL_GREEN_BIAS,fBiasRGB); glPixelTransferf(GL_BLUE_BIAS,fBiasRGB); } // }}} } // namespace qt }