4 #define IMAGER_NO_CONTEXT
8 2d bitmask with test and set operations
12 btm_new(i_img_dim xsize,i_img_dim 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 (" i_DFp ")",
19 i_DFcp(xsize, ysize));
22 btm->data=(char*)mymalloc(bytes); /* checked 4jul05 tonyc */
25 memset(btm->data, 0, bytes);
31 btm_destroy(struct i_bitmap *btm) {
38 btm_test(struct i_bitmap *btm,i_img_dim x,i_img_dim y) {
40 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) return 0;
42 return (1<<(btno%8))&(btm->data[btno/8]);
46 btm_set(struct i_bitmap *btm,i_img_dim x,i_img_dim y) {
48 if (x<0 || x>btm->xsize-1 || y<0 || y>btm->ysize-1) abort();
50 btm->data[btno/8]|=1<<(btno%8);
58 Bucketed linked list - stack type
62 llink_new(struct llink* p,size_t size);
64 llist_llink_push(struct llist *lst, struct llink *lnk,const void *data);
66 llink_destroy(struct llink* l);
70 =synopsis struct llist *l = llist_new(100, sizeof(foo);
72 Create a new stack structure. Implemented as a linked list of pools.
80 multip - number of entries in each pool
84 ssize - size of the objects being pushed/popped
92 llist_new(int multip, size_t ssize) {
94 l = mymalloc(sizeof(struct llist)); /* checked 4jul05 tonyc */
105 =synopsis llist_push(l, &foo);
107 Push an item on the stack.
113 llist_push(struct llist *l,const void *data) {
114 size_t ssize = l->ssize;
115 int multip = l->multip;
117 /* fprintf(stderr,"llist_push: data=0x%08X\n",data);
118 fprintf(stderr,"Chain size: %d\n", l->count); */
121 l->t = l->h = llink_new(NULL,ssize*multip); /* Tail is empty - list is empty */
122 /* fprintf(stderr,"Chain empty - extended\n"); */
124 else { /* Check for overflow in current tail */
125 if (l->t->fill >= l->multip) {
126 struct llink* nt = llink_new(l->t, ssize*multip);
129 /* fprintf(stderr,"Chain extended\n"); */
132 /* fprintf(stderr,"0x%08X\n",l->t); */
133 if (llist_llink_push(l,l->t,data)) {
135 im_fatal(aIMCTX, 3, "out of memory\n");
142 Pop an item off the list, storing it at C<data> which must have enough room for an object of the size supplied to llist_new().
144 returns 0 if the list is empty
150 llist_pop(struct llist *l,void *data) {
151 /* int ssize=l->ssize;
152 int multip=l->multip;*/
153 if (l->t == NULL) return 0;
156 memcpy(data,(char*)(l->t->data)+l->ssize*l->t->fill,l->ssize);
158 if (!l->t->fill) { /* This link empty */
159 if (l->t->p == NULL) { /* and it's the only link */
165 llink_destroy(l->t->n);
172 llist_dump(struct llist *l) {
178 for(j=0;j<lnk->fill;j++) {
179 /* memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
180 /*memcpy(&k,(char*)(lnk->data)+l->ssize*j,sizeof(void*));*/
181 printf("%d - %p\n",i,*(void **)((char *)(lnk->data)+l->ssize*j));
189 =item llist_destroy()
191 Destroy a linked-list based stack.
197 llist_destroy(struct llist *l) {
198 struct llink *t,*lnk = l->h;
199 while( lnk != NULL ) {
209 static struct llink *
210 llink_new(struct llink* p,size_t size) {
212 l = mymalloc(sizeof(struct llink)); /* checked 4jul05 tonyc */
216 l->data = mymalloc(size); /* checked 4jul05 tonyc - depends on caller to llist_push */
220 /* free's the data pointer, itself, and sets the previous' next pointer to null */
223 llink_destroy(struct llink* l) {
224 if (l->p != NULL) { l->p->n=NULL; }
230 /* if it returns true there wasn't room for the
234 llist_llink_push(struct llist *lst, struct llink *lnk, const void *data) {
236 multip = lst->multip;
238 /* fprintf(stderr,"llist_llink_push: data=0x%08X -> 0x%08X\n",data,*(int*)data);
239 fprintf(stderr,"ssize = %d, multip = %d, fill = %d\n",lst->ssize,lst->multip,lnk->fill); */
240 if (lnk->fill == lst->multip) return 1;
241 /* memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize); */
242 memcpy((char*)(lnk->data)+lnk->fill*lst->ssize,data,lst->ssize);
244 /* printf("data=%X res=%X\n",*(int*)data,*(int*)(lnk->data));*/
251 Oct-tree implementation
259 t=(struct octt*)mymalloc(sizeof(struct octt)); /* checked 4jul05 tonyc */
260 for(i=0;i<8;i++) t->t[i]=NULL;
266 /* returns 1 if the colors wasn't in the octtree already */
270 octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b) {
277 /* printf("[r,g,b]=[%d,%d,%d]\n",r,g,b); */
280 ci=((!!(r&cm))<<2)+((!!(g&cm))<<1)+!!(b&cm);
281 /* printf("idx[%d]=%d\n",i,ci); */
282 if (c->t[ci] == NULL) {
288 c->cnt++; /* New. The only thing really needed (I think) */
294 octt_delete(struct octt *ct) {
296 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 */
302 octt_dump(struct octt *ct) {
304 /* printf("node [0x%08X] -> (%d)\n",ct,ct->cnt); */
306 if (ct->t[i] != NULL)
307 printf("[ %d ] -> %p\n", i, (void *)ct->t[i]);
309 if (ct->t[i] != NULL)
313 /* note that all calls of octt_count are operating on the same overflow
314 variable so all calls will know at the same time if an overflow
315 has occured and stops there. */
318 octt_count(struct octt *ct,int *tot,int max,int *overflow) {
321 if (!(*overflow)) return;
322 for(i=0;i<8;i++) if (ct->t[i]!=NULL) {
323 octt_count(ct->t[i],tot,max,overflow);
327 if ( (*tot) > (*overflow) ) *overflow=0;
330 /* This whole function is new */
331 /* walk through the tree and for each colour, store its seen count in the
332 space pointed by *col_usage_it_adr */
334 octt_histo(struct octt *ct, unsigned int **col_usage_it_adr) {
337 for(i = 0; i < 8; i++)
338 if (ct->t[i] != NULL) {
339 octt_histo(ct->t[i], col_usage_it_adr);
343 *(*col_usage_it_adr)++ = ct->cnt;
350 return x < 0 ? -x : x;