Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/fill/eggfill.c
blob: 5de388d6c5fea067d7371d5e4d8c60fca888b4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
eggfill.c

Fill function and queue list


Copyright 2007, NATE-LSI-EPUSP

Oficina is developed in Brazil at Escola Politécnica of 
Universidade de São Paulo. NATE is part of LSI (Integrable
Systems Laboratory) and stands for Learning, Work and Entertainment
Research Group. Visit our web page: 
www.lsi.usp.br/nate
Suggestions, bugs and doubts, please email oficina@lsi.usp.br

Oficina is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation version 2 of 
the License.

Oficina is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public
License along with Oficina; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
Boston, MA  02110-1301  USA.
The copy of the GNU General Public License is found in the 
COPYING file included in the source distribution.


Authors:

Joyce Alessandra Saul               (joycealess@gmail.com)
Andre Mossinato                     (andremossinato@gmail.com)
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)
Pedro Kayatt                        (pekayatt@gmail.com)
Rafael Barbolo Lopes                (barbolo@gmail.com)
Alexandre A. Gonçalves Martinazzo   (alexandremartinazzo@gmail.com)

Colaborators:
Bruno Gola                          (brunogola@gmail.com)

Group Manager:
Irene Karaguilla Ficheman           (irene@lsi.usp.br)

Cientific Coordinator:
Roseli de Deus Lopes                (roseli@lsi.usp.br)

*/

#include <gtk/gtk.h>
#include "eggfill.h"

#define front(q)   ( (q)->front )
#define rear(q)    ( (q)->rear )

/* this queue has a Header that points to the Front and Rear elements */
/* empty queue: q->front = NULL and q->rear = NULL                      */

/* check if queue q is empty */
int queue_is_empty(queue *q){
	return ((front(q)==NULL) && (rear(q)==NULL));
}

queue *queue_init(void){
    queue *q;
    q = (queue*)malloc(sizeof(queue));
    /* check if there is enough space */
    if( q == NULL )
        printf( "Out of space!!!" );
    q->front = NULL;
    q->rear = NULL;
    
    return q;
}

void queue_destroy(queue *q){
	queue_make_empty(q);
	free(q);
}

void queue_make_empty(queue *q){
    if( q == NULL )
    	printf( "Must use CreateQueue first" );
    else
        while( !queue_is_empty(q) )
            queue_dequeue(q);
}


void queue_enqueue(int element, queue *q){
    no *tmp;
    tmp = (no*)malloc(sizeof(no));
    if(tmp == NULL) {
        printf("Out of space!!!");
        return;
    } else {
        tmp->info = element;
	tmp->next = NULL;
	if (queue_is_empty(q)){
	    q->front = tmp;
	} else { 
	    q->rear->next = tmp;
	}
	q->rear = tmp;
    }
}

void queue_dequeue(queue *q){
    if(queue_is_empty(q)) {
        printf( "Empty queue" );
    }
    else {
        no *tmp = q->front;
        q->front = q->front->next;
        if (q->front==NULL) {
		    q->rear=NULL;
        }
        free(tmp);
    }
}/* end of queue*/

void fill(GdkDrawable *drawable, GdkGC *gc, int x, int y, int width, int height, int color){

    printf("Entrando no fill\n");
    int color_start;
    queue *lista_xy;
    
    lista_xy = queue_init();

    GdkImage *image;
    image = gdk_drawable_get_image(drawable,0,0,width,height);
    printf("0x%x\n", image);
    
    color_start = gdk_image_get_pixel(image, x, y);
   
    if (color!=color_start) {
        queue_enqueue(x, lista_xy);
	queue_enqueue(y, lista_xy);
        gdk_image_put_pixel(image, x, y, color);
        while (!queue_is_empty(lista_xy)) {
            if (x+1 < width){
                if (gdk_image_get_pixel(image, x+1, y) == color_start){
                    gdk_image_put_pixel(image, x+1, y, color);
                    queue_enqueue(x+1, lista_xy);
                    queue_enqueue(y, lista_xy);
                }
	    }
            
            if (x-1 >= 0){
                if (gdk_image_get_pixel(image, x-1, y) == color_start){
                    gdk_image_put_pixel(image, x-1, y, color);
                    queue_enqueue(x-1, lista_xy);
                    queue_enqueue(y, lista_xy);
                }
            }
            if (y+1 < height){
                if (gdk_image_get_pixel(image, x, y+1) == color_start){
                    gdk_image_put_pixel(image, x, y+1, color);
                    queue_enqueue(x, lista_xy);
                    queue_enqueue(y+1, lista_xy);
                }
            }
            if (y-1 >= 0){
                if (gdk_image_get_pixel(image, x, y-1) == color_start){
                    gdk_image_put_pixel(image, x, y-1, color);
                    queue_enqueue(x, lista_xy);
                    queue_enqueue(y-1, lista_xy);
                }
            }
            x = lista_xy->front->info;
            queue_dequeue(lista_xy);
            y = lista_xy->front->info;
            queue_dequeue(lista_xy);
       }
   }
   gdk_draw_image(drawable, gc, image, 0,0,0,0,width,height);
   if (image != NULL) {
	   g_object_unref(image);
	   printf("Imagem %x\n", image);
   } else {
	   printf("Image = null\n");
   }
   queue_destroy(lista_xy);
}