image_draw.h

Go to the documentation of this file.
00001 #ifndef IMAGE_DRAW_H
00002 #define IMAGE_DRAW_H
00003 
00004 #include <cmath>
00005 #include "image.h"
00006 
00007 namespace mimas{
00008 
00009 /* Bresenham line function */
00010 template< typename T>
00011   void drawLine( image_ref<T> &image, int x1, int y1, int x2, int y2, T colour = T())
00012 {
00013   int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
00014 
00015   dx=x2-x1;     /* the horizontal distance of the line */
00016   dy=y2-y1;     /* the vertical distance of the line */
00017   dxabs=std::abs(dx);
00018   dyabs=std::abs(dy);
00019   sdx=sgn(dx);
00020   sdy=sgn(dy);
00021   x=dyabs>>1;
00022   y=dxabs>>1;
00023   px=x1;
00024   py=y1;
00025 
00026   image.setPixel(px,py,colour);
00027 
00028   if (dxabs>=dyabs) /* the line is more horizontal than vertical */
00029     {
00030       for(i=0;i<dxabs;i++)
00031   {
00032     y+=dyabs;
00033     if (y>=dxabs)
00034       {
00035         y-=dxabs;
00036         py+=sdy;
00037       }
00038     px+=sdx;
00039     image.setPixel(px,py,colour);
00040   }
00041     }
00042   else /* the line is more vertical than horizontal */
00043     {
00044       for(i=0;i<dyabs;i++)
00045   {
00046     x+=dxabs;
00047     if (x>=dyabs)
00048       {
00049         x-=dyabs;
00050         px+=sdx;
00051       }
00052     py+=sdy;
00053     image.setPixel(px,py,colour);
00054 
00055 
00056   }
00057     }
00058 }
00059     
00060 template< typename T>
00061 void drawBox(image_ref<T> &image, int x1, int y1, int x2, int y2, T colour = T())
00062 {
00063   drawLine(image, x1,y1,x1,y2, colour);
00064   drawLine(image, x1,y1,x2,y1, colour);
00065   drawLine(image, x2,y1,x2,y2, colour);
00066   drawLine(image, x1,y2,x2,y2, colour);
00067 }
00068 
00069 template< typename T>
00070 void drawThickLine(image_ref<T> &image, int x1, int y1, int x2, int y2, T colour, int width)
00071 {
00072   int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
00073   int p,q;
00074 
00075   dx=x2-x1;     /* the horizontal distance of the line */
00076   dy=y2-y1;     /* the vertical distance of the line */
00077   dxabs=std::abs(dx);
00078   dyabs=std::abs(dy);
00079   sdx=sgn(dx);
00080   sdy=sgn(dy);
00081   x=dyabs>>1;
00082   y=dxabs>>1;
00083   px=x1;
00084   py=y1;
00085 
00086   for (p=-width/2; p<width/2; p++)
00087     for (q=-width/2; q<width/2; q++)
00088       image.setPixel(px+p,py+q,colour);
00089 
00090   if (dxabs>=dyabs) /* the line is more horizontal than vertical */
00091     {       for(i=0;i<dxabs;i++)
00092       {
00093   y+=dyabs;
00094   if (y>=dxabs)
00095     {
00096       y-=dxabs;
00097       py+=sdy;
00098     }
00099   px+=sdx;
00100 
00101   for (p=-width/2; p<width/2; p++)
00102     for (q=-width/2; q<width/2; q++)
00103       image.setPixel(px+p,py+q,colour);
00104       }
00105     }
00106   else /* the line is more vertical than horizontal */
00107     {
00108       for(i=0;i<dyabs;i++)
00109   {
00110     x+=dxabs;
00111     if (x>=dyabs)
00112       {
00113         x-=dyabs;
00114         px+=sdx;
00115       }
00116     py+=sdy;
00117           
00118     for (p=-width/2; p<width/2; p++)
00119       for (q=-width/2; q<width/2; q++)
00120         image.setPixel(px+p,py+q,colour);
00121 
00122   }
00123     }
00124 }
00125 
00126 }
00127 
00128 #endif

[GNU/Linux] [Qt] [Mesa] [STL] [Lapack] [Boost] [Magick++] [Xalan-C and Xerces-C] [doxygen] [graphviz] [FFTW] [popt] [xine] [Gnuplot] [gnu-arch] [gcc] [gstreamer] [autoconf/automake/make] [freshmeat.net] [opensource.org] [sourceforge.net] [MMVL]
mimas 2.1 - Copyright Mon Oct 30 11:31:17 2006, Bala Amavasai, Stuart Meikle, Arul Selvan, Fabio Caparrelli, Jan Wedekind, Manuel Boissenin, ...