static void dot (gfloat* buf, const GeglRectangle* roi, GeglChantO *o) { gint cx0 = CELL_X(roi->x, o->size); gint cy0 = CELL_Y(roi->y, o->size); gint block_count_x = CELL_X(roi->x + roi->width - 1, o->size) - cx0 + 1; gint block_count_y = CELL_Y(roi->y + roi->height - 1, o->size) - cy0 + 1; gfloat* block_colors = g_new0 (gfloat, block_count_x * block_count_y * 4); gint x; gint y; gint c; gfloat radius2 = (o->size * o->ratio / 2.0); radius2 *= radius2; /* calculate the average color of all the blocks */ calc_block_colors(block_colors, buf, roi, o->size); /* set each pixel to the average color of the block it belongs to */ for (y=0; y<roi->height; ++y) { gint cy = CELL_Y(y + roi->y, o->size) - cy0; for (x=0; x<roi->width; ++x) { gint cx = CELL_X(x + roi->x, o->size) - cx0; gfloat cellx = x + roi->x - (cx0 + cx) * o->size - o->size / 2.0; gfloat celly = y + roi->y - (cy0 + cy) * o->size - o->size / 2.0; if (cellx * cellx + celly * celly > radius2) for (c=0; c<4; ++c) *buf++ = 0; else for (c=0; c<4; ++c) *buf++ = block_colors[(cy*block_count_x + cx)*4 + c]; } } g_free (block_colors); }
static void calc_block_colors (gfloat* block_colors, const gfloat* input, const GeglRectangle* roi, gint size) { gint cx0 = CELL_X(roi->x, size); gint cy0 = CELL_Y(roi->y, size); gint cx1 = CELL_X(roi->x + roi->width - 1, size); gint cy1 = CELL_Y(roi->y + roi->height - 1, size); gint cx; gint cy; gfloat weight = 1.0f / (size * size); gint line_width = roi->width + 2*size; /* loop over the blocks within the region of interest */ for (cy=cy0; cy<=cy1; ++cy) { for (cx=cx0; cx<=cx1; ++cx) { gint px = (cx * size) - roi->x + size; gint py = (cy * size) - roi->y + size; /* calculate the average color for this block */ gint j,i,c; gfloat col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; for (j=py; j<py+size; ++j) { for (i=px; i<px+size; ++i) { for (c=0; c<4; ++c) col[c] += input[(j*line_width + i)*4 + c]; } } for (c=0; c<4; ++c) block_colors[c] = weight * col[c]; block_colors += 4; } } }
map_t* map_create(const char* file_name) { FILE* file = fopen(file_name, "r"); if (!file) return NULL; int size_x, size_y; int count = fscanf(file, "%d,%d\n", &size_x, &size_y); if (EOF == count || count < 2 || size_x <= 0 || size_y <= 0) { fclose(file); return NULL; } map_t* map = (map_t*)malloc(sizeof(map_t)); map->terrain = (cell_t*)malloc(sizeof(cell_t)*size_x*size_y); map->size_x = size_x; map->size_y = size_y; char buffer[1024]; int x, y; for (y = 0; y < size_y; ++y) { for (x = 0; x < size_x; ++x) { char flag = fgetc(file); if (EOF == flag || '\n' == flag || '\r' == flag) { free(map->terrain); free(map); fclose(file); return NULL; } int index = XY_TO_CELL_INDEX(map, x, y); cell_t* cell = INDEX_TO_CELL(map, index); CELL_INDEX(map, cell) = index; CELL_X(map, cell) = x; CELL_Y(map, cell) = y; CELL_FLAG(cell) = (unsigned)(flag - '0'); } fgets(buffer, sizeof(buffer), file); } map->astar = astar_create(map); return map; }