Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-----
- /What's the secret of your perfect skin? asked a facebook friend of mine.
- I smiled, happy that my efforts have not gone waste and replied "Photoshop." :P
- We all love photoshop. It is, without a doubt a huge software.
- But basically all photo editors work in somewhat similar way.
- A bitmap editor (for eg MS Paint) makes a 2D array of the pixels and allows you to manipulate the pixels using a mouse.
- 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.
- Here's the code to a very basic version of it.
- 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. :)
- ------*/
- /* --------------------------
- //Shaha Hassan
- // 30 Aug 2014
- Graphical Editor
- I M N Create a new M ×N image with all pixels initially colored white (O).
- C Clear the table by setting all pixels white (O). The size remains unchanged.
- L X Y C Colors the pixel (X, Y ) in color (C).
- V X Y1 Y2 C Draw a vertical segment of color (C) in column X, between the rows Y 1 and Y 2 inclusive.
- H X Y1 Y2 C Draw a horizontal segment of color (C) in the row Y , between the columns X1 and X2 inclusive.
- K X1 Y1 X2 Y2 C Draw a filled rectangle of color C, where (X1, Y 1) is the
- upper-left and (X2, Y 2) the lower right corner.
- F X Y C Fill the region R with the color C, where R is defined as follows.
- Pixel (X, Y ) belongs to R. Any other pixel which is the same color as pixel (X, Y )
- and shares a common side with any pixel in R also belongs to this region.
- S Name Write the file name in MSDOS 8.3 format followed by the contents of the current image.
- X Terminate the session.
- -----------------------*/
- /* show() and init() function can be modified to make the code compilable for modern compilers */
- #include<stdio.h>
- #include<string.h>
- #include<conio.h>
- #define PIX 219
- void paint(int x, int y, char col, char bg);
- int isValid(int x, int y);
- void setf(char cl, int x, int y);
- void show();
- void init();
- char canvas[30][30];
- int r,c;
- int tonum(char c){
- switch(c){
- case 'B': return 9;
- case 'G': return 10;
- case 'C': return 11;
- case 'R': return 12;
- case 'M': return 13;
- case 'Y': return 14;
- case 'W': return 15;
- }
- return 15;
- }
- char col[16] = {"WWWWWWWWWBGCRMYW"};
- int main() {
- int flag=1, i, j, x1, y1, x2, y2;
- char name[10], cmd, col;
- while(flag) {
- init();
- show();
- scanf("%s", &cmd);
- switch(cmd) {
- case 'I':
- scanf("%d", &c);
- scanf("%d", &r);
- for(i=0; i<r; i++) {
- memset(canvas[i],'W',c);
- }
- break;
- case 'C':
- for(i=0; i<r; i++) {
- memset(canvas[i],'W',c);
- }
- break;
- case 'L':
- scanf("%d", &x1);
- scanf("%d", &y1);
- scanf("%1s", &col);
- setf(col,x1, y1);
- break;
- case 'H':
- scanf("%d", &x1);
- scanf("%d", &x2);
- scanf("%d", &y1);
- scanf("%1s", &col);
- for(i=x1; i<=x2; i++) {
- setf(col,i,y1);
- }
- break;
- case 'V':
- scanf("%d", &y1);
- scanf("%d", &x1);
- scanf("%d", &x2);
- scanf("%1s", &col);
- for(i=x1; i<=x2; i++) {
- setf(col,y1, i);
- }
- init();
- show();
- break;
- case 'K': //rect
- scanf("%d", &x1);
- scanf("%d", &y2);
- scanf("%d", &x2);
- scanf("%d", &y2);
- scanf("%1s", &col);
- for(i=y1; i<=y2; i++) {
- for(j=x1; j<=x2; j++) {
- setf(col, i,j);
- }
- }
- break;
- case 'F':
- scanf("%d", &x1);
- scanf("%d", &y1);
- scanf("%1s", &col);
- paint(x1,y1,col, canvas[y1][x1]);
- break;
- case 'S':
- scanf("%c", name);
- printf("%s\n",name);
- break;
- case 'X':
- flag = 0;
- break;
- default:
- do{
- scanf("%c",&col);
- }while(col!='\n');
- }//switch
- }//while
- getch();
- return 0;
- }
- void init(){
- int i;
- clrscr();
- for(i=9; i<16; i++) {
- textcolor(i);
- cprintf("%c%-3c", PIX,col[i]);
- }
- printf("\nI M N :Initialize a M by N canvas");
- printf("\nC :clear");
- printf("\nL X Y C: color (x,y) with col C");
- printf("\nV X Y1 Y2 C : vertical line (x,y1)-(x,y2) col-C");
- printf("\nH X1 X2 Y C: horiz line (x1,y)-(x2,y) col-C");
- printf("\nK X1 X2 Y1 Y2 C :rectangle (x1,y1) (x2,y2) with color C");
- printf("\nF X Y C : fill regin (x,y) with C");
- printf("\nX : exit");
- gotoxy(1,10);
- }
- void show() {
- int i,j;
- printf("\nrange X[0-%d], Y[0-%d] \n",r-1, c-1);
- for(i=0; i<r;i++) {
- for(j=0; j<c; j++) {
- textcolor(tonum(canvas[i][j]));
- cprintf("%c%c", PIX, PIX);
- }
- printf("\n");
- }
- }
- void setf(char cl, int x, int y){
- canvas[y][x] = cl;
- }
- void paint(int x, int y, char col, char bg) {
- if(canvas[y][x]==bg) {
- setf(col,x,y);
- if(isValid(x+1,y)) paint(x+1,y,col,bg);
- if(isValid(x-1,y)) paint(x-1,y,col,bg);
- if(isValid(x,y+1)) paint(x,y+1,col,bg);
- if(isValid(x,y-1)) paint(x,y-1,col,bg);
- }
- }
- int isValid(int x, int y) {
- if( (x<0)||(y<0)||(x>=c)||(y>=r) ) return 0;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement