Works out where a picture's corners lie, its pixel size when attached or pasted, and its clip boundary in pixel coordinates for saving.
How the precision is kept
A picture is held as a corner, two pixel vectors and its pixel counts, all 64-bit numbers. Corners, pixel size and the two-point rescale are single products, sums and divisions. Clip points are solved back to pixel coordinates by a 2 × 2 determinant.
Calculations under this policy
6 calculations in 2 source files. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.
The insertion point, pixel vectors and clip boundary a DWG image record holds.
det = u × v; a = (d × v) ÷ det; b = (u × d) ÷ det; pixel = (a − 0.5, h − 0.5 − b)
The pixel vectors go through the reference's 2 × 2 matrix. Clip points are solved by Cramer's rule; the clip is left out only when |det| ≤ 1e-300, so a picture squeezed nearly flat divides by a very small determinant.
pictures.js
lines 77 to 101
CadCraft 0.8.2
/**
* A reference to a picture block → what a DWG's IMAGE holds: { file, insert, u, v, size, clip? (pixel coordinates, y down
* from the top-left pixel's centre, as the file keeps them), fade, transparent? } in the reference's space, or null when the block is
* not a picture (anything besides its frame and name in it).
*/
export function picturePlacement(ref, block) {
const fr = pictureFrame(block);
if (!fr || fr.ole || block.entities.some((e) => e !== fr && !e.picName)) return null; // an OLE object's preview is drawn like a picture but is no IMAGE (ole.js, 0.7 D7.23)
const p = fr.pic, m = insertMatrix(ref, block);
const at = mApply(m, { x: p.x, y: p.y });
const vec = (x, y) => ({ x: m[0] * x + m[2] * y, y: m[1] * x + m[3] * y });
const u = vec(p.ux, p.uy), v = vec(p.vx, p.vy);
const out = { file: p.file, insert: at, u, v, size: { x: p.w, y: p.h }, fade: Number.isFinite(p.fade) ? p.fade : 0, ...(p.transparent ? { transparent: true } : {}) };
if (p.clipped) {
const det = u.x * v.y - u.y * v.x;
if (Math.abs(det) > 1e-300) {
out.clip = fr.pts.map((q) => {
const w = mApply(m, q), dx = w.x - at.x, dy = w.y - at.y;
const a = (dx * v.y - dy * v.x) / det, b = (u.x * dy - u.y * dx) / det; // along the width, up, in pixels
return { x: a - 0.5, y: p.h - 0.5 - b };
});
}
}
return out;
}
Pixel size of a picture in a PDF
64-bit
How many pixels a PNG keeps when a plot caps its resolution.
w₂ = round(maxDpi × mm ÷ 25.4), at least 1
25.4 mm per inch is the exact definition. The picture is shrunk only when both target sizes are smaller than its own; a JPEG is never resampled.
Resizes a pasted picture so two picked points are a stated distance apart.
f = wanted ÷ |b − a|; sx′ = sx·f; sy′ = sy·f
Points closer than 1e-9 drawing units, an absolute limit, are refused. Otherwise one division and two multiplications; the corner stays where it was put.