feat: residue and a solid background stop a ready sheet before payment
Some checks failed
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Integration suite on a real stack (push) Failing after 4m10s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Has been skipped

Dropstar's items 2 and 3 (2026-10-21): both were warnings the customer
could pay past; now one dot is enough to stop the order.

- every pixel is read (images in strips at 1:1, large PNGs while they
  stream, PDFs rendered in strips at about 0,2 mm): the old check shrank
  the image first and could miss a small dot, or sampled every n-th pixel
- a residue is ink under 2 mm on both sides, measured from where the ink
  starts and ends in each cell: the same dot gets the same verdict wherever
  it falls on the grid, from 0,5 to 4 mm (his acceptance test)
- a solid background is under 1,5 % of transparent pixels; a JPG sheet
  always has one
- the sheet says how many dots there are, circles them on the preview, and
  asks to erase them and send again; the order waits for the check to end
- the page no longer offers JPG for a ready sheet

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-22 16:37:46 -03:00
parent fe7bf4ef07
commit e3c31e43e5
8 changed files with 258 additions and 71 deletions

View File

@@ -3,7 +3,7 @@
Loaded as classic scripts in the order listed there: they share one global
scope and run top to bottom, exactly as the original did. */
// ── Análise real da folha: lê o canal alfa, separa cada ilha de arte e mede.
// Grade de 1 mm. Só roda em arquivo que o navegador abre; PDF precisa do servidor.
// A grade de 1 mm agrupa a tinta; a medida de cada ilha sai dos pixels.
const MM=0.1; // grade fina: 1 mm
const CELULAS_MAX=1200000; // teto de custo da análise
// Folha grande vira grade mais grossa. Sem isso, 57 x 480 cm dá 2,7 milhões de
@@ -46,39 +46,75 @@ function grupos(bits,cw,ch){
return {marca,n};
}
// ── Mapa de tinta: cada pixel da folha lido, nenhum pulado.
// Regra da Dropstar (21/10/2026): resíduo é uma ilha de tinta menor que 2 mm
// nos dois lados, e um ponto só já barra o pedido. Reduzir a imagem antes de
// olhar perde ponto pequeno, e medir pela grade erra até uma célula: o mesmo
// ponto de 1,5 mm saía resíduo ou arte conforme caía na grade. Aqui a grade só
// agrupa; cada célula guarda onde a tinta começa e termina, em pixels, e cada
// ilha é medida por esses extremos, em milímetros reais.
const ALFA_TINTA=10; // acima disto o pixel leva tinta (e branco de base)
function novoMapa(wPx,hPx,larguraCm){
const pxPorCm=wPx/larguraCm, passo=passoDe(larguraCm,hPx/pxPorCm), pxCel=passo*pxPorCm;
const cw=Math.max(1,Math.ceil(wPx/pxCel)), ch=Math.max(1,Math.ceil(hPx/pxCel)), n=cw*ch;
const colDe=new Uint32Array(wPx);
for(let x=0;x<wPx;x++) colDe[x]=Math.min(cw-1,Math.floor(x/pxCel));
return {wPx,hPx,larguraCm,pxPorCm,passo,pxCel,cw,ch,colDe,bits:new Uint8Array(n),
x0:new Uint32Array(n),x1:new Uint32Array(n),y0:new Uint32Array(n),y1:new Uint32Array(n),tinta:0};
}
// A linha y da folha: o alfa de cada pixel em alfa[inicio + x*de].
function mapaLinha(m,y,alfa,inicio,de){
const base=Math.min(m.ch-1,Math.floor(y/m.pxCel))*m.cw;
const {bits,x0,x1,y0,y1,colDe}=m;
let tinta=0;
for(let x=0,i=inicio;x<m.wPx;x++,i+=de){
if(alfa[i]<=ALFA_TINTA) continue;
tinta++;
const c=base+colDe[x];
if(!bits[c]){ bits[c]=1; x0[c]=x1[c]=x; y0[c]=y1[c]=y; }
else{ if(x<x0[c]) x0[c]=x; else if(x>x1[c]) x1[c]=x; y1[c]=y; }
}
m.tinta+=tinta;
}
// Uma imagem que o navegador abre, lida em faixas no tamanho real (1:1).
async function mapaDaImagem(img,larguraCm){
const w=img.naturalWidth||img.width, h=img.naturalHeight||img.height;
const m=novoMapa(w,h,larguraCm);
const faixa=Math.max(1,Math.min(h,Math.floor(4e6/w)));
const cv=document.createElement('canvas'); cv.width=w; cv.height=faixa;
const ctx=cv.getContext('2d',{willReadFrequently:true});
ctx.imageSmoothingEnabled=false;
for(let topo=0;topo<h;topo+=faixa){
const n=Math.min(faixa,h-topo);
ctx.clearRect(0,0,w,faixa);
ctx.drawImage(img,0,topo,w,n,0,0,w,n);
const d=ctx.getImageData(0,0,w,n).data;
for(let r=0;r<n;r++) mapaLinha(m,topo+r,d,r*w*4+3,4);
await new Promise(r=>setTimeout(r)); // a página não congela
}
return m;
}
// ── O que a folha permite medir, e só isso.
// Peça = pixels a menos de 2 mm entre si · letras de uma palavra ficam juntas.
// Peça = tinta a menos de 2 mm entre si · letras de uma palavra ficam juntas.
// Encostadas = peças que só se unem quando a folga chega a 6 mm.
const R_PECA=1, R_PERTO=2; // peça: 2 mm · aviso de encostadas: 4 mm
function analisarFolha(img, larguraCm){
const alturaCmEst=larguraCm*img.height/img.width;
const passo=passoDe(larguraCm, alturaCmEst);
const cw=Math.max(8,Math.round(larguraCm/passo));
const ch=Math.max(8,Math.round(cw*img.height/img.width));
const cv=document.createElement('canvas'); cv.width=cw; cv.height=ch;
const ctx=cv.getContext('2d',{willReadFrequently:true});
ctx.clearRect(0,0,cw,ch); ctx.drawImage(img,0,0,cw,ch);
const d=ctx.getImageData(0,0,cw,ch).data;
const bits=new Uint8Array(cw*ch);
let opacos=0;
for(let i=0,j=3;i<bits.length;i++,j+=4) if(d[j]>10){ bits[i]=1; opacos++; }
// peças: agrupa pela distância real entre pixels
const RESIDUO_MM=2;
function analisarMapa(m){
const {cw,ch,bits}=m;
const gp=grupos(inchar(bits,cw,ch,R_PECA), cw, ch);
const caixas=[];
for(let p=0;p<bits.length;p++){
if(!bits[p]) continue;
const id=gp.marca[p]; if(id<0) continue;
const x=p%cw, y=(p/cw)|0;
const c=caixas[id] || (caixas[id]={x0:x,x1:x,y0:y,y1:y,area:0,id});
if(x<c.x0)c.x0=x; if(x>c.x1)c.x1=x; if(y<c.y0)c.y0=y; if(y>c.y1)c.y1=y;
c.area++;
const c=caixas[id] || (caixas[id]={x0:m.x0[p],x1:m.x1[p],y0:m.y0[p],y1:m.y1[p],id});
if(m.x0[p]<c.x0)c.x0=m.x0[p]; if(m.x1[p]>c.x1)c.x1=m.x1[p];
if(m.y0[p]<c.y0)c.y0=m.y0[p]; if(m.y1[p]>c.y1)c.y1=m.y1[p];
}
const pecasTodas=caixas.filter(Boolean);
const limRes=Math.max(1, Math.round(0.2/passo)); // 2 mm na grade em uso
const residuos=pecasTodas.filter(o=>(o.x1-o.x0)<limRes && (o.y1-o.y0)<limRes);
const artes=pecasTodas.filter(o=>!((o.x1-o.x0)<limRes && (o.y1-o.y0)<limRes));
const mm=px=>px/m.pxPorCm*10;
const ilhas=caixas.filter(Boolean).map(c=>({...c, wMm:mm(c.x1-c.x0+1), hMm:mm(c.y1-c.y0+1)}));
const residuos=ilhas.filter(c=>c.wMm<RESIDUO_MM && c.hMm<RESIDUO_MM);
const deResiduo=new Set(residuos.map(c=>c.id));
// encostadas: peças diferentes que se unem quando a folga chega a 6 mm
const gper=grupos(inchar(bits,cw,ch,R_PERTO), cw, ch);
@@ -86,22 +122,27 @@ function analisarFolha(img, larguraCm){
for(let p=0;p<bits.length;p++){
if(!bits[p]) continue;
const a=gp.marca[p], b=gper.marca[p];
if(a<0||b<0) continue;
if(a<0||b<0||deResiduo.has(a)) continue;
if(!porPerto.has(b)) porPerto.set(b,new Set());
porPerto.get(b).add(a);
}
let encostadas=0;
porPerto.forEach(set=>{ if(set.size>1) encostadas+=set.size; });
const total=m.wPx*m.hPx;
return {
dpi: Math.round(img.width/(larguraCm/2.54)),
artes: artes.length,
dpi: Math.round(m.wPx/(m.larguraCm/2.54)),
artes: ilhas.length-residuos.length,
residuos: residuos.length,
// Onde está cada ponto, em fração da folha: os círculos da prévia.
pontos: residuos.map(c=>({x:(c.x0+c.x1+1)/2/m.wPx, y:(c.y0+c.y1+1)/2/m.hPx,
mm:+Math.max(c.wMm,c.hMm).toFixed(1)})),
encostadas,
fundoChapado: opacos/(cw*ch) > 0.985,
aproveitamento: Math.max(1,Math.min(100,Math.round(opacos/(cw*ch)*100))),
alturaCm: ch*passo,
grade: +(passo*10).toFixed(1) // mm por célula analisada
// Sem transparência: o que parece branco na tela sai branco no filme.
fundoChapado: (total-m.tinta)/total < 0.015,
aproveitamento: Math.max(1,Math.min(100,Math.round(m.tinta/total*100))),
alturaCm: m.hPx/m.pxPorCm,
grade: +(m.passo*10).toFixed(1) // mm por célula que agrupa
};
}