floodfill.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef __FLOOD_FILL_H__
  2. #define __FLOOD_FILL_H__ 1
  3. // Floodfill algorithm for drawing module
  4. // Copyright (C) 2015 fgalliat (Xtase)
  5. //
  6. // This file is part of Duktape-nspire.
  7. //
  8. // Duktape-nspire is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // Duktape-nspire is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Lesser General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with Duktape-nspire. If not, see <http://www.gnu.org/licenses/>.
  20. /***************************************
  21. * Flood fill : non recursive algorithm *
  22. ***************************************/
  23. // later look at : http://www.codeproject.com/Articles/6017/QuickFill-An-efficient-flood-fill-algorithm
  24. typedef struct ffnode_ { void* next; int x, y; } ffnode_t;
  25. typedef struct queue_ { ffnode_t* head; ffnode_t* last; } queue_t;
  26. bool _isEmpty(queue_t* queue) {
  27. return queue->head == NULL;
  28. }
  29. void _add(queue_t* queue, ffnode_t* node) {
  30. if ( queue->head == NULL ) { queue->head = node; }
  31. if ( queue->last != NULL ) { queue->last->next = node; }
  32. queue->last = node;
  33. }
  34. ffnode_t* _remove(queue_t* queue) {
  35. if ( queue->head == NULL ) { return NULL; }
  36. ffnode_t* rem = queue->head;
  37. queue->head = queue->head->next;
  38. return rem;
  39. }
  40. queue_t* new_queue() {
  41. queue_t *queue = (queue_t*)malloc(sizeof(queue_t));
  42. queue->head = NULL;
  43. queue->last = NULL;
  44. return queue;
  45. }
  46. ffnode_t* new_ffnode(int x, int y) {
  47. ffnode_t *node = (ffnode_t*)malloc(sizeof(ffnode_t));
  48. node->x = x; node->y = y;
  49. node->next = NULL;
  50. return node;
  51. }
  52. // beware w/ memeroy hole
  53. void flood_fill(uint16_t* image, int imageWidth, int imageHeight, int startx, int starty, uint16_t color) {
  54. ffnode_t *node = NULL;
  55. ffnode_t *w = NULL;
  56. ffnode_t *e = NULL;
  57. ffnode_t *n = NULL;
  58. ffnode_t *s = NULL;
  59. queue_t* q = new_queue();
  60. node = new_ffnode(startx, starty);
  61. _add(q,node);
  62. while ( !_isEmpty(q) ) {
  63. node = _remove(q);
  64. if ( node->x >= 0 && node->x < imageWidth && node->y >= 0 && node->y < imageHeight ) {
  65. if (image[ (node->y*imageWidth)+node->x ] != color) {
  66. image[ (node->y*imageWidth)+node->x ] = color;
  67. e=new_ffnode( node->x+1, node->y );
  68. _add(q,e);
  69. w=new_ffnode( node->x-1, node->y );
  70. _add(q,w);
  71. s=new_ffnode( node->x, node->y+1 );
  72. _add(q,s);
  73. n=new_ffnode( node->x, node->y-1 );
  74. _add(q,n);
  75. }
  76. }
  77. //node = NULL;
  78. free(node);
  79. }
  80. //q = NULL;
  81. free(q);
  82. }
  83. #endif