/*--------------------------------------------------------------------------- * FindBipResponder() *--------------------------------------------------------------------------- * * Synopsis: Return the BIP Responder channel given the ObexServerApp pointer. * * Return: BipObexClient* or BipObexServer* */ void *FindBipResponder(void *app) { I8 i; /* Go through the BIP server array. */ for (i = 0; i < BIP_NUM_RESPONDERS; i++) { if (BIP(responder)[i] && &BIP(responder)[i]->primary.sApp == app) { return &(BIP(responder)[i]->primary); } } return 0; }
/*--------------------------------------------------------------------------- * FindBipInitiator() *--------------------------------------------------------------------------- * * Synopsis: Return the BIP Initiator channel given the ObexClientApp pointer. * * Return: BipObexClient* or BipObexServer* */ void *FindBipInitiator(void *app) { I8 i; /* Go through the BIP client array. */ for (i = 0; i < BIP_NUM_INITIATORS; i++) { if (BIP(initiator)[i] && &BIP(initiator)[i]->primary.cApp == app) { return &(BIP(initiator)[i]->primary); } } return 0; }
//Color one blob for one thread void ColorBlob( cv::Mat BinImPad, std::vector< std::pair<int,int> > *indices, const int &start, const int &end, unsigned char ¤t_index, unsigned char &replace_ind ){ //Check the 8-connected neighborhood and set them if they are within bounds std::pair<int,int> CI = indices->back(); indices->pop_back(); if( (CI.second-1)>=start ){//Bounds checking for current thread for( int i=-1; i<2; ++i ) if( BIP((CI.first+i),(CI.second-1)) ==replace_ind ){ BIP( (CI.first+i),(CI.second-1) ) = current_index; std::pair<int,int> PushInd( (CI.first+i),(CI.second-1) ); indices->push_back( PushInd ); } } if( (CI.second+1)<end ){//Bounds checking for next thread for( int i=-1; i<2; ++i ) if( BIP((CI.first+i),(CI.second+1)) ==replace_ind ){ BIP( (CI.first+i),(CI.second+1) ) = current_index; std::pair<int,int> PushInd( (CI.first+i),(CI.second+1) ); indices->push_back( PushInd ); } } if( BIP((CI.first-1),(CI.second))==replace_ind ){ BIP( (CI.first-1),(CI.second) ) = current_index; std::pair<int,int> PushInd( (CI.first-1),(CI.second) ); indices->push_back( PushInd ); } if( BIP((CI.first+1),(CI.second))==replace_ind ){ BIP( (CI.first+1),(CI.second) ) = current_index; std::pair<int,int> PushInd( (CI.first+1),(CI.second) ); indices->push_back( PushInd ); } if(!indices->empty()){ ColorBlob( BinImPad, indices, start, end, current_index, replace_ind ); } }
/*--------------------------------------------------------------------------- * BipAppCallBack() *--------------------------------------------------------------------------- * * Synopsis: Call the application with the specified event. * * Return: */ void BipAppCallBack(BipCallbackParms *parms, U16 status, BipEvent event) { BipCallback callback = 0; /* Application callback function */ parms->status = status; parms->event = event; parms->channel = parms->obex.client->channel; switch (parms->channel) { #if BIP_NUM_INITIATORS > 0 case BIPCH_INITIATOR_PRIMARY: case BIPCH_INITIATOR_SECONDARY: callback = BIP(initiatorCallback); break; #endif #if BIP_NUM_RESPONDERS > 0 case BIPCH_RESPONDER_PRIMARY: case BIPCH_RESPONDER_SECONDARY: callback = BIP(responderCallback); break; #endif default: break; } /* Copy the channel data pointer into parms */ switch (parms->channel) { case BIPCH_INITIATOR_PRIMARY: case BIPCH_RESPONDER_SECONDARY: parms->data = parms->obex.client->data; break; case BIPCH_INITIATOR_SECONDARY: case BIPCH_RESPONDER_PRIMARY: parms->data = &parms->obex.server->request; break; default: break; } Assert(callback); callback(parms); }
//Computes the area and the centroid for a given label bool GetStats(cv::Mat LabelIm, int i, int j, cv::Mat BinImPad, std::vector<int> &one_stat){ //Seed with the first pixel std::pair<int,int> seed(i,j); std::vector< std::pair<int,int> > indices, queueue; indices.push_back(seed); queueue.push_back(seed); BIP(i,j) = 0; double x_men = 0, y_men = 0; //centroid //Find indices of all pixels with the same label while( !queueue.empty() ){ std::pair<int, int> index = queueue.back(); queueue.pop_back(); //Check 8-neighborhood for( int k=-1; k<2; ++k ) for( int l=-1; l<2; ++l ) if( (k==0) && (l==0) ) continue; else{ if( BIP( (index.first+k),(index.second+l) ) ){ std::pair<int,int> sed( (index.first+k),(index.second+l) ); queueue.push_back( sed ); indices.push_back( sed ); LabelIm.at<unsigned char>( sed.first, sed.second ) = BIP( sed.first,sed.second ); BIP( sed.first, sed.second ) = 0; x_men += sed.first; y_men += sed.second; } } } x_men = round(x_men/((double)indices.size())); y_men = round(y_men/((double)indices.size())); one_stat[1] = indices.size(); one_stat[2] = (int)x_men; one_stat[3] = (int)y_men; if( (indices.size())>=15 ){ return true; } else{ std::vector< std::pair<int,int> >::iterator it; for( it=indices.begin() ; it < indices.end(); ++it ) LabelIm.at<unsigned char>(it->first, it->second) = 0; return false; } }
//This function binarizes the nuclei in an image and colors connected components void computebin(cv::Mat src, cv::Mat BinImPad, cv::Mat BinIm ){ //Get Image Dimensions int thresh = GetThresh(src); int nthreads, tid; #pragma omp parallel private(tid,nthreads) { tid = omp_get_thread_num(); nthreads = omp_get_num_threads(); int start, end; start = tid*(rows/nthreads); end = (tid+1)*(rows/nthreads); if( tid == (nthreads-1) ) end = rows; for( int j=start; j<rows; ++j ) for( int i=0; i<cols; ++i ) if( src.at<unsigned char>(i,j) > thresh ){ BinIm.at<unsigned char>(i,j) = 0; BIP((i+1),(j+1)) = 0; } else{ BinIm.at<unsigned char>(i,j) = 255; BIP((i+1),(j+1)) = UCHAR_MAX; } } //Set the borders of the padded image for( int i=0; i<(cols+2); ++i ){ BIP(i,0) = 0; BIP(i,(rows+1)) = 0; } for( int i=1; i<(rows+2); ++i ){ BIP(0,i) = 0; BIP((cols+1),i) = 0; } if(write_files){ std::string out_str1 = "binary_" + file_open; cv::imwrite(out_str1.c_str(),BinIm); } std::cout<<"The threshold is: "<<thresh<<std::endl; }
int main(int argc, char *argv[]) { { libmaus2::lz::LineSplittingGzipOutputStream LSG("gzsplit",4,17); for ( uint64_t i = 0; i < 17; ++i ) LSG << "line_" << i << "\n"; } { libmaus2::lz::LineSplittingGzipOutputStream LSG("nogzsplit",4,17); } testGzip(); testlz4(); #if 0 maskBamDuplicateFlag(std::cin,std::cout); return 0; #endif #if 0 { libmaus2::lz::BgzfInflateDeflateParallel BIDP(std::cin,std::cout,Z_DEFAULT_COMPRESSION,32,128); libmaus2::autoarray::AutoArray<char> B(64*1024,false); int r; uint64_t t = 0; uint64_t last = std::numeric_limits<uint64_t>::max(); uint64_t lcnt = 0; uint64_t const mod = 64*1024*1024; libmaus2::timing::RealTimeClock rtc; rtc.start(); libmaus2::timing::RealTimeClock lrtc; lrtc.start(); while ( (r = BIDP.read(B.begin(),B.size())) ) { BIDP.write(B.begin(),r); lcnt += r; t += r; if ( t/mod != last/mod ) { if ( isatty(STDERR_FILENO) ) std::cerr << "\r" << std::string(60,' ') << "\r"; std::cerr << rtc.formatTime(rtc.getElapsedSeconds()) << " " << t/(1024*1024) << "MB, " << (lcnt/lrtc.getElapsedSeconds())/(1024.0*1024.0) << "MB/s"; if ( isatty(STDERR_FILENO) ) std::cerr << std::flush; else std::cerr << std::endl; lrtc.start(); last = t; lcnt = 0; } } if ( isatty(STDERR_FILENO) ) std::cerr << "\r" << std::string(60,' ') << "\r"; std::cerr << rtc.formatTime(rtc.getElapsedSeconds()) << " " << t/(1024*1024) << "MB, " << (t/rtc.getElapsedSeconds())/(1024.0*1024.0) << "MB/s"; std::cerr << std::endl; return 0; } #endif #if 0 { ::libmaus2::lz::BgzfDeflateParallel BDP(std::cout,32,128,Z_DEFAULT_COMPRESSION); while ( std::cin ) { libmaus2::autoarray::AutoArray<char> B(16384); std::cin.read(B.begin(),B.size()); int64_t const r = std::cin.gcount(); BDP.write(B.begin(),r); } BDP.flush(); std::cout.flush(); } return 0; #endif #if 0 { try { libmaus2::lz::BgzfInflateParallel BIP(std::cin /* ,4,16 */); uint64_t c = 0; uint64_t b = 0; uint64_t d = 0; libmaus2::timing::RealTimeClock rtc; rtc.start(); libmaus2::autoarray::AutoArray<uint8_t> adata(64*1024,false); while ( (d=BIP.read(reinterpret_cast<char *>(adata.begin()),adata.size())) != 0 ) { b += d; if ( ++c % (16*1024) == 0 ) { std::cerr << c << "\t" << b/(1024.0*1024.0*1024.0) << "\t" << static_cast<double>(b)/(1024.0*1024.0*rtc.getElapsedSeconds()) << " MB/s" << std::endl; } } std::cerr << c << "\t" << b/(1024.0*1024.0*1024.0) << "\t" << static_cast<double>(b)/(1024.0*1024.0*rtc.getElapsedSeconds()) << " MB/s" << std::endl; std::cerr << "decoded " << b << " bytes in " << rtc.getElapsedSeconds() << " seconds." << std::endl; } catch(std::exception const & ex) { std::cerr << ex.what() << std::endl; return EXIT_FAILURE; } } return 0; #endif std::cerr << "Testing random data on bgzf..."; testBgzfRandom(); std::cerr << "done." << std::endl; std::cerr << "Testing mono..."; testBgzfMono(); std::cerr << "done." << std::endl; ::libmaus2::lz::BgzfDeflate<std::ostream> bdefl(std::cout); char const * str = "Hello, world.\n"; bdefl.write(reinterpret_cast<char const *>(str),strlen(str)); bdefl.flush(); bdefl.write(reinterpret_cast<char const *>(str),strlen(str)); bdefl.flush(); bdefl.addEOFBlock(); return 0; ::libmaus2::lz::BgzfInflateStream SW(std::cin); ::libmaus2::autoarray::AutoArray<char> BB(200,false); while ( SW.read(BB.begin(),BB.size()) ) { } if ( argc < 2 ) return EXIT_FAILURE; return 0; #if 0 ::libmaus2::lz::GzipHeader GZH(argv[1]); return 0; #endif std::ostringstream ostr; ::libmaus2::autoarray::AutoArray<uint8_t> message = ::libmaus2::util::GetFileSize::readFile(argv[1]); std::cerr << "Deflating message of length " << message.size() << "..."; ::libmaus2::lz::Deflate DEFL(ostr); DEFL.write ( reinterpret_cast<char const *>(message.begin()), message.size() ); DEFL.flush(); std::cerr << "done." << std::endl; std::cerr << "Checking output..."; std::istringstream istr(ostr.str()); ::libmaus2::lz::Inflate INFL(istr); int c; uint64_t i = 0; while ( (c=INFL.get()) >= 0 ) { assert ( c == message[i] ); i++; } std::cerr << "done." << std::endl; // std::cerr << "Message size " << message.size() << std::endl; std::string testfilename = "test"; ::libmaus2::lz::BlockDeflate BD(testfilename); BD.write ( message.begin(), message.size() ); BD.flush(); uint64_t const decpos = message.size() / 3; ::libmaus2::lz::BlockInflate BI(testfilename,decpos); ::libmaus2::autoarray::AutoArray<uint8_t> dmessage (message.size(),false); uint64_t const red = BI.read(dmessage.begin()+decpos,dmessage.size()); assert ( red == dmessage.size()-decpos ); std::cerr << "("; for ( uint64_t i = decpos; i < message.size(); ++i ) assert ( message[i] == dmessage[i] ); std::cerr << ")\n"; std::string shortmes1("123456789"); std::string shortmes2("AA"); std::string shortmes3("BB"); std::string shortmes4("CC"); std::string textfile1("test1"); std::string textfile2("test2"); std::string textfile3("test3"); std::string textfile4("test4"); ::libmaus2::lz::BlockDeflate BD1(textfile1); BD1.write ( reinterpret_cast<uint8_t const *>(shortmes1.c_str()), shortmes1.size() ); BD1.flush(); ::libmaus2::lz::BlockDeflate BD2(textfile2); BD2.write ( reinterpret_cast<uint8_t const *>(shortmes2.c_str()), shortmes2.size() ); BD2.flush(); ::libmaus2::lz::BlockDeflate BD3(textfile3); BD3.write ( reinterpret_cast<uint8_t const *>(shortmes3.c_str()), shortmes3.size() ); BD3.flush(); ::libmaus2::lz::BlockDeflate BD4(textfile4); BD4.write ( reinterpret_cast<uint8_t const *>(shortmes4.c_str()), shortmes4.size() ); BD4.flush(); std::vector < std::string > filenames; filenames.push_back(textfile1); filenames.push_back(textfile2); filenames.push_back(textfile3); filenames.push_back(textfile4); for ( uint64_t j = 0; j <= 15; ++j ) { ::libmaus2::lz::ConcatBlockInflate CBI(filenames,j); for ( uint64_t i = 0; i < j; ++i ) std::cerr << ' '; for ( uint64_t i = 0; i < CBI.n-j; ++i ) std::cerr << (char)CBI.get(); std::cerr << std::endl; } return 0; }
void computeLabels(cv::Mat src, cv::Mat BinImPad){ cv::Mat LabelIm = cvCreateMat(cols,rows,src.type()); for( int j=0; j<rows; ++j ) for( int i=0; i<cols; ++i ) LabelIm.at<unsigned char>(i,j)=0; int nthreads, tid; //omp_set_num_threads(4); bool redo_coloring = true; #pragma omp parallel private(tid,nthreads) { //Color blob recursively as soon as a pixel in FG is found tid = omp_get_thread_num(); nthreads = omp_get_num_threads(); int end1; unsigned char start_index, label_index; label_index = UCHAR_MAX * tid / nthreads; ++label_index; start_index = label_index; const int start = tid*(rows/nthreads) + 1; end1 = (tid+1)*(rows/nthreads) + 1; if( tid==(nthreads-1) ) ++end1; ++end1; const int end = end1; for( int j=start; j<end; ++j ) for( int i=1; i<=cols; ++i ){ if( BIP(i,j)==UCHAR_MAX ){ std::vector< std::pair<int,int> > indices; std::pair<int,int> one_index; one_index.first = i; one_index.second = j; indices.push_back(one_index); BIP(i,j) = label_index; unsigned char temp = UCHAR_MAX; ColorBlob( BinImPad, &indices, start, end, label_index, temp ); ++label_index; } } #pragma omp barrier //Re-color blob if the previous thread has assigned it //to a different color if( tid==0 && write_files ){ std::string out_str1 = "label_intermediate.png"; cv::imwrite(out_str1.c_str(),BinImPad); } while( redo_coloring ){ #pragma omp barrier if( tid == 0 ) redo_coloring = false; for( int i=1; i<=cols; ++i ){ if( (BIP((i-1),(start-1)) && BIP(i,start)) && (BIP((i-1),(start-1))!=BIP(i,start)) ){ redo_coloring = true; unsigned char lund=BIP(i,start); BIP(i,start)=BIP((i-1),(start-1)); std::pair<int,int> one_index; one_index.first = i; one_index.second = start; std::vector< std::pair<int,int> > indices; indices.push_back(one_index); unsigned char temp = BIP(i,start); ColorBlob( BinImPad, &indices, start, end, temp, lund ); } else if( (BIP(i,(start-1)) && BIP(i,start)) && (BIP(i,(start-1))!=BIP(i,start)) ){ redo_coloring = true; unsigned char lund=BIP(i,start); BIP(i,start)=BIP(i,(start-1)); std::pair<int,int> one_index; one_index.first = i; one_index.second = start; std::vector< std::pair<int,int> > indices; indices.push_back(one_index); unsigned char temp = BIP(i,start); ColorBlob( BinImPad, &indices, start, end, temp, lund ); } else if( BIP((i+1),(start-1)) && BIP(i,start) && (BIP((i+1),(start-1))!=BIP(i,start)) ){ redo_coloring = true; unsigned char lund=BIP(i,start); BIP(i,start)=BIP((i+1),(start-1)); std::pair<int,int> one_index; one_index.first = i; one_index.second = start; std::vector< std::pair<int,int> > indices; indices.push_back(one_index); unsigned char temp = BIP(i,start); ColorBlob( BinImPad, &indices, start, end, temp, lund ); } } #pragma omp barrier if( tid==0 && write_files ){ std::string out_str1 = "label_intermediate1.png"; cv::imwrite(out_str1.c_str(),BinImPad); } } } //Output the centroids and areas std::vector< std::vector<int> > statistics; for( int j=1; j<=rows; ++j ) for( int i=1; i<=cols; ++i ) if( ((int)BIP(i,j))>0){ std::vector<int> one_stat(4); one_stat[0] = BIP(i,j); bool bigEnough = GetStats(LabelIm,i,j,BinImPad,one_stat); if(bigEnough) statistics.push_back( one_stat ); //else std::cout<<one_stat[0]<<"\t"<<one_stat[1]<<"\t"<<one_stat[2]<<"\t"<<one_stat[3]<<"\n"; } std::string out_str1 = "label_final.png"; cv::imwrite(out_str1.c_str(),LabelIm); out_str1 = "binary_" + file_open; IplImage *img = cvLoadImage(out_str1.c_str(), CV_LOAD_IMAGE_COLOR); CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.3, 0.3, 0, 1); std::vector< std::vector<int> >::iterator it; int i=1; for( it=statistics.begin() ; it < statistics.end(); ++it ){ std::stringstream ss; ss<<i<<","<<it->at(1);++i; std::string sss=ss.str(); cvPutText(img, sss.c_str(), cvPoint((it->at(3)),(it->at(2))), &font, cvScalar(255, 255, 100, 0)); cvCircle(img, cvPoint((it->at(3)),(it->at(2))), 3, cvScalar(0,255,0), 1); } cvShowImage("Binary Image", img); out_str1 = "final.png"; cvSaveImage(out_str1.c_str(),img); return; }