Image Splitter — Go 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 Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package imagesplitter ports the tile geometry of CosmoDev's image-splitter
// tool (display source: the deterministic half of the island's split()).
//
// Language: Go (standard library only).
// CosmoDev polyglot showcase port of the `image-splitter` tool, ported from
// src/tools/ImageSplitter.tsx.
//
// The live tool rasterizes each grid cell through a canvas and re-encodes it
// to PNG (canvas-bound), so this port carries the deterministic half: the
// floor-divided tile metrics, the "grid finer than the image" guard, the
// row-major cell enumeration, and the `<name>-<row>x<col>.png` output naming.
//
// display source — part of CosmoDev's polyglot tool pages.
package imagesplitter
import (
"errors"
"fmt"
"math"
"strings"
)
// ErrGridTooFine mirrors the island's user-facing split() error: it fires
// whenever a tile dimension would round down to 0 (the grid is finer than
// the image).
var ErrGridTooFine = errors.New("The grid is finer than the image — reduce rows or columns.")
// TileRect is 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. Row/Col are 0-based, row-major like the island's nested loops.
type TileRect struct {
Row, Col int
X, Y int // tile origin in the original image
Width int
Height int
}
// ClampGrid clamps a rows/cols field value to the tool's 1-20 range.
// math.Floor(v+0.5) mirrors JavaScript's Math.round for the positive inputs
// a spinner produces; the clamps mirror the NumberField handler's
// Math.max(1, Math.min(20, Math.round(v))).
func ClampGrid(value float64) int {
rounded := int(math.Floor(value + 0.5))
if rounded < 1 {
return 1
}
if rounded > 20 {
return 20
}
return rounded
}
// TileSize returns the shared tile metrics for a width x height image cut
// rows x cols ways. Go integer division truncates toward zero, which 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 browser tool. A grid
// finer than the image yields ErrGridTooFine.
func TileSize(width, height, rows, cols int) (tileW, tileH int, err error) {
tileW = width / cols
tileH = height / rows
if tileW < 1 || tileH < 1 {
return 0, 0, ErrGridTooFine
}
return tileW, tileH, nil
}
// TileRects enumerates every tile as a source rectangle, row-major (the same
// order the island's nested for-loops visit cells, and the order tiles
// appear in the output grid). X/Y is where the tile starts in the image.
func TileRects(width, height, rows, cols int) ([]TileRect, error) {
tileW, tileH, err := TileSize(width, height, rows, cols)
if err != nil {
return nil, err
}
rects := make([]TileRect, 0, rows*cols)
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
rects = append(rects, TileRect{
Row: row,
Col: col,
X: col * tileW,
Y: row * tileH,
Width: tileW,
Height: tileH,
})
}
}
return rects, nil
}
// TileName renders one tile's output file name: the extensionless source
// name plus the 1-based grid position, e.g. photo.jpg at row 0, col 2 ->
// photo-1x3.png — matching the download file names the tool generates. The
// LastIndex cut mirrors the island's extension-stripping regex replace
// (/\.[^.]+$/ with an empty replacement): strip a final dot only when at
// least one character follows it ("file." keeps its dot, a bare ".hidden"
// stem strips clean).
func TileName(fileName string, row, col int) string {
if i := strings.LastIndex(fileName, "."); i >= 0 && i < len(fileName)-1 {
fileName = fileName[:i]
}
return fmt.Sprintf("%s-%dx%d.png", fileName, row+1, col+1)
}
Also available in 13 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 →