#include #include #include #include #include #include #include #include using std::string; using std::min; namespace icl{ namespace{ Rect computeRect(const Rect &rect, const Size &imageSize, PaintEngine::AlignMode mode){ // {{{ open switch(mode){ case PaintEngine::NoAlign: return Rect(rect.x, rect.y, imageSize.width, imageSize.height); case PaintEngine::Centered: { int cx = rect.x+rect.width/2; int cy = rect.y+rect.height/2; return Rect(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_poWidget(widget),m_bBCIAutoFlag(false), m_oFont(QFont("Arial",30)), m_poImageBufferForIncompatibleDepth(0){ m_fLineWidth = 1; m_fPointSize = 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); memset(m_afFillColor,0,4*sizeof(float)); for(int i=0;i<4;m_afLineColor[i++]=255); memset(m_aiBCI,0,3*sizeof(int)); } // }}} GLPaintEngine::~GLPaintEngine(){ // {{{ open if(m_poImageBufferForIncompatibleDepth) delete m_poImageBufferForIncompatibleDepth; glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); } // }}} void GLPaintEngine::fontsize(int size){ // {{{ open m_oFont.setPointSize(size); } // }}} void GLPaintEngine::font(string name, int size, PaintEngine::TextWeight weight, PaintEngine::TextStyle style){ // {{{ open m_oFont.setFamily(name.c_str()); m_oFont.setPointSize(size); m_oFont.setStyle(style == PaintEngine::StyleNormal ? QFont::StyleNormal : style == PaintEngine::StyleItalic ? QFont::StyleItalic : QFont::StyleOblique); m_oFont.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(int r, int g, int b, int a){ // {{{ open m_afLineColor[0] = (float)r/255.0; m_afLineColor[1] = (float)g/255.0; m_afLineColor[2] = (float)b/255.0; m_afLineColor[3] = (float)a/255.0; } // }}} void GLPaintEngine::fill(int r, int g, int b, int a){ // {{{ open m_afFillColor[0] = (float)r/255.0; m_afFillColor[1] = (float)g/255.0; m_afFillColor[2] = (float)b/255.0; m_afFillColor[3] = (float)a/255.0; } void GLPaintEngine::linewidth(float w){ m_fLineWidth = w; } void GLPaintEngine::pointsize(float s){ m_fPointSize = s; } // }}} void GLPaintEngine::line(const Point &a, const Point &b){ // {{{ open glEnable(GL_LINE_SMOOTH); glLineWidth(m_fLineWidth); glColor4fv(m_afLineColor); glBegin(GL_LINES); glVertex2f(a.x,a.y); glVertex2f(b.x,b.y); glEnd(); glDisable(GL_LINE_SMOOTH); } // }}} void GLPaintEngine::point(const Point &p){ // {{{ open glColor4fv(m_afLineColor); glPointSize(m_fPointSize); glBegin(GL_POINTS); glVertex2f((GLfloat)p.x,(GLfloat)p.y); glEnd(); } // }}} void GLPaintEngine::image(const Rect &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 Rect &r,ImgBase *image, PaintEngine::AlignMode mode, scalemode sm){ // {{{ open ICLASSERT_RETURN(image); glColor4f(1,1,1,1); GLTextureMapBaseImage texmapImage; texmapImage.bci(m_aiBCI[0],m_aiBCI[1],m_aiBCI[2]); texmapImage.updateTextures(image); texmapImage.drawTo(computeRect(r,image->getSize(),mode), Size(m_poWidget->width(),m_poWidget->height()),sm); } // }}} void GLPaintEngine::rect(const Rect &r){ // {{{ open glLineWidth(m_fLineWidth); glColor4fv(m_afFillColor); 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_afLineColor); glBegin(GL_LINE_LOOP); 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(); } void GLPaintEngine::triangle(const Point &a, const Point &b, const Point &c){ glColor4fv(m_afFillColor); 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_fLineWidth); glEnable(GL_LINE_SMOOTH); glColor4fv(m_afLineColor); 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 Point &a, const Point &b, const Point &c, const Point &d){ glEnable(GL_LINE_SMOOTH); glColor4fv(m_afFillColor); 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_afLineColor); 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 Rect &r){ // {{{ open glLineWidth(m_fLineWidth); glEnable(GL_LINE_SMOOTH); glColor4fv(m_afFillColor); GLfloat w2 = 0.5*(r.width); GLfloat h2= 0.5*(r.height); GLfloat cx = r.x+w2; GLfloat cy = r.y+h2; static const GLint NSTEPS = 32; static const GLfloat D_ARC = (2*M_PI)/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); } // }}} }