// Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. // Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). // This is not open-source software: see the license for permitted uses. // Arrays are contiguous float64, int32, or byte booleans, owned by Python. // Coordinates use attention-grid cells; valid indices retain transcript positions. // Compile without fused arithmetic to preserve reference feature rounding. #include #include #include static double median(std::vector& v) { std::sort(v.begin(),v.end()); const auto n=v.size(); return n%2 ? v[n/2] : (v[n/2-1]+v[n/2])/2.0; } extern "C" void neighborhood(int n, int m, const double* xy, const double* lengths, const int* owner, const int* valid, double* out) { for (int r=0;r=0 && v nearby;std::vector ys,adv; for(int v=std::max(0,u-12);v=.1 && ratio<=1.5)adv.push_back(delta/chars); } row[16]=nearby.size();row[17]=adv.empty()?0:median(adv);row[18]=ys.empty()?0:median(ys)-py; } } // Sum neighbors in transcript order to match the reference floating-point order. extern "C" void pool_profiles(int n, int m, const double* xy, const int* owner, const int* valid, const int* lookup, const double* profiles, double* out) { for(int r=0;r1)continue; const int offset=static_cast(std::nearbyint(xy[2*v+1]-xy[2*u+1])); for(int k=0;k<5;++k)row[1+offset+k]+=profiles[5*lookup[v]+k]; ++count; } if(count)for(int k=0;k<7;++k)row[k]/=count; row[7]=count; } } // The caller supplies a true seed and a zeroed output mask. Visit only its // eight-connected component; unrelated components do not need labels. extern "C" void peak_component_bounds(int h, int w, const unsigned char* mask, int seed, unsigned char* selected, int* bounds) { bounds[0]=seed%w; bounds[1]=seed/w; bounds[2]=bounds[0]+1; bounds[3]=bounds[1]+1; std::vector queue;queue.push_back(seed);selected[seed]=1; for(size_t k=0;k #include // Interleave eight independent rows to overlap dependent node loads. Each row // still accumulates leaves in tree order; the tail handles incomplete groups. // Compact nodes avoid converting integer links and fit two nodes per cache line. struct TreeNode { double value, threshold; int feature, left, right, flags; }; static_assert(sizeof(TreeNode)==32); extern "C" void predict_compact_trees(int rows,int cols,const double* x,int trees, const int* roots,const TreeNode* nodes,double baseline,double* out) { std::fill(out,out+rows,baseline); for(int t=0;t #include extern "C" int component_boxes(int h,int w,const uint8_t* mask,int* out) { const int n=h*w;std::vector seen(n);std::vector q;q.reserve(n);int count=0; for(int seed=0;seedx1)x1=x+1;if(yy1)y1=y+1; for(int dy=-1;dy<=1;++dy)for(int dx=-1;dx<=1;++dx){int nx=x+dx,ny=y+dy; if(nx<0||nx>=w||ny<0||ny>=h)continue;int j=ny*w+nx; if(mask[j]&&!seen[j]){seen[j]=1;q.push_back(j);} } } out[count*4]=x0;out[count*4+1]=y0;out[count*4+2]=x1;out[count*4+3]=y1;++count; } return count; } // Bounds are clipped, nonempty cell-edge rectangles in x0,y0,x1,y1 order. extern "C" void box_maxima(int width,const double* values,int count, const int* bounds,double* out) { for(int k=0;k=maximum)maximum=v; } out[k]=maximum; } } // Share marginal sums and cumulative mass across quantile rectangles. #include #include extern "C" void mass_boxes(int h,int w,const double* v,int count,const double* q,double* out) { std::vector x(w,0),y(h,0); for(int r=0;r