void ImageProcessing::drawRectangle( FrameBuffer * frame, Rect const & rect, RawPixel const & colour ) { Point tl = const_cast<Rect &>(rect).topLeft(); Point br = const_cast<Rect &>(rect).bottomRight(); unsigned int tlx = tl.x(); unsigned int tly = tl.y(); unsigned int brx = br.x(); unsigned int bry = br.y(); drawBresenhamLine( frame, tlx, tly, brx, tly, colour ); drawBresenhamLine( frame, brx, tly, brx, bry, colour ); drawBresenhamLine( frame, brx, bry, tlx, bry, colour ); drawBresenhamLine( frame, tlx, bry, tlx, tly, colour ); }
void renderGElement(XPM *canvas, GElement *el){ switch(el->type){ case LINE: printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y); if(cohenSutherlandFrameLine(canvas, &el->data.line.st, &el->data.line.en) == 1){ printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y); drawBresenhamLine(canvas, el->data.line.st, el->data.line.en, 1); } else{ fprintf(stderr, "*** Line was totally outside the canvas.\n"); } break; default: fprintf(stderr, "Unhandled type!\n"); break; } }
void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glLoadIdentity(); glTranslatef(-1, -1, 0); glPushMatrix(); glScalef((GLfloat) 2 / MATRIX_SIZE, (GLfloat) 2 / MATRIX_SIZE, 0); glColor3f(0.7f, 0.7f, 0.7f); for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { glPushMatrix(); glTranslatef((GLfloat) i, (GLfloat) j, 0); drawSquareBorder(); glPopMatrix(); } } glPopMatrix(); /* making sure we have both points set */ if (linex1 * liney1 * linex2 * liney2 > 0) { drawBresenhamLine(); drawLine(); } drawLinePoints(); drawDebugInfo(); glFlush(); }
void drawPendulum(pstate *states, int states_len) { int left_loopings = 0, right_loopings = 0; int green_color = COLOR_GREEN; int red_color = COLOR_RED; int blue_color = COLOR_BLUE; // clear surface if (whitebg) { generic_surface_fill(sf, COLOR_WHITE); green_color = 0xff009600; red_color = 0xff960000; blue_color = 0xff000096; } else generic_surface_fill(sf, COLOR_BLACK); // precompute scaling double maxpendlength = l1 + l2b; // this scale is in pixels per meter double scale = (double)(sf->width < sf->height ? sf->width : sf->height) / 2.0 * (4.0/5.0) / maxpendlength; // draw center (main axis of pendulum) drawDot(sf, sf->width/2, sf->height/2, red_color); // draw maximum reach of common axis and outer pendulum drawCircle(sf, sf->width/2, sf->height/2, scale*l1, red_color); drawCircle(sf, sf->width/2, sf->height/2, scale*(l1+l2b), red_color); // draw data int last_xpos, last_ypos, xpos, ypos; int last_section, cur_section; double xpos_, ypos_; for (int i = 0; i < states_len; i++) { pstate *s = &states[i]; pstateToCartesianEnd(s, &xpos_, &ypos_); xpos = (int)(xpos_ * scale); ypos = (int)(ypos_ * scale); xpos += sf->width/2; ypos += sf->height/2; cur_section = s->phi2/(M_PI/2); if (cur_section < 0) cur_section *= -1; cur_section = cur_section%4; assert(cur_section >= 0 && cur_section < 4); if (i != 0) { // the first point does not have a previous one // to which we could draw a line, so ignore it //fprintf(stderr, "(%d|%d)->(%d|%d)\n", last_xpos, last_ypos, xpos, ypos); int color = green_color; if (last_section == 1/*upper left*/ && cur_section == 2/*upper right*/) { color = red_color; right_loopings++; } if (last_section == 2/*upper right*/ && cur_section == 1/*upper left*/) { color = blue_color; left_loopings++; } if (show_normal_lines || color != green_color) { drawBresenhamLine(sf, last_xpos, last_ypos, xpos, ypos, color); } } last_xpos = xpos; last_ypos = ypos; last_section = cur_section; } fprintf(stderr, "pendulum rendered. %d loopings to the left, %d loopings to the right.\n", left_loopings, right_loopings); }
void ImageProcessing::drawBresenhamLine( FrameBuffer * frame, Point const start, Point const end, RawPixel const colour ) { drawBresenhamLine( frame, start.x(), start.y(), end.x(), end.y(), colour ); }
void ImageProcessing::SegmentColours( FrameBuffer * frame, FrameBuffer * outFrame, unsigned int threshold, unsigned int minLength, unsigned int minSize, unsigned int subSample, ColourDefinition const & target, RawPixel const & mark, std::vector<VisionObject> & results ) { FrameBufferIterator it( frame ); FrameBufferIterator oit( outFrame ); Pixel cPixel; RawPixel oPixel; // unsigned int len; FloodFillState state; unsigned int count; for( unsigned int row = 0; row < frame->height; row = row + subSample ) { it.goPosition(row, subSample); oit.goPosition(row, subSample); count = 0; for( unsigned int col = subSample; col < frame->width; col = col + subSample, it.goRight( subSample ),oit.goRight( subSample ) ) { oit.getPixel( & oPixel ); if ( oPixel == RawPixel( 0, 0, 0 ) ) { it.getPixel( & cPixel ); if ( target.isMatch( cPixel ) ) { count++; } else { count = 0; } if ( count >= minLength ) { state.initialize(); doFloodFill( frame, outFrame, Point( col, row), cPixel, threshold, & target, subSample, & state ); #ifdef XX_DEBUG if ( state.size() > minSize ) { std::cout << "Flood fill returns size " << state.size() << std::endl; } #endif if ( state.size() > minSize ) { unsigned int tlx = state.bBox().topLeft().x(); unsigned int tly = state.bBox().topLeft().y(); unsigned int brx = state.bBox().bottomRight().x(); unsigned int bry = state.bBox().bottomRight().y(); drawBresenhamLine( outFrame, tlx, tly, tlx, bry, mark ); drawBresenhamLine( outFrame, tlx, bry, brx, bry, mark ); drawBresenhamLine( outFrame, brx, bry, brx, tly, mark ); drawBresenhamLine( outFrame, brx, tly, tlx, tly, mark ); drawBresenhamLine( frame, tlx, tly, tlx, bry, mark ); drawBresenhamLine( frame, tlx, bry, brx, bry, mark ); drawBresenhamLine( frame, brx, bry, brx, tly, mark ); drawBresenhamLine( frame, brx, tly, tlx, tly, mark ); // swapColours( outFrame, 0, state.bBox(), 1, ColourDefinition( Pixel(colour), Pixel(colour) ), state.averageColour() ); VisionObject vo( target.name, state.size(), state.x(), state.y(), state.averageColour(), state.bBox() ); std::vector<VisionObject>::iterator i; for( i = results.begin(); i != results.end(); ++i) { if ( (*i).size < vo.size ) { break; } } results.insert(i, vo ); } count = 0; } } else { count = 0; } } } }
void ImageProcessing::segmentScanLines( FrameBuffer * frame, FrameBuffer * outFrame, unsigned int threshold, unsigned int minLength, unsigned int maxLength, unsigned int minSize, RawPixel const & mark, unsigned int subSample, ColourDefinition const * target ) { FrameBufferIterator it( frame ); RawPixel pPixel; RawPixel cPixel; unsigned int startLine; int diff; unsigned int avgRed, avgGreen, avgBlue; unsigned int len; FloodFillState state; for( unsigned int row = 0; row < frame->height; row = row + subSample ) { it.goPosition( row, 0 ); it.getPixel( & pPixel ); // Extend to the left startLine = 0; avgRed = 0; avgGreen = 0; avgBlue = 0; it.goPosition(row, subSample); for( unsigned int col = subSample; col <= frame->width + subSample; col = col + subSample, it.goRight( subSample ) ) { if ( col < frame->width ) { it.getPixel( & cPixel ); diff = cPixel.diffIntensity( pPixel ); if ( diff < 0 ) { diff = -diff; } } if ( ( static_cast<unsigned int>(diff) > threshold ) || ( col >= static_cast<unsigned int>( frame->width - 1 ) ) ) { len = col - startLine; if ( ( len >= minLength ) && (len <= maxLength) ) { Pixel colour( (subSample * avgRed ) / len,(subSample * avgGreen ) / len,(subSample * avgBlue ) / len); if ( target == 0 ) { FrameBufferIterator out( outFrame, row, startLine ); for( unsigned int i = startLine; i < col; i++, out.goRight() ) { out.setPixel( colour ); } } else if (target->isMatch( colour ) ) { state.initialize(); doFloodFill( frame, outFrame, Point( startLine, row), colour, threshold, target, 1, & state ); #ifdef XX_DEBUG if ( state.size() > minSize ) { std::cout << "Flood fill returns size " << state.size() << std::endl; } #endif if ( state.size() > minSize ) { unsigned int tlx = state.bBox().topLeft().x(); unsigned int tly = state.bBox().topLeft().y(); unsigned int brx = state.bBox().bottomRight().x(); unsigned int bry = state.bBox().bottomRight().y(); drawBresenhamLine( outFrame, tlx, tly, tlx, bry, mark ); drawBresenhamLine( outFrame, tlx, bry, brx, bry, mark ); drawBresenhamLine( outFrame, brx, bry, brx, tly, mark ); drawBresenhamLine( outFrame, brx, tly, tlx, tly, mark ); drawBresenhamLine( frame, tlx, tly, tlx, bry, mark ); drawBresenhamLine( frame, tlx, bry, brx, bry, mark ); drawBresenhamLine( frame, brx, bry, brx, tly, mark ); drawBresenhamLine( frame, brx, tly, tlx, tly, mark ); // swapColours( outFrame, 0, state.bBox(), 1, ColourDefinition( Pixel(colour), Pixel(colour) ), state.averageColour() ); } } } avgRed = 0; avgGreen = 0; avgBlue = 0; startLine = col; } else { avgRed = avgRed + cPixel.red; avgGreen = avgGreen + cPixel.green; avgBlue = avgBlue + cPixel.blue; } pPixel = cPixel; } } }