unsigned int stable_count(unsigned long ary[], unsigned long n, unsigned long nbits, unsigned long offset, unsigned long size) { unsigned long j = 1 << size; unsigned long *count = malloc(sizeof(unsigned long)*j); unsigned long *pos = malloc(sizeof(unsigned long)*j); unsigned long *output = malloc(sizeof(unsigned long)*n); unsigned long x; for(x=0;x<j;x++){ count[x] = pos[x] = 0; } for(x=0;x<n;x++){ unsigned long m = masked( ary[x], offset, size); count[m]++; } for(x=0;x<=j;x++){ pos[x+1] = pos[x] + count[x]; } for(x=0;x<n;x++){ unsigned long m = masked( ary[x], offset, size); output[pos[m]++] = ary[x]; } for(x=0;x<n;x++){ ary[x] = output[x]; } return 0; /* success */ }
static void accum(IplImage *a, IplImage *acc, int ax, int ay, int accx, int accy, int dist, IplImage *mask) { float weight = 1.0f/(dist+1); int dx, dy, dir = 1, i, w = PATCH_W, h = PATCH_W; int stride = a->widthStep, astride = acc->widthStep; uint8_t *data = (uint8_t*)a->imageData + ay*stride + ax*a->nChannels; uint8_t *adata = (uint8_t*)acc->imageData + accy*astride + accx*acc->nChannels*sizeof(float); if (!masked(mask, accx, accy)) return; if (!masked(mask, accx, accy)) { update_accum(a, acc, ax, ay, accx, accy, weight); return; } if (ax+w >= a->width) w -= ax+w - a->width; if (accx+w >= acc->width) w -= accx+w - acc->width; if (ay+h >= a->height) h -= ay+h - a->height; if (accy+h >= acc->height) h -= accy+h - acc->height; for (dy = 0; dy < h; dy++) { uint8_t *line = data; float *aline = (float*)adata; for (dx = 0; dx < w; dx++) { //if (!masked(mask, rx, ry)) continue; for (i = 0; i < a->nChannels; i++) aline[i] += weight*line[i]; aline[a->nChannels] += weight; line += dir*a->nChannels; aline += dir*acc->nChannels; } data += a->widthStep; adata += acc->widthStep; } }
WSFrameHeaderInfo WSHyBiFrameHeader::info() const { WSFrameHeaderInfo inf; inf.fin = fin(); inf.opcode = opcode(); inf.hasLength = true; inf.masked = masked(); if (masked()) { inf.maskingKey.resize(4); maskingKey(&inf.maskingKey[0]); } inf.payloadLength = payloadLength(); return inf; }
//============================================================================= // Fill 4volume. //============================================================================= void TbTracking::fillATrackVolume(TbTrackVolume* track_volume) { // Fills the given TbTrackVolume (that has a particular space-time volume) // with clusters from all planes (ie m_clusters) that fall in this space-time // volume. for (unsigned int iplane = 0; iplane < m_nPlanes; iplane++) { // Check for any clusters, or track contains seed condition. if (m_clusterFinder->empty(iplane) || iplane == track_volume->seed()->plane() || masked(iplane)) continue; // Create an iterator and find the appropriate cluster on the ith plane // whose TOA is at the start of this TbTrackVolumes' time window. auto ic = m_clusterFinder->getIterator(track_volume->t_lower(), iplane); const auto end = m_clusterFinder->end(iplane); // Loop until the TOA reaches the end of this TbTrackVolumes' time window. // (dummy end condition used in the for loop). for (; ic != end; ++ic) { // Add cluster if inside and not already tracked. track_volume->consider_cluster((*ic)); // Stop if too far ahead in time. if ((*ic)->htime() > track_volume->t_upper()) break; } } }
static void makeOligoHistogram(char *fileName, struct seqList *seqList, int oligoSize, int **retTable, int *retTotal) /* Make up table of oligo occurences. Either pass in an FA file or a seqList. * (the other should be NULL). */ { FILE *f = NULL; int tableSize = (1<<(oligoSize+oligoSize)); int tableByteSize = tableSize * sizeof(int); int *table = needLargeMem(tableByteSize); struct dnaSeq *seq; struct seqList *seqEl = seqList; int *softMask = NULL; int total = 0; if (seqList == NULL) f = mustOpen(fileName, "rb"); memset(table, 0, tableByteSize); for (;;) { DNA *dna; int size; int endIx; int i; int oliVal; if (seqList != NULL) { if (seqEl == NULL) break; seq = seqEl->seq; softMask = seqEl->softMask; seqEl = seqEl->next; } else { seq = faReadOneDnaSeq(f, "", TRUE); if (seq == NULL) break; } dna = seq->dna; size = seq->size; endIx = size-oligoSize; for (i=0; i<=endIx; ++i) { if (softMask == NULL || !masked(softMask+i, oligoSize) ) { if ((oliVal = oligoVal(dna+i, oligoSize)) >= 0) { table[oliVal] += 1; ++total; } } } if (seqList == NULL) freeDnaSeq(&seq); } carefulClose(&f); *retTable = table; *retTotal = total; }
void WSHyBiFrameHeader::maskingKey(uint8_t key[4]) const { if (!masked()) memset(key, 0, 4); else { key[0] = read(9 + payloadLengthLength(), 8); key[1] = read(9 + payloadLengthLength() + 8, 8); key[2] = read(9 + payloadLengthLength() + 16, 8); key[3] = read(9 + payloadLengthLength() + 24, 8); } }
static void improve_guess(IplImage *a, IplImage *b, int ax, int ay, int *xbest, int *ybest, int *dbest, int bx, int by, IplImage *mask) { if (masked(mask, bx, by)) return; int db = *dbest; int d = dist(a, b, ax, ay, bx, by, db, mask); if (d < db) { *dbest = d; *xbest = bx; *ybest = by; } }
int release_irq( struct thread *t, int irq ) { int ans = -1; if ( (irq < 0) || (irq >= IRQ_COUNT) ) return -1; acquire_spinlock( &irq_lock ); if ( handlers[irq] == t ) { handlers[irq] = NULL; if ( masked(irq) == 0 ) mask_irq( irq ); ans = 0; } release_spinlock( &irq_lock ); return ans; }
int request_irq( struct thread *t, int irq ) { int ans = -1; if ( (irq < 0) || (irq >= IRQ_COUNT) ) return -1; acquire_spinlock( &irq_lock ); if ( handlers[irq] == NULL ) { handlers[irq] = t; t->state = THREAD_IRQ; if ( masked( irq ) != 0 ) unmask_irq( irq ); ans = 0; } release_spinlock( &irq_lock ); return ans; }
//============================================================================= // Actual tracking //============================================================================= void TbTracking::performTracking() { // The working of this algorithm is similar to the cluster maker, such that: // - Loop over some set of seed clusters (those on the m_SeedPlanes). // - Create a container (TbTrackVolume) centered on each seed cluster // (in space and time). // - Impose the condition that any formed track must contain this seed. // - Evaluate that 4-volume, to see if it contained a track. // - If a choice (e.g. more than one possible combination of clusters), // take the best fitting. Priority is given to more complete tracks. // - If another track happened to be in the 4volume, but not // containing this seed, then it will be found later. // Iterate over planes in the specified order. for (const auto& plane : m_PlaneSearchOrder) { // Iterate over this planes' clusters - first check for any. if (masked(plane) || m_clusterFinder->empty(plane)) continue; auto ic = m_clusterFinder->first(plane); const auto end = m_clusterFinder->end(plane); for (; ic != end; ++ic) { // Check if the seed has already been used. if ((*ic)->tracked()) continue; // Form the TbTrackVolume container, and fill with clusters that fall // in its space-time volume. TbTrackVolume* track_volume = new TbTrackVolume((*ic), m_search_3vol, m_nPlanes, m_twindow, m_vol_radius, m_vol_radiusY, m_vol_theta, m_vol_thetaY, m_MinNClusters); fillATrackVolume(track_volume); // Look for a track - automatically keeps if suitable. evaluateTrackVolume(track_volume); // PoorMansEvaluation(track_volume); // Alternative evaluator. delete track_volume; } } }
/** Executes the algorithm * */ void SplineBackground::exec() { API::MatrixWorkspace_sptr inWS = getProperty("InputWorkspace"); int spec = getProperty("WorkspaceIndex"); if (spec > static_cast<int>(inWS->getNumberHistograms())) throw std::out_of_range("WorkspaceIndex is out of range."); const MantidVec& X = inWS->readX(spec); const MantidVec& Y = inWS->readY(spec); const MantidVec& E = inWS->readE(spec); const bool isHistogram = inWS->isHistogramData(); const int ncoeffs = getProperty("NCoeff"); const int k = 4; // order of the spline + 1 (cubic) const int nbreak = ncoeffs - (k - 2); if (nbreak <= 0) throw std::out_of_range("Too low NCoeff"); gsl_bspline_workspace *bw; gsl_vector *B; gsl_vector *c, *w, *x, *y; gsl_matrix *Z, *cov; gsl_multifit_linear_workspace *mw; double chisq; int n = static_cast<int>(Y.size()); bool isMasked = inWS->hasMaskedBins(spec); std::vector<int> masked(Y.size()); if (isMasked) { for(API::MatrixWorkspace::MaskList::const_iterator it=inWS->maskedBins(spec).begin();it!=inWS->maskedBins(spec).end();++it) masked[it->first] = 1; n -= static_cast<int>(inWS->maskedBins(spec).size()); } if (n < ncoeffs) { g_log.error("Too many basis functions (NCoeff)"); throw std::out_of_range("Too many basis functions (NCoeff)"); } /* allocate a cubic bspline workspace (k = 4) */ bw = gsl_bspline_alloc(k, nbreak); B = gsl_vector_alloc(ncoeffs); x = gsl_vector_alloc(n); y = gsl_vector_alloc(n); Z = gsl_matrix_alloc(n, ncoeffs); c = gsl_vector_alloc(ncoeffs); w = gsl_vector_alloc(n); cov = gsl_matrix_alloc(ncoeffs, ncoeffs); mw = gsl_multifit_linear_alloc(n, ncoeffs); /* this is the data to be fitted */ int j = 0; for (MantidVec::size_type i = 0; i < Y.size(); ++i) { if (isMasked && masked[i]) continue; gsl_vector_set(x, j, (isHistogram ? (0.5*(X[i]+X[i+1])) : X[i])); // Middle of the bins, if a histogram gsl_vector_set(y, j, Y[i]); gsl_vector_set(w, j, E[i]>0.?1./(E[i]*E[i]):0.); ++j; } if (n != j) { gsl_bspline_free(bw); gsl_vector_free(B); gsl_vector_free(x); gsl_vector_free(y); gsl_matrix_free(Z); gsl_vector_free(c); gsl_vector_free(w); gsl_matrix_free(cov); gsl_multifit_linear_free(mw); throw std::runtime_error("Assertion failed: n != j"); } double xStart = X.front(); double xEnd = X.back(); /* use uniform breakpoints */ gsl_bspline_knots_uniform(xStart, xEnd, bw); /* construct the fit matrix X */ for (int i = 0; i < n; ++i) { double xi=gsl_vector_get(x, i); /* compute B_j(xi) for all j */ gsl_bspline_eval(xi, B, bw); /* fill in row i of X */ for (j = 0; j < ncoeffs; ++j) { double Bj = gsl_vector_get(B, j); gsl_matrix_set(Z, i, j, Bj); } } /* do the fit */ gsl_multifit_wlinear(Z, w, y, c, cov, &chisq, mw); /* output the smoothed curve */ API::MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(inWS,1,X.size(),Y.size()); { outWS->getAxis(1)->setValue(0, inWS->getAxis(1)->spectraNo(spec)); double xi, yi, yerr; for (MantidVec::size_type i=0;i<Y.size();i++) { xi = X[i]; gsl_bspline_eval(xi, B, bw); gsl_multifit_linear_est(B, c, cov, &yi, &yerr); outWS->dataY(0)[i] = yi; outWS->dataE(0)[i] = yerr; } outWS->dataX(0) = X; } gsl_bspline_free(bw); gsl_vector_free(B); gsl_vector_free(x); gsl_vector_free(y); gsl_matrix_free(Z); gsl_vector_free(c); gsl_vector_free(w); gsl_matrix_free(cov); gsl_multifit_linear_free(mw); setProperty("OutputWorkspace",outWS); }
size_t poisson_square(tkernel<glm::tvec2<T, P>> & kernel, const T min_dist, const unsigned int num_probes) { assert(kernel.depth() == 1); std::random_device RD; std::mt19937_64 generator(RD()); std::uniform_real_distribution<> radius_dist(min_dist, min_dist * 2.0); std::uniform_real_distribution<> angle_dist(0.0, 2.0 * glm::pi<T>()); std::uniform_int_distribution<> int_distribute(0, std::numeric_limits<int>::max()); auto occupancy = poisson_square_map<T, P>{ min_dist }; size_t k = 0; // number of valid/final points within the kernel kernel[k] = glm::tvec2<T, P>(0.5, 0.5); auto actives = std::list<size_t>(); actives.push_back(k); occupancy.mask(kernel[k], k); while (!actives.empty() && k < kernel.size() - 1) { // randomly pick an active point const auto pick = int_distribute(generator); auto pick_it = actives.begin(); std::advance(pick_it, pick % actives.size()); const auto active = kernel[*pick_it]; std::vector<std::tuple<glm::tvec2<T, P>, T>> probes{ num_probes }; #pragma omp parallel for for (int i = 0; i < static_cast<int>(num_probes); ++i) { const auto r = radius_dist(generator); const auto a = angle_dist(generator); auto probe = glm::tvec2<T, P>{ active.x + r * cos(a), active.y + r * sin(a) }; // within square? (tilable) if (probe.x < 0.0) probe.x += 1.0; else if (probe.x >= 1.0) probe.x -= 1.0; if (probe.y < 0.0) probe.y += 1.0; else if (probe.y >= 1.0) probe.y -= 1.0; // Note: do NOT make this optimization //if (!tilable && (probe.x < 0.0 || probe.x > 1.0 || probe.y < 0.0 || probe.y > 1.0)) // continue; // points within min_dist? const auto masked = occupancy.masked(probe, kernel); const auto delta = glm::abs(active - probe); probes[i] = std::make_tuple<glm::tvec2<T, P>, T>(std::move(probe), (masked ? static_cast<T>(-1.0) : glm::dot(delta, delta))); } // pick nearest probe from sample set glm::vec2 nearest_probe; auto nearest_dist = 4 * min_dist * min_dist; auto nearest_found = false; for (int i = 0; i < static_cast<int>(num_probes); ++i) { // is this nearest point yet? - optimized by using square distance -> skipping sqrt const auto new_dist = std::get<1>(probes[i]); if (new_dist < 0.0 || nearest_dist < new_dist) continue; if (!nearest_found) nearest_found = true; nearest_dist = new_dist; nearest_probe = std::get<0>(probes[i]); } if (!nearest_found && (actives.size() > 0 || k > 1)) { actives.erase(pick_it); continue; } kernel[++k] = nearest_probe; actives.push_back(k); occupancy.mask(nearest_probe, k); } return k + 1; }
uint8_t WSHyBiFrameHeader::maskingKeyLength() const { return masked() ? 32 : 0; }
static void patchmatch(IplImage *a, IplImage *b, IplImage *ann, IplImage *annd, IplImage *mask) { int iter, rs_start, mag; int w = ann->width, h = ann->height; int32_t *data = (int32_t*)ann->imageData; int32_t *ddata = (int32_t*)annd->imageData; int stride = ann->widthStep/sizeof(int32_t); int aew = a->width - PATCH_W + 1; int aeh = a->height - PATCH_W + 1; int bew = b->width - PATCH_W + 1; int beh = b->height - PATCH_W + 1; int ax, ay; // initialize with random values for (ay = 0; ay < h; ay++) { int xy = ay * stride; for (ax = 0; ax < w; ax++) { uint16_t bx = ax; uint16_t by = ay; while (masked(mask, bx, by)) { bx = rand() % bew; by = rand() % beh; } *(data + xy) = XY_TO_INT(bx, by); *(ddata + xy) = dist(a, b, ax, ay, bx, by, 0, mask); xy += 1; } } for (iter = 0; iter < PM_ITERS; iter++) { int ystart = 0, yend = aeh, ychange = 1; int xstart = 0, xend = aew, xchange = 1; if (iter % 2) { ystart = yend - 1; yend = -1; ychange = -1; xstart = xend - 1; xend = -1; xchange = -1; } for (ay = ystart; ay != yend; ay += ychange) { for (ax = xstart; ax != xend; ax += xchange) { if (!masked(mask, ax, ay)) continue; // current best guess int xy = ay * stride + ax; int v = *(data + xy); int xbest = XY_TO_X(v), ybest = XY_TO_Y(v); int dbest = *(ddata + xy); // Propagation: Improve current guess by trying // correspondences from left and above, // or below and right on odd iterations if ((unsigned)(ax - xchange) < (unsigned)aew) { int vp = *(data + ay * stride + (ax - xchange)); int xp = XY_TO_X(vp) + xchange, yp = XY_TO_Y(vp); if ((unsigned)xp < (unsigned)bew) { improve_guess(a, b, ax, ay, &xbest, &ybest, &dbest, xp, yp, mask); } } if ((unsigned)(ay - ychange) < (unsigned)aeh) { int vp = *(data + ay * stride + (ax - xchange)); int xp = XY_TO_X(vp), yp = XY_TO_Y(vp) + ychange; if ((unsigned)yp < (unsigned)beh) { improve_guess(a, b, ax, ay, &xbest, &ybest, &dbest, xp, yp, mask); } } rs_start = RS_MAX; if (rs_start > max(b->width, b->height)) rs_start = max(b->width, b->height); for (mag = rs_start; mag >= 1; mag /= 2) { // sampling window int xmin = max(xbest - mag, 0); int xmax = min(xbest + mag + 1, bew); int ymin = max(ybest - mag, 0); int ymax = min(ybest + mag + 1, beh); int xp = xmin + rand() % (xmax - xmin); int yp = ymin + rand() % (ymax - ymin); improve_guess(a, b, ax, ay, &xbest, &ybest, &dbest, xp, yp, mask); } *(data + xy) = XY_TO_INT(xbest, ybest); *(ddata + xy) = dbest; } } } }
int is_anagram(char *s1, char *s2) { return masked(s1) == masked(s2); }
void UfrawWorker::developImpl(bool preview, WorkerBase *predecessor) { qDebug() << "UfrawWorker::developImpl()" << this << predecessor; double progressPhaseA = 0.1; double progressPhaseB = 0.5; int nof = config()->settings()->getSetting(UfrawSettings::Fuse)->value().toInt(); Magick::Image normalImg; std::vector<UfrawProcess> ufraw(nof); int firstIdx = -(nof-1)/2; for( int i=0; i<nof; i++ ) { setProgress( double(i) / nof * progressPhaseA ); qDebug() << "UfrawWorker::developImpl() running" << i << "..."; run( ufraw[i], preview, firstIdx+i, nof ); } for( int i=0; i<nof; i++ ) { setProgress( progressPhaseA + double(i) / nof * progressPhaseB ); ufraw[i].waitForFinished(-1); qDebug() << "UfrawWorker::developImpl()" << i << "finished with exitcode" << ufraw[i].exitCode() << ":" << ufraw[i].console(); if( ufraw[i].exitCode() == 0 ) { bool isNormalExposed = (firstIdx+i)==0; bool isOverExposed = (firstIdx+i)>0; qDebug() << "UfrawWorker::developImpl()" << i << "loading" << ufraw[i].output(); Magick::Image img, mask; img.read( ufraw[i].output().toStdString().c_str() ); if( isNormalExposed ) { normalImg = img; normalImg.write( QString("/Users/manuel/tmp/test_normal.tif").toStdString().c_str() ); } else { mask = masked( isOverExposed, 0.98, img ); mask.write( ufraw[i].output().toStdString().c_str() ); mask.write( QString("/Users/manuel/tmp/test_masked%1.tif").arg(i).toStdString().c_str() ); } } } QStringList rawImages; for( int i=0; i<nof; i++ ) { rawImages << ufraw[i].output(); } if( nof > 1 ) { double sigma = config()->settings()->getSetting(UfrawSettings::ExposureSigma)->value().toDouble(); EnfuseProcess enfuse; enfuse.setProgram( m_enfusePath ); enfuse.setInputs( rawImages ); enfuse.setExposureSigma( sigma ); connect( &enfuse, &EnfuseProcess::progress, [&](double progress) { setProgress( progressPhaseA + progressPhaseB + (1-progressPhaseA-progressPhaseB)*progress ); }); enfuse.run(); enfuse.waitForFinished(-1); qDebug() << "UfrawWorker::developImpl() enfuse finished with exitcode" << enfuse.exitCode() << ":" << enfuse.console(); if( enfuse.exitCode() == 0 ) { qDebug() << "UfrawWorker::developImpl()" << "loading" << enfuse.output(); m_img.read( enfuse.output().toStdString().c_str() ); m_img.matte(false); qDebug() << "UfrawWorker::developImpl() fused" << m_img.format().c_str(); } } else { m_img = normalImg; } }