Changeset 453 for pwntcha


Ignore:
Timestamp:
Jan 12, 2005, 3:28:37 AM (18 years ago)
Author:
Sam Hocevar
Message:
  • attempt at the xanga decoder. roughly 50% success.
  • fonts useful to the xanga decoder. x_* fonts are convert'ed with -blur 3.
Location:
pwntcha/trunk
Files:
12 added
2 edited

Legend:

Unmodified
Added
Removed
  • pwntcha/trunk/src/main.c

    r448 r453  
    210210
    211211        printf("%s\n", result);
     212        fflush(stdout);
    212213        free(result);
    213214    }
  • pwntcha/trunk/src/xanga.c

    r448 r453  
    2020
    2121static void fill_white_holes(struct image *img);
     22static void find_glyphs(struct image *img);
     23
     24static char *result;
    2225
    2326/* Main function */
    2427char *decode_xanga(struct image *img)
    2528{
    26     static struct font *font1 = NULL, *font2 = NULL, *font3 = NULL;
    2729    struct image *tmp;
    28     char *result;
    29 
    30     if(!font1)
    31     {
    32         font1 = font_load_variable("font_freemonobold_32_az.bmp",
    33                                    "abcdefghijklmnopqrstuvwxyz");
    34         if(!font1)
    35             exit(1);
    36     }
    37 
    38     if(!font2)
    39     {
    40         font2 = font_load_variable("font_freemonobold_32_az.bmp",
    41                                    "abcdefghijklmnopqrstuvwxyz");
    42         if(!font2)
    43             exit(1);
    44     }
    45 
    46     if(!font3)
    47     {
    48         font3 = font_load_variable("font_freemonobold_32_az.bmp",
    49                                    "abcdefghijklmnopqrstuvwxyz");
    50         if(!font3)
    51             exit(1);
    52     }
    53 
    54     /* Xanga captchas have 7 characters */
    55     result = malloc(8 * sizeof(char));
    56     strcpy(result, "       ");
     30
     31    /* Xanga captchas have 6 characters */
     32    result = malloc(7 * sizeof(char));
     33    strcpy(result, "      ");
    5734
    5835    tmp = image_dup(img);
     
    6542    fill_white_holes(tmp);
    6643//    filter_fill_holes(tmp);
     44    filter_smooth(tmp);
     45    //filter_median(tmp);
    6746image_save(tmp, "xanga3.bmp");
    6847    //filter_detect_lines(tmp);
    6948//    filter_median(tmp);
    7049//image_save(tmp, "xanga4.bmp");
    71     filter_equalize(tmp, 128);
     50//    filter_equalize(tmp, 128);
     51    filter_contrast(tmp);
    7252image_save(tmp, "xanga4.bmp");
    73 return NULL;
    74 
     53
     54#if 0
    7555    /* Detect small objects to guess image orientation */
    7656    filter_median(tmp);
     
    7959    /* Invert rotation and find glyphs */
    8060    filter_median(tmp);
     61#endif
     62    find_glyphs(tmp);
     63image_save(tmp, "xanga5.bmp");
    8164
    8265    /* Clean up our mess */
     
    131114}
    132115
     116static void find_glyphs(struct image *img)
     117{
     118#define FONTS 6
     119    static struct font *fonts[FONTS];
     120    static char *files[] =
     121    {
     122        "x_font_freemonobold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz",
     123        "x_font_freemonobold_24_az.bmp", "abcdefghijklmnopqrstuvwxyz",
     124        "x_font_freesansbold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz",
     125        //"x_font_freeserifbold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz",
     126        "x_font_comic_32_az.bmp", "abcdefghijklmnopqrstuvwxyz",
     127        "x_font_comic_24_az_messed.bmp", "abcdefghijklmnopqrstuvwxyz",
     128        "x_font_freesansbold_36_az_messed.bmp", "abcdefghijklmnopqrstuvwxyz",
     129    };
     130    struct image *tmp;
     131    int x, y, i = 0, f;
     132    int r, g, b;
     133    int xmin, xmax, ymin, ymax, startx = 10, cur = 0;
     134    int bestdist, bestfont, bestx, besty, bestch;
     135
     136    for(f = 0; f < FONTS; f++)
     137    {
     138        if(!fonts[f])
     139        {
     140            fonts[f] = font_load_variable(files[f * 2], files[f * 2 + 1]);
     141            if(!fonts[f])
     142                exit(1);
     143            //filter_smooth(fonts[f]->img);
     144            //filter_contrast(fonts[f]->img);
     145        }
     146    }
     147
     148    tmp = image_new(img->width, img->height);
     149
     150    for(y = 0; y < img->height; y++)
     151        for(x = 0; x < img->width; x++)
     152        {
     153            getpixel(img, x, y, &r, &g, &b);
     154            setpixel(tmp, x, y, 255, g, 255);
     155        }
     156
     157    while(cur < 6)
     158    {
     159        /* Try to find 1st letter */
     160        bestdist = INT_MAX;
     161        for(f = 0; f < FONTS; f++) for(i = 0; i < fonts[f]->size; i++)
     162        {
     163            int localmin = INT_MAX, localx, localy;
     164int sqr;
     165            if(fonts[f]->glyphs[i].c == 'l' || fonts[f]->glyphs[i].c == 'z')
     166                continue;
     167            xmin = fonts[f]->glyphs[i].xmin - 5;
     168            ymin = fonts[f]->glyphs[i].ymin - 3;
     169            xmax = fonts[f]->glyphs[i].xmax + 5;
     170            ymax = fonts[f]->glyphs[i].ymax + 3;
     171sqr = sqrt(xmax - xmin);
     172            for(y = -15; y < 15; y++)
     173                //for(x = startx; x < startx + 15; x++)
     174                for(x = 22 - (xmax - xmin) / 2 + 25 * cur; x < 28 - (xmax - xmin) / 2 + 25 * cur; x++)
     175                {
     176                    int z, t, dist;
     177                    dist = 0;
     178                    for(t = 0; t < ymax - ymin; t++)
     179                        for(z = 0; z < xmax - xmin; z++)
     180                        {
     181                            int r2;
     182                            getgray(fonts[f]->img, xmin + z, ymin + t, &r);
     183                            getgray(img, x + z, y + t, &r2);
     184                            if(r < r2)
     185                                dist += (r - r2) * (r - r2);
     186                            else
     187                                dist += (r - r2) * (r - r2) * 3 / 4;
     188                        }
     189    //                printf("%i %i -> %i\n", x, y, dist);
     190//                    dist /= (xmax - xmin);
     191//                    dist = dist / sqrt((ymax - ymin) * (xmax - xmin)) / (xmax - xmin);
     192                    dist = dist / (xmax - xmin) / sqr;
     193//                    dist = dist * 128 / fonts[f]->glyphs[i].count;
     194                    if(dist < localmin)
     195                    {
     196                        localmin = dist;
     197                        localx = x;
     198                        localy = y;
     199                    }
     200                }
     201            //fprintf(stderr, "%i (%i,%i)\n", localmin, localx - startx, localy);
     202            if(localmin < bestdist)
     203            {
     204//printf("  bestch is now %i (%c) in font %i\n", i, fonts[f]->glyphs[i].c, f);
     205                bestdist = localmin;
     206                bestfont = f;
     207                bestx = localx;
     208                besty = localy;
     209                bestch = i;
     210            }
     211        }
     212//printf("%i (%c) in font %i\n", i, fonts[bestfont]->glyphs[bestch].c, bestfont);
     213//printf("%i < %i < %i\n", 10 + 25 * cur, bestx, 30 + 25 * cur);
     214
     215        /* Draw best glyph in picture (debugging purposes) */
     216        xmin = fonts[bestfont]->glyphs[bestch].xmin - 5;
     217        ymin = fonts[bestfont]->glyphs[bestch].ymin - 3;
     218        xmax = fonts[bestfont]->glyphs[bestch].xmax + 5;
     219        ymax = fonts[bestfont]->glyphs[bestch].ymax + 3;
     220        for(y = 0; y < ymax - ymin; y++)
     221            for(x = 0; x < xmax - xmin; x++)
     222            {
     223                getpixel(fonts[bestfont]->img, xmin + x, ymin + y, &r, &g, &b);
     224                if(r > 128) continue;
     225                setpixel(tmp, bestx + x, besty + y, r, g, b);
     226            }
     227
     228        startx = bestx + xmax - xmin;
     229        result[cur++] = fonts[bestfont]->glyphs[bestch].c;
     230    }
     231
     232    image_swap(img, tmp);
     233    image_free(tmp);
     234}
     235
Note: See TracChangeset for help on using the changeset viewer.