Beispiel #1
0
static int nsplit_partial_groups(void)
{
	struct hostgroup *hg;
	struct servicegroup *sg;

	for (hg = hostgroup_list; hg; hg = hg->next) {
		struct hostsmember *hm, *prev = NULL, *next;
		int removed = 0;

		if (bitmap_isset(map.hostgroups, hg->id)) {
			continue;
		}
		for (hm = hg->members; hm; hm = next) {
			next = hm->next;
			if (bitmap_isset(map.hosts, hm->host_ptr->id)) {
				prev = hm;
				continue;
			}
			/* not a tracked host. Remove it */
			removed++;
			if (prev)
				prev->next = next;
			else
				hg->members = next;
			free(hm);
		}
		if (hg->members) {
			if (removed)
				partial.hostgroups++;
			else
				cached.hostgroups++;
			fcache_hostgroup(fp, hg);
		}
	}

	for (sg = servicegroup_list; sg; sg = sg->next) {
		struct servicesmember *sm, *prev = NULL, *next;
		int removed = 0;
		for (sm = sg->members; sm; sm = next) {
			next = sm->next;
			if (bitmap_isset(map.hosts, sm->service_ptr->host_ptr->id)) {
				prev = sm;
				continue;
			}
			if (prev)
				prev->next = next;
			else
				sg->members = next;
			free(sm);
		}
		if (sg->members) {
			if (removed)
				partial.servicegroups++;
			else
				cached.servicegroups++;
			fcache_servicegroup(fp, sg);
		}
	}
	return 0;
}
Beispiel #2
0
int main(int argc, const char *argv[]) {
        _cleanup_bitmap_free_ Bitmap *b = NULL;
        Iterator it;
        unsigned n = (unsigned) -1, i = 0;

        b = bitmap_new();
        assert_se(b);

        assert_se(bitmap_ensure_allocated(&b) == 0);
        bitmap_free(b);
        b = NULL;
        assert_se(bitmap_ensure_allocated(&b) == 0);

        assert_se(bitmap_isset(b, 0) == false);
        assert_se(bitmap_isset(b, 1) == false);
        assert_se(bitmap_isset(b, 256) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 0) == 0);
        assert_se(bitmap_isset(b, 0) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 0);
        assert_se(bitmap_isset(b, 0) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 1) == 0);
        assert_se(bitmap_isset(b, 1) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 1);
        assert_se(bitmap_isset(b, 1) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 256) == 0);
        assert_se(bitmap_isset(b, 256) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 256);
        assert_se(bitmap_isset(b, 256) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 32) == 0);
        bitmap_unset(b, 0);
        assert_se(bitmap_isset(b, 32) == true);
        bitmap_unset(b, 32);

        BITMAP_FOREACH(n, NULL, it)
                assert_not_reached("NULL bitmap");

        assert_se(bitmap_set(b, 0) == 0);
        assert_se(bitmap_set(b, 1) == 0);
        assert_se(bitmap_set(b, 256) == 0);

        BITMAP_FOREACH(n, b, it) {
                assert_se(n == i);
                if (i == 0)
                        i = 1;
                else if (i == 1)
                        i = 256;
                else if (i == 256)
                        i = (unsigned) -1;
        }
Beispiel #3
0
bool
file_page_isalloc(struct file *f, page_t page)
{
    assert(f != NULL);
    assert(f->f_page_bitmap != NULL);
    return bitmap_isset(f->f_page_bitmap, page);
}
Beispiel #4
0
int
file_alloc_page(struct file *f, page_t *retpage)
{
    page_t page;
    unsigned nbits = bitmap_nbits(f->f_page_bitmap);
    for (page = f->f_last_alloc_page + 1; page < nbits; page++) {
        if (!bitmap_isset(f->f_page_bitmap, page)) {
            bitmap_mark(f->f_page_bitmap, page);
            //assert(file_sync_bitmap(f) == 0);
            break;
        }
    }
    if (page == nbits) {
        // no more space in bitmap
        fprintf(stderr, "no more space in bitmap\n");
        return DBENOMEM;
    }
    // if we do get a valid page number, extend the file if necessary
    if (page * PAGESIZE >= f->f_size) {
        int result = ftruncate(f->f_fd, f->f_size + PAGESIZE);
        if (result == -1) {
            perror("ftruncate");
            return result;
        }
        f->f_size += PAGESIZE;
        f->f_last_alloc_page = page;
    }
    *retpage = page;
    return 0;
}
Beispiel #5
0
void buddy_free(buddy_t *bd, uint64_t addr, unsigned sz)
{
    uint64_t offs   = addr - bd->start;
    unsigned log_sz = log2_roundup(sz);
    unsigned idx    = offs >> log_sz;

    while (log_sz >= MIN_BUDDY_SZ_LOG2) {
        int order_idx = log_sz - MIN_BUDDY_SZ_LOG2;
        // Mark node free
        bitmap_set(&bd->orders[order_idx], idx);
        // Can we coalese up another level?
        if (log_sz == MAX_BUDDY_SZ_LOG2) {
            break;
        }
        if (bitmap_isset(&bd->orders[order_idx], BUDDY(idx)) == 0) {
            // guess not...
            break;
        }
        // Mark both non-free
        bitmap_clear(&bd->orders[order_idx], idx);
        bitmap_clear(&bd->orders[order_idx], BUDDY(idx));

        // Move up an order
        idx = DEC_ORDER(idx);
        log_sz++;
    }
}
Beispiel #6
0
/**
 * Render the bitmap as an OpenGL matrix.
 */
