Advertisement
BinYamin

Graphical Editor

Aug 30th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.22 KB | None | 0 0
  1. /*-----
  2. /What's the secret of your perfect skin? asked a facebook friend of mine.
  3. I smiled, happy that my efforts have not gone waste and replied "Photoshop." :P
  4. We all love photoshop. It is, without a doubt a huge software.
  5. But basically all photo editors work in somewhat similar way.
  6. A bitmap editor (for eg MS Paint) makes a 2D array of the pixels and allows you to manipulate the pixels using a mouse.
  7. so here's a simple bitmap graphical editor. Add mouse functionality, more colours and a little more functions and you will get an MS Paint of your own.
  8. Here's the code to a very basic version of it.
  9. Since I have used graphics.h it can be run only on TurboC but if you can do the graphics using openGL or something, the core of the program can be reused as it is. :)
  10.                     ------*/
  11.  
  12. /* --------------------------
  13.     //Shaha Hassan
  14.     // 30 Aug 2014
  15.  
  16.     Graphical Editor
  17.  
  18.     I M N           Create a new M ×N image with all pixels initially colored white (O).
  19.     C               Clear the table by setting all pixels white (O). The size remains unchanged.
  20.     L X Y C         Colors the pixel (X, Y ) in color (C).
  21.     V X Y1 Y2 C     Draw a vertical segment of color (C) in column X, between the rows Y 1 and Y 2 inclusive.
  22.     H X Y1 Y2 C     Draw a horizontal segment of color (C) in the row Y , between the columns X1 and X2 inclusive.
  23.     K X1 Y1 X2 Y2 C Draw a filled rectangle of color C, where (X1, Y 1) is the
  24.             upper-left and (X2, Y 2) the lower right corner.
  25.     F X Y C         Fill the region R with the color C, where R is defined as follows.
  26.             Pixel (X, Y ) belongs to R. Any other pixel which is the same color as pixel (X, Y )
  27.             and shares a common side with any pixel in R also belongs to this region.
  28.     S Name          Write the file name in MSDOS 8.3 format followed by the contents of the current image.
  29.     X               Terminate the session.
  30.                 -----------------------*/
  31. /* show() and init() function can be modified to make the code compilable for modern compilers */
  32.  
  33.  
  34. #include<stdio.h>
  35. #include<string.h>
  36. #include<conio.h>
  37. #define PIX 219
  38.  
  39. void paint(int x, int y, char col, char bg);
  40. int isValid(int x, int y);
  41. void setf(char cl, int x, int y);
  42. void show();
  43.  
  44. void init();
  45.  
  46.  
  47. char canvas[30][30];
  48. int r,c;
  49.  
  50. int tonum(char c){
  51.     switch(c){
  52.     case 'B': return 9;
  53.     case 'G': return 10;
  54.     case 'C': return 11;
  55.     case 'R': return 12;
  56.     case 'M': return 13;
  57.     case 'Y': return 14;
  58.     case 'W': return 15;
  59.     }
  60.     return 15;
  61. }
  62. char col[16] = {"WWWWWWWWWBGCRMYW"};
  63.  
  64. int main() {
  65.     int flag=1, i, j, x1, y1, x2, y2;
  66.     char name[10], cmd, col;
  67.  
  68.     while(flag) {
  69.     init();
  70.     show();
  71.     scanf("%s", &cmd);
  72.  
  73.     switch(cmd) {
  74.     case 'I':
  75.         scanf("%d", &c);
  76.         scanf("%d", &r);
  77.         for(i=0; i<r; i++) {
  78.         memset(canvas[i],'W',c);
  79.         }
  80.         break;
  81.     case 'C':
  82.         for(i=0; i<r; i++) {
  83.         memset(canvas[i],'W',c);
  84.         }
  85.         break;
  86.     case 'L':
  87.         scanf("%d", &x1);
  88.         scanf("%d", &y1);
  89.         scanf("%1s", &col);
  90.         setf(col,x1, y1);
  91.         break;
  92.     case 'H':
  93.         scanf("%d", &x1);
  94.         scanf("%d", &x2);
  95.         scanf("%d", &y1);
  96.         scanf("%1s", &col);
  97.         for(i=x1; i<=x2; i++) {
  98.         setf(col,i,y1);
  99.         }
  100.         break;
  101.     case 'V':
  102.         scanf("%d", &y1);
  103.         scanf("%d", &x1);
  104.         scanf("%d", &x2);
  105.         scanf("%1s", &col);
  106.         for(i=x1; i<=x2; i++) {
  107.         setf(col,y1, i);
  108.         }
  109.         init();
  110.         show();
  111.         break;
  112.     case 'K':   //rect
  113.         scanf("%d", &x1);
  114.         scanf("%d", &y2);
  115.         scanf("%d", &x2);
  116.         scanf("%d", &y2);
  117.  
  118.         scanf("%1s", &col);
  119.         for(i=y1; i<=y2; i++) {
  120.         for(j=x1; j<=x2; j++) {
  121.             setf(col, i,j);
  122.         }
  123.         }
  124.         break;
  125.     case 'F':
  126.         scanf("%d", &x1);
  127.         scanf("%d", &y1);
  128.         scanf("%1s", &col);
  129.         paint(x1,y1,col, canvas[y1][x1]);
  130.         break;
  131.     case 'S':
  132.         scanf("%c", name);
  133.         printf("%s\n",name);
  134.         break;
  135.     case 'X':
  136.         flag = 0;
  137.         break;
  138.     default:
  139.         do{
  140.         scanf("%c",&col);
  141.         }while(col!='\n');
  142.     }//switch
  143.     }//while
  144.  
  145.     getch();
  146.     return 0;
  147. }
  148.  
  149. void init(){
  150.     int i;
  151.     clrscr();
  152.     for(i=9; i<16; i++) {
  153.     textcolor(i);
  154.     cprintf("%c%-3c", PIX,col[i]);
  155.     }
  156.     printf("\nI M N :Initialize a M by N canvas");
  157.     printf("\nC :clear");
  158.     printf("\nL X Y C: color (x,y) with col C");
  159.     printf("\nV X Y1 Y2 C : vertical line (x,y1)-(x,y2) col-C");
  160.     printf("\nH X1 X2 Y C: horiz line (x1,y)-(x2,y) col-C");
  161.     printf("\nK X1 X2 Y1 Y2 C :rectangle (x1,y1) (x2,y2) with color C");
  162.     printf("\nF X Y C : fill regin (x,y) with C");
  163.     printf("\nX : exit");
  164.     gotoxy(1,10);
  165. }
  166.  
  167. void show() {
  168.     int i,j;
  169.     printf("\nrange X[0-%d], Y[0-%d] \n",r-1, c-1);
  170.  
  171.     for(i=0; i<r;i++) {
  172.     for(j=0; j<c; j++) {
  173.         textcolor(tonum(canvas[i][j]));
  174.         cprintf("%c%c", PIX, PIX);
  175.     }
  176.     printf("\n");
  177.     }
  178. }
  179.  
  180. void setf(char cl, int x, int y){
  181.     canvas[y][x] = cl;
  182. }
  183.  
  184. void paint(int x, int y, char col, char bg) {
  185.     if(canvas[y][x]==bg) {
  186.     setf(col,x,y);
  187.     if(isValid(x+1,y)) paint(x+1,y,col,bg);
  188.     if(isValid(x-1,y)) paint(x-1,y,col,bg);
  189.     if(isValid(x,y+1)) paint(x,y+1,col,bg);
  190.     if(isValid(x,y-1)) paint(x,y-1,col,bg);
  191.     }
  192. }
  193.  
  194. int isValid(int x, int y) {
  195.     if( (x<0)||(y<0)||(x>=c)||(y>=r) ) return 0;
  196.     return 1;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement