Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

Image-draw.cpp

Go to the documentation of this file.
00001 /*************************************************************************
00008  *
00009  * Source code for Real Time Image Library (libRTImage)
00010  *
00011  * Leeds Vision Group give permission for this code to be copied, modified
00012  * and distributed within the University of Leeds subject to the following
00013  * conditions:-
00014  *
00015  * - The code is not to be used for commercial gain.
00016  * - The code and use thereof will be attributed to the authors where
00017  *   appropriate (including demonstrations which rely on it's use).
00018  * - All modified, distributions of the source files will retain this header.
00019  *
00020  ****************************************************************************/
00021 #include "Image.h"
00022 #include <math.h>
00023 
00024 bool Image::draw_line(int x_start,
00025                       int y_start,
00026                       int x_end,
00027                       int y_end,
00028                       PixelGrey intensity)
00034 {
00035         return ImageGrey::draw_line(x_start, y_start, x_end, y_end, intensity);
00036 }
00037 
00038 bool Image::draw_line(int x_start,
00039                       int y_start,
00040                       int x_end,
00041                       int y_end,
00042                       PixelRGB pix)
00048 {
00049         return ImageRGB::draw_line(x_start, y_start, x_end, y_end, pix);
00050 }
00051 
00052 bool ImageGrey::draw_line(int x_start,
00053                       int y_start,
00054                       int x_end,
00055                       int y_end,
00056                       PixelGrey intensity)
00062 {
00063 
00064     bool         ret_val = true;
00065     double       gradient;
00066     double       offset;
00067     int          cnt;
00068     int          lowest_x;
00069     int          highest_x;
00070     int          lowest_y;
00071     int          highest_y;
00072     int          plot_pnt;
00073 
00074     if( ((x_start>=(int) width || y_start>=(int) height || x_start<0 || y_start<0) &&
00075          (x_end>=(int) width || y_end>=(int) height || x_end<0 || y_end<0)) ){
00076         /* Invalid inputs */
00077         ret_val = false ;
00078     }
00079     else{
00080         /* Draw line */
00081         if(x_start>x_end){
00082             highest_x = x_start;
00083             lowest_x  = x_end;
00084         }
00085         else{
00086             highest_x = x_end;
00087             lowest_x  = x_start;
00088         }
00089         if(y_start>y_end){
00090             highest_y = y_start;
00091             lowest_y  = y_end;
00092         }
00093         else{
00094             highest_y = y_end;
00095             lowest_y  = y_start;
00096         }
00097         if(x_start-x_end != 0){
00098             gradient = ((double)y_start-(double)y_end) / 
00099                                    ((double)x_start-(double)x_end) ;
00100             offset = y_start - gradient*x_start;
00101             for(cnt=lowest_y; cnt<=highest_y ; cnt++){
00102                 plot_pnt = (int)((cnt-offset)/gradient + 0.5) ;
00103                 if(plot_pnt<(int) width && cnt<(int) height && plot_pnt>=0 && cnt>=0){
00104                     *(brightness+plot_pnt+(int) width*cnt) = intensity ;
00105                 }
00106             }
00107             for(cnt=lowest_x; cnt<=highest_x ; cnt++){
00108                 plot_pnt = (int)(gradient*cnt + offset + 0.5) ;
00109                 if(plot_pnt<(int) height && cnt<(int) width && plot_pnt>=0 && cnt>=0){
00110                     *(brightness+cnt+(int) width*plot_pnt) = intensity ;
00111                 }
00112             }
00113         }
00114         else{
00115         /* Gradient is infinite */
00116             for(cnt=lowest_y; cnt<=highest_y ; cnt++){
00117                 if(cnt<(int) height && cnt>=0){
00118                     *(brightness+x_start+cnt*(int) width) = intensity ;
00119                 }
00120             }
00121         }
00122     }
00123     return ret_val;
00124 } 
00125 
00126 
00127 bool ImageRGB::draw_line(int x_start,
00128                          int y_start,
00129                          int x_end,
00130                          int y_end,
00131                          PixelRGB pix)
00137 {
00138 
00139     bool         ret_val = true;
00140     double       gradient;
00141     double       offset;
00142     int          cnt;
00143     int          lowest_x;
00144     int          highest_x;
00145     int          lowest_y;
00146     int          highest_y;
00147     int          plot_pnt;
00148 
00149     if( ((x_start>=(int) width || y_start>=(int) height || x_start<0 || y_start<0) &&
00150          (x_end>=(int) width || y_end>=(int) height || x_end<0 || y_end<0)) ){
00151     /* Invalid inputs */
00152         ret_val = false ;
00153     }
00154     else{
00155     /* Draw line */
00156         if(x_start>x_end){
00157             highest_x = x_start;
00158             lowest_x  = x_end;
00159         }
00160         else{
00161             highest_x = x_end;
00162             lowest_x  = x_start;
00163         }
00164 
00165         if(y_start>y_end){
00166             highest_y = y_start;
00167             lowest_y  = y_end;
00168         }
00169         else{
00170             highest_y = y_end;
00171             lowest_y  = y_start;
00172         }
00173 
00174         if(x_start-x_end != 0){
00175             gradient = ((double)y_start-(double)y_end) / 
00176                                    ((double)x_start-(double)x_end) ;
00177             offset = y_start - gradient*x_start;
00178             for(cnt=lowest_y; cnt<=highest_y ; cnt++){
00179                 plot_pnt = (int)((cnt-offset)/gradient + 0.5) ;
00180                 if(plot_pnt<(int) width && cnt<(int) height && plot_pnt>=0 && cnt>=0){
00181                     set_pixel(plot_pnt,cnt,pix);
00182                 }
00183             }
00184             for(cnt=lowest_x; cnt<=highest_x ; cnt++){
00185                 plot_pnt = (int)(gradient*cnt + offset + 0.5) ;
00186                 if(plot_pnt<(int) height && cnt<(int) width && plot_pnt>=0 && cnt>=0){
00187                     set_pixel(cnt,plot_pnt,pix);
00188                 }
00189             }
00190         }
00191         else{
00192         /* Gradient is infinite */
00193             for(cnt=lowest_y; cnt<=highest_y ; cnt++){
00194                 if(cnt<(int) height && cnt>=0){
00195                     set_pixel(x_start, cnt,pix);
00196                 }
00197             }
00198         }
00199     }
00200     return ret_val;
00201 } 
00202 
00203 bool Image::draw_filled_circle(unsigned int centre_x,
00204                                    unsigned int centre_y,
00205                                    unsigned int radius,
00206                                    PixelGrey intensity)
00212 {
00213     return ImageGrey::draw_filled_circle(centre_x, centre_y, radius, intensity);
00214 }
00215 
00216 bool Image::draw_filled_circle(unsigned int centre_x,
00217                                    unsigned int centre_y,
00218                                    unsigned int radius,
00219                                    PixelRGB pix)
00225 {
00226     return ImageRGB::draw_filled_circle(centre_x, centre_y, radius, pix);
00227 }
00228 
00229 
00230 bool ImageGrey::draw_filled_circle(unsigned int centre_x,
00231                                    unsigned int centre_y,
00232                                    unsigned int radius, 
00233                                    PixelGrey intensity)
00239 {
00240 
00241     bool         ret_val = true ;
00242     int          cnt;
00243     unsigned int plot_pnt;
00244 
00245     if( centre_x>= width         ||
00246         centre_y>= height        ||
00247         radius>centre_x         ||
00248         radius>centre_y         ||
00249         radius+centre_x>= width  ||
00250         radius+centre_y>= height ){
00251     /* Inputs are invalid */
00252         ret_val = false ;
00253     }
00254     else{
00255     /* Draw circle */
00256         for(cnt=-(radius-1) ; cnt<(int)radius ; cnt++ ){
00257         /* For each x calculate y using r^2 = x^2 + y^2 */ 
00258         /* For each y calculate x using r^2 = x^2 + y^2 */ 
00259             plot_pnt = (unsigned int)(sqrt((double)(radius*radius - cnt*cnt))
00260                                       +0.5);
00261             draw_line(centre_x+cnt,centre_y+plot_pnt,
00262                       centre_x-cnt,centre_y+plot_pnt,
00263                       intensity);
00264             draw_line(centre_x+cnt,centre_y-plot_pnt,
00265                       centre_x-cnt,centre_y-plot_pnt,
00266                       intensity);
00267             draw_line(centre_x+plot_pnt,centre_y+cnt,
00268                       centre_x-plot_pnt,centre_y+cnt,
00269                       intensity);
00270             draw_line(centre_x+plot_pnt,centre_y-cnt,
00271                       centre_x-plot_pnt,centre_y-cnt,
00272                       intensity);
00273         }
00274     }
00275     return ret_val;
00276 }
00277 
00278 bool ImageRGB::draw_filled_circle(unsigned int centre_x,
00279                                    unsigned int centre_y,
00280                                    unsigned int radius, 
00281                                    PixelRGB pix)
00287 {
00288 
00289     bool         ret_val = true ;
00290     int          cnt;
00291     unsigned int plot_pnt;
00292 
00293     if( centre_x>= width         ||
00294         centre_y>= height        ||
00295         radius>centre_x         ||
00296         radius>centre_y         ||
00297         radius+centre_x>= width  ||
00298         radius+centre_y>= height ){
00299     /* Inputs are invalid */
00300         ret_val = false ;
00301     }
00302     else{
00303     /* Draw circle */
00304         for(cnt=-(radius-1) ; cnt<(int)radius ; cnt++ ){
00305         /* For each x calculate y using r^2 = x^2 + y^2 */ 
00306         /* For each y calculate x using r^2 = x^2 + y^2 */ 
00307             plot_pnt = (unsigned int)(sqrt((double)(radius*radius - cnt*cnt))
00308                                       +0.5);
00309             draw_line(centre_x+cnt,centre_y+plot_pnt,
00310                           centre_x-cnt,centre_y+plot_pnt,
00311                           pix);
00312             draw_line(centre_x+cnt,centre_y-plot_pnt,
00313                           centre_x-cnt,centre_y-plot_pnt,
00314                           pix);
00315             draw_line(centre_x+plot_pnt,centre_y+cnt,
00316                           centre_x-plot_pnt,centre_y+cnt,
00317                           pix);
00318             draw_line(centre_x+plot_pnt,centre_y-cnt,
00319                           centre_x-plot_pnt,centre_y-cnt,
00320                           pix);
00321         }
00322     }
00323     return ret_val;
00324 }
00325 

Generated at Fri Aug 13 17:29:21 2004 for libRTImage by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001