 Timestamp:
 Jan 3, 2005, 4:12:41 PM (16 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

pwntcha/trunk/src/slashdot.c
r385 r386 20 20 /* Our macros */ 21 21 #define FACTOR 1 22 #define FONTNAME "share/font_dilated_half.png" // use with FACTOR = 1 22 23 //#define FONTNAME "share/font.png" // use with FACTOR = 2 23 24 //#define FONTNAME "share/font_dilated.png" // use with FACTOR = 2 24 #define FONTNAME "share/font_dilated_half.png" // use with FACTOR = 1 25 26 static struct image *count_objects(struct image *img); 27 static struct image *rotate(struct image *img); 28 static struct image *cut_cells(struct image *img); 29 static struct image *find_glyphs(struct image *img); 25 30 26 31 /* Global stuff */ 32 struct { int xmin, ymin, xmax, ymax; } objlist[100]; 33 int objects = 0, first = 1, last = 1; 27 34 char *result; 28 35 29 struct 30 { 31 int xmin, ymin, xmax, ymax; 32 } 33 objlist[100]; 34 int objects = 0, first = 1, last = 1; 35 36 /* Functions */ 37 38 struct image *count_objects(struct image *img) 36 /* Main function */ 37 char * slashdot_decode(char *image) 38 { 39 struct image *img, *tmp, *tmp2; 40 41 img = load_image(image); 42 if(img == NULL) 43 return NULL; 44 45 /* Slashdot captchas have 7 characters */ 46 result = malloc(8 * sizeof(char)); 47 48 /* Clean image a bit */ 49 tmp = detect_lines(img); 50 tmp = fill_holes(tmp); 51 52 /* Detect small objects to guess image orientation */ 53 tmp2 = median(tmp); 54 tmp2 = equalize(tmp2); 55 count_objects(tmp2); 56 57 /* Invert rotation and find glyphs */ 58 tmp = rotate(tmp); 59 tmp = median(tmp); 60 tmp = find_glyphs(tmp); 61 62 return result; 63 } 64 65 /* The following functions are local */ 66 67 static struct image *count_objects(struct image *img) 39 68 { 40 69 struct image *dst; … … 108 137 } 109 138 110 st ruct image *rotate(struct image *img)139 static struct image *rotate(struct image *img) 111 140 { 112 141 struct image *dst; 113 142 int x, y, xdest, ydest; 114 int r, g, b, R, G, B; 143 int r, g, b; 144 //int R, G, B; 115 145 int X = objlist[first].xmin  objlist[last].xmin; 116 146 int Y = objlist[first].ymin  objlist[last].ymin; … … 151 181 } 152 182 153 st ruct image *cut_cells(struct image *img)183 static struct image *cut_cells(struct image *img) 154 184 { 155 185 struct image *dst; … … 182 212 } 183 213 184 st ruct image *find_glyphs(struct image *img)214 static struct image *find_glyphs(struct image *img) 185 215 { 186 216 char all[] = "abcdefgijkmnpqrstvwxyz"; … … 194 224 struct image *font = load_image(FONTNAME); 195 225 int x, y, i = 0; 196 int r, g, b , r2, g2, b2;226 int r, g, b; 197 227 int xmin, xmax, ymin, ymax, incell = 0, count = 0, startx = 0, cur = 0; 198 228 int distmin, distx, disty, distch; … … 278 308 } 279 309 280 while(cur < 7)281 {282 /* Try to find 1st letter */283 distmin = 999999999;284 for(i = 0; i < 22; i++)285 {286 int localmin = 99999999, localx, localy;310 while(cur < 7) 311 { 312 /* Try to find 1st letter */ 313 distmin = 999999999; 314 for(i = 0; i < 22; i++) 315 { 316 int localmin = 99999999, localx, localy; 287 317 //if(all[i] == 'i') continue; 288 xmin = glyphs[i].xmin; 289 ymin = glyphs[i].ymin; 290 xmax = glyphs[i].xmax; 291 ymax = glyphs[i].ymax; 292 //printf("trying to find %c (%i×%i)  ", all[i], xmax  xmin, ymax  ymin); 293 for(y = 5 * FACTOR; y < 5 * FACTOR; y++) 294 for(x = startx  5 * FACTOR; x < startx + 5 * FACTOR; x++) 295 { 296 int z, t, dist; 297 dist = 0; 298 for(t = 0; t < ymax  ymin; t++) 299 for(z = 0; z < xmax  xmin; z++) 318 xmin = glyphs[i].xmin; 319 ymin = glyphs[i].ymin; 320 xmax = glyphs[i].xmax; 321 ymax = glyphs[i].ymax; 322 //printf("trying to find %c (%i×%i)  ", all[i], xmax  xmin, ymax  ymin); 323 for(y = 5 * FACTOR; y < 5 * FACTOR; y++) 324 for(x = startx  5 * FACTOR; x < startx + 5 * FACTOR; x++) 325 { 326 int z, t, dist; 327 dist = 0; 328 for(t = 0; t < ymax  ymin; t++) 329 for(z = 0; z < xmax  xmin; z++) 330 { 331 int r2; 332 getgray(font, xmin + z, ymin + t, &r); 333 getgray(img, x + z, y + t, &r2); 334 dist += abs(r  r2); 335 } 336 // printf("%i %i > %i\n", x, y, dist); 337 //dist /= sqrt(xmax  xmin); 338 dist = dist * 128 / glyphs[i].count; 339 if(dist < localmin) 300 340 { 301 getgray(font, xmin + z, ymin + t, &r);302 getgray(img, x + z, y + t, &r2);303 dist += abs(r  r2);341 localmin = dist; 342 localx = x; 343 localy = y; 304 344 } 305 // printf("%i %i > %i\n", x, y, dist); 306 //dist /= sqrt(xmax  xmin); 307 dist = dist * 128 / glyphs[i].count; 308 if(dist < localmin) 309 { 310 localmin = dist; 311 localx = x; 312 localy = y; 313 } 314 } 315 //fprintf(stderr, "%i (%i,%i)\n", localmin, localx  startx, localy); 316 if(localmin < distmin) 317 { 318 distmin = localmin; 319 distx = localx; 320 disty = localy; 321 distch = i; 322 } 323 } 324 325 //fprintf(stderr, "%i (%i,%i)\n", distmin, distx  startx, disty); 326 //printf("min diff: %c  %i (%i, %i)\n", all[distch], distmin, distx, disty); 327 328 /* Print min glyph */ 329 xmin = glyphs[distch].xmin; 330 ymin = glyphs[distch].ymin; 331 xmax = glyphs[distch].xmax; 332 ymax = glyphs[distch].ymax; 333 for(y = 0; y < ymax  ymin; y++) 334 for(x = 0; x < xmax  xmin; x++) 335 { 336 getpixel(font, xmin + x, ymin + y, &r, &g, &b); 337 if(r > 128) continue; 338 setpixel(dst, distx + x, disty + y, r, g, b); 339 } 340 341 startx = distx + xmax  xmin; 342 result[cur++] = all[distch]; 343 } 345 } 346 //fprintf(stderr, "%i (%i,%i)\n", localmin, localx  startx, localy); 347 if(localmin < distmin) 348 { 349 distmin = localmin; 350 distx = localx; 351 disty = localy; 352 distch = i; 353 } 354 } 355 356 //fprintf(stderr, "%i (%i,%i)\n", distmin, distx  startx, disty); 357 //printf("min diff: %c  %i (%i, %i)\n", all[distch], distmin, distx, disty); 358 359 /* Print min glyph */ 360 xmin = glyphs[distch].xmin; 361 ymin = glyphs[distch].ymin; 362 xmax = glyphs[distch].xmax; 363 ymax = glyphs[distch].ymax; 364 for(y = 0; y < ymax  ymin; y++) 365 for(x = 0; x < xmax  xmin; x++) 366 { 367 getpixel(font, xmin + x, ymin + y, &r, &g, &b); 368 if(r > 128) continue; 369 setpixel(dst, distx + x, disty + y, r, g, b); 370 } 371 372 startx = distx + xmax  xmin; 373 result[cur++] = all[distch]; 374 } 344 375 345 376 return dst; 346 377 } 347 378 348 char * slashdot_decode(char *image)349 {350 struct image *img, *tmp, *tmp2;351 352 img = load_image(image);353 if(img == NULL)354 return NULL;355 356 result = malloc(8 * sizeof(char));357 // display(img);358 359 // tmp = equalize(img);360 // display(tmp);361 // tmp = fill_holes(tmp);362 // display(tmp);363 364 // dst = median(tmp);365 //tmp = smooth(img);366 tmp = fill_holes(img);367 tmp = median(tmp);368 //tmp = smooth(tmp);369 //display(tmp);370 //tmp = trick(tmp);371 //display(tmp);372 tmp = equalize(tmp);373 //display(tmp);374 375 tmp = detect_lines(img);376 tmp = fill_holes(tmp);377 //tmp = cut_cells(tmp);378 //display_image(tmp);379 380 tmp2 = median(tmp);381 tmp2 = equalize(tmp2);382 count_objects(tmp2);383 //display_image(tmp2);384 385 //tmp = median(tmp);386 tmp = rotate(tmp);387 tmp = median(tmp);388 //display_image(tmp);389 //tmp = equalize(tmp);390 //tmp = cut_cells(tmp);391 // cvSaveImage(argv[2], tmp);392 393 tmp = find_glyphs(tmp);394 //display_image(tmp);395 396 // cvSaveImage(argv[3], tmp);397 398 return result;399 }400
Note: See TracChangeset
for help on using the changeset viewer.