bool QxPotrace::trace(const QImage &image) { QElapsedTimer timer; timer.start(); potrace_bitmap_t *bitmap = bitmapFromImage(image, m_threshold); potrace_param_t *params = potrace_param_default(); params->alphamax = m_alphaMax; params->opttolerance = m_curveTolerance; params->turdsize = m_turdSize; // params->progress.callback = &Tracer::progress; potrace_state_t *st = potrace_trace(params, bitmap); bm_free(bitmap); potrace_param_free(params); if(!st || st->status != POTRACE_STATUS_OK) return false; m_meshDefs = tracedPolygonsFromPath(st->plist, m_bezierPrecision); potrace_state_free(st); return true; }
int main(int argc, char** argv) { int i; prepare_bit_table(sizeof (BitMapPart) * 8); BitMap left = bm_initialize(16 * 8, "left", 1); BitMap right = bm_initialize(16 * 8, "right", 1); BitMap or = bm_initialize(16 * 8, "OR", 0); BitMap and = bm_initialize(16 * 8, "AND", 0); BitMap xor = bm_initialize(16 * 8, "XOR", 0); BitMap not = bm_initialize(16 * 8, "NOT", 0); BitMap large = bm_initialize(16 * 8, "large", 1); bm_resize(large, 1024 * 1024 * 8); for (i = 0; i < large->part_length; i++) { large->parts[i] = rand(); } for (i = 0; i < 2; i++) { left->parts[i] = rand(); right->parts[i] = rand(); } bm_or(bm_copy(or, left), right); bm_and(bm_copy(and, left), right); bm_xor(bm_copy(xor, left), right); bm_not(bm_copy(not, left), left); printf("| i/d | i/d | l | r |or |and|xor|not|\n"); int d = sizeof (BitMapPart) * 8; for (i = 0; i < 16; i++) { printf("| %3d | %3d | %d | %d |%2d |%2d |%2d |%2d |\n", i / d, i % d, bit(left->parts[i / d], i % d), bit(right->parts[i / d], i % d), bit(or->parts[i / d], i % d), bit(and->parts[i / d], i % d), bit(xor->parts[i / d], i % d), bit(not->parts[i / d], i % d) ); } bm_free(left); bm_free(right); bm_free(or); bm_free(and); }
int main() { int i, j; bitmatrix bm = bm_new(MAX, MAX); // writing for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { bm_set(bm, i, j, i == j); } } // reading for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { assert(bm_get(bm, i, j) == (i == j)); } } bm_free(bm); }
int main() { int x, y, i; potrace_bitmap_t *bm; potrace_param_t *param; potrace_path_t *p; potrace_state_t *st; int n, *tag; potrace_dpoint_t(*c)[3]; /* create a bitmap */ bm = bm_new(WIDTH, HEIGHT); if (!bm) { fprintf(stderr, "Error allocating bitmap: %s\n", strerror(errno)); return 1; } /* fill the bitmap with some pattern */ for (y = 0; y < HEIGHT; y++) { for (x = 0; x < WIDTH; x++) { BM_PUT(bm, x, y, ((x * x + y * y * y) % 10000 < 5000) ? 1 : 0); } } /* set tracing parameters, starting from defaults */ param = potrace_param_default(); if (!param) { fprintf(stderr, "Error allocating parameters: %s\n", strerror(errno)); return 1; } param->turdsize = 0; /* trace the bitmap */ st = potrace_trace(param, bm); if (!st || st->status != POTRACE_STATUS_OK) { fprintf(stderr, "Error tracing bitmap: %s\n", strerror(errno)); return 1; } bm_free(bm); /* output vector data, e.g. as a rudimentary EPS file */ printf("%%!PS-Adobe-3.0 EPSF-3.0\n"); printf("%%%%BoundingBox: 0 0 %d %d\n", WIDTH, HEIGHT); printf("gsave\n"); /* draw each curve */ p = st->plist; while (p != NULL) { n = p->curve.n; tag = p->curve.tag; c = p->curve.c; printf("%f %f moveto\n", c[n - 1][2].x, c[n - 1][2].y); for (i = 0; i < n; i++) { switch (tag[i]) { case POTRACE_CORNER: printf("%f %f lineto\n", c[i][1].x, c[i][1].y); printf("%f %f lineto\n", c[i][2].x, c[i][2].y); break; case POTRACE_CURVETO: printf("%f %f %f %f %f %f curveto\n", c[i][0].x, c[i][0].y, c[i][1].x, c[i][1].y, c[i][2].x, c[i][2].y); break; } } /* at the end of a group of a positive path and its negative children, fill. */ if (p->next == NULL || p->next->sign == '+') { printf("0 setgray fill\n"); } p = p->next; } printf("grestore\n"); printf("%%EOF\n"); potrace_state_free(st); potrace_param_free(param); return 0; }
BMCanvas::~BMCanvas() { bm_free(bmp); }
void BMCanvas::resize(int x, int y, int w, int h) { bm_free(bmp); bmp = bm_create(w, h); Fl_Widget::resize(x, y, w, h); }
void operator delete[](void* ptr) { return bm_free(ptr); }
static void process_file(backend_t *b, const char *infile, const char *outfile, FILE *fin, FILE *fout) { int r; potrace_bitmap_t *bm = NULL; imginfo_t imginfo; int eof_flag = 0; /* to indicate premature eof */ int count; /* number of bitmaps successfully processed, this file */ potrace_state_t *st; for (count=0; ; count++) { /* read a bitmap */ r = bm_read(fin, info.blacklevel, &bm); switch (r) { case -1: /* system error */ fprintf(stderr, ""POTRACE": %s: %s\n", infile, strerror(errno)); exit(2); case -2: /* corrupt file format */ fprintf(stderr, ""POTRACE": %s: file format error: %s\n", infile, bm_read_error); exit(2); case -3: /* empty file */ if (count>0) { /* end of file */ return; } fprintf(stderr, ""POTRACE": %s: empty file\n", infile); exit(2); case -4: /* wrong magic */ if (count>0) { fprintf(stderr, ""POTRACE": %s: warning: junk at end of file\n", infile); return; } fprintf(stderr, ""POTRACE": %s: file format not recognized\n", infile); fprintf(stderr, "Possible input file formats are: pnm (pbm, pgm, ppm), bmp.\n"); exit(2); case 1: /* unexpected end of file */ fprintf(stderr, ""POTRACE": warning: %s: premature end of file\n", infile); eof_flag = 1; break; } /* prepare progress bar, if requested */ if (info.progress) { r = info.progress_bar->init(&info.param->progress, infile, count); if (r) { fprintf(stderr, ""POTRACE": %s\n", strerror(errno)); exit(2); } } else { info.param->progress.callback = NULL; } if (info.invert) { bm_invert(bm); } /* process the image */ st = potrace_trace(info.param, bm); if (!st || st->status != POTRACE_STATUS_OK) { fprintf(stderr, ""POTRACE": %s: %s\n", infile, strerror(errno)); exit(2); } /* calculate image dimensions */ imginfo.pixwidth = bm->w; imginfo.pixheight = bm->h; bm_free(bm); calc_dimensions(&imginfo, st->plist); r = b->page_f(fout, st->plist, &imginfo); if (r) { fprintf(stderr, ""POTRACE": %s: %s\n", outfile, strerror(errno)); exit(2); } potrace_state_free(st); if (info.progress) { info.progress_bar->term(&info.param->progress); } if (eof_flag || !b->multi) { return; } } /* not reached */ }
JNIEXPORT jobject JNICALL Java_com_jiangpeng_android_antrace_Utils_traceImage(JNIEnv* env, jobject thiz, jobject bitmap) { AndroidBitmapInfo info; int ret = 0; void* src_pixels = 0; if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) { return NULL; } if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { return NULL; } if ((ret = AndroidBitmap_lockPixels(env, bitmap, &src_pixels)) < 0) { return NULL; } potrace_param_t* param_t = potrace_param_default(); param_t->turdsize = 15; param_t->opttolerance = 0.8; potrace_bitmap_t* bmp_t = bm_new(info.width, info.height); //memcpy(bmp_t->map, src_pixels, bmp_t->dy * bmp_t->h * BM_WORDSIZE); const int kShiftBits = 20; const int32_t kRedRatio = static_cast<int32_t>((1 << kShiftBits) * 0.21f); const int32_t kGreenRatio = static_cast<int32_t>((1 << kShiftBits) * 0.71f); const int32_t kBlueRatio = static_cast<int32_t>((1 << kShiftBits) * 0.07f); for (uint32_t scan_line = 0; scan_line < info.height; scan_line++) { pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels); pixel32_t* src_line_end = src + info.width; int x = 0; while (src < src_line_end) { int32_t src_red = src->rgba8[0]; int32_t src_green = src->rgba8[1]; int32_t src_blue = src->rgba8[2]; int32_t src_alpha = src->rgba8[3]; int32_t dst_color = (kRedRatio * src_red + kGreenRatio * src_green + kBlueRatio * src_blue) >> kShiftBits; if (dst_color > 128) { BM_PUT(bmp_t, x, info.height - 1 - scan_line, 1); } else { BM_PUT(bmp_t, x, info.height - 1 - scan_line, 0); } src++; ++x; } src_pixels = reinterpret_cast<char*>(src_pixels) + info.stride; } if(s_state != NULL) { potrace_state_free(s_state); s_state = NULL; } s_state = potrace_trace(param_t, bmp_t); potrace_param_free(param_t); bm_free(bmp_t); AndroidBitmap_unlockPixels(env, bitmap); if (!s_state || s_state->status != POTRACE_STATUS_OK) { return NULL; } potrace_path_t* start = s_state->plist; jobject prev = 0; jclass cls = env->FindClass("com/jiangpeng/android/antrace/Objects/path"); jobject retPath = 0; for(potrace_path_t* n = start; n != 0; n = n->next) { jobject path = createPath(env, n); if(retPath == 0) { retPath = path; } if(prev != 0) { jfieldID fid = env->GetFieldID(cls, "next", "Lcom/jiangpeng/android/antrace/Objects/path;"); env->SetObjectField(prev, fid, path); env->DeleteLocalRef(prev); } prev = path; } env->DeleteLocalRef(cls); return retPath; }
static void process_file(FILE *fin, FILE *fout, char *infile, char *outfile) { int r; greymap_t *gm; potrace_bitmap_t *bm; void *sm; int x, y; int count; for (count=0; ; count++) { r = gm_read(fin, &gm); switch (r) { case -1: /* system error */ fprintf(stderr, "mkbitmap: %s: %s\n", infile, strerror(errno)); exit(2); case -2: /* corrupt file format */ fprintf(stderr, "mkbitmap: %s: file format error: %s\n", infile, gm_read_error); exit(2); case -3: /* empty file */ if (count>0) { /* end of file */ return; } fprintf(stderr, "mkbitmap: %s: empty file\n", infile); exit(2); case -4: /* wrong magic */ if (count>0) { fprintf(stderr, "mkbitmap: %s: warning: junk at end of file\n", infile); return; } fprintf(stderr, "mkbitmap: %s: file format not recognized\n", infile); fprintf(stderr, "Possible input file formats are: pnm (pbm, pgm, ppm), bmp.\n"); exit(2); case 1: /* unexpected end of file */ fprintf(stderr, "mkbitmap: %s: warning: premature end of file\n", infile); break; } if (info.invert) { for (y=0; y<gm->h; y++) { for (x=0; x<gm->w; x++) { GM_UPUT(gm, x, y, 255-GM_UGET(gm, x, y)); } } } if (info.highpass) { r = highpass(gm, info.lambda); if (r) { fprintf(stderr, "mkbitmap: %s: %s\n", infile, strerror(errno)); exit(2); } } if (info.lowpass) { lowpass(gm, info.lambda1); } if (info.scale == 1 && info.bilevel) { /* no interpolation necessary */ sm = threshold(gm, info.level); gm_free(gm); } else if (info.scale == 1) { sm = gm; } else if (info.linear) { /* linear interpolation */ sm = interpolate_linear(gm, info.scale, info.bilevel, info.level); gm_free(gm); } else { /* cubic interpolation */ sm = interpolate_cubic(gm, info.scale, info.bilevel, info.level); gm_free(gm); } if (!sm) { fprintf(stderr, "mkbitmap: %s: %s\n", infile, strerror(errno)); exit(2); } if (info.bilevel) { bm = (potrace_bitmap_t *)sm; bm_writepbm(fout, bm); bm_free(bm); } else { gm = (greymap_t *)sm; gm_writepgm(fout, gm, NULL, 1, GM_MODE_POSITIVE, 1.0); gm_free(gm); } } }
int main(int argc, char *argv[]) { int opt; int fullscreen = 0, resizable = 0, borderless = 0; const char *appTitle = DEFAULT_APP_TITLE; const char *game_dir = NULL; const char *pak_filename = "game.pak"; const char *rlog_filename = "rengine.log"; const char *startstate; struct game_state *gs = NULL; int demo = 0; SDL_version compiled, linked; log_init(rlog_filename); json_error = rerror; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) { rerror("SDL_Init: %s", SDL_GetError()); return 1; } atexit(SDL_Quit); while((opt = getopt(argc, argv, "p:g:l:d?")) != -1) { switch(opt) { case 'p': { pak_filename = optarg; } break; case 'g' : { game_dir = optarg; pak_filename = NULL; } break; case 'l': { rlog_filename = optarg; } break; case 'd': { demo = 1; } break; case '?' : { usage(argv[0]); return 1; } } } if(!getcwd(initial_dir, sizeof initial_dir)) { rerror("error in getcwd(): %s", strerror(errno)); return 1; } rlog("Running engine from %s", initial_dir); if(!gdb_new()) { rerror("Unable to create Game Database"); return 1; } re_initialize(); states_initialize(); if(!snd_init()) { rerror("Terminating because of audio problem."); return 1; } /* Don't quite know how to use this in Windows yet. */ SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN); SDL_Log("Testing Log capability."); SDL_VERSION(&compiled); SDL_GetVersion(&linked); rlog("SDL version %d.%d.%d (compile)", compiled.major, compiled.minor, compiled.patch); rlog("SDL version %d.%d.%d (link)", linked.major, linked.minor, linked.patch); if(!demo) { if(pak_filename) { rlog("Loading game PAK file: %s", pak_filename); if(!rs_read_pak(pak_filename)) { rerror("Unable to open PAK file '%s'; Playing demo mode.", pak_filename); goto start_demo; } } else { rlog("Not using a PAK file. Using '%s' instead.", game_dir); if(chdir(game_dir)) { rerror("Unable to change to '%s': %s", game_dir, strerror(errno)); return 1; } } game_ini = re_get_ini(GAME_INI); if(game_ini) { appTitle = ini_get(game_ini, "init", "appTitle", "Rengine"); screenWidth = atoi(ini_get(game_ini, "screen", "width", PARAM(SCREEN_WIDTH))); screenHeight = atoi(ini_get(game_ini, "screen", "height", PARAM(SCREEN_HEIGHT))); resizable = atoi(ini_get(game_ini, "screen", "resizable", "0")) ? SDL_WINDOW_RESIZABLE : 0; borderless = atoi(ini_get(game_ini, "screen", "borderless", "0")) ? SDL_WINDOW_BORDERLESS : 0; fullscreen = atoi(ini_get(game_ini, "screen", "fullscreen", "0")) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; fps = atoi(ini_get(game_ini, "screen", "fps", PARAM(DEFAULT_FPS))); if(fps <= 0) fps = DEFAULT_FPS; filter = !my_stricmp(ini_get(game_ini, "screen", "filter", "nearest"), "linear")? "1": "0"; virt_width = atoi(ini_get(game_ini, "virtual", "width", PARAM(VIRT_WIDTH))); virt_height = atoi(ini_get(game_ini, "virtual", "height", PARAM(VIRT_HEIGHT))); show_cursor = atoi(ini_get(game_ini, "mouse", "show-cursor", PARAM(1)))? 1 : 0; startstate = ini_get(game_ini, "init", "startstate", NULL); if(startstate) { gs = get_state(startstate); if(!gs) { rerror("Unable to get initial state: %s", startstate); return 1; } } else { rerror("No initial state in %s", GAME_INI); return 1; } } else { rerror("Unable to load %s", GAME_INI); return 1; } } else { start_demo: rlog("Starting demo mode"); gs = get_demo_state("demo"); } rlog("Initialising..."); if(!init(appTitle, fullscreen | borderless | resizable)) { return 1; } if(!bmf_init()){ rlog("Quiting; Unable to start FreeType font library"); return 1; } assert(gs); rlog("Entering initial state..."); if(!change_state(gs)) { rlog("Quiting, because of earlier problems with the initial state"); return 1; } frameStart = SDL_GetTicks(); rlog("Event loop starting..."); while(!quit) { gs = current_state(); if(!gs) { break; } if(gs->update) gs->update(gs, bmp); advanceFrame(); } rlog("Event loop stopped."); gs = current_state(); if(gs && gs->deinit) gs->deinit(gs); bm_free(bmp); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); ini_free(game_ini); re_clean_up(); bmf_deinit(); gdb_save("dump.db"); /* For testing the game database functionality. Remove later. */ gdb_close(); snd_deinit(); rlog("Engine shut down."); return 0; }
int bitmap2component( potrace_bitmap_t* aPotrace_bitmap, FILE* aOutfile, OUTPUT_FMT_ID aFormat, int aDpi_X, int aDpi_Y, BMP2CMP_MOD_LAYER aModLayer ) { potrace_param_t* param; potrace_state_t* st; // set tracing parameters, starting from defaults param = potrace_param_default(); if( !param ) { fprintf( stderr, "Error allocating parameters: %s\n", strerror( errno ) ); return 1; } param->turdsize = 0; /* convert the bitmap to curves */ st = potrace_trace( param, aPotrace_bitmap ); if( !st || st->status != POTRACE_STATUS_OK ) { if( st ) { potrace_state_free( st ); } potrace_param_free( param ); fprintf( stderr, "Error tracing bitmap: %s\n", strerror( errno ) ); return 1; } BITMAPCONV_INFO info; info.m_PixmapWidth = aPotrace_bitmap->w; info.m_PixmapHeight = aPotrace_bitmap->h; // the bitmap size in pixels info.m_Paths = st->plist; info.m_Outfile = aOutfile; switch( aFormat ) { case KICAD_LOGO: info.m_Format = KICAD_LOGO; info.m_ScaleX = 1e3 * 25.4 / aDpi_X; // the conversion scale from PPI to micro info.m_ScaleY = 1e3 * 25.4 / aDpi_Y; // Y axis is top to bottom info.CreateOutputFile(); break; case POSTSCRIPT_FMT: info.m_Format = POSTSCRIPT_FMT; info.m_ScaleX = 1.0; // the conversion scale info.m_ScaleY = info.m_ScaleX; // output vector data, e.g. as a rudimentary EPS file (mainly for tests) info.CreateOutputFile(); break; case EESCHEMA_FMT: info.m_Format = EESCHEMA_FMT; info.m_ScaleX = 1000.0 / aDpi_X; // the conversion scale from PPI to UI info.m_ScaleY = -1000.0 / aDpi_Y; // Y axis is bottom to Top for components in libs info.CreateOutputFile(); break; case PCBNEW_KICAD_MOD: info.m_Format = PCBNEW_KICAD_MOD; info.m_ScaleX = 1e6 * 25.4 / aDpi_X; // the conversion scale from PPI to UI info.m_ScaleY = 1e6 * 25.4 / aDpi_Y; // Y axis is top to bottom in modedit info.CreateOutputFile( aModLayer ); break; default: break; } bm_free( aPotrace_bitmap ); potrace_state_free( st ); potrace_param_free( param ); return 0; }