notepad_utils.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: notepad_utils.cpp
00003  */
00004 
00005 /*
00006  * This file is part of notepad.
00007  *
00008  * notepad is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * notepad is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2010 IREX Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <algorithm>
00028 
00029 #include <glib.h>
00030 #include <glib/gprintf.h>
00031 
00032 #include <liberscribble/scb.h> // for ScbDevColor
00033 #include "notepad_utils.h"
00034 #include "log.h"
00035 
00036 namespace notepad 
00037 {
00038     void GCtx::set_color(const ScbDevColor scribblecolor)
00039     {
00040         // assume 8 bit depth: 0x00 00 00 .. 0xFF FF FF
00041         switch (scribblecolor)
00042         {
00043             case ERSCRIBBLE_DEV_COLOR_WHITE:
00044                 color = 0xFFFFFF;
00045                 break;
00046             case ERSCRIBBLE_DEV_COLOR_LIGHT_GRAY:
00047                 color = 0xAAAAAA;
00048                 break;
00049             case ERSCRIBBLE_DEV_COLOR_DARK_GRAY:
00050                 color = 0x888888; // or 55
00051                 break;
00052             case ERSCRIBBLE_DEV_COLOR_BLACK:
00053                 color = 0x000000;
00054                 break;
00055             case ERSCRIBBLE_DEV_COLOR_UNKNOWN:
00056             default:
00057                 color = 0x000000;
00058                 break;
00059         }
00060     }
00061 
00062 
00063     void GCtx::draw_line(GdkImage* image, const int x1, const int y1, const int x2, const int y2)
00064     {
00065         g_assert(image != 0);
00066 
00067         Point p1(x1, y1);
00068         Point p2(x2, y2);
00069 
00070         // draw the end point
00071         draw_brush(image, p2.x, p2.y);
00072 
00073         bool is_steep = abs(p2.y - p1.y) > abs(p2.x - p1.x);
00074         if (is_steep)
00075         {
00076             std::swap(p1.x, p1.y);
00077             std::swap(p2.x, p2.y);
00078         }
00079 
00080         // setup line draw
00081         int deltax   = abs(p2.x - p1.x);
00082         int deltaerr = abs(p2.y - p1.y);
00083         int error = 0;
00084         int x = p1.x;
00085         int y = p1.y;
00086 
00087         // setup step increment
00088         int xstep = (p1.x < p2.x) ? 1 : -1;
00089         int ystep = (p1.y < p2.y) ? 1 : -1;
00090 
00091         if (is_steep)
00092         {
00093             // draw swapped line
00094             for (int numpix = 0; numpix < deltax; numpix++)
00095             {
00096                 x += xstep;
00097                 error += deltaerr;
00098 
00099                 if (2 * error > deltax) 
00100                 {
00101                     y += ystep;
00102                     error -= deltax;
00103                 }
00104 
00105                 draw_brush(image, y, x);
00106             }
00107         }
00108         else
00109         {
00110             // draw normal line
00111             for (int numpix = 0; numpix < deltax; numpix++)
00112             {
00113                 x += xstep;
00114                 error += deltaerr;
00115 
00116                 if (2 * error > deltax) 
00117                 {
00118                     y += ystep;
00119                     error -= deltax;
00120                 }
00121 
00122                 draw_brush(image, x, y);
00123             }
00124         }
00125 
00126     }
00127 
00128 
00129 /*
00130     Below, the different kernel brush shapes are shown as defined in the linux kernel (delta.c).
00131     0 is the centerpoint of the brush
00132         
00133         Defined brushes:
00134 
00135         Brush #1:
00136                         .......0 0x01
00137         
00138         Brush #2:
00139                         ......O0 0x03
00140                         ......OO 0x03
00141 
00142         Brush #3:
00143                         ......O. 0x02
00144                         .....O0O 0x07
00145                         ......O. 0x02
00146 
00147         Brush #4:
00148                         .....OO. 0x06
00149                         ....OO0O 0x0F
00150                         ....OOOO 0x0F
00151                         .....OO. 0x06
00152 
00153         Brush #5:
00154                         .....O.. 0x04
00155                         ....OOO. 0x0E
00156                         ...OO0OO 0x1F
00157                         ....OOO. 0x0E
00158                         .....O.. 0x04
00159 
00160         Brush #6:
00161                         ....OO.. 0x0C
00162                         ...OOOO. 0x1E
00163                         ..OOO0OO 0x3F
00164                         ..OOOOOO 0x3F
00165                         ...OOOO. 0x1E
00166                         ....OO.. 0x0C
00167 
00168 */
00169 
00170     void GCtx::draw_brush(GdkImage* image, const int x, const int y)
00171     {
00172         g_assert(image != 0);
00173         // draw a brush of size: this->size
00174         // at coordinate (x,y)
00175         gint npoints_in_shape = 0;
00176 
00177         // portrait only for now
00178         GdkPoint brush1[1]  = { {x,  y  } };
00179         GdkPoint brush2[4]  = { 
00180                                 {x-1,y  } , {x  ,y  } , 
00181                                 {x-1,y+1} , {x  ,y+1} 
00182                               };
00183 
00184         GdkPoint brush3[5]  = { 
00185                                             {x  ,y-1} , 
00186                                 {x-1,y  } , {x  ,y  } , {x+1,y  } , 
00187                                             {x  ,y+1} 
00188                               };
00189 
00190         GdkPoint brush4[12] = { 
00191                                             {x-1,y-1} , {x  ,y-1} , 
00192                                 {x-2,y  } , {x-1,y  } , {x  ,y  } , {x+1,y  }, 
00193                                 {x-2,y+1} , {x-1,y+1} , {x  ,y+1} , {x+1,y+1}, 
00194                                             {x-1,y+2} , {x  ,y+2} 
00195                               };
00196 
00197         GdkPoint brush5[13] = { 
00198                                                         {x  ,y-2} , 
00199                                             {x-1,y-1} , {x  ,y-1} , {x+1,y-1} , 
00200                                 {x-2,y  } , {x-1,y  } , {x  ,y  } , {x+1,y  } , {x+2,y} ,
00201                                             {x-1,y+1} , {x  ,y+1} , {x+1,y+1} ,       
00202                                                         {x  ,y+2} 
00203                               };
00204 
00205         GdkPoint* shape = NULL;
00206 
00207         switch(size)
00208         {
00209             case 1:
00210                 npoints_in_shape = 1;
00211                 shape = brush1;
00212                 break;
00213             case 2:
00214                 npoints_in_shape = 4;
00215                 shape = brush2;
00216                 break;
00217             case 3:
00218                 npoints_in_shape = 5;
00219                 shape = brush3;
00220                 break;
00221             case 4:
00222                 npoints_in_shape = 12;
00223                 shape = brush4;
00224                 break;
00225             case 5:
00226                 npoints_in_shape = 13;
00227                 shape = brush5;
00228                 break;
00229             default:
00230                 npoints_in_shape = 13;
00231                 break;
00232 
00233         }
00234         draw_points(image, shape, npoints_in_shape);
00235     }
00236 
00237     void GCtx::draw_points(GdkImage* image, const GdkPoint* shape, const int npoints_in_shape)
00238     {
00239         g_assert(image != 0);
00240         g_assert(shape != 0);
00241         g_assert(npoints_in_shape > 0);
00242 
00243 #if MACHINE_IS_DR1000S || MACHINE_IS_DR1000SW
00244         gint w = image->width;
00245 #endif
00246 #if MACHINE_IS_DR800S || MACHINE_IS_DR800SG || MACHINE_IS_DR800SW
00247         gint w = image->width - margin;
00248 #endif
00249         gint h = image->height;
00250 
00251         for (int i = 0; i < npoints_in_shape; i++)
00252         {
00253             // filter part of brush out of region
00254             if (  (shape[i].x > margin && shape[i].x < w) 
00255                && (shape[i].y > margin && shape[i].y < h) )
00256             {
00257                 gdk_image_put_pixel(image, shape[i].x, shape[i].y, color);
00258             }
00259         }
00260     }
00261 
00262 
00263 }
00264 
00265 
Generated by  doxygen 1.6.2-20100208