int median3(int v1, int v2, int v3) { if (v1 > v2) { return median3(v2, v1, v3); } if (v2 > v3) { return median3(v1, v3, v2); } return v2; }
void schro_motion_vector_prediction (SchroMotion * motion, int x, int y, int *pred_x, int *pred_y, int mode) { SchroMotionVector *mv; int vx[3]; int vy[3]; int n = 0; SCHRO_ASSERT (mode == 1 || mode == 2); if (x > 0) { mv = SCHRO_MOTION_GET_BLOCK (motion, x - 1, y); if (mv->using_global == FALSE && (mv->pred_mode & mode)) { vx[n] = mv->u.vec.dx[mode - 1]; vy[n] = mv->u.vec.dy[mode - 1]; n++; } } if (y > 0) { mv = SCHRO_MOTION_GET_BLOCK (motion, x, y - 1); if (mv->using_global == FALSE && (mv->pred_mode & mode)) { vx[n] = mv->u.vec.dx[mode - 1]; vy[n] = mv->u.vec.dy[mode - 1]; n++; } } if (x > 0 && y > 0) { mv = SCHRO_MOTION_GET_BLOCK (motion, x - 1, y - 1); if (mv->using_global == FALSE && (mv->pred_mode & mode)) { vx[n] = mv->u.vec.dx[mode - 1]; vy[n] = mv->u.vec.dy[mode - 1]; n++; } } switch (n) { case 0: *pred_x = 0; *pred_y = 0; break; case 1: *pred_x = vx[0]; *pred_y = vy[0]; break; case 2: *pred_x = (vx[0] + vx[1] + 1) >> 1; *pred_y = (vy[0] + vy[1] + 1) >> 1; break; case 3: *pred_x = median3 (vx[0], vx[1], vx[2]); *pred_y = median3 (vy[0], vy[1], vy[2]); break; default: SCHRO_ASSERT (0); } }
void schro_mf_vector_prediction (SchroMotionField * mf, int x, int y, int *pred_x, int *pred_y, int mode) { int x_num_blocks; SchroMotionVector *mv; int vx[3], vy[3]; int n = 0; int ref = mode - 1; SCHRO_ASSERT (mf && pred_x && pred_y); SCHRO_ASSERT (1 == mode || 2 == mode); x_num_blocks = mf->x_num_blocks; if (0 < x) { mv = &mf->motion_vectors[y * x_num_blocks + x - 1]; vx[n] = mv->u.vec.dx[ref]; vy[n] = mv->u.vec.dy[ref]; ++n; } if (0 < y) { mv = &mf->motion_vectors[(y - 1) * x_num_blocks + x]; vx[n] = mv->u.vec.dx[ref]; vy[n] = mv->u.vec.dy[ref]; ++n; } if (0 < x && 0 < y) { mv = &mf->motion_vectors[(y - 1) * x_num_blocks + x - 1]; vx[n] = mv->u.vec.dx[ref]; vy[n] = mv->u.vec.dy[ref]; ++n; } switch (n) { case 0: *pred_x = 0; *pred_y = 0; break; case 1: *pred_x = vx[0]; *pred_y = vy[0]; break; case 2: *pred_x = (vx[0] + vx[1] + 1) >> 1; *pred_y = (vy[0] + vy[1] + 1) >> 1; break; case 3: *pred_x = median3 (vx[0], vx[1], vx[2]); *pred_y = median3 (vy[0], vy[1], vy[2]); break; default: SCHRO_ASSERT (0); } }
// Prediction used for interpolation / alpha=0 pixels. Does not have to be the same as the guess used for encoding/decoding. inline ColorVal predictScanlines(const Image &image, int p, uint32_t r, uint32_t c, ColorVal grey) { ColorVal left = (c>0 ? image(p,r,c-1) : (r > 0 ? image(p, r-1, c) : grey)); ColorVal top = (r>0 ? image(p,r-1,c) : left); ColorVal topleft = (r>0 && c>0 ? image(p,r-1,c-1) : top); ColorVal gradientTL = left + top - topleft; return median3(gradientTL, left, top); }
void quickSort(vector<Ponto> &v, int left, int right, int type) { if (right - left <= 50) // if small vector insertionSort(v, left, right, type); else { Ponto p = median3(v, left, right, type); // x is pivot int i = left; int j = right-1; // partition step double comp; for(; ; ) { comp = -1; while (comp < 0) { comp = comparaPontos(v[++i], p, type); } comp = -1; while (comp < 0) { comp = comparaPontos(p, v[--j], type); } if (i < j) swap(v[i], v[j]); else break; } swap(v[i], v[right-1]); // reset pivot quickSort(v, left, i-1, type); quickSort(v, i+1, right, type); } }
/** * internal static function to do the partition. * * @see cel_quick_sort */ static void quicksort( uchar_t *arr, uint_t size, cel_compare_fn_t comp, uint_t left, uint_t right) { //printf("left: %d, right: %d\n", left, right); if ( left + 11 <= right ) { //get the privot of the subarray. uchar_t * pivot = median3( arr, size, comp, left, right ); //printf("pivote: %d\n", *pivot); //start partitioning. register uint_t i = left, j = right - 1; for ( ; ; ) { while ( comp( arr + (++i * size), pivot ) < 0 ) ; while ( comp( arr + (--j * size), pivot ) > 0 ) ; if ( i < j ) { cel_mem_swap( arr + i * size, arr + j * size, size ); } else { break; } } //swap the privot back to the small colleciton. cel_mem_swap( arr + i * size, pivot, size ); quicksort( arr, size, comp, left, i - 1 ); quicksort( arr, size, comp, i, right ); } else { //if the number of the items is less than CUTOFF use insertion sort instead. cel_subinsert_sort( arr, size, comp, left, right ); } }
void quickSelect( vector<Comparable> & a, int left, int right, int k ) { if( left + 10 <= right ) { Comparable pivot = median3( a, left, right ); // Begin partitioning int i = left, j = right - 1; for( ; ; ) { while( a[ ++i ] < pivot ) { } while( pivot < a[ --j ] ) { } if( i < j ) swap( a[ i ], a[ j ] ); else break; } swap( a[ i ], a[ right - 1 ] ); // Restore pivot // Recurse; only this part changes if( k <= i ) quickSelect( a, left, i - 1, k ); else if( k > i + 1 ) quickSelect( a, i + 1, right, k ); } else // Do an insertion sort on the subarray insertionSort( a, left, right ); }
void quickSortMedian(int *a, int left, int right) { int pivot, i, j; int temp; if (left + 10 <= right) { pivot = a[median3(a, left, right)]; i = left + 1; j = right; while(i <= j) { while((a[i] <= pivot) && (i <= right)) i++; while((a[j] > pivot) && (j >= left)) j--; if(i < j) swap(&a[i], &a[j]); } swap(&a[left], &a[j]); quickSort(a, left, j - 1); quickSort(a, j + 1, right); } else { insertionSort(a, right - left + 1); } }
inline ColorVal predictScanlines_plane(const plane_t &plane, uint32_t r, uint32_t c, ColorVal grey) { ColorVal left = (c>0 ? plane.get(r,c-1) : (r > 0 ? plane.get(r-1, c) : grey)); ColorVal top = (r>0 ? plane.get(r-1,c) : left); ColorVal topleft = (r>0 && c>0 ? plane.get(r-1,c-1) : top); ColorVal gradientTL = left + top - topleft; return median3(gradientTL, left, top); }
void Qsort(ElementType ori[], int left, int right) { int i, j; ElementType pivot; //对于较小的数组,采用插入排序会比快速排序更优, //但此处为何是小于等于,而不是大于? if(left + CUTOFF <= right){ pivot = median3(ori, left, right); i = left; j = right - 1; for(;;){ while(ori[++i] < pivot); while(ori[--j] > pivot); if(i < j){ swap(&ori[i], &ori[j]); }else{ break; } } swap(&ori[i], &ori[right-1]); Qsort(ori, left, i-1); Qsort(ori, i+1, right); }else{ Insertsort(ori + left, right - left + 1); } }
ColorVal predict_and_calcProps_scanlines(Properties &properties, const ColorRanges *ranges, const Image &image, const int p, const uint32_t r, const uint32_t c, ColorVal &min, ColorVal &max) { ColorVal guess; int which = 0; int index=0; if (p != 3) { for (int pp = 0; pp < p; pp++) { properties[index++] = image(pp,r,c); } if (image.numPlanes()>3) properties[index++] = image(3,r,c); } ColorVal left = (c>0 ? image(p,r,c-1) : grey[p]);; ColorVal top = (r>0 ? image(p,r-1,c) : grey[p]); ColorVal topleft = (r>0 && c>0 ? image(p,r-1,c-1) : grey[p]); ColorVal gradientTL = left + top - topleft; guess = median3(gradientTL, left, top); ranges->snap(p,properties,min,max,guess); if (guess == gradientTL) which = 0; else if (guess == left) which = 1; else if (guess == top) which = 2; properties[index++] = guess; properties[index++] = which; if (c > 0 && r > 0) { properties[index++] = left - topleft; properties[index++] = topleft - top; } else { properties[index++] = 0; properties[index++] = 0; } if (c+1 < image.cols() && r > 0) properties[index++] = top - image(p,r-1,c+1); // top - topright else properties[index++] = 0; if (r > 1) properties[index++] = image(p,r-2,c)-top; // toptop - top else properties[index++] = 0; if (c > 1) properties[index++] = image(p,r,c-2)-left; // leftleft - left else properties[index++] = 0; return guess; }
void qSort(ElementType A[], int left, int right) { int i, j; ElementType pivot; if(left + CUTOFF <= right) { pivot = median3(A, left, right); i = left; j = right - 1; while(1) { while(A[++i] < pivot) { } while(A[--j] > pivot) { } if(i < j) swap(&A[i], &A[j]); else break; } swap(&A[i], &A[right - 1]); //restore pivot qSort(A, left, i-1); qSort(A, i+1, right); } else insertSort(A+left, right-left+1); }
float* SimpleFilter::MedianFilter(float* list, int windowSize) { float* retList = new float[windowSize-2]; for (int i = 0; i < windowSize - 2; i++) { retList[i] = median3(list[i], list[i + 1], list[i + 2]); } return retList; }
int main() { for (size_t i=0; i!=3; ++i) { for (size_t j=0; j!=3; ++j) { for (size_t k=0; k!=3; ++k) { std::cout << i << " " << j << " " << k << " "; std::cout << median3(i, j, k) << std::endl; } } } }
int main () { std::vector<int> v={1, 2, 3}; for (unsigned int i=0; i!=6; ++i) { for (const int& e : v) { std::cout << e; } std::cout << "=" << median3(v[0], v[1], v[2]); std::cout << std::endl; std::next_permutation(v.begin(), v.end()); } }
void quicksort(Time *t, int start, int end) { if (start + 10 <= end) { int pivot = median3(t, start, end); if (pivot != end) { swap(t, pivot, end); } long pivot_val = t[end].s; int i = start; int j = end - 1; while (1) { while (t[i].s < pivot_val) { i++; } while (t[j].s > pivot_val) { j--; } if (i < j) { swap(t, i, j); i++; j--; } else { break; } } swap(t, i, end); quicksort(t, start, i-1); quicksort(t, i+1, end); } else { if (start == end) { return; } else { int curr = start + 1; for (; curr <= end; curr++) { int i; Time temp = t[curr]; for (i=curr-1; i>=0; i--) { if (temp.s < t[i].s) { t[i+1] = t[i]; } else { break; } } t[i+1] = temp; } } } }
void qsorter(SortElem a[], int32 left, int32 right) { int32 pivot; int32 i, j; if (left+CUTOFF <= right) { median3(a, left, right, &pivot); i = left; j = right-1; while (1) { do { i++; } while (a[i].key < pivot); do { j--; } while (a[j].key > pivot); if (j <= i) break; SWAP(a[i], a[j]); } SWAP(a[i], a[right-1]); /* restore pivot */ qsorter(a, left, i); qsorter(a, i+1, right); } /* end if */ }
int partition(long int *a, int lo, int hi) { int i = lo, j = hi + 1; int v; median3(a, lo, hi); v = a[lo]; while (1) { while (a[++i] < v) { if (i == hi) { break; } } while (v < a[--j]) { if (j == lo) { break; } } if (i >= j ) { break; } int temp = a[i]; a[i] = a[j]; a[j] = temp; } int temp = a[lo]; a[lo] = a[j]; a[j] = temp; return j; }
void quicksort(int* a,int left,int right){ if(left+10<=right){ int pivot=median3(a,left,right); int i=left,j=right-1; for(;;){ while(a[++i]<pivot); while(pivot<a[j--]); if(i<j) Swap(a,i,j); else break; } Swap(a,i,right-1); quicksort(a,left,i-1); quicksort(a,i+1,right); } else insertionSort(a,left,right); }
// 此方法来自DSAAC static int partition3(int *data, int left, int right) { // 最后一个不需要移动 // 因为pivotMedian返回时,最右边的数大于pivot // pivot位于 right-1的位置,因此j初始化为right-1 int pivot = median3(data, left, right); int i = left; int j = right - 1; while (i < j) { while (data[++i] < pivot) ; while (data[--j] > pivot) ; if (i < j) { swap2(data, i, j); } } swap2(data, i, right - 1); return i; }
int partition(int a[], int l, int r) { // 选取枢轴值 // 枢轴值最好是数组中所有值的中间值, 但这会减慢快排速度 // 可采用 三数中值分割法 Midean(a[0], a[len], a[len/2]) int pivot = median3(a, l, r); printf("pivot: %d\n", pivot); while (l < r) { while (l < r && a[r] >= pivot) r--; a[l] = a[r]; while (l < r && a[l] <= pivot) l++; a[r] = a[l]; } a[l] = pivot; return l; }
int qSelect(int* a, int k, int from, int to){ int x, i ,idx, ans; if(from > to) return -1; //something wrong swap(&a[median3(a, from, to)], &a[from]); // get the pivot idx = from; x = a[from]; for(i = from + 1; i <= to; i++) if(a[i] < x) swap(&a[++idx], &a[i]); swap(&a[idx], &a[from]); if(idx+1 < k){ ans = qSelect(a, k ,idx +1, to); }else if(idx + 1 > k){ ans = qSelect(a,k,from,idx -1); }else{ ans = a[idx]; } return ans; }
void partition(int a[],int left,int right) { int up,down,pivot,temp; if(left-right+1>3) { median3(a,left,right); up=left; down=right-1; pivot=a[(left+right+1)/2]; a[(left+right+1)/2]=a[right]; a[right]=pivot; while(1) { for(;a[up]<pivot;up++); for(;a[down]>pivot;down--); if(up<down) { temp=a[up]; a[up]=a[down]; a[down]=temp; } else { temp=a[right]; a[right]=a[up]; a[up]=temp; break; } } partition(a,left,up-1); partition(a,up+1,right); } else { insertion(a,left,right); } }
ColorVal predict_and_calcProps_scanlines_plane(Properties &properties, const ColorRanges *ranges, const Image &image, const plane_t &plane, const int p, const uint32_t r, const uint32_t c, ColorVal &min, ColorVal &max, const ColorVal fallback) { ColorVal guess; int which = 0; int index=0; if (p < 3) { for (int pp = 0; pp < p; pp++) { properties[index++] = image(pp,r,c); } if (image.numPlanes()>3) properties[index++] = image(3,r,c); } ColorVal left = (nobordercases || c>0 ? plane.get(r,c-1) : (r > 0 ? plane.get(r-1, c) : fallback)); ColorVal top = (nobordercases || r>0 ? plane.get(r-1,c) : left); ColorVal topleft = (nobordercases || (r>0 && c>0) ? plane.get(r-1,c-1) : (r > 0 ? top : left)); ColorVal gradientTL = left + top - topleft; guess = median3(gradientTL, left, top); ranges->snap(p,properties,min,max,guess); assert(min >= ranges->min(p)); assert(max <= ranges->max(p)); assert(guess >= min); assert(guess <= max); if (guess == gradientTL) which = 0; else if (guess == left) which = 1; else if (guess == top) which = 2; properties[index++] = guess; properties[index++] = which; if (nobordercases || (c > 0 && r > 0)) { properties[index++] = left - topleft; properties[index++] = topleft - top; } else { properties[index++] = 0; properties[index++] = 0; } if (nobordercases || (c+1 < image.cols() && r > 0)) properties[index++] = top - plane.get(r-1,c+1); // top - topright else properties[index++] = 0; if (nobordercases || r > 1) properties[index++] = plane.get(r-2,c)-top; // toptop - top else properties[index++] = 0; if (nobordercases || c > 1) properties[index++] = plane.get(r,c-2)-left; // leftleft - left else properties[index++] = 0; return guess; }
void quicksort(std::vector<T> &vec, int beg, int end, vector<int> &indices) { if(beg >= end) return; if(beg + 1 == end) { if(vec[indices[beg]] > vec[indices[end]]) std::swap(indices[beg], indices[end]); return; } auto &pivot = median3(vec, beg, end, indices); int i = beg, j = end; for(;;) { while(vec[indices[--j]] > pivot); while(vec[pivot] > vec[indices[++i]]); if(i < j) std::swap(indices[i], indices[j]); else break; } std::swap(pivot, indices[i]); quicksort(vec, beg, i-1, indices); quicksort(vec, i+1, end, indices); }
static int quick_sort_2(int *arr, int l, int r, int size) { if (l >= r) return -1; int pivot = median3(arr, l, r); //choosing the median of the first, middle and last element of the partition for the pivot int left = l; int right = r-1; //because the pivot swap to the end, so the right index need to decrease 1 while (/*left < right*/1) { while (less_than(arr[left], /*arr[r-1]*/pivot, &comp)) ++ left; while(less_than(pivot, arr[right], &comp)) -- right; if (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; ++ left; -- right; ++ swap_times; } else break; } swap_pivot(&arr[r-1], &arr[left]); for(int n = 0; n < size; ++n) cout << arr[n] << " "; cout<<endl; quick_sort_2(arr, l, left-1, size); quick_sort_2(arr, left+1, r, size); return 0; }
// Actual prediction. Also sets properties. Property vector should already have the right size before calling this. ColorVal predict_and_calcProps(Properties &properties, const ColorRanges *ranges, const Image &image, const int z, const int p, const uint32_t r, const uint32_t c, ColorVal &min, ColorVal &max) { ColorVal guess; int which = 0; int index = 0; if (p != 3) { for (int pp = 0; pp < p; pp++) { properties[index++] = image(pp,z,r,c); } if (image.numPlanes()>3) properties[index++] = image(3,z,r,c); } ColorVal left; ColorVal top; ColorVal topleft = (r>0 && c>0 ? image(p,z,r-1,c-1) : grey[p]); ColorVal topright = (r>0 && c+1 < image.cols(z) ? image(p,z,r-1,c+1) : grey[p]); ColorVal bottomleft = (r+1 < image.rows(z) && c>0 ? image(p,z,r+1,c-1) : grey[p]); if (z%2 == 0) { // filling horizontal lines left = (c>0 ? image(p,z,r,c-1) : grey[p]); top = image(p,z,r-1,c); ColorVal gradientTL = left + top - topleft; ColorVal bottom = (r+1 < image.rows(z) ? image(p,z,r+1,c) : top); //grey[p]); ColorVal gradientBL = left + bottom - bottomleft; ColorVal avg = (top + bottom)/2; guess = median3(gradientTL, gradientBL, avg); ranges->snap(p,properties,min,max,guess); if (guess == avg) which = 0; else if (guess == gradientTL) which = 1; else if (guess == gradientBL) which = 2; properties[index++] = top-bottom; } else { // filling vertical lines left = image(p,z,r,c-1); top = (r>0 ? image(p,z,r-1,c) : grey[p]); ColorVal gradientTL = left + top - topleft; ColorVal right = (c+1 < image.cols(z) ? image(p,z,r,c+1) : left); //grey[p]); ColorVal gradientTR = right + top - topright; ColorVal avg = (left + right )/2; guess = median3(gradientTL, gradientTR, avg); ranges->snap(p,properties,min,max,guess); if (guess == avg) which = 0; else if (guess == gradientTL) which = 1; else if (guess == gradientTR) which = 2; properties[index++] = left-right; } properties[index++]=guess; properties[index++]=which; if (c > 0 && r > 0) { properties[index++]=left - topleft; properties[index++]=topleft - top; } else { properties[index++]=0; properties[index++]=0; } if (c+1 < image.cols(z) && r > 0) properties[index++]=top - topright; else properties[index++]=0; if (p == 0 || p == 3) { if (r > 1) properties[index++]=image(p,z,r-2,c)-top; // toptop - top else properties[index++]=0; if (c > 1) properties[index++]=image(p,z,r,c-2)-left; // leftleft - left else properties[index++]=0; } return guess; }
int main(void) { try { glPixelStorei(GL_UNPACK_ALIGNMENT,1); glPixelStorei(GL_PACK_ALIGNMENT,1); GLWindow::Settings setts; setts.width = screenWidth; setts.height = screenHeight; setts.match_resolution_exactly=true; setts.is_fullscreen=false; setts.glversion_major= 2; setts.glversion_minor= 1.0; GLWindow win(setts); glewInit(); LDRImage noiseImg = loadImage("noise.png",1); flipVertical(noiseImg); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glPixelStorei(GL_PACK_ALIGNMENT,1); GLTexture inputTex(noiseImg); int width = noiseImg.getDimensions()[1]; int height = noiseImg.getDimensions()[2]; std::cout<<"W H "<<width<<" "<<height<<std::endl; glBindTexture(GL_TEXTURE_2D, inputTex.tex); writeImage(noiseImg, "noiseOut_test.png"); GL::GLShader median2("median.vert", "medianfilter2.frag"); median2.bind(); median2.setTexture("tex", 0); median2.setUniform("invTexDimensions", 1.0f / float(width), 1.0f / float(height)); GL::GLShader median3("median.vert", "medianfilter3.frag"); median3.bind(); median3.setTexture("tex", 0); median3.setUniform("invTexDimensions", 1.0f / float(width), 1.0f / float(height)); GL::GLShader median4("median.vert", "medianfilter4.frag"); median4.bind(); median4.setTexture("tex", 0); median4.setUniform("invTexDimensions", 1.0f / float(width), 1.0f / float(height)); GL::GLShader median5("median.vert", "medianfilter5.frag"); median5.bind(); median5.setTexture("tex", 0); median5.setUniform("invTexDimensions", 1.0f / float(width), 1.0f / float(height)); median4.bind(); std::cout<<"BEGIN "<<std::endl; { while(win.isValid()) { /* #ifndef GL_ES_BUILD const GLenum buffers[]= {GL_COLOR_ATTACHMENT0, GL_BACK_LEFT}; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffers(1, &buffers[1]); #else*/ glBindFramebuffer(GL_FRAMEBUFFER, 0); //#endif glClearColor(1,0,0,1); glClear(GL_COLOR_BUFFER_BIT); screenQuad(); // checkFBOErrors(); win.update(); win.flush(); } }//scope } catch(const std::exception& e) { std::cout<<e.what()<<std::endl; } catch(const std::string& str) { std::cout<<str<<std::endl; } return 0; }