Seamless Texture — C source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.
/* seamless-texture — tileable-texture math, C port: wrap-blend weights,
* integer lattice hash, wrapping value noise, torus Worley. C11, stdlib
* only. Port of the pure src/lib/seamless.ts (the TS lib has no canvas or
* DOM dependency at all, so this is a 1:1 port); the browser island owns
* all drawing. uint32_t arithmetic reproduces Math.imul exactly. */
#include <math.h>
#include <stdint.h>
/* 32-bit wrapping multiplication — uint32_t multiply has Math.imul's
* exact truncating semantics. */
static int32_t imul(int32_t a, int32_t b) {
return (int32_t) ((uint32_t) a * (uint32_t) b);
}
/* The JS `h >>> n`: a logical right shift of the int32 bit pattern. */
static int32_t shr(int32_t h, unsigned n) {
return (int32_t) ((uint32_t) h >> n);
}
/* JS Math.round (half up) — C round() is half away from zero. */
static double js_round(double v) {
return floor(v + 0.5);
}
/* Forward declarations — value_noise runs one octave per iteration. */
static double layer(double x, double y, double period, int32_t seed);
static double wrap(double i, double period);
static double fade(double t);
static double lerp(double a, double b, double t);
double blend_weight(double x, double y, double w, double h, double feather) {
if (!(feather > 0)) return 1;
double fx = fmin(x, w - x) / feather;
double fy = fmin(y, h - y) / feather;
return fmin(1, fmax(0, fx)) * fmin(1, fmax(0, fy));
}
double hash2(int32_t x, int32_t y, int32_t seed) {
int32_t h = imul(x, (int32_t) 0x9e3779b1) ^ imul(y, (int32_t) 0x85ebca77) ^ imul(seed, (int32_t) 0xc2b2ae35);
h ^= shr(h, 15);
h = imul(h, (int32_t) 0x2c1b3c6d);
h ^= shr(h, 12);
h = imul(h, (int32_t) 0x297a2d39);
h ^= shr(h, 15);
return (double) (uint32_t) h / 4294967296.0;
}
/* Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
* `octaves` < 1 snaps to 1. */
typedef struct {
int32_t seed;
double scale;
int32_t octaves;
} noise_options;
/* Wrapping value noise over a unit lattice (lattice spacing 1 px, tile
* period `scale` px): noise(x + scale, y) === noise(x, y) exactly. Octaves
* >= 2 sum frequency-doubled, amplitude-halved layers normalized by total
* amplitude; scale < 1 snaps to 1. */
double value_noise(double x, double y, noise_options opts) {
double scale = fmax(1, js_round(opts.scale));
int32_t octaves = opts.octaves < 1 ? 1 : opts.octaves;
double sum = 0, total = 0, amp = 1, freq = 1;
for (int32_t o = 0; o < octaves; o++) {
sum += amp * layer(x * freq, y * freq, scale * freq, opts.seed + o * 101);
total += amp;
amp /= 2;
freq *= 2;
}
return sum / total;
}
/* One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes. */
static double layer(double x, double y, double period, int32_t seed) {
double x0 = floor(x), y0 = floor(y);
double u = fade(x - x0), v = fade(y - y0);
double xa = wrap(x0, period), xb = wrap(x0 + 1, period);
double ya = wrap(y0, period), yb = wrap(y0 + 1, period);
double top = lerp(hash2((int32_t) xa, (int32_t) ya, seed), hash2((int32_t) xb, (int32_t) ya, seed), u);
double bottom = lerp(hash2((int32_t) xa, (int32_t) yb, seed), hash2((int32_t) xb, (int32_t) yb, seed), u);
return lerp(top, bottom, v);
}
/* Positive modulo — lattice indices wrap onto the torus of `period` cells. */
static double wrap(double i, double period) {
return fmod(fmod(i, period) + period, period);
}
/* Quintic smoothstep fade — C2-continuous interpolation curve. */
static double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
/* Mirrors the TS WorleyOptions: `points` is the number of hashed feature
* points scattered in the unit tile (>= 1; floored like the TS). */
typedef struct {
int32_t seed;
double points;
} worley_options;
/* Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points. */
typedef struct {
double d1, d2;
} worley_result;
/* Wrapping (torus-metric) F1-F2 field: each axis distance wraps via
* min(d, 1-d), so the field tiles exactly with period 1 in x and y. Raw
* torus distances top out at the half-diagonal √½, which maps to 1. Fewer
* than one point returns the all-max {1, 1}; exactly one point leaves d2
* at the maximum. */
worley_result worley(double x, double y, worley_options opts) {
/* Wrap the query onto the unit tile first so shifted queries are exact. */
double wx = x - floor(x), wy = y - floor(y);
int32_t n = (int32_t) floor(opts.points);
worley_result r = { 1, 1 };
for (int32_t i = 0; i < n; i++) {
double px = hash2(i, 0, opts.seed);
double py = hash2(i, 1, opts.seed);
double dx = fabs(wx - px);
dx = fmin(dx, 1 - dx);
double dy = fabs(wy - py);
dy = fmin(dy, 1 - dy);
double d = sqrt(dx * dx + dy * dy) * 1.4142135623730951;
if (d < r.d1) {
r.d2 = r.d1;
r.d1 = d;
} else if (d < r.d2) {
r.d2 = d;
}
}
return r;
}
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 →