DEV Community

Yan Wang
Yan Wang

Posted on Fully Autonomous

A Centimeter-to-Pixel Contract With Explicit Limits

A request for an image in centimeters is not yet an encoded image. It becomes easier to validate when the work is split into four layers: a physical-size request, an integer pixel plan, an encoded file, and a possible print. Each layer has its own inputs and evidence.

This standalone JavaScript example handles only the pixel plan. It accepts two positive centimeter values and an integer pixels-per-inch density, rounds each axis to the nearest whole pixel, and rejects plans outside an example resource budget. It is not a production patch or a complete image-processing pipeline.

Define the input contract

For each axis, the calculation is cm / 2.54 * ppi, rounded with Math.round. Width and height are validated independently. All three inputs must be JavaScript numbers that are finite and positive: a numeric-looking string, NaN, infinity, zero, or a negative value is rejected. Density must additionally be an integer from 1 through 65535.

The result must contain safe integer dimensions of at least one pixel. That matters even when a centimeter input is positive: a sufficiently small value can round to zero. This example rejects such a request. The formal product uses a different policy and clamps that boundary; the example should not be read as an identical implementation.

The 10,000-pixel edge and 40-million-pixel area limits below are choices made for this example. They are not universal browser or codec limits. The area check happens after rounding, using the actual planned integer dimensions.

export function rasterPlan(widthCm, heightCm, ppi) {
  if (![widthCm, heightCm, ppi].every(
    value => typeof value === "number" && Number.isFinite(value) && value > 0
  )) throw new RangeError("dimensions and density must be finite positive numbers");
  if (!Number.isInteger(ppi) || ppi > 65535) {
    throw new RangeError("density must be an integer from 1 to 65535");
  }
  const width = Math.round((widthCm / 2.54) * ppi);
  const height = Math.round((heightCm / 2.54) * ppi);
  if (![width, height].every(n => Number.isSafeInteger(n) && n >= 1)) {
    throw new RangeError("requested size does not produce a valid pixel grid");
  }
  if (width > 10000 || height > 10000 || width * height > 40000000) {
    throw new RangeError("requested pixel grid exceeds the example budget");
  }
  return { width, height, ppi };
}
Enter fullscreen mode Exit fullscreen mode

A per-edge check alone is not enough. At 300 ppi, 60 × 60 cm rounds to 7087 × 7087 pixels. Each edge is below 10,000, but the area exceeds 40 million pixels, so this contract rejects it.

Run a small example

Place the function and the following block, in that order, in one file named one.mjs, then run node one.mjs. The export is valid because the file is an ES module. A successful run produces no assertion output. This is a Node reproduction, not a live-browser demonstration.

import assert from "node:assert/strict";

assert.deepEqual(rasterPlan(5, 7, 300), { width: 591, height: 827, ppi: 300 });
for (const args of [
  ["5", 7, 300],
  [5, 7, 300.5],
  [0.001, 0.001, 1],
  [60, 60, 300],
]) {
  assert.throws(() => rasterPlan(...args));
}
Enter fullscreen mode Exit fullscreen mode

The parent numeric harness covered six valid cases, 16 invalid cases and four rounding cases. Its negative control showed that a width-only guard misses the area cap. Those counts describe pure numeric checks and read-only comparisons with the formal helper, not human testing, browser conversion or image generation.

What the return value cannot establish

The returned ppi is requested plan metadata. It does not prove that a JPEG contains the intended JFIF density, that a PNG contains the intended pHYs value, or that a downstream application will honor either field. Encoding requires its own format-specific write and read-back checks. PNG density is stored in pixels per meter, so conversion to that unit can introduce quantization.

Likewise, a valid pixel grid does not say how an image was fitted into it. Preserving the whole image, cropping and stretching produce different compositions. A lossy JPEG can also change visual detail without changing the grid. Printing introduces another layer of page geometry, scaling and device behavior. Keep those checks separate from the arithmetic contract.

Disclosure: GPT-6.1 Terra generated the initial draft for Yan Wang / LensUp, and an independent AI editor revised it. On October 27, a Codex agent checked the live page text, local formal source and pure numeric cases, including factual, code and disclosure checks. No new image conversion, phone or printer test, recipient acceptance, or personal check by the account owner is claimed.

Top comments (0)