📄 texlibnt.c
字号:
/*************************************************************************** * texlib.c * written by: Stephanie Wojtkowski * * Description: The implementation of texlib.h: a library containing * functions that perform the Wei-Levoy texture synthesis method. ***************************************************************************/#include "texlibnt.h"//Subroutinesint mod(int a, int b);int CompareNeighborhood(Pixel *input, int r, int c, int j, Pixel *output, int h, int w, int i);/*************************************************************************** * GenerateTexture(input, r, c, output, h, w) * Inputs: input - original texture image * r - number of rows in input * c - number of columns in input * output - resulting texture image after Wei-Levoy * h - number of rows in output * w - number of columns in output * * Description: Given an image and the dimensions of the result, generates * a texture patch similar to the original of the given dimensions. ***************************************************************************/void GenerateTexture(Pixel *input, int r, int c, Pixel *output, int h, int w){ int i, j, k; int isize, osize; //input and output sizes int diff; //Difference between two neghborhoods int mindiff; //The current minimum difference btwn neighborhoods Pixel p; //Current replacement pixel value isize = r*c; osize = h*w; //Perform the technique on the lower half of the image first to "seed" //the texture computations for the image for(i=osize/2; i<osize; i++){ //Initialize neighborhood distance mindiff = HUGE_VAL; //For each candidate position in the input image //(use only pixels whose neighborhoods are entirely contained // in the texture sample) for(j=NRAD; j<r-NRAD; j++){ for(k=NRAD; k<c-NRAD; k++){ //Compare the two neighborhoods diff = CompareNeighborhood(input, r, c, j*c+k, output, h, w, i); //If they are more similar than the best match if (diff < mindiff){ mindiff = diff; //Replace the best match with the current match p = input[j*c+k]; } } } //Replace the output pixel with the best match output[i] = p; } //For each position in the output image for(i=0; i<osize; i++){ //Initialize neighborhood distance mindiff = HUGE_VAL; //For each candidate position in the input image //(use only pixels whose neighborhoods are entirely contained // in the texture sample) for(j=NRAD; j<r-NRAD; j++){ for(k=NRAD; k<c-NRAD; k++){ //Compare the two neighborhoods diff = CompareNeighborhood(input, r, c, j*c+k, output, h, w, i); //If they are more similar than the best match if (diff < mindiff){ mindiff = diff; //Replace the best match with the current match p = input[j*c+k]; } } } //Replace the output pixel with the best match output[i] = p; }}/*************************************************************************** * int CompareNeighborhood(input, r, c, j, output, h, w, i) * * Inputs: input - original texture image * r - number of rows in input * c - number of columns in input * j - a position in the input image * output - resulting texture image after Wei-Levoy * h - number of rows in output * w - number of columns in output * i - a position in the output image * * Output: diff - the difference between the neighborhoods of the given * pixels in the input and output images ***************************************************************************/int CompareNeighborhood(Pixel *input, int r, int c, int j, Pixel *output, int h, int w, int i){ int m, n; int diff=0; int val; int ix, iy, ox, oy; int index, outdex; for(n=-NRAD; n<=0; n++) for(m=-NRAD; m<=NRAD; m++){ //Computes the L2 distance between neighborhoods //Other distance metrics can be substituted here ox = mod(mod(i, h) + m, w); oy = mod((((i-mod(i, w)) / w) + n), h); if((oy*w + ox)==i) goto DONE; ix = mod(mod(j, r) + m, c); iy = mod((((j-mod(j, c)) / c) + n), r); index = iy * c + ix; outdex = oy * w + ox; val = abs(output[outdex].r-input[index].r) + abs(output[outdex].g-input[index].g) + abs(output[outdex].b-input[index].b); diff += val*val; } DONE: return diff;}int mod(int a, int b){ if(a < 0) while(a < 0) a += b; else if(a >= b) a = a % b; return a;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -