- Timestamp:
- Jan 12, 2005, 3:28:37 AM (18 years ago)
- Location:
- pwntcha/trunk
- Files:
-
- 12 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pwntcha/trunk/src/main.c
r448 r453 210 210 211 211 printf("%s\n", result); 212 fflush(stdout); 212 213 free(result); 213 214 } -
pwntcha/trunk/src/xanga.c
r448 r453 20 20 21 21 static void fill_white_holes(struct image *img); 22 static void find_glyphs(struct image *img); 23 24 static char *result; 22 25 23 26 /* Main function */ 24 27 char *decode_xanga(struct image *img) 25 28 { 26 static struct font *font1 = NULL, *font2 = NULL, *font3 = NULL;27 29 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, " "); 57 34 58 35 tmp = image_dup(img); … … 65 42 fill_white_holes(tmp); 66 43 // filter_fill_holes(tmp); 44 filter_smooth(tmp); 45 //filter_median(tmp); 67 46 image_save(tmp, "xanga3.bmp"); 68 47 //filter_detect_lines(tmp); 69 48 // filter_median(tmp); 70 49 //image_save(tmp, "xanga4.bmp"); 71 filter_equalize(tmp, 128); 50 // filter_equalize(tmp, 128); 51 filter_contrast(tmp); 72 52 image_save(tmp, "xanga4.bmp"); 73 return NULL; 74 53 54 #if 0 75 55 /* Detect small objects to guess image orientation */ 76 56 filter_median(tmp); … … 79 59 /* Invert rotation and find glyphs */ 80 60 filter_median(tmp); 61 #endif 62 find_glyphs(tmp); 63 image_save(tmp, "xanga5.bmp"); 81 64 82 65 /* Clean up our mess */ … … 131 114 } 132 115 116 static 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; 164 int 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; 171 sqr = 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.