void init_gnugraph() { int k; int c; /* color, 0-15 */ int g; /* gray level, 0-15 */ GrSetMode(GR_default_graphics); numcolors = GrNumColors(); if ( (numcolors < 256) && color_flag ) shading_flag = 0; /* not enough for shading */ /* first index background color */ for ( c = 0 ; c < 16 ; c++ ) color_perm[c] = c; color_perm[background_color] = 0; color_perm[0] = background_color; if ( shading_flag && color_flag ) { for ( c = 0 ; c < 16 ; c++ ) for ( g = 0 ; g <= MAXGRAY ; g++ ) GrSetColor(CINDEX(c,g), (int)(0xff*(g+12.)/(MAXGRAY+12.)*rgb_colors[c][0]), (int)(0xff*(g+12.)/(MAXGRAY+12.)*rgb_colors[c][1]), (int)(0xff*(g+12.)/(MAXGRAY+12.)*rgb_colors[c][2])); } else if ( shading_flag ) /* shading */ { for ( g = 0 ; g <= MAXGRAY ; g++ ) GrSetColor(CINDEX(WHITE,g), (int)(0xff*(g+12.)/(MAXGRAY+12.)), (int)(0xff*(g+12.)/(MAXGRAY+12.)), (int)(0xff*(g+12.)/(MAXGRAY+12.))); } else if ( color_flag ) { for ( c = 0 ; c < 16 ; c++ ) GrSetColor(CINDEX(c,MAXGRAY), (int)(0xff*rgb_colors[c][0]), (int)(0xff*rgb_colors[c][1]), (int)(0xff*rgb_colors[c][2])); } if ( numcolors == 32768 ) GrClearScreen(CINDEX(WHITE,MAXGRAY)); else { GrFreeColor(0); GrSetColor(0,(int)(0xff*rgb_colors[background_color][0]), (int)(0xff*rgb_colors[background_color][1]), (int)(0xff*rgb_colors[background_color][2])); } maxx = GrMaxX(); maxy = GrMaxY(); if ( maxx/3 < maxy/3 ) { xscale = maxx/3; yscale = xscale; /* assume square pixel screen */ } else { yscale = maxy/3; xscale = yscale; /* assume square pixel screen */ } }
double PortraitCut::BVZ_ComputeSpatialOnly() { double E=0; int i,j,index=0, k; Coord np; for (j=0; j<_h; j++) for (i=0; i<_w; i++, ++index) { Coord coord(i,j); for (k=0; k<(int)NEIGHBOR_NUM; k++){ np = coord + NEIGHBORS[k]; if (np>=Coord(0,0) && np<_size) E += BVZ_interaction_penalty(coord,np,_labels[index], _labels[CINDEX(np)]); } } //assert(_finite(E) && !_isnan(E)); return E; }
void DoStatus ( void ) { struct lineAttr rglaStatus[10]; /* color array for status line */ int cch; int ilaStatus = 0; /* index into color array */ int i; char *pchEndBuf; /* save for end of buffer */ char buf[512]; /* * Start with filename, and file type */ strcpy (buf, pFileHead->pName); strcat (buf, " ("); strcpy ((char *)strend(buf), GetFileTypeName ()); /* * Add other file characterisctics */ if (!TESTFLAG (FLAGS (pFileHead), DOSFILE)) { strcat (buf," NL"); } if (TESTFLAG (FLAGS (pFileHead), TEMP)) { strcat (buf, " temp"); } if ((TESTFLAG (FLAGS (pFileHead), READONLY)) | fGlobalRO) { strcat (buf, " No-Edit"); } if (TESTFLAG (FLAGS (pFileHead), DISKRO)) { strcat (buf, " RO-File"); } rglaStatus[ilaStatus].attr = CINDEX(staColor); rglaStatus[ilaStatus++].len = (unsigned char) strlen (buf); if (TESTFLAG (FLAGS(pFileHead), DIRTY)) { strcat (buf, " modified"); rglaStatus[ilaStatus].attr = CINDEX(errColor); rglaStatus[ilaStatus++].len = 9; } pchEndBuf = strend (buf); sprintf (strend(buf), ") Length=%ld ", pFileHead->cLines); /* * Add current location */ if (fDisplayCursorLoc) { sprintf (strend(buf), "Cursor=(%ld,%d)", YCUR(pInsCur)+1, XCUR(pInsCur)+1); } else { sprintf (strend(buf), "Window=(%ld,%d)", YWIN(pInsCur)+1, XWIN(pInsCur)+1); } rglaStatus[ilaStatus].attr = CINDEX(staColor); rglaStatus[ilaStatus++].len = (unsigned char) (strend(buf) - pchEndBuf); /* * Add global state indicators */ if (fInsert | fMeta | fCtrlc | fMacroRecord) { rglaStatus[ilaStatus].attr = CINDEX(infColor); rglaStatus[ilaStatus].len = 0; if (fInsert) { strcat (buf, " insert"); rglaStatus[ilaStatus].len += 7; } if (fMeta) { strcat (buf, " meta"); rglaStatus[ilaStatus].len += 5; } if (fCtrlc) { strcat (buf, " cancel"); rglaStatus[ilaStatus].len += 7; fCtrlc = FALSE; FlushInput (); } if (fMacroRecord) { strcat (buf, " REC"); rglaStatus[ilaStatus].len += 4; } ilaStatus++; } rglaStatus[ilaStatus].attr = CINDEX(staColor); rglaStatus[ilaStatus].len = 0xffff; pchEndBuf = buf; /* * if the net result is too long, eat the first part of the filename with * an elipses (Leave room for BC as well). */ cch = strlen(buf) - (XSIZE - 4); if (cch > 0) { pchEndBuf = buf + cch; pchEndBuf[0] = '.'; pchEndBuf[1] = '.'; pchEndBuf[2] = '.'; i = 0; while ( cch && i <= ilaStatus ) { if ( (int)rglaStatus[i].len > cch ) { rglaStatus[i].len -= cch; cch = 0; } else { cch -= rglaStatus[i].len; rglaStatus[i].len = 0; } i++; } } fReDraw = FALSE; coutb (0, YSIZE+1, pchEndBuf, strlen(pchEndBuf), rglaStatus); fReDraw = TRUE; voutb (XSIZE-2, YSIZE+1, BTWorking() ? "BP" : " ", 2, errColor); RSETFLAG (fDisplay, RSTATUS); }
double PortraitCut::BVZ_Expand(ushort a, double E_old) { Coord p, np; ushort l, nl; Graph *g; float delta, P_00, P_0a, P_a0; Graph::node_id index, nindex; int k, ind=0; double E; /* indeces_a stores node indeces */ /* D_a sto res penalties for assigning label a */ g = new Graph(BVZ_error_function); /* initializing */ E = 0.; for (p.y=0; p.y<_h; p.y++) for (p.x=0; p.x<_w; p.x++, ++ind) { l = _labels[ind]; if (a == l) // 和原始的label一样 { IMREF(indeces_a, p) = INDEX_ACTIVE; //返回点p在graph中的节点索引,默认为0 E += BVZ_data_penalty(p, l); continue; } // 而void *则不同,任何类型的指针都可以直接赋值给它,无需进行强制类型转换://void *p1; //int *p2; // p1 = p2 //label 不一样的话 IMREF(indeces_a, p) = g -> add_node(); delta = BVZ_data_penalty(p, l); IMREF(D_a, p) = BVZ_data_penalty(p, a) - delta; E += delta; } ind=0; for (p.y=0; p.y<_h; p.y++) for (p.x=0; p.x<_w; p.x++, ++ind) { l = _labels[ind]; index = (Graph::node_id) IMREF(indeces_a, p); /* adding interactions */ for (k=0; k<(int)NEIGHBOR_NUM; k++) { np = p + NEIGHBORS[k]; if ( ! ( np>=Coord(0,0) && np<_size ) ) continue; //HUM nl = _labels[CINDEX(np)]; nindex = (Graph::node_id) IMREF(indeces_a, np);//获取在graph 中node的id if (IS_NODE_ID(index)) { if (IS_NODE_ID(nindex)) { P_00 = BVZ_interaction_penalty(p, np, l, nl); P_0a = BVZ_interaction_penalty(p, np, l, a); P_a0 = BVZ_interaction_penalty(p, np, a, nl); delta = (P_00 < P_0a) ? P_00 : P_0a; if (delta > 0) { IMREF(D_a, p) -= delta; E += delta; P_00 -= delta; P_0a -= delta; } delta = (P_00 < P_a0) ? P_00 : P_a0; if (delta > 0) { IMREF(D_a, np) -= delta; E += delta; P_00 -= delta; P_a0 -= delta; } if (P_00 > 0.0001) { fprintf(_fp, "ERROR: BVZ_interaction_penalty() is non-metric %f!\n",P_00); fflush(_fp); /*assert(0);*/ } #ifdef BVZ_ALPHA_SINK g -> add_edge(index, nindex, P_0a, P_a0); #else g -> add_edge(index, nindex, P_a0, P_0a); #endif } else { delta = BVZ_interaction_penalty(p, np, l, a); IMREF(D_a, p) -= delta; E += delta; } } else { if (IS_NODE_ID(nindex)) { delta = BVZ_interaction_penalty(p, np, a, nl); IMREF(D_a, np) -= delta; E += delta; } } }// end for neighbor }// end for image-pixel /* adding source and sink edges */ for (p.y=0; p.y<_h; p.y++) for (p.x=0; p.x<_w; p.x++) { index = (Graph::node_id) IMREF(indeces_a, p); if (IS_NODE_ID(index)) { delta = (float) IMREF(D_a, p); #ifdef BVZ_ALPHA_SINK if (delta > 0) { g -> set_tweights(index, delta, 0); } else { g -> set_tweights(index, 0, -delta); E += delta; } #else if (delta > 0) { g -> set_tweights(index, 0, delta); } else { g -> set_tweights(index, -delta, 0); E += delta; } #endif } } E += g -> maxflow(); //fprintf(_fp, "internal E: %f\n",E); fflush(_fp); if (E < E_old) { //fprintf(_fp,"Writing into _labels\n"); fflush(_fp); ind=0; for (p.y=0; p.y<_h; p.y++) for (p.x=0; p.x<_w; p.x++, ++ind) { index = (Graph::node_id) IMREF(indeces_a, p); if (IS_NODE_ID(index) && g->what_segment(index)==BVZ_TERM_B) { _labels[ind] = a; //进行交换操作 } } delete g; return E; } delete g; return E_old; }