struct socket_buffer * socket_buffer_new(process_t self, int fd, int maxsize) { struct socket_buffer *b = wcalloc(sizeof(*b)); b->self = self; b->fd = fd; b->maxsize = (size_t)maxsize; weenet_event_monitor(self, 0, fd, WEVENT_DELETE, WEVENT_WRITE); return b; }
/** * This function crosses a connected component * and calculates the number of black boxes. * * @param i Current y coordinate * @param j Current x coordinate * @param matrix Binary matrix * @param mark Matrix of marks * * @return Number of black boxes */ void crossCC(int y, int x, t_cc_elt *elt, t_matrix *matrix, char **mark) { int i, j, kmin, kmax, pmin, pmax, first; int pix_count; int xtmp, ytmp; t_coordinate *coord, *res; t_box_coordinate *minmax; t_queue **q; pix_count = 1; q = NULL; q = (t_queue **)wcalloc(1, sizeof(t_queue *)); xtmp = x; ytmp = y; minmax = wcalloc(1, sizeof(t_box_coordinate)); minmax->xmin = x; minmax->xmax = x; minmax->ymin = y; minmax->ymax = y; first = 1; do { if (!first) { res = qDequeue(q); if (res != NULL) { xtmp = res->x; ytmp = res->y; } } kmin = ytmp-1; if (kmin < 0) kmin = 0; kmax = ytmp+1; if (kmax >= matrix->nbrows) kmax = matrix->nbrows - 1; for (i=kmin; i <= kmax; i++) { pmin = xtmp-1; if (pmin < 0) pmin = 0; pmax = xtmp+1; if (pmax >= matrix->nbcols) pmax = matrix->nbcols - 1; for (j=pmin; j <= pmax; j++) { if ((matrix->data[i][j] == 1) && (mark[i][j] == 'o')) { coord = wmalloc(sizeof(t_coordinate)); coord->x = j; coord->y = i; qEnqueue(q, coord); mark[i][j] = 'x'; updateMinMax(minmax, j, i); pix_count++; } } } first = 0; } while (*q != NULL); elt->coord = *minmax; elt->nbpix = pix_count; qDelete(q); wfree(q); }
/** * This function resizes a matrix. */ t_matrix *resizeMatrix(t_matrix *mat, t_cc_elt *cc, t_binary_image *pic) { int height_cc, width_cc; int i, j, itmp, jtmp; t_matrix *ccmat; float percentx, percenty; /* Search of cc informations */ height_cc = cc->coord.ymax - cc->coord.ymin; width_cc = cc->coord.xmax - cc->coord.xmin; /* Creation of the unresized cc matrix */ ccmat = wmalloc(sizeof(t_matrix)); ccmat->nbrows = height_cc; ccmat->nbcols = width_cc; ccmat->data= NULL; ccmat->data = (int **)wmalloc(height_cc * sizeof(int *)); for (i=0; i < height_cc; ++i) ccmat->data[i] = (int *)wcalloc(width_cc, sizeof(int)); for (i=0; i < height_cc; ++i) for (j=0; j < width_cc; ++j) { itmp = i + cc->coord.ymin; jtmp = j + cc->coord.xmin; ccmat->data[i][j] = pic->matrix->data[itmp][jtmp]; } percenty = 10 / height_cc; percentx = 10 / width_cc; for (i=0; i < 10; ++i) for (j=0; j < 10; ++j) { itmp = i / percenty; jtmp = j / percentx; mat->data[i][j] = ccmat->data[itmp][jtmp]; } return(mat); }
/** * This function returns a resized matrix (10x10) for the cc. * * @param cc Connected component * @param pic Binary image * * @return Matrix 10x10 */ t_matrix *resizeCC(t_cc_elt *cc, t_binary_image *pic) { int i, j, itmp, jtmp; t_matrix *ret; int height_cc, width_cc; /* Initialization */ ret = wmalloc(sizeof(t_matrix)); ret->nbrows = 10; ret->nbcols = 10; ret->data= NULL; ret->data = (int **)wmalloc(10 * sizeof(int *)); for (i=0; i < 10; ++i) ret->data[i] = (int *)wcalloc(10, sizeof(int)); for (i=0; i < 10; ++i) for (j=0; j < 10; ++j) ret->data[i][j] = 0; /* Search of cc informations */ height_cc = cc->coord.ymax - cc->coord.ymin; width_cc = cc->coord.xmax - cc->coord.xmin; if ((height_cc <= 10) && (width_cc <= 10)) { /* Just a rewrite of the cc matrix */ for (i=0; i < height_cc; ++i) for (j=0; j < width_cc; ++j) { itmp = i + cc->coord.ymin; jtmp = j + cc->coord.xmin; ret->data[i][j] = pic->matrix->data[itmp][jtmp]; } } else ret = resizeMatrix(ret, cc, pic); return(ret); }
/** * This functions initiates a matrix; * * @return Matrix */ t_matrix *initializeEnv() { int i, j; t_matrix *ret; ret = wmalloc(sizeof(t_matrix)); ret->nbrows = 4; ret->nbcols = 4; ret->data = NULL; ret->data = (int **)wmalloc(ret->nbrows * sizeof(int *)); for (i=0; i < ret->nbrows; ++i) ret->data[i] = (int *)wcalloc(ret->nbcols, sizeof(int)); for (i=0; i < ret->nbrows; ++i) for(j=0; j < ret->nbcols; ++j) ret->data[i][j] = 0; ret->data[3][0] = 1; ret->data[3][1] = 1; ret->data[3][2] = 1; ret->data[3][3] = 1; return(ret); }