From: Ian Jackson Date: Sun, 31 May 2009 23:23:46 +0000 (+0100) Subject: Can find main rectangle X-Git-Tag: 1.9.2~199 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=commitdiff_plain;h=f9c00eb882d1758e95422d8f40ef5606422cffa3 Can find main rectangle --- diff --git a/pctb/convert.c b/pctb/convert.c index 80bc894..95e75b3 100644 --- a/pctb/convert.c +++ b/pctb/convert.c @@ -5,14 +5,37 @@ #include #define eassert assert +#define debug stdout typedef struct { unsigned long rgb; /* on screen */ char c; /* canonical */ } CanonColourInfo; +static int height, width; +static char *image; + +static void debug_flush(void) { + eassert(!fflush(debug)); + eassert(!ferror(debug)); +} + +typedef struct { + int x, y; +} Point; + +typedef struct { /* both inclusive */ + Point tl; + Point br; +} Rect; + +static inline char get(int x, int y) { return image[y * width + x]; } +static inline char get_p(Point p) { return get(p.x,p.y); } + + static const CanonColourInfo canoncolourinfos[]= { { 0x475A5E, '*' }, /* edge */ + { 0x2C5F7A, '*' }, /* edge just under box heading shadow */ { 0x7D9094, '+' }, /* interbox */ { 0xBDC5BF, ' ' }, /* background - pale */ { 0xADB5AF, ' ' }, /* background - dark */ @@ -22,8 +45,78 @@ static const CanonColourInfo canoncolourinfos[]= { { 0,0 } }; -static int height, width; -static char *image; +#define START_MAIN {200,200} + +static void require_rectangle(int tlx, int tly, int brx, int bry, char c) { + int x,y; + for (x=tlx; x<=brx; x++) + for (y=tly; y<=bry; y++) + eassert(get(x,y) == c); +} +static void require_rectangle_r(Rect rr, char c) { + require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, c); +} + +static void debug_rect(const char *what, int whati, Rect rr) { + int y,r,w; + fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati, + rr.tl.x,rr.tl.y, rr.br.x,rr.br.y); + w= rr.br.x - rr.tl.x + 1; + for (y=rr.tl.y; y<=rr.br.y; y++) { + fprintf(debug, "%4d %4d ",y,y-rr.tl.y); + r= fwrite(image + y*width + rr.tl.x, 1, w, debug); + eassert(r==w); + fputc('|',debug); + fputc('\n',debug); + } + debug_flush(); +} + +static void find_main_rectangle(void) { + Rect whole = { {0,0}, {width-1,height-1} }; + Rect mainr = { START_MAIN,START_MAIN }; + +#define WALK_UNTIL(point,coord,increm,stop,edge) \ + for (;;) { \ + if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \ + eassert((point).coord != (stop)); \ + (point).coord += (increm); \ + } + + WALK_UNTIL(mainr.tl, x,-1, whole.tl.x, '*'); + WALK_UNTIL(mainr.tl, y,-1, whole.tl.y, '*'); + WALK_UNTIL(mainr.br, x,+1, whole.br.x, '*'); + WALK_UNTIL(mainr.br, y,+1, whole.br.y, '*'); + + require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, '*'); + require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, '*'); + require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, '*'); + require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, '*'); + +#define CHECK_STRIP_BORDER(tlbr,xy,increm) \ + do { \ + Point csb_p; \ + Rect csb_r; \ + csb_p= mainr.tl; \ + csb_p.xy= mainr.tlbr.xy; \ + if (get_p(csb_p)=='+') { \ + csb_r= mainr; \ + csb_r.tl.xy= csb_p.xy; \ + csb_r.br.xy= csb_p.xy; \ + require_rectangle_r(csb_r, '+'); \ + mainr.tlbr.xy += increm; \ + } \ + } while(0) + + debug_rect("mainr",0, mainr); + + CHECK_STRIP_BORDER(tl,x,+1); + CHECK_STRIP_BORDER(tl,y,+1); + CHECK_STRIP_BORDER(br,x,-1); + CHECK_STRIP_BORDER(br,y,-1); + + debug_rect("mainr",1, mainr); +} static void load_image_and_canonify(void) { struct pam inpam; @@ -53,22 +146,18 @@ static void load_image_and_canonify(void) { image[y*width + x]= cci->c; break; } - if (y==234 && x==82) { - printf("y=%d/%d x=%d/%d rgb=%d,%d,%d rgb_l=%lx c=%c\n", - y,height,x,width, rgb[0],rgb[1],rgb[2], rgb_l, image[y*width+x]); - } } - r= fwrite(image + y*width, 1,width, stdout); eassert(r==width); - putchar('\n'); + fprintf(debug, "%4d ",y); + r= fwrite(image + y*width, 1,width, debug); eassert(r==width); + fputc('\n',debug); } - eassert(!fflush(stdout)); - eassert(!ferror(stdout)); + debug_flush(); } - + int main(void) { load_image_and_canonify(); - /* find_main_rectangle(); + /* repeatedly_find_top_thing(); */ return 0;