Subversion Repositories Kolibri OS

Rev

Rev 7275 | Rev 7444 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #define MAX_CELL_SIZE 256
  2.  
  3. //////////////////////////////////////////////////////////////////////////////////////
  4. //                                                                                  //
  5. //                                  DRAW PIXEL                                      //
  6. //                                                                                  //
  7. //////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. //The 'draw[]' is the array which holds the states should we draw a pixel or not.
  10. //Is need to decrease redraw when using some tools like line, rectangle and selection.
  11.  
  12. struct _pixel_state
  13. {
  14.         unsigned image_rows, image_columns;
  15.         bool draw[MAX_CELL_SIZE*MAX_CELL_SIZE];
  16.         void set_drawable_state();
  17.         bool is_drawable();
  18.         void reset_and_set_all_drawable();
  19.         void set_sizes();
  20. } pixel_state;
  21.  
  22. void _pixel_state::set_drawable_state(int _r, _c, _state)
  23. {
  24.         draw[image_columns*_r + _c] = _state;
  25. }
  26.  
  27. bool _pixel_state::is_drawable(int _r, _c)
  28. {
  29.         return draw[image_columns*_r + _c];
  30. }
  31.  
  32. void _pixel_state::reset_and_set_all_drawable()
  33. {
  34.         int i;
  35.         for (i = 0; i < image_columns*image_rows; i++) draw[i]=true;
  36. }
  37.  
  38. void _pixel_state::set_sizes(dword _r, _c)
  39. {
  40.         image_rows = _r;
  41.         image_columns = _c;
  42. }
  43.  
  44. //////////////////////////////////////////////////////////////////////////////////////
  45. //                                                                                  //
  46. //                                      IMAGE                                       //
  47. //                                                                                  //
  48. //////////////////////////////////////////////////////////////////////////////////////
  49.  
  50. //This stucture determines basic actions that can be done with image (icon).
  51.  
  52. struct _image
  53. {
  54.         unsigned rows, columns;
  55.         dword mas[MAX_CELL_SIZE*MAX_CELL_SIZE];
  56.         dword img;
  57.         _pixel_state pixel_state;
  58.         void create();
  59.         void set_pixel();
  60.         void draw_line();
  61.         void fill();
  62.         void set_image();
  63.         dword get_pixel();
  64.         dword get_image();
  65.         dword get_image_with_replaced_color();
  66.         void move();
  67. };
  68.  
  69. void _image::create(int _rows, _columns)
  70. {
  71.         int i;
  72.         rows = _rows;
  73.         columns = _columns;
  74.         for (i = 0; i < columns*rows; i++) mas[i]=0xBFCAD2;
  75.         pixel_state.set_sizes(rows, columns);
  76. }
  77.  
  78. void _image::set_pixel(int _r, _c, _color)
  79. {
  80.         mas[columns*_r + _c] = _color;
  81. }
  82.  
  83. void _image::draw_line(int x1, int y1, int x2, int y2, dword color) {
  84.         int dx, dy, signX, signY, error, error2;
  85.  
  86.         dx = x2 - x1;
  87.  
  88.         if (dx < 0)
  89.                 dx = -dx;
  90.    
  91.         dy = y2 - y1;
  92.  
  93.         if (dy < 0)
  94.                 dy = -dy;
  95.    
  96.         if (x1 < x2)
  97.                 signX = 1;
  98.         else
  99.                 signX = -1;
  100.    
  101.         if (y1 < y2)
  102.                 signY = 1;
  103.         else
  104.                 signY = -1;
  105.    
  106.         error = dx - dy;
  107.  
  108.         set_pixel(y2, x2, color);
  109.  
  110.         while((x1 != x2) || (y1 != y2))
  111.         {
  112.                 set_pixel(y1, x1, color);
  113.                
  114.                 error2 = error * 2;
  115.  
  116.                 if(error2 > calc(-dy))
  117.                 {
  118.                         error -= dy;
  119.                         x1 += signX;
  120.                 }
  121.  
  122.                 if(error2 < dx)
  123.                 {
  124.                         error += dx;
  125.                         y1 += signY;
  126.                 }
  127.         }
  128. }
  129.  
  130. void _image::fill(int _r, _c, _color)
  131. {
  132.         #define MARKED 6
  133.         int r, c, i, restart;
  134.  
  135.         dword old_color = get_pixel(_r, _c);
  136.         set_pixel(_r, _c, MARKED);
  137.  
  138.         do {
  139.                 restart=false; 
  140.                 for (r = 0; r < rows; r++)
  141.                         for (c = 0; c < columns; c++)
  142.                         {
  143.                                 IF (get_pixel(r,c) != old_color) continue;
  144.                                 IF (get_pixel(r,c) == MARKED) continue;
  145.                                
  146.                                 IF (c>0)               && (get_pixel(r,c-1) == MARKED) set_pixel(r,c,MARKED);
  147.                                 IF (r>0)               && (get_pixel(r-1,c) == MARKED) set_pixel(r,c,MARKED);
  148.                                 IF (c<columns-1) && (get_pixel(r,c+1) == MARKED) set_pixel(r,c,MARKED);
  149.                                 IF (r<rows-1)    && (get_pixel(r+1,c) == MARKED) set_pixel(r,c,MARKED);
  150.                                
  151.                                 IF (get_pixel(r,c)==MARKED) restart=true;
  152.                         }
  153.         }while(restart);
  154.  
  155.         for (i=0; i<columns*rows; i++)
  156.                         IF (mas[i]==MARKED) mas[i] = _color;
  157. }
  158.  
  159. dword _image::get_pixel(int _r, _c)
  160. {
  161.         return mas[columns*_r + _c];
  162. }
  163.  
  164. void _image::set_image(dword _inbuf)
  165. {
  166.         dword i;
  167.         for (i = 0; i < columns*rows; i++;)
  168.         {
  169.                 // mas[i] = ESDWORD[i*4+_inbuf] & 0x00FFFFFF; //for x32 bit color
  170.                 mas[i] = ESDWORD[i*3+_inbuf] & 0xFFFFFF;
  171.         }
  172. }
  173.  
  174. dword _image::get_image()
  175. {
  176.         int r=0, c=0;
  177.         dword i;
  178.  
  179.         free(img);
  180.         i = img = malloc(rows*columns*3);
  181.  
  182.         for (r = 0; r < rows; r++)
  183.                 for (c = 0; c < columns; c++)
  184.                 {
  185.                         rgb.DwordToRgb(get_pixel(r,c));
  186.                         ESBYTE[i] = rgb.b;
  187.                         ESBYTE[i+1] = rgb.g;
  188.                         ESBYTE[i+2] = rgb.r;
  189.                         i += 3;
  190.                 }
  191.         return img;
  192. }
  193.  
  194. dword _image::get_image_with_replaced_color(dword _col_from, _col_to)
  195. {
  196.         int r=0, c=0;
  197.         dword i;
  198.         dword cur_pixel;
  199.  
  200.         free(img);
  201.         i = img = malloc(rows*columns*3);
  202.  
  203.         for (r = 0; r < rows; r++)
  204.                 for (c = 0; c < columns; c++)
  205.                 {
  206.                         cur_pixel = get_pixel(r,c);
  207.                         if (cur_pixel == _col_from) cur_pixel = _col_to;
  208.                         rgb.DwordToRgb(cur_pixel);
  209.                         ESBYTE[i] = rgb.b;
  210.                         ESBYTE[i+1] = rgb.g;
  211.                         ESBYTE[i+2] = rgb.r;
  212.                         i += 3;
  213.                 }
  214.         return img;
  215. }
  216.  
  217. enum {
  218.         MOVE_LEFT,
  219.         MOVE_RIGHT,
  220.         MOVE_UP,
  221.         MOVE_DOWN,
  222.         FLIP_VER,
  223.         FLIP_HOR,
  224.         ROTE
  225. };
  226. void _image::move(int _direction)
  227. {
  228.         int r, c;
  229.         dword first_element_data;
  230.  
  231.         switch(_direction)
  232.         {
  233.                 case MOVE_LEFT:
  234.                                 for (r = 0; r < rows; r++)
  235.                                 {
  236.                                         first_element_data = get_pixel(r, 0);
  237.                                         for (c = 0; c < columns-1; c++) set_pixel(r, c, get_pixel(r, c+1));
  238.                                         set_pixel(r, columns-1, first_element_data);
  239.                                 }
  240.                                 break;
  241.                 case MOVE_RIGHT:
  242.                                 for (r = 0; r < rows; r++)
  243.                                 {
  244.                                         first_element_data = get_pixel(r, columns-1);
  245.                                         for (c = columns-1; c > 0; c--) set_pixel(r, c, get_pixel(r, c-1));
  246.                                         set_pixel(r, 0, first_element_data);
  247.                                 }      
  248.                                 break; 
  249.                 case MOVE_UP:
  250.                                 for (c = 0; c < columns; c++)
  251.                                 {
  252.                                         first_element_data = get_pixel(0, c);
  253.                                         for (r = 0; r < rows-1; r++) set_pixel(r, c, get_pixel(r+1, c));
  254.                                         set_pixel(rows-1, c, first_element_data);
  255.                                 }      
  256.                                 break;
  257.                 case MOVE_DOWN:
  258.                                 for (c = 0; c < columns; c++)
  259.                                 {
  260.                                         first_element_data = get_pixel(rows-1, c);
  261.                                         for (r = rows-1; r > 0; r--) set_pixel(r, c, get_pixel(r-1, c));
  262.                                         set_pixel(0, c, first_element_data);
  263.                                 }
  264.                                 break;
  265.                 case FLIP_HOR:
  266.                                 for (r = 0; r < rows; r++)
  267.                                         for (c = 0; c < columns/2; c++) {
  268.                                                 first_element_data = get_pixel(r, c);
  269.                                                 set_pixel(r, c, get_pixel(r, columns-c-1));
  270.                                                 set_pixel(r, columns-c-1, first_element_data);
  271.                                         }
  272.                                 break;
  273.                 case FLIP_VER:
  274.                                 for (c = 0; c < columns; c++)
  275.                                         for (r = 0; r < rows/2; r++) {
  276.                                                 first_element_data = get_pixel(r, c);
  277.                                                 set_pixel(r, c, get_pixel(rows-r-1, c));
  278.                                                 set_pixel(rows-r-1, c, first_element_data);
  279.                                         }
  280.                                 break; 
  281.         }
  282. }
  283.  
  284.  
  285.  
  286.  
  287.