| 279 | } |
| 280 | |
| 281 | static void |
| 282 | split_tiles(const struct TileSetImage *image) |
| 283 | { |
| 284 | unsigned tile_rows, tile_cols; |
| 285 | size_t tile_size, i, j; |
| 286 | unsigned x1, y1, x2, y2; |
| 287 | |
| 288 | /* Get the number of tiles */ |
| 289 | tile_rows = image->height / iflags.wc_tile_height; |
| 290 | tile_cols = image->width / iflags.wc_tile_width; |
| 291 | num_tiles = tile_rows * tile_cols; |
| 292 | tile_size = (size_t) iflags.wc_tile_height * (size_t) iflags.wc_tile_width; |
| 293 | |
| 294 | /* Allocate the tile array */ |
| 295 | tiles = (struct TileImage *) alloc(num_tiles * sizeof(tiles[0])); |
| 296 | memset(tiles, 0, num_tiles * sizeof(tiles[0])); |
| 297 | |
| 298 | /* Copy the pixels into the tile structures */ |
| 299 | for (y1 = 0; y1 < tile_rows; ++y1) { |
| 300 | for (x1 = 0; x1 < tile_cols; ++x1) { |
| 301 | struct TileImage *tile = &tiles[y1 * tile_cols + x1]; |
| 302 | |
| 303 | tile->width = iflags.wc_tile_width; |
| 304 | tile->height = iflags.wc_tile_height; |
| 305 | tile->pixels = (struct Pixel *) |
| 306 | alloc(tile_size * sizeof (struct Pixel)); |
| 307 | if (image->indexes != NULL) { |
| 308 | tile->indexes = (unsigned char *) alloc(tile_size); |
| 309 | } |
| 310 | for (y2 = 0; y2 < (unsigned) iflags.wc_tile_height; ++y2) { |
| 311 | for (x2 = 0; x2 < (unsigned) iflags.wc_tile_width; ++x2) { |
| 312 | unsigned x = x1 * iflags.wc_tile_width + x2; |
| 313 | unsigned y = y1 * iflags.wc_tile_height + y2; |
| 314 | |
| 315 | i = y * image->width + x; |
| 316 | j = y2 * tile->width + x2; |
| 317 | tile->pixels[j] = image->pixels[i]; |
| 318 | if (image->indexes != NULL) { |
| 319 | tile->indexes[j] = image->indexes[i]; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* Create a blank tile for use when the tile index is invalid */ |
| 327 | blank_tile.width = iflags.wc_tile_width; |
| 328 | blank_tile.height = iflags.wc_tile_height; |
| 329 | blank_tile.pixels = (struct Pixel *) |
| 330 | alloc(tile_size * sizeof (struct Pixel)); |
| 331 | for (i = 0; i < tile_size; ++i) { |
| 332 | blank_tile.pixels[i].r = 0; |
| 333 | blank_tile.pixels[i].g = 0; |
| 334 | blank_tile.pixels[i].b = 0; |
| 335 | blank_tile.pixels[i].a = 255; |
| 336 | } |
| 337 | if (image->indexes) { |
| 338 | blank_tile.indexes = (unsigned char *) alloc(tile_size); |