Image Splitter — C source
Split an image into an even grid of tiles — rows × columns with a live cut-line preview and per-tile or bulk PNG download. Runs entirely in your browser.
This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.
/* Image splitter — tile geometry — C (C11) port of the image-splitter tool. */
#include <stdio.h>
/* One cut of the grid: the source rectangle that becomes a tile. In the
* browser tool this rect is drawImage'd into its own canvas and encoded
* to PNG; the geometry here is the deterministic part of that pipeline. */
typedef struct {
int x, y, w, h;
} TileRect;
/* Split a width x height image into rows x cols tiles, row-major.
* Mirrors splitToTiles in typescript.ts: tileW = floor(width / cols),
* tileH = floor(height / rows) — integer division IS Math.floor for
* positives, so a 1024px width over 3 cols yields 341px tiles and the
* last 1px column is dropped, exactly like the TS. Writes at most
* rows * cols rects to out; returns how many were written. */
int split_tiles(int width, int height, int rows, int cols, TileRect *out) {
int tile_w = width / cols;
int tile_h = height / rows;
int n = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
out[n].x = col * tile_w;
out[n].y = row * tile_h;
out[n].w = tile_w;
out[n].h = tile_h;
n++;
}
}
return n;
}
int main(void) {
/* 1024x768 image, 2 rows x 3 cols -> six 341x384 tiles. */
TileRect tiles[16];
int n = split_tiles(1024, 768, 2, 3, tiles);
for (int i = 0; i < n; i++) {
printf("tile %d: src=(%d, %d) %dx%d\n",
i, tiles[i].x, tiles[i].y, tiles[i].w, tiles[i].h);
}
return 0;
}
Also available in 8 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →