Changeset 389 for pwntcha


Ignore:
Timestamp:
Jan 3, 2005, 10:48:54 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • use OpenCV rather than Imlib2 if both are available.
  • cleaned up the slashdot code.
  • decode phpBB captchas.
  • added filter_contrast.
Location:
pwntcha/trunk
Files:
4 added
7 edited

Legend:

Unmodified
Added
Removed
  • pwntcha/trunk/configure.ac

    r381 r389  
    1919AC_TYPE_SIZE_T
    2020
     21# Use OpenCV?
     22ac_cv_my_have_opencv="no"
     23save_CPPFLAGS="${CPPFLAGS}"
     24AC_PATH_PROG(OPENCV_CONFIG, opencv-config, no)
     25if test "${OPENCV_CONFIG}" != "no"; then
     26  CPPFLAGS="${CPPFLAGS} `opencv-config --cflags`"
     27fi
     28AC_CHECK_HEADERS(cv.h,
     29 [ac_cv_my_have_opencv="yes"],
     30 [ac_cv_my_have_opencv="no"])
     31CPPFLAGS="${save_CPPFLAGS}"
     32AM_CONDITIONAL(USE_OPENCV, test "${ac_cv_my_have_opencv}" = "yes")
     33
    2134# Use Imlib2?
    2235ac_cv_my_have_imlib2="no"
     
    3245AM_CONDITIONAL(USE_IMLIB2, test "${ac_cv_my_have_imlib2}" = "yes")
    3346
    34 # Use Imlib2?
    35 ac_cv_my_have_opencv="no"
    36 save_CPPFLAGS="${CPPFLAGS}"
    37 AC_PATH_PROG(OPENCV_CONFIG, opencv-config, no)
    38 if test "${OPENCV_CONFIG}" != "no"; then
    39   CPPFLAGS="${CPPFLAGS} `opencv-config --cflags`"
    40 fi
    41 AC_CHECK_HEADERS(cv.h,
    42  [ac_cv_my_have_opencv="yes"],
    43  [ac_cv_my_have_opencv="no"])
    44 CPPFLAGS="${save_CPPFLAGS}"
    45 AM_CONDITIONAL(USE_OPENCV, test "${ac_cv_my_have_opencv}" = "yes")
    46 
    4747if test "${ac_cv_my_have_imlib2}" = "no" -a "${ac_cv_my_have_opencv}" = "no"; then
    4848  AC_MSG_ERROR([[cannot find Imlib2 or OpenCV, please install one of them]])
  • pwntcha/trunk/src/Makefile.am

    r385 r389  
    44pwntcha_CFLAGS = $(ADDITIONAL_CFLAGS) -Wall -O6
    55pwntcha_LDFLAGS = $(ADDITIONAL_LDFLAGS)
    6 pwntcha_SOURCES = main.c image.c filters.c slashdot.c common.h
     6pwntcha_SOURCES = main.c image.c filters.c common.h slashdot.c phpbb.c test.c
    77
     8if USE_OPENCV
     9ADDITIONAL_CFLAGS = `opencv-config --cflags`
     10ADDITIONAL_LDFLAGS = `opencv-config --libs opencv highgui`
     11else
    812if USE_IMLIB2
    913ADDITIONAL_CFLAGS = `imlib2-config --cflags` -DX_DISPLAY_MISSING=1
    1014ADDITIONAL_LDFLAGS = `imlib2-config --libs`
    11 else
    12 if USE_OPENCV
    13 ADDITIONAL_CFLAGS = `opencv-config --cflags`
    14 ADDITIONAL_LDFLAGS = `opencv-config --libs opencv highgui`
    1515else
    1616ADDITIONAL_CFLAGS =
  • pwntcha/trunk/src/common.h

    r387 r389  
    1919
    2020/* available CAPTCHA decoders */
    21 char * decode_slashdot(struct image *img);
     21char *decode_phpbb(struct image *img);
     22char *decode_slashdot(struct image *img);
     23char *decode_test(struct image *img);
    2224
    2325/* image operations */
    24 struct image * image_load(char *name);
    25 struct image * image_new(int width, int height);
     26struct image *image_load(char *name);
     27struct image *image_new(int width, int height);
     28void image_free(struct image *img);
     29void image_display(struct image *img);
    2630int getgray(struct image *img, int x, int y, int *g);
    2731int getpixel(struct image *img, int x, int y, int *r, int *g, int *b);
     
    3236struct image *filter_fill_holes(struct image *img);
    3337struct image *filter_detect_lines(struct image *img);
    34 struct image *filter_equalize(struct image *img);
     38struct image *filter_equalize(struct image *img, int threshold);
    3539struct image *filter_trick(struct image *img);
    3640struct image *filter_smooth(struct image *img);
    3741struct image *filter_median(struct image *img);
     42struct image *filter_contrast(struct image *img);
    3843
  • pwntcha/trunk/src/filters.c

    r387 r389  
    143143}
    144144
    145 struct image *filter_equalize(struct image *img)
     145struct image *filter_equalize(struct image *img, int threshold)
    146146{
    147147    struct image *dst;
     
    155155        {
    156156            getpixel(img, x, y, &r, &g, &b);
    157             if(r < 200) r = 50; else r = 200;
     157            if(r < threshold) r = 0; else r = 255;
    158158            setpixel(dst, x, y, r, r, r);
    159159        }
     
    241241struct image *filter_median(struct image *img)
    242242{
    243 #define MSIZE 4
     243#define MSIZE 3
    244244    struct image *dst;
    245245    int x, y, i, j, val[MSIZE*MSIZE];
     
    279279}
    280280
     281struct image *filter_contrast(struct image *img)
     282{
     283    struct image *dst;
     284    int histo[256];
     285    int x, y, i, min = 255, max = 0;
     286    int r, g, b;
     287
     288    dst = image_new(img->width, img->height);
     289
     290    for(y = 0; y < img->height; y++)
     291        for(x = 0; x < img->width; x++)
     292        {
     293            getgray(img, x, y, &r);
     294            if(r < min) min = r;
     295            if(r > max) max = r;
     296        }
     297
     298    if(min == max)
     299        histo[min] = 127;
     300    else
     301        for(i = min; i < max; i++)
     302            histo[i] = (i - min) * 255 / (max - min);
     303
     304    for(y = 0; y < img->height; y++)
     305        for(x = 0; x < img->width; x++)
     306        {
     307            getgray(img, x, y, &r);
     308            setpixel(dst, x, y, histo[r], histo[r], histo[r]);
     309        }
     310
     311    return dst;
     312}
     313
  • pwntcha/trunk/src/image.c

    r387 r389  
    1616#include "common.h"
    1717
    18 #if defined(HAVE_IMLIB2_H)
    19 #   include <Imlib2.h>
    20 #elif defined(HAVE_CV_H)
     18#if defined(HAVE_CV_H)
    2119#   include <cv.h>
    2220#   include <highgui.h>
     21#elif defined(HAVE_IMLIB2_H)
     22#   include <Imlib2.h>
    2323#else
    2424#   error "No imaging library"
    2525#endif
    2626
    27 struct image * image_load(char *name)
     27struct image *image_load(char *name)
    2828{
    29     struct image * img;
    30 #if defined(HAVE_IMLIB2_H)
     29    struct image *img;
     30#if defined(HAVE_CV_H)
     31    IplImage *priv = cvLoadImage(name, -1);
     32#elif defined(HAVE_IMLIB2_H)
    3133    Imlib_Image priv = imlib_load_image(name);
    32 #elif defined(HAVE_CV_H)
    33     IplImage * priv = cvLoadImage(name, -1);
    3434#endif
    3535
     
    3838
    3939    img = malloc(sizeof(struct image));
    40 #if defined(HAVE_IMLIB2_H)
     40#if defined(HAVE_CV_H)
     41    img->width = priv->width;
     42    img->height = priv->height;
     43    img->pitch = priv->widthStep;
     44    img->channels = priv->nChannels;
     45    img->pixels = priv->imageData;
     46#elif defined(HAVE_IMLIB2_H)
    4147    imlib_context_set_image(priv);
    4248    img->width = imlib_image_get_width();
     
    4551    img->channels = 4;
    4652    img->pixels = (char *)imlib_image_get_data();
    47 #elif defined(HAVE_CV_H)
    48     img->width = priv->width;
    49     img->height = priv->height;
    50     img->pitch = priv->widthStep;
    51     img->channels = priv->nChannels;
    52     img->pixels = priv->imageData;
    5353#endif
    5454    img->priv = (void *)priv;
     
    5757}
    5858
    59 struct image * image_new(int width, int height)
     59struct image *image_new(int width, int height)
    6060{
    61     struct image * img;
    62 #if defined(HAVE_IMLIB2_H)
     61    struct image *img;
     62#if defined(HAVE_CV_H)
     63    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
     64#elif defined(HAVE_IMLIB2_H)
    6365    Imlib_Image priv = imlib_create_image(width, height);
    64 #elif defined(HAVE_CV_H)
    65     IplImage * priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
    6666#endif
    6767
     
    7070
    7171    img = malloc(sizeof(struct image));
    72 #if defined(HAVE_IMLIB2_H)
     72#if defined(HAVE_CV_H)
     73    img->width = priv->width;
     74    img->height = priv->height;
     75    img->pitch = priv->widthStep;
     76    img->channels = priv->nChannels;
     77    img->pixels = priv->imageData;
     78#elif defined(HAVE_IMLIB2_H)
    7379    imlib_context_set_image(priv);
    7480    img->width = imlib_image_get_width();
     
    7783    img->channels = 4;
    7884    img->pixels = (char *)imlib_image_get_data();
    79 #elif defined(HAVE_CV_H)
    80     img->width = priv->width;
    81     img->height = priv->height;
    82     img->pitch = priv->widthStep;
    83     img->channels = priv->nChannels;
    84     img->pixels = priv->imageData;
    8585#endif
    8686    img->priv = (void *)priv;
    8787
    8888    return img;
     89}
     90
     91void image_free(struct image *img)
     92{
     93#if defined(HAVE_CV_H)
     94    IplImage *iplimg;
     95    iplimg = (IplImage *)img->priv;
     96    cvReleaseImage(&iplimg);
     97#elif defined(HAVE_IMLIB2_H)
     98    imlib_context_set_image(img->priv);
     99    imlib_free_image();
     100#endif
     101
     102    free(img);
    89103}
    90104
     
    133147void image_display(struct image *img)
    134148{
    135 #if defined(HAVE_IMLIB2_H)
     149#if defined(HAVE_CV_H)
     150    char name[BUFSIZ];
     151    sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
     152    cvNamedWindow(name, 0);
     153    cvShowImage(name, img->priv);
     154    cvResizeWindow(name, img->width * 2, img->height * 2 + 50);
     155    while((unsigned char)cvWaitKey(0) != 0x1b)
     156        ;
     157#elif defined(HAVE_IMLIB2_H)
    136158    //char name[BUFSIZ];
    137159    //static int i = 0;
     
    140162    //imlib_save_image(name);
    141163    //fprintf(stderr, "saved to %s\n", name);
    142 #elif defined(HAVE_CV_H)
    143     char name[BUFSIZ];
    144     sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
    145     cvNamedWindow(name, 0);
    146     cvShowImage(name, img->priv);
    147     cvResizeWindow(name, 320, 120);
    148164#endif
    149165}
  • pwntcha/trunk/src/main.c

    r388 r389  
    1212#include <stdio.h>
    1313#include <stdlib.h>
     14#include <string.h>
    1415#include <getopt.h>
    1516
     
    8889        }
    8990
    90         result = decode_slashdot(img);
     91        if(!strcmp(mode, "test"))
     92            result = decode_test(img);
     93        else if(!strcmp(mode, "phpbb"))
     94            result = decode_phpbb(img);
     95        else if(!strcmp(mode, "slashdot"))
     96            result = decode_slashdot(img);
     97        else
     98        {
     99            if(img->width == 320 && img->height == 50)
     100                result = decode_phpbb(img);
     101            else if(img->height == 69)
     102                result = decode_slashdot(img);
     103            else
     104            {
     105                fprintf(stderr, "%s: could not guess CAPTCHA type\n", argv[0]);
     106                printf("\n");
     107                image_free(img);
     108                continue;
     109            }
     110        }
     111
     112        image_free(img);
     113
    91114        if(!result)
    92115        {
  • pwntcha/trunk/src/slashdot.c

    r387 r389  
    3131/* Global stuff */
    3232struct { int xmin, ymin, xmax, ymax; } objlist[100];
    33 int objects = 0, first = -1, last = -1;
     33int objects, first, last;
    3434char *result;
    3535
     
    3737char * decode_slashdot(struct image *img)
    3838{
    39     struct image *tmp, *tmp2;
     39    struct image *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
     40
     41    /* Initialise local data */
     42    objects = 0;
     43    first = -1;
     44    last = -1;
    4045
    4146    /* Slashdot captchas have 7 characters */
     
    4348
    4449    /* Clean image a bit */
    45     tmp = filter_detect_lines(img);
    46     tmp = filter_fill_holes(tmp);
     50    tmp1 = filter_detect_lines(img);
     51    tmp2 = filter_fill_holes(tmp1);
    4752
    4853    /* Detect small objects to guess image orientation */
    49     tmp2 = filter_median(tmp);
    50     tmp2 = filter_equalize(tmp2);
    51     count_objects(tmp2);
     54    tmp3 = filter_median(tmp2);
     55    tmp4 = filter_equalize(tmp3, 200);
     56    count_objects(tmp4);
    5257
    5358    /* Invert rotation and find glyphs */
    54     tmp = rotate(tmp);
    55     tmp = filter_median(tmp);
    56     tmp = find_glyphs(tmp);
     59    tmp5 = rotate(tmp2);
     60    tmp6 = filter_median(tmp5);
     61    tmp7 = find_glyphs(tmp6);
     62
     63    /* Clean up our mess */
     64    image_free(tmp1);
     65    image_free(tmp2);
     66    image_free(tmp3);
     67    image_free(tmp4);
     68    image_free(tmp5);
     69    image_free(tmp6);
     70    image_free(tmp7);
     71
     72    /* aaaaaaa means decoding failed */
     73    if(!strcmp(result, "aaaaaaa"))
     74        result[0] = '\0';
    5775
    5876    return result;
Note: See TracChangeset for help on using the changeset viewer.