void
bitmap_render(struct bitmap * b)
{	
	int row_i, col_i, x, y;
	
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0, 1.0, 0);

	for (row_i = 0; row_i < b->rows; row_i++)
	{
		for (col_i = 0; col_i < b->cols; col_i++)
		{
			x = (float)col_i * 5 + 2.5;
			y = (float)row_i * 5 + 2.5;
			
			if (bitmap_isset(b, col_i, row_i))
			{
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	glFlush();
}
Beispiel #7
0
/** Freeing is actually easier, as we never have to worry about
    splitting blocks.

    Note that this function can only free addresses and sizes that are
    correctly aligned, so it's only really safe to call this with addresses
    returned from buddy_alloc(). 

    We simply mark the incoming block as free, then while we are not
    at the top level of the tree, see if the buddy is also free. If so,
    we mark them both as unavailable and move up the tree one level. { */
void buddy_free(buddy_t *bd, uint64_t addr, unsigned sz) {
  uint64_t offs = addr - bd->start;
  unsigned log_sz = log2_roundup(sz);
  unsigned idx = offs >> log_sz;

  while (log_sz >= MIN_BUDDY_SZ_LOG2) {
    int order_idx = log_sz - MIN_BUDDY_SZ_LOG2;

    /* Mark this node free. */
    bitmap_set(&bd->orders[order_idx], idx);

    /* Can we coalesce up another level? */
    if (log_sz == MAX_BUDDY_SZ_LOG2)
      break;

    /* Is this node's buddy also free? */
    if (bitmap_isset(&bd->orders[order_idx], BUDDY(idx)) == 0)
      /* no :( */
      break;

    /* FIXME: Ensure max(this, buddy) wouldn't go over the max extent
       of the region. */

    /* Mark them both non free. */
    bitmap_clear(&bd->orders[order_idx], idx);
    bitmap_clear(&bd->orders[order_idx], BUDDY(idx));

    /* Move up an order. */
    idx = DEC_ORDER(idx);
    ++log_sz;
  }

}
Beispiel #8
0
/*
	destroy all pages in physical memory and all swapped pages
*/
void
as_destroy(struct addrspace *as)
{
	int spl = splhigh();
	int i = 0;
	//free all coremap entries
	for (; i < num_frames; i++) {
		if(coremap[i].state != FREE && coremap[i].addrspace == as){
			coremap[i].addrspace = NULL;
			coremap[i].mapped_vaddr = 0;
			coremap[i].state = FREE;
			coremap[i].num_pages_allocated = 0;
		}
	}
	// free all pages in the swap file
	for(; i < array_getnum(as->as_regions); i++) {
		struct as_region* cur = (struct as_region*)array_getguy(as->as_regions, i);
		assert(cur->vbase % PAGE_SIZE == 0);
		// destroy all pages belonging to this region
		int j = 0;
		for (; j < cur->npages; j++) {
			vaddr_t page = cur->vbase + j * PAGE_SIZE;
			assert((page & PAGE_FRAME) == page);
			u_int32_t *pte = get_PTE_from_addrspace(as, page);
			if (((*pte & PTE_PRESENT) == 0) && ((*pte | PTE_SWAPPED) != 0)) {
				// if this page is in swap file...
				off_t file_slot = (*pte & SWAPFILE_OFFSET) >> 12;
				// the occupied bit must be set
				assert(bitmap_isset(swapfile_map, file_slot) != 0);
				bitmap_unmark(swapfile_map, file_slot);
			}
		}
	}
Beispiel #9
0
static int ToMem(u_int32_t flatloc, vaddr_t vaddr)
{
    assert(lock_do_i_hold(&vmlock));
    assert((vaddr & 0xfffff000) == vaddr);
    assert((flatloc & 0xfffff000) == flatloc);
    
    struct uio ku;
    int result;
    u_int32_t bitmaploc, diskloc, diskindex;
    bitmaploc = flatloc/PAGE_SIZE;
    
    assert (bitmap_isset(diskmap, bitmaploc));
    
    diskindex = flatloc / DISKSPACE;
    diskloc = flatloc - diskindex * DISKSPACE;
    
    mk_kuio(&ku, (void*)vaddr , PAGE_SIZE, diskloc, UIO_READ);
    result = VOP_READ(disk[diskindex], &ku);
    if (result) {
        panic(strerror(result));
    }
    
    bitmap_unmark(diskmap, bitmaploc);
    return result;
}
Beispiel #10
0
/*
 * Add an inverse entry for the swapped out page associated with mapping from 
 * vaddr to chunk into the swaparea.
 */
int add_spage (u_int32_t vaddr, u_int32_t chunk, pid_t pid)
{
    int result = 0;
        
    //get the index of the chunk in the swap area
    int chunk_index = (chunk & PAGE_FRAME) / PAGE_SIZE;
    //make sure that the chunk address is valid
    assert( (swaparea[ chunk_index ].paddr & PAGE_FRAME) == chunk );
    if (pid == 0)
		panic("PID = 0 in add_spage!");
    /*
     * Insert a mapping (invert index) of the page addresses by vaddr into the
     * swap area mapping indexed by the chunk
     */
    int spl=splhigh();
    swaparea[ chunk_index ].vaddr = vaddr;
    swaparea[ chunk_index ].last_access_time_sec = 0;
    swaparea[ chunk_index ].last_access_time_nsec = 0;
    //swaparea[ chunk_index ].pid = curthread->pid;
    swaparea[ chunk_index ].pid = pid; // changed by tocurtis

    /*
     * mark (as non-empty) the bitmap describing the swap area chunk
     */
    if(!bitmap_isset(swap_memmap, chunk_index))
        bitmap_mark(swap_memmap, chunk_index);
    
    splx(spl);    

    return result;
}
Beispiel #11
0
static inline void nsplit_cache_timeperiod(struct timeperiod *tp)
{
	if (tp && !bitmap_isset(map.timeperiods, tp->id)) {
		bitmap_set(map.timeperiods, tp->id);
		cached.timeperiods++;
		fcache_timeperiod(fp, tp);
	}
}
Beispiel #12
0
int bitmap_ts_isset(struct bitmap_ts* b, unsigned index) {
    assert(b != NULL);

    lock_acquire(b->lk);
    int ret = bitmap_isset(b->bm, index);
    lock_release(b->lk);
    return ret;
}
Beispiel #13
0
static inline void nsplit_cache_command(struct command *cmd)
{
	if (!cmd || bitmap_isset(map.commands, cmd->id))
		return;

	cached.commands++;
	fcache_command(fp, cmd);
	bitmap_set(map.commands, cmd->id);
}
Beispiel #14
0
int bitmap_ts_isset_blocking(struct bitmap_ts* b, unsigned index) {
    assert(b != NULL);

    lock_acquire(b->lk);
    while (bitmap_isset(b->bm, index) == 1)
        cv_wait(b->cv, b->lk);
    lock_release(b->lk);
    return 0;
}
Beispiel #15
0
void
file_free_page(struct file *f, page_t page)
{
    assert(f != NULL);
    assert(f->f_page_bitmap != NULL);
    assert(bitmap_isset(f->f_page_bitmap, page));
    bitmap_unmark(f->f_page_bitmap, page);
    //assert(file_sync_bitmap(f) == 0);
}
Beispiel #16
0
/*
 * Check if a block is in use.
 */
int
sfs_bused(struct sfs_fs *sfs, daddr_t diskblock)
{
	if (diskblock >= sfs->sfs_sb.sb_nblocks) {
		panic("sfs: sfs_bused called on out of range block %u\n",
		      diskblock);
	}
	return bitmap_isset(sfs->sfs_freemap, diskblock);
}
Beispiel #17
0
/*
 * Check if a block is in use.
 */
static
int
sfs_bused(struct sfs_fs *sfs, uint32_t diskblock)
{
	if (diskblock >= sfs->sfs_super.sp_nblocks) {
		panic("sfs: sfs_bused called on out of range block %u\n", 
		      diskblock);
	}
	return bitmap_isset(sfs->sfs_freemap, diskblock);
}
Beispiel #18
0
/*
 * Check if a block is in use.
 */
static
int
sfs_bused(struct sfs_fs *sfs, u_int32_t diskblock)
{
	/* Don't think we need to synchronize this */
	if (diskblock >= sfs->sfs_super.sp_nblocks) {
		panic("sfs: sfs_bused called on out of range block %u\n", 
		      diskblock);
	}
	return bitmap_isset(sfs->sfs_freemap, diskblock);
}
Beispiel #19
0
static void nsplit_cache_contactgroups(contactgroupsmember *cm)
{
	for (; cm; cm = cm->next) {
		struct contactgroup *cg = cm->group_ptr;
		if (bitmap_isset(map.contactgroups, cg->id))
			continue;
		cached.contactgroups++;
		nsplit_cache_contacts(cg->members);
		bitmap_set(map.contactgroups, cg->id);
		fcache_contactgroup(fp, cg);
	}
}
Beispiel #20
0
static inline void nsplit_cache_servicedependencies(objectlist *olist)
{
	objectlist *list;
	for (list = olist; list; list = list->next) {
		struct servicedependency *dep = (struct servicedependency *)list->object_ptr;
		if (!bitmap_isset(map.hosts, dep->master_service_ptr->host_ptr->id))
			continue;
		cached.servicedependencies++;
		nsplit_cache_timeperiod(dep->dependency_period_ptr);
		fcache_servicedependency(fp, dep);
	}
}
Beispiel #21
0
int
bitmap_nr_allocated(struct bitmap *b)
{
	u_int32_t i;
	int nr = 0;

	for (i = 0; i < b->nbits; i++) {
		if (bitmap_isset(b, i))
			nr++;
	}
	return nr;
}
Beispiel #22
0
static inline void nsplit_cache_hostdependencies(objectlist *olist)
{
	objectlist *list;

	for (list = olist; list; list = list->next) {
		struct hostdependency *dep = (struct hostdependency *)list->object_ptr;
		if (bitmap_isset(map.hosts, dep->master_host_ptr->id)) {
			nsplit_cache_timeperiod(dep->dependency_period_ptr);
			cached.hostdependencies++;
			fcache_hostdependency(fp, dep);
		}
	}
}
Beispiel #23
0
static int DiskClear(u_int32_t flatloc)
{
    assert(lock_do_i_hold(&vmlock));
    assert((flatloc & 0xfffff000) == flatloc);
    
    u_int32_t bitmaploc;
    bitmaploc = flatloc/PAGE_SIZE;
    
    assert (bitmap_isset(diskmap, bitmaploc));
    
    bitmap_unmark(diskmap, bitmaploc);
    return 0;
}
Beispiel #24
0
int
file_write(struct file *f, page_t page, void *buf) {
    assert(f != NULL);
    assert(buf != NULL);
    assert(f->f_page_bitmap != NULL);
    assert(bitmap_isset(f->f_page_bitmap, page));

    int result = pwrite(f->f_fd, buf, PAGESIZE, page * PAGESIZE);
    if (result == -1 || result == 0) {
        return DBEIOCHECKERRNO;
    }
    assert(result == PAGESIZE);
    return 0;
}
Beispiel #25
0
/*
 * swap_io: Does one swap I/O. Panics on failure.
 *
 * Synchronization: none specifically. The physical page should be
 * marked "pinned" (locked) so it won't be touched by other people.
 */
static
void
swap_io(paddr_t pa, off_t swapaddr, enum uio_rw rw)
{
	struct iovec iov;
	struct uio u;
	vaddr_t va;
	int result;

	KASSERT(lock_do_i_hold(global_paging_lock));

	KASSERT(pa != INVALID_PADDR);
	KASSERT(swapaddr % PAGE_SIZE == 0);
	KASSERT(coremap_pageispinned(pa));
	KASSERT(bitmap_isset(swapmap, swapaddr / PAGE_SIZE));

	va = coremap_map_swap_page(pa);

	uio_kinit(&iov, &u, (char *)va, PAGE_SIZE, swapaddr, rw);
	if (rw==UIO_READ) {
		result = VOP_READ(swapstore, &u);
	}
	else {
		result = VOP_WRITE(swapstore, &u);
	}

	coremap_unmap_swap_page(va, pa);

	if (result==EIO) {
		panic("swap: EIO on swapfile (offset %ld)\n",
		      (long)swapaddr);
	}
	else if (result==EINVAL) {
		panic("swap: EINVAL from swapfile (offset %ld)\n",
		      (long)swapaddr);
	}
	else if (result) {
		panic("swap: Error %d from swapfile (offset %ld)\n",
		      result, (long)swapaddr);
	}
}
Beispiel #26
0
/*
 * swap_free: marks a page in the swapfile as unused.
 *
 * Synchronization: uses swaplock.
 */
void
swap_free(off_t swapaddr)
{
	uint32_t index;

	KASSERT(swapaddr != INVALID_SWAPADDR);
	KASSERT(swapaddr % PAGE_SIZE == 0);

	index = swapaddr / PAGE_SIZE;

	lock_acquire(swaplock);

	KASSERT(swap_free_pages < swap_total_pages);
	KASSERT(swap_reserved_pages <= swap_free_pages);

	KASSERT(bitmap_isset(swapmap, index));
	bitmap_unmark(swapmap, index);
	swap_free_pages++;

	lock_release(swaplock);
}
Beispiel #27
0
static void nsplit_cache_contacts(struct contactsmember *cm_list)
{
	struct contactsmember *cm;

	for (cm = cm_list; cm; cm = cm->next) {
		struct commandsmember *cmdm;
		struct contact *c = cm->contact_ptr;

		if (bitmap_isset(map.contacts, c->id))
			continue;
		nsplit_cache_timeperiod(c->host_notification_period_ptr);
		nsplit_cache_timeperiod(c->service_notification_period_ptr);
		for (cmdm = c->host_notification_commands; cmdm; cmdm = cmdm->next) {
			nsplit_cache_command(cmdm->command_ptr);
		}
		for (cmdm = c->service_notification_commands; cmdm; cmdm = cmdm->next) {
			nsplit_cache_command(cmdm->command_ptr);
		}
		bitmap_set(map.contacts, c->id);
		cached.contacts++;
		fcache_contact(fp, c);
	}
}
Beispiel #28
0
/*
 * Add an inverse entry for the physical page associated with mapping from vaddr 
 * to paddr into the coremap. Inverse mapping means the page is indexed by page
 * number calculated using paddr.
 */
int add_ppage (u_int32_t vaddr, u_int32_t paddr, pid_t pid, u_int32_t status)
{
    int result = 0;
    
    int spl=splhigh();    
    paddr = paddr & PAGE_FRAME;
    
    //get the index of the page in the page table
    int page_index = (paddr - coremap_base) / PAGE_SIZE;
    //make sure that the paddr address is valid
    assert( (coremap[ page_index ].paddr & PAGE_FRAME) == paddr );
    
    /*
     * Add the mapping and set the attribute bits
     */
    coremap[ page_index ].vaddr = vaddr;
    //If it is a kernel address allocated by kernel then set kernel attribute flag
    if(vaddr > USERTOP)
        coremap[ page_index ].paddr = SET_VALID(paddr)|SET_DIRTY(paddr)|SET_KERNEL(paddr);
    else
        coremap[ page_index ].paddr = SET_VALID(paddr)|SET_DIRTY(paddr);
    
    /*Initialize _PTE fields for this entry*/
    coremap[ page_index ].last_access_time_sec = 0;
    coremap[ page_index ].last_access_time_nsec = 0;
    coremap[ page_index ].pid = pid;
    coremap[ page_index ].status = PAGE_DIRTY;
    
    /*
     * mark (unavailable) the page entry of coremap.
     */
    if(!bitmap_isset(core_memmap,page_index))
        bitmap_mark(core_memmap, page_index);    
    splx(spl);
        
    return result;
}
Beispiel #29
0
int tricam_process(int argc, char *argv[])
{
  int i, j, w, h, bpp;
  bytemap_t *up_image, *mid_image, *dn_image;
  bytemap_t *gray, *se;
  bitmap_t *up_roi, *mid_roi, *dn_roi;
  bitmap_t *bin, *roi, *tmp;
  char *fn, *ptr;
  real_t value;
  FILE *fd;
  label_info_t *label_info;
  dwordmap_t *labelmap;
  point_t *centroid;
  int label, area;
  uint8_t vmin, vmax;
  real_t vmean;

  bytemap_t *op1, *op2, *op3;
  char *path;
  int next_option;
  char *token1, *token2, *token3;
  real_t cutup = -2550000, cutdown = -4748;

  program_name = argv[0];
  do {
    next_option = getopt_long(argc, argv, short_options, long_options, NULL);
    switch (next_option) {
    case 'h': print_usage(stdout, 0); break;
    case 'o': token1 = strdup(optarg); break;
    case 's': token2 = strdup(optarg); break;
    case 'd': token3 = strdup(optarg); break;
    case 'f': cutup = strtod(optarg, NULL); break;
    case 't': cutdown = strtod(optarg, NULL); break;
    case 'v': verbose = 1; break;
    case 'n': path = strdup(optarg); break;
    case '?': print_usage(stderr, 1); break;
    case -1: break;
    default: abort();
    }
  } while (next_option != -1);

  //  printf("(%s - %s) / %s, %lf, %lf, %s\n", token1, token2, token3, cutup, cutdown, path);

  w = WIDTH; h = HEIGHT; bpp = BPP;

  initialize_screen(w, h, bpp);

  up_image = bytemap_new(w, h);
  mid_image = bytemap_new(w, h);
  dn_image = bytemap_new(w, h);

  gray = bytemap_new(w, h);

  up_roi = bitmap_new(w, h);
  mid_roi = bitmap_new(w, h);
  dn_roi = bitmap_new(w, h);

  bin = bitmap_new(w, h);
  tmp = bitmap_new(w, h);
  roi = bitmap_new(w, h);

  se = bytemap_new(3, 3);
  bytemap_fill(se, 0, 0, 3, 3, 1);

  labelmap = dwordmap_new(w, h);

  path = (char *)malloc(256 * sizeof(char));

  // Up display image ////////////////////
  strcpy(path, argv[1]);
  strcat(path, "_up.bmp");
  fn = strrchr(path, '/') + 1;
  printf("Filename: %s\n", fn);
  printf("Display image\n");
  load_and_display_BMP(path);
  read_bytemap_in_screen(up_image, NULL, NULL);
  bytemap_clear(gray);
  bytemap_copy(gray, UP_DX, UP_DY, up_image, 0, 0, w, h);
  write_bytemap_to_screen(gray, gray, gray);
  wait_keyhit();

  printf("Mean filtering \n");
  mean_smooth(up_image, gray, 3);
  write_bytemap_to_screen(up_image, up_image, up_image);
  wait_keyhit();
  ////////////////////////////////////////

  // MID display image ///////////////////
  strcpy(path, argv[1]);
  strcat(path, "_mid.bmp");
  fn = strrchr(path, '/') + 1;
  printf("Filename: %s\n", fn);
  printf("Display image\n");
  load_and_display_BMP(path);
  read_bytemap_in_screen(mid_image, NULL, NULL);
  bytemap_clear(gray);
  bytemap_copy(gray, MID_DX, MID_DY, mid_image, 0, 0, w, h);
  write_bytemap_to_screen(gray, gray, gray);
  wait_keyhit();

  printf("Mean filtering \n");
  mean_smooth(mid_image, gray, 3);
  write_bytemap_to_screen(mid_image, mid_image, mid_image);
  wait_keyhit();
  /////////////////////////////////////////

  // DN display image /////////////////////
  strcpy(path, argv[1]);
  strcat(path, "_dn.bmp");
  fn = strrchr(path, '/') + 1;
  printf("filename: %s\n", fn);
  printf("Display image\n");
  load_and_display_BMP(path);
  read_bytemap_in_screen(dn_image, NULL, NULL);
  bytemap_clear(gray);
  bytemap_copy(gray, DN_DX, DN_DY, dn_image, 0, 0, w, h);
  write_bytemap_to_screen(gray, gray, gray);
  wait_keyhit();

  printf("Mean filtering \n");
  mean_smooth(dn_image, gray, 3);
  write_bytemap_to_screen(dn_image, dn_image, dn_image);
  wait_keyhit();
  ///////////////////////////////////////

  printf("Up Image thresholding\n");
  bytemap_threshold(bin, up_image, UP_FROM, UP_TO);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Big blobs greping\n");
  bitmap_clear(roi);
  label_info = label_info_new();
  labeling(labelmap, label_info, bin, NEIGHBOR_8);
  for (label = 0; label < label_info_get_count(label_info); label++) {
    label_info_glimpse(&area, label, label_info);
    //printf("area:%d\n", area);
    if (area > 3000) {
      for (i = 0; i < h; i++) {
	for (j = 0; j < w; j++) {
	  if (dwordmap_get_value(labelmap, j, i) == label) {
	    bitmap_set_value(roi, j, i);
	  }
	}
      }
    }
  }
  label_info_destroy(label_info);
  write_bitmap_to_screen(roi, roi, roi);
  wait_keyhit();

  printf("Fill holes\n");
  bitmap_complement(bin, roi);
  labeling_grep_big_blob(roi, bin);
  bitmap_complement(bin, roi);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Image Morphology\n");
  binary_closing(tmp, bin, se);
  binary_opening(up_roi, tmp, se);
  write_bitmap_to_screen(up_roi, up_roi, up_roi);
  wait_keyhit();

  ///////////////////////////////////////
  printf("Mid Image thresholding\n");
  bytemap_threshold(bin, mid_image, MID_FROM, MID_TO);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Big blobs greping\n");
  bitmap_clear(roi);
  label_info = label_info_new();
  labeling(labelmap, label_info, bin, NEIGHBOR_8);
  for (label = 0; label < label_info_get_count(label_info); label++) {
    label_info_glimpse(&area, label, label_info);
    //printf("area:%d\n", area);
    if (area > 3000) {
      for (i = 0; i < h; i++) {
	for (j = 0; j < w; j++) {
	  if (dwordmap_get_value(labelmap, j, i) == label) {
	    bitmap_set_value(roi, j, i);
	  }
	}
      }
    }
  }
  label_info_destroy(label_info);
  write_bitmap_to_screen(roi, roi, roi);
  wait_keyhit();

  printf("Fill holes\n");
  bitmap_complement(bin, roi);
  labeling_grep_big_blob(roi, bin);
  bitmap_complement(bin, roi);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Image Morphology\n");
  binary_closing(tmp, bin, se);
  binary_opening(mid_roi, tmp, se);
  write_bitmap_to_screen(mid_roi, mid_roi, mid_roi);
  wait_keyhit();

  ///////////////////////////////////////////////////
  printf("Dn Image thresholding\n");
  bytemap_threshold(bin, dn_image, DN_FROM, DN_TO);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Big blobs greping\n");
  bitmap_clear(roi);
  label_info = label_info_new();
  labeling(labelmap, label_info, bin, NEIGHBOR_8);
  for (label = 0; label < label_info_get_count(label_info); label++) {
    label_info_glimpse(&area, label, label_info);
    //printf("area:%d\n", area);
    if (area > 3000) {
      for (i = 0; i < h; i++) {
	for (j = 0; j < w; j++) {
	  if (dwordmap_get_value(labelmap, j, i) == label) {
	    bitmap_set_value(roi, j, i);
	  }
	}
      }
    }
  }
  label_info_destroy(label_info);
  write_bitmap_to_screen(roi, roi, roi);
  wait_keyhit();

  printf("Fill holes\n");
  bitmap_complement(bin, roi);
  labeling_grep_big_blob(roi, bin);
  bitmap_complement(bin, roi);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Image Morphology\n");
  binary_closing(tmp, bin, se);
  binary_opening(dn_roi, tmp, se);
  write_bitmap_to_screen(dn_roi, dn_roi, dn_roi);
  wait_keyhit();
  ///////////////////////////////////////////////

  bitmap_clear(roi);
  bitmap_or(roi, up_roi);
  bitmap_or(roi, mid_roi);
  bitmap_or(roi, dn_roi);
  write_bitmap_to_screen(up_roi, mid_roi, dn_roi);
  wait_keyhit();
  write_bitmap_to_screen(roi, roi, roi);
  wait_keyhit();

  /*
  //write_bitmap_to_screen(up_roi, mid_roi, dn_roi);
  //read_bytemap_in_screen(up_image, mid_image, dn_image);

  strcpy(path, argv[1]);
  strcat(path, "_up_roi.bmp");
  save_bytemap_as_BMP(up_image, path);

  strcpy(path, argv[1]);
  strcat(path, "_mid_roi.bmp");
  save_bytemap_as_BMP(mid_image, path);

  strcpy(path, argv[1]);
  strcat(path, "_dn_roi.bmp");
  save_bytemap_as_BMP(dn_image, path);

  read_bytemap_in_screen(gray, NULL, NULL);
  strcpy(path, argv[1]);
  strcat(path, "_roi.bmp");
  save_bytemap_as_BMP(gray, path);
  */

#if 0
  // up image statistic /////////////////////////
  bytemap_get_min_max_on_roi(&vmin, &vmax, up_image, roi);
  bytemap_get_mean_on_roi(&vmean, up_image, roi);
  printf("Up image statistics are min: %d, mean: %lf, max: %d\n", vmin, vmean, vmax);

  fd = fopen("up_stat.txt", "a+");
  fprintf(fd, "%d, %lf, %d\n", vmin, vmean, vmax);
  fclose(fd);
  ////////////////////////////////////////////////

  // mid image statistic /////////////////////////
  bytemap_get_min_max_on_roi(&vmin, &vmax, mid_image, roi);
  bytemap_get_mean_on_roi(&vmean, mid_image, roi);
  printf("Mid image statistics are min: %d, mean: %lf, max: %d\n", vmin, vmean, vmax);

  fd = fopen("mid_stat.txt", "a+");
  fprintf(fd, "%d, %lf, %d\n", vmin, vmean, vmax);
  fclose(fd);
  /////////////////////////////////////////////////

  // dn image statistic ///////////////////////////
  bytemap_get_min_max_on_roi(&vmin, &vmax, dn_image, roi);
  bytemap_get_mean_on_roi(&vmean, dn_image, roi);
  printf("Dn image statistics are min: %d, mean: %lf, max: %d\n", vmin, vmean, vmax);

  fd = fopen("dn_stat.txt", "a+");
  fprintf(fd, "%d, %lf, %d\n", vmin, vmean, vmax);
  fclose(fd);
  /////////////////////////////////////////////////

  return 0;
#endif

#if 1
  // Up image normalization
  printf("Up-Image global normalization\n");
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (bitmap_isset(roi, j, i)) {
	value = bytemap_get_value(up_image, j, i);
	if (value < UP_MEAN) {
	  value = round(128.0 / (UP_MEAN - UP_MIN) * (value - UP_MIN));
	} else {
	  value = round(128.0 / (UP_MAX - UP_MEAN) * (value - UP_MEAN) + 128.0);
	}
	if (value < 0) value = 0;
	if (value > 255) value = 255;
	bytemap_put_value(value, up_image, j, i);
      } else {
	bytemap_put_value(0, up_image, j, i);
      }
    }
  }
  write_bytemap_to_screen(up_image, up_image, up_image);
  strcpy(path, argv[1]);
  strcat(path, "_up_normal.bmp");
  save_bytemap_as_gray_BMP(up_image, path);
  wait_keyhit();
  //////////////////////////////////////////

  // Mid image normalization
  printf("MID-Image global normalization\n");
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (bitmap_isset(roi, j, i)) {
	value = bytemap_get_value(mid_image, j, i);
	if (value < MID_MEAN) {
	  value = round(128.0 / (MID_MEAN - MID_MIN) * (value - MID_MIN));
	} else {
	  value = round(128.0 / (MID_MAX - MID_MEAN) * (value - MID_MEAN) + 128.0);
	}
	if (value < 0) value = 0;
	if (value > 255) value = 255;
	bytemap_put_value(value, mid_image, j, i);
      } else {
	bytemap_put_value(0, mid_image, j, i);
      }
    }
  }
  write_bytemap_to_screen(mid_image, mid_image, mid_image);
  strcpy(path, argv[1]);
  strcat(path, "_mid_normal.bmp");
  save_bytemap_as_gray_BMP(mid_image, path);
  wait_keyhit();
  ///////////////////////////////////////////////

  // Dn image normalization
  printf("Dn image global normalization\n");
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (bitmap_isset(roi, j, i)) {
	value = bytemap_get_value(dn_image, j, i);
	if (value < DN_MEAN) {
	  value = round(128.0 / (DN_MEAN - DN_MIN) * (value - DN_MIN));
	} else {
	  value = round(128.0 / (DN_MAX - DN_MEAN) * (value - DN_MEAN) + 128.0);
	}
	if (value < 0) value = 0;
	if (value > 255) value = 255;
	bytemap_put_value(value, dn_image, j, i);
      } else {
	bytemap_put_value(0, dn_image, j, i);
      }
    }
  }
  write_bytemap_to_screen(dn_image, dn_image, dn_image);
  strcpy(path, argv[1]);
  strcat(path, "_dn_normal.bmp");
  save_bytemap_as_gray_BMP(dn_image, path);
  wait_keyhit();
  ////////////////////////////////////////////

  return 0;
