chiark / gitweb /
WIP ocr
[ypp-sc-tools.db-test.git] / pctb / convert.c
1 #include <pam.h>
2 #include <stdint.h>
3 #include <inttypes.h>
4 #include <assert.h>
5 #include <string.h>
6
7 #define eassert assert
8
9 typedef struct {
10   unsigned long rgb; /* on screen */
11   char c; /* canonical */
12 } CanonColourInfo;
13
14 static const CanonColourInfo canoncolourinfos[]= {
15   { 0x475A5E, '*' }, /* edge */
16   { 0x7D9094, '+' }, /* interbox */
17   { 0xBDC5BF, ' ' }, /* background - pale */
18   { 0xADB5AF, ' ' }, /* background - dark */
19   { 0x000000, 'o' }, /* foreground */
20   { 0xD4B356, ' ' }, /* background (cursor) */
21   { 0xFFFFFF, 'o' }, /* foreground (cursor) */
22   { 0,0 }
23 };
24
25 static int height, width;
26 static char *image;
27
28 static void load_image_and_canonify(void) {
29   struct pam inpam;
30   unsigned char rgb[3];
31   int x,y,r;
32   const CanonColourInfo *cci;
33
34   pnm_readpaminit(stdin, &inpam, sizeof(inpam));
35   height= inpam.height;
36   width= inpam.width;
37   eassert(inpam.maxval == 255);
38   eassert(inpam.bytes_per_sample == 1);
39
40   image= malloc(width*height);
41   eassert(image);
42   memset(image,'?',width*height);
43
44   for (y=0; y<height; y++) {
45     for (x=0; x<width; x++) {
46       r= fread(&rgb,1,3,stdin);  eassert(r==3);
47       unsigned long rgb_l=
48         ((unsigned long)rgb[0]<<16) |
49         ((unsigned long)rgb[1]<<8) |
50                        (rgb[2]);
51       for (cci=canoncolourinfos; cci->c; cci++)
52         if (cci->rgb == rgb_l) {
53           image[y*width + x]= cci->c;
54           break;
55         }
56       if (y==234 && x==82) {
57         printf("y=%d/%d x=%d/%d rgb=%d,%d,%d rgb_l=%lx c=%c\n",
58                y,height,x,width, rgb[0],rgb[1],rgb[2], rgb_l, image[y*width+x]);
59       }
60     }
61     r= fwrite(image + y*width, 1,width, stdout);  eassert(r==width);
62     putchar('\n');
63   }
64   eassert(!fflush(stdout));
65   eassert(!ferror(stdout));
66 }
67  
68 int main(void) {
69   load_image_and_canonify();
70   /*
71   find_main_rectangle();
72   repeatedly_find_top_thing();
73   */
74   return 0;
75 }