int main(int argc, char **argv) { NOTUSED(argc); NOTUSED(argv); initConfig(); parseOptions(argc,argv); initScreen(); initEditor(l81.fb,30,30,30,30); editorOpen(l81.filename); while(1) { resetProgram(); loadProgram(); if (!l81.luaerr) { SDL_setFramerate(&l81.fb->fps_mgr,l81.fps); l81.start_ms = mstime(); while(!processSdlEvents()); if (editorFileWasModified()) editorSave(l81.filename); } editorRun(); } return 0; }
int main(int argc, char **argv) { FILE *fp; int width, height, alpha; unsigned char *image, *fb; SDL_Surface *screen; struct triangles *triangles, *best, *absbest; long long diff; float percdiff, bestdiff; /* Initialization */ srand(time(NULL)); state.max_shapes = 64; state.max_shapes_incremental = 1; state.temperature = 0.10; state.generation = 0; state.absbestdiff = 100; /* 100% is worst diff possible. */ /* Check arity and parse additional args if any. */ if (argc < 4) { showHelp(argv[0]); exit(1); } if (argc > 4) { int j; for (j = 4; j < argc; j++) { int moreargs = j+1 < argc; if (!strcmp(argv[j],"--use-triangles") && moreargs) { opt_use_triangles = atoi(argv[++j]); } else if (!strcmp(argv[j],"--use-circles") && moreargs) { opt_use_circles = atoi(argv[++j]); } else if (!strcmp(argv[j],"--max-shapes") && moreargs) { state.max_shapes = atoi(argv[++j]); } else if (!strcmp(argv[j],"--initial-shapes") && moreargs) { state.max_shapes_incremental = atoi(argv[++j]); } else if (!strcmp(argv[j],"--mutation-rate") && moreargs) { opt_mutation_rate = atoi(argv[++j]); } else if (!strcmp(argv[j],"--restart")) { opt_restart = 1; } else if (!strcmp(argv[j],"--help")) { showHelp(argv[0]); } else { fprintf(stderr,"Invalid options."); showHelp(argv[0]); exit(1); } } } /* Sanity check. */ if (state.max_shapes_incremental > state.max_shapes) state.max_shapes = state.max_shapes_incremental; if (opt_mutation_rate > 1000) opt_mutation_rate = 1000; /* Load the PNG in memory. */ fp = fopen(argv[1],"rb"); if (!fp) { perror("Opening PNG file"); exit(1); } if ((image = PngLoad(fp,&width,&height,&alpha)) == NULL) { printf("Can't load the specified image."); exit(1); } printf("Image %d %d, alpha:%d at %p\n", width, height, alpha, image); fclose(fp); /* Initialize SDL and allocate our arrays of triangles. */ screen = sdlInit(width,height,0); fb = malloc(width*height*3); triangles = mkRandomtriangles(state.max_shapes,width,height); best = mkRandomtriangles(state.max_shapes,width,height); absbest = mkRandomtriangles(state.max_shapes,width,height); state.absbestdiff = bestdiff = 100; /* Load the binary file if any. */ if (!opt_restart) { loadBinary(argv[2],best); } else { best->inuse = state.max_shapes_incremental; } absbest->inuse = best->inuse; memcpy(absbest->triangles,best->triangles, sizeof(struct triangle)*best->count); /* Show the current evolved image and the real image for one scond each. */ memset(fb,0,width*height*3); drawtriangles(fb,width,height,best); sdlShowRgb(screen,fb,width,height); sleep(1); sdlShowRgb(screen,image,width,height); sleep(1); /* Evolve the current solution using simulated annealing. */ while(1) { state.generation++; if (state.temperature > 0 && !(state.generation % 10)) { state.temperature -= 0.00001; if (state.temperature < 0) state.temperature = 0; } /* From time to time allow the current solution to use one more * triangle, up to the configured max number. */ if ((state.generation % 1000) == 0) { if (state.max_shapes_incremental < triangles->count && triangles->inuse > state.max_shapes_incremental-1) { state.max_shapes_incremental++; } } /* Copy what is currenly the best solution, and mutate it. */ memcpy(triangles->triangles,best->triangles, sizeof(struct triangle)*best->count); triangles->inuse = best->inuse; mutatetriangles(triangles,10,width,height); /* Draw the mutated solution, and check what is its fitness. * In our case the fitness is the difference bewteen the target * image and our image. */ memset(fb,0,width*height*3); drawtriangles(fb,width,height,triangles); diff = computeDiff(image,fb,width,height); /* The percentage of difference is calculate taking the ratio between * the maximum difference and the current difference. * The magic constant 422 is actually the max difference between * two pixels as r,g,b coordinates in the space, so sqrt(255^2*3). */ percdiff = (float)diff/(width*height*442)*100; if (percdiff < bestdiff || (state.temperature > 0 && ((float)rand()/RAND_MAX) < state.temperature && (percdiff-state.absbestdiff) < 2*state.temperature)) { /* Save what is currently our "best" solution, even if actually * this may be a jump backward depending on the temperature. * It will be used as a base of the next iteration. */ best->inuse = triangles->inuse; memcpy(best->triangles,triangles->triangles, sizeof(struct triangle)*best->count); if (percdiff < bestdiff) { /* We always save a copy of the absolute best solution we found * so far, after some generation without finding anything better * we may jump back to that solution. * * We also use the absolute best solution to save the program * state in the binary file, and as SVG output. */ absbest->inuse = best->inuse; memcpy(absbest->triangles,best->triangles, sizeof(struct triangle)*best->count); state.absbestdiff = percdiff; } printf("Diff is %f%% (inuse:%d, max:%d, gen:%lld, temp:%f)\n", percdiff, triangles->inuse, state.max_shapes_incremental, state.generation, state.temperature); bestdiff = percdiff; sdlShowRgb(screen,fb,width,height); } processSdlEvents(); /* From time to time save the current state into a binary save * and produce an SVG of the current solution. */ if ((state.generation % 100) == 0) { saveSvg(argv[3],absbest,width,height); saveBinary(argv[2],absbest); } } return 0; }