#endif

#if 0
  printf("Image subtraction between DN and MID for bruise-detection\n");
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (bitmap_isset(roi, j, i)) {
	value = ((real_t)bytemap_get_value(dn_image, j, i) - (real_t)bytemap_get_value(mid_image, j, i) + 255.0) / 2.0;
	if (value < 0) value = 0;
	if (value > 255) value = 255;
	bytemap_put_value(value, gray, j, i);
      } else {
	bytemap_put_value(0, gray, j, i);
      }
    }
  }
  write_bytemap_to_screen(gray, gray, gray);
  wait_keyhit();

  bytemap_threshold(bin, gray, 142, 255);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();

  printf("Image subtraction between UP and MID for spot-detection\n");
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (bitmap_isset(roi, j, i)) {
	value = ((real_t)bytemap_get_value(up_image, j, i) - (real_t)bytemap_get_value(mid_imageb, j, i) + 255.0) / 2.0;
	if (value < 0) value = 0;
	if (value > 255) value = 255;
	bytemap_put_value(value, gray, j, i);
      } else {
	bytemap_put_value(0, gray, j, i);
      }
    }
  }
  write_bytemap_to_screen(gray, gray, gray);
  wait_keyhit();
  
  bytemap_threshold(bin, gray, 1, 110);
  write_bitmap_to_screen(bin, bin, bin);
  wait_keyhit();
