#include #include #include #include // convert hex output to png: // convert -depth 8 -size 33x33 gray:raw.nb x.png // draw square -> calc how much of a pixel is covered by the square, and ADD proportionally // // addsquare(double x, double y, double w, double h) // addcircle(double x, double y, double r) typedef uint8_t Pixel; typedef std::vector PixelVector; typedef std::vector PixelMatrix; double distance(double x0, double y0, double x1, double y1) { return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)); } class bitmap { PixelMatrix _m; int _w, _h; public: int width() const { return _w; } int height() const { return _h; } bitmap(int w, int h) : _w(w), _h(h) { _m.resize(h); for (int i=0 ; i=width() || y<0 || y>=height()) return 0; return _m[y][x]; } void setpixel(int x, int y, Pixel p) { if (x<0 || x>=width() || y<0 || y>=height()) return; _m[y][x]= (_m[y][x]+p)>>1; } void setpixel(double x, double y, Pixel p) { int ix= x*width(); int iy= y*height(); double dx0= double(ix)/width()-x; double dx1= double(ix+1)/width()-x; double dy0= double(iy)/height()-y; double dy1= double(iy+1)/height()-y; setpixel(ix, iy, p*dx0/(dx0+dx1)*dy0/(dy0+dy1)); setpixel(ix, iy+1, p*dx0/(dx0+dx1)*dy1/(dy0+dy1)); setpixel(ix+1, iy, p*dx1/(dx0+dx1)*dy0/(dy0+dy1)); setpixel(ix+1, iy+1, p*dx1/(dx0+dx1)*dy1/(dy0+dy1)); } void blit(double x, double y, double scale, const bitmap& src) { for (int bmx= 0 ; bmx movie; bitmap createcircle(int r) { bitmap bm(2*r+1, 2*r+1); for (double phi= 0 ; phi<2*3.1416 ; phi+=3.1416/100) { bm.setpixel(cos(phi)/3+0.5, sin(phi)/3+0.5, 255); } return bm; } double correlate(const bitmap& a, const bitmap& b, double dx, double dy) { double sum=0; for (double x= 0 ; x<1 ; x+=0.001) for (double y= 0 ; y<1 ; y+=0.001) sum += a.getpixel(x, y)*b.getpixel(x+dx, y+dy); return sum; } // create image of circle of diameter 16 // // create movie of circle shrank to diameter 4, moving left to right // // // calc autocorrelation of subsequent pics of movie // // enlarge first image by 4 // then apply average of auto correlate position of next pic to each pixel int main(int argc, char**argv) { bitmap circle= createcircle(16); circle.dump(); movie mov; for (double x= 0 ; x<1 ; x+=0.01) { bitmap frame(16, 16); frame.blit(x, 0.5, 0.25, circle); mov.push_back(frame); printf("\n"); frame.dump(); } // for (double dx=-0.1 ; dx<0.1 ; dx+=0.01) // for (double dy=-0.1 ; dy<0.1 ; dy+=0.01) // printf("%6.2f %6.2f : %g\n", dx, dy, correlate(mov[0], mov[1], dx, dy)); return 0; }