surface_t* make_plane(point3_t a, point3_t b, point3_t c, color_t* diffuse_color, color_t* ambient_color, color_t* spec_color, float phong_exp) { plane_data_t* data = MALLOC1(plane_data_t) ; data->a = a ; data->b = b ; data->c = c ; surface_t* surface = MALLOC1(surface_t) ; set_sfc_data(surface, data, sfc_hit_plane, diffuse_color, ambient_color, spec_color, phong_exp) ; return surface ; }
/* Create a Chaincode and set its width and height. * * Note: there are other functions that may create Chaincode directly. */ Chaincode *chaincode_create(int w, int h) { Chaincode *cc = MALLOC1(Chaincode); cc->width = w; cc->height = h; cc->node_count = 0; cc->rope_count = 0; cc->node_allocated = 10; cc->rope_allocated = 10; cc->nodes = MALLOC(Node, cc->node_allocated); cc->ropes = MALLOC(Rope, cc->rope_allocated); return cc; }
WordCut *cut_word(unsigned char **pixels, int w, int h) { unsigned char *projection = MALLOC(unsigned char, w); int *histogram = make_histogram(pixels, w, h); unsigned char *shields = MALLOC(unsigned char, w); WordCut *wc = MALLOC1(WordCut); Chaincode *cc; int i; int rope_count; cc = chaincode_compute(pixels, w, h); rope_count = cc->rope_count; /* shield level n prevents cuts with level above n. * (the less the shield level, the stronger it is) */ wc->count = 0; wc->position = MALLOC(int, w); wc->window_start = MALLOC(int, w); wc->window_end = MALLOC(int, w); wc->level = MALLOC(unsigned char, w); wc->max_level = 1; /* XXX */ memset(shields, 255, w); /* weakest shields, prevent no cuts */ for (i = 0; i < rope_count; i++) { int shield_level = get_shield_level(cc, i); get_rope_projection(projection, cc, i); fill_shields(w, shields, shield_level, projection); } for (i = 0; i < w && shields[i] == 255; i++) {} while (i < w) { int begin, end; /* skip shields */ while (i < w && shields[i] < 255) i++; begin = i; /* skip window */ while (i < w && shields[i] == 255) i++; end = i; if (i == w) break; /* add the cut */ wc->level[wc->count] = 1; wc->window_start[wc->count] = begin; wc->window_end[wc->count] = end; wc->position[wc->count] = cut_into_window(histogram, begin, end); wc->count++; } wc->level = REALLOC(unsigned char, wc->level, wc->count); wc->position = REALLOC(int, wc->position, wc->count); wc->window_start = REALLOC(int, wc->window_start, wc->count); wc->window_end = REALLOC(int, wc->window_end, wc->count); FREE(projection); FREE(shields); FREE(histogram); chaincode_destroy(cc); return wc; }