7 2d bitmask with test and set operations
11 btm_new(int xsize,int ysize) {
15 btm=(struct i_bitmap*)mymalloc(sizeof(struct i_bitmap)); /* checked 4jul05 tonyc */
16 bytes = (xsize*ysize+8)/8;
17 if (bytes * 8 / ysize < xsize-1) { /* this is kind of rough */
18 fprintf(stderr, "Integer overflow allocating bitmap %d x %d", xsize, ysize);
21 btm->data=(char*)mymalloc(bytes); /* checked 4jul05 tonyc */
24 for(i=0;i<(xsize*ysize+8)/8;i++) btm->data[i]=0; /* Is this always needed */
30 btm_destroy(struct i_bitmap *btm) {
37 btm_test(struct i_bitmap *btm,int x,int y) {
39 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) return 0;
41 return (1<<(btno%8))&(btm->data[btno/8]);
45 btm_set(struct i_bitmap *btm,int x,int y) {
47 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) abort();
49 btm->data[btno/8]|=1<<(btno%8);
57 Bucketed linked list - stack type
61 llink_new(struct llink* p,int size) {
63 l = mymalloc(sizeof(struct llink)); /* checked 4jul05 tonyc */
67 l->data = mymalloc(size); /* checked 4jul05 tonyc - depends on caller to llist_push */
71 /* free's the data pointer, itself, and sets the previous' next pointer to null */
74 llink_destroy(struct llink* l) {
75 if (l->p != NULL) { l->p->n=NULL; }
81 /* if it returns true there wasn't room for the
85 llist_llink_push(struct llist *lst, struct llink *lnk,void *data) {
89 /* fprintf(stderr,"llist_llink_push: data=0x%08X -> 0x%08X\n",data,*(int*)data);
90 fprintf(stderr,"ssize = %d, multip = %d, fill = %d\n",lst->ssize,lst->multip,lnk->fill); */
91 if (lnk->fill == lst->multip) return 1;
92 /* memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize); */
93 memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize);
95 /* printf("data=%X res=%X\n",*(int*)data,*(int*)(lnk->data));*/
102 llist_new(int multip, int ssize) {
104 l = mymalloc(sizeof(struct llist)); /* checked 4jul05 tonyc */
114 llist_push(struct llist *l,void *data) {
115 int ssize = l->ssize;
116 int multip = l->multip;
118 /* fprintf(stderr,"llist_push: data=0x%08X\n",data);
119 fprintf(stderr,"Chain size: %d\n", l->count); */
122 l->t = l->h = llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */
123 /* fprintf(stderr,"Chain empty - extended\n"); */
125 else { /* Check for overflow in current tail */
126 if (l->t->fill >= l->multip) {
127 struct llink* nt = llink_new(l->t, ssize*multip);
130 /* fprintf(stderr,"Chain extended\n"); */
133 /* fprintf(stderr,"0x%08X\n",l->t); */
134 if (llist_llink_push(l,l->t,data)) {
135 i_fatal(3, "out of memory\n");
139 /* returns 0 if the list is empty */
142 llist_pop(struct llist *l,void *data) {
143 /* int ssize=l->ssize;
144 int multip=l->multip;*/
145 if (l->t == NULL) return 0;
148 memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize);
150 if (!l->t->fill) { /* This link empty */
151 if (l->t->p == NULL) { /* and it's the only link */
157 llink_destroy(l->t->n);
164 llist_dump(struct llist *l) {
170 for(j=0;j<lnk->fill;j++) {
171 /* memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
172 /*memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
173 printf("%d - %p\n",i,*(void **)((char *)(lnk->data)+l->ssize*j));
181 llist_destroy(struct llist *l) {
182 struct llink *t,*lnk = l->h;
183 while( lnk != NULL ) {
197 Oct-tree implementation
205 t=(struct octt*)mymalloc(sizeof(struct octt)); /* checked 4jul05 tonyc */
206 for(i=0;i<8;i++) t->t[i]=NULL;
212 /* returns 1 if the colors wasn't in the octtree already */
216 octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) {
223 /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */
226 ci=((!!(r&cm))<<2)+((!!(g&cm))<<1)+!!(b&cm);
227 /* printf("idx[%d]=%d\n",i,ci); */
228 if (c->t[ci] == NULL) {
234 c->cnt++; /* New. The only thing really needed (I think) */
240 octt_delete(struct octt *ct) {
242 for(i=0;i<8;i++) if (ct->t[i] != NULL) octt_delete(ct->t[i]); /* do not free instance here because it will free itself */
248 octt_dump(struct octt *ct) {
250 /* printf("node [0x%08X] -> (%d)\n",ct,ct->cnt); */
252 if (ct->t[i] != NULL)
253 printf("[ %d ] -> %p\n", i, (void *)ct->t[i]);
255 if (ct->t[i] != NULL)
259 /* note that all calls of octt_count are operating on the same overflow
260 variable so all calls will know at the same time if an overflow
261 has occured and stops there. */
264 octt_count(struct octt *ct,int *tot,int max,int *overflow) {
267 if (!(*overflow)) return;
268 for(i=0;i<8;i++) if (ct->t[i]!=NULL) {
269 octt_count(ct->t[i],tot,max,overflow);
273 if ( (*tot) > (*overflow) ) *overflow=0;
276 /* This whole function is new */
277 /* walk through the tree and for each colour, store its seen count in the
278 space pointed by *col_usage_it_adr */
280 octt_histo(struct octt *ct, unsigned int **col_usage_it_adr) {
283 for(i = 0; i < 8; i++)
284 if (ct->t[i] != NULL) {
285 octt_histo(ct->t[i], col_usage_it_adr);
289 *(*col_usage_it_adr)++ = ct->cnt;