#endif

  dwordmap_destroy(labelmap);

  bytemap_destroy(se);
  return 0;

  bitmap_destroy(roi);
  bitmap_destroy(tmp);
  bitmap_destroy(bin);

  bitmap_destroy(dn_roi);
  bitmap_destroy(mid_roi);
  bitmap_destroy(up_roi);

  bytemap_destroy(gray);

  bytemap_destroy(dn_image);
  bytemap_destroy(mid_image);
  bytemap_destroy(up_image);

  return 0;
}
Beispiel #30
0
static void nsplit_cache_host(struct host *h)
{
	struct hostsmember *parent, *next, *prev = NULL;
	struct servicesmember *sm, *sp, *sp_prev, *sp_next;
	objectlist *olist;

	if (bitmap_isset(htrack, h->id)) {
		return;
	}
	bitmap_set(htrack, h->id);
	nsplit_cache_slaves(h);

	/* massage the parent list */
	for (parent = h->parent_hosts; parent; parent = next) {
		next = parent->next;
		if (bitmap_isset(map.hosts, parent->host_ptr->id)) {
			prev = parent;
			continue;
		}
		free(parent->host_name);
		free(parent);
		if (prev)
			prev->next = next;
		else {
			h->parent_hosts = next;
		}
	}
	cached.hosts++;
	fcache_host(fp, h);
	nsplit_cache_hostdependencies(h->exec_deps);
	nsplit_cache_hostdependencies(h->notify_deps);

	for (olist = h->escalation_list; olist; olist = olist->next) {
		struct hostescalation *he = (struct hostescalation *)olist->object_ptr;
		nsplit_cache_timeperiod(he->escalation_period_ptr);
		nsplit_cache_contactgroups(he->contact_groups);
		nsplit_cache_contacts(he->contacts);
		cached.hostescalations++;
		fcache_hostescalation(fp, he);
	}

	for (sm = h->services; sm; sm = sm->next) {
		struct service *s = sm->service_ptr;
		nsplit_cache_slaves(s);
		/* remove cross-host service parents, if any */
		for (sp_prev = NULL, sp = s->parents; sp; sp_prev = sp, sp = sp_next) {
			sp_next = sp->next;
			if (!bitmap_isset(map.hosts, sp->service_ptr->host_ptr->id))
				nsplit_slist_remove(s->parents, sp, sp_next, sp_prev);
		}
		cached.services++;
		fcache_service(fp, s);
		nsplit_cache_servicedependencies(s->exec_deps);
		nsplit_cache_servicedependencies(s->notify_deps);
		for (olist = s->escalation_list; olist; olist = olist->next) {
			struct serviceescalation *se = (struct serviceescalation *)olist->object_ptr;
			nsplit_cache_timeperiod(se->escalation_period_ptr);
			nsplit_cache_contactgroups(se->contact_groups);
			nsplit_cache_contacts(se->contacts);
			cached.serviceescalations++;
			fcache_serviceescalation(fp, se);
		}
	}
}